Created
February 13, 2013 03:03
-
-
Save gigasquid/4941942 to your computer and use it in GitHub Desktop.
Fizzbuzz with HUnit and Quick Check - in progress from Cincy FP
This file contains 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
-- Example.hs -- Examples from HUnit user's guide | |
-- | |
-- For more examples, check out the tests directory. It contains unit tests | |
-- for HUnit. | |
module Main where | |
import Test.HUnit | |
import Test.QuickCheck | |
fizzbuzz :: Int -> String | |
fizzbuzz x | |
| x `mod` 15 == 0 = "fizzbuzz" | |
| x `mod` 3 == 0 = "fizz" | |
| x `mod` 5 == 0 = "buzz" | |
| otherwise = show x | |
newtype FInt = FInt Int | |
deriving (Show, Enum) | |
fizzbuzz3 :: FInt -> Bool | |
fizzbuzz3 n = if n `mod` 3 == 0 then "fizz" == fizzbuzz n else True | |
instance Arbitrary FInt where | |
arbitrary = oneof [1..100] | |
test1 :: Test | |
test1 = TestCase (assertEqual "for (fizzbuzz 3)," "fizz" (fizzbuzz 3)) | |
test2 :: Test | |
test2 = TestCase (assertEqual "for (fizzbuzz 5)," "buzz" (fizzbuzz 5)) | |
test3 :: Test | |
test3 = TestCase (assertEqual "for (fizzbuzz 15)," "fizzbuzz" (fizzbuzz 15)) | |
test4 :: Test | |
test4 = TestCase (assertEqual "for (fizzbuzz 2)," "2" (fizzbuzz 2)) | |
test5 :: Test | |
test5 = TestCase (assertEqual "for some list, " ["1", "2", "fizz", "4", "buzz", "fizz", "7", "8", "fizz", "buzz", "11", "fizz", "13", "14", "fizzbuzz"] (map fizzbuzz [1..15])) | |
tests :: Test | |
tests = TestList [TestLabel "test1" test1, | |
TestLabel "test2" test2, | |
TestLabel "test3" test3, | |
TestLabel "test4" test4, | |
TestLabel "test5" test5] | |
main = do -- runTestTT tests | |
quickCheck ((\s -> fizzbuzz3 s) :: FInt -> Bool) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment