Add extra spicy test case
[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 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)))
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                                         ; case statements
173 (test-types
174  (typecheck '((data A (foo B))
175               (data B (bar Int))
176               (case (foo (bar 32))
177                 [(foo x) x])))
178  'B)
179
180 (test-types
181  (typecheck '((case 42
182                 [23 (= 1 2)]
183                 [x (= x 1)])))
184  'Bool)
185
186                                         ; type annotations
187
188 (test (annotate-types
189        '((let ([x 42]
190                [y (+ 1 x)])
191            (- y x))))
192
193       '((let ()
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))
197
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)
201
202 (test-expr '(* 10 5) 50)
203
204 (test-expr '(let ((x (+ 1 32))
205                   (y x))
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)
209
210 (test-prog-stdout '((if (= 3 2) 1 (let () (print "hello world!") 0))) "hello world!")
211
212 (test-expr '((lambda (x y) (+ x y)) 1 2) 3)
213 (test-expr '((lambda (x) (+ ((lambda (y) (+ y 1)) 3) x)) 2) 6)
214
215                                         ; passing closures about
216 (test-expr '((lambda (z) ((lambda (x) (x 1)) (lambda (y) (+ z y)))) 2) 3)
217
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)
225
226                                         ; recursion
227 (test-expr '(let [(inc (lambda (f n x)
228                          (if (= n 0)
229                              x
230                              (f f (- n 1) (+ x 1)))))]
231               (inc inc 3 2))
232            5)
233
234 (test-expr '(let ([go (lambda (n m x)
235                         (if (= n 0)
236                             x
237                             (go (- n 1) m (* x m))))]
238                   [pow (lambda (p y) (go p y 1))])
239               
240               (pow 3 2))
241            8)
242
243 (test-expr '(let ([pow (lambda (p y)
244                          (let ([go (lambda (n x)
245                                      (if (= n 0)
246                                          x
247                                          (go (- n 1) (* x y))))])
248                            (go p 1)))])
249               (pow 4 2))
250            16)
251
252                                         ; mutual recursion
253 ;; (test-prog-stdout '((let ([f (lambda (n)
254 ;;                            (if (= n 0)
255 ;;                                0
256 ;;                                (let ()
257 ;;                                  (print "a")
258 ;;                                  (g (- n 1)))))]
259 ;;                       [g (lambda (m)
260 ;;                            (let ()
261 ;;                              (print "b")
262 ;;                               (f (- m 1))))])
263 ;;                       (f 10))) "ababababab")
264
265                                         ; adts and pattern matching
266
267 (test-prog '((data A [foo Bool Int])
268              (let ([(foo x y) (foo (= 3 3) 42)])
269                y))
270            42)
271
272 (test-exception (expand-pattern-matches
273                  '((data A [foo Int]
274                    [bar Bool])
275                    (let ([(foo x) (foo 0)]) x))))
276
277 (test-prog '((data A [foo Int])
278              (let ([x (foo 42)])
279                (let ([(foo y) x])
280                  (+ 2 y))))
281            44)
282
283 (test-prog '((data A [foo Bool Int Int])
284              (let ([x (foo (= 2 1) 123 45)]
285                    [(foo a b c) x])
286                (+ b c)))
287            (+ 123 45))
288
289 (test-prog '((data A [foo Int])
290              (data B [bar A])
291              (let ([(bar (foo x)) (bar (foo 42))])
292                x))
293            42)
294
295 (test-prog '((data Foo [a] [b] [c])
296              (let ([x b])
297                (case x
298                  [a 3]
299                  [b 2]
300                  [c 1])))
301            2)
302
303 (test-prog '((data Foo [foo Int Int] [bar Bool])
304              (case (foo 42 12)
305                [(foo 20 x) 0]
306                [(foo 42 x) x]
307                [(bar x) 0]))
308            12)
309
310 (test-prog '((data Foo [foo Int])
311              (data Bar [bar Foo])
312              (case (bar (foo 42))
313                [(bar (foo x)) x]))
314            42)
315                                         ; todo: make this error for incomplete pattern match 
316 (test-prog '((data A [foo Int] [bar Int B])
317              (data B [baz Int])
318              (let ([val (bar 42 (baz 12))])
319                (case val
320                  [(foo 42) 0]
321                  [(bar 32 (baz 12)) 1]
322                  [(bar 42 (baz x)) x]
323                  [(foo x) 2])))
324            12)
325