diff options
author | Luke T. Shumaker <lukeshu@lukeshu.com> | 2025-05-14 12:20:02 -0600 |
---|---|---|
committer | Luke T. Shumaker <lukeshu@lukeshu.com> | 2025-05-15 14:51:15 -0600 |
commit | 67cec6d2770aa14a13c89247612f16c628ebd54c (patch) | |
tree | 0a64beaa2f57f5b6e4db5e2ea84811cb05406958 /libmisc/tests/test_assert.c | |
parent | f01b89b599d40df5d6c127310b0030cd41c8dc85 (diff) |
libmisc: Remove uses of printf
Diffstat (limited to 'libmisc/tests/test_assert.c')
-rw-r--r-- | libmisc/tests/test_assert.c | 70 |
1 files changed, 39 insertions, 31 deletions
diff --git a/libmisc/tests/test_assert.c b/libmisc/tests/test_assert.c index 131cd3a..cdbc567 100644 --- a/libmisc/tests/test_assert.c +++ b/libmisc/tests/test_assert.c @@ -9,23 +9,22 @@ #include <stdlib.h> #include <string.h> -#include <libmisc/macro.h> -#include <libmisc/assert.h> #include <libmisc/_intercept.h> +#include <libmisc/assert.h> +#include <libmisc/fmt.h> +#include <libmisc/macro.h> #include "test.h" /* Intercept failures and logging *********************************************/ -bool global_failed; -char *global_log; -jmp_buf global_env; +static bool global_failed; +static struct fmt_buf global_log; +static jmp_buf global_env; #define with_intercept() ({ \ global_failed = false; \ - if (global_log) \ - free(global_log); \ - global_log = NULL; \ + global_log_clear(); \ setjmp(global_env) == 0; \ }) @@ -34,12 +33,19 @@ void __lm_abort(void) { longjmp(global_env, 1); } -size_t __lm_light_printf(const char *format, ...) { - va_list va; - va_start(va, format); - size_t ret = (size_t) vasprintf(&global_log, format, va); - va_end(va); - return ret; +void __lm_putchar(unsigned char c) { + if (global_log.len+1 >= global_log.cap) { + global_log.cap += 16; + global_log.dat = realloc(global_log.dat, global_log.cap); + memset(global_log.dat + global_log.len, 0, global_log.cap - global_log.len); + } + ((uint8_t *)global_log.dat)[global_log.len++] = (uint8_t)c; +} + +static void global_log_clear(void) { + if (global_log.dat) + memset(global_log.dat, 0, global_log.cap); + global_log.len = 0; } #define __builtin_unreachable() test_assert(0) @@ -51,21 +57,21 @@ size_t __lm_light_printf(const char *format, ...) { test; \ } \ test_assert(global_failed == false); \ - test_assert(global_log == NULL); \ + test_assert(global_log.len == 0); \ } while (0) -#define test_should_fail(test, exp_log) do { \ - if (with_intercept()) { \ - test; \ - } \ - test_assert(global_failed == true); \ - if (!(global_log != NULL && \ - strcmp(global_log, exp_log) == 0)) { \ - printf("exp = \"%s\"\n" \ - "act = \"%s\"\n", \ - exp_log, global_log); \ - test_assert(0); \ - } \ +#define test_should_fail(test, exp_log) do { \ + if (with_intercept()) { \ + test; \ + } \ + test_assert(global_failed == true); \ + if (!(global_log.len != 0 && \ + strcmp(global_log.dat, exp_log) == 0)) { \ + printf("exp = \"%s\"\n" \ + "act = \"%s\"\n", \ + exp_log, (char *)global_log.dat); \ + test_assert(0); \ + } \ } while (0) /* Actual tests ***************************************************************/ @@ -83,11 +89,13 @@ int main() { test_should_fail(assert_msg(false, NULL), "error: ASSERT: "__FILE__":"LM_STR_(__LINE__)":main(): assertion \"false\" failed\n"); test_should_fail(assert_notreached("xxx"), "error: ASSERT: "__FILE__":"LM_STR_(__LINE__)":main(): assertion \"notreached\" failed: xxx\n"); +#endif - if (global_log) { - free(global_log); - global_log = NULL; + if (global_log.dat) { + global_log_clear(); + free(global_log.dat); + global_log.dat = NULL; + global_log.cap = 0; } -#endif return 0; } |