]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
tests: Add ASSERT_OK_EQ()
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Tue, 27 Aug 2024 10:15:24 +0000 (12:15 +0200)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Tue, 27 Aug 2024 10:15:24 +0000 (12:15 +0200)
src/shared/tests.h
src/test/test-macro.c

index eb848b081f463702c6ed14a8cdb3fbfe240edc90..0c1b8cfa7808ad18d23b15f81266b54201fd59ea 100644 (file)
@@ -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);                                                                  \
index b56f5b86e4764df12b37f2967bb977170aa47e88..69df6bc56740d23a9899181ad9fc43fa8ba1220b 100644 (file)
@@ -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"));