Move utils into its onw file
[scheme.git] / tests.scm
1 (load "codegen.scm")
2 (load "typecheck.scm")
3
4 (define (test-f pred actual expected)
5   (when (not (pred actual expected))
6     (error #f
7            (format "test failed:\nexpected: ~a\nactual:   ~a"
8                    expected actual))))
9
10 (define-syntax test
11   (syntax-rules ()
12     ((_ a e)
13      (begin
14        (display (quote a))
15        (newline)
16        (test-f equal? a e)))))
17
18 (define-syntax test-types
19   (syntax-rules ()
20     ((_ a e)
21      (begin
22        (display (quote a))
23        (newline)
24        (test-f types-equal? a e)))))
25
26 (define (read-file file)
27   (call-with-input-file file
28     (lambda (p)
29       (let loop ((next (read-char p))
30                  (result ""))
31         (if (eof-object? next)
32             result
33             (loop (read-char p) (string-append result (string next))))))))
34
35 (define (test-prog prog exit-code)
36   (display prog)
37   (newline)
38   (compile-to-binary prog "/tmp/test-prog" host-os)
39   (test-f equal? (system "/tmp/test-prog") exit-code))
40
41 (define (test-expr prog exit-code)
42   (test-prog (list prog) exit-code))
43
44 (define (test-prog-stdout prog output)
45   (display prog)
46   (newline)
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)))
51
52 (define-syntax test-exception
53   (syntax-rules ()
54     ((_ f)
55      (begin
56        (display (quote f))
57        (newline)
58        (call/cc (lambda (k)
59                   (with-exception-handler
60                    (lambda (x)
61                      (when (eqv? 'no-exception x)
62                        (error #f "test failed: no exception thrown"))
63                      (k))
64                    (lambda ()
65                      (begin
66                        f
67                        (raise 'no-exception))))))))))
68
69 (test (data-tors '(A . ((foo Int Bool)
70                         (bar Bool))))
71       '((foo (A foo constructor)
72              abs Int (abs Bool A))
73         (foo~0 (A foo 0) abs A Int)
74         (foo~1 (A foo 1) abs A Bool)
75         (bar (A bar constructor) abs Bool A)
76         (bar~0 (A bar 0) abs A Bool)))
77
78 (test (data-tors-type-env
79        '(A . ((foo Int Bool)
80               (bar Bool))))
81       '((foo . (abs Int (abs Bool A)))
82         (foo~0 . (abs A Int))
83         (foo~1 . (abs A Bool))
84         (bar . (abs Bool A))
85         (bar~0 . (abs A Bool))))
86
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))]
93               [z (f 123)])
94           x)))
95
96 (test-exception
97  (expand-pattern-matches '((data A (foo Int Int))
98                            (let ([(foo x) (foo 123 234)])
99                              x))))
100
101 (test-types (typecheck '((lambda (x) (+ ((lambda (y) (x y 3)) 5) 2))))
102             '(abs (abs Int (abs Int Int)) Int))
103
104                                         ; recursive types
105
106 (test-types (substitute '((t1 . (abs t1 t10))) 't1) '(abs t1 t10))
107
108 (test-types (typecheck '((let ([bar (lambda (y) y)]
109                                [foo (lambda (x) (foo (bar #t)))])
110                            foo)))
111             '(abs Bool a))
112
113 (test-types (typecheck '((let ([bar (lambda (y) y)]
114                                [foo (lambda (x) (foo (bar #t)))])
115                            bar)))
116             '(abs a a))
117
118 (test-types (typecheck '((let ([foo 3]
119                                [bar (+ foo baz)]
120                                [baz (- bar 1)])
121                            bar)))
122             'Int)
123
124 (test-types (typecheck '((let ([foo 3]
125                                [bar (baz foo)]
126                                [baz (lambda (x) x)])
127                            baz)))
128             '(abs a a))
129
130 (test-types (typecheck '((let ([foo 3]
131                                [bar (baz foo)]
132                                [baz (lambda (x) x)])
133                            bar)))
134             'Int)
135
136                                         ; mutual recursion
137 (test-types (typecheck '((let ([f (lambda (n) (if (= n 0)
138                                                   0
139                                                   (+ 1 (g (- n 1)))))]
140                                [g (lambda (m) (f m))])
141                            (f 10))))
142             'Int)
143
144 (test-types (typecheck '((let ([pow (lambda (p y)
145                                       (let ([go (lambda (n x)
146                                                   (if (= n 0)
147                                                       x
148                                                       (go (- n 1) (* x y))))])
149                                         (go p 1)))])
150                            (pow 4 2))))
151             'Int)
152
153                                         ; ADTs
154
155
156 (test-types
157  (typecheck
158   '((data A [foo Int])
159     (let ([x (foo 42)]
160           [(foo y) x])
161       x)))
162  'A)
163
164 (test-types
165  (typecheck
166   '((data A [foo Int])
167     (let ([x (foo 42)]
168           [(foo y) x])
169       y)))
170  'Int)
171
172
173                                         ; type annotations
174
175 (test (annotate-types
176        '((let ([x 42]
177                [y (+ 1 x)])
178            (- y x))))
179
180       '((let ()
181           ((let ((x (42 : Int))
182                  (y (((+ : (abs Int (abs Int Int))) (1 : Int) (x : Int)) : Int)))
183              (((- : (abs Int (abs Int Int))) (y : Int) (x : Int)) : Int)) : Int)) : Int))
184
185 (test-expr '(+ 1 2) 3)
186 (test-expr '(bool->int (= 2 0)) 0)
187 (test-expr '((lambda (x) ((lambda (y) (+ x y)) 42)) 100) 142)
188
189 (test-expr '(* 10 5) 50)
190
191 (test-expr '(let ((x (+ 1 32))
192                   (y x))
193               ((lambda (z) (+ 2 z)) (* y x)))
194            67) ; exit code modulos at 256
195 (test-expr '(if ((lambda (x) (= x 2)) 1) 0 (- 32 1)) 31)
196
197 (test-prog-stdout '((if (= 3 2) 1 (let () (print "hello world!") 0))) "hello world!")
198
199 (test-expr '((lambda (x y) (+ x y)) 1 2) 3)
200 (test-expr '((lambda (x) (+ ((lambda (y) (+ y 1)) 3) x)) 2) 6)
201
202                                         ; passing closures about
203 (test-expr '((lambda (z) ((lambda (x) (x 1)) (lambda (y) (+ z y)))) 2) 3)
204
205                                         ; passing builtins about
206 (test-expr '((lambda (x) ((lambda (a b) (a b 3)) + x)) 3) 6)
207 (test-expr '(bool->int ((lambda (x) (x #f)) !)) 1)
208 (test-expr '((lambda (f) (f #t)) bool->int) 1)
209 (test-prog-stdout '((let () ((lambda (f) (f "foo")) print) 0)) "foo")
210 (test-expr '((lambda (f) (f 3 3)) (lambda (x y) (bool->int (= x y)))) 1)
211 (test-expr '(bool->int ((lambda (f) (! (f 2 3))) =)) 1)
212
213                                         ; recursion
214 (test-expr '(let [(inc (lambda (f n x)
215                          (if (= n 0)
216                              x
217                              (f f (- n 1) (+ x 1)))))]
218               (inc inc 3 2))
219            5)
220
221 (test-expr '(let ([go (lambda (n m x)
222                         (if (= n 0)
223                             x
224                             (go (- n 1) m (* x m))))]
225                   [pow (lambda (p y) (go p y 1))])
226               
227               (pow 3 2))
228            8)
229
230 (test-expr '(let ([pow (lambda (p y)
231                          (let ([go (lambda (n x)
232                                      (if (= n 0)
233                                          x
234                                          (go (- n 1) (* x y))))])
235                            (go p 1)))])
236               (pow 4 2))
237            16)
238
239                                         ; mutual recursion
240 ;; (test-prog-stdout '((let ([f (lambda (n)
241 ;;                            (if (= n 0)
242 ;;                                0
243 ;;                                (let ()
244 ;;                                  (print "a")
245 ;;                                  (g (- n 1)))))]
246 ;;                       [g (lambda (m)
247 ;;                            (let ()
248 ;;                              (print "b")
249 ;;                               (f (- m 1))))])
250 ;;                       (f 10))) "ababababab")
251
252                                         ; adts and pattern matching
253
254 (test-prog '((data A [foo Bool Int])
255              (let ([(foo x y) (foo (= 3 3) 42)])
256                y))
257            42)
258
259 (test-exception (expand-pattern-matches
260                  '((data A [foo Int]
261                    [bar Bool])
262                    (let ([(foo x) (foo 0)]) x))))
263
264 (test-prog '((data A [foo Int])
265              (let ([x (foo 42)])
266                (let ([(foo y) x])
267                  (+ 1 y))))
268            43)
269
270 (test-prog '((data A [foo Int])
271              (data B [bar A])
272              (let ([(bar (foo x)) (bar (foo 42))])
273                x))
274            42)