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