summaryrefslogtreecommitdiffstats
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
parent63a52b1bfc99c58f0a944174282da79aab5bde3a (diff)
downloadbfs-526133c11eb9a26a4cffb20bcd10bcbb36d940de.tar.xz
Switch from assert() to bfs_assert()/bfs_verify()
-rw-r--r--src/bfstd.c3
-rw-r--r--src/bftw.c28
-rw-r--r--src/color.c5
-rw-r--r--src/diag.c3
-rw-r--r--src/dir.c4
-rw-r--r--src/eval.c11
-rw-r--r--src/exec.c7
-rw-r--r--src/opt.c13
-rw-r--r--src/parse.c7
-rw-r--r--src/printf.c5
-rw-r--r--src/trie.c33
-rw-r--r--src/xregex.c4
-rw-r--r--tests/bfstd.c13
-rw-r--r--tests/bit.c100
-rw-r--r--tests/trie.c56
15 files changed, 140 insertions, 152 deletions
diff --git a/src/bfstd.c b/src/bfstd.c
index 91383a2..37f5276 100644
--- a/src/bfstd.c
+++ b/src/bfstd.c
@@ -5,7 +5,6 @@
#include "config.h"
#include "diag.h"
#include "xregex.h"
-#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <langinfo.h>
@@ -393,7 +392,7 @@ void close_quietly(int fd) {
int xclose(int fd) {
int ret = close(fd);
if (ret != 0) {
- assert(errno != EBADF);
+ bfs_verify(errno != EBADF);
}
return ret;
}
diff --git a/src/bftw.c b/src/bftw.c
index 14805de..6ec5cfa 100644
--- a/src/bftw.c
+++ b/src/bftw.c
@@ -19,13 +19,13 @@
#include "bftw.h"
#include "bfstd.h"
#include "config.h"
+#include "diag.h"
#include "dir.h"
#include "dstring.h"
#include "list.h"
#include "mtab.h"
#include "stat.h"
#include "trie.h"
-#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
@@ -114,7 +114,7 @@ static void bftw_cache_remove(struct bftw_cache *cache, struct bftw_file *file)
/** Close a bftw_file. */
static void bftw_file_close(struct bftw_cache *cache, struct bftw_file *file) {
- assert(file->fd >= 0);
+ bfs_assert(file->fd >= 0);
if (LIST_ATTACHED(cache, file, lru)) {
bftw_cache_remove(cache, file);
@@ -137,7 +137,7 @@ static int bftw_cache_pop(struct bftw_cache *cache) {
/** Add a bftw_file to the cache. */
static int bftw_cache_add(struct bftw_cache *cache, struct bftw_file *file) {
- assert(file->fd >= 0);
+ bfs_assert(file->fd >= 0);
if (cache->capacity == 0 && bftw_cache_pop(cache) != 0) {
bftw_file_close(cache, file);
@@ -145,7 +145,7 @@ static int bftw_cache_add(struct bftw_cache *cache, struct bftw_file *file) {
return -1;
}
- assert(cache->capacity > 0);
+ bfs_assert(cache->capacity > 0);
--cache->capacity;
LIST_INSERT(cache, cache->target, file, lru);
@@ -169,9 +169,9 @@ static size_t bftw_child_nameoff(const struct bftw_file *parent) {
/** Destroy a cache. */
static void bftw_cache_destroy(struct bftw_cache *cache) {
- assert(!cache->head);
- assert(!cache->tail);
- assert(!cache->target);
+ bfs_assert(!cache->head);
+ bfs_assert(!cache->tail);
+ bfs_assert(!cache->target);
}
/** Create a new bftw_file. */
@@ -230,7 +230,7 @@ static struct bftw_file *bftw_file_new(struct bftw_file *parent, const char *nam
* The opened file descriptor, or negative on error.
*/
static int bftw_file_openat(struct bftw_cache *cache, struct bftw_file *file, struct bftw_file *base, const char *at_path) {
- assert(file->fd < 0);
+ bfs_assert(file->fd < 0);
int at_fd = AT_FDCWD;
if (base) {
@@ -332,7 +332,7 @@ static struct bfs_dir *bftw_file_opendir(struct bftw_cache *cache, struct bftw_f
/** Free a bftw_file. */
static void bftw_file_free(struct bftw_cache *cache, struct bftw_file *file) {
- assert(file->refcount == 0);
+ bfs_assert(file->refcount == 0);
if (file->fd >= 0) {
bftw_file_close(cache, file);
@@ -770,7 +770,7 @@ static enum bftw_action bftw_call_back(struct bftw_state *state, const char *nam
/** Pop a directory to read from the queue. */
static bool bftw_pop_dir(struct bftw_state *state) {
- assert(!state->file);
+ bfs_assert(!state->file);
if (!state->dirs.head) {
return false;
@@ -787,7 +787,7 @@ static bool bftw_pop_dir(struct bftw_state *state) {
/** Pop a file to visit from the queue. */
static bool bftw_pop_file(struct bftw_state *state) {
- assert(!state->file);
+ bfs_assert(!state->file);
state->file = state->files.head;
if (state->file) {
@@ -802,8 +802,8 @@ static bool bftw_pop_file(struct bftw_state *state) {
* Open the current directory.
*/
static int bftw_opendir(struct bftw_state *state) {
- assert(!state->dir);
- assert(!state->de);
+ bfs_assert(!state->dir);
+ bfs_assert(!state->de);
state->direrror = 0;
@@ -863,7 +863,7 @@ static int bftw_gc(struct bftw_state *state, enum bftw_gc_flags flags) {
if (state->dir) {
struct bftw_file *file = state->file;
- assert(file && file->fd >= 0);
+ bfs_assert(file && file->fd >= 0);
if (file->refcount > 1) {
// Keep the fd around if any subdirectories exist
diff --git a/src/color.c b/src/color.c
index 43ea9a4..a723084 100644
--- a/src/color.c
+++ b/src/color.c
@@ -12,7 +12,6 @@
#include "fsade.h"
#include "stat.h"
#include "trie.h"
-#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <stdarg.h>
@@ -699,7 +698,7 @@ static int print_colored(CFILE *cfile, const char *esc, const char *str, size_t
/** Find the offset of the first broken path component. */
static ssize_t first_broken_offset(const char *path, const struct BFTW *ftwbuf, enum bfs_stat_flags flags, size_t max) {
ssize_t ret = max;
- assert(ret >= 0);
+ bfs_assert(ret >= 0);
if (bftw_type(ftwbuf, flags) != BFS_ERROR) {
goto out;
@@ -1100,7 +1099,7 @@ static int cbuff(CFILE *cfile, const char *format, ...) {
}
int cvfprintf(CFILE *cfile, const char *format, va_list args) {
- assert(dstrlen(cfile->buffer) == 0);
+ bfs_assert(dstrlen(cfile->buffer) == 0);
int ret = -1;
if (cvbuff(cfile, format, args) == 0) {
diff --git a/src/diag.c b/src/diag.c
index d7ffaa6..04e3678 100644
--- a/src/diag.c
+++ b/src/diag.c
@@ -6,7 +6,6 @@
#include "ctx.h"
#include "color.h"
#include "expr.h"
-#include <assert.h>
#include <errno.h>
#include <stdarg.h>
#include <stdlib.h>
@@ -117,7 +116,7 @@ static bool highlight_expr_recursive(const struct bfs_ctx *ctx, const struct bfs
for (size_t i = 0; i < ctx->argc; ++i) {
if (&ctx->argv[i] == expr->argv) {
for (size_t j = 0; j < expr->argc; ++j) {
- assert(i + j < ctx->argc);
+ bfs_assert(i + j < ctx->argc);
args[i + j] = true;
ret = true;
}
diff --git a/src/dir.c b/src/dir.c
index 6739f10..d9ee63b 100644
--- a/src/dir.c
+++ b/src/dir.c
@@ -4,7 +4,7 @@
#include "dir.h"
#include "bfstd.h"
#include "config.h"
-#include <assert.h>
+#include "diag.h"
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
@@ -249,7 +249,7 @@ int bfs_closedir(struct bfs_dir *dir) {
int ret = xclose(dir->fd);
#else
int ret = closedir(dir->dir);
- assert(ret == 0 || errno != EBADF);
+ bfs_verify(ret == 0 || errno != EBADF);
#endif
free(dir);
return ret;
diff --git a/src/eval.c b/src/eval.c
index 6cad3a9..342debd 100644
--- a/src/eval.c
+++ b/src/eval.c
@@ -26,7 +26,6 @@
#include "trie.h"
#include "xregex.h"
#include "xtime.h"
-#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <fnmatch.h>
@@ -990,7 +989,7 @@ static bool eval_expr(struct bfs_expr *expr, struct bfs_eval *state) {
}
}
- assert(!state->quit);
+ bfs_assert(!state->quit);
bool ret = expr->eval_fn(expr, state);
@@ -1006,10 +1005,10 @@ static bool eval_expr(struct bfs_expr *expr, struct bfs_eval *state) {
}
if (bfs_expr_never_returns(expr)) {
- assert(state->quit);
+ bfs_assert(state->quit);
} else if (!state->quit) {
- assert(!expr->always_true || ret);
- assert(!expr->always_false || !ret);
+ bfs_assert(!expr->always_true || ret);
+ bfs_assert(!expr->always_false || !ret);
}
return ret;
@@ -1511,7 +1510,7 @@ static void dump_bftw_flags(enum bftw_flags flags) {
DEBUG_FLAG(flags, BFTW_SORT);
DEBUG_FLAG(flags, BFTW_BUFFER);
- assert(!flags);
+ bfs_assert(flags == 0, "Missing bftw flag 0x%X", flags);
}
/**
diff --git a/src/exec.c b/src/exec.c
index 7f22d36..5912ad6 100644
--- a/src/exec.c
+++ b/src/exec.c
@@ -10,7 +10,6 @@
#include "diag.h"
#include "dstring.h"
#include "xspawn.h"
-#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <stdarg.h>
@@ -276,12 +275,12 @@ static void bfs_exec_free_arg(char *arg, const char *tmpl) {
/** Open a file to use as the working directory. */
static int bfs_exec_openwd(struct bfs_exec *execbuf, const struct BFTW *ftwbuf) {
- assert(execbuf->wd_fd < 0);
- assert(!execbuf->wd_path);
+ bfs_assert(execbuf->wd_fd < 0);
+ bfs_assert(!execbuf->wd_path);
if (ftwbuf->at_fd != AT_FDCWD) {
// Rely on at_fd being the immediate parent
- assert(xbaseoff(ftwbuf->at_path) == 0);
+ bfs_assert(xbaseoff(ftwbuf->at_path) == 0);
execbuf->wd_fd = ftwbuf->at_fd;
if (!(execbuf->flags & BFS_EXEC_MULTI)) {
diff --git a/src/opt.c b/src/opt.c
index 4ce9425..4699af4 100644
--- a/src/opt.c
+++ b/src/opt.c
@@ -35,7 +35,6 @@
#include "exec.h"
#include "expr.h"
#include "pwcache.h"
-#include <assert.h>
#include <errno.h>
#include <limits.h>
#include <stdarg.h>
@@ -308,7 +307,7 @@ struct opt_state {
/** Log an optimization. */
BFS_FORMATTER(3, 4)
static bool opt_debug(const struct opt_state *state, int level, const char *format, ...) {
- assert(state->ctx->optlevel >= level);
+ bfs_assert(state->ctx->optlevel >= level);
if (bfs_debug(state->ctx, DEBUG_OPT, "${cyn}-O%d${rs}: ", level)) {
va_list args;
@@ -387,7 +386,7 @@ static struct bfs_expr *de_morgan(const struct opt_state *state, struct bfs_expr
has_parent = false;
}
- assert(expr->eval_fn == eval_and || expr->eval_fn == eval_or);
+ bfs_assert(expr->eval_fn == eval_and || expr->eval_fn == eval_or);
if (expr->eval_fn == eval_and) {
expr->eval_fn = eval_or;
expr->argv = &fake_or_arg;
@@ -446,7 +445,7 @@ static struct bfs_expr *optimize_expr_recursive(struct opt_state *state, struct
* Optimize a negation.
*/
static struct bfs_expr *optimize_not_expr(const struct opt_state *state, struct bfs_expr *expr) {
- assert(expr->eval_fn == eval_not);
+ bfs_assert(expr->eval_fn == eval_not);
struct bfs_expr *rhs = expr->rhs;
@@ -498,7 +497,7 @@ fail:
/** Optimize a conjunction. */
static struct bfs_expr *optimize_and_expr(const struct opt_state *state, struct bfs_expr *expr) {
- assert(expr->eval_fn == eval_and);
+ bfs_assert(expr->eval_fn == eval_and);
struct bfs_expr *lhs = expr->lhs;
struct bfs_expr *rhs = expr->rhs;
@@ -569,7 +568,7 @@ fail:
/** Optimize a disjunction. */
static struct bfs_expr *optimize_or_expr(const struct opt_state *state, struct bfs_expr *expr) {
- assert(expr->eval_fn == eval_or);
+ bfs_assert(expr->eval_fn == eval_or);
struct bfs_expr *lhs = expr->lhs;
struct bfs_expr *rhs = expr->rhs;
@@ -673,7 +672,7 @@ static struct bfs_expr *ignore_result(const struct opt_state *state, struct bfs_
/** Optimize a comma expression. */
static struct bfs_expr *optimize_comma_expr(const struct opt_state *state, struct bfs_expr *expr) {
- assert(expr->eval_fn == eval_comma);
+ bfs_assert(expr->eval_fn == eval_comma);
struct bfs_expr *lhs = expr->lhs;
struct bfs_expr *rhs = expr->rhs;
diff --git a/src/parse.c b/src/parse.c
index 807892c..1a04e68 100644
--- a/src/parse.c
+++ b/src/parse.c
@@ -29,7 +29,6 @@
#include "xregex.h"
#include "xspawn.h"
#include "xtime.h"
-#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <fnmatch.h>
@@ -134,7 +133,7 @@ static struct bfs_expr *new_unary_expr(bfs_eval_fn *eval_fn, struct bfs_expr *rh
expr->lhs = NULL;
expr->rhs = rhs;
- assert(bfs_expr_is_parent(expr));
+ bfs_assert(bfs_expr_is_parent(expr));
expr->persistent_fds = rhs->persistent_fds;
expr->ephemeral_fds = rhs->ephemeral_fds;
@@ -154,7 +153,7 @@ static struct bfs_expr *new_binary_expr(bfs_eval_fn *eval_fn, struct bfs_expr *l
expr->lhs = lhs;
expr->rhs = rhs;
- assert(bfs_expr_is_parent(expr));
+ bfs_assert(bfs_expr_is_parent(expr));
expr->persistent_fds = lhs->persistent_fds + rhs->persistent_fds;
if (lhs->ephemeral_fds > rhs->ephemeral_fds) {
@@ -263,7 +262,7 @@ static void init_highlight(const struct bfs_ctx *ctx, bool *args) {
static void highlight_args(const struct bfs_ctx *ctx, char **argv, size_t argc, bool *args) {
size_t i = argv - ctx->argv;
for (size_t j = 0; j < argc; ++j) {
- assert(i + j < ctx->argc);
+ bfs_assert(i + j < ctx->argc);
args[i + j] = true;
}
}
diff --git a/src/printf.c b/src/printf.c
index 9ccc216..6520d2d 100644
--- a/src/printf.c
+++ b/src/printf.c
@@ -16,7 +16,6 @@
#include "pwcache.h"
#include "stat.h"
#include "xtime.h"
-#include <assert.h>
#include <errno.h>
#include <grp.h>
#include <pwd.h>
@@ -74,7 +73,7 @@ static bool should_color(CFILE *cfile, const struct bfs_printf *directive) {
#define BFS_PRINTF_BUF(buf, format, ...) \
char buf[256]; \
int ret = snprintf(buf, sizeof(buf), format, __VA_ARGS__); \
- assert(ret >= 0 && (size_t)ret < sizeof(buf)); \
+ bfs_assert(ret >= 0 && (size_t)ret < sizeof(buf)); \
(void)ret
/**
@@ -190,7 +189,7 @@ static int bfs_printf_strftime(CFILE *cfile, const struct bfs_printf *directive,
break;
}
- assert(ret >= 0 && (size_t)ret < sizeof(buf));
+ bfs_assert(ret >= 0 && (size_t)ret < sizeof(buf));
(void)ret;
return dyn_fprintf(cfile->file, directive, buf);
diff --git a/src/trie.c b/src/trie.c
index a2921de..8543eb1 100644
--- a/src/trie.c
+++ b/src/trie.c
@@ -86,7 +86,6 @@
#include "config.h"
#include "diag.h"
#include "list.h"
-#include <assert.h>
#include <limits.h>
#include <stdint.h>
#include <stdlib.h>
@@ -139,27 +138,27 @@ static bool trie_is_leaf(uintptr_t ptr) {
/** Decode a pointer to a leaf. */
static struct trie_leaf *trie_decode_leaf(uintptr_t ptr) {
- assert(trie_is_leaf(ptr));
+ bfs_assert(trie_is_leaf(ptr));
return (struct trie_leaf *)(ptr ^ 1);
}
/** Encode a pointer to a leaf. */
static uintptr_t trie_encode_leaf(const struct trie_leaf *leaf) {
uintptr_t ptr = (uintptr_t)leaf ^ 1;
- assert(trie_is_leaf(ptr));
+ bfs_assert(trie_is_leaf(ptr));
return ptr;
}
/** Decode a pointer to an internal node. */
static struct trie_node *trie_decode_node(uintptr_t ptr) {
- assert(!trie_is_leaf(ptr));
+ bfs_assert(!trie_is_leaf(ptr));
return (struct trie_node *)ptr;
}
/** Encode a pointer to an internal node. */
static uintptr_t trie_encode_node(const struct trie_node *node) {
uintptr_t ptr = (uintptr_t)node;
- assert(!trie_is_leaf(ptr));
+ bfs_assert(!trie_is_leaf(ptr));
return ptr;
}
@@ -341,9 +340,9 @@ static void trie_leaf_free(struct trie *trie, struct trie_leaf *leaf) {
/** Compute the size of a trie node with a certain number of children. */
static size_t trie_node_size(unsigned int size) {
// Empty nodes aren't supported
- assert(size > 0);
+ bfs_assert(size > 0);
// Node size must be a power of two
- assert(has_single_bit(size));
+ bfs_assert(has_single_bit(size));
return flex_sizeof(struct trie_node, children, size);
}
@@ -435,7 +434,7 @@ static struct trie_leaf *trie_node_insert(struct trie *trie, uintptr_t *ptr, str
unsigned int bit = 1U << nibble;
// The child must not already be present
- assert(!(node->bitmap & bit));
+ bfs_assert(!(node->bitmap & bit));
node->bitmap |= bit;
unsigned int target = count_ones(node->bitmap & (bit - 1));
@@ -474,7 +473,7 @@ static struct trie_leaf *trie_node_insert(struct trie *trie, uintptr_t *ptr, str
static uintptr_t *trie_jump(uintptr_t *ptr, const char *key, size_t *offset) {
// We only ever need to jump to leaf nodes, since internal nodes are
// guaranteed to be within OFFSET_MAX anyway
- assert(trie_is_leaf(*ptr));
+ bfs_assert(trie_is_leaf(*ptr));
struct trie_node *node = malloc(trie_node_size(1));
if (!node) {
@@ -512,7 +511,7 @@ static uintptr_t *trie_jump(uintptr_t *ptr, const char *key, size_t *offset) {
static struct trie_leaf *trie_split(struct trie *trie, uintptr_t *ptr, struct trie_leaf *leaf, struct trie_leaf *rep, size_t offset, size_t mismatch) {
unsigned char key_nibble = trie_key_nibble(leaf->key, mismatch);
unsigned char rep_nibble = trie_key_nibble(rep->key, mismatch);
- assert(key_nibble != rep_nibble);
+ bfs_assert(key_nibble != rep_nibble);
struct trie_node *node = malloc(trie_node_size(2));
if (!node) {
@@ -570,11 +569,11 @@ static struct trie_leaf *trie_insert_mem_impl(struct trie *trie, const void *key
unsigned char nibble = trie_key_nibble(key, offset);
unsigned int bit = 1U << nibble;
if (node->bitmap & bit) {
- assert(offset < mismatch);
+ bfs_assert(offset < mismatch);
unsigned int index = count_ones(node->bitmap & (bit - 1));
ptr = &node->children[index];
} else {
- assert(offset == mismatch);
+ bfs_assert(offset == mismatch);
return trie_node_insert(trie, ptr, leaf, nibble);
}
}
@@ -600,7 +599,7 @@ static void trie_free_singletons(struct trie *trie, uintptr_t ptr) {
struct trie_node *node = trie_decode_node(ptr);
// Make sure the bitmap is a power of two, i.e. it has just one child
- assert(has_single_bit(node->bitmap));
+ bfs_assert(has_single_bit(node->bitmap));
ptr = node->children[0];
free(node);
@@ -651,12 +650,12 @@ static void trie_remove_impl(struct trie *trie, struct trie_leaf *leaf) {
while (!trie_is_leaf(*child)) {
struct trie_node *node = trie_decode_node(*child);
offset += node->offset;
- assert((offset >> 1) < leaf->length);
+ bfs_assert((offset >> 1) < leaf->length);
unsigned char nibble = trie_key_nibble(leaf->key, offset);
unsigned int bit = 1U << nibble;
unsigned int bitmap = node->bitmap;
- assert(bitmap & bit);
+ bfs_assert(bitmap & bit);
unsigned int index = count_ones(bitmap & (bit - 1));
// Advance the parent pointer, unless this node had only one child
@@ -669,7 +668,7 @@ static void trie_remove_impl(struct trie *trie, struct trie_leaf *leaf) {
child = &node->children[index];
}
- assert(trie_decode_leaf(*child) == leaf);
+ bfs_assert(trie_decode_leaf(*child) == leaf);
if (!parent) {
trie_free_singletons(trie, trie->root);
@@ -683,7 +682,7 @@ static void trie_remove_impl(struct trie *trie, struct trie_leaf *leaf) {
node->bitmap ^= child_bit;
unsigned int parent_size = count_ones(node->bitmap);
- assert(parent_size > 0);
+ bfs_assert(parent_size > 0);
if (parent_size == 1 && trie_collapse_node(parent, node, child_index) == 0) {
return;
}
diff --git a/src/xregex.c b/src/xregex.c
index a7153b7..1143f23 100644
--- a/src/xregex.c
+++ b/src/xregex.c
@@ -3,7 +3,7 @@
#include "xregex.h"
#include "config.h"
-#include <assert.h>
+#include "diag.h"
#include <errno.h>
#include <stdlib.h>
#include <string.h>
@@ -140,7 +140,7 @@ int bfs_regcomp(struct bfs_regex **preg, const char *pattern, enum bfs_regex_typ
syntax = ONIG_SYNTAX_GREP;
break;
}
- assert(syntax);
+ bfs_assert(syntax);
OnigOptionType options = syntax->options;
if (flags & BFS_REGEX_ICASE) {
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);