1e705908039c34a6aad313334b7e4b3d2e9e3b64
[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 t)))
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 number) (y number)))
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
65 (assert (expect-error
66          (macroexpand-1
67           '(defmethod foo0 ((x t) &rest) nil))))
68
69 (assert (expect-error (defgeneric foo1 (x &rest))))
70 (assert (expect-error (defgeneric foo2 (x a &rest))))
71
72 (defgeneric foo3 (x &rest y))
73 (defmethod foo3 ((x t) &rest y) nil)
74 (defmethod foo4 ((x t) &key y &rest z) nil)
75 (defgeneric foo4 (x &key y &rest z))
76
77 (assert (expect-error (defgeneric foo5 (x &rest))))
78 (assert (expect-error (macroexpand-1 '(defmethod foo6 (x &rest)))))
79
80 ;;; structure-class tests setup
81 (defclass structure-class-foo1 () () (:metaclass cl:structure-class))
82 (defclass structure-class-foo2 (structure-class-foo1)
83   () (:metaclass cl:structure-class))
84
85 ;;; standard-class tests setup
86 (defclass standard-class-foo1 () () (:metaclass cl:standard-class))
87 (defclass standard-class-foo2 (standard-class-foo1)
88   () (:metaclass cl:standard-class))
89
90 (assert (typep (class-of (make-instance 'structure-class-foo1))
91                'structure-class))
92 (assert (typep (make-instance 'structure-class-foo1) 'structure-class-foo1))
93 (assert (typep (make-instance 'standard-class-foo1) 'standard-class-foo1))
94
95 ;;; DEFGENERIC's blow-away-old-methods behavior is specified to have
96 ;;; special hacks to distinguish between defined-with-DEFGENERIC-:METHOD
97 ;;; methods and defined-with-DEFMETHOD methods, so that reLOADing
98 ;;; DEFGENERIC-containing files does the right thing instead of 
99 ;;; randomly slicing your generic functions. (APD made this work
100 ;;; in sbcl-0.7.0.2.)
101 (defgeneric born-to-be-redefined (x)
102   (:method ((x integer))
103     'integer))
104 (defmethod born-to-be-redefined ((x real))
105   'real)
106 (assert (eq (born-to-be-redefined 1) 'integer))
107 (defgeneric born-to-be-redefined (x))
108 (assert (eq (born-to-be-redefined 1) 'real)) ; failed until sbcl-0.7.0.2
109 (defgeneric born-to-be-redefined (x)
110   (:method ((x integer))
111     'integer))
112 (defmethod born-to-be-redefined ((x integer))
113   'int)
114 (assert (eq (born-to-be-redefined 1) 'int))
115 (defgeneric born-to-be-redefined (x))
116 (assert (eq (born-to-be-redefined 1) 'int))
117 \f
118 ;;; In the removal of ITERATE from SB-PCL, a bug was introduced
119 ;;; preventing forward-references and also change-class (which
120 ;;; forward-references used interally) from working properly.  One
121 ;;; symptom was reported by Brian Spilsbury (sbcl-devel 2002-04-08),
122 ;;; and another on IRC by Dan Barlow simultaneously.  Better check
123 ;;; that it doesn't happen again.
124 ;;;
125 ;;; First, the forward references:
126 (defclass a (b) ())
127 (defclass b () ())
128 ;;; Then change-class
129 (defclass class-with-slots ()
130   ((a-slot :initarg :a-slot :accessor a-slot)
131    (b-slot :initarg :b-slot :accessor b-slot)
132    (c-slot :initarg :c-slot :accessor c-slot)))
133 (let ((foo (make-instance 'class-with-slots
134                           :a-slot 1
135                           :b-slot 2
136                           :c-slot 3)))
137   (let ((bar (change-class foo 'class-with-slots)))
138     (assert (= (a-slot bar) 1))
139     (assert (= (b-slot bar) 2))
140     (assert (= (c-slot bar) 3))))
141
142 ;;; some more CHANGE-CLASS testing, now that we have an ANSI-compliant
143 ;;; version (thanks to Espen Johnsen)
144 (defclass from-class ()
145   ((foo :initarg :foo :accessor foo)))
146 (defclass to-class ()
147   ((foo :initarg :foo :accessor foo)
148    (bar :initarg :bar :accessor bar)))
149 (let* ((from (make-instance 'from-class :foo 1))
150        (to (change-class from 'to-class :bar 2)))
151   (assert (= (foo to) 1))
152   (assert (= (bar to) 2)))
153
154 ;;; Until Pierre Mai's patch (sbcl-devel 2002-06-18, merged in
155 ;;; sbcl-0.7.4.39) the :MOST-SPECIFIC-LAST option had no effect.
156 (defgeneric bug180 ((x t))
157   (:method-combination list :most-specific-last))
158 (defmethod bug180 list ((x number))
159   'number)
160 (defmethod bug180 list ((x fixnum))
161   'fixnum)
162 (assert (equal (bug180 14) '(number fixnum)))
163 \f
164 ;;; printing a structure class should not loop indefinitely (or cause
165 ;;; a stack overflow):
166 (defclass test-printing-structure-class ()
167   ((slot :initarg :slot))
168   (:metaclass structure-class))
169 (print (make-instance 'test-printing-structure-class :slot 2))
170
171 ;;; structure-classes should behave nicely when subclassed
172 (defclass super-structure ()
173   ((a :initarg :a :accessor a-accessor)
174    (b :initform 2 :reader b-reader))
175   (:metaclass structure-class))
176 (defclass sub-structure (super-structure)
177   ((c :initarg :c :writer c-writer :accessor c-accessor))
178   (:metaclass structure-class))
179 (let ((foo (make-instance 'sub-structure :a 1 :c 3)))
180   (assert (= (a-accessor foo) 1))
181   (assert (= (b-reader foo) 2))
182   (assert (= (c-accessor foo) 3))
183   (setf (a-accessor foo) 4)
184   (c-writer 5 foo)
185   (assert (= (a-accessor foo) 4))
186   (assert (= (c-accessor foo) 5)))
187 \f
188 ;;; At least as of sbcl-0.7.4, PCL has code to support a special
189 ;;; encoding of effective method functions for slot accessors as
190 ;;; FIXNUMs. Given this special casing, it'd be easy for slot accessor
191 ;;; functions to get broken in special ways even though ordinary
192 ;;; generic functions work. As of sbcl-0.7.4 we didn't have any tests
193 ;;; for that possibility. Now we have a few tests:
194 (defclass fish ()
195   ((fin :reader ffin :writer ffin!)
196    (tail :reader ftail :writer ftail!)))
197 (defvar *fish* (make-instance 'fish))
198 (ffin! 'triangular-fin *fish*)
199 (defclass cod (fish) ())
200 (defvar *cod* (make-instance 'cod))
201 (defparameter *clos-dispatch-side-fx* (make-array 0 :fill-pointer 0))
202 (defmethod ffin! (new-fin (cod cod))
203   (format t "~&about to set ~S fin to ~S~%" cod new-fin)
204   (vector-push-extend '(cod) *clos-dispatch-side-fx*)
205   (prog1
206       (call-next-method)
207     (format t "~&done setting ~S fin to ~S~%" cod new-fin)))
208 (defmethod ffin! :before (new-fin (cod cod))
209   (vector-push-extend '(:before cod) *clos-dispatch-side-fx*)
210   (format t "~&exploring the CLOS dispatch zoo with COD fins~%"))
211 (ffin! 'almost-triang-fin *cod*)
212 (assert (eq (ffin *cod*) 'almost-triang-fin))
213 (assert (equalp #((:before cod) (cod)) *clos-dispatch-side-fx*))
214 \f
215 ;;;; success
216
217 (sb-ext:quit :unix-status 104)