-
Notifications
You must be signed in to change notification settings - Fork 4
/
allocate-instance.lsp
130 lines (113 loc) · 3.96 KB
/
allocate-instance.lsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
;-*- 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))
(obj (allocate-instance class)))
(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))
(obj (allocate-instance class
:foo t :a 10 :b 12 :c 1.0 :d 'a :e 17
:f nil :bar t)))
(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))
(obj (allocate-instance class :allow-other-keys nil :xyzzy t)))
(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))
(obj (allocate-instance class)))
(setf (allocate-instance-struct-01-a obj) 'x
(allocate-instance-struct-01-b obj) 1234567890
(allocate-instance-struct-01-c obj) #\Z
(allocate-instance-struct-01-d obj) 'foo)
(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)
;;; Order of evaluation tests
(deftest allocate-instance.order.1
(let* ((class (find-class 'allocate-instance-class-01))
(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)))))
(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
(deftest allocate-instance.error.1
(signals-error (allocate-instance) program-error)
t)
;;; 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
#|
(deftest allocate-instance.error.2
(signals-error (allocate-instance (find-class 'allocate-instance-class-01)
:b)
program-error)
t)
(deftest allocate-instance.error.3
(signals-error (allocate-instance (find-class 'allocate-instance-class-01)
'(a b c) nil)
program-error)
t)
|#