forked from emina/rosette
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.rkt
192 lines (165 loc) · 6.52 KB
/
test.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
#lang racket
(require racket/exn
rackunit
rosette/lib/trace/compile
rosette/lib/roseunit
rosette/lib/trace/tool
rosette/lib/util/syntax
(only-in rosette clear-state!)
racket/runtime-path
"../config.rkt")
(define overwrite-mode
(match (current-command-line-arguments)
[(vector "overwrite-on-demand") 'on-demand]
[(vector "overwrite") 'overwrite]
[_ #f]))
(printf "Current mode: ~a\n" (current-command-line-arguments))
;; convert a path to a filename string so that it can be written and read back
(define (path->name p)
(match-define-values (_ fname _) (split-path p))
(path->string fname))
;; normalize datum in a way that is idempotent
(define (serialize e)
(call-with-input-string (with-output-to-string (thunk (write e))) read))
(define (format-output stats trace _original-map)
(define (format-activated-syntax activated-syntax)
(match-define (list stx path line col) activated-syntax)
(list stx (path->name path) line col))
(define (format-stack-elem/tail proc path line col)
(list (or (object-name proc) proc) (path->name path) line col))
(define (format-each-stack-elem/tail elem)
(match elem
[#f #f]
[(list 'uncertified _ _) #f]
[(list _ elem _) (apply format-stack-elem/tail elem)]))
(define (format-first-stack-elem/tail xs)
(match xs
['() '()]
[(cons #f xs) (format-first-stack-elem/tail xs)]
[(cons (list 'certified a b) _) #:when (eq? a b) '()]
[(cons (list _ _ elem) _) (list (apply format-stack-elem/tail elem))]))
(define (format-trace-elem elem)
(match-define (list ex activated-syntax activated-stack pc) elem)
;; get rid of path information
(define ex-out
(parameterize ([error-print-context-length 0])
(substring-top (exn->string ex) 40)))
(define activated-syntax-out (format-activated-syntax activated-syntax))
(define activated-stack-out
(append (format-first-stack-elem/tail activated-stack)
(filter-map format-each-stack-elem/tail activated-stack)))
(list ex-out activated-syntax-out activated-stack-out pc))
(serialize
`((#:stats ,stats)
(#:trace ,(map format-trace-elem (take-top trace 10))))))
(define (take-top xs n)
(for/list ([x (in-list xs)] [_ (in-range n)]) x))
(define (substring-top s n)
(cond
[(<= (string-length s) n) s]
[else (string-append (substring s 0 n) "....")]))
(define ns (current-namespace))
(define (user-agree?)
(printf "Overwrite this file? (y/N): ")
(equal? "y" (string-trim (read-line))))
(define (run-trace-test fname mode)
(define (perform-test output)
(define outpath (build-path here-dir "output" mode (~a fname ".out")))
(define (do-overwrite)
(with-output-to-file outpath #:exists 'replace
(thunk (pretty-write output)))
(printf "Wrote new output for `~a`: ~v\n" fname output))
(cond
[(file-exists? outpath)
(cond
[(not overwrite-mode)
(check-equal? output (call-with-input-file outpath read))]
[(eq? overwrite-mode 'overwrite) (do-overwrite)]
[else (check-equal? output (call-with-input-file outpath read))
(when (and (not (equal? output (call-with-input-file outpath read)))
(user-agree?))
(do-overwrite))])]
[else (do-overwrite)]))
(define (exn-handler e)
((error-display-handler)
(if (exn? e)
(exn-message e)
(~a e))
e)
(perform-test
`((#:error ,(parameterize ([error-print-context-length 0]) (exn->string e))))))
(clear-state!)
(parameterize ([current-compile symbolic-trace-compile-handler]
[current-namespace (make-base-namespace)]
[error-print-width default-error-print-width])
(namespace-attach-module ns 'rosette)
(with-handlers ([exn:fail? exn-handler])
(do-trace
(thunk
(dynamic-require `(file ,(path->string (build-path here-dir "code" fname))) #f))
#:entry-handler
(λ (entry add-trace! _current-original-map)
(add-trace! entry))
#:post-proc
(λ (stats trace original-map)
(perform-test (format-output stats trace original-map)))))))
(define-runtime-path here-dir ".")
(define (run-mode tests mode params)
(for/list ([filename (in-list tests)])
(define test-file (string->path filename))
(test-suite+
(~a (path-string->string test-file) " (" mode ")")
(let loop ([params params])
(match params
['() (run-trace-test test-file mode)]
[(cons (list p v) params) (parameterize ([p v]) (loop params))])))))
(define regular-tests '("ex-1-1.rkt"
"ex-1-2.rkt"
"ex-1-3.rkt"
"ex-2.rkt"
"ex-3.rkt"
"tail.rkt"
"non-tail.rkt"
"core-form.rkt"
"assertion.rkt"
"if.rkt"
"infeasible.rkt"
"macro.rkt"
"macro-define.rkt"
"solver-limitation.rkt"
"test-track-form.rkt"
"error.rkt"
"forall.rkt"
"infeasible-solver.rkt"
"list.rkt"
"list-2.rkt"
"no-error.rkt"
"test-stack.rkt"
"toplevel.rkt"))
(define solver-tests '("assertion.rkt"
"if.rkt"
"infeasible-solver.rkt"
"infeasible.rkt"
"solver-limitation.rkt"))
(define assertion-tests '("assertion.rkt"
"if.rkt"))
(define all-tests '("ex-1-1.rkt"
"ex-1-2.rkt"
"ex-1-3.rkt"
"ex-2.rkt"
"ex-3.rkt"))
(define regular-suites
(run-mode regular-tests "regular" `()))
(define solver-suites
(run-mode solver-tests "solver" `([,symbolic-trace-skip-infeasible-solver? #t])))
(define assertion-suites
(run-mode assertion-tests "assertion" `([,symbolic-trace-skip-assertion? #t])))
(define all-suites
(run-mode all-tests "all" `([,symbolic-trace-skip-infeasible-solver? #t]
[,symbolic-trace-skip-assertion? #t])))
(module+ test
(require rackunit/text-ui)
(for-each run-tests regular-suites)
(for-each run-tests solver-suites)
(for-each run-tests assertion-suites)
(for-each run-tests all-suites))