FIFO信号量测试 [英] FIFO semaphore test

查看:107
本文介绍了FIFO信号量测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实现了FIFO信号量,但是现在我需要一种方法来测试/证明它们工作正常.一个简单的测试是创建一些线程,这些线程尝试等待一个信号量,然后打印一条带有数字的消息,如果这些数字是按顺序排列的,则应该使用FIFO,但这不足以证明这一点,因为该顺序可能具有偶然发生.因此,我需要一种更好的测试方法.
如有必要,也可以使用锁或条件变量.
谢谢

I have implemented FIFO semaphores but now I need a way to test/prove that they are working properly. A simple test would be to create some threads that try to wait on a semaphore and then print a message with a number and if the numbers are in order it should be FIFO, but this is not good enough to prove it because that order could have occurred by chance. Thus, I need a better way of testing it.
If necessary locks or condition variables can be used too.
Thanks

推荐答案

用句子"描述的内容,但这不足以证明它是正确的,因为该命令可能是偶然发生的"是不知何故是众所周知的难题.

What you describe with your sentence "but this is not good enough to prove it because that order could have occurred by chance" is somehow a known dilema.

1)即使您具有规范,也不能确保该规范符合您的意图.为了说明这一点,我将以正确性限制" 为例.让我们考虑一下分解函数的规范:

1) Even if you have a specification, you can not ensure that the specification match your intention. To illustrate this I will take an example from "the limit of correctness". Let's consider a specification for a factorization function that is:

计算A和B,例如A * B = C

Compute A and B such as A * B = C

但是这还不够,因为您可以实现一个返回A=1B=C的实现.添加A,B != 1仍会导致A=-1B=-C,因此唯一正确的规范必须注明A,B>1.这只是为了说明编写符合真实意图的规范可能有多么复杂.

But it's not enough as you could have an implementation that returns A=1 and B=C. Adding A,B != 1 can still lead to A=-1 and B=-C, so the only correct specification must state A,B>1. That's just to illustrate how complicated it can be to write a specification that match the real intention.

2)即使已经证明算法,也并不意味着实现在实践中是正确的.最好用Donald Knuth的此报价来说明:

2) Even having proved an algorithm, still doesn't mean the implementation is correct in practice. This is best illustrated with this quote from Donald Knuth:

当心上面代码中的错误;一世 只证明了它是正确的,没有尝试过 它.

Beware of bugs in the above code; I have only proved it correct, not tried it.

3)测试只能显示错误的存在,不能显示错误的存在.该引用可以追溯到 Dijkstra :

3) Testing can only reveal the presence of bug, not their absence. This quote goes back to Dijkstra:

测试可以用来显示 存在错误,但从不显示 他们的缺席.

Testing can be used to show the presence of bugs but never to show their absence.

结论:您注定要失败,并且您将永远无法100%地根据自己的意图确定您的代码正确无误!但是东西还不错.对代码具有高度的信心通常就足够了.例如,如果使用多个线程仍然不够用,您可以决定同时使用模糊测试,以使测试执行更加随机.如果您的测试始终通过,那么您可以确信自己的代码是好的.

Conclusion: you are doomed and you will never be 100% sure that your code is correct according to its intent! But stuff aren't that bad. Having a high confidence about the code is usually enough. For instance, if using multiple threads is still not enough for you, you can decide to use fuzzing as well so as to randomize the test execution even more. If your tests always pass, well, you can be pretty confident that your code is good.

这篇关于FIFO信号量测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