From 4ee5b8d6a9ebf49ce30ff7d93ef7401039ffecc6 Mon Sep 17 00:00:00 2001 From: nodist Date: Thu, 8 Oct 2026 10:51:06 -0400 Subject: impl kpl tree test with c strings --- test/tree.c | 43 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) (limited to 'test') diff --git a/test/tree.c b/test/tree.c index caaa23a..aef4061 100644 --- a/test/tree.c +++ b/test/tree.c @@ -1,6 +1,47 @@ #include "kpl_test.h" +typedef struct _c_string_test_node { + KPL_TREE_HEADER(_c_string_test_node); + const char *c_string; +} c_string_test_node; + +static int32_t c_string_test_compare(const void *node_a, const void *node_b) { + const c_string_test_node *a = node_a, *b = node_b; + return strcmp(a->c_string, b->c_string); +} + KPL_TEST(c_string_trees) { - // TODO + const char *c_strings[] = { "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", NULL }; + const int32_t c_string_counter[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 }; + kpl_tree c_string_tree = {}; + kpl_tree_init(&c_string_tree, c_string_test_compare, free); + const char **c_strings_iterator = c_strings; + const int32_t *c_string_counter_iterator = c_string_counter; + while (*c_strings_iterator && *c_string_counter_iterator) { + for (int32_t inserts = 0; inserts < *c_string_counter_iterator; inserts++) { + c_string_test_node *node = calloc(1, sizeof(c_string_test_node)); + node->c_string = *c_strings_iterator; + kpl_tree_insert(&c_string_tree, node); + } + c_strings_iterator++; + c_string_counter_iterator++; + } + c_strings_iterator = c_strings; + c_string_counter_iterator = c_string_counter; + c_string_test_node find = {}, *found; + while (*c_strings_iterator && *c_string_counter_iterator) { + int32_t count = *c_string_counter_iterator; + find.c_string = *c_strings_iterator; + KPL_ASSERT(found = kpl_tree_find(&c_string_tree, &find), "c string node not found"); + while (count) { + KPL_ASSERT(strcmp(found->c_string, *c_strings_iterator) == 0, "c string does not match"); + found = found->tree_duplicates; + count--; + } + KPL_ASSERT(!found, "found pointer should be null on last duplicate"); + c_strings_iterator++; + c_string_counter_iterator++; + } + kpl_tree_free(&c_string_tree); } -- cgit v1.2.3