Copy tests from GHC testsuite; part of #1161.
[packages/base.git] / tests / enum02.hs
1 -- !!! Testing the Int Enum instances.
2 module Main(main) where
3
4 import Control.Exception
5 import Prelude hiding (catch)
6 import Data.Int
7
8 main = do
9 putStrLn "Testing Enum Int8:"
10 testEnumInt8
11 putStrLn "Testing Enum Int16:"
12 testEnumInt16
13 putStrLn "Testing Enum Int32:"
14 testEnumInt32
15 putStrLn "Testing Enum Int64:"
16 testEnumInt64
17
18 #define printTest(x) (do{ putStr ( " " ++ "x" ++ " = " ) ; print (x) })
19
20 testEnumInt8 :: IO ()
21 testEnumInt8 = do
22 -- succ
23 printTest ((succ (0::Int8)))
24 printTest ((succ (minBound::Int8)))
25 mayBomb (printTest ((succ (maxBound::Int8))))
26
27 -- pred
28 printTest (pred (1::Int8))
29 printTest (pred (maxBound::Int8))
30 mayBomb (printTest (pred (minBound::Int8)))
31
32 -- toEnum
33 printTest ((map (toEnum::Int->Int8) [1, fromIntegral (minBound::Int8), fromIntegral (maxBound::Int8)]))
34 mayBomb (printTest ((toEnum (maxBound::Int))::Int8))
35
36 -- fromEnum
37 printTest ((map fromEnum [(1::Int8),minBound,maxBound]))
38
39 -- [x..] aka enumFrom
40 printTest ((take 7 [(1::Int8)..]))
41 printTest ((take 7 [((maxBound::Int8)-5)..])) -- just in case it doesn't catch the upper bound..
42
43 -- [x,y..] aka enumFromThen
44 printTest ((take 7 [(1::Int8),2..]))
45 printTest ((take 7 [(1::Int8),7..]))
46 printTest ((take 7 [(1::Int8),1..]))
47 printTest ((take 7 [(1::Int8),0..]))
48 printTest ((take 7 [(5::Int8),2..]))
49 let x = (minBound::Int8) + 1
50 printTest ((take 7 [x, x-1 ..]))
51 let x = (minBound::Int8) + 5
52 printTest ((take 7 [x, x-1 ..]))
53 let x = (maxBound::Int8) - 5
54 printTest ((take 7 [x, (x+1) ..]))
55
56 -- [x..y] aka enumFromTo
57 printTest ((take 7 ([(1::Int8) .. 5])))
58 printTest ((take 4 ([(1::Int8) .. 1])))
59 printTest ((take 7 ([(1::Int8) .. 0])))
60 printTest ((take 7 ([(5::Int8) .. 0])))
61 printTest ((take 7 ([(maxBound-(5::Int8)) .. maxBound])))
62 printTest ((take 7 ([(minBound+(5::Int8)) .. minBound])))
63
64 -- [x,y..z] aka enumFromThenTo
65 printTest ((take 7 [(5::Int8),4..1]))
66 printTest ((take 7 [(5::Int8),3..1]))
67 printTest ((take 7 [(5::Int8),3..2]))
68 printTest ((take 7 [(1::Int8),2..1]))
69 printTest ((take 7 [(2::Int8),1..2]))
70 printTest ((take 7 [(2::Int8),1..1]))
71 printTest ((take 7 [(2::Int8),3..1]))
72
73 let x = (maxBound::Int8) - 4
74 printTest ((take 7 [x,(x+1)..maxBound]))
75 let x = (minBound::Int8) + 5
76 printTest ((take 7 [x,(x-1)..minBound]))
77
78 testEnumInt16 :: IO ()
79 testEnumInt16 = do
80 -- succ
81 printTest ((succ (0::Int16)))
82 printTest ((succ (minBound::Int16)))
83 mayBomb (printTest ((succ (maxBound::Int16))))
84
85 -- pred
86 printTest (pred (1::Int16))
87 printTest (pred (maxBound::Int16))
88 mayBomb (printTest (pred (minBound::Int16)))
89
90 -- toEnum
91 printTest ((map (toEnum::Int->Int16) [1, fromIntegral (minBound::Int16), fromIntegral (maxBound::Int16)]))
92 mayBomb (printTest ((toEnum (maxBound::Int))::Int16))
93
94
95 -- fromEnum
96 printTest ((map fromEnum [(1::Int16),minBound,maxBound]))
97
98 -- [x..] aka enumFrom
99 printTest ((take 7 [(1::Int16)..]))
100 printTest ((take 7 [((maxBound::Int16)-5)..])) -- just in case it doesn't catch the upper bound..
101
102 -- [x,y..] aka enumFromThen
103 printTest ((take 7 [(1::Int16),2..]))
104 printTest ((take 7 [(1::Int16),7..]))
105 printTest ((take 7 [(1::Int16),1..]))
106 printTest ((take 7 [(1::Int16),0..]))
107 printTest ((take 7 [(5::Int16),2..]))
108 let x = (minBound::Int16) + 1
109 printTest ((take 7 [x, x-1 ..]))
110 let x = (minBound::Int16) + 5
111 printTest ((take 7 [x, x-1 ..]))
112 let x = (maxBound::Int16) - 5
113 printTest ((take 7 [x, (x+1) ..]))
114
115 -- [x..y] aka enumFromTo
116 printTest ((take 7 ([(1::Int16) .. 5])))
117 printTest ((take 4 ([(1::Int16) .. 1])))
118 printTest ((take 7 ([(1::Int16) .. 0])))
119 printTest ((take 7 ([(5::Int16) .. 0])))
120 printTest ((take 7 ([(maxBound-(5::Int16)) .. maxBound])))
121 printTest ((take 7 ([(minBound+(5::Int16)) .. minBound])))
122
123 -- [x,y..z] aka enumFromThenTo
124 printTest ((take 7 [(5::Int16),4..1]))
125 printTest ((take 7 [(5::Int16),3..1]))
126 printTest ((take 7 [(5::Int16),3..2]))
127 printTest ((take 7 [(1::Int16),2..1]))
128 printTest ((take 7 [(2::Int16),1..2]))
129 printTest ((take 7 [(2::Int16),1..1]))
130 printTest ((take 7 [(2::Int16),3..1]))
131
132 let x = (maxBound::Int16) - 4
133 printTest ((take 7 [x,(x+1)..maxBound]))
134 let x = (minBound::Int16) + 5
135 printTest ((take 7 [x,(x-1)..minBound]))
136
137 testEnumInt32 :: IO ()
138 testEnumInt32 = do
139 -- succ
140 printTest ((succ (0::Int32)))
141 printTest ((succ (minBound::Int32)))
142 mayBomb (printTest ((succ (maxBound::Int32))))
143
144 -- pred
145 printTest (pred (1::Int32))
146 printTest (pred (maxBound::Int32))
147 mayBomb (printTest (pred (minBound::Int32)))
148
149 -- toEnum
150 printTest ((map (toEnum::Int->Int32) [1, fromIntegral (minBound::Int32), fromIntegral (maxBound::Int32)]))
151 mayBomb (printTest ((toEnum (maxBound::Int))::Int32))
152
153 -- fromEnum
154 printTest ((map fromEnum [(1::Int32),minBound,maxBound]))
155
156 -- [x..] aka enumFrom
157 printTest ((take 7 [(1::Int32)..]))
158 printTest ((take 7 [((maxBound::Int32)-5)..])) -- just in case it doesn't catch the upper bound..
159
160 -- [x,y..] aka enumFromThen
161 printTest ((take 7 [(1::Int32),2..]))
162 printTest ((take 7 [(1::Int32),7..]))
163 printTest ((take 7 [(1::Int32),1..]))
164 printTest ((take 7 [(1::Int32),0..]))
165 printTest ((take 7 [(5::Int32),2..]))
166 let x = (minBound::Int32) + 1
167 printTest ((take 7 [x, x-1 ..]))
168 let x = (minBound::Int32) + 5
169 printTest ((take 7 [x, x-1 ..]))
170 let x = (maxBound::Int32) - 5
171 printTest ((take 7 [x, (x+1) ..]))
172
173 -- [x..y] aka enumFromTo
174 printTest ((take 7 ([(1::Int32) .. 5])))
175 printTest ((take 4 ([(1::Int32) .. 1])))
176 printTest ((take 7 ([(1::Int32) .. 0])))
177 printTest ((take 7 ([(5::Int32) .. 0])))
178 printTest ((take 7 ([(maxBound-(5::Int32)) .. maxBound])))
179 printTest ((take 7 ([(minBound+(5::Int32)) .. minBound])))
180
181 -- [x,y..z] aka enumFromThenTo
182 printTest ((take 7 [(5::Int32),4..1]))
183 printTest ((take 7 [(5::Int32),3..1]))
184 printTest ((take 7 [(5::Int32),3..2]))
185 printTest ((take 7 [(1::Int32),2..1]))
186 printTest ((take 7 [(2::Int32),1..2]))
187 printTest ((take 7 [(2::Int32),1..1]))
188 printTest ((take 7 [(2::Int32),3..1]))
189
190 let x = (maxBound::Int32) - 4
191 printTest ((take 7 [x,(x+1)..maxBound]))
192 let x = (minBound::Int32) + 5
193 printTest ((take 7 [x,(x-1)..minBound]))
194
195 testEnumInt64 :: IO ()
196 testEnumInt64 = do
197 -- succ
198 printTest ((succ (0::Int64)))
199 printTest ((succ (minBound::Int64)))
200 mayBomb (printTest ((succ (maxBound::Int64))))
201
202 -- pred
203 printTest (pred (1::Int64))
204 printTest (pred (maxBound::Int64))
205 mayBomb (printTest (pred (minBound::Int64)))
206
207 -- toEnum
208 mayBomb (printTest ((map (toEnum::Int->Int64) [1, fromIntegral (minBound::Int64), fromIntegral (maxBound::Int64)])))
209 mayBomb (printTest ((toEnum (maxBound::Int))::Int64))
210
211 -- fromEnum
212 printTest ((map fromEnum [(1::Int64),fromIntegral (minBound::Int) ,fromIntegral (maxBound::Int)]))
213 mayBomb (printTest (fromEnum (maxBound::Int64)))
214
215 -- [x..] aka enumFrom
216 printTest ((take 7 [(1::Int64)..]))
217 printTest ((take 7 [((maxBound::Int64)-5)..])) -- just in case it doesn't catch the upper bound..
218
219 -- [x,y..] aka enumFromThen
220 printTest ((take 7 [(1::Int64),2..]))
221 printTest ((take 7 [(1::Int64),7..]))
222 printTest ((take 7 [(1::Int64),1..]))
223 printTest ((take 7 [(1::Int64),0..]))
224 printTest ((take 7 [(5::Int64),2..]))
225 let x = (minBound::Int64) + 1
226 printTest ((take 7 [x, x-1 ..]))
227 let x = (minBound::Int64) + 5
228 printTest ((take 7 [x, x-1 ..]))
229 let x = (maxBound::Int64) - 5
230 printTest ((take 7 [x, (x+1) ..]))
231
232 -- [x..y] aka enumFromTo
233 printTest ((take 7 ([(1::Int64) .. 5])))
234 printTest ((take 4 ([(1::Int64) .. 1])))
235 printTest ((take 7 ([(1::Int64) .. 0])))
236 printTest ((take 7 ([(5::Int64) .. 0])))
237 printTest ((take 7 ([(maxBound-(5::Int64)) .. maxBound])))
238 printTest ((take 7 ([(minBound+(5::Int64)) .. minBound])))
239
240 -- [x,y..z] aka enumFromThenTo
241 printTest ((take 7 [(5::Int64),4..1]))
242 printTest ((take 7 [(5::Int64),3..1]))
243 printTest ((take 7 [(5::Int64),3..2]))
244 printTest ((take 7 [(1::Int64),2..1]))
245 printTest ((take 7 [(2::Int64),1..2]))
246 printTest ((take 7 [(2::Int64),1..1]))
247 printTest ((take 7 [(2::Int64),3..1]))
248
249 let x = (maxBound::Int64) - 4
250 printTest ((take 7 [x,(x+1)..maxBound]))
251 let x = (minBound::Int64) + 5
252 printTest ((take 7 [x,(x-1)..minBound]))
253
254
255 --
256 --
257 -- Utils
258 --
259 --
260
261
262 mayBomb x = catch x (\(ErrorCall e) -> putStrLn ("error " ++ show e))
263 `catch` (\e -> putStrLn ("Fail: " ++ show (e :: SomeException)))