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-expr prog exit-code)
36 (test-prog (list prog) exit-code))
38 (define (test-prog-stdout prog output)
41 (compile-to-binary prog "/tmp/test-prog" host-os)
42 (system "/tmp/test-prog > /tmp/test-output.txt")
43 (let ((str (read-file "/tmp/test-output.txt")))
46 (test (data-tors '(A . ((foo Int Bool)
48 '((foo (A foo constructor)
50 (foo~0 (A foo 0) abs A Int)
51 (foo~1 (A foo 1) abs A Bool)
52 (bar (A bar constructor) abs Bool A)
53 (bar~0 (A bar 0) abs A Bool)))
55 (test (data-tors-type-env
58 '((foo . (abs Int (abs Bool A)))
60 (foo~1 . (abs A Bool))
62 (bar~0 . (abs A Bool))))
64 (test-types (typecheck '((lambda (x) (+ ((lambda (y) (x y 3)) 5) 2))))
65 '(abs (abs Int (abs Int Int)) Int))
69 (test-types (substitute '((t1 . (abs t1 t10))) 't1) '(abs t1 t10))
71 (test-types (typecheck '((let ([bar (lambda (y) y)]
72 [foo (lambda (x) (foo (bar #t)))])
76 (test-types (typecheck '((let ([bar (lambda (y) y)]
77 [foo (lambda (x) (foo (bar #t)))])
81 (test-types (typecheck '((let ([foo 3]
87 (test-types (typecheck '((let ([foo 3]
93 (test-types (typecheck '((let ([foo 3]
100 (test-types (typecheck '((let ([f (lambda (n) (if (= n 0)
103 [g (lambda (m) (f m))])
107 (test-types (typecheck '((let ([pow (lambda (p y)
108 (let ([go (lambda (n x)
111 (go (- n 1) (* x y))))])
141 (test (let-bindings '(let ([(foo x) a]) x))
144 (test (let-bindings '(let ([x (foo 42)] [(foo y) x]) x))
150 (test (annotate-types
156 ((let ((x (42 : Int))
157 (y (((+ : (abs Int (abs Int Int))) (1 : Int) (x : Int)) : Int)))
158 (((- : (abs Int (abs Int Int))) (y : Int) (x : Int)) : Int)) : Int)) : Int))
160 (test-expr '(+ 1 2) 3)
161 (test-expr '(bool->int (= 2 0)) 0)
162 (test-expr '((lambda (x) ((lambda (y) (+ x y)) 42)) 100) 142)
164 (test-expr '(* 10 5) 50)
166 (test-expr '(let ((x (+ 1 32))
168 ((lambda (z) (+ 2 z)) (* y x)))
169 67) ; exit code modulos at 256
170 (test-expr '(if ((lambda (x) (= x 2)) 1) 0 (- 32 1)) 31)
172 (test-prog-stdout '((if (= 3 2) 1 (let () (print "hello world!") 0))) "hello world!")
174 (test-expr '((lambda (x y) (+ x y)) 1 2) 3)
175 (test-expr '((lambda (x) (+ ((lambda (y) (+ y 1)) 3) x)) 2) 6)
177 ; passing closures about
178 (test-expr '((lambda (z) ((lambda (x) (x 1)) (lambda (y) (+ z y)))) 2) 3)
180 ; passing builtins about
181 (test-expr '((lambda (x) ((lambda (a b) (a b 3)) + x)) 3) 6)
182 (test-expr '(bool->int ((lambda (x) (x #f)) !)) 1)
183 (test-expr '((lambda (f) (f #t)) bool->int) 1)
184 (test-prog-stdout '((let () ((lambda (f) (f "foo")) print) 0)) "foo")
185 (test-expr '((lambda (f) (f 3 3)) (lambda (x y) (bool->int (= x y)))) 1)
186 (test-expr '(bool->int ((lambda (f) (! (f 2 3))) =)) 1)
189 (test-expr '(let [(inc (lambda (f n x)
192 (f f (- n 1) (+ x 1)))))]
196 (test-expr '(let ([go (lambda (n m x)
199 (go (- n 1) m (* x m))))]
200 [pow (lambda (p y) (go p y 1))])
205 (test-expr '(let ([pow (lambda (p y)
206 (let ([go (lambda (n x)
209 (go (- n 1) (* x y))))])
215 ;; (test-prog-stdout '((let ([f (lambda (n)
225 ;; (f 10))) "ababababab")
227 ; adts and pattern matching
229 (test-prog '((data A [foo Bool Int])
230 (let ([(foo x y) (foo (= 3 3) 42)])