Debian Codegraph @

ext/uvector/test.scm

raw
1 ;;
2 ;; test for SRFI-4 module
3 ;;
4
5 (use gauche.test)
6 (use scheme.list)
7
8 (test-start "uniform vector and array")
9 (use gauche.uvector)
10 (test-module 'gauche.uvector)
11
12 (define-macro (expand-uvec tags body)
13 (define (subst tag str)
14 (regexp-replace-all #/@/ str (symbol->string tag)))
15 (define (expand tag body)
16 (cond [(symbol? body) (string->symbol (subst tag (symbol->string body)))]
17 [(string? body) (subst tag body)]
18 [(pair? body) (cons (expand tag (car body))
19 (expand tag (cdr body)))]
20 [else body]))
21 (if (list? tags)
22 `(begin ,@(map (cut expand <> body) tags))
23 (expand tags body)))
24
25 ;;-------------------------------------------------------------------
26 (test-section "reader syntax")
27
28 (test* "#s8()" #t (s8vector? '#s8(0 1 2 3 4)))
29 (test* "#s8()" #f (s8vector? '#(0 1 2 3 4)))
30 (test* "#u8()" #t (u8vector? '#u8(0 1 2 3 4)))
31 (test* "#u8()" #f (u8vector? '#(0 1 2 3 4)))
32 (test* "#s16()" #t (s16vector? '#s16(0 1 2 3 4)))
33 (test* "#s16()" #f (s16vector? '#(0 1 2 3 4)))
34 (test* "#u16()" #t (u16vector? '#u16(0 1 2 3 4)))
35 (test* "#u16()" #f (u16vector? '#(0 1 2 3 4)))
36 (test* "#s32()" #t (s32vector? '#s32(0 1 2 3 4)))
37 (test* "#s32()" #f (s32vector? '#(0 1 2 3 4)))
38 (test* "#u32()" #t (u32vector? '#u32(0 1 2 3 4)))
39 (test* "#u32()" #f (u32vector? '#(0 1 2 3 4)))
40 (test* "#s64()" #t (s64vector? '#s64(0 1 2 3 4)))
41 (test* "#s64()" #f (s64vector? '#(0 1 2 3 4)))
42 (test* "#u64()" #t (u64vector? '#u64(0 1 2 3 4)))
43 (test* "#u64()" #f (u64vector? '#(0 1 2 3 4)))
44 (test* "#f16()" #t (f16vector? '#f16(0.0 1.0 2.0 3.0 4.0)))
45 (test* "#f16()" #f (f16vector? '#(0.0 1.0 2.0 3.0 4.0)))
46 (test* "#f32()" #t (f32vector? '#f32(0.0 1.0 2.0 3.0 4.0)))
47 (test* "#f32()" #f (f32vector? '#(0.0 1.0 2.0 3.0 4.0)))
48 (test* "#f64()" #t (f64vector? '#f64(0.0 1.0 2.0 3.0 4.0)))
49 (test* "#f64()" #f (f64vector? '#(0.0 1.0 2.0 3.0 4.0)))
50 (test* "#c32()" #t (c32vector? '#c32(1+i 1-i 2+2i 2-2i)))
51 (test* "#c32()" #f (c32vector? '#(1+i 1-i 2+2i 2-2i)))
52 (test* "#c64()" #t (c64vector? '#c64(1+i 1-i 2+2i 2-2i)))
53 (test* "#c64()" #f (c64vector? '#(1+i 1-i 2+2i 2-2i)))
54 (test* "#c128()" #t (c128vector? '#c128(1+i 1-i 2+2i 2-2i)))
55 (test* "#c128()" #f (c128vector? '#(1+i 1-i 2+2i 2-2i)))
56
57 ;;-------------------------------------------------------------------
58 (test-section "writer syntax")
59
60 (test* "#s8()" "#s8(0 1 2 3 4)"
61 (with-output-to-string
62 (^[] (write (apply s8vector (iota 5))))))
63 (test* "#u8()" "#u8(0 1 2 3 4)"
64 (with-output-to-string
65 (^[] (write (apply u8vector (iota 5))))))
66 (test* "#s16()" "#s16(0 1 2 3 4)"
67 (with-output-to-string
68 (^[] (write (apply s16vector (iota 5))))))
69 (test* "#u16()" "#u16(0 1 2 3 4)"
70 (with-output-to-string
71 (^[] (write (apply u16vector (iota 5))))))
72 (test* "#s32()" "#s32(0 1 2 3 4)"
73 (with-output-to-string
74 (^[] (write (apply s32vector (iota 5))))))
75 (test* "#u32()" "#u32(0 1 2 3 4)"
76 (with-output-to-string
77 (^[] (write (apply u32vector (iota 5))))))
78 (test* "#s64()" "#s64(0 1 2 3 4)"
79 (with-output-to-string
80 (^[] (write (apply s64vector (iota 5))))))
81 (test* "#u64()" "#u64(0 1 2 3 4)"
82 (with-output-to-string
83 (^[] (write (apply u64vector (iota 5))))))
84 (test* "#f16()" "#f16()"
85 (with-output-to-string
86 (^[] (write (f16vector)))))
87 (test* "#f32()" "#f32()"
88 (with-output-to-string
89 (^[] (write (f32vector)))))
90 (test* "#f64()" "#f64()"
91 (with-output-to-string
92 (^[] (write (f64vector)))))
93 (test* "#c32()" "#c32()"
94 (with-output-to-string
95 (^[] (write (c32vector)))))
96 (test* "#c64()" "#c64()"
97 (with-output-to-string
98 (^[] (write (c64vector)))))
99 (test* "#c128()" "#c128()"
100 (with-output-to-string
101 (^[] (write (c128vector)))))
102
103 ;;-------------------------------------------------------------------
104 (test-section "constructors")
105
106 (expand-uvec
107 (u8 s8 u16 s16 u32 s32 u64 s64 f16 f32 f64 c32 c64 c128)
108 (define (uvmaketest-@ inits)
109 ;; zero length
110 (test* "make @ 0"
111 (make-uvector <@vector> 0)
112 (make-@vector 0))
113 ;; some content
114 (dolist [init inits]
115 (test* (format "make @ 10 ~a" init)
116 (make-uvector <@vector> 10 init)
117 (make-@vector 10 init)))))
118
119 (uvmaketest-s8 '(0 10 -4))
120 (uvmaketest-u8 '(0 4 255))
121 (uvmaketest-s16 '(0 -32768 32767))
122 (uvmaketest-u16 '(0 65535))
123 (uvmaketest-s32 '(0))
124 (uvmaketest-u32 '(0))
125 (uvmaketest-s64 '(0))
126 (uvmaketest-u64 '(0))
127 (uvmaketest-f16 '(0 1.0 -1.0))
128 (uvmaketest-f32 '(0 1.0 -1.0))
129 (uvmaketest-f64 '(0 1.0 -1.0))
130 (uvmaketest-c32 '(0 1.0+i -1.0-i))
131 (uvmaketest-c64 '(0 1.0+i -1.0-i))
132 (uvmaketest-c128 '(0 1.0+i -1.0-i))
133
134 ;; uvector size handling
135 ;; https://github.com/shirok/Gauche/issues/816
136 ;; This test doesn't run by default, for it allocates large mem.
137 '(let ((siz (ash 1 32)))
138 (when (fixnum? siz)
139 (test* "uvector-size over 32bit" siz
140 (uvector-size (make-u8vector siz)))))
141
142 ;;-------------------------------------------------------------------
143 (test-section "ref and set")
144
145 (expand-uvec
146 (u8 s8 u16 s16 u32 s32 u64 s64 f16 f32 f64 c32 c64 c128)
147 (define (uvrefset-test-@ numlist expvec)
148 (test* "@vector-ref|set!" #t
149 (let ([vec (make-@vector (length numlist))]
150 [seq (iota (length numlist))])
151 (for-each (^[n i] (@vector-set! vec i n)) numlist seq)
152 (and (equal? expvec vec)
153 (equal? numlist (map (^i (@vector-ref vec i)) seq))
154 (equal? numlist (map (^i (uvector-ref vec i)) seq))
155 (begin
156 (uvector-set! vec 0 (@vector-ref expvec 1))
157 (equal? (@vector-ref expvec 1) (@vector-ref vec 0)))
158 (begin
159 (set! (uvector-ref vec 1) (@vector-ref expvec 2))
160 (equal? (@vector-ref expvec 2) (@vector-ref vec 1))))))))
161
162 (uvrefset-test-s8 '(0 -1 1 -128 127)
163 '#s8(0 -1 1 -128 127))
164 (uvrefset-test-u8 '(0 1 2 3 255)
165 '#u8(0 1 2 3 255))
166 (uvrefset-test-s16 '(0 -1 1 -32768 32767)
167 '#s16(0 -1 1 -32768 32767))
168 (uvrefset-test-u16 '(0 1 2 3 65535)
169 '#u16(0 1 2 3 65535))
170 (uvrefset-test-s32 '(0 -1 1 #x-80000000 #x7fffffff)
171 '#s32(0 -1 1 #x-80000000 #x7fffffff))
172 (uvrefset-test-u32 '(0 1 2 #xffffffff)
173 '#u32(0 1 2 #xffffffff))
174 (uvrefset-test-s64 '(0 -1 1 #x-8000000000000000 #x7fffffffffffffff)
175 '#s64(0 -1 1 #x-8000000000000000 #x7fffffffffffffff))
176 (uvrefset-test-u64 '(0 1 2 #xffffffffffffffff)
177 '#u64(0 1 2 #xffffffffffffffff))
178 (uvrefset-test-f16 '(0.0 -1.0 1.0)
179 '#f16(0.0 -1.0 1.0))
180 (uvrefset-test-f32 '(0.0 -1.0 1.0)
181 '#f32(0.0 -1.0 1.0))
182 (uvrefset-test-f64 '(0.0 -1.0 1.0)
183 '#f64(0.0 -1.0 1.0))
184 (uvrefset-test-c32 '(0.0 -1-i 1+i)
185 '#c32(0.0 -1-i 1+i))
186 (uvrefset-test-c64 '(0.0 -1-i 1+i)
187 '#c64(0.0 -1-i 1+i))
188 (uvrefset-test-c128 '(0.0 -1-i 1+i)
189 '#c128(0.0 -1-i 1+i))
190
191 (expand-uvec
192 (u8 s8 u16 s16 u32 s32 u64 s64)
193 (define (uvset-clamp-test-@ expect value)
194 (test* "@vector-set! clamp" expect
195 (let1 v (make-@vector 1)
196 (list (with-error-handler (^e 'error)
197 (^[] (@vector-set! v 0 value) (@vector-ref v 0)))
198 (with-error-handler (^e 'error)
199 (^[] (@vector-set! v 0 value 'low) (@vector-ref v 0)))
200 (with-error-handler (^e 'error)
201 (^[] (@vector-set! v 0 value 'high) (@vector-ref v 0)))
202 (with-error-handler (^e 'error)
203 (^[] (@vector-set! v 0 value 'both) (@vector-ref v 0))))))))
204
205 (uvset-clamp-test-s8 '(error -128 error -128) -129)
206 (uvset-clamp-test-s8 '(error error 127 127) 128)
207 (uvset-clamp-test-u8 '(error 0 error 0) -1)
208 (uvset-clamp-test-u8 '(error error 255 255) 256)
209
210 (uvset-clamp-test-s16 '(error -32768 error -32768) -32769)
211 (uvset-clamp-test-s16 '(error error 32767 32767) 32768)
212 (uvset-clamp-test-u16 '(error 0 error 0) -1)
213 (uvset-clamp-test-u16 '(error error 65535 65535) 65536)
214
215 (uvset-clamp-test-s32 '(error -2147483648 error -2147483648) -2147483649)
216 (uvset-clamp-test-s32 '(error error 2147483647 2147483647) 2147483648)
217 (uvset-clamp-test-u32 '(error 0 error 0) -1)
218 (uvset-clamp-test-u32 '(error error 4294967295 4294967295) 4294967296)
219
220
221 (uvset-clamp-test-s64 '(error -9223372036854775808 error -9223372036854775808)
222 -9223372036854775809)
223 (uvset-clamp-test-s64 '(error error 9223372036854775807 9223372036854775807)
224 9223372036854775808)
225 (uvset-clamp-test-u64 '(error 0 error 0) -1)
226 (uvset-clamp-test-u64 '(error error 18446744073709551615 18446744073709551615)
227 18446744073709551616)
228
229 ;;-------------------------------------------------------------------
230 (test-section "conversions")
231
232 (expand-uvec
233 (u8 s8 u16 s16 u32 s32 u64 s64 f16 f32 f64 c32 c64 c128)
234 (define (uvconv-test-@ uvec nums)
235 (test* "@vector conversion" #t
236 (let* ([lis (@vector->list uvec)]
237 [uv2 (list->@vector lis)]
238 [vec (@vector->vector uvec)]
239 [uv3 (vector->@vector vec)])
240 (and (equal? lis nums)
241 (equal? (uvector->list uvec) nums)
242 (equal? uv2 uvec)
243 (equal? vec (list->vector nums))
244 (equal? (uvector->vector uvec) (list->vector nums))
245 (equal? uv3 uvec))))))
246
247 (uvconv-test-s8 '#s8(0 -1 1 -128 127) '(0 -1 1 -128 127))
248 (uvconv-test-u8 '#u8(0 1 254 255) '(0 1 254 255))
249 (uvconv-test-s16 '#s16(0 -1 1 -32768 32767) '(0 -1 1 -32768 32767))
250 (uvconv-test-u16 '#u16(0 1 65534 65535) '(0 1 65534 65535))
251 (uvconv-test-s32 '#s32(0 -1 1 #x-80000000 #x7fffffff)
252 '(0 -1 1 #x-80000000 #x7fffffff))
253 (uvconv-test-u32 '#u32(0 1 #xfffffffe #xffffffff)
254 '(0 1 #xfffffffe #xffffffff))
255 (uvconv-test-s64 '#s64(0 -1 1 #x-8000000000000000 #x7fffffffffffffff)
256 '(0 -1 1 #x-8000000000000000 #x7fffffffffffffff))
257 (uvconv-test-u64 '#u64(0 1 #xffffffffffffffff)
258 '(0 1 #xffffffffffffffff))
259 (uvconv-test-f16 '#f16(0.0 -1.0 1.0) '(0.0 -1.0 1.0))
260 (uvconv-test-f32 '#f32(0.0 -1.0 1.0) '(0.0 -1.0 1.0))
261 (uvconv-test-f64 '#f64(0.0 -1.0 1.0) '(0.0 -1.0 1.0))
262 (uvconv-test-c32 '#c32(0.0 -1.0+i 1.0-i) '(0.0 -1.0+i 1.0-i))
263 (uvconv-test-c64 '#c64(0.0 -1.0+i 1.0-i) '(0.0 -1.0+i 1.0-i))
264 (uvconv-test-c128 '#c128(0.0 -1.0+i 1.0-i) '(0.0 -1.0+i 1.0-i))
265
266 ;;-------------------------------------------------------------------
267 (test-section "comparison")
268
269 (define (uvcomp-tester list-> samples)
270 (dolist [x samples]
271 (dolist [y samples]
272 (test* #"equal? ~(list-> x) ~(list-> y)"
273 (and (= (length x) (length y))
274 (every = x y))
275 (equal? (list-> x) (list-> y))))))
276
277 (for-each (cut uvcomp-tester <> '(() (0) (1) (0 0) (0 1)))
278 (list list->s8vector list->u8vector
279 list->s16vector list->u16vector
280 list->s32vector list->u32vector
281 list->s64vector list->u64vector))
282
283 (for-each (cut uvcomp-tester <> '(() (0) (1) (0 0) (0 1)
284 (+inf.0) (-inf.0) (+nan.0)
285 (0 +inf.0) (0 -inf.0) (0 +nan.0)))
286 (list list->f16vector
287 list->f32vector
288 list->f64vector))
289
290 ;; SRFI-66 procedures
291 ;; Internally we just call builtin equal? and compare, so here we just
292 ;; check it can be callable.
293 (test* "u8vector=?" #t (u8vector=? '#u8(1 2 3) '#u8(1 2 3)))
294 (test* "u8vector=?" (test-error) (u8vector=? '#u8(1 2 3) '#s8(1 2 3)))
295 (test* "u8vector-compare" -1 (u8vector-compare '#u8(2 2) '#u8(1 2 3)))
296 (test* "u8vector-compare" (test-error) (u8vector-compare '#s8(2 2) '#u8(1 2 3)))
297
298 ;;-------------------------------------------------------------------
299 (test-section "copying and filling")
300
301 (expand-uvec
302 (u8 s8 u16 s16 u32 s32 u64 s64 f16 f32 f64 c32 c64 c128)
303 (define (uvcopy-test-@ uvec filler)
304 (test* "@vector-copy|fill!" #t
305 (let* ([c0 (list->@vector (@vector->list uvec))]
306 [c1 (@vector-copy uvec)]
307 [c2 (uvector-copy uvec)]
308 [cr (@vector-reverse-copy uvec)]
309 [len (@vector-length uvec)]
310 [cb (make-@vector (* len 2) filler)])
311 (and (equal? c1 uvec)
312 (equal? c2 uvec)
313 (equal? (@vector->list cr)
314 (reverse (@vector->list uvec)))
315 (begin (@vector-fill! c1 filler)
316 (and (equal? c0 uvec)
317 (every (^n (= n filler)) (@vector->list c1))
318 (begin (@vector-copy! c1 uvec)
319 (equal? c1 c0))))
320 (begin (@vector-reverse-copy! cb 1 cr 1)
321 (and (equal? (@vector-ref cb 0) filler)
322 (equal? (@vector-copy cb 1 len)
323 (@vector-copy uvec 0 (- len 1)))
324 (equal? (@vector-ref cb len) filler))))))))
325
326
327 (uvcopy-test-s8 '#s8(0 -1 1 -128 127) -128)
328 (uvcopy-test-u8 '#u8(0 1 255) 255)
329 (uvcopy-test-s16 '#s16(0 -1 1 -32768 32767) -32768)
330 (uvcopy-test-u16 '#u16(0 1 65535) 32768)
331 (uvcopy-test-s32 '#s32(0 -1 1 #x-80000000 #x7fffffff) #x7fffffff)
332 (uvcopy-test-u32 '#u32(0 1 #xffffffff) #x80000000)
333 (uvcopy-test-s64 '#s64(0 -1 1 #x-8000000000000000 #x7fffffffffffffff)
334 #x7fffffffffffffff)
335 (uvcopy-test-u64 '#u64(0 1 #xffffffffffffffff) #x8000000000000000)
336 (uvcopy-test-f16 '#f16(0 -1.0 1.0) 1.0)
337 (uvcopy-test-f32 '#f32(0 -1.0 1.0) 1.0)
338 (uvcopy-test-f64 '#f64(0 -1.0 1.0) 1.0)
339 (uvcopy-test-c32 '#c32(0+i -1.0-i 1.0) 1.0)
340 (uvcopy-test-c64 '#c64(0+i -1.0-i 1.0) 1.0)
341 (uvcopy-test-c128 '#c128(0+i -1.0-i 1.0) 1.0)
342
343 (expand-uvec
344 (u8 s8 u16 s16 u32 s32 u64 s64 f16 f32 f64 c32 c64 c128)
345 (test* "uvcopy-startend @vector"
346 (list (@vector 1 2 3) (@vector 1 2) (@vector 0 9 9 3))
347 (let* ([v (@vector 0 1 2 3)]
348 [v1 (@vector-copy v 1)]
349 [v2 (@vector-copy v 1 3)])
350 (@vector-fill! v 9 1 3)
351 (list v1 v2 v))))
352
353 (expand-uvec
354 (u8 s8 u16 s16 u32 s32 u64 s64 f16 f32 f64 c32 c64 c128)
355 (begin
356 (test* #"@vector-copy! newapi /tstart" (@vector 0 7 8 9)
357 (@vector-copy! (@vector 0 1 2 3) 1 (@vector 7 8 9 10)))
358 (test* #"@vector-copy! newapi /tstart(over)" (@vector 0 1 2 3)
359 (@vector-copy! (@vector 0 1 2 3) 4 (@vector 7 8 9 10)))
360 (test* #"@vector-copy! newapi /tstart,sstart" (@vector 0 9 10 3)
361 (@vector-copy! (@vector 0 1 2 3) 1 (@vector 7 8 9 10) 2))
362 (test* #"@vector-copy! newapi /tstart,sstart,send" (@vector 0 1 2 9)
363 (@vector-copy! (@vector 0 1 2 3) 3 (@vector 7 8 9 10) 2 3))))
364
365 (test* "uvector-copy! (generic)" '#u16(0 0 1 2 65535 0)
366 (rlet1 v (make-u16vector 6 0)
367 (uvector-copy! v 2 '#s16(1 2 -1))))
368 (test* "uvector-copy! (generic, start)" '#u16(0 0 1 2 65535 0)
369 (rlet1 v (make-u16vector 6 0)
370 (uvector-copy! v 2 '#s16(0 1 2 -1) 1)))
371 (test* "uvector-copy! (generic, start+end)" '#u16(0 0 1 2 65535 0)
372 (rlet1 v (make-u16vector 6 0)
373 (uvector-copy! v 2 '#s16(0 1 2 -1 -2) 1 4)))
374 (test* "uvector-copy! (generic, different size)" '#u16(0 0 257 0)
375 (rlet1 v (make-u16vector 4 0)
376 (uvector-copy! v 2 '#u8(1 1))))
377
378 (expand-uvec
379 (u8 s8 u16 s16 u32 s32 u64 s64 f16 f32 f64 c32 c64 c128)
380 (begin
381 (test* #"@vector-multi-copy! generic" (@vector 0 1 2 3 0 1 2 3 0 1 2)
382 (rlet1 dst (make-@vector 11 0)
383 (@vector-multi-copy! dst 1 4 (@vector 1 2 3))))
384 (test* #"@vector-multi-copy! ssize" (@vector 0 1 2 3 0 0 4 5 6 0 0 7)
385 (rlet1 dst (make-@vector 12 0)
386 (@vector-multi-copy! dst 1 5 (@vector 1 2 3 4 5 6 7 8 9) 0 3)))
387 (test* #"@vector-multi-copy! sstart, ssize" (@vector 0 5 6 7 0 0 8 9 0 0 0 0)
388 (rlet1 dst (make-@vector 12 0)
389 (@vector-multi-copy! dst 1 5 (@vector 1 2 3 4 5 6 7 8 9) 4 3)))
390 (test* #"@vector-multi-copy! ssize, sstride" (@vector 1 2 3 0 2 3 4 0 3 4 5 0)
391 (rlet1 dst (make-@vector 12 0)
392 (@vector-multi-copy! dst 0 4 (@vector 1 2 3 4 5 6 7 8 9) 0 3 1)))
393 (test* #"@vector-multi-copy! count" (@vector 1 2 3 0 2 3 4 0 0 0 0 0)
394 (rlet1 dst (make-@vector 12 0)
395 (@vector-multi-copy! dst 0 4 (@vector 1 2 3 4 5 6 7 8 9) 0 3 1 2)))
396 (test* #"@vector-multi-copy! single item" (@vector 1 0 1 0 1 0 1 0 1 0)
397 (rlet1 dst (make-@vector 10 0)
398 (@vector-multi-copy! dst 0 2 (@vector 1))))))
399
400 (expand-uvec
401 (u8 s8 u16 s16 u32 s32 u64 s64 f16 f32 f64 c32 c64 c128)
402 (begin
403 (test* #"@vector-append base" (@vector) (@vector-append))
404 (test* #"@vector-append unit" (@vector 1 2 3 )
405 (@vector-append (@vector 1 2 3)))
406 (test* #"@vector-append" (@vector 1 2 3 4 5 6 7 8)
407 (@vector-append (@vector 1 2 3)
408 (@vector) (@vector 4 5) (@vector 6 7 8)))))
409
410 ;;-------------------------------------------------------------------
411 (test-section "swapping bytes")
412
413 (test* "swapb s16"
414 '#s16(#x0123 #x4567 #x7654 #x3210)
415 (s16vector-swap-bytes '#s16(#x2301 #x6745 #x5476 #x1032)))
416 (test* "swapb u16"
417 '#u16(#x0123 #x4567 #x7654 #x3210)
418 (u16vector-swap-bytes '#u16(#x2301 #x6745 #x5476 #x1032)))
419 (test* "swapb s32"
420 '#s32(#x01234567 #x76543210)
421 (s32vector-swap-bytes '#s32(#x67452301 #x10325476)))
422 (test* "swapb u32"
423 '#u32(#x01234567 #x76543210)
424 (u32vector-swap-bytes '#u32(#x67452301 #x10325476)))
425 (test* "swapb s64"
426 '#s64(#x0123456789abcdef #x-123456789abcdf0)
427 (s64vector-swap-bytes
428 '#s64(#x-1032547698badcff #x1032547698badcfe)))
429 (test* "swapb u64"
430 '#u64(#x0123456789abcdef #xfedcba9876543210)
431 (u64vector-swap-bytes
432 '#u64(#xefcdab8967452301 #x1032547698badcfe)))
433
434 ;;-------------------------------------------------------------------
435 (test-section "collection interface")
436
437 (use gauche.collection)
438 (use gauche.sequence)
439
440 (define (num-equal? l1 l2)
441 (cond [(pair? l1)
442 (if (pair? l2)
443 (and (num-equal? (car l1) (car l2))
444 (num-equal? (cdr l1) (cdr l2)))
445 #f)]
446 [(vector? l1)
447 (if (vector? l2)
448 (with-iterator (l1 end1 next1)
449 (with-iterator (l2 end2 next2)
450 (let loop ()
451 (if (end1)
452 (if (end2) #t #f)
453 (and (num-equal? (next1) (next2))
454 (loop)))))))]
455 [(number? l1)
456 (if (number? l2)
457 (= l1 l2)
458 #f)]
459 [else (equal? l1 l2)]))
460
461 (expand-uvec
462 (u8 s8 u16 s16 u32 s32 u64 s64 f16 f32 f64 c32 c64 c128)
463 (test* "@vector collection interface" #t
464 (let1 vec (@vector 1 2 3 4)
465 (and (= (fold + 0 vec) 10)
466 (num-equal? (map identity vec) '(1 2 3 4))
467 (= (find (^e (= e 2)) vec) 2)
468 (equal? (coerce-to <@vector> '(1 2 3 4)) vec)
469 (= (ref vec 2) 3)
470 (num-equal? (begin (set! (ref vec 1) 0)
471 (coerce-to <list> vec))
472 '(1 0 3 4))
473 (num-equal? (coerce-to <vector> (subseq vec 1 3)) '#(0 3))))))
474
475 ;;-------------------------------------------------------------------
476 (test-section "arithmetic operations")
477
478 ;; there are too many combinations to write down by hand.
479 ;;
480 ;; for each operation in add, sub, mul
481 ;; for each testdata such that:
482 ;; - vector vs vector normal
483 ;; - vector vs vector underflow
484 ;; - vector vs vector overflow
485 ;; - vector vs smallint normal
486 ;; - vector vs smallint underflow
487 ;; - vector vs smallint overflow
488 ;; - vector vs bignum normal
489 ;; - vector vs bignum underflow
490 ;; - vector vs bignum overflow
491 ;;
492
493 (define *bounds*
494 `((s8 ,(- (expt 2 7)) ,(- (expt 2 7) 1))
495 (u8 0 ,(- (expt 2 8) 1))
496 (s16 ,(- (expt 2 15)) ,(- (expt 2 15) 1))
497 (u16 0 ,(- (expt 2 16) 1))
498 (s32 ,(- (expt 2 31)) ,(- (expt 2 31) 1))
499 (u32 0 ,(- (expt 2 32) 1))
500 (s64 ,(- (expt 2 63)) ,(- (expt 2 63) 1))
501 (u64 0 ,(- (expt 2 64) 1))
502 ))
503
504 (define (tag->min tag) (cadr (assq tag *bounds*)))
505 (define (tag->max tag) (caddr (assq tag *bounds*)))
506
507 (define (arith-test tag min max make add sub mul)
508 (define v0 (make 0 1 2 3))
509 (define v1 (make 4 5 6 7))
510 (define v2 (make (- max 2) (- max 2) (- max 2) (- max 2)))
511 (define v3 (make (+ min 2) (+ min 2) (+ min 2) (+ min 2)))
512 (define v0+v1 (make 4 6 8 10))
513 (define v1-v0 (make 4 4 4 4))
514 (define v0*v1 (make 0 5 12 21))
515 (define v0*2 (make 0 2 4 6))
516 (define v0+v2 (make (- max 2) (- max 1) max max))
517 (define v0*v2 (make 0 (- max 2) max max))
518 (define v0-min-1 (make min min (+ min 1) (+ min 2)))
519 (define v3-v0 (make (+ min 2) (+ min 1) min min))
520 (define v0*v3 (make 0 (+ min 2) min min))
521 (define vmin (make min min min min))
522 (define vmax (make max max max max))
523 (define big32 #xffffffff)
524 (define big64 #xffffffffffffffff)
525
526 (define (gen-tester op v0 v1)
527 (define (safe-test clamp-flag)
528 (with-error-handler (^e 'error)
529 (^[] (op v0 v1 clamp-flag))))
530 (^[] (list (safe-test #f)
531 (safe-test 'high)
532 (safe-test 'low)
533 (safe-test 'both))))
534
535 (define (result-normal v) (list v v v v))
536 (define (result-hi-ok v) (list 'error v 'error v))
537 (define (result-lo-ok v) (list 'error 'error v v))
538
539 ;; Add
540 (test (format #f "~avector-add (v+v)" tag) (result-normal v0+v1)
541 (gen-tester add v0 v1))
542 (test (format #f "~avector-add (v+v)" tag) (result-hi-ok v0+v2)
543 (gen-tester add v0 v2))
544 (test (format #f "~avector-add (v+l)" tag) (result-normal v0+v1)
545 (gen-tester add v0 (coerce-to <list> v1)))
546 (test (format #f "~avector-add (v+vv)" tag) (result-normal v0+v1)
547 (gen-tester add v0 (coerce-to <vector> v1)))
548 (test (format #f "~avector-add (v+s)" tag) (result-normal v1)
549 (gen-tester add v0 4))
550 (test (format #f "~avector-add (v+s)" tag) (result-hi-ok v0+v2)
551 (gen-tester add v0 (- max 2)))
552 (test (format #f "~avector-add (v+s)" tag) (result-lo-ok v0-min-1)
553 (gen-tester add v0 (- min 1)))
554 (test (format #f "~avector-add (v+b)" tag)
555 (case tag
556 [(s64 u64)
557 (result-normal (make big32 (+ big32 1) (+ big32 2) (+ big32 3)))]
558 [else (result-hi-ok vmax)])
559 (gen-tester add v0 big32))
560 (test (format #f "~avector-add (v+b)" tag)
561 (case tag
562 [(s64)
563 (result-normal (make (- big32) (- 1 big32) (- 2 big32) (- 3 big32)))]
564 [else (result-lo-ok vmin)])
565 (gen-tester add v0 (- big32)))
566 (test (format #f "~avector-add (v+b)" tag) (result-hi-ok vmax)
567 (gen-tester add v0 big64))
568 (test (format #f "~avector-add (v+b)" tag) (result-lo-ok vmin)
569 (gen-tester add v0 (- big64)))
570
571 ;; Sub
572 (test (format #f "~avector-sub (v-v)" tag) (result-normal v1-v0)
573 (gen-tester sub v1 v0))
574 (test (format #f "~avector-sub (v-v)" tag) (result-lo-ok v3-v0)
575 (gen-tester sub v3 v0))
576 (test (format #f "~avector-sub (v-l)" tag) (result-normal v1-v0)
577 (gen-tester sub v1 (coerce-to <list> v0)))
578 (test (format #f "~avector-sub (v-vv)" tag) (result-normal v1-v0)
579 (gen-tester sub v1 (coerce-to <vector> v0)))
580 (test (format #f "~avector-sub (v-s)" tag) (result-normal v0)
581 (gen-tester sub v1 4))
582 (test (format #f "~avector-sub (v-s)" tag) (result-lo-ok v0-min-1)
583 (gen-tester sub v0 (- (- min 1))))
584 (test (format #f "~avector-sub (v-b)" tag)
585 (case tag
586 [(s64) (result-normal (make (- big32) (- 1 big32) (- 2 big32) (- 3 big32)))]
587 [else (result-lo-ok vmin)])
588 (gen-tester sub v0 big32))
589 (test (format #f "~avector-sub (v-b)" tag)
590 (case tag
591 [(s64 u64)
592 (result-normal (make big32 (+ big32 1) (+ big32 2) (+ big32 3)))]
593 [else (result-hi-ok vmax)])
594 (gen-tester sub v0 (- big32)))
595 (test (format #f "~avector-sub (v-b)" tag) (result-lo-ok vmin)
596 (gen-tester sub v0 big64))
597 (test (format #f "~avector-sub (v-b)" tag) (result-hi-ok vmax)
598 (gen-tester sub v0 (- big64)))
599
600 ;; Mul
601 (test (format #f "~avector-mul (v*v)" tag) (result-normal v0*v1)
602 (gen-tester mul v0 v1))
603 (test (format #f "~avector-mul (v*v)" tag) (result-hi-ok v0*v2)
604 (gen-tester mul v0 v2))
605 (test (format #f "~avector-mul (v*l)" tag) (result-normal v0*v1)
606 (gen-tester mul v0 (coerce-to <list> v1)))
607 (test (format #f "~avector-mul (v*vv)" tag) (result-normal v0*v1)
608 (gen-tester mul v0 (coerce-to <vector> v1)))
609 (unless (memq tag '(u8 u16 u32 u64))
610 (test (format #f "~avector-mul (v*v)" tag) (result-lo-ok v0*v3)
611 (gen-tester mul v0 v3)))
612 (test (format #f "~avector-mul (v*s)" tag) (result-normal v0*2)
613 (gen-tester mul v0 2))
614 (test (format #f "~avector-mul (v*s)" tag)
615 (if (memq tag '(s8 s16 s32 s64))
616 (result-normal (make 0 -2 -4 -6))
617 (result-lo-ok vmin))
618 (gen-tester mul v0 -2))
619 (test (format #f "~avector-mul (v*s)" tag)
620 (result-hi-ok (make 0 (- max 1) max max))
621 (gen-tester mul v0 (- max 1)))
622 (test (format #f "~avector-mul (v*s)" tag)
623 (case tag
624 [(s8 s16 s32 s64) (result-lo-ok (make 0 (- (- max 1)) min min))]
625 [else (result-lo-ok vmin)])
626 (gen-tester mul v0 (- (- max 1))))
627 (test (format #f "~avector-mul (v*b)" tag)
628 (case tag
629 [(s64 u64) (result-normal (make 0 big32 (* big32 2) (* big32 3)))]
630 [else (result-hi-ok (make 0 max max max))])
631 (gen-tester mul v0 big32))
632 (test (format #f "~avector-mul (v*b)" tag)
633 (case tag
634 [(s64)
635 (result-normal (make 0 (- big32) (- (* big32 2)) (- (* big32 3))))]
636 [else (result-lo-ok (make 0 min min min))])
637 (gen-tester mul v0 (- big32)))
638 )
639
640 (expand-uvec
641 (u8 s8 u16 s16 u32 s32 u64 s64)
642 (arith-test '@ (tag->min '@) (tag->max '@)
643 @vector @vector-add @vector-sub @vector-mul))
644
645 ;; flonum vectors; no clamping, so it's a bit simple
646 (expand-uvec
647 (f16 f32 f64 c32 c64 c128)
648 (begin
649 (test* "@vector-add (v+v)"
650 (@vector 4.0 6.0 8.0 10.0)
651 (@vector-add (@vector 0.0 1.0 2.0 3.0) (@vector 4.0 5.0 6.0 7.0)))
652 (test* "@vector-add (v+s)"
653 (@vector 4.0 5.0 6.0 7.0)
654 (@vector-add (@vector 0.0 1.0 2.0 3.0) 4.0))
655 (test* "@vector-sub (v-v)"
656 (@vector -4.0 -4.0 -4.0 -4.0)
657 (@vector-sub (@vector 0.0 1.0 2.0 3.0) (@vector 4.0 5.0 6.0 7.0)))
658 (test* "@vector-sub (v-s)"
659 (@vector -4.0 -3.0 -2.0 -1.0)
660 (@vector-sub (@vector 0.0 1.0 2.0 3.0) 4.0))
661 (test* "@vector-mul (v*v)"
662 (@vector 0.0 5.0 12.0 21.0)
663 (@vector-mul (@vector 0.0 1.0 2.0 3.0) (@vector 4.0 5.0 6.0 7.0)))
664 (test* "@vector-mul (v*s)"
665 (@vector 0.0 5.0 10.0 15.0)
666 (@vector-mul (@vector 0.0 1.0 2.0 3.0) 5.0))
667 (test* "@vector-div (v/v)"
668 (@vector 0.0 0.5 0.5 0.375)
669 (@vector-div (@vector 0.0 1.0 2.0 3.0) (@vector 1.0 2.0 4.0 8.0)))
670 (test* "@vector-div (v/v)"
671 (@vector 0.0 0.5 1.0 1.5)
672 (@vector-div (@vector 0.0 1.0 2.0 3.0) 2.0))
673 ))
674
675 ;;-------------------------------------------------------------------
676 (test-section "bitwise operations")
677
678 (define (bit-test tag v0 v1 s0 s1 ->list list-> and ior xor)
679 (define (tests opname op logop)
680 (test* (format #f "~svector-~s ~s ~s" tag opname v0 v1)
681 (list-> (map logop (->list v0) (->list v1)))
682 (op v0 v1))
683 (test* (format #f "~svector-~s ~s ~s" tag opname v0 s0)
684 (list-> (map (pa$ logop s0) (->list v0)))
685 (op v0 s0))
686 (test* (format #f "~svector-~s ~s ~s" tag opname v0 s1)
687 (list-> (map (pa$ logop s1) (->list v0)))
688 (op v0 s1)))
689 (tests 'and and logand)
690 (tests 'ior ior logior)
691 (tests 'xor xor logxor))
692
693 (expand-uvec
694 (u8 s8 u16 s16 u32 s32 u64 s64)
695 (define (bit-test-@ v0 v1 s0 s1)
696 (bit-test '@ v0 v1 s0 s1
697 @vector->list
698 list->@vector
699 @vector-and
700 @vector-ior
701 @vector-xor)))
702
703 (bit-test-s8 #s8(#x0f #x70 #x-0f #x-70)
704 #s8(#x55 #x2a #x-55 #x-2a)
705 #x55
706 #x-55)
707 (bit-test-u8 #u8(#x0f #x70 #xf0 #xcc)
708 #u8(#x55 #xaa #x5a #xa5)
709 #x55
710 #xaa)
711 (bit-test-s16 #s16(#x0fff #x7070 #x-0fff #x-7070)
712 #s16(#x3c3c #x-43c3 #x43c3 #x-3c3c)
713 #x55aa
714 #x-55aa)
715 (bit-test-u16 #u16(#x0fff #x7070 #xff00 #xc0c0)
716 #u16(#x3c3c #xc3c3 #x55aa #xaa55)
717 #x55aa
718 #x9696)
719 (bit-test-s32 #s32(#x0fffffff #x70707070 #x-0fffffff #x-70707070)
720 #s32(#x3c3c3c3c #x-43c3c3c3 #x43c3c3c3 #x-3c3c3c3c)
721 #x55aa55aa
722 #x-55aa55aa)
723 (bit-test-u32 #u32(#x0fffffff #x70707070 #xff00ff00 #xc0c0c0c0)
724 #u32(#x3c3c3c3c #xc3c3c3c3 #x55aa55aa #xaa55aa55)
725 #x55aa55aa
726 #x96966969)
727 (bit-test-s64 #s64(#x0fffffffffffffff #x7070707007070707
728 #x-0fffffffffffffff #x-7070707007070707)
729 #s64(#x3c3c3c3cc3c3c3c3 #x-43c3c3c33c3c3c3c
730 #x43c3c3c3c3c3c3c3 #x-3c3c3c3c3c3c3c3c)
731 #x55aa55aa55aa55aa
732 #x-55aa55aa55aa55aa)
733 (bit-test-u64 #u64(#x0fffffffffffffff #x70707070f0f0f0f0
734 #xff00ff00ff00ff00 #xc0c0c0c003030303)
735 #u64(#x3c3c3c3c3c3c3c3c #xc3c3c3c3c3c3c3c3
736 #x55aa55aa55aa55aa #xaa55aa55aa55aa55)
737 #x55aa55aa5a5a5a5a
738 #x9696696988778877)
739
740 ;;-------------------------------------------------------------------
741 (test-section "dot product")
742
743 (define (dotprod-test tag v0 v1 dot)
744 (let1 result (fold (^[e0 e1 sum] (+ sum (* e0 e1)))
745 0
746 (coerce-to <list> v0)
747 (coerce-to <list> v1))
748 (test* (format #f "~svector-dot(~s, ~s)" tag v0 v1)
749 result
750 (dot v0 v1))
751 (test* (format #f "~svector-dot(~s, ~s)" tag v0 (coerce-to <list> v1))
752 result
753 (dot v0 (coerce-to <list> v1)))
754 (test* (format #f "~svector-dot(~s, ~s)" tag v0 (coerce-to <vector> v1))
755 result
756 (dot v0 (coerce-to <vector> v1)))
757 ))
758
759 (expand-uvec
760 (u8 s8 u16 s16 u32 s32 u64 s64 f16 f32 f64)
761 (define (dotprod-test-@ v0 v1)
762 (dotprod-test '@ v0 v1 @vector-dot)))
763
764 (dotprod-test-s8 #s8() #s8())
765 (dotprod-test-s8 #s8(0 1 2 3) #s8(4 5 6 7))
766 (dotprod-test-s8 #s8(0 -1 2 -3) #s8(-4 5 -6 7))
767 (dotprod-test-s8 #s8(127 127 127 127 127) #s8(127 127 127 127 127))
768 (dotprod-test-s8
769 #s8(-128 -128 -128 -128 -128)
770 #s8(127 127 127 127 127))
771 (dotprod-test-u8 #u8(0 1 2 3) #u8(4 5 6 7))
772 (dotprod-test-u8 #u8(255 255 255 255 255) #u8(255 255 255 255 255))
773
774 (dotprod-test-s16 #s16(0 1 2 3) #s16(4 5 6 7))
775 (dotprod-test-s16 #s16(0 -1 2 -3) #s16(-4 5 -6 7))
776 (dotprod-test-s16 #s16(16384 16384 16384 16384 16384)
777 #s16(16384 16384 16384 16384 16384))
778 (dotprod-test-s16 #s16(16384 -16384 16384 -16384 16384)
779 #s16(16384 -16384 16384 -16384 16384))
780 (dotprod-test-s16 #s16(32767 32767 32767 32767 32767)
781 #s16(32767 32767 32767 32767 32767))
782 (dotprod-test-s16 #s16(32767 1 2 3 4)
783 #s16(32767 1 2 3 4))
784 (dotprod-test-s16 #s16(1 2 3 4 32767)
785 #s16(1 2 3 4 32767))
786 (dotprod-test-s16 #s16(32767 -32767 32767 -32767 32767)
787 #s16(32767 32767 32767 32767 32767))
788 (dotprod-test-s16 #s16(-32768 -32768 -32768 -32768 -32768)
789 #s16(32767 32767 32767 32767 32767))
790 (dotprod-test-u16 #u16(0 1 2 3) #u16(4 5 6 7))
791 (dotprod-test-u16 #u16(16384 16384 16384 16384 16384)
792 #u16(16384 16384 16384 16384 16384))
793 (dotprod-test-u16 #u16(65535 65535 65535 65535 65535)
794 #u16(65535 65535 65535 65535 65535))
795 (dotprod-test-u16 #u16(32767 1 2 3 4)
796 #u16(32767 1 2 3 4))
797 (dotprod-test-u16 #u16(1 2 3 4 32767)
798 #u16(1 2 3 4 32767))
799
800 (dotprod-test-s32 #s32(0 1 2 3) #s32(4 5 6 7))
801 (dotprod-test-s32 #s32(0 -1 2 -3) #s32(-4 5 -6 7))
802 (dotprod-test-s32 #s32(16384 16384 16384 16384 16384)
803 #s32(16384 16384 16384 16384 16384))
804 (dotprod-test-s32 #s32(16384 -16384 16384 -16384 16384)
805 #s32(16384 -16384 16384 -16384 16384))
806 (dotprod-test-s32 #s32(32767 32767 32767 32767 32767)
807 #s32(32767 32767 32767 32767 32767))
808 (dotprod-test-s32 #s32(214748367 214748367 214748367 214748367 214748367)
809 #s32(214748367 214748367 214748367 214748367 214748367))
810 (dotprod-test-s32 #s32(214748367 1 2 3 4)
811 #s32(214748367 1 2 3 4))
812 (dotprod-test-s32 #s32(1 2 3 4 214748367)
813 #s32(1 2 3 4 214748367))
814 (dotprod-test-s32 #s32(32767 -32767 32767 -32767 32767)
815 #s32(32767 32767 32767 32767 32767))
816 (dotprod-test-s32 #s32(214748367 -214748367 214748367 -214748367 214748367)
817 #s32(214748367 214748367 214748367 214748367 214748367))
818 (dotprod-test-s32 #s32(-214748368 -214748368 -214748368 -214748368 -214748368)
819 #s32(214748367 214748367 214748367 214748367 214748367))
820 (dotprod-test-u32 #u32(0 1 2 3) #u32(4 5 6 7))
821 (dotprod-test-u32 #u32(16384 16384 16384 16384 16384)
822 #u32(16384 16384 16384 16384 16384))
823 (dotprod-test-u32 #u32(4294967295 4294967295 4294967295 4294967295 4294967295)
824 #u32(4294967295 4294967295 4294967295 4294967295 4294967295))
825 (dotprod-test-u32 #u32(4294967295 1 2 3 4)
826 #u32(4294967295 1 2 3 4))
827 (dotprod-test-u32 #u32(1 2 3 4 4294967295)
828 #u32(1 2 3 4 4294967295))
829
830 (dotprod-test-s64 #s64(0 1 2 3) #s64(4 5 6 7))
831 (dotprod-test-s64 #s64(0 -1 2 -3) #s64(-4 5 -6 7))
832 (dotprod-test-s64 #s64(16384 16384 16384 16384 16384)
833 #s64(16384 16384 16384 16384 16384))
834 (dotprod-test-s64 #s64(16384 -16384 16384 -16384 16384)
835 #s64(16384 -16384 16384 -16384 16384))
836 (dotprod-test-s64 #s64(32767 32767 32767 32767 32767)
837 #s64(32767 32767 32767 32767 32767))
838 (dotprod-test-s64 #s64(214748367 214748367 214748367 214748367 214748367)
839 #s64(214748367 214748367 214748367 214748367 214748367))
840 (dotprod-test-s64 #s64(9223372036854775807 1 2 3 4)
841 #s64(9223372036854775807 1 2 3 4))
842 (dotprod-test-s64 #s64(1 2 3 4 9223372036854775807 1)
843 #s64(1 2 3 4 9223372036854775807 1))
844 (dotprod-test-s64 #s64(32767 -32767 32767 -32767 32767)
845 #s64(32767 -32767 32767 -32767 32767))
846 (dotprod-test-s64 #s64(214748367 -214748367 214748367 -214748367 214748367)
847 #s64(214748367 214748367 214748367 214748367 214748367))
848 (dotprod-test-s64 #s64(-214748368 -214748368 -214748368 -214748368 -214748368)
849 #s64(214748367 214748367 214748367 214748367 214748367))
850 (dotprod-test-s64 #s64(9223372036854775807 -9223372036854775807 9223372036854775807 -9223372036854775807 9223372036854775807)
851 #s64(9223372036854775807 9223372036854775807 9223372036854775807 9223372036854775807 9223372036854775807))
852 (dotprod-test-s64 #s64(-9223372036854775808 -9223372036854775808 -9223372036854775808 -9223372036854775808 -9223372036854775808)
853 #s64(9223372036854775807 9223372036854775807 9223372036854775807 9223372036854775807 9223372036854775807))
854 (dotprod-test-u64 #u64(0 1 2 3) #u64(4 5 6 7))
855 (dotprod-test-u64 #u64(16384 16384 16384 16384 16384)
856 #u64(16384 16384 16384 16384 16384))
857 (dotprod-test-u64 #u64(18446744073709551615 18446744073709551615 18446744073709551615 18446744073709551615 18446744073709551615)
858 #u64(18446744073709551615 18446744073709551615 18446744073709551615 18446744073709551615 18446744073709551615))
859 (dotprod-test-u64 #u64(18446744073709551615 1 2 3 4)
860 #u64(18446744073709551615 1 2 3 4))
861 (dotprod-test-u64 #u64(1 2 3 4 18446744073709551615)
862 #u64(1 2 3 4 18446744073709551615))
863
864 (dotprod-test-f16 #f16(0 1 2 3) #f16(4 5 6 7))
865 (dotprod-test-f16 #f16(0 -1 2 -3) #f16(-4 5 -6 7))
866 (dotprod-test-f16 #f16(16384 16384 16384 16384 16384)
867 #f16(16384 16384 16384 16384 16384))
868 (dotprod-test-f16 #f16(16384 -16384 16384 -16384 16384)
869 #f16(16384 -16384 16384 -16384 16384))
870 (dotprod-test-f16 #f16(32767 32767 32767 32767 32767)
871 #f16(32767 32767 32767 32767 32767))
872 (dotprod-test-f16 #f16(214748367 214748367 214748367 214748367 214748367)
873 #f16(214748367 214748367 214748367 214748367 214748367))
874 (dotprod-test-f16 #f16(9223372036854775807 1 2 3 4)
875 #f16(9223372036854775807 1 2 3 4))
876 (dotprod-test-f16 #f16(1 2 3 4 9223372036854775807 1)
877 #f16(1 2 3 4 9223372036854775807 1))
878 (dotprod-test-f16 #f16(32767 -32767 32767 -32767 32767)
879 #f16(32767 -32767 32767 -32767 32767))
880
881 (dotprod-test-f32 #f32(0 1 2 3) #f32(4 5 6 7))
882 (dotprod-test-f32 #f32(0 -1 2 -3) #f32(-4 5 -6 7))
883 (dotprod-test-f32 #f32(16384 16384 16384 16384 16384)
884 #f32(16384 16384 16384 16384 16384))
885 (dotprod-test-f32 #f32(16384 -16384 16384 -16384 16384)
886 #f32(16384 -16384 16384 -16384 16384))
887 (dotprod-test-f32 #f32(32767 32767 32767 32767 32767)
888 #f32(32767 32767 32767 32767 32767))
889 (dotprod-test-f32 #f32(214748367 214748367 214748367 214748367 214748367)
890 #f32(214748367 214748367 214748367 214748367 214748367))
891 (dotprod-test-f32 #f32(9223372036854775807 1 2 3 4)
892 #f32(9223372036854775807 1 2 3 4))
893 (dotprod-test-f32 #f32(1 2 3 4 9223372036854775807 1)
894 #f32(1 2 3 4 9223372036854775807 1))
895 (dotprod-test-f32 #f32(32767 -32767 32767 -32767 32767)
896 #f32(32767 -32767 32767 -32767 32767))
897
898 (dotprod-test-f64 #f64(0 1 2 3) #f64(4 5 6 7))
899 (dotprod-test-f64 #f64(0 -1 2 -3) #f64(-4 5 -6 7))
900 (dotprod-test-f64 #f64(16384 16384 16384 16384 16384)
901 #f64(16384 16384 16384 16384 16384))
902 (dotprod-test-f64 #f64(16384 -16384 16384 -16384 16384)
903 #f64(16384 -16384 16384 -16384 16384))
904 (dotprod-test-f64 #f64(32767 32767 32767 32767 32767)
905 #f64(32767 32767 32767 32767 32767))
906 (dotprod-test-f64 #f64(214748367 214748367 214748367 214748367 214748367)
907 #f64(214748367 214748367 214748367 214748367 214748367))
908 (dotprod-test-f64 #f64(9223372036854775807 1 2 3 4)
909 #f64(9223372036854775807 1 2 3 4))
910 (dotprod-test-f64 #f64(1 2 3 4 9223372036854775807 1)
911 #f64(1 2 3 4 9223372036854775807 1))
912 (dotprod-test-f64 #f64(32767 -32767 32767 -32767 32767)
913 #f64(32767 -32767 32767 -32767 32767))
914
915 ;;-------------------------------------------------------------------
916 (test-section "range-check")
917
918 (expand-uvec
919 (u8 s8 u16 s16 u32 s32 u64 s64 f16 f32 f64)
920 (define (range-test-@ v min max result)
921 (test* "@vector-range-check" result (@vector-range-check v min max))))
922
923 (range-test-s8 #s8(-4 -2 0 2 4) #f #f #f)
924 (range-test-s8 #s8(-4 -2 0 2 4) -4 4 #f)
925 (range-test-s8 #s8(-4 -2 0 2 4) -4 3 4)
926 (range-test-s8 #s8(-4 -2 0 2 4) -4 0 3)
927 (range-test-s8 #s8(-4 -2 0 2 4) -3 4 0)
928 (range-test-s8 #s8(-4 -2 0 2 4) 0 4 0)
929 (range-test-s8 #s8(-4 -2 0 2 4) #x-ffffffffffffffff #f #f)
930 (range-test-s8 #s8(-4 -2 0 2 4) #f #xffffffffffffffff #f)
931 (range-test-s8 #s8(-4 -2 0 2 4) #xffffffffffffffff #f 0)
932 (range-test-s8 #s8(-4 -2 0 2 4) #f #x-ffffffffffffffff 0)
933 (range-test-s8 #s8(-4 -2 0 2 4) #s8(-4 -3 -2 -1 0) #s8(0 1 2 3 4) #f)
934 (range-test-s8 #s8(-4 -2 0 2 4) #s8(-4 -1 -2 -1 0) #s8(0 1 2 3 4) 1)
935 (range-test-s8 #s8(-4 -2 0 2 4) #s8(-4 -3 -2 -1 0) #s8(0 1 -1 3 4) 2)
936
937 (range-test-u8 #u8(0 2 4 6 9) #f #f #f)
938 (range-test-u8 #u8(0 2 4 6 9) -4 9 #f)
939 (range-test-u8 #u8(0 2 4 6 9) -4 8 4)
940 (range-test-u8 #u8(0 2 4 6 9) -4 4 3)
941 (range-test-u8 #u8(0 2 4 6 9) 3 4 0)
942 (range-test-u8 #u8(0 2 4 6 9) #x-ffffffffffffffff #f #f)
943 (range-test-u8 #u8(0 2 4 6 9) #f #xffffffffffffffff #f)
944 (range-test-u8 #u8(0 2 4 6 9) #xffffffffffffffff #f 0)
945 (range-test-u8 #u8(0 2 4 6 9) #f #x-ffffffffffffffff 1)
946 (range-test-u8 #u8(0 2 4 6 9) #u8(0 1 2 3 4) #u8(5 6 7 8 9) #f)
947 (range-test-u8 #u8(0 2 4 6 9) #u8(0 1 5 3 4) #u8(5 6 7 8 9) 2)
948 (range-test-u8 #u8(0 2 4 6 9) #u8(0 1 2 3 4) #u8(5 6 7 5 6) 3)
949
950 (range-test-s16 #s16(-4 -2 0 2 4) #f #f #f)
951 (range-test-s16 #s16(-4 -2 0 2 4) -4 4 #f)
952 (range-test-s16 #s16(-4 -2 0 2 4) -4 3 4)
953 (range-test-s16 #s16(-4 -2 0 2 4) -4 0 3)
954 (range-test-s16 #s16(-4 -2 0 2 4) -3 4 0)
955 (range-test-s16 #s16(-4 -2 0 2 4) 0 4 0)
956 (range-test-s16 #s16(-4 -2 0 2 4) #x-ffffffffffffffff #f #f)
957 (range-test-s16 #s16(-4 -2 0 2 4) #f #xffffffffffffffff #f)
958 (range-test-s16 #s16(-4 -2 0 2 4) #xffffffffffffffff #f 0)
959 (range-test-s16 #s16(-4 -2 0 2 4) #f #x-ffffffffffffffff 0)
960 (range-test-s16 #s16(-4 -2 0 2 4) #s16(-4 -3 -2 -1 0) #s16(0 1 2 3 4) #f)
961 (range-test-s16 #s16(-4 -2 0 2 4) #s16(-4 -1 -2 -1 0) #s16(0 1 2 3 4) 1)
962 (range-test-s16 #s16(-4 -2 0 2 4) #s16(-4 -3 -2 -1 0) #s16(0 1 -1 3 4) 2)
963
964 (range-test-u16 #u16(0 2 4 6 9) #f #f #f)
965 (range-test-u16 #u16(0 2 4 6 9) -4 9 #f)
966 (range-test-u16 #u16(0 2 4 6 9) -4 8 4)
967 (range-test-u16 #u16(0 2 4 6 9) -4 4 3)
968 (range-test-u16 #u16(0 2 4 6 9) 3 4 0)
969 (range-test-u16 #u16(0 2 4 6 9) #x-ffffffffffffffff #f #f)
970 (range-test-u16 #u16(0 2 4 6 9) #f #xffffffffffffffff #f)
971 (range-test-u16 #u16(0 2 4 6 9) #xffffffffffffffff #f 0)
972 (range-test-u16 #u16(0 2 4 6 9) #f #x-ffffffffffffffff 1)
973 (range-test-u16 #u16(0 2 4 6 9) #u16(0 1 2 3 4) #u16(5 6 7 8 9) #f)
974 (range-test-u16 #u16(0 2 4 6 9) #u16(0 1 5 3 4) #u16(5 6 7 8 9) 2)
975 (range-test-u16 #u16(0 2 4 6 9) #u16(0 1 2 3 4) #u16(5 6 7 5 6) 3)
976
977 (range-test-s32 #s32(-4 -2 0 2 4) #f #f #f)
978 (range-test-s32 #s32(-4 -2 0 2 4) -4 4 #f)
979 (range-test-s32 #s32(-4 -2 0 2 4) -4 3 4)
980 (range-test-s32 #s32(-4 -2 0 2 4) -4 0 3)
981 (range-test-s32 #s32(-4 -2 0 2 4) -3 4 0)
982 (range-test-s32 #s32(-4 -2 0 2 4) 0 4 0)
983 (range-test-s32 #s32(-4 -2 0 2 4) #x-ffffffffffffffff #f #f)
984 (range-test-s32 #s32(-4 -2 0 2 4) #f #xffffffffffffffff #f)
985 (range-test-s32 #s32(-4 -2 0 2 4) #xffffffffffffffff #f 0)
986 (range-test-s32 #s32(-4 -2 0 2 4) #f #x-ffffffffffffffff 0)
987 (range-test-s32 #s32(-4 -2 0 2 4) #s32(-4 -3 -2 -1 0) #s32(0 1 2 3 4) #f)
988 (range-test-s32 #s32(-4 -2 0 2 4) #s32(-4 -1 -2 -1 0) #s32(0 1 2 3 4) 1)
989 (range-test-s32 #s32(-4 -2 0 2 4) #s32(-4 -3 -2 -1 0) #s32(0 1 -1 3 4) 2)
990
991 (range-test-u32 #u32(0 2 4 6 9) #f #f #f)
992 (range-test-u32 #u32(0 2 4 6 9) -4 9 #f)
993 (range-test-u32 #u32(0 2 4 6 9) -4 8 4)
994 (range-test-u32 #u32(0 2 4 6 9) -4 4 3)
995 (range-test-u32 #u32(0 2 4 6 9) 3 4 0)
996 (range-test-u32 #u32(0 2 4 6 9) #x-ffffffffffffffff #f #f)
997 (range-test-u32 #u32(0 2 4 6 9) #f #xffffffffffffffff #f)
998 (range-test-u32 #u32(0 2 4 6 9) #xffffffffffffffff #f 0)
999 (range-test-u32 #u32(0 2 4 6 9) #f #x-ffffffffffffffff 1)
1000 (range-test-u32 #u32(0 2 4 6 9) #u32(0 1 2 3 4) #u32(5 6 7 8 9) #f)
1001 (range-test-u32 #u32(0 2 4 6 9) #u32(0 1 5 3 4) #u32(5 6 7 8 9) 2)
1002 (range-test-u32 #u32(0 2 4 6 9) #u32(0 1 2 3 4) #u32(5 6 7 5 6) 3)
1003
1004 (range-test-s64 #s64(-4 -2 0 2 4) #f #f #f)
1005 (range-test-s64 #s64(-4 -2 0 2 4) -4 4 #f)
1006 (range-test-s64 #s64(-4 -2 0 2 4) -4 3 4)
1007 (range-test-s64 #s64(-4 -2 0 2 4) -4 0 3)
1008 (range-test-s64 #s64(-4 -2 0 2 4) -3 4 0)
1009 (range-test-s64 #s64(-4 -2 0 2 4) 0 4 0)
1010 (range-test-s64 #s64(-4 -2 0 2 4) #x-ffffffffffffffff #f #f)
1011 (range-test-s64 #s64(-4 -2 0 2 4) #f #xffffffffffffffff #f)
1012 (range-test-s64 #s64(-4 -2 0 2 4) #xffffffffffffffff #f 0)
1013 (range-test-s64 #s64(-4 -2 0 2 4) #f #x-ffffffffffffffff 0)
1014 (range-test-s64 #s64(-4 -2 0 2 4) #s64(-4 -3 -2 -1 0) #s64(0 1 2 3 4) #f)
1015 (range-test-s64 #s64(-4 -2 0 2 4) #s64(-4 -1 -2 -1 0) #s64(0 1 2 3 4) 1)
1016 (range-test-s64 #s64(-4 -2 0 2 4) #s64(-4 -3 -2 -1 0) #s64(0 1 -1 3 4) 2)
1017
1018 (range-test-u64 #u64(0 2 4 6 9) #f #f #f)
1019 (range-test-u64 #u64(0 2 4 6 9) -4 9 #f)
1020 (range-test-u64 #u64(0 2 4 6 9) -4 8 4)
1021 (range-test-u64 #u64(0 2 4 6 9) -4 4 3)
1022 (range-test-u64 #u64(0 2 4 6 9) 3 4 0)
1023 (range-test-u64 #u64(0 2 4 6 9) #x-ffffffffffffffff #f #f)
1024 (range-test-u64 #u64(0 2 4 6 9) #f #xffffffffffffffff #f)
1025 (range-test-u64 #u64(0 2 4 6 9) #xffffffffffffffff #f 0)
1026 (range-test-u64 #u64(0 2 4 6 9) #f #x-ffffffffffffffff 1)
1027 (range-test-u64 #u64(0 2 4 6 9) #u64(0 1 2 3 4) #u64(5 6 7 8 9) #f)
1028 (range-test-u64 #u64(0 2 4 6 9) #u64(0 1 5 3 4) #u64(5 6 7 8 9) 2)
1029 (range-test-u64 #u64(0 2 4 6 9) #u64(0 1 2 3 4) #u64(5 6 7 5 6) 3)
1030
1031 (range-test-f32 #f32(-4.0 -2.0 0.0 2.0 4.0) #f #f #f)
1032 (range-test-f32 #f32(-4.0 -2.0 0.0 2.0 4.0) -4.0 4.0 #f)
1033 (range-test-f32 #f32(-4.0 -2.0 0.0 2.0 4.0) -4.0 3.0 4)
1034 (range-test-f32 #f32(-4.0 -2.0 0.0 2.0 4.0) -4.0 0.0 3)
1035 (range-test-f32 #f32(-4.0 -2.0 0.0 2.0 4.0) -3.0 4.0 0)
1036 (range-test-f32 #f32(-4.0 -2.0 0.0 2.0 4.0) 0.0 4.0 0)
1037 (range-test-f32 #f32(-4.0 -2.0 0.0 2.0 4.0) #f32(-4.0 -3.0 -2.0 -1.0 0.0) #f32(0.0 1.0 2.0 3.0 4.0) #f)
1038 (range-test-f32 #f32(-4.0 -2.0 0.0 2.0 4.0) #f32(-4.0 -1.0 -2.0 -1.0 0.0) #f32(0.0 1.0 2.0 3.0 4.0) 1)
1039 (range-test-f32 #f32(-4.0 -2.0 0.0 2.0 4.0) #f32(-4.0 -3.0 -2.0 -1.0 0.0) #f32(0.0 1.0 -1.0 3.0 4.0) 2)
1040
1041 (range-test-f64 #f64(-4.0 -2.0 0.0 2.0 4.0) #f #f #f)
1042 (range-test-f64 #f64(-4.0 -2.0 0.0 2.0 4.0) -4.0 4.0 #f)
1043 (range-test-f64 #f64(-4.0 -2.0 0.0 2.0 4.0) -4.0 3.0 4)
1044 (range-test-f64 #f64(-4.0 -2.0 0.0 2.0 4.0) -4.0 0.0 3)
1045 (range-test-f64 #f64(-4.0 -2.0 0.0 2.0 4.0) -3.0 4.0 0)
1046 (range-test-f64 #f64(-4.0 -2.0 0.0 2.0 4.0) 0.0 4.0 0)
1047 (range-test-f64 #f64(-4.0 -2.0 0.0 2.0 4.0) #f64(-4.0 -3.0 -2.0 -1.0 0.0) #f64(0.0 1.0 2.0 3.0 4.0) #f)
1048 (range-test-f64 #f64(-4.0 -2.0 0.0 2.0 4.0) #f64(-4.0 -1.0 -2.0 -1.0 0.0) #f64(0.0 1.0 2.0 3.0 4.0) 1)
1049 (range-test-f64 #f64(-4.0 -2.0 0.0 2.0 4.0) #f64(-4.0 -3.0 -2.0 -1.0 0.0) #f64(0.0 1.0 -1.0 3.0 4.0) 2)
1050
1051 ;;-------------------------------------------------------------------
1052 (test-section "clamp")
1053
1054 (define (clamp-test tag class tagvector? tagvector-ref tagvector-length
1055 clamp v minv maxv)
1056 (define (clamp-min index v)
1057 (cond [(tagvector? minv)
1058 (max v (tagvector-ref minv index) (tag->min tag))]
1059 [(not minv) (max v (tag->min tag))]
1060 [else (max v minv (tag->min tag))]))
1061 (define (clamp-max index v)
1062 (cond [(tagvector? maxv)
1063 (min v (tagvector-ref maxv index) (tag->max tag))]
1064 [(not maxv) (min v (tag->max tag))]
1065 [else (min v maxv (tag->max tag))]))
1066 (let1 result (map-to class
1067 (^i (clamp-min i (clamp-max i (tagvector-ref v i))))
1068 (iota (tagvector-length v)))
1069 (test* (format #f "~svector-clamp" tag)
1070 result
1071 (clamp v minv maxv))
1072 (when (or (tagvector? minv) (tagvector? maxv))
1073 (test* (format #f "~svector-clamp (list)" tag)
1074 result
1075 (clamp v
1076 (if (tagvector? minv)
1077 (coerce-to <list> minv)
1078 minv)
1079 (if (tagvector? maxv)
1080 (coerce-to <list> maxv)
1081 maxv)))
1082 (test* (format #f "~svector-clamp (vector)" tag)
1083 result
1084 (clamp v
1085 (if (tagvector? minv)
1086 (coerce-to <vector> minv)
1087 minv)
1088 (if (tagvector? maxv)
1089 (coerce-to <vector> maxv)
1090 maxv)))
1091 )
1092 ))
1093
1094 (expand-uvec
1095 (u8 s8 u16 s16 u32 s32 u64 s64)
1096 (define (clamp-test-@ v minv maxv)
1097 (clamp-test '@ <@vector> @vector? @vector-ref @vector-length
1098 @vector-clamp v minv maxv)))
1099
1100 (clamp-test-s8 #s8(0 -127 -4 4 127) #f #f)
1101 (clamp-test-s8 #s8(0 -127 -4 4 127) -8 #f)
1102 (clamp-test-s8 #s8(0 -127 -4 4 127) 0 #f)
1103 (clamp-test-s8 #s8(0 -127 -4 4 127) 8 #f)
1104 (clamp-test-s8 #s8(0 -127 -4 4 127) #f -8)
1105 (clamp-test-s8 #s8(0 -127 -4 4 127) #f 0)
1106 (clamp-test-s8 #s8(0 -127 -4 4 127) #f 8)
1107 (clamp-test-s8 #s8(0 -127 -4 4 127) -1 1)
1108 (clamp-test-s8 #s8(0 -127 -4 4 127)
1109 #x-ffffffffffffffffffffffffffffff 0)
1110 (clamp-test-s8 #s8(0 -127 -4 4 127)
1111 0 #xffffffffffffffffffffffffffffff)
1112 (clamp-test-s8 #s8(0 -127 -4 4 127)
1113 #s8(-3 -4 -6 -8 -19) #s8(3 7 9 2 4))
1114
1115 (clamp-test-u8 #u8(127 0 4 200 255) #f #f)
1116 (clamp-test-u8 #u8(127 0 4 200 255) -4 #f)
1117 (clamp-test-u8 #u8(127 0 4 200 255) 0 #f)
1118 (clamp-test-u8 #u8(127 0 4 200 255) 199 #f)
1119 (clamp-test-u8 #u8(127 0 4 200 255) #f -4)
1120 (clamp-test-u8 #u8(127 0 4 200 255) #f 0)
1121 (clamp-test-u8 #u8(127 0 4 200 255) #f 199)
1122 (clamp-test-u8 #u8(127 0 4 200 255)
1123 #x-ffffffffffffffffffffffffffffff 0)
1124 (clamp-test-u8 #u8(127 0 4 200 255)
1125 0 #xffffffffffffffffffffffffffffff)
1126 (clamp-test-u8 #u8(127 0 4 200 255)
1127 #u8(3 3 3 3 3) #u8(199 199 199 199 199))
1128
1129 (clamp-test-s16 #s16(0 -127 -4 4 127) #f #f)
1130 (clamp-test-s16 #s16(0 -127 -4 4 127) -8 #f)
1131 (clamp-test-s16 #s16(0 -127 -4 4 127) 0 #f)
1132 (clamp-test-s16 #s16(0 -127 -4 4 127) 8 #f)
1133 (clamp-test-s16 #s16(0 -127 -4 4 127) #f -8)
1134 (clamp-test-s16 #s16(0 -127 -4 4 127) #f 0)
1135 (clamp-test-s16 #s16(0 -127 -4 4 127) #f 8)
1136 (clamp-test-s16 #s16(0 -127 -4 4 127) -1 1)
1137 (clamp-test-s16 #s16(0 -127 -4 4 127)
1138 #x-ffffffffffffffffffffffffffffff 0)
1139 (clamp-test-s16 #s16(0 -127 -4 4 127)
1140 0 #xffffffffffffffffffffffffffffff)
1141 (clamp-test-s16 #s16(0 -127 -4 4 127)
1142 #s16(-3 -4 -6 -8 -19) #s16(3 7 9 2 4))
1143
1144 (clamp-test-u16 #u16(127 0 4 200 255) #f #f)
1145 (clamp-test-u16 #u16(127 0 4 200 255) -4 #f)
1146 (clamp-test-u16 #u16(127 0 4 200 255) 0 #f)
1147 (clamp-test-u16 #u16(127 0 4 200 255) 199 #f)
1148 (clamp-test-u16 #u16(127 0 4 200 255) #f -4)
1149 (clamp-test-u16 #u16(127 0 4 200 255) #f 0)
1150 (clamp-test-u16 #u16(127 0 4 200 255) #f 199)
1151 (clamp-test-u16 #u16(127 0 4 200 255)
1152 #x-ffffffffffffffffffffffffffffff 0)
1153 (clamp-test-u16 #u16(127 0 4 200 255)
1154 0 #xffffffffffffffffffffffffffffff)
1155 (clamp-test-u16 #u16(127 0 4 200 255)
1156 #u16(3 3 3 3 3) #u16(199 199 199 199 199))
1157
1158 (clamp-test-s32 #s32(0 -127 -4 4 127) #f #f)
1159 (clamp-test-s32 #s32(0 -127 -4 4 127) -8 #f)
1160 (clamp-test-s32 #s32(0 -127 -4 4 127) 0 #f)
1161 (clamp-test-s32 #s32(0 -127 -4 4 127) 8 #f)
1162 (clamp-test-s32 #s32(0 -127 -4 4 127) #f -8)
1163 (clamp-test-s32 #s32(0 -127 -4 4 127) #f 0)
1164 (clamp-test-s32 #s32(0 -127 -4 4 127) #f 8)
1165 (clamp-test-s32 #s32(0 -127 -4 4 127) -1 1)
1166 (clamp-test-s32 #s32(0 -127 -4 4 127)
1167 #x-ffffffffffffffffffffffffffffff 0)
1168 (clamp-test-s32 #s32(0 -127 -4 4 127)
1169 0 #xffffffffffffffffffffffffffffff)
1170 (clamp-test-s32 #s32(0 -127 -4 4 127)
1171 #s32(-3 -4 -6 -8 -19) #s32(3 7 9 2 4))
1172
1173 (clamp-test-u32 #u32(127 0 4 200 255) #f #f)
1174 (clamp-test-u32 #u32(127 0 4 200 255) -4 #f)
1175 (clamp-test-u32 #u32(127 0 4 200 255) 0 #f)
1176 (clamp-test-u32 #u32(127 0 4 200 255) 199 #f)
1177 (clamp-test-u32 #u32(127 0 4 200 255) #f -4)
1178 (clamp-test-u32 #u32(127 0 4 200 255) #f 0)
1179 (clamp-test-u32 #u32(127 0 4 200 255) #f 199)
1180 (clamp-test-u32 #u32(127 0 4 200 255)
1181 #x-ffffffffffffffffffffffffffffff 0)
1182 (clamp-test-u32 #u32(127 0 4 200 255)
1183 0 #xffffffffffffffffffffffffffffff)
1184 (clamp-test-u32 #u32(127 0 4 200 255)
1185 #u32(3 3 3 3 3) #u32(199 199 199 199 199))
1186
1187 (clamp-test-s64 #s64(0 -127 -4 4 127) #f #f)
1188 (clamp-test-s64 #s64(0 -127 -4 4 127) -8 #f)
1189 (clamp-test-s64 #s64(0 -127 -4 4 127) 0 #f)
1190 (clamp-test-s64 #s64(0 -127 -4 4 127) 8 #f)
1191 (clamp-test-s64 #s64(0 -127 -4 4 127) #f -8)
1192 (clamp-test-s64 #s64(0 -127 -4 4 127) #f 0)
1193 (clamp-test-s64 #s64(0 -127 -4 4 127) #f 8)
1194 (clamp-test-s64 #s64(0 -127 -4 4 127) -1 1)
1195 (clamp-test-s64 #s64(0 -127 -4 4 127)
1196 #x-ffffffffffffffffffffffffffffff 0)
1197 (clamp-test-s64 #s64(0 -127 -4 4 127)
1198 0 #xffffffffffffffffffffffffffffff)
1199 (clamp-test-s64 #s64(0 -127 -4 4 127)
1200 #s64(-3 -4 -6 -8 -19) #s64(3 7 9 2 4))
1201
1202 (clamp-test-u64 #u64(127 0 4 200 255) #f #f)
1203 (clamp-test-u64 #u64(127 0 4 200 255) -4 #f)
1204 (clamp-test-u64 #u64(127 0 4 200 255) 0 #f)
1205 (clamp-test-u64 #u64(127 0 4 200 255) 199 #f)
1206 (clamp-test-u64 #u64(127 0 4 200 255) #f -4)
1207 (clamp-test-u64 #u64(127 0 4 200 255) #f 0)
1208 (clamp-test-u64 #u64(127 0 4 200 255) #f 199)
1209 (clamp-test-u64 #u64(127 0 4 200 255)
1210 #x-ffffffffffffffffffffffffffffff 0)
1211 (clamp-test-u64 #u64(127 0 4 200 255)
1212 0 #xffffffffffffffffffffffffffffff)
1213 (clamp-test-u64 #u64(127 0 4 200 255)
1214 #u64(3 3 3 3 3) #u64(199 199 199 199 199))
1215
1216 ;;-------------------------------------------------------------------
1217 (test-section "block i/o")
1218
1219 (sys-unlink "test.o")
1220
1221 (let* ([data1 '#u8(0 1 2 3 255 254 253 252 4 5 6 7 251 250 249 248)]
1222 [data2 '#u8(1 0 3 2 254 255 252 253 5 4 7 6 250 251 248 249)]
1223 [data3 '#u8(3 2 1 0 252 253 254 255 7 6 5 4 248 249 250 251)]
1224 [data4 '#u8(252 253 254 255 3 2 1 0 248 249 250 251 7 6 5 4)]
1225
1226 [data5 '#u8(3 0 1 3 3 1 0 3 131 0 1 131 131 1 0 131)]
1227 )
1228
1229 ;; Invaliance:
1230 ;; w = (uvector-alias T u8v) <--> w = read-block! T + write-block u8v
1231 (define (test-default-endian data)
1232 (^[T maker size]
1233 (test* (format "native endian ~a" (class-name T))
1234 (uvector-alias T data)
1235 (rlet1 buf (maker size)
1236 (call-with-output-file "test.o" (cut write-block data <>))
1237 (call-with-input-file "test.o" (cut read-block! buf <>))))))
1238
1239 (define (test-reverse-endian T maker size)
1240 (let* ([rev-endians (case (native-endian)
1241 [(big-endian) '(little-endian little)]
1242 [(little-endian arm-little-endian) '(big-endian big)])]
1243 [rev-vec (cond
1244 [(memq T (list <u8vector> <s8vector>)) data1]
1245 [(memq T (list <u16vector> <s16vector>)) data2]
1246 [(memq T (list <u32vector> <s32vector>)) data3]
1247 [(memq T (list <u64vector> <s64vector>)) data4])])
1248 (dolist [rev-endian rev-endians]
1249 (test* (format "reading ~a ~a" rev-endian (class-name T))
1250 (uvector-alias T rev-vec)
1251 (rlet1 buf (maker size)
1252 (call-with-output-file "test.o" (cut write-block data1 <>))
1253 (call-with-input-file "test.o"
1254 (cut read-block! buf <> 0 -1 rev-endian))))
1255 (test* (format "reading ~a ~a, parameter" rev-endian (class-name T))
1256 (uvector-alias T rev-vec)
1257 (rlet1 buf (maker size)
1258 (call-with-output-file "test.o" (cut write-block data1 <>))
1259 (parameterize ([default-endian rev-endian])
1260 (call-with-input-file "test.o" (cut read-block! buf <>)))))
1261 (test* (format "writing ~a ~a" rev-endian (class-name T))
1262 (uvector-alias T data1)
1263 (rlet1 buf (maker size)
1264 (call-with-output-file "test.o"
1265 (cut write-block (uvector-alias T rev-vec) <> 0 -1 rev-endian))
1266 (call-with-input-file "test.o" (cut read-block! buf <>))))
1267 (test* (format "writing ~a ~a, parameter" rev-endian (class-name T))
1268 (uvector-alias T data1)
1269 (rlet1 buf (maker size)
1270 (parameterize ([default-endian rev-endian])
1271 (call-with-output-file "test.o"
1272 (cut write-block (uvector-alias T rev-vec) <>)))
1273 (call-with-input-file "test.o" (cut read-block! buf <>)))))))
1274
1275 (define (run-across fn)
1276 (let1 s (u8vector-length data1)
1277 (fn <u8vector> make-u8vector s)
1278 (fn <s8vector> make-s8vector s)
1279 (fn <u16vector> make-u16vector (/ s 2))
1280 (fn <s16vector> make-s16vector (/ s 2))
1281 (fn <u32vector> make-u32vector (/ s 4))
1282 (fn <s32vector> make-s32vector (/ s 4))
1283 (fn <u64vector> make-u64vector (/ s 8))
1284 (fn <s64vector> make-s64vector (/ s 8))
1285 ))
1286 (define (run-across-f fn)
1287 (let1 s (u8vector-length data5)
1288 (fn <f16vector> make-f16vector (/ s 2))
1289 (fn <f32vector> make-f32vector (/ s 4))
1290 (fn <f64vector> make-f64vector (/ s 8))
1291 ))
1292
1293 (run-across (test-default-endian data1))
1294 (run-across-f (test-default-endian data5))
1295 (run-across test-reverse-endian)
1296 )
1297
1298 ;;-------------------------------------------------------------------
1299 (test-section "string <-> uvector")
1300
1301 (test* "string->u8vector" '#u8(64 65 66 67 68)
1302 (string->u8vector "@ABCD"))
1303 (test* "string->u8vector (start)" '#u8(66 67 68)
1304 (string->u8vector "@ABCD" 2))
1305 (test* "string->u8vector (start, end)" '#u8(65 66 67)
1306 (string->u8vector "@ABCD" 1 4))
1307 (test* "string->u8vector (start, end)" '#u8(64 65 66 67 68)
1308 (string->u8vector "@ABCD" 0 5))
1309 (test* "string->u8vector (OOB)" (test-error)
1310 (string->u8vector "abcde" 2 6))
1311
1312 (test* "string->u8vector (immutable)" '(#t #t)
1313 (let ([a (string->u8vector "@ABCD" 0 -1 #t)]
1314 [b (string->u8vector "@ABCD")])
1315 (list (uvector-immutable? a)
1316 (equal? a b))))
1317
1318 (test* "string->u8vector!" '#u8(64 65 66 67 68)
1319 (let1 v (u8vector 0 1 2 3 4)
1320 (string->u8vector! v 0 "@ABCD")))
1321 (test* "string->u8vector!" '#u8(64 65 66 67 68)
1322 (let1 v (u8vector 0 1 2 3 4)
1323 (string->u8vector! v 0 "@ABCDEFGHIJKLMNOPQRTSUVWXYZ")))
1324 (test* "string->u8vector!" '#u8(64 65 66 3 4)
1325 (let1 v (u8vector 0 1 2 3 4)
1326 (string->u8vector! v 0 "@AB")))
1327 (test* "string->u8vector!" '#u8(0 64 65 66 4)
1328 (let1 v (u8vector 0 1 2 3 4)
1329 (string->u8vector! v 1 "@AB")))
1330 (test* "string->u8vector!" '#u8(0 1 2 3 65)
1331 (let1 v (u8vector 0 1 2 3 4)
1332 (string->u8vector! v 4 "@ABCDE" 1)))
1333 (test* "string->u8vector!" '#u8(0 1 2 3 4)
1334 (let1 v (u8vector 0 1 2 3 4)
1335 (string->u8vector! v 8 "@ABCDE" 1)))
1336 (test* "string->u8vector!" '#u8(0 1 2 3 4)
1337 (let1 v (u8vector 0 1 2 3 4)
1338 (string->u8vector! v -1 "@ABCDE" 1)))
1339
1340 (test* "u8vector->string" "@ABCD"
1341 (u8vector->string '#u8(64 65 66 67 68)))
1342 (test* "u8vector->string (start)" "ABCD"
1343 (u8vector->string '#u8(64 65 66 67 68) 1))
1344 (test* "u8vector->string (start, end)" "BC"
1345 (u8vector->string '#u8(64 65 66 67 68) 2 4))
1346 (test* "u8vector->string (start, end)" "@ABCD"
1347 (u8vector->string '#u8(64 65 66 67 68) 0 5))
1348 (test* "u8vector->string (OOB)" (test-error)
1349 (u8vector->string '#u8(64 65 66 67 68) 0 8))
1350
1351 (test* "u8vector->string (terminator, 0)" "@ABCD"
1352 (u8vector->string '#u8(64 65 66 67 68 0 69 70) 0 8 0))
1353 (test* "u8vector->string (terminator, 0)" "@ABCD\x00;EF"
1354 (u8vector->string '#u8(64 65 66 67 68 0 69 70) 0 8 1))
1355 (test* "u8vector->string (terminator, 0)" "@AB"
1356 (u8vector->string '#u8(64 65 66 67 68 0 69 70) 0 8 67))
1357 (test* "u8vector->string (terminator, 0)" "BCD"
1358 (u8vector->string '#u8(64 65 66 67 68 0 69 70) 2 8 0))
1359
1360 (test* "string->s8vector" '#s8(64 65 66 67 68)
1361 (string->s8vector "@ABCD"))
1362 (test* "string->s8vector (start)" '#s8(66 67 68)
1363 (string->s8vector "@ABCD" 2))
1364 (test* "string->s8vector (start, end)" '#s8(65 66 67)
1365 (string->s8vector "@ABCD" 1 4))
1366 (test* "string->s8vector (start, end)" '#s8(64 65 66 67 68)
1367 (string->s8vector "@ABCD" 0 5))
1368 (test* "string->s8vector (OOB)" (test-error)
1369 (string->s8vector "abcde" 2 6))
1370
1371 (test* "string->s8vector!" '#s8(64 65 66 67 68)
1372 (let1 v (s8vector 0 1 2 3 4)
1373 (string->s8vector! v 0 "@ABCD")))
1374 (test* "string->s8vector!" '#s8(64 65 66 67 68)
1375 (let1 v (s8vector 0 1 2 3 4)
1376 (string->s8vector! v 0 "@ABCDEFGHIJKLMNOPQRTSUVWXYZ")))
1377 (test* "string->s8vector!" '#s8(64 65 66 3 4)
1378 (let1 v (s8vector 0 1 2 3 4)
1379 (string->s8vector! v 0 "@AB")))
1380 (test* "string->s8vector!" '#s8(0 64 65 66 4)
1381 (let1 v (s8vector 0 1 2 3 4)
1382 (string->s8vector! v 1 "@AB")))
1383 (test* "string->s8vector!" '#s8(0 1 2 3 65)
1384 (let1 v (s8vector 0 1 2 3 4)
1385 (string->s8vector! v 4 "@ABCDE" 1)))
1386 (test* "string->s8vector!" '#s8(0 1 2 3 4)
1387 (let1 v (s8vector 0 1 2 3 4)
1388 (string->s8vector! v 8 "@ABCDE" 1)))
1389 (test* "string->s8vector!" '#s8(0 1 2 3 4)
1390 (let1 v (s8vector 0 1 2 3 4)
1391 (string->s8vector! v -1 "@ABCDE" 1)))
1392
1393 (test* "s8vector->string" "@ABCD"
1394 (s8vector->string '#s8(64 65 66 67 68)))
1395 (test* "s8vector->string (start)" "ABCD"
1396 (s8vector->string '#s8(64 65 66 67 68) 1))
1397 (test* "s8vector->string (start, end)" "BC"
1398 (s8vector->string '#s8(64 65 66 67 68) 2 4))
1399 (test* "s8vector->string (start, end)" "@ABCD"
1400 (s8vector->string '#s8(64 65 66 67 68) 0 5))
1401 (test* "s8vector->string (OOB)" (test-error)
1402 (s8vector->string '#s8(64 65 66 67 68) 0 8))
1403
1404 (test* "s8vector->string (terminator,0)" "@ABCD"
1405 (s8vector->string '#s8(64 65 66 67 68 0 69 70) 0 8 0))
1406 (test* "s8vector->string (terminator,1)" "@ABCD\x00;EF"
1407 (s8vector->string '#s8(64 65 66 67 68 0 69 70) 0 8 1))
1408 (test* "s8vector->string (terminator,-1)" "@ABCD"
1409 (s8vector->string '#s8(64 65 66 67 68 -1 69 70) 0 8 -1))
1410
1411 (test* "string->u32vector" '#u32(64 65 66 67 68)
1412 (string->u32vector "@ABCD"))
1413 (test* "string->u32vector (start)" '#u32(66 67 68)
1414 (string->u32vector "@ABCD" 2))
1415 (test* "string->u32vector (start, end)" '#u32(65 66 67)
1416 (string->u32vector "@ABCD" 1 4))
1417 (test* "string->u32vector (start, end)" '#u32(64 65 66 67 68)
1418 (string->u32vector "@ABCD" 0 5))
1419 (test* "string->u32vector (OOB)" (test-error)
1420 (string->u32vector "abcde" 2 6))
1421 (test* "string->u32vector (byte swapping)"
1422 '#u32(#x40000000 #x41000000 #x42000000)
1423 (string->u32vector "@AB" 0 -1
1424 (case (default-endian)
1425 [(big-endian) 'little-endian]
1426 [(little-endian) 'big-endian])))
1427
1428 (test* "string->u32vector!" '#u32(64 65 66 67 68 1 1 1)
1429 (string->u32vector! (make-u32vector 8 1) 0 "@ABCD"))
1430 (test* "string->u32vector!" '#u32(1 1 1 1 64 65 66 67)
1431 (string->u32vector! (make-u32vector 8 1) 4 "@ABCD"))
1432 (test* "string->u32vector!" '#u32(1 1 1 66 67 1 1 1)
1433 (string->u32vector! (make-u32vector 8 1) 3 "@ABCD" 2 4))
1434 (test* "string->u32vector! (byte swapping)"
1435 '#u32(#x40000000 #x41000000 #x42000000)
1436 (string->u32vector! (make-u32vector 3) 0 "@AB" 0 3
1437 (case (default-endian)
1438 [(big-endian) 'little-endian]
1439 [(little-endian) 'big-endian]) ))
1440
1441
1442 (test* "u32vector->string" "@ABCD"
1443 (u32vector->string '#u32(64 65 66 67 68)))
1444 (test* "u32vector->string (start)" "ABCD"
1445 (u32vector->string '#u32(64 65 66 67 68) 1))
1446 (test* "u32vector->string (start, end)" "BC"
1447 (u32vector->string '#u32(64 65 66 67 68) 2 4))
1448 (test* "u32vector->string (start, end)" "@ABCD"
1449 (u32vector->string '#u32(64 65 66 67 68) 0 5))
1450 (test* "u32vector->string (OOB)" (test-error)
1451 (u32vector->string '#u32(64 65 66 67 68) 0 8))
1452 (test* "u32vector->string (terminator)" "@AB"
1453 (u32vector->string '#u32(64 65 66 0 67 68) 0 -1 0))
1454 (test* "u32vector->string (terminator)" "@AB\0CD"
1455 (u32vector->string '#u32(64 65 66 0 67 68) 0 -1 1))
1456 (test* "u32vector->string (terminator)" "@AB\0C"
1457 (u32vector->string '#u32(64 65 66 0 67 68) 0 -1 68))
1458 (test* "u32vector->string (byte swapping)" "@AB"
1459 (u32vector->string '#u32(#x40000000 #x41000000 #x42000000)
1460 0 -1 #f
1461 (case (default-endian)
1462 [(big-endian) 'little-endian]
1463 [(little-endian) 'big-endian])))
1464
1465 (test* "string->s32vector" '#s32(64 65 66 67 68)
1466 (string->s32vector "@ABCD"))
1467 (test* "string->s32vector (start)" '#s32(66 67 68)
1468 (string->s32vector "@ABCD" 2))
1469 (test* "string->s32vector (start, end)" '#s32(65 66 67)
1470 (string->s32vector "@ABCD" 1 4))
1471 (test* "string->s32vector (start, end)" '#s32(64 65 66 67 68)
1472 (string->s32vector "@ABCD" 0 5))
1473 (test* "string->s32vector (OOB)" (test-error)
1474 (string->s32vector "abcde" 2 6))
1475 (test* "string->s32vector (byte swapping)"
1476 '#s32(#x40000000 #x41000000 #x42000000)
1477 (string->s32vector "@AB" 0 -1
1478 (case (default-endian)
1479 [(big-endian) 'little-endian]
1480 [(little-endian) 'big-endian])))
1481
1482 (test* "s32vector->string" "@ABCD"
1483 (s32vector->string '#s32(64 65 66 67 68)))
1484 (test* "s32vector->string (start)" "ABCD"
1485 (s32vector->string '#s32(64 65 66 67 68) 1))
1486 (test* "s32vector->string (start, end)" "BC"
1487 (s32vector->string '#s32(64 65 66 67 68) 2 4))
1488 (test* "s32vector->string (start, end)" "@ABCD"
1489 (s32vector->string '#s32(64 65 66 67 68) 0 5))
1490 (test* "s32vector->string (OOB)" (test-error)
1491 (s32vector->string '#s32(64 65 66 67 68) 0 8))
1492 (test* "s32vector->string (terminator)" "@AB"
1493 (s32vector->string '#s32(64 65 66 0 67 68) 0 -1 0))
1494 (test* "s32vector->string (terminator)" "@AB\0CD"
1495 (s32vector->string '#s32(64 65 66 0 67 68) 0 -1 1))
1496 (test* "s32vector->string (terminator)" "@AB\0C"
1497 (s32vector->string '#s32(64 65 66 0 67 68) 0 -1 68))
1498 (test* "s32vector->string (byte swapping)" "@AB"
1499 (s32vector->string '#s32(#x40000000 #x41000000 #x42000000)
1500 0 -1 #f
1501 (case (default-endian)
1502 [(big-endian) 'little-endian]
1503 [(little-endian) 'big-endian])))
1504
1505 ;; test for multibyte chars
1506 (include "test-utf8")
1507
1508 ;;-------------------------------------------------------------------
1509 (test-section "uvector alias")
1510
1511 (test* "alias u8 u8" #u8(0 1 2 3)
1512 (let* ([src (u8vector 0 1 2 0)]
1513 [dst (uvector-alias <u8vector> src)])
1514 (u8vector-set! src 3 3)
1515 dst))
1516 (test* "alias u8 u8 (range)" #u8(1 2)
1517 (let* ([src (u8vector 0 1 0 1)]
1518 [dst (uvector-alias <u8vector> src 1 3)])
1519 (u8vector-set! src 2 2)
1520 dst))
1521 (test* "alias s8 u8" #s8(1 -1)
1522 (let* ([src (u8vector 0 0 0 0)]
1523 [dst (uvector-alias <s8vector> src 1 3)])
1524 (s8vector-set! dst 0 1)
1525 (u8vector-set! src 2 255)
1526 dst))
1527 ;; the following test cases avoid endian complexity
1528 (test* "alias s8 u16" '(#s8(#x11 #x11 #x22 #x22) #x1111)
1529 (let* ([src (u16vector 0 0 0)]
1530 [dst (uvector-alias <s8vector> src 1)])
1531 (u16vector-set! src 2 #x2222)
1532 (s8vector-set! dst 0 #x11)
1533 (s8vector-set! dst 1 #x11)
1534 (list dst (u16vector-ref src 1))))
1535 (test* "alias u32 u8" #u32(#xaaaaaaaa #xbbbbbbbb)
1536 (let* ([src (make-u8vector 8 #xaa)]
1537 [ali (uvector-alias <u8vector> src 4)]
1538 [dst (uvector-alias <u32vector> src)])
1539 (u8vector-fill! ali #xbb)
1540 dst))
1541
1542 ;; tricky, but should work on IEEE754 compiant floats
1543 (test* "alias u32 f32" #u32(0)
1544 (uvector-alias <u32vector> #f32(0.0)))
1545 (test* "alias u32 f64" #u32(0 0)
1546 (uvector-alias <u32vector> #f64(0.0)))
1547 (test* "alias f64 u32" #f64(0.0)
1548 (uvector-alias <f64vector> #u32(1 1 0 0) 2))
1549 (test* "alias u64 f64" #u64(0)
1550 (uvector-alias <u64vector> #f64(0.0)))
1551
1552 (test* "alias turn-around" #f32(1.5)
1553 (uvector-alias <f32vector>
1554 (uvector-alias <u8vector> #f32(1.0 1.5 2.0))
1555 4 8))
1556
1557 ;; test alignment check
1558 (test* "alias u32 u8 (alignment violation)" (test-error)
1559 (let* ([src (make-u8vector 9)]
1560 [dst (uvector-alias <u32vector>)])
1561 dst))
1562 (test* "alias u32 u8 (alignment violation)" (test-error)
1563 (let* ([src (make-u8vector 32)]
1564 [dst (uvector-alias <u32vector> 2)])
1565 dst))
1566 (test* "alias u32 u8 (alignment violation)" (test-error)
1567 (let* ([src (make-u8vector 32)]
1568 [dst (uvector-alias <u32vector> 4 5)])
1569 dst))
1570
1571 ;; test if immutable property propagates
1572 (test* "immutability violation" (test-error)
1573 (let* ([src '#u8(0 1 2 3)]
1574 [dst (uvector-alias <u8vector> src)])
1575 (u8vector-set! dst 0 1)))
1576
1577 ;;-------------------------------------------------------------------
1578 ; (use gauche.array)
1579 (test-section "gauche.array")
1580 (use gauche.array)
1581 (test-module 'gauche.array)
1582
1583 (test-section "simple array op")
1584
1585 (test* "shape" #t
1586 (and (shape)
1587 (shape -1 -1)
1588 (shape -1 0)
1589 (shape -1 1)
1590 (shape 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8)
1591 #t))
1592
1593 (test* "shape" (test-error) (shape 1))
1594 (test* "shape" (test-error) (shape 1 2 3))
1595 (test* "shape" (test-error) (shape 3 1))
1596
1597 (test* "make-array" #t
1598 (and (make-array (shape))
1599 (make-array (shape) *)
1600 (make-array (shape -1 -1))
1601 (make-array (shape -1 -1) *)
1602 (make-array (shape -1 1))
1603 (make-array (shape 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 1 2 3 4) *)
1604 #t))
1605
1606 (test* "array" #t
1607 (and (array (shape) *)
1608 (array (shape -1 -1))
1609 (array (shape -1 1) * *)
1610 (array (shape 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8) *)
1611 #t))
1612
1613 (test-section "array-rank")
1614 (test* "array-rank (shape)" 2
1615 (array-rank (shape)))
1616 (test* "array-rank (shape)" 2
1617 (array-rank (shape -1 -1)))
1618 (test* "array-rank (shape)" 2
1619 (array-rank (shape 1 2 3 4 5 6 7 8)))
1620
1621 (test* "array-rank (make-array)" 0
1622 (array-rank (make-array (shape))))
1623 (test* "array-rank (make-array)" 1
1624 (array-rank (make-array (shape -1 -1))))
1625 (test* "array-rank (make-array)" 1
1626 (array-rank (make-array (shape -1 1))))
1627 (test* "array-rank (make-array)" 4
1628 (array-rank (make-array (shape 1 2 3 4 5 6 7 8))))
1629
1630 (test* "array-rank (array)" 0
1631 (array-rank (array (shape) *)))
1632 (test* "array-rank (array)" 1
1633 (array-rank (array (shape -1 -1))))
1634 (test* "array-rank (array)" 1
1635 (array-rank (array (shape -1 1) * *)))
1636 (test* "array-rank (array)" 4
1637 (array-rank (array (shape 1 2 3 4 5 6 7 8) *)))
1638
1639 (test-section "array-start and array-end")
1640 (test* "array-start (shape)" 0
1641 (array-start (shape -1 -1) 0))
1642 (test* "array-start (shape)" 0
1643 (array-start (shape -1 -1) 1))
1644 (test* "array-start (shape)" 0
1645 (array-start (shape -1 1) 0))
1646 (test* "array-start (shape)" 0
1647 (array-start (shape -1 1) 1))
1648 (test* "array-start (shape)" 0
1649 (array-start (shape 1 2 3 4 5 6 7 8) 0))
1650 (test* "array-start (shape)" 0
1651 (array-start (shape 1 2 3 4 5 6 7 8) 1))
1652
1653 (test* "array-end (shape)" 1
1654 (array-end (shape -1 -1) 0))
1655 (test* "array-end (shape)" 2
1656 (array-end (shape -1 -1) 1))
1657 (test* "array-end (shape)" 1
1658 (array-end (shape -1 1) 0))
1659 (test* "array-end (shape)" 2
1660 (array-end (shape -1 1) 1))
1661 (test* "array-end (shape)" 4
1662 (array-end (shape 1 2 3 4 5 6 7 8) 0))
1663 (test* "array-end (shape)" 2
1664 (array-end (shape 1 2 3 4 5 6 7 8) 1))
1665
1666 (test* "array-start (make-array)" -1
1667 (array-start (make-array (shape -1 -1)) 0))
1668 (test* "array-start (make-array)" -1
1669 (array-start (make-array (shape -1 1)) 0))
1670 (test* "array-start (make-array)" '(1 3 5 7)
1671 (map (pa$ array-start (make-array (shape 1 2 3 4 5 6 7 8)))
1672 '(0 1 2 3)))
1673
1674 (test* "array-end (make-array)" -1
1675 (array-end (make-array (shape -1 -1)) 0))
1676 (test* "array-end (make-array)" 1
1677 (array-end (make-array (shape -1 1)) 0))
1678 (test* "array-end (make-array)" '(2 4 6 8)
1679 (map (pa$ array-end (make-array (shape 1 2 3 4 5 6 7 8)))
1680 '(0 1 2 3)))
1681
1682 (test* "array-start (array)" -1
1683 (array-start (array (shape -1 -1)) 0))
1684 (test* "array-start (array)" -1
1685 (array-start (array (shape -1 1) * *) 0))
1686 (test* "array-start (array)" '(1 3 5 7)
1687 (map (pa$ array-start (array (shape 1 2 3 4 5 6 7 8) *))
1688 '(0 1 2 3)))
1689
1690 (test* "array-end (array)" -1
1691 (array-end (array (shape -1 -1)) 0))
1692 (test* "array-end (array)" 1
1693 (array-end (array (shape -1 1) * *) 0))
1694 (test* "array-end (array)" '(2 4 6 8)
1695 (map (pa$ array-end (array (shape 1 2 3 4 5 6 7 8) *))
1696 '(0 1 2 3)))
1697
1698 (test-section "array-ref")
1699 (for-each
1700 (^[ls]
1701 (let1 a (car ls)
1702 (for-each
1703 (^t (test* (format "array-ref ~S:" (cdr t)) (car t)
1704 (apply array-ref a (cdr t))))
1705 (cdr ls))))
1706 '((#,(<array> () a) (a))
1707 (#,(<array> (-1 1) a b) (a -1) (b 0))
1708 (#,(<array> (1 2 3 4 5 6 7 8) a) (a 1 3 5 7))
1709 (#,(<array> (1 3 4 6 7 9 10 12) a b c d e f g h i j k l m n o p)
1710 (a 1 4 7 10) (b 1 4 7 11) (i 2 4 7 10) (n 2 5 7 11))
1711 (#,(<array> () a) (a #()))
1712 (#,(<array> (-1 1) a b) (a #(-1)) (b #(0)))
1713 (#,(<array> (1 2 3 4 5 6 7 8) a) (a #(1 3 5 7)))
1714 (#,(<array> (1 3 4 6 7 9 10 12) a b c d e f g h i j k l m n o p)
1715 (a #(1 4 7 10)) (b #(1 4 7 11)) (i #(2 4 7 10)) (n #(2 5 7 11)))
1716 (#,(<array> () a) (a #,(<array> (0 0))))
1717 (#,(<array> (-1 1) a b) (a #,(<array> (0 1) -1)) (b #,(<array> (0 1) 0)))
1718 (#,(<array> (1 2 3 4 5 6 7 8) a) (a #,(<array> (0 4) 1 3 5 7)))
1719 (#,(<array> (1 3 4 6 7 9 10 12) a b c d e f g h i j k l m n o p)
1720 (a #,(<array> (0 4) 1 4 7 10)) (b #,(<array> (0 4) 1 4 7 11))
1721 (i #,(<array> (0 4) 2 4 7 10)) (n #,(<array> (0 4) 2 5 7 11)))
1722 (#,(<u8array> () 2) (2))
1723 (#,(<u8array> (-1 1) 2 3) (2 -1) (3 0))
1724 (#,(<u8array> (1 2 3 4 5 6 7 8) 2) (2 1 3 5 7))
1725 (#,(<u8array> (1 3 4 6 7 9 10 12) 2 3 5 7 11 13 17 23 29 31 37 39 41 43 47 51)
1726 (2 1 4 7 10) (3 1 4 7 11) (29 2 4 7 10) (43 2 5 7 11))
1727 ))
1728
1729 (test-section "array-set!")
1730 (for-each
1731 (^[ls]
1732 (let1 a (car ls)
1733 (for-each
1734 (^t (test* (format "array-set! ~S:" (cdr t)) (car t)
1735 (begin (apply array-set! a (append (cdr t) (list (car t))))
1736 (apply array-ref a (cdr t)))))
1737 (cdr ls))))
1738 '((#,(<array> () a) (x))
1739 (#,(<array> (-1 1) a b) (y -1) (z 0))
1740 (#,(<array> (1 2 3 4 5 6 7 8) a) (x 1 3 5 7))
1741 (#,(<array> (1 3 4 6 7 9 10 12) a b c d e f g h i j k l m n o p)
1742 (w 1 4 7 10) (x 1 4 7 11) (y 2 4 7 10) (z 2 5 7 11))
1743 (#,(<array> () a) (x #()))
1744 (#,(<array> (-1 1) a b) (x #(-1)) (y #(0)))
1745 (#,(<array> (1 2 3 4 5 6 7 8) a) (x #(1 3 5 7)))
1746 (#,(<array> (1 3 4 6 7 9 10 12) a b c d e f g h i j k l m n o p)
1747 (w #(1 4 7 10)) (x #(1 4 7 11)) (y #(2 4 7 10)) (z #(2 5 7 11)))
1748 (#,(<array> () a) (x #,(<array> (0 0))))
1749 (#,(<array> (-1 1) a b) (x #,(<array> (0 1) -1)) (y #,(<array> (0 1) 0)))
1750 (#,(<array> (1 2 3 4 5 6 7 8) a) (x #,(<array> (0 4) 1 3 5 7)))
1751 (#,(<array> (1 3 4 6 7 9 10 12) a b c d e f g h i j k l m n o p)
1752 (w #,(<array> (0 4) 1 4 7 10)) (x #,(<array> (0 4) 1 4 7 11))
1753 (y #,(<array> (0 4) 2 4 7 10)) (z #,(<array> (0 4) 2 5 7 11)))
1754 (#,(<u8array> () 2) (102))
1755 (#,(<u8array> (-1 1) 2 3) (102 -1) (103 0))
1756 (#,(<u8array> (1 2 3 4 5 6 7 8) 2) (102 1 3 5 7))
1757 (#,(<u8array> (1 3 4 6 7 9 10 12) 2 3 5 7 11 13 17 23 29 31 37 39 41 43 47 51)
1758 (102 1 4 7 10) (103 1 4 7 11) (129 2 4 7 10) (143 2 5 7 11))
1759 ))
1760
1761 (test* "array-valid-index? (list)" #t
1762 (array-valid-index? (make-array (shape) 'o)))
1763 (test* "array-valid-index? (list)" #t
1764 (array-valid-index? (make-array (shape -1 1) 'o) -1))
1765 (test* "array-valid-index? (list)" #t
1766 (array-valid-index? (make-array (shape 1 2 3 4 5 6 7 8) 'o) 1 3 5 7))
1767 (test* "array-valid-index? (list)" #f
1768 (array-valid-index? (make-array (shape) 'o) 0))
1769 (test* "array-valid-index? (list)" #f
1770 (array-valid-index? (make-array (shape -1 1) 'o) -2))
1771 (test* "array-valid-index? (list)" #f
1772 (array-valid-index? (make-array (shape 1 2 3 4 5 6 7 8) 'o) 1 3 5))
1773
1774 (test* "array-valid-index? (vector)" #t
1775 (array-valid-index? (make-array (shape) 'o) #()))
1776 (test* "array-valid-index? (vector)" #t
1777 (array-valid-index? (make-array (shape -1 1) 'o) #(0)))
1778 (test* "array-valid-index? (vector)" #t
1779 (array-valid-index? (make-array (shape 1 2 3 4 5 6 7 8) 'o) #(1 3 5 7)))
1780 (test* "array-valid-index? (vector)" #f
1781 (array-valid-index? (make-array (shape) 'o) #(0)))
1782 (test* "array-valid-index? (vector)" #f
1783 (array-valid-index? (make-array (shape -1 1) 'o) #(1)))
1784 (test* "array-valid-index? (vector)" #f
1785 (array-valid-index? (make-array (shape 1 2 3 4 5 6 7 8) 'o) #(1 3 5 7 9)))
1786
1787 (test* "array-valid-index? (array)" #t
1788 (array-valid-index? (make-array (shape) 'o) #,(<array> (0 0))))
1789 (test* "array-valid-index? (array)" #t
1790 (array-valid-index? (make-array (shape -1 1) 'o) #,(<array> (0 1) 0)))
1791 (test* "array-valid-index? (array)" #t
1792 (array-valid-index? (make-array (shape 1 2 3 4 5 6 7 8) 'o) #,(<array> (0 4) 1 3 5 7)))
1793 (test* "array-valid-index? (array)" #f
1794 (array-valid-index? (make-array (shape) 'o) #,(<array> (0 1) 0)))
1795 (test* "array-valid-index? (array)" #f
1796 (array-valid-index? (make-array (shape -1 1) 'o) #,(<array> (0 1) 1)))
1797 (test* "array-valid-index? (array)" #f
1798 (array-valid-index? (make-array (shape 1 2 3 4 5 6 7 8) 'o) #,(<array> (0 4) 1 4 5 7)))
1799
1800 ;;; Share and change:
1801 ;;;
1802 ;;; org brk swp box
1803 ;;;
1804 ;;; 0 1 1 2 5 6
1805 ;;; 6 a b 2 a b 3 d c 0 2 4 6 8: e
1806 ;;; 7 c d 3 e f 4 f e
1807 ;;; 8 e f
1808
1809 (test-section "shared change")
1810
1811 (let* ([org (array (shape 6 9 0 2) 'a 'b 'c 'd 'e 'f)]
1812 [brk (share-array org
1813 (shape 2 4 1 3)
1814 (^[r k] (values
1815 (+ 6 (* 2 (- r 2)))
1816 (- k 1))))]
1817 [swp (share-array org
1818 (shape 3 5 5 7)
1819 (^[r k] (values
1820 (+ 7 (- r 3))
1821 (- 1 (- k 5)))))]
1822 [box (share-array swp
1823 (shape 0 1 2 3 4 5 6 7 8 9)
1824 (^ _ (values 4 6)))]
1825 [org-contents (^[] (list (array-ref org 6 0) (array-ref org 6 1)
1826 (array-ref org 7 0) (array-ref org 7 1)
1827 (array-ref org 8 0) (array-ref org 8 1)))]
1828 [brk-contents (^[] (list (array-ref brk 2 1) (array-ref brk 2 2)
1829 (array-ref brk 3 1) (array-ref brk 3 2)))]
1830 [swp-contents (^[] (list (array-ref swp 3 5) (array-ref swp 3 6)
1831 (array-ref swp 4 5) (array-ref swp 4 6)))]
1832 [box-contents (^[] (list (array-ref box 0 2 4 6 8)))])
1833 (test "org-contents" '(a b c d e f) org-contents)
1834 (test "brk-contents" '(a b e f) brk-contents)
1835 (test "swp-contents" '(d c f e) swp-contents)
1836 (test "box-contents" '(e) box-contents)
1837 (begin (array-set! org 6 0 'x) #t)
1838 (test "org-contents" '(x b c d e f) org-contents)
1839 (test "brk-contents" '(x b e f) brk-contents)
1840 (test "swp-contents" '(d c f e) swp-contents)
1841 (test "box-contents" '(e) box-contents)
1842 (begin (array-set! brk 3 1 'y) #t)
1843 (test "org-contents" '(x b c d y f) org-contents)
1844 (test "brk-contents" '(x b y f) brk-contents)
1845 (test "swk-contents" '(d c f y) swp-contents)
1846 (test "box-contents" '(y) box-contents)
1847 (begin (array-set! swp 4 5 'z) #t)
1848 (test "org-contents" '(x b c d y z) org-contents)
1849 (test "brk-contents" '(x b y z) brk-contents)
1850 (test "swp-contents" '(d c z y) swp-contents)
1851 (test "box-contents" '(y) box-contents)
1852 (begin (array-set! box 0 2 4 6 8 'e) #t)
1853 (test "org-contents" '(x b c d e z) org-contents)
1854 (test "brk-contents" '(x b e z) brk-contents)
1855 (test "swp-contents" '(d c z e) swp-contents)
1856 (test "box-contents" '(e) box-contents)
1857 )
1858
1859 ;;; Check that arrays copy the shape specification
1860
1861 (test-section "array-set! of shape")
1862
1863 (let1 shp (shape 10 12)
1864 (let ([arr (make-array shp)]
1865 [ars (array shp * *)]
1866 [art (share-array (make-array shp) shp identity)])
1867 (array-set! shp 0 0 '?)
1868 (array-set! shp 0 1 '!)
1869 (test* "modifying array shape"
1870 '(2 0 1 0 2 ? ! 1 10 12 1 10 12 1 10 12)
1871 (list (array-rank shp)
1872 (array-start shp 0)
1873 (array-end shp 0)
1874 (array-start shp 1)
1875 (array-end shp 1)
1876 (array-ref shp 0 0)
1877 (array-ref shp 0 1)
1878 (array-rank arr)
1879 (array-start arr 0)
1880 (array-end arr 0)
1881 (array-rank ars)
1882 (array-start ars 0)
1883 (array-end ars 0)
1884 (array-rank art)
1885 (array-start art 0)
1886 (array-end art 0)))
1887 ))
1888 ;;; Check that index arrays work even when they share
1889 ;;;
1890 ;;; arr ixn
1891 ;;; 5 6 0 1
1892 ;;; 4 nw ne 0 4 6
1893 ;;; 5 sw se 1 5 4
1894
1895 (test-section "array access with sharing index array")
1896 (let ([arr (array (shape 4 6 5 7) 'nw 'ne 'sw 'se)]
1897 [ixn (array (shape 0 2 0 2) 4 6 5 4)])
1898 (let ([col0 (share-array ixn
1899 (shape 0 2)
1900 (^k (values k 0)))]
1901 [row0 (share-array ixn
1902 (shape 0 2)
1903 (^k (values 0 k)))]
1904 [wor1 (share-array ixn
1905 (shape 0 2)
1906 (^k (values 1 (- 1 k))))]
1907 [cod (share-array ixn
1908 (shape 0 2)
1909 (^k (case k
1910 [(0) (values 1 0)]
1911 [(1) (values 0 1)])))]
1912 [box (share-array ixn
1913 (shape 0 2)
1914 (^k (values 1 0)))])
1915 (test* "array-ref before change"
1916 '(nw ne nw se sw)
1917 (list (array-ref arr col0)
1918 (array-ref arr row0)
1919 (array-ref arr wor1)
1920 (array-ref arr cod)
1921 (array-ref arr box)))
1922 (array-set! arr col0 'ul)
1923 (array-set! arr row0 'ur)
1924 (array-set! arr cod 'lr)
1925 (array-set! arr box 'll)
1926 (test* "array-ref after change"
1927 '(ul ur ll lr)
1928 (list (array-ref arr 4 5)
1929 (array-ref arr 4 6)
1930 (array-ref arr 5 5)
1931 (array-ref arr 5 6)))
1932 (array-set! arr wor1 'xx)
1933 (test* "array-ref after change" 'xx
1934 (array-ref arr 4 5))
1935 ))
1936
1937 ;;; Check that shape arrays work even when they share
1938 ;;;
1939 ;;; arr shp shq shr shs
1940 ;;; 1 2 3 4 0 1 0 1 0 1 0 1
1941 ;;; 1 10 12 16 20 0 10 12 0 12 20 0 10 10 0 12 12
1942 ;;; 2 10 11 12 13 1 10 11 1 11 13 1 11 12 1 12 12
1943 ;;; 2 12 16
1944 ;;; 3 13 20
1945
1946 (test-section "sharing shape array")
1947 (let1 arr (array (shape 1 3 1 5) 10 12 16 20 10 11 12 13)
1948 (let ([shp (share-array arr
1949 (shape 0 2 0 2)
1950 (^[r k] (values (+ r 1) (+ k 1))))]
1951 [shq (share-array arr
1952 (shape 0 2 0 2)
1953 (^[r k] (values (+ r 1) (* 2 (+ 1 k)))))]
1954 [shr (share-array arr
1955 (shape 0 4 0 2)
1956 (^[r k] (values (- 2 k) (+ r 1))))]
1957 [shs (share-array arr
1958 (shape 0 2 0 2)
1959 (^[r k] (values 2 3)))])
1960 (test* "using make-array shp"
1961 '(2 10 12 10 11)
1962 (let ((arr-p (make-array shp)))
1963 (list (array-rank arr-p)
1964 (array-start arr-p 0)
1965 (array-end arr-p 0)
1966 (array-start arr-p 1)
1967 (array-end arr-p 1))))
1968 (test* "using array shq"
1969 '(2 12 20 11 13)
1970 (let1 arr-q (array shq * * * * * * * * * * * * * * * *)
1971 (list (array-rank arr-q)
1972 (array-start arr-q 0)
1973 (array-end arr-q 0)
1974 (array-start arr-q 1)
1975 (array-end arr-q 1))))
1976 (test* "using share-array"
1977 '(4 10 10 11 12 12 16 13 20)
1978 (let1 arr-r (share-array (array (shape) *)
1979 shr
1980 (^ _ (values)))
1981 (list (array-rank arr-r)
1982 (array-start arr-r 0)
1983 (array-end arr-r 0)
1984 (array-start arr-r 1)
1985 (array-end arr-r 1)
1986 (array-start arr-r 2)
1987 (array-end arr-r 2)
1988 (array-start arr-r 3)
1989 (array-end arr-r 3))))
1990 (test* "using make-array shs"
1991 '(2 12 12 12 12)
1992 (let1 arr-s (make-array shs)
1993 (list (array-rank arr-s)
1994 (array-start arr-s 0)
1995 (array-end arr-s 0)
1996 (array-start arr-s 1)
1997 (array-end arr-s 1))))
1998 ))
1999
2000 (test-section "sharing with sharing subshape")
2001 (let ([super (array (shape 4 7 4 7)
2002 1 * *
2003 * 2 *
2004 * * 3)]
2005 [subshape (share-array (array (shape 0 2 0 3)
2006 * 4 *
2007 * 7 *)
2008 (shape 0 1 0 2)
2009 (^[r k] (values k 1)))])
2010 (let1 sub (share-array super subshape (^k (values k k)))
2011 (test* "subshape check" #t
2012 (equal? subshape (shape 4 7)))
2013 (test* "sharing subshape" '(2 0 1 0 2 4 7)
2014 (list (array-rank subshape)
2015 (array-start subshape 0)
2016 (array-end subshape 0)
2017 (array-start subshape 1)
2018 (array-end subshape 1)
2019 (array-ref subshape 0 0)
2020 (array-ref subshape 0 1)))
2021 (test* "sub check" #t
2022 (equal? sub (array (shape 4 7) 1 2 3)))
2023 (test* "sharing with sharing subshape" '(1 4 7 1 2 3)
2024 (list (array-rank sub)
2025 (array-start sub 0)
2026 (array-end sub 0)
2027 (array-ref sub 4)
2028 (array-ref sub 5)
2029 (array-ref sub 6)))
2030 ))
2031
2032 (test-section "subarray")
2033 (let ([super (array (shape 0 3 0 3)
2034 1 * *
2035 * 2 *
2036 * * 3)]
2037 [subshape (shape 1 3 1 3)])
2038 (let1 sub (subarray super subshape)
2039 (test* "sub check" #t
2040 (equal? sub (array (shape 0 2 0 2) 2 * * 3)))
2041 (test* "sharing with sharing subshape" (list 2 0 2 2 * * 3)
2042 (list (array-rank sub)
2043 (array-start sub 0)
2044 (array-end sub 0)
2045 (array-ref sub 0 0)
2046 (array-ref sub 0 1)
2047 (array-ref sub 1 0)
2048 (array-ref sub 1 1)))
2049 ))
2050
2051 ;;----------------------------------------------------------------
2052 (test-section "array-iteration")
2053
2054 (test* "array-for-each-index (list)" '((0 0) (0 1) (1 0) (1 1))
2055 (let ([ar (make-array (shape 0 2 0 2))]
2056 [ls '()])
2057 (array-for-each-index ar (^[a b] (push! ls (list a b))))
2058 (reverse ls)))
2059
2060 (test* "array-for-each-index (vector)" '(#(0 0) #(0 1) #(1 0) #(1 1))
2061 (let ([ar (make-array (shape 0 2 0 2))]
2062 [ls '()]
2063 [vec (make-vector 2)])
2064 (array-for-each-index ar (^v (push! ls (vector-copy v))) vec)
2065 (reverse ls)))
2066
2067 (test* "array-for-each-index (array)" '(#(0 0) #(0 1) #(1 0) #(1 1))
2068 (let ([ar (make-array (shape 0 2 0 2))]
2069 [ls '()]
2070 [ind (make-array (shape 0 2))])
2071 (array-for-each-index ar (^a (push! ls (array->vector a))) ind)
2072 (reverse ls)))
2073
2074 (test* "array-for-each-index-by-dimension (list)" '(0 1)
2075 (let ([ar (make-array (shape 0 2 5 7))]
2076 [ls '()])
2077 (array-for-each-index-by-dimension ar '(0) (^[a b] (push! ls a)))
2078 (reverse ls)))
2079
2080 (test* "array-for-each-index-by-dimension (vector)" '(#(3 5) #(3 6) #(3 7))
2081 (let ([ar (make-array (shape 0 2 5 8))]
2082 [ls '()])
2083 (array-for-each-index-by-dimension
2084 ar '(1) (^v (push! ls (vector-copy v))) (vector 3 4))
2085 (reverse ls)))
2086
2087 (test* "array-for-each-index-by-dimension (array)" '(#(5) #(6) #(7))
2088 (let ([ar (make-array (shape 5 8 0 2))]
2089 [ls '()])
2090 (array-for-each-index-by-dimension
2091 ar '(0) (^a (push! ls (array->vector a))) (make-array (shape 0 1)))
2092 (reverse ls)))
2093
2094 (test* "shape-for-each (list)" '((0 0) (0 1) (1 0) (1 1))
2095 (let ([sh (shape 0 2 0 2)]
2096 [ls '()])
2097 (shape-for-each sh (^[a b] (push! ls (list a b))))
2098 (reverse ls)))
2099
2100 (test* "shape-for-each (vector)" '(#(0 0) #(0 1) #(1 0) #(1 1))
2101 (let ([sh (shape 0 2 0 2)]
2102 [ls '()]
2103 [vec (make-vector 2)])
2104 (shape-for-each sh (^v (push! ls (vector-copy v))) vec)
2105 (reverse ls)))
2106
2107 (test* "shape-for-each (array)" '(#(0 0) #(0 1) #(1 0) #(1 1))
2108 (let ([sh (shape 0 2 0 2)]
2109 [ls '()]
2110 [ind (make-array (shape 0 2))])
2111 (shape-for-each sh (^a (push! ls (array->vector a))) ind)
2112 (reverse ls)))
2113
2114 (test* "tabulate-array (list)" #,(<array> (1 3 1 3) 11 12 21 22)
2115 (tabulate-array (shape 1 3 1 3) (^[a b] (+ (* a 10) b))))
2116
2117 (test* "tabulate-array (vector)" #,(<array> (2 4 2 4) 12 31 17 36)
2118 (let ([vec (make-vector 2)]
2119 [square (^x (* x x))]
2120 [cube (^x (* x x x))])
2121 (tabulate-array (shape 2 4 2 4) (^v (+ (square (ref v 0))
2122 (cube (ref v 1))))
2123 vec)))
2124
2125 (test* "array-retabulate! (list)" #,(<array> (0 2 0 2) 1.0 0.5 0.25 0.125)
2126 (rlet1 ar #,(<array> (0 2 0 2) 1 2 4 8)
2127 (array-retabulate! ar (^[i j] (/ 1.0 (array-ref ar i j))))))
2128
2129 (test* "array-retabulate! (vector)" #,(<array> (0 2 0 2) 1.0 0.5 0.25 0.125)
2130 (let ([ar #,(<array> (0 2 0 2) 1 2 4 8)]
2131 [vec (make-vector 2)])
2132 (array-retabulate! ar (^v (/ 1.0 (array-ref ar v))) vec)
2133 ar))
2134
2135 (test* "array-retabulate! (uniform)" #,(<f64array> (0 2 0 2) 1.0 0.5 0.25 0.125)
2136 (let ([ar #,(<f64array> (0 2 0 2) 1 2 4 8)]
2137 [vec (make-vector 2)])
2138 (array-retabulate! ar (^v (/ 1.0 (array-ref ar v))) vec)
2139 ar))
2140
2141 (test* "array-map-1" #,(<array> (0 2 0 2) 1 4 9 16)
2142 (array-map (^x (* x x)) #,(<array> (0 2 0 2) 1 2 3 4)))
2143
2144 (test* "array-map-2" #,(<array> (0 2 0 2) 11 22 33 44)
2145 (array-map (^[a b] (+ a b))
2146 #,(<array> (0 2 0 2) 1 2 3 4)
2147 #,(<array> (0 2 0 2) 10 20 30 40)))
2148
2149 (test* "array-map-3" #,(<array> (0 2 0 2) 11.1 22.2 33.3 44.4)
2150 (array-map (lambda (a b c) (+ a b c))
2151 #,(<array> (0 2 0 2) 1 2 3 4)
2152 #,(<array> (0 2 0 2) 10 20 30 40)
2153 #,(<array> (0 2 0 2) .1 .2 .3 .4)))
2154
2155 (test* "array-map-4" #,(<array> (0 2 0 2) 111.0 22.2 3.33 .444)
2156 (array-map (lambda (a b c d) (* d (+ a b c)))
2157 #,(<array> (0 2 0 2) 1 2 3 4)
2158 #,(<array> (0 2 0 2) 10 20 30 40)
2159 #,(<array> (0 2 0 2) .1 .2 .3 .4)
2160 #,(<array> (0 2 0 2) 10.0 1.0 0.1 0.01)))
2161
2162 (test* "array-map-uniform" #,(<array> (0 2 0 2) 111.0 22.2 3.33 .444)
2163 (array-map (lambda (a b c d) (/ (truncate (* (* d (+ a b c)) 10000)) 10000))
2164 #,(<u8array> (0 2 0 2) 1 2 3 4)
2165 #,(<s32array> (0 2 0 2) 10 20 30 40)
2166 #,(<f32array> (0 2 0 2) .1 .2 .3 .4)
2167 #,(<f64array> (0 2 0 2) 10.0 1.0 0.1 0.01)))
2168
2169 (test* "array-every-1" #t
2170 (array-every even? #,(<array> (0 0))))
2171 (test* "array-every-2" #t
2172 (array-every even? #,(<array> () 2)))
2173 (test* "array-every-3" #t
2174 (array-every even? #,(<array> (0 2 0 2) 2 4 6 8)))
2175 (test* "array-every-4" #f
2176 (array-every even? #,(<array> () 3)))
2177 (test* "array-every-5" #f
2178 (array-every even? #,(<array> (0 2 0 2) 2 4 3 8)))
2179
2180 (test* "array-any-1" #t
2181 (array-any even? #,(<array> () 2)))
2182 (test* "array-any-2" #t
2183 (array-any even? #,(<array> (0 2 0 2) 1 2 3 8)))
2184 (test* "array-any-3" #f
2185 (array-any even? #,(<array> (0 0))))
2186 (test* "array-any-4" #f
2187 (array-any even? #,(<array> () 3)))
2188 (test* "array-any-5" #f
2189 (array-any even? #,(<array> (0 2 0 2) 1 3 5 7)))
2190
2191 (test-section "matrices")
2192
2193 ;; zero-base the shape
2194 (define (array-normalize a)
2195 (define (every-other pred ls)
2196 (if (null? ls) #t (and (pred (car ls)) (every-other pred (cddr ls)))))
2197 (if (every-other zero? (array->list (array-shape a)))
2198 a
2199 (subarray a (array-shape a))))
2200
2201 (define (approx-equal? x y . opt)
2202 (< (abs (- x y)) (get-optional opt 0.0000001)))
2203
2204 (define (array-approx-equal? a b)
2205 (or (eq? a b)
2206 (and (eq? (class-of a) (class-of b))
2207 (array-equal? a b approx-equal?))))
2208
2209 (let1 i 0
2210 (for-each
2211 (^t (let-optionals* t (ar (inv #f) (det 0))
2212 (test* (format "array-inverse-~D" (inc! i)) inv
2213 (array-inverse ar)
2214 array-approx-equal?)
2215 (when (and inv (eq? (class-of ar) (class-of inv)))
2216 (test* (format "array-inverse-~D" (inc! i)) (array-normalize ar)
2217 (array-inverse inv)
2218 array-approx-equal?))
2219 (test* (format "determinant-~D" (inc! i)) det
2220 (determinant ar)
2221 approx-equal?)))
2222 '((#,(<array> (0 2 0 2) 1 2 3 4)
2223 #,(<array> (0 2 0 2) -2.0 1.0 1.5 -0.5)
2224 -2)
2225 (#,(<array> (1 3 1 3) 1 2 3 4)
2226 #,(<array> (0 2 0 2) -2.0 1.0 1.5 -0.5)
2227 -2)
2228 (#,(<array> (3 5 7 9) 1 2 3 4)
2229 #,(<array> (0 2 0 2) -2.0 1.0 1.5 -0.5)
2230 -2)
2231 (#,(<array> (0 3 0 3) 1 5 2 1 1 7 0 -3 4)
2232 #,(<array> (0 3 0 3) -25 26 -33 4 -4 5 3 -3 4)
2233 -1)
2234 (#,(<array> (0 3 0 3) 2 0 1 1 1 0 3 2 1)
2235 #,(<array> (0 3 0 3) 1 2 -1 -1 -1 1 -1 -4 2)
2236 1)
2237 (#,(<array> (0 3 0 3) 1 -1 3 2 1 2 -2 -2 1)
2238 #,(<array> (0 3 0 3) 1 -1 -1 -1.2 1.4 0.8 -0.4 0.8 0.6)
2239 5)
2240 (#,(<array> (0 2 0 2) 1 2 3 6))
2241 (#,(<u8array> (0 2 0 2) 1 2 3 4)
2242 #,(<array> (0 2 0 2) -2.0 1.0 1.5 -0.5)
2243 -2)
2244 (#,(<s16array> (0 2 0 2) 1 2 3 4)
2245 #,(<array> (0 2 0 2) -2.0 1.0 1.5 -0.5)
2246 -2)
2247 (#,(<f32array> (0 2 0 2) 1 2 3 4)
2248 #,(<f32array> (0 2 0 2) -2.0 1.0 1.5 -0.5)
2249 -2)
2250 (#,(<f64array> (0 2 0 2) 1 2 3 4)
2251 #,(<f64array> (0 2 0 2) -2.0 1.0 1.5 -0.5)
2252 -2)
2253 )))
2254
2255 (let ((i 0))
2256 (for-each
2257 (^t (let-optionals* t (a b c)
2258 (test* (format "array-mul-~D" (inc! i)) c
2259 (array-mul a b)
2260 array-approx-equal?)))
2261 '((#,(<array> (0 2 0 2) 1 2 3 4)
2262 #,(<array> (0 2 0 2) 4 3 2 1)
2263 #,(<array> (0 2 0 2) 8 5 20 13))
2264 (#,(<array> (1 3 1 3) 1 1 1 1)
2265 #,(<array> (2 4 2 4) 2 2 2 2)
2266 #,(<array> (0 2 0 2) 4 4 4 4))
2267 (#,(<u8array> (3 5 7 9) 3 1 4 1)
2268 #,(<u8array> (55 57 17 19) 2 7 8 1)
2269 #,(<u8array> (0 2 0 2) 14 22 16 29))
2270 (#,(<s16array> (3 4 1 9) 1 -2 3 -4 5 -6 7 -8)
2271 #,(<s16array> (3 11 5 6) 1 -2 3 -4 5 -6 7 -8)
2272 #,(<s16array> (0 1 0 1) 204))
2273 (#,(<f32array> (0 2 0 2) 1 2 3 4)
2274 #,(<f32array> (0 2 0 2) 4 3 2 1)
2275 #,(<f32array> (0 2 0 2) 8 5 20 13))
2276 (#,(<f64array> (0 2 0 2) 1 2 3 4)
2277 #,(<f64array> (0 2 0 2) 4 3 2 1)
2278 #,(<f64array> (0 2 0 2) 8 5 20 13))
2279 (#,(<f64array> (0 2 0 3) 1 2 3 4 5 6)
2280 #,(<f64array> (0 3 0 2) 1 2 3 4 5 6)
2281 #,(<f64array> (0 2 0 2) 22 28 49 64))
2282 )))
2283
2284 (test* "array-vector-mul"
2285 '#s32(3 5 7 9)
2286 (array-vector-mul '#,(<u32array> (0 4 0 2) 1 2 3 4 5 6 7 8)
2287 '#s32(-1 2)))
2288
2289 (test* "vector-array-mul"
2290 '#s32(1 2)
2291 (vector-array-mul '#s32(1 -1 2 -1)
2292 '#,(<u32array> (0 4 0 2) 1 2 3 4 5 6 7 8)))
2293
2294 (let ((i 0))
2295 (for-each
2296 (^t (let-optionals* t (a pow b)
2297 (test* (format "array-expt-~D" (inc! i)) b
2298 (array-expt a pow)
2299 array-approx-equal?)))
2300 '((#,(<array> (0 2 0 2) 1 2 3 4)
2301 0
2302 #,(<array> (0 2 0 2) 1 0 0 1))
2303 (#,(<u8array> (0 2 0 2) 1 2 3 4)
2304 0
2305 #,(<u8array> (0 2 0 2) 1 0 0 1))
2306 (#,(<s16array> (0 2 0 2) 1 2 3 4)
2307 0
2308 #,(<s16array> (0 2 0 2) 1 0 0 1))
2309 (#,(<f32array> (0 2 0 2) 1 2 3 4)
2310 0
2311 #,(<f32array> (0 2 0 2) 1 0 0 1))
2312 (#,(<f64array> (0 2 0 2) 1 2 3 4)
2313 0
2314 #,(<f64array> (0 2 0 2) 1 0 0 1))
2315 (#,(<array> (0 2 0 2) 1 2 3 4)
2316 2
2317 #,(<array> (0 2 0 2) 7 10 15 22))
2318 (#,(<u8array> (0 2 0 2) 1 2 3 4)
2319 2
2320 #,(<u8array> (0 2 0 2) 7 10 15 22))
2321 (#,(<s16array> (0 2 0 2) 1 2 3 4)
2322 2
2323 #,(<s16array> (0 2 0 2) 7 10 15 22))
2324 (#,(<f32array> (0 2 0 2) 1 2 3 4)
2325 2
2326 #,(<f32array> (0 2 0 2) 7 10 15 22))
2327 (#,(<f64array> (0 2 0 2) 1 2 3 4)
2328 2
2329 #,(<f64array> (0 2 0 2) 7 10 15 22))
2330 )))
2331
2332 (let ((i 0))
2333 (for-each
2334 (^t (let-optionals* t (a b c)
2335 (test* (format "array-div-left-~D" (inc! i)) c
2336 (array-div-left a b)
2337 array-approx-equal?)))
2338 '((#,(<array> (0 2 0 2) 1 2 3 4)
2339 #,(<array> (0 2 0 2) 5 6 7 8)
2340 #,(<array> (0 2 0 2) 5 4 -4 -3))
2341 (#,(<u8array> (0 2 0 2) 1 2 3 4)
2342 #,(<u8array> (0 2 0 2) 5 6 7 8)
2343 #,(<array> (0 2 0 2) 5 4 -4 -3))
2344 (#,(<s16array> (0 2 0 2) 1 2 3 4)
2345 #,(<s16array> (0 2 0 2) 5 6 7 8)
2346 #,(<array> (0 2 0 2) 5 4 -4 -3))
2347 (#,(<f32array> (0 2 0 2) 1 2 3 4)
2348 #,(<f32array> (0 2 0 2) 5 6 7 8)
2349 #,(<f32array> (0 2 0 2) 5 4 -4 -3))
2350 (#,(<f64array> (0 2 0 2) 1 2 3 4)
2351 #,(<f64array> (0 2 0 2) 5 6 7 8)
2352 #,(<f64array> (0 2 0 2) 5 4 -4 -3))
2353 )))
2354
2355 (let ((i 0))
2356 (for-each
2357 (^t (let-optionals* t (a b c)
2358 (test* (format "array-div-right-~D" (inc! i)) c
2359 (array-div-right a b)
2360 array-approx-equal?)))
2361 '((#,(<array> (0 2 0 2) 1 2 3 4)
2362 #,(<array> (0 2 0 2) 5 6 7 8)
2363 #,(<array> (0 2 0 2) 3 -2 2 -1))
2364 (#,(<u8array> (0 2 0 2) 1 2 3 4)
2365 #,(<u8array> (0 2 0 2) 5 6 7 8)
2366 #,(<array> (0 2 0 2) 3 -2 2 -1))
2367 (#,(<s16array> (0 2 0 2) 1 2 3 4)
2368 #,(<s16array> (0 2 0 2) 5 6 7 8)
2369 #,(<array> (0 2 0 2) 3 -2 2 -1))
2370 (#,(<f32array> (0 2 0 2) 1 2 3 4)
2371 #,(<f32array> (0 2 0 2) 5 6 7 8)
2372 #,(<f32array> (0 2 0 2) 3 -2 2 -1))
2373 (#,(<f64array> (0 2 0 2) 1 2 3 4)
2374 #,(<f64array> (0 2 0 2) 5 6 7 8)
2375 #,(<f64array> (0 2 0 2) 3 -2 2 -1))
2376 )))
2377
2378 (let ((i1 0) (i2 0))
2379 (for-each
2380 (^t (let-optionals* t (ans a . rest)
2381 (unless (number? a)
2382 (test* (format "array-add-elements-~D" (inc! i1)) ans
2383 (apply array-add-elements a rest)
2384 array-approx-equal?))
2385 (test* (format "array-add-elements!-~D" (inc! i2)) ans
2386 (apply array-add-elements!
2387 (if (number? a) a (array-copy a))
2388 rest)
2389 array-approx-equal?)))
2390 '((#,(<array> (0 2 0 2) 5 5 5 5)
2391 #,(<array> (0 2 0 2) 1 2 3 4)
2392 #,(<array> (0 2 0 2) 4 3 2 1))
2393 (#,(<array> (1 3 1 3) 1 2 3 4)
2394 #,(<array> (1 3 1 3) 1 2 3 4))
2395 (#,(<u8array> (3 5 7 9) 12 17 18 11)
2396 #,(<u8array> (3 5 7 9) 2 7 8 1)
2397 10)
2398 (#,(<s16array> (3 4 1 9) -9 -12 -7 -14 -5 -16 -3 -18)
2399 #,(<s16array> (3 4 1 9) 1 -2 3 -4 5 -6 7 -8)
2400 -10)
2401 (#,(<f32array> (0 2 0 2) 6 7 8 9)
2402 #,(<f32array> (0 2 0 2) 1 2 3 4)
2403 #,(<f32array> (0 2 0 2) 4 3 2 1)
2404 #,(<f32array> (0 2 0 2) 1 2 3 4))
2405 (#,(<f64array> (0 2 0 2) -3 1 1 -3)
2406 #,(<f64array> (0 2 0 2) 1 -2 3 -4)
2407 #,(<f64array> (0 2 0 2) -4 3 -2 1))
2408 (#,(<f64array> (0 2 0 3) 8 10 12 14 16 18)
2409 #,(<f64array> (0 2 0 3) 1 2 3 4 5 6)
2410 #,(<f64array> (0 2 0 3) 7 8 9 10 11 12))
2411 (#,(<f64array> (0 2 0 3) 11 8 13 6 15 4)
2412 10
2413 #,(<f64array> (0 2 0 3) 1 -2 3 -4 5 -6))
2414 )))
2415
2416 (let ((i1 0) (i2 0))
2417 (for-each
2418 (^t (let-optionals* t (ans a . rest)
2419 (unless (number? a)
2420 (test* (format "array-sub-elements-~D" (inc! i1)) ans
2421 (apply array-sub-elements a rest)
2422 array-approx-equal?))
2423 (test* (format "array-sub-elements!-~D" (inc! i2)) ans
2424 (apply array-sub-elements!
2425 (if (number? a) a (array-copy a))
2426 rest)
2427 array-approx-equal?)))
2428 '((#,(<array> (0 2 0 2) -3 -1 1 3)
2429 #,(<array> (0 2 0 2) 1 2 3 4)
2430 #,(<array> (0 2 0 2) 4 3 2 1))
2431 (#,(<array> (1 3 1 3) 1 2 3 4)
2432 #,(<array> (1 3 1 3) 1 2 3 4))
2433 (#,(<u8array> (3 5 7 9) 1 6 7 0)
2434 #,(<u8array> (3 5 7 9) 2 7 8 1)
2435 1)
2436 (#,(<s16array> (3 4 1 9) 11 8 13 6 15 4 17 2)
2437 #,(<s16array> (3 4 1 9) 1 -2 3 -4 5 -6 7 -8)
2438 -10)
2439 (#,(<f32array> (0 2 0 2) -4 -3 -2 -1)
2440 #,(<f32array> (0 2 0 2) 1 2 3 4)
2441 #,(<f32array> (0 2 0 2) 4 3 2 1)
2442 #,(<f32array> (0 2 0 2) 1 2 3 4))
2443 (#,(<f64array> (0 2 0 2) 5 -5 5 -5)
2444 #,(<f64array> (0 2 0 2) 1 -2 3 -4)
2445 #,(<f64array> (0 2 0 2) -4 3 -2 1))
2446 (#,(<f64array> (0 2 0 3) -6 -6 -6 -6 -6 -6)
2447 #,(<f64array> (0 2 0 3) 1 2 3 4 5 6)
2448 #,(<f64array> (0 2 0 3) 7 8 9 10 11 12))
2449 (#,(<f64array> (0 2 0 3) 9 12 7 14 5 16)
2450 10
2451 #,(<f64array> (0 2 0 3) 1 -2 3 -4 5 -6))
2452 )))
2453
2454 (let ((i1 0) (i2 0))
2455 (for-each
2456 (^t (let-optionals* t (ans a . rest)
2457 (unless (number? a)
2458 (test* (format "array-mul-elements-~D" (inc! i1)) ans
2459 (apply array-mul-elements a rest)
2460 array-approx-equal?))
2461 (test* (format "array-mul-elements!-~D" (inc! i2)) ans
2462 (apply array-mul-elements!
2463 (if (number? a) a (array-copy a))
2464 rest)
2465 array-approx-equal?)))
2466 '((#,(<array> (0 2 0 2) 4 6 6 4)
2467 #,(<array> (0 2 0 2) 1 2 3 4)
2468 #,(<array> (0 2 0 2) 4 3 2 1))
2469 (#,(<array> (1 3 1 3) 1 2 3 4)
2470 #,(<array> (1 3 1 3) 1 2 3 4))
2471 (#,(<u8array> (3 5 7 9) 20 70 80 10)
2472 #,(<u8array> (3 5 7 9) 2 7 8 1)
2473 10)
2474 (#,(<s16array> (3 4 1 9) -10 20 -30 40 -50 60 -70 80)
2475 #,(<s16array> (3 4 1 9) 1 -2 3 -4 5 -6 7 -8)
2476 -10)
2477 (#,(<f32array> (0 2 0 2) 4 12 18 16)
2478 #,(<f32array> (0 2 0 2) 1 2 3 4)
2479 #,(<f32array> (0 2 0 2) 4 3 2 1)
2480 #,(<f32array> (0 2 0 2) 1 2 3 4))
2481 (#,(<f64array> (0 2 0 2) -4 -6 -6 -4)
2482 #,(<f64array> (0 2 0 2) 1 -2 3 -4)
2483 #,(<f64array> (0 2 0 2) -4 3 -2 1))
2484 (#,(<f64array> (0 2 0 3) 7 16 27 40 55 72)
2485 #,(<f64array> (0 2 0 3) 1 2 3 4 5 6)
2486 #,(<f64array> (0 2 0 3) 7 8 9 10 11 12))
2487 (#,(<f64array> (0 2 0 3) 10 -20 30 -40 50 -60)
2488 10
2489 #,(<f64array> (0 2 0 3) 1 -2 3 -4 5 -6))
2490 )))
2491
2492 (let ((i1 0) (i2 0))
2493 (for-each
2494 (^t (let-optionals* t (ans a . rest)
2495 (unless (number? a)
2496 (test* (format "array-div-elements-~D" (inc! i1)) ans
2497 (apply array-div-elements a rest)
2498 array-approx-equal?))
2499 (test* (format "array-div-elements!-~D" (inc! i2)) ans
2500 (apply array-div-elements!
2501 (if (number? a) a (array-copy a))
2502 rest)
2503 array-approx-equal?)))
2504 '((#,(<array> (0 2 0 2) 1/4 2/3 3/2 4/1)
2505 #,(<array> (0 2 0 2) 1 2 3 4)
2506 #,(<array> (0 2 0 2) 4 3 2 1))
2507 (#,(<array> (1 3 1 3) 1 2 3 4)
2508 #,(<array> (1 3 1 3) 1 2 3 4))
2509 (#,(<u8array> (3 5 7 9) 2/10 7/10 8/10 1/10)
2510 #,(<u8array> (3 5 7 9) 2 7 8 1)
2511 10)
2512 (#,(<s16array> (3 4 1 9) -1/10 2/10 -3/10 4/10 -5/10 6/10 -7/10 8/10)
2513 #,(<s16array> (3 4 1 9) 1 -2 3 -4 5 -6 7 -8)
2514 -10)
2515 (#,(<f32array> (0 2 0 2) 1/4 2/6 3/6 4/4)
2516 #,(<f32array> (0 2 0 2) 1 2 3 4)
2517 #,(<f32array> (0 2 0 2) 4 3 2 1)
2518 #,(<f32array> (0 2 0 2) 1 2 3 4))
2519 (#,(<f64array> (0 2 0 2) -1/4 -2/3 -3/2 -4/1)
2520 #,(<f64array> (0 2 0 2) 1 -2 3 -4)
2521 #,(<f64array> (0 2 0 2) -4 3 -2 1))
2522 (#,(<f64array> (0 2 0 3) 1/7 2/8 3/9 4/10 5/11 6/12)
2523 #,(<f64array> (0 2 0 3) 1 2 3 4 5 6)
2524 #,(<f64array> (0 2 0 3) 7 8 9 10 11 12))
2525 (#,(<f64array> (0 2 0 3) 10/1 -10/2 10/3 -10/4 10/5 -10/6)
2526 10
2527 #,(<f64array> (0 2 0 3) 1 -2 3 -4 5 -6))
2528 )))
2529
2530 (let ((i 0))
2531 (for-each
2532 (^t (let-optionals* t (a b)
2533 (test* (format "array-transpose-~D" (inc! i)) b
2534 (array-transpose a)
2535 array-approx-equal?)))
2536 '((#,(<array> (0 2 0 2) 1 2 3 4)
2537 #,(<array> (0 2 0 2) 1 3 2 4))
2538 (#,(<u8array> (0 2 0 2) 1 2 3 4)
2539 #,(<u8array> (0 2 0 2) 1 3 2 4))
2540 (#,(<s16array> (0 2 0 2) 1 2 3 4)
2541 #,(<s16array> (0 2 0 2) 1 3 2 4))
2542 (#,(<f32array> (0 2 0 2) 1 2 3 4)
2543 #,(<f32array> (0 2 0 2) 1 3 2 4))
2544 (#,(<f64array> (0 2 0 2) 1 2 3 4)
2545 #,(<f64array> (0 2 0 2) 1 3 2 4))
2546 (#,(<f64array> (0 2 0 3) 1 2 3 4 5 6)
2547 #,(<f64array> (0 3 0 2) 1 4 2 5 3 6))
2548 )))
2549
2550 (let ((i 0))
2551 (for-each
2552 (^t (let-optionals* t (a b)
2553 (test* (format "array-rotate-90-~D" (inc! i)) b
2554 (array-rotate-90 a)
2555 array-approx-equal?)))
2556 '((#,(<array> (0 2 0 2) 1 2 3 4)
2557 #,(<array> (0 2 0 2) 3 1 4 2))
2558 (#,(<u8array> (0 2 0 2) 1 2 3 4)
2559 #,(<u8array> (0 2 0 2) 3 1 4 2))
2560 (#,(<s16array> (0 2 0 2) 1 2 3 4)
2561 #,(<s16array> (0 2 0 2) 3 1 4 2))
2562 (#,(<f32array> (0 2 0 2) 1 2 3 4)
2563 #,(<f32array> (0 2 0 2) 3 1 4 2))
2564 (#,(<f64array> (0 2 0 2) 1 2 3 4)
2565 #,(<f64array> (0 2 0 2) 3 1 4 2))
2566 (#,(<f64array> (0 2 0 3) 1 2 3 4 5 6)
2567 #,(<f64array> (0 3 0 2) 4 1 5 2 6 3))
2568 )))
2569
2570 (let ((i1 0) (i2 0))
2571 (for-each
2572 (^t (let-optionals* t (a b)
2573 (test* (format "array-flip-~D" (inc! i1)) b
2574 (array-flip a)
2575 array-approx-equal?)
2576 (test* (format "array-flip!-~D" (inc! i2)) b
2577 (array-flip! (array-copy a))
2578 array-approx-equal?)))
2579 '((#,(<array> (0 2 0 2) 1 2 3 4)
2580 #,(<array> (0 2 0 2) 3 4 1 2))
2581 (#,(<u8array> (0 2 0 2) 1 2 3 4)
2582 #,(<u8array> (0 2 0 2) 3 4 1 2))
2583 (#,(<s16array> (0 2 0 2) 1 2 3 4)
2584 #,(<s16array> (0 2 0 2) 3 4 1 2))
2585 (#,(<f32array> (0 2 0 2) 1 2 3 4)
2586 #,(<f32array> (0 2 0 2) 3 4 1 2))
2587 (#,(<f64array> (0 2 0 2) 1 2 3 4)
2588 #,(<f64array> (0 2 0 2) 3 4 1 2))
2589 )))
2590
2591 (let ((i1 0) (i2 0))
2592 (for-each
2593 (^t (let-optionals* t (a b)
2594 (test* (format "array-negate-elements-~D" (inc! i1)) b
2595 (array-negate-elements a)
2596 array-approx-equal?)
2597 (test* (format "array-negate-elements!-~D" (inc! i2)) b
2598 (array-negate-elements! (array-copy a))
2599 array-approx-equal?)))
2600 '((#,(<array> (0 2 0 2) 1 2 3 4)
2601 #,(<array> (0 2 0 2) -1 -2 -3 -4))
2602 (#,(<s8array> (0 2 0 2) 1 2 3 4)
2603 #,(<s8array> (0 2 0 2) -1 -2 -3 -4))
2604 (#,(<f32array> (0 2 0 2) 1 2 3 4)
2605 #,(<f32array> (0 2 0 2) -1 -2 -3 -4))
2606 )))
2607
2608 (let ((i1 0) (i2 0))
2609 (for-each
2610 (^t (let-optionals* t (a b)
2611 (test* (format "array-reciprocate-elements~D" (inc! i1)) b
2612 (array-reciprocate-elements a)
2613 array-approx-equal?)
2614 (test* (format "array-reciprocate-elements!-~D" (inc! i2)) b
2615 (array-reciprocate-elements! (array-copy a))
2616 array-approx-equal?)))
2617 '((#,(<array> (0 2 0 2) 1 2 3 4)
2618 #,(<array> (0 2 0 2) 1 1/2 1/3 1/4))
2619 (#,(<f64array> (0 2 0 2) 1 2 3 4)
2620 #,(<f64array> (0 2 0 2) 1 1/2 1/3 1/4))
2621 )))
2622
2623 ;;-------------------------------------------------------------------
2624 ;; NB: copy-port uses read-block! and write-block for block copy,
2625 ;; so we test it here.
2626 (test-section "copy-port")
2627
2628 (define s (make-string 10000 #\z))
2629
2630 (test* "copy-port (default)" #t
2631 (equal? s (call-with-string-io s (^[in out] (copy-port in out)))))
2632
2633 (test* "copy-port (unit byte)" #t
2634 (equal? s (call-with-string-io s (^[in out]
2635 (copy-port in out :unit 'byte)))))
2636
2637 (test* "copy-port (unit char)" #t
2638 (equal? s (call-with-string-io s (^[in out]
2639 (copy-port in out :unit 'char)))))
2640
2641 (test* "copy-port (unit 10)" #t
2642 (equal? s (call-with-string-io s (^[in out]
2643 (copy-port in out :unit 10)))))
2644
2645 (test* "copy-port (unit 100000)" #t
2646 (equal? s (call-with-string-io s (^[in out]
2647 (copy-port in out :unit 100000)))))
2648
2649 ;;-------------------------------------------------------------------
2650 (test-section "binary search")
2651
2652 (use srfi.42)
2653
2654
2655 (let ([data '(;; NB: keep the length of vector a multiple of 6 for skip test
2656 ;; (<vec> . <keys>)
2657 (#s8(-100 -99 -50 -41 -15 -2 -1 0 1 2 5 7 9 18 19 50 99 100)
2658 -101 -100 -99 -98 -50 -49 -1 0 2 3 98 99 100)
2659 (#u8(1 2 3 4 5 9 20 50 70 75 89 100)
2660 0 1 2 3 4 5 6 50 99 100)
2661 (#s16(-4515 -4514 -2 -1 0 1 2 19 27 55 32765 32767)
2662 -32768 -4514 -3 -2 2 32765 32766 32767)
2663 (#u16(0 2 65529 65531 65533 65535)
2664 0 1 2 65528 65529 65530 65531 65532 65533 65534)
2665 (#f64(-10000 -0.5 0 0.1 5.5 100)
2666 -10000 -0.51 -0.5 0 0.1 0.2 100 100.1)
2667 )])
2668 (define (linear-search vec key start end :optional (skip 0))
2669 (let loop ((k start))
2670 (cond [(not (zero? (modulo (- end start) (+ skip 1)))) (test-error)]
2671 [(>= (+ k skip) end) #f]
2672 [(= (~ vec k) key) k]
2673 [(< (~ vec k) key) (loop (+ k skip 1))]
2674 [else #f])))
2675
2676 (define (ranges vec)
2677 (let ([len (uvector-length vec)])
2678 `((0 ,len) (0 0) (0 1) (0 6))))
2679
2680 ;; generates list of (<key> <start> <end> :optional <esize>)
2681 (define (all-args vec keys)
2682 (list-ec (: k keys)
2683 (: r (ranges vec))
2684 (: e '(() (1) (2)))
2685 (append (cons k r) e)))
2686
2687 (define (expected vec arg) (apply linear-search vec arg))
2688
2689 (define (test-1 vec . keys)
2690 (let1 args (all-args vec keys)
2691 (dolist [arg args]
2692 (test* #"binary search ~(cons (class-of vec) arg)"
2693 (apply linear-search vec arg)
2694 (let ([key (car arg)]
2695 [start (cadr arg)]
2696 [end (caddr arg)]
2697 [maybe-skip (cdddr arg)])
2698 (and-let1 r (apply uvector-binary-search vec
2699 key start end maybe-skip)
2700 (+ r start)))))))
2701
2702 (dolist [v data]
2703 (apply test-1 v))
2704 )
2705
2706 ;; We test only on s8; the code path is the same so it should work
2707 ;; on other varieties
2708 (let ((vec '#s8(-10 -7 -2 5 9 33))
2709 (data '(;; search-key expect-floor expect-ceiling
2710 (-25 #f -10)
2711 (-10 -10 -10)
2712 (-9 -10 -7)
2713 (0 -2 5)
2714 (8 5 9)
2715 (33 33 33)
2716 (100 33 #f))))
2717 (define (test-1 entry)
2718 (let1 search-key (car entry)
2719 (list search-key
2720 (pick (uvector-binary-search vec search-key #f #f #f 'floor))
2721 (pick (uvector-binary-search vec search-key #f #f #f 'ceiling)))))
2722 (define (pick index)
2723 (and index (~ vec index)))
2724 (test* "binary search, floor and ceiling" data
2725 (map test-1 data)))
2726
2727 ;;-------------------------------------------------------------------
2728 (test-section "r7rs bytevector")
2729
2730 (test* "make-bytevector" '#u8() (make-bytevector 0))
2731 (test* "make-bytevector" '#u8(0) (make-bytevector 1))
2732 (test* "make-bytevector" '#u8(10 10) (make-bytevector 2 10))
2733 (test* "make-bytevector" '#u8(255 255 255) (make-bytevector 3 -1))
2734 (test* "make-bytevector" (test-error) (make-bytevector 3 256))
2735 (test* "make-bytevector" (test-error) (make-bytevector 3 -129))
2736 (test* "bytevector-fill!" '#u8(2 2 2 2 2)
2737 (rlet1 v (make-bytevector 5)
2738 (bytevector-fill! v 2)))
2739 (test* "bytevector-fill!" '#u8(254 254 254)
2740 (rlet1 v (make-bytevector 3 0)
2741 (bytevector-fill! v -2)))
2742 (test* "bytevector-s8-ref" -127
2743 (bytevector-s8-ref (bytevector #x81) 0))
2744 (test* "bytevector-s8-set!" '#u8(0 255)
2745 (rlet1 v (make-bytevector 2)
2746 (bytevector-s8-set! v 1 -1)))
2747 (test* "bytevector-copy!-r6" '#u8(0 0 2 3 4 0 0 0)
2748 (rlet1 v (make-bytevector 8 0)
2749 (bytevector-copy!-r6 '#u8(1 2 3 4 5) 1 v 2 3)))
2750
2751 ;;-------------------------------------------------------------------
2752 (test-section "generator and uvector")
2753
2754 (use gauche.generator)
2755
2756 (test* "generator->bytevector" '#u8(0 1 2 3 0 1 2 3 0 1)
2757 (generator->bytevector (circular-generator 0 1 2 3) 10))
2758 (test* "generator->bytevector" '#u8(0 1 2 3)
2759 (generator->bytevector (list->generator '(0 1 2 3)) 10))
2760 (test* "generator->bytevector!" '(8 #u8(255 255 0 1 2 3 0 1 2 3))
2761 (let1 vec (make-u8vector 10 255)
2762 (list (generator->bytevector! vec 2 (circular-generator 0 1 2 3))
2763 vec)))
2764
2765 (test* "generator->uvector" '#u32(0 1 2 3 0 1 2 3 0 1)
2766 (generator->uvector (circular-generator 0 1 2 3) 10 <u32vector>))
2767 (test* "generator->uvector!" '(7 #s32(-1 -1 -1 0 1 2 3 0 1 2))
2768 (let1 vec (make-s32vector 10 -1)
2769 (list (generator->uvector! vec 3 (circular-generator 0 1 2 3))
2770 vec)))
2771
2772 ;;-------------------------------------------------------------------
2773 (test-section "SRFI-160 interface")
2774
2775 (use gauche.uvector.u8)
2776 (test-module 'gauche.uvector.u8)
2777 (use gauche.uvector.s8)
2778 (test-module 'gauche.uvector.s8)
2779 (use gauche.uvector.u16)
2780 (test-module 'gauche.uvector.u16)
2781 (use gauche.uvector.s16)
2782 (test-module 'gauche.uvector.s16)
2783 (use gauche.uvector.u32)
2784 (test-module 'gauche.uvector.u32)
2785 (use gauche.uvector.s32)
2786 (test-module 'gauche.uvector.s32)
2787 (use gauche.uvector.u64)
2788 (test-module 'gauche.uvector.u64)
2789 (use gauche.uvector.s64)
2790 (test-module 'gauche.uvector.s64)
2791 (use gauche.uvector.f16)
2792 (test-module 'gauche.uvector.f16)
2793 (use gauche.uvector.f32)
2794 (test-module 'gauche.uvector.f32)
2795 (use gauche.uvector.f64)
2796 (test-module 'gauche.uvector.f64)
2797 (use gauche.uvector.c32)
2798 (test-module 'gauche.uvector.c32)
2799 (use gauche.uvector.c64)
2800 (test-module 'gauche.uvector.c64)
2801 (use gauche.uvector.c128)
2802 (test-module 'gauche.uvector.c128)
2803
2804 ;;-------------------------------------------------------------------
2805 ;; SRFI-207 depends on gauche.uvector, gauche.unicode and gauche.generator,
2806 ;; so we test it after those dependencies are tested.
2807 (test-section "SRFI-207 bytestring")
2808
2809 (use srfi.207)
2810 (test-module 'srfi.207)
2811
2812 (test-include-r7 "../../test/include/srfi-207-test.scm")
2813
2814 (test-end)