0.8.0.70:
[sbcl/lichteblau.git] / tests / clos.impure.lisp
bloba94d8423ae89b7d99df0c45191519408cd0198c7
1 ;;;; miscellaneous side-effectful tests of CLOS
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
5 ;;;;
6 ;;;; While most of SBCL is derived from the CMU CL system, the test
7 ;;;; files (like this one) were written from scratch after the fork
8 ;;;; from CMU CL.
9 ;;;;
10 ;;;; This software is in the public domain and is provided with
11 ;;;; absolutely no warranty. See the COPYING and CREDITS files for
12 ;;;; more information.
14 (load "assertoid.lisp")
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))
25 (+ (struct-a-x a)
26 (struct-a-y a)))
27 (defgeneric jiggle (arg))
28 (defmethod jiggle ((a struct-a))
29 (- (struct-a-x a)
30 (struct-a-y a)))
31 (defmethod jiggle ((b struct-b))
32 (- (struct-b-x b)
33 (struct-b-y b)
34 (struct-b-z 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)
42 (unless (minusp 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))
51 (zut-n-a-m 1 2 3)
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))))
66 (assert (expect-error
67 (macroexpand-1
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
79 ;;;
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)
84 (if x t nil))
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)
89 (compile nil
90 `(lambda ()
91 ,expr))
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
100 ;; after all?
101 ;;(unless expected-failure-p
102 ;; (assert (not warnings-p)))
103 (assert (eq (coerce-to-boolean failure-p) expected-failure-p))))
104 (like (expr)
105 (%like-or-dislike expr nil))
106 (dislike (expr)
107 (%like-or-dislike expr t)))
108 ;; basic sanity
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)))
123 ;; forbidden &AUX
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))
144 'structure-class))
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))
156 'integer))
157 (defmethod born-to-be-redefined ((x real))
158 '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))
164 'integer))
165 (defmethod born-to-be-redefined ((x integer))
166 'int)
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
182 ;;; suite):
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)))
194 (let ((foo (make-instance 'class-with-slots
195 :a-slot 1
196 :b-slot 2
197 :c-slot 3)))
198 (let ((bar (change-class foo 'class-with-slots)))
199 (assert (= (a-slot bar) 1))
200 (assert (= (b-slot bar) 2))
201 (assert (= (c-slot bar) 3))))
203 ;;; some more CHANGE-CLASS testing, now that we have an ANSI-compliant
204 ;;; version (thanks to Espen Johnsen)
205 (defclass from-class ()
206 ((foo :initarg :foo :accessor foo)))
207 (defclass to-class ()
208 ((foo :initarg :foo :accessor foo)
209 (bar :initarg :bar :accessor bar)))
210 (let* ((from (make-instance 'from-class :foo 1))
211 (to (change-class from 'to-class :bar 2)))
212 (assert (= (foo to) 1))
213 (assert (= (bar to) 2)))
215 ;;; Until Pierre Mai's patch (sbcl-devel 2002-06-18, merged in
216 ;;; sbcl-0.7.4.39) the :MOST-SPECIFIC-LAST option had no effect.
217 (defgeneric bug180 (x)
218 (:method-combination list :most-specific-last))
219 (defmethod bug180 list ((x number))
220 'number)
221 (defmethod bug180 list ((x fixnum))
222 'fixnum)
223 (assert (equal (bug180 14) '(number fixnum)))
225 ;;; printing a structure class should not loop indefinitely (or cause
226 ;;; a stack overflow):
227 (defclass test-printing-structure-class ()
228 ((slot :initarg :slot))
229 (:metaclass structure-class))
230 (print (make-instance 'test-printing-structure-class :slot 2))
232 ;;; structure-classes should behave nicely when subclassed
233 (defclass super-structure ()
234 ((a :initarg :a :accessor a-accessor)
235 (b :initform 2 :reader b-reader))
236 (:metaclass structure-class))
237 (defclass sub-structure (super-structure)
238 ((c :initarg :c :writer c-writer :accessor c-accessor))
239 (:metaclass structure-class))
240 (let ((foo (make-instance 'sub-structure :a 1 :c 3)))
241 (assert (= (a-accessor foo) 1))
242 (assert (= (b-reader foo) 2))
243 (assert (= (c-accessor foo) 3))
244 (setf (a-accessor foo) 4)
245 (c-writer 5 foo)
246 (assert (= (a-accessor foo) 4))
247 (assert (= (c-accessor foo) 5)))
249 ;;; At least as of sbcl-0.7.4, PCL has code to support a special
250 ;;; encoding of effective method functions for slot accessors as
251 ;;; FIXNUMs. Given this special casing, it'd be easy for slot accessor
252 ;;; functions to get broken in special ways even though ordinary
253 ;;; generic functions work. As of sbcl-0.7.4 we didn't have any tests
254 ;;; for that possibility. Now we have a few tests:
255 (defclass fish ()
256 ((fin :reader ffin :writer ffin!)
257 (tail :reader ftail :writer ftail!)))
258 (defvar *fish* (make-instance 'fish))
259 (ffin! 'triangular-fin *fish*)
260 (defclass cod (fish) ())
261 (defvar *cod* (make-instance 'cod))
262 (defparameter *clos-dispatch-side-fx* (make-array 0 :fill-pointer 0))
263 (defmethod ffin! (new-fin (cod cod))
264 (format t "~&about to set ~S fin to ~S~%" cod new-fin)
265 (vector-push-extend '(cod) *clos-dispatch-side-fx*)
266 (prog1
267 (call-next-method)
268 (format t "~&done setting ~S fin to ~S~%" cod new-fin)))
269 (defmethod ffin! :before (new-fin (cod cod))
270 (vector-push-extend '(:before cod) *clos-dispatch-side-fx*)
271 (format t "~&exploring the CLOS dispatch zoo with COD fins~%"))
272 (ffin! 'almost-triang-fin *cod*)
273 (assert (eq (ffin *cod*) 'almost-triang-fin))
274 (assert (equalp #((:before cod) (cod)) *clos-dispatch-side-fx*))
276 ;;; Until sbcl-0.7.6.21, the long form of DEFINE-METHOD-COMBINATION
277 ;;; ignored its options; Gerd Moellmann found and fixed the problem
278 ;;; for cmucl (cmucl-imp 2002-06-18).
279 (define-method-combination test-mc (x)
280 ;; X above being a method-group-specifier
281 ((primary () :required t))
282 `(call-method ,(first primary)))
284 (defgeneric gf (obj)
285 (:method-combination test-mc 1))
287 (defmethod gf (obj)
288 obj)
290 ;;; Until sbcl-0.7.7.20, some conditions weren't being signalled, and
291 ;;; some others were of the wrong type:
292 (macrolet ((assert-program-error (form)
293 `(multiple-value-bind (value error)
294 (ignore-errors ,form)
295 (assert (null value))
296 (assert (typep error 'program-error)))))
297 (assert-program-error (defclass foo001 () (a b a)))
298 (assert-program-error (defclass foo002 ()
299 (a b)
300 (:default-initargs x 'a x 'b)))
301 (assert-program-error (defclass foo003 ()
302 ((a :allocation :class :allocation :class))))
303 (assert-program-error (defclass foo004 ()
304 ((a :silly t))))
305 ;; and some more, found by Wolfhard Buss and fixed for cmucl by Gerd
306 ;; Moellmann in sbcl-0.7.8.x:
307 (assert-program-error (progn
308 (defmethod odd-key-args-checking (&key (key 42)) key)
309 (odd-key-args-checking 3)))
310 (assert (= (odd-key-args-checking) 42))
311 (assert (eq (odd-key-args-checking :key t) t))
312 ;; yet some more, fixed in sbcl-0.7.9.xx
313 (assert-program-error (defclass foo005 ()
314 (:metaclass sb-pcl::funcallable-standard-class)
315 (:metaclass 1)))
316 (assert-program-error (defclass foo006 ()
317 ((a :reader (setf a)))))
318 (assert-program-error (defclass foo007 ()
319 ((a :initarg 1))))
320 (assert-program-error (defclass foo008 ()
321 (a :initarg :a)
322 (:default-initargs :a 1)
323 (:default-initargs :a 2)))
324 ;; and also BUG 47d, fixed in sbcl-0.8alpha.0.26
325 (assert-program-error (defgeneric if (x)))
326 ;; DEFCLASS should detect an error if slot names aren't suitable as
327 ;; variable names:
328 (assert-program-error (defclass foo009 ()
329 ((:a :initarg :a))))
330 (assert-program-error (defclass foo010 ()
331 (("a" :initarg :a))))
332 (assert-program-error (defclass foo011 ()
333 ((#1a() :initarg :a))))
334 (assert-program-error (defclass foo012 ()
335 ((t :initarg :t))))
336 (assert-program-error (defclass foo013 () ("a")))
337 ;; specialized lambda lists have certain restrictions on ordering,
338 ;; repeating keywords, and the like:
339 (assert-program-error (defmethod foo014 ((foo t) &rest) nil))
340 (assert-program-error (defmethod foo015 ((foo t) &rest x y) nil))
341 (assert-program-error (defmethod foo016 ((foo t) &allow-other-keys) nil))
342 (assert-program-error (defmethod foo017 ((foo t)
343 &optional x &optional y) nil))
344 (assert-program-error (defmethod foo018 ((foo t) &rest x &rest y) nil))
345 (assert-program-error (defmethod foo019 ((foo t) &rest x &optional y) nil))
346 (assert-program-error (defmethod foo020 ((foo t) &key x &optional y) nil))
347 (assert-program-error (defmethod foo021 ((foo t) &key x &rest y) nil)))
349 ;;; DOCUMENTATION's argument-precedence-order wasn't being faithfully
350 ;;; preserved through the bootstrap process until sbcl-0.7.8.39.
351 ;;; (thanks to Gerd Moellmann)
352 (let ((answer (documentation '+ 'function)))
353 (assert (stringp answer))
354 (defmethod documentation ((x (eql '+)) y) "WRONG")
355 (assert (string= (documentation '+ 'function) answer)))
357 ;;; only certain declarations are permitted in DEFGENERIC
358 (macrolet ((assert-program-error (form)
359 `(multiple-value-bind (value error)
360 (ignore-errors ,form)
361 (assert (null value))
362 (assert (typep error 'program-error)))))
363 (assert-program-error (defgeneric bogus-declaration (x)
364 (declare (special y))))
365 (assert-program-error (defgeneric bogus-declaration2 (x)
366 (declare (notinline concatenate)))))
367 ;;; CALL-NEXT-METHOD should call NO-NEXT-METHOD if there is no next
368 ;;; method.
369 (defmethod no-next-method-test ((x integer)) (call-next-method))
370 (assert (null (ignore-errors (no-next-method-test 1))))
371 (defmethod no-next-method ((g (eql #'no-next-method-test)) m &rest args)
372 'success)
373 (assert (eq (no-next-method-test 1) 'success))
374 (assert (null (ignore-errors (no-next-method-test 'foo))))
376 ;;; regression test for bug 176, following a fix that seems
377 ;;; simultaneously to fix 140 while not exposing 176 (by Gerd
378 ;;; Moellmann, merged in sbcl-0.7.9.12).
379 (dotimes (i 10)
380 (let ((lastname (intern (format nil "C176-~D" (1- i))))
381 (name (intern (format nil "C176-~D" i))))
382 (eval `(defclass ,name
383 (,@(if (= i 0) nil (list lastname)))
384 ()))
385 (eval `(defmethod initialize-instance :after ((x ,name) &rest any)
386 (declare (ignore any))))))
387 (defclass b176 () (aslot-176))
388 (defclass c176-0 (b176) ())
389 (assert (= 1 (setf (slot-value (make-instance 'c176-9) 'aslot-176) 1)))
391 ;;; DEFINE-METHOD-COMBINATION was over-eager at checking for duplicate
392 ;;; primary methods:
393 (define-method-combination dmc-test-mc (&optional (order :most-specific-first))
394 ((around (:around))
395 (primary (dmc-test-mc) :order order :required t))
396 (let ((form (if (rest primary)
397 `(and ,@(mapcar #'(lambda (method)
398 `(call-method ,method))
399 primary))
400 `(call-method ,(first primary)))))
401 (if around
402 `(call-method ,(first around)
403 (,@(rest around)
404 (make-method ,form)))
405 form)))
407 (defgeneric dmc-test-mc (&key k)
408 (:method-combination dmc-test-mc))
410 (defmethod dmc-test-mc dmc-test-mc (&key k)
413 (dmc-test-mc :k 1)
414 ;;; While I'm at it, DEFINE-METHOD-COMBINATION is defined to return
415 ;;; the NAME argument, not some random method object. So:
416 (assert (eq (define-method-combination dmc-test-return-foo)
417 'dmc-test-return-foo))
418 (assert (eq (define-method-combination dmc-test-return-bar :operator and)
419 'dmc-test-return-bar))
420 (assert (eq (define-method-combination dmc-test-return
421 (&optional (order :most-specific-first))
422 ((around (:around))
423 (primary (dmc-test-return) :order order :required t))
424 (let ((form (if (rest primary)
425 `(and ,@(mapcar #'(lambda (method)
426 `(call-method ,method))
427 primary))
428 `(call-method ,(first primary)))))
429 (if around
430 `(call-method ,(first around)
431 (,@(rest around)
432 (make-method ,form)))
433 form)))
434 'dmc-test-return))
436 ;;; DEFMETHOD should signal an ERROR if an incompatible lambda list is
437 ;;; given:
438 (defmethod incompatible-ll-test-1 (x) x)
439 (assert (raises-error? (defmethod incompatible-ll-test-1 (x y) y)))
440 (assert (raises-error? (defmethod incompatible-ll-test-1 (x &rest y) y)))
441 ;;; Sneakily using a bit of MOPness to check some consistency
442 (assert (= (length
443 (sb-pcl:generic-function-methods #'incompatible-ll-test-1)) 1))
445 (defmethod incompatible-ll-test-2 (x &key bar) bar)
446 (assert (raises-error? (defmethod incompatible-ll-test-2 (x) x)))
447 (defmethod incompatible-ll-test-2 (x &rest y) y)
448 (assert (= (length
449 (sb-pcl:generic-function-methods #'incompatible-ll-test-2)) 1))
450 (defmethod incompatible-ll-test-2 ((x integer) &key bar) bar)
451 (assert (= (length
452 (sb-pcl:generic-function-methods #'incompatible-ll-test-2)) 2))
453 (assert (equal (incompatible-ll-test-2 t 1 2) '(1 2)))
454 (assert (eq (incompatible-ll-test-2 1 :bar 'yes) 'yes))
456 ;;; Attempting to instantiate classes with forward references in their
457 ;;; CPL should signal errors (FIXME: of what type?)
458 (defclass never-finished-class (this-one-unfinished-too) ())
459 (multiple-value-bind (result error)
460 (ignore-errors (make-instance 'never-finished-class))
461 (assert (null result))
462 (assert (typep error 'error)))
463 (multiple-value-bind (result error)
464 (ignore-errors (make-instance 'this-one-unfinished-too))
465 (assert (null result))
466 (assert (typep error 'error)))
468 ;;; Classes with :ALLOCATION :CLASS slots should be subclassable (and
469 ;;; weren't for a while in sbcl-0.7.9.xx)
470 (defclass superclass-with-slot ()
471 ((a :allocation :class)))
472 (defclass subclass-for-class-allocation (superclass-with-slot) ())
473 (make-instance 'subclass-for-class-allocation)
475 ;;; bug #136: CALL-NEXT-METHOD was being a little too lexical,
476 ;;; resulting in failure in the following:
477 (defmethod call-next-method-lexical-args ((x integer))
479 (defmethod call-next-method-lexical-args :around ((x integer))
480 (let ((x (1+ x)))
481 (call-next-method)))
482 (assert (= (call-next-method-lexical-args 3) 3))
484 ;;; DEFINE-METHOD-COMBINATION with arguments was hopelessly broken
485 ;;; until 0.7.9.5x
486 (defvar *d-m-c-args-test* nil)
487 (define-method-combination progn-with-lock ()
488 ((methods ()))
489 (:arguments object)
490 `(unwind-protect
491 (progn (lock (object-lock ,object))
492 ,@(mapcar #'(lambda (method)
493 `(call-method ,method))
494 methods))
495 (unlock (object-lock ,object))))
496 (defun object-lock (obj)
497 (push "object-lock" *d-m-c-args-test*)
498 obj)
499 (defun unlock (obj)
500 (push "unlock" *d-m-c-args-test*)
501 obj)
502 (defun lock (obj)
503 (push "lock" *d-m-c-args-test*)
504 obj)
505 (defgeneric d-m-c-args-test (x)
506 (:method-combination progn-with-lock))
507 (defmethod d-m-c-args-test ((x symbol))
508 (push "primary" *d-m-c-args-test*))
509 (defmethod d-m-c-args-test ((x number))
510 (error "foo"))
511 (assert (equal (d-m-c-args-test t) '("primary" "lock" "object-lock")))
512 (assert (equal *d-m-c-args-test*
513 '("unlock" "object-lock" "primary" "lock" "object-lock")))
514 (setf *d-m-c-args-test* nil)
515 (ignore-errors (d-m-c-args-test 1))
516 (assert (equal *d-m-c-args-test*
517 '("unlock" "object-lock" "lock" "object-lock")))
519 ;;; The walker (on which DEFMETHOD depended) didn't know how to handle
520 ;;; SYMBOL-MACROLET properly. In fact, as of sbcl-0.7.10.20 it still
521 ;;; doesn't, but it does well enough to compile the following without
522 ;;; error (the problems remain in asking for a complete macroexpansion
523 ;;; of an arbitrary form).
524 (symbol-macrolet ((x 1))
525 (defmethod bug222 (z)
526 (macrolet ((frob (form) `(progn ,form ,x)))
527 (frob (print x)))))
528 (assert (= (bug222 t) 1))
530 ;;; also, a test case to guard against bogus environment hacking:
531 (eval-when (:compile-toplevel :load-toplevel :execute)
532 (setq bug222-b 3))
533 ;;; this should at the least compile:
534 (let ((bug222-b 1))
535 (defmethod bug222-b (z stream)
536 (macrolet ((frob (form) `(progn ,form ,bug222-b)))
537 (frob (format stream "~D~%" bug222-b)))))
538 ;;; and it would be nice (though not specified by ANSI) if the answer
539 ;;; were as follows:
540 (let ((x (make-string-output-stream)))
541 ;; not specified by ANSI
542 (assert (= (bug222-b t x) 3))
543 ;; specified.
544 (assert (char= (char (get-output-stream-string x) 0) #\1)))
546 ;;; REINITIALIZE-INSTANCE, in the ctor optimization, wasn't checking
547 ;;; for invalid initargs where it should:
548 (defclass class234 () ())
549 (defclass subclass234 (class234) ())
550 (defvar *bug234* 0)
551 (defun bug-234 ()
552 (reinitialize-instance (make-instance 'class234) :dummy 0))
553 (defun subbug-234 ()
554 (reinitialize-instance (make-instance 'subclass234) :dummy 0))
555 (assert (raises-error? (bug-234) program-error))
556 (defmethod shared-initialize :after ((i class234) slots &key dummy)
557 (incf *bug234*))
558 (assert (typep (subbug-234) 'subclass234))
559 (assert (= *bug234*
560 ;; once for MAKE-INSTANCE, once for REINITIALIZE-INSTANCE
563 ;;; also, some combinations of MAKE-INSTANCE and subclassing missed
564 ;;; new methods (Gerd Moellmann sbcl-devel 2002-12-29):
565 (defclass class234-b1 () ())
566 (defclass class234-b2 (class234-b1) ())
567 (defvar *bug234-b* 0)
568 (defun bug234-b ()
569 (make-instance 'class234-b2))
570 (compile 'bug234-b)
571 (bug234-b)
572 (assert (= *bug234-b* 0))
573 (defmethod initialize-instance :before ((x class234-b1) &rest args)
574 (declare (ignore args))
575 (incf *bug234-b*))
576 (bug234-b)
577 (assert (= *bug234-b* 1))
579 ;;; we should be able to make classes with uninterned names:
580 (defclass #:class-with-uninterned-name () ())
582 ;;; SLOT-MISSING should be called when there are missing slots.
583 (defclass class-with-all-slots-missing () ())
584 (defmethod slot-missing (class (o class-with-all-slots-missing)
585 slot-name op
586 &optional new-value)
588 (assert (eq (slot-value (make-instance 'class-with-all-slots-missing) 'foo)
589 'slot-value))
590 (assert (eq (funcall (lambda (x) (slot-value x 'bar))
591 (make-instance 'class-with-all-slots-missing))
592 'slot-value))
593 (assert (eq (funcall (lambda (x) (setf (slot-value x 'baz) 'baz))
594 (make-instance 'class-with-all-slots-missing))
595 'setf))
597 ;;; we should be able to specialize on anything that names a class.
598 (defclass name-for-class () ())
599 (defmethod something-that-specializes ((x name-for-class)) 1)
600 (setf (find-class 'other-name-for-class) (find-class 'name-for-class))
601 (defmethod something-that-specializes ((x other-name-for-class)) 2)
602 (assert (= (something-that-specializes (make-instance 'name-for-class)) 2))
603 (assert (= (something-that-specializes (make-instance 'other-name-for-class))
606 ;;; more forward referenced classes stuff
607 (defclass frc-1 (frc-2) ())
608 (assert (subtypep 'frc-1 (find-class 'frc-2)))
609 (assert (subtypep (find-class 'frc-1) 'frc-2))
610 (assert (not (subtypep (find-class 'frc-2) 'frc-1)))
611 (defclass frc-2 (frc-3) ((a :initarg :a)))
612 (assert (subtypep 'frc-1 (find-class 'frc-3)))
613 (defclass frc-3 () ())
614 (assert (typep (make-instance 'frc-1 :a 2) (find-class 'frc-1)))
615 (assert (typep (make-instance 'frc-2 :a 3) (find-class 'frc-2)))
617 ;;; check that we can define classes with two slots of different names
618 ;;; (even if it STYLE-WARNs).
619 (defclass odd-name-class ()
620 ((name :initarg :name)
621 (cl-user::name :initarg :name2)))
622 (let ((x (make-instance 'odd-name-class :name 1 :name2 2)))
623 (assert (= (slot-value x 'name) 1))
624 (assert (= (slot-value x 'cl-user::name) 2)))
626 ;;; ALLOCATE-INSTANCE should work on structures, even if defined by
627 ;;; DEFSTRUCT (and not DEFCLASS :METACLASS STRUCTURE-CLASS).
628 (defstruct allocatable-structure a)
629 (assert (typep (allocate-instance (find-class 'allocatable-structure))
630 'allocatable-structure))
632 ;;; Bug found by Paul Dietz when devising CPL tests: somewhat
633 ;;; amazingly, calls to CPL would work a couple of times, and then
634 ;;; start returning NIL. A fix was found (relating to the
635 ;;; applicability of constant-dfun optimization) by Gerd Moellmann.
636 (defgeneric cpl (x)
637 (:method-combination list)
638 (:method list ((x broadcast-stream)) 'broadcast-stream)
639 (:method list ((x integer)) 'integer)
640 (:method list ((x number)) 'number)
641 (:method list ((x stream)) 'stream)
642 (:method list ((x structure-object)) 'structure-object))
643 (assert (equal (cpl 0) '(integer number)))
644 (assert (equal (cpl 0) '(integer number)))
645 (assert (equal (cpl 0) '(integer number)))
646 (assert (equal (cpl 0) '(integer number)))
647 (assert (equal (cpl 0) '(integer number)))
648 (assert (equal (cpl (make-broadcast-stream))
649 '(broadcast-stream stream structure-object)))
650 (assert (equal (cpl (make-broadcast-stream))
651 '(broadcast-stream stream structure-object)))
652 (assert (equal (cpl (make-broadcast-stream))
653 '(broadcast-stream stream structure-object)))
655 ;;; Bug in CALL-NEXT-METHOD: assignment to the method's formal
656 ;;; parameters shouldn't affect the arguments to the next method for a
657 ;;; no-argument call to CALL-NEXT-METHOD
658 (defgeneric cnm-assignment (x)
659 (:method (x) x)
660 (:method ((x integer)) (setq x 3)
661 (list x (call-next-method) (call-next-method x))))
662 (assert (equal (cnm-assignment 1) '(3 1 3)))
664 ;;;; success
665 (sb-ext:quit :unix-status 104)