1(* test/array.sml -- some test cases for Array
2   PS 1994-12-10, 1995-06-14, 1995-11-07, 2000-10-19 *)
3
4use "auxil.sml";
5
6load "ArraySlice";
7
8local
9    open Array
10    infix 9 sub
11    val array0 = fromList []
12in
13
14val a = fromList [1,11,21,31,41,51,61];
15val b = fromList [441,551,661];
16val c = fromList [1,11,21,31,41,51,61];
17
18val test1 = check'(fn () => a<>c);
19val test2 =
20    check'(fn () =>
21	   array(0, 11) <> array0
22	   andalso array(0,()) <> tabulate(0, fn _ => ())
23	   andalso tabulate(0, fn _ => ()) <> fromList []
24	   andalso fromList [] <> fromList []
25	   andalso array(0, ()) <> array(0, ())
26	   andalso tabulate(0, fn _ => ()) <> tabulate(0, fn _ => ()));
27
28val d = tabulate(100, fn i => i mod 7 * 10 + 1);
29
30val test3 =
31    check'(fn () => d sub 27 = 61);
32
33val test4a = (tabulate(maxLen+1, fn i => i) seq "WRONG")
34            handle Size => "OK" | _ => "WRONG";
35
36val test4b = (tabulate(~1, fn i => i) seq "WRONG")
37            handle Size => "OK" | _ => "WRONG";
38
39val test4c =
40    check'(fn () => length (tabulate(0, fn i => i div 0)) = 0);
41
42val test5a =
43    check'(fn () => length (fromList []) = 0 andalso length a = 7);
44val test5b =
45    check'(fn () => length array0 = 0);
46
47val test6a = (c sub ~1 seq "WRONG") handle Subscript => "OK" | _ => "WRONG";
48val test6b = (c sub 7  seq "WRONG") handle Subscript => "OK" | _ => "WRONG";
49val test6c = check'(fn () => c sub 0 = 1);
50
51val e = array(203, 0);
52val _ = (copy{src=d, dst=e, di=0};
53	 copy{src=b, dst=e, di=length d};
54	 copy{src=d, dst=e, di=length d + length b});
55
56fun a2v a = vector a
57val ev = Vector.concat [a2v d, a2v b, a2v d]; (* length e = 203 *)
58
59val test7 = check'(fn () => length e = 203);
60
61val test8a = (update(e, ~1, 99) seq "WRONG")
62             handle Subscript => "OK" | _ => "WRONG";
63val test8b = (update(e, length e, 99) seq "WRONG")
64             handle Subscript => "OK" | _ => "WRONG";
65
66val f = ArraySlice.vector(ArraySlice.slice (e, 100, SOME 3));
67
68val test9 = check'(fn () => f = a2v b);
69
70val test9a =
71    check'(fn () => ev = vector e);
72val test9b =
73    check'(fn () => a2v (fromList []) = vector array0);
74
75val _ = copy{src=e, dst=e, di=0};
76val g = array(203, 9999999);
77val _ = copy{src=e, dst=g, di=0};
78
79val test10a = check'(fn () => ev = vector g);
80
81val test10b =
82    check'(fn () => (copy{src=array0, dst=array0, di=0};
83		     array0 <> array(0, 999999)));
84val test10c =
85    check'(fn () => (copy{src=array0, dst=g, di=0};
86		     ev = vector g));
87val test10d =
88    check'(fn () => (copy{src=array0, dst=g, di=203};
89		     ev = vector g));
90val test10e =
91    check'(fn () => (copy{src=array0, dst=g, di=1};
92		     ev = vector g));
93
94val test11a = (copy{src=g, dst=g, di=1}; "WRONG")
95              handle Subscript => "OK" | _ => "WRONG"
96val test11b = (copy{src=g, dst=g, di=202}; "WRONG")
97              handle Subscript => "OK" | _ => "WRONG"
98val test11c = (copy{src=b, dst=g, di = ~1}; "WRONG")
99              handle Subscript => "OK" | _ => "WRONG"
100val test11d = (copy{src=b, dst=g, di=203}; "WRONG")
101              handle Subscript => "OK" | _ => "WRONG"
102val test11e = check'(fn () => ev = vector g);
103
104local
105    val v = ref 0
106    fun setv c = v := c;
107    fun addv c = v := c + !v;
108    fun setvi (i, c) = v := c + i;
109    fun addvi (i, c) = v := c + i + !v;
110    fun cons (x,r) = x ::  r
111    fun consi (i,x,r) = (i,x) ::  r
112    val inplist = [7,9,13];
113    val inp = fromList inplist
114    val pni = fromList (rev inplist)
115    fun copyinp a = copy{src=inp, dst=a, di=0}
116in
117
118val array0 = fromList [] : int array;
119
120val test12a =
121    check'(fn _ =>
122	           foldl cons [1,2] array0 = [1,2]
123	   andalso foldl cons [1,2] inp = [13,9,7,1,2]
124	   andalso (foldl (fn (x, _) => setv x) () inp; !v = 13));
125
126val test12b =
127    check'(fn _ =>
128	           foldr cons [1,2] array0 = [1,2]
129	   andalso foldr cons [1,2] inp = [7,9,13,1,2]
130	   andalso (foldr (fn (x, _) => setv x) () inp; !v = 7));
131
132val test12c =
133    check'(fn _ =>
134	           find (fn _ => true) array0 = NONE
135	   andalso find (fn _ => false) inp = NONE
136	   andalso find (fn x => x=7) inp = SOME 7
137	   andalso find (fn x => x=9) inp = SOME 9
138	   andalso (setv 0; find (fn x => (addv x; x=9)) inp; !v = 7+9));
139
140val test12d =
141    check'(fn _ =>
142           (setv 117; app setv array0; !v = 117)
143	   andalso (setv 0; app addv inp; !v = 7+9+13)
144	   andalso (app setv inp; !v = 13));
145val test12e =
146    let val a = array(length inp, inp sub 0)
147    in
148	check'(fn _ =>
149           (modify (~ : int -> int) array0; true)
150	   andalso (copyinp a; modify ~ a; foldr (op::) [] a = map ~ inplist)
151	   andalso (setv 117; modify (fn x => (setv x; 37)) a; !v = ~13))
152    end
153val test12f =
154    check'(fn _ =>
155	   not (exists (fn i => i>61) a)
156	   andalso exists (fn i => i>41) a
157	   andalso not (exists (fn _ => true) array0));
158val test12g =
159    check'(fn _ =>
160	   (setv 117; exists (fn x => (setv x; false)) array0; !v = 117)
161	   andalso (setv 0; exists (fn x => (addv x; false)) inp; !v = 7+9+13)
162	   andalso (exists (fn x => (setv x; false)) inp; !v = 13));
163val test12h =
164    check'(fn _ =>
165	   not (all (fn i => i<61) a)
166	   andalso all (fn i => i<62) a
167	   andalso all (fn _ => false) array0);
168val test12i =
169    check'(fn _ =>
170	   (setv 117; all (fn x => (setv x; true)) array0; !v = 117)
171	   andalso (setv 0; all (fn x => (addv x; true)) inp; !v = 7+9+13)
172	   andalso (all (fn x => (setv x; true)) inp; !v = 13));
173
174val test13a =
175    check'(fn _ =>
176	           foldli consi [] array0 = []
177	   andalso foldri consi [] array0 = []
178	   andalso (setv 117; foldli (fn (_, x, _) => setv x) () array0;
179		    !v = 117)
180	   andalso (setv 117; foldri (fn (_, x, _) => setv x) () array0;
181		    !v = 117));
182val test13b =
183    check'(fn _ =>
184	           foldli consi [] array0 = []
185	   andalso foldri consi [] array0 = []
186	   andalso foldli consi [] inp = [(2,13),(1,9),(0,7)]
187	   andalso foldri consi [] inp = [(0,7),(1,9),(2,13)]
188	   andalso (foldli (fn (_, x, _) => setv x) () inp; !v = 13)
189	   andalso (foldri (fn (_, x, _) => setv x) () inp; !v = 7));
190
191val test14a =
192    check'(fn _ =>
193	   findi (fn _ => true) array0 = NONE
194   andalso findi (fn _ => false) inp = NONE
195   andalso findi (fn (i, x) => x=9 orelse 117 div (2-i) = 0) inp = SOME (1,9));
196
197val test14b =
198    check'(fn _ =>
199	   (setvi (0,117);
200	    findi (fn arg => (setvi arg; false)) array0;
201	    !v = 117));
202val test14c =
203    check'(fn _ =>
204	   (setvi (0,0);
205	    findi (fn arg => (addvi arg; false)) inp;
206	    !v = 0+7+1+9+2+13));
207
208val test15a =
209    check'(fn _ =>
210           (setvi (0,117); appi setvi array0; !v = 117)
211	   andalso (setvi (0,0); appi addvi inp; !v = 0+7+1+9+2+13)
212	   andalso (appi setvi inp; !v = 2+13));
213
214val test16a =
215    let val a = array(length inp, inp sub 0)
216    in
217	check'(fn _ =>
218           (modifyi (op +) array0; true)
219	   andalso (copyinp a; modifyi (op -) a;
220		    foldr (op::) [] a = [~7,~8,~11]))
221    end
222
223end
224
225val test17 =
226    check'(fn _ =>
227	   let fun invcompare (c1, c2) = Char.compare (c2, c1)
228	       fun coll s1 s2 =
229		   collate invcompare (fromList (explode s1),
230				       fromList (explode s2))
231	   in
232	       coll "" "" = EQUAL
233	       andalso coll "" " " = LESS
234	       andalso coll " " "" = GREATER
235	       andalso coll "ABCD" "ABCD" = EQUAL
236	       andalso coll "ABCD" "ABCD " = LESS
237	       andalso coll "ABCD " "ABCD" = GREATER
238	       andalso coll "B" "ABCD" = LESS
239	       andalso coll "ABCD" "B" = GREATER
240	       andalso coll "CCCB" "CCCABCD" = LESS
241	       andalso coll "CCCABCD" "CCCB" = GREATER
242	       andalso coll "CCCB" "CCCA" = LESS
243	       andalso coll "CCCA" "CCCB" = GREATER
244	   end)
245end
246