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) &key y &rest z) 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:
181 ;;; Then change-class
182 (defclass class-with-slots ()
183 ((a-slot :initarg :a-slot :accessor a-slot)
184 (b-slot :initarg :b-slot :accessor b-slot)
185 (c-slot :initarg :c-slot :accessor c-slot)))
186 (let ((foo (make-instance 'class-with-slots
190 (let ((bar (change-class foo 'class-with-slots)))
191 (assert (= (a-slot bar) 1))
192 (assert (= (b-slot bar) 2))
193 (assert (= (c-slot bar) 3))))
195 ;;; some more CHANGE-CLASS testing, now that we have an ANSI-compliant
196 ;;; version (thanks to Espen Johnsen)
197 (defclass from-class ()
198 ((foo :initarg :foo :accessor foo)))
199 (defclass to-class ()
200 ((foo :initarg :foo :accessor foo)
201 (bar :initarg :bar :accessor bar)))
202 (let* ((from (make-instance 'from-class :foo 1))
203 (to (change-class from 'to-class :bar 2)))
204 (assert (= (foo to) 1))
205 (assert (= (bar to) 2)))
207 ;;; Until Pierre Mai's patch (sbcl-devel 2002-06-18, merged in
208 ;;; sbcl-0.7.4.39) the :MOST-SPECIFIC-LAST option had no effect.
209 (defgeneric bug180 (x)
210 (:method-combination list :most-specific-last))
211 (defmethod bug180 list ((x number))
213 (defmethod bug180 list ((x fixnum))
215 (assert (equal (bug180 14) '(number fixnum)))
217 ;;; printing a structure class should not loop indefinitely (or cause
218 ;;; a stack overflow):
219 (defclass test-printing-structure-class ()
220 ((slot :initarg :slot))
221 (:metaclass structure-class))
222 (print (make-instance 'test-printing-structure-class :slot 2))
224 ;;; structure-classes should behave nicely when subclassed
225 (defclass super-structure ()
226 ((a :initarg :a :accessor a-accessor)
227 (b :initform 2 :reader b-reader))
228 (:metaclass structure-class))
229 (defclass sub-structure (super-structure)
230 ((c :initarg :c :writer c-writer :accessor c-accessor))
231 (:metaclass structure-class))
232 (let ((foo (make-instance 'sub-structure :a 1 :c 3)))
233 (assert (= (a-accessor foo) 1))
234 (assert (= (b-reader foo) 2))
235 (assert (= (c-accessor foo) 3))
236 (setf (a-accessor foo) 4)
238 (assert (= (a-accessor foo) 4))
239 (assert (= (c-accessor foo) 5)))
241 ;;; At least as of sbcl-0.7.4, PCL has code to support a special
242 ;;; encoding of effective method functions for slot accessors as
243 ;;; FIXNUMs. Given this special casing, it'd be easy for slot accessor
244 ;;; functions to get broken in special ways even though ordinary
245 ;;; generic functions work. As of sbcl-0.7.4 we didn't have any tests
246 ;;; for that possibility. Now we have a few tests:
248 ((fin :reader ffin :writer ffin!)
249 (tail :reader ftail :writer ftail!)))
250 (defvar *fish* (make-instance 'fish))
251 (ffin! 'triangular-fin *fish*)
252 (defclass cod (fish) ())
253 (defvar *cod* (make-instance 'cod))
254 (defparameter *clos-dispatch-side-fx* (make-array 0 :fill-pointer 0))
255 (defmethod ffin! (new-fin (cod cod))
256 (format t "~&about to set ~S fin to ~S~%" cod new-fin)
257 (vector-push-extend '(cod) *clos-dispatch-side-fx*)
260 (format t "~&done setting ~S fin to ~S~%" cod new-fin)))
261 (defmethod ffin! :before (new-fin (cod cod))
262 (vector-push-extend '(:before cod) *clos-dispatch-side-fx*)
263 (format t "~&exploring the CLOS dispatch zoo with COD fins~%"))
264 (ffin! 'almost-triang-fin *cod*)
265 (assert (eq (ffin *cod*) 'almost-triang-fin))
266 (assert (equalp #((:before cod) (cod)) *clos-dispatch-side-fx*))
268 ;;; Until sbcl-0.7.6.21, the long form of DEFINE-METHOD-COMBINATION
269 ;;; ignored its options; Gerd Moellmann found and fixed the problem
270 ;;; for cmucl (cmucl-imp 2002-06-18).
271 (define-method-combination test-mc (x)
272 ;; X above being a method-group-specifier
273 ((primary () :required t))
274 `(call-method ,(first primary)))
277 (:method-combination test-mc 1))
282 ;;; Until sbcl-0.7.7.20, some conditions weren't being signalled, and
283 ;;; some others were of the wrong type:
284 (macrolet ((assert-program-error (form)
285 `(multiple-value-bind (value error)
286 (ignore-errors ,form)
287 (assert (null value))
288 (assert (typep error 'program-error)))))
289 (assert-program-error (defclass foo001 () (a b a)))
290 (assert-program-error (defclass foo002 ()
292 (:default-initargs x 'a x 'b)))
293 (assert-program-error (defclass foo003 ()
294 ((a :allocation :class :allocation :class))))
295 (assert-program-error (defclass foo004 ()
297 ;; and some more, found by Wolfhard Buss and fixed for cmucl by Gerd
298 ;; Moellmann in sbcl-0.7.8.x:
299 (assert-program-error (progn
300 (defmethod odd-key-args-checking (&key (key 42)) key)
301 (odd-key-args-checking 3)))
302 (assert (= (odd-key-args-checking) 42))
303 (assert (eq (odd-key-args-checking :key t) t))
304 ;; yet some more, fixed in sbcl-0.7.9.xx
305 (assert-program-error (defclass foo005 ()
306 (:metaclass sb-pcl::funcallable-standard-class)
308 (assert-program-error (defclass foo006 ()
309 ((a :reader (setf a)))))
310 (assert-program-error (defclass foo007 ()
312 (assert-program-error (defclass foo008 ()
314 (:default-initargs :a 1)
315 (:default-initargs :a 2))))
317 ;;; DOCUMENTATION's argument-precedence-order wasn't being faithfully
318 ;;; preserved through the bootstrap process until sbcl-0.7.8.39.
319 ;;; (thanks to Gerd Moellmann)
320 (let ((answer (documentation '+ 'function)))
321 (assert (stringp answer))
322 (defmethod documentation ((x (eql '+)) y) "WRONG")
323 (assert (string= (documentation '+ 'function) answer)))
325 ;;; only certain declarations are permitted in DEFGENERIC
326 (macrolet ((assert-program-error (form)
327 `(multiple-value-bind (value error)
328 (ignore-errors ,form)
329 (assert (null value))
330 (assert (typep error 'program-error)))))
331 (assert-program-error (defgeneric bogus-declaration (x)
332 (declare (special y))))
333 (assert-program-error (defgeneric bogus-declaration2 (x)
334 (declare (notinline concatenate)))))
335 ;;; CALL-NEXT-METHOD should call NO-NEXT-METHOD if there is no next
337 (defmethod no-next-method-test ((x integer)) (call-next-method))
338 (assert (null (ignore-errors (no-next-method-test 1))))
339 (defmethod no-next-method ((g (eql #'no-next-method-test)) m &rest args)
341 (assert (eq (no-next-method-test 1) 'success))
342 (assert (null (ignore-errors (no-next-method-test 'foo))))
344 ;;; regression test for bug 176, following a fix that seems
345 ;;; simultaneously to fix 140 while not exposing 176 (by Gerd
346 ;;; Moellmann, merged in sbcl-0.7.9.12).
348 (let ((lastname (intern (format nil "C176-~D" (1- i))))
349 (name (intern (format nil "C176-~D" i))))
350 (eval `(defclass ,name
351 (,@(if (= i 0) nil (list lastname)))
353 (eval `(defmethod initialize-instance :after ((x ,name) &rest any)
354 (declare (ignore any))))))
355 (defclass b176 () (aslot-176))
356 (defclass c176-0 (b176) ())
357 (assert (= 1 (setf (slot-value (make-instance 'c176-9) 'aslot-176) 1)))
359 ;;; DEFINE-METHOD-COMBINATION was over-eager at checking for duplicate
361 (define-method-combination dmc-test-mc (&optional (order :most-specific-first))
363 (primary (dmc-test-mc) :order order :required t))
364 (let ((form (if (rest primary)
365 `(and ,@(mapcar #'(lambda (method)
366 `(call-method ,method))
368 `(call-method ,(first primary)))))
370 `(call-method ,(first around)
372 (make-method ,form)))
375 (defgeneric dmc-test-mc (&key k)
376 (:method-combination dmc-test-mc))
378 (defmethod dmc-test-mc dmc-test-mc (&key k)
382 ;;; While I'm at it, DEFINE-METHOD-COMBINATION is defined to return
383 ;;; the NAME argument, not some random method object. So:
384 (assert (eq (define-method-combination dmc-test-return-foo)
385 'dmc-test-return-foo))
386 (assert (eq (define-method-combination dmc-test-return-bar :operator and)
387 'dmc-test-return-bar))
388 (assert (eq (define-method-combination dmc-test-return
389 (&optional (order :most-specific-first))
391 (primary (dmc-test-return) :order order :required t))
392 (let ((form (if (rest primary)
393 `(and ,@(mapcar #'(lambda (method)
394 `(call-method ,method))
396 `(call-method ,(first primary)))))
398 `(call-method ,(first around)
400 (make-method ,form)))
404 ;;; DEFMETHOD should signal a PROGRAM-ERROR if an incompatible lambda
406 (defmethod incompatible-ll-test-1 (x) x)
407 (multiple-value-bind (result error)
408 (ignore-errors (defmethod incompatible-ll-test-1 (x y) y))
409 (assert (null result))
410 (assert (typep error 'program-error)))
411 (multiple-value-bind (result error)
412 (ignore-errors (defmethod incompatible-ll-test-1 (x &rest y) y))
413 (assert (null result))
414 (assert (typep error 'program-error)))
415 ;;; Sneakily using a bit of MOPness to check some consistency
417 (sb-pcl:generic-function-methods #'incompatible-ll-test-1)) 1))
419 (defmethod incompatible-ll-test-2 (x &key bar) bar)
420 (multiple-value-bind (result error)
421 (ignore-errors (defmethod incompatible-ll-test-2 (x) x))
422 (assert (null result))
423 (assert (typep error 'program-error)))
424 (defmethod incompatible-ll-test-2 (x &rest y) y)
426 (sb-pcl:generic-function-methods #'incompatible-ll-test-2)) 1))
427 (defmethod incompatible-ll-test-2 ((x integer) &key bar) bar)
429 (sb-pcl:generic-function-methods #'incompatible-ll-test-2)) 2))
430 (assert (equal (incompatible-ll-test-2 t 1 2) '(1 2)))
431 (assert (eq (incompatible-ll-test-2 1 :bar 'yes) 'yes))
433 ;;; Attempting to instantiate classes with forward references in their
434 ;;; CPL should signal errors (FIXME: of what type?)
435 (defclass never-finished-class (this-one-unfinished-too) ())
436 (multiple-value-bind (result error)
437 (ignore-errors (make-instance 'never-finished-class))
438 (assert (null result))
439 (assert (typep error 'error)))
440 (multiple-value-bind (result error)
441 (ignore-errors (make-instance 'this-one-unfinished-too))
442 (assert (null result))
443 (assert (typep error 'error)))
445 ;;; Classes with :ALLOCATION :CLASS slots should be subclassable (and
446 ;;; weren't for a while in sbcl-0.7.9.xx)
447 (defclass superclass-with-slot ()
448 ((a :allocation :class)))
449 (defclass subclass-for-class-allocation (superclass-with-slot) ())
450 (make-instance 'subclass-for-class-allocation)
452 ;;; bug #136: CALL-NEXT-METHOD was being a little too lexical,
453 ;;; resulting in failure in the following:
454 (defmethod call-next-method-lexical-args ((x integer))
456 (defmethod call-next-method-lexical-args :around ((x integer))
459 (assert (= (call-next-method-lexical-args 3) 3))
461 ;;; DEFINE-METHOD-COMBINATION with arguments was hopelessly broken
463 (defvar *d-m-c-args-test* nil)
464 (define-method-combination progn-with-lock ()
468 (progn (lock (object-lock ,object))
469 ,@(mapcar #'(lambda (method)
470 `(call-method ,method))
472 (unlock (object-lock ,object))))
473 (defun object-lock (obj)
474 (push "object-lock" *d-m-c-args-test*)
477 (push "unlock" *d-m-c-args-test*)
480 (push "lock" *d-m-c-args-test*)
482 (defgeneric d-m-c-args-test (x)
483 (:method-combination progn-with-lock))
484 (defmethod d-m-c-args-test ((x symbol))
485 (push "primary" *d-m-c-args-test*))
486 (defmethod d-m-c-args-test ((x number))
488 (assert (equal (d-m-c-args-test t) '("primary" "lock" "object-lock")))
489 (assert (equal *d-m-c-args-test*
490 '("unlock" "object-lock" "primary" "lock" "object-lock")))
491 (setf *d-m-c-args-test* nil)
492 (ignore-errors (d-m-c-args-test 1))
493 (assert (equal *d-m-c-args-test*
494 '("unlock" "object-lock" "lock" "object-lock")))
496 ;;; The walker (on which DEFMETHOD depended) didn't know how to handle
497 ;;; SYMBOL-MACROLET properly. In fact, as of sbcl-0.7.10.20 it still
498 ;;; doesn't, but it does well enough to compile the following without
499 ;;; error (the problems remain in asking for a complete macroexpansion
500 ;;; of an arbitrary form).
501 (symbol-macrolet ((x 1))
502 (defmethod bug222 (z)
503 (macrolet ((frob (form) `(progn ,form ,x)))
505 (assert (= (bug222 t) 1))
507 ;;; also, a test case to guard against bogus environment hacking:
508 (eval-when (:compile-toplevel :load-toplevel :execute)
510 ;;; this should at the least compile:
512 (defmethod bug222-b (z stream)
513 (macrolet ((frob (form) `(progn ,form ,bug222-b)))
514 (frob (format stream "~D~%" bug222-b)))))
515 ;;; and it would be nice (though not specified by ANSI) if the answer
517 (let ((x (make-string-output-stream)))
518 ;; not specified by ANSI
519 (assert (= (bug222-b t x) 3))
521 (assert (char= (char (get-output-stream-string x) 0) #\1)))
523 ;;; REINITIALIZE-INSTANCE, in the ctor optimization, wasn't checking
524 ;;; for invalid initargs where it should:
525 (defclass class234 () ())
526 (defclass subclass234 (class234) ())
529 (reinitialize-instance (make-instance 'class234) :dummy 0))
531 (reinitialize-instance (make-instance 'subclass234) :dummy 0))
532 (assert (raises-error? (bug-234) program-error))
533 (defmethod shared-initialize :after ((i class234) slots &key dummy)
535 (assert (typep (subbug-234) 'subclass234))
537 ;; once for MAKE-INSTANCE, once for REINITIALIZE-INSTANCE
540 ;;; also, some combinations of MAKE-INSTANCE and subclassing missed
541 ;;; new methods (Gerd Moellmann sbcl-devel 2002-12-29):
542 (defclass class234-b1 () ())
543 (defclass class234-b2 (class234-b1) ())
544 (defvar *bug234-b* 0)
546 (make-instance 'class234-b2))
549 (assert (= *bug234-b* 0))
550 (defmethod initialize-instance :before ((x class234-b1) &rest args)
551 (declare (ignore args))
554 (assert (= *bug234-b* 1))
556 ;;; we should be able to make classes with uninterned names:
557 (defclass #:class-with-uninterned-name () ())
559 ;;; SLOT-MISSING should be called when there are missing slots.
560 (defclass class-with-all-slots-missing () ())
561 (defmethod slot-missing (class (o class-with-all-slots-missing)
565 (assert (eq (slot-value (make-instance 'class-with-all-slots-missing) 'foo)
567 (assert (eq (funcall (lambda (x) (slot-value x 'bar))
568 (make-instance 'class-with-all-slots-missing))
570 (assert (eq (funcall (lambda (x) (setf (slot-value x 'baz) 'baz))
571 (make-instance 'class-with-all-slots-missing))
575 (sb-ext:quit :unix-status 104)