2
;;;; Author: Paul Dietz
3
;;;; Created: Sun Apr 20 07:41:24 2003
4
;;;; Contains: Tests of UNION
8
(compile-and-load "cons-aux.lsp")
15
(union-with-check (list 'a) nil)
19
(union-with-check (list 'a) (list 'a))
23
(union-with-check (list 1) (list 1))
27
(let ((x (list 'a 'b)))
28
(union-with-check (list x) (list x)))
32
(let ((x (copy-list '(a b c d e f)))
33
(y (copy-list '(z c y a v b))))
34
(let ((result (union-with-check x y)))
35
(check-union x y result)))
39
(let ((x (copy-list '(a b c d e f)))
40
(y (copy-list '(z c y a v b))))
41
(let ((result (union-with-check x y :test #'eq)))
42
(check-union x y result)))
46
(let ((x (copy-list '(a b c d e f)))
47
(y (copy-list '(z c y a v b))))
48
(let ((result (union-with-check x y :test #'eql)))
49
(check-union x y result)))
53
(let ((x (copy-list '(a b c d e f)))
54
(y (copy-list '(z c y a v b))))
55
(let ((result (union-with-check x y :test #'equal)))
56
(check-union x y result)))
60
(let ((x (copy-list '(a b c d e f)))
61
(y (copy-list '(z c y a v b))))
62
(let ((result (union-with-check x y :test-not (complement #'eql))))
63
(check-union x y result)))
67
(let ((x (copy-list '(a b c d e f)))
68
(y (copy-list '(z c y a v b))))
69
(let ((result (union-with-check x y :test-not (complement #'equal))))
70
(check-union x y result)))
74
(let ((x (copy-list '(a b c d e f)))
75
(y (copy-list '(z c y a v b))))
76
(let ((result (union-with-check x y :test-not (complement #'eq))))
77
(check-union x y result)))
81
(let ((x (copy-list '(1 2 3 4 5 6 7)))
82
(y (copy-list '(10 19 5 3 17 1001 2))))
83
(let ((result (union-with-check x y)))
84
(check-union x y result)))
88
(let ((x (copy-list '(1 2 3 4 5 6 7)))
89
(y (copy-list '(10 19 5 3 17 1001 2))))
90
(let ((result (union-with-check x y :test #'equal)))
91
(check-union x y result)))
95
(let ((x (copy-list '(1 2 3 4 5 6 7)))
96
(y (copy-list '(10 19 5 3 17 1001 2))))
97
(let ((result (union-with-check x y :test #'eql)))
98
(check-union x y result)))
102
(let ((x (copy-list '(1 2 3 4 5 6 7)))
103
(y (copy-list '(10 19 5 3 17 1001 2))))
104
(let ((result (union-with-check x y :test-not (complement #'equal))))
105
(check-union x y result)))
109
(let ((x (copy-list '(1 2 3 4 5 6 7)))
110
(y (copy-list '(10 19 5 3 17 1001 2))))
111
(let ((result (union-with-check x y :test-not (complement #'eql))))
112
(check-union x y result)))
116
(let ((x (copy-list '(1 2 3 4 5 6 7)))
117
(y (copy-list '(10 19 5 3 17 1001 2))))
118
(let ((result (union-with-check-and-key x y #'1+)))
119
(check-union x y result)))
123
(let ((x (copy-list '(1 2 3 4 5 6 7)))
124
(y (copy-list '(10 19 5 3 17 1001 2))))
125
(let ((result (union-with-check-and-key x y #'1+ :test #'equal)))
126
(check-union x y result)))
130
(let ((x (copy-list '(1 2 3 4 5 6 7)))
131
(y (copy-list '(10 19 5 3 17 1001 2))))
132
(let ((result (union-with-check-and-key x y #'1+ :test #'eql)))
133
(check-union x y result)))
137
(let ((x (copy-list '(1 2 3 4 5 6 7)))
138
(y (copy-list '(10 19 5 3 17 1001 2))))
139
(let ((result (union-with-check-and-key x y #'1+
140
:test-not (complement #'equal))))
141
(check-union x y result)))
145
(let ((x (copy-list '(1 2 3 4 5 6 7)))
146
(y (copy-list '(10 19 5 3 17 1001 2))))
147
(let ((result (union-with-check-and-key x y #'1+
148
:test-not (complement #'equal))))
149
(check-union x y result)))
153
(let ((x (copy-list '(1 2 3 4 5 6 7)))
154
(y (copy-list '(10 19 5 3 17 1001 2))))
155
(let ((result (union-with-check-and-key x y nil)))
156
(check-union x y result)))
160
(let ((x (copy-list '(1 2 3 4 5 6 7)))
161
(y (copy-list '(10 19 5 3 17 1001 2))))
162
(let ((result (union-with-check-and-key x y '1+)))
163
(check-union x y result)))
166
;; Do large numbers of random units
169
(do-random-unions 100 100 200)
173
(let ((x (shuffle '(1 4 6 10 45 101)))
174
(y (copy-list '(102 5 2 11 44 6))))
175
(let ((result (union-with-check x y
176
:test #'(lambda (a b)
177
(<= (abs (- a b)) 1)))))
179
(not (eqt result 'failed))
182
'((2 . 1) (5 . 4) (11 . 10) (45 . 44) (102 . 101))
187
;;; Check that union uses eql, not equal or eq
193
while (not (typep x 'bignum))
199
(eqt x y) ;; if bignums are eq, the test is worthless
207
(union-with-check (list (copy-seq "aa"))
208
(list (copy-seq "aa")))
211
;; Check that union does not reverse the arguments to :test, :test-not
219
:test #'(lambda (x y)
220
(when (< y x) (return-from fail 'fail))
228
(union-with-check-and-key
232
:test #'(lambda (x y)
233
(when (< y x) (return-from fail 'fail))
246
(when (< y x) (return-from fail 'fail))
254
(union-with-check-and-key
258
:test-not #'(lambda (x y)
259
(when (< y x) (return-from fail 'fail))
264
(defharmless union.test-and-test-not.1
265
(union (list 1 4 8 10) (list 1 2 3 9 10 13) :test #'eql :test-not #'eql))
267
(defharmless union.test-and-test-not.2
268
(union (list 1 4 8 10) (list 1 2 3 9 10 13) :test-not #'eql :test #'eql))
271
;;; Order of evaluation tests
273
(deftest union.order.1
277
(union (progn (setf x (incf i)) (copy-list '(1 3 5)))
278
(progn (setf y (incf i)) (copy-list '(2 5 8))))
284
(deftest union.order.2
288
(union (progn (setf x (incf i)) (copy-list '(1 3 5)))
289
(progn (setf y (incf i)) (copy-list '(2 5 8)))
290
:test (progn (setf z (incf i)) #'eql)
291
:key (progn (setf w (incf i)) #'identity))
298
(deftest union.order.3
302
(union (progn (setf x (incf i)) (copy-list '(1 3 5)))
303
(progn (setf y (incf i)) (copy-list '(2 5 8)))
304
:key (progn (setf z (incf i)) #'identity)
305
:test (progn (setf w (incf i)) #'eql))
313
(deftest union.allow-other-keys.1
314
(sort (union (list 7 9 1 5) (list 10 11 9 20 1 2) :bad t
315
:allow-other-keys "yes")
317
(1 2 5 7 9 10 11 20))
319
(deftest union.allow-other-keys.2
320
(sort (union (list 7 9 1 5) (list 10 11 9 20 1 2)
321
:allow-other-keys t :also-bad t)
323
(1 2 5 7 9 10 11 20))
325
(deftest union.allow-other-keys.3
326
(sort (union (list 1 2 3) (list 1 2 3)
327
:allow-other-keys t :also-bad t
328
:test #'(lambda (x y) (= x (+ y 100))))
332
(deftest union.allow-other-keys.4
333
(sort (union (list 7 9 1 5) (list 10 11 9 20 1 2)
336
(1 2 5 7 9 10 11 20))
338
(deftest union.allow-other-keys.5
339
(sort (union (list 7 9 1 5) (list 10 11 9 20 1 2)
340
:allow-other-keys nil)
342
(1 2 5 7 9 10 11 20))
344
(deftest union.allow-other-keys.6
345
(sort (union (list 7 9 1 5) (list 10 11 9 20 1 2)
347
:allow-other-keys nil)
349
(1 2 5 7 9 10 11 20))
351
(deftest union.allow-other-keys.7
352
(sort (union (list 7 9 1 5) (list 10 11 9 20 1 2)
354
:allow-other-keys nil
357
(1 2 5 7 9 10 11 20))
359
(deftest union.keywords.9
360
(sort (union (list 1 2 3) (list 1 2 3)
361
:test #'(lambda (x y) (= x (+ y 100)))
368
(deftest union.error.1
369
(signals-error (union) program-error)
372
(deftest union.error.2
373
(signals-error (union nil) program-error)
376
(deftest union.error.3
377
(signals-error (union nil nil :bad t) program-error)
380
(deftest union.error.4
381
(signals-error (union nil nil :key) program-error)
384
(deftest union.error.5
385
(signals-error (union nil nil 1 2) program-error)
388
(deftest union.error.6
389
(signals-error (union nil nil :bad t :allow-other-keys nil) program-error)
392
(deftest union.error.7
393
(signals-error (union (list 1 2) (list 3 4) :test #'identity) program-error)
396
(deftest union.error.8
397
(signals-error (union (list 1 2) (list 3 4) :test-not #'identity) program-error)
400
(deftest union.error.9
401
(signals-error (union (list 1 2) (list 3 4) :key #'cons) program-error)
404
(deftest union.error.10
405
(signals-error (union (list 1 2) (list 3 4) :key #'car) type-error)
408
(deftest union.error.11
409
(signals-error (union (list 1 2 3) (list* 4 5 6)) type-error)
412
(deftest union.error.12
413
(signals-error (union (list* 1 2 3) (list 4 5 6)) type-error)