WIP on typechecker refactor
[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-prog-stdout prog output)
36   (display prog)
37   (newline)
38   (compile-to-binary prog "/tmp/test-prog" host-os)
39   (system "/tmp/test-prog > /tmp/test-output.txt")
40   (let ((str (read-file "/tmp/test-output.txt")))
41     (test str output)))
42
43 (test-types (typecheck '(lambda (x) (+ ((lambda (y) (x y 3)) 5) 2)))
44             '(abs (abs int (abs int int)) int))
45
46                                         ; recursive types
47
48 (test-types (substitute '((t1 . (abs t1 t10))) 't1) '(abs t1 t10))
49
50 (test-types (typecheck '(let ([bar (lambda (y) y)]
51                               [foo (lambda (x) (foo (bar #t)))])
52                           foo))
53             '(abs bool a))
54
55 (test-types (typecheck '(let ([bar (lambda (y) y)]
56                               [foo (lambda (x) (foo (bar #t)))])
57                     bar))
58             '(abs a a))
59
60 (test-types (typecheck '(let ([foo 3]
61                               [bar (+ foo baz)]
62                               [baz (- bar 1)])
63                           bar))
64             'int)
65
66 (test-types (typecheck '(let ([foo 3]
67                               [bar (baz foo)]
68                               [baz (lambda (x) x)])
69                           baz))
70             '(abs a a))
71
72 (test-types (typecheck '(let ([foo 3]
73                               [bar (baz foo)]
74                               [baz (lambda (x) x)])
75                           bar))
76             'int)
77
78                                         ; mutual recursion
79 (test-types (typecheck '(let ([f (lambda (n) (if (= n 0)
80                                                  0
81                                                  (+ 1 (g (- n 1)))))]
82                               [g (lambda (m) (f m))])
83                           (f 10)))
84             'int)
85
86 (test-types (typecheck '(let ([pow (lambda (p y)
87                                      (let ([go (lambda (n x)
88                                                  (if (= n 0)
89                                                      x
90                                                      (go (- n 1) (* x y))))])
91                                        (go p 1)))])
92                           (pow 4 2)))
93             'int)
94
95 (test-prog '(+ 1 2) 3)
96 (test-prog '(bool->int (= 2 0)) 0)
97 (test-prog '((lambda (x) ((lambda (y) (+ x y)) 42)) 100) 142)
98
99 (test-prog '(* 10 5) 50)
100
101 (test-prog '(let ((x (+ 1 32))
102                   (y x))
103               ((lambda (z) (+ 2 z)) (* y x)))
104            67) ; exit code modulos at 256
105 (test-prog '(if ((lambda (x) (= x 2)) 1) 0 (- 32 1)) 31)
106
107 (test-prog-stdout '(if (= 3 2) 1 (let () (print "hello world!") 0)) "hello world!")
108
109 (test-prog '((lambda (x y) (+ x y)) 1 2) 3)
110 (test-prog '((lambda (x) (+ ((lambda (y) (+ y 1)) 3) x)) 2) 6)
111
112                                         ; passing closures about
113 (test-prog '((lambda (z) ((lambda (x) (x 1)) (lambda (y) (+ z y)))) 2) 3)
114
115                                         ; passing builtins about
116 (test-prog '((lambda (x) ((lambda (a b) (a b 3)) + x)) 3) 6)
117 (test-prog '(bool->int ((lambda (x) (x #f)) !)) 1)
118 (test-prog '((lambda (f) (f #t)) bool->int) 1)
119 (test-prog-stdout '(let () ((lambda (f) (f "foo")) print) 0) "foo")
120 (test-prog '((lambda (f) (f 3 3)) (lambda (x y) (bool->int (= x y)))) 1)
121 (test-prog '(bool->int ((lambda (f) (! (f 2 3))) =)) 1)
122
123                                         ; recursion
124 (test-prog '(let [(inc (lambda (f n x)
125                          (if (= n 0)
126                              x
127                              (f f (- n 1) (+ x 1)))))]
128               (inc inc 3 2))
129            5)
130
131 (test-prog '(let ([go (lambda (n m x)
132                         (if (= n 0)
133                              x
134                              (go (- n 1) m (* x m))))]
135                   [pow (lambda (p y) (go p y 1))])
136                      
137               (pow 3 2))
138            8)
139
140 (test-prog '(let ([pow (lambda (p y)
141                          (let ([go (lambda (n x)
142                                      (if (= n 0)
143                                          x
144                                          (go (- n 1) (* x y))))])
145                            (go p 1)))])
146               (pow 4 2))
147            16)