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.
18 ;;; It should be possible to do DEFGENERIC and DEFMETHOD referring to
19 ;;; structure types defined earlier in the file.
20 (defstruct struct-a x y
)
21 (defstruct struct-b x y z
)
22 (defmethod wiggle ((a struct-a
))
25 (defgeneric jiggle
(arg))
26 (defmethod jiggle ((a struct-a
))
29 (defmethod jiggle ((b struct-b
))
33 (assert (= (wiggle (make-struct-a :x
6 :y
5))
34 (jiggle (make-struct-b :x
19 :y
6 :z
2))))
36 ;;; Compiling DEFGENERIC should prevent "undefined function" style
37 ;;; warnings from code within the same file.
38 (defgeneric gf-defined-in-this-file
(x y
))
39 (defun function-using-gf-defined-in-this-file (x y n
)
41 (gf-defined-in-this-file x y
)))
43 ;;; Until Martin Atzmueller ported Pierre Mai's CMU CL fixes in
44 ;;; sbcl-0.6.12.25, the implementation of NO-APPLICABLE-METHOD was
45 ;;; broken in such a way that the code here would signal an error.
46 (defgeneric zut-n-a-m
(a b c
))
47 (defmethod no-applicable-method ((zut-n-a-m (eql #'zut-n-a-m
)) &rest args
)
48 (format t
"~&No applicable method for ZUT-N-A-M ~S, yet.~%" args
))
51 ;;; bug reported and fixed by Alexey Dejneka sbcl-devel 2001-09-10:
52 ;;; This DEFGENERIC shouldn't cause an error.
53 (defgeneric ad-gf
(a) (:method
:around
(x) x
))
55 ;;; DEFGENERIC and DEFMETHOD shouldn't accept &REST when it's not
56 ;;; followed by a variable:
57 ;;; e.g. (DEFMETHOD FOO ((X T) &REST) NIL) should signal an error.
58 (eval-when (:load-toplevel
:compile-toplevel
:execute
)
59 (defmacro expect-error
(&body body
)
60 `(multiple-value-bind (res condition
)
61 (ignore-errors (progn ,@body
))
62 (declare (ignore res
))
63 (typep condition
'error
))))
66 '(defmethod foo0 ((x t
) &rest
) nil
))))
67 (assert (expect-error (defgeneric foo1
(x &rest
))))
68 (assert (expect-error (defgeneric foo2
(x a
&rest
))))
69 (defgeneric foo3
(x &rest y
))
70 (defmethod foo3 ((x t
) &rest y
) nil
)
71 (defmethod foo4 ((x t
) &key y
&rest z
) nil
)
72 (defgeneric foo4
(x &rest z
&key y
))
73 (assert (expect-error (defgeneric foo5
(x &rest
))))
74 (assert (expect-error (macroexpand-1 '(defmethod foo6 (x &rest
)))))
76 ;;; more lambda-list checking
78 ;;; DEFGENERIC lambda lists are subject to various limitations, as per
79 ;;; section 3.4.2 of the ANSI spec. Since Alexey Dejneka's patch for
80 ;;; bug 191-b ca. sbcl-0.7.22, these limitations should be enforced.
81 (labels ((coerce-to-boolean (x)
83 (%like-or-dislike
(expr expected-failure-p
)
84 (declare (type boolean expected-failure-p
))
85 (format t
"~&trying ~S~%" expr
)
86 (multiple-value-bind (fun warnings-p failure-p
)
90 (declare (ignore fun
))
91 ;; In principle the constraint on WARNINGS-P below seems
92 ;; reasonable, but in practice we get warnings about
93 ;; undefined functions from the DEFGENERICs, apparently
94 ;; because the DECLAIMs which ordinarily prevent such
95 ;; warnings don't take effect because EVAL-WHEN
96 ;; (:COMPILE-TOPLEVEL) loses its magic when compiled
97 ;; within a LAMBDA. So maybe we can't test WARNINGS-P
99 ;;(unless expected-failure-p
100 ;; (assert (not warnings-p)))
101 (assert (eq (coerce-to-boolean failure-p
) expected-failure-p
))))
103 (%like-or-dislike expr nil
))
105 (%like-or-dislike expr t
)))
107 (dislike '(defgeneric gf-for-ll-test-0
("a" #p
"b")))
108 (like '(defgeneric gf-for-ll-test-1
()))
109 (like '(defgeneric gf-for-ll-test-2
(x)))
110 ;; forbidden default or supplied-p for &OPTIONAL or &KEY arguments
111 (dislike '(defgeneric gf-for-ll-test-3
(x &optional
(y 0))))
112 (like '(defgeneric gf-for-ll-test-4
(x &optional y
)))
113 (dislike '(defgeneric gf-for-ll-test-5
(x y
&key
(z :z z-p
))))
114 (like '(defgeneric gf-for-ll-test-6
(x y
&key z
)))
115 (dislike '(defgeneric gf-for-ll-test-7
(x &optional
(y 0) &key z
)))
116 (like '(defgeneric gf-for-ll-test-8
(x &optional y
&key z
)))
117 (dislike '(defgeneric gf-for-ll-test-9
(x &optional y
&key
(z :z
))))
118 (like '(defgeneric gf-for-ll-test-10
(x &optional y
&key z
)))
119 (dislike '(defgeneric gf-for-ll-test-11
(&optional
&key
(k :k k-p
))))
120 (like '(defgeneric gf-for-ll-test-12
(&optional
&key k
)))
122 (dislike '(defgeneric gf-for-ll-test-13
(x y z
&optional a
&aux g h
)))
123 (like '(defgeneric gf-for-ll-test-14
(x y z
&optional a
)))
124 (dislike '(defgeneric gf-for-ll-test-bare-aux-1
(x &aux
)))
125 (like '(defgeneric gf-for-ll-test-bare-aux-2
(x)))
126 ;; also can't use bogoDEFMETHODish type-qualifier-ish decorations
127 ;; on required arguments
128 (dislike '(defgeneric gf-for-11-test-15
((arg t
))))
129 (like '(defgeneric gf-for-11-test-16
(arg))))
131 ;;; structure-class tests setup
132 (defclass structure-class-foo1
() () (:metaclass cl
:structure-class
))
133 (defclass structure-class-foo2
(structure-class-foo1)
134 () (:metaclass cl
:structure-class
))
136 ;;; standard-class tests setup
137 (defclass standard-class-foo1
() () (:metaclass cl
:standard-class
))
138 (defclass standard-class-foo2
(standard-class-foo1)
139 () (:metaclass cl
:standard-class
))
141 (assert (typep (class-of (make-instance 'structure-class-foo1
))
143 (assert (typep (make-instance 'structure-class-foo1
) 'structure-class-foo1
))
144 (assert (typep (make-instance 'standard-class-foo1
) 'standard-class-foo1
))
146 ;;; DEFGENERIC's blow-away-old-methods behavior is specified to have
147 ;;; special hacks to distinguish between defined-with-DEFGENERIC-:METHOD
148 ;;; methods and defined-with-DEFMETHOD methods, so that reLOADing
149 ;;; DEFGENERIC-containing files does the right thing instead of
150 ;;; randomly slicing your generic functions. (APD made this work
151 ;;; in sbcl-0.7.0.2.)
152 (defgeneric born-to-be-redefined
(x)
153 (:method
((x integer
))
155 (defmethod born-to-be-redefined ((x real
))
157 (assert (eq (born-to-be-redefined 1) 'integer
))
158 (defgeneric born-to-be-redefined
(x))
159 (assert (eq (born-to-be-redefined 1) 'real
)) ; failed until sbcl-0.7.0.2
160 (defgeneric born-to-be-redefined
(x)
161 (:method
((x integer
))
163 (defmethod born-to-be-redefined ((x integer
))
165 (assert (eq (born-to-be-redefined 1) 'int
))
166 (defgeneric born-to-be-redefined
(x))
167 (assert (eq (born-to-be-redefined 1) 'int
))
169 ;;; In the removal of ITERATE from SB-PCL, a bug was introduced
170 ;;; preventing forward-references and also change-class (which
171 ;;; forward-references used interally) from working properly. One
172 ;;; symptom was reported by Brian Spilsbury (sbcl-devel 2002-04-08),
173 ;;; and another on IRC by Dan Barlow simultaneously. Better check
174 ;;; that it doesn't happen again.
176 ;;; First, the forward references:
179 ;;; Then change-class
180 (defclass class-with-slots
()
181 ((a-slot :initarg
:a-slot
:accessor a-slot
)
182 (b-slot :initarg
:b-slot
:accessor b-slot
)
183 (c-slot :initarg
:c-slot
:accessor c-slot
)))
184 (let ((foo (make-instance 'class-with-slots
188 (let ((bar (change-class foo
'class-with-slots
)))
189 (assert (= (a-slot bar
) 1))
190 (assert (= (b-slot bar
) 2))
191 (assert (= (c-slot bar
) 3))))
193 ;;; some more CHANGE-CLASS testing, now that we have an ANSI-compliant
194 ;;; version (thanks to Espen Johnsen)
195 (defclass from-class
()
196 ((foo :initarg
:foo
:accessor foo
)))
197 (defclass to-class
()
198 ((foo :initarg
:foo
:accessor foo
)
199 (bar :initarg
:bar
:accessor bar
)))
200 (let* ((from (make-instance 'from-class
:foo
1))
201 (to (change-class from
'to-class
:bar
2)))
202 (assert (= (foo to
) 1))
203 (assert (= (bar to
) 2)))
205 ;;; Until Pierre Mai's patch (sbcl-devel 2002-06-18, merged in
206 ;;; sbcl-0.7.4.39) the :MOST-SPECIFIC-LAST option had no effect.
207 (defgeneric bug180
(x)
208 (:method-combination list
:most-specific-last
))
209 (defmethod bug180 list
((x number
))
211 (defmethod bug180 list
((x fixnum
))
213 (assert (equal (bug180 14) '(number fixnum
)))
215 ;;; printing a structure class should not loop indefinitely (or cause
216 ;;; a stack overflow):
217 (defclass test-printing-structure-class
()
218 ((slot :initarg
:slot
))
219 (:metaclass structure-class
))
220 (print (make-instance 'test-printing-structure-class
:slot
2))
222 ;;; structure-classes should behave nicely when subclassed
223 (defclass super-structure
()
224 ((a :initarg
:a
:accessor a-accessor
)
225 (b :initform
2 :reader b-reader
))
226 (:metaclass structure-class
))
227 (defclass sub-structure
(super-structure)
228 ((c :initarg
:c
:writer c-writer
:accessor c-accessor
))
229 (:metaclass structure-class
))
230 (let ((foo (make-instance 'sub-structure
:a
1 :c
3)))
231 (assert (= (a-accessor foo
) 1))
232 (assert (= (b-reader foo
) 2))
233 (assert (= (c-accessor foo
) 3))
234 (setf (a-accessor foo
) 4)
236 (assert (= (a-accessor foo
) 4))
237 (assert (= (c-accessor foo
) 5)))
239 ;;; At least as of sbcl-0.7.4, PCL has code to support a special
240 ;;; encoding of effective method functions for slot accessors as
241 ;;; FIXNUMs. Given this special casing, it'd be easy for slot accessor
242 ;;; functions to get broken in special ways even though ordinary
243 ;;; generic functions work. As of sbcl-0.7.4 we didn't have any tests
244 ;;; for that possibility. Now we have a few tests:
246 ((fin :reader ffin
:writer ffin
!)
247 (tail :reader ftail
:writer ftail
!)))
248 (defvar *fish
* (make-instance 'fish
))
249 (ffin! 'triangular-fin
*fish
*)
250 (defclass cod
(fish) ())
251 (defvar *cod
* (make-instance 'cod
))
252 (defparameter *clos-dispatch-side-fx
* (make-array 0 :fill-pointer
0))
253 (defmethod ffin! (new-fin (cod cod
))
254 (format t
"~&about to set ~S fin to ~S~%" cod new-fin
)
255 (vector-push-extend '(cod) *clos-dispatch-side-fx
*)
258 (format t
"~&done setting ~S fin to ~S~%" cod new-fin
)))
259 (defmethod ffin! :before
(new-fin (cod cod
))
260 (vector-push-extend '(:before cod
) *clos-dispatch-side-fx
*)
261 (format t
"~&exploring the CLOS dispatch zoo with COD fins~%"))
262 (ffin! 'almost-triang-fin
*cod
*)
263 (assert (eq (ffin *cod
*) 'almost-triang-fin
))
264 (assert (equalp #((:before cod
) (cod)) *clos-dispatch-side-fx
*))
266 ;;; Until sbcl-0.7.6.21, the long form of DEFINE-METHOD-COMBINATION
267 ;;; ignored its options; Gerd Moellmann found and fixed the problem
268 ;;; for cmucl (cmucl-imp 2002-06-18).
269 (define-method-combination test-mc
(x)
270 ;; X above being a method-group-specifier
271 ((primary () :required t
))
272 `(call-method ,(first primary
)))
275 (:method-combination test-mc
1))
280 ;;; Until sbcl-0.7.7.20, some conditions weren't being signalled, and
281 ;;; some others were of the wrong type:
282 (macrolet ((assert-program-error (form)
283 `(multiple-value-bind (value error
)
284 (ignore-errors ,form
)
285 (assert (null value
))
286 (assert (typep error
'program-error
)))))
287 (assert-program-error (defclass foo001
() (a b a
)))
288 (assert-program-error (defclass foo002
()
290 (:default-initargs x
'a x
'b
)))
291 (assert-program-error (defclass foo003
()
292 ((a :allocation
:class
:allocation
:class
))))
293 (assert-program-error (defclass foo004
()
295 ;; and some more, found by Wolfhard Buss and fixed for cmucl by Gerd
296 ;; Moellmann in sbcl-0.7.8.x:
297 (assert-program-error (progn
298 (defmethod odd-key-args-checking (&key
(key 42)) key
)
299 (odd-key-args-checking 3)))
300 (assert (= (odd-key-args-checking) 42))
301 (assert (eq (odd-key-args-checking :key t
) t
))
302 ;; yet some more, fixed in sbcl-0.7.9.xx
303 (assert-program-error (defclass foo005
()
304 (:metaclass sb-pcl
::funcallable-standard-class
)
306 (assert-program-error (defclass foo006
()
307 ((a :reader
(setf a
)))))
308 (assert-program-error (defclass foo007
()
310 (assert-program-error (defclass foo008
()
312 (:default-initargs
:a
1)
313 (:default-initargs
:a
2))))
315 ;;; DOCUMENTATION's argument-precedence-order wasn't being faithfully
316 ;;; preserved through the bootstrap process until sbcl-0.7.8.39.
317 ;;; (thanks to Gerd Moellmann)
318 (let ((answer (documentation '+ 'function
)))
319 (assert (stringp answer
))
320 (defmethod documentation ((x (eql '+)) y
) "WRONG")
321 (assert (string= (documentation '+ 'function
) answer
)))
323 ;;; only certain declarations are permitted in DEFGENERIC
324 (macrolet ((assert-program-error (form)
325 `(multiple-value-bind (value error
)
326 (ignore-errors ,form
)
327 (assert (null value
))
328 (assert (typep error
'program-error
)))))
329 (assert-program-error (defgeneric bogus-declaration
(x)
330 (declare (special y
))))
331 (assert-program-error (defgeneric bogus-declaration2
(x)
332 (declare (notinline concatenate
)))))
333 ;;; CALL-NEXT-METHOD should call NO-NEXT-METHOD if there is no next
335 (defmethod no-next-method-test ((x integer
)) (call-next-method))
336 (assert (null (ignore-errors (no-next-method-test 1))))
337 (defmethod no-next-method ((g (eql #'no-next-method-test
)) m
&rest args
)
339 (assert (eq (no-next-method-test 1) 'success
))
340 (assert (null (ignore-errors (no-next-method-test 'foo
))))
342 ;;; regression test for bug 176, following a fix that seems
343 ;;; simultaneously to fix 140 while not exposing 176 (by Gerd
344 ;;; Moellmann, merged in sbcl-0.7.9.12).
346 (let ((lastname (intern (format nil
"C176-~D" (1- i
))))
347 (name (intern (format nil
"C176-~D" i
))))
348 (eval `(defclass ,name
349 (,@(if (= i
0) nil
(list lastname
)))
351 (eval `(defmethod initialize-instance :after
((x ,name
) &rest any
)
352 (declare (ignore any
))))))
353 (defclass b176
() (aslot-176))
354 (defclass c176-0
(b176) ())
355 (assert (= 1 (setf (slot-value (make-instance 'c176-9
) 'aslot-176
) 1)))
357 ;;; DEFINE-METHOD-COMBINATION was over-eager at checking for duplicate
359 (define-method-combination dmc-test-mc
(&optional
(order :most-specific-first
))
361 (primary (dmc-test-mc) :order order
:required t
))
362 (let ((form (if (rest primary
)
363 `(and ,@(mapcar #'(lambda (method)
364 `(call-method ,method
))
366 `(call-method ,(first primary
)))))
368 `(call-method ,(first around
)
370 (make-method ,form
)))
373 (defgeneric dmc-test-mc
(&key k
)
374 (:method-combination dmc-test-mc
))
376 (defmethod dmc-test-mc dmc-test-mc
(&key k
)
380 ;;; While I'm at it, DEFINE-METHOD-COMBINATION is defined to return
381 ;;; the NAME argument, not some random method object. So:
382 (assert (eq (define-method-combination dmc-test-return-foo
)
383 'dmc-test-return-foo
))
384 (assert (eq (define-method-combination dmc-test-return-bar
:operator and
)
385 'dmc-test-return-bar
))
386 (assert (eq (define-method-combination dmc-test-return
387 (&optional
(order :most-specific-first
))
389 (primary (dmc-test-return) :order order
:required t
))
390 (let ((form (if (rest primary
)
391 `(and ,@(mapcar #'(lambda (method)
392 `(call-method ,method
))
394 `(call-method ,(first primary
)))))
396 `(call-method ,(first around
)
398 (make-method ,form
)))
402 ;;; DEFMETHOD should signal a PROGRAM-ERROR if an incompatible lambda
404 (defmethod incompatible-ll-test-1 (x) x
)
405 (multiple-value-bind (result error
)
406 (ignore-errors (defmethod incompatible-ll-test-1 (x y
) y
))
407 (assert (null result
))
408 (assert (typep error
'program-error
)))
409 (multiple-value-bind (result error
)
410 (ignore-errors (defmethod incompatible-ll-test-1 (x &rest y
) y
))
411 (assert (null result
))
412 (assert (typep error
'program-error
)))
413 ;;; Sneakily using a bit of MOPness to check some consistency
415 (sb-pcl:generic-function-methods
#'incompatible-ll-test-1
)) 1))
417 (defmethod incompatible-ll-test-2 (x &key bar
) bar
)
418 (multiple-value-bind (result error
)
419 (ignore-errors (defmethod incompatible-ll-test-2 (x) x
))
420 (assert (null result
))
421 (assert (typep error
'program-error
)))
422 (defmethod incompatible-ll-test-2 (x &rest y
) y
)
424 (sb-pcl:generic-function-methods
#'incompatible-ll-test-2
)) 1))
425 (defmethod incompatible-ll-test-2 ((x integer
) &key bar
) bar
)
427 (sb-pcl:generic-function-methods
#'incompatible-ll-test-2
)) 2))
428 (assert (equal (incompatible-ll-test-2 t
1 2) '(1 2)))
429 (assert (eq (incompatible-ll-test-2 1 :bar
'yes
) 'yes
))
431 ;;; Attempting to instantiate classes with forward references in their
432 ;;; CPL should signal errors (FIXME: of what type?)
433 (defclass never-finished-class
(this-one-unfinished-too) ())
434 (multiple-value-bind (result error
)
435 (ignore-errors (make-instance 'never-finished-class
))
436 (assert (null result
))
437 (assert (typep error
'error
)))
438 (multiple-value-bind (result error
)
439 (ignore-errors (make-instance 'this-one-unfinished-too
))
440 (assert (null result
))
441 (assert (typep error
'error
)))
443 ;;; Classes with :ALLOCATION :CLASS slots should be subclassable (and
444 ;;; weren't for a while in sbcl-0.7.9.xx)
445 (defclass superclass-with-slot
()
446 ((a :allocation
:class
)))
447 (defclass subclass-for-class-allocation
(superclass-with-slot) ())
448 (make-instance 'subclass-for-class-allocation
)
450 ;;; bug #136: CALL-NEXT-METHOD was being a little too lexical,
451 ;;; resulting in failure in the following:
452 (defmethod call-next-method-lexical-args ((x integer
))
454 (defmethod call-next-method-lexical-args :around
((x integer
))
457 (assert (= (call-next-method-lexical-args 3) 3))
459 ;;; DEFINE-METHOD-COMBINATION with arguments was hopelessly broken
461 (defvar *d-m-c-args-test
* nil
)
462 (define-method-combination progn-with-lock
()
466 (progn (lock (object-lock ,object
))
467 ,@(mapcar #'(lambda (method)
468 `(call-method ,method
))
470 (unlock (object-lock ,object
))))
471 (defun object-lock (obj)
472 (push "object-lock" *d-m-c-args-test
*)
475 (push "unlock" *d-m-c-args-test
*)
478 (push "lock" *d-m-c-args-test
*)
480 (defgeneric d-m-c-args-test
(x)
481 (:method-combination progn-with-lock
))
482 (defmethod d-m-c-args-test ((x symbol
))
483 (push "primary" *d-m-c-args-test
*))
484 (defmethod d-m-c-args-test ((x number
))
486 (assert (equal (d-m-c-args-test t
) '("primary" "lock" "object-lock")))
487 (assert (equal *d-m-c-args-test
*
488 '("unlock" "object-lock" "primary" "lock" "object-lock")))
489 (setf *d-m-c-args-test
* nil
)
490 (ignore-errors (d-m-c-args-test 1))
491 (assert (equal *d-m-c-args-test
*
492 '("unlock" "object-lock" "lock" "object-lock")))
494 ;;; The walker (on which DEFMETHOD depended) didn't know how to handle
495 ;;; SYMBOL-MACROLET properly. In fact, as of sbcl-0.7.10.20 it still
496 ;;; doesn't, but it does well enough to compile the following without
497 ;;; error (the problems remain in asking for a complete macroexpansion
498 ;;; of an arbitrary form).
499 (symbol-macrolet ((x 1))
500 (defmethod bug222 (z)
501 (macrolet ((frob (form) `(progn ,form
,x
)))
503 (assert (= (bug222 t
) 1))
505 ;;; also, a test case to guard against bogus environment hacking:
506 (eval-when (:compile-toplevel
:load-toplevel
:execute
)
508 ;;; this should at the least compile:
510 (defmethod bug222-b (z stream
)
511 (macrolet ((frob (form) `(progn ,form
,bug222-b
)))
512 (frob (format stream
"~D~%" bug222-b
)))))
513 ;;; and it would be nice (though not specified by ANSI) if the answer
515 (let ((x (make-string-output-stream)))
516 ;; not specified by ANSI
517 (assert (= (bug222-b t x
) 3))
519 (assert (char= (char (get-output-stream-string x
) 0) #\
1)))
522 (sb-ext:quit
:unix-status
104)