Skip to content

Commit 69f0471

Browse files
Apply suggestion from @Copilot
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 613896a commit 69f0471

File tree

1 file changed

+62
-58
lines changed

1 file changed

+62
-58
lines changed

test/node_16.cpp

Lines changed: 62 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -240,81 +240,85 @@ TEST_SUITE("node 16") {
240240
TEST_CASE("grow to node_48 preserves offset (PR #20)") {
241241
// This test reproduces the bug reported in PR #20:
242242
// When node_16 grows to node_48, the indexes_ array must use 128 offset
243-
244-
leaf_node<void*>* dummy_children[17];
245-
246-
// Create dummy children
247-
for (int i = 0; i < 17; ++i) {
248-
dummy_children[i] = new leaf_node<void*>(nullptr);
249-
}
250-
251-
// Create a node_16 and fill it with 16 children
252-
node_16<void*>* n16 = new node_16<void*>();
253-
254-
// Use ASCII printable characters (which have values > 0)
255-
// This reproduces the scenario described in PR #20 with 'a', 'b', etc.
256-
char test_keys[17];
257-
for (int i = 0; i < 17; ++i) {
258-
test_keys[i] = 'a' + i; // a, b, c, ..., q
259-
}
260-
261-
// Fill node_16 to capacity
262-
for (int i = 0; i < 16; ++i) {
263-
n16->set_child(test_keys[i], dummy_children[i]);
264-
}
265-
266-
REQUIRE(n16->is_full());
267-
268-
// Grow to node_48 by adding one more child
269-
auto* n48 = static_cast<node_48<void*>*>(n16->grow());
270-
REQUIRE(n48 != nullptr);
271-
272-
// CRITICAL TEST: After growing, all original children must still be findable
273-
// This fails without the fix because node_16::grow() doesn't add the 128 offset
274-
for (int i = 0; i < 16; ++i) {
275-
auto** child_ptr = n48->find_child(test_keys[i]);
276-
REQUIRE(child_ptr != nullptr);
277-
REQUIRE(*child_ptr == dummy_children[i]);
278-
}
279-
280-
// Test that we can still add the 17th child
281-
n48->set_child(test_keys[16], dummy_children[16]);
282-
283-
// Verify all 17 children are now accessible
284-
for (int i = 0; i < 17; ++i) {
285-
auto** child_ptr = n48->find_child(test_keys[i]);
286-
REQUIRE(child_ptr != nullptr);
287-
REQUIRE(*child_ptr == dummy_children[i]);
288-
}
289-
290-
// Test iteration (next_partial_key and prev_partial_key)
291-
// This also fails without the fix
243+
292244
SUBCASE("next_partial_key after grow") {
245+
leaf_node<void*>* dummy_children[17];
246+
for (int i = 0; i < 17; ++i) {
247+
dummy_children[i] = new leaf_node<void*>(nullptr);
248+
}
249+
node_16<void*>* n16 = new node_16<void*>();
250+
char test_keys[17];
251+
for (int i = 0; i < 17; ++i) {
252+
test_keys[i] = 'a' + i; // a, b, c, ..., q
253+
}
254+
for (int i = 0; i < 16; ++i) {
255+
n16->set_child(test_keys[i], dummy_children[i]);
256+
}
257+
REQUIRE(n16->is_full());
258+
auto* n48 = static_cast<node_48<void*>*>(n16->grow());
259+
REQUIRE(n48 != nullptr);
260+
for (int i = 0; i < 16; ++i) {
261+
auto** child_ptr = n48->find_child(test_keys[i]);
262+
REQUIRE(child_ptr != nullptr);
263+
REQUIRE(*child_ptr == dummy_children[i]);
264+
}
265+
n48->set_child(test_keys[16], dummy_children[16]);
266+
for (int i = 0; i < 17; ++i) {
267+
auto** child_ptr = n48->find_child(test_keys[i]);
268+
REQUIRE(child_ptr != nullptr);
269+
REQUIRE(*child_ptr == dummy_children[i]);
270+
}
293271
// Starting from first key 'a', we should find all keys in order
294272
char current = n48->next_partial_key('a');
295273
REQUIRE_EQ('a', current);
296-
297274
for (int i = 1; i < 17; ++i) {
298275
current = n48->next_partial_key(current + 1);
299276
REQUIRE_EQ(test_keys[i], current);
300277
}
278+
delete n48;
279+
for (int i = 0; i < 17; ++i) {
280+
delete dummy_children[i];
281+
}
301282
}
302-
283+
303284
SUBCASE("prev_partial_key after grow") {
285+
leaf_node<void*>* dummy_children[17];
286+
for (int i = 0; i < 17; ++i) {
287+
dummy_children[i] = new leaf_node<void*>(nullptr);
288+
}
289+
node_16<void*>* n16 = new node_16<void*>();
290+
char test_keys[17];
291+
for (int i = 0; i < 17; ++i) {
292+
test_keys[i] = 'a' + i; // a, b, c, ..., q
293+
}
294+
for (int i = 0; i < 16; ++i) {
295+
n16->set_child(test_keys[i], dummy_children[i]);
296+
}
297+
REQUIRE(n16->is_full());
298+
auto* n48 = static_cast<node_48<void*>*>(n16->grow());
299+
REQUIRE(n48 != nullptr);
300+
for (int i = 0; i < 16; ++i) {
301+
auto** child_ptr = n48->find_child(test_keys[i]);
302+
REQUIRE(child_ptr != nullptr);
303+
REQUIRE(*child_ptr == dummy_children[i]);
304+
}
305+
n48->set_child(test_keys[16], dummy_children[16]);
306+
for (int i = 0; i < 17; ++i) {
307+
auto** child_ptr = n48->find_child(test_keys[i]);
308+
REQUIRE(child_ptr != nullptr);
309+
REQUIRE(*child_ptr == dummy_children[i]);
310+
}
304311
// Starting from last key 'q', we should find all keys in reverse order
305312
char current = n48->prev_partial_key('q');
306313
REQUIRE_EQ('q', current);
307-
308314
for (int i = 15; i >= 0; --i) {
309315
current = n48->prev_partial_key(current - 1);
310316
REQUIRE_EQ(test_keys[i], current);
311317
}
312-
}
313-
314-
// Clean up
315-
delete n48;
316-
for (int i = 0; i < 17; ++i) {
317-
delete dummy_children[i];
318+
delete n48;
319+
for (int i = 0; i < 17; ++i) {
320+
delete dummy_children[i];
321+
}
318322
}
319323
}
320324
}

0 commit comments

Comments
 (0)