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
/
011types.test.cc
127 lines (111 loc) · 2.86 KB
/
011types.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
void test_to_float_works() {
TEMP(num1, mkref(new_num(3)));
CHECK(equal_floats(to_float(num1), 3.0));
TEMP(num2, mkref(new_num(1.5)));
CHECK(equal_floats(to_float(num2), 1.5));
}
void test_integers_are_interned() {
TEMP(num, mkref(new_num(23)));
CHECK_EQ(num->nrefs, 2); // one extra for intern table
}
void test_floats_are_not_interned() {
TEMP(num, mkref(new_num(1.0)));
CHECK_EQ(num->nrefs, 1);
}
void test_syms_are_interned() {
TEMP(sym, mkref(new_sym("a")));
CHECK_EQ(sym->nrefs, 2); // one extra for intern table
}
void test_strings_are_not_interned() {
TEMP(s, mkref(new_string("a")));
CHECK_EQ(s->nrefs, 1);
}
void test_set_car_increments_nrefs() {
TEMP(pair, mkref(new_cell()));
TEMP(a, mkref(new_cell()));
CHECK_EQ(a->nrefs, 1);
set_car(pair, a);
CHECK_EQ(a->nrefs, 2);
}
void test_set_car_decrements_nrefs_of_old_car() {
TEMP(pair, mkref(new_cell()));
TEMP(a, mkref(new_cell()));
CHECK_EQ(a->nrefs, 1);
set_car(pair, a);
CHECK_EQ(a->nrefs, 2);
TEMP(b, mkref(new_cell()));
set_car(pair, b);
CHECK_EQ(a->nrefs, 1);
}
void test_set_car_is_idempotent() {
TEMP(pair, mkref(new_cell()));
TEMP(a, mkref(new_cell()));
CHECK_EQ(a->nrefs, 1);
set_car(pair, a);
CHECK_EQ(a->nrefs, 2);
set_car(pair, a);
CHECK_EQ(a->nrefs, 2);
}
void test_set_cdr_is_idempotent() {
TEMP(pair, mkref(new_cell()));
TEMP(a, mkref(new_cell()));
CHECK_EQ(a->nrefs, 1);
set_cdr(pair, a);
CHECK_EQ(a->nrefs, 2);
set_cdr(pair, a);
CHECK_EQ(a->nrefs, 2);
}
void test_set_cdr_on_atoms_warns() {
Hide_warnings = true;
TEMP(x, mkref(new_cell()));
TEMP(integer, mkref(new_num(3)));
set_cdr(integer, x);
CHECK_TRACE_WARNS();
TEMP(real, mkref(new_num(3.14)));
CLEAR_TRACE;
set_cdr(real, x);
CHECK_TRACE_WARNS();
TEMP(sym, mkref(new_sym("a")));
CLEAR_TRACE;
set_cdr(sym, x);
CHECK_TRACE_WARNS();
TEMP(str, mkref(new_string("a")));
CLEAR_TRACE;
set_cdr(str, x);
CHECK_TRACE_WARNS();
}
void test_set_increments_nrefs() {
TEMP(t, mkref(new_table()));
TEMP(key, mkref(new_sym("a")));
TEMP(val, mkref(new_num(34)));
int nk=key->nrefs, nv=val->nrefs;
put(t, key, val);
CHECK_EQ(nk+1, key->nrefs);
CHECK_EQ(nv+1, val->nrefs);
}
void test_set_decrements_overridden_values() {
TEMP(t, mkref(new_table()));
TEMP(key, mkref(new_sym("a")));
TEMP(val, mkref(new_num(34)));
TEMP(val2, mkref(new_num(35)));
put(t, key, val);
int nv=val->nrefs;
put(t, key, val2);
CHECK_EQ(nv-1, val->nrefs);
}
void test_set_decrements_key_on_delete() {
TEMP(t, mkref(new_table()));
TEMP(key, mkref(new_sym("a")));
TEMP(val, mkref(new_num(34)));
put(t, key, val);
int nv=val->nrefs;
put(t, key, nil);
CHECK_EQ(nv-1, val->nrefs);
}
void test_set_ignores_nonexistent_key() {
TEMP(t, mkref(new_table()));
TEMP(k, mkref(new_sym("nonexistent key test")));
int nk = k->nrefs;
put(t, k, nil);
CHECK_EQ(nk, k->nrefs);
}