diff options
-rw-r--r-- | .clang-tidy | 1 | ||||
-rw-r--r-- | benchmark/tree_bench.c | 16 | ||||
-rw-r--r-- | test/.clang-tidy | 1 | ||||
-rw-r--r-- | test/bitset_test.c | 4 | ||||
-rw-r--r-- | test/btree_test.c | 80 | ||||
-rw-r--r-- | test/hash_test.c | 4 | ||||
-rw-r--r-- | test/test_malloc.c | 4 | ||||
-rw-r--r-- | zix/btree.c | 57 | ||||
-rw-r--r-- | zix/ring.c | 12 | ||||
-rw-r--r-- | zix/sorted_array.c | 4 | ||||
-rw-r--r-- | zix/strindex.c | 22 | ||||
-rw-r--r-- | zix/tree.c | 4 |
12 files changed, 138 insertions, 71 deletions
diff --git a/.clang-tidy b/.clang-tidy index 1be48bd..38a0df9 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -1,6 +1,5 @@ Checks: > *, - -*-else-after-return, -*-magic-numbers, -*-uppercase-literal-suffix, -android-cloexec-fopen, diff --git a/benchmark/tree_bench.c b/benchmark/tree_bench.c index f87af78..b458bb1 100644 --- a/benchmark/tree_bench.c +++ b/benchmark/tree_bench.c @@ -42,10 +42,10 @@ unique_rand(size_t i) static const size_t prime = 4294967291; if (i >= prime) { return i; // Values >= prime are mapped to themselves - } else { - const size_t residue = ((uint64_t)i * i) % prime; - return (i <= prime / 2) ? residue : prime - residue; } + + const size_t residue = ((uint64_t)i * i) % prime; + return (i <= prime / 2) ? residue : prime - residue; } static int @@ -53,14 +53,18 @@ int_cmp(const void* a, const void* b, const void* ZIX_UNUSED(user_data)) { const intptr_t ia = (intptr_t)a; const intptr_t ib = (intptr_t)b; + // note the (ia - ib) trick here would overflow + if (ia == ib) { return 0; - } else if (ia < ib) { + } + + if (ia < ib) { return -1; - } else { - return 1; } + + return 1; } static int diff --git a/test/.clang-tidy b/test/.clang-tidy index aa81243..dc7dbc4 100644 --- a/test/.clang-tidy +++ b/test/.clang-tidy @@ -1,6 +1,5 @@ Checks: > *, - -*-else-after-return, -*-magic-numbers, -*-uppercase-literal-suffix, -android-cloexec-fopen, diff --git a/test/bitset_test.c b/test/bitset_test.c index 554e238..8fa610f 100644 --- a/test/bitset_test.c +++ b/test/bitset_test.c @@ -53,7 +53,9 @@ main(void) const size_t count = zix_bitset_count_up_to(b, t, N_BITS); if (count != i + 1) { return test_fail("Count %zu != %zu\n", count, i + 1); - } else if (!zix_bitset_get(b, i)) { + } + + if (!zix_bitset_get(b, i)) { return test_fail("Bit %zu is not set\n", i); } } diff --git a/test/btree_test.c b/test/btree_test.c index ad6b6b7..3336d91 100644 --- a/test/btree_test.c +++ b/test/btree_test.c @@ -89,21 +89,24 @@ wildcard_cmp(const void* a, const void* b, const void* user_data) const unsigned test_num = ctx->test_num; const uintptr_t ia = (uintptr_t)a; const uintptr_t ib = (uintptr_t)b; + if (ia == 0) { if (ib >= wildcard_cut(test_num, n_elems)) { return 0; // Wildcard match - } else { - return 1; // Wildcard a > b } - } else if (ib == 0) { + + return 1; // Wildcard a > b + } + + if (ib == 0) { if (ia >= wildcard_cut(test_num, n_elems)) { return 0; // Wildcard match - } else { - return -1; // Wildcard b > a } - } else { - return int_cmp(a, b, user_data); + + return -1; // Wildcard b > a } + + return int_cmp(a, b, user_data); } ZIX_LOG_FUNC(2, 3) @@ -142,11 +145,16 @@ stress(const unsigned test_num, const size_t n_elems) 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)) { + } + + 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)) { + } + + 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); @@ -159,7 +167,9 @@ stress(const unsigned test_num, const size_t n_elems) r = ith_elem(test_num, n_elems, i); if (!zix_btree_find(t, (void*)r, &ti)) { return test_fail(t, "%" PRIuPTR " already in tree\n", (uintptr_t)r); - } else if ((st = zix_btree_insert(t, (void*)r))) { + } + + if ((st = zix_btree_insert(t, (void*)r))) { return test_fail(t, "Insert %" PRIuPTR " failed (%s)\n", (uintptr_t)r, zix_strerror(st)); } @@ -191,10 +201,13 @@ stress(const unsigned test_num, const size_t n_elems) r = ith_elem(test_num, n_elems, i); if (zix_btree_find(t, (void*)r, &ti)) { return test_fail(t, "Find %" PRIuPTR " @ %zu failed\n", (uintptr_t)r, i); - } else if ((uintptr_t)zix_btree_get(ti) != r) { + } + + if ((uintptr_t)zix_btree_get(ti) != r) { return test_fail(t, "Search data corrupt (%" PRIuPTR " != %" PRIuPTR ")\n", (uintptr_t)zix_btree_get(ti), r); } + zix_btree_iter_free(ti); } @@ -208,13 +221,18 @@ stress(const unsigned test_num, const size_t n_elems) if (zix_btree_lower_bound(t, (void*)r, &ti)) { return test_fail(t, "Lower bound %" PRIuPTR " @ %zu failed\n", (uintptr_t)r, i); - } else if (zix_btree_iter_is_end(ti)) { + } + + if (zix_btree_iter_is_end(ti)) { return test_fail(t, "Lower bound %" PRIuPTR " @ %zu hit end\n", (uintptr_t)r, i); - } else if ((uintptr_t)zix_btree_get(ti) != r) { + } + + if ((uintptr_t)zix_btree_get(ti) != r) { return test_fail(t, "Lower bound corrupt (%" PRIuPTR " != %" PRIuPTR "\n", (uintptr_t)zix_btree_get(ti), r); } + zix_btree_iter_free(ti); } @@ -276,11 +294,15 @@ stress(const unsigned test_num, const size_t n_elems) if (zix_btree_remove(t, (void*)r, (void**)&removed, &next)) { return test_fail(t, "Error removing item %" PRIuPTR "\n", (uintptr_t)r); - } else if (removed != r) { + } + + if (removed != r) { return test_fail(t, "Removed wrong item %" PRIuPTR " != %" PRIuPTR "\n", removed, (uintptr_t)r); - } else if (test_num == 0) { + } + + if (test_num == 0) { const uintptr_t next_value = ith_elem(test_num, n_elems, e + 1); if (!((zix_btree_iter_is_end(next) && e == n_elems - 1) || (uintptr_t)zix_btree_get(next) == next_value)) { @@ -329,10 +351,14 @@ stress(const unsigned test_num, const size_t n_elems) uintptr_t removed; if (zix_btree_remove(t, (void*)r, (void**)&removed, &next)) { return test_fail(t, "Deletion of %" PRIuPTR " failed\n", (uintptr_t)r); - } else if (removed != r) { + } + + if (removed != r) { return test_fail(t, "Removed wrong item %" PRIuPTR " != %" PRIuPTR "\n", removed, (uintptr_t)r); - } else if (test_num == 0) { + } + + if (test_num == 0) { const uintptr_t next_value = ith_elem(test_num, n_elems, e + 1); if (!zix_btree_iter_is_end(next) && (uintptr_t)zix_btree_get(next) == next_value) { @@ -370,10 +396,14 @@ stress(const unsigned test_num, const size_t n_elems) if (zix_btree_remove(t, zix_btree_get(next), (void**)&removed, &next)) { return test_fail(t, "Error removing next item %" PRIuPTR "\n", (uintptr_t)r); - } else if (removed != value) { + } + + if (removed != value) { return test_fail(t, "Removed wrong next item %" PRIuPTR " != %" PRIuPTR "\n", removed, (uintptr_t)value); - } else if (removed < last_value) { + } + + if (removed < last_value) { return test_fail(t, "Removed unordered next item %" PRIuPTR " < %" PRIuPTR "\n", removed, (uintptr_t)value); } @@ -408,7 +438,9 @@ stress(const unsigned test_num, const size_t n_elems) const uintptr_t wildcard = 0; if (zix_btree_lower_bound(t, (void*)wildcard, &ti)) { return test_fail(t, "Lower bound failed\n"); - } else if (zix_btree_iter_is_end(ti)) { + } + + if (zix_btree_iter_is_end(ti)) { return test_fail(t, "Lower bound reached end\n"); } @@ -418,7 +450,9 @@ stress(const unsigned test_num, const size_t n_elems) if (iter_data != cut) { return test_fail(t, "Lower bound %" PRIuPTR " != %" PRIuPTR "\n", iter_data, cut); - } else if (wildcard_cmp((void*)wildcard, (void*)iter_data, &ctx)) { + } + + if (wildcard_cmp((void*)wildcard, (void*)iter_data, &ctx)) { return test_fail(t, "Wildcard lower bound %" PRIuPTR " != %" PRIuPTR "\n", iter_data, cut); } @@ -429,7 +463,9 @@ stress(const unsigned test_num, const size_t n_elems) const uintptr_t max = (uintptr_t)-1; if (zix_btree_lower_bound(t, (void*)max, &ti)) { return test_fail(t, "Lower bound failed\n"); - } else if (!zix_btree_iter_is_end(ti)) { + } + + if (!zix_btree_iter_is_end(ti)) { return test_fail(t, "Lower bound of maximum value is not end\n"); } diff --git a/test/hash_test.c b/test/hash_test.c index 91c0127..bfddc49 100644 --- a/test/hash_test.c +++ b/test/hash_test.c @@ -102,7 +102,9 @@ stress(void) ZixStatus st = zix_hash_insert(hash, &strings[i], &inserted); if (st) { return test_fail("Failed to insert `%s'\n", strings[i]); - } else if (*(const void*const*)inserted != strings[i]) { + } + + if (*(const void*const*)inserted != strings[i]) { return test_fail("Corrupt insertion %s != %s\n", strings[i], *(const char*const*)inserted); } diff --git a/test/test_malloc.c b/test/test_malloc.c index 7be62ff..1138502 100644 --- a/test/test_malloc.c +++ b/test/test_malloc.c @@ -36,7 +36,9 @@ test_malloc(size_t size) { if (in_test_malloc_init) { return NULL; // dlsym is asking for memory, but handles this fine - } else if (!test_malloc_sys_malloc) { + } + + if (!test_malloc_sys_malloc) { test_malloc_init(); } diff --git a/zix/btree.c b/zix/btree.c index ba2ad0d..d8bca23 100644 --- a/zix/btree.c +++ b/zix/btree.c @@ -373,7 +373,9 @@ zix_btree_insert(ZixBTree* const t, void* const e) const int cmp = t->cmp(parent->data.inode.vals[i], e, t->cmp_data); if (cmp == 0) { return ZIX_STATUS_EXISTS; - } else if (cmp < 0) { + } + + if (cmp < 0) { // Move to new RHS n = rhs; ++i; @@ -386,7 +388,9 @@ zix_btree_insert(ZixBTree* const t, void* const e) i = zix_btree_node_find(t, n, e, &equal); if (equal) { return ZIX_STATUS_EXISTS; - } else if (!n->is_leaf) { + } + + if (!n->is_leaf) { // Descend to child node left of value parent = n; n = zix_btree_child(n, i); @@ -640,15 +644,18 @@ zix_btree_remove(ZixBTree* const t, } --t->size; return ZIX_STATUS_SUCCESS; - } else { - // Not found in leaf node, or tree - if (ti && !user_iter) { - zix_btree_iter_free(ti); - *next = NULL; - } - return ZIX_STATUS_NOT_FOUND; } - } else if (equal) { + + // Not found in leaf node, or tree + if (ti && !user_iter) { + zix_btree_iter_free(ti); + *next = NULL; + } + + return ZIX_STATUS_NOT_FOUND; + } + + if (equal) { // Found in internal node ZixBTreeNode* const lhs = zix_btree_child(n, i); ZixBTreeNode* const rhs = zix_btree_child(n, i + 1); @@ -738,12 +745,14 @@ zix_btree_find(const ZixBTree* const t, if (equal) { return ZIX_STATUS_SUCCESS; - } else if (n->is_leaf) { + } + + if (n->is_leaf) { break; - } else { - ++(*ti)->level; - n = zix_btree_child(n, i); } + + ++(*ti)->level; + n = zix_btree_child(n, i); } zix_btree_iter_free(*ti); @@ -759,7 +768,9 @@ zix_btree_lower_bound(const ZixBTree* const t, if (!t) { *ti = NULL; return ZIX_STATUS_BAD_ARG; - } else if (!t->root) { + } + + if (!t->root) { *ti = NULL; return ZIX_STATUS_SUCCESS; } @@ -784,11 +795,11 @@ zix_btree_lower_bound(const ZixBTree* const t, if (n->is_leaf) { break; - } else { - ++(*ti)->level; - n = zix_btree_child(n, i); - assert(n); } + + ++(*ti)->level; + n = zix_btree_child(n, i); + assert(n); } const ZixBTreeIterFrame* const frame = &(*ti)->stack[(*ti)->level]; @@ -822,7 +833,9 @@ zix_btree_begin(const ZixBTree* const t) ZixBTreeIter* const i = zix_btree_iter_new(t); if (!i) { return NULL; - } else if (t->size == 0) { + } + + if (t->size == 0) { i->level = 0; i->stack[0].node = NULL; } else { @@ -871,7 +884,9 @@ zix_btree_iter_equals(const ZixBTreeIter* const lhs, const ZixBTreeIter* const r { if (zix_btree_iter_is_end(lhs) && zix_btree_iter_is_end(rhs)) { return true; - } else if (zix_btree_iter_is_end(lhs) || zix_btree_iter_is_end(rhs) || + } + + if (zix_btree_iter_is_end(lhs) || zix_btree_iter_is_end(rhs) || lhs->level != rhs->level) { return false; } @@ -103,9 +103,9 @@ read_space_internal(const ZixRing* ring, uint32_t r, uint32_t w) { if (r < w) { return w - r; - } else { - return (w - r + ring->size) & ring->size_mask; } + + return (w - r + ring->size) & ring->size_mask; } uint32_t @@ -119,11 +119,13 @@ write_space_internal(const ZixRing* ring, uint32_t r, uint32_t w) { if (r == w) { return ring->size - 1; - } else if (r < w) { + } + + if (r < w) { return ((r - w + ring->size) & ring->size_mask) - 1; - } else { - return (r - w) - 1; } + + return (r - w) - 1; } uint32_t diff --git a/zix/sorted_array.c b/zix/sorted_array.c index ffab77c..c96ea2b 100644 --- a/zix/sorted_array.c +++ b/zix/sorted_array.c @@ -151,7 +151,9 @@ zix_sorted_array_find(const ZixSortedArray* a, if (cmp == 0) { *ai = elem_i; return ZIX_STATUS_SUCCESS; - } else if (cmp > 0) { + } + + if (cmp > 0) { upper = i - 1; } else { lower = i + 1; diff --git a/zix/strindex.c b/zix/strindex.c index cbc218d..2fdd04f 100644 --- a/zix/strindex.c +++ b/zix/strindex.c @@ -234,18 +234,20 @@ zix_strindex_find(ZixStrindex* strindex, const char* const str, char** match) *match = n->children[child_i].first; assert(!strncmp(*match, orig_p, strlen(orig_p))); return ZIX_STATUS_SUCCESS; - } else if (strindex_is_leaf(n)) { + } + + if (strindex_is_leaf(n)) { /* Hit leaf early, no match */ return ZIX_STATUS_NOT_FOUND; - } else { - /* Match at this node, continue search downwards (or match) */ - p += label_len; - n = &n->children[child_i]; - if (label_len >= p_len) { - assert(strstr(strindex->s, orig_p) != NULL); - assert(strncmp(orig_p, *match, max_len)); - return ZIX_STATUS_SUCCESS; - } + } + + /* Match at this node, continue search downwards (or match) */ + p += label_len; + n = &n->children[child_i]; + if (label_len >= p_len) { + assert(strstr(strindex->s, orig_p) != NULL); + assert(strncmp(orig_p, *match, max_len)); + return ZIX_STATUS_SUCCESS; } } else { @@ -598,7 +598,9 @@ zix_tree_find(const ZixTree* t, const void* e, ZixTreeIter** ti) const int cmp = t->cmp(e, n->data, t->cmp_data); if (cmp == 0) { break; - } else if (cmp < 0) { + } + + if (cmp < 0) { n = n->left; } else { n = n->right; |