c2162ba57f427ea818c24c2d708d357e8282a22b
[ghc-hetmet.git] / ghc / tests / numeric / should_run / arith003.hs
1 -- $Id: arith003.hs,v 1.4 2000/01/20 13:38:42 simonmar Exp $
2 --
3 -- !!! test Int/Integer arithmetic operations from the Prelude.
4 --
5
6 main
7   = putStr
8        (
9         showit (do_ops int_ops) ++
10         showit (do_ops integer_ops)
11         )
12
13 showit :: Integral a => [(String, a, a, a)] -> String
14 showit stuff = concat
15        [ str ++ " " ++ show l ++ " " ++ show r ++ " = " ++ show result ++ "\n"
16          | (str, l, r, result) <- stuff
17        ]
18
19 do_ops :: Integral a => [((a -> a -> a), String, [(a,a)])]
20         -> [(String, a, a, a)]
21 do_ops ops = [ (str, l, r, l `op` r) | (op,str,args) <- ops, (l,r) <- args ]
22
23 small_operands, non_min_operands, operands, non_max_operands
24    :: Integral a => [a]
25 small_operands  = [ 0, 1, -1, 2, -2 ]
26 operands = small_operands ++ [ fromIntegral minInt, fromIntegral maxInt ]
27 non_min_operands = small_operands ++ [ fromIntegral maxInt ]
28 non_max_operands = small_operands ++ [ fromIntegral minInt ]
29
30 large_operands :: [ Integer ]
31 large_operands = operands ++ 
32    [ fromIntegral minInt - 1,
33      fromIntegral maxInt + 1,
34      fromIntegral minInt ^ 2, 
35      fromIntegral maxInt ^ 2
36    ]
37
38 integer_ops :: [((Integer -> Integer -> Integer), String, [(Integer,Integer)])]
39 integer_ops = [ 
40   ((+),  "(+)",  all_ok),
41   ((-),  "(-)",  all_ok),
42   (div,  "div",  large_non_zero_r),
43   (mod,  "mod",  large_non_zero_r),
44   (quot, "quot", large_non_zero_r),
45   (rem,  "rem",  large_non_zero_r),
46   (gcd,  "gcd",  either_non_zero),
47   (lcm,  "lcm",  either_non_zero)
48   ]
49
50 int_ops :: [((Int -> Int -> Int), String, [(Int,Int)])]
51 int_ops = [ 
52   ((+),  "(+)",  all_ok),
53   ((-),  "(-)",  all_ok),
54   ((^),  "(^)",  small_non_neg_r),
55   (div,  "div",  non_min_l_or_zero_r),
56   (mod,  "mod",  non_min_l_or_zero_r),
57   (quot, "quot", non_min_l_or_zero_r),
58   (rem,  "rem",  non_min_l_or_zero_r),
59   (gcd,  "gcd",  either_non_zero),
60   (lcm,  "lcm",  non_max_r_either_non_zero)
61   ]
62
63 all_ok, non_zero_r, either_non_zero, non_min_l_or_zero_r,
64  non_max_r_either_non_zero, small_non_neg_r
65   :: Integral a => [(a,a)]
66
67 all_ok          = [ (l,r) | l <- operands, r <- operands ]
68 large_non_zero_r = [ (l,r) | l <- operands, r <- large_operands, r /= 0 ]
69 non_zero_r      = [ (l,r) | l <- operands, r <- operands, r /= 0 ]
70 either_non_zero = [ (l,r) | l <- operands, r <- operands, l /= 0 || r /= 0 ]
71 small_non_neg_r = [ (l,r) | l <- operands, r <- small_operands, r >= 0 ]
72 non_min_l_or_zero_r = [ (l,r) | l <- non_min_operands, r <- operands, r /= 0 ]
73 non_max_r_either_non_zero = [ (l,r) | l <- operands, r <- non_max_operands, l /= 0 || r /= 0 ]
74
75 minInt = minBound :: Int
76 maxInt = maxBound :: Int