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