File: interface.impure.lisp

package info (click to toggle)
sbcl 2%3A2.6.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 52,008 kB
  • sloc: lisp: 535,135; ansic: 42,629; sh: 5,737; asm: 2,406; pascal: 717; makefile: 432; python: 56; cpp: 27
file content (565 lines) | stat: -rw-r--r-- 22,448 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
;;;; tests for problems in the interface presented to the user/programmer

;;;; This software is part of the SBCL system. See the README file for
;;;; more information.
;;;;
;;;; While most of SBCL is derived from the CMU CL system, the test
;;;; files (like this one) were written from scratch after the fork
;;;; from CMU CL.
;;;;
;;;; This software is in the public domain and is provided with
;;;; absolutely no warranty. See the COPYING and CREDITS files for
;;;; more information.

(defmacro silently (&rest things)
  `(let ((*standard-output* (make-broadcast-stream))) ,@things))

;; Interpreted closure is a problem for COMPILE
(with-test (:name (disassemble function) :skipped-on :interpreter)
  ;; DISASSEMBLE shouldn't fail on closures or unpurified functions
  (defun disassemble-fun (x) x)
  (silently (disassemble 'disassemble-fun)))

(with-test (:name (disassemble :closure) :skipped-on :interpreter)
  (let ((x 1)) (defun disassemble-closure (y) (if y (setq x y) x)))
  (silently (disassemble 'disassemble-closure)))

(defun interpreted-function-p (x) (typep x 'sb-kernel:interpreted-function))

#+(or sb-eval sb-fasteval)
(with-test (:name (disassemble :interpreted))
    ;; Nor should it fail on interpreted functions
    (let ((sb-ext:*evaluator-mode* :interpret))
      (eval `(defun disassemble-eval (x) x))
      (silently (disassemble 'disassemble-eval)))

    ;; disassemble-eval should still be an interpreted function.
    ;; clhs disassemble: "(If that function is an interpreted function,
    ;; it is first compiled but the result of this implicit compilation
    ;; is not installed.)"
    (assert (interpreted-function-p (symbol-function 'disassemble-eval))))

(with-test (:name (disassemble generic-function))
  ;; nor should it fail on generic functions or other funcallable instances
  (defgeneric disassemble-generic (x))
  (silently (disassemble 'disassemble-generic))
  (let ((fin (make-instance 'sb-mop:funcallable-standard-object)))
    (silently (disassemble fin))))

;;; while we're at it, much the same applies to
;;; FUNCTION-LAMBDA-EXPRESSION:
(defun fle-fun (x) x)

(let ((x 1)) (defun fle-closure (y) (if y (setq x y) x)))

(defclass non-standard-generic-function (generic-function) ()
  (:metaclass sb-mop:funcallable-standard-class))
(defmethod sb-mop:generic-function-name ((generic-function non-standard-generic-function))
  'name)

(with-test (:name function-lambda-expression)
  (flet ((fle-name (x)
           (nth-value 2 (function-lambda-expression x))))
    (assert (eql (fle-name #'fle-fun) 'fle-fun))
    (assert (eql (fle-name #'fle-closure) 'fle-closure))
    (assert (eql (fle-name #'disassemble-generic) 'disassemble-generic))
    (function-lambda-expression
     (make-instance 'sb-mop:funcallable-standard-object))
    (function-lambda-expression
     (make-instance 'non-standard-generic-function))
    (function-lambda-expression
     (make-instance 'standard-generic-function))
    #+(or sb-eval sb-fasteval)
    (progn
      (let ((sb-ext:*evaluator-mode* :interpret))
        (eval `(defun fle-eval (x) x))
        (assert (eql (fle-name (symbol-function 'fle-eval)) 'fle-eval)))

      ;; fle-eval should still be an interpreted function.
      (assert (interpreted-function-p (symbol-function 'fle-eval))))))

(with-test (:name :nested-function-lambda-expression)
  (let* ((lexpr
          '(lambda ()
            (labels ((f1 (z &rest r &key (b (eval 'foo)) (a 3) w)
                       (- a w (length r) (f2 (/  z b))))
                     (f2 (w) (* w .3)))
              (values #'f1 #'f2))))
         (f (compile nil lexpr)))
    (assert (equal (function-lambda-expression f) lexpr))
    (assert (equal (function-lambda-expression (funcall f))
                   '(lambda (z &rest r &key (b (eval 'foo)) (a 3) w)
                     (block f1 (- a w (length r) (f2 (/ z b)))))))
    (assert (equal (function-lambda-expression
                    (nth-value 1 (funcall f)))
                   '(lambda (w) (block f2 (* w 0.3)))))))

;;; Tests of documentation on types and classes

(defun assert-documentation (thing doc-type expected)
  ;; This helper function makes ASSERT errors print THING, DOC-TYPE,
  ;; the return value of DOCUMENTATION and EXPECTED.
  (flet ((assert-documentation-helper (thing doc-type documentation expected)
           (declare (ignore thing doc-type))
           (equal documentation expected)))
    (assert (assert-documentation-helper
             thing doc-type (documentation thing doc-type) expected))))

(defpackage #:documentation.package
  (:documentation "PACKAGE"))

(with-test (:name (documentation package))
  (assert-documentation (find-package '#:documentation.package) t "PACKAGE")
  (setf (documentation (find-package '#:documentation.package) t) "PACKAGE2")
  (assert-documentation (find-package '#:documentation.package) t "PACKAGE2"))

(defclass foo ()
  ()
  (:documentation "FOO"))

(defclass documentation.funcallable-instance ()
  ()
  (:metaclass sb-mop:funcallable-standard-class)
  (:documentation "FEZ"))

(defstruct bar "BAR")

(define-condition baz ()
  ()
  (:documentation "BAZ"))

(macrolet
    ((do-class (name expected &optional structurep)
       `(progn
          (assert-documentation ',name 'type ,expected)
          (assert-documentation (find-class ',name) 'type ,expected)
          (assert-documentation (find-class ',name) 't ,expected)
          ,@(when structurep
              `((assert-documentation ',name 'structure ,expected)))

          (let ((new1 (symbol-name (gensym "NEW1")))
                (new2 (symbol-name (gensym "NEW2")))
                (new3 (symbol-name (gensym "NEW3")))
                (new4 (symbol-name (gensym "NEW4"))))
            (declare (ignorable new4))
            (setf (documentation ',name 'type) new1)
            (assert-documentation (find-class ',name) 'type new1)
            (setf (documentation (find-class ',name) 'type) new2)
            (assert-documentation (find-class ',name) 't new2)
            (setf (documentation (find-class ',name) 't) new3)
            (assert-documentation ',name 'type new3)
            ,@(when structurep
                `((assert-documentation ',name 'structure new3)
                  (setf (documentation ',name 'structure) new4)
                  (assert-documentation ',name 'structure new4)))))))

  (with-test (:name (documentation class standard-class))
    (do-class foo "FOO"))

  (with-test (:name (documentation class sb-mop:funcallable-standard-class))
    (do-class documentation.funcallable-instance "FEZ"))

  (with-test (:name (documentation struct 1))
    (do-class bar "BAR" t))

  (with-test (:name (documentation condition))
    (do-class baz "BAZ")))

(defclass documentation-metaclass (standard-class)
  ()
  (:documentation "metaclass with methods on DOCUMENTATION."))

(defmethod documentation ((thing documentation-metaclass)
                          (doc-type (eql 't)))
  (sb-int:awhen (call-next-method)
    (concatenate 'string ":" sb-int:it)))

(defmethod (setf documentation) (new-value
                                 (thing documentation-metaclass)
                                 (doc-type (eql 't)))
  (call-next-method (when new-value
                      (substitute #\! #\. new-value))
                    thing doc-type))

(defmethod sb-mop:validate-superclass ((class documentation-metaclass)
                                       (superclass standard-class))
  t)

(defclass documentation-class ()
  ()
  (:metaclass documentation-metaclass)
  (:documentation "normal"))

(with-test (:name (documentation :non-stanadard :metaclass))
  (flet ((check (expected class-name)
           (let ((class (find-class class-name)))
             (assert-documentation class-name 'type expected)
             (assert-documentation class 'type expected)
             (assert-documentation class t expected))))
    ;; Make sure methods specialized on the metaclass are not bypassed
    ;; when retrieving and modifying class documentation.
    (check ":normal" 'documentation-class)
    (setf (documentation 'documentation-class 'type) "2.")
    (check ":2!" 'documentation-class)
    (setf (documentation 'documentation-class 'type) nil)
    (check nil 'documentation-class)

    ;; Sanity check: make sure the metaclass has its own documentation
    ;; and is not affected by the above modifications.
    (check "metaclass with methods on DOCUMENTATION."
           'documentation-metaclass)))

(defstruct (frob (:type vector)) "FROB")

(with-test (:name (documentation struct 2))
  (assert-documentation 'frob 'structure "FROB")
  (setf (documentation 'frob 'structure) "NEW5")
  (assert-documentation 'frob 'structure "NEW5"))

(deftype quux ()
  "QUUX"
  't)

(with-test (:name (documentation type))
  (assert-documentation 'quux 'type "QUUX")
  (setf (documentation 'quux 'type) "NEW4")
  (assert-documentation 'quux 'type "NEW4"))

(define-compiler-macro cmacro (x)
  "compiler macro"
  x)

(define-compiler-macro (setf cmacro) (y x)
  "setf compiler macro"
  (declare (ignore x))
  y)

(with-test (:name (documentation compiler-macro))
  (assert-documentation 'cmacro 'compiler-macro "compiler macro")
  (assert-documentation '(setf cmacro) 'compiler-macro "setf compiler macro"))

(defun (setf documentation.setf) (x)
  "(setf foo) documentation"
  x)

(with-test (:name (documentation function setf))
  (flet ((expect (documentation)
           (assert-documentation
            '(setf documentation.setf) 'function documentation)
           (assert-documentation
            #'(setf documentation.setf) 'function documentation)
           (assert-documentation
            #'(setf documentation.setf) t documentation)))
    (expect "(setf foo) documentation")
    ;; The original test checked this twice. No idea why.
    (expect "(setf foo) documentation")

    ;; Modification
    (setf (documentation '(setf documentation.setf) 'function)
          "(setf bar) documentation")
    (expect "(setf bar) documentation")

    (setf (documentation #'(setf documentation.setf) 'function)
          "(setf baz) documentation")
    (expect "(setf baz) documentation")

    (setf (documentation #'(setf documentation.setf) t)
          "(setf fez) documentation")
    (expect "(setf fez) documentation")))

(with-test (:name (documentation lambda))
  (let ((f (lambda () "aos the zos" t))
        (g (sb-int:named-lambda fii () "zoot the fruit" t)))
    (dolist (doc-type '(t function))
      (assert-documentation f doc-type "aos the zos")
      (assert-documentation g doc-type "zoot the fruit"))
    (setf (documentation f t) "fire")
    (assert-documentation f t "fire")
    (assert-documentation g t "zoot the fruit")))

(with-test (:name (documentation flet))
  (assert
   (string= (documentation
             (flet ((quux (x)
                      "this is FLET quux"
                      (/ x 2)))
               #'quux)
             t)
            "this is FLET quux")))

(with-test (:name (documentation labels))
  (assert
   (string= (documentation
             (labels ((rec (x)
                        "this is LABELS rec"
                        (if (plusp x)
                            (* x (rec (1- x)))
                            1)))
               #'rec)
             t)
            "this is LABELS rec")))

(let ((x 1))
  (defun docfoo (y)
    "bar"
    (incf x y)))

(with-test (:name (documentation :closure))
  (assert-documentation 'docfoo 'function "bar")
  (assert (string= (setf (documentation 'docfoo 'function) "baz") "baz"))
  (assert-documentation 'docfoo 'function "baz")
  (assert-documentation #'docfoo t "baz")
  (assert (string= (setf (documentation #'docfoo t) "zot") "zot"))
  (assert-documentation #'docfoo t "zot")
  (assert-documentation 'docfoo 'function "zot")
  (assert (not (setf (documentation 'docfoo 'function) nil)))
  (assert-documentation 'docfoo 'function nil))

(with-test (:name (documentation :built-in-macro) :skipped-on (not :sb-doc))
  (assert (documentation 'trace 'function)))

(with-test (:name (documentation :built-in-function) :skipped-on (not :sb-doc))
  (assert (documentation 'cons 'function)))

(defvar documentation.variable nil
  "foo variable documentation")

(with-test (:name (documentation variable))
  (assert-documentation 'documentation.variable 'variable
                        "foo variable documentation")
  (setf (documentation 'documentation.variable 'variable)
        "baz variable documentation")
  (assert-documentation 'documentation.variable 'variable
                        "baz variable documentation"))

(with-test (:name (documentation :mismatch-for-function))
  (defun test ()
    "X"
    nil)
  (setf (symbol-function 'test2) #'test)
  (setf (documentation 'test 'function) "Y")
  (assert (equal (documentation #'test t)
                 (documentation 'test 'function)))
  (setf (documentation 'test2 'function) "Z")
  (assert (not
           (equal (documentation 'test 'function)
                  (documentation 'test2 'function)))))

(with-test (:name (documentation setf :on nil))
  (assert
   (handler-case
       (assert (equal (setf (documentation nil 'function) "foo") "foo"))
     (style-warning () t)
     (:no-error (x)
       (declare (ignore x))
       nil))))

(with-test (:name (describe generic-function :assumed-type))
  ;; Signalled an error at one point
  (let ((fun (checked-compile '(lambda ()
                                 (flet ((zoo () (gogo)))
                                   (defmethod gogo () nil)
                                   (describe 'gogo)))
                              :allow-style-warnings t)))
    (handler-bind ((warning #'muffle-warning)) ; implicit gf
      (silently (funcall fun)))))

(defmacro bug-643958-test ()
  "foo"
  :ding!)

(with-test (:name :bug-643958)
  (assert (equal "foo" (documentation 'bug-643958-test 'function)))
  (setf (documentation 'bug-643958-test 'function) "bar")
  (assert (equal "bar" (documentation 'bug-643958-test 'function))))

(with-test (:name (:endianness :in *features*))
  (assert
   (or (member :big-endian *features*)
       (member :little-endian *features*))))

(with-test (:name (apropos :inherited :bug-1364413))
  (let* ((package (make-package "BUGGALO" :use nil))
         (symbol (intern "BUGGALO" package)))
    (export (list symbol) package)
    (let ((inherits (make-package "BUGGALO-INHERITS" :use (list package))))
      (assert (= (length (apropos-list "BUGGALO" package)) 1))
      (assert (= (length (apropos-list "BUGGALO" inherits)) 1))
      (delete-package inherits))
    (delete-package package)))

(with-test (:name (apropos :inherited :external-only :bug-1364413))
  (let* ((package (make-package "BUGGALO" :use nil))
         (symbol (intern "BUGGALO" package)))
    (export (list symbol) package)
    (let ((inherits (make-package "BUGGALO-INHERITS" :use (list package))))
      (assert (= (length (apropos-list "BUGGALO" package t)) 1))
      (assert (= (length (apropos-list "BUGGALO" inherits t)) 0))
      (delete-package inherits))
    (delete-package package)))

(with-test (:name (apropos :once-only))
  (assert (= (length (apropos-list "UPDATE-INSTANCE-FOR-REDEFINED-CLASS")) 1)))

(defgeneric gf-arglist-1 (x &key y))
(defmethod gf-arglist-1 (x &key (y nil) (z nil z-p))
  (list x y z z-p))

(defgeneric gf-arglist-2 (x &key y))
(defmethod gf-arglist-2 ((x integer) &key (y nil) ((z f) nil z-p)) (list x y f z-p))
(defmethod gf-arglist-2 ((x string) &key (y nil) ((z w) nil z-p)) (list x y w z-p))

(defgeneric gf-arglist-3 (x &key ((:y y))))

(defgeneric gf-arglist-4 (x &key ((:y z))))

(defgeneric gf-arglist-5 (x &key y))
(defmethod gf-arglist-5 ((x integer) &key z &allow-other-keys) (list x z))

(with-test (:name (:generic-function-pretty-arglist 1))
  (assert (equal (sb-pcl::generic-function-pretty-arglist #'gf-arglist-1)
                 '(x &key y z))))
(with-test (:name (:generic-function-pretty-arglist 2))
  (assert (or (equal (sb-pcl::generic-function-pretty-arglist #'gf-arglist-2)
                     '(x &key y ((z w))))
              (equal (sb-pcl::generic-function-pretty-arglist #'gf-arglist-2)
                     '(x &key y ((z f)))))))
(with-test (:name (:generic-function-pretty-arglist 3))
  (assert (equal (sb-pcl::generic-function-pretty-arglist #'gf-arglist-3)
                 '(x &key y))))
(with-test (:name (:generic-function-pretty-arglist 4))
  (assert (equal (sb-pcl::generic-function-pretty-arglist #'gf-arglist-4)
                 '(x &key ((:y z))))))
(with-test (:name (:generic-function-pretty-arglist 5))
  (assert (equal (sb-pcl::generic-function-pretty-arglist #'gf-arglist-5)
                 '(x &key y z &allow-other-keys))))

(defgeneric traced-gf (x))
(defmethod traced-gf (x) (1+ x))

(with-test (:name (trace generic-function))
  (assert (= (traced-gf 3) 4))
  (trace traced-gf)
  (let ((output (with-output-to-string (*trace-output*)
                  (assert (= (traced-gf 3) 4)))))
    (assert (> (length output) 0))
    (assert (search "0: (TRACED-GF 3)" output))
    (assert (search "0: TRACED-GF returned 4" output)))
  (assert (typep #'traced-gf 'standard-generic-function))
  (untrace traced-gf)
  (let ((output (with-output-to-string (*trace-output*)
                  (assert (= (traced-gf 3) 4)))))
    (assert (= (length output) 0))))

(untrace traced-gf)
(with-test (:name (trace generic-function :methods t))
  (trace :methods t traced-gf)
  (let ((output (with-output-to-string (*trace-output*)
                  (assert (= (traced-gf 4) 5)))))
    (assert (> (length output) 0))
    (assert (search "0: (TRACED-GF 4)" output))
    (assert (search "0: TRACED-GF returned 5" output))
    (assert (search "1: ((METHOD TRACED-GF (T)) 4)" output))
    (assert (search "1: (METHOD TRACED-GF (T)) returned 5" output)))
  (untrace traced-gf)
  (let ((output (with-output-to-string (*trace-output*)
                  (assert (= (traced-gf 3) 4)))))
    (assert (= (length output) 0))))

(defmethod traced-gf :before (x) :before)
(defmethod traced-gf :after ((x integer)) :after)

(untrace traced-gf)
(with-test (:name (trace generic-function :methods :combined))
  (trace :methods t traced-gf)
  (let ((output (with-output-to-string (*trace-output*)
                  (assert (= (traced-gf 5) 6)))))
    (assert (> (length output) 0))
    (assert (search "0: (TRACED-GF 5)" output))
    (assert (search "0: TRACED-GF returned 6" output))
    (assert (search "1: ((SB-PCL::COMBINED-METHOD TRACED-GF) 5)" output))
    (assert (search "1: (SB-PCL::COMBINED-METHOD TRACED-GF) returned 6" output))
    (assert (search "2: ((METHOD TRACED-GF :BEFORE (T)) 5)" output))
    (assert (search "2: (METHOD TRACED-GF :BEFORE (T)) returned :BEFORE" output))
    (assert (search "2: ((METHOD TRACED-GF :AFTER (INTEGER)) 5)" output))
    (assert (search "2: (METHOD TRACED-GF :AFTER (INTEGER)) returned :AFTER" output))
    (assert (search "2: ((METHOD TRACED-GF (T)) 5)" output))
    (assert (search "2: (METHOD TRACED-GF (T)) returned 6" output)))
  (untrace traced-gf)
  (let ((output (with-output-to-string (*trace-output*)
                  (assert (= (traced-gf 3) 4)))))
    (assert (= (length output) 0))))

(let* ((mf (lambda (args nms)
             (declare (ignore nms))
             (* 2 (car args))))
       (m (make-instance 'standard-method
                         :specializers (list (find-class 'integer))
                         :qualifiers nil
                         :lambda-list '(x)
                         :function mf)))
  (add-method #'traced-gf m))

(untrace traced-gf)
(with-test (:name (trace generic-function :methods :method-function))
  (trace :methods t traced-gf)
  (let ((output (with-output-to-string (*trace-output*)
                  (assert (= (traced-gf 5) 10)))))
    (assert (> (length output) 0))
    (assert (search "0: (TRACED-GF 5)" output))
    (assert (search "0: TRACED-GF returned 10" output))
    (assert (search "1: ((SB-PCL::COMBINED-METHOD TRACED-GF) 5)" output))
    (assert (search "1: (SB-PCL::COMBINED-METHOD TRACED-GF) returned 10" output))
    (assert (search "2: ((METHOD TRACED-GF :BEFORE (T)) 5)" output))
    (assert (search "2: (METHOD TRACED-GF :BEFORE (T)) returned :BEFORE" output))
    (assert (search "2: ((METHOD TRACED-GF :AFTER (INTEGER)) 5)" output))
    (assert (search "2: (METHOD TRACED-GF :AFTER (INTEGER)) returned :AFTER" output))
    (assert (search "2: ((METHOD TRACED-GF (INTEGER)) 5)" output))
    (assert (search "2: (METHOD TRACED-GF (INTEGER)) returned 10" output)))
  (untrace traced-gf)
  (let ((output (with-output-to-string (*trace-output*)
                  (assert (= (traced-gf 5) 10)))))
    (assert (= (length output) 0))))

(with-test (:name :undefined-fun-macro-error)
  (assert (search "is a macro" (princ-to-string (make-condition 'undefined-function :name 'cond)))))

#+gc-stress (invoke-restart 'run-tests::skip-file)

(defun testme (a b) (values "nice" (+ a b)))
(compile 'testme)
(defparameter trace-this-f1 #'testme)
(sb-int:encapsulate-funobj trace-this-f1 'testme)

(defun funky (a b c) (lambda (z) (values "nice" a b (+ (incf a) (decf c) z))))
(compile 'funky)
(defparameter trace-this-f2 (funky 10 'wat 19))
(setf (symbol-function 'funky-closure) trace-this-f2)
(sb-int:encapsulate-funobj trace-this-f2 'trace-this-f2)

(with-test (:name :trace-funobj-encapsulation)
  (assert (search "returned \"nice\""
                  (with-output-to-string (*trace-output*) (funcall trace-this-f1 1 2))))
  (assert (search "returned \"nice\""
                  (with-output-to-string (*trace-output*) (testme 3 4))))
  (assert (search "returned \"nice\""
                  (with-output-to-string (*trace-output*) (funcall trace-this-f2 1))))
  (assert (search "returned \"nice\""
                  (with-output-to-string (*trace-output*) (funky-closure 5)))))

;;; https://bugs.launchpad.net/sbcl/+bug/1850531
(with-test (:name :describe-function-not-named-by-designator)
  (describe (formatter "~&~A~A") (make-broadcast-stream))) ; should not crash

(defun test-intercepted-load (arg) (apply #'load arg (list :foo :bar :allow-other-keys t)))
(compile 'test-intercepted-load)
(sb-int:encapsulate 'load 'interceptor
 (compile nil '(lambda (realfun pathname &rest things)
                (if (eq pathname :testme)
                    :yes
                    (apply realfun pathname things)))))

(with-test (:name :load-encapsulatable)
  (assert (eq (test-intercepted-load :testme) :yes)))

;;;; success