4 (define (test-f pred actual expected)
5 (when (not (pred actual expected))
7 (format "test failed:\nexpected: ~a\nactual: ~a"
10 (define (test . xs) (apply test-f (cons equal? xs)))
12 (define-syntax test-types
18 (test-f types-equal? a e)))))
20 (define (read-file file)
21 (call-with-input-file file
23 (let loop ((next (read-char p))
25 (if (eof-object? next)
27 (loop (read-char p) (string-append result (string next))))))))
29 (define (test-prog prog exit-code)
32 (compile-to-binary prog "/tmp/test-prog" host-os)
33 (test (system "/tmp/test-prog") exit-code))
35 (define (test-prog-stdout prog output)
38 (compile-to-binary prog "/tmp/test-prog" host-os)
39 (system "/tmp/test-prog > /tmp/test-output.txt")
40 (let ((str (read-file "/tmp/test-output.txt")))
43 (test-types (typecheck '(lambda (x) (+ ((lambda (y) (x y 3)) 5) 2)))
44 '(abs (abs int (abs int int)) int))
48 (test-types (substitute '((t1 . (abs t1 t10))) 't1) '(abs t1 t10))
50 (test-types (typecheck '(let ([bar (lambda (y) y)]
51 [foo (lambda (x) (foo (bar #t)))])
55 (test-types (typecheck '(let ([bar (lambda (y) y)]
56 [foo (lambda (x) (foo (bar #t)))])
60 (test-types (typecheck '(let ([foo 3]
66 (test-types (typecheck '(let ([foo 3]
72 (test-types (typecheck '(let ([foo 3]
79 (test-types (typecheck '(let ([f (lambda (n) (if (= n 0)
82 [g (lambda (m) (f m))])
86 (test-types (typecheck '(let ([pow (lambda (p y)
87 (let ([go (lambda (n x)
90 (go (- n 1) (* x y))))])
95 (test-prog '(+ 1 2) 3)
96 (test-prog '(bool->int (= 2 0)) 0)
97 (test-prog '((lambda (x) ((lambda (y) (+ x y)) 42)) 100) 142)
99 (test-prog '(* 10 5) 50)
101 (test-prog '(let ((x (+ 1 32))
103 ((lambda (z) (+ 2 z)) (* y x)))
104 67) ; exit code modulos at 256
105 (test-prog '(if ((lambda (x) (= x 2)) 1) 0 (- 32 1)) 31)
107 (test-prog-stdout '(if (= 3 2) 1 (let () (print "hello world!") 0)) "hello world!")
109 (test-prog '((lambda (x y) (+ x y)) 1 2) 3)
110 (test-prog '((lambda (x) (+ ((lambda (y) (+ y 1)) 3) x)) 2) 6)
112 ; passing closures about
113 (test-prog '((lambda (z) ((lambda (x) (x 1)) (lambda (y) (+ z y)))) 2) 3)
115 ; passing builtins about
116 (test-prog '((lambda (x) ((lambda (a b) (a b 3)) + x)) 3) 6)
117 (test-prog '(bool->int ((lambda (x) (x #f)) !)) 1)
118 (test-prog '((lambda (f) (f #t)) bool->int) 1)
119 (test-prog-stdout '(let () ((lambda (f) (f "foo")) print) 0) "foo")
120 (test-prog '((lambda (f) (f 3 3)) (lambda (x y) (bool->int (= x y)))) 1)
121 (test-prog '(bool->int ((lambda (f) (! (f 2 3))) =)) 1)
124 (test-prog '(let [(inc (lambda (f n x)
127 (f f (- n 1) (+ x 1)))))]
131 (test-prog '(let ([go (lambda (n m x)
134 (go (- n 1) m (* x m))))]
135 [pow (lambda (p y) (go p y 1))])
140 (test-prog '(let ([pow (lambda (p y)
141 (let ([go (lambda (n x)
144 (go (- n 1) (* x y))))])