From 012116d975ff72da6f66486aa6547e2b5ce5e991 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Sat, 18 Sep 2021 20:11:35 -0400 Subject: Use assertions in Bitset test --- test/bitset_test.c | 66 ++++++++++++------------------------------------------ 1 file changed, 14 insertions(+), 52 deletions(-) (limited to 'test/bitset_test.c') diff --git a/test/bitset_test.c b/test/bitset_test.c index 261b1c3..ef61eeb 100644 --- a/test/bitset_test.c +++ b/test/bitset_test.c @@ -1,30 +1,17 @@ // Copyright 2014-2020 David Robillard // SPDX-License-Identifier: ISC -#include "zix/attributes.h" +#undef NDEBUG + #include "zix/bitset.h" -#include -#include -#include +#include #define N_BITS 256u #define N_ELEMS (ZIX_BITSET_ELEMS(N_BITS)) #define MIN(a, b) (((a) < (b)) ? (a) : (b)) -ZIX_LOG_FUNC(1, 2) -static int -test_fail(const char* fmt, ...) -{ - va_list args; - va_start(args, fmt); - fprintf(stderr, "error: "); - vfprintf(stderr, fmt, args); - va_end(args); - return 1; -} - int main(void) { @@ -32,39 +19,28 @@ main(void) ZixBitsetTally t[N_ELEMS]; zix_bitset_clear(b, t, N_BITS); - if (zix_bitset_count_up_to(b, t, N_BITS) != 0) { - return test_fail("Cleared bitset has non-zero count\n"); - } + assert(zix_bitset_count_up_to(b, t, N_BITS) == 0); for (size_t i = 0; i < N_BITS; ++i) { zix_bitset_set(b, t, i); - const size_t count = zix_bitset_count_up_to(b, t, N_BITS); - if (count != i + 1) { - return test_fail("Count %" PRIuPTR " != %" PRIuPTR "\n", count, i + 1); - } + assert(zix_bitset_get(b, i)); - if (!zix_bitset_get(b, i)) { - return test_fail("Bit %" PRIuPTR " is not set\n", i); - } + const size_t count = zix_bitset_count_up_to(b, t, N_BITS); + assert(count == i + 1); } for (size_t i = 0; i <= N_BITS; ++i) { const size_t count = zix_bitset_count_up_to(b, t, i); - if (count != i) { - return test_fail( - "Count to %" PRIuPTR " is %" PRIuPTR " != %" PRIuPTR "\n", i, count, i); - } + assert(count == i); } for (size_t i = 0; i <= N_BITS; ++i) { if (i < N_BITS) { zix_bitset_reset(b, t, i); } + const size_t count = zix_bitset_count_up_to(b, t, i); - if (count != 0) { - return test_fail( - "Count to %" PRIuPTR " is %" PRIuPTR " != %d\n", i, count, 0); - } + assert(count == 0); } zix_bitset_clear(b, t, N_BITS); @@ -73,13 +49,8 @@ main(void) const size_t count = zix_bitset_count_up_to(b, t, i + 1); const size_t result = MIN(N_BITS / 2, i / 2 + 1); - if (count != result) { - return test_fail("Count to %" PRIuPTR " is %" PRIuPTR " != %" PRIuPTR - "\n", - i, - count, - result); - } + + assert(count == result); } zix_bitset_clear(b, t, N_BITS); @@ -89,18 +60,9 @@ main(void) const size_t count = zix_bitset_count_up_to_if(b, t, i); const size_t result = MIN(N_BITS / 2, i / 2); - if (count != result) { - return test_fail("Count to %" PRIuPTR " is %" PRIuPTR " != %" PRIuPTR - "\n", - i, - count, - result); - } + assert(count == result); } else { - if (zix_bitset_count_up_to_if(b, t, i) != (size_t)-1) { - return test_fail( - "Got unexpected non-zero count at index %" PRIuPTR "\n", i); - } + assert(zix_bitset_count_up_to_if(b, t, i) == (size_t)-1); } } -- cgit v1.2.1