This repository has been archived by the owner on Jan 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
022primitives.test.cc
174 lines (150 loc) · 4.27 KB
/
022primitives.test.cc
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
cell* eval(string s) {
TEMP(form, read(s));
return eval(form);
}
void test_fn_works() {
Warn_on_unknown_var = true;
trace("test") << "- primitives";
TEMP(result, eval("(fn(x) x)"));
// (object function {sig: (x), body: (x)})
CHECK(is_object(result));
CHECK_EQ(type(result), sym_function);
CHECK_EQ(car(get(rep(result), sym_sig)), new_sym("x"));
CHECK_EQ(cdr(get(rep(result), sym_sig)), nil);
CHECK_EQ(car(get(rep(result), sym_body)), new_sym("x"));
CHECK_EQ(cdr(get(rep(result), sym_body)), nil);
}
void test_eval_on_fn_is_idempotent() {
TEMP(a, eval("(fn(x) x)"));
TEMP(result, eval(a));
// (object function {sig: (x), body: (x)})
CHECK(is_object(result));
CHECK_EQ(type(result), sym_function);
CHECK_EQ(car(get(rep(result), sym_sig)), new_sym("x"));
CHECK_EQ(cdr(get(rep(result), sym_sig)), nil);
CHECK_EQ(car(get(rep(result), sym_body)), new_sym("x"));
CHECK_EQ(cdr(get(rep(result), sym_body)), nil);
}
void test_if_sees_args_in_then_and_else() {
trace("test") << "if";
run("(<- f (fn(x) (if 34 x)))");
CLEAR_TRACE;
run("(f 35)");
CHECK_TRACE_TOP("eval", "=> 35");
end_dynamic_scope("f");
}
void test_not_works() {
run("(not 35)");
CHECK_TRACE_TOP("eval", "compiled fn=> false");
}
void test_not_works2() {
run("(not nil)");
CHECK_TRACE_TOP("eval", "compiled fn=> 1");
}
void test_cons_works() {
run("(cons 1 2)");
CHECK_TRACE_TOP("eval", "compiled fn=> (1 ... 2)");
}
void test_assign_to_non_sym_warns() {
trace("test") << "<-";
Hide_warnings = true;
run("(<- 3 nil)");
CHECK_TRACE_WARNS();
}
void test_assign_lexical_var() {
run("((fn (x) (x <- 34) x))");
CHECK_TRACE_TOP("eval", "=> 34");
}
void test_assign_overrides_dynamic_vars() {
run("(<- x 3)");
run("(x <- 5)"); // infix for variety
CLEAR_TRACE;
run("x");
CHECK_TRACE_TOP("eval", "sym=> 5");
end_dynamic_scope("x");
}
void test_assign_overrides_within_lexical_scope() {
run("(<- x 3)");
run("((fn () (<- x 5)))");
CLEAR_TRACE;
run("x");
CHECK_TRACE_TOP("eval", "sym=> 5");
end_dynamic_scope("x");
}
void test_assign_never_overrides_lexical_vars_in_caller_scope() {
run("((fn (x) (<- y x)) 34)"); // prefix <- for variety
CLEAR_TRACE;
run("y");
CHECK_TRACE_CONTENTS("eval", "sym=> 34");
end_dynamic_scope("y");
}
void test_assign_overrides_lexical_var() {
run("((fn (x) (x <- 35) (x <- 36) x) 34)");
CHECK_TRACE_TOP("eval", "=> 36");
}
void test_unbind_works() {
new_dynamic_scope("x", new_num(3));
run("(unbind x)");
CHECK_TRACE_CONTENTS("unbind", "x");
}
void test_unbind_handles_unbound_vars() {
run("(unbind x)");
CHECK_TRACE_DOESNT_CONTAIN("unbind", "x");
}
void test_bound_works() {
run("(bound? 'a)");
CHECK_TRACE_TOP("eval", "=> false");
CLEAR_TRACE;
new_dynamic_scope("a", new_num(3));
run("(bound? 'a)");
CHECK_TRACE_TOP("eval", "=> a");
end_dynamic_scope("a");
}
void test_bound_checks_only_dynamic_scope_on_nil() {
TEMP(call, read("(bound? 'a nil)"));
TEMP(result1, eval(call));
CHECK_EQ(result1, sym_false);
new_lexical_scope();
add_lexical_binding("a", new_num(3));
TEMP(result2, eval(call));
CHECK_EQ(result2, sym_false);
end_lexical_scope();
}
void test_equal_handles_nil() {
trace("test") << "=";
run("(nil = nil)");
CHECK_TRACE_DOESNT_CONTAIN("eval", 1, "=> nil");
CHECK_TRACE_DOESNT_CONTAIN("eval", 1, "=> false");
}
void test_equal_handles_false() {
trace("test") << "=";
run("(false = false)");
CHECK_TRACE_DOESNT_CONTAIN("eval", 1, "=> nil");
CHECK_TRACE_DOESNT_CONTAIN("eval", 1, "=> false");
}
void test_equal_handles_floats() {
run("((/ 3.0 2) = 1.5)");
CHECK_TRACE_DOESNT_CONTAIN("eval", 1, "=> nil");
CHECK_TRACE_DOESNT_CONTAIN("eval", 1, "=> false");
}
void test_equal_handles_float_vs_nil() {
Hide_warnings = true;
run("(nil = 1.5)");
CHECK_TRACE_TOP("eval", "=> nil");
CHECK_TRACE_DOESNT_WARN();
}
void test_eval_handles_eval() {
trace("test") << "eval";
new_dynamic_scope("a", new_num(34));
new_dynamic_scope("x", new_sym("a"));
run("(eval x)");
CHECK_TRACE_TOP("eval", "=> 34");
end_dynamic_scope("x");
end_dynamic_scope("a");
}
void test_eval_warns_on_missing_binding() {
Warn_on_unknown_var = false;
run("(eval 'x nil)");
CHECK_TRACE_TOP("eval", "=> (object incomplete_eval x)");
Warn_on_unknown_var = true;
}