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