Add ast-traverse helper
[scheme.git] / tests.scm
1 (load "codegen.scm")
2 (load "typecheck.scm")
3
4 (define (test actual expected)
5   (when (not (equal? actual expected))
6     (error #f
7            (format "test failed:\nexpected: ~a\nactual:   ~a"
8                    expected actual))))
9
10 (define (read-file file)
11   (call-with-input-file file
12     (lambda (p)
13       (let loop ((next (read-char p))
14                  (result ""))
15         (if (eof-object? next)
16             result
17             (loop (read-char p) (string-append result (string next))))))))
18
19 (define (test-prog prog output)
20   (compile-to-binary prog "/tmp/test-prog")
21   (system "/tmp/test-prog > /tmp/test-output.txt")
22   (let ((str (read-file "/tmp/test-output.txt")))
23     (test (substring str 0 (- (string-length str) 1))
24           output)))
25
26 (test (typecheck '(lambda (x) (+ ((lambda (y) (x y 3)) 5) 2)))
27       '(abs (abs int (abs int int)) int))
28
29 (test-prog '(+ 1 2) "3")
30 (test-prog '((lambda (x) ((lambda (y) (+ x y)) 42)) 100) "142")
31 ; todo: support recursive let
32 (test-prog '(let ((x (+ 1 32))
33                   (y x))
34               ((lambda (z) (+ 1 z)) (* y x)))
35            "1090")
36