/
rapl_interpreter_tests.rkt
244 lines (196 loc) · 9.35 KB
/
rapl_interpreter_tests.rkt
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
#lang plai
(require "rapl.rkt")
(require "rapl_parser.rkt")
(require "rapl_interpreter.rkt")
(require "rapl_serialization.rkt")
(print-only-errors)
(current-directory (build-path (current-directory) "examples"))
(test (parse '(let ([const5 (lambda (_) 5)])
(+ 10 (const5 10))))
(letC 'const5 (lamC '(_) (numC 5))
(plusC (numC 10) (appC (idC 'const5) (list (numC 10))))))
(test (parse '(lambda (x y z) (x y z)))
(lamC '(x y z) (appC (idC 'x) (list (idC 'y) (idC 'z)))))
(test/exn (parse '(+ 1 2 3))
"#<procedure:plusC29> \"Wrong number of arguments\"")
(test (parse '(quote a)) (symbolC 'a))
(test (parse '(void)) (voidC))
(test (interp-exp (parse
'(let ([const5 (lambda (_) 5)])
(+ 10 (const5 10)))))
(numV 15))
(test (interp-exp (parse
'(let ([double (lambda (x) (+ x x))])
(+ 10 (double (+ 1 2))))))
(numV 16))
(test (interp-exp (parse
'(let ([double (lambda (x) (+ x x))])
(let ([quadruple (lambda (x) (double (double x)))])
(+ 10 (quadruple (+ 1 2)))))))
(numV 22))
(test (interp-exp (parse
'(let ([sum (lambda (x y z) (+ x (+ y z)))])
(sum 1 2 3))))
(numV 6))
(test (interp-exp (parse '((file "fact.ttpl") 4)))
(numV 24))
(test (interp-exp (parse
'(((file "around.ttpl")
((file "call.ttpl") 'change)
(lambda (proceed) (lambda (y) (proceed (* y 2))))
(lambda ()
(let ([change (tag 'change (lambda (x) (+ x 5)))])
(change 2)))))))
(numV 9))
(test (interp-exp (parse
'(((file "around.ttpl")
((file "call.ttpl") 'change)
(lambda (proceed) (lambda (y) (proceed (* y 2))))
((file "around.ttpl")
((file "call.ttpl") 'change)
(lambda (proceed) (lambda (y) (proceed (+ y 3))))
(lambda ()
(let ([change (tag 'change (lambda (x) (+ x 5)))])
(change 2))))))))
(numV 12))
(test (interp-exp (parse
'(((file "around.ttpl")
((file "call.ttpl") 'change)
(lambda (proceed) (lambda (y) (proceed (+ y 3))))
((file "around.ttpl")
((file "call.ttpl") 'change)
(lambda (proceed) (lambda (y) (proceed (* y 2))))
(lambda ()
(let ([change (tag 'change (lambda (x) (+ x 5)))])
(change 2))))))))
(numV 15))
(define-type ValueWithOutput
[v*o (v Value?) (o (listof string?))])
(define (with-output [p procedure?]) ValueWithOutput?
(let* ([output (box '())]
[old-sink (unbox write-sink)]
[_ (set-box! write-sink (lambda (s) (list-box-push! output s)))]
[result (p)]
[__ (set-box! write-sink old-sink)])
(v*o result (reverse (unbox output)))))
(define (interp-exp-with-output [exp ExprC?]) ValueWithOutput?
(with-output (lambda () (interp-exp exp))))
(test (interp-exp-with-output (writeC "The answer" (numC 42)))
(v*o (voidV) '("The answer: 42")))
(test (interp-exp-with-output (parse '(((file "fact_advice.ttpl") (lambda () ((file "fact.ttpl") 3))))))
(v*o (numV 6) '("y: 3"
"y: 2"
"y: 1"
"y: 0"
"result: 1"
"result: 1"
"result: 2"
"result: 6")))
(test (interp-exp-with-output (parse '(((file "fact_boxes_advice.ttpl") (lambda () ((file "fact_boxes.ttpl") 3))))))
(v*o (numV 6) '("y before: 3"
"y before: 2"
"y before: 1"
"y before: 0"
"y after: 0"
"result: 1"
"y after: 0"
"result: 1"
"y after: 0"
"result: 2"
"y after: 0"
"result: 6")))
(test (interp-exp-with-output (fileC "double_trace_of_double_thunk.ttpl"))
(v*o (numV 4) '("call: tag2"
"call: tag2"
"call: tag1"
"call: tag1")))
(test (struct->list/recursive (numC 4)) '(numC 4))
(test (struct->list/recursive (plusC (numC 3) (numC 4))) '(plusC (numC 3) (numC 4)))
(test (struct->list/recursive (appC (idC 'foo) (list (numC 4)))) '(appC (idC 'foo) (list (numC 4))))
(test (struct->list/recursive (lamC (list 'foo) (idC 'foo))) '(lamC (list 'foo) (idC 'foo)))
(define (test-roundtrip e) (test (list->struct/recursive rapl-ns (struct->list/recursive e)) e))
(test-roundtrip (plusC (numC 3) (numC 4)))
(test-roundtrip (appC (idC 'foo) (list (numC 4))))
(test-roundtrip (lamC (list 'bar) (multC (numC 4) (idC 'bar))))
(define (interp-query-with-output [trace-file string?] [exps list?]) ValueWithOutput?
(with-output (lambda () (interp-query trace-file exps))))
; Create fact_trace.txt for the next few tests
(define fact-trace-path "traces/fact_trace.txt")
(test (interp-with-tracing (list (parse '((file "fact.ttpl") 3))) fact-trace-path)
(numV 6))
(test (interp-query-with-output fact-trace-path (list (fileC "fact_advice.ttpl")))
(v*o (numV 6) '("y: 3"
"y: 2"
"y: 1"
"y: 0"
"result: 1"
"result: 1"
"result: 2"
"result: 6")))
(test/exn (interp-query "traces/fact_trace.txt" (list (fileC "fact_advice_arg.ttpl")))
"retroactive-side-effect: incorrect argument passed retroactively: expected\n (#(struct:numV 3)) but got\n (#(struct:numV 2))")
(test/exn (interp-query "traces/fact_trace.txt" (list (fileC "fact_advice_base_case.ttpl")))
"retroactive-side-effect: incorrect retroactive result: expected\n #(struct:numV 1) but got\n #(struct:numV 7)")
(test/exn (interp-query "traces/fact_trace.txt" (list (fileC "fact_advice_no_proceed.ttpl")))
"retroactive-side-effect: retroactive advice did not proceed")
(test/exn (interp-query "traces/fact_trace.txt" (list (fileC "fact_advice_double_proceed.ttpl")))
"retroactive-side-effect: retroactive advice proceeded out of order")
(test/exn (interp-query "traces/fact_trace.txt" (list (fileC "fact_advice_bad_read.ttpl")))
"retroactive-side-effect: attempt to retroactively read input")
; Create fact_boxes_trace.txt for the next few tests
(define fact-boxes-trace-path "traces/fact_boxes_trace.txt")
(test (interp-with-tracing (list (parse '((file "fact_boxes.ttpl") 3))) fact-boxes-trace-path)
(numV 6))
(test (interp-query-with-output fact-boxes-trace-path (list (fileC "fact_boxes_advice.ttpl")))
(v*o (numV 6) '("y before: 3"
"y before: 2"
"y before: 1"
"y before: 0"
"y after: 0"
"result: 1"
"y after: 0"
"result: 1"
"y after: 0"
"result: 2"
"y after: 0"
"result: 6")))
(test/exn (interp-query-with-output "traces/fact_boxes_trace.txt" (list (fileC "fact_boxes_advice_bad_set_box.ttpl")))
"retroactive-side-effect: attempt to retroactively set box")
(test (interp-exp-with-output (fileC "test_advice_in_trace_inline.ttpl"))
(v*o (numV 6) '("call: fact"
"call: fact"
"call: fact"
"call: fact"
"call: fact_helper"
"call: fact_helper"
"call: fact_helper"
"call: fact_helper"
"check: #t")))
; Create fact_traced_trace.txt for the next few tests
(define fact-traced-trace-path "traces/fact_traced_trace.txt")
(test (interp-with-tracing (list (parse '(((file "trace.ttpl") (lambda () ((file "fact.ttpl") 3)))))) fact-traced-trace-path)
(numV 6))
(test/exn (interp-query-with-output fact-traced-trace-path (list (fileC "inject_fact_boxes.ttpl")))
"retroactive-side-effect: traces with advice are not supported")
(define empty-trace-path "traces/empty_trace.txt")
(test (interp-with-tracing (list (parse '42)) empty-trace-path)
(numV 42))
(test (interp-query-with-output empty-trace-path (list (fileC "fact_advice.ttpl")))
(v*o (numV 42) '()))
;; Check the unsafe but much simpler versions used for the purpose of presentation
(set-box! retroactive-error-checking #f)
(test (interp-query-with-output fact-boxes-trace-path (list (fileC "fact_boxes_advice.ttpl")))
(v*o (numV 6) '("y before: 3"
"y before: 2"
"y before: 1"
"y before: 0"
"y after: 0"
"result: 1"
"y after: 0"
"result: 1"
"y after: 0"
"result: 2"
"y after: 0"
"result: 6")))
;;(map (lambda (jp) (begin (display-joinpoint jp (current-output-port)) (newline))) (raplTrace-joinpoints fact-trace))
(display "Done!")