0.7.9.48:
[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 a supported state. Package issues
15 ;;;; (both MOP/SB-PCL and CL/SB-PCL) have yet to be resolved, and
16 ;;;; there is likely to be missing functionality.  However, this seems
17 ;;;; a good a way as any of ensuring that we have no regressions.
18
19 (defpackage "MOP-TEST"
20   ;; eventually, we might want "MOP" as well here.
21   (:use "CL"))
22
23 (in-package "MOP-TEST")
24 \f
25 ;;; Readers for Class Metaobjects (pp. 212--214 of AMOP)
26 (defclass red-herring (forward-ref) ())
27
28 (assert (null (sb-pcl:class-direct-slots (sb-pcl:find-class 'forward-ref))))
29 (assert (null (sb-pcl:class-direct-default-initargs
30                (sb-pcl:find-class 'forward-ref))))
31 \f
32 ;;; Readers for Generic Function Metaobjects (pp. 216--218 of AMOP)
33 (defgeneric fn-with-odd-arg-precedence (a b c)
34   (:argument-precedence-order b c a))
35
36 (assert (equal
37          (sb-pcl:generic-function-lambda-list #'fn-with-odd-arg-precedence)
38          '(a b c)))
39 (assert (equal
40          (sb-pcl:generic-function-argument-precedence-order #'fn-with-odd-arg-precedence)
41          '(b c a)))
42 ;;; Test for DOCUMENTATION's order, which was wrong until sbcl-0.7.8.39
43 (assert (equal
44          (sb-pcl:generic-function-argument-precedence-order #'documentation)
45          (let ((ll (sb-pcl:generic-function-lambda-list #'documentation)))
46            (list (nth 1 ll) (nth 0 ll)))))
47
48 (assert (null
49          (sb-pcl:generic-function-declarations #'fn-with-odd-arg-precedence)))
50 (defgeneric gf-with-declarations (x)
51   (declare (optimize (speed 3)))
52   (declare (optimize (safety 0))))
53 (let ((decls (sb-pcl:generic-function-declarations #'gf-with-declarations)))
54   (assert (= (length decls) 2))
55   (assert (member '(optimize (speed 3)) decls :test #'equal))
56   (assert (member '(optimize (safety 0)) decls :test #'equal)))
57 \f
58 ;;; Readers for Slot Definition Metaobjects (pp. 221--224 of AMOP)
59
60 ;;; Ensure that SLOT-DEFINITION-ALLOCATION returns :INSTANCE/:CLASS as
61 ;;; appropriate.
62 (defclass sdm-test-class ()
63   ((an-instance-slot :accessor an-instance-slot)
64    (a-class-slot :allocation :class :accessor a-class-slot)))
65 (dolist (m (list (list #'an-instance-slot :instance)
66                  (list #'a-class-slot :class)))
67   (let ((methods (sb-pcl:generic-function-methods (car m))))
68     (assert (= (length methods) 1))
69     (assert (eq (sb-pcl:slot-definition-allocation
70                  (sb-pcl:accessor-method-slot-definition
71                   (car methods)))
72                 (cadr m)))))
73 \f
74 ;;; Class Finalization Protocol (see section 5.5.2 of AMOP)
75 (let ((finalized-count 0))
76   (defmethod sb-pcl:finalize-inheritance :after ((x sb-pcl::standard-class))
77     (incf finalized-count))
78   (defun get-count () finalized-count))
79 (defclass finalization-test-1 () ())
80 (make-instance 'finalization-test-1)
81 (assert (= (get-count) 1))
82 (defclass finalization-test-2 (finalization-test-3) ())
83 (assert (= (get-count) 1))
84 (defclass finalization-test-3 () ())
85 (make-instance 'finalization-test-3)
86 (assert (or (= (get-count) 2) (= (get-count) 3)))
87 (make-instance 'finalization-test-2)
88 (assert (= (get-count) 3))
89 \f
90 ;;; Bits of FUNCALLABLE-STANDARD-CLASS are easy to break; make sure
91 ;;; that it is at least possible to define classes with that as a
92 ;;; metaclass.
93 (defclass gf-class (standard-generic-function) ()
94   (:metaclass sb-pcl::funcallable-standard-class))
95 (defgeneric g (a b c)
96   (:generic-function-class gf-class))
97 \f
98 ;;;; success
99 (sb-ext:quit :unix-status 104)