Commits

Aleksey Khudyakov committed 2cd88f5

Make test suite compatible with cabal

  • Participants
  • Parent commits 6b219a2

Comments (0)

Files changed (3)

File test/TestNat.hs

 import TypeLevel.Number.Nat as N
 import TypeLevel.Number.Int as I
 
-text :: Bool -> String
-text True  = "OK"
-text False = "Failed"
 
 ----------------------------------------------------------------
 -- Natural numbers
 testAdd :: Integer -> Integer -> ExpQ
 testAdd n m = 
   [| let flag = (n+m) == (N.toInt $ addN (undefined :: $(natT n)) (undefined :: $(natT m)) :: Integer)
-     in printf "Add %3i %3i : %s" (n::Integer) (m::Integer) (text flag) :: String
+     in test "+" n m flag
    |]
 
 testSub :: Integer -> Integer -> ExpQ
 testSub n m = 
   [| let flag = (n-m) == (N.toInt $ subN (undefined :: $(natT n)) (undefined :: $(natT m)) :: Integer)
-     in printf "Sub %3i %3i : %s" (n::Integer) (m::Integer) (text flag) :: String
+     in test "-" n m flag
    |]
 
 testMul :: Integer -> Integer -> ExpQ
 testMul n m = 
   [| let flag = (n*m) == (N.toInt $ mulN (undefined :: $(natT n)) (undefined :: $(natT m)) :: Integer)
-     in printf "Mul %3i %3i : %s" (n::Integer) (m::Integer) (text flag) :: String
+     in test "*" n m flag
    |]
 
 ----------------------------------------------------------------
 testAddZ :: Integer -> Integer -> ExpQ
 testAddZ n m = 
   [| let flag = (n+m) == (I.toInt $ addN (undefined :: $(intT n)) (undefined :: $(intT m)) :: Integer)
-     in printf "Add %3i %3i : %s" (n::Integer) (m::Integer) (text flag) :: String
+     in test "+" n m flag
    |]
 
 testSubZ :: Integer -> Integer -> ExpQ
 testSubZ n m = 
   [| let flag = (n-m) == (I.toInt $ subN (undefined :: $(intT n)) (undefined :: $(intT m)) :: Integer)
-     in printf "Sub %3i %3i : %s" (n::Integer) (m::Integer) (text flag) :: String
+     in test "-" n m flag
    |]
 
 testMulZ :: Integer -> Integer -> ExpQ
 testMulZ n m = 
   [| let flag = (n*m) == (I.toInt $ mulN (undefined :: $(intT n)) (undefined :: $(intT m)) :: Integer)
-     in printf "Mul %3i %3i : %s" (n::Integer) (m::Integer) (text flag) :: String
+     in test "*" n m flag
    |]
 
+test :: String -> Integer -> Integer -> Bool -> IO Bool
+test op n m flag = do
+  _ <- printf "%3i  %s %3i : %s\n" n op m (text flag)
+  return flag
+  where
+    text :: Bool -> String
+    text True  = "OK"
+    text False = "Failed"
 {-# LANGUAGE TemplateHaskell #-}
 import Control.Applicative
 import Language.Haskell.TH
+import System.Exit
 
 import TestNat
 
 
 main :: IO ()
 main = do
-  putStrLn $ unlines $(listE (testAddZ <$> [-9..9] <*> [-9..9]))
-  putStrLn $ unlines $(listE (testSubZ <$> [-9..9] <*> [-9..9]))
-  putStrLn $ unlines $(listE (testMulZ <$> [-9..9] <*> [-9..9]))
+  plus  <- sequence $(listE (testAddZ <$> [-9..9] <*> [-9..9]))
+  minus <- sequence $(listE (testSubZ <$> [-9..9] <*> [-9..9]))
+  mult  <- sequence $(listE (testMulZ <$> [-9..9] <*> [-9..9]))
+  case and $ plus ++ minus ++ mult of
+    True  -> exitSuccess
+    False -> exitFailure
 {-# LANGUAGE TemplateHaskell #-}
 import Control.Applicative
 import Language.Haskell.TH
+import System.Exit
 
 import TestNat
 
 
 main :: IO ()
 main = do
-  putStrLn $ unlines $(listE (testAdd <$> [0..8] <*> [0..8]))
-  putStrLn $ unlines $(listE [testSub n m | m <- [0..8], n <- [m..8]])
-  putStrLn $ unlines $(listE (testMul <$> [0..8] <*> [0..8]))
+  plus  <- sequence $(listE (testAdd <$> [0..8] <*> [0..8]))
+  minus <- sequence $(listE [testSub n m | m <- [0..8], n <- [m..8]])
+  mult  <- sequence $(listE (testMul <$> [0..8] <*> [0..8]))
+  case and $ plus ++ minus ++ mult of
+    True  -> exitSuccess
+    False -> exitFailure