4 (define (test-f pred actual expected)
5 (when (not (pred actual expected))
7 (format "test failed:\nexpected: ~a\nactual: ~a"
16 (test-f equal? a e)))))
18 (define-syntax test-types
24 (test-f types-equal? a e)))))
26 (define (read-file file)
27 (call-with-input-file file
29 (let loop ((next (read-char p))
31 (if (eof-object? next)
33 (loop (read-char p) (string-append result (string next))))))))
35 (define (test-prog prog exit-code)
38 (compile-to-binary prog "/tmp/test-prog" host-os)
39 (test-f equal? (system "/tmp/test-prog") exit-code))
41 (define (test-expr prog exit-code)
42 (test-prog (list prog) exit-code))
44 (define (test-prog-stdout prog output)
47 (compile-to-binary prog "/tmp/test-prog" host-os)
48 (system "/tmp/test-prog > /tmp/test-output.txt")
49 (let ((str (read-file "/tmp/test-output.txt")))
50 (test-f equal? str output)))
52 (define-syntax test-exception
59 (with-exception-handler
61 (when (eqv? 'no-exception x)
62 (error #f "test failed: no exception thrown"))
67 (raise 'no-exception))))))))))
69 (test (data-tors '(A . ((foo Int Bool)
71 '((foo (A foo constructor)
73 (foo~0 (A foo 0 Int) abs A Int)
74 (foo~1 (A foo 1 Bool) abs A Bool)
75 (bar (A bar constructor) abs Bool A)
76 (bar~0 (A bar 0 Bool) abs A Bool)))
78 (test (data-tors-type-env
81 '((foo . (abs Int (abs Bool A)))
83 (foo~1 . (abs A Bool))
85 (bar~0 . (abs A Bool))))
87 (test (expand-pattern-matches
88 '((data A (foo Int Int))
89 (let ([(foo x y) (foo 123 234)] [z (f 123)]) x)))
90 '((data A (foo Int Int))
91 (let ([x (foo~0 (foo 123 234))]
92 [y (foo~1 (foo 123 234))]
97 (expand-pattern-matches '((data A (foo Int Int))
98 (let ([(foo x) (foo 123 234)])
101 (test-types (typecheck '((lambda (x) (+ ((lambda (y) (x y 3)) 5) 2))))
102 '(abs (abs Int (abs Int Int)) Int))
106 (test-types (substitute '((t1 . (abs t1 t10))) 't1) '(abs t1 t10))
108 (test-types (typecheck '((let ([bar (lambda (y) y)]
109 [foo (lambda (x) (foo (bar #t)))])
113 (test-types (typecheck '((let ([bar (lambda (y) y)]
114 [foo (lambda (x) (foo (bar #t)))])
118 (test-types (typecheck '((let ([foo 3]
124 (test-types (typecheck '((let ([foo 3]
126 [baz (lambda (x) x)])
130 (test-types (typecheck '((let ([foo 3]
132 [baz (lambda (x) x)])
137 (test-types (typecheck '((let ([f (lambda (n) (if (= n 0)
140 [g (lambda (m) (f m))])
144 (test-types (typecheck '((let ([pow (lambda (p y)
145 (let ([go (lambda (n x)
148 (go (- n 1) (* x y))))])
174 (typecheck '((data A (foo B))
181 (typecheck '((case 42
188 (test (annotate-types
194 ((let ((x (42 : Int))
195 (y (((+ : (abs Int (abs Int Int))) (1 : Int) (x : Int)) : Int)))
196 (((- : (abs Int (abs Int Int))) (y : Int) (x : Int)) : Int)) : Int)) : Int))
198 (test-expr '(+ 1 2) 3)
199 (test-expr '(bool->int (= 2 0)) 0)
200 (test-expr '((lambda (x) ((lambda (y) (+ x y)) 42)) 100) 142)
202 (test-expr '(* 10 5) 50)
204 (test-expr '(let ((x (+ 1 32))
206 ((lambda (z) (+ 2 z)) (* y x)))
207 67) ; exit code modulos at 256
208 (test-expr '(if ((lambda (x) (= x 2)) 1) 0 (- 32 1)) 31)
210 (test-prog-stdout '((if (= 3 2) 1 (let () (print "hello world!") 0))) "hello world!")
212 (test-expr '((lambda (x y) (+ x y)) 1 2) 3)
213 (test-expr '((lambda (x) (+ ((lambda (y) (+ y 1)) 3) x)) 2) 6)
215 ; passing closures about
216 (test-expr '((lambda (z) ((lambda (x) (x 1)) (lambda (y) (+ z y)))) 2) 3)
218 ; passing builtins about
219 (test-expr '((lambda (x) ((lambda (a b) (a b 3)) + x)) 3) 6)
220 (test-expr '(bool->int ((lambda (x) (x #f)) !)) 1)
221 (test-expr '((lambda (f) (f #t)) bool->int) 1)
222 (test-prog-stdout '((let () ((lambda (f) (f "foo")) print) 0)) "foo")
223 (test-expr '((lambda (f) (f 3 3)) (lambda (x y) (bool->int (= x y)))) 1)
224 (test-expr '(bool->int ((lambda (f) (! (f 2 3))) =)) 1)
227 (test-expr '(let [(inc (lambda (f n x)
230 (f f (- n 1) (+ x 1)))))]
234 (test-expr '(let ([go (lambda (n m x)
237 (go (- n 1) m (* x m))))]
238 [pow (lambda (p y) (go p y 1))])
243 (test-expr '(let ([pow (lambda (p y)
244 (let ([go (lambda (n x)
247 (go (- n 1) (* x y))))])
253 ;; (test-prog-stdout '((let ([f (lambda (n)
263 ;; (f 10))) "ababababab")
265 ; adts and pattern matching
267 (test-prog '((data A [foo Bool Int])
268 (let ([(foo x y) (foo (= 3 3) 42)])
272 (test-exception (expand-pattern-matches
275 (let ([(foo x) (foo 0)]) x))))
277 (test-prog '((data A [foo Int])
283 (test-prog '((data A [foo Bool Int Int])
284 (let ([x (foo (= 2 1) 123 45)]
289 (test-prog '((data A [foo Int])
291 (let ([(bar (foo x)) (bar (foo 42))])
295 (test-prog '((data Foo [a] [b] [c])
303 (test-prog '((data Foo [foo Int Int] [bar Bool])
310 (test-prog '((data Foo [foo Int])