subtypep-cons.lsp 10.8 KB
Newer Older
1 2 3 4 5 6 7
;-*- Mode:     Lisp -*-
;;;; Author:   Paul Dietz
;;;; Created:  Sat Feb 15 11:57:03 2003
;;;; Contains: Tests for subtype relationships on cons types

(in-package :cl-test)

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

10 11 12 13
;;; SUBTYPEP on CONS types

(defvar *cons-types*
  '(cons (cons) (cons *) (cons * *) (cons t) (cons t t)
sds's avatar
sds committed
14
         (cons t *) (cons * t)))
15 16 17

(deftest subtypep.cons.1
  (loop for t1 in *cons-types*
sds's avatar
sds committed
18 19 20 21 22 23
        append (loop for t2 in *cons-types*
                     unless (equal (mapcar #'notnot
                                           (multiple-value-list
                                            (subtypep t1 t2)))
                                   '(t t))
                     collect (list t1 t2)))
24 25 26 27
  nil)

(deftest subtypep.cons.2
  (loop for t1 in '((cons nil) (cons nil *) (cons nil t)
sds's avatar
sds committed
28 29 30
                    (cons * nil) (cons t nil) (cons nil nil))
        unless (subtypep t1 nil)
        collect t1)
31 32 33 34
  nil)

(deftest subtypep.cons.3
  (check-equivalence '(and (cons symbol *) (cons * symbol))
sds's avatar
sds committed
35
                     '(cons symbol symbol))
36 37 38 39
  nil)

(deftest subtypep.cons.4
  (check-equivalence '(and (cons (integer 0 10) *)
sds's avatar
sds committed
40 41 42
                           (cons (integer 5 15) (integer 10 20))
                           (cons * (integer 15 25)))
                     '(cons (integer 5 10) (integer 15 20)))
43 44 45 46 47 48
  nil)

(deftest subtypep.cons.5
  (check-equivalence
   '(and cons (not (cons symbol symbol)))
   '(or (cons (not symbol) *)
sds's avatar
sds committed
49
        (cons * (not symbol))))
50 51
  nil)

52 53 54
(deftest subtypep.cons.6
  (check-equivalence
   '(or (cons integer symbol) (cons integer integer)
sds's avatar
sds committed
55
        (cons symbol integer) (cons symbol symbol))
56 57 58 59 60 61
   '(cons (or integer symbol) (or integer symbol)))
  nil)

(deftest subtypep.cons.7
  (check-equivalence
   '(or (cons (integer 0 8) (integer 5 15))
sds's avatar
sds committed
62 63 64
        (cons (integer 0 7) (integer 0 6))
        (cons (integer 6 15) (integer 0 9))
        (cons (integer 3 15) (integer 4 15)))
65 66
   '(cons (integer 0 15) (integer 0 15)))
  nil)
67 68 69 70 71 72 73 74 75 76 77 78 79

(deftest subtypep.cons.8
  (check-equivalence
   '(or
     (cons integer (cons symbol integer))
     (cons symbol (cons integer symbol))
     (cons symbol (cons symbol integer))
     (cons symbol (cons integer integer))
     (cons integer (cons integer symbol))
     (cons symbol (cons symbol symbol))
     (cons integer (cons integer integer))
     (cons integer (cons symbol symbol)))
   '(cons (or symbol integer)
sds's avatar
sds committed
80
          (cons (or symbol integer) (or symbol integer))))
81
  nil)
82 83 84 85 86 87 88 89 90 91 92

(deftest subtypep.cons.9
  (check-equivalence
   '(or
     (cons (integer 0 (3)) (integer 0 (6)))
     (cons (integer 3 (9)) (integer 0 (3)))
     (cons (integer 0 (6)) (integer 6 (9)))
     (cons (integer 6 (9)) (integer 3 (9)))
     (cons (integer 3 (6)) (integer 3 (6))))
   '(cons (integer 0 (9)) (integer 0 (9))))
  nil)
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114

