[project @ 2000-01-20 13:38:42 by simonmar]
[ghc-hetmet.git] / ghc / tests / numeric / should_run / arith003.hs
index 6102b6b..c2162ba 100644 (file)
--- !!! test arithmetic operations from "Prelude" (gcd, lcm, etc.)
+-- $Id: arith003.hs,v 1.4 2000/01/20 13:38:42 simonmar Exp $
 --
+-- !!! test Int/Integer arithmetic operations from the Prelude.
+--
+
 main
   = putStr
-       (-- w/ Ints
-       show [
-           minInt, maxInt,
-
-           subtract i8 i4,
-           subtract i8m i4,
-           subtract maxInt i4,
-           subtract i0 minInt,
-
-           gcd i8 i6,
-           gcd i8m i6,
-           gcd i8m i6m,
-           gcd i8m i0,
-           gcd i0 i8m,
-           gcd (4070297::Int) (2695911::Int), -- 52,861
-    
-           lcm i8 i6,
-           lcm i8m i6,
-           lcm i8m i6m,
-           lcm i8m i0,
-           lcm i0 i8m,
-           lcm (4070297::Int) (2695911::Int), -- 207,585,147
-
-           i8 ^ i0,
-           i8m ^ i4,
-           i4 ^ i6
-           -- ToDo: more stuff
-
-           ]
-       ++ "\n"
+       (
+       showit (do_ops int_ops) ++
+       showit (do_ops integer_ops)
+       )
 
-       ++ show [
-           quotRem i8 i6,
-           quotRem i8m i6,
-           quotRem i8m i6m,
-           -- quotRem i8m i0,  -- no div by zero
-           quotRem i0 i8m,
-           quotRem (4070297::Int) (2695911::Int), -- 52,861
+showit :: Integral a => [(String, a, a, a)] -> String
+showit stuff = concat
+       [ str ++ " " ++ show l ++ " " ++ show r ++ " = " ++ show result ++ "\n"
+         | (str, l, r, result) <- stuff
+       ]
 
-           divMod i8 i6,
-           divMod i8m i6,
-           divMod i8m i6m,
-           -- divMod i8m i0,  -- no div by zero
-           divMod i0 i8m,
-           divMod (4070297::Int) (2695911::Int) -- 52,861
-            ]
-       ++ "\n"
+do_ops :: Integral a => [((a -> a -> a), String, [(a,a)])]
+       -> [(String, a, a, a)]
+do_ops ops = [ (str, l, r, l `op` r) | (op,str,args) <- ops, (l,r) <- args ]
 
-        -- w/ Integers
-       ++ show [
-           minIntI, maxIntI,
+small_operands, non_min_operands, operands, non_max_operands
+   :: Integral a => [a]
+small_operands  = [ 0, 1, -1, 2, -2 ]
+operands = small_operands ++ [ fromIntegral minInt, fromIntegral maxInt ]
+non_min_operands = small_operands ++ [ fromIntegral maxInt ]
+non_max_operands = small_operands ++ [ fromIntegral minInt ]
 
-           subtract i8I i4I,
-           subtract i8mI i4I,
-           subtract maxIntI i4I,
-           subtract i0I minIntI,
+large_operands :: [ Integer ]
+large_operands = operands ++ 
+   [ fromIntegral minInt - 1,
+     fromIntegral maxInt + 1,
+     fromIntegral minInt ^ 2, 
+     fromIntegral maxInt ^ 2
+   ]
 
-           gcd i8I i6I,
-           gcd i8mI i6I,
-           gcd i8mI i6mI,
-           gcd i8mI i0I,
-           gcd i0I i8mI,
-           gcd (4070297::Integer) (2695911::Integer), -- 52,861
-           gcd minIntI (-1),   -- out of Int range
-           gcd minIntI (1),
-           gcd (-1) minIntI,
-           gcd (1)  minIntI,
-           
-           lcm i8I i6I,
-           lcm i8mI i6I,
-           lcm i8mI i6mI,
-           lcm i8mI i0I,
-           lcm i0I i8mI,
-           lcm (4070297::Integer) (2695911::Integer), -- 207,585,147
-           lcm minIntI (-1),   -- out of Int range
-           lcm minIntI (1),
-           lcm (-1) minIntI,
-           lcm (1)  minIntI,
+integer_ops :: [((Integer -> Integer -> Integer), String, [(Integer,Integer)])]
+integer_ops = [ 
+  ((+),  "(+)",  all_ok),
+  ((-),  "(-)",  all_ok),
+  (div,  "div",  large_non_zero_r),
+  (mod,  "mod",  large_non_zero_r),
+  (quot, "quot", large_non_zero_r),
+  (rem,  "rem",  large_non_zero_r),
+  (gcd,  "gcd",  either_non_zero),
+  (lcm,  "lcm",  either_non_zero)
+  ]
 
-           i8I ^ i0I,
-           i8mI ^ i4I,
-           i4I ^ i6I
-           -- ToDo: more stuff
-           ]
-       ++ "\n"
-        
-       ++ show [
-           quotRem i8I i6I,
-           quotRem i8mI i6I,
-           quotRem i8mI i6mI,
-           -- quotRem i8mI i0I,  -- no div by zero
-           quotRem i0I i8mI,
-           quotRem (4070297::Integer) (2695911::Integer), -- 52,861
-           quotRem minIntI (-1),   -- out of Int range
-           quotRem minIntI (1),
-           quotRem (-1) minIntI,
-           quotRem (1)  minIntI,
+int_ops :: [((Int -> Int -> Int), String, [(Int,Int)])]
+int_ops = [ 
+  ((+),  "(+)",  all_ok),
+  ((-),  "(-)",  all_ok),
+  ((^),  "(^)",  small_non_neg_r),
+  (div,  "div",  non_min_l_or_zero_r),
+  (mod,  "mod",  non_min_l_or_zero_r),
+  (quot, "quot", non_min_l_or_zero_r),
+  (rem,  "rem",  non_min_l_or_zero_r),
+  (gcd,  "gcd",  either_non_zero),
+  (lcm,  "lcm",  non_max_r_either_non_zero)
+  ]
 
-           divMod i8I i6I,
-           divMod i8mI i6I,
-           divMod i8mI i6mI,
-           -- divMod i8mI i0I,  -- no div by zero
-           divMod i0I i8mI,
-           divMod (4070297::Integer) (2695911::Integer), -- 52,861
-           divMod minIntI (-1),   -- out of Int range
-           divMod minIntI (1),
-           divMod (-1) minIntI,
-           divMod (1)  minIntI
-           ]
+all_ok, non_zero_r, either_non_zero, non_min_l_or_zero_r,
+ non_max_r_either_non_zero, small_non_neg_r
+  :: Integral a => [(a,a)]
 
-       ++ "\n"
-       )
-  where
-    i0, i4, i4m, i6, i6m, i8, i8m :: Int
-    i0 = 0
-    i4 = 4
-    i4m = -4
-    i6 = 6
-    i6m = -6
-    i8 = 8
-    i8m = -8
-
-    i0I, i4I, i4mI, i6I, i6mI, i8I, i8mI :: Integer
-    i0I = 0
-    i4I = 4
-    i4mI = -4
-    i6I = 6
-    i6mI = -6
-    i8I = 8
-    i8mI = -8
+all_ok          = [ (l,r) | l <- operands, r <- operands ]
+large_non_zero_r = [ (l,r) | l <- operands, r <- large_operands, r /= 0 ]
+non_zero_r      = [ (l,r) | l <- operands, r <- operands, r /= 0 ]
+either_non_zero = [ (l,r) | l <- operands, r <- operands, l /= 0 || r /= 0 ]
+small_non_neg_r = [ (l,r) | l <- operands, r <- small_operands, r >= 0 ]
+non_min_l_or_zero_r = [ (l,r) | l <- non_min_operands, r <- operands, r /= 0 ]
+non_max_r_either_non_zero = [ (l,r) | l <- operands, r <- non_max_operands, l /= 0 || r /= 0 ]
 
 minInt = minBound :: Int
 maxInt = maxBound :: Int
-
-minIntI = fromIntegral minInt :: Integer
-maxIntI = fromIntegral maxInt :: Integer