[PATCH 1/3] rbtree_test: use rb_add() and rb_add_cached() for the basic tests

From: Yiwei Lin

Date: Mon Sep 28 2026 - 08:52:06 EST


insert() and insert_cached() open-code exactly what the generic
rb_add() and rb_add_cached() helpers do. Use the helpers instead.

Besides removing duplicated code, this makes rbtree_test cover the
helpers themselves, which is what most in-tree rbtree users call
nowadays.

Signed-off-by: Yiwei Lin <s921975628@xxxxxxxxx>
Assisted-by: LLM
---
lib/rbtree_test.c | 37 ++++++++-----------------------------
1 file changed, 8 insertions(+), 29 deletions(-)

diff --git a/lib/rbtree_test.c b/lib/rbtree_test.c
index 768c5e6453f37..c1386d328cfb1 100644
--- a/lib/rbtree_test.c
+++ b/lib/rbtree_test.c
@@ -30,41 +30,20 @@ static struct test_node *nodes = NULL;

static struct rnd_state rnd;

-static void insert(struct test_node *node, struct rb_root_cached *root)
+static inline bool less(struct rb_node *a, const struct rb_node *b)
{
- struct rb_node **new = &root->rb_root.rb_node, *parent = NULL;
- u32 key = node->key;
-
- while (*new) {
- parent = *new;
- if (key < rb_entry(parent, struct test_node, rb)->key)
- new = &parent->rb_left;
- else
- new = &parent->rb_right;
- }
+ return rb_entry(a, struct test_node, rb)->key <
+ rb_entry(b, struct test_node, rb)->key;
+}

- rb_link_node(&node->rb, parent, new);
- rb_insert_color(&node->rb, &root->rb_root);
+static void insert(struct test_node *node, struct rb_root_cached *root)
+{
+ rb_add(&node->rb, &root->rb_root, less);
}

static void insert_cached(struct test_node *node, struct rb_root_cached *root)
{
- struct rb_node **new = &root->rb_root.rb_node, *parent = NULL;
- u32 key = node->key;
- bool leftmost = true;
-
- while (*new) {
- parent = *new;
- if (key < rb_entry(parent, struct test_node, rb)->key)
- new = &parent->rb_left;
- else {
- new = &parent->rb_right;
- leftmost = false;
- }
- }
-
- rb_link_node(&node->rb, parent, new);
- rb_insert_color_cached(&node->rb, root, leftmost);
+ rb_add_cached(&node->rb, root, less);
}

static inline void erase(struct test_node *node, struct rb_root_cached *root)
--
2.34.1