Skip to content

Instantly share code, notes, and snippets.

@TheSeamau5
Last active August 29, 2015 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TheSeamau5/f662caa5e704e8c6e9ea to your computer and use it in GitHub Desktop.
Save TheSeamau5/f662caa5e704e8c6e9ea to your computer and use it in GitHub Desktop.
Quickcheck in Elm
quickCheck : Generator a -> Int -> (a -> Bool) -> String
quickCheck randomGenerator numberOfCases testingCondition =
let listGenerator = list numberOfCases <| randomGenerator
testInputs = fst <| generate listGenerator (initialSeed 1)
getOutput input = (input, testingCondition input)
testOutputs = map getOutput testInputs
failingOutputs = filter (\x -> (snd x) == False) testOutputs
successString = "Ok, passed " ++ (toString numberOfCases) ++ " tests."
failingString fail = "The following input has failed the test: " ++ (toString fail)
in
if failingOutputs == []
then successString
else failingString <| fst <| head failingOutputs
{-
Examples :
> quickCheck (float 0 1) 100 (\x -> (x + 1) == x)
"The following input has failed the test: 0.35925459858478387"
> quickCheck (float 0 1) 1000 (\x -> x == x)
"Ok, passed 1000 tests."
-}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment