{- - Test suite for PrimRecFunc (Haskell). - Runnable as a main program, which should print "All N tests passed". - - Copyright (c) 2014 Project Nayuki - All rights reserved. Contact Nayuki for licensing. - https://www.nayuki.io/page/primitive-recursive-functions -} module Main where import PrimRecFunc {---- Test suites ----} data TestSuite = TestSuite Prf [([Int], Int)] -- Note: A test case has type ([Int], Int) testsuites :: [TestSuite] testsuites = [ -- Primitive functions TestSuite Z [ ([0], 0), ([1], 0), ([2], 0), ([5], 0) ], TestSuite S [ ([0], 1), ([1], 2), ([2], 3), ([5], 6) ], TestSuite (I 1 0) [([0], 0), ([3], 3)], TestSuite (I 2 0) [([4, 5], 4)], TestSuite (I 2 1) [([4, 5], 5)], TestSuite (I 3 0) [([7, 8, 9], 7)], TestSuite (I 3 1) [([7, 8, 9], 8)], TestSuite (I 3 2) [([7, 8, 9], 9)], -- Boolean functions TestSuite PrimRecFunc.not [ ([0], 1), ([1], 0) ], TestSuite PrimRecFunc.and [ ([0, 0], 0), ([0, 1], 0), ([1, 0], 0), ([1, 1], 1) ], TestSuite PrimRecFunc.or [ ([0, 0], 0), ([0, 1], 1), ([1, 0], 1), ([1, 1], 1) ], TestSuite PrimRecFunc.xor [ ([0, 0], 0), ([0, 1], 1), ([1, 0], 1), ([1, 1], 0) ], TestSuite PrimRecFunc.mux [ ([0, 0, 0], 0), ([0, 0, 1], 1), ([0, 1, 0], 0), ([0, 1, 1], 1), ([1, 0, 0], 0), ([1, 0, 1], 0), ([1, 1, 0], 1), ([1, 1, 1], 1), ([0, 3, 7], 7), ([1, 3, 7], 3), ([0, 5, 2], 2), ([1, 5, 2], 5) ], -- Comparison functions TestSuite PrimRecFunc.z [ ([0], 1), ([1], 0), ([2], 0), ([5], 0) ], TestSuite PrimRecFunc.nz [ ([0], 0), ([1], 1), ([2], 1), ([5], 1) ], TestSuite PrimRecFunc.eq [ ([0, 0], 1), ([0, 1], 0), ([0, 2], 0), ([1, 0], 0), ([1, 1], 1), ([1, 2], 0), ([2, 0], 0), ([2, 1], 0), ([2, 2], 1), ([5, 0], 0), ([6, 6], 1), ([3, 7], 0) ], TestSuite PrimRecFunc.neq [ ([0, 0], 0), ([0, 1], 1), ([0, 2], 1), ([1, 0], 1), ([1, 1], 0), ([1, 2], 1), ([2, 0], 1), ([2, 1], 1), ([2, 2], 0), ([5, 0], 1), ([6, 6], 0), ([3, 7], 1) ], TestSuite PrimRecFunc.lt [ ([0, 0], 0), ([0, 1], 1), ([0, 2], 1), ([1, 0], 0), ([1, 1], 0), ([1, 2], 1), ([2, 0], 0), ([2, 1], 0), ([2, 2], 0), ([5, 0], 0), ([6, 6], 0), ([3, 7], 1) ], TestSuite PrimRecFunc.le [ ([0, 0], 1), ([0, 1], 1), ([0, 2], 1), ([1, 0], 0), ([1, 1], 1), ([1, 2], 1), ([2, 0], 0), ([2, 1], 0), ([2, 2], 1), ([5, 0], 0), ([6, 6], 1), ([3, 7], 1) ], TestSuite PrimRecFunc.gt [ ([0, 0], 0), ([0, 1], 0), ([0, 2], 0), ([1, 0], 1), ([1, 1], 0), ([1, 2], 0), ([2, 0], 1), ([2, 1], 1), ([2, 2], 0), ([5, 0], 1), ([6, 6], 0), ([3, 7], 0) ], TestSuite PrimRecFunc.ge [ ([0, 0], 1), ([0, 1], 0), ([0, 2], 0), ([1, 0], 1), ([1, 1], 1), ([1, 2], 0), ([2, 0], 1), ([2, 1], 1), ([2, 2], 1), ([5, 0], 1), ([6, 6], 1), ([3, 7], 0) ], TestSuite PrimRecFunc.even [ ([0], 1), ([1], 0), ([2], 1), ([3], 0), ([4], 1), ([5], 0) ], TestSuite PrimRecFunc.odd [ ([0], 0), ([1], 1), ([2], 0), ([3], 1), ([4], 0), ([5], 1) ], TestSuite PrimRecFunc.divisible [ ([ 0, 0], 1), ([ 1, 0], 0), ([ 2, 0], 0), ([ 0, 1], 1), ([ 1, 1], 1), ([ 2, 1], 1), ([ 0, 2], 1), ([ 1, 2], 0), ([ 2, 2], 1), ([ 3, 2], 0), ([ 0, 3], 1), ([ 1, 3], 0), ([ 2, 3], 0), ([ 3, 3], 1), ([ 4, 3], 0), ([ 5, 3], 0), ([ 6, 3], 1), ([ 7, 5], 0), ([25, 5], 1) ], TestSuite PrimRecFunc.prime [ ([ 0], 0), ([ 1], 0), ([ 2], 1), ([ 3], 1), ([ 4], 0), ([ 5], 1), ([ 6], 0), ([ 7], 1), ([ 8], 0), ([ 9], 0), ([10], 0), ([11], 1), ([12], 0), ([13], 1), ([14], 0), ([15], 0), ([16], 0), ([17], 1), ([18], 0), ([19], 1), ([20], 0), ([21], 0), ([22], 0), ([23], 1), ([24], 0), ([25], 0), ([26], 0), ([27], 0), ([28], 0), ([29], 1), ([30], 0) ], -- Arithmetic functions TestSuite (PrimRecFunc.const 0) [([0], 0), ([9], 0)], TestSuite (PrimRecFunc.const 1) [([0], 1), ([1], 1), ([3], 1)], TestSuite (PrimRecFunc.const 2) [([0], 2), ([1], 2), ([2], 2)], TestSuite (PrimRecFunc.const 3) [([0], 3), ([3], 3), ([5], 3)], TestSuite PrimRecFunc.pred [ ([0], 0), ([1], 0), ([2], 1), ([3], 2), ([9], 8) ], TestSuite PrimRecFunc.add [ ([0, 0], 0), ([0, 1], 1), ([0, 3], 3), ([1, 0], 1), ([2, 0], 2), ([1, 1], 2), ([2, 5], 7), ([6, 3], 9) ], TestSuite PrimRecFunc.sub [ ([0, 0], 0), ([0, 1], 0), ([0, 2], 0), ([1, 0], 1), ([1, 1], 0), ([1, 2], 0), ([2, 0], 2), ([2, 1], 1), ([2, 2], 0), ([2, 3], 0), ([3, 0], 3), ([5, 2], 3), ([7, 6], 1) ], TestSuite PrimRecFunc.subrev [ ([0, 0], 0), ([1, 0], 0), ([2, 0], 0), ([0, 1], 1), ([1, 1], 0), ([2, 1], 0), ([0, 2], 2), ([1, 2], 1), ([2, 2], 0), ([3, 2], 0), ([0, 3], 3), ([2, 5], 3), ([6, 7], 1) ], TestSuite PrimRecFunc.diff [ ([0, 0], 0), ([0, 1], 1), ([0, 2], 2), ([1, 0], 1), ([1, 1], 0), ([1, 2], 1), ([2, 0], 2), ([2, 1], 1), ([2, 2], 0), ([5, 0], 5), ([6, 6], 0), ([3, 7], 4) ], TestSuite PrimRecFunc.min [ ([0, 0], 0), ([0, 1], 0), ([0, 2], 0), ([1, 0], 0), ([1, 1], 1), ([1, 2], 1), ([2, 0], 0), ([2, 1], 1), ([2, 2], 2), ([3, 0], 0), ([5, 2], 2), ([7, 6], 6) ], TestSuite PrimRecFunc.max [ ([0, 0], 0), ([0, 1], 1), ([0, 2], 2), ([1, 0], 1), ([1, 1], 1), ([1, 2], 2), ([2, 0], 2), ([2, 1], 2), ([2, 2], 2), ([3, 0], 3), ([5, 2], 5), ([7, 6], 7) ], TestSuite PrimRecFunc.mul [ ([0, 0], 0), ([0, 1], 0), ([0, 2], 0), ([1, 0], 0), ([3, 0], 0), ([1, 1], 1), ([1, 2], 2), ([2, 1], 2), ([2, 2], 4), ([3, 7], 21), ([5, 8], 40) ], TestSuite PrimRecFunc.pow [ ([0, 0], 1), ([0, 1], 0), ([0, 2], 0), ([1, 0], 1), ([1, 1], 1), ([1, 2], 1), ([2, 0], 1), ([2, 1], 2), ([2, 2], 4), ([2, 3], 8), ([2, 4], 16), ([2, 5], 32), ([2, 6], 64), ([2, 7], 128), ([2, 8], 256), ([2, 9], 512), ([3, 1], 3), ([3, 2], 9), ([4, 3], 64), ([5, 3], 125), ([6, 2], 36) ], TestSuite PrimRecFunc.sqrt [ ([ 0], 0), ([ 1], 1), ([ 2], 1), ([ 3], 1), ([ 4], 2), ([ 5], 2), ([ 6], 2), ([ 7], 2), ([ 8], 2), ([ 9], 3), ([10], 3), ([15], 3), ([16], 4), ([17], 4), ([23], 4) ], TestSuite PrimRecFunc.log [ ([0, 0], 0), ([0, 1], 1), ([0, 2], 2), ([0, 4], 4), ([0, 7], 7), ([1, 0], 0), ([1, 1], 1), ([1, 2], 2), ([1, 5], 5), ([1, 9], 9), ([2, 0], 0), ([2, 1], 0), ([2, 2], 1), ([2, 3], 1), ([2, 4], 2), ([2, 5], 2), ([2, 6], 2), ([2, 7], 2), ([2, 8], 3), ([2, 9], 3), ([2, 15], 3), ([2, 16], 4), ([2, 17], 4), ([3, 0], 0), ([3, 1], 0), ([3, 2], 0), ([3, 3], 1), ([3, 4], 1), ([3, 8], 1), ([3, 9], 2), ([3, 10], 2), ([3, 26], 2), ([3, 27], 3), ([4, 0], 0), ([4, 1], 0), ([4, 2], 0), ([4, 3], 0), ([4, 4], 1), ([4, 5], 1), ([4, 8], 1), ([4, 15], 1), ([4, 16], 2) ], TestSuite PrimRecFunc.div [ ([ 0, 0], 0), ([ 1, 0], 1), ([ 2, 0], 2), ([ 3, 0], 3), ([ 0, 1], 0), ([ 1, 1], 1), ([ 2, 1], 2), ([ 3, 1], 3), ([ 0, 2], 0), ([ 1, 2], 0), ([ 2, 2], 1), ([ 3, 2], 1), ([ 4, 2], 2), ([11, 2], 5), ([14, 2], 7), ([ 0, 3], 0), ([ 1, 3], 0), ([ 2, 3], 0), ([ 3, 3], 1), ([ 4, 3], 1), ([ 5, 3], 1), ([ 6, 3], 2), ([11, 3], 3), ([18, 3], 6), ([ 8, 4], 2), ([ 0, 4], 0), ([23, 4], 5), ([20, 5], 4), ([21, 5], 4), ([ 5, 6], 0), ([30, 6], 5), ([ 2, 7], 0), ([19, 7], 2) ], TestSuite PrimRecFunc.mod [ ([ 0, 0], 0), ([ 1, 0], 1), ([ 2, 0], 2), ([ 3, 0], 3), ([ 0, 1], 0), ([ 1, 1], 0), ([ 2, 1], 0), ([ 3, 1], 0), ([ 0, 2], 0), ([ 1, 2], 1), ([ 2, 2], 0), ([ 3, 2], 1), ([ 0, 3], 0), ([ 1, 3], 1), ([ 2, 3], 2), ([ 3, 3], 0), ([ 4, 3], 1), ([ 5, 3], 2), ([ 7, 4], 3), ([21, 5], 1), ([30, 6], 0), ([19, 7], 5) ], TestSuite PrimRecFunc.factorial [ ([0], 1), ([1], 1), ([2], 2), ([3], 6), ([4], 24), ([5], 120), ([6], 720) ], TestSuite PrimRecFunc.gcd [ ([ 0, 0], 0), ([ 0, 1], 1), ([ 0, 2], 2), ([ 0, 3], 3), ([ 1, 0], 1), ([ 1, 1], 1), ([ 1, 2], 1), ([ 1, 3], 1), ([ 2, 0], 2), ([ 2, 1], 1), ([ 2, 2], 2), ([ 2, 3], 1), ([ 2, 4], 2), ([ 3, 0], 3), ([ 3, 1], 1), ([ 3, 2], 1), ([ 3, 3], 3), ([ 6, 2], 2), ([ 6, 3], 3), ([ 6, 6], 6), ([12, 2], 2), ([12, 3], 3), ([12, 4], 4), ([12, 6], 6) ], TestSuite PrimRecFunc.lcm [ ([ 0, 0], 0), ([ 0, 1], 0), ([ 0, 2], 0), ([ 1, 0], 0), ([ 1, 1], 1), ([ 1, 2], 2), ([ 1, 3], 3), ([ 2, 0], 0), ([ 2, 1], 2), ([ 2, 2], 2), ([ 2, 3], 6), ([ 2, 4], 4), ([ 3, 0], 0), ([ 3, 1], 3), ([ 3, 2], 6), ([ 3, 3], 3), ([ 3, 5], 15), ([ 6, 15], 30), ([ 6, 30], 30), ([10, 15], 30) ], TestSuite PrimRecFunc.divisiblecount [ ([ 0, 0], 0), ([ 1, 0], 0), ([ 2, 0], 0), ([ 4, 0], 0), ([ 7, 0], 0), ([ 0, 1], 0), ([ 1, 1], 1), ([ 2, 1], 2), ([ 5, 1], 5), ([ 9, 1], 9), ([ 0, 2], 0), ([ 1, 2], 0), ([ 2, 2], 1), ([ 3, 2], 0), ([ 4, 2], 2), ([ 5, 2], 0), ([ 6, 2], 1), ([ 7, 2], 0), ([ 8, 2], 3), ([ 9, 2], 0), ([10, 2], 1), ([ 0, 3], 0), ([ 1, 3], 0), ([ 2, 3], 0), ([ 3, 3], 1), ([ 4, 3], 0), ([ 5, 3], 0), ([ 6, 3], 1), ([ 7, 3], 0), ([ 8, 3], 0), ([ 9, 3], 2), ([10, 3], 0), ([ 0, 4], 0), ([ 3, 4], 0), ([ 4, 4], 1), ([ 5, 4], 0), ([ 8, 4], 1), ([16, 4], 2) ], TestSuite PrimRecFunc.nthprime [ ([0], 2), ([1], 3), ([2], 5), ([3], 7) ], TestSuite PrimRecFunc.fibonacci [ ([0], 0), ([1], 1), ([2], 1), ([3], 2), ([4], 3), ([5], 5) ], -- Bitwise functions TestSuite PrimRecFunc.shl [ ([0, 0], 0), ([1, 0], 1), ([2, 0], 2), ([3, 0], 3), ([0, 1], 0), ([1, 1], 2), ([2, 1], 4), ([3, 1], 6), ([0, 2], 0), ([1, 2], 4), ([2, 2], 8), ([3, 2], 12), ([0, 3], 0), ([1, 3], 8), ([2, 3], 16), ([3, 3], 24) ], TestSuite PrimRecFunc.shr [ ([0, 0], 0), ([1, 0], 1), ([2, 0], 2), ([3, 0], 3), ([0, 1], 0), ([1, 1], 0), ([2, 1], 1), ([3, 1], 1), ([0, 2], 0), ([1, 2], 0), ([2, 2], 0), ([3, 2], 0), ([4, 2], 1), ([5, 2], 1), ([6, 2], 1), ([7, 2], 1), ([8, 2], 2) ], TestSuite PrimRecFunc.band [ ([0, 0], 0), ([1, 0], 0), ([2, 0], 0), ([3, 0], 0), ([0, 1], 0), ([1, 1], 1), ([2, 1], 0), ([3, 1], 1), ([0, 2], 0), ([1, 2], 0), ([2, 2], 2), ([3, 2], 2), ([0, 3], 0), ([1, 3], 1), ([2, 3], 2), ([3, 3], 3) ], TestSuite PrimRecFunc.bandnot [ ([0, 0], 0), ([1, 0], 1), ([2, 0], 2), ([3, 0], 3), ([0, 1], 0), ([1, 1], 0), ([2, 1], 2), ([3, 1], 2), ([0, 2], 0), ([1, 2], 1), ([2, 2], 0), ([3, 2], 1), ([0, 3], 0), ([1, 3], 0), ([2, 3], 0), ([3, 3], 0) ], TestSuite PrimRecFunc.bor [ ([0, 0], 0), ([1, 0], 1), ([2, 0], 2), ([3, 0], 3), ([0, 1], 1), ([1, 1], 1), ([2, 1], 3), ([3, 1], 3), ([0, 2], 2), ([1, 2], 3), ([2, 2], 2), ([3, 2], 3), ([0, 3], 3), ([1, 3], 3), ([2, 3], 3), ([3, 3], 3) ], TestSuite PrimRecFunc.bxor [ ([0, 0], 0), ([1, 0], 1), ([2, 0], 2), ([3, 0], 3), ([0, 1], 1), ([1, 1], 0), ([2, 1], 3), ([3, 1], 2), ([0, 2], 2), ([1, 2], 3), ([2, 2], 0), ([3, 2], 1), ([0, 3], 3), ([1, 3], 2), ([2, 3], 1), ([3, 3], 0) ], TestSuite PrimRecFunc.getbit [ ([ 0, 0], 0), ([ 1, 0], 1), ([ 2, 0], 0), ([ 3, 0], 1), ([ 0, 1], 0), ([ 1, 1], 0), ([ 2, 1], 1), ([ 3, 1], 1), ([ 0, 2], 0), ([ 1, 2], 0), ([ 2, 2], 0), ([ 3, 2], 0), ([ 4, 2], 1), ([ 5, 2], 1), ([ 6, 2], 1), ([ 7, 2], 1), ([ 0, 3], 0), ([ 5, 3], 0), ([ 8, 3], 1), ([ 9, 3], 1), ([16, 3], 0) ]] {---- Main program ----} main = do putStrLn "Running tests..." doTestSuites testsuites (False, 0) doTestSuites :: [TestSuite] -> (Bool, Int) -> IO () doTestSuites [] (False, count) = putStrLn \$ "All " ++ (show count) ++ " tests passed" doTestSuites [] (True, _) = return () doTestSuites ((TestSuite f cases):tss) info = do newinfo <- doTestCases f cases info doTestSuites tss newinfo doTestCases :: Prf -> [([Int], Int)] -> (Bool, Int) -> IO (Bool, Int) doTestCases _ [] info = return info doTestCases f ((arg, ans):tcs) (failed, count) = do let actual = eval f arg if actual /= ans then do if Prelude.not failed then putStrLn "One or more tests failed:" else return () putStrLn \$ " " ++ (show f) ++ " " ++ (show arg) ++ " = " ++ (show actual) ++ " != " ++ (show ans) doTestCases f tcs (True, count + 1) else doTestCases f tcs (failed, count + 1)