-
Notifications
You must be signed in to change notification settings - Fork 11
/
tests-driver.scm
135 lines (112 loc) · 3.58 KB
/
tests-driver.scm
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
(define all-tests '())
(define-syntax add-tests-with-string-output
(syntax-rules (=>)
[(_ test-name [expr => output-string] ...)
(set! all-tests
(cons
'(test-name [expr string output-string] ...)
all-tests))]))
;(define (run-compile expr)
; (let ([p (open-output-file "stst.s" 'replace)])
; (emit-program expr p)
; (close-output-port p)))
(define (build)
(unless (zero? (system "make"))
(error 'make "could not build target")))
;(define (execute)
; (unless (zero? (system "./stst > stst.out"))
; (error 'make "produced program exited abnormally")))
;(define (build-program expr)
; (run-compile expr)
; (build))
;(define (get-string)
; (with-output-to-string
; (lambda ()
; (with-input-from-file "stst.out"
; (lambda ()
; (let f ()
; (let ([c (read-char)])
; (cond
; [(eof-object? c) (void)]
; [else (display c) (f)]))))))))
;
;(define (test-with-string-output test-id expr expected-output)
; (run-compile expr)
; (build)
; (execute)
; (unless (string=? expected-output (get-string))
; (error 'test "output mismatch for test ~s, expected ~s, got ~s"
; test-id expected-output (get-string))))
(define (test-one test-id test)
(let ([expr (car test)]
[type (cadr test)]
[out (caddr test)])
(printf "test ~s:~s ..." test-id expr)
(flush-output-port)
(case type
[(string) (test-with-string-output test-id expr out)]
[else (errorf 'test "invalid test type ~s" type)])
(printf " ok\n")))
(define (test-all)
(let f ([i 0] [ls (reverse all-tests)])
(if (null? ls)
(printf "passed all ~s tests\n" i)
(let ([x (car ls)] [ls (cdr ls)])
(let* ([test-name (car x)]
[tests (cdr x)]
[n (length tests)])
(printf "Performing ~a tests ...\n" test-name)
(let g ([i i] [tests tests])
(cond
[(null? tests) (f i ls)]
[else
(test-one i (car tests))
(g (add1 i) (cdr tests))])))))))
(define input-filter
(make-parameter (lambda (x) x)
(lambda (x)
(unless (procedure? x)
(errorf 'input-filter "not a procedure ~s" x))
x)))
(define runtime-file
(make-parameter
"runtime.c"
(lambda (fname)
(unless (string? fname) (error 'runtime-file "not a string" fname))
fname)))
(define compile-port
(make-parameter
(current-output-port)
(lambda (p)
(unless (output-port? p)
(errorf 'compile-port "not an output port ~s" p))
p)))
(define show-compiler-output (make-parameter #f))
(define (run-compile expr)
(let ([p (open-output-file "stst.s" 'replace)])
(parameterize ([compile-port p])
(emit-program expr))
(close-output-port p)))
(define (execute)
(unless (fxzero? (system "./stst > stst.out"))
(error 'execute "produced program exited abnormally")))
(define (get-string)
(with-output-to-string
(lambda ()
(with-input-from-file "stst.out"
(lambda ()
(let f ()
(let ([c (read-char)])
(cond
[(eof-object? c) (void)]
[else (display c) (f)]))))))))
(define (test-with-string-output test-id expr expected-output)
(run-compile expr)
(build)
(execute)
(unless (string=? expected-output (get-string))
(errorf 'test "output mismatch for test ~s, expected ~s, got ~s"
test-id expected-output (get-string))))
(define (emit . args)
(apply fprintf (compile-port) args)
(newline (compile-port)))