a198dd4ac5a00e4cd71e9a88f8ae32eefbca994c
[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-prog '(+ 1 2) 3)
54 (test-prog '((lambda (x) ((lambda (y) (+ x y)) 42)) 100) 142)
55
56 (test-prog '(* 10 5) 50)
57
58 (test-prog '(let ((x (+ 1 32))
59                   (y x))
60               ((lambda (z) (+ 2 z)) (* y x)))
61            67) ; exit code modulos at 256
62 (test-prog '(if ((lambda (x) (= x 2)) 1) 0 (- 32 1)) 31)
63
64 (test-prog-stdout '(if (= 3 2) 1 (let () (print "hello world!") 0)) "hello world!")
65
66 (test-prog '((lambda (x y) (+ x y)) 1 2) 3)
67 (test-prog '((lambda (x) (+ ((lambda (y) (+ y 1)) 3) x)) 2) 6)
68
69                                         ; passing closures about
70 (test-prog '((lambda (z) ((lambda (x) (x 1)) (lambda (y) (+ z y)))) 2) 3)
71
72                                         ; passing builtins about
73 (test-prog '((lambda (x) ((lambda (a b) (a b 3)) + x)) 3) 6)
74 (test-prog '(bool->int ((lambda (x) (x #f)) !)) 1)
75 (test-prog '((lambda (f) (f #t)) bool->int) 1)
76 (test-prog-stdout '(let () ((lambda (f) (f "foo")) print) 0) "foo")
77 (test-prog '((lambda (f) (f 3 3)) (lambda (x y) (bool->int (= x y)))) 1)
78 (test-prog '(bool->int ((lambda (f) (! (f 2 3))) =)) 1)
79
80                                         ; recursion
81 (test-prog '(let [(inc (lambda (f n x)
82                          (if (= n 0)
83                              x
84                              (f f (- n 1) (+ x 1)))))]
85               (inc inc 3 2))
86            5)