1 (* Auxiliary functions for test cases
*)
5 fun check b
= if b
then "OK" else "WRONG";
6 fun check
' f
= (if f () then "OK" else "WRONG") handle _
=> "EXN";
8 fun range (from
, to
) p
=
11 (from
> to
) orelse (p from
) andalso (range (from
+1, to
) p
)
14 fun checkrange bounds
= check
o range bounds
;
16 fun tst0 s s
' = print (s ^
" \t" ^ s
' ^
"\n");
17 fun tst s b
= tst0
s (check b
);
18 fun tst
' s f
= tst0
s (check
' f
);
20 fun tstrange s bounds
= (tst s
) o range bounds
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
26 modified to work for
Int.precision
= SOME
32 -- ME
1998-10-07
29 (*KILL
05/11/1997 11:04. tho
.:
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
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
;
45 fun pr_ln s s
' = print (s ^
": " ^ s
' ^
"\n")
48 val test1
= checkrange (0, 1025)
49 (fn i
=> i
= w2i (i2w i
));
50 val _
= pr_ln
"test1" test1
52 val test3
= checkrange (~
1000, 1000)
53 (fn i
=> i
= toIntX (i2w i
));
54 val _
= pr_ln
"test3" test3
56 val test5a
= checkrange (0,15)
57 (fn i
=> (i
+960) div 2 * 2 + 1
58 = w2i (orb (i2w i
, i2w
961)));
59 val _
= pr_ln
"test5a" test5a
60 val test5b
= checkrange (0,513)
61 (fn i
=> i
= w2i (orb (i2w i
, i2w i
)));
62 val _
= pr_ln
"test5b" test5b
63 val test6a
= checkrange (0,15)
64 (fn i
=> i
div 2 * 2 = w2i (andb (i2w i
, i2w ~
2)));
65 val _
= pr_ln
"test6a" test6a
66 val test6b
= checkrange (0,513)
67 (fn i
=> i
= w2i (andb (i2w i
, i2w i
)));
68 val _
= pr_ln
"test6b" test6b
69 val test7a
= checkrange (0,15)
70 (fn i
=> i
+960 = w2i (xorb (i2w i
, i2w
960)));
71 val _
= pr_ln
"test7a" test7a
72 val test7b
= checkrange (0, 513)
73 (fn i
=> 0 = w2i (xorb (i2w i
, i2w i
)));
74 val _
= pr_ln
"test7b" test7b
75 val test8a
= check(~
1 = w2i (notb (i2w
0)));
76 val _
= pr_ln
"test8a" test8a
77 val test8b
= check (0 = w2i (notb (i2w ~
1)));
78 val _
= pr_ln
"test8b" test8b
79 val maxposint
= valOf
Int.maxInt
;
80 val maxnegint
= (Int.~ maxposint
)-1;
82 | pwr2 n
= 2 * pwr2 (n
-1);
84 | rwp i n
= rwp
i (n
-1) div 2;
86 val test9a
= checkrange (0,29)
87 (fn k
=> pwr2 k
= w2i (<< (i2w
1, i2w k
)));
88 val _
= pr_ln
"test9a" test9a
89 val test9b
= checkrange (32,65)
90 (fn k
=> 0 = w2i (<< (i2w
1, i2w k
)));
91 val _
= pr_ln
"test9b" test9b
92 val test9c
= check (maxnegint
= w2i (<< (i2w
1, i2w
31)));
93 val _
= pr_ln
"test9c" test9c
94 val test9d
= checkrange (0, 1025)
95 (fn i
=> 2 * i
= w2i (<< (i2w i
, i2w
1)));
96 val _
= pr_ln
"test9d" test9d
97 val test9e
= checkrange (0, 1025)
98 (fn i
=> i
div 2 = w2i (>> (i2w i
, i2w
1)));
99 val _
= pr_ln
"test9e" test9e
100 val test9f
= checkrange (0,65)
101 (fn k
=> rwp maxposint k
= w2i (>> (i2w maxposint
, i2w k
)));
102 val _
= pr_ln
"test9f" test9f
103 val test9g
= checkrange (32,65)
104 (fn k
=> 0 = w2i (<< (i2w ~
1, i2w k
)));
105 val _
= pr_ln
"test9g" test9g
106 val test9h
= checkrange (1,65)
107 (fn k
=> 0 = w2i (>> (i2w
1, i2w k
)));
108 val _
= pr_ln
"test9h" test9h
110 val test10a
= checkrange (1,65)
111 (fn k
=> 0 = w2i (~
>> (i2w
1, i2w k
)));
112 val _
= pr_ln
"test10a" test10a
113 val test10b
= checkrange (1,65)
114 (fn k
=> ~
1 = w2i (~
>> (i2w ~
1, i2w k
)));
115 val _
= pr_ln
"test10b" test10b
116 val test10c
= checkrange (~
513, 513)
117 (fn i
=> i
div 2 = toIntX (~
>> (i2w i
, i2w
1)));
118 val _
= pr_ln
"test10c" test10c
119 val test10d
= checkrange (0,65)
120 (fn k
=> rwp maxnegint k
= toIntX (~
>> (i2w maxnegint
, i2w k
)));
121 val _
= pr_ln
"test10d" test10d
125 val test11a
= check (i2w
256 > i2w
255);
126 val _
= pr_ln
"test11a" test11a
127 val test11b
= check (i2w
0 < i2w ~
1);
128 val _
= pr_ln
"test11b" test11b
129 val test11c
= check (i2w maxposint
>= i2w maxposint
);
130 val _
= pr_ln
"test11c" test11c
131 val test11d
= check (i2w maxnegint
>= i2w
127);
132 val _
= pr_ln
"test11d" test11d
133 val test11e
= check (i2w
1 <= i2w
1);
134 val _
= pr_ln
"test11e" test11e
135 val test11f
= check (i2w
0 <= i2w
1);
136 val _
= pr_ln
"test11f" test11f
137 val test11g
= check (i2w
0 < i2w maxposint
);
138 val _
= pr_ln
"test11g" test11g
139 val test11h
= check (i2w maxposint
< i2w maxnegint
);
140 val _
= pr_ln
"test11h" test11h
141 val test11i
= check (i2w maxnegint
< i2w ~
1);
142 val _
= pr_ln
"test11i" test11i
148 val test12a
= checkrange(0, 300) (fn k
=> w2i (i2w k
+ i2w
17) = add(k
, 17));
149 val _
= pr_ln
"test12a" test12a
150 val test12b
= checkrange(0, 300) (fn k
=> w2i (i2w k
- i2w
17) = sub(k
, 17));
151 val _
= pr_ln
"test12b" test12b
152 val test12c
= checkrange(0, 300) (fn k
=> w2i (i2w k
* i2w
17) = mul(k
, 17));
153 val _
= pr_ln
"test12c" test12c
154 val test12d
= checkrange(0, 300)
155 (fn k
=> w2i (i2w k
div i2w
17) = idiv(k
, 17));
156 val _
= pr_ln
"test12d" test12d
157 val test12e
= checkrange(0, 300)
158 (fn k
=> w2i (i2w k
mod i2w
17) = imod(k
, 17));
159 val _
= pr_ln
"test12e" test12e
160 val test12f
= checkrange(0, 300)
161 (fn k
=> w2i (i2w k
+ i2w maxnegint
) = add(k
, maxnegint
));
162 val _
= pr_ln
"test12f" test12f
163 val test12g
= checkrange(0, 300)
164 (fn k
=> w2i (i2w maxnegint
- i2w k
- i2w
1) = sub(maxposint
,k
));
165 val _
= pr_ln
"test12g" test12g
166 val test12h
= checkrange(0, 300)
167 (fn k
=> w2i (i2w k
* i2w maxnegint
) = mul(imod(k
, 2), maxnegint
));
168 val _
= pr_ln
"test12h" test12h
169 val test12i
= checkrange(0, 300)
170 (fn k
=> w2i (i2w k
* i2w maxposint
+ i2w k
) = mul(imod(k
, 2), maxnegint
));
171 val _
= pr_ln
"test12i" test12i
172 val test12j
= checkrange(0, 300)
173 (fn k
=> w2i (i2w k
div i2w ~
1) = 0);
174 val _
= pr_ln
"test12j" test12j
175 val test12k
= checkrange(0, 300)
176 (fn k
=> w2i (i2w k
mod i2w ~
1) = k
);
177 val _
= pr_ln
"test12k" test12k
178 val test12l
= check(w2i (i2w maxposint
+ i2w
1) = maxnegint
);
179 val _
= pr_ln
"test12l" test12l
180 val test12m
= check(w2i (i2w maxnegint
- i2w
1) = maxposint
);
181 val _
= pr_ln
"test12m" test12m
182 val test12n
= check(w2i (i2w ~
1 div i2w
2) = maxposint
);
183 val _
= pr_ln
"test12n" test12n
184 val test12o
= check(w2i (i2w ~
1 mod i2w
2) = 1);
185 val _
= pr_ln
"test12o" test12o
186 val test12p
= check(w2i (i2w ~
1 div i2w
100) = idiv(maxposint
, 50));
187 val _
= pr_ln
"test12p" test12p
189 val test12q
= check(w2i (i2w ~
1 mod i2w
10) = 7);
190 val _
= pr_ln
"test12q" test12q
192 val test12r
= (i2w
17 div i2w
0 seq
"WRONG")
193 handle Div
=> "OK" | _
=> "WRONG";
194 val _
= pr_ln
"test12r" test12r
195 val test12s
= (i2w
17 mod i2w
0 seq
"WRONG")
196 handle Div
=> "OK" | _
=> "WRONG";
197 val _
= pr_ln
"test12s" test12s
201 SOME res
=> res
= i2w r
204 fun chkScan fmt
= chk (StringCvt.scanString (scan fmt
))
207 List.map (chk fromString
)
209 (" \n\t20AfGrap", 8367),
210 ("0w20Af", 0 (*8367*)),
211 (" \n\t0w20AfGrap", 0 (*8367*)),
222 val _
= pr_ln
"test13a" (concat test13a
)
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"];
227 val _
= pr_ln
"test13b" (concat test13b
)
230 List.map (chkScan
StringCvt.DEC
)
232 (" \n\t10789crap", 10789),
234 (" \n\t0w10789crap", 10789),
245 val _
= pr_ln
"test14a" (concat test14a
)
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"];
251 val _
= pr_ln
"test14b" (concat test14b
)
253 List.map (chkScan
StringCvt.BIN
)
255 (" \n\t10010crap", 18),
257 (" \n\t0w10010crap", 18),
268 val _
= pr_ln
"test15a" (concat test15a
)
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"];
274 val _
= pr_ln
"test15b" (concat test15b
)
276 List.map (chkScan
StringCvt.OCT
)
278 (" \n\t2071crap", 1081),
280 (" \n\t0w2071crap", 1081),
291 val _
= pr_ln
"test16a" (concat test16a
)
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"];
297 val _
= pr_ln
"test16b" (concat test16b
)
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),
314 val _
= pr_ln
"test17a" (concat test17a
)
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+",
320 val _
= pr_ln
"test17b" (concat test17b
)
325 fromString (toString (fromInt i
)) = SOME (fromInt i
);
327 fun scanFmt radix i
=
328 let val w
= fromInt i
330 in StringCvt.scanString (scan radix
) s
= SOME w
end;
334 check
'(fn _
=> range (0, 1200) fromToString
);
335 val _
= pr_ln
"test18" test18
337 check
'(fn _
=> range (0, 1200) (scanFmt
StringCvt.BIN
));
338 val _
= pr_ln
"test19" test19
340 check
'(fn _
=> range (0, 1200) (scanFmt
StringCvt.OCT
));
341 val _
= pr_ln
"test20" test20
343 check
'(fn _
=> range (0, 1200) (scanFmt
StringCvt.DEC
));
344 val _
= pr_ln
"test21" test21
346 check
'(fn _
=> range (0, 1200) (scanFmt
StringCvt.HEX
));
347 val _
= pr_ln
"test22" test22