Import Debian changes 20180207-1
[hcoop/debian/mlton.git] / regression / array.sml
CommitLineData
7f918cf1
CE
1(* Auxiliary functions for test cases *)
2
3infix 1 seq
4fun e1 seq e2 = e2;
5fun check b = if b then "OK" else "WRONG";
6fun check' f = (if f () then "OK" else "WRONG") handle _ => "EXN";
7
8fun 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
14fun checkrange bounds = check o range bounds;
15
16fun tst0 s s' = print (s ^ " \t" ^ s' ^ "\n");
17fun tst s b = tst0 s (check b);
18fun tst' s f = tst0 s (check' f);
19
20fun tstrange s bounds = (tst s) o range bounds
21
22(* test/array.sml -- some test cases for Array
23 PS 1994-12-10, 1995-06-14, 1995-11-07 *)
24
25val _ = print "Testing Array...\n"
26
27local
28 open Array
29 infix 9 sub
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),
34 dst = dst, di = di}
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))
45in
46
47val a = fromList [1,11,21,31,41,51,61];
48val b = fromList [441,551,661];
49val c = fromList [1,11,21,31,41,51,61];
50
51val test1 = tst' "test1" (fn () => a<>c);
52
53val test2 =
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 _ => ()));
61
62val d = tabulate(100, fn i => i mod 7 * 10 + 1);
63
64val test3 =
65 tst' "test3" (fn () => d sub 27 = 61);
66
67val test4a = tst0 "test4a"
68 ((tabulate(maxLen+1, fn i => i) seq "WRONG")
69 handle Overflow => "OK" | Size => "OK" | _ => "WRONG");
70
71val test4b = tst0 "test4b"
72 ((tabulate(~1, fn i => i) seq "WRONG")
73 handle Size => "OK" | _ => "WRONG");
74
75val test4c =
76 tst' "test4c" (fn () => length (tabulate(0, fn i => i div 0)) = 0);
77
78val test5a =
79 tst' "test5a" (fn () => length (fromList []) = 0 andalso length a = 7);
80val test5b =
81 tst' "test5b" (fn () => length array0 = 0);
82
83val test6a = tst0 "test6a" ((c sub ~1 seq "WRONG") handle Subscript => "OK" | _ => "WRONG");
84val test6b = tst0 "test6b" ((c sub 7 seq "WRONG") handle Subscript => "OK" | _ => "WRONG");
85val test6c = tst' "test6c" (fn () => c sub 0 = 1);
86
87val e = array(203, 0);
88val _ = (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});
91
92fun a2v a = extract(a, 0, NONE);
93val ev = Vector.concat [a2v d, a2v b, a2v d]; (* length e = 203 *)
94
95val test7 = tst' "test7" (fn () => length e = 203);
96
97val test8a = tst0 "test8a" ((update(e, ~1, 99) seq "WRONG")
98 handle Subscript => "OK" | _ => "WRONG");
99val test8b = tst0 "test8b" ((update(e, length e, 99) seq "WRONG")
100 handle Subscript => "OK" | _ => "WRONG");
101
102val f = extract (e, 100, SOME 3);
103
104val test9 = tst' "test9" (fn () => f = a2v b);
105
106val test9a =
107 tst' "test9a" (fn () => ev = extract(e, 0, SOME (length e))
108 andalso ev = extract(e, 0, NONE));
109val test9b =
110 tst' "test9b" (fn () => Vector.fromList [] = extract(e, 100, SOME 0));
111val test9c = (extract(e, ~1, SOME (length e)) seq "WRONG")
112 handle Subscript => "OK" | _ => "WRONG"
113val test9d = (extract(e, length e+1, SOME 0) seq "WRONG")
114 handle Subscript => "OK" | _ => "WRONG"
115val test9e = (extract(e, 0, SOME (length e+1)) seq "WRONG")
116 handle Subscript => "OK" | _ => "WRONG"
117val test9f = (extract(e, 20, SOME ~1) seq "WRONG")
118 handle Subscript => "OK" | _ => "WRONG"
119val test9g = (extract(e, ~1, NONE) seq "WRONG")
120 handle Subscript => "OK" | _ => "WRONG"
121val test9h = (extract(e, length e+1, NONE) seq "WRONG")
122 handle Subscript => "OK" | _ => "WRONG"
123val test9i =
124 tst' "test9i" (fn () => a2v (fromList []) = extract(e, length e, SOME 0)
125 andalso a2v (fromList []) = extract(e, length e, NONE));
126val test9j =
127 tst' "test9j" (fn () => extract(e, 3, SOME(length e - 3)) = extract(e, 3, NONE));
128
129val _ = copy{src=e, si=0, dst=e, di=0, len=NONE};
130val g = array(203, 9999999);
131val _ = copy{src=e, si=0, dst=g, di=0, len=NONE};
132
133val test10a = tst' "test10a" (fn () => ev = extract(e, 0, SOME (length e))
134 andalso ev = extract(e, 0, NONE));
135val test10b = tst' "test10b" (fn () => ev = extract(g, 0, SOME (length g))
136 andalso ev = extract(g, 0, NONE));
137
138val _ = copy{src=g, si=203, dst=g, di=0, len=SOME 0};
139val test10c = tst' "test10c" (fn () => ev = extract(g, 0, SOME (length g)));
140
141val _ = copy{src=g, si=0, dst=g, di=203, len=SOME 0};
142val test10d = tst' "test10d" (fn () => ev = extract(g, 0, SOME (length g)));
143
144val _ = copy{src=g, si=0, dst=g, di=1, len=SOME (length g-1)};
145val test10e = tst' "test10e" (fn () => a2v b = extract(g, 101, SOME 3));
146
147val _ = copy{src=g, si=1, dst=g, di=0, len=SOME (length g-1)};
148val test10f = tst' "test10f" (fn () => a2v b = extract(g, 100, SOME 3));
149
150val _ = copy{src=g, si=202, dst=g, di=202, len=SOME 1};
151
152val test10g =
153 tst' "test10g" (fn () => g sub 202 = 10 * (202-1-103) mod 7 + 1);
154val test10h =
155 tst' "test10h" (fn () => (copy{src=array0, si=0, dst=array0, di=0, len=SOME 0};
156 array0 <> array(0, 999999)));
157val test10i =
158 tst' "test10i" (fn () => (copy{src=array0, si=0, dst=array0, di=0, len=NONE};
159 array0 <> array(0, 999999)));
160
161val test11a = tst0 "test11a" ((copy{src=g, si= ~1, dst=g, di=0, len=NONE}; "WRONG")
162 handle Subscript => "OK" | _ => "WRONG")
163val test11b = tst0 "test11b" ((copy{src=g, si=0, dst=g, di= ~1, len=NONE}; "WRONG")
164 handle Subscript => "OK" | _ => "WRONG")
165val test11c = tst0 "test11c" ((copy{src=g, si=1, dst=g, di=0, len=NONE}; "OK")
166 handle _ => "WRONG")
167val test11d = tst0 "test11d" ((copy{src=g, si=0, dst=g, di=1, len=NONE}; "WRONG")
168 handle Subscript => "OK" | _ => "WRONG")
169val test11e = tst0 "test11e" ((copy{src=g, si=203, dst=g, di=0, len=NONE}; "OK")
170 handle _ => "WRONG")
171
172val test11f = tst0 "test11f" ((copy{src=g, si= ~1, dst=g, di=0, len=SOME (length g)}; "WRONG")
173 handle Subscript => "OK" | _ => "WRONG")
174val test11g = tst0 "test11g" ((copy{src=g, si=0, dst=g, di= ~1, len=SOME (length g)}; "WRONG")
175 handle Subscript => "OK" | _ => "WRONG")
176val test11h = tst0 "test11h" ((copy{src=g, si=1, dst=g, di=0, len=SOME (length g)}; "WRONG")
177 handle Subscript => "OK" | _ => "WRONG")
178val test11i = tst0 "test11i" ((copy{src=g, si=0, dst=g, di=1, len=SOME (length g)}; "WRONG")
179 handle Subscript => "OK" | _ => "WRONG")
180val test11j = tst0 "test11j" ((copy{src=g, si=0, dst=g, di=0, len=SOME (length g+1)}; "WRONG")
181 handle Subscript => "OK" | _ => "WRONG")
182val test11k = tst0 "test11k" ((copy{src=g, si=203, dst=g, di=0, len=SOME 1}; "WRONG")
183 handle Subscript => "OK" | _ => "WRONG")
184
185local
186 val v = ref 0
187 fun setv c = v := c;
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)
196 fun copyinp a =
197 copy{src=inp, si=0, dst=a, di=0, len=NONE}
198in
199
200val array0 = fromList [] : int array;
201
202val test12a =
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));
207
208val test12b =
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));
213
214val test12c =
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));
221
222val test12d =
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));
227
228val test12e =
229 let val a = array(length inp, inp sub 0)
230 in
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))
235 end
236
237val test13a =
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)])
243
244val test13b =
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)]);
260
261val test13c = tst0 "test13c" ((foldli consi [] (inp, ~1, NONE) seq "WRONG")
262 handle Subscript => "OK" | _ => "WRONG");
263val test13d = tst0 "test13d" ((foldli consi [] (inp, 4, NONE) seq "WRONG")
264 handle Subscript => "OK" | _ => "WRONG");
265val test13e = tst0 "test13e" ((foldli consi [] (inp, ~1, SOME 2) seq "WRONG")
266 handle Subscript => "OK" | _ => "WRONG");
267val test13f = tst0 "test13f" ((foldli consi [] (inp, 4, SOME 0) seq "WRONG")
268 handle Subscript => "OK" | _ => "WRONG");
269val test13g = tst0 "test13g" ((foldli consi [] (inp, 0, SOME 4) seq "WRONG")
270 handle Subscript => "OK" | _ => "WRONG");
271val test13h = tst0 "test13h" ((foldli consi [] (inp, 2, SOME ~1) seq "WRONG")
272 handle Subscript => "OK" | _ => "WRONG");
273
274val test13i = tst0 "test13i" ((foldri consi [] (inp, ~1, NONE) seq "WRONG")
275 handle Subscript => "OK" | _ => "WRONG");
276val test13j = tst0 "test13j" ((foldri consi [] (inp, 4, NONE) seq "WRONG")
277 handle Subscript => "OK" | _ => "WRONG");
278val test13k = tst0 "test13k" ((foldri consi [] (inp, ~1, SOME 2) seq "WRONG")
279 handle Subscript => "OK" | _ => "WRONG");
280val test13l = tst0 "test13l" ((foldri consi [] (inp, 4, SOME 0) seq "WRONG")
281 handle Subscript => "OK" | _ => "WRONG");
282val test13m = tst0 "test13m" ((foldri consi [] (inp, 0, SOME 4) seq "WRONG")
283 handle Subscript => "OK" | _ => "WRONG");
284val test13n = tst0 "test13n" ((foldri consi [] (inp, 2, SOME ~1) seq "WRONG")
285 handle Subscript => "OK" | _ => "WRONG");
286
287val test14a =
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)
292 = SOME (1,9));
293
294val test14b =
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)
299 = SOME (1,9));
300
301val test14c = (findi (fn _ => true) (inp, ~1, NONE) seq "WRONG")
302 handle Subscript => "OK" | _ => "WRONG";
303val test14d = (findi (fn _ => true) (inp, 4, NONE) seq "WRONG")
304 handle Subscript => "OK" | _ => "WRONG";
305val test14e = (findi (fn _ => true) (inp, ~1, SOME 2) seq "WRONG")
306 handle Subscript => "OK" | _ => "WRONG";
307val test14f = (findi (fn _ => true) (inp, 4, SOME 0) seq "WRONG")
308 handle Subscript => "OK" | _ => "WRONG";
309val test14g = (findi (fn _ => true) (inp, 0, SOME 4) seq "WRONG")
310 handle Subscript => "OK" | _ => "WRONG";
311val test14h = (findi (fn _ => true) (inp, 2, SOME ~1) seq "WRONG")
312 handle Subscript => "OK" | _ => "WRONG";
313
314val test15a =
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));
319val test15b =
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));
331
332val test15c = tst0 "test15c" ((appi setvi (inp, ~1, NONE) seq "WRONG")
333 handle Subscript => "OK" | _ => "WRONG");
334val test15d = tst0 "test15d" ((appi setvi (inp, 4, NONE) seq "WRONG")
335 handle Subscript => "OK" | _ => "WRONG");
336val test15e = tst0 "test15e" ((appi setvi (inp, ~1, SOME 2) seq "WRONG")
337 handle Subscript => "OK" | _ => "WRONG");
338val test15f = tst0 "test15f" ((appi setvi (inp, 4, SOME 0) seq "WRONG")
339 handle Subscript => "OK" | _ => "WRONG");
340val test15g = tst0 "test15g" ((appi setvi (inp, 0, SOME 4) seq "WRONG")
341 handle Subscript => "OK" | _ => "WRONG");
342val test15h = tst0 "test15h" ((appi setvi (inp, 2, SOME ~1) seq "WRONG")
343 handle Subscript => "OK" | _ => "WRONG");
344
345val test16a =
346 let val a = array(length inp, inp sub 0)
347 in
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))
375 end
376
377val test16b = tst0 "test16b" ((modifyi (op+) (inp, ~1, NONE) seq "WRONG")
378 handle Subscript => "OK" | _ => "WRONG");
379val test16c = tst0 "test16c" ((modifyi (op+) (inp, 4, NONE) seq "WRONG")
380 handle Subscript => "OK" | _ => "WRONG");
381val test16d = tst0 "test16d" ((modifyi (op+) (inp, ~1, SOME 2) seq "WRONG")
382 handle Subscript => "OK" | _ => "WRONG");
383val test16e = tst0 "test16e" ((modifyi (op+) (inp, 4, SOME 0) seq "WRONG")
384 handle Subscript => "OK" | _ => "WRONG");
385val test16f = tst0 "test16f" ((modifyi (op+) (inp, 0, SOME 4) seq "WRONG")
386 handle Subscript => "OK" | _ => "WRONG");
387val test16g = tst0 "test16g" ((modifyi (op+) (inp, 2, SOME ~1) seq "WRONG")
388 handle Subscript => "OK" | _ => "WRONG");
389end
390
391end
392