From 11f090116f9fc033c3d5642cb04ac3524651e102 Mon Sep 17 00:00:00 2001 From: Christoffer Lerno Date: Fri, 23 Jan 2026 13:09:34 +0100 Subject: [PATCH] Allow disabling asserts. --- src/compiler_tests/tests.c | 6 ++++++ src/utils/common.h | 11 +++++------ 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/compiler_tests/tests.c b/src/compiler_tests/tests.c index 0c9ef5433..3ff9cc684 100644 --- a/src/compiler_tests/tests.c +++ b/src/compiler_tests/tests.c @@ -10,6 +10,12 @@ #include "benchmark.h" #include "utils/json.h" +#define TEST_ASSERT(condition_, string_) while (!(condition_)) { FATAL_ERROR(string_); } +#define TEST_ASSERTF(condition_, format_, ...) while (!(condition_)) { FATAL_ERROR(format_, __VA_ARGS__); } +#define EXPECT(_string, _value, _expected) \ + do { long long __tempval1 = _value; long long __tempval2 = _expected; \ + TEST_ASSERT(__tempval1 == __tempval2, "Checking " _string ": expected %lld but was %lld.", __tempval2, __tempval1); } while(0) + void test_file(void) { File file; diff --git a/src/utils/common.h b/src/utils/common.h index 6c8404c39..cb7901999 100644 --- a/src/utils/common.h +++ b/src/utils/common.h @@ -141,7 +141,6 @@ ##__VA_ARGS__, __func__, __FILE__, __LINE__); } while(0) -#define ASSERT(_condition) do { if (!(_condition)) { FATAL_ERROR("Violated assert: " #_condition); } } while (0) #define WARNING(_string, ...) do { eprintf("WARNING: "); eprintf(_string, ##__VA_ARGS__); eprintf("\n"); } while(0) #define UNREACHABLE_VOID FATAL_ERROR("Should be unreachable"); #define UNREACHABLE UNREACHABLE_VOID; return 0; @@ -149,12 +148,12 @@ #define TODO FATAL_ERROR("TODO reached"); #define UNSUPPORTED do { error_exit("Unsupported functionality"); } while (0) -#define TEST_ASSERT(condition_, string_) while (!(condition_)) { FATAL_ERROR(string_); } -#define TEST_ASSERTF(condition_, format_, ...) while (!(condition_)) { FATAL_ERROR(format_, __VA_ARGS__); } +#if NDEBUG && NOASSERTS +#define ASSERT(_condition) do { } while (0) +#else +#define ASSERT(_condition) do { if (!(_condition)) { FATAL_ERROR("Violated assert: " #_condition); } } while (0) +#endif -#define EXPECT(_string, _value, _expected) \ - do { long long __tempval1 = _value; long long __tempval2 = _expected; \ - TEST_ASSERT(__tempval1 == __tempval2, "Checking " _string ": expected %lld but was %lld.", __tempval2, __tempval1); } while(0) void evprintf(const char *format, va_list list); void eprintf(const char *format, ...);