Repository navigation
Fix rbtree foreach traversal #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,14 @@ | ||
| #include "om.h" | ||
| #include "om_test.h" | ||
|
|
||
| static bool _rbt_foreach_once(om_rbt_node_t* node, void* arg) { | ||
| int* count = (int*)arg; | ||
| (*count)++; | ||
| ck_assert_msg(node->key[0] == 'B', "遍历回调节点错误,应为B,实际为%c", | ||
| node->key[0]); | ||
| return true; | ||
| } | ||
|
|
||
| START_TEST(_MALLOC) { | ||
| void* ptr = NULL; | ||
|
|
||
|
|
@@ -127,6 +135,18 @@ START_TEST(_RBT) { | |
| } | ||
| END_TEST | ||
|
|
||
| START_TEST(_RBT_FOREACH) { | ||
| RBT_ROOT(rbt); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion (testing): Consider adding a dedicated test for traversing an empty tree. Since the fix now returns Suggested implementation: START_TEST(_RBT_FOREACH) {
RBT_ROOT(rbt);
om_rbt_node_t node = {.key = "B"};
int count = 0;
om_rbtree_insert(&rbt, &node);
om_rbtree_foreach(&rbt, _rbt_foreach_once, &count);
ck_assert_msg(count == 1, "单节点遍历应执行1次回调,实际执行%d次", count);
}
END_TEST
START_TEST(_RBT_FOREACH_EMPTY) {
RBT_ROOT(rbt);
int count = 0;
om_rbtree_foreach(&rbt, _rbt_foreach_once, &count);
ck_assert_msg(count == 0, "空树遍历不应执行回调,实际执行%d次", count);
}
END_TESTTo actually run the new |
||
| om_rbt_node_t node = {.key = "B"}; | ||
| int count = 0; | ||
|
|
||
| om_rbtree_insert(&rbt, &node); | ||
| om_rbtree_foreach(&rbt, _rbt_foreach_once, &count); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion (testing): Add a test that verifies early termination when the callback returns false. Since the tests only use callbacks that return Suggested implementation: static bool _rbt_foreach_early_stop(om_rbt_node_t* node, void* data) {
int* count = (int*)data;
(*count)++;
/* 在访问到第2个节点后中止遍历 */
if (*count >= 2) {
return false;
}
return true;
}
START_TEST(_RBT_FOREACH) {END_TEST
START_TEST(_RBT_FOREACH_EARLY_STOP) {
RBT_ROOT(rbt);
om_rbt_node_t node_a = {.key = "A"};
om_rbt_node_t node_b = {.key = "B"};
om_rbt_node_t node_c = {.key = "C"};
int count = 0;
om_rbtree_insert(&rbt, &node_b);
om_rbtree_insert(&rbt, &node_a);
om_rbtree_insert(&rbt, &node_c);
om_rbtree_foreach(&rbt, _rbt_foreach_early_stop, &count);
ck_assert_msg(count == 2, "回调返回false后应只访问前2个节点,实际访问%d个", count);
}
END_TEST
Suite* make_om_base_suite(void) { tcase_add_test(tc_rbt, _RBT);
tcase_add_test(tc_rbt, _RBT_FOREACH);
tcase_add_test(tc_rbt, _RBT_FOREACH_EARLY_STOP);
|
||
|
|
||
| ck_assert_msg(count == 1, "单节点遍历应执行1次回调,实际执行%d次", count); | ||
| } | ||
| END_TEST | ||
|
|
||
| Suite* make_om_base_suite(void) { | ||
| Suite* om_base = suite_create("底层API测试"); | ||
|
|
||
|
|
@@ -145,6 +165,7 @@ Suite* make_om_base_suite(void) { | |
| TCase* tc_rbt = tcase_create("红黑树测试"); | ||
| suite_add_tcase(om_base, tc_rbt); | ||
| tcase_add_test(tc_rbt, _RBT); | ||
| tcase_add_test(tc_rbt, _RBT_FOREACH); | ||
|
|
||
| return om_base; | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion (testing): Add tests to cover multi-node traversals and verify full in-order behavior, not just the single-node case.
Given the traversal bug fixed in this PR, this test should exercise more than the single-node case. Please build a small multi-node tree (e.g., three nodes that force visiting both left and right branches) and assert that all nodes are visited and that the visit sequence is strictly in-order by key (for example, inserting keys "A", "B", "C" and collecting the visited keys in the callback to check the exact order).