temporarily disable elisp exception tests
[bpt/guile.git] / test-suite / tests / arrays.test
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 ;;;
27 ;;; array?
28 ;;;
29
30 (define exception:wrong-num-indices
31 (cons 'misc-error "^wrong number of indices.*"))
32
33 (define exception:length-non-negative
34 (cons 'read-error ".*array length must be non-negative.*"))
35
36
37 (with-test-prefix "array?"
38
39 (let ((bool (make-typed-array 'b #t '(5 6)))
40 (char (make-typed-array 'a #\a '(5 6)))
41 (byte (make-typed-array 'u8 0 '(5 6)))
42 (short (make-typed-array 's16 0 '(5 6)))
43 (ulong (make-typed-array 'u32 0 '(5 6)))
44 (long (make-typed-array 's32 0 '(5 6)))
45 (longlong (make-typed-array 's64 0 '(5 6)))
46 (float (make-typed-array 'f32 0 '(5 6)))
47 (double (make-typed-array 'f64 0 '(5 6)))
48 (complex (make-typed-array 'c64 0 '(5 6)))
49 (scm (make-typed-array #t 0 '(5 6))))
50
51 (with-test-prefix "is bool"
52 (pass-if (eq? #t (typed-array? bool 'b)))
53 (pass-if (eq? #f (typed-array? char 'b)))
54 (pass-if (eq? #f (typed-array? byte 'b)))
55 (pass-if (eq? #f (typed-array? short 'b)))
56 (pass-if (eq? #f (typed-array? ulong 'b)))
57 (pass-if (eq? #f (typed-array? long 'b)))
58 (pass-if (eq? #f (typed-array? longlong 'b)))
59 (pass-if (eq? #f (typed-array? float 'b)))
60 (pass-if (eq? #f (typed-array? double 'b)))
61 (pass-if (eq? #f (typed-array? complex 'b)))
62 (pass-if (eq? #f (typed-array? scm 'b))))
63
64 (with-test-prefix "is char"
65 (pass-if (eq? #f (typed-array? bool 'a)))
66 (pass-if (eq? #t (typed-array? char 'a)))
67 (pass-if (eq? #f (typed-array? byte 'a)))
68 (pass-if (eq? #f (typed-array? short 'a)))
69 (pass-if (eq? #f (typed-array? ulong 'a)))
70 (pass-if (eq? #f (typed-array? long 'a)))
71 (pass-if (eq? #f (typed-array? longlong 'a)))
72 (pass-if (eq? #f (typed-array? float 'a)))
73 (pass-if (eq? #f (typed-array? double 'a)))
74 (pass-if (eq? #f (typed-array? complex 'a)))
75 (pass-if (eq? #f (typed-array? scm 'a))))
76
77 (with-test-prefix "is byte"
78 (pass-if (eq? #f (typed-array? bool 'u8)))
79 (pass-if (eq? #f (typed-array? char 'u8)))
80 (pass-if (eq? #t (typed-array? byte 'u8)))
81 (pass-if (eq? #f (typed-array? short 'u8)))
82 (pass-if (eq? #f (typed-array? ulong 'u8)))
83 (pass-if (eq? #f (typed-array? long 'u8)))
84 (pass-if (eq? #f (typed-array? longlong 'u8)))
85 (pass-if (eq? #f (typed-array? float 'u8)))
86 (pass-if (eq? #f (typed-array? double 'u8)))
87 (pass-if (eq? #f (typed-array? complex 'u8)))
88 (pass-if (eq? #f (typed-array? scm 'u8))))
89
90 (with-test-prefix "is short"
91 (pass-if (eq? #f (typed-array? bool 's16)))
92 (pass-if (eq? #f (typed-array? char 's16)))
93 (pass-if (eq? #f (typed-array? byte 's16)))
94 (pass-if (eq? #t (typed-array? short 's16)))
95 (pass-if (eq? #f (typed-array? ulong 's16)))
96 (pass-if (eq? #f (typed-array? long 's16)))
97 (pass-if (eq? #f (typed-array? longlong 's16)))
98 (pass-if (eq? #f (typed-array? float 's16)))
99 (pass-if (eq? #f (typed-array? double 's16)))
100 (pass-if (eq? #f (typed-array? complex 's16)))
101 (pass-if (eq? #f (typed-array? scm 's16))))
102
103 (with-test-prefix "is ulong"
104 (pass-if (eq? #f (typed-array? bool 'u32)))
105 (pass-if (eq? #f (typed-array? char 'u32)))
106 (pass-if (eq? #f (typed-array? byte 'u32)))
107 (pass-if (eq? #f (typed-array? short 'u32)))
108 (pass-if (eq? #t (typed-array? ulong 'u32)))
109 (pass-if (eq? #f (typed-array? long 'u32)))
110 (pass-if (eq? #f (typed-array? longlong 'u32)))
111 (pass-if (eq? #f (typed-array? float 'u32)))
112 (pass-if (eq? #f (typed-array? double 'u32)))
113 (pass-if (eq? #f (typed-array? complex 'u32)))
114 (pass-if (eq? #f (typed-array? scm 'u32))))
115
116 (with-test-prefix "is long"
117 (pass-if (eq? #f (typed-array? bool 's32)))
118 (pass-if (eq? #f (typed-array? char 's32)))
119 (pass-if (eq? #f (typed-array? byte 's32)))
120 (pass-if (eq? #f (typed-array? short 's32)))
121 (pass-if (eq? #f (typed-array? ulong 's32)))
122 (pass-if (eq? #t (typed-array? long 's32)))
123 (pass-if (eq? #f (typed-array? longlong 's32)))
124 (pass-if (eq? #f (typed-array? float 's32)))
125 (pass-if (eq? #f (typed-array? double 's32)))
126 (pass-if (eq? #f (typed-array? complex 's32)))
127 (pass-if (eq? #f (typed-array? scm 's32))))
128
129 (with-test-prefix "is long long"
130 (pass-if (eq? #f (typed-array? bool 's64)))
131 (pass-if (eq? #f (typed-array? char 's64)))
132 (pass-if (eq? #f (typed-array? byte 's64)))
133 (pass-if (eq? #f (typed-array? short 's64)))
134 (pass-if (eq? #f (typed-array? ulong 's64)))
135 (pass-if (eq? #f (typed-array? long 's64)))
136 (pass-if (eq? #t (typed-array? longlong 's64)))
137 (pass-if (eq? #f (typed-array? float 's64)))
138 (pass-if (eq? #f (typed-array? double 's64)))
139 (pass-if (eq? #f (typed-array? complex 's64)))
140 (pass-if (eq? #f (typed-array? scm 's64))))
141
142 (with-test-prefix "is float"
143 (pass-if (eq? #f (typed-array? bool 'f32)))
144 (pass-if (eq? #f (typed-array? char 'f32)))
145 (pass-if (eq? #f (typed-array? byte 'f32)))
146 (pass-if (eq? #f (typed-array? short 'f32)))
147 (pass-if (eq? #f (typed-array? ulong 'f32)))
148 (pass-if (eq? #f (typed-array? long 'f32)))
149 (pass-if (eq? #f (typed-array? longlong 'f32)))
150 (pass-if (eq? #t (typed-array? float 'f32)))
151 (pass-if (eq? #f (typed-array? double 'f32)))
152 (pass-if (eq? #f (typed-array? complex 'f32)))
153 (pass-if (eq? #f (typed-array? scm 'f32))))
154
155 (with-test-prefix "is double"
156 (pass-if (eq? #f (typed-array? bool 'f64)))
157 (pass-if (eq? #f (typed-array? char 'f64)))
158 (pass-if (eq? #f (typed-array? byte 'f64)))
159 (pass-if (eq? #f (typed-array? short 'f64)))
160 (pass-if (eq? #f (typed-array? ulong 'f64)))
161 (pass-if (eq? #f (typed-array? long 'f64)))
162 (pass-if (eq? #f (typed-array? longlong 'f64)))
163 (pass-if (eq? #f (typed-array? float 'f64)))
164 (pass-if (eq? #t (typed-array? double 'f64)))
165 (pass-if (eq? #f (typed-array? complex 'f64)))
166 (pass-if (eq? #f (typed-array? scm 'f64))))
167
168 (with-test-prefix "is complex"
169 (pass-if (eq? #f (typed-array? bool 'c64)))
170 (pass-if (eq? #f (typed-array? char 'c64)))
171 (pass-if (eq? #f (typed-array? byte 'c64)))
172 (pass-if (eq? #f (typed-array? short 'c64)))
173 (pass-if (eq? #f (typed-array? ulong 'c64)))
174 (pass-if (eq? #f (typed-array? long 'c64)))
175 (pass-if (eq? #f (typed-array? longlong 'c64)))
176 (pass-if (eq? #f (typed-array? float 'c64)))
177 (pass-if (eq? #f (typed-array? double 'c64)))
178 (pass-if (eq? #t (typed-array? complex 'c64)))
179 (pass-if (eq? #f (typed-array? scm 'c64))))
180
181 (with-test-prefix "is scm"
182 (pass-if (eq? #f (typed-array? bool #t)))
183 (pass-if (eq? #f (typed-array? char #t)))
184 (pass-if (eq? #f (typed-array? byte #t)))
185 (pass-if (eq? #f (typed-array? short #t)))
186 (pass-if (eq? #f (typed-array? ulong #t)))
187 (pass-if (eq? #f (typed-array? long #t)))
188 (pass-if (eq? #f (typed-array? longlong #t)))
189 (pass-if (eq? #f (typed-array? float #t)))
190 (pass-if (eq? #f (typed-array? double #t)))
191 (pass-if (eq? #f (typed-array? complex #t)))
192 (pass-if (eq? #t (typed-array? scm #t))))
193
194 (with-test-prefix "typed-array? returns #f"
195 (pass-if (eq? #f (typed-array? '(1 2 3) 'c64)))
196 (pass-if (eq? #f (typed-array? '(1 2 3) #t)))
197 (pass-if (eq? #f (typed-array? 99 'c64)))
198 (pass-if (eq? #f (typed-array? 99 #t))))))
199
200 ;;;
201 ;;; array-equal?
202 ;;;
203
204 (with-test-prefix/c&e "array-equal?"
205
206 (pass-if "#s16(...)"
207 (array-equal? #s16(1 2 3) #s16(1 2 3))))
208
209 ;;;
210 ;;; make-shared-array
211 ;;;
212
213 (define exception:mapping-out-of-range
214 (cons 'misc-error "^mapping out of range")) ;; per scm_make_shared_array
215
216 (with-test-prefix/c&e "make-shared-array"
217
218 ;; this failed in guile 1.8.0
219 (pass-if "vector unchanged"
220 (let* ((a (make-array #f '(0 7)))
221 (s (make-shared-array a list '(0 7))))
222 (array-equal? a s)))
223
224 (pass-if-exception "vector, high too big" exception:mapping-out-of-range
225 (let* ((a (make-array #f '(0 7))))
226 (make-shared-array a list '(0 8))))
227
228 (pass-if-exception "vector, low too big" exception:out-of-range
229 (let* ((a (make-array #f '(0 7))))
230 (make-shared-array a list '(-1 7))))
231
232 (pass-if "truncate columns"
233 (array-equal? (make-shared-array #2((a b c) (d e f) (g h i)) list 3 2)
234 #2((a b) (d e) (g h))))
235
236 (pass-if "pick one column"
237 (array-equal? (make-shared-array #2((a b c) (d e f) (g h i))
238 (lambda (i) (list i 2))
239 '(0 2))
240 #(c f i)))
241
242 (pass-if "diagonal"
243 (array-equal? (make-shared-array #2((a b c) (d e f) (g h i))
244 (lambda (i) (list i i))
245 '(0 2))
246 #(a e i)))
247
248 ;; this failed in guile 1.8.0
249 (pass-if "2 dims from 1 dim"
250 (array-equal? (make-shared-array #1(a b c d e f g h i j k l)
251 (lambda (i j) (list (+ (* i 3) j)))
252 4 3)
253 #2((a b c) (d e f) (g h i) (j k l))))
254
255 (pass-if "reverse columns"
256 (array-equal? (make-shared-array #2((a b c) (d e f) (g h i))
257 (lambda (i j) (list i (- 2 j)))
258 3 3)
259 #2((c b a) (f e d) (i h g))))
260
261 (pass-if "fixed offset, 0 based becomes 1 based"
262 (let* ((x #2((a b c) (d e f) (g h i)))
263 (y (make-shared-array x
264 (lambda (i j) (list (1- i) (1- j)))
265 '(1 3) '(1 3))))
266 (and (eq? (array-ref x 0 0) 'a)
267 (eq? (array-ref y 1 1) 'a))))
268
269 ;; this failed in guile 1.8.0
270 (pass-if "stride every third element"
271 (array-equal? (make-shared-array #1(a b c d e f g h i j k l)
272 (lambda (i) (list (* i 3)))
273 4)
274 #1(a d g j)))
275
276 (pass-if "shared of shared"
277 (let* ((a #2((1 2 3) (4 5 6) (7 8 9)))
278 (s1 (make-shared-array a (lambda (i) (list i 1)) 3))
279 (s2 (make-shared-array s1 list '(1 2))))
280 (and (eqv? 5 (array-ref s2 1))
281 (eqv? 8 (array-ref s2 2))))))
282
283 ;;;
284 ;;; array-contents
285 ;;;
286
287 (define (every-two x) (make-shared-array x (lambda (i) (list (* i 2))) 2))
288
289 (with-test-prefix/c&e "array-contents"
290
291 (pass-if "simple vector"
292 (let* ((a (make-array 0 4)))
293 (eq? a (array-contents a))))
294
295 (pass-if "offset vector"
296 (let* ((a (make-array 0 '(1 4))))
297 (array-copy! #(1 2 3 4) (array-contents a))
298 (array-equal? #1@1(1 2 3 4) a)))
299
300 (pass-if "offset vector, strict"
301 (let* ((a (make-array 0 '(1 4))))
302 (array-copy! #(1 2 3 4) (array-contents a #t))
303 (array-equal? #1@1(1 2 3 4) a)))
304
305 (pass-if "stepped vector"
306 (let* ((a (make-array 0 4)))
307 (array-copy! #(99 66) (array-contents (every-two a)))
308 (array-equal? #(99 0 66 0) a)))
309
310 ;; this failed in 2.0.9.
311 (pass-if "stepped vector, strict"
312 (let* ((a (make-array 0 4)))
313 (not (array-contents (every-two a) #t))))
314
315 (pass-if "plain rank 2 array"
316 (let* ((a (make-array 0 2 2)))
317 (array-copy! #(1 2 3 4) (array-contents a #t))
318 (array-equal? #2((1 2) (3 4)) a)))
319
320 (pass-if "offset rank 2 array"
321 (let* ((a (make-array 0 '(1 2) '(1 2))))
322 (array-copy! #(1 2 3 4) (array-contents a #t))
323 (array-equal? #2@1@1((1 2) (3 4)) a)))
324
325 (pass-if "transposed rank 2 array"
326 (let* ((a (make-array 0 4 4)))
327 (not (array-contents (transpose-array a 1 0) #t))))
328
329 ;; This is a consequence of (array-contents? a #t) => #t.
330 (pass-if "empty array"
331 (let ((a (make-typed-array 'f64 2 0 0)))
332 (f64vector? (array-contents a))))
333
334 (pass-if "broadcast vector I"
335 (let* ((a (make-array 0 4))
336 (b (make-shared-array a (lambda (i j k) (list k)) 1 1 4)))
337 (array-copy! #(1 2 3 4) (array-contents b #t))
338 (array-equal? #(1 2 3 4) a)))
339
340 (pass-if "broadcast vector II"
341 (let* ((a (make-array 0 4))
342 (b (make-shared-array a (lambda (i j k) (list k)) 2 1 4)))
343 (not (array-contents b))))
344
345 ;; FIXME maybe this should be allowed.
346 ;; (pass-if "broadcast vector -> empty"
347 ;; (let* ((a (make-array 0 4))
348 ;; (b (make-shared-array a (lambda (i j k) (list k)) 0 1 4)))
349 ;; (if #f #f)))
350
351 (pass-if "broadcast 2-rank I"
352 (let* ((a #2((1 2 3) (4 5 6)))
353 (b (make-shared-array a (lambda (i j) (list 0 j)) 2 3)))
354 (not (array-contents b))))
355
356 (pass-if "broadcast 2-rank II"
357 (let* ((a #2((1 2 3) (4 5 6)))
358 (b (make-shared-array a (lambda (i j) (list i 0)) 2 3)))
359 (not (array-contents b))))
360
361 (pass-if "literal array"
362 (not (not (array-contents #2((1 2 3) (4 5 6)))))))
363
364
365 ;;;
366 ;;; shared-array-root
367 ;;;
368
369 (define amap1 (lambda (i) (list (* 2 i))))
370 (define amap2 (lambda (i j) (list (+ 1 (* 2 i)) (+ 1 (* 2 j)))))
371
372 (with-test-prefix/c&e "shared-array-root"
373
374 (pass-if "plain vector"
375 (let* ((a (make-vector 4 0))
376 (b (make-shared-array a amap1 2)))
377 (eq? (shared-array-root a) (shared-array-root b) (array-contents a))))
378
379 (pass-if "plain array rank 2"
380 (let* ((a (make-array 0 4 4))
381 (b (make-shared-array a amap2 2 2)))
382 (eq? (shared-array-root a) (shared-array-root b) (array-contents a))))
383
384 (pass-if "uniform array rank 2"
385 (let* ((a (make-typed-array 'c64 0 4 4))
386 (b (make-shared-array a amap2 2 2)))
387 (eq? (shared-array-root a) (shared-array-root b) (array-contents a))))
388
389 (pass-if "bit array rank 2"
390 (let* ((a (make-typed-array 'b #f 4 4))
391 (b (make-shared-array a amap2 2 2)))
392 (eq? (shared-array-root a) (shared-array-root b) (array-contents a)))))
393
394 ;;;
395 ;;; transpose-array
396 ;;;
397
398 ; see strings.test.
399 (define exception:wrong-type-arg
400 (cons #t "Wrong type"))
401
402 (with-test-prefix/c&e "transpose-array"
403
404 (pass-if-exception "non array argument" exception:wrong-type-arg
405 (transpose-array 99))
406
407 (pass-if "rank 0"
408 (let* ((a #0(99))
409 (b (transpose-array a)))
410 (and (array-equal? a b)
411 (eq? (shared-array-root a) (shared-array-root b)))))
412
413 (pass-if "rank 1"
414 (let* ((a #(1 2 3))
415 (b (transpose-array a 0)))
416 (and (array-equal? a b)
417 (eq? (shared-array-root a) (shared-array-root b)))))
418
419 (pass-if "rank 2"
420 (let* ((a #2((1 2 3) (4 5 6)))
421 (b (transpose-array a 1 0))
422 (c (transpose-array a 0 1)))
423 (and (array-equal? b #2((1 4) (2 5) (3 6)))
424 (array-equal? c a)
425 (eq? (shared-array-root a)
426 (shared-array-root b)
427 (shared-array-root c)))))
428
429 ; rank > 2 is needed to check against the inverted axis index logic.
430 (pass-if "rank 3"
431 (let* ((a #3(((0 1 2 3) (4 5 6 7) (8 9 10 11))
432 ((12 13 14 15) (16 17 18 19) (20 21 22 23))))
433 (b (transpose-array a 1 2 0)))
434 (and (array-equal? b #3(((0 4 8) (12 16 20)) ((1 5 9) (13 17 21))
435 ((2 6 10) (14 18 22)) ((3 7 11) (15 19 23))))
436 (eq? (shared-array-root a)
437 (shared-array-root b))))))
438
439 ;;;
440 ;;; array->list
441 ;;;
442
443 (with-test-prefix/c&e "array->list"
444 (pass-if-equal "uniform vector" '(1 2 3) (array->list #s16(1 2 3)))
445 (pass-if-equal "vector" '(1 2 3) (array->list #(1 2 3)))
446 (pass-if-equal "rank 2 array" '((1 2) (3 4) (5 6)) (array->list #2((1 2) (3 4) (5 6))))
447 (pass-if-equal "empty vector" '() (array->list #()))
448
449 (pass-if-equal "http://bugs.gnu.org/12465 - ok"
450 '(3 4)
451 (let* ((a #2((1 2) (3 4)))
452 (b (make-shared-array a (lambda (j) (list 1 j)) 2)))
453 (array->list b)))
454 (pass-if-equal "http://bugs.gnu.org/12465 - bad"
455 '(2 4)
456 (let* ((a #2((1 2) (3 4)))
457 (b (make-shared-array a (lambda (i) (list i 1)) 2)))
458 (array->list b))))
459
460 ;;;
461 ;;; array-fill!
462 ;;;
463
464 (with-test-prefix "array-fill!"
465
466 (with-test-prefix "bool"
467 (let ((a (make-bitvector 1 #t)))
468 (pass-if "#f" (array-fill! a #f) #t)
469 (pass-if "#t" (array-fill! a #t) #t)))
470
471 (with-test-prefix "char"
472 (let ((a (make-string 1 #\a)))
473 (pass-if "x" (array-fill! a #\x) #t)))
474
475 (with-test-prefix "byte"
476 (let ((a (make-s8vector 1 0)))
477 (pass-if "0" (array-fill! a 0) #t)
478 (pass-if "127" (array-fill! a 127) #t)
479 (pass-if "-128" (array-fill! a -128) #t)
480 (pass-if-exception "128" exception:out-of-range
481 (array-fill! a 128))
482 (pass-if-exception "-129" exception:out-of-range
483 (array-fill! a -129))
484 (pass-if-exception "symbol" exception:wrong-type-arg
485 (array-fill! a 'symbol))))
486
487 (with-test-prefix "short"
488 (let ((a (make-s16vector 1 0)))
489 (pass-if "0" (array-fill! a 0) #t)
490 (pass-if "123" (array-fill! a 123) #t)
491 (pass-if "-123" (array-fill! a -123) #t)))
492
493 (with-test-prefix "ulong"
494 (let ((a (make-u32vector 1 1)))
495 (pass-if "0" (array-fill! a 0) #t)
496 (pass-if "123" (array-fill! a 123) #t)
497 (pass-if-exception "-123" exception:out-of-range
498 (array-fill! a -123) #t)))
499
500 (with-test-prefix "long"
501 (let ((a (make-s32vector 1 -1)))
502 (pass-if "0" (array-fill! a 0) #t)
503 (pass-if "123" (array-fill! a 123) #t)
504 (pass-if "-123" (array-fill! a -123) #t)))
505
506 (with-test-prefix "float"
507 (let ((a (make-f32vector 1 1.0)))
508 (pass-if "0.0" (array-fill! a 0) #t)
509 (pass-if "123.0" (array-fill! a 123.0) #t)
510 (pass-if "-123.0" (array-fill! a -123.0) #t)
511 (pass-if "0" (array-fill! a 0) #t)
512 (pass-if "123" (array-fill! a 123) #t)
513 (pass-if "-123" (array-fill! a -123) #t)
514 (pass-if "5/8" (array-fill! a 5/8) #t)))
515
516 (with-test-prefix "double"
517 (let ((a (make-f64vector 1 1/3)))
518 (pass-if "0.0" (array-fill! a 0) #t)
519 (pass-if "123.0" (array-fill! a 123.0) #t)
520 (pass-if "-123.0" (array-fill! a -123.0) #t)
521 (pass-if "0" (array-fill! a 0) #t)
522 (pass-if "123" (array-fill! a 123) #t)
523 (pass-if "-123" (array-fill! a -123) #t)
524 (pass-if "5/8" (array-fill! a 5/8) #t)))
525
526 (with-test-prefix "noncompact"
527 (let* ((a (make-array 0 3 3))
528 (b (make-shared-array a (lambda (i) (list i i)) 3)))
529 (array-fill! b 9)
530 (pass-if
531 (and (equal? b #(9 9 9))
532 (equal? a #2((9 0 0) (0 9 0) (0 0 9))))))))
533
534 ;;;
535 ;;; array-in-bounds?
536 ;;;
537
538 (with-test-prefix/c&e "array-in-bounds?"
539
540 (pass-if (let ((a (make-array #f '(425 425))))
541 (eq? #f (array-in-bounds? a 0)))))
542
543 ;;;
544 ;;; array-prototype
545 ;;;
546
547 (with-test-prefix "array-type"
548
549 (with-test-prefix/c&e "on make-foo-vector"
550
551 (pass-if "bool"
552 (eq? 'b (array-type (make-bitvector 1))))
553
554 (pass-if "char"
555 (eq? 'a (array-type (make-string 1))))
556
557 (pass-if "byte"
558 (eq? 'u8 (array-type (make-u8vector 1))))
559
560 (pass-if "short"
561 (eq? 's16 (array-type (make-s16vector 1))))
562
563 (pass-if "ulong"
564 (eq? 'u32 (array-type (make-u32vector 1))))
565
566 (pass-if "long"
567 (eq? 's32 (array-type (make-s32vector 1))))
568
569 (pass-if "long long"
570 (eq? 's64 (array-type (make-s64vector 1))))
571
572 (pass-if "float"
573 (eq? 'f32 (array-type (make-f32vector 1))))
574
575 (pass-if "double"
576 (eq? 'f64 (array-type (make-f64vector 1))))
577
578 (pass-if "complex"
579 (eq? 'c64 (array-type (make-c64vector 1))))
580
581 (pass-if "scm"
582 (eq? #t (array-type (make-vector 1)))))
583
584 (with-test-prefix "on make-typed-array"
585
586 (let ((types '(b a u8 s8 u16 s16 u32 s32 u64 u64 f32 f64 c32 c64)))
587 (for-each (lambda (type)
588 (pass-if (symbol->string type)
589 (eq? type
590 (array-type (make-typed-array type
591 *unspecified*
592 '(5 6))))))
593 types))))
594
595 ;;;
596 ;;; array-set!
597 ;;;
598
599 (with-test-prefix "array-set!"
600
601 (with-test-prefix "bitvector"
602
603 ;; in Guile 1.8.0 a bug in bitvector_set() caused a segv in array-set!
604 ;; on a bitvector like the following
605 (let ((a (make-bitvector 1)))
606 (pass-if "one elem set #t"
607 (begin
608 (array-set! a #t 0)
609 (eq? #t (array-ref a 0))))
610 (pass-if "one elem set #f"
611 (begin
612 (array-set! a #f 0)
613 (eq? #f (array-ref a 0))))))
614
615 (with-test-prefix "byte"
616
617 (let ((a (make-s8vector 1)))
618
619 (pass-if "-128"
620 (begin (array-set! a -128 0) #t))
621 (pass-if "0"
622 (begin (array-set! a 0 0) #t))
623 (pass-if "127"
624 (begin (array-set! a 127 0) #t))
625 (pass-if-exception "-129" exception:out-of-range
626 (begin (array-set! a -129 0) #t))
627 (pass-if-exception "128" exception:out-of-range
628 (begin (array-set! a 128 0) #t))))
629
630 (with-test-prefix "short"
631
632 (let ((a (make-s16vector 1)))
633 ;; true if n can be array-set! into a
634 (define (fits? n)
635 (false-if-exception (begin (array-set! a n 0) #t)))
636
637 (with-test-prefix "store/fetch"
638 ;; Check array-ref gives back what was put with array-set!.
639 ;; In Guile 1.6.4 and earlier, array-set! only demanded an inum and
640 ;; would silently truncate to a short.
641
642 (do ((n 1 (1+ (* 2 n)))) ;; n=2^k-1
643 ((not (fits? n)))
644 (array-set! a n 0)
645 (pass-if n
646 (= n (array-ref a 0))))
647
648 (do ((n -1 (* 2 n))) ;; -n=2^k
649 ((not (fits? n)))
650 (array-set! a n 0)
651 (pass-if n
652 (= n (array-ref a 0))))))))
653
654 ;;;
655 ;;; array-set!
656 ;;;
657
658 (with-test-prefix "array-set!"
659
660 (with-test-prefix "one dim"
661 (let ((a (make-array #f '(3 5))))
662 (pass-if "start"
663 (array-set! a 'y 3)
664 #t)
665 (pass-if "end"
666 (array-set! a 'y 5)
667 #t)
668 (pass-if-exception "start-1" exception:out-of-range
669 (array-set! a 'y 2))
670 (pass-if-exception "end+1" exception:out-of-range
671 (array-set! a 'y 6))
672 (pass-if-exception "two indexes" exception:wrong-num-indices
673 (array-set! a 'y 6 7))))
674
675 (with-test-prefix "two dim"
676 (let ((a (make-array #f '(3 5) '(7 9))))
677 (pass-if "start"
678 (array-set! a 'y 3 7)
679 #t)
680 (pass-if "end"
681 (array-set! a 'y 5 9)
682 #t)
683 (pass-if-exception "start i-1" exception:out-of-range
684 (array-set! a 'y 2 7))
685 (pass-if-exception "end i+1" exception:out-of-range
686 (array-set! a 'y 6 9))
687 (pass-if-exception "one index" exception:wrong-num-indices
688 (array-set! a 'y 4))
689 (pass-if-exception "three indexes" exception:wrong-num-indices
690 (array-set! a 'y 4 8 0)))))
691
692 ;;;
693 ;;; uniform-vector
694 ;;;
695
696 (with-test-prefix "typed arrays"
697
698 (with-test-prefix "array-ref byte"
699
700 (let ((a (make-s8vector 1)))
701
702 (pass-if "0"
703 (begin
704 (array-set! a 0 0)
705 (= 0 (array-ref a 0))))
706 (pass-if "127"
707 (begin
708 (array-set! a 127 0)
709 (= 127 (array-ref a 0))))
710 (pass-if "-128"
711 (begin
712 (array-set! a -128 0)
713 (= -128 (array-ref a 0))))))
714
715 (with-test-prefix "shared with rank 1 equality"
716
717 (let ((a #f64(1 2 3 4)))
718
719 (pass-if "change offset"
720 (let ((b (make-shared-array a (lambda (i) (list (+ i 1))) 3)))
721 (and (eq? (array-type b) (array-type a))
722 (= 3 (array-length b))
723 (array-equal? b #f64(2 3 4)))))
724
725 (pass-if "change stride"
726 (let ((c (make-shared-array a (lambda (i) (list (* i 2))) 2)))
727 (and (eq? (array-type c) (array-type a))
728 (= 2 (array-length c))
729 (array-equal? c #f64(1 3))))))))
730
731 ;;;
732 ;;; syntax
733 ;;;
734
735 (with-test-prefix/c&e "syntax"
736
737 (pass-if "rank and lower bounds"
738 ;; uniform u32 array of rank 2 with index ranges 2..3 and 7..8.
739 (let ((a '#2u32@2@7((1 2) (3 4))))
740 (and (array? a)
741 (typed-array? a 'u32)
742 (= (array-rank a) 2)
743 (let loop ((bounds '((2 7) (2 8) (3 7) (3 8)))
744 (result #t))
745 (if (null? bounds)
746 result
747 (and result
748 (loop (cdr bounds)
749 (apply array-in-bounds? a (car bounds)))))))))
750
751 (pass-if "negative lower bound"
752 (let ((a '#1@-3(a b)))
753 (and (array? a)
754 (= (array-rank a) 1)
755 (array-in-bounds? a -3) (array-in-bounds? a -2)
756 (eq? 'a (array-ref a -3))
757 (eq? 'b (array-ref a -2)))))
758
759 (pass-if-exception "negative length" exception:length-non-negative
760 (with-input-from-string "'#1:-3(#t #t)" read))
761
762 (pass-if "bitvector is self-evaluating"
763 (equal? (compile (bitvector)) (bitvector)))
764
765 ; this failed in 2.0.9.
766 (pass-if "typed arrays that are not uniform arrays"
767 (let ((a #2b((#t #f) (#f #t)))
768 (b (make-typed-array 'b #f 2 2)))
769 (array-set! b #t 0 0)
770 (array-set! b #t 1 1)
771 (array-equal? a b))))
772
773 ;;;
774 ;;; equal? with vector and one-dimensional array
775 ;;;
776
777 (with-test-prefix/c&e "equal?"
778 (pass-if "array and non-array"
779 (not (equal? #2f64((0 1) (2 3)) 100)))
780
781 (pass-if "empty vectors of different types"
782 (not (equal? #s32() #f64())))
783
784 (pass-if "empty arrays of different types"
785 (not (equal? #2s32() #2f64())))
786
787 (pass-if "empty arrays of the same type"
788 (equal? #s32() #s32()))
789
790 (pass-if "identical uniform vectors of the same type"
791 (equal? #s32(1) #s32(1)))
792
793 (pass-if "nonidentical uniform vectors of the same type"
794 (not (equal? #s32(1) #s32(-1))))
795
796 (pass-if "identical uniform vectors of different types"
797 (not (equal? #s32(1) #s64(1))))
798
799 (pass-if "nonidentical uniform vectors of different types"
800 (not (equal? #s32(1) #s64(-1))))
801
802 (pass-if "vector and one-dimensional array"
803 (equal? (make-shared-array #2((a b c) (d e f) (g h i))
804 (lambda (i) (list i i))
805 '(0 2))
806 #(a e i))))
807
808 ;;;
809 ;;; slices as generalized vectors
810 ;;;
811
812 (define (array-row a i)
813 (make-shared-array a (lambda (j) (list i j))
814 (cadr (array-dimensions a))))
815
816 (with-test-prefix/c&e "generalized vector slices"
817 (pass-if (equal? (array-row #2u32((0 1) (2 3)) 1)
818 #u32(2 3)))
819 (pass-if (equal? (array-ref (array-row #2u32((0 1) (2 3)) 1) 0)
820 2)))
821
822 ;;;
823 ;;; printing arrays
824 ;;;
825
826 (with-test-prefix/c&e "printing arrays"
827 (pass-if-equal "writing 1D arrays that aren't vectors"
828 "#1(b c)"
829 (format #f "~a" (make-shared-array #(a b c)
830 (lambda (i) (list (+ i 1)))
831 2))))