0.pre8.99:
[sbcl.git] / tests / type.impure.lisp
1 ;;;; This software is part of the SBCL system. See the README file for
2 ;;;; more information.
3 ;;;;
4 ;;;; While most of SBCL is derived from the CMU CL system, the test
5 ;;;; files (like this one) were written from scratch after the fork
6 ;;;; from CMU CL.
7 ;;;; 
8 ;;;; This software is in the public domain and is provided with
9 ;;;; absolutely no warranty. See the COPYING and CREDITS files for
10 ;;;; more information.
11
12 (load "assertoid.lisp")
13 (use-package "ASSERTOID")
14
15 (defmacro assert-nil-nil (expr)
16   `(assert (equal '(nil nil) (multiple-value-list ,expr))))
17 (defmacro assert-nil-t (expr)
18   `(assert (equal '(nil t) (multiple-value-list ,expr))))
19 (defmacro assert-t-t (expr)
20   `(assert (equal '(t t) (multiple-value-list ,expr))))
21
22 (defmacro assert-t-t-or-uncertain (expr)
23   `(assert (let ((list (multiple-value-list ,expr)))
24              (or (equal '(nil nil) list)
25                  (equal '(t t) list)))))
26
27 (let ((types '(character
28                integer fixnum (integer 0 10)
29                single-float (single-float -1.0 1.0) (single-float 0.1)
30                (real 4 8) (real -1 7) (real 2 11)
31                null symbol keyword
32                (member #\a #\b #\c) (member 1 #\a) (member 3.0 3.3)
33                (integer -1 1)
34                unsigned-byte
35                (rational -1 7) (rational -2 4)
36                ratio
37                )))
38   (dolist (i types)
39     (format t "type I=~S~%" i)
40     (dolist (j types)
41       (format t "  type J=~S~%" j)
42       (assert (subtypep i `(or ,i ,j)))
43       (assert (subtypep i `(or ,j ,i)))
44       (assert (subtypep i `(or ,i ,i ,j)))
45       (assert (subtypep i `(or ,j ,i)))
46       (dolist (k types)
47         (format t "    type K=~S~%" k)
48         (assert (subtypep `(or ,i ,j) `(or ,i ,j ,k)))
49         (assert (subtypep `(or ,i ,j) `(or ,k ,j ,i)))))))
50
51 ;;; gotchas that can come up in handling subtypeness as "X is a
52 ;;; subtype of Y if each of the elements of X is a subtype of Y"
53 (let ((subtypep-values (multiple-value-list
54                         (subtypep '(single-float -1.0 1.0)
55                                   '(or (real -100.0 0.0)
56                                        (single-float 0.0 100.0))))))
57   (assert (member subtypep-values
58                   '(;; The system isn't expected to
59                     ;; understand the subtype relationship.
60                     (nil nil)
61                     ;; But if it does, that'd be neat.
62                     (t t)
63                     ;; (And any other return would be wrong.)
64                     )
65                   :test #'equal)))
66
67 (defun type-evidently-= (x y)
68   (and (subtypep x y)
69        (subtypep y x)))
70
71 (assert (subtypep 'single-float 'float))
72
73 (assert (type-evidently-= '(integer 0 10) '(or (integer 0 5) (integer 4 10))))
74
75 ;;; Bug 50(c,d): numeric types with empty ranges should be NIL
76 (assert (type-evidently-= 'nil '(integer (0) (0))))
77 (assert (type-evidently-= 'nil '(rational (0) (0))))
78 (assert (type-evidently-= 'nil '(float (0.0) (0.0))))
79
80 ;;; sbcl-0.6.10 did (UPGRADED-ARRAY-ELEMENT-TYPE 'SOME-UNDEF-TYPE)=>T
81 ;;; and (UPGRADED-COMPLEX-PART-TYPE 'SOME-UNDEF-TYPE)=>T.
82 (assert (raises-error? (upgraded-array-element-type 'some-undef-type)))
83 (assert (eql (upgraded-array-element-type t) t))
84 (assert (raises-error? (upgraded-complex-part-type 'some-undef-type)))
85 (assert (subtypep (upgraded-complex-part-type 'fixnum) 'real))
86
87 ;;; Do reasonable things with undefined types, and with compound types
88 ;;; built from undefined types.
89 ;;;
90 ;;; part I: TYPEP
91 (assert (typep #(11) '(simple-array t 1)))
92 (assert (typep #(11) '(simple-array (or integer symbol) 1)))
93 (assert (raises-error? (typep #(11) '(simple-array undef-type 1))))
94 (assert (not (typep 11 '(simple-array undef-type 1))))
95 ;;; part II: SUBTYPEP
96
97 (assert (subtypep '(vector some-undef-type) 'vector))
98 (assert (not (subtypep '(vector some-undef-type) 'integer)))
99 (assert-nil-nil (subtypep 'utype-1 'utype-2))
100 (assert-nil-nil (subtypep '(vector utype-1) '(vector utype-2)))
101 (assert-nil-nil (subtypep '(vector utype-1) '(vector t)))
102 (assert-nil-nil (subtypep '(vector t) '(vector utype-2)))
103
104 ;;; ANSI specifically disallows bare AND and OR symbols as type specs.
105 #| ; Alas, this is part of bug 10, still unfixed as of sbcl-0.7.2.
106 (assert (raises-error? (typep 11 'and)))
107 (assert (raises-error? (typep 11 'or)))
108 |#
109 ;;; Of course empty lists of subtypes are still OK.
110 (assert (typep 11 '(and)))
111 (assert (not (typep 11 '(or))))
112
113 ;;; bug 12: type system didn't grok nontrivial intersections
114 (assert (subtypep '(and symbol (satisfies keywordp)) 'symbol))
115 (assert (not (subtypep '(and symbol (satisfies keywordp)) 'null)))
116 (assert (subtypep 'keyword 'symbol))
117 (assert (not (subtypep 'symbol 'keyword)))
118 (assert (subtypep 'ratio 'real))
119 (assert (subtypep 'ratio 'number))
120
121 ;;; bug 50.g: Smarten up hairy type specifiers slightly. We may wish
122 ;;; to revisit this, perhaps by implementing a COMPLEMENT type
123 ;;; (analogous to UNION and INTERSECTION) to take the logic out of the
124 ;;; HAIRY domain.
125 (assert-nil-t (subtypep 'atom 'cons))
126 (assert-nil-t (subtypep 'cons 'atom))
127 ;;; These two are desireable but not necessary for ANSI conformance;
128 ;;; maintenance work on other parts of the system broke them in
129 ;;; sbcl-0.7.13.11 -- CSR
130 #+nil
131 (assert-nil-t (subtypep '(not list) 'cons))
132 #+nil
133 (assert-nil-t (subtypep '(not float) 'single-float))
134 (assert-t-t (subtypep '(not atom) 'cons))
135 (assert-t-t (subtypep 'cons '(not atom)))
136 ;;; ANSI requires that SUBTYPEP relationships among built-in primitive
137 ;;; types never be uncertain, i.e. never return NIL as second value.
138 ;;; Prior to about sbcl-0.7.2.6, ATOM caused a lot of problems here
139 ;;; (because it's a negation type, implemented as a HAIRY-TYPE, and
140 ;;; CMU CL's HAIRY-TYPE logic punted a lot).
141 (assert-t-t (subtypep 'integer 'atom))
142 (assert-t-t (subtypep 'function 'atom))
143 (assert-nil-t (subtypep 'list 'atom))
144 (assert-nil-t (subtypep 'atom 'integer))
145 (assert-nil-t (subtypep 'atom 'function))
146 (assert-nil-t (subtypep 'atom 'list))
147 ;;; ATOM is equivalent to (NOT CONS):
148 (assert-t-t (subtypep 'integer '(not cons)))
149 (assert-nil-t (subtypep 'list '(not cons)))
150 (assert-nil-t (subtypep '(not cons) 'integer))
151 (assert-nil-t (subtypep '(not cons) 'list))
152 ;;; And we'd better check that all the named types are right. (We also
153 ;;; do some more tests on ATOM here, since once CSR experimented with
154 ;;; making it a named type.)
155 (assert-t-t (subtypep 'nil 'nil))
156 (assert-t-t (subtypep 'nil 'atom))
157 (assert-t-t (subtypep 'nil 't))
158 (assert-nil-t (subtypep 'atom 'nil))
159 (assert-t-t (subtypep 'atom 'atom))
160 (assert-t-t (subtypep 'atom 't))
161 (assert-nil-t (subtypep 't 'nil))
162 (assert-nil-t (subtypep 't 'atom))
163 (assert-t-t (subtypep 't 't))
164 ;;; Also, LIST is now somewhat special, in that (NOT LIST) should be
165 ;;; recognized as a subtype of ATOM:
166 (assert-t-t (subtypep '(not list) 'atom))
167 (assert-nil-t (subtypep 'atom '(not list)))
168 ;;; These used to fail, because when the two arguments to subtypep are
169 ;;; of different specifier-type types (e.g. HAIRY and UNION), there
170 ;;; are two applicable type methods -- in this case
171 ;;; HAIRY-COMPLEX-SUBTYPEP-ARG1-TYPE-METHOD and
172 ;;; UNION-COMPLEX-SUBTYPEP-ARG2-TYPE-METHOD. Both of these exist, but
173 ;;; [!%]INVOKE-TYPE-METHOD aren't smart enough to know that if one of
174 ;;; them returns NIL, NIL (indicating uncertainty) it should try the
175 ;;; other. However, as of sbcl-0.7.2.6 or so, CALL-NEXT-METHOD-ish
176 ;;; logic in those type methods fixed it.
177 (assert-nil-t (subtypep '(not cons) 'list))
178 (assert-nil-t (subtypep '(not single-float) 'float))
179 ;;; Somewhere along the line (probably when adding CALL-NEXT-METHOD-ish
180 ;;; logic in SUBTYPEP type methods) we fixed bug 58 too:
181 (assert-t-t (subtypep '(and zilch integer) 'zilch))
182 (assert-t-t (subtypep '(and integer zilch) 'zilch))
183
184 ;;; Bug 84: SB-KERNEL:CSUBTYPEP was a bit enthusiastic at
185 ;;; special-casing calls to subtypep involving *EMPTY-TYPE*,
186 ;;; corresponding to the NIL type-specifier; we were bogusly returning
187 ;;; NIL, T (indicating surety) for the following:
188 (assert-nil-nil (subtypep '(satisfies some-undefined-fun) 'nil))
189
190 ;;; It turns out that, as of sbcl-0.7.2, we require to be able to
191 ;;; detect this to compile src/compiler/node.lisp (and in particular,
192 ;;; the definition of the component structure). Since it's a sensible
193 ;;; thing to want anyway, let's test for it here:
194 (assert-t-t (subtypep '(or some-undefined-type (member :no-ir2-yet :dead))
195                       '(or some-undefined-type (member :no-ir2-yet :dead))))
196 ;;; BUG 158 (failure to compile loops with vector references and
197 ;;; increments of greater than 1) was a symptom of type system
198 ;;; uncertainty, to wit:
199 (assert-t-t (subtypep '(and (mod 536870911) (or (integer 0 0) (integer 2 536870912)))
200                       '(mod 536870911))) ; aka SB-INT:INDEX.
201 ;;; floating point types can be tricky.
202 (assert-t-t (subtypep '(member 0.0) '(single-float 0.0 0.0)))
203 (assert-t-t (subtypep '(member -0.0) '(single-float 0.0 0.0)))
204 (assert-t-t (subtypep '(member 0.0) '(single-float -0.0 0.0)))
205 (assert-t-t (subtypep '(member -0.0) '(single-float 0.0 -0.0)))
206 (assert-t-t (subtypep '(member 0.0d0) '(double-float 0.0d0 0.0d0)))
207 (assert-t-t (subtypep '(member -0.0d0) '(double-float 0.0d0 0.0d0)))
208 (assert-t-t (subtypep '(member 0.0d0) '(double-float -0.0d0 0.0d0)))
209 (assert-t-t (subtypep '(member -0.0d0) '(double-float 0.0d0 -0.0d0)))
210
211 (assert-nil-t (subtypep '(single-float 0.0 0.0) '(member 0.0)))
212 (assert-nil-t (subtypep '(single-float 0.0 0.0) '(member -0.0)))
213 (assert-nil-t (subtypep '(single-float -0.0 0.0) '(member 0.0)))
214 (assert-nil-t (subtypep '(single-float 0.0 -0.0) '(member -0.0)))
215 (assert-nil-t (subtypep '(double-float 0.0d0 0.0d0) '(member 0.0d0)))
216 (assert-nil-t (subtypep '(double-float 0.0d0 0.0d0) '(member -0.0d0)))
217 (assert-nil-t (subtypep '(double-float -0.0d0 0.0d0) '(member 0.0d0)))
218 (assert-nil-t (subtypep '(double-float 0.0d0 -0.0d0) '(member -0.0d0)))
219
220 (assert-t-t (subtypep '(member 0.0 -0.0) '(single-float 0.0 0.0)))
221 (assert-t-t (subtypep '(single-float 0.0 0.0) '(member 0.0 -0.0)))
222 (assert-t-t (subtypep '(member 0.0d0 -0.0d0) '(double-float 0.0d0 0.0d0)))
223 (assert-t-t (subtypep '(double-float 0.0d0 0.0d0) '(member 0.0d0 -0.0d0)))
224
225 (assert-t-t (subtypep '(not (single-float 0.0 0.0)) '(not (member 0.0))))
226 (assert-t-t (subtypep '(not (double-float 0.0d0 0.0d0)) '(not (member 0.0d0))))
227
228 (assert-t-t (subtypep '(float -0.0) '(float 0.0)))
229 (assert-t-t (subtypep '(float 0.0) '(float -0.0)))
230 (assert-t-t (subtypep '(float (0.0)) '(float (-0.0))))
231 (assert-t-t (subtypep '(float (-0.0)) '(float (0.0))))
232 \f
233 ;;;; Douglas Thomas Crosher rewrote the CMU CL type test system to
234 ;;;; allow inline type tests for CONDITIONs and STANDARD-OBJECTs, and
235 ;;;; generally be nicer, and Martin Atzmueller ported the patches.
236 ;;;; They look nice but they're nontrivial enough that it's not
237 ;;;; obvious from inspection that everything is OK. Let's make sure
238 ;;;; that things still basically work.
239
240 ;; structure type tests setup
241 (defstruct structure-foo1)
242 (defstruct (structure-foo2 (:include structure-foo1))
243   x)
244 (defstruct (structure-foo3 (:include structure-foo2)))
245 (defstruct (structure-foo4 (:include structure-foo3))
246   y z)
247
248 ;; structure-class tests setup
249 (defclass structure-class-foo1 () () (:metaclass cl:structure-class))
250 (defclass structure-class-foo2 (structure-class-foo1)
251   () (:metaclass cl:structure-class))
252 (defclass structure-class-foo3 (structure-class-foo2)
253   () (:metaclass cl:structure-class))
254 (defclass structure-class-foo4 (structure-class-foo3)
255   () (:metaclass cl:structure-class))
256
257 ;; standard-class tests setup
258 (defclass standard-class-foo1 () () (:metaclass cl:standard-class))
259 (defclass standard-class-foo2 (standard-class-foo1)
260   () (:metaclass cl:standard-class))
261 (defclass standard-class-foo3 (standard-class-foo2)
262   () (:metaclass cl:standard-class))
263 (defclass standard-class-foo4 (standard-class-foo3)
264   () (:metaclass cl:standard-class))
265
266 ;; condition tests setup
267 (define-condition condition-foo1 (condition) ())
268 (define-condition condition-foo2 (condition-foo1) ())
269 (define-condition condition-foo3 (condition-foo2) ())
270 (define-condition condition-foo4 (condition-foo3) ())
271
272 ;;; inline type tests
273 (format t "~&/setting up *TESTS-OF-INLINE-TYPE-TESTS*~%")
274 (defparameter *tests-of-inline-type-tests*
275   '(progn
276
277      ;; structure type tests
278      (assert (typep (make-structure-foo3) 'structure-foo2))
279      (assert (not (typep (make-structure-foo1) 'structure-foo4)))
280      (assert (typep (nth-value 1
281                                (ignore-errors (structure-foo2-x
282                                                (make-structure-foo1))))
283                     'type-error))
284      (assert (null (ignore-errors
285                      (setf (structure-foo2-x (make-structure-foo1)) 11))))
286
287      ;; structure-class tests
288      (assert (typep (make-instance 'structure-class-foo3)
289                     'structure-class-foo2))
290      (assert (not (typep (make-instance 'structure-class-foo1)
291                          'structure-class-foo4)))
292      (assert (null (ignore-errors
293                      (setf (slot-value (make-instance 'structure-class-foo1)
294                                        'x)
295                            11))))
296
297      ;; standard-class tests
298      (assert (typep (make-instance 'standard-class-foo3)
299                     'standard-class-foo2))
300      (assert (not (typep (make-instance 'standard-class-foo1)
301                          'standard-class-foo4)))
302      (assert (null (ignore-errors
303                      (setf (slot-value (make-instance 'standard-class-foo1) 'x)
304                            11))))
305
306      ;; condition tests
307      (assert (typep (make-condition 'condition-foo3)
308                     'condition-foo2))
309      (assert (not (typep (make-condition 'condition-foo1)
310                          'condition-foo4)))
311      (assert (null (ignore-errors
312                      (setf (slot-value (make-condition 'condition-foo1) 'x)
313                            11))))
314      (assert (subtypep 'error 't))
315      (assert (subtypep 'simple-condition 'condition))
316      (assert (subtypep 'simple-error 'simple-condition))
317      (assert (subtypep 'simple-error 'error))
318      (assert (not (subtypep 'condition 'simple-condition)))
319      (assert (not (subtypep 'error 'simple-error)))
320      (assert (eq (car (sb-pcl:class-direct-superclasses
321                        (find-class 'simple-condition)))
322                  (find-class 'condition)))
323
324     (let ((subclasses (mapcar #'find-class
325                               '(simple-type-error
326                                 simple-error
327                                 simple-warning
328                                 sb-int:simple-file-error
329                                 sb-int:simple-style-warning))))
330       (assert (null (set-difference
331                      (sb-pcl:class-direct-subclasses (find-class
332                                                       'simple-condition))
333                      subclasses))))
334
335      ;; precedence lists
336      (assert (equal (sb-pcl:class-precedence-list
337                      (find-class 'simple-condition))
338                     (mapcar #'find-class '(simple-condition
339                                            condition
340                                            sb-kernel:instance
341                                            t))))
342
343      ;; stream classes
344      (assert (equal (sb-pcl:class-direct-superclasses (find-class
345                                                        'fundamental-stream))
346                     (mapcar #'find-class '(standard-object stream))))
347      (assert (null (set-difference
348                     (sb-pcl:class-direct-subclasses (find-class
349                                                      'fundamental-stream))
350                     (mapcar #'find-class '(fundamental-binary-stream
351                                            fundamental-character-stream
352                                            fundamental-output-stream
353                                            fundamental-input-stream)))))
354      (assert (equal (sb-pcl:class-precedence-list (find-class
355                                                    'fundamental-stream))
356                     (mapcar #'find-class '(fundamental-stream
357                                            standard-object
358                                            sb-pcl::std-object
359                                            sb-pcl::slot-object
360                                            stream
361                                            sb-kernel:instance
362                                            t))))
363      (assert (equal (sb-pcl:class-precedence-list (find-class
364                                                    'fundamental-stream))
365                     (mapcar #'find-class '(fundamental-stream
366                                            standard-object
367                                            sb-pcl::std-object
368                                            sb-pcl::slot-object stream
369                                            sb-kernel:instance t))))
370      (assert (subtypep (find-class 'stream) (find-class t)))
371      (assert (subtypep (find-class 'fundamental-stream) 'stream))
372      (assert (not (subtypep 'stream 'fundamental-stream)))))
373 ;;; Test under the interpreter.
374 (eval *tests-of-inline-type-tests*)
375 (format t "~&/done with interpreted *TESTS-OF-INLINE-TYPE-TESTS*~%")
376 ;;; Test under the compiler.
377 (defun tests-of-inline-type-tests ()
378   #.*tests-of-inline-type-tests*)
379 (tests-of-inline-type-tests)
380 (format t "~&/done with compiled (TESTS-OF-INLINE-TYPE-TESTS)~%")
381 \f
382 ;;; Redefinition of classes should alter the type hierarchy (BUG 140):
383 (defclass superclass () ())
384 (defclass maybe-subclass () ())
385 (assert-nil-t (subtypep 'maybe-subclass 'superclass))
386 (defclass maybe-subclass (superclass) ())
387 (assert-t-t (subtypep 'maybe-subclass 'superclass))
388 (defclass maybe-subclass () ())
389 (assert-nil-t (subtypep 'maybe-subclass 'superclass))
390 \f
391 ;;; Prior to sbcl-0.7.6.27, there was some confusion in ARRAY types
392 ;;; specialized on some as-yet-undefined type which would cause this
393 ;;; program to fail (bugs #123 and #165). Verify that it doesn't.
394 (defun foo (x)
395   (declare (type (vector bar) x))
396   (aref x 1))
397 (deftype bar () 'single-float)
398 (assert (eql (foo (make-array 3 :element-type 'bar :initial-element 0.0f0))
399              0.0f0))
400 \f
401 ;;; success
402 (quit :unix-status 104)