Skip to content

Commit e54e0f3

Browse files
committed
Add new unit tests which check the shape of the map and adapt existing ones
The shape of a sharing map is now different than before, as now leafs can be directly attached to internal nodes. This adapts the various unit tests that check that the map has a certain shape.
1 parent d89731d commit e54e0f3

File tree

1 file changed

+54
-17
lines changed

1 file changed

+54
-17
lines changed

unit/util/sharing_map.cpp

Lines changed: 54 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ void sharing_map_internals_test()
7676

7777
sm.insert("i", "1");
7878
count = sm.count_unmarked_nodes(false, marked, false);
79-
REQUIRE(count == 3);
79+
REQUIRE(count == 2);
8080
REQUIRE(marked.empty());
8181

8282
count = sm.count_unmarked_nodes(true, marked, false);
@@ -119,7 +119,8 @@ void sharing_map_internals_test()
119119
{
120120
std::set<const void *> marked;
121121
std::size_t count = 0;
122-
std::size_t chunk = 3;
122+
const std::size_t chunk = sharing_map_unsignedt::chunk;
123+
const std::size_t levels = sharing_map_unsignedt::levels;
123124

124125
sharing_map_unsignedt sm;
125126

@@ -128,58 +129,94 @@ void sharing_map_internals_test()
128129

129130
sm.insert(0, "a");
130131
count = sm.count_unmarked_nodes(false, marked, false);
131-
REQUIRE(count == 3);
132+
REQUIRE(count == 2);
132133

133134
SECTION("first node decisive")
134135
{
135136
sm.insert(1, "b");
136137
count = sm.count_unmarked_nodes(false, marked, false);
137-
REQUIRE(count == 5);
138+
REQUIRE(count == 3);
138139

139140
sm.replace(1, "c");
140141
count = sm.count_unmarked_nodes(false, marked, false);
141-
REQUIRE(count == 5);
142+
REQUIRE(count == 3);
142143

143144
sm.erase(1);
144145
count = sm.count_unmarked_nodes(false, marked, false);
145-
REQUIRE(count == 3);
146+
REQUIRE(count == 2);
146147
}
147148

148149
SECTION("second node decisive")
149150
{
150151
sm.insert(1 << chunk, "b");
151152
count = sm.count_unmarked_nodes(false, marked, false);
152-
REQUIRE(count == 6);
153+
REQUIRE(count == 4);
153154

154155
sm.replace(1 << chunk, "c");
155156
count = sm.count_unmarked_nodes(false, marked, false);
156-
REQUIRE(count == 6);
157+
REQUIRE(count == 4);
157158

158159
sm.erase(1 << chunk);
159160
count = sm.count_unmarked_nodes(false, marked, false);
160-
REQUIRE(count == 4);
161+
REQUIRE(count == 3);
161162
}
162163

163164
SECTION("third node decisive")
164165
{
165166
sm.insert(1 << (2 * chunk), "b");
166167
count = sm.count_unmarked_nodes(false, marked, false);
167-
REQUIRE(count == 7);
168+
REQUIRE(count == 5);
168169

169170
sm.replace(1 << (2 * chunk), "c");
170171
count = sm.count_unmarked_nodes(false, marked, false);
171-
REQUIRE(count == 7);
172+
REQUIRE(count == 5);
172173

173174
sm.erase(1 << (2 * chunk));
174175
count = sm.count_unmarked_nodes(false, marked, false);
175-
REQUIRE(count == 5);
176+
REQUIRE(count == 4);
177+
}
178+
179+
SECTION("last non-container node is decisive")
180+
{
181+
sm.insert(1 << (chunk * (levels - 1)), "b");
182+
count = sm.count_unmarked_nodes(false, marked, false);
183+
REQUIRE(count == levels + 2); // inner nodes + leafs
184+
185+
sm.replace(1 << (chunk * (levels - 1)), "c");
186+
count = sm.count_unmarked_nodes(false, marked, false);
187+
REQUIRE(count == levels + 2); // inner nodes + leafs
188+
189+
sm.erase(1 << (chunk * (levels - 1)));
190+
count = sm.count_unmarked_nodes(false, marked, false);
191+
REQUIRE(count == levels + 1); // inner nodes + leafs
192+
}
193+
194+
SECTION("stored in container node")
195+
{
196+
// existing leaf will be migrated to the bottom
197+
sm.insert(1 << (chunk * levels), "b");
198+
count = sm.count_unmarked_nodes(false, marked, false);
199+
REQUIRE(count == levels + 1 + 2); // inner nodes + container + leafs
200+
201+
sm.replace(1 << (chunk * levels), "c");
202+
count = sm.count_unmarked_nodes(false, marked, false);
203+
REQUIRE(count == levels + 1 + 2); // inner nodes + container + leafs
204+
205+
sm.erase(1 << (chunk * levels));
206+
count = sm.count_unmarked_nodes(false, marked, false);
207+
REQUIRE(count == levels + 1 + 1); // inner nodes + container + leafs
208+
209+
// existing leaf still in container, not migrating necessary
210+
sm.insert(1 << (chunk * levels), "d");
211+
count = sm.count_unmarked_nodes(false, marked, false);
212+
REQUIRE(count == levels + 1 + 2); // inner nodes + container + leafs
176213
}
177214
}
178215

179216
SECTION("delta view (sharing, one of the maps is deeper)")
180217
{
181218
std::set<const void *> marked;
182-
std::size_t chunk = 3;
219+
std::size_t chunk = sharing_map_unsignedt::chunk;
183220

184221
std::vector<sharing_map_unsignedt> v;
185222
v.reserve(2);
@@ -200,10 +237,10 @@ void sharing_map_internals_test()
200237
sharing_map_unsignedt::sharing_map_statst sms;
201238

202239
sms = sharing_map_unsignedt::get_sharing_stats(v.begin(), v.end());
203-
REQUIRE(sms.num_leafs == 3);
204-
REQUIRE(sms.num_unique_leafs == 2);
205-
REQUIRE(sms.num_nodes == 3 + 7);
206-
REQUIRE(sms.num_unique_nodes == 3 + 5);
240+
REQUIRE(sms.num_leafs == 1 + 2);
241+
REQUIRE(sms.num_unique_leafs == 1 + 1);
242+
REQUIRE(sms.num_nodes == 2 + 5);
243+
REQUIRE(sms.num_unique_nodes == 2 + 4);
207244
#endif
208245

209246
sharing_map_unsignedt::delta_viewt delta_view;

0 commit comments

Comments
 (0)