prog_util: add ASSERT() macro

This commit is contained in:
Eric Biggers 2018-12-23 12:03:00 -06:00
parent becd91bb63
commit 6c26eb18ea
3 changed files with 18 additions and 11 deletions

View File

@ -95,6 +95,14 @@ msg_errno(const char *format, ...)
va_end(va);
}
/* Abort with an error message */
_noreturn void
assertion_failed(const char *expr, const char *file, int line)
{
msg("Assertion failed: %s at %s:%d", expr, file, line);
abort();
}
/* malloc() wrapper */
void *
xmalloc(size_t size)

View File

@ -44,8 +44,10 @@
#ifdef __GNUC__
# define _printf(str_idx, args_idx) \
__attribute__((format(printf, str_idx, args_idx)))
# define _noreturn __attribute__((noreturn))
#else
# define _printf(str_idx, args_idx)
# define _noreturn
#endif
#ifdef _WIN32
@ -119,6 +121,11 @@ extern const tchar *program_invocation_name;
extern void _printf(1, 2) msg(const char *fmt, ...);
extern void _printf(1, 2) msg_errno(const char *fmt, ...);
extern void _noreturn
assertion_failed(const char *expr, const char *file, int line);
#define ASSERT(expr) if (!(expr)) assertion_failed(#expr, __FILE__, __LINE__)
extern void *xmalloc(size_t size);
extern u64 timer_ticks(void);

View File

@ -13,16 +13,6 @@
static unsigned int rng_seed;
static void
assertion_failed(const char *file, int line)
{
fprintf(stderr, "Assertion failed at %s:%d\n", file, line);
fprintf(stderr, "RNG seed was %u\n", rng_seed);
abort();
}
#define ASSERT(expr) if (!(expr)) assertion_failed(__FILE__, __LINE__);
typedef u32 (*cksum_fn_t)(u32, const void *, size_t);
static u32
@ -138,7 +128,9 @@ static void test_random_buffers(u8 *buffer, size_t limit, u32 num_iter)
int
tmain(int argc, tchar *argv[])
{
u8 *buffer = malloc(32768);
u8 *buffer = xmalloc(32768);
program_invocation_name = get_filename(argv[0]);
rng_seed = time(NULL);
srand(rng_seed);