Skip to content

Instantly share code, notes, and snippets.

@iporsut
Created June 11, 2018 00:24
Show Gist options
  • Save iporsut/e768af3270ff2e219d593b916cdbcf2b to your computer and use it in GitHub Desktop.
Save iporsut/e768af3270ff2e219d593b916cdbcf2b to your computer and use it in GitHub Desktop.
QuickCheck FizzBuzz
{-# 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