Skip to content

Instantly share code, notes, and snippets.

@sarnesjo
Created July 1, 2012 05:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sarnesjo/3026990 to your computer and use it in GitHub Desktop.
Save sarnesjo/3026990 to your computer and use it in GitHub Desktop.
Haskell testing frameworks: hspec and test-framework
$ runhaskell VectorSpec.hs
add
- adds vectors component-wise
- is commutative
- is associative FAILED [1]
1) add is associative FAILED
*** Failed! Falsifiable (after 2 tests):
Vector3 1.1001901680139465 (-0.9257152410333577) 0.6268870116292593
Vector3 5.660283886683175 (-0.3875235884525388) (-0.19651550534280757)
Vector3 (-15.612462084799699) (-0.9968561942657519) (-0.17202233352139276)
Finished in 0.0574 seconds, used 0.0579 seconds of CPU time
3 examples, 1 failure
$ runhaskell VectorTest.hs
add:
adds vectors component-wise: [OK, passed 100 tests]
is commutative: [OK, passed 100 tests]
is associative: [Failed]
Falsifiable with seed 8765271772400398670, after 2 tests. Reason: Vector3 0.6194019472565273 2.2286012762686647 1.4842337771953162
Vector3 (-0.12050427314286641) 6.728314210587664 0.5253529132523509
Vector3 (-1.2137609419705317) (-1.2456931332848524) 0.16754306174937786
Properties Total
Passed 2 2
Failed 1 1
Total 3 3
module Vector3 where
data Vector3 = Vector3 Double Double Double deriving (Eq, Show)
add (Vector3 x1 y1 z1) (Vector3 x2 y2 z2) = Vector3 (x1+x2) (y1+y2) (z1+z2)
import Control.Monad
import Test.Hspec.Monadic
import Test.Hspec.QuickCheck
import Test.QuickCheck.Arbitrary
import Vector3
instance Arbitrary Vector3 where
arbitrary = liftM3 Vector3 arbitrary arbitrary arbitrary
main = hspec $ do
describe "add" $ do
prop "adds vectors component-wise" $
\x1 y1 z1 x2 y2 z2 -> (Vector3 x1 y1 z1) `add` (Vector3 x2 y2 z2) == Vector3 (x1+x2) (y1+y2) (z1+z2)
prop "is commutative" $
\v1 v2 -> v1 `add` v2 == v2 `add` v1
prop "is associative" $
\v1 v2 v3 -> (v1 `add` v2) `add` v3 == v1 `add` (v2 `add` v3)
import Control.Monad
import Test.Framework
import Test.Framework.Providers.QuickCheck2
import Test.QuickCheck.Arbitrary
import Vector3
instance Arbitrary Vector3 where
arbitrary = liftM3 Vector3 arbitrary arbitrary arbitrary
main = defaultMain [
testGroup "add" [
testProperty "adds vectors component-wise" $
\x1 y1 z1 x2 y2 z2 -> (Vector3 x1 y1 z1) `add` (Vector3 x2 y2 z2) == Vector3 (x1+x2) (y1+y2) (z1+z2),
testProperty "is commutative" $
\v1 v2 -> v1 `add` v2 == v2 `add` v1,
testProperty "is associative" $
\v1 v2 v3 -> (v1 `add` v2) `add` v3 == v1 `add` (v2 `add` v3)]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment