Add strings and print primitive
[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 exit-code)
20   (compile-to-binary prog "/tmp/test-prog")
21   (system "/tmp/test-prog"))
22
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 str output)))
28
29 (test (typecheck '(lambda (x) (+ ((lambda (y) (x y 3)) 5) 2)))
30       '(abs (abs int (abs int int)) int))
31
32 (test-prog '(+ 1 2) 3)
33 (test-prog '((lambda (x) ((lambda (y) (+ x y)) 42)) 100) 142)
34 (test-prog '(let ((x (+ 1 32))
35                   (y x))
36               ((lambda (z) (+ 1 z)) (* y x)))
37            1090)
38 (test-prog '(if ((lambda (x) (= x 2)) 1) 0 (- 32 1)) 31)
39 (test-prog-stdout '(if (= 3 2) 1 (let () (print "hello world!") 0)) "hello world!")
40