-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasic-test.c
220 lines (185 loc) · 6.46 KB
/
basic-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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#include <stdlib.h>
#include <string.h>
#include <criterion/criterion.h>
#include <criterion/new/assert.h>
#include "schaf.h"
#define expect_stringify(exp, v) do { \
char *s = stringify(v); \
cr_expect_str_eq(s, exp); \
free(s); \
} while (0)
#define value_idfunc list
#define V(x) \
_Generic(x, int: value_of_int, char *: value_of_string, Value: value_idfunc)(x)
#define expect_v_eq(expected, actual) do { \
Value vexp = expected, vact = actual; \
if (value_is_int(vexp)) \
expect_int_eq(vexp, vact); \
else if (value_is_string(vexp)) \
expect_vstr_eq(value_to_string(vexp), vact); \
else if (value_is_symbol(vexp)) \
expect_vsym_eq(value_to_string(vexp), vact); \
} while (0)
#define expect_list_eq(expected, actual) do { \
Value exp = expected, act = actual; \
cr_expect(value_is_pair(act)); \
expect_int_eq(length(exp), length(act)); \
for (; exp != Qnil; exp = cdr(exp), act = cdr(act)) \
expect_v_eq(car(exp), car(act)); \
} while (0)
#define expect_pair_eq(ecar, ecdr, act) do { \
Value a = act; \
cr_expect(value_is_pair(a)); \
expect_v_eq(V(ecar), car(a)); \
expect_v_eq(V(ecdr), cdr(a)); \
} while (0)
#define expect_int_eq(exp, act) cr_expect(eq(int, exp, act))
#define expect_string_eq(exp, act) cr_expect_str_eq(act, exp)
#define expect_error(pattern, v) do { \
expect_int_eq(Qundef, v); \
char *m = strstr(error_message(), pattern); \
cr_expect_not_null(m, "expected \"%s\" includes \"%s\"", \
error_message(), pattern); \
} while (0)
#define expect_no_error(v) \
cr_expect_neq(v, Qundef, "got error with a message: '%s'", error_message())
#define expect_vx_eq(t, n, exp, act) do { \
Value a = act; \
expect_no_error(a); \
expect_int_eq(t, value_type_of(a)); \
expect_##n##_eq(exp, value_to_##n(a)); \
} while (0)
#define expect_x_eq_parsed(x, exp, act) expect_##x##_eq(exp, parse_expr_string(act))
#define expect_vint_eq(exp, act) expect_vx_eq(TYPE_INT, int, exp, act)
#define expect_vstr_eq(exp, act) expect_vx_eq(TYPE_STR, string, exp, act)
#define expect_vsym_eq(exp, act) expect_vx_eq(TYPE_SYMBOL, string, exp, act)
#define expect_vint_eq_parsed(exp, act) expect_x_eq_parsed(vint, exp, act)
#define expect_vstr_eq_parsed(exp, act) expect_x_eq_parsed(vstr, exp, act)
#define expect_vsym_eq_parsed(exp, act) expect_x_eq_parsed(vsym, exp, act)
#define expect_list_eq_parsed(exp, act) expect_x_eq_parsed(list, exp, act)
#define expect_pair_eq_parsed(ecar, ecdr, act) expect_pair_eq(ecar, ecdr, parse_expr_string(act))
#define expect_parse_error(exp, act) expect_error(exp, parse_expr_string(act))
#define expect_runtime_error(exp, act) expect_error(exp, eval_string(act))
static Value parse_expr_string(const char *in)
{
Value v = parse_string(in);
if (v == Qundef || v == Qnil)
return v;
return car(v);
}
// terminate with Qundef
static Value list(Value arg, ...)
{
if (arg == Qundef)
return Qnil;
Value vec[0x100] = { arg, }, o;
va_list ap;
va_start(ap, arg);
long i = 1;
while ((o = va_arg(ap, Value)) != Qundef)
vec[i++] = o;
va_end(ap);
Value l = Qnil;
while (--i >= 0)
l = cons(vec[i], l);
return l;
}
Test(schaf, printing) {
expect_stringify("#t", Qtrue);
expect_stringify("#f", Qfalse);
expect_stringify("<undef>", Qundef);
expect_stringify("()", Qnil);
expect_stringify("0", value_of_int(0));
expect_stringify("42", value_of_int(42));
expect_stringify("-42", value_of_int(-42));
expect_stringify("foo", value_of_symbol("foo"));
expect_stringify("(1)", cons(V(1), Qnil));
expect_stringify("(1 . 2)", cons(V(1), V(2)));
expect_stringify("(1 2)", list(V(1), V(2), Qundef));
}
Test(schaf, parse_int) {
expect_vint_eq_parsed(42, "42");
expect_vint_eq_parsed(-42, "-42");
}
Test(schaf, parse_nil) {
cr_expect(parse_expr_string("()") == Qnil);
}
Test(schaf, parse_list) {
expect_list_eq_parsed(list(V(1), V(2), Qundef), "(1 2)");
}
Test(schaf, parse_string) {
expect_vstr_eq_parsed("abc", "\"abc\"");
expect_vstr_eq_parsed("a\\b", "\"a\\\\b\"");
expect_vstr_eq_parsed("a\"b", "\"a\\\"b\"");
}
Test(schaf, parse_string_list) {
expect_list_eq_parsed(list(V("abc"), V("def"), Qundef),
"(\"abc\" \"def\")");
}
#define caaaar(x) (car(car(car(car(x)))))
Test(schaf, cxr) {
expect_vint_eq(42, caaaar(parse_expr_string("((((42))))")));
}
Test(schaf, parse_ident) {
expect_vsym_eq_parsed("a", "a");
}
Test(schaf, parse_dot) {
expect_pair_eq_parsed(1, 2, "(1 . 2)");
}
Test(schaf, parse_peculiar) {
expect_vint_eq_parsed(42, "+42");
cr_expect(value_is_symbol(parse_expr_string("+")));
}
Test(schaf, parse_lambda) {
expect_list_eq_parsed(list(value_of_symbol("lambda"), Qnil, V(42), Qundef),
"(lambda () 42)");
}
Test(schaf, parse_broken) {
expect_parse_error("got 'EOF'", "(");
expect_parse_error("got 'EOF'", "'");
}
Test(schaf, parse_error_line_column) {
expect_parse_error("<inline>:1:3: ", "(1");
expect_parse_error("<inline>:2:3: ", "\n(1");
expect_parse_error("<inline>:2:5: ", "()\n()(1");
}
Test(schaf, runtime_error_line_column) {
expect_runtime_error(
"unbound variable: h\n"
"\t<inline>:2:14 in 'g'\n"
"\t<inline>:1:14 in 'f'\n"
"\t<inline>:3:2 in <toplevel>"
,
"(define (f) (g))\n"
"(define (g) (h))\n"
"(f)");
}
Test(schaf, div0) {
expect_runtime_error("divided by zero", "(/ 42 0)");
}
Test(schaf, modulo) {
expect_runtime_error("divided by zero", "(modulo 13 0)");
}
Test(schaf, unbound_variable) {
expect_runtime_error("unbound variable: x", "x");
expect_runtime_error("unbound variable: x", "(+ x 2)");
}
Test(schaf, if) {
expect_runtime_error("2..3 but got 1", "(if #f)");
expect_runtime_error("2..3 but got 4", "(if #f 1 2 3)");
}
Test(schaf, set) {
expect_runtime_error("unbound variable: x", "(begin (set! x 42) x)");
}
Test(schaf, let) {
expect_runtime_error("but got 1", "(let ((x 42)))");
expect_runtime_error("but got 1", "(let ((x 42) (y 100)))");
}
Test(schaf, applicable) {
expect_runtime_error("expected procedure", "(1 1)");
expect_runtime_error("expected procedure", "(() 1)");
}
Test(schaf, map) {
expect_runtime_error("expected pair but got integer", "(map + 1)");
expect_runtime_error("expected pair but got integer", "(for-each + 1)");
}