Add WIP on passing about closures
[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   (display prog)
21   (newline)
22   (compile-to-binary prog "/tmp/test-prog")
23   (test (system "/tmp/test-prog") exit-code))
24
25 (define (test-prog-stdout prog output)
26   (display prog)
27   (newline)
28   (compile-to-binary prog "/tmp/test-prog")
29   (system "/tmp/test-prog > /tmp/test-output.txt")
30   (let ((str (read-file "/tmp/test-output.txt")))
31     (test str output)))
32
33 (test (typecheck '(lambda (x) (+ ((lambda (y) (x y 3)) 5) 2)))
34       '(abs (abs int (abs int int)) int))
35
36 (test-prog '(+ 1 2) 3)
37 (test-prog '((lambda (x) ((lambda (y) (+ x y)) 42)) 100) 142)
38
39 (test-prog '(* 10 5) 50)
40
41 (test-prog '(let ((x (+ 1 32))
42                   (y x))
43               ((lambda (z) (+ 2 z)) (* y x)))
44            67) ; exit code modulos at 256
45 (test-prog '(if ((lambda (x) (= x 2)) 1) 0 (- 32 1)) 31)
46
47 (test-prog-stdout '(if (= 3 2) 1 (let () (print "hello world!") 0)) "hello world!")
48
49 (test-prog '((lambda (x y) (+ x y)) 1 2) 3)
50 (test-prog '((lambda (x) (+ ((lambda (y) (+ y 1)) 3) x)) 2) 6)
51
52                                         ; passing closures about
53 (test-prog '((lambda (z) ((lambda (x) (x 1)) (lambda (y) (+ z y)))) 2) 3)