87ed2a6461004eaf44ef2fcd3c1e0ea18aa87ef4
[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 (test . xs) (apply test-f (cons equal? xs)))
11
12 (define-syntax test-types
13   (syntax-rules ()
14     ((_ a e)
15      (begin
16        (display (quote a))
17        (newline)
18        (test-f types-equal? a e)))))
19
20 (define (read-file file)
21   (call-with-input-file file
22     (lambda (p)
23       (let loop ((next (read-char p))
24                  (result ""))
25         (if (eof-object? next)
26             result
27             (loop (read-char p) (string-append result (string next))))))))
28
29 (define (test-prog prog exit-code)
30   (display prog)
31   (newline)
32   (compile-to-binary prog "/tmp/test-prog" host-os)
33   (test (system "/tmp/test-prog") exit-code))
34
35 (define (test-expr prog exit-code)
36   (test-prog (list prog) exit-code))
37
38 (define (test-prog-stdout prog output)
39   (display prog)
40   (newline)
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")))
44     (test str output)))
45
46 (test (data-tors '(A . ((foo Int Bool)
47                         (bar Bool))))
48       '((foo . (constructor . (abs Int (abs Bool A))))
49         (foo~0 . (0 . (abs A Int)))
50         (foo~1 . (1 . (abs A Bool)))
51         (bar . (constructor . (abs Bool A)))
52         (bar~0 . (0 . (
53
54                        abs A Bool)))))
55
56 (test (data-tors-env
57        '(A . ((foo Int Bool)
58               (bar Bool))))
59       '((foo . (abs Int (abs Bool A)))
60         (foo~0 . (abs A Int))
61         (foo~1 . (abs A Bool))
62         (bar . (abs Bool A))
63         (bar~0 . (abs A Bool))))
64
65 (test-types (typecheck '((lambda (x) (+ ((lambda (y) (x y 3)) 5) 2))))
66             '(abs (abs Int (abs Int Int)) Int))
67
68                                         ; recursive types
69
70 (test-types (substitute '((t1 . (abs t1 t10))) 't1) '(abs t1 t10))
71
72 (test-types (typecheck '((let ([bar (lambda (y) y)]
73                                [foo (lambda (x) (foo (bar #t)))])
74                            foo)))
75             '(abs Bool a))
76
77 (test-types (typecheck '((let ([bar (lambda (y) y)]
78                                [foo (lambda (x) (foo (bar #t)))])
79                            bar)))
80             '(abs a a))
81
82 (test-types (typecheck '((let ([foo 3]
83                                [bar (+ foo baz)]
84                                [baz (- bar 1)])
85                            bar)))
86             'Int)
87
88 (test-types (typecheck '((let ([foo 3]
89                                [bar (baz foo)]
90                                [baz (lambda (x) x)])
91                            baz)))
92             '(abs a a))
93
94 (test-types (typecheck '((let ([foo 3]
95                                [bar (baz foo)]
96                                [baz (lambda (x) x)])
97                            bar)))
98             'Int)
99
100                                         ; mutual recursion
101 (test-types (typecheck '((let ([f (lambda (n) (if (= n 0)
102                                                   0
103                                                   (+ 1 (g (- n 1)))))]
104                                [g (lambda (m) (f m))])
105                            (f 10))))
106             'Int)
107
108 (test-types (typecheck '((let ([pow (lambda (p y)
109                                       (let ([go (lambda (n x)
110                                                   (if (= n 0)
111                                                       x
112                                                       (go (- n 1) (* x y))))])
113                                         (go p 1)))])
114                            (pow 4 2))))
115             'Int)
116
117                                         ; ADTs
118
119
120 (test-types
121  (typecheck
122   '((data A
123           [foo Int]
124           [bar Bool])
125     (let ([x (foo 42)]
126           [(foo y) x])
127       x)))
128  'A)
129
130 (test-types
131  (typecheck
132   '((data A
133           [foo Int]
134           [bar Bool])
135     (let ([x (foo 42)]
136           [(foo y) x])
137       y)))
138  'Int)
139
140
141                                         ; pattern matching
142 (test (let-bindings '(let ([(foo x) a]) x))
143       '((x (foo~0 a))))
144
145 (test (let-bindings '(let ([x (foo 42)] [(foo y) x]) x))
146       '((x (foo 42))
147         (y (foo~0 x))))
148
149                                         ; type annotations
150
151 (test (annotate-types
152        '((let ([x 42]
153                [y (+ 1 x)])
154            (- y x))))
155
156       '((let ()
157           ((let ((x (42 : Int))
158                  (y (((+ : (abs Int (abs Int Int))) (1 : Int) (x : Int)) : Int)))
159              (((- : (abs Int (abs Int Int))) (y : Int) (x : Int)) : Int)) : Int)) : Int))
160
161 (test-expr '(+ 1 2) 3)
162 (test-expr '(bool->int (= 2 0)) 0)
163 (test-expr '((lambda (x) ((lambda (y) (+ x y)) 42)) 100) 142)
164
165 (test-expr '(* 10 5) 50)
166
167 (test-expr '(let ((x (+ 1 32))
168                   (y x))
169               ((lambda (z) (+ 2 z)) (* y x)))
170            67) ; exit code modulos at 256
171 (test-expr '(if ((lambda (x) (= x 2)) 1) 0 (- 32 1)) 31)
172
173 (test-prog-stdout '((if (= 3 2) 1 (let () (print "hello world!") 0))) "hello world!")
174
175 (test-expr '((lambda (x y) (+ x y)) 1 2) 3)
176 (test-expr '((lambda (x) (+ ((lambda (y) (+ y 1)) 3) x)) 2) 6)
177
178                                         ; passing closures about
179 (test-expr '((lambda (z) ((lambda (x) (x 1)) (lambda (y) (+ z y)))) 2) 3)
180
181                                         ; passing builtins about
182 (test-expr '((lambda (x) ((lambda (a b) (a b 3)) + x)) 3) 6)
183 (test-expr '(bool->int ((lambda (x) (x #f)) !)) 1)
184 (test-expr '((lambda (f) (f #t)) bool->int) 1)
185 (test-prog-stdout '((let () ((lambda (f) (f "foo")) print) 0)) "foo")
186 (test-expr '((lambda (f) (f 3 3)) (lambda (x y) (bool->int (= x y)))) 1)
187 (test-expr '(bool->int ((lambda (f) (! (f 2 3))) =)) 1)
188
189                                         ; recursion
190 (test-expr '(let [(inc (lambda (f n x)
191                          (if (= n 0)
192                              x
193                              (f f (- n 1) (+ x 1)))))]
194               (inc inc 3 2))
195            5)
196
197 (test-expr '(let ([go (lambda (n m x)
198                         (if (= n 0)
199                             x
200                             (go (- n 1) m (* x m))))]
201                   [pow (lambda (p y) (go p y 1))])
202               
203               (pow 3 2))
204            8)
205
206 (test-expr '(let ([pow (lambda (p y)
207                          (let ([go (lambda (n x)
208                                      (if (= n 0)
209                                          x
210                                          (go (- n 1) (* x y))))])
211                            (go p 1)))])
212               (pow 4 2))
213            16)
214
215                                         ; mutual recursion
216 ;; (test-prog-stdout '((let ([f (lambda (n)
217 ;;                            (if (= n 0)
218 ;;                                0
219 ;;                                (let ()
220 ;;                                  (print "a")
221 ;;                                  (g (- n 1)))))]
222 ;;                       [g (lambda (m)
223 ;;                            (let ()
224 ;;                              (print "b")
225 ;;                               (f (- m 1))))])
226 ;;                       (f 10))) "ababababab")
227
228                                         ; adts and pattern matching
229
230 (test-prog '((data (A [foo Int]))
231              (let ([(foo x) (foo 42)])
232                x))
233            42)