1.0.32.18: additional allocation information
[sbcl.git] / contrib / sb-introspect / test-driver.lisp
1 ;;;; This software is part of the SBCL system. See the README file for
2 ;;;; more information.
3 ;;;;
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.
9
10 (defpackage :sb-introspect-test
11   (:use "SB-INTROSPECT" "CL" "SB-RT"))
12
13 (in-package :sb-introspect-test)
14
15 (deftest function-lambda-list.1
16     (function-lambda-list 'cl-user::one)
17   (cl-user::a cl-user::b cl-user::c))
18
19 (deftest function-lambda-list.2
20     (function-lambda-list 'the)
21   (sb-c::value-type sb-c::form))
22
23 (deftest function-lambda-list.3
24     (function-lambda-list #'(sb-pcl::slow-method cl-user::j (t)))
25   (sb-pcl::method-args sb-pcl::next-methods))
26
27 (deftest definition-source-plist.1
28     (let* ((source (find-definition-source #'cl-user::one))
29            (plist (definition-source-plist source)))
30       (values (= (definition-source-file-write-date source)
31                  (file-write-date "test.lisp"))
32               (or (equal (getf plist :test-outer)
33                          "OUT")
34                   plist)))
35   t t)
36
37 (deftest definition-source-plist.2
38     (let ((plist (definition-source-plist
39                      (find-definition-source #'cl-user::four))))
40       (values (or (equal (getf plist :test-outer) "OUT")
41                   plist)
42               (or (equal (getf plist :test-inner) "IN")
43                   plist)))
44   t t)
45
46 (defun matchp (object form-number)
47   (let ((ds (sb-introspect:find-definition-source object)))
48     (and (pathnamep (sb-introspect:definition-source-pathname ds))
49          (= form-number
50             (first (sb-introspect:definition-source-form-path ds))))))
51
52 (defun matchp-name (type object form-number)
53   (let ((ds (car (sb-introspect:find-definition-sources-by-name object type))))
54     (and (pathnamep (sb-introspect:definition-source-pathname ds))
55          (= form-number
56             (first (sb-introspect:definition-source-form-path ds))))))
57
58 (defun matchp-length (type object form-numbers)
59   (let ((ds (sb-introspect:find-definition-sources-by-name object type)))
60     (= (length ds) form-numbers)))
61
62 (deftest find-source-stuff.1
63     (matchp-name :function 'cl-user::one 2)
64   t)
65
66 (deftest find-source-stuff.2
67     (matchp #'cl-user::one 2)
68   t)
69
70 (deftest find-source-stuff.3
71     (matchp-name :generic-function 'cl-user::two 3)
72   t)
73
74 (deftest find-source-stuff.4
75     (matchp (car (sb-pcl:generic-function-methods #'cl-user::two)) 4)
76   t)
77
78 (deftest find-source-stuff.5
79     (matchp-name :variable 'cl-user::*a* 8)
80   t)
81
82 (deftest find-source-stuff.6
83     (matchp-name :variable 'cl-user::*b* 9)
84   t)
85
86 (deftest find-source-stuff.7
87     (matchp-name :class 'cl-user::a 10)
88   t)
89
90 (deftest find-source-stuff.8
91     (matchp-name :condition 'cl-user::b 11)
92   t)
93
94 (deftest find-source-stuff.9
95     (matchp-name :structure 'cl-user::c 12)
96   t)
97
98 (deftest find-source-stuff.10
99     (matchp-name :function 'cl-user::make-c 12)
100   t)
101
102 (deftest find-source-stuff.11
103     (matchp-name :function 'cl-user::c-e 12)
104   t)
105
106 (deftest find-source-stuff.12
107     (matchp-name :structure 'cl-user::d 13)
108   t)
109
110 (deftest find-source-stuff.13
111     (matchp-name :function 'cl-user::make-d 13)
112   t)
113
114 (deftest find-source-stuff.14
115     (matchp-name :function 'cl-user::d-e 13)
116   t)
117
118 (deftest find-source-stuff.15
119     (matchp-name :package 'cl-user::e 14)
120   t)
121
122 (deftest find-source-stuff.16
123     (matchp-name :symbol-macro 'cl-user::f 15)
124   t)
125
126 (deftest find-source-stuff.17
127     (matchp-name :type 'cl-user::g 16)
128   t)
129
130 (deftest find-source-stuff.18
131     (matchp-name :constant 'cl-user::+h+ 17)
132   t)
133
134 (deftest find-source-stuff.19
135     (matchp-length :method 'cl-user::j 2)
136   t)
137
138 (deftest find-source-stuff.20
139     (matchp-name :macro 'cl-user::l 20)
140   t)
141
142 (deftest find-source-stuff.21
143     (matchp-name :compiler-macro 'cl-user::m 21)
144   t)
145
146 (deftest find-source-stuff.22
147     (matchp-name :setf-expander 'cl-user::n 22)
148   t)
149
150 (deftest find-source-stuff.23
151     (matchp-name :function  '(setf cl-user::o) 23)
152   t)
153
154
155 (deftest find-source-stuff.24
156     (matchp-name :method  '(setf cl-user::p) 24)
157   t)
158
159
160 (deftest find-source-stuff.25
161     (matchp-name :macro  'cl-user::q 25)
162   t)
163
164
165 (deftest find-source-stuff.26
166     (matchp-name :method-combination 'cl-user::r 26)
167   t)
168
169
170 (deftest find-source-stuff.27
171     (matchp-name :setf-expander 'cl-user::s 27)
172   t)
173
174 (deftest find-source-stuff.28
175     (let ((fin (make-instance 'sb-mop:funcallable-standard-object)))
176       (sb-mop:set-funcallable-instance-function fin #'cl-user::one)
177       (matchp fin 2))
178   t)
179
180 (deftest find-source-stuff.29
181     (unwind-protect
182          (progn
183            (sb-profile:profile cl-user::one)
184            (matchp-name :function 'cl-user::one 2))
185       (sb-profile:unprofile cl-user::one))
186   t)
187
188 (deftest find-source-stuff.30
189     ;; Test finding a type that isn't one
190     (not (find-definition-sources-by-name 'fboundp :type))
191   t)
192
193 ;;; Check wrt. interplay of generic functions and their methods.
194
195 (defgeneric xuuq (gf.a gf.b          &rest gf.rest &key gf.k-X))
196 (defmethod  xuuq ((m1.a number) m1.b &rest m1.rest &key gf.k-X m1.k-Y m1.k-Z)
197   (declare (ignore m1.a m1.b m1.rest gf.k-X m1.k-Y m1.k-Z))
198   'm1)
199 (defmethod  xuuq ((m2.a string) m2.b &rest m2.rest &key gf.k-X m1.k-Y m2.k-Q)
200   (declare (ignore m2.a m2.b m2.rest gf.k-X m1.k-Y m2.k-Q))
201   'm2)
202
203 ;; XUUQ's lambda list should look similiar to
204 ;;
205 ;;    (GF.A GF.B &REST GF.REST &KEY GF.K-X M1.K-Z M1.K-Y M2.K-Q)
206 ;;
207 (deftest gf-interplay.1
208     (multiple-value-bind (required optional restp rest keyp keys allowp
209                                 auxp aux morep more-context more-count)
210         (sb-int:parse-lambda-list (function-lambda-list #'xuuq))
211       (and (equal required '(gf.a gf.b))
212            (null optional)
213            (and restp (eql rest 'gf.rest))
214            (and keyp
215                 (member 'gf.k-X keys)
216                 (member 'm1.k-Y keys)
217                 (member 'm1.k-Z keys)
218                 (member 'm2.k-Q keys))
219            (not allowp)
220            (and (not auxp) (null aux))
221            (and (not morep) (null more-context) (not more-count))))
222   t)
223
224 ;;; Check what happens when there's no explicit DEFGENERIC.
225
226 (defmethod kroolz (r1 r2 &optional opt &aux aux)
227   (declare (ignore r1 r2 opt aux))
228   'kroolz)
229
230 (deftest gf-interplay.2
231     (equal (function-lambda-list #'kroolz) '(r1 r2 &optional opt))
232   t)
233
234 ;;;; Check correctness of DEFTYPE-LAMBDA-LIST.
235 (deftype foobar-type
236     (&whole w &environment e r1 r2 &optional o &rest rest &key k1 k2 k3)
237   (declare (ignore w e r1 r2 o rest k1 k2 k3))
238   nil)
239
240 (deftest deftype-lambda-list.1
241     (multiple-value-bind (arglist found?) (deftype-lambda-list 'foobar-type)
242           (and found?
243                (equal arglist '(&whole w &environment e
244                                 r1 r2 &optional o &rest rest &key k1 k2 k3))))
245   t)
246
247 (deftest deftype-lambda-list.2
248     (equal (multiple-value-list (deftype-lambda-list (gensym)))
249            '(nil nil))
250   t)
251
252 ;;; Test allocation-information
253
254 (defun tai (x kind info &key ignore)
255   (multiple-value-bind (kind2 info2) (sb-introspect:allocation-information x)
256     (unless (eq kind kind2)
257       (error "wanted ~S, got ~S" kind kind2))
258     (when (not (null ignore))
259       (setf info2 (copy-list info2))
260       (dolist (key ignore)
261         (remf info2 key))
262       (setf info (copy-list info))
263       (dolist (key ignore)
264         (remf info key)))
265     (equal info info2)))
266
267 (deftest allocation-infromation.1
268     (tai nil :heap '(:space :static))
269   t)
270
271 (deftest allocation-information.2
272     (tai t :heap '(:space :static))
273   t)
274
275 (deftest allocation-information.3
276     (tai 42 :immediate nil)
277   t)
278
279 (deftest allocation-information.4
280     #+gencgc
281     (tai #'cons :heap
282          ;; FIXME: This is the canonical GENCGC result. On PPC we sometimes get
283          ;; :LARGE T, which doesn't seem right -- but ignore that for now.
284          '(:space :dynamic :generation 6 :write-protected t :boxed t :pinned nil :large nil)
285          :ignore #+ppc '(:large) #-ppc nil)
286     #-gencgc
287     (tai :cons :heap
288          ;; FIXME: Figure out what's the right cheney-result. SPARC at least
289          ;; has exhibited both :READ-ONLY and :DYNAMIC, which seems wrong.
290          '()
291          :ignore '(:space))
292   t)
293
294 #+sb-thread
295 (deftest allocation-information.thread.1
296     (let ((x (list 1 2 3)))
297       (declare (dynamic-extent x))
298       (tai x :stack sb-thread:*current-thread*))
299   t)
300
301 #+sb-thread
302 (progn
303    (defun thread-tai ()
304      (let ((x (list 1 2 3)))
305        (declare (dynamic-extent x))
306        (let ((child (sb-thread:make-thread
307                      (lambda ()
308                        (sb-introspect:allocation-information x)))))
309          (equal (list :stack sb-thread:*current-thread*)
310                 (multiple-value-list (sb-thread:join-thread child))))))
311
312    (deftest allocation-information.thread.2
313        (thread-tai)
314      t)
315
316    (defun thread-tai2 ()
317      (let* ((sem (sb-thread:make-semaphore))
318             (obj nil)
319             (child (sb-thread:make-thread
320                     (lambda ()
321                       (let ((x (list 1 2 3)))
322                         (declare (dynamic-extent x))
323                         (setf obj x)
324                         (sb-thread:wait-on-semaphore sem)))
325                     :name "child")))
326        (loop until obj)
327        (unwind-protect
328             (equal (list :stack child)
329                    (multiple-value-list
330                     (sb-introspect:allocation-information obj)))
331          (sb-thread:signal-semaphore sem)
332          (sb-thread:join-thread child))))
333
334    (deftest allocation-information.thread.3
335        (thread-tai2)
336      t))