/
word8array.sml
194 lines (159 loc) · 5.73 KB
/
word8array.sml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
(* test/word8array.sml -- some test cases for Word8Array
PS 1994-12-21, 1995-05-11, 2000-10-24
- modified for the MLKit; mael 2005-11-28 *)
fun ptest t s = print(t ^ ": " ^ s ^ "<br>")
infix 1 seq
fun e1 seq e2 = e2;
fun check b = if b then "OK" else "WRONG";
fun check' f = (if f () then "OK" else "WRONG") handle _ => "EXN";
fun range (from, to) p =
let open Int
in
(from > to) orelse (p from) andalso (range (from+1, to) p)
end;
fun checkrange bounds p = check'(fn _ => range bounds p)
val _ = print "<h2>File word8array.sml: Testing Word8Array...</h2>";
local
open Word8Array
infix 9 sub;
val array0 = fromList [];
in
val i2w = Word8.fromInt;
val w127 = i2w 127;
val a = fromList (map i2w [0,1,2,3,4,5,6]);
val b = fromList (map i2w [44,55,66]);
val c = fromList (map i2w [0,1,2,3,4,5,6]);
val test1 =
check'(fn () => a<>c);
val _ = ptest "test1" test1
val test2 =
check'(fn () =>
array(0, w127) <> array0
andalso array(0, w127) <> tabulate(0, fn _ => w127)
andalso tabulate(0, fn _ => w127) <> fromList []
andalso array(0, w127) <> array(0, w127)
andalso tabulate(0, fn _ => w127) <> tabulate(0, fn _ => w127)
andalso fromList [] <> fromList []);
val _ = ptest "test2" test2
val d = tabulate(100, fn i => i2w (i mod 7));
val test3 = check' (fn () => d sub 27 = i2w 6);
val _ = ptest "test3" test3
val test4a = (tabulate(maxLen+1, i2w) seq "WRONG")
handle Size => "OK" | _ => "WRONG";
val _ = ptest "test4a" test4a
val test4b = (tabulate(~1, i2w) seq "WRONG")
handle Size => "OK" | _ => "WRONG";
val _ = ptest "test4b" test4b
val test4c =
check'(fn () => length (tabulate(0, fn i => i2w (i div 0))) = 0);
val _ = ptest "test4c" test4c
val test5a = check'(fn () => length (fromList []) = 0 andalso length a = 7);
val _ = ptest "test5a" test5a
val test5b = check'(fn () => length array0 = 0);
val _ = ptest "test5b" test5b
val test6a = (c sub ~1 seq "WRONG") handle Subscript => "OK" | _ => "WRONG";
val _ = ptest "test6a" test6a
val test6b = (c sub 7 seq "WRONG") handle Subscript => "OK" | _ => "WRONG";
val _ = ptest "test6b" test6b
val test6c = check'(fn () => c sub 0 = i2w 0);
val _ = ptest "test6c" test6c
val e = array(203, i2w 0);
val _ = (copy{src=d, dst=e, di=0};
copy{src=b, dst=e, di=length d};
copy{src=d, dst=e, di=length d + length b});
fun a2v a = vector a;
val ev = Word8Vector.concat [a2v d, a2v b, a2v d];
val test7 = check'(fn () => length e = 203);
val _ = ptest "test7" test7
val test8a = (update(e, ~1, w127); "WRONG")
handle Subscript => "OK" | _ => "WRONG";
val _ = ptest "test8a" test8a
val test8b = (update(e, length e, w127); "WRONG")
handle Subscript => "OK" | _ => "WRONG";
val _ = ptest "test8b" test8b
val f = Word8ArraySlice.vector(Word8ArraySlice.slice(e, 100, SOME 3));
val test9 = check'(fn () => f = a2v b);
val _ = ptest "test9" test9
val test9a =
check'(fn () => ev = vector e);
val _ = ptest "test9a" test9a
val test9b =
check'(fn () => Word8Vector.fromList [] = vector array0);
val _ = ptest "test9b" test9b
val _ = copy{src=e, dst=e, di=0};
val g = array(203, w127);
val _ = copy{src=e, dst=g, di=0};
val test10a = check'(fn () => ev = vector g);
val _ = ptest "test10a" test10a
val test10b =
check'(fn () => (copy{src=array0, dst=array0, di=0};
array0 <> array(0, 0w99)));
val _ = ptest "test10b" test10b
val test10c =
check'(fn () => (copy{src=array0, dst=g, di=0};
ev = vector g));
val _ = ptest "test10c" test10c
val test10d =
check'(fn () => (copy{src=array0, dst=g, di=203};
ev = vector g));
val _ = ptest "test10d" test10d
val test10e =
check'(fn () => (copy{src=array0, dst=g, di=1};
ev = vector g));
val _ = ptest "test10e" test10e
val test11a = (copy{src=g, dst=g, di=1}; "WRONG")
handle Subscript => "OK" | _ => "WRONG"
val _ = ptest "test11a" test11a
val test11b = (copy{src=g, dst=g, di= 202}; "WRONG")
handle Subscript => "OK" | _ => "WRONG"
val _ = ptest "test11b" test11b
val test11c = (copy{src=b, dst=g, di = ~1}; "WRONG")
handle Subscript => "OK" | _ => "WRONG"
val _ = ptest "test11c" test11c
val test11d = (copy{src=b, dst=g, di=203}; "WRONG")
handle Subscript => "OK" | _ => "WRONG"
val _ = ptest "test11d" test11d
val test11e = check'(fn () => ev = vector g);
val _ = ptest "test11e" test11e
val test12 =
check'(fn _ =>
let fun invcompare (c1, c2) = Word8.compare(c2, c1)
val fromString =
fromList o List.map (Word8.fromInt o ord) o explode
fun coll s1 s2 =
collate invcompare (fromString s1, fromString s2)
in
coll "" "" = EQUAL
andalso coll "" " " = LESS
andalso coll " " "" = GREATER
andalso coll "ABCD" "ABCD" = EQUAL
andalso coll "ABCD" "ABCD " = LESS
andalso coll "ABCD " "ABCD" = GREATER
andalso coll "B" "ABCD" = LESS
andalso coll "ABCD" "B" = GREATER
andalso coll "CCCB" "CCCABCD" = LESS
andalso coll "CCCABCD" "CCCB" = GREATER
andalso coll "CCCB" "CCCA" = LESS
andalso coll "CCCA" "CCCB" = GREATER
end)
val _ = ptest "test12" test12
val test13 =
check'(fn _ =>
NONE = find (fn i => i > 0w7) a
andalso SOME 0w5 = find (fn i => i > 0w4) a
andalso NONE = find (fn _ => true) (fromList []));
val _ = ptest "test13" test13
val test14 =
check'(fn _ =>
not (exists (fn i => i > 0w7) a)
andalso exists (fn i => i > 0w4) a
andalso not (exists (fn _ => true) (fromList [])));
val _ = ptest "test14" test14
val test15 =
check'(fn _ =>
not (all (fn i => i < 0w6) a)
andalso all (fn i => i < 0w7) a
andalso all (fn _ => false) (fromList []));
val _ = ptest "test15" test15
val _ = print "Test ended."
end;