1 ;;;; ramap.test --- test array mapping functions -*- scheme -*-
3 ;;;; Copyright (C) 2004, 2005, 2006, 2009, 2013 Free Software Foundation, Inc.
5 ;;;; This library is free software; you can redistribute it and/or
6 ;;;; modify it under the terms of the GNU Lesser General Public
7 ;;;; License as published by the Free Software Foundation; either
8 ;;;; version 3 of the License, or (at your option) any later version.
10 ;;;; This library is distributed in the hope that it will be useful,
11 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ;;;; Lesser General Public License for more details.
15 ;;;; You should have received a copy of the GNU Lesser General Public
16 ;;;; License along with this library; if not, write to the Free Software
17 ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 (define-module (test-suite test-ramap)
20 #:use-module (test-suite lib))
22 (define exception:shape-mismatch
23 (cons 'misc-error ".*shape mismatch.*"))
25 (define (array-row a i)
26 (make-shared-array a (lambda (j) (list i j))
27 (cadr (array-dimensions a))))
29 (define (array-col a j)
30 (make-shared-array a (lambda (i) (list i j))
31 (car (array-dimensions a))))
37 (with-test-prefix "array-index-map!"
41 (array-index-map! (make-array #f '(1 1))
43 (set! nlst (cons n nlst))))
46 (with-test-prefix "empty arrays"
48 (pass-if "all axes empty"
49 (array-index-map! (make-typed-array 'f64 0 0 0) (const 0))
50 (array-index-map! (make-typed-array 'b #t 0 0) (const #t))
51 (array-index-map! (make-typed-array #t 0 0 0) (const 0))
54 (pass-if "last axis empty"
55 (array-index-map! (make-typed-array 'f64 0 2 0) (const 0))
56 (array-index-map! (make-typed-array 'b #t 2 0) (const #t))
57 (array-index-map! (make-typed-array #t 0 2 0) (const 0))
60 ; the 'f64 cases fail in 2.0.9 with out-of-range.
61 (pass-if "axis empty, other than last"
62 (array-index-map! (make-typed-array 'f64 0 0 2) (const 0))
63 (array-index-map! (make-typed-array 'b #t 0 2) (const #t))
64 (array-index-map! (make-typed-array #t 0 0 2) (const 0))
71 (with-test-prefix "array-copy!"
73 (with-test-prefix "empty arrays"
75 (pass-if "empty other than last, #t"
76 (let* ((b (make-array 0 2 2))
77 (c (make-shared-array b (lambda (i j) (list i j)) 0 2)))
78 (array-copy! #2:0:2() c)
79 (array-equal? #2:0:2() c)))
81 (pass-if "empty other than last, 'f64"
82 (let* ((b (make-typed-array 'f64 0 2 2))
83 (c (make-shared-array b (lambda (i j) (list i j)) 0 2)))
84 (array-copy! #2:0:2() c)
85 (array-equal? #2f64:0:2() c)))
87 ;; FIXME add type 'b cases.
95 (with-test-prefix "array-map!"
97 (pass-if-exception "no args" exception:wrong-num-args
100 (pass-if-exception "one arg" exception:wrong-num-args
101 (array-map! (make-array #f 5)))
103 (with-test-prefix "no sources"
106 (array-map! (make-array #f 5) (lambda () #f))
109 (pass-if-exception "closure 1" exception:wrong-num-args
110 (array-map! (make-array #f 5) (lambda (x) #f)))
112 (pass-if-exception "closure 2" exception:wrong-num-args
113 (array-map! (make-array #f 5) (lambda (x y) #f)))
115 (pass-if-exception "subr_1" exception:wrong-num-args
116 (array-map! (make-array #f 5) length))
118 (pass-if-exception "subr_2" exception:wrong-num-args
119 (array-map! (make-array #f 5) logtest))
121 (pass-if-exception "subr_2o" exception:wrong-num-args
122 (array-map! (make-array #f 5) number->string))
124 (pass-if-exception "dsubr" exception:wrong-num-args
125 (array-map! (make-array #f 5) sqrt))
128 (let ((a (make-array 'foo 5)))
130 (equal? a (make-array #t 5))))
133 (let ((a (make-array 'foo 5)))
135 (equal? a (make-array 0 5))))
137 ;; in Guile 1.6.4 and earlier this resulted in a segv
139 (array-map! (make-array #f 5) noop)
142 (with-test-prefix "one source"
144 (pass-if-exception "closure 0" exception:wrong-num-args
145 (array-map! (make-array #f 5) (lambda () #f)
149 (let ((a (make-array #f 5)))
150 (array-map! a (lambda (x) 'foo) (make-array #f 5))
151 (equal? a (make-array 'foo 5))))
153 (pass-if-exception "closure 2" exception:wrong-num-args
154 (array-map! (make-array #f 5) (lambda (x y) #f)
158 (let ((a (make-array #f 5)))
159 (array-map! a length (make-array '(x y z) 5))
160 (equal? a (make-array 3 5))))
162 (pass-if-exception "subr_2" exception:wrong-num-args
163 (array-map! (make-array #f 5) logtest
167 (let ((a (make-array #f 5)))
168 (array-map! a number->string (make-array 99 5))
169 (equal? a (make-array "99" 5))))
172 (let ((a (make-array #f 5)))
173 (array-map! a sqrt (make-array 16.0 5))
174 (equal? a (make-array 4.0 5))))
177 (let ((a (make-array 'foo 5)))
178 (array-map! a = (make-array 0 5))
179 (equal? a (make-array #t 5))))
182 (let ((a (make-array 'foo 5)))
183 (array-map! a - (make-array 99 5))
184 (equal? a (make-array -99 5))))
186 ;; in Guile 1.6.5 and 1.6.6 this was an error
188 (let ((a (make-array #f 5)))
189 (array-map! a 1+ (make-array 123 5))
190 (equal? a (make-array 124 5)))))
192 (with-test-prefix "two sources"
194 (pass-if-exception "closure 0" exception:wrong-num-args
195 (array-map! (make-array #f 5) (lambda () #f)
196 (make-array #f 5) (make-array #f 5)))
198 (pass-if-exception "closure 1" exception:wrong-num-args
199 (array-map! (make-array #f 5) (lambda (x) #f)
200 (make-array #f 5) (make-array #f 5)))
203 (let ((a (make-array #f 5)))
204 (array-map! a (lambda (x y) 'foo)
205 (make-array #f 5) (make-array #f 5))
206 (equal? a (make-array 'foo 5))))
208 (pass-if-exception "subr_1" exception:wrong-num-args
209 (array-map! (make-array #f 5) length
210 (make-array #f 5) (make-array #f 5)))
213 (let ((a (make-array 'foo 5)))
214 (array-map! a logtest
215 (make-array 999 5) (make-array 999 5))
216 (equal? a (make-array #t 5))))
219 (let ((a (make-array #f 5)))
220 (array-map! a number->string
221 (make-array 32 5) (make-array 16 5))
222 (equal? a (make-array "20" 5))))
224 (pass-if-exception "dsubr" exception:wrong-num-args
225 (let ((a (make-array #f 5)))
227 (make-array 16.0 5) (make-array 16.0 5))
228 (equal? a (make-array 4.0 5))))
231 (let ((a (make-array 'foo 5)))
232 (array-map! a = (make-array 99 5) (make-array 77 5))
233 (equal? a (make-array #f 5))))
236 (let ((a (make-array 'foo 5)))
237 (array-map! a - (make-array 99 5) (make-array 11 5))
238 (equal? a (make-array 88 5))))
241 (let ((a (make-array #f 4)))
242 (array-map! a + #(1 2 3 4) #(5 6 7 8))
243 (equal? a #(6 8 10 12))))
245 (pass-if "noncompact arrays 1"
246 (let ((a #2((0 1) (2 3)))
247 (c (make-array 0 2)))
249 (array-map! c + (array-row a 1) (array-row a 1))
250 (array-equal? c #(4 6)))))
252 (pass-if "noncompact arrays 2"
253 (let ((a #2((0 1) (2 3)))
254 (c (make-array 0 2)))
256 (array-map! c + (array-col a 1) (array-col a 1))
257 (array-equal? c #(2 6)))))
259 (pass-if "noncompact arrays 3"
260 (let ((a #2((0 1) (2 3)))
261 (c (make-array 0 2)))
263 (array-map! c + (array-col a 1) (array-row a 1))
264 (array-equal? c #(3 6)))))
266 (pass-if "noncompact arrays 4"
267 (let ((a #2((0 1) (2 3)))
268 (c (make-array 0 2)))
270 (array-map! c + (array-col a 1) (array-row a 1))
271 (array-equal? c #(3 6)))))))
277 (with-test-prefix "array-for-each"
279 (with-test-prefix "1 source"
280 (pass-if-equal "noncompact array"
282 (let* ((a #2((0 1) (2 3)))
284 (p (lambda (x) (set! l (cons x l)))))
288 (pass-if-equal "vector"
290 (let* ((a #(0 1 2 3))
292 (p (lambda (x) (set! l (cons x l)))))
296 (pass-if-equal "shared array"
298 (let* ((a #2((0 1) (2 3)))
299 (a' (make-shared-array a
305 (p (lambda (x) (set! l (cons x l)))))
306 (array-for-each p a')
309 (with-test-prefix "3 sources"
310 (pass-if-equal "noncompact arrays 1"
312 (let* ((a #2((0 1) (2 3)))
314 (rec (lambda args (set! l (cons args l)))))
315 (array-for-each rec (array-row a 1) (array-row a 1) (array-row a 1))
318 (pass-if-equal "noncompact arrays 2"
320 (let* ((a #2((0 1) (2 3)))
322 (rec (lambda args (set! l (cons args l)))))
323 (array-for-each rec (array-row a 1) (array-row a 1) (array-col a 1))
326 (pass-if-equal "noncompact arrays 3"
328 (let* ((a #2((0 1) (2 3)))
330 (rec (lambda args (set! l (cons args l)))))
331 (array-for-each rec (array-row a 1) (array-col a 1) (array-col a 1))
334 (pass-if-equal "noncompact arrays 4"
336 (let* ((a #2((0 1) (2 3)))
338 (rec (lambda args (set! l (cons args l)))))
339 (array-for-each rec (array-col a 1) (array-col a 0) (array-row a 1))
342 (with-test-prefix "empty arrays"
344 (pass-if "empty other than last, #t" ; fails in 2.0.9 with bad a.
346 (b (make-array 0 2 2))
347 (c (make-shared-array b (lambda (i j) (list i j)) 0 2)))
348 (array-for-each (lambda (c) (set! a (cons c a))) c)
351 (pass-if "empty other than last, f64" ; fails in 2.0.9 with out of range.
353 (b (make-typed-array 'f64 0 2 2))
354 (c (make-shared-array b (lambda (i j) (list i j)) 0 2)))
355 (array-for-each (lambda (c) (set! a (cons c a))) c)
358 ;; FIXME add type 'b cases.
360 (pass-if-exception "empty arrays shape check" exception:shape-mismatch
362 (b (make-typed-array 'f64 0 0 2))
363 (c (make-typed-array 'f64 0 2 0)))
364 (array-for-each (lambda (b c) (set! a (cons* b c a))) b c)))))