202 lines
18 KiB
Diff
202 lines
18 KiB
Diff
From c6d8c9e1d90a26789aefcfa66903b48f859a7d76 Mon Sep 17 00:00:00 2001
|
|
From: Unique-Usman <usmanakinyemi202@gmail.com>
|
|
Date: Wed, 20 Mar 2024 23:05:55 +0530
|
|
Subject: [PATCH] Added more ASSERT macro and also make some test file to use
|
|
them
|
|
|
|
[dtardon: Only the macro definitions have been backported.]
|
|
|
|
(cherry picked from commit 5f0e4d2fb4188b58dd24c749a732faf6fef1f75b)
|
|
|
|
Related: RHEL-108481
|
|
---
|
|
src/shared/tests.h | 170 ++++++++++++++++++++++++++++++++++-----------
|
|
1 file changed, 131 insertions(+), 39 deletions(-)
|
|
|
|
diff --git a/src/shared/tests.h b/src/shared/tests.h
|
|
index 89248d171a..8a3b928dfd 100644
|
|
--- a/src/shared/tests.h
|
|
+++ b/src/shared/tests.h
|
|
@@ -154,50 +154,142 @@ static inline int run_test_table(void) {
|
|
} \
|
|
})
|
|
|
|
+#define ASSERT_TRUE(expr) \
|
|
+ ({ \
|
|
+ if (!(expr)) { \
|
|
+ log_error("%s:%i: Assertion failed: expected \"%s\" to be true", \
|
|
+ PROJECT_FILE, __LINE__, #expr); \
|
|
+ abort(); \
|
|
+ } \
|
|
+ })
|
|
+
|
|
+#define ASSERT_FALSE(expr) \
|
|
+ ({ \
|
|
+ if ((expr)) { \
|
|
+ log_error("%s:%i: Assertion failed: expected \"%s\" to be false", \
|
|
+ PROJECT_FILE, __LINE__, #expr); \
|
|
+ abort(); \
|
|
+ } \
|
|
+ })
|
|
+
|
|
+#define ASSERT_NULL(expr) \
|
|
+ ({ \
|
|
+ if ((expr) != NULL) { \
|
|
+ log_error("%s:%i: Assertion failed: expected \"%s\" to be NULL", \
|
|
+ PROJECT_FILE, __LINE__, #expr); \
|
|
+ abort(); \
|
|
+ } \
|
|
+ })
|
|
+
|
|
+#define ASSERT_NOT_NULL(expr) \
|
|
+ ({ \
|
|
+ if ((expr) == NULL) { \
|
|
+ log_error("%s:%i: Assertion failed: expected \"%s\" to be not NULL", \
|
|
+ PROJECT_FILE, __LINE__, #expr); \
|
|
+ abort(); \
|
|
+ } \
|
|
+ })
|
|
+
|
|
+#define ASSERT_STREQ(expr1, expr2) \
|
|
+ ({ \
|
|
+ const char* _expr1 = (expr1); \
|
|
+ const char* _expr2 = (expr2); \
|
|
+ if (strcmp(_expr1, _expr2) != 0) { \
|
|
+ log_error("%s:%i: Assertion failed: expected \"%s == %s\", but \"%s != %s\"", \
|
|
+ PROJECT_FILE, __LINE__, #expr1, #expr2, _expr1, _expr2); \
|
|
+ abort(); \
|
|
+ } \
|
|
+ })
|
|
+
|
|
/* DECIMAL_STR_FMT() uses _Generic which cannot be used in string concatenation so we have to format the
|
|
* input into strings first and then format those into the final assertion message. */
|
|
|
|
-#define ASSERT_EQ(expr1, expr2) \
|
|
- ({ \
|
|
- typeof(expr1) _expr1 = (expr1); \
|
|
- typeof(expr2) _expr2 = (expr2); \
|
|
- 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_EQ(expr1, expr2) \
|
|
+ ({ \
|
|
+ typeof(expr1) _expr1 = (expr1); \
|
|
+ typeof(expr2) _expr2 = (expr2); \
|
|
+ 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_GE(expr1, expr2) \
|
|
+ ({ \
|
|
+ typeof(expr1) _expr1 = (expr1); \
|
|
+ typeof(expr2) _expr2 = (expr2); \
|
|
+ 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_LE(expr1, expr2) \
|
|
+ ({ \
|
|
+ typeof(expr1) _expr1 = (expr1); \
|
|
+ typeof(expr2) _expr2 = (expr2); \
|
|
+ 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_NE(expr1, expr2) \
|
|
+ ({ \
|
|
+ typeof(expr1) _expr1 = (expr1); \
|
|
+ typeof(expr2) _expr2 = (expr2); \
|
|
+ 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_GE(expr1, expr2) \
|
|
- ({ \
|
|
- typeof(expr1) _expr1 = (expr1); \
|
|
- typeof(expr2) _expr2 = (expr2); \
|
|
- 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_GT(expr1, expr2) \
|
|
+ ({ \
|
|
+ typeof(expr1) _expr1 = (expr1); \
|
|
+ typeof(expr2) _expr2 = (expr2); \
|
|
+ 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_LE(expr1, expr2) \
|
|
- ({ \
|
|
- typeof(expr1) _expr1 = (expr1); \
|
|
- typeof(expr2) _expr2 = (expr2); \
|
|
- 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_LT(expr1, expr2) \
|
|
+ ({ \
|
|
+ typeof(expr1) _expr1 = (expr1); \
|
|
+ typeof(expr2) _expr2 = (expr2); \
|
|
+ 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(); \
|
|
+ } \
|
|
})
|