tests: add new ASSERT_OK_OR macro

IN_SET() fails if __VA_ARGS__ is just one item. I inserted a bogus 0 item into
the check to work around this.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek
2025-06-28 19:11:13 +02:00
parent 0adbb11b4e
commit 367b325554
2 changed files with 26 additions and 0 deletions

View File

@@ -182,6 +182,22 @@ _noreturn_ void log_test_failed_internal(const char *file, int line, const char
})
#endif
#ifdef __COVERITY__
# define ASSERT_OK_OR(expr, ...) \
({ \
typeof(expr) _result = (expr); \
__coverity_check__(_result >= 0 || IN_SET(_result, 0, __VA_ARGS__) \
})
#else
# define ASSERT_OK_OR(expr, ...) \
({ \
typeof(expr) _result = (expr); \
if (_result < 0 && !IN_SET(_result, 0, __VA_ARGS__)) \
log_test_failed("\"%s\" failed with unexpected error: %"PRIiMAX"/%s", \
#expr, (intmax_t) _result, STRERROR(_result)); \
})
#endif
/* For functions that return a boolean on success and a negative errno on failure. */
#ifdef __COVERITY__
# define ASSERT_OK_POSITIVE(expr) __coverity_check__((expr) > 0)

View File

@@ -127,4 +127,14 @@ TEST(ASSERT) {
ASSERT_SIGNAL(ASSERT_NE_ID128(SD_ID128_NULL, SD_ID128_NULL), SIGABRT);
}
TEST(ASSERT_OK_OR) {
ASSERT_OK_OR(0, -EINVAL, -EUCLEAN);
ASSERT_OK_OR(99, -EINVAL, -EUCLEAN);
ASSERT_OK_OR(-EINVAL, -EINVAL, -EUCLEAN);
ASSERT_OK_OR(-EUCLEAN, -EUCLEAN);
ASSERT_OK_OR(-1, -EPERM);
ASSERT_SIGNAL(ASSERT_OK_OR(-1, -2), SIGABRT);
}
DEFINE_TEST_MAIN(LOG_INFO);