Formulate destructors properly
[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 '(data A
47                         (foo Int Bool)
48                         (bar Bool)))
49       '((foo . (abs Int (abs Bool A)))
50         (foo~0 . (abs A Int))
51         (foo~1 . (abs A Bool))
52         (bar . (abs Bool A))
53         (bar~0 . (abs A Bool))))
54
55 (test-types (typecheck '((lambda (x) (+ ((lambda (y) (x y 3)) 5) 2))))
56             '(abs (abs Int (abs Int Int)) Int))
57
58                                         ; recursive types
59
60 (test-types (substitute '((t1 . (abs t1 t10))) 't1) '(abs t1 t10))
61
62 (test-types (typecheck '((let ([bar (lambda (y) y)]
63                                [foo (lambda (x) (foo (bar #t)))])
64                            foo)))
65             '(abs Bool a))
66
67 (test-types (typecheck '((let ([bar (lambda (y) y)]
68                                [foo (lambda (x) (foo (bar #t)))])
69                            bar)))
70             '(abs a a))
71
72 (test-types (typecheck '((let ([foo 3]
73                                [bar (+ foo baz)]
74                                [baz (- bar 1)])
75                            bar)))
76             'Int)
77
78 (test-types (typecheck '((let ([foo 3]
79                                [bar (baz foo)]
80                                [baz (lambda (x) x)])
81                            baz)))
82             '(abs a a))
83
84 (test-types (typecheck '((let ([foo 3]
85                                [bar (baz foo)]
86                                [baz (lambda (x) x)])
87                            bar)))
88             'Int)
89
90                                         ; mutual recursion
91 (test-types (typecheck '((let ([f (lambda (n) (if (= n 0)
92                                                   0
93                                                   (+ 1 (g (- n 1)))))]
94                                [g (lambda (m) (f m))])
95                            (f 10))))
96             'Int)
97
98 (test-types (typecheck '((let ([pow (lambda (p y)
99                                       (let ([go (lambda (n x)
100                                                   (if (= n 0)
101                                                       x
102                                                       (go (- n 1) (* x y))))])
103                                         (go p 1)))])
104                            (pow 4 2))))
105             'Int)
106
107 (test-types
108  (typecheck
109   '((data A
110           [foo Int]
111           [bar Bool])
112     (let ([x (foo 42)]
113           [(foo y) x])
114       x)))
115  'A)
116
117 (test-types
118  (typecheck
119   '((data A
120           [foo Int]
121           [bar Bool])
122     (let ([x (foo 42)]
123           [(foo y) x])
124       y)))
125   'Int)
126
127 (test-expr '(+ 1 2) 3)
128 (test-expr '(bool->int (= 2 0)) 0)
129 (test-expr '((lambda (x) ((lambda (y) (+ x y)) 42)) 100) 142)
130
131 (test-expr '(* 10 5) 50)
132
133 (test-expr '(let ((x (+ 1 32))
134                   (y x))
135               ((lambda (z) (+ 2 z)) (* y x)))
136            67) ; exit code modulos at 256
137 (test-expr '(if ((lambda (x) (= x 2)) 1) 0 (- 32 1)) 31)
138
139 (test-prog-stdout '((if (= 3 2) 1 (let () (print "hello world!") 0))) "hello world!")
140
141 (test-expr '((lambda (x y) (+ x y)) 1 2) 3)
142 (test-expr '((lambda (x) (+ ((lambda (y) (+ y 1)) 3) x)) 2) 6)
143
144                                         ; passing closures about
145 (test-expr '((lambda (z) ((lambda (x) (x 1)) (lambda (y) (+ z y)))) 2) 3)
146
147                                         ; passing builtins about
148 (test-expr '((lambda (x) ((lambda (a b) (a b 3)) + x)) 3) 6)
149 (test-expr '(bool->int ((lambda (x) (x #f)) !)) 1)
150 (test-expr '((lambda (f) (f #t)) bool->int) 1)
151 (test-prog-stdout '((let () ((lambda (f) (f "foo")) print) 0)) "foo")
152 (test-expr '((lambda (f) (f 3 3)) (lambda (x y) (bool->int (= x y)))) 1)
153 (test-expr '(bool->int ((lambda (f) (! (f 2 3))) =)) 1)
154
155                                         ; recursion
156 (test-expr '(let [(inc (lambda (f n x)
157                          (if (= n 0)
158                              x
159                              (f f (- n 1) (+ x 1)))))]
160               (inc inc 3 2))
161            5)
162
163 (test-expr '(let ([go (lambda (n m x)
164                         (if (= n 0)
165                              x
166                              (go (- n 1) m (* x m))))]
167                   [pow (lambda (p y) (go p y 1))])
168                      
169               (pow 3 2))
170            8)
171
172 (test-expr '(let ([pow (lambda (p y)
173                          (let ([go (lambda (n x)
174                                      (if (= n 0)
175                                          x
176                                          (go (- n 1) (* x y))))])
177                            (go p 1)))])
178               (pow 4 2))
179            16)
180
181 (test-prog-stdout '(let ([f (lambda (n)
182                               (if (= n 0)
183                                   0
184                                   (let ()
185                                     (print "a")
186                                     (g (- n 1)))))]
187                          [g (lambda (m)
188                               (let ()
189                                 (print "b")
190                                  (f (- m 1))))])
191                          (f 10)) "ababababab")