[LTP] [PATCH v3 09/12] Add TST_EXP_EQ_STRN macro

Andrea Cervesato andrea.cervesato@suse.de
Thu Jan 9 15:11:12 CET 2025


From: Andrea Cervesato <andrea.cervesato@suse.com>

New TST_EXP_EQ_STRN macro can be used to compare two strings, passing
their length if needed.

Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.com>
---
 include/tst_test_macros.h | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)

diff --git a/include/tst_test_macros.h b/include/tst_test_macros.h
index b2ca32f7710df33898092746140c57e5c6428525..dcfe5307df3e2bf2aed1e7e449f9799c65ee6d0d 100644
--- a/include/tst_test_macros.h
+++ b/include/tst_test_macros.h
@@ -425,4 +425,25 @@ const char *tst_errno_names(char *buf, const int *exp_errs, int exp_errs_cnt);
 	}                                                                      \
 } while (0)
 
+/**
+ * TST_EXP_EQ_STRN() - Compare two strings, providing length as well.
+ *
+ * @STR_A: string to compare.
+ * @STR_B: string to compare.
+ * @LEN: length of the string.
+ */
+#define TST_EXP_EQ_STRN(STR_A, STR_B, LEN) do {                                \
+	TST_PASS = strncmp(STR_A, STR_B, LEN) == 0;                            \
+                                                                               \
+	if (TST_PASS) {                                                        \
+		tst_res_(__FILE__, __LINE__, TPASS,                            \
+			"%s == %s (%s)",                                       \
+			#STR_A, #STR_B, STR_B);                                \
+	} else {                                                               \
+		tst_res_(__FILE__, __LINE__, TFAIL,                            \
+			"%s (%s) != %s (%s)",                                  \
+			#STR_A, STR_A, #STR_B, STR_B);                         \
+	}                                                                      \
+} while (0)
+
 #endif	/* TST_TEST_MACROS_H__ */

-- 
2.43.0



More information about the ltp mailing list