(deftest subtypep.cons.10
  (check-equivalence
   '(or
     (cons (rational 0 (3)) (rational 0 (6)))
     (cons (rational 3 (9)) (rational 0 (3)))
     (cons (rational 0 (6)) (rational 6 (9)))
     (cons (rational 6 (9)) (rational 3 (9)))
     (cons (rational 3 (6)) (rational 3 (6))))
   '(cons (rational 0 (9)) (rational 0 (9))))
  nil)

(deftest subtypep.cons.11
  (check-equivalence
   '(or
     (cons (real 0 (3)) (real 0 (6)))
     (cons (real 3 (9)) (real 0 (3)))
     (cons (real 0 (6)) (real 6 (9)))
     (cons (real 6 (9)) (real 3 (9)))
     (cons (real 3 (6)) (real 3 (6))))
   '(cons (real 0 (9)) (real 0 (9))))
  nil)
115 116 117 118 119

;;; Test suggested by C.R.
(deftest subtypep.cons.12
  (check-all-not-subtypep
   '(cons (or integer symbol)
sds's avatar
sds committed
120
          (or integer symbol))
121
   '(or (cons integer symbol)
sds's avatar
sds committed
122
        (cons symbol integer)))
123
  nil)
124 125 126 127 128 129 130 131 132 133

(deftest subtypep.cons.13
  (check-all-not-subtypep '(not list) 'cons)
  nil)


;;; a -> b, a ==> b
(deftest subtypep.cons.14
  (check-all-subtypep
   '(and (or (cons (not symbol)) (cons * integer))
sds's avatar
sds committed
134
         (cons symbol))
135 136 137 138 139 140 141
   '(cons * integer))
  nil)

;;; a -> b, not b ==> not a
(deftest subtypep.cons.15
  (check-all-subtypep
   '(and (or (cons (not symbol)) (cons * integer))
sds's avatar
sds committed
142
         (cons * (not integer)))
143 144 145 146 147 148 149
   '(cons (not symbol)))
  nil)

;;; (and (or a b) (or (not b) c)) ==> (or a c)
(deftest subtypep.cons.16
  (check-all-subtypep
   '(and (or (cons symbol (cons * *))
sds's avatar
sds committed
150 151 152
             (cons * (cons integer *)))
         (or (cons * (cons (not integer) *))
             (cons * (cons * float))))
153
   '(or (cons symbol (cons * *))
sds's avatar
sds committed
154
        (cons * (cons * float))))
155 156 157 158 159
  nil)

(deftest subtypep.cons.17
  (check-all-subtypep
   '(and (or (cons symbol (cons * *))
sds's avatar
sds committed
160 161 162 163 164
             (cons * (cons integer *)))
         (or (cons * (cons (not integer)))
             (cons * (cons * float)))
         (or (cons * (cons * (not float)))
             (cons symbol (cons * *))))
165 166 167 168 169 170 171
   '(cons symbol))
  nil)

(deftest subtypep.cons.18
  (check-all-subtypep
   '(cons symbol)
   '(or (cons symbol (not integer))
sds's avatar
sds committed
172
        (cons * integer)))
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
  nil)

(deftest subtypep.cons.19
  (check-equivalence
   '(or
     (cons (eql a) (eql x))
     (cons (eql b) (eql y))
     (cons (eql c) (eql z))
     (cons (eql a) (eql y))
     (cons (eql b) (eql z))
     (cons (eql c) (eql x))
     (cons (eql a) (eql z))
     (cons (eql b) (eql x))
     (cons (eql c) (eql y)))
   '(cons (member a b c) (member x y z)))
  nil)

(deftest subtypep.cons.20
  (check-equivalence
   '(or
     (cons (eql a) (eql x))
     (cons (eql b) (eql y))
     (cons (eql a) (eql y))
     (cons (eql b) (eql z))
     (cons (eql c) (eql x))
     (cons (eql a) (eql z))
     (cons (eql b) (eql x))
     (cons (eql c) (eql y)))
   '(and (cons (member a b c) (member x y z))
sds's avatar
sds committed
202
         (not (cons (eql c) (eql z)))))
