Add 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 (test . xs) (apply test-f (cons equal? xs)))
11 (define (test-types . xs) (apply test-f (cons types-equal? xs)))
12
13 (define (read-file file)
14   (call-with-input-file file
15     (lambda (p)
16       (let loop ((next (read-char p))
17                  (result ""))
18         (if (eof-object? next)
19             result
20             (loop (read-char p) (string-append result (string next))))))))
21
22 (define (test-prog prog exit-code)
23   (display prog)
24   (newline)
25   (compile-to-binary prog "/tmp/test-prog" host-os)
26   (test (system "/tmp/test-prog") exit-code))
27
28 (define (test-prog-stdout prog output)
29   (display prog)
30   (newline)
31   (compile-to-binary prog "/tmp/test-prog" host-os)
32   (system "/tmp/test-prog > /tmp/test-output.txt")
33   (let ((str (read-file "/tmp/test-output.txt")))
34     (test str output)))
35
36 (test-types (typecheck '(lambda (x) (+ ((lambda (y) (x y 3)) 5) 2)))
37             '(abs (abs int (abs int int)) int))
38
39                                         ; recursive types
40
41 (test-types (substitute '((t1 (abs t1 t10))) 't1) '(abs t1 t10))
42
43 (test-types (typecheck '(let ([bar (lambda (y) y)]
44                               [foo (lambda (x) (foo (bar #t)))])
45                           foo))
46             '(abs bool a))
47
48 (test-types (typecheck '(let ([bar (lambda (y) y)]
49                         [foo (lambda (x) (foo (bar #t)))])
50                     bar))
51             '(abs a a))
52
53 (test-types (typecheck '(let ([foo 3]
54                               [bar (+ foo baz)]
55                               [baz (- bar 1)])
56                           bar))
57             'int)
58
59 (test-types (typecheck '(let ([foo 3]
60                               [bar (baz foo)]
61                               [baz (lambda (x) x)])
62                           baz))
63             '(abs a a))
64
65 (test-types (typecheck '(let ([foo 3]
66                               [bar (baz foo)]
67                               [baz (lambda (x) x)])
68                           bar))
69             'int)
70
71 (test-prog '(+ 1 2) 3)
72 (test-prog '(bool->int (= 2 0)) 0)
73 (test-prog '((lambda (x) ((lambda (y) (+ x y)) 42)) 100) 142)
74
75 (test-prog '(* 10 5) 50)
76
77 (test-prog '(let ((x (+ 1 32))
78                   (y x))
79               ((lambda (z) (+ 2 z)) (* y x)))
80            67) ; exit code modulos at 256
81 (test-prog '(if ((lambda (x) (= x 2)) 1) 0 (- 32 1)) 31)
82
83 (test-prog-stdout '(if (= 3 2) 1 (let () (print "hello world!") 0)) "hello world!")
84
85 (test-prog '((lambda (x y) (+ x y)) 1 2) 3)
86 (test-prog '((lambda (x) (+ ((lambda (y) (+ y 1)) 3) x)) 2) 6)
87
88                                         ; passing closures about
89 (test-prog '((lambda (z) ((lambda (x) (x 1)) (lambda (y) (+ z y)))) 2) 3)
90
91                                         ; passing builtins about
92 (test-prog '((lambda (x) ((lambda (a b) (a b 3)) + x)) 3) 6)
93 (test-prog '(bool->int ((lambda (x) (x #f)) !)) 1)
94 (test-prog '((lambda (f) (f #t)) bool->int) 1)
95 (test-prog-stdout '(let () ((lambda (f) (f "foo")) print) 0) "foo")
96 (test-prog '((lambda (f) (f 3 3)) (lambda (x y) (bool->int (= x y)))) 1)
97 (test-prog '(bool->int ((lambda (f) (! (f 2 3))) =)) 1)
98
99                                         ; recursion
100 (test-prog '(let [(inc (lambda (f n x)
101                          (if (= n 0)
102                              x
103                              (f f (- n 1) (+ x 1)))))]
104               (inc inc 3 2))
105            5)
106
107 (test-prog '(let ([go (lambda (n m x)
108                         (if (= n 0)
109                              x
110                              (go (- n 1) m (* x m))))]
111                   [pow (lambda (p y) (go p y 1))])
112                      
113               (pow 3 2))
114            8)
115
116 (test-prog '(let ([pow (lambda (p y)
117                          (let ([go (lambda (n x)
118                                      (if (= n 0)
119                                          x
120                                          (go (- n 1) (* x y))))])
121                            (go p 1)))])
122               (pow 4 2))
123            16)
124
125 (test-prog-stdout '(let ([f (lambda (n)
126                               (if (= n 0)
127                                   0
128                                   (let ()
129                                     (print "a")
130                                     (g (- n 1)))))]
131                          [g (lambda (m)
132                               (let ()
133                                 (print "b")
134                                  (f (- m 1))))])
135                          (f 10)) "ababababab")