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