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 (load "assertoid.lisp")
16 (defpackage "CLOS-IMPURE"
17 (:use "CL" "ASSERTOID"))
18 (in-package "CLOS-IMPURE")
20 ;;; It should be possible to do DEFGENERIC and DEFMETHOD referring to
21 ;;; structure types defined earlier in the file.
22 (defstruct struct-a x y)
23 (defstruct struct-b x y z)
24 (defmethod wiggle ((a struct-a))
27 (defgeneric jiggle (arg))
28 (defmethod jiggle ((a struct-a))
31 (defmethod jiggle ((b struct-b))
35 (assert (= (wiggle (make-struct-a :x 6 :y 5))
36 (jiggle (make-struct-b :x 19 :y 6 :z 2))))
38 ;;; Compiling DEFGENERIC should prevent "undefined function" style
39 ;;; warnings from code within the same file.
40 (defgeneric gf-defined-in-this-file (x y))
41 (defun function-using-gf-defined-in-this-file (x y n)
43 (gf-defined-in-this-file x y)))
45 ;;; Until Martin Atzmueller ported Pierre Mai's CMU CL fixes in
46 ;;; sbcl-0.6.12.25, the implementation of NO-APPLICABLE-METHOD was
47 ;;; broken in such a way that the code here would signal an error.
48 (defgeneric zut-n-a-m (a b c))
49 (defmethod no-applicable-method ((zut-n-a-m (eql #'zut-n-a-m)) &rest args)
50 (format t "~&No applicable method for ZUT-N-A-M ~S, yet.~%" args))
53 ;;; bug reported and fixed by Alexey Dejneka sbcl-devel 2001-09-10:
54 ;;; This DEFGENERIC shouldn't cause an error.
55 (defgeneric ad-gf (a) (:method :around (x) x))
57 ;;; DEFGENERIC and DEFMETHOD shouldn't accept &REST when it's not
58 ;;; followed by a variable:
59 ;;; e.g. (DEFMETHOD FOO ((X T) &REST) NIL) should signal an error.
60 (eval-when (:load-toplevel :compile-toplevel :execute)
61 (defmacro expect-error (&body body)
62 `(multiple-value-bind (res condition)
63 (ignore-errors (progn ,@body))
64 (declare (ignore res))
65 (typep condition 'error))))
68 '(defmethod foo0 ((x t) &rest) nil))))
69 (assert (expect-error (defgeneric foo1 (x &rest))))
70 (assert (expect-error (defgeneric foo2 (x a &rest))))
71 (defgeneric foo3 (x &rest y))
72 (defmethod foo3 ((x t) &rest y) nil)
73 (defmethod foo4 ((x t) &rest z &key y) nil)
74 (defgeneric foo4 (x &rest z &key y))
75 (assert (expect-error (defgeneric foo5 (x &rest))))
76 (assert (expect-error (macroexpand-1 '(defmethod foo6 (x &rest)))))
78 ;;; more lambda-list checking
80 ;;; DEFGENERIC lambda lists are subject to various limitations, as per
81 ;;; section 3.4.2 of the ANSI spec. Since Alexey Dejneka's patch for
82 ;;; bug 191-b ca. sbcl-0.7.22, these limitations should be enforced.
83 (labels ((coerce-to-boolean (x)
85 (%like-or-dislike (expr expected-failure-p)
86 (declare (type boolean expected-failure-p))
87 (format t "~&trying ~S~%" expr)
88 (multiple-value-bind (fun warnings-p failure-p)
92 (declare (ignore fun))
93 ;; In principle the constraint on WARNINGS-P below seems
94 ;; reasonable, but in practice we get warnings about
95 ;; undefined functions from the DEFGENERICs, apparently
96 ;; because the DECLAIMs which ordinarily prevent such
97 ;; warnings don't take effect because EVAL-WHEN
98 ;; (:COMPILE-TOPLEVEL) loses its magic when compiled
99 ;; within a LAMBDA. So maybe we can't test WARNINGS-P
101 ;;(unless expected-failure-p
102 ;; (assert (not warnings-p)))
103 (assert (eq (coerce-to-boolean failure-p) expected-failure-p))))
105 (%like-or-dislike expr nil))
107 (%like-or-dislike expr t)))
109 (dislike '(defgeneric gf-for-ll-test-0 ("a" #p"b")))
110 (like '(defgeneric gf-for-ll-test-1 ()))
111 (like '(defgeneric gf-for-ll-test-2 (x)))
112 ;; forbidden default or supplied-p for &OPTIONAL or &KEY arguments
113 (dislike '(defgeneric gf-for-ll-test-3 (x &optional (y 0))))
114 (like '(defgeneric gf-for-ll-test-4 (x &optional y)))
115 (dislike '(defgeneric gf-for-ll-test-5 (x y &key (z :z z-p))))
116 (like '(defgeneric gf-for-ll-test-6 (x y &key z)))
117 (dislike '(defgeneric gf-for-ll-test-7 (x &optional (y 0) &key z)))
118 (like '(defgeneric gf-for-ll-test-8 (x &optional y &key z)))
119 (dislike '(defgeneric gf-for-ll-test-9 (x &optional y &key (z :z))))
120 (like '(defgeneric gf-for-ll-test-10 (x &optional y &key z)))
121 (dislike '(defgeneric gf-for-ll-test-11 (&optional &key (k :k k-p))))
122 (like '(defgeneric gf-for-ll-test-12 (&optional &key k)))
124 (dislike '(defgeneric gf-for-ll-test-13 (x y z &optional a &aux g h)))
125 (like '(defgeneric gf-for-ll-test-14 (x y z &optional a)))
126 (dislike '(defgeneric gf-for-ll-test-bare-aux-1 (x &aux)))
127 (like '(defgeneric gf-for-ll-test-bare-aux-2 (x)))
128 ;; also can't use bogoDEFMETHODish type-qualifier-ish decorations
129 ;; on required arguments
130 (dislike '(defgeneric gf-for-11-test-15 ((arg t))))
131 (like '(defgeneric gf-for-11-test-16 (arg))))
133 ;;; structure-class tests setup
134 (defclass structure-class-foo1 () () (:metaclass cl:structure-class))
135 (defclass structure-class-foo2 (structure-class-foo1)
136 () (:metaclass cl:structure-class))
138 ;;; standard-class tests setup
139 (defclass standard-class-foo1 () () (:metaclass cl:standard-class))
140 (defclass standard-class-foo2 (standard-class-foo1)
141 () (:metaclass cl:standard-class))
143 (assert (typep (class-of (make-instance 'structure-class-foo1))
145 (assert (typep (make-instance 'structure-class-foo1) 'structure-class-foo1))
146 (assert (typep (make-instance 'standard-class-foo1) 'standard-class-foo1))
148 ;;; DEFGENERIC's blow-away-old-methods behavior is specified to have
149 ;;; special hacks to distinguish between defined-with-DEFGENERIC-:METHOD
150 ;;; methods and defined-with-DEFMETHOD methods, so that reLOADing
151 ;;; DEFGENERIC-containing files does the right thing instead of
152 ;;; randomly slicing your generic functions. (APD made this work
153 ;;; in sbcl-0.7.0.2.)
154 (defgeneric born-to-be-redefined (x)
155 (:method ((x integer))
157 (defmethod born-to-be-redefined ((x real))
159 (assert (eq (born-to-be-redefined 1) 'integer))
160 (defgeneric born-to-be-redefined (x))
161 (assert (eq (born-to-be-redefined 1) 'real)) ; failed until sbcl-0.7.0.2
162 (defgeneric born-to-be-redefined (x)
163 (:method ((x integer))
165 (defmethod born-to-be-redefined ((x integer))
167 (assert (eq (born-to-be-redefined 1) 'int))
168 (defgeneric born-to-be-redefined (x))
169 (assert (eq (born-to-be-redefined 1) 'int))
171 ;;; In the removal of ITERATE from SB-PCL, a bug was introduced
172 ;;; preventing forward-references and also change-class (which
173 ;;; forward-references used interally) from working properly. One
174 ;;; symptom was reported by Brian Spilsbury (sbcl-devel 2002-04-08),
175 ;;; and another on IRC by Dan Barlow simultaneously. Better check
176 ;;; that it doesn't happen again.
178 ;;; First, the forward references:
179 (defclass forward-ref-a (forward-ref-b) ())
180 (defclass forward-ref-b () ())
181 ;;; (a couple more complicated examples found by Paul Dietz' test
183 (defclass forward-ref-c1 (forward-ref-c2) ())
184 (defclass forward-ref-c2 (forward-ref-c3) ())
186 (defclass forward-ref-d1 (forward-ref-d2 forward-ref-d3) ())
187 (defclass forward-ref-d2 (forward-ref-d4 forward-ref-d5) ())
189 ;;; Then change-class
190 (defclass class-with-slots ()
191 ((a-slot :initarg :a-slot :accessor a-slot)
192 (b-slot :initarg :b-slot :accessor b-slot)
193 (c-slot :initarg :c-slot :accessor c-slot)))
195 (let ((foo (make-instance 'class-with-slots
199 (let ((bar (change-class foo 'class-with-slots)))
200 (assert (= (a-slot bar) 1))
201 (assert (= (b-slot bar) 2))
202 (assert (= (c-slot bar) 3))))
204 ;;; some more CHANGE-CLASS testing, now that we have an ANSI-compliant
205 ;;; version (thanks to Espen Johnsen)
206 (defclass from-class ()
207 ((foo :initarg :foo :accessor foo)))
208 (defclass to-class ()
209 ((foo :initarg :foo :accessor foo)
210 (bar :initarg :bar :accessor bar)))
211 (let* ((from (make-instance 'from-class :foo 1))
212 (to (change-class from 'to-class :bar 2)))
213 (assert (= (foo to) 1))
214 (assert (= (bar to) 2)))
216 ;;; Until Pierre Mai's patch (sbcl-devel 2002-06-18, merged in
217 ;;; sbcl-0.7.4.39) the :MOST-SPECIFIC-LAST option had no effect.
218 (defgeneric bug180 (x)
219 (:method-combination list :most-specific-last))
220 (defmethod bug180 list ((x number))
222 (defmethod bug180 list ((x fixnum))
224 (assert (equal (bug180 14) '(number fixnum)))
226 ;;; printing a structure class should not loop indefinitely (or cause
227 ;;; a stack overflow):
228 (defclass test-printing-structure-class ()
229 ((slot :initarg :slot))
230 (:metaclass structure-class))
231 (print (make-instance 'test-printing-structure-class :slot 2))
233 ;;; structure-classes should behave nicely when subclassed
234 (defclass super-structure ()
235 ((a :initarg :a :accessor a-accessor)
236 (b :initform 2 :reader b-reader))
237 (:metaclass structure-class))
238 (defclass sub-structure (super-structure)
239 ((c :initarg :c :writer c-writer :accessor c-accessor))
240 (:metaclass structure-class))
241 (let ((foo (make-instance 'sub-structure :a 1 :c 3)))
242 (assert (= (a-accessor foo) 1))
243 (assert (= (b-reader foo) 2))
244 (assert (= (c-accessor foo) 3))
245 (setf (a-accessor foo) 4)
247 (assert (= (a-accessor foo) 4))
248 (assert (= (c-accessor foo) 5)))
250 ;;; At least as of sbcl-0.7.4, PCL has code to support a special
251 ;;; encoding of effective method functions for slot accessors as
252 ;;; FIXNUMs. Given this special casing, it'd be easy for slot accessor
253 ;;; functions to get broken in special ways even though ordinary
254 ;;; generic functions work. As of sbcl-0.7.4 we didn't have any tests
255 ;;; for that possibility. Now we have a few tests:
257 ((fin :reader ffin :writer ffin!)
258 (tail :reader ftail :writer ftail!)))
259 (defvar *fish* (make-instance 'fish))
260 (ffin! 'triangular-fin *fish*)
261 (defclass cod (fish) ())
262 (defvar *cod* (make-instance 'cod))
263 (defparameter *clos-dispatch-side-fx* (make-array 0 :fill-pointer 0))
264 (defmethod ffin! (new-fin (cod cod))
265 (format t "~&about to set ~S fin to ~S~%" cod new-fin)
266 (vector-push-extend '(cod) *clos-dispatch-side-fx*)
269 (format t "~&done setting ~S fin to ~S~%" cod new-fin)))
270 (defmethod ffin! :before (new-fin (cod cod))
271 (vector-push-extend '(:before cod) *clos-dispatch-side-fx*)
272 (format t "~&exploring the CLOS dispatch zoo with COD fins~%"))
273 (ffin! 'almost-triang-fin *cod*)
274 (assert (eq (ffin *cod*) 'almost-triang-fin))
275 (assert (equalp #((:before cod) (cod)) *clos-dispatch-side-fx*))
277 ;;; Until sbcl-0.7.6.21, the long form of DEFINE-METHOD-COMBINATION
278 ;;; ignored its options; Gerd Moellmann found and fixed the problem
279 ;;; for cmucl (cmucl-imp 2002-06-18).
280 (define-method-combination test-mc (x)
281 ;; X above being a method-group-specifier
282 ((primary () :required t))
283 `(call-method ,(first primary)))
286 (:method-combination test-mc 1))
291 ;;; Until sbcl-0.7.7.20, some conditions weren't being signalled, and
292 ;;; some others were of the wrong type:
293 (macrolet ((assert-program-error (form)
294 `(multiple-value-bind (value error)
295 (ignore-errors ,form)
296 (unless (and (null value) (typep error 'program-error))
297 (error "~S failed: ~S, ~S" ',form value error)))))
298 (assert-program-error (defclass foo001 () (a b a)))
299 (assert-program-error (defclass foo002 ()
301 (:default-initargs x 'a x 'b)))
302 (assert-program-error (defclass foo003 ()
303 ((a :allocation :class :allocation :class))))
304 (assert-program-error (defclass foo004 ()
306 ;; and some more, found by Wolfhard Buss and fixed for cmucl by Gerd
307 ;; Moellmann in sbcl-0.7.8.x:
308 (assert-program-error (progn
309 (defmethod odd-key-args-checking (&key (key 42)) key)
310 (odd-key-args-checking 3)))
311 (assert (= (odd-key-args-checking) 42))
312 (assert (eq (odd-key-args-checking :key t) t))
313 ;; yet some more, fixed in sbcl-0.7.9.xx
314 (assert-program-error (defclass foo005 ()
315 (:metaclass sb-pcl::funcallable-standard-class)
317 (assert-program-error (defclass foo006 ()
318 ((a :reader (setf a)))))
319 (assert-program-error (defclass foo007 ()
321 (assert-program-error (defclass foo008 ()
323 (:default-initargs :a 1)
324 (:default-initargs :a 2)))
325 ;; and also BUG 47d, fixed in sbcl-0.8alpha.0.26
326 (assert-program-error (defgeneric if (x)))
327 ;; DEFCLASS should detect an error if slot names aren't suitable as
329 (assert-program-error (defclass foo009 ()
331 (assert-program-error (defclass foo010 ()
332 (("a" :initarg :a))))
333 (assert-program-error (defclass foo011 ()
334 ((#1a() :initarg :a))))
335 (assert-program-error (defclass foo012 ()
337 (assert-program-error (defclass foo013 () ("a")))
338 ;; specialized lambda lists have certain restrictions on ordering,
339 ;; repeating keywords, and the like:
340 (assert-program-error (defmethod foo014 ((foo t) &rest) nil))
341 (assert-program-error (defmethod foo015 ((foo t) &rest x y) nil))
342 (assert-program-error (defmethod foo016 ((foo t) &allow-other-keys) nil))
343 (assert-program-error (defmethod foo017 ((foo t)
344 &optional x &optional y) nil))
345 (assert-program-error (defmethod foo018 ((foo t) &rest x &rest y) nil))
346 (assert-program-error (defmethod foo019 ((foo t) &rest x &optional y) nil))
347 (assert-program-error (defmethod foo020 ((foo t) &key x &optional y) nil))
348 (assert-program-error (defmethod foo021 ((foo t) &key x &rest y) nil)))
350 ;;; DOCUMENTATION's argument-precedence-order wasn't being faithfully
351 ;;; preserved through the bootstrap process until sbcl-0.7.8.39.
352 ;;; (thanks to Gerd Moellmann)
353 (let ((answer (documentation '+ 'function)))
354 (assert (stringp answer))
355 (defmethod documentation ((x (eql '+)) y) "WRONG")
356 (assert (string= (documentation '+ 'function) answer)))
358 ;;; only certain declarations are permitted in DEFGENERIC
359 (macrolet ((assert-program-error (form)
360 `(multiple-value-bind (value error)
361 (ignore-errors ,form)
362 (assert (null value))
363 (assert (typep error 'program-error)))))
364 (assert-program-error (defgeneric bogus-declaration (x)
365 (declare (special y))))
366 (assert-program-error (defgeneric bogus-declaration2 (x)
367 (declare (notinline concatenate)))))
368 ;;; CALL-NEXT-METHOD should call NO-NEXT-METHOD if there is no next
370 (defmethod no-next-method-test ((x integer)) (call-next-method))
371 (assert (null (ignore-errors (no-next-method-test 1))))
372 (defmethod no-next-method ((g (eql #'no-next-method-test)) m &rest args)
374 (assert (eq (no-next-method-test 1) 'success))
375 (assert (null (ignore-errors (no-next-method-test 'foo))))
377 ;;; regression test for bug 176, following a fix that seems
378 ;;; simultaneously to fix 140 while not exposing 176 (by Gerd
379 ;;; Moellmann, merged in sbcl-0.7.9.12).
381 (let ((lastname (intern (format nil "C176-~D" (1- i))))
382 (name (intern (format nil "C176-~D" i))))
383 (eval `(defclass ,name
384 (,@(if (= i 0) nil (list lastname)))
386 (eval `(defmethod initialize-instance :after ((x ,name) &rest any)
387 (declare (ignore any))))))
388 (defclass b176 () (aslot-176))
389 (defclass c176-0 (b176) ())
390 (assert (= 1 (setf (slot-value (make-instance 'c176-9) 'aslot-176) 1)))
392 ;;; DEFINE-METHOD-COMBINATION was over-eager at checking for duplicate
394 (define-method-combination dmc-test-mc (&optional (order :most-specific-first))
396 (primary (dmc-test-mc) :order order :required t))
397 (let ((form (if (rest primary)
398 `(and ,@(mapcar #'(lambda (method)
399 `(call-method ,method))
401 `(call-method ,(first primary)))))
403 `(call-method ,(first around)
405 (make-method ,form)))
408 (defgeneric dmc-test-mc (&key k)
409 (:method-combination dmc-test-mc))
411 (defmethod dmc-test-mc dmc-test-mc (&key k)
415 ;;; While I'm at it, DEFINE-METHOD-COMBINATION is defined to return
416 ;;; the NAME argument, not some random method object. So:
417 (assert (eq (define-method-combination dmc-test-return-foo)
418 'dmc-test-return-foo))
419 (assert (eq (define-method-combination dmc-test-return-bar :operator and)
420 'dmc-test-return-bar))
421 (assert (eq (define-method-combination dmc-test-return
422 (&optional (order :most-specific-first))
424 (primary (dmc-test-return) :order order :required t))
425 (let ((form (if (rest primary)
426 `(and ,@(mapcar #'(lambda (method)
427 `(call-method ,method))
429 `(call-method ,(first primary)))))
431 `(call-method ,(first around)
433 (make-method ,form)))
437 ;;; DEFMETHOD should signal an ERROR if an incompatible lambda list is
439 (defmethod incompatible-ll-test-1 (x) x)
440 (assert (raises-error? (defmethod incompatible-ll-test-1 (x y) y)))
441 (assert (raises-error? (defmethod incompatible-ll-test-1 (x &rest y) y)))
442 ;;; Sneakily using a bit of MOPness to check some consistency
444 (sb-pcl:generic-function-methods #'incompatible-ll-test-1)) 1))
446 (defmethod incompatible-ll-test-2 (x &key bar) bar)
447 (assert (raises-error? (defmethod incompatible-ll-test-2 (x) x)))
448 (defmethod incompatible-ll-test-2 (x &rest y) y)
450 (sb-pcl:generic-function-methods #'incompatible-ll-test-2)) 1))
451 (defmethod incompatible-ll-test-2 ((x integer) &key bar) bar)
453 (sb-pcl:generic-function-methods #'incompatible-ll-test-2)) 2))
455 ;;; Per Christophe, this is an illegal method call because of 7.6.5
456 (assert (raises-error? (incompatible-ll-test-2 t 1 2)))
458 (assert (eq (incompatible-ll-test-2 1 :bar 'yes) 'yes))
460 (defmethod incompatible-ll-test-3 ((x integer)) x)
461 (remove-method #'incompatible-ll-test-3
462 (find-method #'incompatible-ll-test-3
464 (list (find-class 'integer))))
465 (assert (raises-error? (defmethod incompatible-ll-test-3 (x y) (list x y))))
468 ;;; Attempting to instantiate classes with forward references in their
469 ;;; CPL should signal errors (FIXME: of what type?)
470 (defclass never-finished-class (this-one-unfinished-too) ())
471 (multiple-value-bind (result error)
472 (ignore-errors (make-instance 'never-finished-class))
473 (assert (null result))
474 (assert (typep error 'error)))
475 (multiple-value-bind (result error)
476 (ignore-errors (make-instance 'this-one-unfinished-too))
477 (assert (null result))
478 (assert (typep error 'error)))
480 ;;; Classes with :ALLOCATION :CLASS slots should be subclassable (and
481 ;;; weren't for a while in sbcl-0.7.9.xx)
482 (defclass superclass-with-slot ()
483 ((a :allocation :class)))
484 (defclass subclass-for-class-allocation (superclass-with-slot) ())
485 (make-instance 'subclass-for-class-allocation)
487 ;;; bug #136: CALL-NEXT-METHOD was being a little too lexical,
488 ;;; resulting in failure in the following:
489 (defmethod call-next-method-lexical-args ((x integer))
491 (defmethod call-next-method-lexical-args :around ((x integer))
494 (assert (= (call-next-method-lexical-args 3) 3))
496 ;;; DEFINE-METHOD-COMBINATION with arguments was hopelessly broken
498 (defvar *d-m-c-args-test* nil)
499 (define-method-combination progn-with-lock ()
503 (progn (lock (object-lock ,object))
504 ,@(mapcar #'(lambda (method)
505 `(call-method ,method))
507 (unlock (object-lock ,object))))
508 (defun object-lock (obj)
509 (push "object-lock" *d-m-c-args-test*)
512 (push "unlock" *d-m-c-args-test*)
515 (push "lock" *d-m-c-args-test*)
517 (defgeneric d-m-c-args-test (x)
518 (:method-combination progn-with-lock))
519 (defmethod d-m-c-args-test ((x symbol))
520 (push "primary" *d-m-c-args-test*))
521 (defmethod d-m-c-args-test ((x number))
523 (assert (equal (d-m-c-args-test t) '("primary" "lock" "object-lock")))
524 (assert (equal *d-m-c-args-test*
525 '("unlock" "object-lock" "primary" "lock" "object-lock")))
526 (setf *d-m-c-args-test* nil)
527 (ignore-errors (d-m-c-args-test 1))
528 (assert (equal *d-m-c-args-test*
529 '("unlock" "object-lock" "lock" "object-lock")))
531 ;;; The walker (on which DEFMETHOD depended) didn't know how to handle
532 ;;; SYMBOL-MACROLET properly. In fact, as of sbcl-0.7.10.20 it still
533 ;;; doesn't, but it does well enough to compile the following without
534 ;;; error (the problems remain in asking for a complete macroexpansion
535 ;;; of an arbitrary form).
536 (symbol-macrolet ((x 1))
537 (defmethod bug222 (z)
538 (macrolet ((frob (form) `(progn ,form ,x)))
540 (assert (= (bug222 t) 1))
542 ;;; also, a test case to guard against bogus environment hacking:
543 (eval-when (:compile-toplevel :load-toplevel :execute)
545 ;;; this should at the least compile:
547 (defmethod bug222-b (z stream)
548 (macrolet ((frob (form) `(progn ,form ,bug222-b)))
549 (frob (format stream "~D~%" bug222-b)))))
550 ;;; and it would be nice (though not specified by ANSI) if the answer
552 (let ((x (make-string-output-stream)))
553 ;; not specified by ANSI
554 (assert (= (bug222-b t x) 3))
556 (assert (char= (char (get-output-stream-string x) 0) #\1)))
558 ;;; REINITIALIZE-INSTANCE, in the ctor optimization, wasn't checking
559 ;;; for invalid initargs where it should:
560 (defclass class234 () ())
561 (defclass subclass234 (class234) ())
564 (reinitialize-instance (make-instance 'class234) :dummy 0))
566 (reinitialize-instance (make-instance 'subclass234) :dummy 0))
567 (assert (raises-error? (bug-234) program-error))
568 (defmethod shared-initialize :after ((i class234) slots &key dummy)
570 (assert (typep (subbug-234) 'subclass234))
572 ;; once for MAKE-INSTANCE, once for REINITIALIZE-INSTANCE
575 ;;; also, some combinations of MAKE-INSTANCE and subclassing missed
576 ;;; new methods (Gerd Moellmann sbcl-devel 2002-12-29):
577 (defclass class234-b1 () ())
578 (defclass class234-b2 (class234-b1) ())
579 (defvar *bug234-b* 0)
581 (make-instance 'class234-b2))
584 (assert (= *bug234-b* 0))
585 (defmethod initialize-instance :before ((x class234-b1) &rest args)
586 (declare (ignore args))
589 (assert (= *bug234-b* 1))
591 ;;; we should be able to make classes with uninterned names:
592 (defclass #:class-with-uninterned-name () ())
594 ;;; SLOT-MISSING should be called when there are missing slots.
595 (defclass class-with-all-slots-missing () ())
596 (defmethod slot-missing (class (o class-with-all-slots-missing)
600 (assert (eq (slot-value (make-instance 'class-with-all-slots-missing) 'foo)
602 (assert (eq (funcall (lambda (x) (slot-value x 'bar))
603 (make-instance 'class-with-all-slots-missing))
605 (assert (eq (funcall (lambda (x) (setf (slot-value x 'baz) 'baz))
606 (make-instance 'class-with-all-slots-missing))
607 ;; SLOT-MISSING's value is specified to be ignored; we
611 ;;; we should be able to specialize on anything that names a class.
612 (defclass name-for-class () ())
613 (defmethod something-that-specializes ((x name-for-class)) 1)
614 (setf (find-class 'other-name-for-class) (find-class 'name-for-class))
615 (defmethod something-that-specializes ((x other-name-for-class)) 2)
616 (assert (= (something-that-specializes (make-instance 'name-for-class)) 2))
617 (assert (= (something-that-specializes (make-instance 'other-name-for-class))
620 ;;; more forward referenced classes stuff
621 (defclass frc-1 (frc-2) ())
622 (assert (subtypep 'frc-1 (find-class 'frc-2)))
623 (assert (subtypep (find-class 'frc-1) 'frc-2))
624 (assert (not (subtypep (find-class 'frc-2) 'frc-1)))
625 (defclass frc-2 (frc-3) ((a :initarg :a)))
626 (assert (subtypep 'frc-1 (find-class 'frc-3)))
627 (defclass frc-3 () ())
628 (assert (typep (make-instance 'frc-1 :a 2) (find-class 'frc-1)))
629 (assert (typep (make-instance 'frc-2 :a 3) (find-class 'frc-2)))
631 ;;; check that we can define classes with two slots of different names
632 ;;; (even if it STYLE-WARNs).
633 (defclass odd-name-class ()
634 ((name :initarg :name)
635 (cl-user::name :initarg :name2)))
636 (let ((x (make-instance 'odd-name-class :name 1 :name2 2)))
637 (assert (= (slot-value x 'name) 1))
638 (assert (= (slot-value x 'cl-user::name) 2)))
640 ;;; ALLOCATE-INSTANCE should work on structures, even if defined by
641 ;;; DEFSTRUCT (and not DEFCLASS :METACLASS STRUCTURE-CLASS).
642 (defstruct allocatable-structure a)
643 (assert (typep (allocate-instance (find-class 'allocatable-structure))
644 'allocatable-structure))
646 ;;; Bug found by Paul Dietz when devising CPL tests: somewhat
647 ;;; amazingly, calls to CPL would work a couple of times, and then
648 ;;; start returning NIL. A fix was found (relating to the
649 ;;; applicability of constant-dfun optimization) by Gerd Moellmann.
651 (:method-combination list)
652 (:method list ((x broadcast-stream)) 'broadcast-stream)
653 (:method list ((x integer)) 'integer)
654 (:method list ((x number)) 'number)
655 (:method list ((x stream)) 'stream)
656 (:method list ((x structure-object)) 'structure-object))
657 (assert (equal (cpl 0) '(integer number)))
658 (assert (equal (cpl 0) '(integer number)))
659 (assert (equal (cpl 0) '(integer number)))
660 (assert (equal (cpl 0) '(integer number)))
661 (assert (equal (cpl 0) '(integer number)))
662 (assert (equal (cpl (make-broadcast-stream))
663 '(broadcast-stream stream structure-object)))
664 (assert (equal (cpl (make-broadcast-stream))
665 '(broadcast-stream stream structure-object)))
666 (assert (equal (cpl (make-broadcast-stream))
667 '(broadcast-stream stream structure-object)))
669 ;;; Bug in CALL-NEXT-METHOD: assignment to the method's formal
670 ;;; parameters shouldn't affect the arguments to the next method for a
671 ;;; no-argument call to CALL-NEXT-METHOD
672 (defgeneric cnm-assignment (x)
674 (:method ((x integer)) (setq x 3)
675 (list x (call-next-method) (call-next-method x))))
676 (assert (equal (cnm-assignment 1) '(3 1 3)))
678 ;;; Bug reported by Istvan Marko 2003-07-09
679 (let ((class-name (gentemp)))
680 (loop for i from 1 to 9
681 for slot-name = (intern (format nil "X~D" i))
682 for initarg-name = (intern (format nil "X~D" i) :keyword)
683 collect `(,slot-name :initarg ,initarg-name) into slot-descs
684 append `(,initarg-name (list 0)) into default-initargs
685 finally (eval `(defclass ,class-name ()
687 (:default-initargs ,@default-initargs))))
688 (let ((f (compile nil `(lambda () (make-instance ',class-name)))))
689 (assert (typep (funcall f) class-name))))
691 ;;; bug 262: DEFMETHOD failed on a generic function without a lambda
693 (ensure-generic-function 'bug262)
694 (defmethod bug262 (x y)
696 (assert (equal (bug262 1 2) '(1 2)))
698 ;;; salex on #lisp 2003-10-13 reported that type declarations inside
699 ;;; WITH-SLOTS are too hairy to be checked
700 (defun ensure-no-notes (form)
701 (handler-case (compile nil `(lambda () ,form))
702 (sb-ext:compiler-note (c)
703 ;; FIXME: it would be better to check specifically for the "type
704 ;; is too hairy" note
707 (ensure-no-notes '(with-slots (a) *x*
708 (declare (integer a))
710 (ensure-no-notes '(with-slots (a) *x*
711 (declare (integer a))
712 (declare (notinline slot-value))
715 ;;; from CLHS 7.6.5.1
716 (defclass character-class () ((char :initarg :char)))
717 (defclass picture-class () ((glyph :initarg :glyph)))
718 (defclass character-picture-class (character-class picture-class) ())
720 (defmethod width ((c character-class) &key font) font)
721 (defmethod width ((p picture-class) &key pixel-size) pixel-size)
723 (assert (raises-error?
724 (width (make-instance 'character-class :char #\Q)
725 :font 'baskerville :pixel-size 10)
727 (assert (raises-error?
728 (width (make-instance 'picture-class :glyph #\Q)
729 :font 'baskerville :pixel-size 10)
731 (assert (eq (width (make-instance 'character-picture-class :char #\Q)
732 :font 'baskerville :pixel-size 10)
735 ;;; class redefinition shouldn't give any warnings, in the usual case
736 (defclass about-to-be-redefined () ((some-slot :accessor some-slot)))
737 (handler-bind ((warning #'error))
738 (defclass about-to-be-redefined () ((some-slot :accessor some-slot))))
740 ;;; attempts to add accessorish methods to generic functions with more
741 ;;; complex lambda lists should fail
742 (defgeneric accessoroid (object &key &allow-other-keys))
743 (assert (raises-error?
744 (defclass accessoroid-class () ((slot :accessor accessoroid)))
747 ;;; reported by Bruno Haible sbcl-devel 2004-04-15
748 (defclass shared-slot-and-redefinition ()
749 ((size :initarg :size :initform 1 :allocation :class)))
750 (let ((i (make-instance 'shared-slot-and-redefinition)))
751 (defclass shared-slot-and-redefinition ()
752 ((size :initarg :size :initform 2 :allocation :class)))
753 (assert (= (slot-value i 'size) 1)))
755 ;;; reported by Bruno Haible sbcl-devel 2004-04-15
756 (defclass superclass-born-to-be-obsoleted () (a))
757 (defclass subclass-born-to-be-obsoleted (superclass-born-to-be-obsoleted) ())
758 (defparameter *born-to-be-obsoleted*
759 (make-instance 'subclass-born-to-be-obsoleted))
760 (defparameter *born-to-be-obsoleted-obsoleted* nil)
761 (defmethod update-instance-for-redefined-class
762 ((o subclass-born-to-be-obsoleted) a d pl &key)
763 (setf *born-to-be-obsoleted-obsoleted* t))
764 (make-instances-obsolete 'superclass-born-to-be-obsoleted)
765 (slot-boundp *born-to-be-obsoleted* 'a)
766 (assert *born-to-be-obsoleted-obsoleted*)
768 ;;; additional test suggested by Bruno Haible sbcl-devel 2004-04-21
769 (defclass super-super-obsoleted () (a))
770 (defclass super-obsoleted-1 (super-super-obsoleted) ())
771 (defclass super-obsoleted-2 (super-super-obsoleted) ())
772 (defclass obsoleted (super-obsoleted-1 super-obsoleted-2) ())
773 (defparameter *obsoleted* (make-instance 'obsoleted))
774 (defparameter *obsoleted-counter* 0)
775 (defmethod update-instance-for-redefined-class ((o obsoleted) a d pl &key)
776 (incf *obsoleted-counter*))
777 (make-instances-obsolete 'super-super-obsoleted)
778 (slot-boundp *obsoleted* 'a)
779 (assert (= *obsoleted-counter* 1))
781 ;;; shared -> local slot transfers of inherited slots, reported by
784 (defclass super-with-magic-slot ()
785 ((magic :initarg :size :initform 1 :allocation :class)))
786 (defclass sub-of-super-with-magic-slot (super-with-magic-slot) ())
787 (setq i (make-instance 'sub-of-super-with-magic-slot))
788 (defclass super-with-magic-slot ()
789 ((magic :initarg :size :initform 2)))
790 (assert (= 1 (slot-value i 'magic))))
792 ;;; MAKE-INSTANCES-OBSOLETE return values
793 (defclass one-more-to-obsolete () ())
794 (assert (eq 'one-more-to-obsolete
795 (make-instances-obsolete 'one-more-to-obsolete)))
796 (assert (eq (find-class 'one-more-to-obsolete)
797 (make-instances-obsolete (find-class 'one-more-to-obsolete))))
799 ;;; Sensible error instead of a BUG. Reported by Thomas Burdick.
800 (multiple-value-bind (value err)
802 (defclass slot-def-with-duplicate-accessors ()
803 ((slot :writer get-slot :reader get-slot))))
804 (assert (typep err 'error))
805 (assert (not (typep err 'sb-int:bug))))
807 ;;; BUG 321: errors in parsing DEFINE-METHOD-COMBINATION arguments
810 (define-method-combination w-args ()
812 (:arguments arg1 arg2 &aux (extra :extra))
813 `(progn ,@(mapcar (lambda (method) `(call-method ,method)) method-list)))
814 (defgeneric mc-test-w-args (p1 p2 s)
815 (:method-combination w-args)
816 (:method ((p1 number) (p2 t) s)
817 (vector-push-extend (list 'number p1 p2) s))
818 (:method ((p1 string) (p2 t) s)
819 (vector-push-extend (list 'string p1 p2) s))
820 (:method ((p1 t) (p2 t) s) (vector-push-extend (list t p1 p2) s)))
821 (let ((v (make-array 0 :adjustable t :fill-pointer t)))
822 (assert (= (mc-test-w-args 1 2 v) 1))
823 (assert (equal (aref v 0) '(number 1 2)))
824 (assert (equal (aref v 1) '(t 1 2))))
826 ;;; BUG 276: declarations and mutation.
827 (defmethod fee ((x fixnum))
830 (assert (= (fee 1) 1/2))
831 (defmethod fum ((x fixnum))
834 (assert (= (fum 3) 3/2))
835 (defmethod fii ((x fixnum))
836 (declare (special x))
839 (assert (= (fii 1) 1/2))
841 (defmethod faa ((*faa* string-stream))
842 (setq *faa* (make-broadcast-stream *faa*))
843 (write-line "Break, you sucker!" *faa*)
845 (assert (eq 'ok (faa (make-string-output-stream))))
847 ;;; Bug reported by Zach Beane; incorrect return of (function
848 ;;; ',fun-name) in defgeneric
850 (typep (funcall (compile nil
851 '(lambda () (flet ((nonsense () nil))
852 (defgeneric nonsense ())))))
856 (typep (funcall (compile nil
857 '(lambda () (flet ((nonsense-2 () nil))
858 (defgeneric nonsense-2 ()
862 ;;; bug reported by Bruno Haible: (setf find-class) using a
863 ;;; forward-referenced class
864 (defclass fr-sub (fr-super) ())
865 (setf (find-class 'fr-alt) (find-class 'fr-super))
866 (assert (eq (find-class 'fr-alt) (find-class 'fr-super)))
869 (sb-ext:quit :unix-status 104)