Skip to content

Instantly share code, notes, and snippets.

@VoQn
Created September 26, 2010 07:29
Show Gist options
  • Save VoQn/597692 to your computer and use it in GitHub Desktop.
Save VoQn/597692 to your computer and use it in GitHub Desktop.
閏年判定する関数をHUnit使用するテストする開発コア
import Test.HUnit
-- Target Function for Test
leap :: Integral a => a -> Bool
leap year = if d 100 then d 400
else d 4
where divisible = (== 0) . (mod year)
d = divisible
-- Test Method
leapTest :: Integral a => a -> Bool -> Test
leapTest year = let label = "tested: (leap " ++ (show year) ++ "),"
actural = leap year
in (label ~:) . (actural ~=?)
-- Make Test Method by Tapple (argument, expeted_result)
makeCaseByPair :: Integral a => (a, Bool) -> Test
makeCaseByPair pair = let year = fst pair
expected = snd pair
in leapTest year expected
-- Zip Test Cases
zipCase :: Integral a => [(a, Bool)] -> Test
zipCase = TestList . (map makeCaseByPair)
-- testCase
tests = zipCase [(3, False), (4, True), (5, False),
(7, False), (8, True), (9, False),
(96, True), (100, False), (104, True),
(300, False), (400, True), (500, False),
(1900, False), (2000, True), (2100, False)]
-- Run It
-- *Main> runTestTT tests
-- EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment