1 /* Test read/write array */
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,
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),
22 /* Note that this makes b the same size as a,
23 * despite the different arguments ... <sigh>
29 a2 : make_array ('any, 3, 4, 5),
34 (read_array (file_search ("array.data"), a2),
38 (read_array (file_search ("array.data"), b2),
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),
48 (write_data (b2, sconcat (maxima_tempdir, "/tmp-maxima-array.data")),
50 read_array (sconcat (maxima_tempdir, "/tmp-maxima-array.data"), b3),
54 /* Test read/write comma-separated data */
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),
73 (m2 : read_matrix (file_search ("rectangular.csv")),
77 (m3 : read_matrix (file_search ("really-csv.data"), 'comma),
81 (m4 : read_matrix (file_search ("really-space-separated.csv"), 'space),
85 (write_data (m2, sconcat (maxima_tempdir, "/tmp-matrix.csv")),
86 m5 : read_matrix(sconcat (maxima_tempdir, "/tmp-matrix.csv")),
90 (write_data (m2, sconcat (maxima_tempdir, "/tmp-matrix.data", 'comma)),
91 m6 : read_matrix(sconcat (maxima_tempdir, "/tmp-matrix.data", 'comma)),
95 (write_data (m2, sconcat (maxima_tempdir, "/tmp-matrix-2.csv", 'space)),
96 m7 : read_matrix(sconcat (maxima_tempdir, "/tmp-matrix-2.csv", 'space)),
101 read_array (file_search ("rectangular.data"), a),
102 write_data (a, sconcat (maxima_tempdir, "/tmp-array.csv")),
104 read_array (sconcat (maxima_tempdir, "/tmp-array.csv"), a2),
108 /* Test read/write variable-length lines */
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],
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],
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\""],
148 (l2 : read_list (file_search ("ragged.data")),
152 (l2_nested: read_nested_list (file_search ("ragged.data")),
153 is (l_nested = l2_nested));
156 (read_hashed_array (file_search ("ragged.data"), h2),
174 is (ev (l) = ev (l2)));
177 /* Test read/write rectangular (same number of items on each line) data */
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),
199 (m2: read_matrix (file_search ("rectangular.data")),
203 (read_hashed_array (file_search ("rectangular.data"), h2),
207 (l2_nested : read_nested_list (file_search ("rectangular.data")),
208 is (l_nested = l2_nested));
211 (l2 : read_list (file_search ("rectangular.data")),
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));
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)));
225 (write_data (l2_nested, sconcat (maxima_tempdir, "/tmp-nested-list.data")),
226 m3 : read_matrix (sconcat (maxima_tempdir, "/tmp-nested-list.data")),
230 (write_data (l2, sconcat (maxima_tempdir, "/tmp-list.data")),
231 l3 : read_list (sconcat (maxima_tempdir, "/tmp-list.data")),
259 is (ev (m) = ev (m2)));
262 /* Test read/write with different separator characters */
268 [false, false, false],
269 [false, false, false, false],
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]],
285 (l_nested_2 : read_nested_list (file_search ("semicolon.data"), 'semicolon),
286 is (l_nested = l_nested_2));
289 (l_nested_3 : read_nested_list (file_search ("pipe.data"), 'pipe),
290 is (l_nested = l_nested_3));
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));
298 /* Test read/write on streams (instead of files) */
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")),
310 is (equal (a1, a2)));
313 (s : openw (sconcat (maxima_tempdir, "/tmp-lisp-array.data")),
316 a3 : make_array ('any, 3, 4, 5),
317 s : openr (sconcat (maxima_tempdir, "/tmp-lisp-array.data")),
320 is (equal (a1, a3)));
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")),
329 is (equal (b1, b2)));
332 (s : openw (sconcat (maxima_tempdir, "/tmp-maxima-array.data")),
335 b3 : array (b, 2, 3, 4),
336 s : openr (sconcat (maxima_tempdir, "/tmp-maxima-array.data")),
339 is (equal (b1, b3)));
342 (m1 : read_matrix (file_search ("rectangular.data")),
343 s : openr (file_search ("rectangular.data")),
344 m2 : read_matrix (s),
349 (s : openw (sconcat (maxima_tempdir, "/tmp-matrix.data")),
352 s : openr (sconcat (maxima_tempdir, "/tmp-matrix.data")),
353 m3 : read_matrix (s),
358 (l1 : read_list (file_search ("ragged.data")),
359 s : openr (file_search ("ragged.data")),
365 (s : openw (sconcat (maxima_tempdir, "/tmp-list.data")),
368 s : openr (sconcat (maxima_tempdir, "/tmp-list.data")),
374 (l1 : read_nested_list (file_search ("ragged.data")),
375 s : openr (file_search ("ragged.data")),
376 l2 : read_nested_list (s),
381 (s : openw (sconcat (maxima_tempdir, "/tmp-nested-list.data")),
384 s : openr (sconcat (maxima_tempdir, "/tmp-nested-list.data")),
385 l3 : read_nested_list (s),
390 (read_hashed_array (file_search ("ragged.data"), h1),
391 s : openr (file_search ("ragged.data")),
392 read_hashed_array (s, h2),
394 is (equal (h1, h2)));
397 (s : openw (sconcat (maxima_tempdir, "/tmp-hashed-array.data")),
400 s : openr (sconcat (maxima_tempdir, "/tmp-hashed-array.data")),
401 read_hashed_array (s, h3),
403 is (equal (h1, h3)));
406 /* Write and read binary data */
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];
412 (assume_external_byte_order (lsb),
414 read_binary_list (file_search ("test-binary.f8-lsb"), L),
416 [-1.125, 0.375, 3.750, -7.875, 2.625];
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];
422 (assume_external_byte_order (msb),
424 read_binary_list (file_search ("test-binary.f8-msb"), L),
426 [-1.125, 0.375, 3.750, -7.875, 2.625];
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]),
435 (assume_external_byte_order (lsb),
436 write_binary_data (L4, tmp_lsb));
439 (L4b : read_binary_list (tmp_lsb),
443 (assume_external_byte_order (msb),
444 write_binary_data (L4, tmp_msb));
447 (L4c : read_binary_list (tmp_msb),
451 (A4 : make_array (any, 3, 7, 19),
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));
462 (A4b : make_array (any, 3, 7, 19),
463 read_binary_array (tmp2_msb, A4b),
467 is (listarray (read_binary_array (tmp2_msb)) = L4);
470 (assume_external_byte_order (lsb),
471 write_binary_data (A4, tmp2_lsb));
474 (A4c : make_array (any, 3, 7, 19),
475 read_binary_array (tmp2_lsb, A4c),
479 is (listarray (read_binary_array (tmp2_lsb)) = L4);
482 /* Read specific number of data via optional argument */
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));
488 (s : openr (file_search ("ragged.data")),
489 l2 : read_list (s, false, 20),
490 l3 : read_list (s, false, 12),
493 [length (l2), length (l3), length (l4), append (l2, l3, l4)]);
494 ''([20, 12, length (l1) - (20 + 12), l1]);
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));
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),
506 [length (l2), length (l3), length (l4), append (l2, l3, l4)]);
507 ''([50, 30, length (l1) - (50 + 30), l1]);
509 /* NEED ADDITIONAL TESTS HERE FOR PARTIAL READS TO PRODUCE ARRAYS */
511 /* read from a file and return a new array */
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)))]);
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)))]);
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)))]);
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)))]);