summaryrefslogtreecommitdiffstats
path: root/test/btree_test.c
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2021-09-10 20:11:33 -0400
committerDavid Robillard <d@drobilla.net>2021-09-10 20:11:33 -0400
commit129bcfb52322c2e27fc0e63605bc04c99ac40f8c (patch)
tree0ca0e82ca1dabe19c06397f785143a8b25ed8447 /test/btree_test.c
parentc9e48e055296a19eb6dfcac48495f690ada73087 (diff)
downloadzix-129bcfb52322c2e27fc0e63605bc04c99ac40f8c.tar.gz
zix-129bcfb52322c2e27fc0e63605bc04c99ac40f8c.tar.bz2
zix-129bcfb52322c2e27fc0e63605bc04c99ac40f8c.zip
Remove destroy field of BTree and add zix_btree_clear()
If this is used, it is only when clearing or freeing a tree. Allowing it to be given as a parameter directly there is clearer and avoids bloating the tree itself with information that isn't needed.
Diffstat (limited to 'test/btree_test.c')
-rw-r--r--test/btree_test.c63
1 files changed, 53 insertions, 10 deletions
diff --git a/test/btree_test.c b/test/btree_test.c
index 6914053..4e84ac4 100644
--- a/test/btree_test.c
+++ b/test/btree_test.c
@@ -55,11 +55,6 @@ ith_elem(const unsigned test_num, const size_t n_elems, const size_t i)
}
}
-static void
-destroy(void* ZIX_UNUSED(ptr))
-{
-}
-
typedef struct {
unsigned test_num;
size_t n_elems;
@@ -104,10 +99,11 @@ ZIX_LOG_FUNC(2, 3)
static int
test_fail(ZixBTree* t, const char* fmt, ...)
{
- zix_btree_free(t);
+ zix_btree_free(t, NULL);
if (expect_failure) {
return EXIT_SUCCESS;
}
+
va_list args;
va_start(args, fmt);
fprintf(stderr, "error: ");
@@ -116,6 +112,50 @@ test_fail(ZixBTree* t, const char* fmt, ...)
return EXIT_FAILURE;
}
+static const size_t n_clear_insertions = 1024u;
+
+static void
+destroy(void* const ptr)
+{
+ assert(ptr);
+ assert((uintptr_t)ptr <= n_clear_insertions);
+}
+
+static void
+no_destroy(void* const ptr)
+{
+ assert(!ptr);
+}
+
+static void
+test_clear(void)
+{
+ ZixBTree* t = zix_btree_new(int_cmp, NULL);
+
+ for (uintptr_t r = 0u; r < n_clear_insertions; ++r) {
+ assert(!zix_btree_insert(t, (void*)(r + 1u)));
+ }
+
+ zix_btree_clear(t, destroy);
+ assert(zix_btree_size(t) == 0);
+
+ zix_btree_free(t, no_destroy);
+}
+
+static void
+test_free(void)
+{
+ ZixBTree* t = zix_btree_new(int_cmp, NULL);
+
+ for (uintptr_t r = 0u; r < n_clear_insertions; ++r) {
+ assert(!zix_btree_insert(t, (void*)(r + 1u)));
+ }
+
+ assert(zix_btree_size(t) == n_clear_insertions);
+
+ zix_btree_free(t, destroy);
+}
+
static int
stress(const unsigned test_num, const size_t n_elems)
{
@@ -124,7 +164,7 @@ stress(const unsigned test_num, const size_t n_elems)
}
uintptr_t r = 0;
- ZixBTree* t = zix_btree_new(int_cmp, NULL, NULL);
+ ZixBTree* t = zix_btree_new(int_cmp, NULL);
ZixStatus st = ZIX_STATUS_SUCCESS;
if (!t) {
@@ -441,12 +481,12 @@ stress(const unsigned test_num, const size_t n_elems)
zix_btree_iter_free(next);
next = NULL;
- zix_btree_free(t);
+ zix_btree_free(t, NULL);
// Test lower_bound with wildcard comparator
TestContext ctx = {test_num, n_elems};
- if (!(t = zix_btree_new(wildcard_cmp, &ctx, destroy))) {
+ if (!(t = zix_btree_new(wildcard_cmp, &ctx))) {
return test_fail(t, "Failed to allocate tree\n");
}
@@ -499,7 +539,7 @@ stress(const unsigned test_num, const size_t n_elems)
}
zix_btree_iter_free(ti);
- zix_btree_free(t);
+ zix_btree_free(t, NULL);
return EXIT_SUCCESS;
}
@@ -512,6 +552,9 @@ main(int argc, char** argv)
return EXIT_FAILURE;
}
+ test_clear();
+ test_free();
+
const unsigned n_tests = 3u;
const size_t n_elems = (argc > 1) ? strtoul(argv[1], NULL, 10) : 524288u;