0.7.6.23:
[sbcl.git] / tests / clos.impure.lisp
1 ;;;; miscellaneous side-effectful tests of CLOS
2
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.
13
14 (defpackage "FOO"
15   (:use "CL"))
16 (in-package "FOO")
17 \f
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))
23   (+ (struct-a-x a)
24      (struct-a-y a)))
25 (defgeneric jiggle (arg))
26 (defmethod jiggle ((a struct-a))
27   (- (struct-a-x a)
28      (struct-a-y a)))
29 (defmethod jiggle ((b struct-b))
30   (- (struct-b-x b)
31      (struct-b-y b)
32      (struct-b-z b)))
33 (assert (= (wiggle (make-struct-a :x 6 :y 5))
34            (jiggle (make-struct-b :x 19 :y 6 :z 2))))
35
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)
40   (unless (minusp n)
41     (gf-defined-in-this-file x y)))
42
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))
49 (zut-n-a-m 1 2 3)
50
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))
54
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))))
64 (assert (expect-error
65          (macroexpand-1
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)))))
75
76 ;;; more lambda-list checking
77 ;;;
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)
82            (if x t nil))
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)
87              (compile nil
88                       `(lambda ()
89                          ,expr))
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
98              ;; after all?
99              ;;(unless expected-failure-p
100              ;;  (assert (not warnings-p)))
101              (assert (eq (coerce-to-boolean failure-p) expected-failure-p))))
102          (like (expr)
103            (%like-or-dislike expr nil))
104          (dislike (expr)
105            (%like-or-dislike expr t)))
106   ;; basic sanity
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)))
121   ;; forbidden &AUX
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   ;; also can't use bogoDEFMETHODish type-qualifier-ish decorations
125   ;; on required arguments
126   (dislike '(defgeneric gf-for-11-test-15 ((arg t))))
127   (like '(defgeneric gf-for-11-test-16 (arg))))
128
129 ;;; structure-class tests setup
130 (defclass structure-class-foo1 () () (:metaclass cl:structure-class))
131 (defclass structure-class-foo2 (structure-class-foo1)
132   () (:metaclass cl:structure-class))
133
134 ;;; standard-class tests setup
135 (defclass standard-class-foo1 () () (:metaclass cl:standard-class))
136 (defclass standard-class-foo2 (standard-class-foo1)
137   () (:metaclass cl:standard-class))
138
139 (assert (typep (class-of (make-instance 'structure-class-foo1))
140                'structure-class))
141 (assert (typep (make-instance 'structure-class-foo1) 'structure-class-foo1))
142 (assert (typep (make-instance 'standard-class-foo1) 'standard-class-foo1))
143
144 ;;; DEFGENERIC's blow-away-old-methods behavior is specified to have
145 ;;; special hacks to distinguish between defined-with-DEFGENERIC-:METHOD
146 ;;; methods and defined-with-DEFMETHOD methods, so that reLOADing
147 ;;; DEFGENERIC-containing files does the right thing instead of 
148 ;;; randomly slicing your generic functions. (APD made this work
149 ;;; in sbcl-0.7.0.2.)
150 (defgeneric born-to-be-redefined (x)
151   (:method ((x integer))
152     'integer))
153 (defmethod born-to-be-redefined ((x real))
154   'real)
155 (assert (eq (born-to-be-redefined 1) 'integer))
156 (defgeneric born-to-be-redefined (x))
157 (assert (eq (born-to-be-redefined 1) 'real)) ; failed until sbcl-0.7.0.2
158 (defgeneric born-to-be-redefined (x)
159   (:method ((x integer))
160     'integer))
161 (defmethod born-to-be-redefined ((x integer))
162   'int)
163 (assert (eq (born-to-be-redefined 1) 'int))
164 (defgeneric born-to-be-redefined (x))
165 (assert (eq (born-to-be-redefined 1) 'int))
166 \f
167 ;;; In the removal of ITERATE from SB-PCL, a bug was introduced
168 ;;; preventing forward-references and also change-class (which
169 ;;; forward-references used interally) from working properly.  One
170 ;;; symptom was reported by Brian Spilsbury (sbcl-devel 2002-04-08),
171 ;;; and another on IRC by Dan Barlow simultaneously.  Better check
172 ;;; that it doesn't happen again.
173 ;;;
174 ;;; First, the forward references:
175 (defclass a (b) ())
176 (defclass b () ())
177 ;;; Then change-class
178 (defclass class-with-slots ()
179   ((a-slot :initarg :a-slot :accessor a-slot)
180    (b-slot :initarg :b-slot :accessor b-slot)
181    (c-slot :initarg :c-slot :accessor c-slot)))
182 (let ((foo (make-instance 'class-with-slots
183                           :a-slot 1
184                           :b-slot 2
185                           :c-slot 3)))
186   (let ((bar (change-class foo 'class-with-slots)))
187     (assert (= (a-slot bar) 1))
188     (assert (= (b-slot bar) 2))
189     (assert (= (c-slot bar) 3))))
190
191 ;;; some more CHANGE-CLASS testing, now that we have an ANSI-compliant
192 ;;; version (thanks to Espen Johnsen)
193 (defclass from-class ()
194   ((foo :initarg :foo :accessor foo)))
195 (defclass to-class ()
196   ((foo :initarg :foo :accessor foo)
197    (bar :initarg :bar :accessor bar)))
198 (let* ((from (make-instance 'from-class :foo 1))
199        (to (change-class from 'to-class :bar 2)))
200   (assert (= (foo to) 1))
201   (assert (= (bar to) 2)))
202
203 ;;; Until Pierre Mai's patch (sbcl-devel 2002-06-18, merged in
204 ;;; sbcl-0.7.4.39) the :MOST-SPECIFIC-LAST option had no effect.
205 (defgeneric bug180 (x)
206   (:method-combination list :most-specific-last))
207 (defmethod bug180 list ((x number))
208   'number)
209 (defmethod bug180 list ((x fixnum))
210   'fixnum)
211 (assert (equal (bug180 14) '(number fixnum)))
212 \f
213 ;;; printing a structure class should not loop indefinitely (or cause
214 ;;; a stack overflow):
215 (defclass test-printing-structure-class ()
216   ((slot :initarg :slot))
217   (:metaclass structure-class))
218 (print (make-instance 'test-printing-structure-class :slot 2))
219
220 ;;; structure-classes should behave nicely when subclassed
221 (defclass super-structure ()
222   ((a :initarg :a :accessor a-accessor)
223    (b :initform 2 :reader b-reader))
224   (:metaclass structure-class))
225 (defclass sub-structure (super-structure)
226   ((c :initarg :c :writer c-writer :accessor c-accessor))
227   (:metaclass structure-class))
228 (let ((foo (make-instance 'sub-structure :a 1 :c 3)))
229   (assert (= (a-accessor foo) 1))
230   (assert (= (b-reader foo) 2))
231   (assert (= (c-accessor foo) 3))
232   (setf (a-accessor foo) 4)
233   (c-writer 5 foo)
234   (assert (= (a-accessor foo) 4))
235   (assert (= (c-accessor foo) 5)))
236 \f
237 ;;; At least as of sbcl-0.7.4, PCL has code to support a special
238 ;;; encoding of effective method functions for slot accessors as
239 ;;; FIXNUMs. Given this special casing, it'd be easy for slot accessor
240 ;;; functions to get broken in special ways even though ordinary
241 ;;; generic functions work. As of sbcl-0.7.4 we didn't have any tests
242 ;;; for that possibility. Now we have a few tests:
243 (defclass fish ()
244   ((fin :reader ffin :writer ffin!)
245    (tail :reader ftail :writer ftail!)))
246 (defvar *fish* (make-instance 'fish))
247 (ffin! 'triangular-fin *fish*)
248 (defclass cod (fish) ())
249 (defvar *cod* (make-instance 'cod))
250 (defparameter *clos-dispatch-side-fx* (make-array 0 :fill-pointer 0))
251 (defmethod ffin! (new-fin (cod cod))
252   (format t "~&about to set ~S fin to ~S~%" cod new-fin)
253   (vector-push-extend '(cod) *clos-dispatch-side-fx*)
254   (prog1
255       (call-next-method)
256     (format t "~&done setting ~S fin to ~S~%" cod new-fin)))
257 (defmethod ffin! :before (new-fin (cod cod))
258   (vector-push-extend '(:before cod) *clos-dispatch-side-fx*)
259   (format t "~&exploring the CLOS dispatch zoo with COD fins~%"))
260 (ffin! 'almost-triang-fin *cod*)
261 (assert (eq (ffin *cod*) 'almost-triang-fin))
262 (assert (equalp #((:before cod) (cod)) *clos-dispatch-side-fx*))
263 \f
264 ;;; Until sbcl-0.7.6.21, the long form of DEFINE-METHOD-COMBINATION
265 ;;; ignored its options; Gerd Moellmann found and fixed the problem
266 ;;; for cmucl (cmucl-imp 2002-06-18).
267 (define-method-combination test-mc (x)
268   ;; X above being a method-group-specifier
269   ((primary () :required t))
270   `(call-method ,(first primary)))
271
272 (defgeneric gf (obj)
273   (:method-combination test-mc 1))
274
275 (defmethod gf (obj)
276   obj)
277 \f
278 ;;;; success
279
280 (sb-ext:quit :unix-status 104)