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
/
Copy path020lookup.test.cc
87 lines (79 loc) · 2.39 KB
/
020lookup.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
void test_Curr_lexical_scope_has_nil_cdr_on_startup() {
CHECK_EQ(Curr_lexical_scopes.size(), 1);
CHECK_EQ(cdr(Curr_lexical_scope), nil);
}
void test_lookup_returns_dynamic_binding() {
TEMP(var, mkref(new_sym("a")));
TEMP(val, mkref(new_num(34)));
new_dynamic_scope(var, val);
CHECK_EQ(lookup(var), val);
end_dynamic_scope(var);
}
void test_lookup_returns_lexical_binding() {
TEMP(var, mkref(new_sym("a")));
TEMP(val, mkref(new_num(34)));
new_lexical_scope();
add_lexical_binding(var, val);
CHECK_EQ(lookup(var), val);
end_lexical_scope();
}
void test_lexical_binding_always_overrides_dynamic() {
TEMP(var, mkref(new_sym("a")));
TEMP(val, mkref(new_num(34)));
TEMP(dyn_val, mkref(new_num(35)));
new_dynamic_scope(var, dyn_val);
new_lexical_scope();
add_lexical_binding(var, val);
CHECK_EQ(lookup(var), val);
end_lexical_scope();
CHECK_EQ(lookup(var), new_num(35));
end_dynamic_scope(var);
}
void test_nil_lexical_binding_works() {
TEMP(var, mkref(new_sym("a")));
TEMP(dyn_val, mkref(new_num(35)));
new_dynamic_scope(var, dyn_val);
new_lexical_scope();
add_lexical_binding(var, nil);
CHECK_EQ(lookup(var), nil);
end_lexical_scope();
end_dynamic_scope(var);
}
void test_lexical_scopes_nest_correctly() {
TEMP(var, mkref(new_sym("a")));
TEMP(val, mkref(new_num(34)));
TEMP(val2, mkref(new_num(35)));
TEMP(dyn_val, mkref(new_num(36)));
new_dynamic_scope(var, dyn_val);
new_lexical_scope();
CHECK(Curr_lexical_scope != nil);
add_lexical_binding(var, val);
CHECK_EQ(lookup(var), val);
new_lexical_scope();
add_lexical_binding(var, val2);
CHECK_EQ(lookup(var), val2);
end_lexical_scope();
end_lexical_scope();
CHECK_EQ(lookup(var), dyn_val);
end_dynamic_scope(var);
}
void test_lower_lexical_scopes_are_available() {
TEMP(var, mkref(new_sym("a")));
TEMP(val, mkref(new_num(34)));
new_lexical_scope();
add_lexical_binding(var, val);
CHECK_EQ(lookup(var), val);
new_lexical_scope();
CHECK_EQ(lookup(var), val);
end_lexical_scope();
end_lexical_scope();
}
void test_new_dynamic_scope_increments_refcounts() {
TEMP(var, mkref(new_sym("a")));
TEMP(val, mkref(new_num(34)));
int nk=var->nrefs, nv=val->nrefs;
new_dynamic_scope(var, val);
CHECK_EQ(nk+1, var->nrefs);
CHECK_EQ(nv+1, val->nrefs);
end_dynamic_scope(var);
}