* tests/alist.test, tests/bit-operations.test,
[bpt/guile.git] / test-suite / tests / common-list.test
1 ;;;; common-list.test --- tests guile's common list functions -*- scheme -*-
2 ;;;; Copyright (C) 2000, 2001 Free Software Foundation, Inc.
3 ;;;;
4 ;;;; This program is free software; you can redistribute it and/or modify
5 ;;;; it under the terms of the GNU General Public License as published by
6 ;;;; the Free Software Foundation; either version 2, or (at your option)
7 ;;;; any later version.
8 ;;;;
9 ;;;; This program is distributed in the hope that it will be useful,
10 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 ;;;; GNU General Public License for more details.
13 ;;;;
14 ;;;; You should have received a copy of the GNU General Public License
15 ;;;; along with this software; see the file COPYING. If not, write to
16 ;;;; the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
17 ;;;; Boston, MA 02111-1307 USA
18 ;;;;
19 ;;;; As a special exception, the Free Software Foundation gives permission
20 ;;;; for additional uses of the text contained in its release of GUILE.
21 ;;;;
22 ;;;; The exception is that, if you link the GUILE library with other files
23 ;;;; to produce an executable, this does not by itself cause the
24 ;;;; resulting executable to be covered by the GNU General Public License.
25 ;;;; Your use of that executable is in no way restricted on account of
26 ;;;; linking the GUILE library code into it.
27 ;;;;
28 ;;;; This exception does not however invalidate any other reasons why
29 ;;;; the executable file might be covered by the GNU General Public License.
30 ;;;;
31 ;;;; This exception applies only to the code released by the
32 ;;;; Free Software Foundation under the name GUILE. If you copy
33 ;;;; code from other Free Software Foundation releases into a copy of
34 ;;;; GUILE, as the General Public License permits, the exception does
35 ;;;; not apply to the code that you add in this way. To avoid misleading
36 ;;;; anyone as to the status of such modified files, you must delete
37 ;;;; this exception notice from them.
38 ;;;;
39 ;;;; If you write modifications of your own for GUILE, it is your choice
40 ;;;; whether to permit this exception to apply to your modifications.
41 ;;;; If you do not wish that, delete this exception notice.
42
43 (use-modules (ice-9 documentation)
44 (ice-9 common-list))
45
46
47 ;;;
48 ;;; miscellaneous
49 ;;;
50
51
52 (define (documented? object)
53 (not (not (object-documentation object))))
54
55
56 ;;;
57 ;;; intersection
58 ;;;
59
60 (with-test-prefix "intersection"
61
62 (pass-if "documented?"
63 (documented? intersection))
64
65 (pass-if "both arguments empty"
66 (eq? (intersection '() '()) '()))
67
68 (pass-if "first argument empty"
69 (eq? (intersection '() '(1)) '()))
70
71 (pass-if "second argument empty"
72 (eq? (intersection '(1) '()) '()))
73
74 (pass-if "disjoint arguments"
75 (eq? (intersection '(1) '(2)) '()))
76
77 (pass-if "equal arguments"
78 (equal? (intersection '(1) '(1)) '(1)))
79
80 (pass-if "reverse argument order"
81 (equal? (intersection '(1 2 3) '(3 2 1)) '(1 2 3)))
82
83 (pass-if "multiple matches in first list"
84 (equal? (intersection '(1 1 2 2 3) '(3 2 1)) '(1 1 2 2 3)))
85
86 (pass-if "multiple matches in second list"
87 (equal? (intersection '(1 2 3) '(3 3 2 2 1)) '(1 2 3)))
88
89 (pass-if "mixed arguments"
90 (equal? (intersection '(1 2 3 5 7 8 10) '(1 3 4 7 8 9)) '(1 3 7 8)))
91
92 )
93
94
95 ;;;
96 ;;; set-difference
97 ;;;
98
99 (with-test-prefix "set-difference"
100
101 (pass-if "documented?"
102 (documented? set-difference))
103
104 (pass-if "both arguments empty"
105 (eq? (set-difference '() '()) '()))
106
107 (pass-if "first argument empty"
108 (eq? (set-difference '() '(1)) '()))
109
110 (pass-if "second argument empty"
111 (equal? (set-difference '(1) '()) '(1)))
112
113 (pass-if "disjoint arguments"
114 (equal? (set-difference '(1) '(2)) '(1)))
115
116 (pass-if "equal arguments"
117 (eq? (set-difference '(1) '(1)) '()))
118
119 (pass-if "reverse argument order"
120 (eq? (set-difference '(1 2 3) '(3 2 1)) '()))
121
122 (pass-if "multiple matches in first list"
123 (eq? (set-difference '(1 1 2 2 3) '(3 2 1)) '()))
124
125 (pass-if "multiple matches in second list"
126 (eq? (set-difference '(1 2 3) '(3 3 2 2 1)) '()))
127
128 (pass-if "mixed arguments"
129 (equal? (set-difference '(1 2 3 5 7 8 10) '(1 3 4 7 8 9)) '(2 5 10)))
130
131 )
132
133
134 ;;;
135 ;;; remove-if
136 ;;;
137
138 (with-test-prefix "remove-if"
139
140 (pass-if "documented?"
141 (documented? remove-if))
142
143 (pass-if "empty list, remove all"
144 (eq? (remove-if (lambda (x) #t) '()) '()))
145
146 (pass-if "empty list, remove none"
147 (eq? (remove-if (lambda (x) #f) '()) '()))
148
149 (pass-if "non-empty list, remove all"
150 (eq? (remove-if (lambda (x) #t) '(1 2 3 4)) '()))
151
152 (pass-if "non-empty list, remove none"
153 (equal? (remove-if (lambda (x) #f) '(1 2 3 4)) '(1 2 3 4)))
154
155 (pass-if "non-empty list, remove some"
156 (equal? (remove-if odd? '(1 2 3 4)) '(2 4)))
157
158 )
159
160
161 ;;;
162 ;;; remove-if-not
163 ;;;
164
165
166 (with-test-prefix "remove-if-not"
167
168 (pass-if "documented?"
169 (documented? remove-if-not))
170
171 (pass-if "empty list, remove all"
172 (eq? (remove-if-not (lambda (x) #f) '()) '()))
173
174 (pass-if "empty list, remove none"
175 (eq? (remove-if-not (lambda (x) #t) '()) '()))
176
177 (pass-if "non-empty list, remove all"
178 (eq? (remove-if-not (lambda (x) #f) '(1 2 3 4)) '()))
179
180 (pass-if "non-empty list, remove none"
181 (equal? (remove-if-not (lambda (x) #t) '(1 2 3 4)) '(1 2 3 4)))
182
183 (pass-if "non-empty list, remove some"
184 (equal? (remove-if-not odd? '(1 2 3 4)) '(1 3)))
185
186 )
187
188
189 ;;;
190 ;;; delete-if!
191 ;;;
192
193
194 (with-test-prefix "delete-if!"
195
196 (pass-if "documented?"
197 (documented? delete-if!))
198
199 (pass-if "empty list, remove all"
200 (eq? (delete-if! (lambda (x) #t) '()) '()))
201
202 (pass-if "empty list, remove none"
203 (eq? (delete-if! (lambda (x) #f) '()) '()))
204
205 (pass-if "non-empty list, remove all"
206 (eq? (delete-if! (lambda (x) #t) '(1 2 3 4)) '()))
207
208 (pass-if "non-empty list, remove none"
209 (equal? (delete-if! (lambda (x) #f) '(1 2 3 4)) '(1 2 3 4)))
210
211 (pass-if "non-empty list, remove some"
212 (equal? (delete-if! odd? '(1 2 3 4)) '(2 4)))
213
214 )
215
216
217 ;;;
218 ;;; delete-if-not!
219 ;;;
220
221
222 (with-test-prefix "delete-if-not!"
223
224 (pass-if "documented?"
225 (documented? delete-if-not!))
226
227 (pass-if "empty list, remove all"
228 (eq? (delete-if-not! (lambda (x) #f) '()) '()))
229
230 (pass-if "empty list, remove none"
231 (eq? (delete-if-not! (lambda (x) #t) '()) '()))
232
233 (pass-if "non-empty list, remove all"
234 (eq? (delete-if-not! (lambda (x) #f) '(1 2 3 4)) '()))
235
236 (pass-if "non-empty list, remove none"
237 (equal? (delete-if-not! (lambda (x) #t) '(1 2 3 4)) '(1 2 3 4)))
238
239 (pass-if "non-empty list, remove some"
240 (equal? (delete-if-not! odd? '(1 2 3 4)) '(1 3)))
241
242 )