Merge commit '122f24cc8a3637ed42d7792ad1ff8ec0c49c58df'
[bpt/guile.git] / test-suite / tests / arrays.test
1 ;;;; unif.test --- tests guile's uniform arrays -*- scheme -*-
2 ;;;;
3 ;;;; Copyright 2004, 2006, 2009, 2010, 2011, 2012, 2013 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 "sanity"
37 ;; At the current time of writing, bignums have a tc7 that is one bit
38 ;; away from strings. It used to be that the vector implementation
39 ;; registered for strings had the TYP7S mask, not the TYP7 mask,
40 ;; making the system think that bignums were vectors. Doh!
41 (pass-if (not (uniform-vector? 12345678901234567890123456789))))
42
43 (with-test-prefix "array?"
44
45 (let ((bool (make-typed-array 'b #t '(5 6)))
46 (char (make-typed-array 'a #\a '(5 6)))
47 (byte (make-typed-array 'u8 0 '(5 6)))
48 (short (make-typed-array 's16 0 '(5 6)))
49 (ulong (make-typed-array 'u32 0 '(5 6)))
50 (long (make-typed-array 's32 0 '(5 6)))
51 (longlong (make-typed-array 's64 0 '(5 6)))
52 (float (make-typed-array 'f32 0 '(5 6)))
53 (double (make-typed-array 'f64 0 '(5 6)))
54 (complex (make-typed-array 'c64 0 '(5 6)))
55 (scm (make-typed-array #t 0 '(5 6))))
56
57 (with-test-prefix "is bool"
58 (pass-if (eq? #t (typed-array? bool 'b)))
59 (pass-if (eq? #f (typed-array? char 'b)))
60 (pass-if (eq? #f (typed-array? byte 'b)))
61 (pass-if (eq? #f (typed-array? short 'b)))
62 (pass-if (eq? #f (typed-array? ulong 'b)))
63 (pass-if (eq? #f (typed-array? long 'b)))
64 (pass-if (eq? #f (typed-array? longlong 'b)))
65 (pass-if (eq? #f (typed-array? float 'b)))
66 (pass-if (eq? #f (typed-array? double 'b)))
67 (pass-if (eq? #f (typed-array? complex 'b)))
68 (pass-if (eq? #f (typed-array? scm 'b))))
69
70 (with-test-prefix "is char"
71 (pass-if (eq? #f (typed-array? bool 'a)))
72 (pass-if (eq? #t (typed-array? char 'a)))
73 (pass-if (eq? #f (typed-array? byte 'a)))
74 (pass-if (eq? #f (typed-array? short 'a)))
75 (pass-if (eq? #f (typed-array? ulong 'a)))
76 (pass-if (eq? #f (typed-array? long 'a)))
77 (pass-if (eq? #f (typed-array? longlong 'a)))
78 (pass-if (eq? #f (typed-array? float 'a)))
79 (pass-if (eq? #f (typed-array? double 'a)))
80 (pass-if (eq? #f (typed-array? complex 'a)))
81 (pass-if (eq? #f (typed-array? scm 'a))))
82
83 (with-test-prefix "is byte"
84 (pass-if (eq? #f (typed-array? bool 'u8)))
85 (pass-if (eq? #f (typed-array? char 'u8)))
86 (pass-if (eq? #t (typed-array? byte 'u8)))
87 (pass-if (eq? #f (typed-array? short 'u8)))
88 (pass-if (eq? #f (typed-array? ulong 'u8)))
89 (pass-if (eq? #f (typed-array? long 'u8)))
90 (pass-if (eq? #f (typed-array? longlong 'u8)))
91 (pass-if (eq? #f (typed-array? float 'u8)))
92 (pass-if (eq? #f (typed-array? double 'u8)))
93 (pass-if (eq? #f (typed-array? complex 'u8)))
94 (pass-if (eq? #f (typed-array? scm 'u8))))
95
96 (with-test-prefix "is short"
97 (pass-if (eq? #f (typed-array? bool 's16)))
98 (pass-if (eq? #f (typed-array? char 's16)))
99 (pass-if (eq? #f (typed-array? byte 's16)))
100 (pass-if (eq? #t (typed-array? short 's16)))
101 (pass-if (eq? #f (typed-array? ulong 's16)))
102 (pass-if (eq? #f (typed-array? long 's16)))
103 (pass-if (eq? #f (typed-array? longlong 's16)))
104 (pass-if (eq? #f (typed-array? float 's16)))
105 (pass-if (eq? #f (typed-array? double 's16)))
106 (pass-if (eq? #f (typed-array? complex 's16)))
107 (pass-if (eq? #f (typed-array? scm 's16))))
108
109 (with-test-prefix "is ulong"
110 (pass-if (eq? #f (typed-array? bool 'u32)))
111 (pass-if (eq? #f (typed-array? char 'u32)))
112 (pass-if (eq? #f (typed-array? byte 'u32)))
113 (pass-if (eq? #f (typed-array? short 'u32)))
114 (pass-if (eq? #t (typed-array? ulong 'u32)))
115 (pass-if (eq? #f (typed-array? long 'u32)))
116 (pass-if (eq? #f (typed-array? longlong 'u32)))
117 (pass-if (eq? #f (typed-array? float 'u32)))
118 (pass-if (eq? #f (typed-array? double 'u32)))
119 (pass-if (eq? #f (typed-array? complex 'u32)))
120 (pass-if (eq? #f (typed-array? scm 'u32))))
121
122 (with-test-prefix "is long"
123 (pass-if (eq? #f (typed-array? bool 's32)))
124 (pass-if (eq? #f (typed-array? char 's32)))
125 (pass-if (eq? #f (typed-array? byte 's32)))
126 (pass-if (eq? #f (typed-array? short 's32)))
127 (pass-if (eq? #f (typed-array? ulong 's32)))
128 (pass-if (eq? #t (typed-array? long 's32)))
129 (pass-if (eq? #f (typed-array? longlong 's32)))
130 (pass-if (eq? #f (typed-array? float 's32)))
131 (pass-if (eq? #f (typed-array? double 's32)))
132 (pass-if (eq? #f (typed-array? complex 's32)))
133 (pass-if (eq? #f (typed-array? scm 's32))))
134
135 (with-test-prefix "is long long"
136 (pass-if (eq? #f (typed-array? bool 's64)))
137 (pass-if (eq? #f (typed-array? char 's64)))
138 (pass-if (eq? #f (typed-array? byte 's64)))
139 (pass-if (eq? #f (typed-array? short 's64)))
140 (pass-if (eq? #f (typed-array? ulong 's64)))
141 (pass-if (eq? #f (typed-array? long 's64)))
142 (pass-if (eq? #t (typed-array? longlong 's64)))
143 (pass-if (eq? #f (typed-array? float 's64)))
144 (pass-if (eq? #f (typed-array? double 's64)))
145 (pass-if (eq? #f (typed-array? complex 's64)))
146 (pass-if (eq? #f (typed-array? scm 's64))))
147
148 (with-test-prefix "is float"
149 (pass-if (eq? #f (typed-array? bool 'f32)))
150 (pass-if (eq? #f (typed-array? char 'f32)))
151 (pass-if (eq? #f (typed-array? byte 'f32)))
152 (pass-if (eq? #f (typed-array? short 'f32)))
153 (pass-if (eq? #f (typed-array? ulong 'f32)))
154 (pass-if (eq? #f (typed-array? long 'f32)))
155 (pass-if (eq? #f (typed-array? longlong 'f32)))
156 (pass-if (eq? #t (typed-array? float 'f32)))
157 (pass-if (eq? #f (typed-array? double 'f32)))
158 (pass-if (eq? #f (typed-array? complex 'f32)))
159 (pass-if (eq? #f (typed-array? scm 'f32))))
160
161 (with-test-prefix "is double"
162 (pass-if (eq? #f (typed-array? bool 'f64)))
163 (pass-if (eq? #f (typed-array? char 'f64)))
164 (pass-if (eq? #f (typed-array? byte 'f64)))
165 (pass-if (eq? #f (typed-array? short 'f64)))
166 (pass-if (eq? #f (typed-array? ulong 'f64)))
167 (pass-if (eq? #f (typed-array? long 'f64)))
168 (pass-if (eq? #f (typed-array? longlong 'f64)))
169 (pass-if (eq? #f (typed-array? float 'f64)))
170 (pass-if (eq? #t (typed-array? double 'f64)))
171 (pass-if (eq? #f (typed-array? complex 'f64)))
172 (pass-if (eq? #f (typed-array? scm 'f64))))
173
174 (with-test-prefix "is complex"
175 (pass-if (eq? #f (typed-array? bool 'c64)))
176 (pass-if (eq? #f (typed-array? char 'c64)))
177 (pass-if (eq? #f (typed-array? byte 'c64)))
178 (pass-if (eq? #f (typed-array? short 'c64)))
179 (pass-if (eq? #f (typed-array? ulong 'c64)))
180 (pass-if (eq? #f (typed-array? long 'c64)))
181 (pass-if (eq? #f (typed-array? longlong 'c64)))
182 (pass-if (eq? #f (typed-array? float 'c64)))
183 (pass-if (eq? #f (typed-array? double 'c64)))
184 (pass-if (eq? #t (typed-array? complex 'c64)))
185 (pass-if (eq? #f (typed-array? scm 'c64))))
186
187 (with-test-prefix "is scm"
188 (pass-if (eq? #f (typed-array? bool #t)))
189 (pass-if (eq? #f (typed-array? char #t)))
190 (pass-if (eq? #f (typed-array? byte #t)))
191 (pass-if (eq? #f (typed-array? short #t)))
192 (pass-if (eq? #f (typed-array? ulong #t)))
193 (pass-if (eq? #f (typed-array? long #t)))
194 (pass-if (eq? #f (typed-array? longlong #t)))
195 (pass-if (eq? #f (typed-array? float #t)))
196 (pass-if (eq? #f (typed-array? double #t)))
197 (pass-if (eq? #f (typed-array? complex #t)))
198 (pass-if (eq? #t (typed-array? scm #t))))))
199
200 ;;;
201 ;;; array-equal?
202 ;;;
203
204 (with-test-prefix "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 "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 ;;; shared-array-root
285 ;;;
286
287 (with-test-prefix "shared-array-root"
288
289 (define amap1 (lambda (i) (list (* 2 i))))
290 (define amap2 (lambda (i j) (list (+ 1 (* 2 i)) (+ 1 (* 2 j)))))
291
292 (pass-if "plain vector"
293 (let* ((a (make-vector 4 0))
294 (b (make-shared-array a amap1 2)))
295 (eq? (shared-array-root a) (shared-array-root b) (array-contents a))))
296
297 (pass-if "plain array rank 2"
298 (let* ((a (make-array 0 4 4))
299 (b (make-shared-array a amap2 2 2)))
300 (eq? (shared-array-root a) (shared-array-root b) (array-contents a))))
301
302 (pass-if "uniform array rank 2"
303 (let* ((a (make-typed-array 'c64 0 4 4))
304 (b (make-shared-array a amap2 2 2)))
305 (eq? (shared-array-root a) (shared-array-root b) (array-contents a))))
306
307 (pass-if "bit array rank 2"
308 (let* ((a (make-typed-array 'b #f 4 4))
309 (b (make-shared-array a amap2 2 2)))
310 (eq? (shared-array-root a) (shared-array-root b) (array-contents a)))))
311
312 ;;;
313 ;;; transpose-array
314 ;;;
315
316 ; see strings.test.
317 (define exception:wrong-type-arg
318 (cons #t "Wrong type"))
319
320 (with-test-prefix "transpose-array"
321
322 (pass-if-exception "non array argument" exception:wrong-type-arg
323 (transpose-array 99))
324
325 (pass-if "rank 0"
326 (let* ((a #0(99))
327 (b (transpose-array a)))
328 (and (array-equal? a b)
329 (eq? (shared-array-root a) (shared-array-root b)))))
330
331 (pass-if "rank 1"
332 (let* ((a #(1 2 3))
333 (b (transpose-array a 0)))
334 (and (array-equal? a b)
335 (eq? (shared-array-root a) (shared-array-root b)))))
336
337 (pass-if "rank 2"
338 (let* ((a #2((1 2 3) (4 5 6)))
339 (b (transpose-array a 1 0))
340 (c (transpose-array a 0 1)))
341 (and (array-equal? b #2((1 4) (2 5) (3 6)))
342 (array-equal? c a)
343 (eq? (shared-array-root a)
344 (shared-array-root b)
345 (shared-array-root c)))))
346
347 ; rank > 2 is needed to check against the inverted axis index logic.
348 (pass-if "rank 3"
349 (let* ((a #3(((0 1 2 3) (4 5 6 7) (8 9 10 11))
350 ((12 13 14 15) (16 17 18 19) (20 21 22 23))))
351 (b (transpose-array a 1 2 0)))
352 (and (array-equal? b #3(((0 4 8) (12 16 20)) ((1 5 9) (13 17 21))
353 ((2 6 10) (14 18 22)) ((3 7 11) (15 19 23))))
354 (eq? (shared-array-root a)
355 (shared-array-root b))))))
356
357 ;;;
358 ;;; array->list
359 ;;;
360
361 (with-test-prefix "array->list"
362 (pass-if-equal '(1 2 3) (array->list #s16(1 2 3)))
363 (pass-if-equal '(1 2 3) (array->list #(1 2 3)))
364 (pass-if-equal '((1 2) (3 4) (5 6)) (array->list #2((1 2) (3 4) (5 6))))
365 (pass-if-equal '() (array->list #()))
366
367 (pass-if-equal "http://bugs.gnu.org/12465 - ok"
368 '(3 4)
369 (let* ((a #2((1 2) (3 4)))
370 (b (make-shared-array a (lambda (j) (list 1 j)) 2)))
371 (array->list b)))
372 (pass-if-equal "http://bugs.gnu.org/12465 - bad"
373 '(2 4)
374 (let* ((a #2((1 2) (3 4)))
375 (b (make-shared-array a (lambda (i) (list i 1)) 2)))
376 (array->list b))))
377
378 ;;;
379 ;;; array-fill!
380 ;;;
381
382 (with-test-prefix "array-fill!"
383
384 (with-test-prefix "bool"
385 (let ((a (make-bitvector 1 #t)))
386 (pass-if "#f" (array-fill! a #f) #t)
387 (pass-if "#t" (array-fill! a #t) #t)))
388
389 (with-test-prefix "char"
390 (let ((a (make-string 1 #\a)))
391 (pass-if "x" (array-fill! a #\x) #t)))
392
393 (with-test-prefix "byte"
394 (let ((a (make-s8vector 1 0)))
395 (pass-if "0" (array-fill! a 0) #t)
396 (pass-if "127" (array-fill! a 127) #t)
397 (pass-if "-128" (array-fill! a -128) #t)
398 (pass-if-exception "128" exception:out-of-range
399 (array-fill! a 128))
400 (pass-if-exception "-129" exception:out-of-range
401 (array-fill! a -129))
402 (pass-if-exception "symbol" exception:wrong-type-arg
403 (array-fill! a 'symbol))))
404
405 (with-test-prefix "short"
406 (let ((a (make-s16vector 1 0)))
407 (pass-if "0" (array-fill! a 0) #t)
408 (pass-if "123" (array-fill! a 123) #t)
409 (pass-if "-123" (array-fill! a -123) #t)))
410
411 (with-test-prefix "ulong"
412 (let ((a (make-u32vector 1 1)))
413 (pass-if "0" (array-fill! a 0) #t)
414 (pass-if "123" (array-fill! a 123) #t)
415 (pass-if-exception "-123" exception:out-of-range
416 (array-fill! a -123) #t)))
417
418 (with-test-prefix "long"
419 (let ((a (make-s32vector 1 -1)))
420 (pass-if "0" (array-fill! a 0) #t)
421 (pass-if "123" (array-fill! a 123) #t)
422 (pass-if "-123" (array-fill! a -123) #t)))
423
424 (with-test-prefix "float"
425 (let ((a (make-f32vector 1 1.0)))
426 (pass-if "0.0" (array-fill! a 0) #t)
427 (pass-if "123.0" (array-fill! a 123.0) #t)
428 (pass-if "-123.0" (array-fill! a -123.0) #t)
429 (pass-if "0" (array-fill! a 0) #t)
430 (pass-if "123" (array-fill! a 123) #t)
431 (pass-if "-123" (array-fill! a -123) #t)
432 (pass-if "5/8" (array-fill! a 5/8) #t)))
433
434 (with-test-prefix "double"
435 (let ((a (make-f64vector 1 1/3)))
436 (pass-if "0.0" (array-fill! a 0) #t)
437 (pass-if "123.0" (array-fill! a 123.0) #t)
438 (pass-if "-123.0" (array-fill! a -123.0) #t)
439 (pass-if "0" (array-fill! a 0) #t)
440 (pass-if "123" (array-fill! a 123) #t)
441 (pass-if "-123" (array-fill! a -123) #t)
442 (pass-if "5/8" (array-fill! a 5/8) #t)))
443
444 (with-test-prefix "noncompact"
445 (let* ((a (make-array 0 3 3))
446 (b (make-shared-array a (lambda (i) (list i i)) 3)))
447 (array-fill! b 9)
448 (pass-if
449 (and (equal? b #(9 9 9))
450 (equal? a #2((9 0 0) (0 9 0) (0 0 9))))))))
451
452 ;;;
453 ;;; array-copy!
454 ;;;
455
456 (with-test-prefix "array-copy!"
457
458 (pass-if "rank 2"
459 (let ((a #2((1 2) (3 4)))
460 (b (make-array 0 2 2))
461 (c (make-array 0 2 2))
462 (d (make-array 0 2 2))
463 (e (make-array 0 2 2)))
464 (array-copy! a b)
465 (array-copy! a (transpose-array c 1 0))
466 (array-copy! (transpose-array a 1 0) d)
467 (array-copy! (transpose-array a 1 0) (transpose-array e 1 0))
468 (and (equal? a #2((1 2) (3 4)))
469 (equal? b #2((1 2) (3 4)))
470 (equal? c #2((1 3) (2 4)))
471 (equal? d #2((1 3) (2 4)))
472 (equal? e #2((1 2) (3 4))))))
473
474 (pass-if "rank 1"
475 (let* ((a #2((1 2) (3 4)))
476 (b (make-shared-array a (lambda (j) (list 1 j)) 2))
477 (c (make-shared-array a (lambda (i) (list (- 1 i) 1)) 2))
478 (d (make-array 0 2))
479 (e (make-array 0 2)))
480 (array-copy! b d)
481 (array-copy! c e)
482 (and (equal? d #(3 4))
483 (equal? e #(4 2)))))
484
485 (pass-if "rank 0"
486 (let ((a #0(99))
487 (b (make-array 0)))
488 (array-copy! a b)
489 (equal? b #0(99)))))
490
491
492 ;;;
493 ;;; array-in-bounds?
494 ;;;
495
496 (with-test-prefix "array-in-bounds?"
497
498 (pass-if (let ((a (make-array #f '(425 425))))
499 (eq? #f (array-in-bounds? a 0)))))
500
501 ;;;
502 ;;; array-prototype
503 ;;;
504
505 (with-test-prefix "array-type"
506
507 (with-test-prefix "on make-foo-vector"
508
509 (pass-if "bool"
510 (eq? 'b (array-type (make-bitvector 1))))
511
512 (pass-if "char"
513 (eq? 'a (array-type (make-string 1))))
514
515 (pass-if "byte"
516 (eq? 'u8 (array-type (make-u8vector 1))))
517
518 (pass-if "short"
519 (eq? 's16 (array-type (make-s16vector 1))))
520
521 (pass-if "ulong"
522 (eq? 'u32 (array-type (make-u32vector 1))))
523
524 (pass-if "long"
525 (eq? 's32 (array-type (make-s32vector 1))))
526
527 (pass-if "long long"
528 (eq? 's64 (array-type (make-s64vector 1))))
529
530 (pass-if "float"
531 (eq? 'f32 (array-type (make-f32vector 1))))
532
533 (pass-if "double"
534 (eq? 'f64 (array-type (make-f64vector 1))))
535
536 (pass-if "complex"
537 (eq? 'c64 (array-type (make-c64vector 1))))
538
539 (pass-if "scm"
540 (eq? #t (array-type (make-vector 1)))))
541
542 (with-test-prefix "on make-typed-array"
543
544 (let ((types '(b a u8 s8 u16 s16 u32 s32 u64 u64 f32 f64 c32 c64)))
545 (for-each (lambda (type)
546 (pass-if (symbol->string type)
547 (eq? type
548 (array-type (make-typed-array type
549 *unspecified*
550 '(5 6))))))
551 types))))
552
553 ;;;
554 ;;; array-set!
555 ;;;
556
557 (with-test-prefix "array-set!"
558
559 (with-test-prefix "bitvector"
560
561 ;; in Guile 1.8.0 a bug in bitvector_set() caused a segv in array-set!
562 ;; on a bitvector like the following
563 (let ((a (make-bitvector 1)))
564 (pass-if "one elem set #t"
565 (begin
566 (array-set! a #t 0)
567 (eq? #t (array-ref a 0))))
568 (pass-if "one elem set #f"
569 (begin
570 (array-set! a #f 0)
571 (eq? #f (array-ref a 0))))))
572
573 (with-test-prefix "byte"
574
575 (let ((a (make-s8vector 1)))
576
577 (pass-if "-128"
578 (begin (array-set! a -128 0) #t))
579 (pass-if "0"
580 (begin (array-set! a 0 0) #t))
581 (pass-if "127"
582 (begin (array-set! a 127 0) #t))
583 (pass-if-exception "-129" exception:out-of-range
584 (begin (array-set! a -129 0) #t))
585 (pass-if-exception "128" exception:out-of-range
586 (begin (array-set! a 128 0) #t))))
587
588 (with-test-prefix "short"
589
590 (let ((a (make-s16vector 1)))
591 ;; true if n can be array-set! into a
592 (define (fits? n)
593 (false-if-exception (begin (array-set! a n 0) #t)))
594
595 (with-test-prefix "store/fetch"
596 ;; Check array-ref gives back what was put with array-set!.
597 ;; In Guile 1.6.4 and earlier, array-set! only demanded an inum and
598 ;; would silently truncate to a short.
599
600 (do ((n 1 (1+ (* 2 n)))) ;; n=2^k-1
601 ((not (fits? n)))
602 (array-set! a n 0)
603 (pass-if n
604 (= n (array-ref a 0))))
605
606 (do ((n -1 (* 2 n))) ;; -n=2^k
607 ((not (fits? n)))
608 (array-set! a n 0)
609 (pass-if n
610 (= n (array-ref a 0))))))))
611
612 ;;;
613 ;;; array-set!
614 ;;;
615
616 (with-test-prefix "array-set!"
617
618 (with-test-prefix "one dim"
619 (let ((a (make-array #f '(3 5))))
620 (pass-if "start"
621 (array-set! a 'y 3)
622 #t)
623 (pass-if "end"
624 (array-set! a 'y 5)
625 #t)
626 (pass-if-exception "start-1" exception:out-of-range
627 (array-set! a 'y 2))
628 (pass-if-exception "end+1" exception:out-of-range
629 (array-set! a 'y 6))
630 (pass-if-exception "two indexes" exception:wrong-num-indices
631 (array-set! a 'y 6 7))))
632
633 (with-test-prefix "two dim"
634 (let ((a (make-array #f '(3 5) '(7 9))))
635 (pass-if "start"
636 (array-set! a 'y 3 7)
637 #t)
638 (pass-if "end"
639 (array-set! a 'y 5 9)
640 #t)
641 (pass-if-exception "start i-1" exception:out-of-range
642 (array-set! a 'y 2 7))
643 (pass-if-exception "end i+1" exception:out-of-range
644 (array-set! a 'y 6 9))
645 (pass-if-exception "one index" exception:wrong-num-indices
646 (array-set! a 'y 4))
647 (pass-if-exception "three indexes" exception:wrong-num-indices
648 (array-set! a 'y 4 8 0)))))
649
650 ;;;
651 ;;; uniform-vector
652 ;;;
653
654 (with-test-prefix "uniform-vector"
655
656 (with-test-prefix "uniform-vector-ref byte"
657
658 (let ((a (make-s8vector 1)))
659
660 (pass-if "0"
661 (begin
662 (array-set! a 0 0)
663 (= 0 (uniform-vector-ref a 0))))
664 (pass-if "127"
665 (begin
666 (array-set! a 127 0)
667 (= 127 (uniform-vector-ref a 0))))
668 (pass-if "-128"
669 (begin
670 (array-set! a -128 0)
671 (= -128 (uniform-vector-ref a 0))))))
672
673 (with-test-prefix "shared with rank 1 remain uniform vectors"
674
675 (let ((a #f64(1 2 3 4)))
676
677 (pass-if "change offset"
678 (let ((b (make-shared-array a (lambda (i) (list (+ i 1))) 3)))
679 (and (uniform-vector? b)
680 (= 3 (uniform-vector-length b))
681 (array-equal? b #f64(2 3 4)))))
682
683 (pass-if "change stride"
684 (let ((c (make-shared-array a (lambda (i) (list (* i 2))) 2)))
685 (and (uniform-vector? c)
686 (= 2 (uniform-vector-length c))
687 (array-equal? c #f64(1 3))))))))
688
689 ;;;
690 ;;; syntax
691 ;;;
692
693 (with-test-prefix "syntax"
694
695 (pass-if "rank and lower bounds"
696 ;; uniform u32 array of rank 2 with index ranges 2..3 and 7..8.
697 (let ((a '#2u32@2@7((1 2) (3 4))))
698 (and (array? a)
699 (typed-array? a 'u32)
700 (= (array-rank a) 2)
701 (let loop ((bounds '((2 7) (2 8) (3 7) (3 8)))
702 (result #t))
703 (if (null? bounds)
704 result
705 (and result
706 (loop (cdr bounds)
707 (apply array-in-bounds? a (car bounds)))))))))
708
709 (pass-if "negative lower bound"
710 (let ((a '#1@-3(a b)))
711 (and (array? a)
712 (= (array-rank a) 1)
713 (array-in-bounds? a -3) (array-in-bounds? a -2)
714 (eq? 'a (array-ref a -3))
715 (eq? 'b (array-ref a -2)))))
716
717 (pass-if-exception "negative length" exception:length-non-negative
718 (with-input-from-string "'#1:-3(#t #t)" read))
719
720 (pass-if "bitvector is self-evaluating"
721 (equal? (compile (bitvector)) (bitvector))))
722
723 ;;;
724 ;;; equal? with vector and one-dimensional array
725 ;;;
726
727 (with-test-prefix "equal?"
728 (pass-if "array and non-array"
729 (not (equal? #2f64((0 1) (2 3)) 100)))
730
731 (pass-if "empty vectors of different types"
732 (not (equal? #s32() #f64())))
733
734 (pass-if "empty arrays of different types"
735 (not (equal? #2s32() #2f64())))
736
737 (pass-if "empty arrays of the same type"
738 (equal? #s32() #s32()))
739
740 (pass-if "identical uniform vectors of the same type"
741 (equal? #s32(1) #s32(1)))
742
743 (pass-if "nonidentical uniform vectors of the same type"
744 (not (equal? #s32(1) #s32(-1))))
745
746 (pass-if "identical uniform vectors of different types"
747 (not (equal? #s32(1) #s64(1))))
748
749 (pass-if "nonidentical uniform vectors of different types"
750 (not (equal? #s32(1) #s64(-1))))
751
752 (pass-if "vector and one-dimensional array"
753 (equal? (make-shared-array #2((a b c) (d e f) (g h i))
754 (lambda (i) (list i i))
755 '(0 2))
756 #(a e i))))
757
758 ;;;
759 ;;; slices as generalized vectors
760 ;;;
761
762 (let ((array #2u32((0 1) (2 3))))
763 (define (array-row a i)
764 (make-shared-array a (lambda (j) (list i j))
765 (cadr (array-dimensions a))))
766 (with-test-prefix "generalized vector slices"
767 (pass-if (equal? (array-row array 1)
768 #u32(2 3)))
769 (pass-if (equal? (array-ref (array-row array 1) 0)
770 2))))