| ▲ | sunshowers a day ago | |||||||||||||||||||||||||||||||
The simplest practical property-based tests are where you serialize some randomly generated data of a particular shape to JSON, then deserialize it, and ensure that the output is the same. A more complex kind of PBT is if you have two implementations of an algorithm or data structure, one that's fast but tricky and the other one slow but easy to verify. (Say, quick sort vs bubble sort.) Generate data or operations randomly and ensure the results are the same. | ||||||||||||||||||||||||||||||||
| ▲ | eru a day ago | parent [-] | |||||||||||||||||||||||||||||||
> The simplest practical property-based tests are where you serialize some randomly generated data of a particular shape to JSON, then deserialize it, and ensure that the output is the same. Testing that f(g(x)) == x for all x and some f and g that are supposed to be inverses of each other is a good test, but it's probably not the simplest. The absolute simplest I can think of is just running your functionality on some randomly generated input and seeing that it doesn't crash unexpectedly. For things like sorting, testing against an oracle is great. But even when you don't have an oracle, there's lots of other possibilities: * Test that sorting twice has the same effect as sorting once. * Start with a known already in-order input like [1, 2, 3, ..., n]; shuffle it, and then check that your sorting algorithm re-creates the original. * Check that the output of your sorting algorithm is in-order. * Check that input and output of your sorting algorithm have the same elements in the same multiplicity. (If you don't already have a datastructure / algorithm that does this efficiently, you can probe it with more randomness: create a random input (say a list of numbers), pick a random number X, count how many times X appears in your list (via a linear scan); then check that you get the same count after sorting. * Check that permuting your input doesn't make a difference. * Etc. | ||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||