2
;;;; Author: Paul Dietz
3
;;;; Created: Sun Apr 20 07:39:19 2003
4
;;;; Contains: Tests of INTERSECTION
8
(compile-and-load "cons-aux.lsp")
10
(deftest intersection.1
11
(intersection nil nil)
14
(deftest intersection.2
15
(intersection (loop for i from 1 to 100 collect i) nil)
18
(deftest intersection.3
19
(intersection nil (loop for i from 1 to 100 collect i))
22
(deftest intersection.4
23
(let* ((x (copy-list '(a 1 c 7 b 4 3 z)))
24
(xcopy (make-scaffold-copy x))
25
(y (copy-list '(3 y c q z a 18)))
26
(ycopy (make-scaffold-copy y))
27
(result (intersection x y)))
29
(check-scaffold-copy x xcopy)
30
(check-scaffold-copy y ycopy)
35
(not (member e result))))
38
(or (not (member e x))
41
for hd on result count
43
(member (car hd) (cdr hd)))))))
46
(deftest intersection.5
47
(let* ((x (copy-list '(a a a)))
48
(xcopy (make-scaffold-copy x))
49
(y (copy-list '(a a a b b b)))
50
(ycopy (make-scaffold-copy y))
51
(result (intersection x y)))
53
(check-scaffold-copy x xcopy)
54
(check-scaffold-copy y ycopy)
56
(not (member 'b result))))
59
(deftest intersection.6
60
(intersection (list 1000000000000 'a 'b 'c)
61
(list (1+ 999999999999) 'd 'e 'f))
64
(deftest intersection.7
65
(intersection (list 'a 10 'b 17)
66
(list 'c 'd 4 'e 'f 10 1 13 'z))
69
(deftest intersection.8
70
(intersection (list 'a (copy-seq "aaa") 'b)
71
(list 'd (copy-seq "aaa") 'e))
74
(deftest intersection.9
75
(intersection (list 'a (copy-seq "aaa") 'b)
76
(list 'd (copy-seq "aaa") 'e)
80
;; Same as 9, but with a symbol function designator for :test
81
(deftest intersection.9-a
82
(intersection (list 'a (copy-seq "aaa") 'b)
83
(list 'd (copy-seq "aaa") 'e)
87
(deftest intersection.9-b
88
(intersection (list 'a (copy-seq "aaa") 'b)
89
(list 'd (copy-seq "aaa") 'e)
90
:test-not #'(lambda (p q) (not (equal p q))))
93
(deftest intersection.10
97
for i from 0 to 1000 by 3
100
for i from 0 to 1000 by 7
103
(loop for i from 0 to 1000 by 21 collect i))
106
(deftest intersection.11
110
for i from 0 to 999 by 5
113
for i from 0 to 999 by 7
115
:test #'(lambda (a b)
119
(loop for i from 0 to 999 by (* 3 5 7) collect i))
122
(deftest intersection.11-a
126
for i from 0 to 999 by 5
129
for i from 0 to 999 by 7
136
(loop for i from 0 to 999 by (* 3 5 7) collect i))
140
;; Do large numbers of random intersection tests
143
(deftest intersection.12
144
(intersection-12-body 100 100)
152
(deftest intersection.13
153
(let ((x (copy-list '(0 5 8 13 31 42)))
154
(y (copy-list '(3 5 42 0 7 100 312 33))))
156
(sort (copy-list (intersection x y)) #'<)
157
(sort (copy-list (intersection x y :key #'1+)) #'<)))
160
;; Same as 13, but with a symbol function designator for :key
161
(deftest intersection.13-a
162
(let ((x (copy-list '(0 5 8 13 31 42)))
163
(y (copy-list '(3 5 42 0 7 100 312 33))))
165
(sort (copy-list (intersection x y)) #'<)
166
(sort (copy-list (intersection x y :key '1+)) #'<)))
169
;; Test that a nil key argument is ignored
171
(deftest intersection.14
173
((result (intersection (copy-list '(a b c d))
174
(copy-list '(e c f b g))
179
(every #'(lambda (x) (member x '(b c))) result)
183
;; Test that intersection preserves the order of arguments to :test, :test-not
185
(deftest intersection.15
186
(let ((list1 (list 1 2 3 4))
187
(list2 (list 4 5 6 7)))
193
(when (< y x) (return-from fail 'fail))
197
(deftest intersection.16
198
(let ((list1 (list 1 2 3 4))
199
(list2 (list 4 5 6 7)))
206
(when (< y x) (return-from fail 'fail))
210
(deftest intersection.17
211
(let ((list1 (list 1 2 3 4))
212
(list2 (list 4 5 6 7)))
218
(when (< y x) (return-from fail 'fail))
222
(deftest intersection.18
223
(let ((list1 (list 1 2 3 4))
224
(list2 (list 4 5 6 7)))
231
(when (< y x) (return-from fail 'fail))
235
(defharmless intersection.test-and-test-not.1
236
(intersection '(a b c) '(a c e) :test #'eql :test-not #'eql))
238
(defharmless intersection.test-and-test-not.2
239
(intersection '(a b c) '(a c e) :test-not #'eql :test #'eql))
241
;;; Order of argument evaluation tests
243
(deftest intersection.order.1
246
(intersection (progn (setf x (incf i)) (list 'a 'b))
247
(progn (setf y (incf i)) (list 'c 'd)))
251
(deftest intersection.order.2
254
(intersection (progn (setf x (incf i)) (list 'a 'b))
255
(progn (setf y (incf i)) (list 'c 'd))
260
(deftest intersection.order.3
263
(intersection (progn (setf x (incf i)) (list 'a 'b))
264
(progn (setf y (incf i)) (list 'c 'd))
265
:test (progn (setf z (incf i)) #'eq)
266
:test (progn (setf w (incf i))
271
(deftest intersection.order.4
274
(intersection (progn (setf x (incf i)) (list 'a 'b))
275
(progn (setf y (incf i)) (list 'c 'd))
276
:test (progn (setf z (incf i)) #'eq)
277
:key (progn (setf w (incf i)) #'identity))
281
(deftest intersection.order.5
284
(intersection (progn (setf x (incf i)) (list 'a 'b))
285
(progn (setf y (incf i)) (list 'c 'd))
286
:key (progn (setf z (incf i)) #'identity)
287
:test (progn (setf w (incf i)) #'eq))
294
(deftest intersection.allow-other-keys.1
295
(let ((list1 (list 1 2 3 4))
296
(list2 (list 4 5 6 7)))
297
(intersection list1 list2 :bad t :allow-other-keys 1))
300
(deftest intersection.allow-other-keys.2
301
(let ((list1 (list 1 2 3 4))
302
(list2 (list 4 5 6 7)))
303
(intersection list1 list2 :allow-other-keys :foo :also-bad t))
306
(deftest intersectionallow-other-keys.3
307
(let ((list1 (list 1 2 3 4))
308
(list2 (list 4 5 6 7)))
309
(intersection list1 list2 :allow-other-keys :foo :also-bad t
310
:test #'(lambda (x y) (= x (1+ y)))))
313
(deftest intersection.allow-other-keys.4
314
(let ((list1 (list 1 2 3 4))
315
(list2 (list 4 5 6 7)))
316
(intersection list1 list2 :allow-other-keys t))
319
(deftest intersection.allow-other-keys.5
320
(let ((list1 (list 1 2 3 4))
321
(list2 (list 4 5 6 7)))
322
(intersection list1 list2 :allow-other-keys nil))
325
(deftest intersection.allow-other-keys.6
326
(let ((list1 (list 1 2 3 4))
327
(list2 (list 4 5 6 7)))
328
(intersection list1 list2 :allow-other-keys t
329
:allow-other-keys nil :bad t))
332
(deftest intersection.allow-other-keys.7
334
(let ((list1 (list 1 2 3 4))
335
(list2 (list 4 5 6 7)))
336
(intersection list1 list2 :allow-other-keys t
337
:allow-other-keys nil
338
:test #'(lambda (x y) (eql x (1- y)))))
342
(deftest intersection.keywords.8
344
(let ((list1 (list 1 2 3 4))
345
(list2 (list 4 5 6 7)))
346
(intersection list1 list2
347
:test #'(lambda (x y) (eql x (1- y)))
354
(deftest intersection.error.1
355
(signals-error (intersection) program-error)
358
(deftest intersection.error.2
359
(signals-error (intersection nil) program-error)
362
(deftest intersection.error.3
363
(signals-error (intersection nil nil :bad t) program-error)
366
(deftest intersection.error.4
367
(signals-error (intersection nil nil :key) program-error)
370
(deftest intersection.error.5
371
(signals-error (intersection nil nil 1 2) program-error)
374
(deftest intersection.error.6
375
(signals-error (intersection nil nil :bad t :allow-other-keys nil)
379
(deftest intersection.error.7
380
(signals-error (intersection '(a b c) '(d e f) :test #'identity)
384
(deftest intersection.error.8
385
(signals-error (intersection '(a b c) '(d e f) :test-not #'identity)
389
(deftest intersection.error.9
390
(signals-error (intersection '(a b c) '(d e f) :key #'cons)
394
(deftest intersection.error.10
395
(signals-error (intersection '(a b c) '(d e f) :key #'car)
399
(deftest intersection.error.11
400
(signals-error (intersection '(a b c) '(d e f . g))
404
(deftest intersection.error.12
405
(signals-error (intersection '(a b . c) '(d e f))