-
Notifications
You must be signed in to change notification settings - Fork 0
/
srfi-78.ss
300 lines (269 loc) · 9.81 KB
/
srfi-78.ss
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
; <PLAINTEXT>
; Copyright (c) 2005-2006 Sebastian Egner.
;
; Permission is hereby granted, free of charge, to any person obtaining
; a copy of this software and associated documentation files (the
; ``Software''), to deal in the Software without restriction, including
; without limitation the rights to use, copy, modify, merge, publish,
; distribute, sublicense, and/or sell copies of the Software, and to
; permit persons to whom the Software is furnished to do so, subject to
; the following conditions:
;
; The above copyright notice and this permission notice shall be
; included in all copies or substantial portions of the Software.
;
; THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND,
; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
; NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
; LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
; OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
; WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
;
; -----------------------------------------------------------------------
;
; Lightweight testing (reference implementation)
; ==============================================
;
; in R5RS + SRFI 23 (error) + SRFI 42 (comprehensions)
;
; history of this file:
; SE, 25-Oct-2004: first version based on code used in SRFIs 42 and 67
; SE, 19-Jan-2006: (arg ...) made optional in check-ec
;
; Naming convention "check:<identifier>" is used only internally.
; -- portability --
; PLT: (require (lib "23.ss" "srfi") (lib "42.ss" "srfi"))
; Scheme48: ,open srfi-23 srfi-42
(library (srfi-78)
(export
check
check-ec
check-report
check-set-mode!
check-reset!
check-passed?
)
(import
(rnrs)
(srfi-42)
)
; -- utilities --
(define check:write write)
; You can also use a pretty printer if you have one.
; However, the output might not improve for most cases
; because the pretty printers usually output a trailing
; newline.
; PLT: (require (lib "pretty.ss")) (define check:write pretty-print)
; Scheme48: ,open pp (define check:write p)
; -- mode --
(define check:mode
(let ([mode 100])
(case-lambda
[() mode]
[(new-mode)
(set! mode
(case new-mode
[(off) 0]
[(summary) 1]
[(report-failed) 10]
[(report) 100]
[else (error 'check:mode "unrecognized mode: ~s" mode)]))])))
(define check-set-mode!
(lambda (mode)
(check:mode mode)))
; -- state --
(define check:correct
(let ([correct 0])
(case-lambda
[() correct]
[(new-correct) (set! correct new-correct)])))
(define check:failed
(let ([failed '()])
(case-lambda
[() failed]
[(new-failed) (set! failed new-failed)])))
(define check-reset!
(lambda ()
(check:correct 0)
(check:failed '())))
(define check:add-correct!
(lambda ()
(check:correct (+ (check:correct) 1))))
(define check:add-failed!
(lambda (expression actual-result expected-result)
(check:failed
(cons (list expression actual-result expected-result)
(check:failed)))))
; -- reporting --
(define check:report-expression
(lambda (expression)
(newline)
(check:write expression)
(display " => ")))
(define check:report-actual-result
(lambda (actual-result)
(check:write actual-result)
(display " ; ")))
(define check:report-correct
(lambda (cases)
(display "correct")
(if (not (= cases 1))
(begin (display " (")
(display cases)
(display " cases checked)")))
(newline)))
(define check:report-failed
(lambda (expected-result)
(display "*** failed ***")
(newline)
(display " ; expected result: ")
(check:write expected-result)
(newline)))
(define check-report
(lambda ()
(if (>= (check:mode) 1)
(begin
(newline)
(display "; *** checks *** : ")
(display (check:correct))
(display " correct, ")
(display (length (check:failed)))
(display " failed.")
(if (or (null? (check:failed)) (<= (check:mode) 1))
(newline)
(let* ([w (car (reverse (check:failed)))]
[expression (car w)]
[actual-result (cadr w)]
[expected-result (caddr w)])
(display " First failed example:")
(newline)
(check:report-expression expression)
(check:report-actual-result actual-result)
(check:report-failed expected-result)))))))
(define check-passed?
(lambda (expected-total-count)
(and (= (length (check:failed)) 0)
(= (check:correct) expected-total-count))))
; -- simple checks --
(define check:proc
(lambda (expression thunk equal expected-result)
(case (check:mode)
[(0) #f]
[(1)
(let ([actual-result (thunk)])
(if (equal actual-result expected-result)
(check:add-correct!)
(check:add-failed!
expression
actual-result
expected-result)))]
[(10)
(let ([actual-result (thunk)])
(if (equal actual-result expected-result)
(check:add-correct!)
(begin
(check:report-expression expression)
(check:report-actual-result actual-result)
(check:report-failed expected-result)
(check:add-failed!
expression
actual-result
expected-result))))]
[(100)
(check:report-expression expression)
(let ([actual-result (thunk)])
(check:report-actual-result actual-result)
(if (equal actual-result expected-result)
(begin (check:report-correct 1) (check:add-correct!))
(begin
(check:report-failed expected-result)
(check:add-failed!
expression
actual-result
expected-result))))]
[else (error "unrecognized mode" (check:mode))])
(if #f #f)))
(define-syntax check
(syntax-rules (=>)
[(check expr => expected) (check expr (=> equal?) expected)]
[(check expr (=> equal) expected)
(if (>= (check:mode) 1)
(check:proc 'expr (lambda () expr) equal expected))]))
; -- parametric checks --
(define check:proc-ec
(lambda (w)
(let ([correct? (car w)]
[expression (cadr w)]
[actual-result (caddr w)]
[expected-result (cadddr w)]
[cases (car (cddddr w))])
(if correct?
(begin
(if (>= (check:mode) 100)
(begin
(check:report-expression expression)
(check:report-actual-result actual-result)
(check:report-correct cases)))
(check:add-correct!))
(begin
(if (>= (check:mode) 10)
(begin
(check:report-expression expression)
(check:report-actual-result actual-result)
(check:report-failed expected-result)))
(check:add-failed!
expression
actual-result
expected-result))))))
(define-syntax check-ec:make
(syntax-rules (=>)
[(check-ec:make qualifiers expr (=> equal) expected (arg ...))
(if (>= (check:mode) 1)
(check:proc-ec
(let ([cases 0])
(let ([w (first-ec #f qualifiers
(:let equal-pred equal)
(:let expected-result expected)
(:let actual-result (let ([arg arg] ...) expr))
(begin (set! cases (+ cases 1)))
(if (not (equal-pred
actual-result
expected-result)))
(list
(list 'let (list (list 'arg arg) ...) 'expr)
actual-result
expected-result
cases))])
(if w
(cons #f w)
(list #t
'(check-ec qualifiers expr (=> equal) expected
(arg ...))
(if #f #f) (if #f #f) cases))))))]))
; (*) is a compile-time check that (arg ...) is a list
; of pairwise disjoint bound variables at this point.
(define-syntax check-ec
(syntax-rules (nested =>)
[(check-ec expr => expected)
(check-ec:make (nested) expr (=> equal?) expected ())]
[(check-ec expr (=> equal) expected)
(check-ec:make (nested) expr (=> equal) expected ())]
[(check-ec expr => expected (arg ...))
(check-ec:make (nested) expr (=> equal?) expected (arg ...))]
[(check-ec expr (=> equal) expected (arg ...))
(check-ec:make (nested) expr (=> equal) expected (arg ...))]
[(check-ec qualifiers expr => expected)
(check-ec:make qualifiers expr (=> equal?) expected ())]
[(check-ec qualifiers expr (=> equal) expected)
(check-ec:make qualifiers expr (=> equal) expected ())]
[(check-ec qualifiers expr => expected (arg ...))
(check-ec:make qualifiers expr (=> equal?) expected (arg ...))]
[(check-ec qualifiers expr (=> equal) expected (arg ...))
(check-ec:make qualifiers expr (=> equal) expected (arg ...))]
[(check-ec (nested q1 ...) q etc ...)
(check-ec (nested q1 ... q) etc ...)]
[(check-ec q1 q2 etc ...)
(check-ec (nested q1 q2) etc ...)]))
)