203 204
  nil)

205 206 207 208 209 210 211 212 213 214 215 216
;;; Test case that came up in SBCL
(deftest subtypep.cons.21
  (check-all-subtypep
   '(cons integer single-float)
   '(or (cons fixnum single-float) (cons bignum single-float)))
  nil)

(deftest subtypep.cons.22
  (check-all-subtypep
   '(cons single-float integer)
   '(or (cons single-float fixnum) (cons single-float bignum)))
  nil)
217 218 219 220 221

;;; More test cases from SBCL, CMUCL, culled from random test failures

(deftest subtype.cons.23
  (let ((t1 '(cons t (cons (not long-float) symbol)))
sds's avatar
sds committed
222
        (t2 '(not (cons symbol (cons integer integer)))))
223 224 225 226 227
    (subtypep-and-contrapositive-are-consistent t1 t2))
  t)

(deftest subtype.cons.24
  (let ((t1 '(cons (eql 3671) (cons short-float (eql -663423073525))))
sds's avatar
sds committed
228
        (t2 '(not (cons t (cons (not complex) (cons integer t))))))
229 230 231 232 233
    (subtypep-and-contrapositive-are-consistent t1 t2))
  t)

(deftest subtype.cons.25
  (let ((t1 '(cons t (cons (not long-float) (integer 44745969 61634129))))
sds's avatar
sds committed
234
        (t2 '(not (cons (eql -3) (cons short-float (cons t float))))))
235 236 237 238 239
    (subtypep-and-contrapositive-are-consistent t1 t2))
  t)

(deftest subtype.cons.26
  (let ((t1 '(cons integer (cons single-float (cons t t))))
sds's avatar
sds committed
240
        (t2 '(cons t (cons (not complex) (not (eql 8))))))
241 242 243 244 245
    (subtypep-and-contrapositive-are-consistent t1 t2))
  t)

(deftest subtype.cons.27
  (let ((t1 '(cons (not (integer -27 30))
sds's avatar
sds committed
246 247
                   (cons rational (cons integer integer))))
        (t2 '(not (cons integer (cons integer (eql 378132631))))))
248 249 250 251 252
    (subtypep-and-contrapositive-are-consistent t1 t2))
  t)

(deftest subtype.cons.28
  (let ((t1 '(cons (integer -1696888 -1460338)
sds's avatar
sds committed
253 254 255
                   (cons single-float symbol)))
        (t2 '(not (cons (not (integer -14 20))
                        (cons (not integer) cons)))))
256 257 258
    (subtypep-and-contrapositive-are-consistent t1 t2))
  t)

259 260
(deftest subtypep.cons.29
  (let ((t2 '(or (not (cons unsigned-byte cons))
sds's avatar
sds committed
261
                 (not (cons (integer -6 22) rational)))))
262 263
    (subtypep-and-contrapositive-are-consistent 'cons t2))
  t)
264

265 266
(deftest subtypep.cons.30
  (let ((t1 '(not (cons t (cons t (cons cons t)))))
sds's avatar
sds committed
267 268 269 270
        (t2 '(or (or (cons (cons t integer) t)
                     (not (cons t (cons t cons))))
                 (not (cons (cons (eql -27111309) t)
                            (cons t (eql 1140730)))))))
271 272
    (subtypep-and-contrapositive-are-consistent t1 t2))
  t)
273

274 275
(deftest subtypep.cons.31
  (let ((t2 '(or
sds's avatar
sds committed
276 277 278 279 280 281 282
              (not
               (cons (or (cons t ratio) (cons short-float t))
                     (cons (cons (eql -7418623) (integer -9 53))
                           (cons cons t))))
              (not
               (cons (cons t (eql -265039))
                     (cons (cons t cons) t))))))
283 284
    (subtypep-and-contrapositive-are-consistent 'cons t2))
  t)
285 286 287

(deftest subtypep.cons.32
  (let ((t2 '(cons t
sds's avatar
sds committed
288 289
                   (or (not (cons integer (eql 0)))
                       (not (cons (or float (eql 0)) cons))))))
290 291 292 293 294
    (subtypep-and-contrapositive-are-consistent 'cons t2))
  t)

(deftest subtypep.cons.33
  (let ((t2 '(or (not (cons (cons t cons) (cons t (cons unsigned-byte t))))
sds's avatar
sds committed
295
                 (not (cons (cons integer t) (cons t (cons cons t)))))))
296 297 298 299 300
    (subtypep-and-contrapositive-are-consistent 'cons t2))
  t)

(deftest subtypep.cons.34
  (let ((t2 '(or (not (cons (or (eql 0) ratio) (not cons)))
sds's avatar
sds committed
301
                 (not (cons integer cons)))))
302 303
    (subtypep-and-contrapositive-are-consistent 'cons t2))
  t)
304 305 306 307 308 309 310 311 312 313 314 315 316

(deftest subtypep.cons.35
  (notnot-mv (subtypep '(cons nil t) 'float))
  t t)

(deftest subtypep.cons.36
  (notnot-mv (subtypep '(cons t nil) 'symbol))
  t t)

(deftest subtypep.cons.37
  (notnot-mv (subtypep '(cons nil nil) 'real))
  t t)

317 318
(deftest subtypep.cons.38
  (let ((t1 '(cons t (complex (real -32 0))))
sds's avatar
sds committed
319
        (t2 `(not (cons t (complex (integer * -500))))))
320 321
    (subtypep-and-contrapositive-are-consistent t1 t2))
  t)
pfdietz's avatar
pfdietz committed
322 323 324 325 326 327

;;; From GCL

(deftest subtypep.cons.39
  (values (subtypep t '(and (not (cons cons (cons cons t))) (not (cons t cons)))))
  nil)
328 329 330

(deftest subtypep.cons.40
  (let ((type1 '(cons (eql 0) cons))
sds's avatar
sds committed
331
        (type2 '(cons unsigned-byte symbol)))
332 333 334 335
    (values
     (subtypep* type1 type2)
     (subtypep* `(not ,type2) `(not ,type1))))
  nil nil)
336

337
;;; From sbcl 0.9.5.31
338 339 340

(deftest subtypep.cons.41
  (let ((type1 '(cons t (complex (real -10 -4))))
sds's avatar
sds committed
341
        (type2 '(not (cons t (complex (integer -200 -100))))))
342
    (multiple-value-bind (sub1 success1)
sds's avatar
sds committed
343
        (subtypep* type1 type2)
344
      (multiple-value-bind (sub2 success2)
sds's avatar
sds committed
345 346 347 348
          (subtypep* `(not ,type2) `(not ,type1))
        (if (and success1 success2 (not (eq sub1 sub2)))
            (values sub1 sub2)
            nil))))
349 350
  nil)

351 352
(deftest subtypep.cons.42
    (let ((t1 '(cons (cons (cons (real -744833699 -744833699) cons) (integer -234496 215373))
sds's avatar
sds committed
353 354
                integer))
          (t2 '(cons (cons (cons integer integer) (integer -234496 215373)) t)))
355 356 357
      (values (subtypep `(not ,t2) `(not ,t1))))
  nil)

358 359 360 361
;;;; From sbcl 0.9.6.57

(deftest subtypep.cons.43
  (let* ((n -3.926510009989861d7)
sds's avatar
sds committed
362 363 364 365
         (t1 '(not (cons float t)))
         (t2 `(or (not (cons (eql 0) (real ,n ,n)))
                  (not (cons t (eql 0))))))
    (multiple-value-bind
366 367 368 369 370 371
     (sub1 good1)
     (subtypep* t1 t2)
     (multiple-value-bind
      (sub2 good2)
      (subtypep* `(not ,t2) `(not ,t1))
      (or (not good1)
sds's avatar
sds committed
372 373 374
          (not good2)
          (and sub1 sub2)
          (and (not sub1) (not sub2))))))
375 376
  t)