| 1 |
type word8 = Word8.word |
| 2 |
|
| 3 |
(* Auxiliary functions for test cases *) |
| 4 |
|
| 5 |
infix 1 seq |
| 6 |
fun e1 seq e2 = e2; |
| 7 |
fun check b = if b then "OK" else "WRONG"; |
| 8 |
fun check' f = (if f () then "OK" else "WRONG") handle _ => "EXN"; |
| 9 |
|
| 10 |
fun range (from, to) p = |
| 11 |
let open Int |
| 12 |
in |
| 13 |
(from > to) orelse (p from) andalso (range (from+1, to) p) |
| 14 |
end; |
| 15 |
|
| 16 |
fun checkrange bounds = check o range bounds; |
| 17 |
|
| 18 |
fun tst0 s s' = print (s ^ " \t" ^ s' ^ "\n"); |
| 19 |
fun tst s b = tst0 s (check b); |
| 20 |
fun tst' s f = tst0 s (check' f); |
| 21 |
|
| 22 |
fun tstrange s bounds = (tst s) o range bounds |
| 23 |
|
| 24 |
(* test/vector.sml -- some test cases for Vector |
| 25 |
PS 1994-12-10, 1995-06-14 *) |
| 26 |
|
| 27 |
(*KILL 05/11/1997 11:05. tho.: |
| 28 |
use "auxil.sml"; |
| 29 |
*) |
| 30 |
|
| 31 |
local |
| 32 |
open Word8Vector; |
| 33 |
fun extract (vec, s, l) = |
| 34 |
Word8VectorSlice.vector (Word8VectorSlice.slice (vec, s, l)) |
| 35 |
fun mapi f (vec, s, l) = |
| 36 |
Word8VectorSlice.mapi (fn (i,x) => f (i+s,x)) (Word8VectorSlice.slice (vec, s, l)) |
| 37 |
val i2w = Word8.fromInt; |
| 38 |
infix 9 sub; |
| 39 |
in |
| 40 |
|
| 41 |
val a = fromList (List.map i2w [0,1,2,3,4,5,6]); |
| 42 |
val b = fromList (List.map i2w [44,55,66]); |
| 43 |
val c = fromList (List.map i2w [0,1,2,3,4,5,6]); |
| 44 |
|
| 45 |
fun equal (v, v') = |
| 46 |
let |
| 47 |
val n = Word8Vector.length v |
| 48 |
val n' = Word8Vector.length v' |
| 49 |
fun loop i = |
| 50 |
i = n |
| 51 |
orelse (Word8Vector.sub (v, i) = Word8Vector.sub (v', i) |
| 52 |
andalso loop (i + 1)) |
| 53 |
in |
| 54 |
n = n' andalso loop 0 |
| 55 |
end |
| 56 |
|
| 57 |
val test1:unit = tst' "test1" (fn _ => not (equal (a,b))); |
| 58 |
val test2:unit = tst' "test2" (fn _ => equal (a, c)); |
| 59 |
|
| 60 |
val d = tabulate(100, fn i => i2w (i mod 7)); |
| 61 |
|
| 62 |
val test3:unit = tst' "test3" (fn _ => d sub 27 = i2w 6); |
| 63 |
|
| 64 |
val test4a:unit = tst0 "test4a" ((tabulate(maxLen+1, i2w) seq "WRONG") |
| 65 |
handle Overflow => "OK" | Size => "OK" | _ => "WRONG") |
| 66 |
|
| 67 |
val test4b:unit = tst0 "test4b" ((tabulate(~1, i2w) seq "WRONG") |
| 68 |
handle Size => "OK" | _ => "WRONG") |
| 69 |
|
| 70 |
val test4c:unit = tst' "test4c" (fn _ => length (tabulate(0, fn i => i2w (i div 0))) = 0); |
| 71 |
|
| 72 |
val test5:unit = tst' "test5" (fn _ => length (fromList []) = 0 andalso length a = 7); |
| 73 |
|
| 74 |
val test6a:unit = tst0 "test6a" ((c sub ~1 seq "WRONG") handle Subscript => "OK" | _ => "WRONG") |
| 75 |
val test6b:unit = tst0 "test6b" ((c sub 7 seq "WRONG") handle Subscript => "OK" | _ => "WRONG") |
| 76 |
val test6c:unit = tst' "test6c" (fn _ => c sub 0 = i2w 0); |
| 77 |
|
| 78 |
val e = concat [d, b, d]; |
| 79 |
|
| 80 |
val test7:unit = tst' "test7" (fn _ => length e = 203); |
| 81 |
|
| 82 |
val test8:unit = tst' "test8" (fn _ => length (concat []) = 0); |
| 83 |
|
| 84 |
val f = extract (e, 100, SOME 3); |
| 85 |
|
| 86 |
val test9:unit = tst' "test9" (fn _ => equal (f, b)); |
| 87 |
|
| 88 |
val test9a:unit = tst' "test9a" (fn _ => equal (e, extract(e, 0, SOME (length e))) |
| 89 |
andalso equal (e, extract(e, 0, NONE))); |
| 90 |
val test9b:unit = tst' "test9b" (fn _ => equal (fromList [], |
| 91 |
extract(e, 100, SOME 0))); |
| 92 |
val test9c:unit = tst0 "test9c" ((extract(e, ~1, SOME (length e)) seq "WRONG") |
| 93 |
handle Subscript => "OK" | _ => "WRONG") |
| 94 |
val test9d:unit = tst0 "test9d" ((extract(e, length e + 1, SOME 0) seq "WRONG") |
| 95 |
handle Subscript => "OK" | _ => "WRONG") |
| 96 |
val test9e:unit = tst0 "test9e" ((extract(e, 0, SOME (length e+1)) seq "WRONG") |
| 97 |
handle Subscript => "OK" | _ => "WRONG") |
| 98 |
val test9f:unit = tst0 "test9f" ((extract(e, 20, SOME ~1) seq "WRONG") |
| 99 |
handle Subscript => "OK" | _ => "WRONG") |
| 100 |
val test9g:unit = tst0 "test9g" ((extract(e, ~1, NONE) seq "WRONG") |
| 101 |
handle Subscript => "OK" | _ => "WRONG") |
| 102 |
val test9h:unit = tst0 "test9h" ((extract(e, length e + 1, NONE) seq "WRONG") |
| 103 |
handle Subscript => "OK" | _ => "WRONG") |
| 104 |
val test9i:unit = tst' "test9i" (fn _ => equal (fromList [], extract (e, length e, SOME 0)) |
| 105 |
andalso equal (fromList [], extract(e, length e, NONE))); |
| 106 |
|
| 107 |
fun chkiter iter f vec (res', last') = |
| 108 |
tst' "test_chkiter" (fn _ => |
| 109 |
let val last = ref (0w255:word8) |
| 110 |
val res = iter (fn x => (last := x; f x)) vec |
| 111 |
in equal (res, res') andalso !last = last' end) |
| 112 |
|
| 113 |
fun chkiteri iter f vec (res', last') = |
| 114 |
tst' "test_chkiteri" (fn _ => |
| 115 |
let val last = ref ~1 |
| 116 |
val res = iter (fn (i, x) => (last := i; f x)) vec |
| 117 |
in equal (res, res') andalso !last = last' end) |
| 118 |
|
| 119 |
val test10a:unit = |
| 120 |
chkiter map (fn x => 0w2*x) b (fromList [0w88,0w110,0w132], 0w66) |
| 121 |
|
| 122 |
val test11a:unit = |
| 123 |
chkiteri mapi (fn x => 0w2*x) (b, 0, NONE) (fromList [0w88,0w110,0w132], 2) |
| 124 |
val test11b:unit = |
| 125 |
chkiteri mapi (fn x => 0w2*x) (b, 1, NONE) (fromList [0w110,0w132], 2) |
| 126 |
val test11c:unit = |
| 127 |
chkiteri mapi (fn x => 0w2*x) (b, 1, SOME 0) (fromList [], ~1) |
| 128 |
val test11d:unit = |
| 129 |
chkiteri mapi (fn x => 0w2*x) (b, 1, SOME 1) (fromList [0w110], 1) |
| 130 |
val test11e:unit = |
| 131 |
chkiteri mapi (fn x => 0w2*x) (b, 3, NONE) (fromList [], ~1) |
| 132 |
|
| 133 |
val test11f:unit = |
| 134 |
tst0 "test11f" ((mapi #2 (b, 0, SOME 4) seq "WRONG") |
| 135 |
handle Subscript => "OK" | _ => "WRONG") |
| 136 |
val test11g:unit = |
| 137 |
tst0 "test11g" ((mapi #2 (b, 3, SOME 1) seq "WRONG") |
| 138 |
handle Subscript => "OK" | _ => "WRONG") |
| 139 |
val test11h:unit = |
| 140 |
tst0 "test11h" ((mapi #2 (b, 4, SOME 0) seq "WRONG") |
| 141 |
handle Subscript => "OK" | _ => "WRONG") |
| 142 |
val test11i:unit = |
| 143 |
tst0 "test11i" ((mapi #2 (b, 4, NONE) seq "WRONG") |
| 144 |
handle Subscript => "OK" | _ => "WRONG") |
| 145 |
end; |