union.lsp 9.83 KB
Newer Older
1 2
;-*- Mode:     Lisp -*-
;;;; Author:   Paul Dietz
3 4
;;;; Created:  Sun Apr 20 07:41:24 2003
;;;; Contains: Tests of UNION
5 6

(in-package :cl-test)
7

8 9
(compile-and-load "cons-aux.lsp")

10 11
(deftest union.1
  (union nil nil)
12 13
  nil)

14 15
(deftest union.2
  (union-with-check (list 'a) nil)
16 17
  (a))

18 19
(deftest union.3
  (union-with-check (list 'a) (list 'a))
20 21 22
  (a))

(deftest union-4
23
  (union-with-check (list 1) (list 1))
24 25
  (1))

26 27 28
(deftest union.5
  (let ((x (list 'a 'b)))
    (union-with-check (list x) (list x)))
29 30
  ((a b)))

31 32 33 34 35
(deftest union.6
  (let ((x (copy-list '(a b c d e f)))
	(y (copy-list '(z c y a v b))))
    (let ((result (union-with-check x y)))
      (check-union x y result)))
36 37
  t)

38 39 40 41 42
(deftest union.6-a
  (let ((x (copy-list '(a b c d e f)))
	(y (copy-list '(z c y a v b))))
    (let ((result (union-with-check x y :test #'eq)))
      (check-union x y result)))
43 44
  t)

45 46 47 48 49
(deftest union.7
  (let ((x (copy-list '(a b c d e f)))
	(y (copy-list '(z c y a v b))))
    (let ((result (union-with-check x y :test #'eql)))
      (check-union x y result)))
50 51
  t)

52 53 54 55 56
(deftest union.8
  (let ((x (copy-list '(a b c d e f)))
	(y (copy-list '(z c y a v b))))
    (let ((result (union-with-check x y :test #'equal)))
      (check-union x y result)))
57 58
  t)

59 60 61 62 63
(deftest union.9
  (let ((x (copy-list '(a b c d e f)))
	(y (copy-list '(z c y a v b))))
    (let ((result (union-with-check x y :test-not (complement #'eql))))
      (check-union x y result)))
64 65
  t)

66 67 68 69 70
(deftest union.10
  (let ((x (copy-list '(a b c d e f)))
	(y (copy-list '(z c y a v b))))
    (let ((result (union-with-check x y :test-not (complement #'equal))))
      (check-union x y result)))
71 72
  t)

73 74 75 76 77
(deftest union.11
  (let ((x (copy-list '(a b c d e f)))
	(y (copy-list '(z c y a v b))))
    (let ((result (union-with-check x y :test-not (complement #'eq))))
      (check-union x y result)))
78 79
  t)

80 81 82 83 84
(deftest union.12
  (let ((x (copy-list '(1 2 3 4 5 6 7)))
	(y (copy-list '(10 19 5 3 17 1001 2))))
    (let ((result (union-with-check x y)))
      (check-union x y result)))
85 86
  t)

87 88 89 90 91
(deftest union.13
  (let ((x (copy-list '(1 2 3 4 5 6 7)))
	(y (copy-list '(10 19 5 3 17 1001 2))))
    (let ((result (union-with-check x y :test #'equal)))
      (check-union x y result)))
92 93
  t)

94 95 96 97 98
(deftest union.14
  (let ((x (copy-list '(1 2 3 4 5 6 7)))
	(y (copy-list '(10 19 5 3 17 1001 2))))
    (let ((result (union-with-check x y :test #'eql)))
      (check-union x y result)))
99 100
  t)

101 102 103 104 105
(deftest union.15
  (let ((x (copy-list '(1 2 3 4 5 6 7)))
	(y (copy-list '(10 19 5 3 17 1001 2))))
    (let ((result (union-with-check x y :test-not (complement #'equal))))
      (check-union x y result)))
106 107
  t)

108 109 110 111 112
(deftest union.16
  (let ((x (copy-list '(1 2 3 4 5 6 7)))
	(y (copy-list '(10 19 5 3 17 1001 2))))
    (let ((result (union-with-check x y :test-not (complement  #'eql))))
      (check-union x y result)))
113 114
  t)

115 116 117 118 119
(deftest union.17
  (let ((x (copy-list '(1 2 3 4 5 6 7)))
	(y (copy-list '(10 19 5 3 17 1001 2))))
    (let ((result (union-with-check-and-key x y #'1+)))
      (check-union x y result)))
120 121
  t)

122 123 124 125 126
(deftest union.18
  (let ((x (copy-list '(1 2 3 4 5 6 7)))
	(y (copy-list '(10 19 5 3 17 1001 2))))
    (let ((result (union-with-check-and-key x y #'1+ :test #'equal)))
      (check-union x y result)))
127 128
  t)

129 130 131 132 133
(deftest union.19
  (let ((x (copy-list '(1 2 3 4 5 6 7)))
	(y (copy-list '(10 19 5 3 17 1001 2))))
    (let ((result (union-with-check-and-key x y #'1+ :test #'eql)))
      (check-union x y result)))
134 135
  t)

136 137 138 139 140 141
(deftest union.20
  (let ((x (copy-list '(1 2 3 4 5 6 7)))
	(y (copy-list '(10 19 5 3 17 1001 2))))
    (let ((result (union-with-check-and-key x y #'1+
					    :test-not (complement #'equal))))
      (check-union x y result)))
142 143
  t)

144 145 146 147 148 149
(deftest union.21
  (let ((x (copy-list '(1 2 3 4 5 6 7)))
	(y (copy-list '(10 19 5 3 17 1001 2))))
    (let ((result (union-with-check-and-key x y #'1+
					    :test-not (complement #'equal))))
      (check-union x y result)))
150 151
  t)

152
(deftest union.22
153 154 155 156
  (let ((x (copy-list '(1 2 3 4 5 6 7)))
	(y (copy-list '(10 19 5 3 17 1001 2))))
    (let ((result (union-with-check-and-key x y nil)))
      (check-union x y result)))
157 158
  t)

159
(deftest union.23
160 161 162 163 164 165 166 167
  (let ((x (copy-list '(1 2 3 4 5 6 7)))
	(y (copy-list '(10 19 5 3 17 1001 2))))
    (let ((result (union-with-check-and-key x y '1+)))
      (check-union x y result)))
  t)

;; Do large numbers of random units

168
(deftest union.24
169 170 171
  (do-random-unions 100 100 200)
  nil)

172
(deftest union.25
173 174 175 176 177 178
  (let ((x (shuffle '(1 4 6 10 45 101)))
	(y (copy-list '(102 5 2 11 44 6))))
    (let ((result (union-with-check x y
				    :test #'(lambda (a b)
						    (<= (abs (- a b)) 1)))))
      (and
179
       (not (eqt result 'failed))
180 181 182 183 184 185 186
       (sort
	(sublis
	 '((2 . 1) (5 . 4) (11 . 10) (45 . 44) (102 . 101))
	 (copy-list result))
	#'<))))
  (1 4 6 10 44 101))

187
;;; Check that union uses eql, not equal or eq
188

189
(deftest union.26
190 191 192 193 194 195 196
  (let ((x 1000)
	(y 1000))
    (loop
     while (not (typep x 'bignum))
     do (progn
	  (setf x (* x x))
	  (setf y (* y y))))
197 198 199 200 201 202 203
    (notnot-mv
     (or
      (eqt x y)  ;; if bignums are eq, the test is worthless
      (eql (length
	    (union-with-check
	     (list x) (list x)))
	   1))))
204 205
  t)

206
(deftest union.27
207 208 209 210 211 212
  (union-with-check (list (copy-seq "aa"))
		    (list (copy-seq "aa")))
  ("aa" "aa"))

;; Check that union does not reverse the arguments to :test, :test-not

213 214 215 216 217 218 219 220 221 222
(deftest union.28
  (block fail
    (sort
     (union-with-check
      (list 1 2 3)
      (list 4 5 6)
      :test #'(lambda (x y)
		(when (< y x) (return-from fail 'fail))
		(eql x y)))
     #'<))
223 224
  (1 2 3 4 5 6))

225 226 227 228 229 230 231 232 233 234 235
(deftest union.29
  (block fail
    (sort
     (union-with-check-and-key
      (list 1 2 3)
      (list 4 5 6)
      #'identity
      :test #'(lambda (x y)
		(when (< y x) (return-from fail 'fail))
		(eql x y)))
     #'<))
236 237
  (1 2 3 4 5 6))

238 239 240 241 242 243 244 245 246 247 248
(deftest union.30
  (block fail
    (sort
     (union-with-check
      (list 1 2 3)
      (list 4 5 6)
      :test-not
      #'(lambda (x y)
	  (when (< y x) (return-from fail 'fail))
	  (not (eql x y))))
     #'<))
249 250
  (1 2 3 4 5 6))

251 252 253 254 255 256 257 258 259 260 261
(deftest union.31
  (block fail
    (sort
     (union-with-check-and-key
      (list 1 2 3)
      (list 4 5 6)
      #'identity
      :test-not #'(lambda (x y)
		    (when (< y x) (return-from fail 'fail))
		    (not (eql x y))))
     #'<))
262
  (1 2 3 4 5 6))
263

264 265 266 267 268 269 270
(defharmless union.test-and-test-not.1
  (union (list 1 4 8 10) (list 1 2 3 9 10 13) :test #'eql :test-not #'eql))

(defharmless union.test-and-test-not.2
  (union (list 1 4 8 10) (list 1 2 3 9 10 13) :test-not #'eql :test #'eql))


271 272
;;; Order of evaluation tests

273
(deftest union.order.1
274 275 276 277 278 279 280 281 282 283
  (let ((i 0) x y)
    (values
     (sort
      (union (progn (setf x (incf i)) (copy-list '(1 3 5)))
	     (progn (setf y (incf i)) (copy-list '(2 5 8))))
      #'<)
     i x y))
  (1 2 3 5 8)
  2 1 2)

284
(deftest union.order.2
285 286 287 288 289 290 291 292 293 294 295 296 297
  (let ((i 0) x y z w)
    (values
     (sort
      (union (progn (setf x (incf i)) (copy-list '(1 3 5)))
	     (progn (setf y (incf i)) (copy-list '(2 5 8)))
	     :test (progn (setf z (incf i)) #'eql)
	     :key (progn (setf w (incf i)) #'identity))
      #'<)
     i x y z w))
  (1 2 3 5 8)
  4 1 2 3 4)


298
(deftest union.order.3
299 300 301 302 303 304 305 306 307 308 309 310
  (let ((i 0) x y z w)
    (values
     (sort
      (union (progn (setf x (incf i)) (copy-list '(1 3 5)))
	     (progn (setf y (incf i)) (copy-list '(2 5 8)))
	     :key (progn (setf z (incf i)) #'identity)
	     :test (progn (setf w (incf i)) #'eql))
      #'<)
     i x y z w))
  (1 2 3 5 8)
  4 1 2 3 4)

311 312 313
;;; Keyword tests

(deftest union.allow-other-keys.1
314 315 316 317 318
  (sort (union (list 7 9 1 5) (list 10 11 9 20 1 2) :bad t
	       :allow-other-keys "yes")
	#'<)
  (1 2 5 7 9 10 11 20))

319
(deftest union.allow-other-keys.2
320 321 322 323 324
  (sort (union (list 7 9 1 5) (list 10 11 9 20 1 2)
	       :allow-other-keys t :also-bad t)
	#'<)
  (1 2 5 7 9 10 11 20))

325
(deftest union.allow-other-keys.3
326 327 328 329 330 331
  (sort (union (list 1 2 3) (list 1 2 3)
	       :allow-other-keys t :also-bad t
	       :test #'(lambda (x y) (= x (+ y 100))))
	#'<)
  (1 1 2 2 3 3))

332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
(deftest union.allow-other-keys.4
  (sort (union (list 7 9 1 5) (list 10 11 9 20 1 2)
	       :allow-other-keys t)
	#'<)
  (1 2 5 7 9 10 11 20))

(deftest union.allow-other-keys.5
  (sort (union (list 7 9 1 5) (list 10 11 9 20 1 2)
	       :allow-other-keys nil)
	#'<)
  (1 2 5 7 9 10 11 20))

(deftest union.allow-other-keys.6
  (sort (union (list 7 9 1 5) (list 10 11 9 20 1 2)
	       :allow-other-keys t
	       :allow-other-keys nil)
	#'<)
  (1 2 5 7 9 10 11 20))

(deftest union.allow-other-keys.7
  (sort (union (list 7 9 1 5) (list 10 11 9 20 1 2)
	       :allow-other-keys t
	       :allow-other-keys nil
	       '#:x 1)
	#'<)
  (1 2 5 7 9 10 11 20))

(deftest union.keywords.9
  (sort (union (list 1 2 3) (list 1 2 3)
	       :test #'(lambda (x y) (= x (+ y 100)))
	       :test #'eql)
	#'<)
  (1 1 2 2 3 3))

;;; Error tests
367

368
(deftest union.error.1
369 370
  (signals-error (union) program-error)
  t)
371 372

(deftest union.error.2
373 374
  (signals-error (union nil) program-error)
  t)
375 376

(deftest union.error.3
377 378
  (signals-error (union nil nil :bad t) program-error)
  t)
379 380

(deftest union.error.4
381 382
  (signals-error (union nil nil :key) program-error)
  t)
383 384

(deftest union.error.5
385 386
  (signals-error (union nil nil 1 2) program-error)
  t)
387 388

(deftest union.error.6
389 390
  (signals-error (union nil nil :bad t :allow-other-keys nil) program-error)
  t)
391 392

(deftest union.error.7
393 394
  (signals-error (union (list 1 2) (list 3 4) :test #'identity) program-error)
  t)
395 396

(deftest union.error.8
397 398
  (signals-error (union (list 1 2) (list 3 4) :test-not #'identity) program-error)
  t)
399 400

(deftest union.error.9
401 402
  (signals-error (union (list 1 2) (list 3 4) :key #'cons) program-error)
  t)
403 404

(deftest union.error.10
405 406
  (signals-error (union (list 1 2) (list 3 4) :key #'car) type-error)
  t)
407

408
(deftest union.error.11
409 410
  (signals-error (union (list 1 2 3) (list* 4 5 6)) type-error)
  t)
411 412

(deftest union.error.12
413 414
  (signals-error (union (list* 1 2 3) (list 4 5 6)) type-error)
  t)
415 416 417 418 419 420 421 422 423

(deftest union.error.13
  (check-type-error #'(lambda (x) (union x nil)) #'listp)
  nil)

(deftest union.error.14
  (check-type-error #'(lambda (x) (union nil x)) #'listp)
  nil)