Commit ed649c5415 for strongswan.org
commit ed649c54153413fd9496e0548afc6380b24134ec
Author: Tobias Brunner <tobias@strongswan.org>
Date: Tue Sep 22 16:53:16 2026 +0200
unit-tests: Use correct format specifiers if integer comparison fails
While the macro is generally type-safe due to its use of `typeof()`, the
format string for the error message always used `%d`. For instance, it
would print `-1` for a failed comparison with `UINT64_MAX`, which is
confusing.
This change adds a generic macro that determines the right format string
based on the local variable's type and prints a proper representation
of each value to a temporary buffer. That's necessary because
`_Generic()` is an expression and can't be concatenated with a string.
diff --git a/src/libstrongswan/tests/test_suite.h b/src/libstrongswan/tests/test_suite.h
index b0cdb1214b..406ac689a4 100644
--- a/src/libstrongswan/tests/test_suite.h
+++ b/src/libstrongswan/tests/test_suite.h
@@ -287,6 +287,22 @@ void test_warn_msg(const char *file, int line, char *fmt, ...);
*/
void test_fail_if_worker_failed();
+/**
+ * Generic macro to produce a type-specific format specifier
+ *
+ * The default covers \c int and all automatically promoted smaller types.
+ *
+ * @param x integer variable for which to determine the specifier
+ */
+#define test_int_fmt(x) _Generic((x), \
+ long: "%ld", \
+ long long: "%lld", \
+ unsigned long: "%lu", \
+ unsigned long long: "%llu", \
+ unsigned int: "%u", \
+ default: "%d" \
+)
+
/**
* Check if two integers equal, fail test if not
*
@@ -300,7 +316,11 @@ void test_fail_if_worker_failed();
test_fail_if_worker_failed(); \
if (_a != _b) \
{ \
- test_fail_msg(__FILE__, __LINE__, #a " != " #b " (%d != %d)", _a, _b); \
+ char _sa[32], _sb[32]; \
+ snprintf(_sa, sizeof(_sa), test_int_fmt(_a), _a); \
+ snprintf(_sb, sizeof(_sb), test_int_fmt(_b), _b); \
+ test_fail_msg(__FILE__, __LINE__, \
+ #a " != " #b " (%s != %s)", _sa, _sb); \
} \
})