Commit | Line | Data |
---|---|---|
91e7199f KR |
1 | ;;;; srfi-1.test --- Test suite for Guile's SRFI-1 functions. -*- scheme -*- |
2 | ;;;; | |
3 | ;;;; Copyright 2003 Free Software Foundation, Inc. | |
4 | ;;;; | |
5 | ;;;; This program is free software; you can redistribute it and/or modify | |
6 | ;;;; it under the terms of the GNU General Public License as published by | |
7 | ;;;; the Free Software Foundation; either version 2, or (at your option) | |
8 | ;;;; any later version. | |
9 | ;;;; | |
10 | ;;;; This program 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 | |
13 | ;;;; GNU General Public License for more details. | |
14 | ;;;; | |
15 | ;;;; You should have received a copy of the GNU General Public License | |
16 | ;;;; along with this software; see the file COPYING. If not, write to | |
17 | ;;;; the Free Software Foundation, Inc., 59 Temple Place, Suite 330, | |
18 | ;;;; Boston, MA 02111-1307 USA | |
19 | ||
20 | (use-modules (srfi srfi-1) | |
21 | (test-suite lib)) | |
22 | ||
72f1b979 KR |
23 | (define (ref-delete x lst . proc) |
24 | "Reference implemenation of srfi-1 `delete'." | |
25 | (set! proc (if (null? proc) equal? (car proc))) | |
26 | (do ((ret '()) | |
27 | (lst lst (cdr lst))) | |
28 | ((null? lst) | |
29 | (reverse! ret)) | |
30 | (if (not (proc x (car lst))) | |
31 | (set! ret (cons (car lst) ret))))) | |
32 | ||
33 | (define (ref-delete-duplicates lst . proc) | |
34 | "Reference implemenation of srfi-1 `delete-duplicates'." | |
35 | (set! proc (if (null? proc) equal? (car proc))) | |
36 | (if (null? lst) | |
37 | '() | |
38 | (do ((keep '())) | |
39 | ((null? lst) | |
40 | (reverse! keep)) | |
41 | (let ((elem (car lst))) | |
42 | (set! keep (cons elem keep)) | |
43 | (set! lst (ref-delete elem lst proc)))))) | |
44 | ||
91e7199f | 45 | |
bbd1b480 KR |
46 | ;; |
47 | ;; append-map | |
48 | ;; | |
49 | ||
50 | (with-test-prefix "append-map" | |
51 | ||
52 | (with-test-prefix "one list" | |
53 | ||
54 | (pass-if "()" | |
55 | (equal? '() (append-map noop '(())))) | |
56 | ||
57 | (pass-if "(1)" | |
58 | (equal? '(1) (append-map noop '((1))))) | |
59 | ||
60 | (pass-if "(1 2)" | |
61 | (equal? '(1 2) (append-map noop '((1 2))))) | |
62 | ||
63 | (pass-if "() ()" | |
64 | (equal? '() (append-map noop '(() ())))) | |
65 | ||
66 | (pass-if "() (1)" | |
67 | (equal? '(1) (append-map noop '(() (1))))) | |
68 | ||
69 | (pass-if "() (1 2)" | |
70 | (equal? '(1 2) (append-map noop '(() (1 2))))) | |
71 | ||
72 | (pass-if "(1) (2)" | |
73 | (equal? '(1 2) (append-map noop '((1) (2))))) | |
74 | ||
75 | (pass-if "(1 2) ()" | |
76 | (equal? '(1 2) (append-map noop '(() (1 2)))))) | |
77 | ||
78 | (with-test-prefix "two lists" | |
79 | ||
80 | (pass-if "() / 9" | |
81 | (equal? '() (append-map noop '(()) '(9)))) | |
82 | ||
83 | (pass-if "(1) / 9" | |
84 | (equal? '(1) (append-map noop '((1)) '(9)))) | |
85 | ||
86 | (pass-if "() () / 9 9" | |
87 | (equal? '() (append-map noop '(() ()) '(9 9)))) | |
88 | ||
89 | (pass-if "(1) (2) / 9" | |
90 | (equal? '(1) (append-map noop '((1) (2)) '(9)))) | |
91 | ||
92 | (pass-if "(1) (2) / 9 9" | |
93 | (equal? '(1 2) (append-map noop '((1) (2)) '(9 9)))))) | |
94 | ||
c6e9db20 KR |
95 | ;; |
96 | ;; concatenate and concatenate! | |
97 | ;; | |
98 | ||
99 | (let () | |
100 | (define (common-tests concatenate-proc unmodified?) | |
101 | (define (try lstlst want) | |
102 | (let ((lstlst-copy (copy-tree lstlst)) | |
103 | (got (concatenate-proc lstlst))) | |
104 | (if unmodified? | |
105 | (if (not (equal? lstlst lstlst-copy)) | |
106 | (error "input lists modified"))) | |
107 | (equal? got want))) | |
108 | ||
109 | (pass-if-exception "too few args" exception:wrong-num-args | |
110 | (concatenate-proc)) | |
111 | ||
112 | (pass-if-exception "too many args" exception:wrong-num-args | |
113 | (concatenate-proc '() '())) | |
114 | ||
115 | (pass-if "no lists" | |
116 | (try '() '())) | |
117 | ||
118 | (pass-if (try '((1)) '(1))) | |
119 | (pass-if (try '((1 2)) '(1 2))) | |
120 | (pass-if (try '(() (1)) '(1))) | |
121 | (pass-if (try '(() () (1)) '(1))) | |
122 | ||
123 | (pass-if (try '((1) (2)) '(1 2))) | |
124 | (pass-if (try '(() (1 2)) '(1 2))) | |
125 | ||
126 | (pass-if (try '((1) 2) '(1 . 2))) | |
127 | (pass-if (try '((1) (2) 3) '(1 2 . 3))) | |
128 | (pass-if (try '((1) (2) (3 . 4)) '(1 2 3 . 4))) | |
129 | ) | |
130 | ||
131 | (with-test-prefix "concatenate" | |
132 | (common-tests concatenate #t)) | |
133 | ||
134 | (with-test-prefix "concatenate!" | |
135 | (common-tests concatenate! #f))) | |
136 | ||
f3903293 KR |
137 | ;; |
138 | ;; count | |
139 | ;; | |
140 | ||
141 | (with-test-prefix "count" | |
142 | (pass-if-exception "no args" exception:wrong-num-args | |
143 | (count)) | |
144 | ||
145 | (pass-if-exception "one arg" exception:wrong-num-args | |
146 | (count noop)) | |
147 | ||
148 | (with-test-prefix "one list" | |
149 | (define (or1 x) | |
150 | x) | |
151 | ||
152 | (pass-if "empty list" (= 0 (count or1 '()))) | |
153 | ||
154 | (pass-if-exception "pred arg count 0" exception:wrong-type-arg | |
155 | (count (lambda () x) '(1 2 3))) | |
156 | (pass-if-exception "pred arg count 2" exception:wrong-type-arg | |
157 | (count (lambda (x y) x) '(1 2 3))) | |
158 | ||
159 | (pass-if-exception "improper 1" exception:wrong-type-arg | |
160 | (count or1 1)) | |
161 | (pass-if-exception "improper 2" exception:wrong-type-arg | |
162 | (count or1 '(1 . 2))) | |
163 | (pass-if-exception "improper 3" exception:wrong-type-arg | |
164 | (count or1 '(1 2 . 3))) | |
165 | ||
166 | (pass-if (= 0 (count or1 '(#f)))) | |
167 | (pass-if (= 1 (count or1 '(#t)))) | |
168 | ||
169 | (pass-if (= 0 (count or1 '(#f #f)))) | |
170 | (pass-if (= 1 (count or1 '(#f #t)))) | |
171 | (pass-if (= 1 (count or1 '(#t #f)))) | |
172 | (pass-if (= 2 (count or1 '(#t #t)))) | |
173 | ||
174 | (pass-if (= 0 (count or1 '(#f #f #f)))) | |
175 | (pass-if (= 1 (count or1 '(#f #f #t)))) | |
176 | (pass-if (= 1 (count or1 '(#t #f #f)))) | |
177 | (pass-if (= 2 (count or1 '(#t #f #t)))) | |
178 | (pass-if (= 3 (count or1 '(#t #t #t))))) | |
179 | ||
180 | (with-test-prefix "two lists" | |
181 | (define (or2 x y) | |
182 | (or x y)) | |
183 | ||
184 | (pass-if "arg order" | |
185 | (= 1 (count (lambda (x y) | |
186 | (and (= 1 x) | |
187 | (= 2 y))) | |
188 | '(1) '(2)))) | |
189 | ||
190 | (pass-if "empty lists" (= 0 (count or2 '() '()))) | |
191 | ||
192 | (pass-if-exception "pred arg count 0" exception:wrong-type-arg | |
193 | (count (lambda () #t) '(1 2 3) '(1 2 3))) | |
194 | (pass-if-exception "pred arg count 1" exception:wrong-type-arg | |
195 | (count (lambda (x) x) '(1 2 3) '(1 2 3))) | |
196 | (pass-if-exception "pred arg count 3" exception:wrong-type-arg | |
197 | (count (lambda (x y z) x) '(1 2 3) '(1 2 3))) | |
198 | ||
199 | (pass-if-exception "improper first 1" exception:wrong-type-arg | |
200 | (count or2 1 '(1 2 3))) | |
201 | (pass-if-exception "improper first 2" exception:wrong-type-arg | |
202 | (count or2 '(1 . 2) '(1 2 3))) | |
203 | (pass-if-exception "improper first 3" exception:wrong-type-arg | |
204 | (count or2 '(1 2 . 3) '(1 2 3))) | |
205 | ||
206 | (pass-if-exception "improper second 1" exception:wrong-type-arg | |
207 | (count or2 '(1 2 3) 1)) | |
208 | (pass-if-exception "improper second 2" exception:wrong-type-arg | |
209 | (count or2 '(1 2 3) '(1 . 2))) | |
210 | (pass-if-exception "improper second 3" exception:wrong-type-arg | |
211 | (count or2 '(1 2 3) '(1 2 . 3))) | |
212 | ||
213 | (pass-if (= 0 (count or2 '(#f) '(#f)))) | |
214 | (pass-if (= 1 (count or2 '(#t) '(#f)))) | |
215 | (pass-if (= 1 (count or2 '(#f) '(#t)))) | |
216 | ||
217 | (pass-if (= 0 (count or2 '(#f #f) '(#f #f)))) | |
218 | (pass-if (= 1 (count or2 '(#t #f) '(#t #f)))) | |
219 | (pass-if (= 2 (count or2 '(#t #t) '(#f #f)))) | |
220 | (pass-if (= 2 (count or2 '(#t #f) '(#f #t)))) | |
221 | ||
222 | (with-test-prefix "stop shortest" | |
223 | (pass-if (= 2 (count or2 '(#t #f #t) '(#f #t)))) | |
224 | (pass-if (= 2 (count or2 '(#t #f #t #t) '(#f #t)))) | |
225 | (pass-if (= 2 (count or2 '(#t #f) '(#f #t #t)))) | |
226 | (pass-if (= 2 (count or2 '(#t #f) '(#f #t #t #t)))))) | |
227 | ||
228 | (with-test-prefix "three lists" | |
229 | (define (or3 x y z) | |
230 | (or x y z)) | |
231 | ||
232 | (pass-if "arg order" | |
233 | (= 1 (count (lambda (x y z) | |
234 | (and (= 1 x) | |
235 | (= 2 y) | |
236 | (= 3 z))) | |
237 | '(1) '(2) '(3)))) | |
238 | ||
239 | (pass-if "empty lists" (= 0 (count or3 '() '() '()))) | |
240 | ||
241 | ;; currently bad pred argument gives wrong-num-args when 3 or more | |
242 | ;; lists, as opposed to wrong-type-arg for 1 or 2 lists | |
243 | (pass-if-exception "pred arg count 0" exception:wrong-num-args | |
244 | (count (lambda () #t) '(1 2 3) '(1 2 3) '(1 2 3))) | |
245 | (pass-if-exception "pred arg count 2" exception:wrong-num-args | |
246 | (count (lambda (x y) x) '(1 2 3) '(1 2 3)'(1 2 3) )) | |
247 | (pass-if-exception "pred arg count 4" exception:wrong-num-args | |
248 | (count (lambda (w x y z) x) '(1 2 3) '(1 2 3) '(1 2 3))) | |
249 | ||
250 | (pass-if-exception "improper first 1" exception:wrong-type-arg | |
251 | (count or3 1 '(1 2 3) '(1 2 3))) | |
252 | (pass-if-exception "improper first 2" exception:wrong-type-arg | |
253 | (count or3 '(1 . 2) '(1 2 3) '(1 2 3))) | |
254 | (pass-if-exception "improper first 3" exception:wrong-type-arg | |
255 | (count or3 '(1 2 . 3) '(1 2 3) '(1 2 3))) | |
256 | ||
257 | (pass-if-exception "improper second 1" exception:wrong-type-arg | |
258 | (count or3 '(1 2 3) 1 '(1 2 3))) | |
259 | (pass-if-exception "improper second 2" exception:wrong-type-arg | |
260 | (count or3 '(1 2 3) '(1 . 2) '(1 2 3))) | |
261 | (pass-if-exception "improper second 3" exception:wrong-type-arg | |
262 | (count or3 '(1 2 3) '(1 2 . 3) '(1 2 3))) | |
263 | ||
264 | (pass-if-exception "improper third 1" exception:wrong-type-arg | |
265 | (count or3 '(1 2 3) '(1 2 3) 1)) | |
266 | (pass-if-exception "improper third 2" exception:wrong-type-arg | |
267 | (count or3 '(1 2 3) '(1 2 3) '(1 . 2))) | |
268 | (pass-if-exception "improper third 3" exception:wrong-type-arg | |
269 | (count or3 '(1 2 3) '(1 2 3) '(1 2 . 3))) | |
270 | ||
271 | (pass-if (= 0 (count or3 '(#f) '(#f) '(#f)))) | |
272 | (pass-if (= 1 (count or3 '(#t) '(#f) '(#f)))) | |
273 | (pass-if (= 1 (count or3 '(#f) '(#t) '(#f)))) | |
274 | (pass-if (= 1 (count or3 '(#f) '(#f) '(#t)))) | |
275 | ||
276 | (pass-if (= 0 (count or3 '(#f #f) '(#f #f) '(#f #f)))) | |
277 | ||
278 | (pass-if (= 1 (count or3 '(#t #f) '(#f #f) '(#f #f)))) | |
279 | (pass-if (= 1 (count or3 '(#f #t) '(#f #f) '(#f #f)))) | |
280 | (pass-if (= 1 (count or3 '(#f #f) '(#t #f) '(#f #f)))) | |
281 | (pass-if (= 1 (count or3 '(#f #f) '(#f #t) '(#f #f)))) | |
282 | (pass-if (= 1 (count or3 '(#f #f) '(#f #f) '(#t #f)))) | |
283 | (pass-if (= 1 (count or3 '(#f #f) '(#f #f) '(#f #t)))) | |
284 | ||
285 | (pass-if (= 2 (count or3 '(#t #t) '(#f #f) '(#f #f)))) | |
286 | (pass-if (= 2 (count or3 '(#f #f) '(#t #t) '(#f #f)))) | |
287 | (pass-if (= 2 (count or3 '(#f #f) '(#f #f) '(#t #t)))) | |
288 | (pass-if (= 2 (count or3 '(#f #f) '(#t #f) '(#f #t)))) | |
289 | ||
290 | (with-test-prefix "stop shortest" | |
291 | (pass-if (= 0 (count or3 '() '(#t #t #t) '(#t #t)))) | |
292 | (pass-if (= 0 (count or3 '(#t #t #t) '() '(#t #t)))) | |
293 | (pass-if (= 0 (count or3 '(#t #t #t) '(#t #t) '()))) | |
294 | ||
295 | (pass-if (= 1 (count or3 '(#t) '(#t #t #t) '(#t #t)))) | |
296 | (pass-if (= 1 (count or3 '(#t #t #t) '(#t) '(#t #t)))) | |
297 | (pass-if (= 1 (count or3 '(#t #t #t) '(#t #t) '(#t))))))) | |
c6e9db20 | 298 | |
8ec84fe5 KR |
299 | ;; |
300 | ;; delete and delete! | |
301 | ;; | |
302 | ||
303 | (let () | |
72f1b979 KR |
304 | ;; Call (PROC lst) for all lists of length up to 6, with all combinations |
305 | ;; of elements to be retained or deleted. Elements to retain are numbers, | |
306 | ;; 0 upwards. Elements to be deleted are #f. | |
307 | (define (test-lists proc) | |
308 | (do ((n 0 (1+ n))) | |
309 | ((>= n 6)) | |
310 | (do ((limit (ash 1 n)) | |
311 | (i 0 (1+ i))) | |
312 | ((>= i limit)) | |
313 | (let ((lst '())) | |
314 | (do ((bit 0 (1+ bit))) | |
315 | ((>= bit n)) | |
316 | (set! lst (cons (if (logbit? bit i) bit #f) lst))) | |
317 | (proc lst))))) | |
318 | ||
8ec84fe5 | 319 | (define (common-tests delete-proc) |
72f1b979 KR |
320 | (pass-if-exception "too few args" exception:wrong-num-args |
321 | (delete-proc 0)) | |
322 | ||
323 | (pass-if-exception "too many args" exception:wrong-num-args | |
324 | (delete-proc 0 '() equal? 99)) | |
325 | ||
326 | (pass-if "empty" | |
327 | (eq? '() (delete-proc 0 '()))) | |
328 | ||
329 | (pass-if "equal? (the default)" | |
330 | (equal? '((1) (3)) | |
331 | (delete-proc '(2) '((1) (2) (3))))) | |
332 | ||
333 | (pass-if "eq?" | |
334 | (equal? '((1) (2) (3)) | |
335 | (delete-proc '(2) '((1) (2) (3)) eq?))) | |
336 | ||
8ec84fe5 KR |
337 | (pass-if "called arg order" |
338 | (equal? '(1 2 3) | |
339 | (delete-proc 3 '(1 2 3 4 5) <)))) | |
340 | ||
341 | (with-test-prefix "delete" | |
72f1b979 | 342 | (common-tests delete) |
8ec84fe5 | 343 | |
72f1b979 KR |
344 | (test-lists |
345 | (lambda (lst) | |
346 | (let ((lst-copy (list-copy lst))) | |
347 | (with-test-prefix lst-copy | |
348 | (pass-if "result" | |
349 | (equal? (delete #f lst) | |
350 | (ref-delete #f lst))) | |
351 | (pass-if "non-destructive" | |
352 | (equal? lst-copy lst))))))) | |
353 | ||
8ec84fe5 | 354 | (with-test-prefix "delete!" |
72f1b979 KR |
355 | (common-tests delete!) |
356 | ||
357 | (test-lists | |
358 | (lambda (lst) | |
359 | (pass-if lst | |
360 | (equal? (delete! #f lst) | |
361 | (ref-delete #f lst))))))) | |
362 | ||
363 | ;; | |
364 | ;; delete-duplicates and delete-duplicates! | |
365 | ;; | |
366 | ||
367 | (let () | |
368 | ;; Call (PROC lst) for all lists of length 1 <= n <= 4, with all | |
369 | ;; combinations of numbers 1 to n in the elements | |
370 | (define (test-lists proc) | |
371 | (do ((n 1 (1+ n))) | |
372 | ((> n 4)) | |
373 | (do ((limit (integer-expt n n)) | |
374 | (i 0 (1+ i))) | |
375 | ((>= i limit)) | |
376 | (let ((lst '())) | |
377 | (do ((j 0 (1+ j)) | |
378 | (rem i (quotient rem n))) | |
379 | ((>= j n)) | |
380 | (set! lst (cons (remainder rem n) lst))) | |
381 | (proc lst))))) | |
382 | ||
383 | (define (common-tests delete-duplicates-proc) | |
384 | (pass-if-exception "too few args" exception:wrong-num-args | |
385 | (delete-duplicates-proc)) | |
386 | ||
387 | (pass-if-exception "too many args" exception:wrong-num-args | |
388 | (delete-duplicates-proc '() equal? 99)) | |
389 | ||
390 | (pass-if "empty" | |
391 | (eq? '() (delete-duplicates-proc '()))) | |
392 | ||
393 | (pass-if "equal? (the default)" | |
394 | (equal? '((2)) | |
395 | (delete-duplicates-proc '((2) (2) (2))))) | |
396 | ||
397 | (pass-if "eq?" | |
398 | (equal? '((2) (2) (2)) | |
399 | (delete-duplicates-proc '((2) (2) (2)) eq?))) | |
400 | ||
401 | (pass-if "called arg order" | |
402 | (let ((ok #t)) | |
403 | (delete-duplicates-proc '(1 2 3 4 5) | |
404 | (lambda (x y) | |
405 | (if (> x y) | |
406 | (set! ok #f)) | |
407 | #f)) | |
408 | ok))) | |
409 | ||
410 | (with-test-prefix "delete-duplicates" | |
411 | (common-tests delete-duplicates) | |
412 | ||
413 | (test-lists | |
414 | (lambda (lst) | |
415 | (let ((lst-copy (list-copy lst))) | |
416 | (with-test-prefix lst-copy | |
417 | (pass-if "result" | |
418 | (equal? (delete-duplicates lst) | |
419 | (ref-delete-duplicates lst))) | |
420 | (pass-if "non-destructive" | |
421 | (equal? lst-copy lst))))))) | |
422 | ||
423 | (with-test-prefix "delete-duplicates!" | |
424 | (common-tests delete-duplicates!) | |
425 | ||
426 | (test-lists | |
427 | (lambda (lst) | |
428 | (pass-if lst | |
429 | (equal? (delete-duplicates! lst) | |
430 | (ref-delete-duplicates lst))))))) | |
8ec84fe5 | 431 | |
91e7199f KR |
432 | ;; |
433 | ;; drop | |
434 | ;; | |
435 | ||
436 | (with-test-prefix "drop" | |
437 | ||
438 | (pass-if "'() 0" | |
439 | (null? (drop '() 0))) | |
440 | ||
441 | (pass-if "'(a) 0" | |
442 | (let ((lst '(a))) | |
443 | (eq? lst | |
444 | (drop lst 0)))) | |
445 | ||
446 | (pass-if "'(a b) 0" | |
447 | (let ((lst '(a b))) | |
448 | (eq? lst | |
449 | (drop lst 0)))) | |
450 | ||
451 | (pass-if "'(a) 1" | |
452 | (let ((lst '(a))) | |
453 | (eq? (cdr lst) | |
454 | (drop lst 1)))) | |
455 | ||
456 | (pass-if "'(a b) 1" | |
457 | (let ((lst '(a b))) | |
458 | (eq? (cdr lst) | |
459 | (drop lst 1)))) | |
460 | ||
461 | (pass-if "'(a b) 2" | |
462 | (let ((lst '(a b))) | |
463 | (eq? (cddr lst) | |
464 | (drop lst 2)))) | |
465 | ||
466 | (pass-if "'(a b c) 1" | |
467 | (let ((lst '(a b c))) | |
468 | (eq? (cddr lst) | |
469 | (drop lst 2)))) | |
470 | ||
471 | (pass-if "circular '(a) 0" | |
472 | (let ((lst (circular-list 'a))) | |
473 | (eq? lst | |
474 | (drop lst 0)))) | |
475 | ||
476 | (pass-if "circular '(a) 1" | |
477 | (let ((lst (circular-list 'a))) | |
478 | (eq? lst | |
479 | (drop lst 1)))) | |
480 | ||
481 | (pass-if "circular '(a) 2" | |
482 | (let ((lst (circular-list 'a))) | |
483 | (eq? lst | |
484 | (drop lst 1)))) | |
485 | ||
486 | (pass-if "circular '(a b) 1" | |
487 | (let ((lst (circular-list 'a))) | |
488 | (eq? (cdr lst) | |
489 | (drop lst 0)))) | |
490 | ||
491 | (pass-if "circular '(a b) 2" | |
492 | (let ((lst (circular-list 'a))) | |
493 | (eq? lst | |
494 | (drop lst 1)))) | |
495 | ||
496 | (pass-if "circular '(a b) 5" | |
497 | (let ((lst (circular-list 'a))) | |
498 | (eq? (cdr lst) | |
499 | (drop lst 5)))) | |
500 | ||
501 | (pass-if "'(a . b) 1" | |
502 | (eq? 'b | |
503 | (drop '(a . b) 1))) | |
504 | ||
505 | (pass-if "'(a b . c) 1" | |
506 | (equal? 'c | |
507 | (drop '(a b . c) 2)))) | |
508 | ||
a52ef9e4 KR |
509 | ;; |
510 | ;; filter-map | |
511 | ;; | |
512 | ||
513 | (with-test-prefix "filter-map" | |
514 | ||
515 | (with-test-prefix "one list" | |
516 | (pass-if "(1)" | |
517 | (equal? '(1) (filter-map noop '(1)))) | |
518 | ||
519 | (pass-if "(#f)" | |
520 | (equal? '() (filter-map noop '(#f)))) | |
521 | ||
522 | (pass-if "(1 2)" | |
523 | (equal? '(1 2) (filter-map noop '(1 2)))) | |
524 | ||
525 | (pass-if "(#f 2)" | |
526 | (equal? '(2) (filter-map noop '(#f 2)))) | |
527 | ||
528 | (pass-if "(#f #f)" | |
529 | (equal? '() (filter-map noop '(#f #f)))) | |
530 | ||
531 | (pass-if "(1 2 3)" | |
532 | (equal? '(1 2 3) (filter-map noop '(1 2 3)))) | |
533 | ||
534 | (pass-if "(#f 2 3)" | |
535 | (equal? '(2 3) (filter-map noop '(#f 2 3)))) | |
536 | ||
537 | (pass-if "(1 #f 3)" | |
538 | (equal? '(1 3) (filter-map noop '(1 #f 3)))) | |
539 | ||
540 | (pass-if "(1 2 #f)" | |
541 | (equal? '(1 2) (filter-map noop '(1 2 #f))))) | |
542 | ||
543 | (with-test-prefix "two lists" | |
544 | (pass-if "(1 2 3) (4 5 6)" | |
545 | (equal? '(1 2 3) (filter-map noop '(1 2 3) '(4 5 6)))) | |
546 | ||
547 | (pass-if "(#f 2 3) (4 5)" | |
548 | (equal? '(2) (filter-map noop '(#f 2 3) '(4 5)))) | |
549 | ||
550 | (pass-if "(4 #f) (1 2 3)" | |
551 | (equal? '(4) (filter-map noop '(4 #f) '(1 2 3)))))) | |
552 | ||
15d36a34 KR |
553 | ;; |
554 | ;; length+ | |
555 | ;; | |
556 | ||
557 | (with-test-prefix "length+" | |
558 | (pass-if-exception "too few args" exception:wrong-num-args | |
559 | (length+)) | |
560 | (pass-if-exception "too many args" exception:wrong-num-args | |
561 | (length+ 123 456)) | |
562 | (pass-if (= 0 (length+ '()))) | |
563 | (pass-if (= 1 (length+ '(x)))) | |
564 | (pass-if (= 2 (length+ '(x y)))) | |
565 | (pass-if (= 3 (length+ '(x y z)))) | |
566 | (pass-if (not (length+ (circular-list 1)))) | |
567 | (pass-if (not (length+ (circular-list 1 2)))) | |
568 | (pass-if (not (length+ (circular-list 1 2 3))))) | |
569 | ||
b052db69 KR |
570 | ;; |
571 | ;; list-copy | |
572 | ;; | |
573 | ||
574 | (with-test-prefix "list-copy" | |
b052db69 KR |
575 | (pass-if (equal? '() (list-copy '()))) |
576 | (pass-if (equal? '(1 2) (list-copy '(1 2)))) | |
577 | (pass-if (equal? '(1 2 3) (list-copy '(1 2 3)))) | |
578 | (pass-if (equal? '(1 2 3 4) (list-copy '(1 2 3 4)))) | |
579 | (pass-if (equal? '(1 2 3 4 5) (list-copy '(1 2 3 4 5)))) | |
580 | ||
581 | ;; improper lists can be copied | |
582 | (pass-if (equal? 1 (list-copy 1))) | |
583 | (pass-if (equal? '(1 . 2) (list-copy '(1 . 2)))) | |
584 | (pass-if (equal? '(1 2 . 3) (list-copy '(1 2 . 3)))) | |
585 | (pass-if (equal? '(1 2 3 . 4) (list-copy '(1 2 3 . 4)))) | |
586 | (pass-if (equal? '(1 2 3 4 . 5) (list-copy '(1 2 3 4 . 5))))) | |
587 | ||
91e7199f KR |
588 | ;; |
589 | ;; take | |
590 | ;; | |
591 | ||
592 | (with-test-prefix "take" | |
593 | ||
594 | (pass-if "'() 0" | |
595 | (null? (take '() 0))) | |
596 | ||
597 | (pass-if "'(a) 0" | |
598 | (null? (take '(a) 0))) | |
599 | ||
600 | (pass-if "'(a b) 0" | |
601 | (null? (take '() 0))) | |
602 | ||
603 | (pass-if "'(a b c) 0" | |
604 | (null? (take '() 0))) | |
605 | ||
606 | (pass-if "'(a) 1" | |
607 | (let* ((lst '(a)) | |
608 | (got (take lst 1))) | |
609 | (and (equal? '(a) got) | |
610 | (not (eq? lst got))))) | |
611 | ||
612 | (pass-if "'(a b) 1" | |
613 | (equal? '(a) | |
614 | (take '(a b) 1))) | |
615 | ||
616 | (pass-if "'(a b c) 1" | |
617 | (equal? '(a) | |
618 | (take '(a b c) 1))) | |
619 | ||
620 | (pass-if "'(a b) 2" | |
621 | (let* ((lst '(a b)) | |
622 | (got (take lst 2))) | |
623 | (and (equal? '(a b) got) | |
624 | (not (eq? lst got))))) | |
625 | ||
626 | (pass-if "'(a b c) 2" | |
627 | (equal? '(a b) | |
628 | (take '(a b c) 2))) | |
629 | ||
630 | (pass-if "circular '(a) 0" | |
631 | (equal? '() | |
632 | (take (circular-list 'a) 0))) | |
633 | ||
634 | (pass-if "circular '(a) 1" | |
635 | (equal? '(a) | |
636 | (take (circular-list 'a) 1))) | |
637 | ||
638 | (pass-if "circular '(a) 2" | |
639 | (equal? '(a a) | |
640 | (take (circular-list 'a) 2))) | |
641 | ||
642 | (pass-if "circular '(a b) 5" | |
643 | (equal? '(a b a b a) | |
644 | (take (circular-list 'a 'b) 5))) | |
645 | ||
646 | (pass-if "'(a . b) 1" | |
647 | (equal? '(a) | |
648 | (take '(a . b) 1))) | |
649 | ||
650 | (pass-if "'(a b . c) 1" | |
651 | (equal? '(a) | |
652 | (take '(a b . c) 1))) | |
653 | ||
654 | (pass-if "'(a b . c) 2" | |
655 | (equal? '(a b) | |
656 | (take '(a b . c) 2)))) | |
9a029e41 KR |
657 | |
658 | ;; | |
659 | ;; partition | |
660 | ;; | |
661 | ||
662 | (define (test-partition pred list kept-good dropped-good) | |
663 | (call-with-values (lambda () | |
664 | (partition pred list)) | |
665 | (lambda (kept dropped) | |
666 | (and (equal? kept kept-good) | |
667 | (equal? dropped dropped-good))))) | |
668 | ||
669 | (with-test-prefix "partition" | |
670 | ||
671 | (pass-if "with dropped tail" | |
672 | (test-partition even? '(1 2 3 4 5 6 7) | |
673 | '(2 4 6) '(1 3 5 7))) | |
674 | ||
675 | (pass-if "with kept tail" | |
676 | (test-partition even? '(1 2 3 4 5 6) | |
677 | '(2 4 6) '(1 3 5))) | |
678 | ||
679 | (pass-if "with everything dropped" | |
680 | (test-partition even? '(1 3 5 7) | |
681 | '() '(1 3 5 7))) | |
682 | ||
683 | (pass-if "with everything kept" | |
684 | (test-partition even? '(2 4 6) | |
685 | '(2 4 6) '())) | |
686 | ||
687 | (pass-if "with empty list" | |
688 | (test-partition even? '() | |
689 | '() '())) | |
690 | ||
691 | (pass-if "with reasonably long list" | |
692 | ;; the old implementation from SRFI-1 reference implementation | |
693 | ;; would signal a stack-overflow for a list of only 500 elements! | |
694 | (call-with-values (lambda () | |
695 | (partition even? | |
696 | (make-list 10000 1))) | |
697 | (lambda (even odd) | |
698 | (and (= (length odd) 10000) | |
699 | (= (length even) 0)))))) | |
700 |