1 ;;;; This software is part of the SBCL system. See the README file for
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
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.
12 (in-package "CL-USER")
15 (declare (notinline mapcar
))
16 (mapcar (lambda (args)
17 (destructuring-bind (obj type-spec result
) args
18 (flet ((matches-result?
(x)
19 (eq (if x t nil
) result
)))
20 (assert (matches-result?
(typep obj type-spec
)))
21 (assert (matches-result?
(sb-kernel:ctypep
23 (sb-kernel:specifier-type
25 '((nil (or null vector
) t
)
26 (nil (or number vector
) nil
)
27 (12 (or null vector
) nil
)
28 (12 (and (or number vector
) real
) t
))))
31 ;;; This test is motivated by bug #195, which previously had (THE REAL
32 ;;; #(1 2 3)) give an error which prints as "This is not a (OR
33 ;;; SINGLE-FLOAT DOUBLE-FLOAT RATIONAL)". We ideally want all of the
34 ;;; defined-by-ANSI types to unparse as themselves or at least
35 ;;; something similar (e.g. CHARACTER can unparse to BASE-CHAR, since
36 ;;; the types are equivalent in current SBCL, and EXTENDED-CHAR can
37 ;;; unparse to NIL, since there are no EXTENDED-CHARs currently).
38 (let ((standard-types '(;; from table 4-2 in section 4.2.3 in the
69 standard-generic-function
127 floating-point-inexact
130 floating-point-invalid-operation
133 floating-point-overflow
135 floating-point-underflow
137 (dolist (type standard-types
)
138 (format t
"~&~S~%" type
)
139 (assert (not (sb-kernel:unknown-type-p
(sb-kernel:specifier-type type
))))
140 (assert (atom (sb-kernel:type-specifier
(sb-kernel:specifier-type type
))))))
142 ;;; a bug underlying the reported bug #221: The SB-KERNEL type code
143 ;;; signalled an error on this expression.
144 (subtypep '(function (fixnum) (values package boolean
))
145 '(function (t) (values package boolean
)))
147 ;;; bug reported by Valtteri Vuorik
148 (compile nil
'(lambda () (member (char "foo" 0) '(#\.
#\
/) :test
#'char
=)))
149 (assert (not (equal (multiple-value-list
150 (subtypep '(function ()) '(function (&rest t
))))
153 (assert (not (equal (multiple-value-list
154 (subtypep '(function (&rest t
)) '(function ())))
157 (assert (subtypep '(function)
158 '(function (&optional
* &rest t
))))
159 (assert (equal (multiple-value-list
160 (subtypep '(function)
161 '(function (t &rest t
))))
163 (assert (and (subtypep 'function
'(function))
164 (subtypep '(function) 'function
)))
166 ;;; Absent any exciting generalizations of |R, the type RATIONAL is
167 ;;; partitioned by RATIO and INTEGER. Ensure that the type system
168 ;;; knows about this. [ the type system is permitted to return NIL,
169 ;;; NIL for these, so if future maintenance breaks these tests that
170 ;;; way, that's fine. What the SUBTYPEP calls are _not_ allowed to
171 ;;; return is NIL, T, because that's completely wrong. ]
172 (assert (subtypep '(or integer ratio
) 'rational
))
173 (assert (subtypep 'rational
'(or integer ratio
)))
174 ;;; Likewise, these are allowed to return NIL, NIL, but shouldn't
176 (assert (subtypep t
'(or real
(not real
))))
177 (assert (subtypep t
'(or keyword
(not keyword
))))
178 (assert (subtypep '(and cons
(not (cons symbol integer
)))
179 '(or (cons (not symbol
) *) (cons * (not integer
)))))
180 (assert (subtypep '(or (cons (not symbol
) *) (cons * (not integer
)))
181 '(and cons
(not (cons symbol integer
)))))
182 (assert (subtypep '(or (eql 0) (rational (0) 10))
184 (assert (subtypep '(rational 0 10)
185 '(or (eql 0) (rational (0) 10))))
186 ;;; Until sbcl-0.7.13.7, union of CONS types when the CDRs were the
187 ;;; same type gave exceedingly wrong results
188 (assert (null (subtypep '(or (cons fixnum single-float
)
189 (cons bignum single-float
))
190 '(cons single-float single-float
))))
191 (assert (subtypep '(cons integer single-float
)
192 '(or (cons fixnum single-float
) (cons bignum single-float
))))
194 (assert (not (nth-value 1 (subtypep '(and null some-unknown-type
)
195 'another-unknown-type
))))
198 (dolist (fun '(and if
))
199 (assert (raises-error?
(coerce fun
'function
) type-error
)))
202 (let ((x (make-array 0 :element-type
`(unsigned-byte ,(1+ i
)))))
203 (eval `(typep ,x
(class-of ,x
)))))
205 (assert (not (typep #c
(1 2) '(member #c
(2 1)))))
206 (assert (typep #c
(1 2) '(member #c
(1 2))))
207 (assert (subtypep 'nil
'(complex nil
)))
208 (assert (subtypep '(complex nil
) 'nil
))
209 (assert (subtypep 'nil
'(complex (eql 0))))
210 (assert (subtypep '(complex (eql 0)) 'nil
))
211 (assert (subtypep 'nil
'(complex (integer 0 0))))
212 (assert (subtypep '(complex (integer 0 0)) 'nil
))
213 (assert (subtypep 'nil
'(complex (rational 0 0))))
214 (assert (subtypep '(complex (rational 0 0)) 'nil
))
215 (assert (subtypep 'complex
'(complex real
)))
216 (assert (subtypep '(complex real
) 'complex
))
217 (assert (subtypep '(complex (eql 1)) '(complex (member 1 2))))
218 (assert (subtypep '(complex ratio
) '(complex rational
)))
219 (assert (subtypep '(complex ratio
) 'complex
))
220 (assert (equal (multiple-value-list
221 (subtypep '(complex (integer 1 2))
222 '(member #c
(1 1) #c
(1 2) #c
(2 1) #c
(2 2))))
225 (assert (typep 0 '(real #.
(ash -
1 10000) #.
(ash 1 10000))))
226 (assert (subtypep '(real #.
(ash -
1 1000) #.
(ash 1 1000))
227 '(real #.
(ash -
1 10000) #.
(ash 1 10000))))
228 (assert (subtypep '(real (#.
(ash -
1 1000)) (#.
(ash 1 1000)))
229 '(real #.
(ash -
1 1000) #.
(ash 1 1000))))
231 ;;; Bug, found by Paul F. Dietz
232 (let* ((x (eval #c
(-1 1/2)))
234 (assert (subtypep type
'(complex rational
)))
235 (assert (typep x type
)))
237 ;;; Test derivation of LOG{AND,IOR,XOR} bounds for unsigned arguments.
239 ;;; Fear the Loop of Doom!
241 ;;; (In fact, this is such a fearsome loop that executing it with the
242 ;;; evaluator would take ages... Disable it under those circumstances.)
243 #+#.
(cl:if
(cl:eq sb-ext
:*evaluator-mode
* :compile
) '(and) '(or))
246 (flet ((brute-force (a b c d op minimize
)
247 (loop with extreme
= (if minimize
(ash 1 bits
) 0)
248 with collector
= (if minimize
#'min
#'max
)
249 for i from a upto b do
250 (loop for j from c upto d do
251 (setf extreme
(funcall collector
254 finally
(return extreme
))))
255 (dolist (op '(logand logior logxor
))
256 (dolist (minimize '(t nil
))
257 (let ((deriver (intern (format nil
"~A-DERIVE-UNSIGNED-~:[HIGH~;LOW~]-BOUND"
259 (find-package :sb-c
))))
260 (loop for a from
0 below size do
261 (loop for b from a below size do
262 (loop for c from
0 below size do
263 (loop for d from c below size do
264 (let* ((brute (brute-force a b c d op minimize
))
265 (x-type (sb-c::specifier-type
`(integer ,a
,b
)))
266 (y-type (sb-c::specifier-type
`(integer ,c
,d
)))
267 (derived (funcall deriver x-type y-type
)))
268 (unless (= brute derived
)
269 (format t
"FAIL: ~A [~D,~D] [~D,~D] ~A~%
270 ACTUAL ~D DERIVED ~D~%"
271 op a b c d minimize brute derived
)
272 (assert (= brute derived
)))))))))))))
274 ;;; subtypep on CONS types wasn't taking account of the fact that a
275 ;;; CONS type could be the empty type (but no other non-CONS type) in
277 (multiple-value-bind (yes win
)
278 (subtypep '(and function stream
) 'nil
)
279 (multiple-value-bind (cyes cwin
)
280 (subtypep '(cons (and function stream
) t
)
282 (assert (eq yes cyes
))
283 (assert (eq win cwin
))))
285 ;;; CONS type subtypep could be too enthusiastic about thinking it was
287 (multiple-value-bind (yes win
)
288 (subtypep '(satisfies foo
) '(satisfies bar
))
291 (multiple-value-bind (cyes cwin
)
292 (subtypep '(cons (satisfies foo
) t
)
293 '(cons (satisfies bar
) t
))
295 (assert (null cwin
))))
297 (multiple-value-bind (yes win
)
298 (subtypep 'generic-function
'function
)
301 ;;; this would be in some internal test suite like type.before-xc.lisp
302 ;;; except that generic functions don't exist at that stage.
303 (multiple-value-bind (yes win
)
304 (subtypep 'generic-function
'sb-kernel
:funcallable-instance
)
308 ;;; all sorts of answers are right for this one, but it used to
309 ;;; trigger an AVER instead.
310 (subtypep '(function ()) '(and (function ()) (satisfies identity
)))
312 (assert (sb-kernel:unknown-type-p
(sb-kernel:specifier-type
'an-unkown-type
)))
316 (sb-kernel:specifier-type
'(or (simple-array an-unkown-type
(*))
317 (simple-array an-unkown-type
)))
318 (sb-kernel:specifier-type
'(or (simple-array an-unkown-type
(*))
319 (simple-array an-unkown-type
)))))
323 (sb-kernel:specifier-type
'(simple-array an-unkown-type
(*)))
324 (sb-kernel:specifier-type
'(simple-array an-unkown-type
(*)))))
329 (sb-kernel:specifier-type
'(simple-array an-unkown-type
(*)))
330 (sb-kernel:specifier-type
'(array an-unkown-type
(*))))))
335 (sb-kernel:specifier-type
'(simple-array an-unkown-type
(7)))
336 (sb-kernel:specifier-type
'(simple-array an-unkown-type
(8))))))
339 (sb-kernel:type
/= (sb-kernel:specifier-type
'cons
)
340 (sb-kernel:specifier-type
'(cons single-float single-float
))))
342 (multiple-value-bind (match win
)
343 (sb-kernel:type
= (sb-kernel:specifier-type
'(cons integer
))
344 (sb-kernel:specifier-type
'(cons)))
345 (assert (and (not match
) win
)))
347 (assert (typep #p
"" 'sb-kernel
:instance
))
348 (assert (subtypep '(member #p
"") 'sb-kernel
:instance
))