summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDaan De Meyer <daan.j.demeyer@gmail.com>2024-08-27 12:15:24 +0200
committerDaan De Meyer <daan.j.demeyer@gmail.com>2024-08-27 12:15:24 +0200
commit1ee6a08623610f222bed30d4b39af69a8cf634f7 (patch)
tree65ec2b127ecfda7a04b2916bbc91e4861285ea68
parenttests: Remove some unnecessary quotes (diff)
downloadsystemd-1ee6a08623610f222bed30d4b39af69a8cf634f7.tar.xz
systemd-1ee6a08623610f222bed30d4b39af69a8cf634f7.zip
tests: Add ASSERT_OK_EQ()
-rw-r--r--src/shared/tests.h20
-rw-r--r--src/test/test-macro.c6
2 files changed, 26 insertions, 0 deletions
diff --git a/src/shared/tests.h b/src/shared/tests.h
index eb848b081f..0c1b8cfa78 100644
--- a/src/shared/tests.h
+++ b/src/shared/tests.h
@@ -251,6 +251,26 @@ static inline int run_test_table(void) {
} \
})
+#define ASSERT_OK_EQ(expr1, expr2) \
+ ({ \
+ typeof(expr1) _expr1 = (expr1); \
+ typeof(expr2) _expr2 = (expr2); \
+ if (_expr1 < 0) { \
+ log_error_errno(_expr1, "%s:%i: Assertion failed: expected \"%s\" to succeed but got the following error: %m", \
+ PROJECT_FILE, __LINE__, #expr1); \
+ abort(); \
+ } \
+ if (_expr1 != _expr2) { \
+ char _sexpr1[DECIMAL_STR_MAX(typeof(expr1))]; \
+ char _sexpr2[DECIMAL_STR_MAX(typeof(expr2))]; \
+ xsprintf(_sexpr1, DECIMAL_STR_FMT(_expr1), _expr1); \
+ xsprintf(_sexpr2, DECIMAL_STR_FMT(_expr2), _expr2); \
+ log_error("%s:%i: Assertion failed: expected \"%s == %s\", but %s != %s", \
+ PROJECT_FILE, __LINE__, #expr1, #expr2, _sexpr1, _sexpr2); \
+ abort(); \
+ } \
+ })
+
#define ASSERT_OK_ERRNO(expr) \
({ \
typeof(expr) _result = (expr); \
diff --git a/src/test/test-macro.c b/src/test/test-macro.c
index b56f5b86e4..69df6bc567 100644
--- a/src/test/test-macro.c
+++ b/src/test/test-macro.c
@@ -1129,6 +1129,12 @@ TEST(ASSERT) {
ASSERT_SIGNAL(ASSERT_OK_ZERO(-1), SIGABRT);
ASSERT_SIGNAL(ASSERT_OK_ZERO(-ENOANO), SIGABRT);
+ ASSERT_OK_EQ(0, 0);
+ ASSERT_SIGNAL(ASSERT_OK_EQ(1, 0), SIGABRT);
+ ASSERT_SIGNAL(ASSERT_OK_EQ(255, 5), SIGABRT);
+ ASSERT_SIGNAL(ASSERT_OK_EQ(-1, 0), SIGABRT);
+ ASSERT_SIGNAL(ASSERT_OK_EQ(-ENOANO, 0), SIGABRT);
+
ASSERT_OK_ERRNO(0 >= 0);
ASSERT_OK_ERRNO(255 >= 0);
ASSERT_OK_ERRNO(printf("Hello world\n"));