Debian Codegraph @

test-suite/tests/arrays.test

raw
1 ;;;; arrays.test --- tests guile's uniform arrays -*- scheme -*-
2 ;;;;
3 ;;;; Copyright 2004, 2006, 2009, 2010, 2011, 2012, 2013, 2014, 2015 Free
4 ;;;; Software Foundation, Inc.
5 ;;;;
6 ;;;; This library is free software; you can redistribute it and/or
7 ;;;; modify it under the terms of the GNU Lesser General Public
8 ;;;; License as published by the Free Software Foundation; either
9 ;;;; version 3 of the License, or (at your option) any later version.
10 ;;;;
11 ;;;; This library is distributed in the hope that it will be useful,
12 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 ;;;; Lesser General Public License for more details.
15 ;;;;
16 ;;;; You should have received a copy of the GNU Lesser General Public
17 ;;;; License along with this library; if not, write to the Free Software
18 ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
20 (define-module (test-suite test-arrays)
21 #:use-module ((system base compile) #:select (compile))
22 #:use-module (test-suite lib)
23 #:use-module (srfi srfi-4)
24 #:use-module (srfi srfi-4 gnu))
25
26 (define (array-row a i)
27 (make-shared-array a (lambda (j) (list i j))
28 (cadr (array-dimensions a))))
29
30 (define (array-col a j)
31 (make-shared-array a (lambda (i) (list i j))
32 (car (array-dimensions a))))
33
34 (define exception:wrong-num-indices
35 (cons 'misc-error "^wrong number of indices.*"))
36
37 (define exception:length-non-negative
38 (cons 'read-error ".*array length must be non-negative.*"))
39
40 (define exception:wrong-type-arg
41 (cons #t "Wrong type"))
42
43 (define exception:mapping-out-of-range
44 (cons 'misc-error "^mapping out of range")) ;; per scm_make_shared_array
45
46 ;;;
47 ;;; array?
48 ;;;
49
50 (with-test-prefix "array?"
51
52 (let ((bool (make-typed-array 'b #t '(5 6)))
53 (char (make-typed-array 'a #\a '(5 6)))
54 (byte (make-typed-array 'u8 0 '(5 6)))
55 (short (make-typed-array 's16 0 '(5 6)))
56 (ulong (make-typed-array 'u32 0 '(5 6)))
57 (long (make-typed-array 's32 0 '(5 6)))
58 (longlong (make-typed-array 's64 0 '(5 6)))
59 (float (make-typed-array 'f32 0 '(5 6)))
60 (double (make-typed-array 'f64 0 '(5 6)))
61 (complex (make-typed-array 'c64 0 '(5 6)))
62 (scm (make-typed-array #t 0 '(5 6))))
63
64 (with-test-prefix "is bool"
65 (pass-if (eq? #t (typed-array? bool 'b)))
66 (pass-if (eq? #f (typed-array? char 'b)))
67 (pass-if (eq? #f (typed-array? byte 'b)))
68 (pass-if (eq? #f (typed-array? short 'b)))
69 (pass-if (eq? #f (typed-array? ulong 'b)))
70 (pass-if (eq? #f (typed-array? long 'b)))
71 (pass-if (eq? #f (typed-array? longlong 'b)))
72 (pass-if (eq? #f (typed-array? float 'b)))
73 (pass-if (eq? #f (typed-array? double 'b)))
74 (pass-if (eq? #f (typed-array? complex 'b)))
75 (pass-if (eq? #f (typed-array? scm 'b))))
76
77 (with-test-prefix "is char"
78 (pass-if (eq? #f (typed-array? bool 'a)))
79 (pass-if (eq? #t (typed-array? char 'a)))
80 (pass-if (eq? #f (typed-array? byte 'a)))
81 (pass-if (eq? #f (typed-array? short 'a)))
82 (pass-if (eq? #f (typed-array? ulong 'a)))
83 (pass-if (eq? #f (typed-array? long 'a)))
84 (pass-if (eq? #f (typed-array? longlong 'a)))
85 (pass-if (eq? #f (typed-array? float 'a)))
86 (pass-if (eq? #f (typed-array? double 'a)))
87 (pass-if (eq? #f (typed-array? complex 'a)))
88 (pass-if (eq? #f (typed-array? scm 'a))))
89
90 (with-test-prefix "is byte"
91 (pass-if (eq? #f (typed-array? bool 'u8)))
92 (pass-if (eq? #f (typed-array? char 'u8)))
93 (pass-if (eq? #t (typed-array? byte 'u8)))
94 (pass-if (eq? #f (typed-array? short 'u8)))
95 (pass-if (eq? #f (typed-array? ulong 'u8)))
96 (pass-if (eq? #f (typed-array? long 'u8)))
97 (pass-if (eq? #f (typed-array? longlong 'u8)))
98 (pass-if (eq? #f (typed-array? float 'u8)))
99 (pass-if (eq? #f (typed-array? double 'u8)))
100 (pass-if (eq? #f (typed-array? complex 'u8)))
101 (pass-if (eq? #f (typed-array? scm 'u8))))
102
103 (with-test-prefix "is short"
104 (pass-if (eq? #f (typed-array? bool 's16)))
105 (pass-if (eq? #f (typed-array? char 's16)))
106 (pass-if (eq? #f (typed-array? byte 's16)))
107 (pass-if (eq? #t (typed-array? short 's16)))
108 (pass-if (eq? #f (typed-array? ulong 's16)))
109 (pass-if (eq? #f (typed-array? long 's16)))
110 (pass-if (eq? #f (typed-array? longlong 's16)))
111 (pass-if (eq? #f (typed-array? float 's16)))
112 (pass-if (eq? #f (typed-array? double 's16)))
113 (pass-if (eq? #f (typed-array? complex 's16)))
114 (pass-if (eq? #f (typed-array? scm 's16))))
115
116 (with-test-prefix "is ulong"
117 (pass-if (eq? #f (typed-array? bool 'u32)))
118 (pass-if (eq? #f (typed-array? char 'u32)))
119 (pass-if (eq? #f (typed-array? byte 'u32)))
120 (pass-if (eq? #f (typed-array? short 'u32)))
121 (pass-if (eq? #t (typed-array? ulong 'u32)))
122 (pass-if (eq? #f (typed-array? long 'u32)))
123 (pass-if (eq? #f (typed-array? longlong 'u32)))
124 (pass-if (eq? #f (typed-array? float 'u32)))
125 (pass-if (eq? #f (typed-array? double 'u32)))
126 (pass-if (eq? #f (typed-array? complex 'u32)))
127 (pass-if (eq? #f (typed-array? scm 'u32))))
128
129 (with-test-prefix "is long"
130 (pass-if (eq? #f (typed-array? bool 's32)))
131 (pass-if (eq? #f (typed-array? char 's32)))
132 (pass-if (eq? #f (typed-array? byte 's32)))
133 (pass-if (eq? #f (typed-array? short 's32)))
134 (pass-if (eq? #f (typed-array? ulong 's32)))
135 (pass-if (eq? #t (typed-array? long 's32)))
136 (pass-if (eq? #f (typed-array? longlong 's32)))
137 (pass-if (eq? #f (typed-array? float 's32)))
138 (pass-if (eq? #f (typed-array? double 's32)))
139 (pass-if (eq? #f (typed-array? complex 's32)))
140 (pass-if (eq? #f (typed-array? scm 's32))))
141
142 (with-test-prefix "is long long"
143 (pass-if (eq? #f (typed-array? bool 's64)))
144 (pass-if (eq? #f (typed-array? char 's64)))
145 (pass-if (eq? #f (typed-array? byte 's64)))
146 (pass-if (eq? #f (typed-array? short 's64)))
147 (pass-if (eq? #f (typed-array? ulong 's64)))
148 (pass-if (eq? #f (typed-array? long 's64)))
149 (pass-if (eq? #t (typed-array? longlong 's64)))
150 (pass-if (eq? #f (typed-array? float 's64)))
151 (pass-if (eq? #f (typed-array? double 's64)))
152 (pass-if (eq? #f (typed-array? complex 's64)))
153 (pass-if (eq? #f (typed-array? scm 's64))))
154
155 (with-test-prefix "is float"
156 (pass-if (eq? #f (typed-array? bool 'f32)))
157 (pass-if (eq? #f (typed-array? char 'f32)))
158 (pass-if (eq? #f (typed-array? byte 'f32)))
159 (pass-if (eq? #f (typed-array? short 'f32)))
160 (pass-if (eq? #f (typed-array? ulong 'f32)))
161 (pass-if (eq? #f (typed-array? long 'f32)))
162 (pass-if (eq? #f (typed-array? longlong 'f32)))
163 (pass-if (eq? #t (typed-array? float 'f32)))
164 (pass-if (eq? #f (typed-array? double 'f32)))
165 (pass-if (eq? #f (typed-array? complex 'f32)))
166 (pass-if (eq? #f (typed-array? scm 'f32))))
167
168 (with-test-prefix "is double"
169 (pass-if (eq? #f (typed-array? bool 'f64)))
170 (pass-if (eq? #f (typed-array? char 'f64)))
171 (pass-if (eq? #f (typed-array? byte 'f64)))
172 (pass-if (eq? #f (typed-array? short 'f64)))
173 (pass-if (eq? #f (typed-array? ulong 'f64)))
174 (pass-if (eq? #f (typed-array? long 'f64)))
175 (pass-if (eq? #f (typed-array? longlong 'f64)))
176 (pass-if (eq? #f (typed-array? float 'f64)))
177 (pass-if (eq? #t (typed-array? double 'f64)))
178 (pass-if (eq? #f (typed-array? complex 'f64)))
179 (pass-if (eq? #f (typed-array? scm 'f64))))
180
181 (with-test-prefix "is complex"
182 (pass-if (eq? #f (typed-array? bool 'c64)))
183 (pass-if (eq? #f (typed-array? char 'c64)))
184 (pass-if (eq? #f (typed-array? byte 'c64)))
185 (pass-if (eq? #f (typed-array? short 'c64)))
186 (pass-if (eq? #f (typed-array? ulong 'c64)))
187 (pass-if (eq? #f (typed-array? long 'c64)))
188 (pass-if (eq? #f (typed-array? longlong 'c64)))
189 (pass-if (eq? #f (typed-array? float 'c64)))
190 (pass-if (eq? #f (typed-array? double 'c64)))
191 (pass-if (eq? #t (typed-array? complex 'c64)))
192 (pass-if (eq? #f (typed-array? scm 'c64))))
193
194 (with-test-prefix "is scm"
195 (pass-if (eq? #f (typed-array? bool #t)))
196 (pass-if (eq? #f (typed-array? char #t)))
197 (pass-if (eq? #f (typed-array? byte #t)))
198 (pass-if (eq? #f (typed-array? short #t)))
199 (pass-if (eq? #f (typed-array? ulong #t)))
200 (pass-if (eq? #f (typed-array? long #t)))
201 (pass-if (eq? #f (typed-array? longlong #t)))
202 (pass-if (eq? #f (typed-array? float #t)))
203 (pass-if (eq? #f (typed-array? double #t)))
204 (pass-if (eq? #f (typed-array? complex #t)))
205 (pass-if (eq? #t (typed-array? scm #t))))
206
207 (with-test-prefix "typed-array? returns #f"
208 (pass-if (eq? #f (typed-array? '(1 2 3) 'c64)))
209 (pass-if (eq? #f (typed-array? '(1 2 3) #t)))
210 (pass-if (eq? #f (typed-array? 99 'c64)))
211 (pass-if (eq? #f (typed-array? 99 #t))))))
212
213 ;;;
214 ;;; array-equal?
215 ;;;
216
217 (with-test-prefix/c&e "array-equal?"
218
219 (pass-if "#s16(...)"
220 (array-equal? #s16(1 2 3) #s16(1 2 3)))
221
222 (pass-if "#0f64(...)"
223 (array-equal? #0f64(99) (make-typed-array 'f64 99)))
224
225 (pass-if "#0(...)"
226 (array-equal? #0(99) (make-array 99))))
227
228 ;;;
229 ;;; make-shared-array
230 ;;;
231
232 (with-test-prefix/c&e "make-shared-array"
233
234 ;; this failed in guile 1.8.0
235 (pass-if "vector unchanged"
236 (let* ((a (make-array #f '(0 7)))
237 (s (make-shared-array a list '(0 7))))
238 (array-equal? a s)))
239
240 (pass-if-exception "vector, high too big" exception:mapping-out-of-range
241 (let* ((a (make-array #f '(0 7))))
242 (make-shared-array a list '(0 8))))
243
244 (pass-if-exception "vector, low too big" exception:out-of-range
245 (let* ((a (make-array #f '(0 7))))
246 (make-shared-array a list '(-1 7))))
247
248 (pass-if "truncate columns"
249 (array-equal? (make-shared-array #2((a b c) (d e f) (g h i)) list 3 2)
250 #2((a b) (d e) (g h))))
251
252 (pass-if "pick one column"
253 (array-equal? (make-shared-array #2((a b c) (d e f) (g h i))
254 (lambda (i) (list i 2))
255 '(0 2))
256 #(c f i)))
257
258 (pass-if "diagonal"
259 (array-equal? (make-shared-array #2((a b c) (d e f) (g h i))
260 (lambda (i) (list i i))
261 '(0 2))
262 #(a e i)))
263
264 ;; this failed in guile 1.8.0
265 (pass-if "2 dims from 1 dim"
266 (array-equal? (make-shared-array #1(a b c d e f g h i j k l)
267 (lambda (i j) (list (+ (* i 3) j)))
268 4 3)
269 #2((a b c) (d e f) (g h i) (j k l))))
270
271 (pass-if "reverse columns"
272 (array-equal? (make-shared-array #2((a b c) (d e f) (g h i))
273 (lambda (i j) (list i (- 2 j)))
274 3 3)
275 #2((c b a) (f e d) (i h g))))
276
277 (pass-if "fixed offset, 0 based becomes 1 based"
278 (let* ((x #2((a b c) (d e f) (g h i)))
279 (y (make-shared-array x
280 (lambda (i j) (list (1- i) (1- j)))
281 '(1 3) '(1 3))))
282 (and (eq? (array-ref x 0 0) 'a)
283 (eq? (array-ref y 1 1) 'a))))
284
285 ;; this failed in guile 1.8.0
286 (pass-if "stride every third element"
287 (array-equal? (make-shared-array #1(a b c d e f g h i j k l)
288 (lambda (i) (list (* i 3)))
289 4)
290 #1(a d g j)))
291
292 (pass-if "shared of shared"
293 (let* ((a #2((1 2 3) (4 5 6) (7 8 9)))
294 (s1 (make-shared-array a (lambda (i) (list i 1)) 3))
295 (s2 (make-shared-array s1 list '(1 2))))
296 (and (eqv? 5 (array-ref s2 1))
297 (eqv? 8 (array-ref s2 2))))))
298
299
300 ;;;
301 ;;; array-slice
302 ;;;
303
304 (with-test-prefix/c&e "array-slice"
305
306 (pass-if "vector I"
307 (let ((v (vector 1 2 3)))
308 (array-fill! (array-slice v 1) 'a)
309 (array-equal? v #(1 a 3))))
310
311 (pass-if "vector II"
312 (let ((v (vector 1 2 3)))
313 (array-copy! #(a b c) (array-slice v))
314 (array-equal? v #(a b c))))
315
316 (pass-if "array I"
317 (let ((a (list->array 2 '((1 2 3) (4 5 6)))))
318 (array-fill! (array-slice a 1 1) 'a)
319 (array-equal? a #2((1 2 3) (4 a 6)))))
320
321 (pass-if "array II"
322 (let ((a (list->array 2 '((1 2 3) (4 5 6)))))
323 (array-copy! #(a b c) (array-slice a 1))
324 (array-equal? a #2((1 2 3) (a b c)))))
325
326 (pass-if "array III"
327 (let ((a (list->array 2 '((1 2 3) (4 5 6)))))
328 (array-copy! #2((a b c) (x y z)) (array-slice a))
329 (array-equal? a #2((a b c) (x y z)))))
330
331 (pass-if "rank 0 array"
332 (let ((a (make-array 77)))
333 (array-fill! (array-slice a) 'a)
334 (array-equal? a #0(a)))))
335
336
337 ;;;
338 ;;; array-cell-ref
339 ;;;
340
341 (with-test-prefix/c&e "array-cell-ref"
342
343 (pass-if "vector I"
344 (let ((v (vector 1 2 3)))
345 (equal? 2 (array-cell-ref v 1))))
346
347 (pass-if "vector II"
348 (let ((v (vector 1 2 3)))
349 (array-copy! #(a b c) (array-cell-ref v))
350 (array-equal? v #(a b c))))
351
352 (pass-if "array I"
353 (let ((a (list->array 2 '((1 2 3) (4 5 6)))))
354 (equal? 5 (array-cell-ref a 1 1))))
355
356 (pass-if "array II"
357 (let ((a (list->array 2 '((1 2 3) (4 5 6)))))
358 (array-copy! #(a b c) (array-cell-ref a 1))
359 (array-equal? a #2((1 2 3) (a b c)))))
360
361 (pass-if "array III"
362 (let ((a (list->array 2 '((1 2 3) (4 5 6)))))
363 (array-copy! #2((a b c) (x y z)) (array-cell-ref a))
364 (array-equal? a #2((a b c) (x y z)))))
365
366 (pass-if "rank 0 array"
367 (let ((a (make-array 77)))
368 (equal? (array-cell-ref a) 77))))
369
370
371 ;;;
372 ;;; array-cell-set!
373 ;;;
374
375 (with-test-prefix/c&e "array-cell-set!"
376
377 (pass-if "vector I"
378 (let ((v (vector 1 2 3)))
379 (and (eq? v (array-cell-set! v 'x 1))
380 (array-equal? v #(1 x 3)))))
381
382 (pass-if "vector II"
383 (let ((v (vector 1 2 3)))
384 (and (eq? v (array-cell-set! (array-cell-ref v) #(a b c)))
385 (array-equal? v #(a b c)))))
386
387 (pass-if "array I"
388 (let ((a (list->array 2 '((1 2 3) (4 5 6)))))
389 (and (eq? a (array-cell-set! a 'x 1 1))
390 (array-equal? a #2((1 2 3) (4 x 6))))))
391
392 (pass-if "array II"
393 (let ((a (list->array 2 '((1 2 3) (4 5 6)))))
394 (and (eq? a (array-cell-set! a #(a b c) 1))
395 (array-equal? a #2((1 2 3) (a b c))))))
396
397 (pass-if "array III"
398 (let ((a (list->array 2 '((1 2 3) (4 5 6)))))
399 (and (eq? a (array-cell-set! a #2((a b c) (x y z))))
400 (array-equal? a #2((a b c) (x y z))))))
401
402 (pass-if "rank 0 array"
403 (let ((a (make-array 77)))
404 (and (eq? a (array-cell-set! a 99))
405 (array-equal? a #0(99))))))
406
407
408 ;;;
409 ;;; array-contents
410 ;;;
411
412 (define (every-two x) (make-shared-array x (lambda (i) (list (* i 2))) 2))
413
414 (with-test-prefix/c&e "array-contents"
415
416 (pass-if "0-rank array"
417 (let ((a (make-vector 1 77)))
418 (and
419 (eq? a (array-contents (make-shared-array a (const '(0)))))
420 (eq? a (array-contents (make-shared-array a (const '(0))) #t)))))
421
422 (pass-if "plain vector"
423 (let* ((a (make-array 0 4)))
424 (eq? a (array-contents a))))
425
426 (pass-if "offset vector"
427 (let* ((a (make-array 0 '(1 4))))
428 (array-copy! #(1 2 3 4) (array-contents a))
429 (array-equal? #1@1(1 2 3 4) a)))
430
431 (pass-if "offset vector, strict"
432 (let* ((a (make-array 0 '(1 4))))
433 (array-copy! #(1 2 3 4) (array-contents a #t))
434 (array-equal? #1@1(1 2 3 4) a)))
435
436 (pass-if "stepped vector"
437 (let* ((a (make-array 0 4)))
438 (array-copy! #(99 66) (array-contents (every-two a)))
439 (array-equal? #(99 0 66 0) a)))
440
441 ;; this failed in 2.0.9.
442 (pass-if "stepped vector, strict"
443 (let* ((a (make-array 0 4)))
444 (not (array-contents (every-two a) #t))))
445
446 (pass-if "plain rank 2 array"
447 (let* ((a (make-array 0 2 2)))
448 (array-copy! #(1 2 3 4) (array-contents a #t))
449 (array-equal? #2((1 2) (3 4)) a)))
450
451 (pass-if "offset rank 2 array"
452 (let* ((a (make-array 0 '(1 2) '(1 2))))
453 (array-copy! #(1 2 3 4) (array-contents a #t))
454 (array-equal? #2@1@1((1 2) (3 4)) a)))
455
456 (pass-if "transposed rank 2 array"
457 (let* ((a (make-array 0 4 4)))
458 (not (array-contents (transpose-array a 1 0) #t))))
459
460 ;; This is a consequence of (array-contents? a #t) => #t.
461 (pass-if "empty array"
462 (let ((a (make-typed-array 'f64 2 0 0)))
463 (f64vector? (array-contents a))))
464
465 (pass-if "broadcast vector I"
466 (let* ((a (make-array 0 4))
467 (b (make-shared-array a (lambda (i j k) (list k)) 1 1 4)))
468 (array-copy! #(1 2 3 4) (array-contents b #t))
469 (array-equal? #(1 2 3 4) a)))
470
471 (pass-if "broadcast vector II"
472 (let* ((a (make-array 0 4))
473 (b (make-shared-array a (lambda (i j k) (list k)) 2 1 4)))
474 (not (array-contents b))))
475
476 ;; FIXME maybe this should be allowed.
477 ;; (pass-if "broadcast vector -> empty"
478 ;; (let* ((a (make-array 0 4))
479 ;; (b (make-shared-array a (lambda (i j k) (list k)) 0 1 4)))
480 ;; (if #f #f)))
481
482 (pass-if "broadcast 2-rank I"
483 (let* ((a #2((1 2 3) (4 5 6)))
484 (b (make-shared-array a (lambda (i j) (list 0 j)) 2 3)))
485 (not (array-contents b))))
486
487 (pass-if "broadcast 2-rank II"
488 (let* ((a #2((1 2 3) (4 5 6)))
489 (b (make-shared-array a (lambda (i j) (list i 0)) 2 3)))
490 (not (array-contents b))))
491
492 (pass-if "literal array"
493 (not (not (array-contents #2((1 2 3) (4 5 6)))))))
494
495
496 ;;;
497 ;;; shared-array-root
498 ;;;
499
500 (define amap1 (lambda (i) (list (* 2 i))))
501 (define amap2 (lambda (i j) (list (+ 1 (* 2 i)) (+ 1 (* 2 j)))))
502
503 (with-test-prefix/c&e "shared-array-root"
504
505 (pass-if "plain vector"
506 (let* ((a (make-vector 4 0))
507 (b (make-shared-array a amap1 2)))
508 (eq? (shared-array-root a) (shared-array-root b) (array-contents a))))
509
510 (pass-if "plain array rank 2"
511 (let* ((a (make-array 0 4 4))
512 (b (make-shared-array a amap2 2 2)))
513 (eq? (shared-array-root a) (shared-array-root b) (array-contents a))))
514
515 (pass-if "uniform array rank 2"
516 (let* ((a (make-typed-array 'c64 0 4 4))
517 (b (make-shared-array a amap2 2 2)))
518 (eq? (shared-array-root a) (shared-array-root b) (array-contents a))))
519
520 (pass-if "bit array rank 2"
521 (let* ((a (make-typed-array 'b #f 4 4))
522 (b (make-shared-array a amap2 2 2)))
523 (eq? (shared-array-root a) (shared-array-root b) (array-contents a)))))
524
525 ;;;
526 ;;; shared-array-offset
527 ;;;
528
529 (with-test-prefix/c&e "shared-array-offset"
530
531 (pass-if "plain vector"
532 (zero? (shared-array-offset (make-vector 4 0))))
533
534 (pass-if "plain array rank 2"
535 (zero? (shared-array-offset (make-array 0 4 4))))
536
537 (pass-if "row of rank-2 array, I"
538 (= 0 (shared-array-offset (array-row (make-array 0 5 3) 0))))
539
540 (pass-if "row of rank-2 array, II"
541 (= 4 (shared-array-offset (array-row (make-array 0 6 4) 1))))
542
543 (pass-if "col of rank-2 array, I"
544 (= 0 (shared-array-offset (array-col (make-array 0 5 3) 0))))
545
546 (pass-if "col of rank-2 array, II"
547 (= 1 (shared-array-offset (array-col (make-array 0 6 4) 1)))))
548
549
550 ;;;
551 ;;; shared-array-increments
552 ;;;
553
554 (with-test-prefix "shared-array-increments"
555
556 (pass-if "plain vector"
557 (equal? '(1) (shared-array-increments (make-vector 4 0))))
558
559 (pass-if "plain array rank 2"
560 (equal? '(4 1) (shared-array-increments (make-array 0 3 4))))
561
562 (pass-if "plain array rank 3"
563 (equal? '(20 5 1) (shared-array-increments (make-array 0 3 4 5))))
564
565 (pass-if "row of rank-2 array"
566 (equal? '(1) (shared-array-increments (array-row (make-array 0 5 3) 0))))
567
568 (pass-if "col of rank-2 array"
569 (equal? '(3) (shared-array-increments (array-col (make-array 0 5 3) 0)))))
570
571
572 ;;;
573 ;;; transpose-array
574 ;;;
575
576 ; see strings.test.
577 (with-test-prefix/c&e "transpose-array"
578
579 (pass-if-exception "non array argument" exception:wrong-type-arg
580 (transpose-array 99))
581
582 (pass-if "rank 0"
583 (let* ((a #0(99))
584 (b (transpose-array a)))
585 (and (array-equal? a b)
586 (eq? (shared-array-root a) (shared-array-root b)))))
587
588 (pass-if "rank 1"
589 (let* ((a #(1 2 3))
590 (b (transpose-array a 0)))
591 (and (array-equal? a b)
592 (eq? (shared-array-root a) (shared-array-root b)))))
593
594 (pass-if "rank 2"
595 (let* ((a #2((1 2 3) (4 5 6)))
596 (b (transpose-array a 1 0))
597 (c (transpose-array a 0 1)))
598 (and (array-equal? b #2((1 4) (2 5) (3 6)))
599 (array-equal? c a)
600 (eq? (shared-array-root a)
601 (shared-array-root b)
602 (shared-array-root c)))))
603
604 ; rank > 2 is needed to check against the inverted axis index logic.
605 (pass-if "rank 3"
606 (let* ((a #3(((0 1 2 3) (4 5 6 7) (8 9 10 11))
607 ((12 13 14 15) (16 17 18 19) (20 21 22 23))))
608 (b (transpose-array a 1 2 0)))
609 (and (array-equal? b #3(((0 4 8) (12 16 20)) ((1 5 9) (13 17 21))
610 ((2 6 10) (14 18 22)) ((3 7 11) (15 19 23))))
611 (eq? (shared-array-root a)
612 (shared-array-root b))))))
613
614 ;;;
615 ;;; array->list
616 ;;;
617
618 (with-test-prefix/c&e "array->list"
619 (pass-if-equal "uniform vector" '(1 2 3) (array->list #s16(1 2 3)))
620 (pass-if-equal "vector" '(1 2 3) (array->list #(1 2 3)))
621 (pass-if-equal "rank 2 array" '((1 2) (3 4) (5 6)) (array->list #2((1 2) (3 4) (5 6))))
622 (pass-if-equal "empty vector" '() (array->list #()))
623
624 (pass-if-equal "http://bugs.gnu.org/12465 - ok"
625 '(3 4)
626 (let* ((a #2((1 2) (3 4)))
627 (b (make-shared-array a (lambda (j) (list 1 j)) 2)))
628 (array->list b)))
629 (pass-if-equal "http://bugs.gnu.org/12465 - bad"
630 '(2 4)
631 (let* ((a #2((1 2) (3 4)))
632 (b (make-shared-array a (lambda (i) (list i 1)) 2)))
633 (array->list b))))
634
635 ;;;
636 ;;; array-fill!
637 ;;;
638
639 (with-test-prefix "array-fill!"
640
641 (with-test-prefix "bool"
642 (let ((a (make-bitvector 1 #t)))
643 (pass-if "#f" (array-fill! a #f) #t)
644 (pass-if "#t" (array-fill! a #t) #t)))
645
646 (with-test-prefix "char"
647 (let ((a (make-string 1 #\a)))
648 (pass-if "x" (array-fill! a #\x) #t)))
649
650 (with-test-prefix "byte"
651 (let ((a (make-s8vector 1 0)))
652 (pass-if "0" (array-fill! a 0) #t)
653 (pass-if "127" (array-fill! a 127) #t)
654 (pass-if "-128" (array-fill! a -128) #t)
655 (pass-if-exception "128" exception:out-of-range
656 (array-fill! a 128))
657 (pass-if-exception "-129" exception:out-of-range
658 (array-fill! a -129))
659 (pass-if-exception "symbol" exception:wrong-type-arg
660 (array-fill! a 'symbol))))
661
662 (with-test-prefix "short"
663 (let ((a (make-s16vector 1 0)))
664 (pass-if "0" (array-fill! a 0) #t)
665 (pass-if "123" (array-fill! a 123) #t)
666 (pass-if "-123" (array-fill! a -123) #t)))
667
668 (with-test-prefix "ulong"
669 (let ((a (make-u32vector 1 1)))
670 (pass-if "0" (array-fill! a 0) #t)
671 (pass-if "123" (array-fill! a 123) #t)
672 (pass-if-exception "-123" exception:out-of-range
673 (array-fill! a -123) #t)))
674
675 (with-test-prefix "long"
676 (let ((a (make-s32vector 1 -1)))
677 (pass-if "0" (array-fill! a 0) #t)
678 (pass-if "123" (array-fill! a 123) #t)
679 (pass-if "-123" (array-fill! a -123) #t)))
680
681 (with-test-prefix "float"
682 (let ((a (make-f32vector 1 1.0)))
683 (pass-if "0.0" (array-fill! a 0) #t)
684 (pass-if "123.0" (array-fill! a 123.0) #t)
685 (pass-if "-123.0" (array-fill! a -123.0) #t)
686 (pass-if "0" (array-fill! a 0) #t)
687 (pass-if "123" (array-fill! a 123) #t)
688 (pass-if "-123" (array-fill! a -123) #t)
689 (pass-if "5/8" (array-fill! a 5/8) #t)))
690
691 (with-test-prefix "double"
692 (let ((a (make-f64vector 1 1/3)))
693 (pass-if "0.0" (array-fill! a 0) #t)
694 (pass-if "123.0" (array-fill! a 123.0) #t)
695 (pass-if "-123.0" (array-fill! a -123.0) #t)
696 (pass-if "0" (array-fill! a 0) #t)
697 (pass-if "123" (array-fill! a 123) #t)
698 (pass-if "-123" (array-fill! a -123) #t)
699 (pass-if "5/8" (array-fill! a 5/8) #t)))
700
701 (with-test-prefix "noncompact"
702 (let* ((a (make-array 0 3 3))
703 (b (make-shared-array a (lambda (i) (list i i)) 3)))
704 (array-fill! b 9)
705 (pass-if
706 (and (equal? b #(9 9 9))
707 (equal? a #2((9 0 0) (0 9 0) (0 0 9))))))))
708
709 ;;;
710 ;;; array-in-bounds?
711 ;;;
712
713 (with-test-prefix "array-in-bounds?"
714
715 (pass-if (let ((a (make-array #f '(425 425))))
716 (eq? #f (array-in-bounds? a 0)))))
717
718 ;;;
719 ;;; array-prototype
720 ;;;
721
722 (with-test-prefix "array-type"
723
724 (with-test-prefix "on make-foo-vector"
725
726 (pass-if "bool"
727 (eq? 'b (array-type (make-bitvector 1))))
728
729 (pass-if "char"
730 (eq? 'a (array-type (make-string 1))))
731
732 (pass-if "byte"
733 (eq? 'u8 (array-type (make-u8vector 1))))
734
735 (pass-if "short"
736 (eq? 's16 (array-type (make-s16vector 1))))
737
738 (pass-if "ulong"
739 (eq? 'u32 (array-type (make-u32vector 1))))
740
741 (pass-if "long"
742 (eq? 's32 (array-type (make-s32vector 1))))
743
744 (pass-if "long long"
745 (eq? 's64 (array-type (make-s64vector 1))))
746
747 (pass-if "float"
748 (eq? 'f32 (array-type (make-f32vector 1))))
749
750 (pass-if "double"
751 (eq? 'f64 (array-type (make-f64vector 1))))
752
753 (pass-if "complex"
754 (eq? 'c64 (array-type (make-c64vector 1))))
755
756 (pass-if "scm"
757 (eq? #t (array-type (make-vector 1)))))
758
759 (with-test-prefix "on make-typed-array"
760
761 (let ((types '(b a u8 s8 u16 s16 u32 s32 u64 u64 f32 f64 c32 c64)))
762 (for-each (lambda (type)
763 (pass-if (symbol->string type)
764 (eq? type
765 (array-type (make-typed-array type
766 *unspecified*
767 '(5 6))))))
768 types))))
769
770 ;;;
771 ;;; array-set!
772 ;;;
773
774 (with-test-prefix "array-set!"
775
776 (with-test-prefix "bitvector"
777
778 ;; in Guile 1.8.0 a bug in bitvector_set() caused a segv in array-set!
779 ;; on a bitvector like the following
780 (let ((a (make-bitvector 1)))
781 (pass-if "one elem set #t"
782 (begin
783 (array-set! a #t 0)
784 (eq? #t (array-ref a 0))))
785 (pass-if "one elem set #f"
786 (begin
787 (array-set! a #f 0)
788 (eq? #f (array-ref a 0))))))
789
790 (with-test-prefix "byte"
791
792 (let ((a (make-s8vector 1)))
793
794 (pass-if "-128"
795 (begin (array-set! a -128 0) #t))
796 (pass-if "0"
797 (begin (array-set! a 0 0) #t))
798 (pass-if "127"
799 (begin (array-set! a 127 0) #t))
800 (pass-if-exception "-129" exception:out-of-range
801 (begin (array-set! a -129 0) #t))
802 (pass-if-exception "128" exception:out-of-range
803 (begin (array-set! a 128 0) #t))))
804
805 (with-test-prefix "short"
806
807 (let ((a (make-s16vector 1)))
808 ;; true if n can be array-set! into a
809 (define (fits? n)
810 (false-if-exception (begin (array-set! a n 0) #t)))
811
812 (with-test-prefix "store/fetch"
813 ;; Check array-ref gives back what was put with array-set!.
814 ;; In Guile 1.6.4 and earlier, array-set! only demanded an inum and
815 ;; would silently truncate to a short.
816
817 (do ((n 1 (1+ (* 2 n)))) ;; n=2^k-1
818 ((not (fits? n)))
819 (array-set! a n 0)
820 (pass-if n
821 (= n (array-ref a 0))))
822
823 (do ((n -1 (* 2 n))) ;; -n=2^k
824 ((not (fits? n)))
825 (array-set! a n 0)
826 (pass-if n
827 (= n (array-ref a 0))))))))
828
829 ;;;
830 ;;; array-set!
831 ;;;
832
833 (with-test-prefix "array-set!"
834
835 (with-test-prefix "one dim"
836 (let ((a (make-array #f '(3 5))))
837 (pass-if "start"
838 (array-set! a 'y 3)
839 #t)
840 (pass-if "end"
841 (array-set! a 'y 5)
842 #t)
843 (pass-if-exception "start-1" exception:out-of-range
844 (array-set! a 'y 2))
845 (pass-if-exception "end+1" exception:out-of-range
846 (array-set! a 'y 6))
847 (pass-if-exception "two indexes" exception:wrong-num-indices
848 (array-set! a 'y 6 7))))
849
850 (with-test-prefix "two dim"
851 (let ((a (make-array #f '(3 5) '(7 9))))
852 (pass-if "start"
853 (array-set! a 'y 3 7)
854 #t)
855 (pass-if "end"
856 (array-set! a 'y 5 9)
857 #t)
858 (pass-if-exception "start i-1" exception:out-of-range
859 (array-set! a 'y 2 7))
860 (pass-if-exception "end i+1" exception:out-of-range
861 (array-set! a 'y 6 9))
862 (pass-if-exception "one index" exception:wrong-num-indices
863 (array-set! a 'y 4))
864 (pass-if-exception "three indexes" exception:wrong-num-indices
865 (array-set! a 'y 4 8 0)))))
866
867 ;;;
868 ;;; uniform-vector
869 ;;;
870
871 (with-test-prefix "typed arrays"
872
873 (with-test-prefix "array-ref byte"
874
875 (let ((a (make-s8vector 1)))
876
877 (pass-if "0"
878 (begin
879 (array-set! a 0 0)
880 (= 0 (array-ref a 0))))
881 (pass-if "127"
882 (begin
883 (array-set! a 127 0)
884 (= 127 (array-ref a 0))))
885 (pass-if "-128"
886 (begin
887 (array-set! a -128 0)
888 (= -128 (array-ref a 0))))))
889
890 (with-test-prefix "shared with rank 1 equality"
891
892 (let ((a #f64(1 2 3 4)))
893
894 (pass-if "change offset"
895 (let ((b (make-shared-array a (lambda (i) (list (+ i 1))) 3)))
896 (and (eq? (array-type b) (array-type a))
897 (= 3 (array-length b))
898 (array-equal? b #f64(2 3 4)))))
899
900 (pass-if "change stride"
901 (let ((c (make-shared-array a (lambda (i) (list (* i 2))) 2)))
902 (and (eq? (array-type c) (array-type a))
903 (= 2 (array-length c))
904 (array-equal? c #f64(1 3))))))))
905
906 ;;;
907 ;;; syntax
908 ;;;
909
910 (with-test-prefix/c&e "syntax"
911
912 (pass-if "rank and lower bounds"
913 ;; uniform u32 array of rank 2 with index ranges 2..3 and 7..8.
914 (let ((a '#2u32@2@7((1 2) (3 4))))
915 (and (array? a)
916 (typed-array? a 'u32)
917 (= (array-rank a) 2)
918 (let loop ((bounds '((2 7) (2 8) (3 7) (3 8)))
919 (result #t))
920 (if (null? bounds)
921 result
922 (and result
923 (loop (cdr bounds)
924 (apply array-in-bounds? a (car bounds)))))))))
925
926 (pass-if "negative lower bound"
927 (let ((a '#1@-3(a b)))
928 (and (array? a)
929 (= (array-rank a) 1)
930 (array-in-bounds? a -3) (array-in-bounds? a -2)
931 (eq? 'a (array-ref a -3))
932 (eq? 'b (array-ref a -2)))))
933
934 (pass-if-exception "negative length" exception:length-non-negative
935 (with-input-from-string "'#1:-3(#t #t)" read))
936
937 (pass-if "bitvector is self-evaluating"
938 (equal? (compile (bitvector)) (bitvector)))
939
940 ; this failed in 2.0.9.
941 (pass-if "typed arrays that are not uniform arrays"
942 (let ((a #2b((#t #f) (#f #t)))
943 (b (make-typed-array 'b #f 2 2)))
944 (array-set! b #t 0 0)
945 (array-set! b #t 1 1)
946 (array-equal? a b))))
947
948 ;;;
949 ;;; equal? with vector and one-dimensional array
950 ;;;
951
952 (with-test-prefix/c&e "equal?"
953 (pass-if "array and non-array"
954 (not (equal? #2f64((0 1) (2 3)) 100)))
955
956 (pass-if "empty vectors of different types"
957 (not (equal? #s32() #f64())))
958
959 (pass-if "empty arrays of different types"
960 (not (equal? #2s32() #2f64())))
961
962 (pass-if "empty arrays of the same type"
963 (equal? #s32() #s32()))
964
965 (pass-if "identical uniform vectors of the same type"
966 (equal? #s32(1) #s32(1)))
967
968 (pass-if "nonidentical uniform vectors of the same type"
969 (not (equal? #s32(1) #s32(-1))))
970
971 (pass-if "identical uniform vectors of different types"
972 (not (equal? #s32(1) #s64(1))))
973
974 (pass-if "nonidentical uniform vectors of different types"
975 (not (equal? #s32(1) #s64(-1))))
976
977 (pass-if "vector and one-dimensional array"
978 (equal? (make-shared-array #2((a b c) (d e f) (g h i))
979 (lambda (i) (list i i))
980 '(0 2))
981 #(a e i))))
982
983 ;;;
984 ;;; slices as generalized vectors
985 ;;;
986
987 (with-test-prefix/c&e "generalized vector slices"
988 (pass-if (equal? (array-row #2u32((0 1) (2 3)) 1)
989 #u32(2 3)))
990 (pass-if (equal? (array-ref (array-row #2u32((0 1) (2 3)) 1) 0)
991 2)))
992
993 ;;;
994 ;;; printing arrays
995 ;;;
996
997 (with-test-prefix/c&e "printing arrays"
998 (pass-if-equal "writing 1D arrays that aren't vectors"
999 "#1(b c)"
1000 (format #f "~a" (make-shared-array #(a b c)
1001 (lambda (i) (list (+ i 1)))
1002 2)))
1003
1004 (pass-if-equal "0-array"
1005 "#0(9)"
1006 (format #f "~a" (make-array 9)))
1007
1008 (pass-if-equal "2-array"
1009 "#2f64((0.0 1.0) (2.0 3.0))"
1010 (format #f "~a" #2f64((0 1) (2 3))))
1011
1012 (pass-if-equal "empty 3-array"
1013 "#3()"
1014 (format #f "~a" (make-array 1 0 0 0)))
1015
1016 (pass-if-equal "empty 3-array with last nonempty dim."
1017 "#3:0:0:1()"
1018 (format #f "~a" (make-array 1 0 0 1)))
1019
1020 (pass-if-equal "empty typed 3-array with last nonempty dim."
1021 "#3f64:0:0:1()"
1022 (format #f "~a" (make-typed-array 'f64 1 0 0 1)))
1023
1024 (pass-if-equal "empty 3-array with middle nonempty dim."
1025 "#3:0:1:0()"
1026 (format #f "~a" (make-array 1 0 1 0)))
1027
1028 (pass-if-equal "empty 3-array with first nonempty dim."
1029 "#3(())"
1030 (format #f "~a" (make-array 1 1 0 0)))
1031
1032 (pass-if-equal "3-array with non-zero lower bounds"
1033 "#3@1@0@1(((1 1 1) (1 1 1)) ((1 1 1) (1 1 1)))"
1034 (format #f "~a" (make-array 1 '(1 2) '(0 1) '(1 3))))
1035
1036 (pass-if-equal "3-array with non-zero-lower bounds and last nonempty dim."
1037 "#3@0:0@0:0@1:3()"
1038 (format #f "~a" (make-array 1 0 0 '(1 3))))
1039
1040 (pass-if-equal "3-array with non-zero-lower bounds and middle nonempty dim."
1041 "#3@0:0@1:3@0:0()"
1042 (format #f "~a" (make-array 1 0 '(1 3) 0)))
1043
1044 (pass-if-equal "3-array with non-zero-lower bounds and first nonempty dim."
1045 "#3@1@0@0(() () ())"
1046 (format #f "~a" (make-array 1 '(1 3) 0 0)))
1047
1048 (pass-if-equal "3-array with singleton dim case I"
1049 "#3@1@1@-1(((1 1 1)))"
1050 (format #f "~a" (make-array 1 '(1 1) '(1 1) '(-1 1))))
1051
1052 (pass-if-equal "3-array with singleton dim case II"
1053 "#3@-1@1@1(((1) (1) (1)))"
1054 (format #f "~a" (make-array 1 '(-1 -1) '(1 3) '(1 1))))
1055
1056 (pass-if-equal "3-array with singleton dim case III"
1057 "#3@1@-1@1(((1)) ((1)) ((1)))"
1058 (format #f "~a" (make-array 1 '(1 3) '(-1 -1) '(1 1)))))
1059
1060 ;;;
1061 ;;; reading arrays
1062 ;;;
1063
1064 (with-test-prefix/c&e "reading arrays"
1065
1066 (pass-if "empty 3-array with first nonempty dim I"
1067 (array-equal? (make-array 1 1 0 0)
1068 (call-with-input-string "#3(())" read)))
1069 (pass-if "empty 3-array with first nonempty dim II"
1070 (array-equal? (make-array 1 1 0 0)
1071 #3(())))
1072
1073 (pass-if "empty 3-array with middle nonempty dim I"
1074 (array-equal? (make-array 1 0 1 0)
1075 (call-with-input-string "#3:0:1:0()" read)))
1076 (pass-if "empty 3-array with middle nonempty dim II"
1077 (array-equal? (make-array 1 0 1 0)
1078 #3:0:1:0()))
1079
1080 (pass-if "empty typed 3-array with middle nonempty dim I"
1081 (array-equal? (make-typed-array 'f64 1 0 1 0)
1082 (call-with-input-string "#3f64:0:1:0()" read)))
1083 (pass-if "empty typed 3-array with middle nonempty dim II"
1084 (array-equal? (make-typed-array 'f64 1 0 1 0)
1085 #3f64:0:1:0()))
1086
1087 (pass-if "array with specified size I"
1088 (array-equal? #f64(1 2 3)
1089 (call-with-input-string "#f64:3(1 2 3)" read)))
1090 (pass-if "array with specified size II"
1091 (array-equal? #f64(1 2 3)
1092 #f64:3(1 2 3))))
1093