Skip to content

Instantly share code, notes, and snippets.

@stuartgunter
Last active August 29, 2015 13:56
Show Gist options
  • Save stuartgunter/9130674 to your computer and use it in GitHub Desktop.
Save stuartgunter/9130674 to your computer and use it in GitHub Desktop.
Intro to QuickCheck
data Age = Age Int
deriving (Show)
instance Arbitrary Age where
arbitrary = Age `liftM` choose (0, 100)
fibs = 0 : 1 : zipWith (+) fibs (tail fibs)
type Name = String
data Person = Person Name Age
deriving (Show)
instance Arbitrary Person where
arbitrary = liftM2 Person arbitrary arbitrary
> sample (arbitrary :: Gen Person)
Person "" (Age 93)
Person "_J" (Age 73)
Person "\vw\ENQ" (Age 84)
Person "\148Sm\197" (Age 45)
Person "S\SYN\246" (Age 75)
Person "rI=\246\&6*Fv" (Age 87)
Person "\b:\FSB\227\DC1\187!\SIJs" (Age 77)
Person "" (Age 68)
Person "\200KC\f\f\222D-s\148P,\140" (Age 29)
Person "$)\171.\n" (Age 91)
Person "\teP\DEL5A\251\252\DC4 V" (Age 37)
smallNonNegativeIntegers = choose (0, 500)
prop_Fibonacci =
forAll smallNonNegativeIntegers $ \n ->
let x = fibs !! (n)
y = fibs !! (n+1)
z = fibs !! (n+2)
in x + y == z
prop_Idempotent xs =
sort (sort xs) == sort xs
> quickCheck prop_Idempotent
+++ OK, passed 100 tests.
prop_Idempotent xs =
collect (length xs) $
sort (sort xs) == sort xs
> quickCheck prop_Idempotent
+++ OK, passed 100 tests.
9% 14
7% 0
5% 11
4% 4
4% 21
4% 10
...
prop_Idempotent xs =
classify (length xs < 2) "trivial" $
sort (sort xs) == sort xs
> quickCheck prop_Idempotent
+++ OK, passed 100 tests (8% trivial).
prop_Square x =
x^2 >= x
prop_CubeOfPositive (NonNegative x) =
x^3 >= x
> shrink [1,2,3]
[[],[2,3],[1,3],[1,2],[0,2,3],[1,0,3],[1,1,3],[1,2,0],[1,2,2]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment