Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/comp/om_rbt.c
Original file line number Diff line number Diff line change
Expand Up @@ -328,14 +328,18 @@ static bool _om_rbtree_foreach(om_rbt_node_t *node,
bool (*fun)(om_rbt_node_t *node, void *arg),
void *arg) {
if (node == NULL) {
return true;
}

if (!_om_rbtree_foreach(node->left, fun, arg)) {
return false;
}

if (_om_rbtree_foreach(node->left, fun, arg) && fun(node, arg)) {
return _om_rbtree_foreach(node->right, fun, arg);
if (!fun(node, arg)) {
return false;
}

return false;
return _om_rbtree_foreach(node->right, fun, arg);
}

void om_rbtree_foreach(om_rbt_root_t *root,
Expand Down
21 changes: 21 additions & 0 deletions test/om_test_base.c
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;

Expand Down Expand Up @@ -127,6 +135,18 @@ START_TEST(_RBT) {
}
END_TEST

Copy link
Copy Markdown

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).


START_TEST(_RBT_FOREACH) {
RBT_ROOT(rbt);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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 true when node == NULL, please add a test that calls om_rbtree_foreach on an empty RBT_ROOT and verifies that the callback is never invoked (e.g., a counter stays at 0). This will protect the node == NULL base case from regressions.

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_TEST

To actually run the new _RBT_FOREACH_EMPTY test, you will also need to register it in the suite definition (likely in make_om_base_suite or wherever other START_TEST cases are added with tcase_add_test). Add a corresponding tcase_add_test(tc, _RBT_FOREACH_EMPTY); next to the existing _RBT_FOREACH registration.

om_rbt_node_t node = {.key = "B"};
int count = 0;

om_rbtree_insert(&rbt, &node);
om_rbtree_foreach(&rbt, _rbt_foreach_once, &count);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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 true, they don't verify the new short-circuit behavior. Please add a test where the callback returns false after visiting the first (or second) node, and assert both that traversal stops invoking the callback after that node (e.g., via a call counter or collected keys) and that the false result is propagated (or at least that no further nodes are visited).

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);
  1. This patch assumes bool is already available (e.g., via #include <stdbool.h> in this file or a common header). If not, add that include near the top of om_test_base.c.
  2. The exact callback signature for om_rbtree_foreach was inferred from _rbt_foreach_once; if it differs (e.g., const qualifiers or different parameter order), adjust _rbt_foreach_early_stop to match it.
  3. If your traversal guarantees a different visit order or if the tree may contain fewer than 3 nodes in some configurations, you may want to relax the assertion to ck_assert_int_le(count, 2) and separately assert count >= 1.


ck_assert_msg(count == 1, "单节点遍历应执行1次回调,实际执行%d次", count);
}
END_TEST

Suite* make_om_base_suite(void) {
Suite* om_base = suite_create("底层API测试");

Expand All @@ -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;
}
Loading