June 29, 2012
smallcheck is a testing library to verify properties for test cases to a certain depth. The depth for data values refers to the depth of nested constructors, while for functional values, it refers to the depth of nested case analysis and results. The test cases will be generated by smallcheck. It is now available on Fedora. Install it using:
$ sudo yum install ghc-smallcheck-devel
The following BitAdd.hs example demonstrates the use of smallcheck:
import Test.SmallCheck
and2 (a,b) = a && b
xor2 (a,b) = a /= b
halfAdd (a,b) = (sum,carry)
where sum = xor2 (a,b)
carry = and2 (a,b)
bit False = 0
bit True = 1
num [] = 0
num (a:as) = bit a + 2 * num as
bitAdd a [] = [a]
bitAdd a (b:bs) = s : bitAdd c bs
where (s,c) = halfAdd (a,b)
prop_bitAdd a as = num (bitAdd a as) == bit a + num as
main = smallCheck 3 prop_bitAdd
We ask smallcheck to run tests to a depth of 3. You can compile and run it using:
~~~~ {.shell} $ ghc –make BitAdd.hs [1 of 1] Compiling Main ( BitAdd.hs, BitAdd.o ) Linking BitAdd …
$ ./BitAdd Depth 0: Completed 2 test(s) without failure. Depth 1: Completed 6 test(s) without failure. Depth 2: Completed 14 test(s) without failure. Depth 3: Completed 30 test(s) without failure. ~~~~