[project @ 2000-03-23 09:32:36 by simonmar]
[ghc-hetmet.git] / ghc / tests / numeric / should_run / arith003.hs
1 -- $Id: arith003.hs,v 1.5 2000/03/23 09:32:36 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      fromIntegral minInt ^ 2, 
37      fromIntegral maxInt ^ 2
38    ]
39
40 integer_ops :: [((Integer -> Integer -> Integer), String, [(Integer,Integer)])]
41 integer_ops = [ 
42   ((+),  "(+)",  all_ok),
43   ((-),  "(-)",  all_ok),
44   (div,  "div",  large_non_zero_r),
45   (mod,  "mod",  large_non_zero_r),
46   (quot, "quot", large_non_zero_r),
47   (rem,  "rem",  large_non_zero_r),
48   (gcd,  "gcd",  either_non_zero),
49   (lcm,  "lcm",  either_non_zero)
50   ]
51
52 int_ops :: [((Int -> Int -> Int), String, [(Int,Int)])]
53 int_ops = [ 
54   ((+),  "(+)",  all_ok),
55   ((-),  "(-)",  all_ok),
56   ((^),  "(^)",  small_non_neg_r),
57   (div,  "div",  non_min_l_or_zero_r),
58   (mod,  "mod",  non_min_l_or_zero_r),
59   (quot, "quot", non_min_l_or_zero_r),
60   (rem,  "rem",  non_min_l_or_zero_r),
61   (gcd,  "gcd",  either_non_zero),
62   (lcm,  "lcm",  non_max_r_either_non_zero)
63   ]
64
65 all_ok, non_zero_r, either_non_zero, non_min_l_or_zero_r,
66  non_max_r_either_non_zero, small_non_neg_r
67   :: Integral a => [(a,a)]
68
69 all_ok          = [ (l,r) | l <- operands, r <- operands ]
70 large_non_zero_r = [ (l,r) | l <- operands, r <- large_operands, r /= 0 ]
71 non_zero_r      = [ (l,r) | l <- operands, r <- operands, r /= 0 ]
72 either_non_zero = [ (l,r) | l <- operands, r <- operands, l /= 0 || r /= 0 ]
73 small_non_neg_r = [ (l,r) | l <- operands, r <- small_operands, r >= 0 ]
74 non_min_l_or_zero_r = [ (l,r) | l <- non_min_operands, r <- operands, r /= 0 ]
75 non_max_r_either_non_zero = [ (l,r) | l <- operands, r <- non_max_operands, l /= 0 || r /= 0 ]
76
77 minInt = minBound :: Int
78 maxInt = maxBound :: Int