Created
June 11, 2018 00:24
-
-
Save iporsut/e768af3270ff2e219d593b916cdbcf2b to your computer and use it in GitHub Desktop.
QuickCheck FizzBuzz
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 TemplateHaskell #-} | |
import Test.QuickCheck | |
prop_fizz = forAll inputs (\n -> fizz n == "Fizz") | |
where | |
inputs = must [ divisibleBy 3 | |
, not . divisibleBy 5 | |
] | |
prop_buzz = forAll inputs (\n -> fizz n == "Buzz") | |
where | |
inputs = must [ divisibleBy 5 | |
, not . divisibleBy 3 | |
] | |
prop_fizzbuzz = forAll inputs (\n -> fizz n == "FizzBuzz") | |
where | |
inputs = must [ divisibleBy 3 | |
, divisibleBy 5 | |
] | |
prop_otherwise = forAll inputs (\n -> fizz n == show n) | |
where | |
inputs = must [ not . divisibleBy 3 | |
, not . divisibleBy 5 | |
] | |
must conds = | |
suchThat arbitrary cond | |
where | |
cond n = and (map ($ n) $ (>1):conds) | |
fizz :: Integer -> String | |
fizz i | i `rem` 15 == 0 = "FizzBuzz" | |
| i `rem` 3 == 0 = "Fizz" | |
| i `rem` 5 == 0 = "Buzz" | |
| otherwise = show i | |
divisibleBy x n = n `rem` x == 0 | |
-- Use quickCheckAll TemplateHaskell generate runTests function . | |
$(return []) | |
runTests = $quickCheckAll |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment