--
module HUnitDemo where import Test.HUnit(Test(TestCase,TestList),assertEqual,runTestTT) import Test.QuickCheck -- 1) write a type for the function. Make its body undefined len:: [ a ] -> Int len [] = 0 len (x:xs) = 1 + len xs -- 2) make some examples, and express them as HUnit assertions -- len [] --> 0 -- len [1,1,1,1] --> 4 -- len [1,2] --> 2 ex1 = assertEqual "empty" (len []) 0 ex2 = assertEqual "all the same" (len [1,1,1,1]) 4 ex3 = assertEqual "two" (len [1,2]) 2 -- 3) Collect all the tests together -- By convention we use the name tests -- to collect all the Unit-tests in one file together. lenTest = TestList[TestCase ex1, TestCase ex2, TestCase ex3] tests = runTestTT (TestList[lenTest,sumTests]) -- 4) Then fill in the definition until all tests are passed! --------------------------------------------------------- -- Now try it for the sum function -- 1) write a type for the function. Make its body undefined summ:: [Int] -> Int summ [] = 0 summ (x:xs) = x + summ xs -- 2) make some examples, and express them as HUnit assertions ex4 = assertEqual "sum 0" (summ []) 0 ex5 = assertEqual "sum 2" (summ [2]) 2 ex6= assertEqual "sum 10" (summ [5,4,1]) 10 sumTests = TestList [TestCase ex4, TestCase ex5, TestCase ex6] -- 3) Collect all the tests together -- 4) Then fill in the definition until all tests are passed! --