1(* File "test/array2.sml" 1995-09-12, 1997-03-12, 1998-04-07, 2001-05-29 *)
2
3val _ = load "Array2";			(* MOSML *)
4
5use "auxil.sml";
6
7local
8    open Array2
9in
10
11val a0 = tabulate RowMajor (0, 0, fn (i, j) => 1 div 0);
12fun fill (i, j) = 10 * i + j
13
14local
15    val sequence = ref ([] : (int * int) list)
16in
17val a = tabulate RowMajor
18        (3, 4, fn args => (sequence := args :: !sequence; fill args));
19
20val test0a = check(!sequence = [(2,3), (2,2), (2,1), (2,0),
21				(1,3), (1,2), (1,1), (1,0),
22				(0,3), (0,2), (0,1), (0,0)]);
23end
24
25local
26    val sequence = ref ([] : (int * int) list)
27    val a' =
28	tabulate ColMajor
29        (3, 4, fn args => (sequence := args :: !sequence; fill args));
30in
31    val test0b = check(!sequence = [(2,3), (1,3), (0,3),
32				    (2,2), (1,2), (0,2),
33				    (2,1), (1,1), (0,1),
34				    (2,0), (1,0), (0,0)]
35		       andalso foldi RowMajor
36		                     (fn (i, j, aij, same) =>
37				      same andalso sub(a, i, j) = aij)
38		                     true {base=a', row=0, col=0,
39					   nrows=NONE, ncols=NONE})
40end
41
42val test1a =
43    check'(fn _ =>
44	   sub(a, 1, 2) = 12
45	   andalso sub(a, 0, 0) = 0
46	   andalso sub(a, 0, 3) = 3
47	   andalso sub(a, 2, 0) = 20
48	   andalso sub(a, 2, 3) = 23);
49
50val test1b = (sub(a, 3, 0) seq "WRONG")
51             handle Subscript => "OK" | _ => "WRONG";
52val test1c = (sub(a, 0, 4) seq "WRONG")
53             handle Subscript => "OK" | _ => "WRONG";
54val test1d = (sub(a, ~1, 0) seq "WRONG")
55             handle Subscript => "OK" | _ => "WRONG";
56val test1e = (sub(a, 0, ~1) seq "WRONG")
57             handle Subscript => "OK" | _ => "WRONG";
58val test1f = (sub(a, ~1, ~1) seq "WRONG")
59             handle Subscript => "OK" | _ => "WRONG";
60
61val _ = update(a, 1, 2, 112);
62
63val test2 =
64    check'(fn _ =>
65	   sub(a, 1, 2) = 112
66	   andalso sub(a, 1, 1) = 11
67	   andalso sub(a, 1, 3) = 13
68	   andalso sub(a, 0, 2) =  2
69	   andalso sub(a, 2, 2) = 22);
70
71val test3a = check'(fn _ => dimensions a = (3, 4));
72val test3b = check'(fn _ => nRows a = 3);
73val test3c = check'(fn _ => nCols a = 4);
74
75val test4a =
76    check'(fn _ =>
77	   row(a, 0) = Vector.tabulate(4, fn j => fill(0, j)));
78val test4b =
79    check'(fn _ =>
80	   row(a, 2) = Vector.tabulate(4, fn j => fill(2, j)));
81val test4c = (row(a, 4) seq "WRONG")
82             handle Subscript => "OK" | _ => "WRONG";
83val test4d = (row(a, ~1) seq "WRONG")
84             handle Subscript => "OK" | _ => "WRONG";
85
86val test5a =
87    check'(fn _ =>
88	   column(a, 0) = Vector.tabulate(3, fn i => fill(i, 0)));
89val test5b =
90    check'(fn _ =>
91	   column(a, 3) = Vector.tabulate(3, fn i => fill(i, 3)));
92val test5c = (column(a,  4) seq "WRONG")
93             handle Subscript => "OK" | _ => "WRONG";
94val test5d = (column(a, ~1) seq "WRONG")
95             handle Subscript => "OK" | _ => "WRONG";
96
97val a1 = tabulate RowMajor (3, 4, fill); (* Supposed to be constant below *)
98
99fun testcopy { row, col, nrows, ncols } dst_row dst_col reschk =
100    check'(fn _ =>
101	   let val a2 = tabulate RowMajor (3, 4, fill);
102	       val src = { base=a2, row=row, col=col, nrows=nrows, ncols=ncols}
103	       val _ = copy { src=src, dst=a2,
104			      dst_col=dst_col, dst_row=dst_row }
105	   in reschk a2 end);
106
107fun same a2 = List.all (fn i => row(a2, i) = row(a1, i)) [0, 1, 2]
108
109fun elts matrix a2 = [row(a2, 0), row(a2, 1), row(a2, 2)] = matrix
110
111val test6a =
112    testcopy {row=0, col=0, nrows=NONE, ncols=NONE} 0 0 same
113val test6b =
114    testcopy {row=3,  col=0,  nrows=NONE, ncols=NONE } 0 0 same
115val test6c =
116    testcopy {row=0,  col=4,  nrows=NONE, ncols=NONE } 0 0 same
117val test6d =
118    testcopy {row=3,  col=4,  nrows=NONE, ncols=NONE } 0 0 same
119val test6e =
120    testcopy {row=3,  col=4,  nrows=SOME 0, ncols=SOME 0 } 0 0 same
121val test6f =
122    testcopy {row=1,  col=1,  nrows=SOME 0, ncols=SOME 2 } 0 0 same
123val test6g =
124    testcopy {row=1,  col=1,  nrows=SOME 2, ncols=SOME 0 } 0 0 same
125
126val ### = Vector.fromList
127
128val test6h =
129    testcopy {row=0,  col=0,  nrows=NONE, ncols=SOME 3 } 0 1
130    (elts [ ###[0, 0, 1, 2], ###[10, 10, 11, 12], ###[20, 20, 21, 22]])
131val test6i =
132    testcopy {row=0,  col=0,  nrows=SOME 2, ncols=NONE } 1 0
133    (elts [ ###[0, 1, 2, 3], ###[0, 1, 2, 3], ###[10, 11, 12, 13]])
134val test6j =
135    testcopy {row=0,  col=0,  nrows=SOME 2, ncols=SOME 3 } 1 1
136    (elts [ ###[0, 1, 2, 3], ###[10, 0, 1, 2], ###[20, 10, 11, 12]])
137val test6k =
138    testcopy {row=1,  col=1,  nrows=SOME 2, ncols=SOME 3 } 0 0
139    (elts [ ###[11, 12, 13, 3], ###[21, 22, 23, 13], ###[20, 21, 22, 23]])
140val test6l =
141    testcopy {row=0,  col=1,  nrows=SOME 2, ncols=SOME 3 } 1 0
142    (elts [ ###[0, 1, 2, 3], ###[1, 2, 3, 13], ###[11, 12, 13, 23]])
143val test6m =
144    testcopy {row=0,  col=1,  nrows=SOME 2, ncols=SOME 3 } 1 1
145    (elts [ ###[0, 1, 2, 3], ###[10, 1, 2, 3], ###[20, 11, 12, 13]])
146val test6n =
147    testcopy {row=0,  col=1,  nrows=NONE, ncols=SOME 1 } 0 3
148    (elts [ ###[0, 1, 2, 1], ###[10, 11, 12, 11], ###[20, 21, 22, 21]])
149val test6o =
150    testcopy {row=1,  col=0,  nrows=SOME 1, ncols=NONE } 2 0
151    (elts [ ###[0, 1, 2, 3], ###[10, 11, 12, 13], ###[10, 11, 12, 13]])
152
153fun failcopy { row, col, nrows, ncols } dst_row dst_col =
154    (copy { src={ base=a1, row=row, col=col, nrows=nrows, ncols=ncols},
155	    dst=a1, dst_col=dst_col, dst_row=dst_row } seq "WRONG")
156    handle Subscript => "OK" | _ => "WRONG"
157
158val test7a = failcopy {row=0,   col=0,   nrows=NONE,   ncols=NONE   } 0 1
159val test7b = failcopy {row=0,   col=0,   nrows=NONE,   ncols=NONE   } 1 0
160val test7c = failcopy {row=0,   col=0,   nrows=NONE,   ncols=SOME 4 } 0 1
161val test7d = failcopy {row=0,   col=0,   nrows=SOME 3, ncols=NONE   } 1 0
162val test7e = failcopy {row=0,   col=0,   nrows=NONE,   ncols=SOME 5 } 0 0
163val test7f = failcopy {row=0,   col=0,   nrows=SOME 4, ncols=NONE   } 0 0
164val test7g = failcopy {row= ~1, col=0,   nrows=NONE,   ncols=NONE   } 0 0
165val test7h = failcopy {row=0,   col= ~1, nrows=NONE,   ncols=NONE   } 0 0
166val test7i = failcopy {row=3,   col=0,   nrows=SOME 1, ncols=NONE   } 0 0
167val test7j = failcopy {row=0,   col=4,   nrows=NONE,   ncols=SOME 1 } 0 0
168val test7k = failcopy {row=1,   col=1,   nrows=NONE,   ncols=NONE   } 0 2
169val test7l = failcopy {row=1,   col=1,   nrows=NONE,   ncols=NONE   } 2 0
170val test7m = failcopy {row=1,   col=1,   nrows=NONE,   ncols=SOME 3 } 0 2
171val test7n = failcopy {row=1,   col=1,   nrows=SOME 2, ncols=NONE   } 2 0
172
173val sequence = ref ([] : int list);
174
175fun collect a = sequence := a :: !sequence;
176
177fun collecti (i, j, a) =
178    if fill(i, j) = a then
179	sequence := a :: !sequence
180    else
181	raise Fail ("collecti: Error in " ^ Int.toString a)
182
183val a3 = tabulate RowMajor (3, 4, fill);
184
185val test8aa = check'(fn _ =>
186    (sequence := [];
187     app RowMajor collect a3;
188     !sequence = [23, 22, 21, 20,
189		  13, 12, 11, 10,
190		  3,   2,  1,  0]));
191val test8ab = check'(fn _ =>
192    (sequence := [];
193     app ColMajor collect a3;
194     !sequence = [23, 13, 3,
195		  22, 12, 2,
196		  21, 11, 1,
197		  20, 10, 0]));
198val test8ba = check'(fn _ =>
199    (sequence := [];
200     appi RowMajor collecti { base=a3, row=0, col=0, nrows=NONE, ncols=NONE };
201     !sequence = [23, 22, 21, 20, 13, 12, 11, 10, 3, 2, 1, 0]));
202val test8bb = check'(fn _ =>
203    (sequence := [];
204     appi ColMajor collecti { base=a3, row=0, col=0, nrows=NONE, ncols=NONE };
205     !sequence = [23, 13, 3, 22, 12, 2, 21, 11, 1, 20, 10, 0]));
206val test8c = check'(fn _ =>
207    (sequence := [];
208     appi RowMajor collecti { base=a3, row=0, col=1, nrows=NONE, ncols=NONE };
209     !sequence = [23, 22, 21, 13, 12, 11, 3, 2, 1]));
210val test8d = check'(fn _ =>
211    (sequence := [];
212     appi RowMajor collecti { base=a3, row=1, col=0, nrows=NONE, ncols=NONE };
213     !sequence = [23, 22, 21, 20, 13, 12, 11, 10]));
214val test8e = check'(fn _ =>
215    (sequence := [];
216     appi RowMajor collecti { base=a3, row=1, col=1, nrows=NONE, ncols=NONE };
217     !sequence = [23, 22, 21, 13, 12, 11]));
218val test8f = check'(fn _ =>
219    (sequence := [];
220     appi RowMajor collecti { base=a3, row=3, col=0, nrows=NONE, ncols=NONE };
221     !sequence = []));
222val test8g = check'(fn _ =>
223    (sequence := [];
224     appi RowMajor collecti { base=a3, row=0, col=4, nrows=NONE, ncols=NONE };
225     !sequence = []));
226val test8h = check'(fn _ =>
227    (sequence := [];
228     appi RowMajor collecti { base=a3, row=1, col=1, nrows=SOME 0, ncols=NONE};
229     !sequence = []));
230val test8i = check'(fn _ =>
231    (sequence := [];
232     appi RowMajor collecti { base=a3, row=1, col=1, nrows=NONE, ncols=SOME 0};
233     !sequence = []));
234val test8j = check'(fn _ =>
235    (sequence := [];
236     appi RowMajor collecti { base=a3, row=1, col=1, nrows=SOME 1, ncols=NONE};
237     !sequence = [13, 12, 11]));
238val test8k = check'(fn _ =>
239    (sequence := [];
240     appi RowMajor collecti { base=a3, row=1, col=1, nrows=NONE, ncols=SOME 1};
241     !sequence = [21, 11]));
242val test8l = check'(fn _ =>
243    (sequence := [];
244     appi RowMajor collecti {base=a3, row=0, col=1, nrows=SOME 2, ncols=SOME 2};
245     !sequence = [12, 11, 2, 1]));
246
247fun chkmodify resseq reschk =
248    check'(fn _ =>
249	   let val a3 = tabulate RowMajor (3, 4, fill)
250	   in
251	       sequence := [];
252	       modify RowMajor (fn a => (collect a; a*10)) a3;
253	       !sequence = resseq andalso reschk a3
254	   end)
255
256fun chkmodifyi { row, col, nrows, ncols } resseq reschk =
257    check'(fn _ =>
258	   let val a3 = tabulate RowMajor (3, 4, fill)
259	   in
260	       sequence := [];
261	       modifyi RowMajor (fn (args as (i, j, a)) => (collecti args;
262							    a*10))
263	              {base=a3, row=row, col=col, nrows=nrows, ncols=ncols};
264	       !sequence = resseq andalso reschk a3
265	   end)
266
267val test9a =
268    chkmodify
269      [23, 22, 21, 20, 13, 12, 11, 10, 3, 2, 1, 0]
270      (elts [###[0, 10, 20, 30], ###[100, 110, 120, 130], ###[200, 210, 220, 230]]);
271val test9b =
272    chkmodifyi { row=0, col=0, nrows=NONE, ncols=NONE }
273      [23, 22, 21, 20, 13, 12, 11, 10, 3, 2, 1, 0]
274      (elts [###[0, 10, 20, 30], ###[100, 110, 120, 130], ###[200, 210, 220, 230]]);
275val test9c =
276    chkmodifyi { row=0, col=1, nrows=NONE, ncols=NONE }
277      [23, 22, 21, 13, 12, 11, 3, 2, 1]
278      (elts [###[0, 10, 20, 30], ###[10, 110, 120, 130], ###[20, 210, 220, 230]]);
279val test9d =
280    chkmodifyi { row=1, col=0, nrows=NONE, ncols=NONE }
281      [23, 22, 21, 20, 13, 12, 11, 10]
282      (elts [###[0, 1, 2, 3], ###[100, 110, 120, 130], ###[200, 210, 220, 230]]);
283val test9e =
284    chkmodifyi { row=1, col=1, nrows=NONE, ncols=NONE }
285      [23, 22, 21, 13, 12, 11]
286      (elts [###[0, 1, 2, 3], ###[10, 110, 120, 130], ###[20, 210, 220, 230]]);
287val test9f =
288    chkmodifyi { row=3, col=0, nrows=NONE, ncols=NONE }
289      []
290      (elts [###[0, 1, 2, 3], ###[10, 11, 12, 13], ###[20, 21, 22, 23]]);
291val test9g =
292    chkmodifyi { row=0, col=4, nrows=NONE, ncols=NONE }
293      []
294      (elts [###[0, 1, 2, 3], ###[10, 11, 12, 13], ###[20, 21, 22, 23]]);
295val test9h =
296    chkmodifyi { row=1, col=1, nrows=SOME 0, ncols=NONE }
297      []
298      (elts [###[0, 1, 2, 3], ###[10, 11, 12, 13], ###[20, 21, 22, 23]]);
299val test9i =
300    chkmodifyi { row=1, col=1, nrows=NONE, ncols=SOME 0 }
301      []
302      (elts [###[0, 1, 2, 3], ###[10, 11, 12, 13], ###[20, 21, 22, 23]]);
303val test9j =
304    chkmodifyi { row=1, col=1, nrows=SOME 1, ncols=NONE }
305      [13, 12, 11]
306      (elts [###[0, 1, 2, 3], ###[10, 110, 120, 130], ###[20, 21, 22, 23]]);
307val test9k =
308    chkmodifyi { row=1, col=1, nrows=NONE, ncols=SOME 1 }
309      [21, 11]
310      (elts [###[0, 1, 2, 3], ###[10, 110, 12, 13], ###[20, 210, 22, 23]]);
311val test9l =
312    chkmodifyi { row=0, col=1, nrows=SOME 2, ncols=SOME 2 }
313      [12, 11, 2, 1]
314      (elts [###[0, 10, 20, 3], ###[10, 110, 120, 13], ###[20, 21, 22, 23]]);
315
316fun chkfold traversal resseq =
317    check'(fn _ =>
318	   let val a3 = tabulate RowMajor (3, 4, fill)
319	       val sequence =
320		   fold traversal (fn (a, res) => a :: res) [] a3
321	   in sequence = resseq end)
322
323fun chkfoldi traversal { row, col, nrows, ncols } resseq =
324    check'(fn _ =>
325	   let val a3 = tabulate RowMajor (3, 4, fill)
326	       fun chkidx i j a =
327		   if fill(i, j) = a then ()
328		   else raise Fail ("chkfoldi: Error in " ^ Int.toString a)
329	       val sequence =
330		   foldi traversal
331		   (fn (i, j, a, res) => (chkidx i j a; a :: res)) []
332		   { base=a3, row=row, col=col, nrows=nrows, ncols=ncols}
333	   in sequence = resseq end)
334
335val test10a1 =
336    chkfold RowMajor
337      [23, 22, 21, 20, 13, 12, 11, 10, 3, 2, 1, 0];
338val test10a2 =
339    chkfold ColMajor
340      [23, 13, 3, 22, 12, 2, 21, 11, 1, 20, 10, 0];
341val test10b1 =
342    chkfoldi RowMajor { row=0, col=0, nrows=NONE, ncols=NONE }
343      [23, 22, 21, 20, 13, 12, 11, 10, 3, 2, 1, 0];
344val test10b2 =
345    chkfoldi ColMajor { row=0, col=0, nrows=NONE, ncols=NONE }
346      [23, 13, 3, 22, 12, 2, 21, 11, 1, 20, 10, 0];
347val test10c1 =
348    chkfoldi RowMajor { row=0, col=1, nrows=NONE, ncols=NONE }
349      [23, 22, 21, 13, 12, 11, 3, 2, 1];
350val test10c2 =
351    chkfoldi ColMajor { row=0, col=1, nrows=NONE, ncols=NONE }
352      [23, 13, 3, 22, 12, 2, 21, 11, 1];
353val test10d1 =
354    chkfoldi RowMajor { row=1, col=0, nrows=NONE, ncols=NONE }
355      [23, 22, 21, 20, 13, 12, 11, 10];
356val test10d2 =
357    chkfoldi ColMajor { row=1, col=0, nrows=NONE, ncols=NONE }
358      [23, 13, 22, 12, 21, 11, 20, 10];
359val test10e1 =
360    chkfoldi RowMajor { row=1, col=1, nrows=NONE, ncols=NONE }
361      [23, 22, 21, 13, 12, 11];
362val test10e2 =
363    chkfoldi ColMajor { row=1, col=1, nrows=NONE, ncols=NONE }
364      [23, 13, 22, 12, 21, 11];
365val test10f1 =
366    chkfoldi RowMajor { row=3, col=0, nrows=NONE, ncols=NONE }
367      [];
368val test10f2 =
369    chkfoldi ColMajor { row=3, col=0, nrows=NONE, ncols=NONE }
370      [];
371val test10g1 =
372    chkfoldi RowMajor { row=0, col=4, nrows=NONE, ncols=NONE }
373      [];
374val test10g2 =
375    chkfoldi ColMajor { row=0, col=4, nrows=NONE, ncols=NONE }
376      [];
377val test10h1 =
378    chkfoldi RowMajor { row=1, col=1, nrows=SOME 0, ncols=NONE }
379      [];
380val test10h2 =
381    chkfoldi ColMajor { row=1, col=1, nrows=SOME 0, ncols=NONE }
382      [];
383val test10i1 =
384    chkfoldi RowMajor { row=1, col=1, nrows=NONE, ncols=SOME 0 }
385      [];
386val test10i2 =
387    chkfoldi ColMajor { row=1, col=1, nrows=NONE, ncols=SOME 0 }
388      [];
389val test10j1 =
390    chkfoldi RowMajor { row=1, col=1, nrows=SOME 1, ncols=NONE }
391      [13, 12, 11];
392val test10j2 =
393    chkfoldi ColMajor { row=1, col=1, nrows=SOME 1, ncols=NONE }
394      [13, 12, 11];
395val test10k1 =
396    chkfoldi RowMajor { row=1, col=1, nrows=NONE, ncols=SOME 1 }
397      [21, 11];
398val test10k2 =
399    chkfoldi ColMajor { row=1, col=1, nrows=NONE, ncols=SOME 1 }
400      [21, 11];
401val test10l1 =
402    chkfoldi RowMajor { row=0, col=1, nrows=SOME 2, ncols=SOME 2 }
403      [12, 11, 2, 1];
404val test10l2 =
405    chkfoldi ColMajor { row=0, col=1, nrows=SOME 2, ncols=SOME 2 }
406      [12, 2, 11, 1];
407
408fun faili { row, col, nrows, ncols } =
409    let val reg = {base=a1, row=row, col=col, nrows=nrows, ncols=ncols}
410    in
411	(appi RowMajor ignore reg seq "WRONG")
412	handle Subscript =>
413	(appi ColMajor ignore reg seq "WRONG")
414	handle Subscript =>
415	    (modifyi RowMajor (fn (_, _, a) => a) reg seq "WRONG")
416	    handle Subscript =>
417	    (modifyi ColMajor (fn (_, _, a) => a) reg seq "WRONG")
418	    handle Subscript =>
419		(foldi RowMajor (fn _ => 1) 0 reg seq "WRONG")
420		handle Subscript =>
421		    (foldi ColMajor (fn _ => 1) 0 reg seq "WRONG")
422		    handle Subscript => "OK"
423			 | _ => "WRONG"
424    end
425
426val test11a = faili {row=0,   col=0,   nrows=NONE,   ncols=SOME 5 }
427val test11b = faili {row=0,   col=0,   nrows=SOME 4, ncols=NONE   }
428val test11c = faili {row= ~1, col=0,   nrows=NONE,   ncols=NONE   }
429val test11d = faili {row=0,   col= ~1, nrows=NONE,   ncols=NONE   }
430val test11e = faili {row=3,   col=0,   nrows=SOME 1, ncols=NONE   }
431val test11f = faili {row=0,   col=4,   nrows=NONE,   ncols=SOME 1 }
432val test11g = faili {row=0,   col=1,   nrows=NONE,   ncols=SOME 4 }
433val test11h = faili {row=1,   col=0,   nrows=SOME 3, ncols=NONE   }
434
435fun chkfromlist xss =
436    check'(fn _ =>
437	   let val a        = fromList xss
438	       val elements =
439		   List.tabulate(nRows a,
440				 fn i => Vector.foldr (op::) [] (row(a, i)))
441	   in elements = xss end)
442
443val test12a = chkfromlist []
444val test12b = chkfromlist [[]]
445val test12c = chkfromlist [[], [], []]
446val test12d = chkfromlist [[0]]
447val test12e = chkfromlist [[0, 1]]
448val test12f = chkfromlist [[0], [10]]
449val test12g = chkfromlist [[0, 1, 2], [10, 11, 12]]
450val test12h = chkfromlist [[0, 1, 2], [10, 11, 12], [20, 21, 22]]
451val test12i = (fromList [[], [2]] seq "WRONG")
452              handle Size => "OK" | _ => "WRONG"
453val test12j = (fromList [[2], []] seq "WRONG")
454              handle Size => "OK" | _ => "WRONG"
455val test12k = (fromList [[1, 2, 3], [4, 5, 6], [7, 8]] seq "WRONG")
456              handle Size => "OK" | _ => "WRONG"
457val test12l = (fromList [[1, 2, 3], [7, 8], [4, 5, 6]] seq "WRONG")
458              handle Size => "OK" | _ => "WRONG"
459
460end
461