1/* Test read/write array */
2
3(kill (all),
4 l :
5   [7, 12, FOO, 45, 91,
6    2, -8, 123, -9, -1,
7    8, 18, Bar, -5, 17,
8    6, 13, -57, 28, 53,
9
10    2.5, 19.7, 1.2, 9.1, 1.7,
11    3.5, -8.2, 3.1, 7.2, 7.1,
12    5.4, -5.3, 9.8, 1.8, 6.2,
13    0.8, 52.9, 7.1, 0.2, 1.8,
14
15    baz, 79, 112, 173, 311,
16    18, 97, 211, 317, 113,
17    29, 32, 925, 512, 713,
18    87, 53, 133, 127, 158],
19 a : make_array ('any, 3, 4, 5),
20 fillarray (a, l),
21
22 /* Note that this makes b the same size as a,
23  * despite the different arguments ... <sigh>
24  */
25 array (b, 2, 3, 4),
26
27 fillarray (b, l),
28
29 a2 : make_array ('any, 3, 4, 5),
30 array (b2, 2, 3, 4),
31 0);
320;
33
34(read_array (file_search ("array.data"), a2),
35 is (equal (a, a2)));
36true;
37
38(read_array (file_search ("array.data"), b2),
39 is (equal (b, b2)));
40true;
41
42(write_data (a2, sconcat (maxima_tempdir, "/tmp-lisp-array.data")),
43 a3: make_array('any, 3, 4, 5),
44 read_array (sconcat (maxima_tempdir, "/tmp-lisp-array.data"), a3),
45 is (equal (a2, a3)));
46true;
47
48(write_data (b2, sconcat (maxima_tempdir, "/tmp-maxima-array.data")),
49 array (b3, 2, 3, 4),
50 read_array (sconcat (maxima_tempdir, "/tmp-maxima-array.data"), b3),
51 is (equal (b2, b3)));
52true;
53
54/* Test read/write comma-separated data */
55
56(kill (all),
57 l_nested :
58  [[KEY, XX, YY, ZZ, FOO, Bar, baz],
59   [aa46, 7, 7, 16, FOO3, 2.71, 2.88],
60   [aA31, 31, 61, 25, 916, 6.10, 741],
61   [AA22, 61, 40, 13, 32.75, Bar2, 9.12],
62   [AA16, 19, 25, 31, FOO7, 4.80, 697],
63   [AA16, 13, 49, 7, 5.67, 2.54, 4.54],
64   [AA58, 19, 28, 43, 4.04, 0.90, baz8],
65   [AA34, 16, 22, 19, 4.87, 4.90, 2.85],
66   [AA52, 28, 34, 7, FOO9, Bar4, 293],
67   [AA52, 10, 43, 13, 12.125, 5.69, 0.00],
68   [AA40, 7, 25, 40, 3.15, 7.54, 6.11]],
69 m : apply(matrix, l_nested),
70 0);
710;
72
73(m2 : read_matrix (file_search ("rectangular.csv")),
74 is (m = m2));
75true;
76
77(m3 : read_matrix (file_search ("really-csv.data"), 'comma),
78 is (m = m3));
79true;
80
81(m4 : read_matrix (file_search ("really-space-separated.csv"), 'space),
82 is (m = m4));
83true;
84
85(write_data (m2, sconcat (maxima_tempdir, "/tmp-matrix.csv")),
86 m5 : read_matrix(sconcat (maxima_tempdir, "/tmp-matrix.csv")),
87 is (m2 = m5));
88true;
89
90(write_data (m2, sconcat (maxima_tempdir, "/tmp-matrix.data", 'comma)),
91 m6 : read_matrix(sconcat (maxima_tempdir, "/tmp-matrix.data", 'comma)),
92 is (m2 = m6));
93true;
94
95(write_data (m2, sconcat (maxima_tempdir, "/tmp-matrix-2.csv", 'space)),
96 m7 : read_matrix(sconcat (maxima_tempdir, "/tmp-matrix-2.csv", 'space)),
97 is (m2 = m7));
98true;
99
100(array (a, 10, 6),
101 read_array (file_search ("rectangular.data"), a),
102 write_data (a, sconcat (maxima_tempdir, "/tmp-array.csv")),
103 array (a2, 10, 6),
104 read_array (sconcat (maxima_tempdir, "/tmp-array.csv"), a2),
105 is (equal (a, a2)));
106true;
107
108/* Test read/write variable-length lines */
109
110(kill (all),
111 l_nested :
112  [[aa46,7,7,16,FOO3],
113   [aA31,31,61,25,9.16,513],
114   [AA16,13,49,7,5.67,2.54,4.54],
115   [AA52,7,FOO9,Bar4,2.93],
116   [AA40,7,25,40,3.15,7.54,0.00],
117   [aa46,1325,7,16,FOO3,2.71,2.88],
118   [aA31,31,61,25,9.16],
119   [AA58,19,28,43,4.04,0.90,baz8],
120   [AA34,16,175.25,2.85],
121   [AA40,7,25],
122   [AA22,617,40,Bar2,9.12],
123   [zz7b,12375e-003,12375s-003,12375d-003,123b-001],
124   [w23q,123.375e-001,123.375s-001,123.375d-001,123.456b-001],
125   [q32w,12375E-003,12375S-003,12375D-003,123B-001],
126   [t5u7,123.375E-001,123.375S-001,123.375D-001,123.456B-001],
127   [q129,"Foo","BAR","Foo \\ BAR \"baz quux\""]],
128 l : flatten (l_nested),
129 h[aa46] : [7,7,16,FOO3],
130 h[aA31] : [31,61,25,9.16,513],
131 h[AA16] : [13,49,7,5.67,2.54,4.54],
132 h[AA52] : [7,FOO9,Bar4,2.93],
133 h[AA40] : [7,25,40,3.15,7.54,0.00],
134 h[aa46] : [1325,7,16,FOO3,2.71,2.88],
135 h[aA31] : [31,61,25,9.16],
136 h[AA58] : [19,28,43,4.04,0.90,baz8],
137 h[AA34] : [16,175.25,2.85],
138 h[AA40] : [7,25],
139 h[AA22] : [617,40,Bar2,9.12],
140 h[zz7b] : [12375e-003,12375s-003,12375d-003,123b-001],
141 h[w23q] : [123.375e-001,123.375s-001,123.375d-001,123.456b-001],
142 h[q32w] : [12375E-003,12375S-003,12375D-003,123B-001],
143 h[t5u7] : [123.375E-001,123.375S-001,123.375D-001,123.456B-001],
144 h[q129] : ["Foo","BAR","Foo \\ BAR \"baz quux\""],
145 0);
1460;
147
148(l2 : read_list (file_search ("ragged.data")),
149 is (l = l2));
150true;
151
152(l2_nested: read_nested_list (file_search ("ragged.data")),
153 is (l_nested = l2_nested));
154true;
155
156(read_hashed_array (file_search ("ragged.data"), h2),
157 is (equal (h, h2)));
158true;
159
160(AA16: 16,
161 AA22: 22,
162 aA31: 31,
163 AA34: 34,
164 AA40: 40,
165 aa46: 46,
166 AA52: 52,
167 AA58: 58,
168 FOO3: 13,
169
170 FOO9: 27.25,
171 Bar4: %pi,
172 baz8: -5.75,
173 Bar2: -1217,
174 is (ev (l) = ev (l2)));
175true;
176
177/* Test read/write rectangular (same number of items on each line) data */
178
179(kill (all),
180 l_nested :
181  [[KEY, XX, YY, ZZ, FOO, Bar, baz],
182   [aa46, 7, 7, 16, FOO3, 2.71, 2.88],
183   [aA31, 31, 61, 25, 916, 6.10, 741],
184   [AA22, 61, 40, 13, 32.75, Bar2, 9.12],
185   [AA16, 19, 25, 31, FOO7, 4.80, 697],
186   [AA16, 13, 49, 7, 5.67, 2.54, 4.54],
187   [AA58, 19, 28, 43, 4.04, 0.90, baz8],
188   [AA34, 16, 22, 19, 4.87, 4.90, 2.85],
189   [AA52, 28, 34, 7, FOO9, Bar4, 293],
190   [AA52, 10, 43, 13, 12.125, 5.69, 0.00],
191   [AA40, 7, 25, 40, 3.15, 7.54, 6.11]],
192 m : apply (matrix, l_nested),
193 for i:1 thru length (l_nested)
194  do h [l_nested [i] [1]] : rest (l_nested [i]),
195 l: flatten (l_nested),
196 0);
1970;
198
199(m2: read_matrix (file_search ("rectangular.data")),
200 is (m = m2));
201true;
202
203(read_hashed_array (file_search ("rectangular.data"), h2),
204 is (equal (h, h2)));
205true;
206
207(l2_nested : read_nested_list (file_search ("rectangular.data")),
208 is (l_nested = l2_nested));
209true;
210
211(l2 : read_list (file_search ("rectangular.data")),
212 is (l = l2));
213true;
214
215(write_data (m2, sconcat (maxima_tempdir, "/tmp-matrix.data")),
216 l3_nested : read_nested_list (sconcat (maxima_tempdir, "/tmp-matrix.data")),
217 is (l2_nested = l3_nested));
218true;
219
220(write_data (h2, sconcat (maxima_tempdir, "/tmp-hashed-array.data")),
221 read_hashed_array (sconcat (maxima_tempdir, "/tmp-hashed-array.data"), h3),
222 is (equal (h2, h3)));
223true;
224
225(write_data (l2_nested, sconcat (maxima_tempdir, "/tmp-nested-list.data")),
226 m3 : read_matrix (sconcat (maxima_tempdir, "/tmp-nested-list.data")),
227 is (m2 = m3));
228true;
229
230(write_data (l2, sconcat (maxima_tempdir, "/tmp-list.data")),
231 l3 : read_list (sconcat (maxima_tempdir, "/tmp-list.data")),
232 is (l2 = l3));
233true;
234
235(KEY: -2.5,
236 XX: 3.25,
237 YY: 1.75,
238 ZZ: 5.5,
239 FOO: %pi,
240 Bar: 175,
241 baz: 2119,
242
243 aa46: 46,
244 aA31: 31,
245 AA22: 22,
246 AA16: 16,
247 AA58: 58,
248 AA34: 34,
249 AA52: 52,
250 AA40: 40,
251
252 FOO3: 611,
253 Bar2: 1917,
254 FOO7: 6,
255 baz8: -5,
256 FOO9: -1.5,
257 Bar4: -3.75,
258
259 is (ev (m) = ev (m2)));
260true;
261
262/* Test read/write with different separator characters */
263
264(kill (all),
265 l_nested :
266  [[false],
267   [false, false],
268   [false, false, false],
269   [false, false, false, false],
270   [Foo],
271   [Foo, false],
272   [false, Foo],
273   [Foo, false, false],
274   [false, Foo, false],
275   [false, false, Foo],
276   [Foo, BAR, false],
277   [Foo, false, BAR],
278   [false, Foo, BAR],
279   [Foo, BAR, baz, embedded\ space\ and\ \$p\*cia\!\ c\#\@rs],
280   [12.34, Foo, 12375e-3, BAR, 1234b-2, baz, 1234],
281   ["tab	space comma, semicolon; pipe| dollar$", "quux \"blart\"", ?a\-lisp\-symbol]],
282 0);
2830;
284
285(l_nested_2 : read_nested_list (file_search ("semicolon.data"), 'semicolon),
286 is (l_nested = l_nested_2));
287true;
288
289(l_nested_3 : read_nested_list (file_search ("pipe.data"), 'pipe),
290 is (l_nested = l_nested_3));
291true;
292
293(write_data (l_nested_2, sconcat (maxima_tempdir, "/tmp-tab.data"), 'tab),
294 l_nested_4: read_nested_list (sconcat (maxima_tempdir, "/tmp-tab.data"), 'tab),
295 is (l_nested = l_nested_4));
296true;
297
298/* Test read/write on streams (instead of files) */
299
300(kill (all),
301 0);
3020;
303
304(a1 : make_array ('any, 3, 4, 5),
305 read_array (file_search ("array.data"), a1),
306 a2 : make_array ('any, 3, 4, 5),
307 s : openr (file_search ("array.data")),
308 read_array (s, a2),
309 close (s),
310 is (equal (a1, a2)));
311true;
312
313(s : openw (sconcat (maxima_tempdir, "/tmp-lisp-array.data")),
314 write_data (a2, s),
315 close (s),
316 a3 : make_array ('any, 3, 4, 5),
317 s : openr (sconcat (maxima_tempdir, "/tmp-lisp-array.data")),
318 read_array (s, a3),
319 close (s),
320 is (equal (a1, a3)));
321true;
322
323(b1 : array (b, 2, 3, 4),
324 read_array (file_search ("array.data"), b1),
325 b2 : array (b, 2, 3, 4),
326 s : openr (file_search ("array.data")),
327 read_array (s, b2),
328 close (s),
329 is (equal (b1, b2)));
330true;
331
332(s : openw (sconcat (maxima_tempdir, "/tmp-maxima-array.data")),
333 write_data (b2, s),
334 close (s),
335 b3 : array (b, 2, 3, 4),
336 s : openr (sconcat (maxima_tempdir, "/tmp-maxima-array.data")),
337 read_array (s, b3),
338 close (s),
339 is (equal (b1, b3)));
340true;
341
342(m1 : read_matrix (file_search ("rectangular.data")),
343 s : openr (file_search ("rectangular.data")),
344 m2 : read_matrix (s),
345 close (s),
346 is (m1 = m2));
347true;
348
349(s : openw (sconcat (maxima_tempdir, "/tmp-matrix.data")),
350 write_data (m2, s),
351 close (s),
352 s : openr (sconcat (maxima_tempdir, "/tmp-matrix.data")),
353 m3 : read_matrix (s),
354 close (s),
355 is (m1 = m3));
356true;
357
358(l1 : read_list (file_search ("ragged.data")),
359 s : openr (file_search ("ragged.data")),
360 l2 : read_list (s),
361 close (s),
362 is (l1 = l2));
363true;
364
365(s : openw (sconcat (maxima_tempdir, "/tmp-list.data")),
366 write_data (l2, s),
367 close (s),
368 s : openr (sconcat (maxima_tempdir, "/tmp-list.data")),
369 l3 : read_list (s),
370 close (s),
371 is (l1 = l3));
372true;
373
374(l1 : read_nested_list (file_search ("ragged.data")),
375 s : openr (file_search ("ragged.data")),
376 l2 : read_nested_list (s),
377 close (s),
378 is (l1 = l2));
379true;
380
381(s : openw (sconcat (maxima_tempdir, "/tmp-nested-list.data")),
382 write_data (l2, s),
383 close (s),
384 s : openr (sconcat (maxima_tempdir, "/tmp-nested-list.data")),
385 l3 : read_nested_list (s),
386 close (s),
387 is (l1 = l3));
388true;
389
390(read_hashed_array (file_search ("ragged.data"), h1),
391 s : openr (file_search ("ragged.data")),
392 read_hashed_array (s, h2),
393 close (s),
394 is (equal (h1, h2)));
395true;
396
397(s : openw (sconcat (maxima_tempdir, "/tmp-hashed-array.data")),
398 write_data (h2, s),
399 close (s),
400 s : openr (sconcat (maxima_tempdir, "/tmp-hashed-array.data")),
401 read_hashed_array (s, h3),
402 close (s),
403 is (equal (h1, h3)));
404true;
405
406/* Write and read binary data */
407
408(assume_external_byte_order (lsb),
409 read_binary_list (file_search ("test-binary.f8-lsb")));
410[-1.125, 0.375, 3.750, -7.875, 2.625];
411
412(assume_external_byte_order (lsb),
413 L : makelist (0, 5),
414 read_binary_list (file_search ("test-binary.f8-lsb"), L),
415 L);
416[-1.125, 0.375, 3.750, -7.875, 2.625];
417
418(assume_external_byte_order (msb),
419 read_binary_list (file_search ("test-binary.f8-msb")));
420[-1.125, 0.375, 3.750, -7.875, 2.625];
421
422(assume_external_byte_order (msb),
423 L : makelist (0, 5),
424 read_binary_list (file_search ("test-binary.f8-msb"), L),
425 L);
426[-1.125, 0.375, 3.750, -7.875, 2.625];
427
428(tmp_msb : sconcat (maxima_tempdir, "/tmp-binary.f8-msb"),
429 tmp_lsb : sconcat (maxima_tempdir, "/tmp-binary.f8-lsb"),
430 L : makelist (11.0^(3*i), i, 1, 98),
431 L4 : append ([?most\-negative\-double\-float], - reverse (L), [- 1.0], -1 / L, [?least\-negative\-normalized\-double\-float, 0.0, ?least\-positive\-normalized\-double\-float], reverse (1 / L), [1.0], L, [?most\-positive\-double\-float]),
432 0);
4330;
434
435(assume_external_byte_order (lsb),
436 write_binary_data (L4, tmp_lsb));
437done;
438
439(L4b : read_binary_list (tmp_lsb),
440 is (L4b = L4));
441true;
442
443(assume_external_byte_order (msb),
444 write_binary_data (L4, tmp_msb));
445done;
446
447(L4c : read_binary_list (tmp_msb),
448 is (L4c = L4));
449true;
450
451(A4 : make_array (any, 3, 7, 19),
452 fillarray (A4, L4),
453 0);
4540;
455
456(tmp2_msb : sconcat (maxima_tempdir, "/tmp2-binary.f8-msb"),
457 tmp2_lsb : sconcat (maxima_tempdir, "/tmp2-binary.f8-lsb"),
458 assume_external_byte_order (msb),
459 write_binary_data (A4, tmp2_msb));
460done;
461
462(A4b : make_array (any, 3, 7, 19),
463 read_binary_array (tmp2_msb, A4b),
464 is (A4b = A4b));
465true;
466
467is (listarray (read_binary_array (tmp2_msb)) = L4);
468true;
469
470(assume_external_byte_order (lsb),
471 write_binary_data (A4, tmp2_lsb));
472done;
473
474(A4c : make_array (any, 3, 7, 19),
475 read_binary_array (tmp2_lsb, A4c),
476 is (A4c = A4));
477true;
478
479is (listarray (read_binary_array (tmp2_lsb)) = L4);
480true;
481
482/* Read specific number of data via optional argument */
483
484(l1 : read_list (file_search ("ragged.data")),
485 l2 : read_list (file_search ("ragged.data"), false, 20));
486''(makelist (l1 [i], i, 1, 20));
487
488(s : openr (file_search ("ragged.data")),
489 l2 : read_list (s, false, 20),
490 l3 : read_list (s, false, 12),
491 l4 : read_list (s),
492 close (s),
493 [length (l2), length (l3), length (l4), append (l2, l3, l4)]);
494''([20, 12, length (l1) - (20 + 12), l1]);
495
496(assume_external_byte_order (lsb),
497 l1 : read_binary_list (tmp2_lsb),
498 l2 : read_binary_list (tmp2_lsb, 50));
499''(makelist (l1 [i], i, 1, 50));
500
501(s : openr_binary (tmp2_lsb),
502 l2 : read_binary_list (s, 50),
503 l3 : read_binary_list (s, 30),
504 l4 : read_binary_list (s),
505 close (s),
506 [length (l2), length (l3), length (l4), append (l2, l3, l4)]);
507''([50, 30, length (l1) - (50 + 30), l1]);
508
509/* NEED ADDITIONAL TESTS HERE FOR PARTIAL READS TO PRODUCE ARRAYS */
510
511/* read from a file and return a new array */
512
513block ([a, l],
514  kill (a), /* ensure no declared array */
515  l : read_list (file_search ("rectangular.data")),
516  a : read_array (file_search ("rectangular.data")),
517  [is (length (l) = ?length (a)), every (makelist (l[i] = a[i - 1], i, length (l)))]);
518[true, true];
519
520block ([a, l],
521  l : read_list (file_search ("rectangular.csv")),
522  a : read_array (file_search ("rectangular.csv")),
523  [is (length (l) = ?length (a)), every (makelist (l[i] = a[i - 1], i, length (l)))]);
524[true, true];
525
526block ([a, l],
527  l : read_list (file_search ("really-csv.data"), 'comma),
528  a : read_array (file_search ("really-csv.data"), 'comma),
529  [is (length (l) = ?length (a)), every (makelist (l[i] = a[i - 1], i, length (l)))]);
530[true, true];
531
532block ([a, l],
533  l : read_list (file_search ("really-space-separated.csv"), 'space),
534  a : read_array (file_search ("really-space-separated.csv"), 'space),
535  [is (length (l) = ?length (a)), every (makelist (l[i] = a[i - 1], i, length (l)))]);
536[true, true];
537
538