1 /* Test read_list parsing -- verify expected handling of separator with missing value */
3 (read_from_string (f, S, sep) :=
5 S1: make_string_input_stream (S),
6 L: apply (f, [S1, sep]),
9 kill (a, b, c, d, e, f, g, h, i));
15 read_from_string (read_list, bb, 'comma));
20 read_from_string (read_nested_list, bb, 'comma);
28 read_from_string (read_list, cc, 'comma));
29 [false, a, b, c, false,
30 false, d, e, f, false,
31 false, g, h, i, false];
33 read_from_string (read_nested_list, cc, 'comma);
34 [[false, a, b, c, false],
35 [false, d, e, f, false],
36 [false, g, h, i, false]];
41 read_from_string (read_list, dd, 'comma));
46 read_from_string (read_nested_list, dd, 'comma);
54 read_from_string (read_list, ee, 'comma));
59 read_from_string (read_nested_list, ee, 'comma);
67 read_from_string (read_list, ff, 'comma));
68 [a, false, false, b, false, false, c,
69 d, false, false, e, false, false, f,
70 g, false, false, h, false, false, i];
72 read_from_string (read_nested_list, ff, 'comma);
73 [[a, false, false, b, false, false, c],
74 [d, false, false, e, false, false, f],
75 [g, false, false, h, false, false, i]];
77 /* Test read/write array */
86 2.5, 19.7, 1.2, 9.1, 1.7,
87 3.5, -8.2, 3.1, 7.2, 7.1,
88 5.4, -5.3, 9.8, 1.8, 6.2,
89 0.8, 52.9, 7.1, 0.2, 1.8,
91 baz, 79, 112, 173, 311,
92 18, 97, 211, 317, 113,
93 29, 32, 925, 512, 713,
94 87, 53, 133, 127, 158],
95 a : make_array ('any, 3, 4, 5),
98 /* Note that this makes b the same size as a,
99 * despite the different arguments ... <sigh>
105 a2 : make_array ('any, 3, 4, 5),
110 (read_array (file_search ("array.data"), a2),
114 (read_array (file_search ("array.data"), b2),
118 (write_data (a2, sconcat (maxima_tempdir, "/tmp-lisp-array.data")),
119 a3: make_array('any, 3, 4, 5),
120 read_array (sconcat (maxima_tempdir, "/tmp-lisp-array.data"), a3),
121 is (equal (a2, a3)));
124 (write_data (b2, sconcat (maxima_tempdir, "/tmp-maxima-array.data")),
126 read_array (sconcat (maxima_tempdir, "/tmp-maxima-array.data"), b3),
127 is (equal (b2, b3)));
130 /* Test read/write comma-separated data */
134 [[KEY, XX, YY, ZZ, FOO, Bar, baz],
135 [aa46, 7, 7, 16, FOO3, 2.71, 2.88],
136 [aA31, 31, 61, 25, 916, 6.10, 741],
137 [AA22, 61, 40, 13, 32.75, Bar2, 9.12],
138 [AA16, 19, 25, 31, FOO7, 4.80, 697],
139 [AA16, 13, 49, 7, 5.67, 2.54, 4.54],
140 [AA58, 19, 28, 43, 4.04, 0.90, baz8],
141 [AA34, 16, 22, 19, 4.87, 4.90, 2.85],
142 [AA52, 28, 34, 7, FOO9, Bar4, 293],
143 [AA52, 10, 43, 13, 12.125, 5.69, 0.00],
144 [AA40, 7, 25, 40, 3.15, 7.54, 6.11]],
145 m : apply(matrix, l_nested),
149 (m2 : read_matrix (file_search ("rectangular.csv")),
153 (m3 : read_matrix (file_search ("really-csv.data"), 'comma),
157 (m3a : read_matrix (file_search ("really-csv.data"), ","),
161 (m4 : read_matrix (file_search ("really-space-separated.csv"), 'space),
165 (m4a : read_matrix (file_search ("really-space-separated.csv"), " "),
169 (write_data (m2, sconcat (maxima_tempdir, "/tmp-matrix.csv")),
170 m5 : read_matrix(sconcat (maxima_tempdir, "/tmp-matrix.csv")),
174 (write_data (m2, sconcat (maxima_tempdir, "/tmp-matrix.data", 'comma)),
175 m6 : read_matrix(sconcat (maxima_tempdir, "/tmp-matrix.data", 'comma)),
179 (write_data (m2, sconcat (maxima_tempdir, "/tmp-matrix.data", ",")),
180 m6a : read_matrix(sconcat (maxima_tempdir, "/tmp-matrix.data", ",")),
184 (write_data (m2, sconcat (maxima_tempdir, "/tmp-matrix-2.csv", 'space)),
185 m7 : read_matrix(sconcat (maxima_tempdir, "/tmp-matrix-2.csv", 'space)),
189 (write_data (m2, sconcat (maxima_tempdir, "/tmp-matrix-2.csv", " ")),
190 m7a : read_matrix(sconcat (maxima_tempdir, "/tmp-matrix-2.csv", " ")),
195 read_array (file_search ("rectangular.data"), a),
196 write_data (a, sconcat (maxima_tempdir, "/tmp-array.csv")),
198 read_array (sconcat (maxima_tempdir, "/tmp-array.csv"), a2),
202 /* Test read/write variable-length lines */
207 [aA31,31,61,25,9.16,513],
208 [AA16,13,49,7,5.67,2.54,4.54],
209 [AA52,7,FOO9,Bar4,2.93],
210 [AA40,7,25,40,3.15,7.54,0.00],
211 [aa46,1325,7,16,FOO3,2.71,2.88],
212 [aA31,31,61,25,9.16],
213 [AA58,19,28,43,4.04,0.90,baz8],
214 [AA34,16,175.25,2.85],
216 [AA22,617,40,Bar2,9.12],
217 [zz7b,12375e-003,12375s-003,12375d-003,123b-001],
218 [w23q,123.375e-001,123.375s-001,123.375d-001,123.456b-001],
219 [q32w,12375E-003,12375S-003,12375D-003,123B-001],
220 [t5u7,123.375E-001,123.375S-001,123.375D-001,123.456B-001],
221 [q129,"Foo","BAR","Foo \\ BAR \"baz quux\""]],
222 l : flatten (l_nested),
223 h[aa46] : [7,7,16,FOO3],
224 h[aA31] : [31,61,25,9.16,513],
225 h[AA16] : [13,49,7,5.67,2.54,4.54],
226 h[AA52] : [7,FOO9,Bar4,2.93],
227 h[AA40] : [7,25,40,3.15,7.54,0.00],
228 h[aa46] : [1325,7,16,FOO3,2.71,2.88],
229 h[aA31] : [31,61,25,9.16],
230 h[AA58] : [19,28,43,4.04,0.90,baz8],
231 h[AA34] : [16,175.25,2.85],
233 h[AA22] : [617,40,Bar2,9.12],
234 h[zz7b] : [12375e-003,12375s-003,12375d-003,123b-001],
235 h[w23q] : [123.375e-001,123.375s-001,123.375d-001,123.456b-001],
236 h[q32w] : [12375E-003,12375S-003,12375D-003,123B-001],
237 h[t5u7] : [123.375E-001,123.375S-001,123.375D-001,123.456B-001],
238 h[q129] : ["Foo","BAR","Foo \\ BAR \"baz quux\""],
242 (l2 : read_list (file_search ("ragged.data")),
246 (l2_nested: read_nested_list (file_search ("ragged.data")),
247 is (l_nested = l2_nested));
250 (read_hashed_array (file_search ("ragged.data"), h2),
268 is (ev (l) = ev (l2)));
271 /* Test read/write rectangular (same number of items on each line) data */
275 [[KEY, XX, YY, ZZ, FOO, Bar, baz],
276 [aa46, 7, 7, 16, FOO3, 2.71, 2.88],
277 [aA31, 31, 61, 25, 916, 6.10, 741],
278 [AA22, 61, 40, 13, 32.75, Bar2, 9.12],
279 [AA16, 19, 25, 31, FOO7, 4.80, 697],
280 [AA16, 13, 49, 7, 5.67, 2.54, 4.54],
281 [AA58, 19, 28, 43, 4.04, 0.90, baz8],
282 [AA34, 16, 22, 19, 4.87, 4.90, 2.85],
283 [AA52, 28, 34, 7, FOO9, Bar4, 293],
284 [AA52, 10, 43, 13, 12.125, 5.69, 0.00],
285 [AA40, 7, 25, 40, 3.15, 7.54, 6.11]],
286 m : apply (matrix, l_nested),
287 for i:1 thru length (l_nested)
288 do h [l_nested [i] [1]] : rest (l_nested [i]),
289 l: flatten (l_nested),
293 (m2: read_matrix (file_search ("rectangular.data")),
297 (read_hashed_array (file_search ("rectangular.data"), h2),
301 (l2_nested : read_nested_list (file_search ("rectangular.data")),
302 is (l_nested = l2_nested));
305 (l2 : read_list (file_search ("rectangular.data")),
309 (write_data (m2, sconcat (maxima_tempdir, "/tmp-matrix.data")),
310 l3_nested : read_nested_list (sconcat (maxima_tempdir, "/tmp-matrix.data")),
311 is (l2_nested = l3_nested));
314 (write_data (h2, sconcat (maxima_tempdir, "/tmp-hashed-array.data")),
315 read_hashed_array (sconcat (maxima_tempdir, "/tmp-hashed-array.data"), h3),
316 is (equal (h2, h3)));
319 (write_data (l2_nested, sconcat (maxima_tempdir, "/tmp-nested-list.data")),
320 m3 : read_matrix (sconcat (maxima_tempdir, "/tmp-nested-list.data")),
324 (write_data (l2, sconcat (maxima_tempdir, "/tmp-list.data")),
325 l3 : read_list (sconcat (maxima_tempdir, "/tmp-list.data")),
353 is (ev (m) = ev (m2)));
356 /* Test read/write with different separator characters */
362 [false, false, false],
363 [false, false, false, false],
375 [Foo, BAR, baz, embedded\ space\ and\ \$p\*cia\!\ c\#\@rs],
376 [12.34, Foo, 12375e-3, BAR, 1234b-2, baz, 1234],
377 ["tab space comma, semicolon; pipe| dollar$", "quux \"blart\"", ?a\-lisp\-symbol],
384 (l_nested_2 : read_nested_list (file_search ("semicolon.data"), 'semicolon),
385 is (l_nested = l_nested_2));
388 (write_data (l_nested_2, sconcat (maxima_tempdir, "/tmp-semicolon.data"), ";"),
389 l_nested_2a : read_nested_list (sconcat (maxima_tempdir, "/tmp-semicolon.data"), ";"),
390 is (l_nested = l_nested_2a));
393 (l_nested_3 : read_nested_list (file_search ("pipe.data"), 'pipe),
394 is (l_nested = l_nested_3));
397 (write_data (l_nested_3, sconcat (maxima_tempdir, "/tmp-pipe.data"), "|"),
398 l_nested_3a : read_nested_list (sconcat (maxima_tempdir, "/tmp-pipe.data"), "|"),
399 is (l_nested = l_nested_3a));
402 (write_data (l_nested, sconcat (maxima_tempdir, "/tmp-tab.data"), 'tab),
403 /* tab not recognized by read_nested_list, since Maxima parser
404 * (barring hackery to recognize tab characters as tokens)
405 * just burns through multiple tabs.
407 l_nested_4: read_nested_list (sconcat (maxima_tempdir, "/tmp-tab.data"), 'space),
408 is (l_nested = l_nested_4));
411 /* ASCII character 9 is the tab character. ascii(9) returns a 1-element string. */
412 (write_data (l_nested, sconcat (maxima_tempdir, "/tmp-tab.data"), ascii(9)),
413 l_nested_4a: read_nested_list (sconcat (maxima_tempdir, "/tmp-tab.data"), " "),
414 is (l_nested = l_nested_4a));
417 /* Test read/write on streams (instead of files) */
423 (a1 : make_array ('any, 3, 4, 5),
424 read_array (file_search ("array.data"), a1),
425 a2 : make_array ('any, 3, 4, 5),
426 s : openr (file_search ("array.data")),
429 is (equal (a1, a2)));
432 (s : openw (sconcat (maxima_tempdir, "/tmp-lisp-array.data")),
435 a3 : make_array ('any, 3, 4, 5),
436 s : openr (sconcat (maxima_tempdir, "/tmp-lisp-array.data")),
439 is (equal (a1, a3)));
442 (b1 : array (b, 2, 3, 4),
443 read_array (file_search ("array.data"), b1),
444 b2 : array (b, 2, 3, 4),
445 s : openr (file_search ("array.data")),
448 is (equal (b1, b2)));
451 (s : openw (sconcat (maxima_tempdir, "/tmp-maxima-array.data")),
454 b3 : array (b, 2, 3, 4),
455 s : openr (sconcat (maxima_tempdir, "/tmp-maxima-array.data")),
458 is (equal (b1, b3)));
461 (m1 : read_matrix (file_search ("rectangular.data")),
462 s : openr (file_search ("rectangular.data")),
463 m2 : read_matrix (s),
468 (s : openw (sconcat (maxima_tempdir, "/tmp-matrix.data")),
471 s : openr (sconcat (maxima_tempdir, "/tmp-matrix.data")),
472 m3 : read_matrix (s),
477 (l1 : read_list (file_search ("ragged.data")),
478 s : openr (file_search ("ragged.data")),
484 (s : openw (sconcat (maxima_tempdir, "/tmp-list.data")),
487 s : openr (sconcat (maxima_tempdir, "/tmp-list.data")),
493 (l1 : read_nested_list (file_search ("ragged.data")),
494 s : openr (file_search ("ragged.data")),
495 l2 : read_nested_list (s),
500 (s : openw (sconcat (maxima_tempdir, "/tmp-nested-list.data")),
503 s : openr (sconcat (maxima_tempdir, "/tmp-nested-list.data")),
504 l3 : read_nested_list (s),
509 (read_hashed_array (file_search ("ragged.data"), h1),
510 s : openr (file_search ("ragged.data")),
511 read_hashed_array (s, h2),
513 is (equal (h1, h2)));
516 (s : openw (sconcat (maxima_tempdir, "/tmp-hashed-array.data")),
519 s : openr (sconcat (maxima_tempdir, "/tmp-hashed-array.data")),
520 read_hashed_array (s, h3),
522 is (equal (h1, h3)));
525 /* Write and read binary data */
527 (assume_external_byte_order (lsb),
528 read_binary_list (file_search ("test-binary.f8-lsb")));
529 [-1.125, 0.375, 3.750, -7.875, 2.625];
531 (assume_external_byte_order (lsb),
533 read_binary_list (file_search ("test-binary.f8-lsb"), L),
535 [-1.125, 0.375, 3.750, -7.875, 2.625];
537 (assume_external_byte_order (msb),
538 read_binary_list (file_search ("test-binary.f8-msb")));
539 [-1.125, 0.375, 3.750, -7.875, 2.625];
541 (assume_external_byte_order (msb),
543 read_binary_list (file_search ("test-binary.f8-msb"), L),
545 [-1.125, 0.375, 3.750, -7.875, 2.625];
547 (tmp_msb : sconcat (maxima_tempdir, "/tmp-binary.f8-msb"),
548 tmp_lsb : sconcat (maxima_tempdir, "/tmp-binary.f8-lsb"),
549 L : makelist (11.0^(3*i), i, 1, 98),
550 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]),
554 (assume_external_byte_order (lsb),
555 write_binary_data (L4, tmp_lsb));
558 (L4b : read_binary_list (tmp_lsb),
562 (assume_external_byte_order (msb),
563 write_binary_data (L4, tmp_msb));
566 (L4c : read_binary_list (tmp_msb),
570 (A4 : make_array (any, 3, 7, 19),
575 (tmp2_msb : sconcat (maxima_tempdir, "/tmp2-binary.f8-msb"),
576 tmp2_lsb : sconcat (maxima_tempdir, "/tmp2-binary.f8-lsb"),
577 assume_external_byte_order (msb),
578 write_binary_data (A4, tmp2_msb));
581 (A4b : make_array (any, 3, 7, 19),
582 read_binary_array (tmp2_msb, A4b),
586 is (listarray (read_binary_array (tmp2_msb)) = L4);
589 (assume_external_byte_order (lsb),
590 write_binary_data (A4, tmp2_lsb));
593 (A4c : make_array (any, 3, 7, 19),
594 read_binary_array (tmp2_lsb, A4c),
598 is (listarray (read_binary_array (tmp2_lsb)) = L4);
601 /* Read specific number of data via optional argument */
603 (l1 : read_list (file_search ("ragged.data")),
604 l2 : read_list (file_search ("ragged.data"), false, 20));
605 ''(makelist (l1 [i], i, 1, 20));
607 (s : openr (file_search ("ragged.data")),
608 l2 : read_list (s, false, 20),
609 l3 : read_list (s, false, 12),
612 [length (l2), length (l3), length (l4), append (l2, l3, l4)]);
613 ''([20, 12, length (l1) - (20 + 12), l1]);
615 (assume_external_byte_order (lsb),
616 l1 : read_binary_list (tmp2_lsb),
617 l2 : read_binary_list (tmp2_lsb, 50));
618 ''(makelist (l1 [i], i, 1, 50));
620 (s : openr_binary (tmp2_lsb),
621 l2 : read_binary_list (s, 50),
622 l3 : read_binary_list (s, 30),
623 l4 : read_binary_list (s),
625 [length (l2), length (l3), length (l4), append (l2, l3, l4)]);
626 ''([50, 30, length (l1) - (50 + 30), l1]);
628 /* NEED ADDITIONAL TESTS HERE FOR PARTIAL READS TO PRODUCE ARRAYS */
630 /* read from a file and return a new array */
633 kill (a), /* ensure no declared array */
634 l : read_list (file_search ("rectangular.data")),
635 a : read_array (file_search ("rectangular.data")),
636 [is (length (l) = ?length (a)), every (makelist (l[i] = a[i - 1], i, length (l)))]);
640 l : read_list (file_search ("rectangular.csv")),
641 a : read_array (file_search ("rectangular.csv")),
642 [is (length (l) = ?length (a)), every (makelist (l[i] = a[i - 1], i, length (l)))]);
646 l : read_list (file_search ("really-csv.data"), 'comma),
647 a : read_array (file_search ("really-csv.data"), 'comma),
648 [is (length (l) = ?length (a)), every (makelist (l[i] = a[i - 1], i, length (l)))]);
652 l : read_list (file_search ("really-space-separated.csv"), 'space),
653 a : read_array (file_search ("really-space-separated.csv"), 'space),
654 [is (length (l) = ?length (a)), every (makelist (l[i] = a[i - 1], i, length (l)))]);
657 /* verify that empty lines are ignored by
658 * read_matrix, read_list, read_array, and read_hashed_array,
659 * but not read_nested_list.
678 read_from_string (f, S, [a]) :=
680 S1: make_string_input_stream (S),
681 L: apply (f, cons (S1, a)),
687 read_from_string (read_matrix, zz, 'comma);
688 matrix ([11,22,33,44],
692 [888,999,1111,2222]);
694 read_from_string (read_list, zz, 'comma);
701 (yy: make_array (fixnum, 5*4),
702 read_from_string (read_array, zz, yy, 'comma),
704 ''(make_array (fixnum, 5*4), fillarray (%%, aa), %%);
707 read_from_string (read_hashed_array, zz, hh, 'comma),
708 hh1[11]: [22, 33, 44],
709 hh1[55]: [66, 77, 88],
710 hh1[99]: [111, 222, 333],
711 hh1[444]: [555, 666, 777],
712 hh1[888]: [999, 1111, 2222],
713 is (equal (hh, hh1)));
716 read_from_string (read_nested_list, zz, 'comma);