1 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; This software is derived from the CMU CL system, which was
5 ;;;; written at Carnegie Mellon University and released into the
6 ;;;; public domain. The software is in the public domain and is
7 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
8 ;;;; files for more information.
10 (defpackage :sb-introspect-test
11 (:use "SB-INTROSPECT" "CL" "SB-RT"))
13 (in-package :sb-introspect-test)
15 (defmacro deftest* ((name &key fails-on) form &rest results)
17 (when (sb-impl::featurep ',fails-on)
18 (pushnew ',name sb-rt::*expected-failures*))
19 (deftest ,name ,form ,@results)))
21 (deftest function-lambda-list.1
22 (function-lambda-list 'cl-user::one)
23 (cl-user::a cl-user::b cl-user::c))
25 (deftest function-lambda-list.2
26 (function-lambda-list 'the)
27 (sb-c::value-type sb-c::form))
29 (deftest function-lambda-list.3
30 (function-lambda-list #'(sb-pcl::slow-method cl-user::j (t)))
31 (sb-pcl::method-args sb-pcl::next-methods))
33 (deftest definition-source-plist.1
34 (let* ((source (find-definition-source #'cl-user::one))
35 (plist (definition-source-plist source)))
36 (values (= (definition-source-file-write-date source)
37 (file-write-date "test.lisp"))
38 (or (equal (getf plist :test-outer)
43 (deftest definition-source-plist.2
44 (let ((plist (definition-source-plist
45 (find-definition-source #'cl-user::four))))
46 (values (or (equal (getf plist :test-outer) "OUT")
48 (or (equal (getf plist :test-inner) "IN")
52 (defun matchp (object form-number)
53 (let ((ds (sb-introspect:find-definition-source object)))
54 (and (pathnamep (sb-introspect:definition-source-pathname ds))
56 (first (sb-introspect:definition-source-form-path ds))))))
58 (defun matchp-name (type object form-number)
59 (let ((ds (car (sb-introspect:find-definition-sources-by-name object type))))
60 (and (pathnamep (sb-introspect:definition-source-pathname ds))
62 (first (sb-introspect:definition-source-form-path ds))))))
64 (defun matchp-length (type object form-numbers)
65 (let ((ds (sb-introspect:find-definition-sources-by-name object type)))
66 (= (length ds) form-numbers)))
68 (deftest find-source-stuff.1
69 (matchp-name :function 'cl-user::one 2)
72 (deftest find-source-stuff.2
73 (matchp #'cl-user::one 2)
76 (deftest find-source-stuff.3
77 (matchp-name :generic-function 'cl-user::two 3)
80 (deftest find-source-stuff.4
81 (matchp (car (sb-pcl:generic-function-methods #'cl-user::two)) 4)
84 (deftest find-source-stuff.5
85 (matchp-name :variable 'cl-user::*a* 8)
88 (deftest find-source-stuff.6
89 (matchp-name :variable 'cl-user::*b* 9)
92 (deftest find-source-stuff.7
93 (matchp-name :class 'cl-user::a 10)
96 (deftest find-source-stuff.8
97 (matchp-name :condition 'cl-user::b 11)
100 (deftest find-source-stuff.9
101 (matchp-name :structure 'cl-user::c 12)
104 (deftest find-source-stuff.10
105 (matchp-name :function 'cl-user::make-c 12)
108 (deftest find-source-stuff.11
109 (matchp-name :function 'cl-user::c-e 12)
112 (deftest find-source-stuff.12
113 (matchp-name :structure 'cl-user::d 13)
116 (deftest find-source-stuff.13
117 (matchp-name :function 'cl-user::make-d 13)
120 (deftest find-source-stuff.14
121 (matchp-name :function 'cl-user::d-e 13)
124 (deftest find-source-stuff.15
125 (matchp-name :package 'cl-user::e 14)
128 (deftest find-source-stuff.16
129 (matchp-name :symbol-macro 'cl-user::f 15)
132 (deftest find-source-stuff.17
133 (matchp-name :type 'cl-user::g 16)
136 (deftest find-source-stuff.18
137 (matchp-name :constant 'cl-user::+h+ 17)
140 (deftest find-source-stuff.19
141 (matchp-length :method 'cl-user::j 2)
144 (deftest find-source-stuff.20
145 (matchp-name :macro 'cl-user::l 20)
148 (deftest find-source-stuff.21
149 (matchp-name :compiler-macro 'cl-user::m 21)
152 (deftest find-source-stuff.22
153 (matchp-name :setf-expander 'cl-user::n 22)
156 (deftest find-source-stuff.23
157 (matchp-name :function '(setf cl-user::o) 23)
160 (deftest find-source-stuff.24
161 (matchp-name :method '(setf cl-user::p) 24)
164 (deftest find-source-stuff.25
165 (matchp-name :macro 'cl-user::q 25)
169 (deftest find-source-stuff.26
170 (matchp-name :method-combination 'cl-user::r 26)
174 (deftest find-source-stuff.27
175 (matchp-name :setf-expander 'cl-user::s 27)
178 (deftest find-source-stuff.28
179 (let ((fin (make-instance 'sb-mop:funcallable-standard-object)))
180 (sb-mop:set-funcallable-instance-function fin #'cl-user::one)
184 (deftest find-source-stuff.29
187 (sb-profile:profile cl-user::one)
188 (matchp-name :function 'cl-user::one 2))
189 (sb-profile:unprofile cl-user::one))
192 (deftest find-source-stuff.30
193 ;; Test finding a type that isn't one
194 (not (find-definition-sources-by-name 'fboundp :type))
197 (deftest find-source-stuff.31
198 (matchp-name :function 'cl-user::compile-time-too-fun 28)
201 (deftest find-source-stuff.32
202 (matchp-name :function 'cl-user::loaded-as-source-fun 3)
205 (deftest find-source-stuff.
206 (matchp-name :variable 'cl-user::**global** 29)
209 ;;; Check wrt. interplay of generic functions and their methods.
211 (defgeneric xuuq (gf.a gf.b &rest gf.rest &key gf.k-X))
212 (defmethod xuuq ((m1.a number) m1.b &rest m1.rest &key gf.k-X m1.k-Y m1.k-Z)
213 (declare (ignore m1.a m1.b m1.rest gf.k-X m1.k-Y m1.k-Z))
215 (defmethod xuuq ((m2.a string) m2.b &rest m2.rest &key gf.k-X m1.k-Y m2.k-Q)
216 (declare (ignore m2.a m2.b m2.rest gf.k-X m1.k-Y m2.k-Q))
219 ;; XUUQ's lambda list should look similiar to
221 ;; (GF.A GF.B &REST GF.REST &KEY GF.K-X M1.K-Z M1.K-Y M2.K-Q)
223 (deftest gf-interplay.1
224 (multiple-value-bind (required optional restp rest keyp keys allowp
225 auxp aux morep more-context more-count)
226 (sb-int:parse-lambda-list (function-lambda-list #'xuuq))
227 (and (equal required '(gf.a gf.b))
229 (and restp (eql rest 'gf.rest))
231 (member 'gf.k-X keys)
232 (member 'm1.k-Y keys)
233 (member 'm1.k-Z keys)
234 (member 'm2.k-Q keys))
236 (and (not auxp) (null aux))
237 (and (not morep) (null more-context) (not more-count))))
240 ;;; Check what happens when there's no explicit DEFGENERIC.
242 (defmethod kroolz (r1 r2 &optional opt &aux aux)
243 (declare (ignore r1 r2 opt aux))
246 (deftest gf-interplay.2
247 (equal (function-lambda-list #'kroolz) '(r1 r2 &optional opt))
250 ;;;; Check correctness of DEFTYPE-LAMBDA-LIST.
252 (&whole w &environment e r1 r2 &optional o &rest rest &key k1 k2 k3)
253 (declare (ignore w e r1 r2 o rest k1 k2 k3))
256 (deftest deftype-lambda-list.1
257 (deftype-lambda-list 'foobar-type)
258 (&whole w &environment e r1 r2 &optional o &rest rest &key k1 k2 k3)
261 (deftest deftype-lambda-list.2
262 (deftype-lambda-list (gensym))
266 ;; ARRAY is a primitive type with associated translator function.
267 (deftest deftype-lambda-list.3
268 (deftype-lambda-list 'array)
269 (&optional (sb-kernel::element-type '*) (sb-kernel::dimensions '*))
272 ;; VECTOR is a primitive type that is defined by means of DEFTYPE.
273 (deftest deftype-lambda-list.4
274 (deftype-lambda-list 'vector)
275 (&optional sb-kernel::element-type sb-kernel::size)
278 ;;; Test allocation-information
280 (defun tai (x kind info &key ignore)
281 (multiple-value-bind (kind2 info2) (sb-introspect:allocation-information x)
282 (unless (eq kind kind2)
283 (error "wanted ~S, got ~S" kind kind2))
284 (when (not (null ignore))
285 (setf info2 (copy-list info2))
288 (setf info (copy-list info))
293 (deftest allocation-infromation.1
294 (tai nil :heap '(:space :static))
297 (deftest allocation-information.2
298 (tai t :heap '(:space :static))
301 (deftest allocation-information.3
302 (tai 42 :immediate nil)
305 ;;; Skip the whole damn test on GENCGC PPC -- the combination is just
306 ;;; to flaky for this to make too much sense. GENCGC SPARC almost
307 ;;; certainly exhibits the same behavior patterns (or antipatterns) as
310 ;;; -- It appears that this test can also fail due to systematic issues
311 ;;; (possibly with the C compiler used) which we cannot detect based on
312 ;;; *features*. Until this issue has been fixed, I am marking this test
313 ;;; as failing on Windows to allow installation of the contrib on
314 ;;; affected builds, even if the underlying issue is (possibly?) not even
315 ;;; strictly related to windows. C.f. lp1057631. --DFL
317 (deftest* (allocation-information.4
318 ;; Ignored as per the comment above, even though it seems
319 ;; unlikely that this is the right condition.
320 :fails-on (or :win32 (and (or :ppc :sparc) :gencgc)))
323 ;; FIXME: This is the canonical GENCGC result. On PPC we sometimes get
324 ;; :LARGE T, which doesn't seem right -- but ignore that for now.
325 `(:space :dynamic :generation ,sb-vm:+pseudo-static-generation+
326 :write-protected t :boxed t :pinned nil :large nil)
327 :ignore (list :page #+ppc :large))
330 ;; FIXME: Figure out what's the right cheney-result. SPARC at least
331 ;; has exhibited both :READ-ONLY and :DYNAMIC, which seems wrong.
337 (deftest allocation-information.thread.1
338 (let ((x (list 1 2 3)))
339 (declare (dynamic-extent x))
340 (tai x :stack sb-thread:*current-thread*))
346 (let ((x (list 1 2 3)))
347 (declare (dynamic-extent x))
348 (let ((child (sb-thread:make-thread
350 (sb-introspect:allocation-information x)))))
351 (equal (list :stack sb-thread:*current-thread*)
352 (multiple-value-list (sb-thread:join-thread child))))))
354 (deftest allocation-information.thread.2
358 (defun thread-tai2 ()
359 (let* ((sem (sb-thread:make-semaphore))
361 (child (sb-thread:make-thread
363 (let ((x (list 1 2 3)))
364 (declare (dynamic-extent x))
366 (sb-thread:wait-on-semaphore sem)))
370 (equal (list :stack child)
372 (sb-introspect:allocation-information obj)))
373 (sb-thread:signal-semaphore sem)
374 (sb-thread:join-thread child))))
376 (deftest allocation-information.thread.3
380 ;;;; Test FUNCTION-TYPE
382 (defun type-equal (typespec1 typespec2)
383 (or (equal typespec1 typespec2) ; TYPE= punts on &keywords in FTYPEs.
384 (sb-kernel:type= (sb-kernel:values-specifier-type typespec1)
385 (sb-kernel:values-specifier-type typespec2))))
387 (defmacro interpret (form)
388 `(let ((sb-ext:*evaluator-mode* :interpret))
393 (declaim (ftype (function (integer &optional string) string) moon))
394 (defun moon (int &optional suffix)
395 (concatenate 'string (princ-to-string int) suffix))
397 (deftest function-type.1
398 (values (type-equal (function-type 'moon) (function-type #'moon))
399 (type-equal (function-type #'moon)
400 '(function (integer &optional string)
401 (values string &rest t))))
404 (defun sun (x y &key k1)
405 (declare (fixnum x y))
406 (declare (boolean k1))
407 (declare (ignore x y k1))
410 (deftest function-type.2
411 (values (type-equal (function-type 'sun) (function-type #'sun))
412 (type-equal (function-type #'sun)
413 '(function (fixnum fixnum &key (:k1 (member nil t)))
414 (values (member t) &optional))))
419 (deftest function-type.5
422 (values (symbol-name s))))
423 (type-equal (function-type #'f)
424 '(function (symbol) (values simple-string &optional))))
429 (deftest function-type.6
435 (type-equal (function-type #'closure)
436 '(function (fixnum) (values fixnum &optional)))))
439 ;; Anonymous functions
441 (deftest function-type.7
442 (type-equal (function-type #'(lambda (x) (declare (fixnum x)) x))
443 '(function (fixnum) (values fixnum &optional)))
446 ;; Interpreted functions
449 (deftest function-type.8
450 (type-equal (function-type (interpret (lambda (x) (declare (fixnum x)) x)))
451 '(function (&rest t) *))
456 (defgeneric earth (x y))
458 (deftest function-type+gfs.1
459 (values (type-equal (function-type 'earth) (function-type #'earth))
460 (type-equal (function-type 'earth) '(function (t t) *)))
463 ;; Implicitly created generic functions.
465 ;; (FUNCTION-TYPE 'MARS) => FUNCTION at the moment. (1.0.31.26)
469 (defmethod mars (x y) (+ x y))
472 (deftest function-type+gfs.2
473 (values (type-equal (function-type 'mars) (function-type #'mars))
474 (type-equal (function-type 'mars) '(function (t t) *)))
477 ;; DEFSTRUCT created functions
479 ;; These do not yet work because SB-KERNEL:%FUN-NAME does not work on
480 ;; functions defined by DEFSTRUCT. (1.0.35.x)
487 (defstruct (struct (:predicate our-struct-p)
488 (:copier copy-our-struct))
491 (deftest function-type+defstruct.1
492 (values (type-equal (function-type 'struct-a)
493 (function-type #'struct-a))
494 (type-equal (function-type 'struct-a)
495 '(function (struct) (values fixnum &optional))))
498 (deftest function-type+defstruct.2
499 (values (type-equal (function-type 'our-struct-p)
500 (function-type #'our-struct-p))
501 (type-equal (function-type 'our-struct-p)
502 '(function (t) (values (member t nil) &optional))))
505 (deftest function-type+defstruct.3
506 (values (type-equal (function-type 'copy-our-struct)
507 (function-type #'copy-our-struct))
508 (type-equal (function-type 'copy-our-struct)
509 '(function (struct) (values struct &optional))))
512 (defstruct (typed-struct :named (:type list)
513 (:predicate typed-struct-p))
516 (deftest function-type+defstruct.4
517 (values (type-equal (function-type 'typed-struct-a)
518 (function-type #'typed-struct-a))
519 (type-equal (function-type 'typed-struct-a)
520 '(function (list) (values fixnum &optional))))
523 (deftest function-type+defstruct.5
524 (values (type-equal (function-type 'typed-struct-p)
525 (function-type #'typed-struct-p))
526 (type-equal (function-type 'typed-struct-p)
527 '(function (t) (values (member t nil) &optional))))
534 (defun (setf sun) (value x y &key k1)
535 (declare (boolean value))
536 (declare (fixnum x y))
537 (declare (boolean k1))
538 (declare (ignore x y k1))
541 (deftest function-type+setf.1
542 (values (type-equal (function-type '(setf sun))
543 (function-type #'(setf sun)))
544 (type-equal (function-type '(setf sun))
545 '(function ((member nil t)
547 &key (:k1 (member nil t)))
548 (values (member nil t) &optional))))
553 (deftest function-type+misc.1
555 (type-equal (function-type #'nullary)
556 '(function () (values null &optional))))
559 ;;; Defstruct accessor, copier, and predicate
561 (deftest defstruct-fun-sources
562 (let ((copier (find-definition-source #'cl-user::copy-three))
563 (accessor (find-definition-source #'cl-user::three-four))
564 (predicate (find-definition-source #'cl-user::three-p)))
565 (values (and (equalp copier accessor)
566 (equalp copier predicate))
568 (file-namestring (definition-source-pathname copier)))
570 (definition-source-form-path copier))))
575 (deftest defstruct-fun-sources-by-name
576 (let ((copier (car (find-definition-sources-by-name 'cl-user::copy-three :function)))
577 (accessor (car (find-definition-sources-by-name 'cl-user::three-four :function)))
578 (predicate (car (find-definition-sources-by-name 'cl-user::three-p :function))))
579 (values (and (equalp copier accessor)
580 (equalp copier predicate))
582 (file-namestring (definition-source-pathname copier)))
584 (definition-source-form-path copier))))