-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.c
84 lines (68 loc) · 2.93 KB
/
test.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include "roots.h"
#include <stdio.h>
void test_balanced() {
printf("testing 'balanced' utility function:\n");
printf("\tshould be true: %s\n", balanced("()") ? "true" : "false");
printf("\tshould be false: %s\n", balanced("(") ? "true" : "false");
printf("\tshould be false: %s\n", balanced("())(") ? "true" : "false");
}
void test(char *input, char *expected_str) {
Value actual = eval(read(input), nil());
Value expected = read(expected_str);
if (!empty(eq(actual, expected))) {
printf("ok: %s\n", input);
} else {
annotate(expected, "not ok, expected: ");
printf("input: %s\n", input);
annotate(actual, "actual: ");
}
}
int main() {
printf("Hello world.\n\n");
test_balanced();
test("()", "()");
// don't have a way to read dotted pair literals yet
test("(cons 1 ())", "(1)");
test("(quote (1 2))", "(1 2)");
test("(cons 1 (quote (2 3)))", "(1 2 3)");
// don't have a way to read dotted pair literals yet
test("(cons (quote (1 2)) (cons 3 ()))", "((1 2) 3)");
test("(head (quote (1 2)))", "1");
test("(tail (quote (1 2)))", "(2)");
test("(quote (1 2))", "(1 2)");
test("(eq (quote foo) (quote foo))", "t");
test("(eq (quote foo) (quote bar))", "()");
test("(eq 1 1)", "t");
test("(eq (head (quote (1))) 1)", "t");
test("(eq (quote (1 2)) (quote (1 2)))", "t");
test("(eq (cons 1 2) (cons 1 2))", "t");
test("(head (quote (foo bar)))", "foo");
test("(eq 1 (head (quote (1 2))))", "t");
test("(eq (quote foo) (head (cons (quote foo) (quote bar))))", "t");
test("(atom (quote foo))", "t");
test("(atom (quote (1 2)))", "()");
test("(if t 1 0)", "1");
test("(if (atom ()) (quote (1 2)) 0)", "(1 2)");
test("(if (atom (quote (1 2))) (cons 1 2) 0)", "0");
test("(let (n 1) n)", "1");
test("(let (n 1) (cons n ()))", "(1)");
test("(let (x (quote (1 2))) x)", "(1 2)");
test("(let (x 1) (let (y 2) (cons x (cons y ()))))", "(1 2)");
test("((lambda (x) x) 2)", "2");
test("((lambda (x) (cons x ())) 1)", "(1)");
test("((lambda (x y) (cons x (cons y ()))) 1 2)", "(1 2)");
test("(let (f (lambda (x y) (cons x (cons y ())))) (f 1 2))", "(1 2)");
test("(let (f (lambda (x y z) (cons x (cons y (cons z ()))))) (f 1 2 3))", "(1 2 3)");
test("((lambda (x) ((lambda (y) (cons x (cons y ()))) 2)) 1)", "(1 2)");
test("((lambda (x) ((lambda (x) x) 2)) 1)", "2");
test("(let (f (lambda (x) x)) (f 1))", "1");
test("(((lambda (x) (lambda (y) x)) 1) 2)", "1");
test("(let (f (lambda (x) (lambda (y) (cons x (cons y ()))))) ((f 1) 2))", "(1 2)");
test("(let (f (lambda (x) x)) (f (cons 1 ())))", "(1)");
test("(let (f (lambda (x) (if (eq x 0) (f 1) x))) (f 0))", "1");
test("(let (fib (lambda (n) (if (eq n 1) 1 (if (eq n 2) 1 (+ (fib (- n 1)) (fib (- n 2))))))) (fib 5))", "5");
test("(cond (1 1) ((quote ()) 2))", "1");
test("(cond ((quote ()) 2) (1 1))", "1");
test("(let (count (lambda (l) (if (eq l (quote ())) 0 (count (tail l))))) (count (quote (1))))", "0");
return 0;
}