Debian Codegraph @

test/include/srfi-116-tests.scm

raw
1 (import
2 (scheme base)
3 (srfi 128)
4 (ilists ilists))
5
6 (cond-expand
7 (chibi
8 (import (rename (except (chibi test) test-equal)
9 (test test-equal*))))
10 (else
11 (import (rename (srfi 64) (test-equal test-equal*)))))
12
13 (define (test-equal a b)
14 (cond
15 ((and (pair? a) (pair? b))
16 (begin
17 (test-equal (car a) (car b))
18 (test-equal (cdr a) (cdr b))))
19 ((and (vector? a) (vector? b))
20 (vector-for-each
21 test-equal
22 a
23 b))
24 ((and (ipair? a) (ipair? b))
25 (begin
26 (test-equal (icar a) (icar b))
27 (test-equal (icdr a) (icdr b))))
28 (else (test-equal* a b))))
29
30 (test-begin "ilists")
31
32 (test-group "ilists/constructors"
33 (define abc (ilist 'a 'b 'c))
34 (define abc-dot-d (ipair* 'a 'b 'c 'd))
35 (define abc-copy (ilist-copy abc))
36 (test-equal 'a (icar abc))
37 (test-equal 'b (icadr abc))
38 (test-equal 'c (icaddr abc))
39 (test-equal (ipair 2 1) (xipair 1 2))
40 (test-equal 'd (icdddr abc-dot-d))
41 (test-equal (iq c c c c) (make-ilist 4 'c))
42 (test-equal (iq 0 1 2 3) (ilist-tabulate 4 values))
43 (test-equal (iq 0 1 2 3 4) (iiota 5))
44 (test-equal abc abc-copy)
45 (test-assert (not (eq? abc abc-copy)))
46 ) ; end ilists/constructors
47
48 (test-group "ilists/predicates"
49 (test-assert (ipair? (ipair 1 2)))
50 (test-assert (proper-ilist? '()))
51 (test-assert (proper-ilist? (iq 1 2 3)))
52 (test-assert (ilist? '()))
53 (test-assert (ilist? (iq 1 2 3)))
54 (test-assert (dotted-ilist? (ipair 1 2)))
55 (test-assert (dotted-ilist? 2))
56 (test-assert (null-ilist? '()))
57 (test-assert (not (null-ilist? (iq 1 2 3))))
58 (test-error (null-ilist? 'a))
59 (test-assert (not-ipair? 'a))
60 (test-assert (not (not-ipair? (ipair 'a 'b))))
61 (test-assert (ilist= = (iq 1 2 3) (iq 1 2 3)))
62 (test-assert (ilist= = (iq 1 2 3) (iq 1 2 3) (iq 1 2 3)))
63 (test-assert (not (ilist= = (iq 1 2 3 4) (iq 1 2 3))))
64 (test-assert (not (ilist= = (iq 1 2 3) (iq 1 2 3 4))))
65 (test-assert (ilist= = (iq 1 2 3) (iq 1 2 3)))
66 (test-assert (not (ilist= = (iq 1 2 3) (iq 1 2 3 4) (iq 1 2 3 4))))
67 (test-assert (not (ilist= = (iq 1 2 3) (iq 1 2 3) (iq 1 2 3 4))))
68 ) ; end ilist/predicates
69
70 (test-group "ilist/cxrs"
71 (define ab (ipair 'a 'b))
72 (define cd (ipair 'c 'd))
73 (define ef (ipair 'e 'f))
74 (define gh (ipair 'g 'h))
75 (define abcd (ipair ab cd))
76 (define efgh (ipair ef gh))
77 (define abcdefgh (ipair abcd efgh))
78 (define ij (ipair 'i 'j))
79 (define kl (ipair 'k 'l))
80 (define mn (ipair 'm 'n))
81 (define op (ipair 'o 'p))
82 (define ijkl (ipair ij kl))
83 (define mnop (ipair mn op))
84 (define ijklmnop (ipair ijkl mnop))
85 (define abcdefghijklmnop (ipair abcdefgh ijklmnop))
86 (test-equal 'a (icaar abcd))
87 (test-equal 'b (icdar abcd))
88 (test-equal 'c (icadr abcd))
89 (test-equal 'd (icddr abcd))
90 (test-equal 'a (icaaar abcdefgh))
91 (test-equal 'b (icdaar abcdefgh))
92 (test-equal 'c (icadar abcdefgh))
93 (test-equal 'd (icddar abcdefgh))
94 (test-equal 'e (icaadr abcdefgh))
95 (test-equal 'f (icdadr abcdefgh))
96 (test-equal 'g (icaddr abcdefgh))
97 (test-equal 'h (icdddr abcdefgh))
98 (test-equal 'a (icaaaar abcdefghijklmnop))
99 (test-equal 'b (icdaaar abcdefghijklmnop))
100 (test-equal 'c (icadaar abcdefghijklmnop))
101 (test-equal 'd (icddaar abcdefghijklmnop))
102 (test-equal 'e (icaadar abcdefghijklmnop))
103 (test-equal 'f (icdadar abcdefghijklmnop))
104 (test-equal 'g (icaddar abcdefghijklmnop))
105 (test-equal 'h (icdddar abcdefghijklmnop))
106 (test-equal 'i (icaaadr abcdefghijklmnop))
107 (test-equal 'j (icdaadr abcdefghijklmnop))
108 (test-equal 'k (icadadr abcdefghijklmnop))
109 (test-equal 'l (icddadr abcdefghijklmnop))
110 (test-equal 'm (icaaddr abcdefghijklmnop))
111 (test-equal 'n (icdaddr abcdefghijklmnop))
112 (test-equal 'o (icadddr abcdefghijklmnop))
113 (test-equal 'p (icddddr abcdefghijklmnop))
114 ) ; end ilists/cxrs
115
116 (test-group "ilists/selectors"
117 (define ten (ilist 1 2 3 4 5 6 7 8 9 10))
118 (define abcde (iq a b c d e))
119 (define dotted (ipair 1 (ipair 2 (ipair 3 'd))))
120 (test-equal 'c (ilist-ref (iq a b c d) 2))
121 (test-equal 1 (ifirst ten))
122 (test-equal 2 (isecond ten))
123 (test-equal 3 (ithird ten))
124 (test-equal 4 (ifourth ten))
125 (test-equal 5 (ififth ten))
126 (test-equal 6 (isixth ten))
127 (test-equal 7 (iseventh ten))
128 (test-equal 8 (ieighth ten))
129 (test-equal 9 (ininth ten))
130 (test-equal 10 (itenth ten))
131 (test-error (ilist-ref '() 2))
132 (test-equal '(1 2) (call-with-values (lambda () (icar+icdr (ipair 1 2))) list))
133 (test-equal (iq a b) (itake abcde 2))
134 (test-equal (iq c d e) (idrop abcde 2))
135 (test-equal (iq c d e) (ilist-tail abcde 2))
136 (test-equal (iq 1 2) (itake dotted 2))
137 (test-equal (ipair 3 'd) (idrop dotted 2))
138 (test-equal (ipair 3 'd) (ilist-tail dotted 2))
139 (test-equal 'd (idrop dotted 3))
140 (test-equal 'd (ilist-tail dotted 3))
141 (test-equal abcde (iappend (itake abcde 4) (idrop abcde 4)))
142 (test-equal (iq d e) (itake-right abcde 2))
143 (test-equal (iq a b c) (idrop-right abcde 2))
144 (test-equal (ipair 2 (ipair 3 'd)) (itake-right dotted 2))
145 (test-equal (iq 1) (idrop-right dotted 2))
146 (test-equal 'd (itake-right dotted 0))
147 (test-equal (iq 1 2 3) (idrop-right dotted 0))
148 (test-equal abcde (call-with-values (lambda () (isplit-at abcde 3)) iappend))
149 (test-equal 'c (ilast (iq a b c)))
150 (test-equal (iq c) (last-ipair (iq a b c)))
151 ) ; end ilists/selectors
152
153 (test-group "ilists/misc"
154 (test-equal 0 (ilength '()))
155 (test-equal 3 (ilength (iq 1 2 3)))
156 (test-equal (iq x y) (iappend (iq x) (iq y)))
157 (test-equal (iq a b c d) (iappend (iq a b) (iq c d)))
158 (test-equal (iq a) (iappend '() (iq a)))
159 (test-equal (iq x y) (iappend (iq x y)))
160 (test-equal '() (iappend))
161 (test-equal (iq a b c d) (iconcatenate (iq (a b) (c d))))
162 (test-equal (iq c b a) (ireverse (iq a b c)))
163 (test-equal (iq (e (f)) d (b c) a) (ireverse (iq a (b c) d (e (f)))))
164 (test-equal (ipair 2 (ipair 1 'd)) (iappend-reverse (iq 1 2) 'd))
165 (test-equal (iq (one 1 odd) (two 2 even) (three 3 odd))
166 (izip (iq one two three) (iq 1 2 3) (iq odd even odd)))
167 (test-equal (iq (1) (2) (3)) (izip (iq 1 2 3)))
168 (test-equal (iq 1 2 3) (iunzip1 (iq (1) (2) (3))))
169 (test-equal (iq (1 2 3) (one two three))
170 (call-with-values
171 (lambda () (iunzip2 (iq (1 one) (2 two) (3 three))))
172 ilist))
173 (test-equal (iq (1 2 3) (one two three) (a b c))
174 (call-with-values
175 (lambda () (iunzip3 (iq (1 one a) (2 two b) (3 three c))))
176 ilist))
177 (test-equal (iq (1 2 3) (one two three) (a b c) (4 5 6))
178 (call-with-values
179 (lambda () (iunzip4 (iq (1 one a 4) (2 two b 5) (3 three c 6))))
180 ilist))
181 (test-equal (iq (1 2 3) (one two three) (a b c) (4 5 6) (#t #f #t))
182 (call-with-values
183 (lambda () (iunzip5 (iq (1 one a 4 #t) (2 two b 5 #f) (3 three c 6 #t))))
184 ilist))
185 (test-equal 3 (icount even? (iq 3 1 4 1 5 9 2 5 6)))
186 (test-equal 3 (icount < (iq 1 2 4 8) (iq 2 4 6 8 10 12 14 16)))
187 ) ; end ilists/misc
188
189 (test-group "ilists/folds"
190 ;; We have to be careful to test-equal both single-list and multiple-list
191 ;; code paths, as they are different in this implementation.
192
193 (define lis (iq 1 2 3))
194 (define (z x y ans) (ipair (ilist x y) ans))
195 (define squares (iq 1 4 9 16 25 36 49 64 81 100))
196 (test-equal 6 (ifold + 0 lis))
197 (test-equal (iq 3 2 1) (ifold ipair '() lis))
198 (test-equal 2 (ifold
199 (lambda (x count) (if (symbol? x) (+ count 1) count))
200 0
201 (iq a 0 b)))
202 (test-equal 4 (ifold
203 (lambda (s max-len) (max max-len (string-length s)))
204 0
205 (iq "ab" "abcd" "abc")))
206 (test-equal 32 (ifold
207 (lambda (a b ans) (+ (* a b) ans))
208 0
209 (iq 1 2 3)
210 (iq 4 5 6)))
211 (test-equal (iq (b d) (a c))
212 (ifold z '() (iq a b) (iq c d)))
213 (test-equal lis (ifold-right ipair '() lis))
214 (test-equal (iq 0 2 4) (ifold-right
215 (lambda (x l) (if (even? x) (ipair x l) l))
216 '()
217 (iq 0 1 2 3 4)))
218 (test-equal (iq (a c) (b d))
219 (ifold-right z '() (iq a b) (iq c d)))
220 (test-equal (iq (c) (b c) (a b c))
221 (ipair-fold ipair '() (iq a b c)))
222 (test-equal (iq ((b) (d)) ((a b) (c d)))
223 (ipair-fold z '() (iq a b) (iq c d)))
224 (test-equal (iq (a b c) (b c) (c))
225 (ipair-fold-right ipair '() (iq a b c)))
226 (test-equal (iq ((a b) (c d)) ((b) (d)))
227 (ipair-fold-right z '() (iq a b) (iq c d)))
228 (test-equal 5 (ireduce max 0 (iq 1 3 5 4 2 0)))
229 (test-equal 1 (ireduce - 0 (iq 1 2)))
230 (test-equal -1 (ireduce-right - 0 (iq 1 2)))
231 (test-equal squares
232 (iunfold (lambda (x) (> x 10))
233 (lambda (x) (* x x))
234 (lambda (x) (+ x 1))
235 1))
236 (test-equal squares
237 (iunfold-right zero?
238 (lambda (x) (* x x))
239 (lambda (x) (- x 1))
240 10))
241 (test-equal (iq 1 2 3) (iunfold null-ilist? icar icdr (iq 1 2 3)))
242 (test-equal (iq 3 2 1) (iunfold-right null-ilist? icar icdr (iq 1 2 3)))
243 (test-equal (iq 1 2 3 4)
244 (iunfold null-ilist? icar icdr (iq 1 2) (lambda (x) (iq 3 4))))
245 (test-equal (iq b e h) (imap icadr (iq (a b) (d e) (g h))))
246 (test-equal (iq b e h) (imap-in-order icadr (iq (a b) (d e) (g h))))
247 (test-equal (iq 5 7 9) (imap + (iq 1 2 3) (iq 4 5 6)))
248 (test-equal (iq 5 7 9) (imap-in-order + (iq 1 2 3) (iq 4 5 6)))
249 (set! z (let ((count 0)) (lambda (ignored) (set! count (+ count 1)) count)))
250 (test-equal (iq 1 2) (imap-in-order z (iq a b)))
251 (test-equal '#(0 1 4 9 16)
252 (let ((v (make-vector 5)))
253 (ifor-each (lambda (i)
254 (vector-set! v i (* i i)))
255 (iq 0 1 2 3 4))
256 v))
257 (test-equal '#(5 7 9 11 13)
258 (let ((v (make-vector 5)))
259 (ifor-each (lambda (i j)
260 (vector-set! v i (+ i j)))
261 (iq 0 1 2 3 4)
262 (iq 5 6 7 8 9))
263 v))
264 (test-equal (iq 1 -1 3 -3 8 -8)
265 (iappend-map (lambda (x) (ilist x (- x))) (iq 1 3 8)))
266 (test-equal (iq 1 4 2 5 3 6)
267 (iappend-map ilist (iq 1 2 3) (iq 4 5 6)))
268 (test-equal (vector (iq 0 1 2 3 4) (iq 1 2 3 4) (iq 2 3 4) (iq 3 4) (iq 4))
269 (let ((v (make-vector 5)))
270 (ipair-for-each (lambda (lis) (vector-set! v (icar lis) lis)) (iq 0 1 2 3 4))
271 v))
272 (test-equal (vector (iq 5 6 7 8 9) (iq 6 7 8 9) (iq 7 8 9) (iq 8 9) (iq 9))
273 (let ((v (make-vector 5)))
274 (ipair-for-each (lambda (i j) (vector-set! v (icar i) j))
275 (iq 0 1 2 3 4)
276 (iq 5 6 7 8 9))
277 v))
278 (test-equal (iq 1 9 49)
279 (ifilter-map (lambda (x) (and (number? x) (* x x))) (iq a 1 b 3 c 7)))
280 (test-equal (iq 5 7 9)
281 (ifilter-map
282 (lambda (x y) (and (number? x) (number? y) (+ x y)))
283 (iq 1 a 2 b 3 4)
284 (iq 4 0 5 y 6 z)))
285 ) ; end ilists/folds
286
287 (test-group "ilists/filtering"
288 (test-equal (iq 0 8 8 -4) (ifilter even? (iq 0 7 8 8 43 -4)))
289 (test-equal (list (iq one four five) (iq 2 3 6))
290 (call-with-values
291 (lambda () (ipartition symbol? (iq one 2 3 four five 6)))
292 list))
293 (test-equal (iq 7 43) (iremove even? (iq 0 7 8 8 43 -4)))
294 ) ; end ilists/filtering
295
296 (test-group "ilists/searching"
297 (test-equal 2 (ifind even? (iq 1 2 3)))
298 (test-equal #t (iany even? (iq 1 2 3)))
299 (test-equal #f (ifind even? (iq 1 7 3)))
300 (test-equal #f (iany even? (iq 1 7 3)))
301 (test-error (ifind even? (ipair 1 (ipair 3 'x))))
302 (test-error (iany even? (ipair 1 (ipair 3 'x))))
303 (test-equal 4 (ifind even? (iq 3 1 4 1 5 9)))
304 (test-equal (iq -8 -5 0 0) (ifind-tail even? (iq 3 1 37 -8 -5 0 0)))
305 (test-equal (iq 2 18) (itake-while even? (iq 2 18 3 10 22 9)))
306 (test-equal (iq 3 10 22 9) (idrop-while even? (iq 2 18 3 10 22 9)))
307 (test-equal (list (iq 2 18) (iq 3 10 22 9))
308 (call-with-values
309 (lambda () (ispan even? (iq 2 18 3 10 22 9)))
310 list))
311 (test-equal (list (iq 3 1) (iq 4 1 5 9))
312 (call-with-values
313 (lambda () (ibreak even? (iq 3 1 4 1 5 9)))
314 list))
315 (test-equal #t (iany integer? (iq a 3 b 2.7)))
316 (test-equal #f (iany integer? (iq a 3.1 b 2.7)))
317 (test-equal #t (iany < (iq 3 1 4 1 5) (iq 2 7 1 8 2)))
318 (test-equal #t (ievery integer? (iq 1 2 3 4 5)))
319 (test-equal #f (ievery integer? (iq 1 2 3 4.5 5)))
320 (test-equal #t (ievery (lambda (a b) (< a b)) (iq 1 2 3) (iq 4 5 6)))
321 (test-equal 2 (ilist-index even? (iq 3 1 4 1 5 9)))
322 (test-equal 1 (ilist-index < (iq 3 1 4 1 5 9 2 5 6) (iq 2 7 1 8 2)))
323 (test-equal #f (ilist-index = (iq 3 1 4 1 5 9 2 5 6) (iq 2 7 1 8 2)))
324 (test-equal (iq a b c) (imemq 'a (iq a b c)))
325 (test-equal (iq b c) (imemq 'b (iq a b c)))
326 (test-equal #f (imemq 'a (iq b c d)))
327 (test-equal #f (imemq (ilist 'a) (iq b (a) c)))
328 (test-equal (iq (a) c) (imember (ilist 'a) (iq b (a) c)))
329 (test-equal (iq 101 102) (imemv 101 (iq 100 101 102)))
330 ) ; end ilists/searching
331
332 (test-group "ilists/deletion"
333 (test-equal (iq 1 2 4 5) (idelete 3 (iq 1 2 3 4 5)))
334 (test-equal (iq 3 4 5) (idelete 5 (iq 3 4 5 6 7) <))
335 (test-equal (iq a b c z) (idelete-duplicates (iq a b a c a b c z)))
336 ) ; end ilists/deletion
337
338 (test-group "ilists/alists"
339 (define e (iq (a 1) (b 2) (c 3)))
340 (define e2 (iq (2 3) (5 7) (11 13)))
341 (test-equal (iq a 1) (iassq 'a e))
342 (test-equal (iq b 2) (iassq 'b e))
343 (test-equal #f (iassq 'd e))
344 (test-equal #f (iassq (ilist 'a) (iq ((a)) ((b)) ((c)))))
345 (test-equal (iq (a)) (iassoc (ilist 'a) (iq ((a)) ((b)) ((c)))))
346 (test-equal (iq 5 7) (iassv 5 e2))
347 (test-equal (iq 11 13) (iassoc 5 e2 <))
348 (test-equal (ipair (iq 1 1) e2) (ialist-cons 1 (ilist 1) e2))
349 (test-equal (iq (2 3) (11 13)) (ialist-delete 5 e2))
350 (test-equal (iq (2 3) (5 7)) (ialist-delete 5 e2 <))
351 ) ; end ilists/alists
352
353 (test-group "ilists/replacers"
354 (test-equal (ipair 1 3) (replace-icar (ipair 2 3) 1))
355 (test-equal (ipair 1 3) (replace-icdr (ipair 1 2) 3))
356 ) ; end ilists/replacers
357
358 (test-group "ilists/conversion"
359 (test-equal (ipair 1 2) (pair->ipair '(1 . 2)))
360 (test-equal '(1 . 2) (ipair->pair (ipair 1 2)))
361 (test-equal (iq 1 2 3) (list->ilist '(1 2 3)))
362 (test-equal '(1 2 3) (ilist->list (iq 1 2 3)))
363 (test-equal (ipair 1 (ipair 2 3)) (list->ilist '(1 2 . 3)))
364 (test-equal '(1 2 . 3) (ilist->list (ipair 1 (ipair 2 3))))
365 (test-equal (ipair (ipair 1 2) (ipair 3 4)) (tree->itree '((1 . 2) . (3 . 4))))
366 (test-equal '((1 . 2) . (3 . 4)) (itree->tree (ipair (ipair 1 2) (ipair 3 4))))
367 (test-equal (ipair (ipair 1 2) (ipair 3 4)) (gtree->itree (cons (ipair 1 2) (ipair 3 4))))
368 (test-equal '((1 . 2) . (3 . 4)) (gtree->tree (cons (ipair 1 2) (ipair 3 4))))
369 (test-equal 6 (iapply + (iq 1 2 3)))
370 (test-equal 15 (iapply + 1 2 (iq 3 4 5)))
371 ) ; end ilists/conversion
372
373 (test-group
374 "comparators"
375
376 (let ((c ipair-comparator))
377 (test-equal #t (comparator-test-type c (ipair 1 2)))
378 (test-equal #f (comparator-test-type c 1))
379 (test-equal #t (=? c (ipair 1 2) (ipair 1 2)))
380 (test-equal #f (=? c (ipair 1 2) (ipair 1 3)))
381 (test-equal #f (=? c (ipair 1 2) (ipair 3 1)))
382 (test-equal #t (<? c (ipair 1 2) (ipair 1 3)))
383 (test-equal #f (>? c (ipair 1 2) (ipair 1 3)))
384 (test-equal #t (<? c (ipair 1 2) (ipair 3 1)))
385 (test-equal #f (>? c (ipair 1 2) (ipair 3 1)))
386 (test-equal (comparator-hash c (ipair 1 2))
387 (comparator-hash c (ipair 1 2)))))
388
389 (test-end)