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 | |
forAll :: (Show a, Testable testable) => Gen a -> (a -> testable) -> Property | |
forAll = undefined |
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
class Testable a where | |
property :: a -> Property |
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, -- 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 |
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 -> (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 } |
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 :: 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 |
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
#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); |
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
overFailure :: Result -> (Result -> Result) -> Result | |
overFailure Success _ = Success | |
overFailure failure f = f failure |
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
runProp :: Property -> StdGen -> Result | |
runProp prop rand = runGen (getGen prop) rand |
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
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 } |
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)) |