allocate-instance.lsp 3.96 KB
Newer Older
pfdietz's avatar
pfdietz committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
;-*- Mode:     Lisp -*-
;;;; Author:   Paul Dietz
;;;; Created:  Mon Apr 28 21:06:58 2003
;;;; Contains: Tests of ALLOCATE-INSTANCE

(in-package :cl-test)

;;; According to the CLHS, the meaning of adding methods to
;;; ALLOCATE-INSTANCE is unspecified, so this will not be tested
;;; here.

(defclass allocate-instance-class-01 ()
  ((a :initform 'x) (b :initarg :b)
   (c :type float) (d :allocation :class)
   (e :initarg :e) (f :documentation "foo"))
  (:default-initargs :b 'y))

(deftest allocate-instance.1
  (let* ((class (find-class 'allocate-instance-class-01))
sds's avatar
sds committed
20
         (obj (allocate-instance class)))
pfdietz's avatar
pfdietz committed
21
22
23
24
25
26
27
28
29
30
    (values
     (eqt (class-of obj) class)
     (typep* obj 'allocate-instance-class-01)
     (typep* obj class)
     (map-slot-boundp* obj '(a b c d e f))))
  t t t
  (nil nil nil nil nil nil))

(deftest allocate-instance.2
  (let* ((class (find-class 'allocate-instance-class-01))
sds's avatar
sds committed
31
32
33
         (obj (allocate-instance class
                                 :foo t :a 10 :b 12 :c 1.0 :d 'a :e 17
                                 :f nil :bar t)))
pfdietz's avatar
pfdietz committed
34
35
36
37
38
39
40
41
42
43
    (values
     (eqt (class-of obj) class)
     (typep* obj 'allocate-instance-class-01)
     (typep* obj class)
     (map-slot-boundp* obj '(a b c d e f))))
  t t t
  (nil nil nil nil nil nil))

(deftest allocate-instance.3
  (let* ((class (find-class 'allocate-instance-class-01))
sds's avatar
sds committed
44
         (obj (allocate-instance class :allow-other-keys nil :xyzzy t)))
pfdietz's avatar
pfdietz committed
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
    (values
     (eqt (class-of obj) class)
     (typep* obj 'allocate-instance-class-01)
     (typep* obj class)
     (map-slot-boundp* obj '(a b c d e f))))
  t t t
  (nil nil nil nil nil nil))

(defclass allocate-instance-class-02 ()
  (a (b :allocation :class)))

(deftest allocate-instance.4
  (let ((class (find-class 'allocate-instance-class-02)))
    (setf (slot-value (allocate-instance class) 'b) 'x)
    (let ((obj (allocate-instance class)))
      (values
       (eqt (class-of obj) class)
       (typep* obj 'allocate-instance-class-02)
       (typep* obj class)
       (slot-boundp* obj 'a)
       (slot-value obj 'b))))
  t t t nil x)

(defstruct allocate-instance-struct-01
  a
  (b 0 :type integer)
  (c #\a :type character)
  (d 'a :type symbol))

(deftest allocate-instance.5
  (let* ((class (find-class 'allocate-instance-struct-01))
sds's avatar
sds committed
76
         (obj   (allocate-instance class)))
pfdietz's avatar
pfdietz committed
77
    (setf (allocate-instance-struct-01-a obj) 'x
sds's avatar
sds committed
78
79
80
          (allocate-instance-struct-01-b obj) 1234567890
          (allocate-instance-struct-01-c obj) #\Z
          (allocate-instance-struct-01-d obj) 'foo)
pfdietz's avatar
pfdietz committed
81
82
83
84
85
86
87
88
89
90
91
    (values
     (eqt (class-of obj) class)
     (typep* obj 'allocate-instance-struct-01)
     (typep* obj class)
     (allocate-instance-struct-01-a obj)
     (allocate-instance-struct-01-b obj)
     (allocate-instance-struct-01-c obj)
     (allocate-instance-struct-01-d obj)))
  t t t
  x 1234567890 #\Z foo)

92
93
94
95
;;; Order of evaluation tests

(deftest allocate-instance.order.1
  (let* ((class (find-class 'allocate-instance-class-01))
sds's avatar
sds committed
96
97
98
99
100
         (i 0) x y z w
         (obj (allocate-instance (progn (setf x (incf i)) class)
                                 :e (setf y (incf i))
                                 :b (setf z (incf i))
                                 :e (setf w (incf i)))))
101
102
103
104
105
106
107
108
109
    (values
     (eqt (class-of obj) class)
     (typep* obj 'allocate-instance-class-01)
     (typep* obj class)
     i x y z w))
  t t t 4 1 2 3 4)

;;; Error tests

pfdietz's avatar
pfdietz committed
110
(deftest allocate-instance.error.1
111
112
  (signals-error (allocate-instance) program-error)
  t)
113

114
115
116
117
118
;;; Duane Rettig made a convincing argument that the next two
;;; tests are bad, since the caller of allocate-instance
;;; is supposed to have checked that the initargs are valid

#|
119
(deftest allocate-instance.error.2
120
  (signals-error (allocate-instance (find-class 'allocate-instance-class-01)
sds's avatar
sds committed
121
122
                                     :b)
                 program-error)
123
  t)
124
125

(deftest allocate-instance.error.3
126
  (signals-error (allocate-instance (find-class 'allocate-instance-class-01)
sds's avatar
sds committed
127
128
                                     '(a b c) nil)
                 program-error)
129
  t)
130
|#