summaryrefslogtreecommitdiffstats
path: root/test/btree_test.c
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2019-10-18 16:35:18 +0200
committerDavid Robillard <d@drobilla.net>2019-10-18 17:07:56 +0200
commitf94df5c346719c4b811f556f9ad4bcb98196a78c (patch)
treee3a1e2dda55b9bba433c83dd3d99adc8d4b013cb /test/btree_test.c
parent640a3b38b918fee3607530d13b0a7897d8583bc6 (diff)
downloadzix-f94df5c346719c4b811f556f9ad4bcb98196a78c.tar.gz
zix-f94df5c346719c4b811f556f9ad4bcb98196a78c.tar.bz2
zix-f94df5c346719c4b811f556f9ad4bcb98196a78c.zip
Improve BTree test coverage
Diffstat (limited to 'test/btree_test.c')
-rw-r--r--test/btree_test.c33
1 files changed, 29 insertions, 4 deletions
diff --git a/test/btree_test.c b/test/btree_test.c
index c45b3e9..ec8ba41 100644
--- a/test/btree_test.c
+++ b/test/btree_test.c
@@ -131,10 +131,11 @@ test_fail(ZixBTree* t, const char* fmt, ...)
}
static int
-stress(int test_num, size_t n_elems)
+stress(const int test_num, const size_t n_elems)
{
+ assert(n_elems > 0);
+
uintptr_t r = 0;
- ZixBTreeIter* ti = NULL;
ZixBTree* t = zix_btree_new(int_cmp, NULL, NULL);
ZixStatus st = ZIX_STATUS_SUCCESS;
@@ -143,14 +144,22 @@ stress(int test_num, size_t n_elems)
}
// Ensure begin iterator is end on empty tree
- ti = zix_btree_begin(t);
+ ZixBTreeIter* ti = zix_btree_begin(t);
+ ZixBTreeIter* end = zix_btree_end(t);
if (!ti) {
return test_fail(t, "Failed to allocate iterator\n");
} else if (!zix_btree_iter_is_end(ti)) {
return test_fail(t, "Begin iterator on empty tree is not end\n");
+ } else if (!zix_btree_iter_equals(ti, end)) {
+ return test_fail(t, "Begin and end of empty tree are not equal\n");
}
+ zix_btree_iter_free(end);
zix_btree_iter_free(ti);
+ if (zix_btree_iter_copy(NULL)) {
+ return test_fail(t, "Copy of null iterator returned non-null\n");
+ }
+
// Insert n_elems elements
for (size_t i = 0; i < n_elems; ++i) {
r = ith_elem(test_num, n_elems, i);
@@ -167,6 +176,22 @@ stress(int test_num, size_t n_elems)
return test_fail(t, "Tree size %zu != %zu\n", zix_btree_size(t), n_elems);
}
+ // Ensure begin no longer equals end
+ ti = zix_btree_begin(t);
+ end = zix_btree_end(t);
+ if (zix_btree_iter_equals(ti, end)) {
+ return test_fail(t, "Begin and end of non-empty tree are equal\n");
+ }
+ zix_btree_iter_free(end);
+
+ // Ensure non-null iterator copying works
+ ZixBTreeIter* begin_copy = zix_btree_iter_copy(ti);
+ if (!zix_btree_iter_equals(ti, begin_copy)) {
+ return test_fail(t, "Iterator copy is not equal to original\n");
+ }
+ zix_btree_iter_free(begin_copy);
+ zix_btree_iter_free(ti);
+
// Search for all elements
for (size_t i = 0; i < n_elems; ++i) {
r = ith_elem(test_num, n_elems, i);
@@ -421,7 +446,7 @@ main(int argc, char** argv)
}
const unsigned n_tests = 3;
- unsigned n_elems = (argc > 1) ? atol(argv[1]) : 100000;
+ unsigned n_elems = (argc > 1) ? atol(argv[1]) : 524288U;
printf("Running %u tests with %u elements", n_tests, n_elems);
for (unsigned i = 0; i < n_tests; ++i) {