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