Debian Codegraph @

test/list.scm

raw
1 ;;;
2 ;;; tests for some builtin list operations
3 ;;;
4
5 (use gauche.test)
6 (test-start "builtin list operations")
7
8 ;;--------------------------------------------------------------------------
9 (test-section "ref,set!")
10
11 (test* "list-ref" '(e d c b a)
12 (map (^k (list-ref '(a b c d e) k)) '(4 3 2 1 0)))
13 (test* "list-set!" '(a (b) c (d) e)
14 (rlet1 lis (list 'a 'b 'c 'd 'e)
15 (for-each (lambda (k)
16 (when (odd? k)
17 (list-set! lis k (list (list-ref lis k)))))
18 '(4 3 2 1 0))))
19 (let ()
20 (define (ensure-error name proc)
21 (test* #"~name (negative)" (test-error) (proc -1))
22 (test* #"~name (too big)" (test-error) (proc 5))
23 (test* #"~name (nonexact)" (test-error) (proc 1.0))
24 (test* #"~name (noninteger)" (test-error) (proc 1/4)))
25
26 (ensure-error "list-ref" (^k (list-ref '(a b c d e) k)))
27 (ensure-error "list-set!" (^k (list-set! (list 'a 'b 'c 'd 'e) k 0)))
28 )
29
30 ;;--------------------------------------------------------------------------
31 (test-section "iota")
32
33 (test* "iota" '(0 1 2 3 4) (iota 5))
34 (test* "iota" '(5 6 7 8 9) (iota 5 5))
35 (test* "iota" '(10 20 30 40 50) (iota 5 10 10))
36 (test* "iota" '(1.0 1.5 2.0 2.5 3.0) (iota 5 1 0.5))
37
38 ;;--------------------------------------------------------------------------
39 (test-section "length")
40
41 (let ()
42 (define (test-length data k)
43 (test* #"length~|data| vs ~k"
44 '(#t
45 #t #t #f
46 #t #f #f
47 #f #t #f
48 #f #f #t
49 #f #t #t)
50 (list (if (list? data)
51 (= (length data) k)
52 #t)
53 (length<=? data (+ k 1))
54 (length<=? data k)
55 (length<=? data (- k 1))
56 (length<? data (+ k 1))
57 (length<? data k)
58 (length<? data (- k 1))
59 (length=? data (+ k 1))
60 (length=? data k)
61 (length=? data (- k 1))
62 (length>? data (+ k 1))
63 (length>? data k)
64 (length>? data (- k 1))
65 (length>=? data (+ k 1))
66 (length>=? data k)
67 (length>=? data (- k 1)))))
68 (test-length '() 0)
69 (test-length 'a 0)
70 (test-length '(a) 1)
71 (test-length '(a . b) 1)
72 (test-length '(a b c d e) 5))
73
74 (test* "num-pairs (proper)" 5
75 (num-pairs '(a b c d e)))
76 (test* "num-pairs (proper)" 0
77 (num-pairs '()))
78 (test* "num-pairs (dotted)" 4
79 (num-pairs '(a b c d . e)))
80 (test* "num-pairs (dotted)" 0
81 (num-pairs 'a))
82 (test* "num-pairs (circular)" 1
83 (num-pairs '#0=(a . #0#)))
84 (test* "num-pairs (circular)" 5
85 (num-pairs '#0=(a b c d e . #0#)))
86 (test* "num-pairs (circular)" 5
87 (num-pairs '(a b . #0=(c d e . #0#))))
88
89 ;;--------------------------------------------------------------------------
90 (test-section "copy, append and reverse")
91
92 (test* "copy" '((a b c d e) #f)
93 (let* ([x '(a b c d e)]
94 [y (list-copy x)])
95 (list x (eq? x y))))
96 (test* "copy" '((a b c d . e) #f)
97 (let* ([x '(a b c d . e)]
98 [y (list-copy x)])
99 (list x (eq? x y))))
100 (test* "copy" '(x #t)
101 (let* ([x 'x]
102 [y (list-copy x)])
103 (list x (eq? x y))))
104 (test* "copy" '(() #t)
105 (let* ([x '()]
106 [y (list-copy x)])
107 (list x (eq? x y))))
108 (test* "copy" (test-error) ;detect circular list
109 (let* ([x '#0=(a b c . #0#)]
110 [y (list-copy x)])
111 (list x (eq? x y))))
112
113 (test* "append (inlined)" '() (append))
114 (test* "append (inlined)" 1 (append 1))
115 (test* "append (inlined)" '(1 2 3 4 5) (append '(1 2 3) '() '(4 5)))
116 (test* "append (inlined)" '(1 2 3 . 5) (append '(1 2 3) 5))
117 (test* "append (inlined)" (test-error) (append 1 2))
118 (test* "append (inlined)" (test-error) (append '(1) '(2 . 3) 4))
119 (test* "append" '() (apply append '()))
120 (test* "append" 1 (apply append '(1)))
121 (test* "append" '(1 2 3 4 5) (apply append '((1 2 3) () (4 5))))
122 (test* "append" '(1 2 3 . 5) (apply append '((1 2 3) 5)))
123 (test* "append" (test-error) (apply append '(1 2)))
124 (test* "append" (test-error) (apply append '((1) (2 . 3) 4)))
125 (test* "append!" '() (append!))
126 (test* "append!" 1 (append! 1))
127 (test* "append!" '(1 2 3 4 5) (append! (list 1 2 3) '() '(4 5)))
128 (test* "append!" '(1 2 3 . 5) (append! (list 1 2 3) 5))
129 (test* "append!" (test-error) (append! (list* 1 2 3) 5))
130 (test* "append!" (test-error) (append! (list 1 2 3) (list* 1 2 3) 5))
131 (test* "reverse" '(5 4 3 2 1) (reverse '(1 2 3 4 5)))
132 (test* "reverse" '() (reverse '()))
133 (test* "reverse!" '(5 4 3 2 1) (reverse! (list 1 2 3 4 5)))
134 (test* "reverse!" '() (reverse! '()))
135 (test* "reverse 2args" '(1 2 3 4 5) (reverse '(3 2 1) '(4 5)))
136 (test* "reverse 2args" '(1 2 3 4 . 5) (reverse '(4 3 2 1) 5))
137 (test* "reverse! 2args" '(1 2 3 4 5) (reverse! (list 3 2 1) (list 4 5)))
138 (test* "reverse! 2args" '(1 2 3 4 . 5) (reverse! (list 4 3 2 1) 5))
139
140 ;;--------------------------------------------------------------------------
141 (test-section "folding")
142
143 (test* "fold" 55
144 (fold + 0 '(1 2 3 4 5 6 7 8 9 10)))
145 (test* "fold" '(e d c b a)
146 (fold cons '() '(a b c d e)))
147 (test* "fold" 3
148 (fold (^[x c] (if (symbol? x) (+ c 1) c))
149 0
150 '(a 3 b 8 c 9)))
151 (test* "fold" '(c 3 b 2 a 1)
152 (fold list* '() '(a b c) '(1 2 3 4 5)))
153 (test* "fold-right" '(1 2 3 4 5)
154 (fold-right cons '() '(1 2 3 4 5)))
155 (test* "fold-right" '(2 4 6)
156 (fold-right (^[x l] (if (even? x) (cons x l) l))
157 '()
158 '(1 2 3 4 5 6 7)))
159 (test* "fold-right" '(a 1 b 2 c 3)
160 (fold-right list* '() '(a b c) '(1 2 3 4 5)))
161
162 (test* "fold-left" '(((z . a) . b) . c)
163 (fold-left cons 'z '(a b c)))
164 (test* "fold-left" '(c b a . z)
165 (fold-left (^[a b] (cons b a)) 'z '(a b c)))
166 (test* "fold-left" 21
167 (fold-left + 0 '(1 2 3) '(4 5 6)))
168 (test* "fold-left" '(((z a A) b B) c C)
169 (fold-left list 'z '(a b c) '(A B C)))
170 (test* "count" 3 (count even? '(3 1 4 1 5 9 2 6 5)))
171 (test* "count" 3
172 (count < '(1 2 4 8) '(2 4 6 8 10 12 14 16)))
173 (test* "count" 2
174 (count < '(3 1 4 1) '#0=(1 10 . #0#)))
175 (test* "reduce" 55 (reduce + 0 (iota 10 1)))
176 (test* "reduce-right" '(1 2 3 4 5 6 7 8 9 . 10)
177 (reduce-right cons 0 (iota 10 1)))
178 (test* "concatenate" '(1 2 3 4 5)
179 (concatenate '((1 2 3) () (4 5))))
180 (test* "concatenate" '(1 2 3 4 . 5)
181 (concatenate '((1 2 3) () (4) 5)))
182 (test* "concatenate!" '(1 2 3 4 5)
183 (concatenate (list (list 1 2 3) '() (list 4 5))))
184 (test* "concatenate!" '(1 2 3 4 . 5)
185 (concatenate (list (list 1 2 3) '() (list 4) 5)))
186 (test* "append-reverse" '(1 2 3 4 5)
187 (append-reverse '(3 2 1) '(4 5)))
188 (test* "append-reverse" '(1 2 3 4 . 5)
189 (append-reverse '(4 3 2 1) 5))
190 (test* "append-reverse!" '(1 2 3 4 5)
191 (append-reverse! (list 3 2 1) (list 4 5)))
192 (test* "append-reverse!" '(1 2 3 4 . 5)
193 (append-reverse! (list 4 3 2 1) 5))
194 (test* "append-map" '(1 -1 3 -3 5 -5)
195 (append-map (^x (list x (- x))) '(1 3 5)))
196 (test* "append-map" '(1 -2 3 -4 5 -6)
197 (append-map (^[x y] (list x (- y)))
198 '(1 3 5) '(2 4 6 8)))
199 (test* "append-map!" '(1 -2 3 -4 5 -6)
200 (append-map! (^[x y] (list x (- y)))
201 '(1 3 5) '(2 4 6 8)))
202
203 ;;--------------------------------------------------------------------------
204 (test-section "filtering")
205
206 (test* "filter-map" '(1 9 49)
207 (filter-map (^x (and (number? x) (* x x)))
208 '(a 1 b 3 c 7)))
209 (test* "filter" '(0 8 8 -4)
210 (filter even? '(0 7 8 8 9 -4)))
211 (test* "remove" '(7 43)
212 (remove even? '(0 7 8 8 43 -4)))
213
214 (test* "filter!" '(0 8 8 -4)
215 (filter even? (list 0 7 8 8 9 -4)))
216 (test* "remove!" '(7 43)
217 (remove! even? (list 0 7 8 8 43 -4)))
218
219 ;;--------------------------------------------------------------------------
220 (test-section "searching")
221
222 (test* "find" 4
223 (find even? '(3 1 4 1 5 9)))
224 (test* "find" #f
225 (find even? '(3 1 1 5 9)))
226 (test* "find-tail" '(-8 -5 0 0)
227 (find-tail even? '(3 1 -8 -5 0 0)))
228 (test* "find-tail" #f
229 (find-tail even? '(3 1 -9 -5 1 -1)))
230
231 (test* "member" #f
232 (member '"b" '("a" "b" "c") eq?))
233 (test* "member" '(2.0 3.0)
234 (member '2.0 '(1.0 2.0 3.0) eqv?))
235 (test* "member" '("b" "c")
236 (member '"b" '("a" "b" "c") equal?))
237 (test* "member" '("b" "c")
238 (member '"b" '("a" "b" "c")))
239 (test* "member" '("b" "c")
240 (member '"b" '("a" "b" "c") string=?))
241 (test* "member" '("b" "c")
242 (member '"B" '("a" "b" "c") string-ci=?))
243 (test* "delete" '(b c d e)
244 (delete 'a '(a b a c a d a e a) eq?))
245 (test* "delete" '(b c d e)
246 (delete 'a '(a b a c a d a e) eq?))
247 (test* "delete" '(b c d e)
248 (delete 'a '(b a c a d a e a) eq?))
249 (test* "delete" '(b c d e)
250 (delete 'a '(b a c a d a e) eq?))
251 (test* "delete" '(b c d e)
252 (delete 'a '(b c d e) eq?))
253 (test* "delete" '(b c d e . f)
254 (delete 'a '(b a c a d a e . f) eq?))
255 (test* "delete" '(2.0 3.0 4.0 5.0)
256 (delete '1.0 '(1.0 2.0 1.0 3.0 4.0 1.0 5.0) eqv?))
257 (test* "delete" '("b" "c" "d" "e")
258 (delete "a" '("a" "b" "a" "c" "d" "a" "e")))
259 (test* "delete" '("b" "c" "d" "e")
260 (delete "a" '("a" "b" "a" "c" "d" "a" "e") string=?))
261 (test* "delete" '("b" "c" "d" "e")
262 (delete "A" '("a" "b" "a" "c" "d" "a" "e") string-ci=?))
263 (test* "delete!" '(b c d e)
264 (delete! 'a (list-copy '(a b a c a d a e a)) eq?))
265 (test* "delete!" '(b c d e)
266 (delete! 'a (list-copy '(a b a c a d a e)) eq?))
267 (test* "delete!" '(b c d e)
268 (delete! 'a (list-copy '(b a c a d a e a)) eq?))
269 (test* "delete!" '(b c d e)
270 (delete! 'a (list-copy '(b a c a d a e)) eq?))
271 (test* "delete!" '(b c d e)
272 (delete! 'a (list-copy '(b c d e)) eq?))
273 (test* "delete!" '(b c d e . f)
274 (delete! 'a (list-copy '(b c a d e . f)) eq?))
275 (test* "delete!" '(2.0 3.0 4.0 5.0)
276 (delete! '1.0 (list 1.0 2.0 1.0 3.0 4.0 1.0 5.0) eqv?))
277 (test* "delete!" '("b" "c" "d" "e")
278 (delete! "a" (list "a" "b" "a" "c" "d" "a" "e")))
279 (test* "delete!" '("b" "c" "d" "e")
280 (delete! "a" (list "a" "b" "a" "c" "d" "a" "e") string=?))
281 (test* "delete!" '("b" "c" "d" "e")
282 (delete! "A" (list "a" "b" "a" "c" "d" "a" "e") string-ci=?))
283 (test* "delete-duplicates" '(a b c d e)
284 (delete-duplicates '(a b c d e) eq?))
285 (test* "delete-duplicates" '(a b c d e)
286 (delete-duplicates '(a b a c b a d d a e) eq?))
287 (test* "delete-duplicates" '(a b c d . e)
288 (delete-duplicates '(a b a c b a d d a . e) eq?))
289 (test* "delete-duplicates" '(1.0 2.0 3.0 4.0 5.0)
290 (delete-duplicates '(1.0 2.0 1.0 2.0 3.0 3.0 4.0 1.0 5.0) eqv?))
291 (test* "delete-duplicates" '("a" "b" "c" "d" "e")
292 (delete-duplicates '("a" "b" "b" "a" "b" "c" "d" "a" "e")))
293 (test* "delete-duplicates" '("a" "b" "c" "d" "e")
294 (delete-duplicates '("a" "b" "a" "a" "c" "d" "a" "e") string=?))
295 (test* "delete-duplicates" '("A" "b" "c" "d" "e")
296 (delete-duplicates '("A" "b" "a" "B" "c" "d" "a" "e") string-ci=?))
297 (test* "delete-duplicates!" '(a b c d e)
298 (delete-duplicates! (list-copy '(a b c d e)) eq?))
299 (test* "delete-duplicates!" '(a b c d e)
300 (delete-duplicates! (list-copy '(a b a c b a d d a e)) eq?))
301 (test* "delete-duplicates!" '(1.0 2.0 3.0 4.0 5.0)
302 (delete-duplicates! (list 1.0 2.0 1.0 2.0 3.0 3.0 4.0 1.0 5.0) eqv?))
303 (test* "delete-duplicates!" '("a" "b" "c" "d" "e")
304 (delete-duplicates! (list "a" "b" "b" "a" "b" "c" "d" "a" "e")))
305 (test* "delete-duplicates!" '("a" "b" "c" "d" "e")
306 (delete-duplicates! (list "a" "b" "a" "a" "c" "d" "a" "e") string=?))
307 (test* "delete-duplicates!" '("A" "b" "c" "d" "e")
308 (delete-duplicates! (list "A" "b" "a" "B" "c" "d" "a" "e") string-ci=?))
309
310 (test* "any" #f (any even? '()))
311
312 (test* "any" #f (any even? '(1 3)))
313
314 (test* "any" #t (any even? '(1 2)))
315
316 (test* "any" 1 (any string->number '("1" "a")))
317
318 (test* "any" 1 (any string->number '("1" "2")))
319
320 (test* "any" 1
321 (any string->number '("1" "2") '(10 10)))
322
323 (test* "any" 1
324 (any string->number '("1" "2") '(10)))
325
326 (test* "any" #f
327 (any string->number '("1" "2") '()))
328
329 (test* "every" #t (every odd? '()))
330
331 (test* "every" #t (every odd? '(1 3)))
332
333 (test* "every" #f (every odd? '(1 2)))
334
335 (test* "every" #f (every string->number '("1" "a")))
336
337 (test* "every" 2 (every string->number '("1" "2")))
338
339 (test* "every" 2
340 (every string->number '("1" "2") '(10 10)))
341
342 (test* "every" 1
343 (every string->number '("1" "2") '(10)))
344
345 (test* "every" #t
346 (every string->number '("1" "2") '()))
347
348 ;;--------------------------------------------------------------------------
349 (test-section "take and drop")
350
351 (test* "take" '(a b) (take '(a b c d e) 2))
352 (test* "drop" '(c d e) (drop '(a b c d e) 2))
353 (test* "take" '(1 2) (take '(1 2 3 . d) 2))
354 (test* "drop" '(3 . d) (drop '(1 2 3 . d) 2))
355 (test* "take" '(1 2 3) (take '(1 2 3 . d) 3))
356 (test* "drop" 'd (drop '(1 2 3 . d) 3))
357 (test* "take-right" '(d e) (take-right '(a b c d e) 2))
358 (test* "drop-right" '(a b c) (drop-right '(a b c d e) 2))
359 (test* "take-right" '(2 3 . d) (take-right '(1 2 3 . d) 2))
360 (test* "drop-right" '(1) (drop-right '(1 2 3 . d) 2))
361 (test* "take-right" 'd (take-right '(1 2 3 . d) 0))
362 (test* "drop-right" '(1 2 3) (drop-right '(1 2 3 . d) 0))
363 (test* "take!" '(1 2) (take! (list-copy '(1 2 3 . d)) 2))
364 (test* "drop-right!"'(1 2) (drop-right! (list-copy '(1 2 3 . d)) 1))
365 (test* "split-at" '((a b c) (d e f g h))
366 (call-with-values (^[] (split-at '(a b c d e f g h) 3))
367 list))
368 (test* "split-at!" '((a b c) (d e f g h))
369 (call-with-values
370 (^[] (split-at! (list 'a 'b 'c 'd 'e 'f 'g 'h) 3))
371 list))
372 (test* "partition" '((one four five) (2 3 6))
373 (values->list (partition symbol? '(one 2 3 four five 6))))
374
375 (test* "split-at* (normal)" '((a b c) (d))
376 (receive r (split-at* '(a b c d) 3) r))
377 (test* "split-at* (boundary)" '(() (a b c d))
378 (receive r (split-at* '(a b c d) 0) r))
379 (test* "split-at* (boundary)" '((a b c d) ())
380 (receive r (split-at* '(a b c d) 4) r))
381 (test* "split-at* (error)" (test-error)
382 (receive r (split-at* '(a b c d) -1) r))
383 (test* "split-at* (shorten)" '((a b c d) ())
384 (receive r (split-at* '(a b c d) 5) r))
385 (test* "split-at* (fill)" '((a b c d #f #f) ())
386 (receive r (split-at* '(a b c d) 6 #t) r))
387 (test* "split-at* (fill)" '((a b c d z z) ())
388 (receive r (split-at* '(a b c d) 6 #t 'z) r))
389
390 (test* "take* (normal)" '(a b c) (take* '(a b c d) 3))
391 (test* "take* (boundary)" '() (take* '(a b c d) 0))
392 (test* "take* (boundary)" '(a b c d) (take* '(a b c d) 4))
393 (test* "take* (error)" (test-error) (take* '(a b c d) -1))
394 (test* "take* (shorten)" '(a b c d) (take* '(a b c d) 5))
395 (test* "take* (fill)" '(a b c d #f #f) (take* '(a b c d) 6 #t))
396 (test* "take* (fill)" '(a b c d z z) (take* '(a b c d) 6 #t 'z))
397
398 (test* "drop* (normal)" '(c d) (drop* '(a b c d) 2))
399 (test* "drop* (boundary)" '(a b c d) (drop* '(a b c d) 0))
400 (test* "drop* (boundary)" '() (drop* '(a b c d) 4))
401 (test* "drop* (error)" (test-error) (drop* '(a b c d) -3))
402 (test* "drop* (past)" '() (drop* '(a b c d) 5))
403
404 (test* "take-right* (normal)" '(b c d) (take-right* '(a b c d) 3))
405 (test* "take-right* (boundary)" '() (take-right* '(a b c d) 0))
406 (test* "take-right* (boundary)" '(a b c d) (take-right* '(a b c d) 4))
407 (test* "take-right* (error)" (test-error) (take-right* '(a b c d) -1))
408 (test* "take-right* (shorten)" '(a b c d) (take-right* '(a b c d) 6))
409 (test* "take-right* (fill)" '(z z a b c d) (take-right* '(a b c d) 6 #t 'z))
410
411 (test* "take-right* (long)" '(a b c d e f)
412 (take-right* (fold (^[i r] (list* 'a 'b 'c 'd 'e 'f r))
413 '() (iota 1668))
414 6))
415
416 (test* "drop-right* (normal)" '(a b c) (drop-right* '(a b c d) 1))
417 (test* "drop-right* (boundary)" '() (drop-right* '(a b c d) 4))
418 (test* "drop-right* (boundary)" '(a b c d) (drop-right* '(a b c d) 0))
419 (test* "drop-right* (error)" (test-error) (drop-right* '(a b c d) -1))
420 (test* "drop-right* (past)" '() (drop-right* '(a b c d) 7))
421
422 ;;--------------------------------------------------------------------------
423 ;; slice and intercept
424
425 (test* "slices (normal)" '((0 1 2 3) (4 5 6 7) (8 9 10 11) (12 13 14 15))
426 (slices (iota 16) 4))
427 (test* "slices (boundary)" '()
428 (slices '() 4))
429 (test* "slices (short)" '((0 1 2 3) (4 5 6 7) (8 9 10 11) (12))
430 (slices (iota 13) 4))
431 (test* "slices (short)" '((0 1))
432 (slices (iota 2) 4))
433 (test* "slices (fill)" '((0 1 2 3) (4 5 6 7) (8 9 10 11) (12 #f #f #f))
434 (slices (iota 13) 4 #t))
435 (test* "slices (fill)" '((0 1 2 3) (4 5 6 7) (8 9 10 11) (12 -1 -1 -1))
436 (slices (iota 13) 4 #t -1))
437
438 (test* "intersperse" '(1 + 2 + 3) (intersperse '+ '(1 2 3)))
439 (test* "intersperse" '(1 + 2) (intersperse '+ '(1 2)))
440 (test* "intersperse" '(1) (intersperse '+ '(1)))
441 (test* "intersperse" '() (intersperse '+ '()))
442
443 ;;--------------------------------------------------------------------------
444 ;; associative lists
445
446 (test* "assq" '(a 1) (assq 'a '((a 1) (b 2) (c 3))))
447 (test* "assq" #f (assq 'd '((a 1) (b 2) (c 3))))
448 (test* "assq" #f (assq (list 'a) '(((a)) ((b)) ((c)))))
449 (test* "assv" '(b 2) (assv 'b '((a 1) (b 2) (c 3))))
450 (test* "assv" #f (assv 'd '((a 1) (b 2) (c 3))))
451 (test* "assv" #f (assv (list 'a) '(((a)) ((b)) ((c)))))
452 (test* "assoc" '((a)) (assoc (list 'a) '(((a)) ((b)) ((c)))))
453 (test* "assoc" '("a") (assoc "a" '(("c") ("b") ("a"))))
454 (test* "assoc" '("a") (assoc "A" '(("c") ("b") ("a")) string-ci=?))
455 (test* "acons" '((1 . 2) . 3) (acons 1 2 3))
456 (test* "alist-copy" '((a 1) (a 2))
457 (let* ([x '((b 2) (a 1))]
458 [y (alist-copy x)])
459 (set-cdr! (assq 'a y) (list 2))
460 (list (assq 'a x) (assq 'a y))))
461 (test* "alist-copy (circular)" (test-error)
462 (alist-copy '((a . 1) . #0=((b . 2) (c . 3) . #0#))))
463 (test* "alist-copy (dotted)" (test-error)
464 (alist-copy '((a . 1) (b . 2) . c)))
465 (test* "alist-copy (non-pair)" '(((a . 1) b (c . 2))
466 ((a . 1) b (c . 3)))
467 (let* ([orig '((a . 1) b (c . 2))]
468 [copy (alist-copy orig)])
469 (set-cdr! (assq 'c copy) 3)
470 (list orig copy)))
471
472 (test* "alist-delete" '((a 1) (b 2) (c 3))
473 (alist-delete 'x '((a 1) (b 2) (c 3))))
474 (test* "alist-delete" '((b 2) (c 3))
475 (alist-delete 'a '((a 1) (b 2) (c 3))))
476 (test* "alist-delete" '((a 1) (c 3))
477 (alist-delete 'b '((a 1) (b 2) (c 3))))
478 (test* "alist-delete" '((a 1) (b 2))
479 (alist-delete 'c '((a 1) (b 2) (c 3))))
480 (test* "alist-delete!" '((a 1) (b 2) (c 3))
481 (rlet1 l (alist-copy '((a 1) (b 2) (c 3)))
482 (alist-delete! 'x l)))
483 (test* "alist-delete!" '((b 2) (c 3))
484 (alist-delete! 'a (alist-copy '((a 1) (b 2) (c 3)))))
485 (test* "alist-delete!" '(z (b 2) (c 3))
486 (alist-delete! 'a (list-copy '(z (a 1) (b 2) (c 3)))))
487 (test* "alist-delete!" '(z (b 2) (c 3))
488 (alist-delete! 'a (list-copy '((a 1) z (b 2) (c 3)))))
489 (test* "alist-delete!" '((a 1) (c 3))
490 (rlet1 l (alist-copy '((a 1) (b 2) (c 3)))
491 (alist-delete! 'b l)))
492 (test* "alist-delete!" '((a 1) (b 2))
493 (rlet1 l (alist-copy '((a 1) (b 2) (c 3)))
494 (alist-delete! 'c l)))
495
496 (test* "rassoc" '(5 . "b")
497 (rassoc "b" '((3 . "a") (5 . "b"))))
498 (test* "rassq" '(5 . b)
499 (rassq 'b '((3 . a) (5 . b))))
500 (test* "rassv" '("b" . 5)
501 (rassoc 5 '(("a" . 3) ("b" . 5))))
502
503 (test* "assoc-ref" 5
504 (assoc-ref '(("a" . 3) ("b" . 5)) "b"))
505 (test* "assoc-ref" 7
506 (assoc-ref '(("a" . 3) ("b" . 5)) "c" 7))
507 (test* "assq-ref" 5
508 (assq-ref '((a . 3) (b . 5)) 'b))
509 (test* "assq-ref" 7
510 (assq-ref '((a . 3) (b . 5)) 'c 7))
511 (test* "assv-ref" 'b
512 (assv-ref '((3 . a) (5 . b)) 5))
513 (test* "assv-ref" 'c
514 (assv-ref '((3 . a) (5 . b)) 7 'c))
515
516 (test* "rassoc-ref" 5
517 (rassoc-ref '((3 . "a") (5 . "b")) "b"))
518 (test* "rassoc-ref" 7
519 (rassoc-ref '((3 . "a") (5 . "b")) "c" 7))
520 (test* "rassq-ref" 5
521 (rassq-ref '((3 . a) (5 . b)) 'b))
522 (test* "rassq-ref" #f
523 (rassq-ref '((3 . a) (5 . b)) 'c))
524 (test* "rassv-ref" 'b
525 (rassv-ref '((a . 3) (b . 5)) 5))
526 (test* "rassv-ref" #f
527 (rassv-ref '((a . 3) (b . 5)) 7))
528
529 (test* "assoc-set!" '(("a" . 3) ("b" . 9))
530 (assoc-set! (list (cons "a" 3) (cons "b" 5)) "b" 9))
531 (test* "assoc-set!" '(("c" . 9) ("a" . 3) ("b" . 5))
532 (assoc-set! (list (cons "a" 3) (cons "b" 5)) "c" 9))
533 (test* "assq-set!" '((a . 3) (b . 9))
534 (assq-set! (list (cons 'a 3) (cons 'b 5)) 'b 9))
535 (test* "assq-set!" '((c . 9) (a . 3) (b . 5))
536 (assq-set! (list (cons 'a 3) (cons 'b 5)) 'c 9))
537 (test* "assv-set!" '((3 . a) (5 . c))
538 (assv-set! (list (cons 3 'a) (cons 5 'b)) 5 'c))
539 (test* "assv-set!" '((9 . c) (3 . a) (5 . b))
540 (assv-set! (list (cons 3 'a) (cons 5 'b)) 9 'c))
541
542 (test* "assoc-adjoin" '((c . 3) (a . 1) (b . 2))
543 (assoc-adjoin '((a . 1) (b . 2)) 'c 3))
544 (test* "assoc-adjoin" '((a . 4) (b . 2))
545 (assoc-adjoin '((a . 1) (b . 2)) 'a 4))
546 (test* "assoc-adjoin" '((a . 1) (b . 5))
547 (assoc-adjoin '((a . 1) (b . 2)) 'b 5))
548 (test* "assoc-adjoin" '((a . 1))
549 (assoc-adjoin '() 'a 1))
550
551 (test* "alist-merge" '()
552 (alist-merge + '()))
553 (test* "alist-merge" '((a . 1) (b . 2))
554 (alist-merge + '((a . 1) (b . 2))))
555 (test* "alist-merge" '((a . 1) (b . 2) (c . 3) (d . 4))
556 (alist-merge + '((a . 1) (b . 2)) '((c . 3) (d . 4))))
557 (test* "alist-merge" '((a . 5) (b . 2) (d . 3))
558 (alist-merge + '((a . 1) (b . 2)) '((d . 3) (a . 4))))
559 (test* "alist-merge" '((a . 1) (b . 6) (c . 3))
560 (alist-merge + '((a . 1) (b . 2)) '((c . 3) (b . 4))))
561
562 (test* "alist-merge" '((a 1 5) (b 2 4) (c 3))
563 (alist-merge append '((a 1) (b 2)) '((c 3) (b 4)) '() '((a 5))))
564 (test* "alist-merge" '(("a" 1 4) ("b" 2 3))
565 (alist-merge string=? append '(("a" 1) ("b" 2)) '(("b" 3) ("a" 4))))
566
567
568 (test* "assoc-update-in" '((a (aa . 1) (ab . 2))
569 (b (ba . 3) (bb (bba . 104) (bbb . 5))))
570 (assoc-update-in '((a (aa . 1) (ab . 2))
571 (b (ba . 3) (bb (bba . 4) (bbb . 5))))
572 '(b bb bba)
573 (lambda (x) (+ x 100))
574 0
575 eq?))
576 (test* "assoc-update-in" '((a (aa . 1) (ab . 2))
577 (b (ba . 3) (bb (bbc . 100) (bba . 4) (bbb . 5))))
578 (assoc-update-in '((a (aa . 1) (ab . 2))
579 (b (ba . 3) (bb (bba . 4) (bbb . 5))))
580 '(b bb bbc)
581 (lambda (x) (+ x 100))
582 0
583 eq?))
584 (test* "assoc-update-in" '((c (d (e . 100)))
585 (a (aa . 1) (ab . 2))
586 (b (ba . 3) (bb (bba . 4) (bbb . 5))))
587 (assoc-update-in '((a (aa . 1) (ab . 2))
588 (b (ba . 3) (bb (bba . 4) (bbb . 5))))
589 '(c d e)
590 (lambda (x) (+ x 100))
591 0
592 eq?))
593 (test* "assoc-update-in" '((a (aa . 1)))
594 (assoc-update-in '()
595 '(a aa)
596 -
597 -1))
598 (test* "assoc-update-in" '(("a" ("aa" . 101) ("ab" . 2))
599 ("b" ("ba" . 3)
600 ("bb" ("bba" . 4) ("bbb" . 5))))
601 (assoc-update-in '(("a" ("aa" . 1) ("ab" . 2))
602 ("b" ("ba" . 3) ("bb" ("bba" . 4) ("bbb" . 5))))
603 '("a" "aa")
604 (lambda (x) (+ x 100))
605 0
606 string=?))
607
608 ;;--------------------------------------------------------------------------
609
610 (test-section "circular list and equality")
611
612 ;; At this moment we haven't tested #n=, #n# notation, so we build
613 ;; circular structure at runtime.
614
615 (let ()
616 (define (cdr-cycle . lis)
617 (set-cdr! (last-pair lis) lis)
618 lis)
619 (define (car-cycle . lis)
620 (if (null? lis)
621 lis
622 (let* ([head (cons #f (car lis))]
623 [next (let loop ([lis (cdr lis)])
624 (if (null? lis)
625 head
626 (cons (loop (cdr lis)) (car lis))))])
627 (set-car! head next)
628 head)))
629
630 (test* "equal? w/ cdr-cycle 1" #t
631 (equal? (cdr-cycle 1 2 3) (cdr-cycle 1 2 3 1 2 3)))
632 (test* "equal? w/ cdr-cycle 2" #f
633 (equal? (cdr-cycle 1 2 3) (cdr-cycle 1 2 3 1 2)))
634 (test* "equal? w/ cdr-cycle 3" #f
635 (equal? (cdr-cycle 1 2 3)
636 (apply append (make-list 10000 (list 1 2 3)))))
637 (test* "equal? w/ cdr-cycle 4" #t
638 (equal? (cdr-cycle 1 2 3)
639 (apply cdr-cycle (apply append (make-list 10000 (list 1 2 3))))))
640 (test* "equal? w/ cdr-cycle 5" #f
641 (equal? (cdr-cycle 1 2 3) '(1)))
642
643 (test* "equal? w/ car-cycle 1" #t
644 (equal? (car-cycle 1) (car-cycle 1)))
645 (test* "equal? w/ car-cycle 2" #f
646 (equal? (car-cycle 1) (car-cycle 2)))
647 (test* "equal? w/ car-cycle 3" #t
648 (equal? (car-cycle 1 2 3) (car-cycle 1 2 3 1 2 3)))
649 (test* "equal? w/ car-cycle 4" #f
650 (equal? (car-cycle 1 2 3) (car-cycle 1 2 3 1 2)))
651
652 (test* "equal? w/ car/cdr-cycle 1" #t
653 (equal? (cdr-cycle (car-cycle 1))
654 (cdr-cycle (car-cycle 1))))
655 (test* "equal? w/ car/cdr-cycle 2" #f
656 (equal? (cdr-cycle (car-cycle 1))
657 (car-cycle (cdr-cycle 1))))
658 (test* "equal? w/ car/cdr-cycle 3" #t
659 (equal? (cdr-cycle (car-cycle 1 2 3))
660 (cdr-cycle (car-cycle 1 2 3 1 2 3))))
661 (test* "equal? w/ car/cdr-cycle 4" #t
662 (equal? (cdr-cycle (car-cycle 1 2 3))
663 (cdr-cycle (car-cycle 1 2 3) (car-cycle 1 2 3 1 2 3))))
664 (test* "equal? w/ car/cdr-cycle 4" #f
665 (equal? (cdr-cycle (car-cycle 1 2 3))
666 (cdr-cycle (car-cycle 1 2 3) (car-cycle 1 2 3 1 2 3 1))))
667 )
668
669 ;;--------------------------------------------------------------------------
670 (test-section "monotonic-merge")
671
672 ;; monotonic-merge is a core function to implement Dylan-style class
673 ;; precedence list. those tests are taken from examples in
674 ;; http://www.webcom.com/~haahr/dylan/linearization-oopsla96.html
675
676 (test "monotonic-merge"
677 '(menu choice-widget popup-mixin object)
678 (^[] (monotonic-merge
679 '((menu choice-widget object)
680 (menu popup-mixin)
681 (popup-mixin object)))))
682
683 (test "monotonic-merge"
684 '(pedal-wheel-boat engineless small-catamaran
685 small-multihull day-boat wheel-boat boat object)
686 (^[] (monotonic-merge
687 '((pedal-wheel-boat engineless day-boat wheel-boat boat object)
688 (small-catamaran small-multihull day-boat boat object)
689 (pedal-wheel-boat small-catamaran)))))
690
691 (test "monotonic-merge"
692 #f
693 (^[] (monotonic-merge
694 '((hv-grid vh-grid)
695 (hv-grid horizontal-grid vertical-grid grid-layout object)
696 (vh-grid vertical-grid horizontal-grid grid-layout object)))))
697
698 ;;--------------------------------------------------------------------------
699 (test-section "immutable pairs")
700
701 (test* "ipair" #t (ipair? (ipair 1 2)))
702 (test* "ipair" #f (ipair? '()))
703 (test* "ipair" #f (ipair? (cons 1 2)))
704 (test* "ipair" '(1 . 2)
705 (let1 p (ipair 1 2)
706 (cons (car p) (cdr p))))
707
708 (test* "ilist" '(#t #t #t #f)
709 (let1 p (ilist 1 2 3)
710 (list (ipair? p)
711 (ipair? (cdr p))
712 (ipair? (cddr p))
713 (ipair? (cdddr p)))))
714 (test* "ilist" '() (ilist))
715
716 (test* "ipair immutability" (test-error)
717 (let1 p (ipair 1 2)
718 (set-car! p 5)))
719 (test* "ipair immutability" (test-error)
720 (let1 p (ipair 1 2)
721 (set-cdr! p 5)))
722
723 ;;--------------------------------------------------------------------------
724 (test-section "extended pairs")
725
726 (let ()
727 (test* "extended pair" #f (extended-pair? (cons 1 2)))
728 ;(test* "extended pair" #t (extended-pair? '(1 2)))
729 (test* "extended pair" #t (extended-pair? (extended-cons 1 2)))
730 (test* "pair-attributes" '() (pair-attributes (cons 1 2)))
731 (test* "pair-attributes" '() (pair-attributes (ipair 1 2)))
732 (test* "pair-attributes" '() (pair-attributes (extended-cons 1 2)))
733 (test* "pair-attributes" '((a . b) (c d))
734 (pair-attributes (extended-cons 1 2 '((a . b) (c d)))))
735 (test* "pair-attributes-get/set!" 'hoi
736 (let1 z (extended-cons 1 2)
737 (pair-attribute-set! z 'foo 'hoi)
738 (pair-attribute-get z 'foo)))
739 (test* "pair-attributes-get/set!" 'poi
740 (let1 z (extended-cons 1 2 '((foo . hoi)))
741 (pair-attribute-set! z 'foo 'poi)
742 (pair-attribute-get z 'foo)))
743 (test* "set-car! to extended pair" '(3 . 2)
744 (rlet1 z (extended-cons 1 2)
745 (set-car! z 3)))
746 (test* "set-cdr! to extended pair" '(1 . 4)
747 (rlet1 z (extended-cons 1 2)
748 (set-cdr! z 4)))
749 )
750
751 (let* ((data1 '((a . b) (c . d)))
752 (data2 (list (cons 'A 'B) (cons 'C 'D)))
753 (p1 (extended-cons 1 2 data1))
754 (p2 (extended-cons 1 2 data2)))
755 ;; extended-pair-set! shouldn't clobber the original list
756 (test* "pair-attribute-set! noclobber 1" '((e . z) (a . b) (c . y))
757 (begin
758 (pair-attribute-set! p1 'e 'z)
759 (pair-attribute-set! p1 'c 'y)
760 (pair-attributes p1)))
761 (test* "pair-attribute-set! noclobber 2" '((E . Z) (A . Y) (C . D))
762 (begin
763 (pair-attribute-set! p2 'A 'Y)
764 (pair-attribute-set! p2 'E 'Z)
765 (pair-attributes p2)))
766 (test* "pair-attribute-set! noclobber" #t
767 (equal? data1 '((a . b) (c . d))))
768 (test* "pair-attribute-set! noclobber" #t
769 (equal? data2 '((A . B) (C . D))))
770 ;; prevent optimization
771 (set! data1 #f)
772 (set! data2 #f)
773 )
774
775 (test-end)