276e4718ed94f93af9eb5e2f5599d1fe46b3fa1d
[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 (deftest find-source-stuff.24
155     (matchp-name :method  '(setf cl-user::p) 24)
156   t)
157
158 (deftest find-source-stuff.25
159     (matchp-name :macro  'cl-user::q 25)
160   t)
161
162
163 (deftest find-source-stuff.26
164     (matchp-name :method-combination 'cl-user::r 26)
165   t)
166
167
168 (deftest find-source-stuff.27
169     (matchp-name :setf-expander 'cl-user::s 27)
170   t)
171
172 (deftest find-source-stuff.28
173     (let ((fin (make-instance 'sb-mop:funcallable-standard-object)))
174       (sb-mop:set-funcallable-instance-function fin #'cl-user::one)
175       (matchp fin 2))
176   t)
177
178 (deftest find-source-stuff.29
179     (unwind-protect
180          (progn
181            (sb-profile:profile cl-user::one)
182            (matchp-name :function 'cl-user::one 2))
183       (sb-profile:unprofile cl-user::one))
184   t)
185
186 (deftest find-source-stuff.30
187     ;; Test finding a type that isn't one
188     (not (find-definition-sources-by-name 'fboundp :type))
189   t)
190
191 (deftest find-source-stuff.31
192     (matchp-name :function 'cl-user::compile-time-too-fun 28)
193   t)
194
195 (deftest find-source-stuff.32
196     (matchp-name :function 'cl-user::loaded-as-source-fun 3)
197   t)
198
199 ;;; Check wrt. interplay of generic functions and their methods.
200
201 (defgeneric xuuq (gf.a gf.b          &rest gf.rest &key gf.k-X))
202 (defmethod  xuuq ((m1.a number) m1.b &rest m1.rest &key gf.k-X m1.k-Y m1.k-Z)
203   (declare (ignore m1.a m1.b m1.rest gf.k-X m1.k-Y m1.k-Z))
204   'm1)
205 (defmethod  xuuq ((m2.a string) m2.b &rest m2.rest &key gf.k-X m1.k-Y m2.k-Q)
206   (declare (ignore m2.a m2.b m2.rest gf.k-X m1.k-Y m2.k-Q))
207   'm2)
208
209 ;; XUUQ's lambda list should look similiar to
210 ;;
211 ;;    (GF.A GF.B &REST GF.REST &KEY GF.K-X M1.K-Z M1.K-Y M2.K-Q)
212 ;;
213 (deftest gf-interplay.1
214     (multiple-value-bind (required optional restp rest keyp keys allowp
215                                 auxp aux morep more-context more-count)
216         (sb-int:parse-lambda-list (function-lambda-list #'xuuq))
217       (and (equal required '(gf.a gf.b))
218            (null optional)
219            (and restp (eql rest 'gf.rest))
220            (and keyp
221                 (member 'gf.k-X keys)
222                 (member 'm1.k-Y keys)
223                 (member 'm1.k-Z keys)
224                 (member 'm2.k-Q keys))
225            (not allowp)
226            (and (not auxp) (null aux))
227            (and (not morep) (null more-context) (not more-count))))
228   t)
229
230 ;;; Check what happens when there's no explicit DEFGENERIC.
231
232 (defmethod kroolz (r1 r2 &optional opt &aux aux)
233   (declare (ignore r1 r2 opt aux))
234   'kroolz)
235
236 (deftest gf-interplay.2
237     (equal (function-lambda-list #'kroolz) '(r1 r2 &optional opt))
238   t)
239
240 ;;;; Check correctness of DEFTYPE-LAMBDA-LIST.
241 (deftype foobar-type
242     (&whole w &environment e r1 r2 &optional o &rest rest &key k1 k2 k3)
243   (declare (ignore w e r1 r2 o rest k1 k2 k3))
244   nil)
245
246 (deftest deftype-lambda-list.1
247     (deftype-lambda-list 'foobar-type)
248   (&whole w &environment e r1 r2 &optional o &rest rest &key k1 k2 k3)
249   t)
250
251 (deftest deftype-lambda-list.2
252     (deftype-lambda-list (gensym))
253   nil
254   nil)
255
256 ;; ARRAY is a primitive type with associated translator function.
257 (deftest deftype-lambda-list.3
258     (deftype-lambda-list 'array)
259   (&optional (sb-kernel::element-type '*) (sb-kernel::dimensions '*))
260   t)
261
262 ;; VECTOR is a primitive type that is defined by means of DEFTYPE.
263 (deftest deftype-lambda-list.4
264     (deftype-lambda-list 'vector)
265   (&optional sb-kernel::element-type sb-kernel::size)
266   t)
267
268 ;;; Test allocation-information
269
270 (defun tai (x kind info &key ignore)
271   (multiple-value-bind (kind2 info2) (sb-introspect:allocation-information x)
272     (unless (eq kind kind2)
273       (error "wanted ~S, got ~S" kind kind2))
274     (when (not (null ignore))
275       (setf info2 (copy-list info2))
276       (dolist (key ignore)
277         (remf info2 key))
278       (setf info (copy-list info))
279       (dolist (key ignore)
280         (remf info key)))
281     (equal info info2)))
282
283 (deftest allocation-infromation.1
284     (tai nil :heap '(:space :static))
285   t)
286
287 (deftest allocation-information.2
288     (tai t :heap '(:space :static))
289   t)
290
291 (deftest allocation-information.3
292     (tai 42 :immediate nil)
293   t)
294
295 ;;; Skip the whole damn test on GENCGC PPC -- the combination is just
296 ;;; to flaky for this to make too much sense.
297 #-(and ppc gencgc)
298 (deftest allocation-information.4
299     #+gencgc
300     (tai #'cons :heap
301          ;; FIXME: This is the canonical GENCGC result. On PPC we sometimes get
302          ;; :LARGE T, which doesn't seem right -- but ignore that for now.
303          '(:space :dynamic :generation 6 :write-protected t :boxed t :pinned nil :large nil)
304          :ignore (list :page #+ppc :large))
305     #-gencgc
306     (tai :cons :heap
307          ;; FIXME: Figure out what's the right cheney-result. SPARC at least
308          ;; has exhibited both :READ-ONLY and :DYNAMIC, which seems wrong.
309          '()
310          :ignore '(:space))
311   t)
312
313 #+sb-thread
314 (deftest allocation-information.thread.1
315     (let ((x (list 1 2 3)))
316       (declare (dynamic-extent x))
317       (tai x :stack sb-thread:*current-thread*))
318   t)
319
320 #+sb-thread
321 (progn
322    (defun thread-tai ()
323      (let ((x (list 1 2 3)))
324        (declare (dynamic-extent x))
325        (let ((child (sb-thread:make-thread
326                      (lambda ()
327                        (sb-introspect:allocation-information x)))))
328          (equal (list :stack sb-thread:*current-thread*)
329                 (multiple-value-list (sb-thread:join-thread child))))))
330
331    (deftest allocation-information.thread.2
332        (thread-tai)
333      t)
334
335    (defun thread-tai2 ()
336      (let* ((sem (sb-thread:make-semaphore))
337             (obj nil)
338             (child (sb-thread:make-thread
339                     (lambda ()
340                       (let ((x (list 1 2 3)))
341                         (declare (dynamic-extent x))
342                         (setf obj x)
343                         (sb-thread:wait-on-semaphore sem)))
344                     :name "child")))
345        (loop until obj)
346        (unwind-protect
347             (equal (list :stack child)
348                    (multiple-value-list
349                     (sb-introspect:allocation-information obj)))
350          (sb-thread:signal-semaphore sem)
351          (sb-thread:join-thread child))))
352
353    (deftest allocation-information.thread.3
354        (thread-tai2)
355      t))
356
357 ;;;; Test FUNCTION-TYPE
358
359 (defun type-equal (typespec1 typespec2)
360   (or (equal typespec1 typespec2)   ; TYPE= punts on &keywords in FTYPEs.
361       (sb-kernel:type= (sb-kernel:values-specifier-type typespec1)
362                        (sb-kernel:values-specifier-type typespec2))))
363
364 (defmacro interpret (form)
365   `(let ((sb-ext:*evaluator-mode* :interpret))
366      (eval ',form)))
367
368 ;; Functions
369
370 (declaim (ftype (function (integer &optional string) string) moon))
371 (defun moon (int &optional suffix)
372   (concatenate 'string (princ-to-string int) suffix))
373
374 (deftest function-type.1
375     (values (type-equal (function-type 'moon) (function-type #'moon))
376             (type-equal (function-type #'moon)
377                         '(function (integer &optional string)
378                           (values string &rest t))))
379   t t)
380
381 (defun sun (x y &key k1)
382   (declare (fixnum x y))
383   (declare (boolean k1))
384   (declare (ignore x y k1))
385   t)
386
387 (deftest function-type.2
388     (values (type-equal (function-type 'sun) (function-type #'sun))
389             (type-equal (function-type #'sun)
390                         '(function (fixnum fixnum &key (:k1 (member nil t)))
391                           (values (member t) &optional))))
392   t t)
393
394 ;; Local functions
395
396 (deftest function-type.5
397     (flet ((f (s)
398              (declare (symbol s))
399              (values (symbol-name s))))
400       (type-equal (function-type #'f)
401                   '(function (symbol) (values simple-string &optional))))
402   t)
403
404 ;; Closures
405
406 (deftest function-type.6
407     (let ((x 10))
408       (declare (fixnum x))
409       (flet ((closure (y)
410                (declare (fixnum y))
411                (setq x (+ x y))))
412         (type-equal (function-type #'closure)
413                     '(function (fixnum) (values fixnum &optional)))))
414   t)
415
416 ;; Anonymous functions
417
418 (deftest function-type.7
419     (type-equal (function-type #'(lambda (x) (declare (fixnum x)) x))
420                 '(function (fixnum) (values fixnum &optional)))
421   t)
422
423 ;; Interpreted functions
424
425 #+sb-eval
426 (deftest function-type.8
427     (type-equal (function-type (interpret (lambda (x) (declare (fixnum x)) x)))
428                 '(function (&rest t) *))
429   t)
430
431 ;; Generic functions
432
433 (defgeneric earth (x y))
434
435 (deftest function-type+gfs.1
436     (values (type-equal (function-type 'earth) (function-type #'earth))
437             (type-equal (function-type 'earth) '(function (t t) *)))
438   t t)
439
440 ;; Implicitly created generic functions.
441
442 ;; (FUNCTION-TYPE 'MARS) => FUNCTION at the moment. (1.0.31.26)
443
444 ;; See LP #520695.
445
446 (defmethod mars (x y) (+ x y))
447
448 #+ nil
449 (deftest function-type+gfs.2
450     (values (type-equal (function-type 'mars) (function-type #'mars))
451             (type-equal (function-type 'mars) '(function (t t) *)))
452   t t)
453
454 ;; DEFSTRUCT created functions
455
456 ;; These do not yet work because SB-KERNEL:%FUN-NAME does not work on
457 ;; functions defined by DEFSTRUCT. (1.0.35.x)
458
459 ;; See LP #520692.
460
461 #+nil
462 (progn
463
464   (defstruct (struct (:predicate our-struct-p)
465                      (:copier copy-our-struct))
466     (a 42 :type fixnum))
467
468   (deftest function-type+defstruct.1
469       (values (type-equal (function-type 'struct-a)
470                           (function-type #'struct-a))
471               (type-equal (function-type 'struct-a)
472                           '(function (struct) (values fixnum &optional))))
473     t t)
474
475   (deftest function-type+defstruct.2
476       (values (type-equal (function-type 'our-struct-p)
477                           (function-type #'our-struct-p))
478               (type-equal (function-type 'our-struct-p)
479                           '(function (t) (values (member t nil) &optional))))
480     t t)
481
482   (deftest function-type+defstruct.3
483       (values (type-equal (function-type 'copy-our-struct)
484                           (function-type #'copy-our-struct))
485               (type-equal (function-type 'copy-our-struct)
486                           '(function (struct) (values struct &optional))))
487     t t)
488
489   (defstruct (typed-struct :named (:type list)
490                            (:predicate typed-struct-p))
491     (a 42 :type fixnum))
492
493   (deftest function-type+defstruct.4
494       (values (type-equal (function-type 'typed-struct-a)
495                           (function-type #'typed-struct-a))
496               (type-equal (function-type 'typed-struct-a)
497                           '(function (list) (values fixnum &optional))))
498     t t)
499
500   (deftest function-type+defstruct.5
501       (values (type-equal (function-type 'typed-struct-p)
502                           (function-type #'typed-struct-p))
503               (type-equal (function-type 'typed-struct-p)
504                           '(function (t) (values (member t nil) &optional))))
505     t t)
506
507   ) ; #+nil (progn ...
508
509 ;; SETF functions
510
511 (defun (setf sun) (value x y &key k1)
512   (declare (boolean value))
513   (declare (fixnum x y))
514   (declare (boolean k1))
515   (declare (ignore x y k1))
516   value)
517
518 (deftest function-type+setf.1
519     (values (type-equal (function-type '(setf sun))
520                         (function-type #'(setf sun)))
521             (type-equal (function-type '(setf sun))
522                         '(function ((member nil t)
523                                     fixnum fixnum
524                                     &key (:k1 (member nil t)))
525                           (values (member nil t) &optional))))
526   t t)
527
528 ;; Misc
529
530 (deftest function-type+misc.1
531     (flet ((nullary ()))
532       (type-equal (function-type #'nullary)
533                   '(function () (values null &optional))))
534   t)
535
536 ;;; Defstruct accessor, copier, and predicate
537
538 (deftest defstruct-fun-sources
539     (let ((copier (find-definition-source #'cl-user::copy-three))
540           (accessor (find-definition-source #'cl-user::three-four))
541           (predicate (find-definition-source #'cl-user::three-p)))
542       (values (and (equalp copier accessor)
543                    (equalp copier predicate))
544               (equal "test.lisp"
545                      (file-namestring (definition-source-pathname copier)))
546               (equal '(5)
547                      (definition-source-form-path copier))))
548  t
549  t
550  t)
551
552 (deftest defstruct-fun-sources-by-name
553     (let ((copier (car (find-definition-sources-by-name 'cl-user::copy-three :function)))
554           (accessor (car (find-definition-sources-by-name 'cl-user::three-four :function)))
555           (predicate (car (find-definition-sources-by-name 'cl-user::three-p :function))))
556       (values (and (equalp copier accessor)
557                    (equalp copier predicate))
558               (equal "test.lisp"
559                      (file-namestring (definition-source-pathname copier)))
560               (equal '(5)
561                      (definition-source-form-path copier))))
562  t
563  t
564  t)