Skip to content

Commit 184f0e2

Browse files
Zhaoningxnashif
authored andcommitted
kernel: rbtree: test rbtree minmax api
Add a testcase to test some api to enhance the coverage of rbtree's source code. Signed-off-by: Ningx Zhao <[email protected]>
1 parent 7f727ac commit 184f0e2

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

tests/unit/rbtree/main.c

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,9 +245,43 @@ void test_rbtree_spam(void)
245245
} while (size < MAX_NODES);
246246
}
247247

248+
/**
249+
* @brief Test removing a node with abnormal color.
250+
*
251+
* @details Initialize a tree and insert it,
252+
* and test APIs rb_get_min(), rb_get_max().
253+
*
254+
* @ingroup lib_rbtree_tests
255+
*
256+
* @see rb_get_min(), rb_get_max()
257+
*/
258+
void test_rb_get_minmax(void)
259+
{
260+
struct rbnode temp = {0};
261+
262+
/* Initialize a tree and insert it */
263+
(void)memset(&tree, 0, sizeof(tree));
264+
tree.lessthan_fn = node_lessthan;
265+
(void)memset(nodes, 0, sizeof(nodes));
266+
267+
zassert_true(rb_get_min(&tree) == NULL, "the tree is invalid");
268+
269+
for (int i = 0; i < 8; i++) {
270+
rb_insert(&tree, &nodes[i]);
271+
}
272+
273+
rb_remove(&tree, &temp);
274+
275+
/* Check if tree's max and min node are expected */
276+
zassert_true(rb_get_min(&tree) == &nodes[0], "the tree is invalid");
277+
zassert_true(rb_get_max(&tree) == &nodes[7], "the tree is invalid");
278+
}
279+
248280
void test_main(void)
249281
{
250282
ztest_test_suite(test_rbtree,
251-
ztest_unit_test(test_rbtree_spam));
283+
ztest_unit_test(test_rbtree_spam),
284+
ztest_unit_test(test_rb_get_minmax)
285+
);
252286
ztest_run_test_suite(test_rbtree);
253287
}

0 commit comments

Comments
 (0)