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
23 (* test
/int.sml
-- here we test only the `exotic
' operations
24 PS
1995-02-25, 1996-07-02 *)
26 (*KILL
05/11/1997 10:59. tho
.:
30 val _
= print
"\nFile int.sml: Testing structure Int...\n"
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
);
39 val test1a
= divmod
"test1a" (10, 3, 3, 1);
40 val test1b
= divmod
"test1b" (~
10, 3, ~
4, 2);
41 val test1c
= divmod
"test1c" (~
10, ~
3, 3, ~
1);
42 val test1d
= divmod
"test1d" (10, ~
3, ~
4, ~
2);
44 val test2a
= quotrem
"test2a" (10, 3, 3, 1);
45 val test2b
= quotrem
"test2b" (~
10, 3, ~
3, ~
1);
46 val test2c
= quotrem
"test2c" (~
10, ~
3, 3, ~
1);
47 val test2d
= quotrem
"test2d" (10, ~
3, ~
3, 1);
49 val test3
= tst
"test3" (max(~
5, 2) = 2 andalso max(5, 2) = 5);
50 val test4
= tst
"test4" (min(~
5, 3) = ~
5 andalso min(5, 2) = 2);
52 val test5
= tst
"test5" (sign ~
57 = ~
1 andalso sign
99 = 1 andalso sign
0 = 0);
53 val test6
= tst
"test6" (sameSign(~
255, ~
256) andalso sameSign(255, 256)
54 andalso sameSign(0, 0));
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"
69 fun chkScan fmt
= chk (StringCvt.scanString (scan fmt
))
72 List.map (chk fromString
)
77 (" \n\t10789crap", 10789),
78 (" \n\t+10789crap", 10789),
79 (" \n\t~10789crap", ~
10789),
80 (" \n\t-10789crap", ~
10789),
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"];
94 List.map (chkScan
StringCvt.DEC
)
99 (" \n\t10789crap", 10789),
100 (" \n\t+10789crap", 10789),
101 (" \n\t~10789crap", ~
10789),
102 (" \n\t-10789crap", ~
10789),
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"];
117 List.map (chkScan
StringCvt.BIN
)
122 (" \n\t10010crap", 18),
123 (" \n\t+10010crap", 18),
124 (" \n\t~10010crap", ~
18),
125 (" \n\t-10010crap", ~
18),
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"];
140 List.map (chkScan
StringCvt.OCT
)
145 (" \n\t2071crap", 1081),
146 (" \n\t+2071crap", 1081),
147 (" \n\t~2071crap", ~
1081),
148 (" \n\t-2071crap", ~
1081),
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"];
163 List.map (chkScan
StringCvt.HEX
)
168 (" \n\t20AfGrap", 8367),
169 (" \n\t+20AfGrap", 8367),
170 (" \n\t~20AfGrap", ~
8367),
171 (" \n\t-20AfGrap", ~
8367),
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"];
199 fromString (toString i
) = SOME i
;
201 fun scanFmt radix i
=
202 StringCvt.scanString (scan radix
) (fmt radix i
) = SOME i
;
206 tst
' "test18" (fn _
=> range (~
1200, 1200) fromToString
);
209 tst
' "test19" (fn _
=> range (~
1200, 1200) (scanFmt
StringCvt.BIN
));
212 tst
' "test20" (fn _
=> range (~
1200, 1200) (scanFmt
StringCvt.OCT
));
215 tst
' "test21" (fn _
=> range (~
1200, 1200) (scanFmt
StringCvt.DEC
));
218 tst
' "test22" (fn _
=> range (~
1200, 1200) (scanFmt
StringCvt.HEX
));