Debian Codegraph @

test-suite/tests/srfi-1.test

raw
1 ;;;; srfi-1.test --- Test suite for Guile's SRFI-1 functions. -*- scheme -*-
2 ;;;;
3 ;;;; Copyright 2003-2006, 2008-2011, 2014, 2020 Free Software Foundation, Inc.
4 ;;;;
5 ;;;; This library is free software; you can redistribute it and/or
6 ;;;; modify it under the terms of the GNU Lesser General Public
7 ;;;; License as published by the Free Software Foundation; either
8 ;;;; version 3 of the License, or (at your option) any later version.
9 ;;;;
10 ;;;; This library is distributed in the hope that it will be useful,
11 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ;;;; Lesser General Public License for more details.
14 ;;;;
15 ;;;; You should have received a copy of the GNU Lesser General Public
16 ;;;; License along with this library; if not, write to the Free Software
17 ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
19 (define-module (test-srfi-1)
20 #:use-module (test-suite lib)
21 #:use-module (ice-9 copy-tree)
22 #:use-module (srfi srfi-1))
23
24 (define list+-bad-arg-exception
25 '(wrong-type-arg . "^Argument not a proper or circular list"))
26
27 (define (ref-delete x lst . proc)
28 "Reference implemenation of srfi-1 `delete'."
29 (set! proc (if (null? proc) equal? (car proc)))
30 (do ((ret '())
31 (lst lst (cdr lst)))
32 ((null? lst)
33 (reverse! ret))
34 (if (not (proc x (car lst)))
35 (set! ret (cons (car lst) ret)))))
36
37 (define (ref-delete-duplicates lst . proc)
38 "Reference implemenation of srfi-1 `delete-duplicates'."
39 (set! proc (if (null? proc) equal? (car proc)))
40 (if (null? lst)
41 '()
42 (do ((keep '()))
43 ((null? lst)
44 (reverse! keep))
45 (let ((elem (car lst)))
46 (set! keep (cons elem keep))
47 (set! lst (ref-delete elem lst proc))))))
48
49
50 ;;
51 ;; alist-copy
52 ;;
53
54 (with-test-prefix "alist-copy"
55
56 ;; return a list which is the pairs making up alist A, the spine and cells
57 (define (alist-pairs a)
58 (let more ((a a)
59 (result a))
60 (if (pair? a)
61 (more (cdr a) (cons a result))
62 result)))
63
64 ;; return a list of the elements common to lists X and Y, compared with eq?
65 (define (common-elements x y)
66 (if (null? x)
67 '()
68 (if (memq (car x) y)
69 (cons (car x) (common-elements (cdr x) y))
70 (common-elements (cdr x) y))))
71
72 ;; validate an alist-copy of OLD to NEW
73 ;; lists must be equal, and must comprise new pairs
74 (define (valid-alist-copy? old new)
75 (and (equal? old new)
76 (null? (common-elements old new))))
77
78 (pass-if-exception "too few args" exception:wrong-num-args
79 (alist-copy))
80
81 (pass-if-exception "too many args" exception:wrong-num-args
82 (alist-copy '() '()))
83
84 (let ((old '()))
85 (pass-if old (valid-alist-copy? old (alist-copy old))))
86
87 (let ((old '((1 . 2))))
88 (pass-if old (valid-alist-copy? old (alist-copy old))))
89
90 (let ((old '((1 . 2) (3 . 4))))
91 (pass-if old (valid-alist-copy? old (alist-copy old))))
92
93 (let ((old '((1 . 2) (3 . 4) (5 . 6))))
94 (pass-if old (valid-alist-copy? old (alist-copy old)))))
95
96 ;;
97 ;; alist-delete
98 ;;
99
100 (with-test-prefix "alist-delete"
101
102 (pass-if "equality call arg order"
103 (let ((good #f))
104 (alist-delete 'k '((ak . 123))
105 (lambda (k ak)
106 (if (and (eq? k 'k) (eq? ak 'ak))
107 (set! good #t))))
108 good))
109
110 (pass-if "delete keys greater than 5"
111 (equal? '((4 . x) (5 . y))
112 (alist-delete 5 '((4 . x) (5 . y) (6 . z)) <)))
113
114 (pass-if "empty"
115 (equal? '() (alist-delete 'x '())))
116
117 (pass-if "(y)"
118 (equal? '() (alist-delete 'y '((y . 1)))))
119
120 (pass-if "(n)"
121 (equal? '((n . 1)) (alist-delete 'y '((n . 1)))))
122
123 (pass-if "(y y)"
124 (equal? '() (alist-delete 'y '((y . 1) (y . 2)))))
125
126 (pass-if "(n y)"
127 (equal? '((n . 1)) (alist-delete 'y '((n . 1) (y . 2)))))
128
129 (pass-if "(y n)"
130 (equal? '((n . 2)) (alist-delete 'y '((y . 1) (n . 2)))))
131
132 (pass-if "(n n)"
133 (equal? '((n . 1) (n . 2)) (alist-delete 'y '((n . 1) (n . 2)))))
134
135 (pass-if "(y y y)"
136 (equal? '() (alist-delete 'y '((y . 1) (y . 2) (y . 3)))))
137
138 (pass-if "(n y y)"
139 (equal? '((n . 1)) (alist-delete 'y '((n . 1) (y . 2) (y . 3)))))
140
141 (pass-if "(y n y)"
142 (equal? '((n . 2)) (alist-delete 'y '((y . 1) (n . 2) (y . 3)))))
143
144 (pass-if "(n n y)"
145 (equal? '((n . 1) (n . 2)) (alist-delete 'y '((n . 1) (n . 2) (y . 3)))))
146
147 (pass-if "(y y n)"
148 (equal? '( (n . 3)) (alist-delete 'y '((y . 1) (y . 2) (n . 3)))))
149
150 (pass-if "(n y n)"
151 (equal? '((n . 1) (n . 3)) (alist-delete 'y '((n . 1) (y . 2) (n . 3)))))
152
153 (pass-if "(y n n)"
154 (equal? '((n . 2) (n . 3)) (alist-delete 'y '((y . 1) (n . 2) (n . 3)))))
155
156 (pass-if "(n n n)"
157 (equal? '((n . 1) (n . 2) (n . 3)) (alist-delete 'y '((n . 1) (n . 2) (n . 3))))))
158
159 ;;
160 ;; append-map
161 ;;
162
163 (with-test-prefix "append-map"
164
165 (with-test-prefix "one list"
166
167 (pass-if "()"
168 (equal? '() (append-map noop '(()))))
169
170 (pass-if "(1)"
171 (equal? '(1) (append-map noop '((1)))))
172
173 (pass-if "(1 2)"
174 (equal? '(1 2) (append-map noop '((1 2)))))
175
176 (pass-if "() ()"
177 (equal? '() (append-map noop '(() ()))))
178
179 (pass-if "() (1)"
180 (equal? '(1) (append-map noop '(() (1)))))
181
182 (pass-if "() (1 2)"
183 (equal? '(1 2) (append-map noop '(() (1 2)))))
184
185 (pass-if "(1) (2)"
186 (equal? '(1 2) (append-map noop '((1) (2)))))
187
188 (pass-if "(1 2) ()"
189 (equal? '(1 2) (append-map noop '(() (1 2))))))
190
191 (with-test-prefix "two lists"
192
193 (pass-if "() / 9"
194 (equal? '() (append-map noop '(()) '(9))))
195
196 (pass-if "(1) / 9"
197 (equal? '(1) (append-map noop '((1)) '(9))))
198
199 (pass-if "() () / 9 9"
200 (equal? '() (append-map noop '(() ()) '(9 9))))
201
202 (pass-if "(1) (2) / 9"
203 (equal? '(1) (append-map noop '((1) (2)) '(9))))
204
205 (pass-if "(1) (2) / 9 9"
206 (equal? '(1 2) (append-map noop '((1) (2)) '(9 9))))))
207
208 ;;
209 ;; append-reverse
210 ;;
211
212 (with-test-prefix "append-reverse"
213
214 ;; return a list which is the cars and cdrs of LST
215 (define (list-contents lst)
216 (if (null? lst)
217 '()
218 (cons* (car lst) (cdr lst) (list-contents (cdr lst)))))
219
220 (define (valid-append-reverse revhead tail want)
221 (let ((revhead-contents (list-contents revhead))
222 (got (append-reverse revhead tail)))
223 (and (equal? got want)
224 ;; revhead unchanged
225 (equal? revhead-contents (list-contents revhead)))))
226
227 (pass-if-exception "too few args (0)" exception:wrong-num-args
228 (append-reverse))
229
230 (pass-if-exception "too few args (1)" exception:wrong-num-args
231 (append-reverse '(x)))
232
233 (pass-if-exception "too many args (3)" exception:wrong-num-args
234 (append-reverse '() '() #f))
235
236 (pass-if (valid-append-reverse '() '() '()))
237 (pass-if (valid-append-reverse '() '(1 2 3) '(1 2 3)))
238
239 (pass-if (valid-append-reverse '(1) '() '(1)))
240 (pass-if (valid-append-reverse '(1) '(2) '(1 2)))
241 (pass-if (valid-append-reverse '(1) '(2 3) '(1 2 3)))
242
243 (pass-if (valid-append-reverse '(1 2) '() '(2 1)))
244 (pass-if (valid-append-reverse '(1 2) '(3) '(2 1 3)))
245 (pass-if (valid-append-reverse '(1 2) '(3 4) '(2 1 3 4)))
246
247 (pass-if (valid-append-reverse '(1 2 3) '() '(3 2 1)))
248 (pass-if (valid-append-reverse '(1 2 3) '(4) '(3 2 1 4)))
249 (pass-if (valid-append-reverse '(1 2 3) '(4 5) '(3 2 1 4 5))))
250
251 ;;
252 ;; append-reverse!
253 ;;
254
255 (with-test-prefix "append-reverse!"
256
257 (pass-if-exception "too few args (0)" exception:wrong-num-args
258 (append-reverse!))
259
260 (pass-if-exception "too few args (1)" exception:wrong-num-args
261 (append-reverse! '(x)))
262
263 (pass-if-exception "too many args (3)" exception:wrong-num-args
264 (append-reverse! '() '() #f))
265
266 (pass-if (equal? '() (append-reverse! '() '())))
267 (pass-if (equal? '(1 2 3) (append-reverse! '() '(1 2 3))))
268
269 (pass-if (equal? '(1) (append-reverse! '(1) '())))
270 (pass-if (equal? '(1 2) (append-reverse! '(1) '(2))))
271 (pass-if (equal? '(1 2 3) (append-reverse! '(1) '(2 3))))
272
273 (pass-if (equal? '(2 1) (append-reverse! '(1 2) '())))
274 (pass-if (equal? '(2 1 3) (append-reverse! '(1 2) '(3))))
275 (pass-if (equal? '(2 1 3 4) (append-reverse! '(1 2) '(3 4))))
276
277 (pass-if (equal? '(3 2 1) (append-reverse! '(1 2 3) '())))
278 (pass-if (equal? '(3 2 1 4) (append-reverse! '(1 2 3) '(4))))
279 (pass-if (equal? '(3 2 1 4 5) (append-reverse! '(1 2 3) '(4 5)))))
280
281 ;;
282 ;; assoc
283 ;;
284
285 (with-test-prefix "assoc"
286
287 (pass-if "not found"
288 (let ((alist '((a . 1)
289 (b . 2)
290 (c . 3))))
291 (eqv? #f (assoc 'z alist))))
292
293 (pass-if "found"
294 (let ((alist '((a . 1)
295 (b . 2)
296 (c . 3))))
297 (eqv? (second alist) (assoc 'b alist))))
298
299 ;; this was wrong in guile 1.8.0 (a gremlin newly introduced in the 1.8
300 ;; series, 1.6.x and earlier was ok)
301 (pass-if "= arg order"
302 (let ((alist '((b . 1)))
303 (good #f))
304 (assoc 'a alist (lambda (x y)
305 (set! good (and (eq? x 'a)
306 (eq? y 'b)))))
307 good))
308
309 ;; likewise this one bad in guile 1.8.0
310 (pass-if "srfi-1 example <"
311 (let ((alist '((1 . a)
312 (5 . b)
313 (6 . c))))
314 (eq? (third alist) (assoc 5 alist <)))))
315
316 ;;
317 ;; break
318 ;;
319
320 (with-test-prefix "break"
321
322 (define (test-break lst want-v1 want-v2)
323 (call-with-values
324 (lambda ()
325 (break negative? lst))
326 (lambda (got-v1 got-v2)
327 (and (equal? got-v1 want-v1)
328 (equal? got-v2 want-v2)))))
329
330 (pass-if "empty"
331 (test-break '() '() '()))
332
333 (pass-if "y"
334 (test-break '(1) '(1) '()))
335
336 (pass-if "n"
337 (test-break '(-1) '() '(-1)))
338
339 (pass-if "yy"
340 (test-break '(1 2) '(1 2) '()))
341
342 (pass-if "ny"
343 (test-break '(-1 1) '() '(-1 1)))
344
345 (pass-if "yn"
346 (test-break '(1 -1) '(1) '(-1)))
347
348 (pass-if "nn"
349 (test-break '(-1 -2) '() '(-1 -2)))
350
351 (pass-if "yyy"
352 (test-break '(1 2 3) '(1 2 3) '()))
353
354 (pass-if "nyy"
355 (test-break '(-1 1 2) '() '(-1 1 2)))
356
357 (pass-if "yny"
358 (test-break '(1 -1 2) '(1) '(-1 2)))
359
360 (pass-if "nny"
361 (test-break '(-1 -2 1) '() '(-1 -2 1)))
362
363 (pass-if "yyn"
364 (test-break '(1 2 -1) '(1 2) '(-1)))
365
366 (pass-if "nyn"
367 (test-break '(-1 1 -2) '() '(-1 1 -2)))
368
369 (pass-if "ynn"
370 (test-break '(1 -1 -2) '(1) '(-1 -2)))
371
372 (pass-if "nnn"
373 (test-break '(-1 -2 -3) '() '(-1 -2 -3))))
374
375 ;;
376 ;; break!
377 ;;
378
379 (with-test-prefix "break!"
380
381 (define (test-break! lst want-v1 want-v2)
382 (call-with-values
383 (lambda ()
384 (break! negative? lst))
385 (lambda (got-v1 got-v2)
386 (and (equal? got-v1 want-v1)
387 (equal? got-v2 want-v2)))))
388
389 (pass-if "empty"
390 (test-break! '() '() '()))
391
392 (pass-if "y"
393 (test-break! (list 1) '(1) '()))
394
395 (pass-if "n"
396 (test-break! (list -1) '() '(-1)))
397
398 (pass-if "yy"
399 (test-break! (list 1 2) '(1 2) '()))
400
401 (pass-if "ny"
402 (test-break! (list -1 1) '() '(-1 1)))
403
404 (pass-if "yn"
405 (test-break! (list 1 -1) '(1) '(-1)))
406
407 (pass-if "nn"
408 (test-break! (list -1 -2) '() '(-1 -2)))
409
410 (pass-if "yyy"
411 (test-break! (list 1 2 3) '(1 2 3) '()))
412
413 (pass-if "nyy"
414 (test-break! (list -1 1 2) '() '(-1 1 2)))
415
416 (pass-if "yny"
417 (test-break! (list 1 -1 2) '(1) '(-1 2)))
418
419 (pass-if "nny"
420 (test-break! (list -1 -2 1) '() '(-1 -2 1)))
421
422 (pass-if "yyn"
423 (test-break! (list 1 2 -1) '(1 2) '(-1)))
424
425 (pass-if "nyn"
426 (test-break! (list -1 1 -2) '() '(-1 1 -2)))
427
428 (pass-if "ynn"
429 (test-break! (list 1 -1 -2) '(1) '(-1 -2)))
430
431 (pass-if "nnn"
432 (test-break! (list -1 -2 -3) '() '(-1 -2 -3))))
433
434 ;;
435 ;; car+cdr
436 ;;
437
438 (with-test-prefix "car+cdr"
439
440 (pass-if "(1 . 2)"
441 (call-with-values
442 (lambda ()
443 (car+cdr '(1 . 2)))
444 (lambda (x y)
445 (and (eqv? x 1)
446 (eqv? y 2))))))
447
448 ;;
449 ;; concatenate and concatenate!
450 ;;
451
452 (let ()
453 (define (common-tests concatenate-proc unmodified?)
454 (define (try lstlst want)
455 (let ((lstlst-copy (copy-tree lstlst))
456 (got (concatenate-proc lstlst)))
457 (if unmodified?
458 (if (not (equal? lstlst lstlst-copy))
459 (error "input lists modified")))
460 (equal? got want)))
461
462 (pass-if-exception "too few args" exception:wrong-num-args
463 (concatenate-proc))
464
465 (pass-if-exception "too many args" exception:wrong-num-args
466 (concatenate-proc '() '()))
467
468 (pass-if-exception "number" '(wrong-type-arg . "Apply to non-list")
469 (concatenate-proc 123))
470
471 (pass-if-exception "vector" '(wrong-type-arg . "Apply to non-list")
472 (concatenate-proc #(1 2 3)))
473
474 (pass-if "no lists"
475 (try '() '()))
476
477 (pass-if (try '((1)) '(1)))
478 (pass-if (try '((1 2)) '(1 2)))
479 (pass-if (try '(() (1)) '(1)))
480 (pass-if (try '(() () (1)) '(1)))
481
482 (pass-if (try '((1) (2)) '(1 2)))
483 (pass-if (try '(() (1 2)) '(1 2)))
484
485 (pass-if (try '((1) 2) '(1 . 2)))
486 (pass-if (try '((1) (2) 3) '(1 2 . 3)))
487 (pass-if (try '((1) (2) (3 . 4)) '(1 2 3 . 4)))
488 )
489
490 (with-test-prefix "concatenate"
491 (common-tests concatenate #t))
492
493 (with-test-prefix "concatenate!"
494 (common-tests concatenate! #f)))
495
496 ;;
497 ;; count
498 ;;
499
500 (with-test-prefix "count"
501 (pass-if-exception "no args" exception:wrong-num-args
502 (count))
503
504 (pass-if-exception "one arg" exception:wrong-num-args
505 (count noop))
506
507 (with-test-prefix "one list"
508 (define (or1 x)
509 x)
510
511 (pass-if "empty list" (= 0 (count or1 '())))
512
513 (pass-if-exception "pred arg count 0" exception:wrong-num-args
514 (count (lambda () x) '(1 2 3)))
515 (pass-if-exception "pred arg count 2" exception:wrong-num-args
516 (count (lambda (x y) x) '(1 2 3)))
517
518 (pass-if-exception "improper 1" exception:wrong-type-arg
519 (count or1 1))
520 (pass-if-exception "improper 2" exception:wrong-type-arg
521 (count or1 '(1 . 2)))
522 (pass-if-exception "improper 3" exception:wrong-type-arg
523 (count or1 '(1 2 . 3)))
524
525 (pass-if (= 0 (count or1 '(#f))))
526 (pass-if (= 1 (count or1 '(#t))))
527
528 (pass-if (= 0 (count or1 '(#f #f))))
529 (pass-if (= 1 (count or1 '(#f #t))))
530 (pass-if (= 1 (count or1 '(#t #f))))
531 (pass-if (= 2 (count or1 '(#t #t))))
532
533 (pass-if (= 0 (count or1 '(#f #f #f))))
534 (pass-if (= 1 (count or1 '(#f #f #t))))
535 (pass-if (= 1 (count or1 '(#t #f #f))))
536 (pass-if (= 2 (count or1 '(#t #f #t))))
537 (pass-if (= 3 (count or1 '(#t #t #t)))))
538
539 (with-test-prefix "two lists"
540 (define (or2 x y)
541 (or x y))
542
543 (pass-if "arg order"
544 (= 1 (count (lambda (x y)
545 (and (= 1 x)
546 (= 2 y)))
547 '(1) '(2))))
548
549 (pass-if "empty lists" (= 0 (count or2 '() '())))
550
551 (pass-if-exception "pred arg count 0" exception:wrong-num-args
552 (count (lambda () #t) '(1 2 3) '(1 2 3)))
553 (pass-if-exception "pred arg count 1" exception:wrong-num-args
554 (count (lambda (x) x) '(1 2 3) '(1 2 3)))
555 (pass-if-exception "pred arg count 3" exception:wrong-num-args
556 (count (lambda (x y z) x) '(1 2 3) '(1 2 3)))
557
558 (pass-if-exception "improper first 1" exception:wrong-type-arg
559 (count or2 1 '(1 2 3)))
560 (pass-if-exception "improper first 2" exception:wrong-type-arg
561 (count or2 '(1 . 2) '(1 2 3)))
562 (pass-if-exception "improper first 3" exception:wrong-type-arg
563 (count or2 '(1 2 . 3) '(1 2 3)))
564
565 (pass-if-exception "improper second 1" exception:wrong-type-arg
566 (count or2 '(1 2 3) 1))
567 (pass-if-exception "improper second 2" exception:wrong-type-arg
568 (count or2 '(1 2 3) '(1 . 2)))
569 (pass-if-exception "improper second 3" exception:wrong-type-arg
570 (count or2 '(1 2 3) '(1 2 . 3)))
571
572 (pass-if (= 0 (count or2 '(#f) '(#f))))
573 (pass-if (= 1 (count or2 '(#t) '(#f))))
574 (pass-if (= 1 (count or2 '(#f) '(#t))))
575
576 (pass-if (= 0 (count or2 '(#f #f) '(#f #f))))
577 (pass-if (= 1 (count or2 '(#t #f) '(#t #f))))
578 (pass-if (= 2 (count or2 '(#t #t) '(#f #f))))
579 (pass-if (= 2 (count or2 '(#t #f) '(#f #t))))
580
581 (with-test-prefix "stop shortest"
582 (pass-if (= 2 (count or2 '(#t #f #t) '(#f #t))))
583 (pass-if (= 2 (count or2 '(#t #f #t #t) '(#f #t))))
584 (pass-if (= 2 (count or2 '(#t #f) '(#f #t #t))))
585 (pass-if (= 2 (count or2 '(#t #f) '(#f #t #t #t))))))
586
587 (with-test-prefix "three lists"
588 (define (or3 x y z)
589 (or x y z))
590
591 (pass-if "arg order"
592 (= 1 (count (lambda (x y z)
593 (and (= 1 x)
594 (= 2 y)
595 (= 3 z)))
596 '(1) '(2) '(3))))
597
598 (pass-if "empty lists" (= 0 (count or3 '() '() '())))
599
600 ;; currently bad pred argument gives wrong-num-args when 3 or more
601 ;; lists, as opposed to wrong-type-arg for 1 or 2 lists
602 (pass-if-exception "pred arg count 0" exception:wrong-num-args
603 (count (lambda () #t) '(1 2 3) '(1 2 3) '(1 2 3)))
604 (pass-if-exception "pred arg count 2" exception:wrong-num-args
605 (count (lambda (x y) x) '(1 2 3) '(1 2 3)'(1 2 3) ))
606 (pass-if-exception "pred arg count 4" exception:wrong-num-args
607 (count (lambda (w x y z) x) '(1 2 3) '(1 2 3) '(1 2 3)))
608
609 (pass-if-exception "improper first 1" exception:wrong-type-arg
610 (count or3 1 '(1 2 3) '(1 2 3)))
611 (pass-if-exception "improper first 2" exception:wrong-type-arg
612 (count or3 '(1 . 2) '(1 2 3) '(1 2 3)))
613 (pass-if-exception "improper first 3" exception:wrong-type-arg
614 (count or3 '(1 2 . 3) '(1 2 3) '(1 2 3)))
615
616 (pass-if-exception "improper second 1" exception:wrong-type-arg
617 (count or3 '(1 2 3) 1 '(1 2 3)))
618 (pass-if-exception "improper second 2" exception:wrong-type-arg
619 (count or3 '(1 2 3) '(1 . 2) '(1 2 3)))
620 (pass-if-exception "improper second 3" exception:wrong-type-arg
621 (count or3 '(1 2 3) '(1 2 . 3) '(1 2 3)))
622
623 (pass-if-exception "improper third 1" exception:wrong-type-arg
624 (count or3 '(1 2 3) '(1 2 3) 1))
625 (pass-if-exception "improper third 2" exception:wrong-type-arg
626 (count or3 '(1 2 3) '(1 2 3) '(1 . 2)))
627 (pass-if-exception "improper third 3" exception:wrong-type-arg
628 (count or3 '(1 2 3) '(1 2 3) '(1 2 . 3)))
629
630 (pass-if (= 0 (count or3 '(#f) '(#f) '(#f))))
631 (pass-if (= 1 (count or3 '(#t) '(#f) '(#f))))
632 (pass-if (= 1 (count or3 '(#f) '(#t) '(#f))))
633 (pass-if (= 1 (count or3 '(#f) '(#f) '(#t))))
634
635 (pass-if (= 0 (count or3 '(#f #f) '(#f #f) '(#f #f))))
636
637 (pass-if (= 1 (count or3 '(#t #f) '(#f #f) '(#f #f))))
638 (pass-if (= 1 (count or3 '(#f #t) '(#f #f) '(#f #f))))
639 (pass-if (= 1 (count or3 '(#f #f) '(#t #f) '(#f #f))))
640 (pass-if (= 1 (count or3 '(#f #f) '(#f #t) '(#f #f))))
641 (pass-if (= 1 (count or3 '(#f #f) '(#f #f) '(#t #f))))
642 (pass-if (= 1 (count or3 '(#f #f) '(#f #f) '(#f #t))))
643
644 (pass-if (= 2 (count or3 '(#t #t) '(#f #f) '(#f #f))))
645 (pass-if (= 2 (count or3 '(#f #f) '(#t #t) '(#f #f))))
646 (pass-if (= 2 (count or3 '(#f #f) '(#f #f) '(#t #t))))
647 (pass-if (= 2 (count or3 '(#f #f) '(#t #f) '(#f #t))))
648
649 (with-test-prefix "stop shortest"
650 (pass-if (= 0 (count or3 '() '(#t #t #t) '(#t #t))))
651 (pass-if (= 0 (count or3 '(#t #t #t) '() '(#t #t))))
652 (pass-if (= 0 (count or3 '(#t #t #t) '(#t #t) '())))
653
654 (pass-if (= 1 (count or3 '(#t) '(#t #t #t) '(#t #t))))
655 (pass-if (= 1 (count or3 '(#t #t #t) '(#t) '(#t #t))))
656 (pass-if (= 1 (count or3 '(#t #t #t) '(#t #t) '(#t)))))
657
658 (pass-if "apply list unchanged"
659 (let ((lst (list (list 1 2) (list 3 4) (list 5 6))))
660 (and (equal? 2 (apply count or3 lst))
661 ;; lst unmodified
662 (equal? '((1 2) (3 4) (5 6)) lst))))))
663
664 ;;
665 ;; delete and delete!
666 ;;
667
668 (let ()
669 ;; Call (PROC lst) for all lists of length up to 6, with all combinations
670 ;; of elements to be retained or deleted. Elements to retain are numbers,
671 ;; 0 upwards. Elements to be deleted are #f.
672 (define (test-lists proc)
673 (do ((n 0 (1+ n)))
674 ((>= n 6))
675 (do ((limit (ash 1 n))
676 (i 0 (1+ i)))
677 ((>= i limit))
678 (let ((lst '()))
679 (do ((bit 0 (1+ bit)))
680 ((>= bit n))
681 (set! lst (cons (if (logbit? bit i) bit #f) lst)))
682 (proc lst)))))
683
684 (define (common-tests delete-proc)
685 (pass-if-exception "too few args" exception:wrong-num-args
686 (delete-proc 0))
687
688 (pass-if-exception "too many args" exception:wrong-num-args
689 (delete-proc 0 '() equal? 99))
690
691 (pass-if "empty"
692 (eq? '() (delete-proc 0 '() equal?)))
693
694 (pass-if "equal?"
695 (equal? '((1) (3))
696 (delete-proc '(2) '((1) (2) (3)) equal?)))
697
698 (pass-if "eq?"
699 (equal? '((1) (2) (3))
700 (delete-proc '(2) '((1) (2) (3)) eq?)))
701
702 (pass-if "called arg order"
703 (equal? '(1 2 3)
704 (delete-proc 3 '(1 2 3 4 5) <))))
705
706 (with-test-prefix "delete"
707 (common-tests delete)
708
709 (test-lists
710 (lambda (lst)
711 (let ((lst-copy (list-copy lst)))
712 (with-test-prefix lst-copy
713 (pass-if "result"
714 (equal? (delete #f lst equal?)
715 (ref-delete #f lst equal?)))
716 (pass-if "non-destructive"
717 (equal? lst-copy lst)))))))
718
719 (with-test-prefix "delete!"
720 (common-tests delete!)
721
722 (test-lists
723 (lambda (lst)
724 (pass-if lst
725 (equal? (delete! #f lst)
726 (ref-delete #f lst)))))))
727
728 ;;
729 ;; delete-duplicates and delete-duplicates!
730 ;;
731
732 (let ()
733 ;; Call (PROC lst) for all lists of length 1 <= n <= 4, with all
734 ;; combinations of numbers 1 to n in the elements
735 (define (test-lists proc)
736 (do ((n 1 (1+ n)))
737 ((> n 4))
738 (do ((limit (integer-expt n n))
739 (i 0 (1+ i)))
740 ((>= i limit))
741 (let ((lst '()))
742 (do ((j 0 (1+ j))
743 (rem i (quotient rem n)))
744 ((>= j n))
745 (set! lst (cons (remainder rem n) lst)))
746 (proc lst)))))
747
748 (define (common-tests delete-duplicates-proc)
749 (pass-if-exception "too few args" exception:wrong-num-args
750 (delete-duplicates-proc))
751
752 (pass-if-exception "too many args" exception:wrong-num-args
753 (delete-duplicates-proc '() equal? 99))
754
755 (pass-if "empty"
756 (eq? '() (delete-duplicates-proc '())))
757
758 (pass-if "equal? (the default)"
759 (equal? '((2))
760 (delete-duplicates-proc '((2) (2) (2)))))
761
762 (pass-if "eq?"
763 (equal? '((2) (2) (2))
764 (delete-duplicates-proc '((2) (2) (2)) eq?)))
765
766 (pass-if "called arg order"
767 (let ((ok #t))
768 (delete-duplicates-proc '(1 2 3 4 5)
769 (lambda (x y)
770 (if (> x y)
771 (set! ok #f))
772 #f))
773 ok)))
774
775 (with-test-prefix "delete-duplicates"
776 (common-tests delete-duplicates)
777
778 (test-lists
779 (lambda (lst)
780 (let ((lst-copy (list-copy lst)))
781 (with-test-prefix lst-copy
782 (pass-if "result"
783 (equal? (delete-duplicates lst)
784 (ref-delete-duplicates lst)))
785 (pass-if "non-destructive"
786 (equal? lst-copy lst)))))))
787
788 (with-test-prefix "delete-duplicates!"
789 (common-tests delete-duplicates!)
790
791 (test-lists
792 (lambda (lst)
793 (pass-if lst
794 (equal? (delete-duplicates! lst)
795 (ref-delete-duplicates lst)))))))
796
797 ;;
798 ;; drop
799 ;;
800
801 (with-test-prefix "drop"
802
803 (pass-if "'() 0"
804 (null? (drop '() 0)))
805
806 (pass-if "'(a) 0"
807 (let ((lst '(a)))
808 (eq? lst
809 (drop lst 0))))
810
811 (pass-if "'(a b) 0"
812 (let ((lst '(a b)))
813 (eq? lst
814 (drop lst 0))))
815
816 (pass-if "'(a) 1"
817 (let ((lst '(a)))
818 (eq? (cdr lst)
819 (drop lst 1))))
820
821 (pass-if "'(a b) 1"
822 (let ((lst '(a b)))
823 (eq? (cdr lst)
824 (drop lst 1))))
825
826 (pass-if "'(a b) 2"
827 (let ((lst '(a b)))
828 (eq? (cddr lst)
829 (drop lst 2))))
830
831 (pass-if "'(a b c) 1"
832 (let ((lst '(a b c)))
833 (eq? (cddr lst)
834 (drop lst 2))))
835
836 (pass-if "circular '(a) 0"
837 (let ((lst (circular-list 'a)))
838 (eq? lst
839 (drop lst 0))))
840
841 (pass-if "circular '(a) 1"
842 (let ((lst (circular-list 'a)))
843 (eq? lst
844 (drop lst 1))))
845
846 (pass-if "circular '(a) 2"
847 (let ((lst (circular-list 'a)))
848 (eq? lst
849 (drop lst 1))))
850
851 (pass-if "circular '(a b) 1"
852 (let ((lst (circular-list 'a)))
853 (eq? (cdr lst)
854 (drop lst 0))))
855
856 (pass-if "circular '(a b) 2"
857 (let ((lst (circular-list 'a)))
858 (eq? lst
859 (drop lst 1))))
860
861 (pass-if "circular '(a b) 5"
862 (let ((lst (circular-list 'a)))
863 (eq? (cdr lst)
864 (drop lst 5))))
865
866 (pass-if "'(a . b) 1"
867 (eq? 'b
868 (drop '(a . b) 1)))
869
870 (pass-if "'(a b . c) 1"
871 (equal? 'c
872 (drop '(a b . c) 2))))
873
874 ;;
875 ;; drop-right
876 ;;
877
878 (with-test-prefix "drop-right"
879
880 (pass-if-exception "() -1" exception:out-of-range
881 (drop-right '() -1))
882 (pass-if (equal? '() (drop-right '() 0)))
883 (pass-if-exception "() 1" exception:wrong-type-arg
884 (drop-right '() 1))
885
886 (pass-if-exception "(1) -1" exception:out-of-range
887 (drop-right '(1) -1))
888 (pass-if (equal? '(1) (drop-right '(1) 0)))
889 (pass-if (equal? '() (drop-right '(1) 1)))
890 (pass-if-exception "(1) 2" exception:wrong-type-arg
891 (drop-right '(1) 2))
892
893 (pass-if-exception "(4 5) -1" exception:out-of-range
894 (drop-right '(4 5) -1))
895 (pass-if (equal? '(4 5) (drop-right '(4 5) 0)))
896 (pass-if (equal? '(4) (drop-right '(4 5) 1)))
897 (pass-if (equal? '() (drop-right '(4 5) 2)))
898 (pass-if-exception "(4 5) 3" exception:wrong-type-arg
899 (drop-right '(4 5) 3))
900
901 (pass-if-exception "(4 5 6) -1" exception:out-of-range
902 (drop-right '(4 5 6) -1))
903 (pass-if (equal? '(4 5 6) (drop-right '(4 5 6) 0)))
904 (pass-if (equal? '(4 5) (drop-right '(4 5 6) 1)))
905 (pass-if (equal? '(4) (drop-right '(4 5 6) 2)))
906 (pass-if (equal? '() (drop-right '(4 5 6) 3)))
907 (pass-if-exception "(4 5 6) 4" exception:wrong-type-arg
908 (drop-right '(4 5 6) 4))
909
910 (pass-if "(a b . c) 0"
911 (equal? (drop-right '(a b . c) 0) '(a b)))
912 (pass-if "(a b . c) 1"
913 (equal? (drop-right '(a b . c) 1) '(a))))
914
915 ;;
916 ;; drop-right!
917 ;;
918
919 (with-test-prefix "drop-right!"
920
921 (pass-if-exception "() -1" exception:out-of-range
922 (drop-right! '() -1))
923 (pass-if (equal? '() (drop-right! '() 0)))
924 (pass-if-exception "() 1" exception:wrong-type-arg
925 (drop-right! '() 1))
926
927 (pass-if-exception "(1) -1" exception:out-of-range
928 (drop-right! (list 1) -1))
929 (pass-if (equal? '(1) (drop-right! (list 1) 0)))
930 (pass-if (equal? '() (drop-right! (list 1) 1)))
931 (pass-if-exception "(1) 2" exception:wrong-type-arg
932 (drop-right! (list 1) 2))
933
934 (pass-if-exception "(4 5) -1" exception:out-of-range
935 (drop-right! (list 4 5) -1))
936 (pass-if (equal? '(4 5) (drop-right! (list 4 5) 0)))
937 (pass-if (equal? '(4) (drop-right! (list 4 5) 1)))
938 (pass-if (equal? '() (drop-right! (list 4 5) 2)))
939 (pass-if-exception "(4 5) 3" exception:wrong-type-arg
940 (drop-right! (list 4 5) 3))
941
942 (pass-if-exception "(4 5 6) -1" exception:out-of-range
943 (drop-right! (list 4 5 6) -1))
944 (pass-if (equal? '(4 5 6) (drop-right! (list 4 5 6) 0)))
945 (pass-if (equal? '(4 5) (drop-right! (list 4 5 6) 1)))
946 (pass-if (equal? '(4) (drop-right! (list 4 5 6) 2)))
947 (pass-if (equal? '() (drop-right! (list 4 5 6) 3)))
948 (pass-if-exception "(4 5 6) 4" exception:wrong-type-arg
949 (drop-right! (list 4 5 6) 4)))
950
951 ;;
952 ;; drop-while
953 ;;
954
955 (with-test-prefix "drop-while"
956
957 (pass-if (equal? '() (drop-while odd? '())))
958 (pass-if (equal? '() (drop-while odd? '(1))))
959 (pass-if (equal? '() (drop-while odd? '(1 3))))
960 (pass-if (equal? '() (drop-while odd? '(1 3 5))))
961
962 (pass-if (equal? '(2) (drop-while odd? '(2))))
963 (pass-if (equal? '(2) (drop-while odd? '(1 2))))
964 (pass-if (equal? '(4) (drop-while odd? '(1 3 4))))
965
966 (pass-if (equal? '(2 1) (drop-while odd? '(2 1))))
967 (pass-if (equal? '(4 3) (drop-while odd? '(1 4 3))))
968 (pass-if (equal? '(4 1 3) (drop-while odd? '(4 1 3)))))
969
970 ;;
971 ;; eighth
972 ;;
973
974 (with-test-prefix "eighth"
975 (pass-if-exception "() -1" exception:wrong-type-arg
976 (eighth '(a b c d e f g)))
977 (pass-if (eq? 'h (eighth '(a b c d e f g h))))
978 (pass-if (eq? 'h (eighth '(a b c d e f g h i)))))
979
980 ;;
981 ;; fifth
982 ;;
983
984 (with-test-prefix "fifth"
985 (pass-if-exception "() -1" exception:wrong-type-arg
986 (fifth '(a b c d)))
987 (pass-if (eq? 'e (fifth '(a b c d e))))
988 (pass-if (eq? 'e (fifth '(a b c d e f)))))
989
990 ;;
991 ;; filter-map
992 ;;
993
994 (with-test-prefix "filter-map"
995
996 (with-test-prefix "one list"
997 (pass-if-exception "'x" exception:wrong-type-arg
998 (filter-map noop 'x))
999
1000 (pass-if-exception "'(1 . x)" exception:wrong-type-arg
1001 (filter-map noop '(1 . x)))
1002
1003 (pass-if "(1)"
1004 (equal? '(1) (filter-map noop '(1))))
1005
1006 (pass-if "(#f)"
1007 (equal? '() (filter-map noop '(#f))))
1008
1009 (pass-if "(1 2)"
1010 (equal? '(1 2) (filter-map noop '(1 2))))
1011
1012 (pass-if "(#f 2)"
1013 (equal? '(2) (filter-map noop '(#f 2))))
1014
1015 (pass-if "(#f #f)"
1016 (equal? '() (filter-map noop '(#f #f))))
1017
1018 (pass-if "(1 2 3)"
1019 (equal? '(1 2 3) (filter-map noop '(1 2 3))))
1020
1021 (pass-if "(#f 2 3)"
1022 (equal? '(2 3) (filter-map noop '(#f 2 3))))
1023
1024 (pass-if "(1 #f 3)"
1025 (equal? '(1 3) (filter-map noop '(1 #f 3))))
1026
1027 (pass-if "(1 2 #f)"
1028 (equal? '(1 2) (filter-map noop '(1 2 #f)))))
1029
1030 (with-test-prefix "two lists"
1031 (pass-if-exception "'x '(1 2 3)" exception:wrong-type-arg
1032 (filter-map noop 'x '(1 2 3)))
1033
1034 (pass-if-exception "'(1 2 3) 'x" exception:wrong-type-arg
1035 (filter-map noop '(1 2 3) 'x))
1036
1037 (pass-if-exception "'(1 . x) '(1 2 3)" exception:wrong-type-arg
1038 (filter-map noop '(1 . x) '(1 2 3)))
1039
1040 (pass-if-exception "'(1 2 3) '(1 . x)" exception:wrong-type-arg
1041 (filter-map noop '(1 2 3) '(1 . x)))
1042
1043 (pass-if "(1 2 3) (4 5 6)"
1044 (equal? '(5 7 9) (filter-map + '(1 2 3) '(4 5 6))))
1045
1046 (pass-if "(#f 2 3) (4 5)"
1047 (equal? '(2) (filter-map noop '(#f 2 3) '(4 5))))
1048
1049 (pass-if "(4 #f) (1 2 3)"
1050 (equal? '(4) (filter-map noop '(4 #f) '(1 2 3))))
1051
1052 (pass-if "() (1 2 3)"
1053 (equal? '() (filter-map noop '() '(1 2 3))))
1054
1055 (pass-if "(1 2 3) ()"
1056 (equal? '() (filter-map noop '(1 2 3) '()))))
1057
1058 (with-test-prefix "three lists"
1059 (pass-if-exception "'x '(1 2 3) '(1 2 3)" exception:wrong-type-arg
1060 (filter-map noop 'x '(1 2 3) '(1 2 3)))
1061
1062 (pass-if-exception "'(1 2 3) 'x '(1 2 3)" exception:wrong-type-arg
1063 (filter-map noop '(1 2 3) 'x '(1 2 3)))
1064
1065 (pass-if-exception "'(1 2 3) '(1 2 3) 'x" exception:wrong-type-arg
1066 (filter-map noop '(1 2 3) '(1 2 3) 'x))
1067
1068 (pass-if-exception "'(1 . x) '(1 2 3) '(1 2 3)" exception:wrong-type-arg
1069 (filter-map noop '(1 . x) '(1 2 3) '(1 2 3)))
1070
1071 (pass-if-exception "'(1 2 3) '(1 . x) '(1 2 3)" exception:wrong-type-arg
1072 (filter-map noop '(1 2 3) '(1 . x) '(1 2 3)))
1073
1074 (pass-if-exception "'(1 2 3) '(1 2 3) '(1 . x)" exception:wrong-type-arg
1075 (filter-map noop '(1 2 3) '(1 2 3) '(1 . x)))
1076
1077 (pass-if "(1 2 3) (4 5 6) (7 8 9)"
1078 (equal? '(12 15 18) (filter-map + '(1 2 3) '(4 5 6) '(7 8 9))))
1079
1080 (pass-if "(#f 2 3) (4 5) (7 8 9)"
1081 (equal? '(2) (filter-map noop '(#f 2 3) '(4 5) '(7 8 9))))
1082
1083 (pass-if "(#f 2 3) (7 8 9) (4 5)"
1084 (equal? '(2) (filter-map noop '(#f 2 3) '(7 8 9) '(4 5))))
1085
1086 (pass-if "(4 #f) (1 2 3) (7 8 9)"
1087 (equal? '(4) (filter-map noop '(4 #f) '(1 2 3) '(7 8 9))))
1088
1089 (pass-if "apply list unchanged"
1090 (let ((lst (list (list 1 #f 2) (list 3 4 5) (list 6 7 8))))
1091 (and (equal? '(1 2) (apply filter-map noop lst))
1092 ;; lst unmodified
1093 (equal? lst '((1 #f 2) (3 4 5) (6 7 8))))))))
1094
1095 ;;
1096 ;; find
1097 ;;
1098
1099 (with-test-prefix "find"
1100 (pass-if (eqv? #f (find odd? '())))
1101 (pass-if (eqv? #f (find odd? '(0))))
1102 (pass-if (eqv? #f (find odd? '(0 2))))
1103 (pass-if (eqv? 1 (find odd? '(1))))
1104 (pass-if (eqv? 1 (find odd? '(0 1))))
1105 (pass-if (eqv? 1 (find odd? '(0 1 2))))
1106 (pass-if (eqv? 1 (find odd? '(2 0 1))))
1107 (pass-if (eqv? 1 (find (lambda (x) (= 1 x)) '(2 0 1)))))
1108
1109 ;;
1110 ;; find-tail
1111 ;;
1112
1113 (with-test-prefix "find-tail"
1114 (pass-if (let ((lst '()))
1115 (eq? #f (find-tail odd? lst))))
1116 (pass-if (let ((lst '(0)))
1117 (eq? #f (find-tail odd? lst))))
1118 (pass-if (let ((lst '(0 2)))
1119 (eq? #f (find-tail odd? lst))))
1120 (pass-if (let ((lst '(1)))
1121 (eq? lst (find-tail odd? lst))))
1122 (pass-if (let ((lst '(1 2)))
1123 (eq? lst (find-tail odd? lst))))
1124 (pass-if (let ((lst '(2 1)))
1125 (eq? (cdr lst) (find-tail odd? lst))))
1126 (pass-if (let ((lst '(2 1 0)))
1127 (eq? (cdr lst) (find-tail odd? lst))))
1128 (pass-if (let ((lst '(2 0 1)))
1129 (eq? (cddr lst) (find-tail odd? lst))))
1130 (pass-if (let ((lst '(2 0 1)))
1131 (eq? (cddr lst) (find-tail (lambda (x) (= 1 x)) lst)))))
1132
1133 ;;
1134 ;; fold
1135 ;;
1136
1137 (with-test-prefix "fold"
1138 (pass-if-exception "no args" exception:wrong-num-args
1139 (fold))
1140
1141 (pass-if-exception "one arg" exception:wrong-num-args
1142 (fold 123))
1143
1144 (pass-if-exception "two args" exception:wrong-num-args
1145 (fold 123 noop))
1146
1147 (with-test-prefix "one list"
1148
1149 (pass-if "arg order"
1150 (eq? #t (fold (lambda (x prev)
1151 (and (= 1 x)
1152 (= 2 prev)))
1153 2 '(1))))
1154
1155 (pass-if "empty list" (= 123 (fold + 123 '())))
1156
1157 (pass-if-exception "proc arg count 0" exception:wrong-num-args
1158 (fold (lambda () x) 123 '(1 2 3)))
1159 (pass-if-exception "proc arg count 1" exception:wrong-num-args
1160 (fold (lambda (x) x) 123 '(1 2 3)))
1161 (pass-if-exception "proc arg count 3" exception:wrong-num-args
1162 (fold (lambda (x y z) x) 123 '(1 2 3)))
1163
1164 (pass-if-exception "improper 1" exception:wrong-type-arg
1165 (fold + 123 1))
1166 (pass-if-exception "improper 2" exception:wrong-type-arg
1167 (fold + 123 '(1 . 2)))
1168 (pass-if-exception "improper 3" exception:wrong-type-arg
1169 (fold + 123 '(1 2 . 3)))
1170
1171 (pass-if (= 3 (fold + 1 '(2))))
1172 (pass-if (= 6 (fold + 1 '(2 3))))
1173 (pass-if (= 10 (fold + 1 '(2 3 4)))))
1174
1175 (with-test-prefix "two lists"
1176
1177 (pass-if "arg order"
1178 (eq? #t (fold (lambda (x y prev)
1179 (and (= 1 x)
1180 (= 2 y)
1181 (= 3 prev)))
1182 3 '(1) '(2))))
1183
1184 (pass-if "empty lists" (= 1 (fold + 1 '() '())))
1185
1186 ;; currently bad proc argument gives wrong-num-args when 2 or more
1187 ;; lists, as opposed to wrong-type-arg for 1 list
1188 (pass-if-exception "proc arg count 2" exception:wrong-num-args
1189 (fold (lambda (x prev) x) 1 '(1 2 3) '(1 2 3)))
1190 (pass-if-exception "proc arg count 4" exception:wrong-num-args
1191 (fold (lambda (x y z prev) x) 1 '(1 2 3) '(1 2 3)))
1192
1193 (pass-if-exception "improper first 1" list+-bad-arg-exception
1194 (fold + 1 1 '(1 2 3)))
1195 (pass-if-exception "improper first 2" list+-bad-arg-exception
1196 (fold + 1 '(1 . 2) '(1 2 3)))
1197 (pass-if-exception "improper first 3" list+-bad-arg-exception
1198 (fold + 1 '(1 2 . 3) '(1 2 3)))
1199
1200 (pass-if-exception "improper second 1" list+-bad-arg-exception
1201 (fold + 1 '(1 2 3) 1))
1202 (pass-if-exception "improper second 2" list+-bad-arg-exception
1203 (fold + 1 '(1 2 3) '(1 . 2)))
1204 (pass-if-exception "improper second 3" list+-bad-arg-exception
1205 (fold + 1 '(1 2 3) '(1 2 . 3)))
1206
1207 (pass-if (= 6 (fold + 1 '(2) '(3))))
1208 (pass-if (= 15 (fold + 1 '(2 3) '(4 5))))
1209 (pass-if (= 28 (fold + 1 '(2 3 4) '(5 6 7))))
1210
1211 (with-test-prefix "stop shortest"
1212 (pass-if (= 13 (fold + 1 '(1 2 3) '(4 5))))
1213 (pass-if (= 13 (fold + 1 '(4 5) '(1 2 3))))
1214 (pass-if (= 11 (fold + 1 '(3 4) '(1 2 9 9))))
1215 (pass-if (= 11 (fold + 1 '(1 2 9 9) '(3 4)))))
1216
1217 (pass-if "apply list unchanged"
1218 (let ((lst (list (list 1 2) (list 3 4))))
1219 (and (equal? 11 (apply fold + 1 lst))
1220 ;; lst unmodified
1221 (equal? '((1 2) (3 4)) lst)))))
1222
1223 (with-test-prefix "three lists"
1224
1225 (pass-if "arg order"
1226 (eq? #t (fold (lambda (x y z prev)
1227 (and (= 1 x)
1228 (= 2 y)
1229 (= 3 z)
1230 (= 4 prev)))
1231 4 '(1) '(2) '(3))))
1232
1233 (pass-if "empty lists" (= 1 (fold + 1 '() '() '())))
1234
1235 (pass-if-exception "proc arg count 3" exception:wrong-num-args
1236 (fold (lambda (x y prev) x) 1 '(1 2 3) '(1 2 3)'(1 2 3) ))
1237 (pass-if-exception "proc arg count 5" exception:wrong-num-args
1238 (fold (lambda (w x y z prev) x) 1 '(1 2 3) '(1 2 3) '(1 2 3)))
1239
1240 (pass-if-exception "improper first 1" exception:wrong-type-arg
1241 (fold + 1 1 '(1 2 3) '(1 2 3)))
1242 (pass-if-exception "improper first 2" exception:wrong-type-arg
1243 (fold + 1 '(1 . 2) '(1 2 3) '(1 2 3)))
1244 (pass-if-exception "improper first 3" exception:wrong-type-arg
1245 (fold + 1 '(1 2 . 3) '(1 2 3) '(1 2 3)))
1246
1247 (pass-if-exception "improper second 1" exception:wrong-type-arg
1248 (fold + 1 '(1 2 3) 1 '(1 2 3)))
1249 (pass-if-exception "improper second 2" exception:wrong-type-arg
1250 (fold + 1 '(1 2 3) '(1 . 2) '(1 2 3)))
1251 (pass-if-exception "improper second 3" exception:wrong-type-arg
1252 (fold + 1 '(1 2 3) '(1 2 . 3) '(1 2 3)))
1253
1254 (pass-if-exception "improper third 1" exception:wrong-type-arg
1255 (fold + 1 '(1 2 3) '(1 2 3) 1))
1256 (pass-if-exception "improper third 2" exception:wrong-type-arg
1257 (fold + 1 '(1 2 3) '(1 2 3) '(1 . 2)))
1258 (pass-if-exception "improper third 3" exception:wrong-type-arg
1259 (fold + 1 '(1 2 3) '(1 2 3) '(1 2 . 3)))
1260
1261 (pass-if (= 10 (fold + 1 '(2) '(3) '(4))))
1262 (pass-if (= 28 (fold + 1 '(2 5) '(3 6) '(4 7))))
1263 (pass-if (= 55 (fold + 1 '(2 5 8) '(3 6 9) '(4 7 10))))
1264
1265 (with-test-prefix "stop shortest"
1266 (pass-if (= 28 (fold + 1 '(2 5 9) '(3 6) '(4 7))))
1267 (pass-if (= 28 (fold + 1 '(2 5) '(3 6 9) '(4 7))))
1268 (pass-if (= 28 (fold + 1 '(2 5) '(3 6) '(4 7 9)))))
1269
1270 (pass-if "apply list unchanged"
1271 (let ((lst (list (list 1 2) (list 3 4) (list 5 6))))
1272 (and (equal? 22 (apply fold + 1 lst))
1273 ;; lst unmodified
1274 (equal? '((1 2) (3 4) (5 6)) lst))))))
1275
1276 ;;
1277 ;; fold-right
1278 ;;
1279
1280 (with-test-prefix "fold-right"
1281
1282 (pass-if "one list"
1283 (equal? (iota 10)
1284 (fold-right cons '() (iota 10))))
1285
1286 (pass-if "two lists"
1287 (equal? (zip (iota 10) (map integer->char (iota 10)))
1288 (fold-right (lambda (x y z)
1289 (cons (list x y) z))
1290 '()
1291 (iota 10)
1292 (map integer->char (iota 10)))))
1293
1294 (pass-if "tail-recursive"
1295 (= 1e6 (fold-right (lambda (x y) (+ 1 y))
1296 0
1297 (iota 1e6)))))
1298 ;;
1299 ;; unfold
1300 ;;
1301
1302 (with-test-prefix "unfold"
1303
1304 (pass-if "basic"
1305 (equal? (iota 10)
1306 (unfold (lambda (x) (>= x 10))
1307 identity
1308 1+
1309 0)))
1310
1311 (pass-if "tail-gen"
1312 (equal? (append (iota 10) '(tail 10))
1313 (unfold (lambda (x) (>= x 10))
1314 identity
1315 1+
1316 0
1317 (lambda (seed) (list 'tail seed)))))
1318
1319 (pass-if "tail-recursive"
1320 ;; Bug #30071.
1321 (pair? (unfold (lambda (x) (>= x 1e6))
1322 identity
1323 1+
1324 0))))
1325
1326 ;;
1327 ;; length+
1328 ;;
1329
1330 (with-test-prefix "length+"
1331 (pass-if-exception "too few args" exception:wrong-num-args
1332 (length+))
1333 (pass-if-exception "too many args" exception:wrong-num-args
1334 (length+ 123 456))
1335 (pass-if-exception "not a pair" list+-bad-arg-exception
1336 (length+ 'x))
1337 (pass-if-exception "improper list" list+-bad-arg-exception
1338 (length+ '(x y . z)))
1339 (pass-if (= 0 (length+ '())))
1340 (pass-if (= 1 (length+ '(x))))
1341 (pass-if (= 2 (length+ '(x y))))
1342 (pass-if (= 3 (length+ '(x y z))))
1343 (pass-if (not (length+ (circular-list 1))))
1344 (pass-if (not (length+ (circular-list 1 2))))
1345 (pass-if (not (length+ (circular-list 1 2 3)))))
1346
1347 ;;
1348 ;; last
1349 ;;
1350
1351 (with-test-prefix "last"
1352
1353 (pass-if-exception "empty" exception:wrong-type-arg
1354 (last '()))
1355 (pass-if "one elem"
1356 (eqv? 1 (last '(1))))
1357 (pass-if "two elems"
1358 (eqv? 2 (last '(1 2))))
1359 (pass-if "three elems"
1360 (eqv? 3 (last '(1 2 3))))
1361 (pass-if "four elems"
1362 (eqv? 4 (last '(1 2 3 4)))))
1363
1364 ;;
1365 ;; list=
1366 ;;
1367
1368 (with-test-prefix "list="
1369
1370 (pass-if "no lists"
1371 (eq? #t (list= eqv?)))
1372
1373 (with-test-prefix "one list"
1374
1375 (pass-if "empty"
1376 (eq? #t (list= eqv? '())))
1377 (pass-if "one elem"
1378 (eq? #t (list= eqv? '(1))))
1379 (pass-if "two elems"
1380 (eq? #t (list= eqv? '(2)))))
1381
1382 (with-test-prefix "two lists"
1383
1384 (pass-if "empty / empty"
1385 (eq? #t (list= eqv? '() '())))
1386
1387 (pass-if "one / empty"
1388 (eq? #f (list= eqv? '(1) '())))
1389
1390 (pass-if "empty / one"
1391 (eq? #f (list= eqv? '() '(1))))
1392
1393 (pass-if "one / one same"
1394 (eq? #t (list= eqv? '(1) '(1))))
1395
1396 (pass-if "one / one diff"
1397 (eq? #f (list= eqv? '(1) '(2))))
1398
1399 (pass-if "called arg order"
1400 (let ((good #t))
1401 (list= (lambda (x y)
1402 (set! good (and good (= (1+ x) y)))
1403 #t)
1404 '(1 3) '(2 4))
1405 good)))
1406
1407 (with-test-prefix "three lists"
1408
1409 (pass-if "empty / empty / empty"
1410 (eq? #t (list= eqv? '() '() '())))
1411
1412 (pass-if "one / empty / empty"
1413 (eq? #f (list= eqv? '(1) '() '())))
1414
1415 (pass-if "one / one / empty"
1416 (eq? #f (list= eqv? '(1) '(1) '())))
1417
1418 (pass-if "one / diff / empty"
1419 (eq? #f (list= eqv? '(1) '(2) '())))
1420
1421 (pass-if "one / one / one"
1422 (eq? #t (list= eqv? '(1) '(1) '(1))))
1423
1424 (pass-if "two / two / diff"
1425 (eq? #f (list= eqv? '(1 2) '(1 2) '(1 99))))
1426
1427 (pass-if "two / two / two"
1428 (eq? #t (list= eqv? '(1 2) '(1 2) '(1 2))))
1429
1430 (pass-if "called arg order"
1431 (let ((good #t))
1432 (list= (lambda (x y)
1433 (set! good (and good (= (1+ x) y)))
1434 #t)
1435 '(1 4) '(2 5) '(3 6))
1436 good))))
1437
1438 ;;
1439 ;; list-copy
1440 ;;
1441
1442 (with-test-prefix "list-copy"
1443 (pass-if (equal? '() (list-copy '())))
1444 (pass-if (equal? '(1 2) (list-copy '(1 2))))
1445 (pass-if (equal? '(1 2 3) (list-copy '(1 2 3))))
1446 (pass-if (equal? '(1 2 3 4) (list-copy '(1 2 3 4))))
1447 (pass-if (equal? '(1 2 3 4 5) (list-copy '(1 2 3 4 5))))
1448
1449 ;; improper lists can be copied
1450 (pass-if (equal? 1 (list-copy 1)))
1451 (pass-if (equal? '(1 . 2) (list-copy '(1 . 2))))
1452 (pass-if (equal? '(1 2 . 3) (list-copy '(1 2 . 3))))
1453 (pass-if (equal? '(1 2 3 . 4) (list-copy '(1 2 3 . 4))))
1454 (pass-if (equal? '(1 2 3 4 . 5) (list-copy '(1 2 3 4 . 5))))
1455
1456 (let ((src (list 1 2 3 4 5)))
1457 (define (find-pair? p lst)
1458 (let lp ((lst lst))
1459 (and (pair? lst) (or (eq? p lst) (lp (cdr lst))))))
1460 (pair-for-each (lambda (p) (pass-if (not (find-pair? p src))))
1461 (list-copy src))))
1462
1463 ;;
1464 ;; list-index
1465 ;;
1466
1467 (with-test-prefix "list-index"
1468 (pass-if-exception "no args" exception:wrong-num-args
1469 (list-index))
1470
1471 (pass-if-exception "one arg" exception:wrong-num-args
1472 (list-index noop))
1473
1474 (with-test-prefix "one list"
1475
1476 (pass-if "empty list" (eq? #f (list-index symbol? '())))
1477
1478 (pass-if-exception "pred arg count 0" exception:wrong-num-args
1479 (list-index (lambda () x) '(1 2 3)))
1480 (pass-if-exception "pred arg count 2" exception:wrong-num-args
1481 (list-index (lambda (x y) x) '(1 2 3)))
1482
1483 (pass-if-exception "improper 1" exception:wrong-type-arg
1484 (list-index symbol? 1))
1485 (pass-if-exception "improper 2" exception:wrong-type-arg
1486 (list-index symbol? '(1 . 2)))
1487 (pass-if-exception "improper 3" exception:wrong-type-arg
1488 (list-index symbol? '(1 2 . 3)))
1489
1490 (pass-if (eqv? #f (list-index symbol? '(1))))
1491 (pass-if (eqv? 0 (list-index symbol? '(x))))
1492
1493 (pass-if (eqv? #f (list-index symbol? '(1 2))))
1494 (pass-if (eqv? 0 (list-index symbol? '(x 1))))
1495 (pass-if (eqv? 1 (list-index symbol? '(1 x))))
1496
1497 (pass-if (eqv? #f (list-index symbol? '(1 2 3))))
1498 (pass-if (eqv? 0 (list-index symbol? '(x 1 2))))
1499 (pass-if (eqv? 1 (list-index symbol? '(1 x 2))))
1500 (pass-if (eqv? 2 (list-index symbol? '(1 2 x)))))
1501
1502 (with-test-prefix "two lists"
1503 (define (sym1 x y)
1504 (symbol? x))
1505 (define (sym2 x y)
1506 (symbol? y))
1507
1508 (pass-if "arg order"
1509 (eqv? 0 (list-index (lambda (x y)
1510 (and (= 1 x)
1511 (= 2 y)))
1512 '(1) '(2))))
1513
1514 (pass-if "empty lists" (eqv? #f (list-index sym2 '() '())))
1515
1516 (pass-if-exception "pred arg count 0" exception:wrong-num-args
1517 (list-index (lambda () #t) '(1 2 3) '(1 2 3)))
1518 (pass-if-exception "pred arg count 1" exception:wrong-num-args
1519 (list-index (lambda (x) x) '(1 2 3) '(1 2 3)))
1520 (pass-if-exception "pred arg count 3" exception:wrong-num-args
1521 (list-index (lambda (x y z) x) '(1 2 3) '(1 2 3)))
1522
1523 (pass-if-exception "improper first 1" exception:wrong-type-arg
1524 (list-index sym2 1 '(1 2 3)))
1525 (pass-if-exception "improper first 2" exception:wrong-type-arg
1526 (list-index sym2 '(1 . 2) '(1 2 3)))
1527 (pass-if-exception "improper first 3" exception:wrong-type-arg
1528 (list-index sym2 '(1 2 . 3) '(1 2 3)))
1529
1530 (pass-if-exception "improper second 1" exception:wrong-type-arg
1531 (list-index sym2 '(1 2 3) 1))
1532 (pass-if-exception "improper second 2" exception:wrong-type-arg
1533 (list-index sym2 '(1 2 3) '(1 . 2)))
1534 (pass-if-exception "improper second 3" exception:wrong-type-arg
1535 (list-index sym2 '(1 2 3) '(1 2 . 3)))
1536
1537 (pass-if (eqv? #f (list-index sym2 '(1) '(2))))
1538 (pass-if (eqv? 0 (list-index sym2 '(1) '(x))))
1539
1540 (pass-if (eqv? #f (list-index sym2 '(1 2) '(3 4))))
1541 (pass-if (eqv? 0 (list-index sym2 '(1 2) '(x 3))))
1542 (pass-if (eqv? 1 (list-index sym2 '(1 2) '(3 x))))
1543
1544 (pass-if (eqv? #f (list-index sym2 '(1 2 3) '(3 4 5))))
1545 (pass-if (eqv? 0 (list-index sym2 '(1 2 3) '(x 3 4))))
1546 (pass-if (eqv? 1 (list-index sym2 '(1 2 3) '(3 x 4))))
1547 (pass-if (eqv? 2 (list-index sym2 '(1 2 3) '(3 4 x))))
1548
1549 (with-test-prefix "stop shortest"
1550 (pass-if (eqv? #f (list-index sym1 '(1 2 x) '(4 5))))
1551 (pass-if (eqv? #f (list-index sym2 '(4 5) '(1 2 x))))
1552 (pass-if (eqv? #f (list-index sym1 '(3 4) '(1 2 x y))))
1553 (pass-if (eqv? #f (list-index sym2 '(1 2 x y) '(3 4))))))
1554
1555 (with-test-prefix "three lists"
1556 (define (sym1 x y z)
1557 (symbol? x))
1558 (define (sym2 x y z)
1559 (symbol? y))
1560 (define (sym3 x y z)
1561 (symbol? z))
1562
1563 (pass-if "arg order"
1564 (eqv? 0 (list-index (lambda (x y z)
1565 (and (= 1 x)
1566 (= 2 y)
1567 (= 3 z)))
1568 '(1) '(2) '(3))))
1569
1570 (pass-if "empty lists" (eqv? #f (list-index sym3 '() '() '())))
1571
1572 ;; currently bad pred argument gives wrong-num-args when 3 or more
1573 ;; lists, as opposed to wrong-type-arg for 1 or 2 lists
1574 (pass-if-exception "pred arg count 0" exception:wrong-num-args
1575 (list-index (lambda () #t) '(1 2 3) '(1 2 3) '(1 2 3)))
1576 (pass-if-exception "pred arg count 2" exception:wrong-num-args
1577 (list-index (lambda (x y) x) '(1 2 3) '(1 2 3)'(1 2 3) ))
1578 (pass-if-exception "pred arg count 4" exception:wrong-num-args
1579 (list-index (lambda (w x y z) x) '(1 2 3) '(1 2 3) '(1 2 3)))
1580
1581 (pass-if-exception "improper first 1" exception:wrong-type-arg
1582 (list-index sym3 1 '(1 2 3) '(1 2 3)))
1583 (pass-if-exception "improper first 2" exception:wrong-type-arg
1584 (list-index sym3 '(1 . 2) '(1 2 3) '(1 2 3)))
1585 (pass-if-exception "improper first 3" exception:wrong-type-arg
1586 (list-index sym3 '(1 2 . 3) '(1 2 3) '(1 2 3)))
1587
1588 (pass-if-exception "improper second 1" exception:wrong-type-arg
1589 (list-index sym3 '(1 2 3) 1 '(1 2 3)))
1590 (pass-if-exception "improper second 2" exception:wrong-type-arg
1591 (list-index sym3 '(1 2 3) '(1 . 2) '(1 2 3)))
1592 (pass-if-exception "improper second 3" exception:wrong-type-arg
1593 (list-index sym3 '(1 2 3) '(1 2 . 3) '(1 2 3)))
1594
1595 (pass-if-exception "improper third 1" exception:wrong-type-arg
1596 (list-index sym3 '(1 2 3) '(1 2 3) 1))
1597 (pass-if-exception "improper third 2" exception:wrong-type-arg
1598 (list-index sym3 '(1 2 3) '(1 2 3) '(1 . 2)))
1599 (pass-if-exception "improper third 3" exception:wrong-type-arg
1600 (list-index sym3 '(1 2 3) '(1 2 3) '(1 2 . 3)))
1601
1602 (pass-if (eqv? #f (list-index sym3 '(#f) '(#f) '(#f))))
1603 (pass-if (eqv? 0 (list-index sym3 '(#f) '(#f) '(x))))
1604
1605 (pass-if (eqv? #f (list-index sym3 '(#f #f) '(#f #f) '(#f #f))))
1606 (pass-if (eqv? 0 (list-index sym3 '(#f #f) '(#f #f) '(x #f))))
1607 (pass-if (eqv? 1 (list-index sym3 '(#f #f) '(#f #f) '(#f x))))
1608
1609 (pass-if (eqv? #f (list-index sym3 '(#f #f #f) '(#f #f #f) '(#f #f #f))))
1610 (pass-if (eqv? 0 (list-index sym3 '(#f #f #f) '(#f #f #f) '(x #f #f))))
1611 (pass-if (eqv? 1 (list-index sym3 '(#f #f #f) '(#f #f #f) '(#f x #f))))
1612 (pass-if (eqv? 2 (list-index sym3 '(#f #f #f) '(#f #f #f) '(#f #f x))))
1613
1614 (with-test-prefix "stop shortest"
1615 (pass-if (eqv? #f (list-index sym2 '() '(x x x) '(x x))))
1616 (pass-if (eqv? #f (list-index sym1 '(x x x) '() '(x x))))
1617 (pass-if (eqv? #f (list-index sym2 '(x x x) '(x x) '())))
1618
1619 (pass-if (eqv? #f (list-index sym2 '(#t) '(#t x x) '(#t x))))
1620 (pass-if (eqv? #f (list-index sym1 '(#t x x) '(#t) '(#t x))))
1621 (pass-if (eqv? #f (list-index sym1 '(#t x x) '(#t x) '(#t)))))
1622
1623 (pass-if "apply list unchanged"
1624 (let ((lst (list (list 1 2) (list 3 4) (list 5 6))))
1625 (and (equal? #f (apply list-index sym3 lst))
1626 ;; lst unmodified
1627 (equal? '((1 2) (3 4) (5 6)) lst))))))
1628
1629 ;;
1630 ;; list-tabulate
1631 ;;
1632
1633 (with-test-prefix "list-tabulate"
1634
1635 (pass-if-exception "-1" exception:wrong-type-arg
1636 (list-tabulate -1 identity))
1637 (pass-if "0"
1638 (equal? '() (list-tabulate 0 identity)))
1639 (pass-if "1"
1640 (equal? '(0) (list-tabulate 1 identity)))
1641 (pass-if "2"
1642 (equal? '(0 1) (list-tabulate 2 identity)))
1643 (pass-if "3"
1644 (equal? '(0 1 2) (list-tabulate 3 identity)))
1645 (pass-if "4"
1646 (equal? '(0 1 2 3) (list-tabulate 4 identity)))
1647 (pass-if "string ref proc"
1648 (equal? '(#\a #\b #\c #\d) (list-tabulate 4
1649 (lambda (i)
1650 (string-ref "abcd" i))))))
1651
1652 ;;
1653 ;; lset=
1654 ;;
1655
1656 (with-test-prefix "lset="
1657
1658 ;; in guile 1.6.7 and earlier, lset= incorrectly demanded at least one
1659 ;; list arg
1660 (pass-if "no args"
1661 (eq? #t (lset= eq?)))
1662
1663 (with-test-prefix "one arg"
1664
1665 (pass-if "()"
1666 (eq? #t (lset= eqv? '())))
1667
1668 (pass-if "(1)"
1669 (eq? #t (lset= eqv? '(1))))
1670
1671 (pass-if "(1 2)"
1672 (eq? #t (lset= eqv? '(1 2)))))
1673
1674 (with-test-prefix "two args"
1675
1676 (pass-if "() ()"
1677 (eq? #t (lset= eqv? '() '())))
1678
1679 (pass-if "(1) (1)"
1680 (eq? #t (lset= eqv? '(1) '(1))))
1681
1682 (pass-if "(1) (2)"
1683 (eq? #f (lset= eqv? '(1) '(2))))
1684
1685 (pass-if "(1) (1 2)"
1686 (eq? #f (lset= eqv? '(1) '(1 2))))
1687
1688 (pass-if "(1 2) (2 1)"
1689 (eq? #t (lset= eqv? '(1 2) '(2 1))))
1690
1691 (pass-if "called arg order"
1692 (let ((good #t))
1693 (lset= (lambda (x y)
1694 (if (not (= x (1- y)))
1695 (set! good #f))
1696 #t)
1697 '(1 1) '(2 2))
1698 good)))
1699
1700 (with-test-prefix "three args"
1701
1702 (pass-if "() () ()"
1703 (eq? #t (lset= eqv? '() '() '())))
1704
1705 (pass-if "(1) (1) (1)"
1706 (eq? #t (lset= eqv? '(1) '(1) '(1))))
1707
1708 (pass-if "(1) (1) (2)"
1709 (eq? #f (lset= eqv? '(1) '(1) '(2))))
1710
1711 (pass-if "(1) (1) (1 2)"
1712 (eq? #f (lset= eqv? '(1) '(1) '(1 2))))
1713
1714 (pass-if "(1 2 3) (3 2 1) (1 3 2)"
1715 (eq? #t (lset= eqv? '(1 2 3) '(3 2 1) '(1 3 2))))
1716
1717 (pass-if "called arg order"
1718 (let ((good #t))
1719 (lset= (lambda (x y)
1720 (if (not (= x (1- y)))
1721 (set! good #f))
1722 #t)
1723 '(1 1) '(2 2) '(3 3))
1724 good))))
1725
1726 ;;
1727 ;; lset-adjoin
1728 ;;
1729
1730 (with-test-prefix "lset-adjoin"
1731
1732 ;; in guile 1.6.7 and earlier, lset-adjoin didn't actually use the given
1733 ;; `=' procedure, all comparisons were just with `equal?
1734 ;;
1735 (with-test-prefix "case-insensitive ="
1736
1737 (pass-if "(\"x\") \"X\""
1738 (equal? '("x") (lset-adjoin string-ci=? '("x") "X"))))
1739
1740 (pass-if "called arg order"
1741 (let ((good #f))
1742 (lset-adjoin (lambda (x y)
1743 (set! good (and (= x 1) (= y 2)))
1744 (= x y))
1745 '(1) 2)
1746 good))
1747
1748 (pass-if (equal? '() (lset-adjoin = '())))
1749
1750 (pass-if (equal? '(1) (lset-adjoin = '() 1)))
1751
1752 (pass-if (equal? '(1) (lset-adjoin = '() 1 1)))
1753
1754 (pass-if (equal? '(2 1) (lset-adjoin = '() 1 2)))
1755
1756 (pass-if (equal? '(3 1 2) (lset-adjoin = '(1 2) 1 2 3 2 1)))
1757
1758 (pass-if "apply list unchanged"
1759 (let ((lst (list 1 2)))
1760 (and (equal? '(2 1 3) (apply lset-adjoin = '(3) lst))
1761 ;; lst unmodified
1762 (equal? '(1 2) lst))))
1763
1764 (pass-if "(1 1) 1 1"
1765 (equal? '(1 1) (lset-adjoin = '(1 1) 1 1)))
1766
1767 ;; duplicates among args are cast out
1768 (pass-if "(2) 1 1"
1769 (equal? '(1 2) (lset-adjoin = '(2) 1 1))))
1770
1771 ;;
1772 ;; lset-difference and lset-difference!
1773 ;;
1774
1775 (begin
1776 (define (test-shared-behavior diff)
1777 (pass-if-exception "proc - num" exception:wrong-type-arg
1778 (diff 123 '(4)))
1779 (pass-if-exception "proc - list" exception:wrong-type-arg
1780 (diff (list 1 2 3) '(4)))
1781
1782 (pass-if "called arg order"
1783 (let ((good #f))
1784 (diff (lambda (x y)
1785 (set! good (and (= x 1) (= y 2)))
1786 (= x y))
1787 (list 1) (list 2))
1788 good))
1789
1790 (pass-if (equal? '() (diff = '())))
1791 (pass-if (equal? '(1) (diff = (list 1))))
1792 (pass-if (equal? '(1 2) (diff = (list 1 2))))
1793
1794 (pass-if (equal? '() (diff = (list ) '(3))))
1795 (pass-if (equal? '() (diff = (list 3) '(3))))
1796 (pass-if (equal? '(1) (diff = (list 1 3) '(3))))
1797 (pass-if (equal? '(1) (diff = (list 3 1) '(3))))
1798 (pass-if (equal? '(1) (diff = (list 1 3 3) '(3))))
1799 (pass-if (equal? '(1) (diff = (list 3 1 3) '(3))))
1800 (pass-if (equal? '(1) (diff = (list 3 3 1) '(3))))
1801
1802 (pass-if (equal? '(1) (diff = (list 1 2 3) '(2 3))))
1803 (pass-if (equal? '(1) (diff = (list 1 2 3) '(3 2))))
1804 (pass-if (equal? '(1) (diff = (list 1 2 3) '(3) '(2))))
1805 (pass-if (equal? '(1) (diff = (list 1 2 3) '(2) '(3))))
1806 (pass-if (equal? '(1) (diff = (list 1 2 3) '(2) '(2 3))))
1807 (pass-if (equal? '(1) (diff = (list 1 2 3) '(2) '(3 2))))
1808
1809 (pass-if (equal? '(1 2) (diff = (list 1 2 3) '(3) '(3))))
1810 (pass-if (equal? '(1 2) (diff = (list 1 3 2) '(3) '(3))))
1811 (pass-if (equal? '(1 2) (diff = (list 3 1 2) '(3) '(3))))
1812
1813 (pass-if (equal? '(1 2 3) (diff = (list 1 2 3 4) '(4))))
1814 (pass-if (equal? '(1 2 3) (diff = (list 1 2 4 3) '(4))))
1815 (pass-if (equal? '(1 2 3) (diff = (list 1 4 2 3) '(4))))
1816 (pass-if (equal? '(1 2 3) (diff = (list 4 1 2 3) '(4))))
1817
1818 (pass-if (equal? '(1 2) (diff = (list 1 2 3 4) '(4) '(3))))
1819 (pass-if (equal? '(1 2) (diff = (list 1 3 2 4) '(4) '(3))))
1820 (pass-if (equal? '(1 2) (diff = (list 3 1 2 4) '(4) '(3))))
1821 (pass-if (equal? '(1 2) (diff = (list 1 3 4 2) '(4) '(3))))
1822 (pass-if (equal? '(1 2) (diff = (list 3 1 4 2) '(4) '(3))))
1823 (pass-if (equal? '(1 2) (diff = (list 3 4 1 2) '(4) '(3)))))
1824
1825 (with-test-prefix "lset-difference"
1826 (test-shared-behavior lset-difference))
1827
1828 (with-test-prefix "lset-difference!"
1829 (test-shared-behavior lset-difference!)))
1830
1831 ;;
1832 ;; lset-diff+intersection
1833 ;;
1834
1835 (with-test-prefix "lset-diff+intersection"
1836
1837 (pass-if "called arg order"
1838 (let ((good #f))
1839 (lset-diff+intersection (lambda (x y)
1840 (set! good (and (= x 1) (= y 2)))
1841 (= x y))
1842 '(1) '(2))
1843 good)))
1844
1845 ;;
1846 ;; lset-diff+intersection!
1847 ;;
1848
1849 (with-test-prefix "lset-diff+intersection"
1850
1851 (pass-if "called arg order"
1852 (let ((good #f))
1853 (lset-diff+intersection (lambda (x y)
1854 (set! good (and (= x 1) (= y 2)))
1855 (= x y))
1856 (list 1) (list 2))
1857 good)))
1858
1859 ;;
1860 ;; lset-intersection
1861 ;;
1862
1863 (with-test-prefix "lset-intersection"
1864
1865 (pass-if "called arg order"
1866 (let ((good #f))
1867 (lset-intersection (lambda (x y)
1868 (set! good (and (= x 1) (= y 2)))
1869 (= x y))
1870 '(1) '(2))
1871 good)))
1872
1873 ;;
1874 ;; lset-intersection!
1875 ;;
1876
1877 (with-test-prefix "lset-intersection"
1878
1879 (pass-if "called arg order"
1880 (let ((good #f))
1881 (lset-intersection (lambda (x y)
1882 (set! good (and (= x 1) (= y 2)))
1883 (= x y))
1884 (list 1) (list 2))
1885 good)))
1886
1887 ;;
1888 ;; lset-union
1889 ;;
1890
1891 (with-test-prefix "lset-union"
1892
1893 (pass-if "no args"
1894 (eq? '() (lset-union eq?)))
1895
1896 (pass-if "one arg"
1897 (equal? '(1 2 3) (lset-union eq? '(1 2 3))))
1898
1899 (pass-if "'() '()"
1900 (equal? '() (lset-union eq? '() '())))
1901
1902 (pass-if "'() '(1 2 3)"
1903 (equal? '(1 2 3) (lset-union eq? '() '(1 2 3))))
1904
1905 (pass-if "'(1 2 3) '()"
1906 (equal? '(1 2 3) (lset-union eq? '(1 2 3) '())))
1907
1908 (pass-if "'(1 2 3) '(4 3 5)"
1909 (equal? '(5 4 1 2 3) (lset-union eq? '(1 2 3) '(4 3 5))))
1910
1911 (pass-if "'(1 2 3) '(4) '(3 5))"
1912 (equal? '(5 4 1 2 3) (lset-union eq? '(1 2 3) '(4) '(3 5))))
1913
1914 ;; in guile 1.6.7 and earlier, `=' was called with the arguments the wrong
1915 ;; way around
1916 (pass-if "called arg order"
1917 (let ((good #f))
1918 (lset-union (lambda (x y)
1919 (set! good (and (= x 1) (= y 2)))
1920 (= x y))
1921 '(1) '(2))
1922 good)))
1923
1924 ;;
1925 ;; member
1926 ;;
1927
1928 (with-test-prefix "member"
1929
1930 (pass-if-exception "no args" exception:wrong-num-args
1931 (member))
1932
1933 (pass-if-exception "one arg" exception:wrong-num-args
1934 (member 1))
1935
1936 (pass-if "1 (1 2 3)"
1937 (let ((lst '(1 2 3)))
1938 (eq? lst (member 1 lst))))
1939
1940 (pass-if "2 (1 2 3)"
1941 (let ((lst '(1 2 3)))
1942 (eq? (cdr lst) (member 2 lst))))
1943
1944 (pass-if "3 (1 2 3)"
1945 (let ((lst '(1 2 3)))
1946 (eq? (cddr lst) (member 3 lst))))
1947
1948 (pass-if "4 (1 2 3)"
1949 (let ((lst '(1 2 3)))
1950 (eq? #f (member 4 lst))))
1951
1952 (pass-if "called arg order"
1953 (let ((good #f))
1954 (member 1 '(2) (lambda (x y)
1955 (set! good (and (eqv? 1 x)
1956 (eqv? 2 y)))))
1957 good)))
1958
1959 ;;
1960 ;; ninth
1961 ;;
1962
1963 (with-test-prefix "ninth"
1964 (pass-if-exception "() -1" exception:wrong-type-arg
1965 (ninth '(a b c d e f g h)))
1966 (pass-if (eq? 'i (ninth '(a b c d e f g h i))))
1967 (pass-if (eq? 'i (ninth '(a b c d e f g h i j)))))
1968
1969
1970 ;;
1971 ;; not-pair?
1972 ;;
1973
1974 (with-test-prefix "not-pair?"
1975 (pass-if "inum"
1976 (eq? #t (not-pair? 123)))
1977 (pass-if "pair"
1978 (eq? #f (not-pair? '(x . y))))
1979 (pass-if "symbol"
1980 (eq? #t (not-pair? 'x))))
1981
1982 ;;
1983 ;; take
1984 ;;
1985
1986 (with-test-prefix "take"
1987
1988 (pass-if "'() 0"
1989 (null? (take '() 0)))
1990
1991 (pass-if "'(a) 0"
1992 (null? (take '(a) 0)))
1993
1994 (pass-if "'(a b) 0"
1995 (null? (take '() 0)))
1996
1997 (pass-if "'(a b c) 0"
1998 (null? (take '() 0)))
1999
2000 (pass-if "'(a) 1"
2001 (let* ((lst '(a))
2002 (got (take lst 1)))
2003 (and (equal? '(a) got)
2004 (not (eq? lst got)))))
2005
2006 (pass-if "'(a b) 1"
2007 (equal? '(a)
2008 (take '(a b) 1)))
2009
2010 (pass-if "'(a b c) 1"
2011 (equal? '(a)
2012 (take '(a b c) 1)))
2013
2014 (pass-if "'(a b) 2"
2015 (let* ((lst '(a b))
2016 (got (take lst 2)))
2017 (and (equal? '(a b) got)
2018 (not (eq? lst got)))))
2019
2020 (pass-if "'(a b c) 2"
2021 (equal? '(a b)
2022 (take '(a b c) 2)))
2023
2024 (pass-if "circular '(a) 0"
2025 (equal? '()
2026 (take (circular-list 'a) 0)))
2027
2028 (pass-if "circular '(a) 1"
2029 (equal? '(a)
2030 (take (circular-list 'a) 1)))
2031
2032 (pass-if "circular '(a) 2"
2033 (equal? '(a a)
2034 (take (circular-list 'a) 2)))
2035
2036 (pass-if "circular '(a b) 5"
2037 (equal? '(a b a b a)
2038 (take (circular-list 'a 'b) 5)))
2039
2040 (pass-if "'(a . b) 1"
2041 (equal? '(a)
2042 (take '(a . b) 1)))
2043
2044 (pass-if "'(a b . c) 1"
2045 (equal? '(a)
2046 (take '(a b . c) 1)))
2047
2048 (pass-if "'(a b . c) 2"
2049 (equal? '(a b)
2050 (take '(a b . c) 2))))
2051
2052 ;;
2053 ;; take-while
2054 ;;
2055
2056 (with-test-prefix "take-while"
2057
2058 (pass-if (equal? '() (take-while odd? '())))
2059 (pass-if (equal? '(1) (take-while odd? '(1))))
2060 (pass-if (equal? '(1 3) (take-while odd? '(1 3))))
2061 (pass-if (equal? '(1 3 5) (take-while odd? '(1 3 5))))
2062
2063 (pass-if (equal? '() (take-while odd? '(2))))
2064 (pass-if (equal? '(1) (take-while odd? '(1 2))))
2065 (pass-if (equal? '(1 3) (take-while odd? '(1 3 4))))
2066
2067 (pass-if (equal? '() (take-while odd? '(2 1))))
2068 (pass-if (equal? '(1) (take-while odd? '(1 4 3))))
2069 (pass-if (equal? '() (take-while odd? '(4 1 3)))))
2070
2071 ;;
2072 ;; take-while!
2073 ;;
2074
2075 (with-test-prefix "take-while!"
2076
2077 (pass-if (equal? '() (take-while! odd? '())))
2078 (pass-if (equal? '(1) (take-while! odd? (list 1))))
2079 (pass-if (equal? '(1 3) (take-while! odd? (list 1 3))))
2080 (pass-if (equal? '(1 3 5) (take-while! odd? (list 1 3 5))))
2081
2082 (pass-if (equal? '() (take-while! odd? (list 2))))
2083 (pass-if (equal? '(1) (take-while! odd? (list 1 2))))
2084 (pass-if (equal? '(1 3) (take-while! odd? (list 1 3 4))))
2085
2086 (pass-if (equal? '() (take-while! odd? (list 2 1))))
2087 (pass-if (equal? '(1) (take-while! odd? (list 1 4 3))))
2088 (pass-if (equal? '() (take-while! odd? (list 4 1 3)))))
2089
2090 ;;
2091 ;; partition
2092 ;;
2093
2094 (define (test-partition pred list kept-good dropped-good)
2095 (call-with-values (lambda ()
2096 (partition pred list))
2097 (lambda (kept dropped)
2098 (and (equal? kept kept-good)
2099 (equal? dropped dropped-good)))))
2100
2101 (with-test-prefix "partition"
2102
2103 (pass-if "with dropped tail"
2104 (test-partition even? '(1 2 3 4 5 6 7)
2105 '(2 4 6) '(1 3 5 7)))
2106
2107 (pass-if "with kept tail"
2108 (test-partition even? '(1 2 3 4 5 6)
2109 '(2 4 6) '(1 3 5)))
2110
2111 (pass-if "with everything dropped"
2112 (test-partition even? '(1 3 5 7)
2113 '() '(1 3 5 7)))
2114
2115 (pass-if "with everything kept"
2116 (test-partition even? '(2 4 6)
2117 '(2 4 6) '()))
2118
2119 (pass-if "with empty list"
2120 (test-partition even? '()
2121 '() '()))
2122
2123 (pass-if "with reasonably long list"
2124 ;; the old implementation from SRFI-1 reference implementation
2125 ;; would signal a stack-overflow for a list of only 500 elements!
2126 (call-with-values (lambda ()
2127 (partition even?
2128 (make-list 10000 1)))
2129 (lambda (even odd)
2130 (and (= (length odd) 10000)
2131 (= (length even) 0)))))
2132
2133 (pass-if-exception "with improper list"
2134 exception:wrong-type-arg
2135 (partition symbol? '(a b . c))))
2136
2137 ;;
2138 ;; partition!
2139 ;;
2140
2141 (define (test-partition! pred list kept-good dropped-good)
2142 (call-with-values (lambda ()
2143 (partition! pred list))
2144 (lambda (kept dropped)
2145 (and (equal? kept kept-good)
2146 (equal? dropped dropped-good)))))
2147
2148 (with-test-prefix "partition!"
2149
2150 (pass-if "with dropped tail"
2151 (test-partition! even? (list 1 2 3 4 5 6 7)
2152 '(2 4 6) '(1 3 5 7)))
2153
2154 (pass-if "with kept tail"
2155 (test-partition! even? (list 1 2 3 4 5 6)
2156 '(2 4 6) '(1 3 5)))
2157
2158 (pass-if "with everything dropped"
2159 (test-partition! even? (list 1 3 5 7)
2160 '() '(1 3 5 7)))
2161
2162 (pass-if "with everything kept"
2163 (test-partition! even? (list 2 4 6)
2164 '(2 4 6) '()))
2165
2166 (pass-if "with empty list"
2167 (test-partition! even? '()
2168 '() '()))
2169
2170 (pass-if "with reasonably long list"
2171 ;; the old implementation from SRFI-1 reference implementation
2172 ;; would signal a stack-overflow for a list of only 500 elements!
2173 (call-with-values (lambda ()
2174 (partition! even?
2175 (make-list 10000 1)))
2176 (lambda (even odd)
2177 (and (= (length odd) 10000)
2178 (= (length even) 0)))))
2179
2180 (pass-if-exception "with improper list"
2181 exception:wrong-type-arg
2182 (partition! symbol? (cons* 'a 'b 'c))))
2183
2184 ;;
2185 ;; reduce
2186 ;;
2187
2188 (with-test-prefix "reduce"
2189
2190 (pass-if "empty"
2191 (let* ((calls '())
2192 (ret (reduce (lambda (x prev)
2193 (set! calls (cons (list x prev) calls))
2194 x)
2195 1 '())))
2196 (and (equal? calls '())
2197 (equal? ret 1))))
2198
2199 (pass-if "one elem"
2200 (let* ((calls '())
2201 (ret (reduce (lambda (x prev)
2202 (set! calls (cons (list x prev) calls))
2203 x)
2204 1 '(2))))
2205 (and (equal? calls '())
2206 (equal? ret 2))))
2207
2208 (pass-if "two elems"
2209 (let* ((calls '())
2210 (ret (reduce (lambda (x prev)
2211 (set! calls (cons (list x prev) calls))
2212 x)
2213 1 '(2 3))))
2214 (and (equal? calls '((3 2)))
2215 (equal? ret 3))))
2216
2217 (pass-if "three elems"
2218 (let* ((calls '())
2219 (ret (reduce (lambda (x prev)
2220 (set! calls (cons (list x prev) calls))
2221 x)
2222 1 '(2 3 4))))
2223 (and (equal? calls '((4 3)
2224 (3 2)))
2225 (equal? ret 4))))
2226
2227 (pass-if "four elems"
2228 (let* ((calls '())
2229 (ret (reduce (lambda (x prev)
2230 (set! calls (cons (list x prev) calls))
2231 x)
2232 1 '(2 3 4 5))))
2233 (and (equal? calls '((5 4)
2234 (4 3)
2235 (3 2)))
2236 (equal? ret 5)))))
2237
2238 ;;
2239 ;; reduce-right
2240 ;;
2241
2242 (with-test-prefix "reduce-right"
2243
2244 (pass-if "empty"
2245 (let* ((calls '())
2246 (ret (reduce-right (lambda (x prev)
2247 (set! calls (cons (list x prev) calls))
2248 x)
2249 1 '())))
2250 (and (equal? calls '())
2251 (equal? ret 1))))
2252
2253 (pass-if "one elem"
2254 (let* ((calls '())
2255 (ret (reduce-right (lambda (x prev)
2256 (set! calls (cons (list x prev) calls))
2257 x)
2258 1 '(2))))
2259 (and (equal? calls '())
2260 (equal? ret 2))))
2261
2262 (pass-if "two elems"
2263 (let* ((calls '())
2264 (ret (reduce-right (lambda (x prev)
2265 (set! calls (cons (list x prev) calls))
2266 x)
2267 1 '(2 3))))
2268 (and (equal? calls '((2 3)))
2269 (equal? ret 2))))
2270
2271 (pass-if "three elems"
2272 (let* ((calls '())
2273 (ret (reduce-right (lambda (x prev)
2274 (set! calls (cons (list x prev) calls))
2275 x)
2276 1 '(2 3 4))))
2277 (and (equal? calls '((2 3)
2278 (3 4)))
2279 (equal? ret 2))))
2280
2281 (pass-if "four elems"
2282 (let* ((calls '())
2283 (ret (reduce-right (lambda (x prev)
2284 (set! calls (cons (list x prev) calls))
2285 x)
2286 1 '(2 3 4 5))))
2287 (and (equal? calls '((2 3)
2288 (3 4)
2289 (4 5)))
2290 (equal? ret 2)))))
2291
2292 ;;
2293 ;; remove
2294 ;;
2295
2296 (with-test-prefix "remove"
2297
2298 (pass-if (equal? '() (remove odd? '())))
2299 (pass-if (equal? '() (remove odd? '(1))))
2300 (pass-if (equal? '(2) (remove odd? '(2))))
2301
2302 (pass-if (equal? '() (remove odd? '(1 3))))
2303 (pass-if (equal? '(2) (remove odd? '(2 3))))
2304 (pass-if (equal? '(2) (remove odd? '(1 2))))
2305 (pass-if (equal? '(2 4) (remove odd? '(2 4))))
2306
2307 (pass-if (equal? '() (remove odd? '(1 3 5))))
2308 (pass-if (equal? '(2) (remove odd? '(2 3 5))))
2309 (pass-if (equal? '(2) (remove odd? '(1 2 5))))
2310 (pass-if (equal? '(2 4) (remove odd? '(2 4 5))))
2311
2312 (pass-if (equal? '(6) (remove odd? '(1 3 6))))
2313 (pass-if (equal? '(2 6) (remove odd? '(2 3 6))))
2314 (pass-if (equal? '(2 6) (remove odd? '(1 2 6))))
2315 (pass-if (equal? '(2 4 6) (remove odd? '(2 4 6)))))
2316
2317 ;;
2318 ;; remove!
2319 ;;
2320
2321 (with-test-prefix "remove!"
2322
2323 (pass-if (equal? '() (remove! odd? '())))
2324 (pass-if (equal? '() (remove! odd? (list 1))))
2325 (pass-if (equal? '(2) (remove! odd? (list 2))))
2326
2327 (pass-if (equal? '() (remove! odd? (list 1 3))))
2328 (pass-if (equal? '(2) (remove! odd? (list 2 3))))
2329 (pass-if (equal? '(2) (remove! odd? (list 1 2))))
2330 (pass-if (equal? '(2 4) (remove! odd? (list 2 4))))
2331
2332 (pass-if (equal? '() (remove! odd? (list 1 3 5))))
2333 (pass-if (equal? '(2) (remove! odd? (list 2 3 5))))
2334 (pass-if (equal? '(2) (remove! odd? (list 1 2 5))))
2335 (pass-if (equal? '(2 4) (remove! odd? (list 2 4 5))))
2336
2337 (pass-if (equal? '(6) (remove! odd? (list 1 3 6))))
2338 (pass-if (equal? '(2 6) (remove! odd? (list 2 3 6))))
2339 (pass-if (equal? '(2 6) (remove! odd? (list 1 2 6))))
2340 (pass-if (equal? '(2 4 6) (remove! odd? (list 2 4 6)))))
2341
2342 ;;
2343 ;; seventh
2344 ;;
2345
2346 (with-test-prefix "seventh"
2347 (pass-if-exception "() -1" exception:wrong-type-arg
2348 (seventh '(a b c d e f)))
2349 (pass-if (eq? 'g (seventh '(a b c d e f g))))
2350 (pass-if (eq? 'g (seventh '(a b c d e f g h)))))
2351
2352 ;;
2353 ;; sixth
2354 ;;
2355
2356 (with-test-prefix "sixth"
2357 (pass-if-exception "() -1" exception:wrong-type-arg
2358 (sixth '(a b c d e)))
2359 (pass-if (eq? 'f (sixth '(a b c d e f))))
2360 (pass-if (eq? 'f (sixth '(a b c d e f g)))))
2361
2362 ;;
2363 ;; split-at
2364 ;;
2365
2366 (with-test-prefix "split-at"
2367
2368 (define (equal-values? lst thunk)
2369 (call-with-values thunk
2370 (lambda got
2371 (equal? lst got))))
2372
2373 (pass-if-exception "() -1" exception:out-of-range
2374 (split-at '() -1))
2375 (pass-if (equal-values? '(() ())
2376 (lambda () (split-at '() 0))))
2377 (pass-if-exception "() 1" exception:wrong-type-arg
2378 (split-at '() 1))
2379
2380 (pass-if-exception "(1) -1" exception:out-of-range
2381 (split-at '(1) -1))
2382 (pass-if (equal-values? '(() (1)) (lambda () (split-at '(1) 0))))
2383 (pass-if (equal-values? '((1) ()) (lambda () (split-at '(1) 1))))
2384 (pass-if-exception "(1) 2" exception:wrong-type-arg
2385 (split-at '(1) 2))
2386
2387 (pass-if-exception "(4 5) -1" exception:out-of-range
2388 (split-at '(4 5) -1))
2389 (pass-if (equal-values? '(() (4 5)) (lambda () (split-at '(4 5) 0))))
2390 (pass-if (equal-values? '((4) (5)) (lambda () (split-at '(4 5) 1))))
2391 (pass-if (equal-values? '((4 5) ()) (lambda () (split-at '(4 5) 2))))
2392 (pass-if-exception "(4 5) 3" exception:wrong-type-arg
2393 (split-at '(4 5) 3))
2394
2395 (pass-if-exception "(4 5 6) -1" exception:out-of-range
2396 (split-at '(4 5 6) -1))
2397 (pass-if (equal-values? '(() (4 5 6)) (lambda () (split-at '(4 5 6) 0))))
2398 (pass-if (equal-values? '((4) (5 6)) (lambda () (split-at '(4 5 6) 1))))
2399 (pass-if (equal-values? '((4 5) (6)) (lambda () (split-at '(4 5 6) 2))))
2400 (pass-if (equal-values? '((4 5 6) ()) (lambda () (split-at '(4 5 6) 3))))
2401 (pass-if-exception "(4 5 6) 4" exception:wrong-type-arg
2402 (split-at '(4 5 6) 4)))
2403
2404 ;;
2405 ;; split-at!
2406 ;;
2407
2408 (with-test-prefix "split-at!"
2409
2410 (define (equal-values? lst thunk)
2411 (call-with-values thunk
2412 (lambda got
2413 (equal? lst got))))
2414
2415 (pass-if-exception "() -1" exception:out-of-range
2416 (split-at! '() -1))
2417 (pass-if (equal-values? '(() ())
2418 (lambda () (split-at! '() 0))))
2419 (pass-if-exception "() 1" exception:wrong-type-arg
2420 (split-at! '() 1))
2421
2422 (pass-if-exception "(1) -1" exception:out-of-range
2423 (split-at! (list 1) -1))
2424 (pass-if (equal-values? '(() (1)) (lambda () (split-at! (list 1) 0))))
2425 (pass-if (equal-values? '((1) ()) (lambda () (split-at! (list 1) 1))))
2426 (pass-if-exception "(1) 2" exception:wrong-type-arg
2427 (split-at! (list 1) 2))
2428
2429 (pass-if-exception "(4 5) -1" exception:out-of-range
2430 (split-at! (list 4 5) -1))
2431 (pass-if (equal-values? '(() (4 5)) (lambda () (split-at! (list 4 5) 0))))
2432 (pass-if (equal-values? '((4) (5)) (lambda () (split-at! (list 4 5) 1))))
2433 (pass-if (equal-values? '((4 5) ()) (lambda () (split-at! (list 4 5) 2))))
2434 (pass-if-exception "(4 5) 3" exception:wrong-type-arg
2435 (split-at! (list 4 5) 3))
2436
2437 (pass-if-exception "(4 5 6) -1" exception:out-of-range
2438 (split-at! (list 4 5 6) -1))
2439 (pass-if (equal-values? '(() (4 5 6)) (lambda () (split-at! (list 4 5 6) 0))))
2440 (pass-if (equal-values? '((4) (5 6)) (lambda () (split-at! (list 4 5 6) 1))))
2441 (pass-if (equal-values? '((4 5) (6)) (lambda () (split-at! (list 4 5 6) 2))))
2442 (pass-if (equal-values? '((4 5 6) ()) (lambda () (split-at! (list 4 5 6) 3))))
2443 (pass-if-exception "(4 5 6) 4" exception:wrong-type-arg
2444 (split-at! (list 4 5 6) 4)))
2445
2446 ;;
2447 ;; span
2448 ;;
2449
2450 (with-test-prefix "span"
2451
2452 (define (test-span lst want-v1 want-v2)
2453 (call-with-values
2454 (lambda ()
2455 (span positive? lst))
2456 (lambda (got-v1 got-v2)
2457 (and (equal? got-v1 want-v1)
2458 (equal? got-v2 want-v2)))))
2459
2460 (pass-if "empty"
2461 (test-span '() '() '()))
2462
2463 (pass-if "y"
2464 (test-span '(1) '(1) '()))
2465
2466 (pass-if "n"
2467 (test-span '(-1) '() '(-1)))
2468
2469 (pass-if "yy"
2470 (test-span '(1 2) '(1 2) '()))
2471
2472 (pass-if "ny"
2473 (test-span '(-1 1) '() '(-1 1)))
2474
2475 (pass-if "yn"
2476 (test-span '(1 -1) '(1) '(-1)))
2477
2478 (pass-if "nn"
2479 (test-span '(-1 -2) '() '(-1 -2)))
2480
2481 (pass-if "yyy"
2482 (test-span '(1 2 3) '(1 2 3) '()))
2483
2484 (pass-if "nyy"
2485 (test-span '(-1 1 2) '() '(-1 1 2)))
2486
2487 (pass-if "yny"
2488 (test-span '(1 -1 2) '(1) '(-1 2)))
2489
2490 (pass-if "nny"
2491 (test-span '(-1 -2 1) '() '(-1 -2 1)))
2492
2493 (pass-if "yyn"
2494 (test-span '(1 2 -1) '(1 2) '(-1)))
2495
2496 (pass-if "nyn"
2497 (test-span '(-1 1 -2) '() '(-1 1 -2)))
2498
2499 (pass-if "ynn"
2500 (test-span '(1 -1 -2) '(1) '(-1 -2)))
2501
2502 (pass-if "nnn"
2503 (test-span '(-1 -2 -3) '() '(-1 -2 -3))))
2504
2505 ;;
2506 ;; span!
2507 ;;
2508
2509 (with-test-prefix "span!"
2510
2511 (define (test-span! lst want-v1 want-v2)
2512 (call-with-values
2513 (lambda ()
2514 (span! positive? lst))
2515 (lambda (got-v1 got-v2)
2516 (and (equal? got-v1 want-v1)
2517 (equal? got-v2 want-v2)))))
2518
2519 (pass-if "empty"
2520 (test-span! '() '() '()))
2521
2522 (pass-if "y"
2523 (test-span! (list 1) '(1) '()))
2524
2525 (pass-if "n"
2526 (test-span! (list -1) '() '(-1)))
2527
2528 (pass-if "yy"
2529 (test-span! (list 1 2) '(1 2) '()))
2530
2531 (pass-if "ny"
2532 (test-span! (list -1 1) '() '(-1 1)))
2533
2534 (pass-if "yn"
2535 (test-span! (list 1 -1) '(1) '(-1)))
2536
2537 (pass-if "nn"
2538 (test-span! (list -1 -2) '() '(-1 -2)))
2539
2540 (pass-if "yyy"
2541 (test-span! (list 1 2 3) '(1 2 3) '()))
2542
2543 (pass-if "nyy"
2544 (test-span! (list -1 1 2) '() '(-1 1 2)))
2545
2546 (pass-if "yny"
2547 (test-span! (list 1 -1 2) '(1) '(-1 2)))
2548
2549 (pass-if "nny"
2550 (test-span! (list -1 -2 1) '() '(-1 -2 1)))
2551
2552 (pass-if "yyn"
2553 (test-span! (list 1 2 -1) '(1 2) '(-1)))
2554
2555 (pass-if "nyn"
2556 (test-span! (list -1 1 -2) '() '(-1 1 -2)))
2557
2558 (pass-if "ynn"
2559 (test-span! (list 1 -1 -2) '(1) '(-1 -2)))
2560
2561 (pass-if "nnn"
2562 (test-span! (list -1 -2 -3) '() '(-1 -2 -3))))
2563
2564 ;;
2565 ;; take!
2566 ;;
2567
2568 (with-test-prefix "take!"
2569
2570 (pass-if-exception "() -1" exception:out-of-range
2571 (take! '() -1))
2572 (pass-if (equal? '() (take! '() 0)))
2573 (pass-if-exception "() 1" exception:wrong-type-arg
2574 (take! '() 1))
2575
2576 (pass-if-exception "(1) -1" exception:out-of-range
2577 (take! '(1) -1))
2578 (pass-if (equal? '() (take! '(1) 0)))
2579 (pass-if (equal? '(1) (take! '(1) 1)))
2580 (pass-if-exception "(1) 2" exception:wrong-type-arg
2581 (take! '(1) 2))
2582
2583 (pass-if-exception "(4 5) -1" exception:out-of-range
2584 (take! '(4 5) -1))
2585 (pass-if (equal? '() (take! '(4 5) 0)))
2586 (pass-if (equal? '(4) (take! '(4 5) 1)))
2587 (pass-if (equal? '(4 5) (take! '(4 5) 2)))
2588 (pass-if-exception "(4 5) 3" exception:wrong-type-arg
2589 (take! '(4 5) 3))
2590
2591 (pass-if-exception "(4 5 6) -1" exception:out-of-range
2592 (take! '(4 5 6) -1))
2593 (pass-if (equal? '() (take! '(4 5 6) 0)))
2594 (pass-if (equal? '(4) (take! '(4 5 6) 1)))
2595 (pass-if (equal? '(4 5) (take! '(4 5 6) 2)))
2596 (pass-if (equal? '(4 5 6) (take! '(4 5 6) 3)))
2597 (pass-if-exception "(4 5 6) 4" exception:wrong-type-arg
2598 (take! '(4 5 6) 4)))
2599
2600
2601 ;;
2602 ;; take-right
2603 ;;
2604
2605 (with-test-prefix "take-right"
2606
2607 (pass-if-exception "() -1" exception:out-of-range
2608 (take-right '() -1))
2609 (pass-if (equal? '() (take-right '() 0)))
2610 (pass-if-exception "() 1" exception:wrong-type-arg
2611 (take-right '() 1))
2612
2613 (pass-if-exception "(1) -1" exception:out-of-range
2614 (take-right '(1) -1))
2615 (pass-if (equal? '() (take-right '(1) 0)))
2616 (pass-if (equal? '(1) (take-right '(1) 1)))
2617 (pass-if-exception "(1) 2" exception:wrong-type-arg
2618 (take-right '(1) 2))
2619
2620 (pass-if-exception "(4 5) -1" exception:out-of-range
2621 (take-right '(4 5) -1))
2622 (pass-if (equal? '() (take-right '(4 5) 0)))
2623 (pass-if (equal? '(5) (take-right '(4 5) 1)))
2624 (pass-if (equal? '(4 5) (take-right '(4 5) 2)))
2625 (pass-if-exception "(4 5) 3" exception:wrong-type-arg
2626 (take-right '(4 5) 3))
2627
2628 (pass-if-exception "(4 5 6) -1" exception:out-of-range
2629 (take-right '(4 5 6) -1))
2630 (pass-if (equal? '() (take-right '(4 5 6) 0)))
2631 (pass-if (equal? '(6) (take-right '(4 5 6) 1)))
2632 (pass-if (equal? '(5 6) (take-right '(4 5 6) 2)))
2633 (pass-if (equal? '(4 5 6) (take-right '(4 5 6) 3)))
2634 (pass-if-exception "(4 5 6) 4" exception:wrong-type-arg
2635 (take-right '(4 5 6) 4))
2636
2637 (pass-if "(a b . c) 0"
2638 (equal? (take-right '(a b . c) 0) 'c))
2639 (pass-if "(a b . c) 1"
2640 (equal? (take-right '(a b . c) 1) '(b . c))))
2641
2642 ;;
2643 ;; tenth
2644 ;;
2645
2646 (with-test-prefix "tenth"
2647 (pass-if-exception "() -1" exception:wrong-type-arg
2648 (tenth '(a b c d e f g h i)))
2649 (pass-if (eq? 'j (tenth '(a b c d e f g h i j))))
2650 (pass-if (eq? 'j (tenth '(a b c d e f g h i j k)))))
2651
2652 ;;
2653 ;; xcons
2654 ;;
2655
2656 (with-test-prefix "xcons"
2657 (pass-if (equal? '(y . x) (xcons 'x 'y))))