0.8.16.43: Fixes for various CLOS/MOP bugs
[sbcl.git] / tests / mop.impure.lisp
1 ;;;; miscellaneous side-effectful tests of the MOP
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 ;;;; Note that the MOP is not in an entirely supported state.
15 ;;;; However, this seems a good a way as any of ensuring that we have
16 ;;;; no regressions.
17
18 (defpackage "MOP-TEST"
19   (:use "CL" "SB-MOP"))
20
21 (in-package "MOP-TEST")
22 \f
23 ;;; Readers for Class Metaobjects (pp. 212--214 of AMOP)
24 (defclass red-herring (forward-ref) ())
25
26 (assert (null (class-direct-slots (find-class 'forward-ref))))
27 (assert (null (class-direct-default-initargs
28                (find-class 'forward-ref))))
29 \f
30 ;;; Readers for Generic Function Metaobjects (pp. 216--218 of AMOP)
31 (defgeneric fn-with-odd-arg-precedence (a b c)
32   (:argument-precedence-order b c a))
33
34 (assert (equal
35          (generic-function-lambda-list #'fn-with-odd-arg-precedence)
36          '(a b c)))
37 (assert (equal
38          (generic-function-argument-precedence-order #'fn-with-odd-arg-precedence)
39          '(b c a)))
40 ;;; Test for DOCUMENTATION's order, which was wrong until sbcl-0.7.8.39
41 (assert (equal
42          (generic-function-argument-precedence-order #'documentation)
43          (let ((ll (generic-function-lambda-list #'documentation)))
44            (list (nth 1 ll) (nth 0 ll)))))
45
46 (assert (null
47          (generic-function-declarations #'fn-with-odd-arg-precedence)))
48 (defgeneric gf-with-declarations (x)
49   (declare (optimize (speed 3)))
50   (declare (optimize (safety 0))))
51 (let ((decls (generic-function-declarations #'gf-with-declarations)))
52   (assert (= (length decls) 2))
53   (assert (member '(optimize (speed 3)) decls :test #'equal))
54   (assert (member '(optimize (safety 0)) decls :test #'equal)))
55 \f
56 ;;; Readers for Slot Definition Metaobjects (pp. 221--224 of AMOP)
57
58 ;;; Ensure that SLOT-DEFINITION-ALLOCATION returns :INSTANCE/:CLASS as
59 ;;; appropriate.
60 (defclass sdm-test-class ()
61   ((an-instance-slot :accessor an-instance-slot)
62    (a-class-slot :allocation :class :accessor a-class-slot)))
63 (dolist (m (list (list #'an-instance-slot :instance)
64                  (list #'a-class-slot :class)))
65   (let ((methods (generic-function-methods (car m))))
66     (assert (= (length methods) 1))
67     (assert (eq (slot-definition-allocation
68                  (accessor-method-slot-definition
69                   (car methods)))
70                 (cadr m)))))
71 \f
72 ;;; Class Finalization Protocol (see section 5.5.2 of AMOP)
73 (let ((finalized-count 0))
74   (defmethod finalize-inheritance :after ((x standard-class))
75     (incf finalized-count))
76   (defun get-count () finalized-count))
77 (defclass finalization-test-1 () ())
78 (make-instance 'finalization-test-1)
79 (assert (= (get-count) 1))
80 (defclass finalization-test-2 (finalization-test-3) ())
81 (assert (= (get-count) 1))
82 (defclass finalization-test-3 () ())
83 (make-instance 'finalization-test-3)
84 (assert (or (= (get-count) 2) (= (get-count) 3)))
85 (make-instance 'finalization-test-2)
86 (assert (= (get-count) 3))
87 \f
88 ;;; Bits of FUNCALLABLE-STANDARD-CLASS are easy to break; make sure
89 ;;; that it is at least possible to define classes with that as a
90 ;;; metaclass.
91 (defclass gf-class (standard-generic-function) ()
92   (:metaclass funcallable-standard-class))
93 (defgeneric g (a b c)
94   (:generic-function-class gf-class))
95 \f
96 ;;; until sbcl-0.7.12.47, PCL wasn't aware of some direct class
97 ;;; relationships.  These aren't necessarily true, but are probably
98 ;;; not going to change often.
99 (dolist (x '(number array sequence character symbol))
100   (assert (eq (car (class-direct-superclasses (find-class x)))
101               (find-class t)))
102   (assert (member (find-class x)
103                   (class-direct-subclasses (find-class t)))))
104 \f
105 ;;; the class-prototype of the NULL class used to be some weird
106 ;;; standard-instance-like thing.  Make sure it's actually NIL.
107 ;;;
108 ;;; (and FIXME: eventually turn this into asserting that the prototype
109 ;;; of all built-in-classes is of the relevant type)
110 (assert (null (class-prototype (find-class 'null))))
111 \f
112 ;;; simple consistency checks for the SB-MOP package: all of the
113 ;;; functionality specified in AMOP is in functions and classes:
114 (assert (null (loop for x being each external-symbol in "SB-MOP"
115                     unless (or (fboundp x) (find-class x)) collect x)))
116 ;;; and all generic functions in SB-MOP have at least one specified
117 ;;; method, except for UPDATE-DEPENDENT
118 (assert (null (loop for x being each external-symbol in "SB-MOP"
119                     unless (or (not (fboundp x))
120                                (eq x 'update-dependent)
121                                (not (typep (fdefinition x) 'generic-function))
122                                (> (length (generic-function-methods
123                                            (fdefinition x)))
124                                   0))
125                     collect x)))
126 \f
127 ;;; make sure that ENSURE-CLASS-USING-CLASS's arguments are the right
128 ;;; way round (!)
129 (defvar *e-c-u-c-arg-order* nil)
130 (defmethod ensure-class-using-class :after
131     (class (name (eql 'e-c-u-c-arg-order)) &key &allow-other-keys)
132   (setf *e-c-u-c-arg-order* t))
133 (defclass e-c-u-c-arg-orderoid () ())
134 (assert (null *e-c-u-c-arg-order*))
135 (defclass e-c-u-c-arg-order () ())
136 (assert (eq *e-c-u-c-arg-order* t))
137 \f
138 ;;; verify that FIND-CLASS works after FINALIZE-INHERITANCE
139 (defclass automethod-class (standard-class) ())
140 (defmethod validate-superclass ((c1 automethod-class) (c2 standard-class))
141   t)
142 (defmethod finalize-inheritance :after ((x automethod-class))
143   (format t "~&~S ~S~%" x (find-class (class-name x))))
144 (defclass automethod-object () ()
145   (:metaclass automethod-class))
146 (defvar *automethod-object* (make-instance 'automethod-object))
147 (assert (typep *automethod-object* 'automethod-object))
148 \f
149 ;;; COMPUTE-EFFECTIVE-SLOT-DEFINITION should take three arguments, one
150 ;;; of which is the name of the slot.
151 (defvar *compute-effective-slot-definition-count* 0)
152 (defmethod compute-effective-slot-definition :before
153     (class (name (eql 'foo)) dsds)
154   (incf *compute-effective-slot-definition-count*))
155 (defclass cesd-test-class ()
156   ((foo :initarg :foo)))
157 (make-instance 'cesd-test-class :foo 3)
158 ;;; FIXME: this assertion seems a little weak.  I don't know why
159 ;;; COMPUTE-EFFECTIVE-SLOT-DEFINITION gets called twice in this
160 ;;; sequence, nor whether that's compliant with AMOP.  -- CSR,
161 ;;; 2003-04-17
162 (assert (> *compute-effective-slot-definition-count* 0))
163 \f
164 ;;; this used to cause a nasty uncaught metacircularity in PCL.
165 (defclass substandard-method (standard-method) ())
166 (defgeneric substandard-defgeneric (x y)
167   (:method-class substandard-method)
168   (:method ((x number) (y number)) (+ x y))
169   (:method ((x string) (y string)) (concatenate 'string x y)))
170 (assert (= (substandard-defgeneric 1 2) 3))
171 (assert (string= (substandard-defgeneric "1" "2") "12"))
172 \f
173 (let* ((x (find-class 'pathname))
174        (xs (class-direct-subclasses x)))
175   (assert (>= (length xs) 1))
176   (assert (member (find-class 'logical-pathname) xs)))
177
178 ;;; BUG 338: "MOP specializers as type specifiers"
179 ;;;  (reported by Bruno Haible sbcl-devel 2004-06-11)
180 (let* ((m (defmethod eql-specialized-method ((x (eql 4.0))) 3.0))
181        (spec (first (sb-mop:method-specializers m))))
182   (assert (not (typep 1 spec)))
183   (assert (typep 4.0 spec)))
184 \f
185 ;;; BUG #334, relating to programmatic addition of slots to a class
186 ;;; with COMPUTE-SLOTS.
187 ;;;
188 ;;; FIXME: the DUMMY classes here are to prevent class finalization
189 ;;; before the compute-slots method is around.  This should probably
190 ;;; be done by defining the COMPUTE-SLOTS methods on a metaclass,
191 ;;; which can be defined before.
192 ;;;
193 ;;; a. adding an :allocation :instance slot
194 (defclass class-to-add-instance-slot (dummy-ctais) ())
195 (defmethod compute-slots ((c (eql (find-class 'class-to-add-instance-slot))))
196   (append (call-next-method)
197           (list (make-instance 'standard-effective-slot-definition
198                                :name 'y
199                                :allocation :instance))))
200 (defclass dummy-ctais () ((x :allocation :class)))
201 (assert (equal (mapcar #'slot-definition-allocation 
202                        (class-slots (find-class 'class-to-add-instance-slot)))
203                ;; FIXME: is the order really guaranteed?
204                '(:class :instance)))
205 (assert (typep (slot-definition-location 
206                 (cadr (class-slots (find-class 'class-to-add-instance-slot)))) 
207                'unsigned-byte))
208 #| (assert (typep (slot-definition-location (car ...)) '???)) |#
209 (let ((x (make-instance 'class-to-add-instance-slot)))
210   (assert (not (slot-boundp x 'x)))
211   (setf (slot-value x 'x) t)
212   (assert (not (slot-boundp x 'y)))
213   (setf (slot-value x 'y) 1)
214   (assert (= 1 (slot-value x 'y))))
215 (let ((x (make-instance 'class-to-add-instance-slot)))
216   (assert (slot-boundp x 'x))
217   (assert (eq t (slot-value x 'x)))
218   (assert (not (slot-boundp x 'y))))
219
220 ;;; b. adding an :allocation :class slot
221 (defclass class-to-add-class-slot (dummy-ctacs) ())
222 (defmethod compute-slots ((c (eql (find-class 'class-to-add-class-slot))))
223   (append (call-next-method)
224           (list (make-instance 'standard-effective-slot-definition
225                                :name 'y
226                                :allocation :class))))
227 (defclass dummy-ctacs () ((x :allocation :class)))
228 (assert (equal (mapcar #'slot-definition-allocation 
229                        (class-slots (find-class 'class-to-add-class-slot)))
230                '(:class :class)))
231 (let ((x (make-instance 'class-to-add-class-slot)))
232   (assert (not (slot-boundp x 'x)))
233   (setf (slot-value x 'x) nil)
234   (assert (not (slot-boundp x 'y)))
235   (setf (slot-value x 'y) 1)
236   (assert (= 1 (slot-value x 'y))))
237 (let ((x (make-instance 'class-to-add-class-slot)))
238   (assert (slot-boundp x 'x))
239   (assert (eq nil (slot-value x 'x)))
240   (assert (slot-boundp x 'y))
241   (assert (= 1 (slot-value x 'y))))
242 ;;; extra paranoia: check that we haven't broken the instance-slot class
243 (let ((x (make-instance 'class-to-add-instance-slot)))
244   (assert (slot-boundp x 'x))
245   (assert (eq t (slot-value x 'x)))
246   (assert (not (slot-boundp x 'y))))
247 \f
248 ;;;; the CTOR optimization was insufficiently careful about its
249 ;;;; assumptions: firstly, it failed with a failed AVER for
250 ;;;; non-standard-allocation slots:
251 (defclass class-with-frob-slot ()
252   ((frob-slot :initarg :frob-slot :allocation :frob)))
253 (handler-case
254     (funcall (compile nil '(lambda ()
255                             (make-instance 'class-with-frob-slot
256                              :frob-slot 1))))
257   (sb-int:bug (c) (error c))
258   (error () "Probably OK: haven't implemented SLOT-BOUNDP-USING-CLASS"))
259 ;;; secondly, it failed to take account of the fact that we might wish
260 ;;; to customize (setf slot-value-using-class)
261 (defclass class-with-special-ssvuc ()
262   ((some-slot :initarg :some-slot)))
263 (defvar *special-ssvuc-counter* 0)
264 (defmethod (setf slot-value-using-class) :before
265     (new-value class (instance class-with-special-ssvuc) slotd)
266   (incf *special-ssvuc-counter*))
267 (let ((fun (compile nil '(lambda () (make-instance 'class-with-special-ssvuc
268                                      :some-slot 1)))))
269   (assert (= *special-ssvuc-counter* 0))
270   (funcall fun)
271   (assert (= *special-ssvuc-counter* 1))
272   (funcall fun)
273   (assert (= *special-ssvuc-counter* 2)))
274 ;;; and now with the customization after running the function once
275 (defclass class-with-special-ssvuc-2 ()
276   ((some-slot :initarg :some-slot)))
277 (defvar *special-ssvuc-counter-2* 0)
278 (let ((fun (compile nil '(lambda () (make-instance 'class-with-special-ssvuc-2
279                                      :some-slot 1)))))
280   (assert (= *special-ssvuc-counter-2* 0))
281   (funcall fun)
282   (assert (= *special-ssvuc-counter-2* 0))
283   (defmethod (setf slot-value-using-class) :before
284       (new-value class (instance class-with-special-ssvuc-2) slotd)
285     (incf *special-ssvuc-counter-2*))
286   (funcall fun)
287   (assert (= *special-ssvuc-counter-2* 1)))
288 \f
289 ;;; vicious metacycle detection and resolution wasn't good enough: it
290 ;;; didn't take account that the slots (and hence the slot readers)
291 ;;; might be inherited from superclasses.  This example, due to Bruno
292 ;;; Haible, also tests programmatic addition of accessors.
293 (defclass auto-accessors-direct-slot-definition-class (standard-class)
294   ((containing-class-name :initarg :containing-class-name)))
295 (defmethod validate-superclass
296     ((c1 auto-accessors-direct-slot-definition-class) (c2 standard-class))
297   t)
298 (defclass auto-accessors-class (standard-class)
299   ())
300 (defmethod direct-slot-definition-class ((class auto-accessors-class)
301                                          &rest initargs)
302   (let ((dsd-class-name (gensym)))
303     (sb-pcl:ensure-class
304      dsd-class-name
305      :metaclass 'auto-accessors-direct-slot-definition-class
306      :direct-superclasses (list (find-class 'standard-direct-slot-definition))
307      :containing-class-name (class-name class))
308     (eval `(defmethod initialize-instance :after ((dsd ,dsd-class-name)
309                                                   &rest args)
310             (when (and (null (slot-definition-readers dsd))
311                        (null (slot-definition-writers dsd)))
312               (let* ((containing-class-name
313                       (slot-value (class-of dsd) 'containing-class-name))
314                      (accessor-name
315                       (intern
316                        (concatenate 'string
317                                     (symbol-name containing-class-name)
318                                     "-"
319                                     (symbol-name (slot-definition-name dsd)))
320                        (symbol-package containing-class-name))))
321                 (setf (slot-definition-readers dsd) (list accessor-name))
322                 (setf (slot-definition-writers dsd)
323                       (list (list 'setf accessor-name)))))))
324     (find-class dsd-class-name)))
325 (defmethod validate-superclass ((c1 auto-accessors-class) (c2 standard-class))
326   t)
327 (defclass testclass15 ()
328   ((x :initarg :x) (y))
329   (:metaclass auto-accessors-class))
330 (let ((inst (make-instance 'testclass15 :x 12)))
331   (assert (equal (list (testclass15-x inst) (setf (testclass15-y inst) 13))
332                  '(12 13))))
333
334 ;;; bug reported by Bruno Haible on sbcl-devel 2004-11-17: incorrect
335 ;;; handling of multiple values for non-standard slot-options
336 (progn
337   (defclass option-slot-definition (sb-mop:standard-direct-slot-definition)
338     ((option :accessor sl-option :initarg :my-option)))
339   (defclass option-slot-class (standard-class)
340     ())
341   (defmethod sb-mop:direct-slot-definition-class 
342       ((c option-slot-class) &rest args)
343     (declare (ignore args))
344     (find-class 'option-slot-definition))
345   (defmethod sb-mop:validate-superclass 
346       ((c1 option-slot-class) (c2 standard-class))
347     t)
348   (eval '(defclass test-multiple-slot-option-bug ()
349           ((x :my-option bar :my-option baz))
350           (:metaclass option-slot-class)))
351   (assert (null (set-difference 
352                  '(bar baz)
353                  (sl-option (first (sb-mop:class-direct-slots 
354                                     (find-class 'test-multiple-slot-option-bug))))))))
355
356 ;;; bug reported by Bruno Haibel on sbcl-devel 2004-11-19: AMOP requires
357 ;;; that CLASS-PROTOYPE signals an error if the class is not yet finalized
358 (defclass prototype-not-finalized-sub (prototype-not-finalized-super) ())
359 (multiple-value-bind (val err)
360     (ignore-errors (sb-mop:class-prototype (find-class 'prototype-not-finalized-super)))
361   (assert (null val))
362   (assert (typep err 'error)))
363
364 ;;; AMOP says so
365 (find-method (fdefinition 'sb-mop:allocate-instance) () '(built-in-class))
366 (dolist (class-name '(fixnum bignum symbol))
367   (let ((class (find-class class-name)))
368     (multiple-value-bind (value error) (ignore-errors (allocate-instance class))
369       (assert (null value))
370       (assert (typep error 'error)))))
371
372 \f
373 ;;;; success
374 (sb-ext:quit :unix-status 104)