[LTP] [PATCH] tst_test_macros: Add TST_EXP_EQ_*() macros

Cyril Hrubis chrubis@suse.cz
Fri Feb 18 11:34:13 CET 2022


This is a family of macros to compare two values, so far long integers
and size_t/ssize_t have been added but we can add anything else once
needed as well.

The intended usage is:

...
        siginfo_t infop;

        TST_EXP_PASS(waitid(P_PGID, pid_group, &infop, WEXITED));

        TST_EXP_EQ_LI(infop.si_pid, pid_child);
        TST_EXP_EQ_LI(infop.si_status, 0);
        TST_EXP_EQ_LI(infop.si_signo, SIGCHLD);
        TST_EXP_EQ_LI(infop.si_code, CLD_EXITED);
...

And the output (with intentionaly wrong assertion for si_signo) looks like:

...
waitid05.c:34: TPASS: waitid(P_PGID, pid_group, &infop, WEXITED) passed
waitid05.c:36: TPASS: infop.si_pid == pid_child (18443)
waitid05.c:37: TPASS: infop.si_status == 0 (0)
waitid05.c:38: TFAIL: infop.si_signo (17) != SIGHUP (1)
waitid05.c:39: TPASS: infop.si_code == CLD_EXITED (1)
...

Signed-off-by: Cyril Hrubis <chrubis@suse.cz>
---
 include/tst_test_macros.h | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)

diff --git a/include/tst_test_macros.h b/include/tst_test_macros.h
index 8893dbf0e..33f03e22f 100644
--- a/include/tst_test_macros.h
+++ b/include/tst_test_macros.h
@@ -214,4 +214,29 @@ extern void *TST_RET_PTR;
 #define TST_EXP_EXPR(EXPR, FMT, ...)						\
 	tst_res_(__FILE__, __LINE__, (EXPR) ? TPASS : TFAIL, "Expect: " FMT, ##__VA_ARGS__);
 
+#define TST_EXP_EQ_(VAL_A, SVAL_A, VAL_B, SVAL_B, TYPE, PFS) do {\
+	TYPE tst_tmp_a__ = VAL_A; \
+	TYPE tst_tmp_b__ = VAL_B; \
+	if (tst_tmp_a__ == tst_tmp_b__) { \
+		tst_res_(__FILE__, __LINE__, TPASS, \
+			SVAL_A " == " SVAL_B " (" PFS ")", tst_tmp_a__); \
+	} else { \
+		tst_res_(__FILE__, __LINE__, TFAIL, \
+			SVAL_A " (" PFS ") != " SVAL_B " (" PFS ")", \
+			tst_tmp_a__, tst_tmp_b__); \
+	} \
+} while (0) \
+
+#define TST_EXP_EQ_LI(VAL_A, VAL_B) \
+		TST_EXP_EQ_(VAL_A, #VAL_A, VAL_B, #VAL_B, long long, "%lli")
+
+#define TST_EXP_EQ_LU(VAL_A, VAL_B) \
+		TST_EXP_EQ_(VAL_A, #VAL_A, VAL_B, #VAL_B, unsigned long long, "%llu")
+
+#define TST_EXP_EQ_SZ(VAL_A, VAL_B) \
+		TST_EXP_EQ_(VAL_A, #VAL_A, VAL_B, #VAL_B, size_t, "%zu")
+
+#define TST_EXP_EQ_SSZ(VAL_A, VAL_B) \
+		TST_EXP_EQ_(VAL_A, #VAL_A, VAL_B, #VAL_B, ssize_t, "%zi")
+
 #endif	/* TST_TEST_MACROS_H__ */
-- 
2.34.1



More information about the ltp mailing list