Import Upstream version 20180207
[hcoop/debian/mlton.git] / regression / word.sml
CommitLineData
7f918cf1
CE
1(* Auxiliary functions for test cases *)
2
3infix 1 seq
4fun e1 seq e2 = e2;
5fun check b = if b then "OK" else "WRONG";
6fun check' f = (if f () then "OK" else "WRONG") handle _ => "EXN";
7
8fun range (from, to) p =
9 let open Int
10 in
11 (from > to) orelse (p from) andalso (range (from+1, to) p)
12 end;
13
14fun checkrange bounds = check o range bounds;
15
16fun tst0 s s' = print (s ^ " \t" ^ s' ^ "\n");
17fun tst s b = tst0 s (check b);
18fun tst' s f = tst0 s (check' f);
19
20fun tstrange s bounds = (tst s) o range bounds
21
22(* test/word.sml -- some test cases for Word, appropriate for a two's
23 complement machine whose Int.precision = SOME 31
24 PS 1995-03-19, 1995-07-12, 1995-11-06, 1996-04-01, 1996-10-01
25
26 modified to work for Int.precision = SOME 32 -- ME 1998-10-07
27*)
28
29(*KILL 05/11/1997 11:04. tho.:
30use "auxil.sml";
31*)
32
33local
34 (* Isn't this disgusting: *)
35 val [gt, lt, ge, le] =
36 [op>, op<, op>=, op<=] : (int * int -> bool) list
37 val [add, sub, mul, idiv, imod] =
38 [op+, op-, op*, op div, op mod] : (int * int -> int) list
39 open Word;
40 val op > = gt and op < = lt and op >= = ge and op <= = le;
41 val op + = add and op - = sub and op * = mul
42 and op div = idiv and op mod = imod;
43 val i2w = fromInt
44 and w2i = toIntX;
45 fun pr_ln s s' = print (s ^ ": " ^ s' ^ "\n")
46in
47
48val test1 = checkrange (0, 1025)
49 (fn i => i = w2i (i2w i));
50val _ = pr_ln "test1" test1
51
52val test3 = checkrange (~1000, 1000)
53 (fn i => i = toIntX (i2w i));
54val _ = pr_ln "test3" test3
55
56val test5a = checkrange (0,15)
57 (fn i => (i+960) div 2 * 2 + 1
58 = w2i (orb (i2w i, i2w 961)));
59val _ = pr_ln "test5a" test5a
60val test5b = checkrange (0,513)
61 (fn i => i = w2i (orb (i2w i, i2w i)));
62val _ = pr_ln "test5b" test5b
63val test6a = checkrange (0,15)
64 (fn i => i div 2 * 2 = w2i (andb (i2w i, i2w ~2)));
65val _ = pr_ln "test6a" test6a
66val test6b = checkrange (0,513)
67 (fn i => i = w2i (andb (i2w i, i2w i)));
68val _ = pr_ln "test6b" test6b
69val test7a = checkrange (0,15)
70 (fn i => i+960 = w2i (xorb (i2w i, i2w 960)));
71val _ = pr_ln "test7a" test7a
72val test7b = checkrange (0, 513)
73 (fn i => 0 = w2i (xorb (i2w i, i2w i)));
74val _ = pr_ln "test7b" test7b
75val test8a = check(~1 = w2i (notb (i2w 0)));
76val _ = pr_ln "test8a" test8a
77val test8b = check (0 = w2i (notb (i2w ~1)));
78val _ = pr_ln "test8b" test8b
79val maxposint = valOf Int.maxInt;
80val maxnegint = (Int.~ maxposint)-1;
81fun pwr2 0 = 1
82 | pwr2 n = 2 * pwr2 (n-1);
83fun rwp i 0 = i
84 | rwp i n = rwp i (n-1) div 2;
85
86val test9a = checkrange (0,29)
87 (fn k => pwr2 k = w2i (<< (i2w 1, i2w k)));
88val _ = pr_ln "test9a" test9a
89val test9b = checkrange (32,65)
90 (fn k => 0 = w2i (<< (i2w 1, i2w k)));
91val _ = pr_ln "test9b" test9b
92val test9c = check (maxnegint = w2i (<< (i2w 1, i2w 31)));
93val _ = pr_ln "test9c" test9c
94val test9d = checkrange (0, 1025)
95 (fn i => 2 * i = w2i (<< (i2w i, i2w 1)));
96val _ = pr_ln "test9d" test9d
97val test9e = checkrange (0, 1025)
98 (fn i => i div 2 = w2i (>> (i2w i, i2w 1)));
99val _ = pr_ln "test9e" test9e
100val test9f = checkrange (0,65)
101 (fn k => rwp maxposint k = w2i (>> (i2w maxposint, i2w k)));
102val _ = pr_ln "test9f" test9f
103val test9g = checkrange (32,65)
104 (fn k => 0 = w2i (<< (i2w ~1, i2w k)));
105val _ = pr_ln "test9g" test9g
106val test9h = checkrange (1,65)
107 (fn k => 0 = w2i (>> (i2w 1, i2w k)));
108val _ = pr_ln "test9h" test9h
109
110val test10a = checkrange (1,65)
111 (fn k => 0 = w2i (~>> (i2w 1, i2w k)));
112val _ = pr_ln "test10a" test10a
113val test10b = checkrange (1,65)
114 (fn k => ~1 = w2i (~>> (i2w ~1, i2w k)));
115val _ = pr_ln "test10b" test10b
116val test10c = checkrange (~513, 513)
117 (fn i => i div 2 = toIntX (~>> (i2w i, i2w 1)));
118val _ = pr_ln "test10c" test10c
119val test10d = checkrange (0,65)
120 (fn k => rwp maxnegint k = toIntX (~>> (i2w maxnegint, i2w k)));
121val _ = pr_ln "test10d" test10d
122local
123 open Word
124in
125val test11a = check (i2w 256 > i2w 255);
126val _ = pr_ln "test11a" test11a
127val test11b = check (i2w 0 < i2w ~1);
128val _ = pr_ln "test11b" test11b
129val test11c = check (i2w maxposint >= i2w maxposint);
130val _ = pr_ln "test11c" test11c
131val test11d = check (i2w maxnegint >= i2w 127);
132val _ = pr_ln "test11d" test11d
133val test11e = check (i2w 1 <= i2w 1);
134val _ = pr_ln "test11e" test11e
135val test11f = check (i2w 0 <= i2w 1);
136val _ = pr_ln "test11f" test11f
137val test11g = check (i2w 0 < i2w maxposint);
138val _ = pr_ln "test11g" test11g
139val test11h = check (i2w maxposint < i2w maxnegint);
140val _ = pr_ln "test11h" test11h
141val test11i = check (i2w maxnegint < i2w ~1);
142val _ = pr_ln "test11i" test11i
143end;
144
145local
146 open Word
147in
148val test12a = checkrange(0, 300) (fn k => w2i (i2w k + i2w 17) = add(k, 17));
149val _ = pr_ln "test12a" test12a
150val test12b = checkrange(0, 300) (fn k => w2i (i2w k - i2w 17) = sub(k, 17));
151val _ = pr_ln "test12b" test12b
152val test12c = checkrange(0, 300) (fn k => w2i (i2w k * i2w 17) = mul(k, 17));
153val _ = pr_ln "test12c" test12c
154val test12d = checkrange(0, 300)
155 (fn k => w2i (i2w k div i2w 17) = idiv(k, 17));
156val _ = pr_ln "test12d" test12d
157val test12e = checkrange(0, 300)
158 (fn k => w2i (i2w k mod i2w 17) = imod(k, 17));
159val _ = pr_ln "test12e" test12e
160val test12f = checkrange(0, 300)
161 (fn k => w2i (i2w k + i2w maxnegint) = add(k, maxnegint));
162val _ = pr_ln "test12f" test12f
163val test12g = checkrange(0, 300)
164 (fn k => w2i (i2w maxnegint - i2w k - i2w 1) = sub(maxposint,k));
165val _ = pr_ln "test12g" test12g
166val test12h = checkrange(0, 300)
167 (fn k => w2i (i2w k * i2w maxnegint) = mul(imod(k, 2), maxnegint));
168val _ = pr_ln "test12h" test12h
169val test12i = checkrange(0, 300)
170 (fn k => w2i (i2w k * i2w maxposint + i2w k) = mul(imod(k, 2), maxnegint));
171val _ = pr_ln "test12i" test12i
172val test12j = checkrange(0, 300)
173 (fn k => w2i (i2w k div i2w ~1) = 0);
174val _ = pr_ln "test12j" test12j
175val test12k = checkrange(0, 300)
176 (fn k => w2i (i2w k mod i2w ~1) = k);
177val _ = pr_ln "test12k" test12k
178val test12l = check(w2i (i2w maxposint + i2w 1) = maxnegint);
179val _ = pr_ln "test12l" test12l
180val test12m = check(w2i (i2w maxnegint - i2w 1) = maxposint);
181val _ = pr_ln "test12m" test12m
182val test12n = check(w2i (i2w ~1 div i2w 2) = maxposint);
183val _ = pr_ln "test12n" test12n
184val test12o = check(w2i (i2w ~1 mod i2w 2) = 1);
185val _ = pr_ln "test12o" test12o
186val test12p = check(w2i (i2w ~1 div i2w 100) = idiv(maxposint, 50));
187val _ = pr_ln "test12p" test12p
188(*31bit
189val test12q = check(w2i (i2w ~1 mod i2w 10) = 7);
190val _ = pr_ln "test12q" test12q
191*)
192val test12r = (i2w 17 div i2w 0 seq "WRONG")
193 handle Div => "OK" | _ => "WRONG";
194val _ = pr_ln "test12r" test12r
195val test12s = (i2w 17 mod i2w 0 seq "WRONG")
196 handle Div => "OK" | _ => "WRONG";
197val _ = pr_ln "test12s" test12s
198fun chk f (s, r) =
199 check'(fn _ =>
200 case f s of
201 SOME res => res = i2w r
202 | NONE => false)
203
204fun chkScan fmt = chk (StringCvt.scanString (scan fmt))
205
206val test13a =
207 List.map (chk fromString)
208 [("20Af", 8367),
209 (" \n\t20AfGrap", 8367),
210 ("0w20Af", 0 (*8367*)),
211 (" \n\t0w20AfGrap", 0 (*8367*)),
212 ("0", 0),
213 ("0w", 0),
214 ("0W1", 0),
215 ("0w ", 0),
216 ("0wx", 0),
217 ("0wX", 0),
218 ("0wx1", 1),
219 ("0wX1", 1),
220 ("0wx ", 0),
221 ("0wX ", 0)];
222val _ = pr_ln "test13a" (concat test13a)
223val test13b =
224 List.map (fn s => case fromString s of NONE => "OK" | _ => "WRONG")
225 ["", "-", "~", "+", " \n\t", " \n\t-", " \n\t~", " \n\t+",
226 "+1", "~1", "-1", "GG"];
227val _ = pr_ln "test13b" (concat test13b)
228
229val test14a =
230 List.map (chkScan StringCvt.DEC)
231 [("10789", 10789),
232 (" \n\t10789crap", 10789),
233 ("0w10789", 10789),
234 (" \n\t0w10789crap", 10789),
235 ("0", 0),
236 ("0w", 0),
237 ("0W1", 0),
238 ("0w ", 0),
239 ("0wx", 0),
240 ("0wX", 0),
241 ("0wx1", 0),
242 ("0wX1", 0),
243 ("0wx ", 0),
244 ("0wX ", 0)];
245val _ = pr_ln "test14a" (concat test14a)
246val test14b =
247 List.map (fn s => case StringCvt.scanString (scan StringCvt.DEC) s
248 of NONE => "OK" | _ => "WRONG")
249 ["", "-", "~", "+", " \n\t", " \n\t-", " \n\t~", " \n\t+",
250 "+1", "~1", "-1", "ff"];
251val _ = pr_ln "test14b" (concat test14b)
252val test15a =
253 List.map (chkScan StringCvt.BIN)
254 [("10010", 18),
255 (" \n\t10010crap", 18),
256 ("0w10010", 18),
257 (" \n\t0w10010crap", 18),
258 ("0", 0),
259 ("0w", 0),
260 ("0W1", 0),
261 ("0w ", 0),
262 ("0wx", 0),
263 ("0wX", 0),
264 ("0wx1", 0),
265 ("0wX1", 0),
266 ("0wx ", 0),
267 ("0wX ", 0)];
268val _ = pr_ln "test15a" (concat test15a)
269val test15b =
270 List.map (fn s => case StringCvt.scanString (scan StringCvt.BIN) s
271 of NONE => "OK" | _ => "WRONG")
272 ["", "-", "~", "+", " \n\t", " \n\t-", " \n\t~", " \n\t+",
273 "+1", "~1", "-1", "2", "8", "ff"];
274val _ = pr_ln "test15b" (concat test15b)
275val test16a =
276 List.map (chkScan StringCvt.OCT)
277 [("2071", 1081),
278 (" \n\t2071crap", 1081),
279 ("0w2071", 1081),
280 (" \n\t0w2071crap", 1081),
281 ("0", 0),
282 ("0w", 0),
283 ("0W1", 0),
284 ("0w ", 0),
285 ("0wx", 0),
286 ("0wX", 0),
287 ("0wx1", 0),
288 ("0wX1", 0),
289 ("0wx ", 0),
290 ("0wX ", 0)];
291val _ = pr_ln "test16a" (concat test16a)
292val test16b =
293 List.map (fn s => case StringCvt.scanString (scan StringCvt.OCT) s
294 of NONE => "OK" | _ => "WRONG")
295 ["", "-", "~", "+", " \n\t", " \n\t-", " \n\t~", " \n\t+",
296 "+1", "~1", "-1", "8", "ff"];
297val _ = pr_ln "test16b" (concat test16b)
298val test17a =
299 List.map (chkScan StringCvt.HEX)
300 [("20Af", 8367), (" \n\t20AfGrap", 8367),
301 ("0wx20Af", 8367), (" \n\t0wx20AfGrap", 8367),
302 ("0wX20Af", 8367), (" \n\t0wX20AfGrap", 8367),
303 ("0x20Af", 8367), (" \n\t0x20AfGrap", 8367),
304 ("0X20Af", 8367), (" \n\t0X20AfGrap", 8367),
305 ("0", 0),
306 ("0w", 0),
307 ("0w ", 0),
308 ("0w1", 0 (*1*)),
309 ("0W1", 0),
310 ("0wx", 0),
311 ("0wX", 0),
312 ("0wx1", 1),
313 ("0wX1", 1)];
314val _ = pr_ln "test17a" (concat test17a)
315val test17b =
316 List.map (fn s => case StringCvt.scanString (scan StringCvt.HEX) s
317 of NONE => "OK" | _ => "WRONG")
318 ["", "-", "~", "+", " \n\t", " \n\t-", " \n\t~", " \n\t+",
319 "+1", "~1", "-1"];
320val _ = pr_ln "test17b" (concat test17b)
321end;
322
323local
324 fun fromToString i =
325 fromString (toString (fromInt i)) = SOME (fromInt i);
326
327 fun scanFmt radix i =
328 let val w = fromInt i
329 val s = fmt radix w
330 in StringCvt.scanString (scan radix) s = SOME w end;
331
332in
333val test18 =
334 check'(fn _ => range (0, 1200) fromToString);
335val _ = pr_ln "test18" test18
336val test19 =
337 check'(fn _ => range (0, 1200) (scanFmt StringCvt.BIN));
338val _ = pr_ln "test19" test19
339val test20 =
340 check'(fn _ => range (0, 1200) (scanFmt StringCvt.OCT));
341val _ = pr_ln "test20" test20
342val test21 =
343 check'(fn _ => range (0, 1200) (scanFmt StringCvt.DEC));
344val _ = pr_ln "test21" test21
345val test22 =
346 check'(fn _ => range (0, 1200) (scanFmt StringCvt.HEX));
347val _ = pr_ln "test22" test22
348end
349end;