Fix graph not considering original binds when recursing
[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 '((lambda (x) ((lambda (y) (+ x y)) 42)) 100) 142)
73
74 (test-prog '(* 10 5) 50)
75
76 (test-prog '(let ((x (+ 1 32))
77                   (y x))
78               ((lambda (z) (+ 2 z)) (* y x)))
79            67) ; exit code modulos at 256
80 (test-prog '(if ((lambda (x) (= x 2)) 1) 0 (- 32 1)) 31)
81
82 (test-prog-stdout '(if (= 3 2) 1 (let () (print "hello world!") 0)) "hello world!")
83
84 (test-prog '((lambda (x y) (+ x y)) 1 2) 3)
85 (test-prog '((lambda (x) (+ ((lambda (y) (+ y 1)) 3) x)) 2) 6)
86
87                                         ; passing closures about
88 (test-prog '((lambda (z) ((lambda (x) (x 1)) (lambda (y) (+ z y)))) 2) 3)
89
90                                         ; passing builtins about
91 (test-prog '((lambda (x) ((lambda (a b) (a b 3)) + x)) 3) 6)
92 (test-prog '(bool->int ((lambda (x) (x #f)) !)) 1)
93 (test-prog '((lambda (f) (f #t)) bool->int) 1)
94 (test-prog-stdout '(let () ((lambda (f) (f "foo")) print) 0) "foo")
95 (test-prog '((lambda (f) (f 3 3)) (lambda (x y) (bool->int (= x y)))) 1)
96 (test-prog '(bool->int ((lambda (f) (! (f 2 3))) =)) 1)
97
98                                         ; recursion
99 (test-prog '(let [(inc (lambda (f n x)
100                          (if (= n 0)
101                              x
102                              (f f (- n 1) (+ x 1)))))]
103               (inc inc 3 2))
104            5)