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
相关问题
- Why it isn't advised to call the release() met
- Get access to Angular service instance from JavaSc
- Port of the Rails app when running Cucumber tests
- Selenium View Mouse/Pointer
- What is a correct approach to manage test data usi
相关文章
- Web Test recorder does not allow me to record a te
- Factory_girl has_one relation with validates_prese
- What is the difference between `assert_frame_equal
- How do I send cookies with request when testing Fl
- Unit test Angular 2 service subject
- Unit/Integration testing FTP access
- How to set a test for multiple fetches with Promis
- How Do I Seed My Database in the setupBeforeClass
You can run the test a few times, e.g. 10, and test that each time the order was correct. This will ensure that it happened not by chance.
P.S. Multiple threads in a unit test is usually avoided
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) 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:
But it's not enough as you could have an implementation that returns
A=1
andB=C
. AddingA,B != 1
can still lead toA=-1
andB=-C
, so the only correct specification must stateA,B>1
. That's just to illustrate how complicated it can be to write a specification that match the real intention.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:
3) Testing can only reveal the presence of bug, not their absence. This quote goes back to Dijkstra:
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.