1 ;;;; miscellaneous side-effectful tests of the MOP
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.
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
18 (defpackage "MOP-TEST"
21 (in-package "MOP-TEST")
23 ;;; Readers for Class Metaobjects (pp. 212--214 of AMOP)
24 (defclass red-herring
(forward-ref) ())
26 (assert (null (class-direct-slots (find-class 'forward-ref
))))
27 (assert (null (class-direct-default-initargs
28 (find-class 'forward-ref
))))
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
))
35 (generic-function-lambda-list #'fn-with-odd-arg-precedence
)
38 (generic-function-argument-precedence-order #'fn-with-odd-arg-precedence
)
40 ;;; Test for DOCUMENTATION's order, which was wrong until sbcl-0.7.8.39
42 (generic-function-argument-precedence-order #'documentation
)
43 (let ((ll (generic-function-lambda-list #'documentation
)))
44 (list (nth 1 ll
) (nth 0 ll
)))))
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
)))
56 ;;; Readers for Slot Definition Metaobjects (pp. 221--224 of AMOP)
58 ;;; Ensure that SLOT-DEFINITION-ALLOCATION returns :INSTANCE/:CLASS as
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
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))
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
91 (defclass gf-class
(standard-generic-function) ()
92 (:metaclass funcallable-standard-class
))
94 (:generic-function-class gf-class
))
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
)))
102 (assert (member (find-class x
)
103 (class-direct-subclasses (find-class t
)))))
105 ;;; the class-prototype of the NULL class used to be some weird
106 ;;; standard-instance-like thing. Make sure it's actually NIL.
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
))))
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
127 ;;; make sure that ENSURE-CLASS-USING-CLASS's arguments are the right
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
))
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
))
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
))
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,
162 (assert (> *compute-effective-slot-definition-count
* 0))
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"))
174 (sb-ext:quit
:unix-status
104)