User Tools

Site Tools


blog:2009:03:30:quickcheck_like_framework_for_cpp

QuickCheck-like Framework for C++

I just published a preliminary version of QuickCheck++, an attempt at bringing some of the benefits of Haskell's QuickCheck to C++.

As already said, I quite like the QuickCheck concept of specifying properties rather than a few ad-hoc test cases.

In the past, I sometimes missed QuickCheck when I was coding in some language other than Haskell. It won't be the case anymore for C++ as I just published QuickCheck++, a QuickCheck-like framework for C++.

It is a mix between imperative unit testing approaches à la JUnit, and an approach à la QuickCheck, using property specifications and randomly-generated test cases.

Yet another stupid framework or useful stuff? I have not used it seriously yet so I couldn't say. I'll keep you informed. In the meantime, why not try it for yourself and let me know what you think?

Discussion

Akira Takahashi, 2010/03/09 12:44

Hi, thanks for great library.

It checked by VC++2005, and since there was a bug by conversion of unsigned/signed, it corrected.

http://cid-8361f69420bd2fb3.skydrive.live.com/self.aspx/public/C++/QuickCheck++/generate.hh

Cyril Soldani, 2010/03/25 17:30

Thanks.

I checked your code, but could not include it for two reasons:

  • I didn't see the signedness problem with the original code ;
  • Your code is machine and/or compiler dependent (signedness of different types may depend on the compiler or machine, e.g. a char may be both signed or unsigned depending on the compiler).

However, while checking signedness problems (with g++ -Wsign-compare -Wsign-conversion, I found (and corrected) another bug in oneOf(). Two more warnings to add to my list!

Eric, 2010/08/24 03:42

Hello,

Have you every tried to extract theorems from the types of a C++ function or method and see if the actual method holds. Read the paper Theorems for Free!. I think that it would be cool to see if you could get some methods that are not purely functional to hold fro some of these theorems. What I am getting at is the function signature can be used to extract properties that can then be used to check it non-functional C++ counter part – and all for Free!

Cyril Soldani, 2010/08/26 16:24

I am afraid this is not possible for C++ as it is in Haskell. As I understand it, the Theorems for free approach heavily depends on parametricity. However, parametricity in Haskell offers much more stronger guarantees than templating in C++. Let me go through an example.

Given the Haskell function signatures

r :: [a] -> [a]
f :: a -> b

I can deduce the theorem

map f . r = r . map f

r takes a list of fully parametric arguments. These are not equipped with any operation (the a type is not qualified with any type class). r must then manipulate the list elements polymorphically (e.g. r could be id, rev, … but not sort which would require Ord a).

On the contrary, C++ templates do not guarantee that. Put boldly, one could say that C++ templates are just advanced macros. If I know that r has signature:

template<class A> list<A> *r(const list<A> &xs);

I do not know whether or not r inspects its elements. For example, r could return a sorted version of its input list. There is no equivalent of the Ord a qualification in C++. Calling a r that compares its elements on a list of uncomparable elements is not a type error. It will trigger an instantiation error (the compiler will not know how to compile the comparisons), but this is harder to use as it requires to inspect the function body and not just its signature. I think that C++ types are just not rich enough to allow free theorems.

Enter your comment. Wiki syntax is allowed:
 
blog/2009/03/30/quickcheck_like_framework_for_cpp.txt · Last modified: 2011/02/20 20:28 (external edit)