1 ;;;; miscellaneous side-effectful tests of CLOS
3 ;;;; This software is part of the SBCL system. See the README file for
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
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 #+interpreter
(sb-ext:exit
:code
104)
16 (load "compiler-test-util.lisp")
17 (defpackage "CLOS-IMPURE"
18 (:import-from
"SB-KERNEL" "IMPLICIT-GENERIC-FUNCTION-WARNING")
19 (:use
"CL" "SB-EXT" "ASSERTOID" "TEST-UTIL" "COMPILER-TEST-UTIL"))
20 (in-package "CLOS-IMPURE")
22 ;;; It should be possible to do DEFGENERIC and DEFMETHOD referring to
23 ;;; structure types defined earlier in the file.
24 (defstruct struct-a x y
)
25 (defstruct struct-b x y z
)
26 (handler-bind ((implicit-generic-function-warning #'muffle-warning
))
27 (defmethod wiggle ((a struct-a
))
30 (defgeneric jiggle
(arg))
31 (defmethod jiggle ((a struct-a
))
34 (defmethod jiggle ((b struct-b
))
39 (with-test (:name
(defmethod defstruct :same-file
))
40 (assert (= (wiggle (make-struct-a :x
6 :y
5))
41 (jiggle (make-struct-b :x
19 :y
6 :z
2)))))
43 ;;; Compiling DEFGENERIC should prevent "undefined function" style
44 ;;; warnings from code within the same file.
45 (defgeneric gf-defined-in-this-file
(x y
))
46 (defun function-using-gf-defined-in-this-file (x y n
)
48 (gf-defined-in-this-file x y
)))
50 ;;; Until Martin Atzmueller ported Pierre Mai's CMU CL fixes in
51 ;;; sbcl-0.6.12.25, the implementation of NO-APPLICABLE-METHOD was
52 ;;; broken in such a way that the code here would signal an error.
53 (defgeneric zut-n-a-m
(a b c
))
54 (defmethod no-applicable-method ((zut-n-a-m (eql #'zut-n-a-m
)) &rest args
)
55 (declare (ignore args
))
56 :no-applicable-method
)
57 (with-test (:name no-applicable-method
)
58 (assert (eq :no-applicable-method
(zut-n-a-m 1 2 3))))
60 ;;; bug reported and fixed by Alexey Dejneka sbcl-devel 2001-09-10:
61 ;;; This DEFGENERIC shouldn't cause an error.
62 (defgeneric ad-gf
(a) (:method
:around
(x) x
))
64 ;;; DEFGENERIC and DEFMETHOD shouldn't accept &REST when it's not
65 ;;; followed by a variable:
66 ;;; e.g. (DEFMETHOD FOO ((X T) &REST) NIL) should signal an error.
67 ;;; Of course most of these are totally redundant
68 ;;; now that there is only one function to parse all lambda lists.
69 (eval-when (:load-toplevel
:compile-toplevel
:execute
)
70 (defmacro expect-error
(form)
71 `(multiple-value-bind (res condition
)
72 (let ((*error-output
* (make-broadcast-stream)))
73 (ignore-errors (eval ',form
))) ; delay until *error-output* is bound
74 (declare (ignore res
))
75 (typep condition
'error
))))
76 (assert (expect-error (defmethod foo0 ((x t
) &rest
) nil
)))
77 (assert (expect-error (defgeneric foo1
(x &rest
))))
78 (assert (expect-error (defgeneric foo2
(x a
&rest
))))
79 (defgeneric foo3
(x &rest y
))
80 (defmethod foo3 ((x t
) &rest y
) y nil
)
81 (defgeneric foo4
(x &rest z
&key y
))
82 (defmethod foo4 ((x t
) &rest z
&key y
) z y nil
)
83 (assert (expect-error (defgeneric foo5
(x &rest
))))
84 (assert (expect-error (defmethod foo6 (x &rest
))))
86 ;;; legal method specializers
87 (defclass bug-525916-1
() ())
88 (defclass bug-525916-2
() ())
89 (with-test (:name
(defmethod :specializer-syntax
:bug-525916
))
90 (assert (expect-error (defmethod invalid ((arg)) arg
)))
91 (assert (expect-error (defmethod invalid (nil) 1)))
92 (assert (expect-error (defmethod invalid ((arg . bug-525916-1
)) arg
)))
93 (assert (expect-error (defmethod invalid ((arg bug-525916-1 bug-525916-2
)) arg
))))
95 ;;; more lambda-list checking
97 ;;; DEFGENERIC lambda lists are subject to various limitations, as per
98 ;;; section 3.4.2 of the ANSI spec. Since Alexey Dejneka's patch for
99 ;;; bug 191-b ca. sbcl-0.7.22, these limitations should be enforced.
100 (with-test (:name
(defgeneric :lambda-list
))
101 (labels ((coerce-to-boolean (x)
103 (test-case (lambda-list
105 expected-failure-p expected-warnings-p message
)
106 (declare (type boolean expected-failure-p
))
107 #+nil
(format t
"~&trying ~S~%" lambda-list
)
108 (let ((*error-output
* (make-string-output-stream) ))
109 (multiple-value-bind (fun warnings-p failure-p
)
110 (compile nil
`(lambda () (defgeneric ,(gensym) ,lambda-list
)))
111 (declare (ignore fun
))
112 (assert (eq (coerce-to-boolean failure-p
) expected-failure-p
))
113 (assert (eq (coerce-to-boolean warnings-p
) expected-warnings-p
))
115 (assert (search message
(get-output-stream-string
116 *error-output
*))))))))
118 (test-case '("a" #p
"b")
119 t t
"Required argument is not a symbol: \"a\"")
122 ;; repeated names and keywords
124 t t
"The variable X occurs more than once")
125 (test-case '(x &rest x
)
126 t t
"The variable X occurs more than once")
127 (test-case '(&optional x x
)
128 t t
"The variable X occurs more than once")
129 (test-case '(&key x
((:y x
)))
130 t t
"The variable X occurs more than once")
131 (test-case '(&key x
((:x y
)))
132 t t
"The keyword :X occurs more than once")
133 (test-case '(&key
((:x a
)) ((:x b
)))
134 t t
"The keyword :X occurs more than once")
135 ;; illegal variable names
137 t t
":PI is a keyword and cannot be used")
139 t t
"COMMON-LISP:PI names a defined constant")
140 ;; forbidden default or supplied-p for &OPTIONAL or &KEY arguments
141 (test-case '(x &optional
(y 0))
142 t t
"Invalid &OPTIONAL argument specifier (Y 0)")
143 (test-case '(x &optional y
))
144 (test-case '(x y
&key
(z :z z-p
))
145 t t
"Invalid &KEY argument specifier (Z :Z Z-P)")
146 (test-case '(x y
&key z
))
147 (test-case '(x &optional
(y 0) &key z
)
148 t t
"Invalid &OPTIONAL argument specifier (Y 0)")
149 (test-case '(x &optional y
&key z
)
150 nil t
"&OPTIONAL and &KEY found in the same lambda list")
151 (test-case '(x &optional y
&key
(z :z
))
152 t t
"Invalid &KEY argument specifier (Z :Z)")
153 (test-case '(x &optional y
&key z
)
154 nil t
"&OPTIONAL and &KEY found in the same lambda list")
155 (test-case '(&optional
&key
(k :k k-p
))
156 t t
"Invalid &KEY argument specifier (K :K K-P)")
157 (test-case '(&optional
&key k
))
159 (test-case '(x y z
&optional a
&aux g h
)
160 t t
"&AUX is not allowed in a generic function lambda list")
161 (test-case '(x y z
&optional a
))
163 t t
"&AUX is not allowed in a generic function lambda list")
165 ;; also can't use bogoDEFMETHODish type-qualifier-ish decorations
166 ;; on required arguments
167 (test-case '((arg t
))
168 t t
"Required argument is not a symbol: (ARG T)")
171 (with-test (:name
(defmethod :lambda-list
))
172 (flet ((test-case (lambda-list messages
)
174 (fun failure-p warnings style-warnings notes errors
)
175 (checked-compile `(lambda () (defmethod ,(gensym) ,lambda-list
))
177 (declare (ignore fun warnings style-warnings notes
))
179 (assert (= (length messages
) (length errors
)))
180 (loop for message in messages
182 do
(assert (search message
(princ-to-string error
)))))))
184 (lambda (spec) (apply #'test-case spec
))
185 '(;; Invalid specialized required argument
186 (((x t t
)) ("arg is not a non-NIL symbol or a list of two elements: (X T T)"))
187 ;; Repeated names and keywords
188 (((x t
) (x t
)) ("The variable X occurs more than once"))
189 (((x t
) &rest x
) ("The variable X occurs more than once"))
190 ((&optional x x
) ("The variable X occurs more than once"))
191 ((&key x
((:y x
))) ("The variable X occurs more than once"))
192 ((&key x
((:x y
))) ("The keyword :X occurs more than once"))
193 ;; Illegal variable names
194 (((:pi t
)) (":PI is a keyword and cannot be used"))
195 (((pi t
)) ("COMMON-LISP:PI names a defined constant"))))))
198 ;;; Explicit :metaclass option with structure-class and
201 (defclass structure-class-foo1
() () (:metaclass cl
:structure-class
))
202 (defclass structure-class-foo2
(structure-class-foo1)
203 () (:metaclass cl
:structure-class
))
204 (with-test (:name
(defclass :metaclass cl
:structure-class
))
205 (assert (typep (class-of (make-instance 'structure-class-foo1
))
207 (assert (typep (make-instance 'structure-class-foo1
) 'structure-class-foo1
))
208 (assert (typep (class-of (make-instance 'structure-class-foo2
))
210 (assert (typep (make-instance 'structure-class-foo2
) 'structure-class-foo2
)))
212 (defclass standard-class-foo1
() () (:metaclass cl
:standard-class
))
213 (defclass standard-class-foo2
(standard-class-foo1)
214 () (:metaclass cl
:standard-class
))
215 (with-test (:name
(defclass :metaclass cl
:standard-class
))
216 (assert (typep (make-instance 'standard-class-foo1
) 'standard-class-foo1
))
217 (assert (typep (make-instance 'standard-class-foo2
) 'standard-class-foo2
)))
219 ;;; DEFGENERIC's blow-away-old-methods behavior is specified to have
220 ;;; special hacks to distinguish between defined-with-DEFGENERIC-:METHOD
221 ;;; methods and defined-with-DEFMETHOD methods, so that reLOADing
222 ;;; DEFGENERIC-containing files does the right thing instead of
223 ;;; randomly slicing your generic functions. (APD made this work
224 ;;; in sbcl-0.7.0.2.)
225 (defgeneric born-to-be-redefined
(x)
226 (:method
((x integer
))
228 (defmethod born-to-be-redefined ((x real
))
230 (assert (eq (born-to-be-redefined 1) 'integer
))
231 (defgeneric born-to-be-redefined
(x))
232 (assert (eq (born-to-be-redefined 1) 'real
)) ; failed until sbcl-0.7.0.2
233 (defgeneric born-to-be-redefined
(x)
234 (:method
((x integer
))
236 (defmethod born-to-be-redefined ((x integer
))
238 (assert (eq (born-to-be-redefined 1) 'int
))
239 (defgeneric born-to-be-redefined
(x))
240 (assert (eq (born-to-be-redefined 1) 'int
))
242 ;;; In the removal of ITERATE from SB-PCL, a bug was introduced
243 ;;; preventing forward-references and also CHANGE-CLASS (which
244 ;;; forward-references used interally) from working properly.
246 ;;; One symptom was reported by Brian Spilsbury (sbcl-devel
247 ;;; 2002-04-08), and another on IRC by Dan Barlow simultaneously.
248 ;;; Better check that it doesn't happen again.
250 ;;; Some more CHANGE-CLASS testing (which only applies to the now
251 ;;; ANSI-compliant version) thanks to Espen Johnsen)
253 ;; First, the forward references:
254 (defclass forward-ref-a
(forward-ref-b) ())
255 (defclass forward-ref-b
() ())
256 ;; (a couple more complicated examples found by Paul Dietz' test
258 (defclass forward-ref-c1
(forward-ref-c2) ())
259 (defclass forward-ref-c2
(forward-ref-c3) ())
261 (defclass forward-ref-d1
(forward-ref-d2 forward-ref-d3
)
263 (defclass forward-ref-d2
(forward-ref-d4 forward-ref-d5
)
267 (defun change-class-test-case (spec)
268 (destructuring-bind (from-class from-initargs
272 (let ((from (typecase from-class
274 (apply #'make-instance from-class from-initargs
))
275 ((cons (eql :class
) (cons symbol
))
276 (find-class (second from-class
))))))
278 (apply #'change-class from to-class to-initargs
))
279 ;; These local functions make ASSERT produce better error
281 (slot-value-equal (instance name value expected
)
282 (declare (ignore instance name
))
283 (equal value expected
))
284 (slot-not-bound (instance name
)
285 (not (slot-boundp instance name
))))
288 (assert-error (change)))
290 (assert-error (change) type-error
))
293 (loop :for
(name value
) :in expected-slots
296 (assert (slot-not-bound to name
)))
300 to name
(slot-value to name
) value
))))))))))))
302 (defclass change-class.smoke
.1 ()
303 ((foo :initarg
:foo
)))
304 (defclass change-class.smoke
.2 (change-class.smoke
.1) ())
305 (defclass change-class.smoke
.3 (change-class.smoke
.1)
306 ((foo :initarg
:foo
)))
307 (defclass change-class.smoke
.4 ()
308 ((foo :initarg
:foo
) (bar :initarg
:bar
)))
309 (defclass change-class.smoke
.5 ()
310 ((a :initarg
:a
) (b :initarg
:b
) (c :initarg
:c
)))
311 (defclass change-class.smoke
.6 () ())
313 (with-test (:name
(change-class :smoke
))
315 #'change-class-test-case
316 '(;; Test unbound slots.
317 (change-class.smoke
.1 () change-class.smoke
.1 ()
319 (change-class.smoke
.1 () change-class.smoke
.2 ()
321 (change-class.smoke
.1 () change-class.smoke
.3 ()
323 (change-class.smoke
.1 () change-class.smoke
.4 ()
324 ((foo :unbound
) (bar :unbound
)))
325 (change-class.smoke
.4 (:bar
1) change-class.smoke
.1 ()
328 ;; Bound slots are retained.
329 (change-class.smoke
.1 (:foo
:baz
) change-class.smoke
.1 ()
331 (change-class.smoke
.1 (:foo
:baz
) change-class.smoke
.2 ()
333 (change-class.smoke
.1 (:foo
:baz
) change-class.smoke
.3 ()
335 (change-class.smoke
.1 (:foo
:baz
) change-class.smoke
.4 ()
336 ((foo :baz
) (bar :unbound
)))
337 (change-class.smoke
.4 (:foo
:baz
) change-class.smoke
.1 ()
341 (change-class.smoke
.5 (:a
1 :b
2 :c
3) change-class.smoke
.5 ()
344 ;; Original test by Espen Johnsen
345 (change-class.smoke
.1 (:foo
1) change-class.smoke
.4 (:bar
2)
348 ;; Cannot change objects into metaobjects.
349 (change-class.smoke
.6 () class
()
351 (change-class.smoke
.6 () generic-function
()
353 (change-class.smoke
.6 () method
()
355 (change-class.smoke
.6 () slot-definition
()
358 ;; Test for type-checking
360 (locally (declare (optimize (safety 3))) ; force slot type-checking
361 (defclass change-class.type-check
.1 ()
362 ((foo :initarg
:foo
:type real
)))
363 (defclass change-class.type-check
.2 ()
364 ((foo :initarg
:foo
:type integer
))))
366 (with-test (:name
(change-class :type-check
))
368 #'change-class-test-case
369 '(;; These are allowed.
370 (change-class.type-check
.1 () change-class.type-check
.2 ()
372 (change-class.type-check
.1 (:foo
1) change-class.type-check
.2 ()
374 (change-class.type-check
.1 (:foo
1.0) change-class.type-check
.2 (:foo
2)
377 ;; These are not allowed.
378 (change-class.type-check
.1 () change-class.type-check
.2 (:foo
1.0)
380 (change-class.type-check
.1 (:foo
1.0) change-class.type-check
.2 ()
383 ;; Type-mismatches should be recoverable via USE-VALUE restart.
384 (let* ((from (make-instance 'change-class.type-check
.1 :foo
1.0))
385 (to (handler-bind ((type-error (lambda (condition)
386 (declare (ignore condition
))
388 (change-class from
'change-class.type-check
.2))))
389 (assert (equal (slot-value to
'foo
) 3))))
391 ;; Test interaction with initforms and -args
393 (defclass change-class.initforms
.1 ()
395 (defclass change-class.initforms
.2 ()
396 ((foo :initarg
:foo
)))
397 (defclass change-class.initforms
.3 ()
398 ((foo :initarg
:foo
:initform
:bar
)))
399 (defclass change-class.initforms
.4 ()
400 ((foo :initarg
:foo
))
404 (with-test (:name
(change-class :initforms
))
406 #'change-class-test-case
407 '(;; Initialization of added slot.
408 (change-class.initforms
.1 () change-class.initforms
.3 ()
410 (change-class.initforms
.1 () change-class.initforms
.3 (:foo
:fez
)
412 (change-class.initforms
.1 () change-class.initforms
.4 ()
413 ((foo :unbound
))) ; default initargs are not used
414 (change-class.initforms
.1 () change-class.initforms
.4 (:foo
:fez
)
417 ;; Unbound slot remains unbound.
418 (change-class.initforms
.2 () change-class.initforms
.3 ()
420 (change-class.initforms
.2 () change-class.initforms
.3 (:foo
:fez
)
422 (change-class.initforms
.2 () change-class.initforms
.4 ()
424 (change-class.initforms
.2 () change-class.initforms
.4 (:foo
:fez
)
427 ;; Value is retained.
428 (change-class.initforms
.2 (:foo
:baz
) change-class.initforms
.3 ()
430 (change-class.initforms
.2 (:foo
:baz
) change-class.initforms
.3 (:foo
:fez
)
432 (change-class.initforms
.2 (:foo
:baz
) change-class.initforms
.4 ()
434 (change-class.initforms
.2 (:foo
:baz
) change-class.initforms
.4 (:foo
:fez
)
437 ;; Test for FORWARD-REFERENCED-CLASS
439 (defclass change-class.forward-referenced
.1 () ())
440 ;; CHANGE-CLASS.FORWARD-REFERENCED.2 is only needed to create the
441 ;; FORWARD-REFERENCED-CLASS CHANGE-CLASS.FORWARD-REFERENCED.3.
442 (defclass change-class.forward-referenced
.2 (change-class.forward-referenced
.3) ())
444 (with-test (:name
(change-class sb-pcl
:forward-referenced-class
))
446 #'change-class-test-case
447 '(;; Changing instances of "ordinary classes" to classes which are
448 ;; instances of FORWARD-REFERENCED-CLASS is not allowed.
449 (change-class.forward-referenced
.1 () change-class.forward-referenced
.3 ()
452 ;; Changing instances of FORWARD-REFERENCED-CLASS into
453 ;; non-CLASSes and in particular non-CLASS metaobjects is not
455 ((:class change-class.forward-referenced
.3) () change-class.forward-referenced
.1 ()
457 ((:class change-class.forward-referenced
.3) () generic-function
()
459 ((:class change-class.forward-referenced
.3) () method
()
461 ((:class change-class.forward-referenced
.3) () slot-definition
()
464 ;; Changing instances of FORWARD-REFERENCED-CLASS into CLASS is
465 ;; allowed but destructive. Therefore has to be final test case.
466 ((:class change-class.forward-referenced
.3) () standard-class
()
469 ;; Test for FUNCALLABLE-STANDARD-CLASS
471 (defclass change-class.funcallable
.1 () ())
472 (defclass change-class.funcallable
.2 () ()
473 (:metaclass sb-mop
:funcallable-standard-class
))
474 (defclass change-class.funcallable
.3 () ()
475 (:metaclass sb-mop
:funcallable-standard-class
))
477 (with-test (:name
(change-class sb-mop
:funcallable-standard-class
))
479 #'change-class-test-case
480 '(;; Cannot change STANDARD-OBJECT into FUNCALLABLE-STANDARD-OBJECT
482 (change-class.funcallable
.1 () change-class.funcallable
.2 ()
484 (change-class.funcallable
.2 () change-class.funcallable
.1 ()
486 ;; FUNCALLABLE-STANDARD-OBJECTs should work.
487 (change-class.funcallable
.2 () change-class.funcallable
.2 ()
489 (change-class.funcallable
.2 () change-class.funcallable
.3 ()
492 ;;; Until Pierre Mai's patch (sbcl-devel 2002-06-18, merged in
493 ;;; sbcl-0.7.4.39) the :MOST-SPECIFIC-LAST option had no effect.
494 (defgeneric bug180
(x)
495 (:method-combination list
:most-specific-last
))
496 (defmethod bug180 list
((x number
))
498 (defmethod bug180 list
((x fixnum
))
500 (assert (equal (bug180 14) '(number fixnum
)))
502 ;;; printing a structure class should not loop indefinitely (or cause
503 ;;; a stack overflow):
504 (defclass test-printing-structure-class
()
505 ((slot :initarg
:slot
))
506 (:metaclass structure-class
))
507 (with-test (:name
:test-printing-structure-class
)
508 (write-to-string (make-instance 'test-printing-structure-class
:slot
2)))
510 ;;; structure-classes should behave nicely when subclassed
511 (defclass super-structure
()
512 ((a :initarg
:a
:accessor a-accessor
)
513 (b :initform
2 :reader b-reader
))
514 (:metaclass structure-class
))
515 (defclass sub-structure
(super-structure)
516 ((c :initarg
:c
:writer c-writer
:accessor c-accessor
))
517 (:metaclass structure-class
))
518 (let ((foo (make-instance 'sub-structure
:a
1 :c
3)))
519 (assert (= (a-accessor foo
) 1))
520 (assert (= (b-reader foo
) 2))
521 (assert (= (c-accessor foo
) 3))
522 (setf (a-accessor foo
) 4)
524 (assert (= (a-accessor foo
) 4))
525 (assert (= (c-accessor foo
) 5)))
527 ;;; At least as of sbcl-0.7.4, PCL has code to support a special
528 ;;; encoding of effective method functions for slot accessors as
529 ;;; FIXNUMs. Given this special casing, it'd be easy for slot accessor
530 ;;; functions to get broken in special ways even though ordinary
531 ;;; generic functions work. As of sbcl-0.7.4 we didn't have any tests
532 ;;; for that possibility. Now we have a few tests:
534 ((fin :reader ffin
:writer ffin
!)
535 (tail :reader ftail
:writer ftail
!)))
536 (defvar *fish
* (make-instance 'fish
))
537 (ffin! 'triangular-fin
*fish
*)
538 (defclass cod
(fish) ())
539 (defvar *cod
* (make-instance 'cod
))
540 (defparameter *clos-dispatch-side-fx
* (make-array 0 :fill-pointer
0))
541 (defmethod ffin! (new-fin (cod cod
))
542 ; (format t "~&about to set ~S fin to ~S~%" cod new-fin)
543 (vector-push-extend '(cod) *clos-dispatch-side-fx
*)
546 ; (format t "~&done setting ~S fin to ~S~%" cod new-fin)
548 (defmethod ffin! :before
(new-fin (cod cod
))
549 (vector-push-extend '(:before cod
) *clos-dispatch-side-fx
*)
550 ;(format t "~&exploring the CLOS dispatch zoo with COD fins~%")
552 (ffin! 'almost-triang-fin
*cod
*)
553 (assert (eq (ffin *cod
*) 'almost-triang-fin
))
554 (assert (equalp #((:before cod
) (cod)) *clos-dispatch-side-fx
*))
556 ;;; Until sbcl-0.7.6.21, the long form of DEFINE-METHOD-COMBINATION
557 ;;; ignored its options; Gerd Moellmann found and fixed the problem
558 ;;; for cmucl (cmucl-imp 2002-06-18).
559 (define-method-combination test-mc
(x)
560 ;; X above being a method-group-specifier
561 ((primary () :required t
))
563 `(call-method ,(first primary
)))
566 (:method-combination test-mc
1))
571 ;;; Until sbcl-0.7.7.20, some conditions weren't being signalled, and
572 ;;; some others were of the wrong type:
573 (macrolet ((assert-program-error (form)
574 `(multiple-value-bind (value error
)
575 (ignore-errors (let ((*error-output
* (make-broadcast-stream)))
577 (unless (and (null value
) (typep error
'program-error
))
578 (error "~S failed: ~S, ~S" ',form value error
)))))
579 (assert-program-error (defclass foo001
() (a b a
)))
580 (assert-program-error (defclass foo002
()
582 (:default-initargs x
'a x
'b
)))
583 (assert-program-error (defclass foo003
()
584 ((a :allocation
:class
:allocation
:class
))))
585 (assert-program-error (defclass foo004
()
587 ;; and some more, found by Wolfhard Buss and fixed for cmucl by Gerd
588 ;; Moellmann in sbcl-0.7.8.x:
589 (assert-program-error (progn
590 (defmethod odd-key-args-checking (&key
(key 42)) key
)
591 (odd-key-args-checking 3)))
592 (assert (= (odd-key-args-checking) 42))
593 (assert (eq (odd-key-args-checking :key t
) t
))
594 ;; yet some more, fixed in sbcl-0.7.9.xx
595 (assert-program-error (defclass foo005
()
596 (:metaclass sb-pcl
::funcallable-standard-class
)
598 (assert-program-error (defclass foo006
()
599 ((a :reader
(setf a
)))))
600 (assert-program-error (defclass foo007
()
602 (assert-program-error (defclass foo008
()
604 (:default-initargs
:a
1)
605 (:default-initargs
:a
2)))
606 ;; and also BUG 47d, fixed in sbcl-0.8alpha.0.26
607 (assert-program-error (defgeneric if
(x)))
608 ;; DEFCLASS should detect an error if slot names aren't suitable as
610 (assert-program-error (defclass foo009
()
612 (assert-program-error (defclass foo010
()
613 (("a" :initarg
:a
))))
614 (assert-program-error (defclass foo011
()
615 ((#1a
() :initarg
:a
))))
616 (assert-program-error (defclass foo012
()
618 (assert-program-error (defclass foo013
() ("a")))
619 ;; specialized lambda lists have certain restrictions on ordering,
620 ;; repeating keywords, and the like:
621 (assert-program-error (defmethod foo014 ((foo t
) &rest
) nil
))
622 (assert-program-error (defmethod foo015 ((foo t
) &rest x y
) nil
))
623 (assert-program-error (defmethod foo016 ((foo t
) &allow-other-keys
) nil
))
624 (assert-program-error (defmethod foo017 ((foo t
)
625 &optional x
&optional y
) nil
))
626 (assert-program-error (defmethod foo018 ((foo t
) &rest x
&rest y
) nil
))
627 (assert-program-error (defmethod foo019 ((foo t
) &rest x
&optional y
) nil
))
628 (assert-program-error (defmethod foo020 ((foo t
) &key x
&optional y
) nil
))
629 (assert-program-error (defmethod foo021 ((foo t
) &key x
&rest y
) nil
)))
631 ;;; DOCUMENTATION's argument-precedence-order wasn't being faithfully
632 ;;; preserved through the bootstrap process until sbcl-0.7.8.39.
633 ;;; (thanks to Gerd Moellmann)
634 (with-test (:name
(documentation :argument-precedence-order
))
638 (let ((answer (documentation 'foo022
'function
)))
639 (assert (stringp answer
))
640 (defmethod documentation ((x (eql 'foo022
)) y
) "WRONG")
641 (assert (string= (documentation 'foo022
'function
) answer
))))
643 ;;; only certain declarations are permitted in DEFGENERIC
644 (macrolet ((assert-program-error (form)
645 `(multiple-value-bind (value error
)
646 (ignore-errors (let ((*error-output
* (make-broadcast-stream)))
648 (assert (null value
))
649 (assert (typep error
'program-error
)))))
650 (assert-program-error (defgeneric bogus-declaration
(x)
651 (declare (special y
))))
652 (assert-program-error (defgeneric bogus-declaration2
(x)
653 (declare (notinline concatenate
)))))
654 ;;; CALL-NEXT-METHOD should call NO-NEXT-METHOD if there is no next
656 (handler-bind ((implicit-generic-function-warning #'muffle-warning
))
657 (defmethod no-next-method-test ((x integer
)) (call-next-method)))
658 (assert (null (ignore-errors (no-next-method-test 1))))
659 (defmethod no-next-method ((g (eql #'no-next-method-test
)) m
&rest args
)
660 (declare (ignore args
))
662 (assert (eq (no-next-method-test 1) 'success
))
663 (assert (null (ignore-errors (no-next-method-test 'foo
))))
665 ;;; regression test for bug 176, following a fix that seems
666 ;;; simultaneously to fix 140 while not exposing 176 (by Gerd
667 ;;; Moellmann, merged in sbcl-0.7.9.12).
669 (let ((lastname (intern (format nil
"C176-~D" (1- i
))))
670 (name (intern (format nil
"C176-~D" i
))))
671 (eval `(defclass ,name
672 (,@(if (= i
0) nil
(list lastname
)))
674 (eval `(defmethod initialize-instance :after
((x ,name
) &rest any
)
675 (declare (ignore any
))))))
676 (defclass b176
() (aslot-176))
677 (defclass c176-0
(b176) ())
678 (assert (= 1 (setf (slot-value (make-instance 'c176-9
) 'aslot-176
) 1)))
680 ;;; DEFINE-METHOD-COMBINATION was over-eager at checking for duplicate
682 (define-method-combination dmc-test-mc
(&optional
(order :most-specific-first
))
684 (primary (dmc-test-mc) :order order
:required t
))
685 (let ((form (if (rest primary
)
686 `(and ,@(mapcar #'(lambda (method)
687 `(call-method ,method
))
689 `(call-method ,(first primary
)))))
691 `(call-method ,(first around
)
693 (make-method ,form
)))
696 (defgeneric dmc-test-mc
(&key k
)
697 (:method-combination dmc-test-mc
))
699 (defmethod dmc-test-mc dmc-test-mc
(&key k
)
703 ;;; While I'm at it, DEFINE-METHOD-COMBINATION is defined to return
704 ;;; the NAME argument, not some random method object. So:
705 (assert (eq (define-method-combination dmc-test-return-foo
)
706 'dmc-test-return-foo
))
707 (assert (eq (define-method-combination dmc-test-return-bar
:operator and
)
708 'dmc-test-return-bar
))
709 (assert (eq (define-method-combination dmc-test-return
710 (&optional
(order :most-specific-first
))
712 (primary (dmc-test-return) :order order
:required t
))
713 (let ((form (if (rest primary
)
714 `(and ,@(mapcar #'(lambda (method)
715 `(call-method ,method
))
717 `(call-method ,(first primary
)))))
719 `(call-method ,(first around
)
721 (make-method ,form
)))
725 ;;; DEFINE-METHOD-COMBINATION should, according to the description in 7.7,
726 ;;; allow you to do everything in the body forms yourself if you specify
727 ;;; exactly one method group whose qualifier-pattern is *
729 ;;; The specific language is:
730 ;;; "The use of method group specifiers provides a convenient syntax to select
731 ;;; methods, to divide them among the possible roles, and to perform the
732 ;;; necessary error checking. It is possible to perform further filtering of
733 ;;; methods in the body forms by using normal list-processing operations and
734 ;;; the functions method-qualifiers and invalid-method-error. It is permissible
735 ;;; to use setq on the variables named in the method group specifiers and to
736 ;;; bind additional variables. It is also possible to bypass the method group
737 ;;; specifier mechanism and do everything in the body forms. This is
738 ;;; accomplished by writing a single method group with * as its only
739 ;;; qualifier-pattern; the variable is then bound to a list of all of the
740 ;;; applicable methods, in most-specific-first order."
741 (define-method-combination wam-test-method-combination-a
()
743 (do ((methods all-methods
(rest methods
))
747 (let ((primary (nreverse primary
))
748 (around (nreverse around
)))
750 (let ((form (if (rest primary
)
751 `(call-method ,(first primary
) ,(rest primary
))
752 `(call-method ,(first primary
)))))
754 `(call-method ,(first around
) (,@(rest around
)
755 (make-method ,form
)))
757 `(make-method (error "No primary methods")))))
758 (let* ((method (first methods
))
759 (qualifier (first (method-qualifiers method
))))
761 ((equal :around qualifier
)
762 (push method around
))
764 (push method primary
))))))
766 (defgeneric wam-test-mc-a
(val)
767 (:method-combination wam-test-method-combination-a
))
768 (assert-error (wam-test-mc-a 13))
769 (defmethod wam-test-mc-a ((val number
))
770 (+ val
(if (next-method-p) (call-next-method) 0)))
771 (assert (= (wam-test-mc-a 13) 13))
772 (defmethod wam-test-mc-a :around
((val number
))
773 (+ val
(if (next-method-p) (call-next-method) 0)))
774 (assert (= (wam-test-mc-a 13) 26))
776 ;;; DEFINE-METHOD-COMBINATION
777 ;;; When two methods are in the same method group and have the same
778 ;;; specializers, their sort order within the group may be ambiguous. Therefore,
779 ;;; we should throw an error when we have two methods in the same group with
780 ;;; the same specializers /as long as/ we have more than one method group
781 ;;; or our single method group qualifier-pattern is not *. This resolves the
782 ;;; apparent conflict with the above 'It is also possible to bypass' language.
784 ;;; The language specifying this behavior is:
785 ;;; "Note that two methods with identical specializers, but with different
786 ;;; qualifiers, are not ordered by the algorithm described in Step 2 of the
787 ;;; method selection and combination process described in Section 7.6.6
788 ;;; (Method Selection and Combination). Normally the two methods play different
789 ;;; roles in the effective method because they have different qualifiers, and
790 ;;; no matter how they are ordered in the result of Step 2, the effective
791 ;;; method is the same. If the two methods play the same role and their order
792 ;;; matters, an error is signaled. This happens as part of the qualifier
793 ;;; pattern matching in define-method-combination."
795 ;;; Note that the spec pretty much equates 'method group' and 'role'.
796 ;; First we ensure that it fails correctly when there is more than one
798 (define-method-combination wam-test-method-combination-b
()
800 (primary * :required t
))
801 (let ((form (if (rest primary
)
802 `(call-method ,(first primary
) ,(rest primary
))
803 `(call-method ,(first primary
)))))
805 `(call-method ,(first around
) (,@(rest around
)
806 (make-method ,form
)))
809 (defgeneric wam-test-mc-b
(val)
810 (:method-combination wam-test-method-combination-b
))
811 (defmethod wam-test-mc-b ((val number
))
812 (+ val
(if (next-method-p) (call-next-method) 0)))
813 (assert (= (wam-test-mc-b 13) 13))
814 (defmethod wam-test-mc-b :around
((val number
))
815 (+ val
(if (next-method-p) (call-next-method) 0)))
816 (assert (= (wam-test-mc-b 13) 26))
817 (defmethod wam-test-mc-b :somethingelse
((val number
))
818 (+ val
(if (next-method-p) (call-next-method) 0)))
819 (let ((*error-output
* (make-broadcast-stream)))
820 (assert-error (wam-test-mc-b 13)))
822 ;;; now, ensure that it fails with a single group with a qualifier-pattern
824 (define-method-combination wam-test-method-combination-c
()
825 ((methods listp
:required t
))
827 `(call-method ,(first methods
) ,(rest methods
))
828 `(call-method ,(first methods
))))
830 (defgeneric wam-test-mc-c
(val)
831 (:method-combination wam-test-method-combination-c
))
832 (assert-error (wam-test-mc-c 13))
833 (defmethod wam-test-mc-c :foo
((val number
))
834 (+ val
(if (next-method-p) (call-next-method) 0)))
835 (assert (= (wam-test-mc-c 13) 13))
836 (defmethod wam-test-mc-c :bar
((val number
))
837 (+ val
(if (next-method-p) (call-next-method) 0)))
838 (let ((*error-output
* (make-broadcast-stream)))
839 (assert-error (wam-test-mc-c 13)))
841 ;;; DEFMETHOD should signal an ERROR if an incompatible lambda list is
843 (handler-bind ((implicit-generic-function-warning #'muffle-warning
))
844 (defmethod incompatible-ll-test-1 (x) x
))
845 (assert-error (defmethod incompatible-ll-test-1 (x y
) y
))
846 (assert-error (defmethod incompatible-ll-test-1 (x &rest y
) y
))
847 ;;; Sneakily using a bit of MOPness to check some consistency
849 (sb-pcl:generic-function-methods
#'incompatible-ll-test-1
)) 1))
851 (handler-bind ((implicit-generic-function-warning #'muffle-warning
))
852 (defmethod incompatible-ll-test-2 (x &key bar
) bar
))
853 (assert-error (defmethod incompatible-ll-test-2 (x) x
))
854 (defmethod incompatible-ll-test-2 (x &rest y
) y
)
856 (sb-pcl:generic-function-methods
#'incompatible-ll-test-2
)) 1))
857 (defmethod incompatible-ll-test-2 ((x integer
) &key bar
) bar
)
859 (sb-pcl:generic-function-methods
#'incompatible-ll-test-2
)) 2))
861 ;;; Per Christophe, this is an illegal method call because of 7.6.5
862 (handler-bind ((style-warning #'muffle-warning
))
863 (eval '(assert-error (incompatible-ll-test-2 t
1 2))))
865 (assert (eq (incompatible-ll-test-2 1 :bar
'yes
) 'yes
))
867 (handler-bind ((implicit-generic-function-warning #'muffle-warning
))
868 (defmethod incompatible-ll-test-3 ((x integer
)) x
))
869 (remove-method #'incompatible-ll-test-3
870 (find-method #'incompatible-ll-test-3
872 (list (find-class 'integer
))))
873 (assert-error (defmethod incompatible-ll-test-3 (x y
) (list x y
)))
876 ;;; Attempting to instantiate classes with forward references in their
877 ;;; CPL should signal errors (FIXME: of what type?)
878 (defclass never-finished-class
(this-one-unfinished-too) ())
879 (multiple-value-bind (result error
)
880 (ignore-errors (make-instance 'never-finished-class
))
881 (assert (null result
))
882 (assert (typep error
'error
)))
883 (multiple-value-bind (result error
)
884 (ignore-errors (make-instance 'this-one-unfinished-too
))
885 (assert (null result
))
886 (assert (typep error
'error
)))
888 ;;; Classes with :ALLOCATION :CLASS slots should be subclassable (and
889 ;;; weren't for a while in sbcl-0.7.9.xx)
890 (defclass superclass-with-slot
()
891 ((a :allocation
:class
)))
892 (defclass subclass-for-class-allocation
(superclass-with-slot) ())
893 (make-instance 'subclass-for-class-allocation
)
895 ;;; bug #136: CALL-NEXT-METHOD was being a little too lexical,
896 ;;; resulting in failure in the following:
897 (handler-bind ((implicit-generic-function-warning #'muffle-warning
))
898 (defmethod call-next-method-lexical-args ((x integer
))
900 (defmethod call-next-method-lexical-args :around
((x integer
))
902 (declare (ignorable x
))
904 (assert (= (call-next-method-lexical-args 3) 3))
906 ;;; DEFINE-METHOD-COMBINATION with arguments was hopelessly broken
908 (defvar *d-m-c-args-test
* nil
)
909 (define-method-combination progn-with-lock
()
913 (progn (lock (object-lock ,object
))
914 ,@(mapcar #'(lambda (method)
915 `(call-method ,method
))
917 (unlock (object-lock ,object
))))
918 (defun object-lock (obj)
919 (push "object-lock" *d-m-c-args-test
*)
922 (push "unlock" *d-m-c-args-test
*)
925 (push "lock" *d-m-c-args-test
*)
927 (defgeneric d-m-c-args-test
(x)
928 (:method-combination progn-with-lock
))
929 (defmethod d-m-c-args-test ((x symbol
))
930 (push "primary" *d-m-c-args-test
*))
931 (defmethod d-m-c-args-test ((x number
))
933 (assert (equal (d-m-c-args-test t
) '("primary" "lock" "object-lock")))
934 (assert (equal *d-m-c-args-test
*
935 '("unlock" "object-lock" "primary" "lock" "object-lock")))
936 (setf *d-m-c-args-test
* nil
)
937 (ignore-errors (d-m-c-args-test 1))
938 (assert (equal *d-m-c-args-test
*
939 '("unlock" "object-lock" "lock" "object-lock")))
941 ;;; The walker (on which DEFMETHOD depended) didn't know how to handle
942 ;;; SYMBOL-MACROLET properly. In fact, as of sbcl-0.7.10.20 it still
943 ;;; doesn't, but it does well enough to compile the following without
944 ;;; error (the problems remain in asking for a complete macroexpansion
945 ;;; of an arbitrary form).
946 (defgeneric bug222
(z))
947 (symbol-macrolet ((x 1))
948 (defmethod bug222 (z)
949 (macrolet ((frob (form) `(progn ,form
,x
)))
950 (frob (princ-to-string x
)))))
951 (assert (= (bug222 t
) 1))
953 ;;; also, a test case to guard against bogus environment hacking:
955 (eval-when (:compile-toplevel
:load-toplevel
:execute
)
957 (defgeneric bug222-b
(z stream
))
958 ;;; this should at the least compile:
960 (defmethod bug222-b (z stream
)
961 (macrolet ((frob (form)
962 ;; This macro's expander should see the global value of BUG222-B.
963 ;; But forcing it do to so by explicit call of SYMBOL-VALUE
964 ;; would defeat the purpose of the test. So ignore the warning.
965 (declare (muffle-conditions warning
))
966 `(progn ,form
,bug222-b
)))
967 (frob (format stream
"~D~%" bug222-b
)))))
968 ;;; and it would be nice (though not specified by ANSI) if the answer
970 (let ((x (make-string-output-stream)))
971 (let ((value (bug222-b t x
)))
972 ;; not specified by ANSI
973 #+#.
(cl:if
(cl:eq sb-ext
:*evaluator-mode
* :compile
) '(and) '(or))
974 (assert (= value
3)))
976 (assert (char= (char (get-output-stream-string x
) 0) #\
1)))
978 ;;; REINITIALIZE-INSTANCE, in the ctor optimization, wasn't checking
979 ;;; for invalid initargs where it should:
980 (defclass class234
() ())
981 (defclass subclass234
(class234) ())
984 (reinitialize-instance (make-instance 'class234
) :dummy
0))
986 (reinitialize-instance (make-instance 'subclass234
) :dummy
0))
987 (assert-error (bug-234) program-error
)
988 (defmethod shared-initialize :after
((i class234
) slots
&key dummy
)
989 (declare (ignore dummy
))
991 (assert (typep (subbug-234) 'subclass234
))
993 ;; once for MAKE-INSTANCE, once for REINITIALIZE-INSTANCE
996 ;;; also, some combinations of MAKE-INSTANCE and subclassing missed
997 ;;; new methods (Gerd Moellmann sbcl-devel 2002-12-29):
998 (defclass class234-b1
() ())
999 (defclass class234-b2
(class234-b1) ())
1000 (defvar *bug234-b
* 0)
1002 (make-instance 'class234-b2
))
1005 (assert (= *bug234-b
* 0))
1006 (defmethod initialize-instance :before
((x class234-b1
) &rest args
)
1007 (declare (ignore args
))
1010 (assert (= *bug234-b
* 1))
1012 ;;; we should be able to make classes with uninterned names:
1013 (defclass #:class-with-uninterned-name
() ())
1015 ;;; SLOT-MISSING should be called when there are missing slots.
1016 (defclass class-with-all-slots-missing
() ())
1017 (defmethod slot-missing (class (o class-with-all-slots-missing
)
1019 &optional new-value
)
1020 (declare (ignore new-value
))
1023 (with-test (:name
:slot-value-missing
)
1024 (assert (equal (multiple-value-list
1025 (slot-value (make-instance 'class-with-all-slots-missing
) 'foo
))
1027 (assert (equal (multiple-value-list
1028 (funcall (lambda (x) (slot-value x
'bar
))
1029 (make-instance 'class-with-all-slots-missing
)))
1032 (with-test (:name
:slot-boundp-missing
)
1033 (assert (equal (multiple-value-list
1034 (slot-boundp (make-instance 'class-with-all-slots-missing
) 'foo
))
1036 (assert (equal (multiple-value-list
1037 (funcall (lambda (x) (slot-boundp x
'bar
))
1038 (make-instance 'class-with-all-slots-missing
)))
1041 (with-test (:name
:slot-setf-missing
)
1042 (assert (equal (multiple-value-list
1043 (setf (slot-value (make-instance 'class-with-all-slots-missing
) 'foo
) 10))
1045 (assert (equal (multiple-value-list
1046 (funcall (lambda (x) (setf (slot-value x
'bar
) 20))
1047 (make-instance 'class-with-all-slots-missing
)))
1050 (macrolet ((try (which)
1051 `(assert (eq ((lambda (x)
1052 (declare (,which sb-pcl
::set-slot-value
))
1053 (setf (slot-value x
'b
) 'baz
))
1054 (make-instance 'class-with-all-slots-missing
))
1055 ;; SLOT-MISSING's value is specified to be ignored; we
1056 ;; return NEW-VALUE.
1061 ;;; we should be able to specialize on anything that names a class.
1062 (defclass name-for-class
() ())
1063 (handler-bind ((implicit-generic-function-warning #'muffle-warning
))
1064 (defmethod something-that-specializes ((x name-for-class
)) 1))
1065 (setf (find-class 'other-name-for-class
) (find-class 'name-for-class
))
1066 (defmethod something-that-specializes ((x other-name-for-class
)) 2)
1067 (assert (= (something-that-specializes (make-instance 'name-for-class
)) 2))
1068 (assert (= (something-that-specializes (make-instance 'other-name-for-class
))
1071 ;;; more forward referenced classes stuff
1072 (defclass frc-1
(frc-2) ())
1073 (assert (subtypep 'frc-1
(find-class 'frc-2
)))
1074 (assert (subtypep (find-class 'frc-1
) 'frc-2
))
1075 (assert (not (subtypep (find-class 'frc-2
) 'frc-1
)))
1076 (defclass frc-2
(frc-3) ((a :initarg
:a
)))
1077 (assert (subtypep 'frc-1
(find-class 'frc-3
)))
1078 (defclass frc-3
() ())
1079 (assert (typep (make-instance 'frc-1
:a
2) (find-class 'frc-1
)))
1080 (assert (typep (make-instance 'frc-2
:a
3) (find-class 'frc-2
)))
1082 ;;; check that we can define classes with two slots of different names
1083 ;;; (even if it STYLE-WARNs).
1084 (defclass odd-name-class
()
1085 ((name :initarg
:name
)
1086 (cl-user::name
:initarg
:name2
)))
1087 (handler-bind ((style-warning #'muffle-warning
))
1088 (let ((x (make-instance 'odd-name-class
:name
1 :name2
2)))
1089 (assert (= (slot-value x
'name
) 1))
1090 (assert (= (slot-value x
'cl-user
::name
) 2))))
1092 ;;; ALLOCATE-INSTANCE should work on structures, even if defined by
1093 ;;; DEFSTRUCT (and not DEFCLASS :METACLASS STRUCTURE-CLASS).
1094 (defstruct allocatable-structure a
)
1095 (assert (typep (allocate-instance (find-class 'allocatable-structure
))
1096 'allocatable-structure
))
1098 ;;; Bug found by Paul Dietz when devising CPL tests: somewhat
1099 ;;; amazingly, calls to CPL would work a couple of times, and then
1100 ;;; start returning NIL. A fix was found (relating to the
1101 ;;; applicability of constant-dfun optimization) by Gerd Moellmann.
1103 (:method-combination list
)
1104 (:method list
((x broadcast-stream
)) 'broadcast-stream
)
1105 (:method list
((x integer
)) 'integer
)
1106 (:method list
((x number
)) 'number
)
1107 (:method list
((x stream
)) 'stream
)
1108 (:method list
((x structure-object
)) 'structure-object
))
1109 (assert (equal (cpl 0) '(integer number
)))
1110 (assert (equal (cpl 0) '(integer number
)))
1111 (assert (equal (cpl 0) '(integer number
)))
1112 (assert (equal (cpl 0) '(integer number
)))
1113 (assert (equal (cpl 0) '(integer number
)))
1114 (assert (equal (cpl (make-broadcast-stream))
1115 '(broadcast-stream stream structure-object
)))
1116 (assert (equal (cpl (make-broadcast-stream))
1117 '(broadcast-stream stream structure-object
)))
1118 (assert (equal (cpl (make-broadcast-stream))
1119 '(broadcast-stream stream structure-object
)))
1121 ;;; Bug in CALL-NEXT-METHOD: assignment to the method's formal
1122 ;;; parameters shouldn't affect the arguments to the next method for a
1123 ;;; no-argument call to CALL-NEXT-METHOD
1124 (defgeneric cnm-assignment
(x)
1126 (:method
((x integer
)) (setq x
3)
1127 (list x
(call-next-method) (call-next-method x
))))
1128 (assert (equal (cnm-assignment 1) '(3 1 3)))
1130 ;;; Bug reported by Istvan Marko 2003-07-09
1131 (let ((class-name (gentemp)))
1132 (loop for i from
1 to
9
1133 for slot-name
= (intern (format nil
"X~D" i
))
1134 for initarg-name
= (intern (format nil
"X~D" i
) :keyword
)
1135 collect
`(,slot-name
:initarg
,initarg-name
) into slot-descs
1136 append
`(,initarg-name
(list 0)) into default-initargs
1137 finally
(eval `(defclass ,class-name
()
1139 (:default-initargs
,@default-initargs
))))
1140 (let ((f (compile nil
`(lambda () (make-instance ',class-name
)))))
1141 (assert (typep (funcall f
) class-name
))))
1143 ;;; bug 262: DEFMETHOD failed on a generic function without a lambda
1145 (ensure-generic-function 'bug262
)
1146 (defmethod bug262 (x y
)
1148 (assert (equal (bug262 1 2) '(1 2)))
1150 ;;; salex on #lisp 2003-10-13 reported that type declarations inside
1151 ;;; WITH-SLOTS are too hairy to be checked
1152 (defun ensure-no-notes (form)
1153 (handler-case (compile nil
`(lambda () ,form
))
1154 (sb-ext:compiler-note
(c)
1155 ;; FIXME: it would be better to check specifically for the "type
1156 ;; is too hairy" note
1159 (ensure-no-notes '(with-slots (a) *x
*
1160 (declare (integer a
))
1162 (ensure-no-notes '(with-slots (a) *x
*
1163 (declare (integer a
))
1164 (declare (notinline slot-value
))
1167 ;;; from CLHS 7.6.5.1
1168 (defclass character-class
() ((char :initarg
:char
)))
1169 (defclass picture-class
() ((glyph :initarg
:glyph
)))
1170 (defclass character-picture-class
(character-class picture-class
) ())
1172 (handler-bind ((implicit-generic-function-warning #'muffle-warning
))
1173 (defmethod width ((c character-class
) &key font
) font
))
1174 (defmethod width ((p picture-class
) &key pixel-size
) pixel-size
)
1177 (width (make-instance 'character-class
:char
#\Q
)
1178 :font
'baskerville
:pixel-size
10)
1181 (width (make-instance 'picture-class
:glyph
#\Q
)
1182 :font
'baskerville
:pixel-size
10)
1184 (assert (eq (width (make-instance 'character-picture-class
:char
#\Q
)
1185 :font
'baskerville
:pixel-size
10)
1188 ;;; class redefinition shouldn't give any warnings, in the usual case
1189 (defclass about-to-be-redefined
() ((some-slot :accessor some-slot
)))
1190 (handler-bind ((warning #'error
))
1191 (defclass about-to-be-redefined
() ((some-slot :accessor some-slot
))))
1193 ;;; attempts to add accessorish methods to generic functions with more
1194 ;;; complex lambda lists should fail
1195 (defgeneric accessoroid
(object &key
&allow-other-keys
))
1197 (defclass accessoroid-class
() ((slot :accessor accessoroid
)))
1200 ;;; reported by Bruno Haible sbcl-devel 2004-04-15
1201 (defclass shared-slot-and-redefinition
()
1202 ((size :initarg
:size
:initform
1 :allocation
:class
)))
1203 (let ((i (make-instance 'shared-slot-and-redefinition
)))
1204 (defclass shared-slot-and-redefinition
()
1205 ((size :initarg
:size
:initform
2 :allocation
:class
)))
1206 (assert (= (slot-value i
'size
) 1)))
1208 ;;; reported by Bruno Haible sbcl-devel 2004-04-15
1209 (defclass superclass-born-to-be-obsoleted
() (a))
1210 (defclass subclass-born-to-be-obsoleted
(superclass-born-to-be-obsoleted) ())
1211 (defparameter *born-to-be-obsoleted
*
1212 (make-instance 'subclass-born-to-be-obsoleted
))
1213 (defparameter *born-to-be-obsoleted-obsoleted
* nil
)
1214 (defmethod update-instance-for-redefined-class
1215 ((o subclass-born-to-be-obsoleted
) a d pl
&key
)
1216 (setf *born-to-be-obsoleted-obsoleted
* t
))
1217 (make-instances-obsolete 'superclass-born-to-be-obsoleted
)
1218 (slot-boundp *born-to-be-obsoleted
* 'a
)
1219 (assert *born-to-be-obsoleted-obsoleted
*)
1221 ;;; additional test suggested by Bruno Haible sbcl-devel 2004-04-21
1222 (defclass super-super-obsoleted
() (a))
1223 (defclass super-obsoleted-1
(super-super-obsoleted) ())
1224 (defclass super-obsoleted-2
(super-super-obsoleted) ())
1225 (defclass obsoleted
(super-obsoleted-1 super-obsoleted-2
) ())
1226 (defparameter *obsoleted
* (make-instance 'obsoleted
))
1227 (defparameter *obsoleted-counter
* 0)
1228 (defmethod update-instance-for-redefined-class ((o obsoleted
) a d pl
&key
)
1229 (incf *obsoleted-counter
*))
1230 (make-instances-obsolete 'super-super-obsoleted
)
1231 (slot-boundp *obsoleted
* 'a
)
1232 (assert (= *obsoleted-counter
* 1))
1234 ;;; yet another MAKE-INSTANCES-OBSOLETE test, this time from Nikodemus
1235 ;;; Siivola. Not all methods for accessing slots are created equal...
1236 (defclass yet-another-obsoletion-super
() ((obs :accessor obs-of
:initform
0)))
1237 (defclass yet-another-obsoletion-sub
(yet-another-obsoletion-super) ())
1238 (defmethod shared-initialize :after
((i yet-another-obsoletion-super
)
1240 (declare (ignore init
))
1243 (defvar *yao-super
* (make-instance 'yet-another-obsoletion-super
))
1244 (defvar *yao-sub
* (make-instance 'yet-another-obsoletion-sub
))
1246 (assert (= (obs-of *yao-super
*) 1))
1247 (assert (= (obs-of *yao-sub
*) 1))
1248 (make-instances-obsolete 'yet-another-obsoletion-super
)
1249 (assert (= (obs-of *yao-sub
*) 2))
1250 (assert (= (obs-of *yao-super
*) 2))
1251 (make-instances-obsolete 'yet-another-obsoletion-super
)
1252 (assert (= (obs-of *yao-super
*) 3))
1253 (assert (= (obs-of *yao-sub
*) 3))
1254 (assert (= (slot-value *yao-super
* 'obs
) 3))
1255 (assert (= (slot-value *yao-sub
* 'obs
) 3))
1257 ;;; one more MIO test: variable slot names
1258 (defclass mio
() ((x :initform
42)))
1259 (defvar *mio-slot
* 'x
)
1260 (defparameter *mio-counter
* 0)
1261 (defmethod update-instance-for-redefined-class ((instance mio
) new old plist
&key
)
1262 (incf *mio-counter
*))
1264 (let ((x (make-instance 'mio
)))
1265 (make-instances-obsolete 'mio
)
1266 (slot-value x
*mio-slot
*))
1268 (let ((x (make-instance 'mio
)))
1269 (make-instances-obsolete 'mio
)
1270 (setf (slot-value x
*mio-slot
*) 13))
1272 (let ((x (make-instance 'mio
)))
1273 (make-instances-obsolete 'mio
)
1274 (slot-boundp x
*mio-slot
*))
1276 (let ((x (make-instance 'mio
)))
1277 (make-instances-obsolete 'mio
)
1278 (slot-makunbound x
*mio-slot
*))
1280 (assert (= 4 *mio-counter
*))
1282 ;;; :class -> :instance slot allocation transfers of inherited slots,
1283 ;;; reported by Bruno Haible
1284 (with-test (:name
(defclass :redefinition-class-
>instance-allocation
))
1286 (defclass super-with-magic-slot
()
1287 ((magic :initarg
:size
:initform
1 :allocation
:class
)))
1288 (defclass sub-of-super-with-magic-slot
(super-with-magic-slot) ())
1289 (setq i
(make-instance 'sub-of-super-with-magic-slot
))
1290 (defclass super-with-magic-slot
()
1291 ((magic :initarg
:size
:initform
2)))
1292 (assert (= 1 (slot-value i
'magic
)))))
1294 ;;; MAKE-INSTANCES-OBSOLETE return values
1295 (with-test (:name
(make-instances-obsolete :return-values
) )
1296 (defclass one-more-to-obsolete
() ())
1297 (assert (eq 'one-more-to-obsolete
1298 (make-instances-obsolete 'one-more-to-obsolete
)))
1299 (assert (eq (find-class 'one-more-to-obsolete
)
1300 (make-instances-obsolete (find-class 'one-more-to-obsolete
)))))
1302 ;;; Sensible error instead of a BUG. Reported by Thomas Burdick.
1303 (with-test (:name
(defclass :slot-with-duplicate-accessors
))
1304 (assert-error (defclass slot-with-duplicate-accessors
()
1305 ((slot :writer get-slot
:reader get-slot
)))
1306 (and error
(not sb-int
:bug
))))
1308 ;;; BUG 321: errors in parsing DEFINE-METHOD-COMBINATION arguments
1311 (define-method-combination w-args
()
1313 (:arguments arg1 arg2
&aux
(extra :extra
))
1314 `(progn ,arg1
,arg2
,extra
1315 ,@(mapcar (lambda (method) `(call-method ,method
)) method-list
)))
1316 (defgeneric mc-test-w-args
(p1 p2 s
)
1317 (:method-combination w-args
)
1318 (:method
((p1 number
) (p2 t
) s
)
1319 (vector-push-extend (list 'number p1 p2
) s
))
1320 (:method
((p1 string
) (p2 t
) s
)
1321 (vector-push-extend (list 'string p1 p2
) s
))
1322 (:method
((p1 t
) (p2 t
) s
) (vector-push-extend (list t p1 p2
) s
)))
1323 (let ((v (make-array 0 :adjustable t
:fill-pointer t
)))
1324 (assert (= (mc-test-w-args 1 2 v
) 1))
1325 (assert (equal (aref v
0) '(number 1 2)))
1326 (assert (equal (aref v
1) '(t 1 2))))
1328 ;;; BUG 276: declarations and mutation.
1329 (handler-bind ((implicit-generic-function-warning #'muffle-warning
))
1330 (defmethod fee ((x fixnum
))
1333 (assert (= (fee 1) 1/2))
1334 (handler-bind ((implicit-generic-function-warning #'muffle-warning
))
1335 (defmethod fum ((x fixnum
))
1338 (assert (= (fum 3) 3/2))
1339 (handler-bind ((implicit-generic-function-warning #'muffle-warning
))
1340 (defmethod fii ((x fixnum
))
1341 (declare (special x
))
1344 (assert (= (fii 1) 1/2))
1346 (handler-bind ((implicit-generic-function-warning #'muffle-warning
))
1347 (defmethod faa ((*faa
* string-stream
))
1348 (setq *faa
* (make-broadcast-stream *faa
*))
1349 (write-line "Break, you sucker!" *faa
*)
1351 (assert (eq 'ok
(faa (make-string-output-stream))))
1352 (handler-bind ((implicit-generic-function-warning #'muffle-warning
))
1353 (defmethod fex ((x fixnum
) (y fixnum
))
1354 (multiple-value-setq (x y
) (values (/ x y
) (/ y x
)))
1356 (assert (equal (fex 5 3) '(5/3 3/5)))
1358 ;;; Bug reported by Zach Beane; incorrect return of (function
1359 ;;; ',fun-name) in defgeneric
1361 (typep (funcall (compile nil
1362 '(lambda () (flet ((nonsense () nil
))
1363 (declare (ignorable #'nonsense
))
1364 (defgeneric nonsense
())))))
1368 (typep (funcall (compile nil
1369 '(lambda () (flet ((nonsense-2 () nil
))
1370 (declare (ignorable #'nonsense-2
))
1371 (defgeneric nonsense-2
()
1375 ;;; bug reported by Bruno Haible: (setf find-class) using a
1376 ;;; forward-referenced class
1377 (defclass fr-sub
(fr-super) ())
1378 (setf (find-class 'fr-alt
) (find-class 'fr-super
))
1379 (assert (eq (find-class 'fr-alt
) (find-class 'fr-super
)))
1382 ;;; ANSI Figure 4-8: all defined classes. Check that we can define
1383 ;;; methods on all of these.
1385 (defgeneric method-for-defined-classes
(x))
1386 (dolist (c '(arithmetic-error
1387 generic-function simple-error array hash-table
1389 bit-vector integer simple-warning
1390 broadcast-stream list standard-class
1391 built-in-class logical-pathname standard-generic-function
1392 cell-error method standard-method
1393 character method-combination standard-object
1394 class null storage-condition
1395 complex number stream
1396 concatenated-stream package stream-error
1397 condition package-error string
1398 cons parse-error string-stream
1399 control-error pathname structure-class
1400 division-by-zero print-not-readable structure-object
1401 echo-stream program-error style-warning
1402 end-of-file random-state symbol
1403 error ratio synonym-stream
1404 file-error rational t
1405 file-stream reader-error two-way-stream
1406 float readtable type-error
1407 floating-point-inexact real unbound-slot
1408 floating-point-invalid-operation restart unbound-variable
1409 floating-point-overflow sequence undefined-function
1410 floating-point-underflow serious-condition vector
1411 function simple-condition warning
))
1412 (eval `(defmethod method-for-defined-classes ((x ,c
)) (princ x
))))
1413 (assert (string= (with-output-to-string (*standard-output
*)
1414 (method-for-defined-classes #\
3))
1419 ;;; When class definition does not complete due to a bad accessor
1420 ;;; name, do not cause an error when a new accessor name is provided
1421 ;;; during class redefinition
1423 (defun existing-name (object)
1426 (assert-error (defclass redefinition-of-accessor-class
()
1427 ((slot :accessor existing-name
))))
1429 (defclass redefinition-of-accessor-class
()
1430 ((slot :accessor new-name
)))
1434 (load "package-ctor-bug.lisp")
1435 (assert (= (package-ctor-bug:test
) 3))
1436 (delete-package "PACKAGE-CTOR-BUG")
1437 (load "package-ctor-bug.lisp")
1438 (assert (= (package-ctor-bug:test
) 3))
1440 (with-test (:name
(defmethod (setf find-class
) integer
))
1441 (handler-bind ((warning #'muffle-warning
))
1444 (deftype defined-type
() 'integer
)
1446 (defmethod method-on-defined-type ((x defined-type
)) x
))
1447 (deftype defined-type-and-class
() 'integer
)
1448 (setf (find-class 'defined-type-and-class
) (find-class 'integer
))
1449 (defmethod method-on-defined-type-and-class
1450 ((x defined-type-and-class
))
1452 (assert (= (method-on-defined-type-and-class 3) 4))))))
1455 (let (#+nil
; no more sb-pcl::*max-emf-precomputation-methods* as of
1457 (sb-pcl::*max-emf-precomputation-methods
* 0))
1458 (handler-bind ((warning #'muffle-warning
)) ; "invalid qualifiers"
1459 (eval '(defgeneric bug-281
(x)
1460 (:method-combination
+)
1461 (:method
((x symbol
)) 1)
1462 (:method
+ ((x number
)) x
))))
1463 (assert (= 1 (funcall 'bug-281
1)))
1464 (assert (= 4.2 (funcall 'bug-281
4.2)))
1465 (multiple-value-bind (val err
) (ignore-errors (funcall 'bug-281
'symbol
))
1467 (assert (typep err
'error
))))
1469 ;;; RESTART-CASE and CALL-METHOD
1471 ;;; from Bruno Haible
1473 (defun rc-cm/prompt-for-new-values
()
1474 (format *debug-io
* "~&New values: ")
1475 (finish-output *debug-io
*)
1476 (list (read *debug-io
*)))
1478 (defun rc-cm/add-method-restarts
(form method
)
1479 (let ((block (gensym))
1487 :report
(lambda (stream)
1488 (format stream
"Try calling ~S again." ,method
))
1491 :report
(lambda (stream)
1492 (format stream
"Specify return values for ~S call."
1494 :interactive
(lambda () (rc-cm/prompt-for-new-values
))
1495 (return-from ,block
(values-list l
)))))))))
1497 (defun rc-cm/convert-effective-method
(efm)
1499 (if (eq (car efm
) 'call-method
)
1500 (let ((method-list (third efm
)))
1501 (if (or (typep (first method-list
) 'method
) (rest method-list
))
1502 ;; Reduce the case of multiple methods to a single one.
1503 ;; Make the call to the next-method explicit.
1504 (rc-cm/convert-effective-method
1505 `(call-method ,(second efm
)
1507 (call-method ,(first method-list
) ,(rest method-list
))))))
1508 ;; Now the case of at most one method.
1509 (if (typep (second efm
) 'method
)
1510 ;; Wrap the method call in a RESTART-CASE.
1511 (rc-cm/add-method-restarts
1512 (cons (rc-cm/convert-effective-method
(car efm
))
1513 (rc-cm/convert-effective-method
(cdr efm
)))
1515 ;; Normal recursive processing.
1516 (cons (rc-cm/convert-effective-method
(car efm
))
1517 (rc-cm/convert-effective-method
(cdr efm
))))))
1518 (cons (rc-cm/convert-effective-method
(car efm
))
1519 (rc-cm/convert-effective-method
(cdr efm
))))
1522 (define-method-combination standard-with-restarts
()
1525 (primary () :required t
)
1527 (flet ((call-methods-sequentially (methods)
1528 (mapcar #'(lambda (method)
1529 `(call-method ,method
))
1531 (let ((form (if (or before after
(rest primary
))
1532 `(multiple-value-prog1
1534 ,@(call-methods-sequentially before
)
1535 (call-method ,(first primary
) ,(rest primary
)))
1536 ,@(call-methods-sequentially (reverse after
)))
1537 `(call-method ,(first primary
)))))
1540 `(call-method ,(first around
)
1541 (,@(rest around
) (make-method ,form
)))))
1542 (rc-cm/convert-effective-method form
))))
1544 (defgeneric rc-cm
/testgf16
(x)
1545 (:method-combination standard-with-restarts
))
1546 (defclass rc-cm
/testclass16a
() ())
1547 (defclass rc-cm
/testclass16b
(rc-cm/testclass16a
) ())
1548 (defclass rc-cm
/testclass16c
(rc-cm/testclass16a
) ())
1549 (defclass rc-cm
/testclass16d
(rc-cm/testclass16b rc-cm
/testclass16c
) ())
1550 (defmethod rc-cm/testgf16
((x rc-cm
/testclass16a
))
1552 (not (null (find-restart 'method-redo
)))
1553 (not (null (find-restart 'method-return
)))))
1554 (defmethod rc-cm/testgf16
((x rc-cm
/testclass16b
))
1555 (cons 'b
(call-next-method)))
1556 (defmethod rc-cm/testgf16
((x rc-cm
/testclass16c
))
1557 (cons 'c
(call-next-method)))
1558 (defmethod rc-cm/testgf16
((x rc-cm
/testclass16d
))
1559 (cons 'd
(call-next-method)))
1560 (assert (equal (rc-cm/testgf16
(make-instance 'rc-cm
/testclass16d
))
1563 ;;; test case from Gerd Moellmann
1564 (define-method-combination r-c
/c-m-1
()
1565 ((primary () :required t
))
1566 `(restart-case (call-method ,(first primary
))))
1568 (defgeneric r-c
/c-m-1-gf
()
1569 (:method-combination r-c
/c-m-1
)
1572 (assert (null (r-c/c-m-1-gf
)))
1574 (handler-bind ((warning #'error
))
1575 (eval '(defclass class-for-ctor
/class-slot
()
1576 ((class-slot :initarg
:class-slot
:allocation
:class
))))
1577 (eval '(let ((c1 (make-instance 'class-for-ctor
/class-slot
))
1578 (c2 (make-instance 'class-for-ctor
/class-slot
:class-slot
1)))
1579 (assert (equal (list (slot-value c1
'class-slot
)
1580 (slot-value c2
'class-slot
))
1583 ;;; tests of ctors on anonymous classes
1584 (defparameter *unnamed
* (defclass ctor-unnamed-literal-class
() ()))
1585 (setf (class-name *unnamed
*) nil
)
1586 (setf (find-class 'ctor-unnamed-literal-class
) nil
)
1587 (defparameter *unnamed2
* (defclass ctor-unnamed-literal-class2
() ()))
1588 (defun ctor-unnamed-literal-class ()
1589 (make-instance '#.
*unnamed
*))
1590 (compile 'ctor-unnamed-literal-class
)
1591 (defun ctor-unnamed-literal-class2 ()
1592 (make-instance '#.
(find-class 'ctor-unnamed-literal-class2
)))
1593 (compile 'ctor-unnamed-literal-class2
)
1594 (defun ctor-unnamed-literal-class2/symbol
()
1595 (make-instance 'ctor-unnamed-literal-class2
))
1596 (compile 'ctor-unnamed-literal-class2
/symbol
)
1597 (setf (class-name *unnamed2
*) nil
)
1598 (setf (find-class 'ctor-unnamed-literal-class2
) nil
)
1599 (with-test (:name
(:ctor
:unnamed-before
))
1600 (assert (typep (ctor-unnamed-literal-class) *unnamed
*)))
1601 (with-test (:name
(:ctor
:unnamed-after
))
1602 (assert (typep (ctor-unnamed-literal-class2) *unnamed2
*)))
1603 (with-test (:name
(:ctor
:unnamed-after
/symbol
))
1604 (assert-error (ctor-unnamed-literal-class2/symbol
)))
1606 ;;; classes with slot types shouldn't break if the types don't name
1607 ;;; classes (bug #391)
1608 (defclass slot-type-superclass
() ((slot :type fixnum
)))
1609 (defclass slot-type-subclass
(slot-type-superclass)
1610 ((slot :type
(integer 1 5))))
1611 (let ((instance (make-instance 'slot-type-subclass
)))
1612 (setf (slot-value instance
'slot
) 3))
1614 ;;; ctors where there's a non-standard SHARED-INITIALIZE method and an
1615 ;;; initarg which isn't self-evaluating (kpreid on #lisp 2006-01-29)
1616 (defclass kpreid-enode
()
1617 ((slot :initarg not-a-keyword
)))
1618 (defmethod shared-initialize ((o kpreid-enode
) slots
&key
&allow-other-keys
)
1620 (defun make-kpreid-enode ()
1621 (make-instance 'kpreid-enode
'not-a-keyword
3))
1622 (with-test (:name
(:ctor
:non-keyword-initarg
))
1623 (let ((x (make-kpreid-enode))
1624 (y (make-kpreid-enode)))
1625 (= (slot-value x
'slot
) (slot-value y
'slot
))))
1627 ;;; defining a class hierarchy shouldn't lead to spurious classoid
1628 ;;; errors on TYPEP questions (reported by Tim Moore on #lisp
1630 (defclass backwards-2
(backwards-1) (a b
))
1631 (defclass backwards-3
(backwards-2) ())
1632 (defun typep-backwards-3 (x)
1633 (typep x
'backwards-3
))
1634 (defclass backwards-1
() (a b
))
1635 (assert (not (typep-backwards-3 1)))
1636 (assert (not (typep-backwards-3 (make-instance 'backwards-2
))))
1637 (assert (typep-backwards-3 (make-instance 'backwards-3
)))
1639 (defgeneric remove-method-1
(x)
1640 (:method
((x integer
)) (1+ x
)))
1641 (defgeneric remove-method-2
(x)
1642 (:method
((x integer
)) (1- x
)))
1643 (assert (eq #'remove-method-1
1644 (remove-method #'remove-method-1
1645 (find-method #'remove-method-2
1647 (list (find-class 'integer
))))))
1648 (assert (= (remove-method-1 3) 4))
1649 (assert (= (remove-method-2 3) 2))
1651 ;;; ANSI doesn't require these restarts, but now that we have them we
1652 ;;; better test them too.
1653 (defclass slot-unbound-restart-test
() ((x)))
1654 (let ((test (make-instance 'slot-unbound-restart-test
)))
1655 (assert (not (slot-boundp test
'x
)))
1656 (assert (= 42 (handler-bind ((unbound-slot (lambda (c) (use-value 42 c
))))
1657 (slot-value test
'x
))))
1658 (assert (not (slot-boundp test
'x
)))
1659 (assert (= 13 (handler-bind ((unbound-slot (lambda (c) (store-value 13 c
))))
1660 (slot-value test
'x
))))
1661 (assert (= 13 (slot-value test
'x
))))
1662 ;;; Using class instances as specializers, reported by Pascal Costanza, ref CLHS 7.6.2
1663 (defclass class-as-specializer-test
()
1665 (handler-bind ((implicit-generic-function-warning #'muffle-warning
))
1666 (eval `(defmethod class-as-specializer-test1 ((x ,(find-class 'class-as-specializer-test
)))
1668 (assert (eq 'foo
(class-as-specializer-test1 (make-instance 'class-as-specializer-test
))))
1669 (handler-bind ((implicit-generic-function-warning #'muffle-warning
))
1670 (funcall (compile nil
`(lambda ()
1671 (defmethod class-as-specializer-test2 ((x ,(find-class 'class-as-specializer-test
)))
1673 (assert (eq 'bar
(class-as-specializer-test2 (make-instance 'class-as-specializer-test
))))
1675 ;;; CHANGE-CLASS and tricky allocation.
1676 (defclass foo-to-be-changed
()
1677 ((a :allocation
:class
:initform
1)))
1678 (defclass bar-to-be-changed
(foo-to-be-changed) ())
1679 (defvar *bar-to-be-changed
* (make-instance 'bar-to-be-changed
))
1680 (defclass baz-to-be-changed
()
1681 ((a :allocation
:instance
:initform
2)))
1682 (change-class *bar-to-be-changed
* 'baz-to-be-changed
)
1683 (assert (= (slot-value *bar-to-be-changed
* 'a
) 1))
1685 ;;; proper name and class redefinition
1686 (defvar *to-be-renamed1
* (defclass to-be-renamed1
() ()))
1687 (defvar *to-be-renamed2
* (defclass to-be-renamed2
() ()))
1688 (setf (find-class 'to-be-renamed1
) (find-class 'to-be-renamed2
))
1689 (defvar *renamed1
* (defclass to-be-renamed1
() ()))
1690 (assert (not (eq *to-be-renamed1
* *to-be-renamed2
*)))
1691 (assert (not (eq *to-be-renamed1
* *renamed1
*)))
1692 (assert (not (eq *to-be-renamed2
* *renamed1
*)))
1694 ;;; CLASS-NAME (and various other standardized generic functions) have
1695 ;;; their effective methods precomputed; in the process of rearranging
1696 ;;; (SETF FIND-CLASS) and FINALIZE-INHERITANCE, this broke.
1697 (defclass class-with-odd-class-name-method
()
1698 ((a :accessor class-name
)))
1700 ;;; another case where precomputing (this time on PRINT-OBJECT) and
1701 ;;; lazily-finalized classes caused problems. (report from James Y
1702 ;;; Knight sbcl-devel 20-07-2006)
1704 (defclass base-print-object
() ())
1705 ;;; this has the side-effect of finalizing BASE-PRINT-OBJECT, and
1706 ;;; additionally the second specializer (STREAM) changes the cache
1707 ;;; structure to require two keys, not just one.
1708 ;; warning is "Specializing on the second argument to PRINT-OBJECT ..."
1709 (handler-bind ((warning #'muffle-warning
))
1710 (defmethod print-object ((o base-print-object
) (s stream
))
1712 ;;; unfinalized as yet
1713 (defclass sub-print-object
(base-print-object) ())
1714 ;;; the accessor causes an eager finalization
1715 (defclass subsub-print-object
(sub-print-object)
1718 ;;; triggers a discriminating function (and so cache) recomputation.
1719 ;;; The method on BASE-PRINT-OBJECT will cause the system to attempt
1720 ;;; to fill the cache for all subclasses of BASE-PRINT-OBJECT which
1721 ;;; have valid wrappers; however, in the course of doing so, the
1722 ;;; SUB-PRINT-OBJECT class gets finalized, which invalidates the
1723 ;;; SUBSUB-PRINT-OBJECT wrapper; if an invalid wrapper gets into a
1724 ;;; cache with more than one key, then failure ensues.
1725 (reinitialize-instance #'print-object
)
1727 ;;; bug in long-form method combination: if there's an applicable
1728 ;;; method not part of any method group, we need to call
1729 ;;; INVALID-METHOD-ERROR. (MC27 test case from Bruno Haible)
1730 (define-method-combination mc27
()
1732 (ignored (:ignore
:unused
)))
1734 ,@(mapcar #'(lambda (method) `(call-method ,method
)) normal
)))
1735 (defgeneric test-mc27
(x)
1736 (:method-combination mc27
)
1737 (:method
:ignore
((x number
)) (declare (notinline /)) (/ 0)))
1738 (handler-bind ((style-warning #'muffle-warning
))
1739 (assert-error (test-mc27 7)))
1741 (define-method-combination mc27prime
()
1743 (ignored (:ignore
)))
1744 `(list 'result
,@(mapcar (lambda (m) `(call-method ,m
)) normal
)))
1745 (defgeneric test-mc27prime
(x)
1746 (:method-combination mc27prime
)
1747 (:method
:ignore
((x number
)) (declare (notinline /)) (/ 0)))
1748 (handler-bind ((style-warning #'muffle-warning
))
1749 (assert (equal '(result) (test-mc27prime 3))))
1750 (assert-error (test-mc27 t
)) ; still no-applicable-method
1752 ;;; more invalid wrappers. This time for a long-standing bug in the
1753 ;;; compiler's expansion for TYPEP on various class-like things, with
1754 ;;; user-visible consequences.
1755 (defclass obsolete-again
() ())
1756 (defvar *obsolete-again
* (make-instance 'obsolete-again
))
1757 (defvar *obsolete-again-hash
* (sxhash *obsolete-again
*))
1758 (make-instances-obsolete (find-class 'obsolete-again
))
1759 (assert (not (streamp *obsolete-again
*)))
1760 (make-instances-obsolete (find-class 'obsolete-again
))
1761 (assert (= (sxhash *obsolete-again
*) *obsolete-again-hash
*))
1762 (compile (defun is-a-structure-object-p (x) (typep x
'structure-object
)))
1763 (make-instances-obsolete (find-class 'obsolete-again
))
1764 (assert (not (is-a-structure-object-p *obsolete-again
*)))
1766 ;;; overeager optimization of slot-valuish things
1767 (defclass listoid
()
1768 ((caroid :initarg
:caroid
)
1769 (cdroid :initarg
:cdroid
:initform nil
)))
1770 (handler-bind ((implicit-generic-function-warning #'muffle-warning
))
1771 (defmethod lengthoid ((x listoid
))
1773 (loop until
(null x
)
1774 do
(incf result
) (setq x
(slot-value x
'cdroid
)))
1776 (with-test (:name
((:setq
:method-parameter
) slot-value
))
1777 (assert (= (lengthoid (make-instance 'listoid
)) 1))
1778 (assert (= (lengthoid
1779 (make-instance 'listoid
:cdroid
1780 (make-instance 'listoid
:cdroid
1781 (make-instance 'listoid
))))
1786 ;;;; Tests for argument parsing in fast-method-functions.
1790 (eval-when (:compile-toplevel
:load-toplevel
:execute
)
1791 (setf (symbol-value 'a
) 'invalid
))
1793 (defmacro test1
(lambda-list values args
&key declarations cnm
)
1795 (fmakunbound 'll-method
)
1796 (fmakunbound 'll-function
)
1797 (handler-bind ((implicit-generic-function-warning #'muffle-warning
))
1798 (defmethod ll-method ,lambda-list
1801 `((when nil
(call-next-method))))
1803 (defun ll-function ,lambda-list
1807 (assert (equal (ll-method ,@args
)
1808 (ll-function ,@args
))))))
1810 (defmacro test
(&rest args
)
1812 (test1 ,@args
:cnm nil
)
1813 (test1 ,@args
:cnm t
)))
1815 ;; Just plain arguments
1818 (test (a b c d e f g h i
) (a b c d e f g h i
) (1 2 3 4 5 6 7 8 9))
1820 (test (*foo
*) (*foo
* (symbol-value '*foo
*)) (1))
1822 (test (a) (a (symbol-value 'a
)) (1)
1823 :declarations
((declare (special a
))))
1827 (test (a &optional b c
) (a b c
) (1))
1828 (test (a &optional b c
) (a b c
) (1 2))
1829 (test (a &optional b c
) (a b c
) (1 2 3))
1831 (test (a &optional
(b 'b b-p
) (c 'c c-p
)) (a b c b-p c-p
) (1))
1832 (test (a &optional
(b 'b b-p
) (c 'c c-p
)) (a b c b-p c-p
) (1 2))
1833 (test (a &optional
(b 'b b-p
) (c 'c c-p
)) (a b c b-p c-p
) (1 2 3))
1835 (test (&optional
*foo
*) (*foo
* (symbol-value '*foo
*)) ())
1836 (test (&optional
*foo
*) (*foo
* (symbol-value '*foo
*)) (1))
1838 (test (&optional
(*foo
* 'z foo-p
)) (*foo
* (symbol-value '*foo
*) foo-p
) ())
1839 (test (&optional
(*foo
* 'z foo-p
)) (*foo
* (symbol-value '*foo
*) foo-p
) (1))
1841 (test (&optional a
) (a (symbol-value 'a
)) ()
1842 :declarations
((declare (special a
))))
1843 (test (&optional a
) (a (symbol-value 'a
)) (1)
1844 :declarations
((declare (special a
))))
1846 (test (&optional
(a 'z a-p
)) (a (symbol-value 'a
) a-p
) ()
1847 :declarations
((declare (special a
))))
1848 (test (&optional
(a 'z a-p
)) (a (symbol-value 'a
) a-p
) (1)
1849 :declarations
((declare (special a
))))
1851 (defparameter *count
* 0)
1853 (test (&optional
(a (incf *count
*)) (b (incf *count
*)))
1854 (a b
*count
* (setf *count
* 0))
1857 ;; Keywords with some &RESTs thrown in
1864 (1 :c
3 :c
1 :b
2 :b
4)))
1865 (eval `(test (a &key b c
) (a b c
) ,args
))
1866 (eval `(test (a &key
(b 'b b-p
) (c 'c c-p
))
1869 (eval `(test (a &rest rest
&key
(b 'b b-p
) (c 'c c-p
))
1870 (a b c b-p c-p rest
)
1872 (eval `(test (a &rest
*foo
* &key
(b 'b b-p
) (c 'c c-p
))
1873 (a b c b-p c-p
*foo
* (symbol-value '*foo
*))
1875 (eval `(test (a &rest
*foo
* &key
(b 'b b-p
) (c 'c c-p
))
1876 (a b c b-p c-p
*foo
* (symbol-value '*foo
*))
1878 :declarations
((declare (special b-p
))))))
1882 (:*foo
* 1 :*foo
* 2)))
1883 (eval `(test (&key
*foo
*) (*foo
* (symbol-value '*foo
*)) ,args
))
1884 (eval `(test (&key
(*foo
* 'z foo-p
)) (*foo
* (symbol-value '*foo
*) foo-p
)
1886 (eval `(test (&key
((:*foo
* a
) 'z foo-p
)) (a (symbol-value 'a
) foo-p
)
1888 (eval `(test (&key
((:*foo
* a
) 'z foo-p
)) (a (symbol-value 'a
) foo-p
)
1890 :declarations
((declare (special a
))))))
1892 (defparameter *count
* 0)
1894 (test (&key
(a (incf *count
*)) (b (incf *count
*)))
1895 (a b
*count
* (setf *count
* 0))
1898 (test (&key a b
&allow-other-keys
) (a b
) (:a
1 :b
2 :c
3))
1900 (handler-bind ((implicit-generic-function-warning #'muffle-warning
))
1901 (defmethod clim-style-lambda-list-test (a b
&optional c d
&key x y
)
1902 (list a b c d x y
)))
1904 (clim-style-lambda-list-test 1 2)
1908 (test (&aux
(a (incf *count
*)) (b (incf *count
*)))
1909 (a b
*count
* (setf *count
* 0))
1912 ;;;; long-form method combination with &rest in :arguments
1913 ;;;; (this had a bug what with fixed in 1.0.4.something)
1914 (define-method-combination long-form-with-
&rest
()
1916 (:arguments x
&rest others
)
1918 ,@(mapcar (lambda (method)
1919 `(call-method ,method
))
1921 (list ,x
(length ,others
))))
1923 (defgeneric test-long-form-with-
&rest
(x &rest others
)
1924 (:method-combination long-form-with-
&rest
))
1926 (defmethod test-long-form-with-&rest
(x &rest others
)
1930 (with-test (:name
(define-method-combination :long-form-with-
&rest
))
1931 (assert (equal '(:foo
13)
1932 (apply #'test-long-form-with-
&rest
:foo
(make-list 13)))))
1934 ;;;; slot-missing for non-standard classes on SLOT-VALUE
1936 ;;;; FIXME: This is arguably not right, actually: CLHS seems to say
1937 ;;;; we should just signal an error at least for built-in classes, but
1938 ;;;; for a while we were hitting NO-APPLICABLE-METHOD, which is definitely
1939 ;;;; wrong -- so test this for now at least.
1941 (defvar *magic-symbol
* (gensym "MAGIC"))
1943 (set *magic-symbol
* 42)
1945 (defmethod slot-missing (class instance
(slot-name (eql *magic-symbol
*)) op
1948 (setf (symbol-value *magic-symbol
*) new
)
1949 (symbol-value *magic-symbol
*)))
1951 (with-test (:name
(slot-missing :non-standard-classes
))
1952 (assert (eql 42 (slot-value (cons t t
) *magic-symbol
*)))
1953 (assert (eql 13 (setf (slot-value 123 *magic-symbol
*) 13)))
1954 (assert (eql 13 (slot-value 'foobar
*magic-symbol
*))))
1956 ;;;; Built-in structure and condition layouts should have NIL in
1957 ;;;; LAYOUT-FOR-STD-CLASS-P, and classes should have T.
1959 (with-test (:name
(sb-pcl::layout-for-std-class-p
:builtin
))
1960 (assert (not (sb-pcl::layout-for-std-class-p
(sb-pcl::find-layout
'warning
))))
1961 (assert (not (sb-pcl::layout-for-std-class-p
(sb-pcl::find-layout
'hash-table
))))
1962 (assert (eq t
(sb-pcl::layout-for-std-class-p
(sb-pcl::find-layout
'standard-object
)))))
1964 ;;;; bug 402: PCL used to warn about non-standard declarations
1965 (declaim (declaration bug-402-d
))
1966 (defgeneric bug-402-gf
(x))
1967 (with-test (:name
(defmethod :non-standard-declaration
:bug-402
))
1968 (handler-bind ((warning #'error
))
1969 (eval '(defmethod bug-402-gf (x)
1970 (declare (bug-402-d x
))
1973 ;;;; non-keyword :default-initargs + :before method on shared initialize
1974 ;;;; interacted badly with CTOR optimizations
1975 (defclass ctor-default-initarg-problem
()
1976 ((slot :initarg slotto
))
1977 (:default-initargs slotto
123))
1978 (defmethod shared-initialize :before
((instance ctor-default-initarg-problem
) slot-names
&rest initargs
)
1979 (format nil
"~&Rock on: ~A~%" initargs
))
1980 (defun provoke-ctor-default-initarg-problem ()
1981 (make-instance 'ctor-default-initarg-problem
))
1982 (with-test (:name
(make-instance :non-keyword-default-initargs
1983 shared-initialize
:before
))
1984 (handler-bind ((warning #'error
))
1985 (assert (= 123 (slot-value (provoke-ctor-default-initarg-problem) 'slot
)))))
1987 ;;;; discriminating net on streams used to generate code deletion notes on
1989 (defgeneric stream-fd
(stream direction
))
1990 (defmethod stream-fd ((stream sb-sys
:fd-stream
) direction
)
1991 (declare (ignore direction
))
1992 (sb-sys:fd-stream-fd stream
))
1993 (defmethod stream-fd ((stream synonym-stream
) direction
)
1994 (stream-fd (symbol-value (synonym-stream-symbol stream
)) direction
))
1995 (defmethod stream-fd ((stream two-way-stream
) direction
)
1999 (two-way-stream-input-stream stream
) direction
))
2002 (two-way-stream-output-stream stream
) direction
))))
2003 (with-test (:name
(:discriminating-name
:code-deletion-note
))
2004 (handler-bind ((compiler-note #'error
))
2005 (stream-fd sb-sys
:*stdin
* :output
)
2006 (stream-fd sb-sys
:*stdin
* :output
)))
2008 (with-test (:name
:bug-380
)
2009 (defclass bug-380
()
2010 ((slot :accessor bug380-slot
)))
2011 (fmakunbound 'foo-slot
)
2012 (defgeneric foo-slot
(x y z
))
2014 ((slot :accessor foo-slot-value
))))
2016 ;;; SET and (SETF SYMBOL-VALUE) used to confuse permuation vector
2019 ((x :initform
:fih
)))
2021 ((x :initform
:fah
)))
2022 (declaim (special *fih
*))
2023 (handler-bind ((implicit-generic-function-warning #'muffle-warning
))
2024 (defmethod fihfah ((*fih
* fih
))
2025 (set '*fih
* (make-instance 'fah
))
2026 (list (slot-value *fih
* 'x
)
2027 (eval '(slot-value *fih
* 'x
)))))
2028 (defmethod fihfah ((fah fah
))
2029 (declare (special fah
))
2030 (set 'fah
(make-instance 'fih
))
2031 (list (slot-value fah
'x
)
2032 (eval '(slot-value fah
'x
))))
2033 (with-test (:name
:set-of-a-method-specializer
)
2034 (assert (equal '(:fah
:fah
) (fihfah (make-instance 'fih
))))
2035 (assert (equal '(:fih
:fih
) (fihfah (make-instance 'fah
)))))
2037 (handler-bind ((implicit-generic-function-warning #'muffle-warning
))
2038 (defmethod no-implicit-declarations-for-local-specials ((faax double-float
))
2039 (declare (special faax
))
2040 (set 'faax
(when (< faax
0) (- faax
)))
2042 (with-test (:name
:no-implicit-declarations-for-local-specials
)
2043 (assert (not (no-implicit-declarations-for-local-specials 1.0d0
))))
2045 (defstruct bug-357-a
2048 (slot3 (coerce pi
'single-float
) :type single-float
))
2049 (defclass bug-357-b
(bug-357-a)
2050 ((slot2 :initform
't2
)
2051 (slot4 :initform -
44)
2054 (slot7 :initform
(floor (* pi pi
)))
2055 (slot8 :initform
88))
2056 (:metaclass structure-class
))
2057 (defstruct (bug-357-c (:include bug-357-b
(slot8 -
88) (slot5 :ok
)))
2060 (slot11 (floor (exp 3))))
2061 (with-test (:name
:bug-357
)
2063 (list (bug-357-c-slot1 x
)
2072 (bug-357-c-slot10 x
)
2073 (bug-357-c-slot11 x
))))
2074 (let ((base (slots (make-bug-357-c))))
2075 (assert (equal base
(slots (make-instance 'bug-357-c
))))
2076 (assert (equal base
'(nil t2
3.1415927 -
44 :ok t
9 -
88 nil t
20))))))
2078 (defclass class-slot-shared-initialize
()
2079 ((a :allocation
:class
:initform
:ok
)))
2080 (with-test (:name
:class-slot-shared-initialize
)
2081 (let ((x (make-instance 'class-slot-shared-initialize
)))
2082 (assert (eq :ok
(slot-value x
'a
)))
2083 (slot-makunbound x
'a
)
2084 (assert (not (slot-boundp x
'a
)))
2085 (shared-initialize x
'(a))
2086 (assert (slot-boundp x
'a
))
2087 (assert (eq :ok
(slot-value x
'a
)))))
2089 (declaim (ftype (function (t t t
) (values single-float
&optional
))
2090 i-dont-want-to-be-clobbered-1
2091 i-dont-want-to-be-clobbered-2
))
2092 (handler-bind ((warning #'muffle-warning
))
2093 (defgeneric i-dont-want-to-be-clobbered-1
(x y z
))
2094 (defmethod i-dont-want-to-be-clobbered-2 ((x cons
) y z
)
2096 (defun i-cause-an-gf-info-update ()
2097 (i-dont-want-to-be-clobbered-2 t t t
))
2098 (with-test (:name
(defgeneric :should-clobber-ftype
))
2099 ;; (because it doesn't check the argument or result types)
2100 (assert (equal '(function (t t t
) *)
2101 (sb-kernel:type-specifier
2102 (sb-int:info
:function
2103 :type
'i-dont-want-to-be-clobbered-1
))))
2104 (assert (equal '(function (t t t
) *)
2105 (sb-kernel:type-specifier
2106 (sb-int:info
:function
2107 :type
'i-dont-want-to-be-clobbered-2
))))
2108 (assert (eq :defined-method
2109 (sb-int:info
:function
2110 :where-from
'i-dont-want-to-be-clobbered-1
)))
2111 (assert (eq :defined-method
2112 (sb-int:info
:function
2113 :where-from
'i-dont-want-to-be-clobbered-2
))))
2115 (with-test (:name
:bogus-parameter-specializer-name-error
)
2118 (let ((*error-output
* (make-broadcast-stream)))
2119 (eval `(defmethod #:fii
((x "a string")) 'string
)))
2120 (sb-int:reference-condition
(c)
2121 (when (member '(:ansi-cl
:macro defmethod
)
2122 (sb-int:reference-condition-references c
)
2126 (defclass remove-default-initargs-test
()
2127 ((x :initarg
:x
:initform
42)))
2128 (defclass remove-default-initatgs-test
()
2129 ((x :initarg
:x
:initform
42))
2130 (:default-initargs
:x
0))
2131 (defclass remove-default-initargs-test
()
2132 ((x :initarg
:x
:initform
42)))
2133 (with-test (:name
:remove-default-initargs
)
2134 (assert (= 42 (slot-value (make-instance 'remove-default-initargs-test
)
2137 ;; putting this inside WITH-TEST interferes with recognition of the class name
2139 (defclass bug-485019
()
2140 ((array :initarg
:array
)))
2141 (with-test (:name
:bug-485019
)
2142 ;; there was a bug in WALK-SETQ, used in method body walking, in the
2143 ;; presence of declarations on symbol macros.
2144 (handler-bind ((implicit-generic-function-warning #'muffle-warning
))
2145 (defmethod bug-485019 ((bug-485019 bug-485019
))
2146 (with-slots (array) bug-485019
2147 (declare (type (or null simple-array
) array
))
2148 (setf array
(make-array 4)))
2150 (funcall 'bug-485019
(make-instance 'bug-485019
)))
2152 ;;; The compiler didn't propagate the declarared type before applying
2153 ;;; the transform for (SETF SLOT-VALUE), so the generic accessor was used.
2154 (defstruct foo-520366
2156 (defun quux-520366 (cont)
2158 (defun bar-520366 (foo-struct)
2159 (declare (type foo-520366 foo-struct
))
2160 (with-slots (slot) foo-struct
2162 (quux-520366 #'(lambda ()
2166 (with-test (:name
:bug-520366
)
2167 (let ((callees (find-named-callees #'bar-520366
)))
2168 (assert (equal (list #'quux-520366
) callees
))))
2170 (defgeneric no-applicable-method
/retry
(x))
2171 (defmethod no-applicable-method/retry
((x string
))
2173 (with-test (:name
:no-applicable-method
/retry
)
2174 (assert (equal "cons"
2175 (handler-bind ((error
2177 (declare (ignore c
))
2178 (let ((r (find-restart 'sb-pcl
::retry
)))
2180 (eval `(defmethod no-applicable-method/retry
((x cons
))
2182 (invoke-restart r
))))))
2183 (no-applicable-method/retry
(cons t t
))))))
2185 (defgeneric no-primary-method
/retry
(x))
2186 (defmethod no-primary-method/retry
:before
(x) (assert x
))
2187 (with-test (:name
:no-primary-method
/retry
)
2188 (assert (equal "ok!"
2189 (handler-bind ((error
2191 (declare (ignore c
))
2192 (let ((r (find-restart 'sb-pcl
::retry
)))
2194 (eval `(defmethod no-primary-method/retry
(x)
2196 (invoke-restart r
))))))
2197 (no-primary-method/retry
(cons t t
))))))
2199 ;;; test that a cacheing strategy for make-instance initargs checking
2200 ;;; can handle class redefinitions
2202 (defun cacheing-initargs-redefinitions-make-instances
2203 (&optional
(initarg :slot
))
2204 (declare (notinline make-instance
))
2205 (make-instance 'cacheing-initargs-redefinitions-check
)
2206 (make-instance 'cacheing-initargs-redefinitions-check initarg
3))
2208 (defclass cacheing-initargs-redefinitions-check
()
2209 ((slot :initarg
:slot
)))
2211 (with-test (:name
(make-instance :initargs-checking-before-redefinition
))
2212 (make-instance 'cacheing-initargs-redefinitions-check
)
2213 (make-instance 'cacheing-initargs-redefinitions-check
:slot
3)
2214 (cacheing-initargs-redefinitions-make-instances :slot
)
2215 (assert-error (cacheing-initargs-redefinitions-make-instances :slot2
)))
2217 (defclass cacheing-initargs-redefinitions-check
()
2218 ((slot :initarg
:slot2
)))
2220 (with-test (:name
(make-instance :initargs-checking-after-redefinition
))
2221 (make-instance 'cacheing-initargs-redefinitions-check
)
2222 (make-instance 'cacheing-initargs-redefinitions-check
:slot2
3)
2223 (cacheing-initargs-redefinitions-make-instances :slot2
)
2224 (assert-error (cacheing-initargs-redefinitions-make-instances :slot
)))
2226 (defmethod initialize-instance :after
2227 ((class cacheing-initargs-redefinitions-check
) &key slot
)
2228 (declare (ignore slot
))
2231 (with-test (:name
(make-instance :initargs-checking-new-method-initargs
))
2232 (make-instance 'cacheing-initargs-redefinitions-check
)
2233 (make-instance 'cacheing-initargs-redefinitions-check
:slot2
3)
2234 (cacheing-initargs-redefinitions-make-instances :slot2
)
2235 (let ((thing (cacheing-initargs-redefinitions-make-instances :slot
)))
2236 (assert (not (slot-boundp thing
'slot
)))))
2241 (with-test (:name
(defmethod :specializer-builtin-class-alias
:bug-618387
))
2242 (let ((alias (gensym)))
2243 (setf (find-class alias
) (find-class 'symbol
))
2244 (handler-bind ((implicit-generic-function-warning #'muffle-warning
))
2245 (eval `(defmethod lp-618387 ((s ,alias
))
2247 (assert (equal "FOO" (funcall 'lp-618387
:foo
)))))
2249 (with-test (:name
(defmethod :pcl-spurious-ignore-warnings
))
2250 (defgeneric no-spurious-ignore-warnings
(req &key key
))
2251 (handler-bind ((warning (lambda (x) (error "~A" x
))))
2253 '(defmethod no-spurious-ignore-warnings ((req number
) &key key
)
2254 (declare (ignore key
))
2255 (check-type req integer
))))
2256 (defgeneric should-get-an-ignore-warning
(req &key key
))
2258 (handler-bind ((warning (lambda (c) (setq warnings
1) (muffle-warning c
))))
2259 (eval '(defmethod should-get-an-ignore-warning ((req integer
) &key key
)
2260 (check-type req integer
))))
2261 (assert (= warnings
1))))
2263 (handler-bind ((style-warning #'muffle-warning
))
2264 (eval '(defgeneric generic-function-pretty-arglist-optional-and-key
(req &optional opt
&key key
)
2265 (:method
(req &optional opt
&key key
)
2266 (list req opt key
)))))
2267 (with-test (:name
:generic-function-pretty-arglist-optional-and-key
)
2268 (handler-bind ((warning #'error
))
2269 ;; Used to signal a style-warning
2270 (assert (equal '(req &optional opt
&key key
)
2271 (sb-pcl::generic-function-pretty-arglist
2272 #'generic-function-pretty-arglist-optional-and-key
)))))
2274 (with-test (:name
:bug-894202
)
2277 (let ((name (gensym "FOO"))
2278 (decl (gensym "BAR")))
2279 (eval `(defgeneric ,name
()
2280 (declare (,decl
)))))
2284 (with-test (:name
:bug-898331
)
2285 (handler-bind ((warning #'error
))
2286 (eval `(defgeneric bug-898331
(request type remaining-segment-requests all-requests
)))
2287 (eval `(defmethod bug-898331 ((request cons
) (type (eql :cancel
))
2288 remaining-segment-requests
2289 all-segment-requests
)
2290 (declare (ignore all-segment-requests
))
2291 (check-type request t
)))))
2293 (with-test (:name
(defmethod :bug-1001799
))
2294 ;; compilation of the defmethod used to cause infinite recursion
2295 (let ((pax (gensym "PAX"))
2296 (pnr (gensym "PNR"))
2297 (sup (gensym "SUP"))
2298 (frob (gensym "FROB"))
2299 (sb-ext:*evaluator-mode
* :compile
))
2302 (declaim (optimize (speed 1) (space 1) (safety 3) (debug 3) (compilation-speed 1)))
2303 (defclass ,pax
(,sup
)
2304 ((,pnr
:type
(or null
,pnr
))))
2305 (defclass ,pnr
(,sup
)
2306 ((,pax
:type
(or null
,pax
))))
2309 (handler-bind ((implicit-generic-function-warning #'muffle-warning
))
2310 (defmethod ,frob
((pnr ,pnr
))
2311 (slot-value pnr
',pax
)))
2312 (declaim (optimize (safety 1) (debug 1)))))))
2314 (defclass bug-1099708
() ((slot-1099708 :initarg
:slot-1099708
)))
2315 (defun make-1099708-1 ()
2316 (make-instance 'bug-1099708
:slot-1099708
'#1= (1 2 .
#1#)))
2317 (defun make-1099708-2 ()
2318 (make-instance 'bug-1099708
:slot-1099708
'#2= (1 2 .
#2#)))
2319 (with-test (:name
:bug-1099708
)
2320 ;; caused infinite equal testing in function name lookup
2321 (assert (not (eql (slot-value (make-1099708-1) 'slot-1099708
)
2322 (slot-value (make-1099708-2) 'slot-1099708
)))))
2324 (defclass bug-1099708b-list
()
2325 ((slot-1099708b-list :initarg
:slot-1099708b-list
)))
2326 (defun make-1099708b-list-1 ()
2327 (make-instance 'bug-1099708b-list
:slot-1099708b-list
'(some value
)))
2328 (defun make-1099708b-list-2 ()
2329 (make-instance 'bug-1099708b-list
:slot-1099708b-list
'(some value
)))
2330 (with-test (:name
:bug-1099708b-list
)
2331 (assert (eql (slot-value (make-1099708b-list-1) 'slot-1099708b-list
)
2332 (slot-value (make-1099708b-list-1) 'slot-1099708b-list
)))
2333 (assert (eql (slot-value (make-1099708b-list-2) 'slot-1099708b-list
)
2334 (slot-value (make-1099708b-list-2) 'slot-1099708b-list
)))
2335 (assert (not (eql (slot-value (make-1099708b-list-1) 'slot-1099708b-list
)
2336 (slot-value (make-1099708b-list-2) 'slot-1099708b-list
)))))
2338 (defclass bug-1099708b-string
()
2339 ((slot-1099708b-string :initarg
:slot-1099708b-string
)))
2340 (defun make-1099708b-string-1 ()
2341 (make-instance 'bug-1099708b-string
:slot-1099708b-string
"string"))
2342 (defun make-1099708b-string-2 ()
2343 (make-instance 'bug-1099708b-string
:slot-1099708b-string
"string"))
2344 (with-test (:name
:bug-1099708b-string
)
2345 (assert (eql (slot-value (make-1099708b-string-1) 'slot-1099708b-string
)
2346 (slot-value (make-1099708b-string-1) 'slot-1099708b-string
)))
2347 (assert (eql (slot-value (make-1099708b-string-2) 'slot-1099708b-string
)
2348 (slot-value (make-1099708b-string-2) 'slot-1099708b-string
)))
2349 (assert (not (eql (slot-value (make-1099708b-string-1) 'slot-1099708b-string
)
2350 (slot-value (make-1099708b-string-2) 'slot-1099708b-string
)))))
2352 (defclass bug-1099708b-bitvector
()
2353 ((slot-1099708b-bitvector :initarg
:slot-1099708b-bitvector
)))
2354 (defun make-1099708b-bitvector-1 ()
2355 (make-instance 'bug-1099708b-bitvector
:slot-1099708b-bitvector
#*1011))
2356 (defun make-1099708b-bitvector-2 ()
2357 (make-instance 'bug-1099708b-bitvector
:slot-1099708b-bitvector
#*1011))
2358 (with-test (:name
:bug-1099708b-bitvector
)
2359 (assert (eql (slot-value (make-1099708b-bitvector-1) 'slot-1099708b-bitvector
)
2360 (slot-value (make-1099708b-bitvector-1) 'slot-1099708b-bitvector
)))
2361 (assert (eql (slot-value (make-1099708b-bitvector-2) 'slot-1099708b-bitvector
)
2362 (slot-value (make-1099708b-bitvector-2) 'slot-1099708b-bitvector
)))
2363 (assert (not (eql (slot-value (make-1099708b-bitvector-1) 'slot-1099708b-bitvector
)
2364 (slot-value (make-1099708b-bitvector-2) 'slot-1099708b-bitvector
)))))
2366 (defclass bug-1099708b-pathname
()
2367 ((slot-1099708b-pathname :initarg
:slot-1099708b-pathname
)))
2368 #+nil
; after change 58602640ed, I don't see how to make this assert something useful
2369 (with-test (:name
:bug-1099708b-pathname
)
2370 (defun make-1099708b-pathname-1 ()
2371 (make-instance 'bug-1099708b-pathname
:slot-1099708b-pathname
#p
"pn"))
2372 (defun make-1099708b-pathname-2 ()
2373 (make-instance 'bug-1099708b-pathname
:slot-1099708b-pathname
#p
"pn"))
2374 (assert (eql (slot-value (make-1099708b-pathname-1) 'slot-1099708b-pathname
)
2375 (slot-value (make-1099708b-pathname-1) 'slot-1099708b-pathname
)))
2376 (assert (eql (slot-value (make-1099708b-pathname-2) 'slot-1099708b-pathname
)
2377 (slot-value (make-1099708b-pathname-2) 'slot-1099708b-pathname
)))
2378 (assert (not (eql (slot-value (make-1099708b-pathname-1) 'slot-1099708b-pathname
)
2379 (slot-value (make-1099708b-pathname-2) 'slot-1099708b-pathname
)))))
2381 (defclass bug-1099708c-list
()
2382 ((slot-1099708c-list :initarg
:slot-1099708c-list
)))
2384 (defun make-1099708c-list-1 ()
2385 (make-instance 'bug-1099708c-list
:slot-1099708c-list
#1='(some value
)))
2386 (defun make-1099708c-list-2 ()
2387 (make-instance 'bug-1099708c-list
:slot-1099708c-list
#1#)))
2388 (with-test (:name
:bug-1099708c-list
)
2389 (assert (eql (slot-value (make-1099708c-list-1) 'slot-1099708c-list
)
2390 (slot-value (make-1099708c-list-1) 'slot-1099708c-list
)))
2391 (assert (eql (slot-value (make-1099708c-list-2) 'slot-1099708c-list
)
2392 (slot-value (make-1099708c-list-2) 'slot-1099708c-list
)))
2393 (assert (eql (slot-value (make-1099708c-list-1) 'slot-1099708c-list
)
2394 (slot-value (make-1099708c-list-2) 'slot-1099708c-list
))))
2398 ;;; Define a class and force the "fallback" constructor generator to be
2399 ;;; used by having a HAIRY-AROUND-OR-NONSTANDARD-PRIMARY-METHOD-P on
2400 ;;; SHARED-INITIALIZE.
2401 (defclass bug-1179858
()
2402 ((foo :initarg
:foo
:reader bug-1179858-foo
))
2403 (:default-initargs
:foo
(error "Should not be evaluated")))
2404 (defmethod shared-initialize :around
((instance bug-1179858
) (slot-names t
) &key
)
2407 (with-test (:name
(make-instance :fallback-generator-initarg-handling
:bug-1179858
))
2408 ;; Now compile a lambda containing MAKE-INSTANCE to exercise the
2409 ;; fallback constructor generator. Call the resulting compiled
2410 ;; function to trigger the bug.
2411 (funcall (compile nil
'(lambda () (make-instance 'bug-1179858
:foo t
)))))
2413 ;;; Other brokenness, found while investigating: fallback-generator
2414 ;;; handling of non-keyword initialization arguments
2415 (defclass bug-1179858b
()
2416 ((foo :initarg foo
:reader bug-1179858b-foo
))
2417 (:default-initargs foo
14))
2418 (defmethod shared-initialize :around
((instance bug-1179858b
) (slot-names t
) &key
)
2421 (with-test (:name
(make-instance :fallback-generator-non-keyword-initarg
:bug-1179858
))
2422 (flet ((foo= (n i
) (= (bug-1179858b-foo i
) n
)))
2424 (foo= 14 (funcall (compile nil
'(lambda () (make-instance 'bug-1179858b
))))))
2426 (foo= 15 (funcall (compile nil
'(lambda () (make-instance 'bug-1179858b
'foo
15))))))))
2428 (with-test (:name
(:cpl-violation-setup
:bug-309076
))
2431 (defclass bug-309076-broken-class
(standard-class) ()
2432 (:metaclass sb-mop
:funcallable-standard-class
))
2433 (sb-mop:finalize-inheritance
(find-class 'bug-309076-broken-class
)))))
2435 (defclass bug-309076-class
(standard-class) ())
2436 (with-test (:name
(:cpl-violation-irrelevant-class
:bug-309076
))
2437 (defmethod sb-mop:validate-superclass
((x bug-309076-class
) (y standard-class
)) t
)
2438 (assert (typep (make-instance 'bug-309076-class
) 'bug-309076-class
)))
2440 (eval-when (:compile-toplevel
:load-toplevel
:execute
)
2442 (handler-bind ((implicit-generic-function-warning #'muffle-warning
))
2447 (declare (special a
))
2452 (sb-mop:make-method-lambda
2454 (find-method #'b
() ())
2455 '(lambda () (declare (special a
)) a
)
2460 (with-test (:name
:make-method-lambda-leakage
)
2461 ;; lambda list of X leaks into the invocation of make-method-lambda
2462 ;; during code-walking performed by make-method-lambda invoked by
2464 (sb-cltl2:macroexpand-all
'(defmethod x (a) (macro))))
2466 (with-test (:name
(defmethod :undefined-function
:bug-503095
))
2467 (flet ((test-load (file)
2468 (let (implicit-gf-warning)
2470 ((sb-ext:implicit-generic-function-warning
2472 (setf implicit-gf-warning x
)
2473 (muffle-warning x
)))
2474 ((or warning error
) #'error
))
2476 (assert implicit-gf-warning
))))
2477 (multiple-value-bind (fasl warnings errorsp
)
2478 (compile-file "bug-503095.lisp" :print nil
:verbose nil
)
2480 (progn (assert (and fasl
(not warnings
) (not errorsp
)))
2482 (and fasl
(delete-file fasl
))))
2483 (test-load "bug-503095-2.lisp")))
2485 (defclass a-633911
()
2486 ((x-633911 :initform nil
2487 :accessor x-633911
)))
2488 (defclass b-633911
()
2489 ((x-633911 :initform nil
2490 :accessor x-633911
)))
2491 (with-test (:name
:accessor-and-plain-method
)
2492 (defmethod x-633911 ((b a-633911
)) 10)
2493 (assert (= (x-633911 (make-instance 'a-633911
)) 10)))
2495 (with-test (:name
(built-in-class :subclass error
))
2497 (when (and (class-name c
) (typep c
'built-in-class
))
2498 (assert-error (eval `(defclass ,(gensym) (,(class-name c
))
2500 (sb-pcl::map-all-classes
#'mapper
)))
2502 (defclass slot-value-using-class-a
() ())
2503 (defclass slot-value-using-class-b
() (x))
2505 (with-test (:name
:svuc-with-bad-slotd
)
2506 (let* ((a (make-instance 'slot-value-using-class-a
))
2507 (b (make-instance 'slot-value-using-class-b
))
2508 (slotd (car (sb-mop:class-slots
(class-of b
)))))
2509 (assert-error (sb-mop:slot-value-using-class
(class-of a
) a slotd
))
2510 (assert-error (setf (sb-mop:slot-value-using-class
(class-of a
) a slotd
) t
))))
2512 ;; A test that the *FGENS* cache works.
2513 ;; This is not a test in the CLOS problem domain, but in the implementation
2514 ;; of discriminating functions.
2515 (defgeneric f
(x y
) (:method
(x y
) (+ x y
)))
2516 (defgeneric g
(x y
) (:method
(x y
) (* x y
)))
2517 (with-test (:name
:fgens-sharing
)
2518 (let ((count0 (hash-table-count sb-pcl
::*fgens
*))
2520 (count1 (hash-table-count sb-pcl
::*fgens
*))
2522 (count2 (hash-table-count sb-pcl
::*fgens
*)))
2523 (declare (ignore foo bar
))
2524 (assert (= count0 count1 count2
))))
2527 ;;; Classes shouldn't be their own direct or indirect superclasses or
2530 (with-test (:name
(sb-mop:ensure-class
:class-is-direct-superclass
2533 (defclass class-with-self-as-superclass
(class-with-self-as-superclass) ())))
2535 (with-test (:name
(sb-mop:ensure-class
:superclass-cycle
:bug-1418883
))
2536 ;; These have a superclass cycle from the beginning.
2537 (defclass class-with-superclass-cycle1
(class-with-superclass-cycle2) ())
2539 (defclass class-with-superclass-cycle2
(class-with-superclass-cycle1) ())))
2541 (with-test (:name
(sb-mop:ensure-class
:self-metaclass
))
2542 ;; These have a metaclass cycle from the beginning.
2544 (defclass class-with-self-as-metaclass
() ()
2545 (:metaclass class-with-self-as-metaclass
))))
2547 (with-test (:name
(sb-pcl::update-class
:class-becomes-direct-superclass
2549 (defclass class-with-eventual-self-as-superclass
() ())
2550 ;; Update class to introduce superclass.
2552 (defclass class-with-eventual-self-as-superclass
2553 (class-with-eventual-self-as-superclass) ())))
2555 (with-test (:name
(sb-pcl::update-class
:superclasses-become-cyclic
2557 ;; Nothing wrong with these.
2558 (defclass class-with-eventual-superclass-cycle1
() ())
2559 (defclass class-with-eventual-superclass-cycle2
2560 (class-with-eventual-superclass-cycle1) ())
2561 ;; Update first class to introduce the superclass cycle.
2563 (defclass class-with-eventual-superclass-cycle1
2564 (class-with-eventual-superclass-cycle2) ())))
2566 (with-test (:name
(sb-pcl::update-class
:becomes-self-metaclass
))
2567 (defclass class-with-eventual-self-as-metaclass
() ())
2568 ;; Try to update metaclass to self.
2570 (defclass class-with-eventual-self-as-metaclass
() ()
2571 (:metaclass class-with-eventual-self-as-metaclass
))))
2573 (with-test (:name
:function-keywords
)
2574 (defgeneric function-keywords-test
(&key
))
2575 (assert (equal (function-keywords
2576 (defmethod function-keywords-test (&key a b
)
2577 (declare (ignore a b
))))
2580 (with-test (:name
:superclass-finalization
)
2581 (let* ((class1 (gensym "CLASS1-"))
2582 (class2 (gensym "CLASS2-")))
2583 (eval `(defclass ,class1
() ()))
2584 (eval `(defclass ,class2
(,class1
) ()))
2585 (let ((instance (make-instance class2
)))
2586 (sb-mop:finalize-inheritance
(find-class class1
))
2587 (assert (not (sb-kernel:layout-invalid
(sb-kernel:layout-of instance
)))))))
2589 (with-test (:name
(allocate-instance :on symbol
))
2590 (let ((class (gensym "CLASS-")))
2591 (eval `(defclass ,class
() ()))
2593 (funcall (checked-compile `(lambda ()
2594 (allocate-instance ',class
)))))))
2596 (defclass unbound-slot-after-allocation
=class
()
2597 ((abc :allocation
:class
)
2598 (d :accessor unbound-slot-after-allocation
=class
)))
2600 (with-test (:name
:unbound-slot-after-allocation
=class
)
2601 (assert-error (unbound-slot-after-allocation=class
2602 (make-instance 'unbound-slot-after-allocation
=class
))
2605 (with-test (:name
:layouf-of-nil
)
2606 (assert (eq (sb-kernel:layout-of nil
) (sb-kernel:find-layout
'null
))))
2608 (with-test (:name
(defmethod :on-classless-type
))
2609 (handler-bind ((timeout (lambda (condition)
2610 (declare (ignore condition
))
2611 (error "Timeout"))))
2612 (sb-ext:with-timeout
0.1
2613 (assert-error (funcall (checked-compile `(lambda ()
2614 (defmethod foo ((bar keyword
))))
2616 sb-pcl
:class-not-found-error
))))