Better handling of children deletion in delete-lambda.
[sbcl.git] / tests / type.before-xc.lisp
blob37a3e80be31c88adc7f34e9bfff7ced08913194d
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
5 ;;;; more information.
6 ;;;;
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
9 ;;;; from CMU CL.
10 ;;;;
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))
70 *empty-type*)))
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))))
76 #+nil ; not any more
77 (assert (type= (specifier-type 'sequence)
78 (type-union (specifier-type 'list) (specifier-type 'vector))))
79 #+nil ; not any more
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
94 null
95 (satisfies keywordp)
96 (satisfies foo)
97 (not fixnum)
98 (not null)
99 (and symbol (satisfies foo))
100 (and (satisfies foo) string)
101 (or symbol sequence)
102 (or single-float character)
103 (or float (satisfies bar))
104 integer (integer 0 1)
105 character standard-char
106 (member 1 2 3)))
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))
144 nil))
145 (assert-secondnil (subtypep '(or (satisfies foo) (satisfies bar))
146 nil)))
148 ;;; tests of 2-value quantifieroids FOO/TYPE
149 (macrolet ((2= (v1 v2 expr2)
150 (let ((x1 (gensym))
151 (x2 (gensym)))
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
156 (xsubtypep (x y)
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)
213 (single-float 0.1)))
214 (specifier-type '(or (real -1 7)
215 (single-float 0.1)
216 (single-float -1.0 1.0)))))
217 (assert (not (csubtypep (specifier-type '(or (real -1 7)
218 (single-float 0.1)
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)
232 (assert yes)
233 (assert win))
234 (multiple-value-bind (yes win)
235 (subtypep 'symbol 'instance)
236 (assert (not yes))
237 (assert win))
238 (multiple-value-bind (yes win)
239 (subtypep 'package 'funcallable-instance)
240 (assert (not yes))
241 (assert win))
242 (multiple-value-bind (yes win)
243 (subtypep 'symbol 'funcallable-instance)
244 (assert (not yes))
245 (assert win))
246 (multiple-value-bind (yes win)
247 (subtypep 'funcallable-instance 'function)
248 (assert yes)
249 (assert win))
250 (multiple-value-bind (yes win)
251 (subtypep 'array 'instance)
252 (assert (not yes))
253 (assert win))
254 (multiple-value-bind (yes win)
255 (subtypep 'character 'instance)
256 (assert (not yes))
257 (assert win))
258 (multiple-value-bind (yes win)
259 (subtypep 'number 'instance)
260 (assert (not yes))
261 (assert win))
262 (multiple-value-bind (yes win)
263 (subtypep 'package '(and (or symbol package) instance))
264 (assert yes)
265 (assert win))
266 (multiple-value-bind (yes win)
267 (subtypep '(and (or double-float integer) instance) 'nil)
268 (assert yes)
269 (assert win))
270 (multiple-value-bind (yes win)
271 (subtypep '(and (or double-float integer) funcallable-instance) 'nil)
272 (assert yes)
273 (assert win))
274 (multiple-value-bind (yes win) (subtypep 'instance 'type-specifier)
275 (assert (not yes))
276 (assert (not win)))
277 (multiple-value-bind (yes win) (subtypep 'type-specifier 'instance)
278 (assert (not yes))
279 (assert win))
280 (multiple-value-bind (yes win) (subtypep 'class 'type-specifier)
281 (assert yes)
282 (assert win))
283 (multiple-value-bind (yes win) (subtypep 'classoid 'type-specifier)
284 (assert yes)
285 (assert win))
286 (multiple-value-bind (yes win)
287 (subtypep '(and (function (t)) funcallable-instance) 'nil)
288 (assert (not win))
289 (assert (not yes)))
290 (multiple-value-bind (yes win)
291 (subtypep '(and fixnum function) 'nil)
292 (assert yes)
293 (assert win))
294 (multiple-value-bind (yes win)
295 (subtypep '(and fixnum hash-table) 'nil)
296 (assert yes)
297 (assert win))
298 (multiple-value-bind (yes win)
299 (subtypep '(function) '(function (t &rest t)))
300 (assert (not yes))
301 (assert win))
302 ;; Used to run out of stack.
303 (multiple-value-bind (yes win)
304 (subtypep 'null '(or unk0 unk1))
305 (assert (not yes))
306 (assert (not win)))
308 (multiple-value-bind (yes win)
309 (subtypep '(and function instance) nil)
310 (assert yes)
311 (assert win))
312 (multiple-value-bind (yes win)
313 (subtypep nil '(and function instance))
314 (assert yes)
315 (assert win))
316 (multiple-value-bind (yes win)
317 (subtypep '(and function funcallable-instance) 'funcallable-instance)
318 (assert yes)
319 (assert win))
320 (multiple-value-bind (yes win)
321 (subtypep 'funcallable-instance '(and function funcallable-instance))
322 (assert yes)
323 (assert win))
324 (multiple-value-bind (yes win)
325 (subtypep 'stream 'instance)
326 (assert (not win))
327 (assert (not yes)))
328 (multiple-value-bind (yes win)
329 (subtypep 'stream 'funcallable-instance)
330 (assert (not yes))
331 (assert win))
332 (multiple-value-bind (yes win)
333 (subtypep '(and stream instance) 'instance)
334 (assert yes)
335 (assert win))
336 (multiple-value-bind (yes win)
337 (subtypep '(and stream funcallable-instance) 'funcallable-instance)
338 (assert yes)
339 (assert win))
340 (multiple-value-bind (yes win)
341 (subtypep '(and stream instance) 'stream)
342 (assert yes)
343 (assert win))
344 (multiple-value-bind (yes win)
345 (subtypep '(and stream funcallable-instance) 'stream)
346 (assert yes)
347 (assert win))
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)))
361 (assert (eq a b)))))
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)))