From b924fc2f66d46ee10aa3b800a6521d3940919f9f Mon Sep 17 00:00:00 2001 From: rsiddharth Date: Fri, 17 Apr 2020 21:02:35 -0400 Subject: nserver/ -> ./ --- tests/tstree_tests.c | 132 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 132 insertions(+) create mode 100644 tests/tstree_tests.c (limited to 'tests/tstree_tests.c') diff --git a/tests/tstree_tests.c b/tests/tstree_tests.c new file mode 100644 index 0000000..d8b5f9f --- /dev/null +++ b/tests/tstree_tests.c @@ -0,0 +1,132 @@ +/* SPDX-License-Identifier: BSD-3-Clause */ +/* + * Copyright © 2010, Zed A. Shaw. + * Copyright © 2020 rsiddharth + */ + +#include "minunit.h" +#include +#include +#include +#include + +TSTree *node = NULL; +char *valueA = "VALUEA"; +char *valueB = "VALUEB"; +char *value2 = "VALUE2"; +char *value4 = "VALUE4"; +char *reverse = "VALUER"; +int traverse_count = 0; + +struct tagbstring test1 = bsStatic("TEST"); +struct tagbstring test2 = bsStatic("TEST2"); +struct tagbstring test3 = bsStatic("TSET"); +struct tagbstring test4 = bsStatic("T"); + +char *test_insert() +{ + node = TSTree_insert(node, bdata(&test1), blength(&test1), valueA); + mu_assert(node != NULL, "Failed to insert into tst."); + + node = TSTree_insert(node, bdata(&test2), blength(&test2), value2); + mu_assert(node != NULL, + "Failed to insert into tst with second name."); + + node = TSTree_insert(node, bdata(&test3), blength(&test3), reverse); + mu_assert(node != NULL, + "Failed to insert into tst with reverse name"); + + node = TSTree_insert(node, bdata(&test4), blength(&test4), value4); + mu_assert(node != NULL, + "Failed to insert into tst with second name."); + + return NULL; +} + +char *test_search_exact() +{ + // tst returns the last one inserted + void *res = TSTree_search(node, bdata(&test1), blength(&test1)); + mu_assert(res == valueA, + "Got the wrong value back, should get A not B."); + + // tst does not find if not exact + res = TSTree_search(node, "TESTNO", strlen("TESTNO")); + mu_assert(res == NULL, "Should not find anything."); + + return NULL; +} + +char *test_search_prefix() +{ + void *res = TSTree_search_prefix( + node, bdata(&test2), blength(&test2)); + debug("result: %p, expected: %p", res, valueA); + mu_assert(res == valueA, "Got wrong valueA by prefix."); + + + res = TSTree_search_prefix(node, bdata(&test3), blength(&test3)); + debug("result: %p, expected: %p", res, valueA); + mu_assert(res == value4, "Got wrong value4 for prefix of 1."); + + res = TSTree_search_prefix(node, "TE", strlen("TE")); + mu_assert(res == value4, "Should find for short prefix"); + + res = TSTree_search_prefix(node, "TE--", strlen("TE--")); + mu_assert(res == value4, "Should find for partial prefix."); + + return NULL; +} + +char *test_collect() +{ + DArray *arr = TSTree_collect(node, "TE", strlen("TE")); + mu_assert(DArray_count(arr) == 2, "Must collect TEST & TEST2"); + DArray_destroy(arr); + + arr = TSTree_collect(node, "T", strlen("T")); + mu_assert(DArray_count(arr) == 4, "Must collect all keys"); + DArray_destroy(arr); + + return NULL; +} + +void TSTree_traverse_test_cb(void *value, void *data) +{ + assert(value != NULL && "Should not get NULL value."); + assert(data == valueA && "Expecting valueA as the data."); + traverse_count++; +} + +char *test_traverse() +{ + traverse_count = 0; + TSTree_traverse(node, TSTree_traverse_test_cb, valueA); + debug("traverse count is: %d", traverse_count); + mu_assert(traverse_count == 4, "Didn't find 4 keys."); + + return NULL; +} + +char *test_destroy() +{ + TSTree_destroy(node); + + return NULL; +} + +char *all_tests() +{ + mu_suite_start(); + + mu_run_test(test_insert); + mu_run_test(test_search_exact); + mu_run_test(test_search_prefix); + mu_run_test(test_collect); + mu_run_test(test_traverse); + mu_run_test(test_destroy); + + return NULL; +} + +RUN_TESTS(all_tests); -- cgit v1.2.3