From 3756c77e536a293fc735726eac0e5f0c3a8b89da Mon Sep 17 00:00:00 2001 From: "Luke T. Shumaker" Date: Sat, 21 Dec 2024 23:50:09 -0700 Subject: libmisc: Check that assert.h has no deps --- libmisc/tests/test_assert_min.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 libmisc/tests/test_assert_min.c (limited to 'libmisc/tests') diff --git a/libmisc/tests/test_assert_min.c b/libmisc/tests/test_assert_min.c new file mode 100644 index 0000000..9c0394b --- /dev/null +++ b/libmisc/tests/test_assert_min.c @@ -0,0 +1,17 @@ +/* libmisc/tests/test_assert_min.c - Tests for minimal + * + * Copyright (C) 2024 Luke T. Shumaker + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +/* Don't include *anything* else. */ +#include + +static_assert(1 == 1); + +int main() { + assert_msg(1, "foo"); + assert(1); + return 0; + assert_notreached("ret"); +} -- cgit v1.2.3-2-g168b From 79284b25994d90149f4a9a5286ec739770db94ea Mon Sep 17 00:00:00 2001 From: "Luke T. Shumaker" Date: Tue, 17 Dec 2024 19:18:21 -0700 Subject: libmisc: Rework how test-intercepts work --- libmisc/tests/test_assert.c | 18 +++++++++++------ libmisc/tests/test_log.c | 47 ++++++++++++++++++++++++++------------------- libmisc/tests/test_rand.c | 13 ++----------- 3 files changed, 41 insertions(+), 37 deletions(-) (limited to 'libmisc/tests') diff --git a/libmisc/tests/test_assert.c b/libmisc/tests/test_assert.c index 189f6de..0c51bbd 100644 --- a/libmisc/tests/test_assert.c +++ b/libmisc/tests/test_assert.c @@ -5,12 +5,14 @@ */ #include +#include /* for va_list, va_start(), va_end() */ #include -#include #include +#include #include #include +#include #include "test.h" @@ -28,17 +30,21 @@ jmp_buf global_env; setjmp(global_env) == 0; \ }) -[[noreturn]] void abort(void) { +void __lm_abort(void) { global_failed = true; longjmp(global_env, 1); } -#define __builtin_unreachable() test_assert(0) - -int vprintf(const char *format, va_list ap) { - return vasprintf(&global_log, format, ap); +int __lm_printf(const char *format, ...) { + va_list va; + va_start(va, format); + int ret = vasprintf(&global_log, format, va); + va_end(va); + return ret; } +#define __builtin_unreachable() test_assert(0) + /* Utilities ******************************************************************/ #define test_should_succeed(test) do { \ diff --git a/libmisc/tests/test_log.c b/libmisc/tests/test_log.c index 286738d..e819c09 100644 --- a/libmisc/tests/test_log.c +++ b/libmisc/tests/test_log.c @@ -13,36 +13,43 @@ #define LOG_NAME FROBNICATE #include +#include + #include "test.h" /* Intercept output ***********************************************************/ static char *log_output = NULL; -int vprintf(const char *format, va_list ap) { - return vasprintf(&log_output, format, ap); +int __lm_printf(const char *format, ...) { + va_list va; + va_start(va, format); + int ret = vasprintf(&log_output, format, va); + va_end(va); + return ret; } /* Actual tests ***************************************************************/ -#define should_print(_exp, cmd) do { \ - char *exp = _exp; \ - test_assert(!log_output); \ - cmd; \ - if (!exp) \ - test_assert(!log_output); \ - else \ - if (!(log_output != NULL && \ - strcmp(log_output, exp) == 0)) { \ - printf("exp = \"%s\"\n" \ - "act = \"%s\"\n", \ - exp, log_output); \ - test_assert(0); \ - } \ - if (log_output) { \ - free(log_output); \ - log_output = NULL; \ - } \ +#define should_print(_exp, cmd) do { \ + char *exp = _exp; \ + test_assert(!log_output); \ + cmd; \ + if (!exp) \ + test_assert(!log_output); \ + else { \ + test_assert(log_output); \ + if (strcmp(log_output, exp)) { \ + printf("exp = \"%s\"\n" \ + "act = \"%s\"\n", \ + exp, log_output); \ + test_assert(0); \ + } \ + } \ + if (log_output) { \ + free(log_output); \ + log_output = NULL; \ + } \ } while (0) int main() { diff --git a/libmisc/tests/test_rand.c b/libmisc/tests/test_rand.c index fff1b27..295ea61 100644 --- a/libmisc/tests/test_rand.c +++ b/libmisc/tests/test_rand.c @@ -8,6 +8,7 @@ #include #include +#include #include "test.h" @@ -15,19 +16,9 @@ jmp_buf *__catch; -void __assert_msg_fail(const char *expr, - const char *file, unsigned int line, const char *func, - const char *msg) { - static bool in_fail = false; +void __lm_abort(void) { if (__catch) longjmp(*__catch, 1); - if (!in_fail) { - in_fail = true; - printf("error: %s:%u:%s(): assertion \"%s\" failed%s%s\n", - file, line, func, - expr, - msg ? ": " : "", msg); - } abort(); } -- cgit v1.2.3-2-g168b From e783ff4d07ecb0a7bae90651c17735069a094a82 Mon Sep 17 00:00:00 2001 From: "Luke T. Shumaker" Date: Thu, 26 Dec 2024 18:36:11 -0700 Subject: libmisc: test_rand: Avoid divide-by-zero, even if its result is ignored --- libmisc/tests/test_rand.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'libmisc/tests') diff --git a/libmisc/tests/test_rand.c b/libmisc/tests/test_rand.c index 295ea61..282c6a4 100644 --- a/libmisc/tests/test_rand.c +++ b/libmisc/tests/test_rand.c @@ -48,14 +48,14 @@ static void test_n(uint64_t cnt) { bool seen[MAX_SEE_ALL] = {0}; for (int i = 0; i < ROUNDS; i++) { uint64_t val = rand_uint63n(cnt); - sum += ((double)val)/(cnt-1); + sum += val; test_assert(val < cnt); if (cnt < MAX_SEE_ALL) seen[val] = true; } if (cnt > 1) { - test_assert(sum/ROUNDS > 0.45); - test_assert(sum/ROUNDS < 0.55); + test_assert(sum/ROUNDS > 0.45*(cnt-1)); + test_assert(sum/ROUNDS < 0.55*(cnt-1)); } if (cnt < MAX_SEE_ALL) { for (uint64_t i = 0; i < cnt; i++) -- cgit v1.2.3-2-g168b From 1aecc70750ee6ce9c96ebf3e6b4a7fb322ff8ca3 Mon Sep 17 00:00:00 2001 From: "Luke T. Shumaker" Date: Tue, 17 Dec 2024 19:18:21 -0700 Subject: Build the full matrix of CMAKE_BUILD_TYPE --- libmisc/tests/test_assert.c | 2 ++ libmisc/tests/test_log.c | 2 ++ libmisc/tests/test_rand.c | 6 ++++++ 3 files changed, 10 insertions(+) (limited to 'libmisc/tests') diff --git a/libmisc/tests/test_assert.c b/libmisc/tests/test_assert.c index 0c51bbd..3c2d6b6 100644 --- a/libmisc/tests/test_assert.c +++ b/libmisc/tests/test_assert.c @@ -74,6 +74,7 @@ int __lm_printf(const char *format, ...) { static_assert(sizeof(char) == 1); int main() { +#ifndef NDEBUG test_should_succeed(assert(true)); test_should_fail(assert(false), "error: ASSERT: "__FILE__":"LM_STR_(__LINE__)":main(): assertion \"false\" failed\n"); @@ -88,5 +89,6 @@ int main() { free(global_log); global_log = NULL; } +#endif return 0; } diff --git a/libmisc/tests/test_log.c b/libmisc/tests/test_log.c index e819c09..49a76ca 100644 --- a/libmisc/tests/test_log.c +++ b/libmisc/tests/test_log.c @@ -57,6 +57,7 @@ int main() { errorf("val=%d", 42)); should_print("info : FROBNICATE: val=0\n", infof("val=%d", 0)); +#ifndef NDEBUG #define CONFIG_FROBNICATE_DEBUG 1 should_print("debug: FROBNICATE: val=-2\n", debugf("val=%d", -2)); @@ -64,5 +65,6 @@ int main() { #define CONFIG_FROBNICATE_DEBUG 0 should_print(NULL, debugf("val=%d", -2)); +#endif return 0; } diff --git a/libmisc/tests/test_rand.c b/libmisc/tests/test_rand.c index 282c6a4..8076155 100644 --- a/libmisc/tests/test_rand.c +++ b/libmisc/tests/test_rand.c @@ -14,6 +14,7 @@ /* Intercept failures *********************************************************/ +#ifndef NDEBUG jmp_buf *__catch; void __lm_abort(void) { @@ -34,6 +35,7 @@ void __lm_abort(void) { __catch = old_catch; \ } \ } while (0); +#endif /* Actual tests ***************************************************************/ @@ -42,7 +44,11 @@ void __lm_abort(void) { static void test_n(uint64_t cnt) { if (cnt == 0 || cnt > UINT64_C(1)<<63) { +#ifndef NDEBUG should_abort(rand_uint63n(cnt)); +#else + return; +#endif } else { double sum = 0; bool seen[MAX_SEE_ALL] = {0}; -- cgit v1.2.3-2-g168b