Last active
August 29, 2015 13:56
-
-
Save stuartgunter/9130674 to your computer and use it in GitHub Desktop.
Intro to QuickCheck
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data Age = Age Int | |
deriving (Show) | |
instance Arbitrary Age where | |
arbitrary = Age `liftM` choose (0, 100) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fibs = 0 : 1 : zipWith (+) fibs (tail fibs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Name = String | |
data Person = Person Name Age | |
deriving (Show) | |
instance Arbitrary Person where | |
arbitrary = liftM2 Person arbitrary arbitrary |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
> 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
prop_Idempotent xs = | |
sort (sort xs) == sort xs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
> quickCheck prop_Idempotent | |
+++ OK, passed 100 tests. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
prop_Idempotent xs = | |
collect (length xs) $ | |
sort (sort xs) == sort xs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
> quickCheck prop_Idempotent | |
+++ OK, passed 100 tests. | |
9% 14 | |
7% 0 | |
5% 11 | |
4% 4 | |
4% 21 | |
4% 10 | |
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
prop_Idempotent xs = | |
classify (length xs < 2) "trivial" $ | |
sort (sort xs) == sort xs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
> quickCheck prop_Idempotent | |
+++ OK, passed 100 tests (8% trivial). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
prop_Square x = | |
x^2 >= x | |
prop_CubeOfPositive (NonNegative x) = | |
x^3 >= x |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
> 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