In the previous post we attempted to define some properties for a run-length encoding (RLE) implementation, but got stuck because the random values be

Generating interesting inputs for property-based testing

submited by
Style Pass
2021-06-22 06:30:04

In the previous post we attempted to define some properties for a run-length encoding (RLE) implementation, but got stuck because the random values being generated by FsCheck were not very useful.

In this post we’ll look at a couple of ways of generating “interesting” inputs, and how to observe them so that we can be sure that they are indeed interesting.

So what is an “interesting” input? For this scenario, it’s a string that has some runs in it. Which means that a string consisting of random characters like this…

Without trying to reimplement the RLE logic, one way to determine whether there are runs is to see if the number of distinct characters is much less than the length of the string. If this is true, then by the pigeonhole principle there must be duplicates of some character. This doesn’t ensure that there are runs, but if we make the difference large enough, most of the “interesting” inputs will have runs.

To test all this, let’s create a dummy property propIsInterestingString which we can use to monitor the input generated by FsCheck. The actual property test itself should always succeed, so we’ll just use true. Here’s the code:

Leave a Comment