From 3e4787b7ac8cb28a1125ebc1e042d00bc58a91b5 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Wed, 25 Jun 2025 17:10:41 -0400 Subject: diag: Simplify macros with __VA_OPT__ --- tests/tests.h | 33 +++++++++++++-------------------- 1 file changed, 13 insertions(+), 20 deletions(-) (limited to 'tests') diff --git a/tests/tests.h b/tests/tests.h index d395c7c..cd5715f 100644 --- a/tests/tests.h +++ b/tests/tests.h @@ -8,6 +8,7 @@ #ifndef BFS_TESTS_H #define BFS_TESTS_H +#include "bfs.h" #include "bfstd.h" #include "diag.h" @@ -44,31 +45,23 @@ bool bfs_check_impl(bool result); /** * Check a condition, logging a message on failure but continuing. */ -#define bfs_check(...) \ - bfs_check_(#__VA_ARGS__, __VA_ARGS__, "", ) +#define bfs_check(cond, ...) \ + bfs_check_impl((cond) || (bfs_check_(#cond, __VA_ARGS__), false)) -#define bfs_check_(str, cond, format, ...) \ - bfs_check_impl((cond) || (bfs_check__(format, BFS_DIAG_MSG_(format, str), __VA_ARGS__), false)) - -#define bfs_check__(format, ...) \ - bfs_diagf(sizeof(format) > 1 \ - ? BFS_DIAG_FORMAT_("%s" format "%s") \ - : BFS_DIAG_FORMAT_("Check failed: `%s`"), \ - BFS_DIAG_ARGS_(__VA_ARGS__)) +#define bfs_check_(str, ...) \ + BFS_VA_IF(__VA_ARGS__) \ + (bfs_diag(__VA_ARGS__)) \ + (bfs_diag("Check failed: `%s`", str)) /** * Check a condition, logging the current error string on failure. */ -#define bfs_echeck(...) \ - bfs_echeck_(#__VA_ARGS__, __VA_ARGS__, "", ) - -#define bfs_echeck_(str, cond, format, ...) \ - bfs_check_impl((cond) || (bfs_echeck__(format, BFS_DIAG_MSG_(format, str), __VA_ARGS__), false)) +#define bfs_echeck(cond, ...) \ + bfs_check_impl((cond) || (bfs_echeck_(#cond, __VA_ARGS__), false)) -#define bfs_echeck__(format, ...) \ - bfs_diagf(sizeof(format) > 1 \ - ? BFS_DIAG_FORMAT_("%s" format "%s: %s") \ - : BFS_DIAG_FORMAT_("Check failed: `%s`: %s"), \ - BFS_DIAG_ARGS_(__VA_ARGS__ errstr(), )) +#define bfs_echeck_(str, ...) \ + BFS_VA_IF(__VA_ARGS__) \ + (bfs_ediag(__VA_ARGS__)) \ + (bfs_ediag("Check failed: `%s`", str)) #endif // BFS_TESTS_H -- cgit v1.2.3