Small simplification to maybe_adjust_large_object()
[sbcl.git] / tests / interface.impure.lisp
blob67b1e05232e0053e503eadefb2192c333544c369
1 ;;;; tests for problems in the interface presented to the user/programmer
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
5 ;;;;
6 ;;;; While most of SBCL is derived from the CMU CL system, the test
7 ;;;; files (like this one) were written from scratch after the fork
8 ;;;; from CMU CL.
9 ;;;;
10 ;;;; This software is in the public domain and is provided with
11 ;;;; absolutely no warranty. See the COPYING and CREDITS files for
12 ;;;; more information.
14 (load "assertoid.lisp")
15 (load "test-util.lisp")
16 (use-package "ASSERTOID")
17 (use-package "TEST-UTIL")
19 (defmacro silently (&rest things)
20 `(let ((*standard-output* (make-broadcast-stream))) ,@things))
22 ;; Interpreted closure is a problem for COMPILE
23 (with-test (:name :disassemble :skipped-on :interpreter)
24 ;;; DISASSEMBLE shouldn't fail on closures or unpurified functions
25 (defun disassemble-fun (x) x)
26 (silently (disassemble 'disassemble-fun)))
28 (with-test (:name :disassemble-closure :skipped-on :interpreter)
29 (let ((x 1)) (defun disassemble-closure (y) (if y (setq x y) x)))
30 (silently (disassemble 'disassemble-closure)))
32 #+sb-eval
33 (eval-when (:compile-toplevel :load-toplevel :execute)
34 (import 'sb-eval:interpreted-function-p))
35 #+sb-fasteval
36 (eval-when (:compile-toplevel :load-toplevel :execute)
37 (import 'sb-interpreter:interpreted-function-p))
39 #+(or sb-eval sb-fasteval)
40 (with-test (:name :disassemble-interpreted)
41 ;; Nor should it fail on interpreted functions
42 (let ((sb-ext:*evaluator-mode* :interpret))
43 (eval `(defun disassemble-eval (x) x))
44 (silently (disassemble 'disassemble-eval)))
46 ;; disassemble-eval should still be an interpreted function.
47 ;; clhs disassemble: "(If that function is an interpreted function,
48 ;; it is first compiled but the result of this implicit compilation
49 ;; is not installed.)"
50 (assert (interpreted-function-p (symbol-function 'disassemble-eval))))
52 (with-test (:name :disassemble-generic)
53 ;; nor should it fail on generic functions or other funcallable instances
54 (defgeneric disassemble-generic (x))
55 (silently (disassemble 'disassemble-generic))
56 (let ((fin (make-instance 'sb-mop:funcallable-standard-object)))
57 (silently (disassemble fin))))
59 ;;; while we're at it, much the same applies to
60 ;;; FUNCTION-LAMBDA-EXPRESSION:
61 (defun fle-fun (x) x)
63 (let ((x 1)) (defun fle-closure (y) (if y (setq x y) x)))
65 (with-test (:name :function-lambda-expression)
66 (flet ((fle-name (x)
67 (nth-value 2 (function-lambda-expression x))))
68 (assert (eql (fle-name #'fle-fun) 'fle-fun))
69 (assert (eql (fle-name #'fle-closure) 'fle-closure))
70 (assert (eql (fle-name #'disassemble-generic) 'disassemble-generic))
71 (function-lambda-expression
72 (make-instance 'sb-mop:funcallable-standard-object))
73 (function-lambda-expression
74 (make-instance 'generic-function))
75 (function-lambda-expression
76 (make-instance 'standard-generic-function))
77 #+(or sb-eval sb-fasteval)
78 (progn
79 (let ((sb-ext:*evaluator-mode* :interpret))
80 (eval `(defun fle-eval (x) x))
81 (assert (eql (fle-name (symbol-function 'fle-eval)) 'fle-eval)))
83 ;; fle-eval should still be an interpreted function.
84 (assert (interpreted-function-p (symbol-function 'fle-eval))))))
87 ;;; support for DESCRIBE tests
88 (defstruct to-be-described a b)
89 (defclass forward-describe-class (forward-describe-ref) (a))
90 (let ((sb-ext:*evaluator-mode* :compile))
91 (eval `(let (x) (defun closure-to-describe () (incf x)))))
93 (with-test (:name (describe :empty-gf))
94 (assert-no-signal
95 (silently (describe (make-instance 'generic-function)))
96 warning)
97 (assert-signal
98 (silently (describe (make-instance 'standard-generic-function)))
99 warning))
101 ;;; DESCRIBE should run without signalling an error.
102 (with-test (:name (describe :no-error))
103 (silently
104 (describe (make-to-be-described))
105 (describe 12)
106 (describe "a string")
107 (describe 'symbolism)
108 (describe (find-package :cl))
109 (describe '(a list))
110 (describe #(a vector))
111 ;; bug 824974
112 (describe 'closure-to-describe)))
114 ;;; The DESCRIBE-OBJECT methods for built-in CL stuff should do
115 ;;; FRESH-LINE and TERPRI neatly.
116 (with-test (:name (describe fresh-line terpri))
117 (dolist (i (list (make-to-be-described :a 14) 12 "a string"
118 #0a0 #(1 2 3) #2a((1 2) (3 4)) 'sym :keyword
119 (find-package :keyword) (list 1 2 3)
120 nil (cons 1 2) (make-hash-table)
121 (let ((h (make-hash-table)))
122 (setf (gethash 10 h) 100
123 (gethash 11 h) 121)
125 (make-condition 'simple-error)
126 (make-condition 'simple-error :format-control "fc")
127 #'car #'make-to-be-described (lambda (x) (+ x 11))
128 (constantly 'foo) #'(setf to-be-described-a)
129 #'describe-object (find-class 'to-be-described)
130 (find-class 'forward-describe-class)
131 (find-class 'forward-describe-ref) (find-class 'cons)))
132 (let ((s (with-output-to-string (s)
133 (write-char #\x s)
134 (describe i s))))
135 (macrolet ((check (form)
136 `(or ,form
137 (error "misbehavior in DESCRIBE of ~S:~% ~S" i ',form))))
138 (check (char= #\x (char s 0)))
139 ;; one leading #\NEWLINE from FRESH-LINE or the like, no more
140 (check (char= #\newline (char s 1)))
141 (check (char/= #\newline (char s 2)))
142 ;; one trailing #\NEWLINE from TERPRI or the like, no more
143 (let ((n (length s)))
144 (check (char= #\newline (char s (- n 1))))
145 (check (char/= #\newline (char s (- n 2)))))))))
147 (with-test (:name (describe :argument-precedence-order))
148 ;; Argument precedence order information is only interesting for two
149 ;; or more required parameters.
150 (assert (not (search "Argument precedence order"
151 (with-output-to-string (stream)
152 (describe #'class-name stream)))))
153 (assert (search "Argument precedence order"
154 (with-output-to-string (stream)
155 (describe #'add-method stream)))))
157 (with-test (:name (describe sb-kernel:funcallable-instance))
158 (assert (search "Slots with :INSTANCE allocation"
159 (with-output-to-string (stream)
160 (describe #'class-name stream)))))
163 ;;; Tests of documentation on types and classes
165 (defun assert-documentation (thing doc-type expected)
166 ;; This helper function makes ASSERT errors print THING, DOC-TYPE,
167 ;; the return value of DOCUMENTATION and EXPECTED.
168 (flet ((assert-documentation-helper (thing doc-type documentation expected)
169 (declare (ignore thing doc-type))
170 (equal documentation expected)))
171 (assert (assert-documentation-helper
172 thing doc-type (documentation thing doc-type) expected))))
174 (defpackage #:documentation.package
175 (:documentation "PACKAGE"))
177 (with-test (:name (documentation package))
178 (assert-documentation (find-package '#:documentation.package) t "PACKAGE")
179 (setf (documentation (find-package '#:documentation.package) t) "PACKAGE2")
180 (assert-documentation (find-package '#:documentation.package) t "PACKAGE2"))
182 (defclass foo ()
184 (:documentation "FOO"))
186 (defclass documentation.funcallable-instance ()
188 (:metaclass sb-mop:funcallable-standard-class)
189 (:documentation "FEZ"))
191 (defstruct bar "BAR")
193 (define-condition baz ()
195 (:documentation "BAZ"))
197 (macrolet
198 ((do-class (name expected &optional structurep)
199 `(progn
200 (assert-documentation ',name 'type ,expected)
201 (assert-documentation (find-class ',name) 'type ,expected)
202 (assert-documentation (find-class ',name) 't ,expected)
203 ,@(when structurep
204 `((assert-documentation ',name 'structure ,expected)))
206 (let ((new1 (symbol-name (gensym "NEW1")))
207 (new2 (symbol-name (gensym "NEW2")))
208 (new3 (symbol-name (gensym "NEW3")))
209 (new4 (symbol-name (gensym "NEW4"))))
210 (declare (ignorable new4))
211 (setf (documentation ',name 'type) new1)
212 (assert-documentation (find-class ',name) 'type new1)
213 (setf (documentation (find-class ',name) 'type) new2)
214 (assert-documentation (find-class ',name) 't new2)
215 (setf (documentation (find-class ',name) 't) new3)
216 (assert-documentation ',name 'type new3)
217 ,@(when structurep
218 `((assert-documentation ',name 'structure new3)
219 (setf (documentation ',name 'structure) new4)
220 (assert-documentation ',name 'structure new4)))))))
222 (with-test (:name (documentation class standard-class))
223 (do-class foo "FOO"))
225 (with-test (:name (documentation class sb-mop:funcallable-standard-class))
226 (do-class documentation.funcallable-instance "FEZ"))
228 (with-test (:name (documentation struct 1))
229 (do-class bar "BAR" t))
231 (with-test (:name (documentation condition))
232 (do-class baz "BAZ")))
234 (defclass documentation-metaclass (standard-class)
236 (:documentation "metaclass with methods on DOCUMENTATION."))
238 (defmethod documentation ((thing documentation-metaclass)
239 (doc-type (eql 't)))
240 (sb-int:awhen (call-next-method)
241 (concatenate 'string ":" sb-int:it)))
243 (defmethod (setf documentation) (new-value
244 (thing documentation-metaclass)
245 (doc-type (eql 't)))
246 (call-next-method (when new-value
247 (substitute #\! #\. new-value))
248 thing doc-type))
250 (defmethod sb-mop:validate-superclass ((class documentation-metaclass)
251 (superclass standard-class))
254 (defclass documentation-class ()
256 (:metaclass documentation-metaclass)
257 (:documentation "normal"))
259 (with-test (:name (documentation :non-stanadard :metaclass))
260 (flet ((check (expected class-name)
261 (let ((class (find-class class-name)))
262 (assert-documentation class-name 'type expected)
263 (assert-documentation class 'type expected)
264 (assert-documentation class t expected))))
265 ;; Make sure methods specialized on the metaclass are not bypassed
266 ;; when retrieving and modifying class documentation.
267 (check ":normal" 'documentation-class)
268 (setf (documentation 'documentation-class 'type) "2.")
269 (check ":2!" 'documentation-class)
270 (setf (documentation 'documentation-class 'type) nil)
271 (check nil 'documentation-class)
273 ;; Sanity check: make sure the metaclass has its own documentation
274 ;; and is not affected by the above modifications.
275 (check "metaclass with methods on DOCUMENTATION."
276 'documentation-metaclass)))
278 (defstruct (frob (:type vector)) "FROB")
280 (with-test (:name (documentation struct 2))
281 (assert-documentation 'frob 'structure "FROB")
282 (setf (documentation 'frob 'structure) "NEW5")
283 (assert-documentation 'frob 'structure "NEW5"))
285 (deftype quux ()
286 "QUUX"
289 (with-test (:name (documentation type))
290 (assert-documentation 'quux 'type "QUUX")
291 (setf (documentation 'quux 'type) "NEW4")
292 (assert-documentation 'quux 'type "NEW4"))
294 (define-compiler-macro cmacro (x)
295 "compiler macro"
298 (define-compiler-macro (setf cmacro) (y x)
299 "setf compiler macro"
300 (declare (ignore x))
303 (with-test (:name (documentation compiler-macro))
304 (assert-documentation 'cmacro 'compiler-macro "compiler macro")
305 (assert-documentation '(setf cmacro) 'compiler-macro "setf compiler macro"))
307 (defun (setf documentation.setf) (x)
308 "(setf foo) documentation"
311 (with-test (:name (documentation function setf))
312 (flet ((expect (documentation)
313 (assert-documentation
314 '(setf documentation.setf) 'function documentation)
315 (assert-documentation
316 #'(setf documentation.setf) 'function documentation)
317 (assert-documentation
318 #'(setf documentation.setf) t documentation)))
319 (expect "(setf foo) documentation")
320 ;; The original test checked this twice. No idea why.
321 (expect "(setf foo) documentation")
323 ;; Modification
324 (setf (documentation '(setf documentation.setf) 'function)
325 "(setf bar) documentation")
326 (expect "(setf bar) documentation")
328 (setf (documentation #'(setf documentation.setf) 'function)
329 "(setf baz) documentation")
330 (expect "(setf baz) documentation")
332 (setf (documentation #'(setf documentation.setf) t)
333 "(setf fez) documentation")
334 (expect "(setf fez) documentation")))
336 (with-test (:name (documentation lambda))
337 (let ((f (lambda () "aos the zos" t))
338 (g (sb-int:named-lambda fii () "zoot the fruit" t)))
339 (dolist (doc-type '(t function))
340 (assert-documentation f doc-type "aos the zos")
341 (assert-documentation g doc-type "zoot the fruit"))
342 (setf (documentation f t) "fire")
343 (assert-documentation f t "fire")
344 (assert-documentation g t "zoot the fruit")))
346 (with-test (:name (documentation flet))
347 (assert
348 (string= (documentation
349 (flet ((quux (x)
350 "this is FLET quux"
351 (/ x 2)))
352 #'quux)
354 "this is FLET quux")))
356 (with-test (:name (documentation labels))
357 (assert
358 (string= (documentation
359 (labels ((rec (x)
360 "this is LABELS rec"
361 (if (plusp x)
362 (* x (rec (1- x)))
363 1)))
364 #'rec)
366 "this is LABELS rec")))
368 (let ((x 1))
369 (defun docfoo (y)
370 "bar"
371 (incf x y)))
373 (with-test (:name (documentation :closure))
374 (assert-documentation 'docfoo 'function "bar")
375 (assert (string= (setf (documentation 'docfoo 'function) "baz") "baz"))
376 (assert-documentation 'docfoo 'function "baz")
377 (assert-documentation #'docfoo t "baz")
378 (assert (string= (setf (documentation #'docfoo t) "zot") "zot"))
379 (assert-documentation #'docfoo t "zot")
380 (assert-documentation 'docfoo 'function "zot")
381 (assert (not (setf (documentation 'docfoo 'function) nil)))
382 (assert-documentation 'docfoo 'function nil))
384 (with-test (:name (documentation :built-in-macro) :skipped-on '(not :sb-doc))
385 (assert (documentation 'trace 'function)))
387 (with-test (:name (documentation :built-in-function) :skipped-on '(not :sb-doc))
388 (assert (documentation 'cons 'function)))
390 (defvar documentation.variable nil
391 "foo variable documentation")
393 (with-test (:name (documentation variable))
394 (assert-documentation 'documentation.variable 'variable
395 "foo variable documentation")
396 (setf (documentation 'documentation.variable 'variable)
397 "baz variable documentation")
398 (assert-documentation 'documentation.variable 'variable
399 "baz variable documentation"))
401 (with-test (:name (documentation :mismatch-for-function))
402 (defun test ()
404 nil)
405 (setf (symbol-function 'test2) #'test)
406 (setf (documentation 'test 'function) "Y")
407 (assert (equal (documentation #'test t)
408 (documentation 'test 'function)))
409 (setf (documentation 'test2 'function) "Z")
410 (assert (not
411 (equal (documentation 'test 'function)
412 (documentation 'test2 'function)))))
414 (with-test (:name (documentation setf :on nil))
415 (assert
416 (handler-case
417 (assert (equal (setf (documentation nil 'function) "foo") "foo"))
418 (style-warning () t)
419 (:no-error (x)
420 (declare (ignore x))
421 nil))))
423 (with-test (:name (describe generic-function :assumed-type))
424 ;; Signalled an error at one point
425 (let ((fun (checked-compile '(lambda ()
426 (flet ((zoo () (gogo)))
427 (defmethod gogo () nil)
428 (describe 'gogo)))
429 :allow-style-warnings t)))
430 (handler-bind ((warning #'muffle-warning)) ; implicit gf
431 (silently (funcall fun)))))
433 (defmacro bug-643958-test ()
434 "foo"
435 :ding!)
437 (with-test (:name :bug-643958)
438 (assert (equal "foo" (documentation 'bug-643958-test 'function)))
439 (setf (documentation 'bug-643958-test 'function) "bar")
440 (assert (equal "bar" (documentation 'bug-643958-test 'function))))
442 (defclass cannot-print-this ()
444 (defmethod print-object ((oops cannot-print-this) stream)
445 (error "No go!"))
446 (with-test (:name :describe-suppresses-print-errors)
447 (handler-bind ((error #'continue))
448 (with-output-to-string (s)
449 (describe (make-instance 'cannot-print-this) s))))
450 (with-test (:name :backtrace-suppresses-print-errors)
451 (handler-bind ((error #'continue))
452 (with-output-to-string (s)
453 (labels ((foo (n x)
454 (when (plusp n)
455 (foo (1- n) x))
456 (when (zerop n)
457 (sb-debug:print-backtrace :count 100 :stream s))))
458 (foo 100 (make-instance 'cannot-print-this))))))
459 (with-test (:name :backtrace-and-circles)
460 (handler-bind ((error #'continue))
461 (with-output-to-string (s)
462 (labels ((foo (n x)
463 (when (plusp n)
464 (foo (1- n) x))
465 (when (zerop n)
466 (sb-debug:print-backtrace :count 100 :stream s))))
467 (foo 100 (let ((list (list t)))
468 (nconc list list)))))))
470 (with-test (:name :endianness-in-features)
471 (assert
472 (or (member :big-endian *features*)
473 (member :little-endian *features*))))
475 (with-test (:name (trace generic-function))
476 (defgeneric traced-gf (x))
477 (defmethod traced-gf (x) (1+ x))
478 (assert (= (traced-gf 3) 4))
479 (trace traced-gf)
480 (let ((output (with-output-to-string (*trace-output*)
481 (assert (= (traced-gf 3) 4)))))
482 (assert (> (length output) 0)))
483 (assert (typep #'traced-gf 'standard-generic-function))
484 (untrace traced-gf)
485 (let ((output (with-output-to-string (*trace-output*)
486 (assert (= (traced-gf 3) 4)))))
487 (assert (= (length output) 0))))
489 (with-test (:name (apropos :inherited :bug-1364413))
490 (let* ((package (make-package "BUGGALO" :use nil))
491 (symbol (intern "BUGGALO" package)))
492 (export (list symbol) package)
493 (let ((inherits (make-package "BUGGALO-INHERITS" :use (list package))))
494 (assert (= (length (apropos-list "BUGGALO" package)) 1))
495 (assert (= (length (apropos-list "BUGGALO" inherits)) 1))
496 (delete-package inherits))
497 (delete-package package)))
499 (with-test (:name (apropos :inherited :external-only :bug-1364413))
500 (let* ((package (make-package "BUGGALO" :use nil))
501 (symbol (intern "BUGGALO" package)))
502 (export (list symbol) package)
503 (let ((inherits (make-package "BUGGALO-INHERITS" :use (list package))))
504 (assert (= (length (apropos-list "BUGGALO" package t)) 1))
505 (assert (= (length (apropos-list "BUGGALO" inherits t)) 0))
506 (delete-package inherits))
507 (delete-package package)))
509 (with-test (:name (apropos :once-only))
510 (assert (= (length (apropos-list "UPDATE-INSTANCE-FOR-REDEFINED-CLASS")) 1)))
512 (defgeneric gf-arglist-1 (x &key y))
513 (defmethod gf-arglist-1 (x &key (y nil) (z nil z-p))
514 (list x y z z-p))
516 (defgeneric gf-arglist-2 (x &key y))
517 (defmethod gf-arglist-2 ((x integer) &key (y nil) ((z f) nil z-p)) (list x y f z-p))
518 (defmethod gf-arglist-2 ((x string) &key (y nil) ((z w) nil z-p)) (list x y w z-p))
520 (defgeneric gf-arglist-3 (x &key ((:y y))))
522 (defgeneric gf-arglist-4 (x &key ((:y z))))
524 (defgeneric gf-arglist-5 (x &key y))
525 (defmethod gf-arglist-5 ((x integer) &key z &allow-other-keys) (list x z))
527 (with-test (:name (:generic-function-pretty-arglist 1))
528 (assert (equal (sb-pcl::generic-function-pretty-arglist #'gf-arglist-1)
529 '(x &key y z))))
530 (with-test (:name (:generic-function-pretty-arglist 2))
531 (assert (or (equal (sb-pcl::generic-function-pretty-arglist #'gf-arglist-2)
532 '(x &key y ((z w))))
533 (equal (sb-pcl::generic-function-pretty-arglist #'gf-arglist-2)
534 '(x &key y ((z f)))))))
535 (with-test (:name (:generic-function-pretty-arglist 3))
536 (assert (equal (sb-pcl::generic-function-pretty-arglist #'gf-arglist-3)
537 '(x &key y))))
538 (with-test (:name (:generic-function-pretty-arglist 4))
539 (assert (equal (sb-pcl::generic-function-pretty-arglist #'gf-arglist-4)
540 '(x &key ((:y z))))))
541 (with-test (:name (:generic-function-pretty-arglist 5))
542 (assert (equal (sb-pcl::generic-function-pretty-arglist #'gf-arglist-5)
543 '(x &key y z &allow-other-keys))))
544 ;;;; success