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