aa7bfc8cc3a53a44fbe92b35a3ae59bee6b30eec
[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 (defmacro deftest* ((name &key fails-on) form &rest results)
16   `(progn
17      (when (sb-impl::featurep ',fails-on)
18        (pushnew ',name sb-rt::*expected-failures*))
19      (deftest ,name ,form ,@results)))
20
21 (deftest function-lambda-list.1
22     (function-lambda-list 'cl-user::one)
23   (cl-user::a cl-user::b cl-user::c))
24
25 (deftest function-lambda-list.2
26     (function-lambda-list 'the)
27   (sb-c::value-type sb-c::form))
28
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))
32
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)
39                          "OUT")
40                   plist)))
41   t t)
42
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")
47                   plist)
48               (or (equal (getf plist :test-inner) "IN")
49                   plist)))
50   t t)
51
52 (defun matchp (object form-number)
53   (let ((ds (sb-introspect:find-definition-source object)))
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-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))
61          (= form-number
62             (first (sb-introspect:definition-source-form-path ds))))))
63
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)))
67
68 (deftest find-source-stuff.1
69     (matchp-name :function 'cl-user::one 2)
70   t)
71
72 (deftest find-source-stuff.2
73     (matchp #'cl-user::one 2)
74   t)
75
76 (deftest find-source-stuff.3
77     (matchp-name :generic-function 'cl-user::two 3)
78   t)
79
80 (deftest find-source-stuff.4
81     (matchp (car (sb-pcl:generic-function-methods #'cl-user::two)) 4)
82   t)
83
84 (deftest find-source-stuff.5
85     (matchp-name :variable 'cl-user::*a* 8)
86   t)
87
88 (deftest find-source-stuff.6
89     (matchp-name :variable 'cl-user::*b* 9)
90   t)
91
92 (deftest find-source-stuff.7
93     (matchp-name :class 'cl-user::a 10)
94   t)
95
96 (deftest find-source-stuff.8
97     (matchp-name :condition 'cl-user::b 11)
98   t)
99
100 (deftest find-source-stuff.9
101     (matchp-name :structure 'cl-user::c 12)
102   t)
103
104 (deftest find-source-stuff.10
105     (matchp-name :function 'cl-user::make-c 12)
106   t)
107
108 (deftest find-source-stuff.11
109     (matchp-name :function 'cl-user::c-e 12)
110   t)
111
112 (deftest find-source-stuff.12
113     (matchp-name :structure 'cl-user::d 13)
114   t)
115
116 (deftest find-source-stuff.13
117     (matchp-name :function 'cl-user::make-d 13)
118   t)
119
120 (deftest find-source-stuff.14
121     (matchp-name :function 'cl-user::d-e 13)
122   t)
123
124 (deftest find-source-stuff.15
125     (matchp-name :package 'cl-user::e 14)
126   t)
127
128 (deftest find-source-stuff.16
129     (matchp-name :symbol-macro 'cl-user::f 15)
130   t)
131
132 (deftest find-source-stuff.17
133     (matchp-name :type 'cl-user::g 16)
134   t)
135
136 (deftest find-source-stuff.18
137     (matchp-name :constant 'cl-user::+h+ 17)
138   t)
139
140 (deftest find-source-stuff.19
141     (matchp-length :method 'cl-user::j 2)
142   t)
143
144 (deftest find-source-stuff.20
145     (matchp-name :macro 'cl-user::l 20)
146   t)
147
148 (deftest find-source-stuff.21
149     (matchp-name :compiler-macro 'cl-user::m 21)
150   t)
151
152 (deftest find-source-stuff.22
153     (matchp-name :setf-expander 'cl-user::n 22)
154   t)
155
156 (deftest find-source-stuff.23
157     (matchp-name :function  '(setf cl-user::o) 23)
158   t)
159
160 (deftest find-source-stuff.24
161     (matchp-name :method  '(setf cl-user::p) 24)
162   t)
163
164 (deftest find-source-stuff.25
165     (matchp-name :macro  'cl-user::q 25)
166   t)
167
168
169 (deftest find-source-stuff.26
170     (matchp-name :method-combination 'cl-user::r 26)
171   t)
172
173
174 (deftest find-source-stuff.27
175     (matchp-name :setf-expander 'cl-user::s 27)
176   t)
177
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)
181       (matchp fin 2))
182   t)
183
184 (deftest find-source-stuff.29
185     (unwind-protect
186          (progn
187            (sb-profile:profile cl-user::one)
188            (matchp-name :function 'cl-user::one 2))
189       (sb-profile:unprofile cl-user::one))
190   t)
191
192 (deftest find-source-stuff.30
193     ;; Test finding a type that isn't one
194     (not (find-definition-sources-by-name 'fboundp :type))
195   t)
196
197 (deftest find-source-stuff.31
198     (matchp-name :function 'cl-user::compile-time-too-fun 28)
199   t)
200
201 (deftest find-source-stuff.32
202     (matchp-name :function 'cl-user::loaded-as-source-fun 3)
203   t)
204
205 ;;; Check wrt. interplay of generic functions and their methods.
206
207 (defgeneric xuuq (gf.a gf.b          &rest gf.rest &key gf.k-X))
208 (defmethod  xuuq ((m1.a number) m1.b &rest m1.rest &key gf.k-X m1.k-Y m1.k-Z)
209   (declare (ignore m1.a m1.b m1.rest gf.k-X m1.k-Y m1.k-Z))
210   'm1)
211 (defmethod  xuuq ((m2.a string) m2.b &rest m2.rest &key gf.k-X m1.k-Y m2.k-Q)
212   (declare (ignore m2.a m2.b m2.rest gf.k-X m1.k-Y m2.k-Q))
213   'm2)
214
215 ;; XUUQ's lambda list should look similiar to
216 ;;
217 ;;    (GF.A GF.B &REST GF.REST &KEY GF.K-X M1.K-Z M1.K-Y M2.K-Q)
218 ;;
219 (deftest gf-interplay.1
220     (multiple-value-bind (required optional restp rest keyp keys allowp
221                                 auxp aux morep more-context more-count)
222         (sb-int:parse-lambda-list (function-lambda-list #'xuuq))
223       (and (equal required '(gf.a gf.b))
224            (null optional)
225            (and restp (eql rest 'gf.rest))
226            (and keyp
227                 (member 'gf.k-X keys)
228                 (member 'm1.k-Y keys)
229                 (member 'm1.k-Z keys)
230                 (member 'm2.k-Q keys))
231            (not allowp)
232            (and (not auxp) (null aux))
233            (and (not morep) (null more-context) (not more-count))))
234   t)
235
236 ;;; Check what happens when there's no explicit DEFGENERIC.
237
238 (defmethod kroolz (r1 r2 &optional opt &aux aux)
239   (declare (ignore r1 r2 opt aux))
240   'kroolz)
241
242 (deftest gf-interplay.2
243     (equal (function-lambda-list #'kroolz) '(r1 r2 &optional opt))
244   t)
245
246 ;;;; Check correctness of DEFTYPE-LAMBDA-LIST.
247 (deftype foobar-type
248     (&whole w &environment e r1 r2 &optional o &rest rest &key k1 k2 k3)
249   (declare (ignore w e r1 r2 o rest k1 k2 k3))
250   nil)
251
252 (deftest deftype-lambda-list.1
253     (deftype-lambda-list 'foobar-type)
254   (&whole w &environment e r1 r2 &optional o &rest rest &key k1 k2 k3)
255   t)
256
257 (deftest deftype-lambda-list.2
258     (deftype-lambda-list (gensym))
259   nil
260   nil)
261
262 ;; ARRAY is a primitive type with associated translator function.
263 (deftest deftype-lambda-list.3
264     (deftype-lambda-list 'array)
265   (&optional (sb-kernel::element-type '*) (sb-kernel::dimensions '*))
266   t)
267
268 ;; VECTOR is a primitive type that is defined by means of DEFTYPE.
269 (deftest deftype-lambda-list.4
270     (deftype-lambda-list 'vector)
271   (&optional sb-kernel::element-type sb-kernel::size)
272   t)
273
274 ;;; Test allocation-information
275
276 (defun tai (x kind info &key ignore)
277   (multiple-value-bind (kind2 info2) (sb-introspect:allocation-information x)
278     (unless (eq kind kind2)
279       (error "wanted ~S, got ~S" kind kind2))
280     (when (not (null ignore))
281       (setf info2 (copy-list info2))
282       (dolist (key ignore)
283         (remf info2 key))
284       (setf info (copy-list info))
285       (dolist (key ignore)
286         (remf info key)))
287     (equal info info2)))
288
289 (deftest allocation-infromation.1
290     (tai nil :heap '(:space :static))
291   t)
292
293 (deftest allocation-information.2
294     (tai t :heap '(:space :static))
295   t)
296
297 (deftest allocation-information.3
298     (tai 42 :immediate nil)
299   t)
300
301 ;;; Skip the whole damn test on GENCGC PPC -- the combination is just
302 ;;; to flaky for this to make too much sense.  GENCGC SPARC almost
303 ;;; certainly exhibits the same behavior patterns (or antipatterns) as
304 ;;; GENCGC PPC.
305 ;;;
306 ;;; -- It appears that this test can also fail due to systematic issues
307 ;;; (possibly with the C compiler used) which we cannot detect based on
308 ;;; *features*.  Until this issue has been fixed, I am marking this test
309 ;;; as failing on Windows to allow installation of the contrib on
310 ;;; affected builds, even if the underlying issue is (possibly?) not even
311 ;;; strictly related to windows.  C.f. lp1057631.  --DFL
312 ;;;
313 (deftest* (allocation-information.4
314            ;; Ignored as per the comment above, even though it seems
315            ;; unlikely that this is the right condition.
316            :fails-on (or :win32 (and (or :ppc :sparc) :gencgc)))
317     #+gencgc
318     (tai #'cons :heap
319          ;; FIXME: This is the canonical GENCGC result. On PPC we sometimes get
320          ;; :LARGE T, which doesn't seem right -- but ignore that for now.
321          `(:space :dynamic :generation ,sb-vm:+pseudo-static-generation+
322            :write-protected t :boxed t :pinned nil :large nil)
323          :ignore (list :page #+ppc :large))
324     #-gencgc
325     (tai :cons :heap
326          ;; FIXME: Figure out what's the right cheney-result. SPARC at least
327          ;; has exhibited both :READ-ONLY and :DYNAMIC, which seems wrong.
328          '()
329          :ignore '(:space))
330   t)
331
332 #+sb-thread
333 (deftest allocation-information.thread.1
334     (let ((x (list 1 2 3)))
335       (declare (dynamic-extent x))
336       (tai x :stack sb-thread:*current-thread*))
337   t)
338
339 #+sb-thread
340 (progn
341    (defun thread-tai ()
342      (let ((x (list 1 2 3)))
343        (declare (dynamic-extent x))
344        (let ((child (sb-thread:make-thread
345                      (lambda ()
346                        (sb-introspect:allocation-information x)))))
347          (equal (list :stack sb-thread:*current-thread*)
348                 (multiple-value-list (sb-thread:join-thread child))))))
349
350    (deftest allocation-information.thread.2
351        (thread-tai)
352      t)
353
354    (defun thread-tai2 ()
355      (let* ((sem (sb-thread:make-semaphore))
356             (obj nil)
357             (child (sb-thread:make-thread
358                     (lambda ()
359                       (let ((x (list 1 2 3)))
360                         (declare (dynamic-extent x))
361                         (setf obj x)
362                         (sb-thread:wait-on-semaphore sem)))
363                     :name "child")))
364        (loop until obj)
365        (unwind-protect
366             (equal (list :stack child)
367                    (multiple-value-list
368                     (sb-introspect:allocation-information obj)))
369          (sb-thread:signal-semaphore sem)
370          (sb-thread:join-thread child))))
371
372    (deftest allocation-information.thread.3
373        (thread-tai2)
374      t))
375
376 ;;;; Test FUNCTION-TYPE
377
378 (defun type-equal (typespec1 typespec2)
379   (or (equal typespec1 typespec2)   ; TYPE= punts on &keywords in FTYPEs.
380       (sb-kernel:type= (sb-kernel:values-specifier-type typespec1)
381                        (sb-kernel:values-specifier-type typespec2))))
382
383 (defmacro interpret (form)
384   `(let ((sb-ext:*evaluator-mode* :interpret))
385      (eval ',form)))
386
387 ;; Functions
388
389 (declaim (ftype (function (integer &optional string) string) moon))
390 (defun moon (int &optional suffix)
391   (concatenate 'string (princ-to-string int) suffix))
392
393 (deftest function-type.1
394     (values (type-equal (function-type 'moon) (function-type #'moon))
395             (type-equal (function-type #'moon)
396                         '(function (integer &optional string)
397                           (values string &rest t))))
398   t t)
399
400 (defun sun (x y &key k1)
401   (declare (fixnum x y))
402   (declare (boolean k1))
403   (declare (ignore x y k1))
404   t)
405
406 (deftest function-type.2
407     (values (type-equal (function-type 'sun) (function-type #'sun))
408             (type-equal (function-type #'sun)
409                         '(function (fixnum fixnum &key (:k1 (member nil t)))
410                           (values (member t) &optional))))
411   t t)
412
413 ;; Local functions
414
415 (deftest function-type.5
416     (flet ((f (s)
417              (declare (symbol s))
418              (values (symbol-name s))))
419       (type-equal (function-type #'f)
420                   '(function (symbol) (values simple-string &optional))))
421   t)
422
423 ;; Closures
424
425 (deftest function-type.6
426     (let ((x 10))
427       (declare (fixnum x))
428       (flet ((closure (y)
429                (declare (fixnum y))
430                (setq x (+ x y))))
431         (type-equal (function-type #'closure)
432                     '(function (fixnum) (values fixnum &optional)))))
433   t)
434
435 ;; Anonymous functions
436
437 (deftest function-type.7
438     (type-equal (function-type #'(lambda (x) (declare (fixnum x)) x))
439                 '(function (fixnum) (values fixnum &optional)))
440   t)
441
442 ;; Interpreted functions
443
444 #+sb-eval
445 (deftest function-type.8
446     (type-equal (function-type (interpret (lambda (x) (declare (fixnum x)) x)))
447                 '(function (&rest t) *))
448   t)
449
450 ;; Generic functions
451
452 (defgeneric earth (x y))
453
454 (deftest function-type+gfs.1
455     (values (type-equal (function-type 'earth) (function-type #'earth))
456             (type-equal (function-type 'earth) '(function (t t) *)))
457   t t)
458
459 ;; Implicitly created generic functions.
460
461 ;; (FUNCTION-TYPE 'MARS) => FUNCTION at the moment. (1.0.31.26)
462
463 ;; See LP #520695.
464
465 (defmethod mars (x y) (+ x y))
466
467 #+ nil
468 (deftest function-type+gfs.2
469     (values (type-equal (function-type 'mars) (function-type #'mars))
470             (type-equal (function-type 'mars) '(function (t t) *)))
471   t t)
472
473 ;; DEFSTRUCT created functions
474
475 ;; These do not yet work because SB-KERNEL:%FUN-NAME does not work on
476 ;; functions defined by DEFSTRUCT. (1.0.35.x)
477
478 ;; See LP #520692.
479
480 #+nil
481 (progn
482
483   (defstruct (struct (:predicate our-struct-p)
484                      (:copier copy-our-struct))
485     (a 42 :type fixnum))
486
487   (deftest function-type+defstruct.1
488       (values (type-equal (function-type 'struct-a)
489                           (function-type #'struct-a))
490               (type-equal (function-type 'struct-a)
491                           '(function (struct) (values fixnum &optional))))
492     t t)
493
494   (deftest function-type+defstruct.2
495       (values (type-equal (function-type 'our-struct-p)
496                           (function-type #'our-struct-p))
497               (type-equal (function-type 'our-struct-p)
498                           '(function (t) (values (member t nil) &optional))))
499     t t)
500
501   (deftest function-type+defstruct.3
502       (values (type-equal (function-type 'copy-our-struct)
503                           (function-type #'copy-our-struct))
504               (type-equal (function-type 'copy-our-struct)
505                           '(function (struct) (values struct &optional))))
506     t t)
507
508   (defstruct (typed-struct :named (:type list)
509                            (:predicate typed-struct-p))
510     (a 42 :type fixnum))
511
512   (deftest function-type+defstruct.4
513       (values (type-equal (function-type 'typed-struct-a)
514                           (function-type #'typed-struct-a))
515               (type-equal (function-type 'typed-struct-a)
516                           '(function (list) (values fixnum &optional))))
517     t t)
518
519   (deftest function-type+defstruct.5
520       (values (type-equal (function-type 'typed-struct-p)
521                           (function-type #'typed-struct-p))
522               (type-equal (function-type 'typed-struct-p)
523                           '(function (t) (values (member t nil) &optional))))
524     t t)
525
526   ) ; #+nil (progn ...
527
528 ;; SETF functions
529
530 (defun (setf sun) (value x y &key k1)
531   (declare (boolean value))
532   (declare (fixnum x y))
533   (declare (boolean k1))
534   (declare (ignore x y k1))
535   value)
536
537 (deftest function-type+setf.1
538     (values (type-equal (function-type '(setf sun))
539                         (function-type #'(setf sun)))
540             (type-equal (function-type '(setf sun))
541                         '(function ((member nil t)
542                                     fixnum fixnum
543                                     &key (:k1 (member nil t)))
544                           (values (member nil t) &optional))))
545   t t)
546
547 ;; Misc
548
549 (deftest function-type+misc.1
550     (flet ((nullary ()))
551       (type-equal (function-type #'nullary)
552                   '(function () (values null &optional))))
553   t)
554
555 ;;; Defstruct accessor, copier, and predicate
556
557 (deftest defstruct-fun-sources
558     (let ((copier (find-definition-source #'cl-user::copy-three))
559           (accessor (find-definition-source #'cl-user::three-four))
560           (predicate (find-definition-source #'cl-user::three-p)))
561       (values (and (equalp copier accessor)
562                    (equalp copier predicate))
563               (equal "test.lisp"
564                      (file-namestring (definition-source-pathname copier)))
565               (equal '(5)
566                      (definition-source-form-path copier))))
567  t
568  t
569  t)
570
571 (deftest defstruct-fun-sources-by-name
572     (let ((copier (car (find-definition-sources-by-name 'cl-user::copy-three :function)))
573           (accessor (car (find-definition-sources-by-name 'cl-user::three-four :function)))
574           (predicate (car (find-definition-sources-by-name 'cl-user::three-p :function))))
575       (values (and (equalp copier accessor)
576                    (equalp copier predicate))
577               (equal "test.lisp"
578                      (file-namestring (definition-source-pathname copier)))
579               (equal '(5)
580                      (definition-source-form-path copier))))
581  t
582  t
583  t)