Skip to content

Instantly share code, notes, and snippets.

instance
(Show a, Arbitrary a,
Testable testable)
=> Testable (a -> testable)
where
property f = forAll arbitrary f
forAll :: (Show a, Testable testable) => Gen a -> (a -> testable) -> Property
forAll = undefined
class Testable a where
property :: a -> Property
instance
(Show a, Arbitrary a, -- Given a type `a` supporting random generation
Testable testable) -- And an already existing testable function
=> Testable (a -> testable) -- The function (a -> testable) is also testable
where
property = undefined -- With the provided implementation
forAll :: (Show a, Testable testable) => Gen a -> (a -> testable) -> Property
forAll argGen 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
subProp = property (prop arg) -- Use the `a` to access the sub-property
result = runProp subProp rand2 -- Use the second generator to run it
in overFailure result $ \failure -> -- Enrich the counter-example with `arg`
failure { counterExample = show arg : counterExample failure }
rapidCheck :: Testable prop => prop -> IO Result
rapidCheck = rapidCheckWith 100
rapidCheckWith :: Testable prop => Int -> prop -> IO Result
rapidCheckWith attemptNb prop = do
seed <- randomIO
return $ rapidCheckImpl attemptNb seed prop
#include <unordered_map>
#include <vector>
//------------------------------------------------------------------------
long long bst_count_classic(int n)
{
if (n <= 1) return 1;
std::vector<long long> memo(n+1, 0);
overFailure :: Result -> (Result -> Result) -> Result
overFailure Success _ = Success
overFailure failure f = f failure
runProp :: Property -> StdGen -> Result
runProp prop rand = runGen (getGen prop) rand
rapidCheckImpl :: Testable prop => Int -> Int -> prop -> Result
rapidCheckImpl attemptNb startSeed prop = runAll (property prop)
where
runAll prop = foldMap (runOne prop) [startSeed .. startSeed + attemptNb - 1]
runOne prop seed =
let result = runProp prop (mkStdGen seed)
in overFailure result $ \failure -> failure { seed = seed }
instance Arbitrary Integer where
arbitrary = Gen $ \rand ->
fromIntegral (fst (next rand))