4 (define (test-f pred actual expected)
5 (when (not (pred actual expected))
7 (format "test failed:\nexpected: ~a\nactual: ~a"
10 (define (test . xs) (apply test-f (cons equal? xs)))
11 (define (test-types . xs) (apply test-f (cons types-equal? xs)))
13 (define (read-file file)
14 (call-with-input-file file
16 (let loop ((next (read-char p))
18 (if (eof-object? next)
20 (loop (read-char p) (string-append result (string next))))))))
22 (define (test-prog prog exit-code)
25 (compile-to-binary prog "/tmp/test-prog" host-os)
26 (test (system "/tmp/test-prog") exit-code))
28 (define (test-prog-stdout prog output)
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")))
36 (test-types (typecheck '(lambda (x) (+ ((lambda (y) (x y 3)) 5) 2)))
37 '(abs (abs int (abs int int)) int))
41 (test-types (substitute '((t1 (abs t1 t10))) 't1) '(abs t1 t10))
43 (test-types (typecheck '(let ([bar (lambda (y) y)]
44 [foo (lambda (x) (foo (bar #t)))])
48 (test-types (typecheck '(let ([bar (lambda (y) y)]
49 [foo (lambda (x) (foo (bar #t)))])
53 (test-prog '(+ 1 2) 3)
54 (test-prog '((lambda (x) ((lambda (y) (+ x y)) 42)) 100) 142)
56 (test-prog '(* 10 5) 50)
58 (test-prog '(let ((x (+ 1 32))
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)
64 (test-prog-stdout '(if (= 3 2) 1 (let () (print "hello world!") 0)) "hello world!")
66 (test-prog '((lambda (x y) (+ x y)) 1 2) 3)
67 (test-prog '((lambda (x) (+ ((lambda (y) (+ y 1)) 3) x)) 2) 6)
69 ; passing closures about
70 (test-prog '((lambda (z) ((lambda (x) (x 1)) (lambda (y) (+ z y)))) 2) 3)
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)
81 (test-prog '(let [(inc (lambda (f n x)
84 (f f (- n 1) (+ x 1)))))]