1 ;;;; srfi-1.test --- Test suite for Guile's SRFI-1 functions. -*- scheme -*-
3 ;;;; Copyright 2003 Free Software Foundation, Inc.
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.
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.
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
20 (use-modules (srfi srfi-1)
23 (define (ref-delete x lst . proc)
24 "Reference implemenation of srfi-1 `delete'."
25 (set! proc (if (null? proc) equal? (car proc)))
30 (if (not (proc x (car lst)))
31 (set! ret (cons (car lst) ret)))))
33 (define (ref-delete-duplicates lst . proc)
34 "Reference implemenation of srfi-1 `delete-duplicates'."
35 (set! proc (if (null? proc) equal? (car proc)))
41 (let ((elem (car lst)))
42 (set! keep (cons elem keep))
43 (set! lst (ref-delete elem lst proc))))))
51 ;; Call (PROC lst) for all lists of length up to 6, with all combinations
52 ;; of elements to be retained or deleted. Elements to retain are numbers,
53 ;; 0 upwards. Elements to be deleted are #f.
54 (define (test-lists proc)
57 (do ((limit (ash 1 n))
61 (do ((bit 0 (1+ bit)))
63 (set! lst (cons (if (logbit? bit i) bit #f) lst)))
66 (define (common-tests delete-proc)
67 (pass-if-exception "too few args" exception:wrong-num-args
70 (pass-if-exception "too many args" exception:wrong-num-args
71 (delete-proc 0 '() equal? 99))
74 (eq? '() (delete-proc 0 '())))
76 (pass-if "equal? (the default)"
78 (delete-proc '(2) '((1) (2) (3)))))
81 (equal? '((1) (2) (3))
82 (delete-proc '(2) '((1) (2) (3)) eq?)))
84 (pass-if "called arg order"
86 (delete-proc 3 '(1 2 3 4 5) <))))
88 (with-test-prefix "delete"
93 (let ((lst-copy (list-copy lst)))
94 (with-test-prefix lst-copy
96 (equal? (delete #f lst)
98 (pass-if "non-destructive"
99 (equal? lst-copy lst)))))))
101 (with-test-prefix "delete!"
102 (common-tests delete!)
107 (equal? (delete! #f lst)
108 (ref-delete #f lst)))))))
111 ;; delete-duplicates and delete-duplicates!
115 ;; Call (PROC lst) for all lists of length 1 <= n <= 4, with all
116 ;; combinations of numbers 1 to n in the elements
117 (define (test-lists proc)
120 (do ((limit (integer-expt n n))
125 (rem i (quotient rem n)))
127 (set! lst (cons (remainder rem n) lst)))
130 (define (common-tests delete-duplicates-proc)
131 (pass-if-exception "too few args" exception:wrong-num-args
132 (delete-duplicates-proc))
134 (pass-if-exception "too many args" exception:wrong-num-args
135 (delete-duplicates-proc '() equal? 99))
138 (eq? '() (delete-duplicates-proc '())))
140 (pass-if "equal? (the default)"
142 (delete-duplicates-proc '((2) (2) (2)))))
145 (equal? '((2) (2) (2))
146 (delete-duplicates-proc '((2) (2) (2)) eq?)))
148 (pass-if "called arg order"
150 (delete-duplicates-proc '(1 2 3 4 5)
157 (with-test-prefix "delete-duplicates"
158 (common-tests delete-duplicates)
162 (let ((lst-copy (list-copy lst)))
163 (with-test-prefix lst-copy
165 (equal? (delete-duplicates lst)
166 (ref-delete-duplicates lst)))
167 (pass-if "non-destructive"
168 (equal? lst-copy lst)))))))
170 (with-test-prefix "delete-duplicates!"
171 (common-tests delete-duplicates!)
176 (equal? (delete-duplicates! lst)
177 (ref-delete-duplicates lst)))))))
183 (with-test-prefix "drop"
186 (null? (drop '() 0)))
213 (pass-if "'(a b c) 1"
214 (let ((lst '(a b c)))
218 (pass-if "circular '(a) 0"
219 (let ((lst (circular-list 'a)))
223 (pass-if "circular '(a) 1"
224 (let ((lst (circular-list 'a)))
228 (pass-if "circular '(a) 2"
229 (let ((lst (circular-list 'a)))
233 (pass-if "circular '(a b) 1"
234 (let ((lst (circular-list 'a)))
238 (pass-if "circular '(a b) 2"
239 (let ((lst (circular-list 'a)))
243 (pass-if "circular '(a b) 5"
244 (let ((lst (circular-list 'a)))
248 (pass-if "'(a . b) 1"
252 (pass-if "'(a b . c) 1"
254 (drop '(a b . c) 2))))
260 (with-test-prefix "take"
263 (null? (take '() 0)))
266 (null? (take '(a) 0)))
269 (null? (take '() 0)))
271 (pass-if "'(a b c) 0"
272 (null? (take '() 0)))
277 (and (equal? '(a) got)
278 (not (eq? lst got)))))
284 (pass-if "'(a b c) 1"
291 (and (equal? '(a b) got)
292 (not (eq? lst got)))))
294 (pass-if "'(a b c) 2"
298 (pass-if "circular '(a) 0"
300 (take (circular-list 'a) 0)))
302 (pass-if "circular '(a) 1"
304 (take (circular-list 'a) 1)))
306 (pass-if "circular '(a) 2"
308 (take (circular-list 'a) 2)))
310 (pass-if "circular '(a b) 5"
312 (take (circular-list 'a 'b) 5)))
314 (pass-if "'(a . b) 1"
318 (pass-if "'(a b . c) 1"
320 (take '(a b . c) 1)))
322 (pass-if "'(a b . c) 2"
324 (take '(a b . c) 2))))