Strange Loops

No Matter Where You Go, There You Are

Haskell QuickCheck

| Comments

Had some problems with the ghc I built from darwinports. Initially when I tried it with the QuickCheck module, it failed with a missing symbol (from a shared object I think). Downloaded the prebuilt binary package for my mac off the haskell website. QuickCheck is pretty cool and I think it would be a nice tool to have in the testing toolkit of most mainstream languages.

From the example below, floating point addition isn’t associative and QuickCheck figured that out after 2 tests. Haskell language features seem well suited to this sort of specification type testing, strongly statically typed and purely functional. Not a substitute for unit testing but a very handy tool. I think of it as assertions on steroids.

– GHCI session
*Main> :load “Test3.hs”
Compiling Main ( Test3.hs, interpreted )
Ok, modules loaded: Main.
*Main> quickCheck prop_Foo
OK, passed 100 tests.
*Main> quickCheck prop_FloatAddition
OK, passed 100 tests.
*Main> quickCheck prop_IntAddition
OK, passed 100 tests.
*Main> quickCheck prop_FloatAssociativeAddition
Falsifiable, after 2 tests:
3.5
-3.3333333
-1.25

– Testing out QuickCheck on GHC6.4
module Main where

import Test.QuickCheck

prop_IntAddition :: Integer -> Integer -> Bool
prop_IntAddition x y = x + y == y + x

prop_FloatAddition :: Float -> Float -> Bool
prop_FloatAddition x y = x + y == y + x

prop_FloatAssociativeAddition :: Float -> Float -> Float -> Bool
prop_FloatAssociativeAddition x y z = (x + y) + z == x + (y + z)

prop_Foo :: Bool
prop_Foo = foo == 1

foo :: Integer
foo = 1