Import Upstream version 20180207
[hcoop/debian/mlton.git] / regression / int.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
23(* test/int.sml -- here we test only the `exotic' operations
24 PS 1995-02-25, 1996-07-02 *)
25
26(*KILL 05/11/1997 10:59. tho.:
27use "auxil.sml";
28*)
29
30val _ = print "\nFile int.sml: Testing structure Int...\n"
31
32local
33 open Int
34 infix 7 quot rem
35 fun divmod s (i, d, q, r) = tst s (i div d = q andalso i mod d = r);
36 fun quotrem s (i, d, q, r) = tst s (i quot d = q andalso i rem d = r);
37in
38
39val test1a = divmod "test1a" (10, 3, 3, 1);
40val test1b = divmod "test1b" (~10, 3, ~4, 2);
41val test1c = divmod "test1c" (~10, ~3, 3, ~1);
42val test1d = divmod "test1d" (10, ~3, ~4, ~2);
43
44val test2a = quotrem "test2a" (10, 3, 3, 1);
45val test2b = quotrem "test2b" (~10, 3, ~3, ~1);
46val test2c = quotrem "test2c" (~10, ~3, 3, ~1);
47val test2d = quotrem "test2d" (10, ~3, ~3, 1);
48
49val test3 = tst "test3" (max(~5, 2) = 2 andalso max(5, 2) = 5);
50val test4 = tst "test4" (min(~5, 3) = ~5 andalso min(5, 2) = 2);
51
52val test5 = tst "test5" (sign ~57 = ~1 andalso sign 99 = 1 andalso sign 0 = 0);
53val test6 = tst "test6" (sameSign(~255, ~256) andalso sameSign(255, 256)
54 andalso sameSign(0, 0));
55
56val test12 =
57 tst0 "test12" (case (minInt, maxInt) of
58 (SOME mi, SOME ma) => check(sign mi = ~1 andalso sign ma = 1
59 andalso sameSign(mi, ~1) andalso sameSign(ma, 1))
60 | (NONE, NONE) => "OK"
61 | _ => "WRONG")
62
63fun chk f (s, r) =
64 tst' "chk" (fn _ =>
65 case f s of
66 SOME res => res = r
67 | NONE => false)
68
69fun chkScan fmt = chk (StringCvt.scanString (scan fmt))
70
71val test13a =
72 List.map (chk fromString)
73 [("10789", 10789),
74 ("+10789", 10789),
75 ("~10789", ~10789),
76 ("-10789", ~10789),
77 (" \n\t10789crap", 10789),
78 (" \n\t+10789crap", 10789),
79 (" \n\t~10789crap", ~10789),
80 (" \n\t-10789crap", ~10789),
81 ("0w123", 0),
82 ("0W123", 0),
83 ("0x123", 0),
84 ("0X123", 0),
85 ("0wx123", 0),
86 ("0wX123", 0)];
87
88val test13b =
89 List.map (fn s => tst0 "test13b" (case fromString s of NONE => "OK" | _ => "WRONG"))
90 ["", "-", "~", "+", " \n\t", " \n\t-", " \n\t~", " \n\t+",
91 "+ 1", "~ 1", "- 1", "ff"];
92
93val test14a =
94 List.map (chkScan StringCvt.DEC)
95 [("10789", 10789),
96 ("+10789", 10789),
97 ("~10789", ~10789),
98 ("-10789", ~10789),
99 (" \n\t10789crap", 10789),
100 (" \n\t+10789crap", 10789),
101 (" \n\t~10789crap", ~10789),
102 (" \n\t-10789crap", ~10789),
103 ("0w123", 0),
104 ("0W123", 0),
105 ("0x123", 0),
106 ("0X123", 0),
107 ("0wx123", 0),
108 ("0wX123", 0)];
109
110val test14b =
111 List.map (fn s => tst0 "test14b" (case StringCvt.scanString (scan StringCvt.DEC) s
112 of NONE => "OK" | _ => "WRONG"))
113 ["", "-", "~", "+", " \n\t", " \n\t-", " \n\t~", " \n\t+",
114 "+ 1", "~ 1", "- 1", "ff"];
115
116val test15a =
117 List.map (chkScan StringCvt.BIN)
118 [("10010", 18),
119 ("+10010", 18),
120 ("~10010", ~18),
121 ("-10010", ~18),
122 (" \n\t10010crap", 18),
123 (" \n\t+10010crap", 18),
124 (" \n\t~10010crap", ~18),
125 (" \n\t-10010crap", ~18),
126 ("0w101", 0),
127 ("0W101", 0),
128 ("0x101", 0),
129 ("0X101", 0),
130 ("0wx101", 0),
131 ("0wX101", 0)];
132
133val test15b =
134 List.map (fn s => tst0 "test15b" (case StringCvt.scanString (scan StringCvt.BIN) s
135 of NONE => "OK" | _ => "WRONG"))
136 ["", "-", "~", "+", " \n\t", " \n\t-", " \n\t~", " \n\t+",
137 "+ 1", "~ 1", "- 1", "2", "8", "ff"];
138
139val test16a =
140 List.map (chkScan StringCvt.OCT)
141 [("2071", 1081),
142 ("+2071", 1081),
143 ("~2071", ~1081),
144 ("-2071", ~1081),
145 (" \n\t2071crap", 1081),
146 (" \n\t+2071crap", 1081),
147 (" \n\t~2071crap", ~1081),
148 (" \n\t-2071crap", ~1081),
149 ("0w123", 0),
150 ("0W123", 0),
151 ("0x123", 0),
152 ("0X123", 0),
153 ("0wx123", 0),
154 ("0wX123", 0)];
155
156val test16b =
157 List.map (fn s => tst0 "test16b" (case StringCvt.scanString (scan StringCvt.OCT) s
158 of NONE => "OK" | _ => "WRONG"))
159 ["", "-", "~", "+", " \n\t", " \n\t-", " \n\t~", " \n\t+",
160 "+ 1", "~ 1", "- 1", "8", "ff"];
161
162val test17a =
163 List.map (chkScan StringCvt.HEX)
164 [("20Af", 8367),
165 ("+20Af", 8367),
166 ("~20Af", ~8367),
167 ("-20Af", ~8367),
168 (" \n\t20AfGrap", 8367),
169 (" \n\t+20AfGrap", 8367),
170 (" \n\t~20AfGrap", ~8367),
171 (" \n\t-20AfGrap", ~8367),
172 ("0w123", 0),
173 ("0W123", 0),
174 ("0x", 0),
175 ("0x ", 0),
176 ("0xG", 0),
177 ("0X", 0),
178 ("0XG", 0),
179 ("0x123", 291),
180 ("0X123", 291),
181 ("-0x123", ~291),
182 ("-0X123", ~291),
183 ("~0x123", ~291),
184 ("~0X123", ~291),
185 ("+0x123", 291),
186 ("+0X123", 291),
187 ("0wx123", 0),
188 ("0wX123", 0)];
189
190val test17b =
191 List.map (fn s => tst0 "test17b" (case StringCvt.scanString (scan StringCvt.HEX) s
192 of NONE => "OK" | _ => "WRONG"))
193 ["", "-", "~", "+", " \n\t", " \n\t-", " \n\t~", " \n\t+",
194 "+ 1", "~ 1", "- 1"];
195
196
197local
198 fun fromToString i =
199 fromString (toString i) = SOME i;
200
201 fun scanFmt radix i =
202 StringCvt.scanString (scan radix) (fmt radix i) = SOME i;
203
204in
205val test18 =
206 tst' "test18" (fn _ => range (~1200, 1200) fromToString);
207
208val test19 =
209 tst' "test19" (fn _ => range (~1200, 1200) (scanFmt StringCvt.BIN));
210
211val test20 =
212 tst' "test20" (fn _ => range (~1200, 1200) (scanFmt StringCvt.OCT));
213
214val test21 =
215 tst' "test21" (fn _ => range (~1200, 1200) (scanFmt StringCvt.DEC));
216
217val test22 =
218 tst' "test22" (fn _ => range (~1200, 1200) (scanFmt StringCvt.HEX));
219end
220
221end