summaryrefslogtreecommitdiffstats
path: root/tests/bit.c
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@tavianator.com>2024-07-07 12:59:39 -0400
committerTavian Barnes <tavianator@tavianator.com>2024-07-07 13:12:56 -0400
commit3206124fb3af2481fc45e705f7bba3ea56016433 (patch)
tree994f03b0d9d1db2e410a7bef155f0e5b4e03fe28 /tests/bit.c
parent144353ab004bcd3e85f4cdd0a848add7811df2fe (diff)
downloadbfs-3206124fb3af2481fc45e705f7bba3ea56016433.tar.xz
tests: Simplify unit tests with a global variable
It's a little awkward to thread the test result through manually; much easier to just make bfs_check() update a global variable.
Diffstat (limited to 'tests/bit.c')
-rw-r--r--tests/bit.c112
1 files changed, 54 insertions, 58 deletions
diff --git a/tests/bit.c b/tests/bit.c
index 49e167d..64fb5ea 100644
--- a/tests/bit.c
+++ b/tests/bit.c
@@ -56,86 +56,82 @@ bfs_static_assert(INTMAX_MAX == IWIDTH_MAX(INTMAX_WIDTH));
#define check_eq(a, b) \
bfs_check((a) == (b), "(0x%jX) %s != %s (0x%jX)", (uintmax_t)(a), #a, #b, (uintmax_t)(b))
-bool check_bit(void) {
- bool ret = true;
-
+void check_bit(void) {
const char *str = "\x1\x2\x3\x4";
uint32_t word;
memcpy(&word, str, sizeof(word));
#if ENDIAN_NATIVE == ENDIAN_LITTLE
- ret &= check_eq(word, 0x04030201);
+ check_eq(word, 0x04030201);
#elif ENDIAN_NATIVE == ENDIAN_BIG
- ret &= check_eq(word, 0x01020304);
+ check_eq(word, 0x01020304);
#else
# warning "Skipping byte order tests on mixed/unknown-endian machine"
#endif
- ret &= check_eq(bswap((uint8_t)0x12), 0x12);
- ret &= check_eq(bswap((uint16_t)0x1234), 0x3412);
- ret &= check_eq(bswap((uint32_t)0x12345678), 0x78563412);
- ret &= check_eq(bswap((uint64_t)0x1234567812345678), 0x7856341278563412);
-
- ret &= check_eq(count_ones(0x0U), 0);
- ret &= check_eq(count_ones(0x1U), 1);
- ret &= check_eq(count_ones(0x2U), 1);
- ret &= check_eq(count_ones(0x3U), 2);
- ret &= check_eq(count_ones(0x137FU), 10);
-
- ret &= check_eq(count_zeros(0U), UINT_WIDTH);
- ret &= check_eq(count_zeros(0UL), ULONG_WIDTH);
- ret &= check_eq(count_zeros(0ULL), ULLONG_WIDTH);
- ret &= check_eq(count_zeros((uint8_t)0), 8);
- ret &= check_eq(count_zeros((uint16_t)0), 16);
- ret &= check_eq(count_zeros((uint32_t)0), 32);
- ret &= check_eq(count_zeros((uint64_t)0), 64);
-
- ret &= check_eq(rotate_left((uint8_t)0xA1, 4), 0x1A);
- ret &= check_eq(rotate_left((uint16_t)0x1234, 12), 0x4123);
- ret &= check_eq(rotate_left((uint32_t)0x12345678, 20), 0x67812345);
- ret &= check_eq(rotate_left((uint32_t)0x12345678, 0), 0x12345678);
-
- ret &= check_eq(rotate_right((uint8_t)0xA1, 4), 0x1A);
- ret &= check_eq(rotate_right((uint16_t)0x1234, 12), 0x2341);
- ret &= check_eq(rotate_right((uint32_t)0x12345678, 20), 0x45678123);
- ret &= check_eq(rotate_right((uint32_t)0x12345678, 0), 0x12345678);
+ check_eq(bswap((uint8_t)0x12), 0x12);
+ check_eq(bswap((uint16_t)0x1234), 0x3412);
+ check_eq(bswap((uint32_t)0x12345678), 0x78563412);
+ check_eq(bswap((uint64_t)0x1234567812345678), 0x7856341278563412);
+
+ check_eq(count_ones(0x0U), 0);
+ check_eq(count_ones(0x1U), 1);
+ check_eq(count_ones(0x2U), 1);
+ check_eq(count_ones(0x3U), 2);
+ check_eq(count_ones(0x137FU), 10);
+
+ check_eq(count_zeros(0U), UINT_WIDTH);
+ check_eq(count_zeros(0UL), ULONG_WIDTH);
+ check_eq(count_zeros(0ULL), ULLONG_WIDTH);
+ check_eq(count_zeros((uint8_t)0), 8);
+ check_eq(count_zeros((uint16_t)0), 16);
+ check_eq(count_zeros((uint32_t)0), 32);
+ check_eq(count_zeros((uint64_t)0), 64);
+
+ check_eq(rotate_left((uint8_t)0xA1, 4), 0x1A);
+ check_eq(rotate_left((uint16_t)0x1234, 12), 0x4123);
+ check_eq(rotate_left((uint32_t)0x12345678, 20), 0x67812345);
+ check_eq(rotate_left((uint32_t)0x12345678, 0), 0x12345678);
+
+ check_eq(rotate_right((uint8_t)0xA1, 4), 0x1A);
+ check_eq(rotate_right((uint16_t)0x1234, 12), 0x2341);
+ check_eq(rotate_right((uint32_t)0x12345678, 20), 0x45678123);
+ check_eq(rotate_right((uint32_t)0x12345678, 0), 0x12345678);
for (unsigned int i = 0; i < 16; ++i) {
uint16_t n = (uint16_t)1 << i;
for (unsigned int j = i; j < 16; ++j) {
uint16_t m = (uint16_t)1 << j;
uint16_t nm = n | m;
- ret &= check_eq(count_ones(nm), 1 + (n != m));
- ret &= check_eq(count_zeros(nm), 15 - (n != m));
- ret &= check_eq(leading_zeros(nm), 15 - j);
- ret &= check_eq(trailing_zeros(nm), i);
- ret &= check_eq(first_leading_one(nm), 16 - j);
- ret &= check_eq(first_trailing_one(nm), i + 1);
- ret &= check_eq(bit_width(nm), j + 1);
- ret &= check_eq(bit_floor(nm), m);
+ check_eq(count_ones(nm), 1 + (n != m));
+ check_eq(count_zeros(nm), 15 - (n != m));
+ check_eq(leading_zeros(nm), 15 - j);
+ check_eq(trailing_zeros(nm), i);
+ check_eq(first_leading_one(nm), 16 - j);
+ check_eq(first_trailing_one(nm), i + 1);
+ check_eq(bit_width(nm), j + 1);
+ check_eq(bit_floor(nm), m);
if (n == m) {
- ret &= check_eq(bit_ceil(nm), m);
- ret &= bfs_check(has_single_bit(nm));
+ check_eq(bit_ceil(nm), m);
+ bfs_check(has_single_bit(nm));
} else {
if (j < 15) {
- ret &= check_eq(bit_ceil(nm), (m << 1));
+ check_eq(bit_ceil(nm), (m << 1));
}
- ret &= bfs_check(!has_single_bit(nm));
+ bfs_check(!has_single_bit(nm));
}
}
}
- ret &= check_eq(leading_zeros((uint16_t)0), 16);
- ret &= check_eq(trailing_zeros((uint16_t)0), 16);
- ret &= check_eq(first_leading_one(0U), 0);
- ret &= check_eq(first_trailing_one(0U), 0);
- ret &= check_eq(bit_width(0U), 0);
- ret &= check_eq(bit_floor(0U), 0);
- ret &= check_eq(bit_ceil(0U), 1);
-
- ret &= bfs_check(!has_single_bit(0U));
- ret &= bfs_check(!has_single_bit(UINT32_MAX));
- ret &= bfs_check(has_single_bit((uint32_t)1 << (UINT_WIDTH - 1)));
-
- return ret;
+ check_eq(leading_zeros((uint16_t)0), 16);
+ check_eq(trailing_zeros((uint16_t)0), 16);
+ check_eq(first_leading_one(0U), 0);
+ check_eq(first_trailing_one(0U), 0);
+ check_eq(bit_width(0U), 0);
+ check_eq(bit_floor(0U), 0);
+ check_eq(bit_ceil(0U), 1);
+
+ bfs_check(!has_single_bit(0U));
+ bfs_check(!has_single_bit(UINT32_MAX));
+ bfs_check(has_single_bit((uint32_t)1 << (UINT_WIDTH - 1)));
}