-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
quid-pro-quo-test.lisp
358 lines (292 loc) · 11.9 KB
/
quid-pro-quo-test.lisp
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
(defpackage quid-pro-quo-test
(:use #:quid-pro-quo #:closer-common-lisp #:closer-mop #:fiveam)
(:export #:test-qpq))
(in-package #:quid-pro-quo-test)
(def-suite tests)
(in-suite tests)
(defgeneric test-qpq (arg1 arg2)
(:method-combination contract :invariant-check nil)
(:method :require "first arg > 123" ((m integer) (n number))
(> m 123))
(:method :require "second arg < 100" ((m number) (n integer))
(print "more strict")
(< n 100))
(:method :require "first arg = 12345678900987654321.0" ((m number) (n number))
(print "less strict")
(= m 12345678900987654321.0))
(:method :around ((m number) (n number))
(call-next-method))
(:method :before ((m number) (n number))
(list (- m 1) (- n 1)))
(:method ((m number) (n number))
(list m n))
(:method :after ((m number) (n number))
(list (+ m 1) (+ n 1)))
(:method :guarantee "results are fixnum" ((m integer) (n integer))
(<= most-negative-fixnum (reduce #'+ (results)) most-positive-fixnum))
(:method :guarantee "999" ((m number) (n integer))
999)
(:method :guarantee "always true" ((m number) (n number))
t))
(test should-warn-overly-strict-precondition
(signals overly-strict-precondition-warning
(test-qpq 12345678900987654321.0 100)))
(test should-not-warn-overly-strict-precondition
(with-contracts-enabled (:invariants t :preconditions nil :postconditions t)
(is (equal (list 12345678900987654321.0 100)
(test-qpq 12345678900987654321.0 100)))))
;; (test should-have-correct-method-in-warning
;; (handler-case
;; (progn
;; (test-qpq 12345678900987654321.0 100)
;; (fail "Failed to signal OVERLY-STRICT-PRECONDITION-WARNING."))
;; (overly-strict-precondition-warning (c)
;; (is (eq (fdefinition 'test-qpq) (slot-value c 'qpq::function))))))
(test should-succeed-with-integers
(is (equal (list 124 2) (test-qpq 124 2))))
(test should-fail-n-<-100-precondition
(signals precondition-error
(test-qpq 1 12345678900987654321.0)))
(test should-not-fail-n-<-100-precondition
(with-contracts-enabled (:preconditions nil)
(is (equal (list 1 12345678900987654321.0)
(test-qpq 1 12345678900987654321.0)))))
;; (test should-have-correct-method-in-precondition-error
;; (handler-case
;; (progn
;; (test-qpq 1 12345678900987654321.0)
;; (fail "Failed to signal PRECONDITION-ERROR."))
;; (precondition-error (c)
;; (is (eq (fdefinition 'test-qpq)
;; (method-generic-function (slot-value c 'qpq::failed-check)))))))
(test should-fail-result-postcondition
(signals postcondition-error
(test-qpq most-positive-fixnum most-positive-fixnum)))
(test should-not-fail-result-postcondition
(with-contracts-disabled ()
(is (equal (list most-positive-fixnum most-positive-fixnum)
(test-qpq most-positive-fixnum most-positive-fixnum)))))
;; (test should-have-correct-method-in-postcondition-error
;; (handler-case
;; (progn
;; (test-qpq most-positive-fixnum most-positive-fixnum)
;; (fail "Failed to signal POSTCONDITION-ERROR."))
;; (postcondition-error (c)
;; (is (eq (fdefinition 'test-qpq)
;; (method-generic-function (slot-value c 'qpq::failed-check)))))))
;; FIXME: This is here so that the tests compile on CLISP. However, it shouldn't
;; be necessary, as defining a CONTRACTED-CLASS should guarantee that all
;; accessors for slots on that class have the correct method combination.
#+clisp
(defgeneric my-slot (x)
(:method-combination contract))
(defclass foo ()
((my-slot :accessor my-slot :initform nil)
(your-slot :accessor your-slot :initform t))
(:metaclass contracted-class)
(:invariants (lambda (instance)
(declare (ignore instance))
t)))
(defclass bar (foo)
((yet-another-slot :accessor yet-another-slot :initform 'yas))
(:metaclass contracted-class)
(:invariants (lambda (instance)
(declare (ignore instance))
t)))
(defrequirement my-slot ((bar bar))
t)
(defguarantee my-slot ((bar bar))
t)
(defclass bar-2 (foo)
()
(:metaclass contracted-class)
(:invariants (lambda (instance)
(declare (ignore instance))
t)))
#| Example:
(let* ((my-foo (make-instance 'foo))
(a-slot (progn (format t " !! Accessing my-slot.~%")
(my-slot my-foo))))
(setf (my-slot my-foo) (progn (format t " !! Setting my-slot.~%")
9999))
(list (my-slot my-foo) a-slot (your-slot my-foo)))
(let* ((my-bar (make-instance 'bar))
(a-slot (progn (format t " !! Accessing my-slot.~%")
(my-slot my-bar))))
(setf (my-slot my-bar) (progn (format t " !! Setting my-slot.~%")
9999))
(list (my-slot my-bar) a-slot (your-slot my-bar)))
(let* ((my-bar-2 (make-instance 'bar-2))
(a-slot (progn (format t " !! Accessing my-slot.~%")
(my-slot my-bar-2))))
(setf (my-slot my-bar-2) (progn (format t " !! Setting my-slot.~%")
9999))
(list (my-slot my-bar-2) a-slot (your-slot my-bar-2)))
(my-slot (make-instance 'bar))
(yet-another-slot (make-instance 'bar))
(my-slot (make-instance 'bar-2))
|#
(defclass test-1 ()
((my-slot :accessor my-slot :initarg :my-slot :initform 0))
(:metaclass contracted-class)
(:invariants (lambda (instance)
"Invariant of test"
(numberp (slot-value instance 'my-slot)))
(lambda (instance)
(< (slot-value instance 'my-slot)
4))))
(defclass test-2 (test-1)
((another-slot :accessor another-slot :initarg :another-slot
:initform nil))
(:metaclass contracted-class)
(:invariants (lambda (instance)
"Test-2 invariant"
(< (length (slot-value instance 'another-slot))
4))))
(test should-fail-invariant-after-writer
(signals after-invariant-error
(setf (my-slot (make-instance 'test-1)) 5)))
(test should-not-fail-invariant-after-writer
(with-contracts-enabled (:invariants nil)
(let ((test (make-instance 'test-1)))
(setf (my-slot test) 5)
(is (= 5 (my-slot test))))))
(test should-have-correct-values-in-invariant-error
(let ((test (make-instance 'test-1)))
(handler-case
(progn
(setf (my-slot test) 5)
(fail "Failed to signal AFTER-INVARIANT-ERROR."))
(after-invariant-error (c)
(is (eq #'(setf my-slot)
(method-generic-function (slot-value c 'qpq::failed-check))))
(is (eq test (slot-value c 'qpq::object)))))))
(test should-fail-on-invariant-of-superclass
(signals after-invariant-error
(setf (my-slot (make-instance 'test-2)) nil)))
(defrequirement test-qpq ((m test-2) (n test-1))
"first arg < 123"
(< (my-slot m) 123))
(defrequirement test-qpq ((m test-1) (n test-2))
"second arg needs null another-slot"
(null (another-slot n)))
(defrequirement test-qpq ((m test-1) (n test-1))
"first arg needs non-zero my-slot"
(not (zerop (my-slot m))))
(defmethod test-qpq :around ((m test-1) (n test-1))
(call-next-method))
(defmethod test-qpq :before ((m test-1) (n test-1))
(list m n 'before))
(defmethod test-qpq ((m test-1) (n test-1))
(list m n))
(defmethod test-qpq :after ((m test-1) (n test-1))
(list m n 'after))
(defguarantee test-qpq ((m test-1) (n test-2))
(null (another-slot n)))
(defguarantee test-qpq ((m test-1) (n test-1))
(or (zerop (my-slot m)) (zerop (my-slot n))))
(defun fail-invariant (m)
(setf (my-slot m) nil))
(test should-succeed-with-test-objects
(let ((first (make-instance 'test-1 :my-slot 1))
(second (make-instance 'test-1)))
(is (equal (list first second) (test-qpq first second)))))
(test should-fail-not-zerop-my-slot-precondition
(let ((first (make-instance 'test-1))
(second (make-instance 'test-1)))
(signals precondition-error
(test-qpq first second))))
(test should-pass-with-weakened-precondition
(let ((first (make-instance 'test-2))
(second (make-instance 'test-1)))
;; This succeeds because the method TEST-QPQ has a weakened precondition for
;; first arguments of type TEST-2.
(is (equal (list first second) (test-qpq first second)))))
(test should-fail-zerop-my-slot-postcondition
(let ((first (make-instance 'test-1 :my-slot 1))
(second (make-instance 'test-1 :my-slot 1)))
(signals postcondition-error
(test-qpq first second))))
(test should-fail-with-weakened-postcondition
(let ((first (make-instance 'test-1 :my-slot 1))
(second (make-instance 'test-2 :my-slot 1)))
;; The weakened postcondition for second argument of class TEST-2 does not
;; cause the method to succeed.
(signals postcondition-error
(test-qpq first second))))
(test should-create-successfully
(is (typep (make-instance 'test-1 :my-slot -1)
'test-1)))
(test should-fail-invariant-at-creation
(signals creation-invariant-error
(make-instance 'test-1 :my-slot nil)))
(test should-fail-invariant-after-method-call
(signals after-invariant-error
(fail-invariant (make-instance 'test-1))))
(defclass non-contracted-superclass ()
((foo :initform 10 :initarg :foo :accessor foo)))
(defclass contracted-subclass (non-contracted-superclass)
()
(:metaclass contracted-class)
(:invariants (lambda (instance)
(> (slot-value instance 'foo) 5))))
(test should-fail-invariant-on-subclass-creation
(signals creation-invariant-error
(make-instance 'contracted-subclass :foo 5)))
(test should-fail-invariant-before-setf
(let ((instance (with-contracts-disabled ()
(make-instance 'contracted-subclass :foo 5))))
(signals before-invariant-error
(setf (foo instance) 6))))
(test should-fail-invariant-on-superclass-writer
(let ((instance (make-instance 'contracted-subclass)))
(signals after-invariant-error
(setf (foo instance) 5))))
#| FIXME: currently this results in a stack overflow
(defclass inv-class ()
((foo :initform 10 :initarg :foo :accessor foo))
(:metaclass contracted-class)
(:invariants (lambda (instance)
(> (foo instance) 5))))
(test should-not-recurse-on-reader-in-invariant
(is (typep (make-instance 'inv-class) 'inv-class)))
|#
;;; This next section uses a bunch of features without much rigor, just to make
;;; sure everything seems to work.
(defclass feature-test ()
((slot1 :accessor slot1 :initarg :slot1 :initform 0 :type integer))
(:metaclass contracted-class)
(:invariants (lambda (instance)
(numberp (slot-value instance 'slot1)))
"yet another invariant"))
(defclass feature-test-2 (feature-test)
((slot2 :accessor slot2 :initarg :slot2 :type integer)))
(test should-pass-type-invariant-when-slot-is-unbound
(is (= 0 (slot1 (make-instance 'feature-test-2)))))
(defgeneric test-qpq-/ (arg1 arg2)
(:method-combination contract :invariant-check nil)
(:method :require "first arg isn't zero" ((m feature-test) (n feature-test))
(not (zerop (slot1 m))))
(:method ((m feature-test) (n feature-test))
(/ (slot1 n) (slot1 m))))
(test should-fail-not-zerop-precondition
(signals precondition-error
(test-qpq-/ (make-instance 'feature-test) (make-instance 'feature-test))))
(test should-fail-type-invariant
;; NOTE: fall back to type error, in case the compiler does the type check
(signals ((or creation-invariant-error type-error))
(make-instance 'feature-test :slot1 nil)))
(test should-succeed-and-divide
(is (= 4
(test-qpq-/ (make-instance 'feature-test :slot1 2)
(make-instance 'feature-test :slot1 8)))))
(defclass function-invariant-test ()
()
(:metaclass contracted-class)
(:invariants #'integerp))
(test should-fail-invariant-function
(signals creation-invariant-error
(make-instance 'function-invariant-test)))
(test should-upgrade-subclass
(is-true (typep (defclass sub-invariant-test (function-invariant-test) ())
'contracted-class)))