1 type word8
= Word8.word
3 (* Auxiliary functions for test cases
*)
7 fun check b
= if b
then "OK" else "WRONG";
8 fun check
' f
= (if f () then "OK" else "WRONG") handle _
=> "EXN";
10 fun range (from
, to
) p
=
13 (from
> to
) orelse (p from
) andalso (range (from
+1, to
) p
)
16 fun checkrange bounds
= check
o range bounds
;
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
);
22 fun tstrange s bounds
= (tst s
) o range bounds
24 (* test
/vector
.sml
-- some test cases for
Vector
25 PS
1994-12-10, 1995-06-14 *)
27 (*KILL
05/11/1997 11:05. tho
.:
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
;
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]);
47 val n
= Word8Vector
.length v
48 val n
' = Word8Vector
.length v
'
51 orelse (Word8Vector
.sub (v
, i
) = Word8Vector
.sub (v
', i
)
57 val test1
:unit
= tst
' "test1" (fn _
=> not (equal (a
,b
)));
58 val test2
:unit
= tst
' "test2" (fn _
=> equal (a
, c
));
60 val d
= tabulate(100, fn i
=> i2w (i
mod 7));
62 val test3
:unit
= tst
' "test3" (fn _
=> d sub
27 = i2w
6);
64 val test4a
:unit
= tst0
"test4a" ((tabulate(maxLen
+1, i2w
) seq
"WRONG")
65 handle Overflow
=> "OK" | Size
=> "OK" | _
=> "WRONG")
67 val test4b
:unit
= tst0
"test4b" ((tabulate(~
1, i2w
) seq
"WRONG")
68 handle Size
=> "OK" | _
=> "WRONG")
70 val test4c
:unit
= tst
' "test4c" (fn _
=> length (tabulate(0, fn i
=> i2w (i
div 0))) = 0);
72 val test5
:unit
= tst
' "test5" (fn _
=> length (fromList
[]) = 0 andalso length a
= 7);
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);
78 val e
= concat
[d
, b
, d
];
80 val test7
:unit
= tst
' "test7" (fn _
=> length e
= 203);
82 val test8
:unit
= tst
' "test8" (fn _
=> length (concat
[]) = 0);
84 val f
= extract (e
, 100, SOME
3);
86 val test9
:unit
= tst
' "test9" (fn _
=> equal (f
, b
));
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
)));
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)
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)
120 chkiter
map (fn x
=> 0w2
*x
) b (fromList
[0w88
,0w110
,0w132
], 0w66
)
123 chkiteri
mapi (fn x
=> 0w2
*x
) (b
, 0, NONE
) (fromList
[0w88
,0w110
,0w132
], 2)
125 chkiteri
mapi (fn x
=> 0w2
*x
) (b
, 1, NONE
) (fromList
[0w110
,0w132
], 2)
127 chkiteri
mapi (fn x
=> 0w2
*x
) (b
, 1, SOME
0) (fromList
[], ~
1)
129 chkiteri
mapi (fn x
=> 0w2
*x
) (b
, 1, SOME
1) (fromList
[0w110
], 1)
131 chkiteri
mapi (fn x
=> 0w2
*x
) (b
, 3, NONE
) (fromList
[], ~
1)
134 tst0
"test11f" ((mapi #
2 (b
, 0, SOME
4) seq
"WRONG")
135 handle Subscript
=> "OK" | _
=> "WRONG")
137 tst0
"test11g" ((mapi #
2 (b
, 3, SOME
1) seq
"WRONG")
138 handle Subscript
=> "OK" | _
=> "WRONG")
140 tst0
"test11h" ((mapi #
2 (b
, 4, SOME
0) seq
"WRONG")
141 handle Subscript
=> "OK" | _
=> "WRONG")
143 tst0
"test11i" ((mapi #
2 (b
, 4, NONE
) seq
"WRONG")
144 handle Subscript
=> "OK" | _
=> "WRONG")