summaryrefslogtreecommitdiff
path: root/libmisc/tests/test_assert.c
diff options
context:
space:
mode:
authorLuke T. Shumaker <lukeshu@lukeshu.com>2024-11-19 23:18:45 -0700
committerLuke T. Shumaker <lukeshu@lukeshu.com>2024-11-23 13:50:30 -0700
commit70ba86d426accc06696402a8d3928ee5845b8f89 (patch)
treeb920cb0bed517d3552050d0e39b8c289b5921090 /libmisc/tests/test_assert.c
parent910c85365ff3d7b2601132fd53542b6e594fa855 (diff)
libmisc: Logging/assert fixes
Diffstat (limited to 'libmisc/tests/test_assert.c')
-rw-r--r--libmisc/tests/test_assert.c85
1 files changed, 55 insertions, 30 deletions
diff --git a/libmisc/tests/test_assert.c b/libmisc/tests/test_assert.c
index 949b4f9..a2ac743 100644
--- a/libmisc/tests/test_assert.c
+++ b/libmisc/tests/test_assert.c
@@ -4,62 +4,87 @@
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
+#include <setjmp.h>
#include <stdbool.h>
+#include <string.h>
+#include <stdlib.h>
#include <libmisc/assert.h>
#include "test.h"
-/* Intercept failures *********************************************************/
-
-bool global_failed = false;
-bool global_unreachable = false;
-
-void __assert_msg_fail(const char *expr,
- const char *file, unsigned int line, const char *func,
- const char *msg) {
- static bool in_fail = false;
- if (!in_fail) {
- in_fail = true;
- printf("error: %s:%u:%s(): assertion \"%s\" failed%s%s\n",
- file, line, func,
- expr,
- msg ? ": " : "", msg);
- }
+#define UNUSED(name)
+
+/* Intercept failures and logging *********************************************/
+
+bool global_failed;
+char *global_log;
+jmp_buf global_env;
+
+#define with_intercept() ({ \
+ global_failed = false; \
+ if (global_log) \
+ free(global_log); \
+ global_log = NULL; \
+ setjmp(global_env) == 0; \
+ })
+
+__attribute__((noreturn)) void abort(void) {
global_failed = true;
+ longjmp(global_env, 1);
}
-#define __builtin_unreachable() do { global_unreachable = true; } while (0)
+#define __builtin_unreachable() test_assert(0)
+
+int vprintf(const char *format, va_list ap) {
+ return vasprintf(&global_log, format, ap);
+}
/* Utilities ******************************************************************/
-#define test_should_succeed(test) do { \
- global_failed = false; \
- test; \
- test_assert(global_failed == false); \
+#define test_should_succeed(test) do { \
+ if (with_intercept()) { \
+ test; \
+ } \
+ test_assert(global_failed == false); \
+ test_assert(global_log == NULL); \
} while (0)
-#define test_should_fail(test) do { \
- global_failed = false; \
- test; \
- test_assert(global_failed == true); \
+#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); \
+ } \
} while (0)
+#define _STR(x) #x
+#define STR(x) _STR(x)
+
/* Actual tests ***************************************************************/
static_assert(sizeof(char) == 1);
int main() {
test_should_succeed(assert(true));
- test_should_fail(assert(false));
+ test_should_fail(assert(false), "error: ASSERT: "__FILE__":"STR(__LINE__)":main(): assertion \"false\" failed\n");
test_should_succeed(assert_msg(true, "foo"));
- test_should_fail(assert_msg(false, "foo"));
+ test_should_fail(assert_msg(false, "foo"), "error: ASSERT: "__FILE__":"STR(__LINE__)":main(): assertion \"false\" failed: foo\n");
test_should_succeed(assert_msg(true, NULL));
- test_should_fail(assert_msg(false, NULL));
+ test_should_fail(assert_msg(false, NULL), "error: ASSERT: "__FILE__":"STR(__LINE__)":main(): assertion \"false\" failed\n");
- test_should_fail(assert_notreached(""));
- test_assert(global_unreachable == true);
+ test_should_fail(assert_notreached("xxx"), "error: ASSERT: "__FILE__":"STR(__LINE__)":main(): assertion \"notreached\" failed: xxx\n");
+ if (global_log) {
+ free(global_log);
+ global_log = NULL;
+ }
return 0;
}