| 1 |
(* Auxiliary functions for test cases *) |
| 2 |
|
| 3 |
infix 1 seq |
| 4 |
fun e1 seq e2 = e2; |
| 5 |
fun check b = if b then "OK" else "WRONG"; |
| 6 |
fun check' f = (if f () then "OK" else "WRONG") handle _ => "EXN"; |
| 7 |
|
| 8 |
fun 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 |
|
| 14 |
fun checkrange bounds = check o range bounds; |
| 15 |
|
| 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); |
| 19 |
|
| 20 |
fun 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.: |
| 30 |
use "auxil.sml"; |
| 31 |
*) |
| 32 |
|
| 33 |
local |
| 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") |
| 46 |
in |
| 47 |
|
| 48 |
val test1 = checkrange (0, 1025) |
| 49 |
(fn i => i = w2i (i2w i)); |
| 50 |
val _ = pr_ln "test1" test1 |
| 51 |
|
| 52 |
val test3 = checkrange (~1000, 1000) |
| 53 |
(fn i => i = toIntX (i2w i)); |
| 54 |
val _ = pr_ln "test3" test3 |
| 55 |
|
| 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; |
| 81 |
fun pwr2 0 = 1 |
| 82 |
| pwr2 n = 2 * pwr2 (n-1); |
| 83 |
fun rwp i 0 = i |
| 84 |
| rwp i n = rwp i (n-1) div 2; |
| 85 |
|
| 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 |
| 109 |
|
| 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 |
| 122 |
local |
| 123 |
open Word |
| 124 |
in |
| 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 |
| 143 |
end; |
| 144 |
|
| 145 |
local |
| 146 |
open Word |
| 147 |
in |
| 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 |
| 188 |
(*31bit |
| 189 |
val test12q = check(w2i (i2w ~1 mod i2w 10) = 7); |
| 190 |
val _ = pr_ln "test12q" test12q |
| 191 |
*) |
| 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 |
| 198 |
fun chk f (s, r) = |
| 199 |
check'(fn _ => |
| 200 |
case f s of |
| 201 |
SOME res => res = i2w r |
| 202 |
| NONE => false) |
| 203 |
|
| 204 |
fun chkScan fmt = chk (StringCvt.scanString (scan fmt)) |
| 205 |
|
| 206 |
val 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)]; |
| 222 |
val _ = pr_ln "test13a" (concat test13a) |
| 223 |
val 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"]; |
| 227 |
val _ = pr_ln "test13b" (concat test13b) |
| 228 |
|
| 229 |
val 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)]; |
| 245 |
val _ = pr_ln "test14a" (concat test14a) |
| 246 |
val 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"]; |
| 251 |
val _ = pr_ln "test14b" (concat test14b) |
| 252 |
val 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)]; |
| 268 |
val _ = pr_ln "test15a" (concat test15a) |
| 269 |
val 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"]; |
| 274 |
val _ = pr_ln "test15b" (concat test15b) |
| 275 |
val 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)]; |
| 291 |
val _ = pr_ln "test16a" (concat test16a) |
| 292 |
val 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"]; |
| 297 |
val _ = pr_ln "test16b" (concat test16b) |
| 298 |
val 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)]; |
| 314 |
val _ = pr_ln "test17a" (concat test17a) |
| 315 |
val 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"]; |
| 320 |
val _ = pr_ln "test17b" (concat test17b) |
| 321 |
end; |
| 322 |
|
| 323 |
local |
| 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 |
|
| 332 |
in |
| 333 |
val test18 = |
| 334 |
check'(fn _ => range (0, 1200) fromToString); |
| 335 |
val _ = pr_ln "test18" test18 |
| 336 |
val test19 = |
| 337 |
check'(fn _ => range (0, 1200) (scanFmt StringCvt.BIN)); |
| 338 |
val _ = pr_ln "test19" test19 |
| 339 |
val test20 = |
| 340 |
check'(fn _ => range (0, 1200) (scanFmt StringCvt.OCT)); |
| 341 |
val _ = pr_ln "test20" test20 |
| 342 |
val test21 = |
| 343 |
check'(fn _ => range (0, 1200) (scanFmt StringCvt.DEC)); |
| 344 |
val _ = pr_ln "test21" test21 |
| 345 |
val test22 = |
| 346 |
check'(fn _ => range (0, 1200) (scanFmt StringCvt.HEX)); |
| 347 |
val _ = pr_ln "test22" test22 |
| 348 |
end |
| 349 |
end; |