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)
22 (compile-to-binary prog "/tmp/test-prog" 'darwin)
23 (test (system "/tmp/test-prog") exit-code))
25 (define (test-prog-stdout prog output)
28 (compile-to-binary prog "/tmp/test-prog" 'darwin)
29 (system "/tmp/test-prog > /tmp/test-output.txt")
30 (let ((str (read-file "/tmp/test-output.txt")))
33 (test (typecheck '(lambda (x) (+ ((lambda (y) (x y 3)) 5) 2)))
34 '(abs (abs int (abs int int)) int))
36 (test-prog '(+ 1 2) 3)
37 (test-prog '((lambda (x) ((lambda (y) (+ x y)) 42)) 100) 142)
39 (test-prog '(* 10 5) 50)
41 (test-prog '(let ((x (+ 1 32))
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)
47 (test-prog-stdout '(if (= 3 2) 1 (let () (print "hello world!") 0)) "hello world!")
49 (test-prog '((lambda (x y) (+ x y)) 1 2) 3)
50 (test-prog '((lambda (x) (+ ((lambda (y) (+ y 1)) 3) x)) 2) 6)
52 ; passing closures about
53 (test-prog '((lambda (z) ((lambda (x) (x 1)) (lambda (y) (+ z y)))) 2) 3)