optimize access to arrays of rank 1 or 2
[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 ;;; array->list
211 ;;;
212
213 (with-test-prefix "array->list"
214 (pass-if-equal '(1 2 3) (array->list #s16(1 2 3)))
215 (pass-if-equal '(1 2 3) (array->list #(1 2 3)))
216 (pass-if-equal '((1 2) (3 4) (5 6)) (array->list #2((1 2) (3 4) (5 6))))
217 (pass-if-equal '() (array->list #()))
218
219 (pass-if-equal "http://bugs.gnu.org/12465 - ok"
220 '(3 4)
221 (let* ((a #2((1 2) (3 4)))
222 (b (make-shared-array a (lambda (j) (list 1 j)) 2)))
223 (array->list b)))
224 (pass-if-equal "http://bugs.gnu.org/12465 - bad"
225 '(2 4)
226 (let* ((a #2((1 2) (3 4)))
227 (b (make-shared-array a (lambda (i) (list i 1)) 2)))
228 (array->list b))))
229
230 ;;;
231 ;;; generalized-vector->list
232 ;;;
233
234 (with-test-prefix "generalized-vector->list"
235 (pass-if-equal '(1 2 3) (generalized-vector->list #s16(1 2 3)))
236 (pass-if-equal '(1 2 3) (generalized-vector->list #(1 2 3)))
237 (pass-if-equal '() (generalized-vector->list #()))
238
239 (pass-if-equal "http://bugs.gnu.org/12465 - ok"
240 '(3 4)
241 (let* ((a #2((1 2) (3 4)))
242 (b (make-shared-array a (lambda (j) (list 1 j)) 2)))
243 (generalized-vector->list b)))
244 (pass-if-equal "http://bugs.gnu.org/12465 - bad"
245 '(2 4)
246 (let* ((a #2((1 2) (3 4)))
247 (b (make-shared-array a (lambda (i) (list i 1)) 2)))
248 (generalized-vector->list b))))
249
250 ;;;
251 ;;; array-fill!
252 ;;;
253
254 (with-test-prefix "array-fill!"
255
256 (with-test-prefix "bool"
257 (let ((a (make-bitvector 1 #t)))
258 (pass-if "#f" (array-fill! a #f) #t)
259 (pass-if "#t" (array-fill! a #t) #t)))
260
261 (with-test-prefix "char"
262 (let ((a (make-string 1 #\a)))
263 (pass-if "x" (array-fill! a #\x) #t)))
264
265 (with-test-prefix "byte"
266 (let ((a (make-s8vector 1 0)))
267 (pass-if "0" (array-fill! a 0) #t)
268 (pass-if "127" (array-fill! a 127) #t)
269 (pass-if "-128" (array-fill! a -128) #t)
270 (pass-if-exception "128" exception:out-of-range
271 (array-fill! a 128))
272 (pass-if-exception "-129" exception:out-of-range
273 (array-fill! a -129))
274 (pass-if-exception "symbol" exception:wrong-type-arg
275 (array-fill! a 'symbol))))
276
277 (with-test-prefix "short"
278 (let ((a (make-s16vector 1 0)))
279 (pass-if "0" (array-fill! a 0) #t)
280 (pass-if "123" (array-fill! a 123) #t)
281 (pass-if "-123" (array-fill! a -123) #t)))
282
283 (with-test-prefix "ulong"
284 (let ((a (make-u32vector 1 1)))
285 (pass-if "0" (array-fill! a 0) #t)
286 (pass-if "123" (array-fill! a 123) #t)
287 (pass-if-exception "-123" exception:out-of-range
288 (array-fill! a -123) #t)))
289
290 (with-test-prefix "long"
291 (let ((a (make-s32vector 1 -1)))
292 (pass-if "0" (array-fill! a 0) #t)
293 (pass-if "123" (array-fill! a 123) #t)
294 (pass-if "-123" (array-fill! a -123) #t)))
295
296 (with-test-prefix "float"
297 (let ((a (make-f32vector 1 1.0)))
298 (pass-if "0.0" (array-fill! a 0) #t)
299 (pass-if "123.0" (array-fill! a 123.0) #t)
300 (pass-if "-123.0" (array-fill! a -123.0) #t)
301 (pass-if "0" (array-fill! a 0) #t)
302 (pass-if "123" (array-fill! a 123) #t)
303 (pass-if "-123" (array-fill! a -123) #t)
304 (pass-if "5/8" (array-fill! a 5/8) #t)))
305
306 (with-test-prefix "double"
307 (let ((a (make-f64vector 1 1/3)))
308 (pass-if "0.0" (array-fill! a 0) #t)
309 (pass-if "123.0" (array-fill! a 123.0) #t)
310 (pass-if "-123.0" (array-fill! a -123.0) #t)
311 (pass-if "0" (array-fill! a 0) #t)
312 (pass-if "123" (array-fill! a 123) #t)
313 (pass-if "-123" (array-fill! a -123) #t)
314 (pass-if "5/8" (array-fill! a 5/8) #t))))
315
316 ;;;
317 ;;; array-in-bounds?
318 ;;;
319
320 (with-test-prefix "array-in-bounds?"
321
322 (pass-if (let ((a (make-array #f '(425 425))))
323 (eq? #f (array-in-bounds? a 0)))))
324
325 ;;;
326 ;;; array-prototype
327 ;;;
328
329 (with-test-prefix "array-type"
330
331 (with-test-prefix "on make-foo-vector"
332
333 (pass-if "bool"
334 (eq? 'b (array-type (make-bitvector 1))))
335
336 (pass-if "char"
337 (eq? 'a (array-type (make-string 1))))
338
339 (pass-if "byte"
340 (eq? 'u8 (array-type (make-u8vector 1))))
341
342 (pass-if "short"
343 (eq? 's16 (array-type (make-s16vector 1))))
344
345 (pass-if "ulong"
346 (eq? 'u32 (array-type (make-u32vector 1))))
347
348 (pass-if "long"
349 (eq? 's32 (array-type (make-s32vector 1))))
350
351 (pass-if "long long"
352 (eq? 's64 (array-type (make-s64vector 1))))
353
354 (pass-if "float"
355 (eq? 'f32 (array-type (make-f32vector 1))))
356
357 (pass-if "double"
358 (eq? 'f64 (array-type (make-f64vector 1))))
359
360 (pass-if "complex"
361 (eq? 'c64 (array-type (make-c64vector 1))))
362
363 (pass-if "scm"
364 (eq? #t (array-type (make-vector 1)))))
365
366 (with-test-prefix "on make-typed-array"
367
368 (let ((types '(b a u8 s8 u16 s16 u32 s32 u64 u64 f32 f64 c32 c64)))
369 (for-each (lambda (type)
370 (pass-if (symbol->string type)
371 (eq? type
372 (array-type (make-typed-array type
373 *unspecified*
374 '(5 6))))))
375 types))))
376
377 ;;;
378 ;;; array-set!
379 ;;;
380
381 (with-test-prefix "array-set!"
382
383 (with-test-prefix "bitvector"
384
385 ;; in Guile 1.8.0 a bug in bitvector_set() caused a segv in array-set!
386 ;; on a bitvector like the following
387 (let ((a (make-bitvector 1)))
388 (pass-if "one elem set #t"
389 (begin
390 (array-set! a #t 0)
391 (eq? #t (array-ref a 0))))
392 (pass-if "one elem set #f"
393 (begin
394 (array-set! a #f 0)
395 (eq? #f (array-ref a 0))))))
396
397 (with-test-prefix "byte"
398
399 (let ((a (make-s8vector 1)))
400
401 (pass-if "-128"
402 (begin (array-set! a -128 0) #t))
403 (pass-if "0"
404 (begin (array-set! a 0 0) #t))
405 (pass-if "127"
406 (begin (array-set! a 127 0) #t))
407 (pass-if-exception "-129" exception:out-of-range
408 (begin (array-set! a -129 0) #t))
409 (pass-if-exception "128" exception:out-of-range
410 (begin (array-set! a 128 0) #t))))
411
412 (with-test-prefix "short"
413
414 (let ((a (make-s16vector 1)))
415 ;; true if n can be array-set! into a
416 (define (fits? n)
417 (false-if-exception (begin (array-set! a n 0) #t)))
418
419 (with-test-prefix "store/fetch"
420 ;; Check array-ref gives back what was put with array-set!.
421 ;; In Guile 1.6.4 and earlier, array-set! only demanded an inum and
422 ;; would silently truncate to a short.
423
424 (do ((n 1 (1+ (* 2 n)))) ;; n=2^k-1
425 ((not (fits? n)))
426 (array-set! a n 0)
427 (pass-if n
428 (= n (array-ref a 0))))
429
430 (do ((n -1 (* 2 n))) ;; -n=2^k
431 ((not (fits? n)))
432 (array-set! a n 0)
433 (pass-if n
434 (= n (array-ref a 0))))))))
435
436 ;;;
437 ;;; array-set!
438 ;;;
439
440 (with-test-prefix "array-set!"
441
442 (with-test-prefix "one dim"
443 (let ((a (make-array #f '(3 5))))
444 (pass-if "start"
445 (array-set! a 'y 3)
446 #t)
447 (pass-if "end"
448 (array-set! a 'y 5)
449 #t)
450 (pass-if-exception "start-1" exception:out-of-range
451 (array-set! a 'y 2))
452 (pass-if-exception "end+1" exception:out-of-range
453 (array-set! a 'y 6))
454 (pass-if-exception "two indexes" exception:wrong-num-indices
455 (array-set! a 'y 6 7))))
456
457 (with-test-prefix "two dim"
458 (let ((a (make-array #f '(3 5) '(7 9))))
459 (pass-if "start"
460 (array-set! a 'y 3 7)
461 #t)
462 (pass-if "end"
463 (array-set! a 'y 5 9)
464 #t)
465 (pass-if-exception "start i-1" exception:out-of-range
466 (array-set! a 'y 2 7))
467 (pass-if-exception "end i+1" exception:out-of-range
468 (array-set! a 'y 6 9))
469 (pass-if-exception "one index" exception:wrong-num-indices
470 (array-set! a 'y 4))
471 (pass-if-exception "three indexes" exception:wrong-num-indices
472 (array-set! a 'y 4 8 0)))))
473
474 ;;;
475 ;;; make-shared-array
476 ;;;
477
478 (define exception:mapping-out-of-range
479 (cons 'misc-error "^mapping out of range")) ;; per scm_make_shared_array
480
481 (with-test-prefix "make-shared-array"
482
483 ;; this failed in guile 1.8.0
484 (pass-if "vector unchanged"
485 (let* ((a (make-array #f '(0 7)))
486 (s (make-shared-array a list '(0 7))))
487 (array-equal? a s)))
488
489 (pass-if-exception "vector, high too big" exception:mapping-out-of-range
490 (let* ((a (make-array #f '(0 7))))
491 (make-shared-array a list '(0 8))))
492
493 (pass-if-exception "vector, low too big" exception:out-of-range
494 (let* ((a (make-array #f '(0 7))))
495 (make-shared-array a list '(-1 7))))
496
497 (pass-if "truncate columns"
498 (array-equal? (make-shared-array #2((a b c) (d e f) (g h i)) list 3 2)
499 #2((a b) (d e) (g h))))
500
501 (pass-if "pick one column"
502 (array-equal? (make-shared-array #2((a b c) (d e f) (g h i))
503 (lambda (i) (list i 2))
504 '(0 2))
505 #(c f i)))
506
507 (pass-if "diagonal"
508 (array-equal? (make-shared-array #2((a b c) (d e f) (g h i))
509 (lambda (i) (list i i))
510 '(0 2))
511 #(a e i)))
512
513 ;; this failed in guile 1.8.0
514 (pass-if "2 dims from 1 dim"
515 (array-equal? (make-shared-array #1(a b c d e f g h i j k l)
516 (lambda (i j) (list (+ (* i 3) j)))
517 4 3)
518 #2((a b c) (d e f) (g h i) (j k l))))
519
520 (pass-if "reverse columns"
521 (array-equal? (make-shared-array #2((a b c) (d e f) (g h i))
522 (lambda (i j) (list i (- 2 j)))
523 3 3)
524 #2((c b a) (f e d) (i h g))))
525
526 (pass-if "fixed offset, 0 based becomes 1 based"
527 (let* ((x #2((a b c) (d e f) (g h i)))
528 (y (make-shared-array x
529 (lambda (i j) (list (1- i) (1- j)))
530 '(1 3) '(1 3))))
531 (and (eq? (array-ref x 0 0) 'a)
532 (eq? (array-ref y 1 1) 'a))))
533
534 ;; this failed in guile 1.8.0
535 (pass-if "stride every third element"
536 (array-equal? (make-shared-array #1(a b c d e f g h i j k l)
537 (lambda (i) (list (* i 3)))
538 4)
539 #1(a d g j)))
540
541 (pass-if "shared of shared"
542 (let* ((a #2((1 2 3) (4 5 6) (7 8 9)))
543 (s1 (make-shared-array a (lambda (i) (list i 1)) 3))
544 (s2 (make-shared-array s1 list '(1 2))))
545 (and (eqv? 5 (array-ref s2 1))
546 (eqv? 8 (array-ref s2 2))))))
547
548 ;;;
549 ;;; uniform-vector-ref
550 ;;;
551
552 (with-test-prefix "uniform-vector-ref"
553
554 (with-test-prefix "byte"
555
556 (let ((a (make-s8vector 1)))
557
558 (pass-if "0"
559 (begin
560 (array-set! a 0 0)
561 (= 0 (uniform-vector-ref a 0))))
562 (pass-if "127"
563 (begin
564 (array-set! a 127 0)
565 (= 127 (uniform-vector-ref a 0))))
566 (pass-if "-128"
567 (begin
568 (array-set! a -128 0)
569 (= -128 (uniform-vector-ref a 0)))))))
570
571 ;;;
572 ;;; syntax
573 ;;;
574
575 (with-test-prefix "syntax"
576
577 (pass-if "rank and lower bounds"
578 ;; uniform u32 array of rank 2 with index ranges 2..3 and 7..8.
579 (let ((a '#2u32@2@7((1 2) (3 4))))
580 (and (array? a)
581 (typed-array? a 'u32)
582 (= (array-rank a) 2)
583 (let loop ((bounds '((2 7) (2 8) (3 7) (3 8)))
584 (result #t))
585 (if (null? bounds)
586 result
587 (and result
588 (loop (cdr bounds)
589 (apply array-in-bounds? a (car bounds)))))))))
590
591 (pass-if "negative lower bound"
592 (let ((a '#1@-3(a b)))
593 (and (array? a)
594 (= (array-rank a) 1)
595 (array-in-bounds? a -3) (array-in-bounds? a -2)
596 (eq? 'a (array-ref a -3))
597 (eq? 'b (array-ref a -2)))))
598
599 (pass-if-exception "negative length" exception:length-non-negative
600 (with-input-from-string "'#1:-3(#t #t)" read))
601
602 (pass-if "bitvector is self-evaluating"
603 (equal? (compile (bitvector)) (bitvector))))
604
605 ;;;
606 ;;; equal? with vector and one-dimensional array
607 ;;;
608
609 (with-test-prefix "equal?"
610 (pass-if "array and non-array"
611 (not (equal? #2f64((0 1) (2 3)) 100)))
612
613 (pass-if "empty vectors of different types"
614 (not (equal? #s32() #f64())))
615
616 (pass-if "empty arrays of different types"
617 (not (equal? #2s32() #2f64())))
618
619 (pass-if "empty arrays of the same type"
620 (equal? #s32() #s32()))
621
622 (pass-if "identical uniform vectors of the same type"
623 (equal? #s32(1) #s32(1)))
624
625 (pass-if "nonidentical uniform vectors of the same type"
626 (not (equal? #s32(1) #s32(-1))))
627
628 (pass-if "identical uniform vectors of different types"
629 (not (equal? #s32(1) #s64(1))))
630
631 (pass-if "nonidentical uniform vectors of different types"
632 (not (equal? #s32(1) #s64(-1))))
633
634 (pass-if "vector and one-dimensional array"
635 (equal? (make-shared-array #2((a b c) (d e f) (g h i))
636 (lambda (i) (list i i))
637 '(0 2))
638 #(a e i))))
639
640 ;;;
641 ;;; slices as generalized vectors
642 ;;;
643
644 (let ((array #2u32((0 1) (2 3))))
645 (define (array-row a i)
646 (make-shared-array a (lambda (j) (list i j))
647 (cadr (array-dimensions a))))
648 (with-test-prefix "generalized vector slices"
649 (pass-if (equal? (array-row array 1)
650 #u32(2 3)))
651 (pass-if (equal? (array-ref (array-row array 1) 0)
652 2))
653 (pass-if (equal? (generalized-vector-ref (array-row array 1) 0)
654 2))))