QuickCheck for C++

2013 January 15

I wrote a QuickCheck clone for C++11 that I call AutoCheck. QuickCheck is the famous Haskell unit testing library.

Unit tests are expressed as properties: functions that perform some computation and return the Boolean value of a post-condition. The arguments of the function form a test case. A canonical example of a property tests that reversing a list twice preserves the original order of the list. In C++, it may be expressed like this:

template <typename Container>
bool reverse_prop(const Container& xs) {
Container ys(xs);
std::reverse(ys.begin(), ys.end());
std::reverse(ys.begin(), ys.end());
return ys == xs;
}

The purpose of QuickCheck is to test such properties with a set of randomly-generated test cases. After the property passes a certain number of tests, it succeeds, and the programmer can, with some level of confidence, presume it works in the general case. If it fails a test, the counterexample is printed to allow the programmer to investigate and debug. Test cases have a notion of growth: they start "small" and grow larger with the hope that the smallest counterexample will be found should the property fail.

Besides the original authors of QuickCheck, I want to thank two related projects for inspiration:

I think I was able to make numerous improvements with C++11 features (lambdas, variadic templates, rvalue references), a more functional style, and an extensible architecture, but these two projects deserve credit.

More documentation and a tutorial can be found in the project wiki. Please take a look and email me if you have any questions or bugs.