1 ;;;; tests of the type system, intended to be executed as soon as
2 ;;;; the cross-compiler is built
4 ;;;; This software is part of the SBCL system. See the README file for
7 ;;;; While most of SBCL is derived from the CMU CL system, the test
8 ;;;; files (like this one) were written from scratch after the fork
11 ;;;; This software is in the public domain and is provided with
12 ;;;; absolutely no warranty. See the COPYING and CREDITS files for
13 ;;;; more information.
15 (unless (find-package "BEFORE-XC-TESTS")
16 (make-package "BEFORE-XC-TESTS" :use
'("SB!XC" "SB!KERNEL" "SB!INT")))
17 (do-external-symbols (s "SB!XC") ; Import all symbols from SB!XC, then use CL
18 (shadowing-import s
"BEFORE-XC-TESTS"))
19 (import '(sb!kernel
::type-union2
) "BEFORE-XC-TESTS")
20 (cl:use-package
'("COMMON-LISP") "BEFORE-XC-TESTS")
22 (in-package "BEFORE-XC-TESTS")
24 (assert (type= (specifier-type '(and fixnum
(satisfies foo
)))
25 (specifier-type '(and (satisfies foo
) fixnum
))))
26 (assert (type= (specifier-type '(member 1 2 3))
27 (specifier-type '(member 2 3 1))))
28 (assert (type= (specifier-type '(and (member 1.0 2 3) single-float
))
29 (specifier-type '(member 1.0))))
31 (assert (typep #(1 2 3) 'simple-vector
))
32 (assert (typep #(1 2 3) 'vector
))
33 (assert (not (typep '(1 2 3) 'vector
)))
34 (assert (not (typep 1 'vector
)))
36 (assert (typep '(1 2 3) 'list
))
37 (assert (typep '(1 2 3) 'cons
))
38 (assert (not (typep '(1 2 3) 'null
)))
39 (assert (not (typep "1 2 3" 'list
)))
40 (assert (not (typep 1 'list
)))
42 (assert (typep nil
'null
))
43 (assert (typep nil
'(member nil
)))
44 (assert (typep nil
'(member 1 2 nil
3)))
45 (assert (not (typep nil
'(member 1 2 3))))
47 (assert (type= *empty-type
*
48 (type-intersection (specifier-type 'list
)
49 (specifier-type 'vector
))))
50 (assert (type= *empty-type
*
51 (type-intersection (specifier-type 'list
)
52 (specifier-type 'vector
))))
53 (assert (type= (specifier-type 'null
)
54 (type-intersection (specifier-type 'list
)
55 (specifier-type '(or vector null
)))))
56 (assert (type= (specifier-type 'null
)
57 (type-intersection (specifier-type 'sequence
)
58 (specifier-type 'symbol
))))
59 (assert (type= (specifier-type 'cons
)
60 (type-intersection (specifier-type 'sequence
)
61 (specifier-type '(or cons number
)))))
62 (assert (type= (specifier-type '(simple-array character
(*)))
63 (type-intersection (specifier-type 'sequence
)
64 (specifier-type '(simple-array character
)))))
65 (assert (type= (specifier-type 'list
)
66 (type-intersection (specifier-type 'sequence
)
67 (specifier-type 'list
))))
68 (assert (type= *empty-type
*
69 (type-intersection (specifier-type '(satisfies keywordp
))
72 (assert (type= (specifier-type 'list
)
73 (type-union (specifier-type 'cons
) (specifier-type 'null
))))
74 (assert (type= (specifier-type 'list
)
75 (type-union (specifier-type 'null
) (specifier-type 'cons
))))
77 (assert (type= (specifier-type 'sequence
)
78 (type-union (specifier-type 'list
) (specifier-type 'vector
))))
80 (assert (type= (specifier-type 'sequence
)
81 (type-union (specifier-type 'vector
) (specifier-type 'list
))))
82 (assert (type= (specifier-type 'list
)
83 (type-union (specifier-type 'cons
) (specifier-type 'list
))))
84 (assert (not (csubtypep (type-union (specifier-type 'list
)
85 (specifier-type '(satisfies foo
)))
86 (specifier-type 'list
))))
87 (assert (csubtypep (specifier-type 'list
)
88 (type-union (specifier-type 'list
)
89 (specifier-type '(satisfies foo
)))))
91 ;;; Identities should be identities.
92 (dolist (type-specifier '(nil
99 (and symbol
(satisfies foo
))
100 (and (satisfies foo
) string
)
102 (or single-float character
)
103 (or float
(satisfies bar
))
104 integer
(integer 0 1)
105 character standard-char
107 (let ((ctype (specifier-type type-specifier
)))
109 (assert (type= *empty-type
* (type-intersection ctype
*empty-type
*)))
110 (assert (type= *empty-type
* (type-intersection *empty-type
* ctype
)))
111 (assert (type= *empty-type
* (type-intersection2 ctype
*empty-type
*)))
112 (assert (type= *empty-type
* (type-intersection2 *empty-type
* ctype
)))
114 (assert (type= ctype
(type-intersection ctype
*universal-type
*)))
115 (assert (type= ctype
(type-intersection *universal-type
* ctype
)))
116 (assert (type= ctype
(type-intersection2 ctype
*universal-type
*)))
117 (assert (type= ctype
(type-intersection2 *universal-type
* ctype
)))
119 (assert (type= *universal-type
* (type-union ctype
*universal-type
*)))
120 (assert (type= *universal-type
* (type-union *universal-type
* ctype
)))
121 (assert (type= *universal-type
* (type-union2 ctype
*universal-type
*)))
122 (assert (type= *universal-type
* (type-union2 *universal-type
* ctype
)))
124 (assert (type= ctype
(type-union ctype
*empty-type
*)))
125 (assert (type= ctype
(type-union *empty-type
* ctype
)))
126 (assert (type= ctype
(type-union2 ctype
*empty-type
*)))
127 (assert (type= ctype
(type-union2 *empty-type
* ctype
)))
129 (assert (csubtypep *empty-type
* ctype
))
130 (assert (csubtypep ctype
*universal-type
*))))
132 (assert (subtypep 'simple-vector
'vector
))
133 (assert (subtypep 'simple-vector
'simple-array
))
134 (assert (subtypep 'vector
'array
))
135 (assert (not (subtypep 'vector
'simple-vector
)))
136 (assert (not (subtypep 'vector
'simple-array
)))
138 (macrolet ((assert-secondnil (expr) `(assert (null (nth-value 1 ,expr
)))))
139 (assert-secondnil (subtypep t
'(satisfies foo
)))
140 (assert-secondnil (subtypep t
'(and (satisfies foo
) (satisfies bar
))))
141 (assert-secondnil (subtypep t
'(or (satisfies foo
) (satisfies bar
))))
142 (assert-secondnil (subtypep '(satisfies foo
) nil
))
143 (assert-secondnil (subtypep '(and (satisfies foo
) (satisfies bar
))
145 (assert-secondnil (subtypep '(or (satisfies foo
) (satisfies bar
))
148 ;;; tests of 2-value quantifieroids FOO/TYPE
149 (macrolet ((2= (v1 v2 expr2
)
152 `(multiple-value-bind (,x1
,x2
) ,expr2
153 (unless (and (eql ,x1
,v1
) (eql ,x2
,v2
))
154 (error "mismatch for EXPR2=~S" ',expr2
))))))
155 (flet (;; SUBTYPEP running in the cross-compiler
157 (csubtypep (specifier-type x
)
158 (specifier-type y
))))
159 (2= t t
(any/type
#'xsubtypep
'fixnum
'(real integer
)))
160 (2= t t
(any/type
#'xsubtypep
'fixnum
'(real cons
)))
161 (2= nil t
(any/type
#'xsubtypep
'fixnum
'(cons vector
)))
162 (2= nil nil
(any/type
#'xsubtypep
'fixnum
'(cons some-unknown-type-foo
)))
163 (2= nil nil
(any/type
#'xsubtypep
'fixnum
'(some-unknown-type-foo cons
)))
164 (2= t t
(any/type
#'xsubtypep
'fixnum
'(some-unknown-type-foo real
)))
165 (2= t t
(any/type
#'xsubtypep
'fixnum
'(real some-unknown-type-foo
)))
166 (2= nil t
(any/type
#'xsubtypep
'fixnum
'()))
167 (2= t t
(every/type
#'xsubtypep
'fixnum
'()))
168 (2= nil nil
(every/type
#'xsubtypep
'fixnum
'(real some-unknown-type-foo
)))
169 (2= nil nil
(every/type
#'xsubtypep
'fixnum
'(some-unknown-type-foo real
)))
170 (2= nil t
(every/type
#'xsubtypep
'fixnum
'(some-unknown-type-foo cons
)))
171 (2= nil t
(every/type
#'xsubtypep
'fixnum
'(cons some-unknown-type-foo
)))
172 (2= t t
(every/type
#'xsubtypep
'fixnum
'(real integer
)))
173 (2= nil t
(every/type
#'xsubtypep
'fixnum
'(real cons
)))
174 (2= nil t
(every/type
#'xsubtypep
'fixnum
'(cons vector
)))))
176 ;;; various dead bugs
177 (assert (union-type-p (type-intersection (specifier-type 'list
)
178 (specifier-type '(or list vector
)))))
179 (assert (type= (type-intersection (specifier-type 'list
)
180 (specifier-type '(or list vector
)))
181 (specifier-type 'list
)))
182 (assert (array-type-p (type-intersection (specifier-type 'vector
)
183 (specifier-type '(or list vector
)))))
184 (assert (type= (type-intersection (specifier-type 'vector
)
185 (specifier-type '(or list vector
)))
186 (specifier-type 'vector
)))
187 (assert (type= (type-intersection (specifier-type 'number
)
188 (specifier-type 'integer
))
189 (specifier-type 'integer
)))
190 (assert (null (type-intersection2 (specifier-type 'symbol
)
191 (specifier-type '(satisfies foo
)))))
192 (assert (intersection-type-p (specifier-type '(and symbol
(satisfies foo
)))))
193 (assert (ctypep :x86
(specifier-type '(satisfies keywordp
))))
194 (assert (type= (specifier-type '(member :x86
))
195 (specifier-type '(and (member :x86
) (satisfies keywordp
)))))
196 (let* ((type1 (specifier-type '(member :x86
)))
197 (type2 (specifier-type '(or keyword null
)))
198 (isect (type-intersection type1 type2
)))
199 (assert (type= isect type1
))
200 (assert (type= isect
(type-intersection type2 type1
)))
201 (assert (type= isect
(type-intersection type2 type1 type2
)))
202 (assert (type= isect
(type-intersection type1 type1 type2 type1
)))
203 (assert (type= isect
(type-intersection type1 type2 type1 type2
))))
204 (let* ((type1 (specifier-type 'keyword
))
205 (type2 (specifier-type '(or keyword null
)))
206 (isect (type-intersection type1 type2
)))
207 (assert (type= isect type1
))
208 (assert (type= isect
(type-intersection type2 type1
)))
209 (assert (type= isect
(type-intersection type2 type1 type2
)))
210 (assert (type= isect
(type-intersection type1 type1 type2 type1
)))
211 (assert (type= isect
(type-intersection type1 type2 type1 type2
))))
212 (assert (csubtypep (specifier-type '(or (single-float -
1.0 1.0)
214 (specifier-type '(or (real -
1 7)
216 (single-float -
1.0 1.0)))))
217 (assert (not (csubtypep (specifier-type '(or (real -
1 7)
219 (single-float -
1.0 1.0)))
220 (specifier-type '(or (single-float -
1.0 1.0)
221 (single-float 0.1))))))
223 (assert (typep #\
, 'character
))
224 (assert (typep #\
@ 'character
))
226 (assert (type= (type-intersection (specifier-type '(member #\a #\c
#\e
))
227 (specifier-type '(member #\b #\c
#\f)))
228 (specifier-type '(member #\c
))))
230 (multiple-value-bind (yes win
)
231 (subtypep 'package
'instance
)
234 (multiple-value-bind (yes win
)
235 (subtypep 'symbol
'instance
)
238 (multiple-value-bind (yes win
)
239 (subtypep 'package
'funcallable-instance
)
242 (multiple-value-bind (yes win
)
243 (subtypep 'symbol
'funcallable-instance
)
246 (multiple-value-bind (yes win
)
247 (subtypep 'funcallable-instance
'function
)
250 (multiple-value-bind (yes win
)
251 (subtypep 'array
'instance
)
254 (multiple-value-bind (yes win
)
255 (subtypep 'character
'instance
)
258 (multiple-value-bind (yes win
)
259 (subtypep 'number
'instance
)
262 (multiple-value-bind (yes win
)
263 (subtypep 'package
'(and (or symbol package
) instance
))
266 (multiple-value-bind (yes win
)
267 (subtypep '(and (or double-float integer
) instance
) 'nil
)
270 (multiple-value-bind (yes win
)
271 (subtypep '(and (or double-float integer
) funcallable-instance
) 'nil
)
274 (multiple-value-bind (yes win
) (subtypep 'instance
'type-specifier
)
277 (multiple-value-bind (yes win
) (subtypep 'type-specifier
'instance
)
280 (multiple-value-bind (yes win
) (subtypep 'class
'type-specifier
)
283 (multiple-value-bind (yes win
) (subtypep 'classoid
'type-specifier
)
286 (multiple-value-bind (yes win
)
287 (subtypep '(and (function (t)) funcallable-instance
) 'nil
)
290 (multiple-value-bind (yes win
)
291 (subtypep '(and fixnum function
) 'nil
)
294 (multiple-value-bind (yes win
)
295 (subtypep '(and fixnum hash-table
) 'nil
)
298 (multiple-value-bind (yes win
)
299 (subtypep '(function) '(function (t &rest t
)))
302 ;; Used to run out of stack.
303 (multiple-value-bind (yes win
)
304 (subtypep 'null
'(or unk0 unk1
))
308 (multiple-value-bind (yes win
)
309 (subtypep '(and function instance
) nil
)
312 (multiple-value-bind (yes win
)
313 (subtypep nil
'(and function instance
))
316 (multiple-value-bind (yes win
)
317 (subtypep '(and function funcallable-instance
) 'funcallable-instance
)
320 (multiple-value-bind (yes win
)
321 (subtypep 'funcallable-instance
'(and function funcallable-instance
))
324 (multiple-value-bind (yes win
)
325 (subtypep 'stream
'instance
)
328 (multiple-value-bind (yes win
)
329 (subtypep 'stream
'funcallable-instance
)
332 (multiple-value-bind (yes win
)
333 (subtypep '(and stream instance
) 'instance
)
336 (multiple-value-bind (yes win
)
337 (subtypep '(and stream funcallable-instance
) 'funcallable-instance
)
340 (multiple-value-bind (yes win
)
341 (subtypep '(and stream instance
) 'stream
)
344 (multiple-value-bind (yes win
)
345 (subtypep '(and stream funcallable-instance
) 'stream
)
349 (assert (type= (specifier-type 'nil
)
350 (specifier-type '(and symbol funcallable-instance
))))
352 (assert (not (type= (specifier-type '(function (t) (values &optional
)))
353 (specifier-type '(function (t) (values))))))
355 ;; Assert that these types are interned by parsing each twice,
356 ;; dropping the specifier-type cache in between.
357 (dolist (spec '(index cons null boolean character base-char extended-char
))
358 (let ((a (specifier-type spec
)))
359 (drop-all-hash-caches)
360 (let ((b (specifier-type spec
)))
362 (drop-all-hash-caches)
363 ;; BOOLEAN's deftype lists the members as (T NIL),
364 ;; but it should also be EQ to (MEMBER NIL T)
365 (assert (eq (specifier-type '(member nil t
)) (specifier-type 'boolean
)))