summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@tavianator.com>2023-05-18 16:44:30 -0400
committerTavian Barnes <tavianator@tavianator.com>2023-05-18 16:46:13 -0400
commit526133c11eb9a26a4cffb20bcd10bcbb36d940de (patch)
tree26787e3cc22df2c44837f72f6ff919ab7808a8f6 /tests
parent63a52b1bfc99c58f0a944174282da79aab5bde3a (diff)
downloadbfs-526133c11eb9a26a4cffb20bcd10bcbb36d940de.tar.xz
Switch from assert() to bfs_assert()/bfs_verify()
Diffstat (limited to 'tests')
-rw-r--r--tests/bfstd.c13
-rw-r--r--tests/bit.c100
-rw-r--r--tests/trie.c56
3 files changed, 83 insertions, 86 deletions
diff --git a/tests/bfstd.c b/tests/bfstd.c
index a986a23..1812a00 100644
--- a/tests/bfstd.c
+++ b/tests/bfstd.c
@@ -1,10 +1,9 @@
// Copyright © Tavian Barnes <tavianator@tavianator.com>
// SPDX-License-Identifier: 0BSD
-#undef NDEBUG
#include "../src/bfstd.h"
#include "../src/config.h"
-#include <assert.h>
+#include "../src/diag.h"
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
@@ -39,11 +38,11 @@ int main(void) {
alignas(64) int foo;
int bar[];
};
- assert(flex_sizeof(struct flexible, bar, 0) >= sizeof(struct flexible));
- assert(flex_sizeof(struct flexible, bar, 16) % alignof(struct flexible) == 0);
- assert(flex_sizeof(struct flexible, bar, SIZE_MAX / sizeof(int) + 1)
- == align_floor(alignof(struct flexible), SIZE_MAX));
- assert(flex_sizeof_impl(8, 16, 4, 4, 1) == 16);
+ bfs_verify(flex_sizeof(struct flexible, bar, 0) >= sizeof(struct flexible));
+ bfs_verify(flex_sizeof(struct flexible, bar, 16) % alignof(struct flexible) == 0);
+ bfs_verify(flex_sizeof(struct flexible, bar, SIZE_MAX / sizeof(int) + 1)
+ == align_floor(alignof(struct flexible), SIZE_MAX));
+ bfs_verify(flex_sizeof_impl(8, 16, 4, 4, 1) == 16);
// From man 3p basename
check_base_dir("usr", ".", "usr");
diff --git a/tests/bit.c b/tests/bit.c
index 7a7b0f3..cb339f4 100644
--- a/tests/bit.c
+++ b/tests/bit.c
@@ -1,11 +1,8 @@
// Copyright © Tavian Barnes <tavianator@tavianator.com>
// SPDX-License-Identifier: 0BSD
-#undef NDEBUG
-
#include "../src/bit.h"
#include "../src/diag.h"
-#include <assert.h>
#include <limits.h>
#include <stdint.h>
#include <stdlib.h>
@@ -54,68 +51,71 @@ bfs_static_assert(UINTMAX_MAX == UWIDTH_MAX(UINTMAX_WIDTH));
bfs_static_assert(INTMAX_MIN == IWIDTH_MIN(INTMAX_WIDTH));
bfs_static_assert(INTMAX_MAX == IWIDTH_MAX(INTMAX_WIDTH));
+#define verify_eq(a, b) \
+ bfs_verify((a) == (b), "(0x%jX) %s != %s (0x%jX)", (uintmax_t)(a), #a, #b, (uintmax_t)(b))
+
int main(void) {
- assert(bswap((uint8_t)0x12) == 0x12);
- assert(bswap((uint16_t)0x1234) == 0x3412);
- assert(bswap((uint32_t)0x12345678) == 0x78563412);
- assert(bswap((uint64_t)0x1234567812345678) == 0x7856341278563412);
-
- assert(count_ones(0x0) == 0);
- assert(count_ones(0x1) == 1);
- assert(count_ones(0x2) == 1);
- assert(count_ones(0x3) == 2);
- assert(count_ones(0x137F) == 10);
-
- assert(count_zeros(0) == INT_WIDTH);
- assert(count_zeros(0L) == LONG_WIDTH);
- assert(count_zeros(0LL) == LLONG_WIDTH);
- assert(count_zeros((uint8_t)0) == 8);
- assert(count_zeros((uint16_t)0) == 16);
- assert(count_zeros((uint32_t)0) == 32);
- assert(count_zeros((uint64_t)0) == 64);
-
- assert(rotate_left((uint8_t)0xA1, 4) == 0x1A);
- assert(rotate_left((uint16_t)0x1234, 12) == 0x4123);
- assert(rotate_left((uint32_t)0x12345678, 20) == 0x67812345);
- assert(rotate_left((uint32_t)0x12345678, 0) == 0x12345678);
-
- assert(rotate_right((uint8_t)0xA1, 4) == 0x1A);
- assert(rotate_right((uint16_t)0x1234, 12) == 0x2341);
- assert(rotate_right((uint32_t)0x12345678, 20) == 0x45678123);
- assert(rotate_right((uint32_t)0x12345678, 0) == 0x12345678);
+ verify_eq(bswap((uint8_t)0x12), 0x12);
+ verify_eq(bswap((uint16_t)0x1234), 0x3412);
+ verify_eq(bswap((uint32_t)0x12345678), 0x78563412);
+ verify_eq(bswap((uint64_t)0x1234567812345678), 0x7856341278563412);
+
+ verify_eq(count_ones(0x0), 0);
+ verify_eq(count_ones(0x1), 1);
+ verify_eq(count_ones(0x2), 1);
+ verify_eq(count_ones(0x3), 2);
+ verify_eq(count_ones(0x137F), 10);
+
+ verify_eq(count_zeros(0), INT_WIDTH);
+ verify_eq(count_zeros(0L), LONG_WIDTH);
+ verify_eq(count_zeros(0LL), LLONG_WIDTH);
+ verify_eq(count_zeros((uint8_t)0), 8);
+ verify_eq(count_zeros((uint16_t)0), 16);
+ verify_eq(count_zeros((uint32_t)0), 32);
+ verify_eq(count_zeros((uint64_t)0), 64);
+
+ verify_eq(rotate_left((uint8_t)0xA1, 4), 0x1A);
+ verify_eq(rotate_left((uint16_t)0x1234, 12), 0x4123);
+ verify_eq(rotate_left((uint32_t)0x12345678, 20), 0x67812345);
+ verify_eq(rotate_left((uint32_t)0x12345678, 0), 0x12345678);
+
+ verify_eq(rotate_right((uint8_t)0xA1, 4), 0x1A);
+ verify_eq(rotate_right((uint16_t)0x1234, 12), 0x2341);
+ verify_eq(rotate_right((uint32_t)0x12345678, 20), 0x45678123);
+ verify_eq(rotate_right((uint32_t)0x12345678, 0), 0x12345678);
for (int i = 0; i < 16; ++i) {
uint16_t n = (uint16_t)1 << i;
for (int j = i; j < 16; ++j) {
uint16_t m = (uint16_t)1 << j;
uint16_t nm = n | m;
- assert(count_ones(nm) == 1 + (n != m));
- assert(count_zeros(nm) == 15 - (n != m));
- assert(leading_zeros(nm) == 15 - j);
- assert(trailing_zeros(nm) == i);
- assert(first_leading_one(nm) == j + 1);
- assert(first_trailing_one(nm) == i + 1);
- assert(bit_width(nm) == j + 1);
- assert(bit_floor(nm) == m);
+ verify_eq(count_ones(nm), 1 + (n != m));
+ verify_eq(count_zeros(nm), 15 - (n != m));
+ verify_eq(leading_zeros(nm), 15 - j);
+ verify_eq(trailing_zeros(nm), i);
+ verify_eq(first_leading_one(nm), j + 1);
+ verify_eq(first_trailing_one(nm), i + 1);
+ verify_eq(bit_width(nm), j + 1);
+ verify_eq(bit_floor(nm), m);
if (n == m) {
- assert(bit_ceil(nm) == m);
- assert(has_single_bit(nm));
+ verify_eq(bit_ceil(nm), m);
+ bfs_verify(has_single_bit(nm));
} else {
if (j < 15) {
- assert(bit_ceil(nm) == (m << 1));
+ verify_eq(bit_ceil(nm), (m << 1));
}
- assert(!has_single_bit(nm));
+ bfs_verify(!has_single_bit(nm));
}
}
}
- assert(leading_zeros((uint16_t)0) == 16);
- assert(trailing_zeros((uint16_t)0) == 16);
- assert(first_leading_one(0) == 0);
- assert(first_trailing_one(0) == 0);
- assert(bit_width(0) == 0);
- assert(bit_floor(0) == 0);
- assert(bit_ceil(0) == 1);
+ verify_eq(leading_zeros((uint16_t)0), 16);
+ verify_eq(trailing_zeros((uint16_t)0), 16);
+ verify_eq(first_leading_one(0), 0);
+ verify_eq(first_trailing_one(0), 0);
+ verify_eq(bit_width(0), 0);
+ verify_eq(bit_floor(0), 0);
+ verify_eq(bit_ceil(0), 1);
return EXIT_SUCCESS;
}
diff --git a/tests/trie.c b/tests/trie.c
index ced14d4..e687f96 100644
--- a/tests/trie.c
+++ b/tests/trie.c
@@ -1,11 +1,9 @@
// Copyright © Tavian Barnes <tavianator@tavianator.com>
// SPDX-License-Identifier: 0BSD
-#undef NDEBUG
-
#include "../src/trie.h"
#include "../src/config.h"
-#include <assert.h>
+#include "../src/diag.h"
#include <stdlib.h>
#include <string.h>
@@ -45,7 +43,7 @@ int main(void) {
trie_init(&trie);
for (size_t i = 0; i < nkeys; ++i) {
- assert(!trie_find_str(&trie, keys[i]));
+ bfs_verify(!trie_find_str(&trie, keys[i]));
const char *prefix = NULL;
for (size_t j = 0; j < i; ++j) {
@@ -58,38 +56,38 @@ int main(void) {
struct trie_leaf *leaf = trie_find_prefix(&trie, keys[i]);
if (prefix) {
- assert(leaf);
- assert(strcmp(prefix, leaf->key) == 0);
+ bfs_verify(leaf);
+ bfs_verify(strcmp(prefix, leaf->key) == 0);
} else {
- assert(!leaf);
+ bfs_verify(!leaf);
}
leaf = trie_insert_str(&trie, keys[i]);
- assert(leaf);
- assert(strcmp(keys[i], leaf->key) == 0);
- assert(leaf->length == strlen(keys[i]) + 1);
+ bfs_verify(leaf);
+ bfs_verify(strcmp(keys[i], leaf->key) == 0);
+ bfs_verify(leaf->length == strlen(keys[i]) + 1);
}
{
size_t i = 0;
TRIE_FOR_EACH(&trie, leaf) {
- assert(leaf == trie_find_str(&trie, keys[i]));
- assert(!leaf->prev || leaf->prev->next == leaf);
- assert(!leaf->next || leaf->next->prev == leaf);
+ bfs_verify(leaf == trie_find_str(&trie, keys[i]));
+ bfs_verify(!leaf->prev || leaf->prev->next == leaf);
+ bfs_verify(!leaf->next || leaf->next->prev == leaf);
++i;
}
- assert(i == nkeys);
+ bfs_verify(i == nkeys);
}
for (size_t i = 0; i < nkeys; ++i) {
struct trie_leaf *leaf = trie_find_str(&trie, keys[i]);
- assert(leaf);
- assert(strcmp(keys[i], leaf->key) == 0);
- assert(leaf->length == strlen(keys[i]) + 1);
+ bfs_verify(leaf);
+ bfs_verify(strcmp(keys[i], leaf->key) == 0);
+ bfs_verify(leaf->length == strlen(keys[i]) + 1);
trie_remove(&trie, leaf);
leaf = trie_find_str(&trie, keys[i]);
- assert(!leaf);
+ bfs_verify(!leaf);
const char *postfix = NULL;
for (size_t j = i + 1; j < nkeys; ++j) {
@@ -102,33 +100,33 @@ int main(void) {
leaf = trie_find_postfix(&trie, keys[i]);
if (postfix) {
- assert(leaf);
- assert(strcmp(postfix, leaf->key) == 0);
+ bfs_verify(leaf);
+ bfs_verify(strcmp(postfix, leaf->key) == 0);
} else {
- assert(!leaf);
+ bfs_verify(!leaf);
}
}
TRIE_FOR_EACH(&trie, leaf) {
- assert(false);
+ bfs_verify(false);
}
// This tests the "jump" node handling on 32-bit platforms
size_t longsize = 1 << 20;
char *longstr = malloc(longsize);
- assert(longstr);
+ bfs_verify(longstr);
memset(longstr, 0xAC, longsize);
- assert(!trie_find_mem(&trie, longstr, longsize));
- assert(trie_insert_mem(&trie, longstr, longsize));
+ bfs_verify(!trie_find_mem(&trie, longstr, longsize));
+ bfs_verify(trie_insert_mem(&trie, longstr, longsize));
memset(longstr + longsize/2, 0xAB, longsize/2);
- assert(!trie_find_mem(&trie, longstr, longsize));
- assert(trie_insert_mem(&trie, longstr, longsize));
+ bfs_verify(!trie_find_mem(&trie, longstr, longsize));
+ bfs_verify(trie_insert_mem(&trie, longstr, longsize));
memset(longstr, 0xAA, longsize/2);
- assert(!trie_find_mem(&trie, longstr, longsize));
- assert(trie_insert_mem(&trie, longstr, longsize));
+ bfs_verify(!trie_find_mem(&trie, longstr, longsize));
+ bfs_verify(trie_insert_mem(&trie, longstr, longsize));
free(longstr);
trie_destroy(&trie);