This file contains hidden or 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
instance | |
(Show a, Arbitrary a, | |
Testable testable) | |
=> Testable (a -> testable) | |
where | |
property f = forAll arbitrary f |
This file contains hidden or 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
{-# LANGUAGE GeneralizedNewtypeDeriving #-} | |
{-# LANGUAGE FlexibleInstances #-} | |
module RapidCheck where | |
import Control.Monad | |
import Data.List | |
import System.Random | |
import Text.Show.Functions | |
This file contains hidden or 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
gcd 1 1 | |
> 1 | |
gcd 0 0 | |
> 0 |
This file contains hidden or 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
rapidCheck prop_gcd_bad | |
> Failure {seed = 1034882204061803680, | |
counterExample = ["1","0"]} |
This file contains hidden or 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 Shrinker a = a -> [a] | |
class Arbitrary a where | |
arbitrary :: Gen a | |
shrink :: Shrinker a | |
shrink = const [] |
This file contains hidden or 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
forAll :: (Show a, Testable testable) | |
=> Gen a -> Shrink a -> (a -> testable) -> Property | |
forAll argGen shrink prop = | |
Property $ Gen $ \rand -> -- Create a new property that will | |
let (rand1, rand2) = split rand -- Split the generator in two | |
arg = runGen argGen rand1 -- Use the first generator to produce an arg | |
runSub = evalSubProp prop rand2 -- Factorize a runner for the sub-property | |
result = runSub arg -- Run the sub-property with value `arg` | |
in overFailure result $ \failure -> -- In case of failure, | |
shrinking shrink arg runSub -- Attempt to shrink the counter example |
This file contains hidden or 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
instance | |
(Show a, Arbitrary a, Testable testable) | |
=> Testable (a -> testable) | |
where | |
property = forAll arbitrary shrink |
This file contains hidden or 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
shrinking :: (Show a) => Shrink a -> a -> (a -> Result) -> Result | |
shrinking shrink arg runSub = | |
let children = shrink arg -- Get the children of the current branch | |
result = findFailing children runSub -- Look for the first failure | |
in case result of | |
Nothing -> Success | |
Just (shrunk, failure) -> -- In case a failure is found | |
shrinking shrink shrunk runSub -- Try to shrink further the child | |
<> -- OR (in case it fails) | |
addToCounterExample shrunk failure -- Add child to the counter example |
This file contains hidden or 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
instance Arbitrary Integer where | |
arbitrary = Gen $ \rand -> fromIntegral $ fst (next rand) | |
shrink n | |
| n == 0 = [] | |
| otherwise = [abs n | n < 0] ++ 0 : rightDichotomy where | |
rightDichotomy = | |
takeWhile | |
(\m -> abs m < abs n) | |
[ n - i | i <- tail (iterate (`quot` 2) n)] |
This file contains hidden or 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 2048 | |
> [0,1024,1536,1792,1920,1984,2016,2032,2040,2044,2046,2047] | |
shrink (-2048) | |
> [2048,0,-1024,-1536,-1792,-1920,-1984,-2016,-2032,-2040,-2044,-2046,-2047] |