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

(in-package :cl-test)

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

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

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

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

(deftest nset-difference.4
  (sort
   (copy-list
    (nset-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 nset-difference.5
  (nset-difference-with-check nil '(a b c d e f g h))
  nil)

(deftest nset-difference.6
  (nset-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 nset-difference.7
  (nset-difference-with-check '(a b c d e) '(d a b e) :test #'eq)
  (c))

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

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

(deftest nset-difference.10
  (nset-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 nset-difference.11
  (nset-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 nset-difference.12
  (nset-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 nset-difference.13
  (do-random-nset-differences 100 100)
  nil)

(deftest nset-difference.14
  (nset-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 nset-difference.15
  (nset-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 nset-difference.16
  (block fail
    (sort
     (copy-list
      (nset-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 nset-difference.17
  (block fail
    (sort
     (copy-list
      (nset-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 nset-difference.18
  (block fail
    (sort
     (copy-list
      (nset-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
     #'<))
  (1 2 3 4))

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

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

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

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

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

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

(deftest nset-difference.order.3
  (let ((i 0) x y z w)
    (values
     (nset-difference (progn (setf x (incf i)) (list 1 2 3 4))
sds's avatar
sds committed
178 179 180 181
                      (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))
182 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 273 274
     i x y z w))
  (4) 4 1 2 3 4)


;;; Keyword tests

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

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

(deftest nset-difference.allow-other-keys.3
  (sort
   (copy-list
    (nset-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 nset-difference.allow-other-keys.4
  (sort
   (copy-list
    (nset-difference
     (list 1 2 3 4 5) (list 2 3 4)
     :allow-other-keys t))
   #'<)
  (1 5))

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

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

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

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

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

;;; Error tests

(deftest nset-difference.error.1
275 276
  (signals-error (nset-difference) program-error)
  t)
277 278

(deftest nset-difference.error.2
279 280
  (signals-error (nset-difference nil) program-error)
  t)
281 282

(deftest nset-difference.error.3
283 284
  (signals-error (nset-difference nil nil :bad t) program-error)
  t)
285 286

(deftest nset-difference.error.4
287 288
  (signals-error (nset-difference nil nil :key) program-error)
  t)
289 290

(deftest nset-difference.error.5
291 292
  (signals-error (nset-difference nil nil 1 2) program-error)
  t)
293 294

(deftest nset-difference.error.6
295 296
  (signals-error (nset-difference nil nil :bad t :allow-other-keys nil) program-error)
  t)
297 298

(deftest nset-difference.error.7
299 300
  (signals-error (nset-difference (list 1 2) (list 3 4) :test #'identity) program-error)
  t)
301 302

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

(deftest nset-difference.error.9
307 308
  (signals-error (nset-difference (list 1 2) (list 3 4) :key #'cons) program-error)
  t)
309 310

(deftest nset-difference.error.10
311 312
  (signals-error (nset-difference (list 1 2) (list 3 4) :key #'car) type-error)
  t)
313 314

(deftest nset-difference.error.11
315 316
  (signals-error (nset-difference (list 1 2 3) (list* 4 5 6)) type-error)
  t)
317 318

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

pfdietz's avatar
pfdietz committed
322 323 324 325 326 327 328
(deftest nset-difference.error.13
  (check-type-error #'(lambda (x) (nset-difference (list 'a 'b) x)) #'listp)
  nil)

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