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
22 (* test
/array
.sml
-- some test cases for Array
23 PS
1994-12-10, 1995-06-14, 1995-11-07 *)
25 val _
= print
"Testing Array...\n"
30 val array0
: int array
= fromList
[]
31 fun extract (arr
, s
, l
) = ArraySlice
.vector (ArraySlice
.slice (arr
, s
, l
))
32 val copy
= fn {src
, si
, len
, dst
, di
} =>
33 ArraySlice
.copy
{src
= ArraySlice
.slice (src
, si
, len
),
35 fun foldli f
b (arr
, s
, l
) =
36 ArraySlice
.foldli (fn (i
,x
,y
) => f (i
+s
,x
,y
)) b (ArraySlice
.slice (arr
, s
, l
))
37 fun foldri f
b (arr
, s
, l
) =
38 ArraySlice
.foldri (fn (i
,x
,y
) => f (i
+s
,x
,y
)) b (ArraySlice
.slice (arr
, s
, l
))
39 fun appi
f (arr
, s
, l
) =
40 ArraySlice
.appi (fn (i
,x
) => f (i
+s
,x
)) (ArraySlice
.slice (arr
, s
, l
))
41 fun modifyi
f (arr
, s
, l
) =
42 ArraySlice
.modifyi (fn (i
,x
) => f (i
+s
,x
)) (ArraySlice
.slice (arr
, s
, l
))
43 fun findi
f (arr
, s
, l
) =
44 ArraySlice
.findi (fn (i
,x
) => f (i
+s
,x
)) (ArraySlice
.slice (arr
, s
, l
))
47 val a
= fromList
[1,11,21,31,41,51,61];
48 val b
= fromList
[441,551,661];
49 val c
= fromList
[1,11,21,31,41,51,61];
51 val test1
= tst
' "test1" (fn () => a
<>c
);
54 tst
' "test2" (fn () =>
55 array(0, 11) <> array0
56 andalso array(0,()) <> tabulate(0, fn _
=> ())
57 andalso tabulate(0, fn _
=> ()) <> fromList
[]
58 andalso fromList
[] <> fromList
[]
59 andalso array(0, ()) <> array(0, ())
60 andalso tabulate(0, fn _
=> ()) <> tabulate(0, fn _
=> ()));
62 val d
= tabulate(100, fn i
=> i
mod 7 * 10 + 1);
65 tst
' "test3" (fn () => d sub
27 = 61);
67 val test4a
= tst0
"test4a"
68 ((tabulate(maxLen
+1, fn i
=> i
) seq
"WRONG")
69 handle Overflow
=> "OK" | Size
=> "OK" | _
=> "WRONG");
71 val test4b
= tst0
"test4b"
72 ((tabulate(~
1, fn i
=> i
) seq
"WRONG")
73 handle Size
=> "OK" | _
=> "WRONG");
76 tst
' "test4c" (fn () => length (tabulate(0, fn i
=> i
div 0)) = 0);
79 tst
' "test5a" (fn () => length (fromList
[]) = 0 andalso length a
= 7);
81 tst
' "test5b" (fn () => length array0
= 0);
83 val test6a
= tst0
"test6a" ((c sub ~
1 seq
"WRONG") handle Subscript
=> "OK" | _
=> "WRONG");
84 val test6b
= tst0
"test6b" ((c sub
7 seq
"WRONG") handle Subscript
=> "OK" | _
=> "WRONG");
85 val test6c
= tst
' "test6c" (fn () => c sub
0 = 1);
87 val e
= array(203, 0);
88 val _
= (copy
{src
=d
, si
=0, dst
=e
, di
=0, len
=NONE
};
89 copy
{src
=b
, si
=0, dst
=e
, di
=length d
, len
=NONE
};
90 copy
{src
=d
, si
=0, dst
=e
, di
=length d
+ length b
, len
=NONE
});
92 fun a2v a
= extract(a
, 0, NONE
);
93 val ev
= Vector.concat
[a2v d
, a2v b
, a2v d
]; (* length e
= 203 *)
95 val test7
= tst
' "test7" (fn () => length e
= 203);
97 val test8a
= tst0
"test8a" ((update(e
, ~
1, 99) seq
"WRONG")
98 handle Subscript
=> "OK" | _
=> "WRONG");
99 val test8b
= tst0
"test8b" ((update(e
, length e
, 99) seq
"WRONG")
100 handle Subscript
=> "OK" | _
=> "WRONG");
102 val f
= extract (e
, 100, SOME
3);
104 val test9
= tst
' "test9" (fn () => f
= a2v b
);
107 tst
' "test9a" (fn () => ev
= extract(e
, 0, SOME (length e
))
108 andalso ev
= extract(e
, 0, NONE
));
110 tst
' "test9b" (fn () => Vector.fromList
[] = extract(e
, 100, SOME
0));
111 val test9c
= (extract(e
, ~
1, SOME (length e
)) seq
"WRONG")
112 handle Subscript
=> "OK" | _
=> "WRONG"
113 val test9d
= (extract(e
, length e
+1, SOME
0) seq
"WRONG")
114 handle Subscript
=> "OK" | _
=> "WRONG"
115 val test9e
= (extract(e
, 0, SOME (length e
+1)) seq
"WRONG")
116 handle Subscript
=> "OK" | _
=> "WRONG"
117 val test9f
= (extract(e
, 20, SOME ~
1) seq
"WRONG")
118 handle Subscript
=> "OK" | _
=> "WRONG"
119 val test9g
= (extract(e
, ~
1, NONE
) seq
"WRONG")
120 handle Subscript
=> "OK" | _
=> "WRONG"
121 val test9h
= (extract(e
, length e
+1, NONE
) seq
"WRONG")
122 handle Subscript
=> "OK" | _
=> "WRONG"
124 tst
' "test9i" (fn () => a2v (fromList
[]) = extract(e
, length e
, SOME
0)
125 andalso a2v (fromList
[]) = extract(e
, length e
, NONE
));
127 tst
' "test9j" (fn () => extract(e
, 3, SOME(length e
- 3)) = extract(e
, 3, NONE
));
129 val _
= copy
{src
=e
, si
=0, dst
=e
, di
=0, len
=NONE
};
130 val g
= array(203, 9999999);
131 val _
= copy
{src
=e
, si
=0, dst
=g
, di
=0, len
=NONE
};
133 val test10a
= tst
' "test10a" (fn () => ev
= extract(e
, 0, SOME (length e
))
134 andalso ev
= extract(e
, 0, NONE
));
135 val test10b
= tst
' "test10b" (fn () => ev
= extract(g
, 0, SOME (length g
))
136 andalso ev
= extract(g
, 0, NONE
));
138 val _
= copy
{src
=g
, si
=203, dst
=g
, di
=0, len
=SOME
0};
139 val test10c
= tst
' "test10c" (fn () => ev
= extract(g
, 0, SOME (length g
)));
141 val _
= copy
{src
=g
, si
=0, dst
=g
, di
=203, len
=SOME
0};
142 val test10d
= tst
' "test10d" (fn () => ev
= extract(g
, 0, SOME (length g
)));
144 val _
= copy
{src
=g
, si
=0, dst
=g
, di
=1, len
=SOME (length g
-1)};
145 val test10e
= tst
' "test10e" (fn () => a2v b
= extract(g
, 101, SOME
3));
147 val _
= copy
{src
=g
, si
=1, dst
=g
, di
=0, len
=SOME (length g
-1)};
148 val test10f
= tst
' "test10f" (fn () => a2v b
= extract(g
, 100, SOME
3));
150 val _
= copy
{src
=g
, si
=202, dst
=g
, di
=202, len
=SOME
1};
153 tst
' "test10g" (fn () => g sub
202 = 10 * (202-1-103) mod 7 + 1);
155 tst
' "test10h" (fn () => (copy
{src
=array0
, si
=0, dst
=array0
, di
=0, len
=SOME
0};
156 array0
<> array(0, 999999)));
158 tst
' "test10i" (fn () => (copy
{src
=array0
, si
=0, dst
=array0
, di
=0, len
=NONE
};
159 array0
<> array(0, 999999)));
161 val test11a
= tst0
"test11a" ((copy
{src
=g
, si
= ~
1, dst
=g
, di
=0, len
=NONE
}; "WRONG")
162 handle Subscript
=> "OK" | _
=> "WRONG")
163 val test11b
= tst0
"test11b" ((copy
{src
=g
, si
=0, dst
=g
, di
= ~
1, len
=NONE
}; "WRONG")
164 handle Subscript
=> "OK" | _
=> "WRONG")
165 val test11c
= tst0
"test11c" ((copy
{src
=g
, si
=1, dst
=g
, di
=0, len
=NONE
}; "OK")
167 val test11d
= tst0
"test11d" ((copy
{src
=g
, si
=0, dst
=g
, di
=1, len
=NONE
}; "WRONG")
168 handle Subscript
=> "OK" | _
=> "WRONG")
169 val test11e
= tst0
"test11e" ((copy
{src
=g
, si
=203, dst
=g
, di
=0, len
=NONE
}; "OK")
172 val test11f
= tst0
"test11f" ((copy
{src
=g
, si
= ~
1, dst
=g
, di
=0, len
=SOME (length g
)}; "WRONG")
173 handle Subscript
=> "OK" | _
=> "WRONG")
174 val test11g
= tst0
"test11g" ((copy
{src
=g
, si
=0, dst
=g
, di
= ~
1, len
=SOME (length g
)}; "WRONG")
175 handle Subscript
=> "OK" | _
=> "WRONG")
176 val test11h
= tst0
"test11h" ((copy
{src
=g
, si
=1, dst
=g
, di
=0, len
=SOME (length g
)}; "WRONG")
177 handle Subscript
=> "OK" | _
=> "WRONG")
178 val test11i
= tst0
"test11i" ((copy
{src
=g
, si
=0, dst
=g
, di
=1, len
=SOME (length g
)}; "WRONG")
179 handle Subscript
=> "OK" | _
=> "WRONG")
180 val test11j
= tst0
"test11j" ((copy
{src
=g
, si
=0, dst
=g
, di
=0, len
=SOME (length g
+1)}; "WRONG")
181 handle Subscript
=> "OK" | _
=> "WRONG")
182 val test11k
= tst0
"test11k" ((copy
{src
=g
, si
=203, dst
=g
, di
=0, len
=SOME
1}; "WRONG")
183 handle Subscript
=> "OK" | _
=> "WRONG")
188 fun addv c
= v
:= c
+ !v
;
189 fun setvi (i
, c
) = v
:= c
+ i
;
190 fun addvi (i
, c
) = v
:= c
+ i
+ !v
;
191 fun cons (x
,r
) = x
:: r
192 fun consi (i
,x
,r
) = (i
,x
) :: r
193 val inplist
= [7,9,13];
194 val inp
= fromList inplist
195 val pni
= fromList (rev inplist
)
197 copy
{src
=inp
, si
=0, dst
=a
, di
=0, len
=NONE
}
200 val array0
= fromList
[] : int array
;
203 tst
' "test12a" (fn _
=>
204 foldl cons
[1,2] array0
= [1,2]
205 andalso foldl cons
[1,2] inp
= [13,9,7,1,2]
206 andalso (foldl (fn (x
, _
) => setv x
) () inp
; !v
= 13));
209 tst
' "test12b" (fn _
=>
210 foldr cons
[1,2] array0
= [1,2]
211 andalso foldr cons
[1,2] inp
= [7,9,13,1,2]
212 andalso (foldr (fn (x
, _
) => setv x
) () inp
; !v
= 7));
215 tst
' "test12c" (fn _
=>
216 find (fn _
=> true) array0
= NONE
217 andalso find (fn _
=> false) inp
= NONE
218 andalso find (fn x
=> x
=7) inp
= SOME
7
219 andalso find (fn x
=> x
=9) inp
= SOME
9
220 andalso (setv
0; find (fn x
=> (addv x
; x
=9)) inp
; !v
= 7+9));
223 tst
' "test12d" (fn _
=>
224 (setv
117; app setv array0
; !v
= 117)
225 andalso (setv
0; app addv inp
; !v
= 7+9+13)
226 andalso (app setv inp
; !v
= 13));
229 let val a
= array(length inp
, inp sub
0)
231 tst
' "test12e" (fn _
=>
232 (modify (~
: int -> int) array0
; true)
233 andalso (copyinp a
; modify ~ a
; foldr (op::) [] a
= map ~ inplist
)
234 andalso (setv
117; modify (fn x
=> (setv x
; 37)) a
; !v
= ~
13))
238 tst
' "test13a" (fn _
=>
239 foldli consi
[] (array0
, 0, NONE
) = []
240 andalso foldri consi
[] (array0
, 0, NONE
) = []
241 andalso foldli consi
[] (inp
, 0, NONE
) = [(2,13),(1,9),(0,7)]
242 andalso foldri consi
[] (inp
, 0, NONE
) = [(0,7),(1,9),(2,13)])
245 tst
' "test13b" (fn _
=>
246 foldli consi
[] (array0
, 0, SOME
0) = []
247 andalso foldri consi
[] (array0
, 0, SOME
0) = []
248 andalso foldli consi
[] (inp
, 0, SOME
0) = []
249 andalso foldri consi
[] (inp
, 0, SOME
0) = []
250 andalso foldli consi
[] (inp
, 3, SOME
0) = []
251 andalso foldri consi
[] (inp
, 3, SOME
0) = []
252 andalso foldli consi
[] (inp
, 0, SOME
3) = [(2,13),(1,9),(0,7)]
253 andalso foldri consi
[] (inp
, 0, SOME
3) = [(0,7),(1,9),(2,13)]
254 andalso foldli consi
[] (inp
, 0, SOME
2) = [(1,9),(0,7)]
255 andalso foldri consi
[] (inp
, 0, SOME
2) = [(0,7),(1,9)]
256 andalso foldli consi
[] (inp
, 1, SOME
2) = [(2,13),(1,9)]
257 andalso foldri consi
[] (inp
, 1, SOME
2) = [(1,9),(2,13)]
258 andalso foldli consi
[] (inp
, 2, SOME
1) = [(2,13)]
259 andalso foldri consi
[] (inp
, 2, SOME
1) = [(2,13)]);
261 val test13c
= tst0
"test13c" ((foldli consi
[] (inp
, ~
1, NONE
) seq
"WRONG")
262 handle Subscript
=> "OK" | _
=> "WRONG");
263 val test13d
= tst0
"test13d" ((foldli consi
[] (inp
, 4, NONE
) seq
"WRONG")
264 handle Subscript
=> "OK" | _
=> "WRONG");
265 val test13e
= tst0
"test13e" ((foldli consi
[] (inp
, ~
1, SOME
2) seq
"WRONG")
266 handle Subscript
=> "OK" | _
=> "WRONG");
267 val test13f
= tst0
"test13f" ((foldli consi
[] (inp
, 4, SOME
0) seq
"WRONG")
268 handle Subscript
=> "OK" | _
=> "WRONG");
269 val test13g
= tst0
"test13g" ((foldli consi
[] (inp
, 0, SOME
4) seq
"WRONG")
270 handle Subscript
=> "OK" | _
=> "WRONG");
271 val test13h
= tst0
"test13h" ((foldli consi
[] (inp
, 2, SOME ~
1) seq
"WRONG")
272 handle Subscript
=> "OK" | _
=> "WRONG");
274 val test13i
= tst0
"test13i" ((foldri consi
[] (inp
, ~
1, NONE
) seq
"WRONG")
275 handle Subscript
=> "OK" | _
=> "WRONG");
276 val test13j
= tst0
"test13j" ((foldri consi
[] (inp
, 4, NONE
) seq
"WRONG")
277 handle Subscript
=> "OK" | _
=> "WRONG");
278 val test13k
= tst0
"test13k" ((foldri consi
[] (inp
, ~
1, SOME
2) seq
"WRONG")
279 handle Subscript
=> "OK" | _
=> "WRONG");
280 val test13l
= tst0
"test13l" ((foldri consi
[] (inp
, 4, SOME
0) seq
"WRONG")
281 handle Subscript
=> "OK" | _
=> "WRONG");
282 val test13m
= tst0
"test13m" ((foldri consi
[] (inp
, 0, SOME
4) seq
"WRONG")
283 handle Subscript
=> "OK" | _
=> "WRONG");
284 val test13n
= tst0
"test13n" ((foldri consi
[] (inp
, 2, SOME ~
1) seq
"WRONG")
285 handle Subscript
=> "OK" | _
=> "WRONG");
288 tst
' "test14a" (fn _
=>
289 findi (fn _
=> true) (array0
, 0, NONE
) = NONE
290 andalso findi (fn _
=> false) (inp
, 0, NONE
) = NONE
291 andalso findi (fn (i
, x
) => x
=9 orelse 117 div (2-i
) = 0) (inp
, 0, NONE
)
295 tst
' "test14b" (fn _
=>
296 findi (fn _
=> true) (array0
, 0, SOME
0) = NONE
297 andalso findi (fn _
=> false) (inp
, 0, NONE
) = NONE
298 andalso findi (fn (i
, x
) => x
=9 orelse 117 div (2-i
) = 0) (inp
, 0, NONE
)
301 val test14c
= (findi (fn _
=> true) (inp
, ~
1, NONE
) seq
"WRONG")
302 handle Subscript
=> "OK" | _
=> "WRONG";
303 val test14d
= (findi (fn _
=> true) (inp
, 4, NONE
) seq
"WRONG")
304 handle Subscript
=> "OK" | _
=> "WRONG";
305 val test14e
= (findi (fn _
=> true) (inp
, ~
1, SOME
2) seq
"WRONG")
306 handle Subscript
=> "OK" | _
=> "WRONG";
307 val test14f
= (findi (fn _
=> true) (inp
, 4, SOME
0) seq
"WRONG")
308 handle Subscript
=> "OK" | _
=> "WRONG";
309 val test14g
= (findi (fn _
=> true) (inp
, 0, SOME
4) seq
"WRONG")
310 handle Subscript
=> "OK" | _
=> "WRONG";
311 val test14h
= (findi (fn _
=> true) (inp
, 2, SOME ~
1) seq
"WRONG")
312 handle Subscript
=> "OK" | _
=> "WRONG";
315 tst
' "test15a" (fn _
=>
316 (setvi (0,117); appi
setvi (array0
, 0, NONE
); !v
= 117)
317 andalso (setvi (0,0); appi
addvi (inp
, 0, NONE
); !v
= 0+7+1+9+2+13)
318 andalso (appi
setvi (inp
, 0, NONE
); !v
= 2+13));
320 tst
' "test15b" (fn _
=>
321 (setvi (0,117); appi
setvi (array0
, 0, SOME
0); !v
= 117)
322 andalso (setvi (0,0); appi
addvi (inp
, 0, SOME
0); !v
= 0)
323 andalso (setvi (0,0); appi
addvi (inp
, 3, SOME
0); !v
= 0)
324 andalso (setvi (0,0); appi
addvi (inp
, 0, SOME
2); !v
= 0+7+1+9)
325 andalso (setvi (0,0); appi
addvi (inp
, 1, SOME
2); !v
= 1+9+2+13)
326 andalso (setvi (0,0); appi
addvi (inp
, 0, SOME
3); !v
= 0+7+1+9+2+13)
327 andalso (appi
setvi (inp
, 1, SOME
2); !v
= 2+13)
328 andalso (appi
setvi (inp
, 0, SOME
2); !v
= 1+9)
329 andalso (appi
setvi (inp
, 0, SOME
1); !v
= 0+7)
330 andalso (appi
setvi (inp
, 0, SOME
3); !v
= 2+13));
332 val test15c
= tst0
"test15c" ((appi
setvi (inp
, ~
1, NONE
) seq
"WRONG")
333 handle Subscript
=> "OK" | _
=> "WRONG");
334 val test15d
= tst0
"test15d" ((appi
setvi (inp
, 4, NONE
) seq
"WRONG")
335 handle Subscript
=> "OK" | _
=> "WRONG");
336 val test15e
= tst0
"test15e" ((appi
setvi (inp
, ~
1, SOME
2) seq
"WRONG")
337 handle Subscript
=> "OK" | _
=> "WRONG");
338 val test15f
= tst0
"test15f" ((appi
setvi (inp
, 4, SOME
0) seq
"WRONG")
339 handle Subscript
=> "OK" | _
=> "WRONG");
340 val test15g
= tst0
"test15g" ((appi
setvi (inp
, 0, SOME
4) seq
"WRONG")
341 handle Subscript
=> "OK" | _
=> "WRONG");
342 val test15h
= tst0
"test15h" ((appi
setvi (inp
, 2, SOME ~
1) seq
"WRONG")
343 handle Subscript
=> "OK" | _
=> "WRONG");
346 let val a
= array(length inp
, inp sub
0)
348 tst
' "test16a" (fn _
=>
349 (modifyi (op +) (array0
, 0, NONE
); true)
350 andalso (modifyi (op +) (array0
, 0, SOME
0); true)
351 andalso (copyinp a
; modifyi (op -) (a
, 0, SOME
0);
352 foldr (op::) [] a
= [7,9,13])
353 andalso (copyinp a
; modifyi (op -) (a
, 3, SOME
0);
354 foldr (op::) [] a
= [7,9,13])
355 andalso (copyinp a
; modifyi (op -) (a
, 0, NONE
);
356 foldr (op::) [] a
= [~
7,~
8,~
11])
357 andalso (copyinp a
; modifyi (op -) (a
, 0, SOME
3);
358 foldr (op::) [] a
= [~
7,~
8,~
11])
359 andalso (copyinp a
; modifyi (op -) (a
, 0, SOME
2);
360 foldr (op::) [] a
= [~
7,~
8,13])
361 andalso (copyinp a
; modifyi (op -) (a
, 1, SOME
2);
362 foldr (op::) [] a
= [7,~
8,~
11])
363 andalso (copyinp a
; setv
117;
364 modifyi (fn x
=> (setvi x
; 37)) (a
, 0, NONE
); !v
= 2+13)
365 andalso (copyinp a
; setv
117;
366 modifyi (fn x
=> (setvi x
; 37)) (a
, 0, SOME
3); !v
= 2+13)
367 andalso (copyinp a
; setv
117;
368 modifyi (fn x
=> (setvi x
; 37)) (a
, 1, SOME
2); !v
= 2+13)
369 andalso (copyinp a
; setv
117;
370 modifyi (fn x
=> (setvi x
; 37)) (a
, 0, SOME
2); !v
= 1+9)
371 andalso (copyinp a
; setv
117;
372 modifyi (fn x
=> (setvi x
; 37)) (a
, 0, SOME
0); !v
= 117)
373 andalso (copyinp a
; setv
117;
374 modifyi (fn x
=> (setvi x
; 37)) (a
, 3, SOME
0); !v
= 117))
377 val test16b
= tst0
"test16b" ((modifyi (op+) (inp
, ~
1, NONE
) seq
"WRONG")
378 handle Subscript
=> "OK" | _
=> "WRONG");
379 val test16c
= tst0
"test16c" ((modifyi (op+) (inp
, 4, NONE
) seq
"WRONG")
380 handle Subscript
=> "OK" | _
=> "WRONG");
381 val test16d
= tst0
"test16d" ((modifyi (op+) (inp
, ~
1, SOME
2) seq
"WRONG")
382 handle Subscript
=> "OK" | _
=> "WRONG");
383 val test16e
= tst0
"test16e" ((modifyi (op+) (inp
, 4, SOME
0) seq
"WRONG")
384 handle Subscript
=> "OK" | _
=> "WRONG");
385 val test16f
= tst0
"test16f" ((modifyi (op+) (inp
, 0, SOME
4) seq
"WRONG")
386 handle Subscript
=> "OK" | _
=> "WRONG");
387 val test16g
= tst0
"test16g" ((modifyi (op+) (inp
, 2, SOME ~
1) seq
"WRONG")
388 handle Subscript
=> "OK" | _
=> "WRONG");