subsetp.lsp 6.11 KB
Newer Older
1 2 3
;-*- Mode:     Lisp -*-
;;;; Author:   Paul Dietz
;;;; Created:  Wed Apr  1 22:10:54 1998
4
;;;; Contains: Tests of SUBSETP
5 6

(in-package :cl-test)
7

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

10 11
(defvar cons-test-24-var '(78 "z" (8 9)))

12 13
(deftest subsetp.1
  (subsetp-with-check (copy-tree '(78)) cons-test-24-var)
14 15
  t)

16 17
(deftest subsetp.2
  (subsetp-with-check (copy-tree '((8 9))) cons-test-24-var)
18 19
  nil)

20 21 22
(deftest subsetp.3
  (subsetp-with-check (copy-tree '((8 9)))
		      cons-test-24-var :test 'equal)
23 24
  t)

25 26 27
(deftest subsetp.4
  (subsetp-with-check (list 78 (copy-seq "Z")) cons-test-24-var
		      :test #'equalp)
28 29
  t)

30 31 32
(deftest subsetp.5
  (subsetp-with-check (list 1) (list 0 2 3 4)
		      :key #'(lambda (i) (floor (/ i 2))))
33 34
  t)

35 36 37
(deftest subsetp.6
  (subsetp-with-check (list 1 6) (list 0 2 3 4)
		      :key #'(lambda (i) (floor (/ i 2))))
38 39
  nil)

40 41 42 43 44
(deftest subsetp.7
  (subsetp-with-check (list '(a . 10) '(b . 20) '(c . 30))
		      (list '(z . c) '(a . y) '(b . 100) '(e . f)
			    '(c . foo))
		      :key #'car)
45 46
  t)

47 48 49 50 51
(deftest subsetp.8
  (subsetp-with-check (copy-tree '((a . 10) (b . 20) (c . 30)))
		      (copy-tree '((z . c) (a . y) (b . 100) (e . f)
				   (c . foo)))
		      :key 'car)
52 53
  t)

54 55 56 57 58 59 60
(deftest subsetp.9
  (subsetp-with-check (list 'a 'b 'c)
		      (copy-tree
		       (list '(z . c) '(a . y) '(b . 100) '(e . f)
			     '(c . foo)))
		      :test #'(lambda (e1 e2)
				(eqt e1 (car e2))))
61 62
  t)

63
(deftest subsetp.10
64 65 66 67 68 69 70
  (subsetp-with-check (list 'a 'b 'c)
		      (copy-tree
		       (list '(z . c) '(a . y) '(b . 100) '(e . f)
			     '(c . foo)))
		      :test #'(lambda (e1 e2)
				(eqt e1 (car e2)))
		      :key nil)
71 72
  t)

73 74 75 76 77 78 79
(deftest subsetp.11
  (subsetp-with-check (list 'a 'b 'c)
		      (copy-tree
		       (list '(z . c) '(a . y) '(b . 100) '(e . f)
			     '(c . foo)))
		      :test-not  #'(lambda (e1 e2)
				     (not (eqt e1 (car e2)))))
80 81 82 83
  t)

;; Check that it maintains order of arguments

84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
(deftest subsetp.12
  (block fail
    (subsetp-with-check
     (list 1 2 3)
     (list 4 5 6)
     :test #'(lambda (x y)
	       (when (< y x) (return-from fail 'fail))
	       t)))
  t)

(deftest subsetp.13
  (block fail
    (subsetp-with-check
     (list 1 2 3)
     (list 4 5 6)
     :key #'identity
     :test #'(lambda (x y)
	       (when (< y x) (return-from fail 'fail))
	       t)))
  t)

(deftest subsetp.14
  (block fail
    (subsetp-with-check
     (list 1 2 3)
     (list 4 5 6)
     :test-not #'(lambda (x y)
		   (when (< y x) (return-from fail 'fail))
		   nil)))
  t)

(deftest subsetp.15
  (block fail
    (subsetp-with-check
     (list 1 2 3)
     (list 4 5 6)
     :key #'identity
     :test-not #'(lambda (x y)
		   (when (< y x) (return-from fail 'fail))
		   nil)))
  t)

126 127 128 129 130 131
(defharmless subsetp.test-and-test-not.1
  (subsetp '(a b c) '(a g c e b) :test #'eql :test-not #'eql))

(defharmless subsetp.test-and-test-not.3
  (subsetp '(a b c) '(a g c e b) :test-not #'eql :test #'eql))

132 133
;;; Order of argument evaluation tests

134
(deftest subsetp.order.1
135 136 137 138 139 140 141 142 143
  (let ((i 0) x y)
    (values
     (notnot (subsetp (progn (setf x (incf i))
			     '(a b c))
		      (progn (setf y (incf i))
			     '(a b c d))))
     i x y))
  t 2 1 2)

144
(deftest subsetp.order.2
145 146 147 148 149 150 151 152 153 154 155
  (let ((i 0) x y z w)
    (values
     (notnot (subsetp (progn (setf x (incf i))
			     '(a b c))
		      (progn (setf y (incf i))
			     '(a b c d))
		      :test (progn (setf z (incf i)) #'eql)
		      :key  (progn (setf w (incf i)) nil)))
     i x y z w))
  t 4 1 2 3 4)

156
(deftest subsetp.order.3
157 158 159 160 161 162 163 164 165 166 167
  (let ((i 0) x y z w)
    (values
     (notnot (subsetp (progn (setf x (incf i))
			     '(a b c))
		      (progn (setf y (incf i))
			     '(a b c d))
		      :key  (progn (setf z (incf i)) nil)
		      :test (progn (setf w (incf i)) #'eql)))
     i x y z w))
  t 4 1 2 3 4)

168 169 170
;;; Keyword tests

(deftest subsetp.allow-other-keys.1
171
  (notnot-mv (subsetp '(1 2 3 4) '(0 1 2 3 4 5) :bad t :allow-other-keys 67))
172 173
  t)

174
(deftest subsetp.allow-other-keys.2
175
  (notnot-mv (subsetp '(1 2 3 4) '(0 1 2 3 4 5)
176 177 178
		   :allow-other-keys #'cons :bad t))
  t)

179
(deftest subsetp.allow-other-keys.3
180
  (notnot-mv (subsetp '(1 2 3 4) '(0 1 2 3 4)
181 182 183 184 185
		   :allow-other-keys (make-hash-table)
		   :bad t
		   :test #'(lambda (x y) (= (1+ x) y))))
  nil)

186
(deftest subsetp.allow-other-keys.4
187
  (notnot-mv (subsetp '(1 2 3 4) '(0 1 2 3 4 5) :allow-other-keys t))
188 189 190
  t)

(deftest subsetp.allow-other-keys.5
191
  (notnot-mv (subsetp '(1 2 3 4) '(0 1 2 3 4 5) :allow-other-keys nil))
192 193 194
  t)

(deftest subsetp.allow-other-keys.6
195
  (notnot-mv (subsetp '(1 2 3 4) '(0 1 2 3 4 5)
196 197 198 199 200
		   :allow-other-keys t :bad1 t
		   :allow-other-keys nil :bad2 t))
  t)

(deftest subsetp.keywords.7
201
  (notnot-mv (subsetp '(1 2 3 4) '(0 1 2 3 4)
202 203 204 205 206
		   :test #'(lambda (x y) (= (1+ x) y))
		   :test #'eql))
  nil)

(deftest subsetp.keywords.8
207
  (notnot-mv (subsetp '(1 2 3 4 10) '(0 1 2 3 4)
208 209 210 211 212 213 214
		   :key nil
		   :key #'(lambda (x) (mod x 2))))
  nil)


;;; Error tests

215
(deftest subsetp.error.1
216 217
  (signals-error (subsetp) program-error)
  t)
218 219

(deftest subsetp.error.2
220 221
  (signals-error (subsetp nil) program-error)
  t)
222 223

(deftest subsetp.error.3
224 225
  (signals-error (subsetp nil nil :bad t) program-error)
  t)
226 227

(deftest subsetp.error.4
228 229
  (signals-error (subsetp nil nil :key) program-error)
  t)
230 231

(deftest subsetp.error.5
232 233
  (signals-error (subsetp nil nil 1 2) program-error)
  t)
234 235

(deftest subsetp.error.6
236 237
  (signals-error (subsetp nil nil :bad t :allow-other-keys nil) program-error)
  t)
238 239

(deftest subsetp.error.7
240 241
  (signals-error (subsetp (list 1 2) (list 3 4) :test #'identity) program-error)
  t)
242 243

(deftest subsetp.error.8
244 245
  (signals-error (subsetp (list 1 2) (list 3 4) :test-not #'identity) program-error)
  t)
246 247

(deftest subsetp.error.9
248 249
  (signals-error (subsetp (list 1 2) (list 3 4) :key #'cons) program-error)
  t)
250 251

(deftest subsetp.error.10
252 253
  (signals-error (subsetp (list 1 2) (list 3 4) :key #'car) type-error)
  t)
254 255

(deftest subsetp.error.11
256 257
  (signals-error (subsetp (list 1 2 3) (list* 4 5 6)) type-error)
  t)
258 259

(deftest subsetp.error.12
260 261
  (signals-error (subsetp (list* 1 2 3) (list 1 2 3 4 5 6)) type-error)
  t)
262 263 264 265 266 267 268 269 270

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

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