4 (define (test actual expected)
5 (when (not (equal? actual expected))
7 (format "test failed:\nexpected: ~a\nactual: ~a"
10 (define (read-file file)
11 (call-with-input-file file
13 (let loop ((next (read-char p))
15 (if (eof-object? next)
17 (loop (read-char p) (string-append result (string next))))))))
19 (define (test-prog prog exit-code)
20 (compile-to-binary prog "/tmp/test-prog")
21 (system "/tmp/test-prog"))
23 (define (test-prog-stdout prog output)
24 (compile-to-binary prog "/tmp/test-prog")
25 (system "/tmp/test-prog > /tmp/test-output.txt")
26 (let ((str (read-file "/tmp/test-output.txt")))
27 (test (substring str 0 (- (string-length str) 1))
30 (test (typecheck '(lambda (x) (+ ((lambda (y) (x y 3)) 5) 2)))
31 '(abs (abs int (abs int int)) int))
33 (test-prog '(+ 1 2) 3)
34 (test-prog '((lambda (x) ((lambda (y) (+ x y)) 42)) 100) 142)
35 (test-prog '(let ((x (+ 1 32))
37 ((lambda (z) (+ 1 z)) (* y x)))
39 (test-prog '(if ((lambda (x) (= x 2)) 1) 0 (- 32 1)) 31)