From 8eb06fd162c8828739f8619437351d98d7105ec9 Mon Sep 17 00:00:00 2001 From: Daan De Meyer Date: Wed, 24 Jul 2024 09:55:21 +0200 Subject: [PATCH] test: Add ASSERT_FAIL() --- src/shared/tests.h | 10 ++++++++++ src/test/test-macro.c | 5 +++++ 2 files changed, 15 insertions(+) diff --git a/src/shared/tests.h b/src/shared/tests.h index 820c3f70b74..ced403483cc 100644 --- a/src/shared/tests.h +++ b/src/shared/tests.h @@ -227,6 +227,16 @@ static inline int run_test_table(void) { } \ }) +#define ASSERT_FAIL(expr) \ + ({ \ + typeof(expr) _result = (expr); \ + if (_result >= 0) { \ + log_error_errno(_result, "%s:%i: Assertion failed: expected \"%s\" to fail, but it succeeded", \ + PROJECT_FILE, __LINE__, #expr); \ + abort(); \ + } \ + }) + #define ASSERT_ERROR(expr1, expr2) \ ({ \ int _expr1 = (expr1); \ diff --git a/src/test/test-macro.c b/src/test/test-macro.c index 9e2875d8a71..1d2993a3ae4 100644 --- a/src/test/test-macro.c +++ b/src/test/test-macro.c @@ -1122,6 +1122,11 @@ TEST(ASSERT) { ASSERT_SIGNAL(ASSERT_OK_ERRNO(-1), SIGABRT); ASSERT_SIGNAL(ASSERT_OK_ERRNO(-ENOANO), SIGABRT); + ASSERT_FAIL(-ENOENT); + ASSERT_FAIL(-EPERM); + ASSERT_SIGNAL(ASSERT_FAIL(0), SIGABRT); + ASSERT_SIGNAL(ASSERT_FAIL(255), SIGABRT); + ASSERT_ERROR(-ENOENT, ENOENT); ASSERT_ERROR(RET_NERRNO(mkdir("/i/will/fail/with/enoent", 666)), ENOENT); ASSERT_SIGNAL(ASSERT_ERROR(0, ENOENT), SIGABRT); -- 2.47.3