set-difference.lsp 7.99 KB
Newer Older
1 2 3 4 5 6 7
;-*- Mode:     Lisp -*-
;;;; Author:   Paul Dietz
;;;; Created:  Sun Apr 20 07:44:06 2003
;;;; Contains: Tests of SET-DIFFERENCE

(in-package :cl-test)

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

10 11 12 13 14 15
(deftest set-difference.1
  (set-difference nil nil)
  nil)

(deftest set-difference.2
  (let ((result
sds's avatar
sds committed
16
         (set-difference-with-check '(a b c) nil)))
17 18 19 20 21
    (check-set-difference '(a b c) nil result))
  t)

(deftest set-difference.3
  (let ((result
sds's avatar
sds committed
22
         (set-difference-with-check '(a b c d e f) '(f b d))))
23 24 25 26 27 28 29
    (check-set-difference '(a b c d e f) '(f b d) result))
  t)

(deftest set-difference.4
  (sort
   (copy-list
    (set-difference-with-check (shuffle '(1 2 3 4 5 6 7 8))
sds's avatar
sds committed
30
                               '(10 101 4 74 2 1391 7 17831)))
31 32 33 34 35 36 37 38 39
   #'<)
  (1 3 5 6 8))

(deftest set-difference.5
  (set-difference-with-check nil '(a b c d e f g h))
  nil)

(deftest set-difference.6
  (set-difference-with-check '(a b c d e) '(d a b e)
sds's avatar
sds committed
40
                             :key nil)
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
  (c))

(deftest set-difference.7
  (set-difference-with-check '(a b c d e) '(d a b e) :test #'eq)
  (c))

(deftest set-difference.8
  (set-difference-with-check '(a b c d e) '(d a b e) :test #'eql)
  (c))

(deftest set-difference.9
  (set-difference-with-check '(a b c d e) '(d a b e) :test #'equal)
  (c))

(deftest set-difference.10
  (set-difference-with-check '(a b c d e) '(d a b e)
sds's avatar
sds committed
57
                             :test 'eq)
58 59 60 61
  (c))

(deftest set-difference.11
  (set-difference-with-check '(a b c d e) '(d a b e)
sds's avatar
sds committed
62
                             :test 'eql)
63 64 65 66
  (c))

(deftest set-difference.12
  (set-difference-with-check '(a b c d e) '(d a b e)
sds's avatar
sds committed
67
                             :test 'equal)
68 69 70 71 72 73 74 75
  (c))

(deftest set-difference.13
  (do-random-set-differences 100 100)
  nil)

(deftest set-difference.14
  (set-difference-with-check '((a . 1) (b . 2) (c . 3))
sds's avatar
sds committed
76 77
                             '((a . 1) (c . 3))
                             :key 'car)
78 79 80 81
  ((b . 2)))

(deftest set-difference.15
  (set-difference-with-check '((a . 1) (b . 2) (c . 3))
sds's avatar
sds committed
82 83
                             '((a . 1) (c . 3))
                             :key #'car)
84 85 86 87 88 89 90 91 92 93 94 95 96
  ((b . 2)))

;;
;; Verify that the :test argument is called with the arguments
;; in the correct order
;;
(deftest set-difference.16
  (block fail
    (sort
     (copy-list
      (set-difference-with-check
       '(1 2 3 4) '(e f g h)
       :test #'(lambda (x y)
sds's avatar
sds committed
97 98 99 100
                 (when (or (member x '(e f g h))
                           (member y '(1 2 3 4)))
                   (return-from fail 'fail))
                 (eqt x y))))
101 102 103 104 105 106 107 108 109 110 111
     #'<))
  (1 2 3 4))

(deftest set-difference.17
  (block fail
    (sort
     (copy-list
      (set-difference-with-check
       '(1 2 3 4) '(e f g h)
       :key #'identity
       :test #'(lambda (x y)
sds's avatar
sds committed
112 113 114 115
                 (when (or (member x '(e f g h))
                           (member y '(1 2 3 4)))
                   (return-from fail 'fail))
                 (eqt x y))))
116 117 118 119 120 121 122 123 124 125 126
     #'<))
  (1 2 3 4))

(deftest set-difference.18
  (block fail
    (sort
     (copy-list
      (set-difference-with-check
       '(1 2 3 4) '(e f g h)
       :test-not
       #'(lambda (x y)
sds's avatar
sds committed
127 128 129 130
           (when (or (member x '(e f g h))
                     (member y '(1 2 3 4)))
             (return-from fail 'fail))
           (not (eqt x y)))))
131 132 133 134 135 136 137 138 139 140 141
     #'<))
  (1 2 3 4))

(deftest set-difference.19
  (block fail
    (sort
     (copy-list
      (set-difference-with-check
       '(1 2 3 4) '(e f g h)
       :test-not
       #'(lambda (x y)
sds's avatar
sds committed
142 143 144 145
           (when (or (member x '(e f g h))
                     (member y '(1 2 3 4)))
             (return-from fail 'fail))
           (not (eqt x y)))))
146 147 148
     #'<))
  (1 2 3 4))

149 150 151 152 153 154
(defharmless set-difference.test-and-test-not.1
  (set-difference (list 1 2 3 4) (list 1 7 3 8) :test #'eql :test-not #'eql))

(defharmless set-difference.test-and-test-not.2
  (set-difference (list 1 2 3 4) (list 1 7 3 8) :test-not #'eql :test #'eql))

155 156 157 158 159 160
;;; Order of argument evaluation tests

(deftest set-difference.order.1
  (let ((i 0) x y)
    (values
     (set-difference (progn (setf x (incf i)) (list 1 2 3 4))
sds's avatar
sds committed
161
                     (progn (setf y (incf i)) (list 2 3 4)))
162 163 164 165 166 167 168
     i x y))
  (1) 2 1 2)

(deftest set-difference.order.2
  (let ((i 0) x y z)
    (values
     (set-difference (progn (setf x (incf i)) (list 1 2 3 4))
sds's avatar
sds committed
169 170 171
                     (progn (setf y (incf i)) (list 2 3 4))
                     :test (progn (setf z (incf i))
                                  #'(lambda (x y) (= x (1- y)))))
172 173 174 175 176 177 178
     i x y z))
  (4) 3 1 2 3)

(deftest set-difference.order.3
  (let ((i 0) x y z w)
    (values
     (set-difference (progn (setf x (incf i)) (list 1 2 3 4))
sds's avatar
sds committed
179 180 181 182
                     (progn (setf y (incf i)) (list 2 3 4))
                     :test (progn (setf z (incf i))
                                  #'(lambda (x y) (= x (1- y))))
                     :key (progn (setf w (incf i)) nil))
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
     i x y z w))
  (4) 4 1 2 3 4)


;;; Keyword tests

(deftest set-difference.allow-other-keys.1
  (sort
   (copy-list
    (set-difference
     (list 1 2 3 4 5) (list 2 3 4)
     :bad t :allow-other-keys t))
   #'<)
  (1 5))

(deftest set-difference.allow-other-keys.2
  (sort
   (copy-list
    (set-difference
     (list 1 2 3 4 5) (list 2 3 4)
     :allow-other-keys t :bad t))
   #'<)
  (1 5))

(deftest set-difference.allow-other-keys.3
  (sort
   (copy-list
    (set-difference
     (list 1 2 3 4 5) (list 2 3 4)
     :allow-other-keys t :bad t :test #'(lambda (x y) (= x (1- y)))))
   #'<)
  (4 5))

(deftest set-difference.allow-other-keys.4
  (sort
   (copy-list
    (set-difference
     (list 1 2 3 4 5) (list 2 3 4)
     :allow-other-keys t))
   #'<)
  (1 5))

(deftest set-difference.allow-other-keys.5
  (sort
   (copy-list
    (set-difference
     (list 1 2 3 4 5) (list 2 3 4)
     :allow-other-keys nil))
   #'<)
  (1 5))

(deftest set-difference.allow-other-keys.6
  (sort
   (copy-list
    (set-difference
     (list 1 2 3 4 5) (list 2 3 4)
     :allow-other-keys t
     :allow-other-keys nil))
   #'<)
  (1 5))

(deftest set-difference.allow-other-keys.7
  (sort
   (copy-list
    (set-difference
     (list 1 2 3 4 5) (list 2 3 4)
     :allow-other-keys t
     :allow-other-keys nil
     '#:x 1))
   #'<)
  (1 5))

(deftest set-difference.keywords.8
  (sort
   (copy-list
    (set-difference
     (list 1 2 3 4 5) (list 2 3 4)
     :test #'eql :test (complement #'eql)))
   #'<)
  (1 5))

(deftest set-difference.keywords.9
  (sort
   (copy-list
    (set-difference
     (list 1 2 3 4 5) (list 2 3 4)
     :test (complement #'eql) :test #'eql))
   #'<)
  nil)

pfdietz's avatar
pfdietz committed
273 274
(def-fold-test set-difference.fold.1 (set-difference '(a b c d e f g h) '(b w h x e y)))

275 276 277 278
;;; Error tests


(deftest set-difference.error.1
279 280
  (signals-error (set-difference) program-error)
  t)
281 282

(deftest set-difference.error.2
283 284
  (signals-error (set-difference nil) program-error)
  t)
285 286

(deftest set-difference.error.3
287 288
  (signals-error (set-difference nil nil :bad t) program-error)
  t)
289 290

(deftest set-difference.error.4
291 292
  (signals-error (set-difference nil nil :key) program-error)
  t)
293 294

(deftest set-difference.error.5
295 296
  (signals-error (set-difference nil nil 1 2) program-error)
  t)
297 298

(deftest set-difference.error.6
299 300
  (signals-error (set-difference nil nil :bad t :allow-other-keys nil) program-error)
  t)
301 302

(deftest set-difference.error.7
303 304
  (signals-error (set-difference (list 1 2) (list 3 4) :test #'identity) program-error)
  t)
305 306

(deftest set-difference.error.8
307 308
  (signals-error (set-difference (list 1 2) (list 3 4) :test-not #'identity) program-error)
  t)
309 310

(deftest set-difference.error.9
311 312
  (signals-error (set-difference (list 1 2) (list 3 4) :key #'cons) program-error)
  t)
313 314

(deftest set-difference.error.10
315 316
  (signals-error (set-difference (list 1 2) (list 3 4) :key #'car) type-error)
  t)
317 318

(deftest set-difference.error.11
319 320
  (signals-error (set-difference (list 1 2 3) (list* 4 5 6)) type-error)
  t)
321 322

(deftest set-difference.error.12
323 324
  (signals-error (set-difference (list* 1 2 3) (list 4 5 6)) type-error)
  t)
325 326 327 328 329 330 331 332 333

(deftest set-difference.error.13
  (check-type-error #'(lambda (x) (set-difference x '(a b c))) #'listp)
  nil)

(deftest set-difference.error.14
  (check-type-error #'(lambda (x) (set-difference '(a b c) x)) #'listp)
  nil)