Skip to contentSkip to conversation

tst_test_macros: Avoid re-evaluating arguments in TST_EXP_{EQ, LE}_*()

PATCH New Wake Liu via ltp 1 participant 1 message Nov 13

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tst_test_macros: Avoid re-evaluating arguments in TST_EXP_{EQ, LE}_*()

Commit 1759ad9f3f0b ("Add silent macros for TST_EXP_EQ_*") split the TPASS message out of TST_EXP_EQ_SILENT_() and printed VAL_A directly in TST_EXP_EQ_LI(), TST_EXP_EQ_LU(), TST_EXP_EQ_SZ() and TST_EXP_EQ_SSZ() because tst_tmp_a__ was no longer in scope. As a result, VAL_A is evaluated a second time whenever the check passes. Commit 1b0395338108 ("tst_test_macros: Add TST_EXP_LE_LU*()") followed the same pattern and evaluates both VAL_A and VAL_B a second time on TPASS.

When an argument has side effects, the test executes it twice on success. For example, stream04 calls:

  TST_EXP_EQ_LI(fwrite(data, 1, DATASIZE, stream), DATASIZE);

which writes DATASIZE (27) bytes twice into the file, and epoll_wait06 calls:

  TST_EXP_EQ_LI(SAFE_EPOLL_WAIT(epfd, &evt_receive, 1, 0), 1);

where the first call consumes the edge-triggered event and the second call in the TPASS message returns 0, printing:

TPASS: SAFE_EPOLL_WAIT(epfd, &evt_receive, 1, 0) == 1 (0)

The first commit also passed size_t instead of ssize_t to TST_EXP_EQ_SILENT_() in TST_EXP_EQ_SSZ().

Pass a SILENT parameter to TST_EXP_EQ_() and TST_EXP_LE_() so the TPASS message is emitted inside the same block using tst_tmp_a__ and tst_tmp_b__, and fix the type in TST_EXP_EQ_SSZ() to ssize_t.

Fixes 1759ad9f3f0b ("Add silent macros for TST_EXP_EQ_*")
Fixes 1b0395338108 ("tst_test_macros: Add TST_EXP_LE_LU*()")
---
 include/tst_test_macros.h | 89 +++++++++++++--------------------------
 1 file changed, 29 insertions(+), 60 deletions(-)
include/tst_test_macros.h +29 -60
index fcecf7f82..53f6c1e80 100644
@@ -759,33 +759,37 @@ const char *tst_errno_names(char *buf, const int *exp_errs, int exp_errs_cnt);
        tst_res_(__FILE__, __LINE__, (EXPR) ? TPASS : TFAIL, "Expect: "       \
                 TST_FMT_(TST_2_(dummy, ##__VA_ARGS__, #EXPR), __VA_ARGS__));
 
-#define TST_EXP_EQ_SILENT_(VAL_A, SVAL_A, VAL_B, SVAL_B, TYPE, PFS) do {       \
+#define TST_EXP_EQ_(VAL_A, SVAL_A, VAL_B, SVAL_B, TYPE, PFS, SILENT) do {      \
 	TYPE tst_tmp_a__ = VAL_A;                                              \
 	TYPE tst_tmp_b__ = VAL_B;                                              \
                                                                                \
-	TST_PASS = 0;                                                          \
+	TST_PASS = tst_tmp_a__ == tst_tmp_b__;                                 \
                                                                                \
-	if (tst_tmp_a__ != tst_tmp_b__) {                                      \
+	if (!TST_PASS) {                                                       \
 		tst_res_(__FILE__, __LINE__, TFAIL,                            \
 			SVAL_A " (" PFS ") != " SVAL_B " (" PFS ")",           \
 			tst_tmp_a__, tst_tmp_b__);                             \
-	} else {                                                               \
-		TST_PASS = 1;                                                  \
+	} else if (!(SILENT)) {                                                \
+		tst_res_(__FILE__, __LINE__, TPASS,                            \
+			SVAL_A " == " SVAL_B " (" PFS ")",                     \
+			tst_tmp_a__);                                          \
 	}                                                                      \
 } while (0)
 
-#define TST_EXP_LE_SILENT_(VAL_A, SVAL_A, VAL_B, SVAL_B, TYPE, PFS) do {       \
+#define TST_EXP_LE_(VAL_A, SVAL_A, VAL_B, SVAL_B, TYPE, PFS, SILENT) do {      \
 	TYPE tst_tmp_a__ = VAL_A;                                              \
 	TYPE tst_tmp_b__ = VAL_B;                                              \
                                                                                \
-	TST_PASS = 0;                                                          \
+	TST_PASS = tst_tmp_a__ <= tst_tmp_b__;                                 \
                                                                                \
-	if (tst_tmp_a__ > tst_tmp_b__) {                                       \
+	if (!TST_PASS) {                                                       \
 		tst_res_(__FILE__, __LINE__, TFAIL,                            \
 			SVAL_A " (" PFS ") > " SVAL_B " (" PFS ")",            \
 			tst_tmp_a__, tst_tmp_b__);                             \
-	} else {                                                               \
-		TST_PASS = 1;                                                  \
+	} else if (!(SILENT)) {                                                \
+		tst_res_(__FILE__, __LINE__, TPASS,                            \
+			SVAL_A " <= " SVAL_B " (" PFS " <= " PFS ")",          \
+			tst_tmp_a__, tst_tmp_b__);                             \
 	}                                                                      \
 } while (0)
 
@@ -797,15 +801,8 @@ const char *tst_errno_names(char *buf, const int *exp_errs, int exp_errs_cnt);
  *
  * Reports a pass if A <= B and a fail otherwise.
  */
-#define TST_EXP_LE_LU(VAL_A, VAL_B) do {                                              \
-	TST_EXP_LE_SILENT_(VAL_A, #VAL_A, VAL_B, #VAL_B, unsigned long long, "%llu"); \
-									              \
-	if (TST_PASS) {                                                               \
-		tst_res_(__FILE__, __LINE__, TPASS,                                   \
-			#VAL_A " <= " #VAL_B " (%llu <= %llu)",                       \
-			(unsigned long long)VAL_A, (unsigned long long)VAL_B);        \
-	}                                                                             \
-} while (0)
+#define TST_EXP_LE_LU(VAL_A, VAL_B) \
+	TST_EXP_LE_(VAL_A, #VAL_A, VAL_B, #VAL_B, unsigned long long, "%llu", 0)
 
 /**
  * TST_EXP_LE_LU_SILENT() - Compare two unsigned long long values, silent variant.
@@ -817,7 +814,7 @@ const char *tst_errno_names(char *buf, const int *exp_errs, int exp_errs_cnt);
  * success, only prints :c:enum:`TFAIL <tst_res_flags>` on failure.
  */
 #define TST_EXP_LE_LU_SILENT(VAL_A, VAL_B) \
-	TST_EXP_LE_SILENT_(VAL_A, #VAL_A, VAL_B, #VAL_B, unsigned long long, "%llu")
+	TST_EXP_LE_(VAL_A, #VAL_A, VAL_B, #VAL_B, unsigned long long, "%llu", 1)
 
 /**
  * TST_EXP_EQ_LI() - Compare two long long values.
@@ -827,15 +824,8 @@ const char *tst_errno_names(char *buf, const int *exp_errs, int exp_errs_cnt);
  *
  * Reports a pass if values are equal and a fail otherwise.
  */
-#define TST_EXP_EQ_LI(VAL_A, VAL_B) do {                                       \
-	TST_EXP_EQ_SILENT_(VAL_A, #VAL_A, VAL_B, #VAL_B, long long, "%lli");   \
-								               \
-	if (TST_PASS) {                                                        \
-		tst_res_(__FILE__, __LINE__, TPASS,                            \
-			#VAL_A " == " #VAL_B " (%lli)",                        \
-			(long long)VAL_A);                                     \
-	}                                                                      \
-} 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", 0)
 
 /**
  * TST_EXP_EQ_LI_SILENT() - Compare two long long values, silent variant.
@@ -847,7 +837,7 @@ const char *tst_errno_names(char *buf, const int *exp_errs, int exp_errs_cnt);
  * success, only prints :c:enum:`TFAIL <tst_res_flags>` on failure.
  */
 #define TST_EXP_EQ_LI_SILENT(VAL_A, VAL_B) \
-	TST_EXP_EQ_SILENT_(VAL_A, #VAL_A, VAL_B, #VAL_B, long long, "%lli")
+	TST_EXP_EQ_(VAL_A, #VAL_A, VAL_B, #VAL_B, long long, "%lli", 1)
 
 /**
  * TST_EXP_EQ_LU() - Compare two unsigned long long values.
@@ -857,15 +847,8 @@ const char *tst_errno_names(char *buf, const int *exp_errs, int exp_errs_cnt);
  *
  * Reports a pass if values are equal and a fail otherwise.
  */
-#define TST_EXP_EQ_LU(VAL_A, VAL_B) do {                                       \
-	TST_EXP_EQ_SILENT_(VAL_A, #VAL_A, VAL_B, #VAL_B, unsigned long long, "%llu"); \
-								               \
-	if (TST_PASS) {                                                        \
-		tst_res_(__FILE__, __LINE__, TPASS,                            \
-			#VAL_A " == " #VAL_B " (%llu)",                        \
-			(unsigned long long)VAL_A);                            \
-	}                                                                      \
-} while (0)
+#define TST_EXP_EQ_LU(VAL_A, VAL_B) \
+	TST_EXP_EQ_(VAL_A, #VAL_A, VAL_B, #VAL_B, unsigned long long, "%llu", 0)
 
 /**
  * TST_EXP_EQ_LU_SILENT() - Compare two unsigned long long values, silent variant.
@@ -877,7 +860,7 @@ const char *tst_errno_names(char *buf, const int *exp_errs, int exp_errs_cnt);
  * success, only prints :c:enum:`TFAIL <tst_res_flags>` on failure.
  */
 #define TST_EXP_EQ_LU_SILENT(VAL_A, VAL_B) \
-	TST_EXP_EQ_SILENT_(VAL_A, #VAL_A, VAL_B, #VAL_B, unsigned long long, "%llu")
+	TST_EXP_EQ_(VAL_A, #VAL_A, VAL_B, #VAL_B, unsigned long long, "%llu", 1)
 
 /**
  * TST_EXP_EQ_SZ() - Compare two size_t values.
@@ -887,15 +870,8 @@ const char *tst_errno_names(char *buf, const int *exp_errs, int exp_errs_cnt);
  *
  * Reports a pass if values are equal and a fail otherwise.
  */
-#define TST_EXP_EQ_SZ(VAL_A, VAL_B) do {                                       \
-	TST_EXP_EQ_SILENT_(VAL_A, #VAL_A, VAL_B, #VAL_B, size_t, "%zu");       \
-								               \
-	if (TST_PASS) {                                                        \
-		tst_res_(__FILE__, __LINE__, TPASS,                            \
-			#VAL_A " == " #VAL_B " (%zu)",                         \
-			(size_t)VAL_A);                                        \
-	}                                                                      \
-} while (0)
+#define TST_EXP_EQ_SZ(VAL_A, VAL_B) \
+	TST_EXP_EQ_(VAL_A, #VAL_A, VAL_B, #VAL_B, size_t, "%zu", 0)
 
 /**
  * TST_EXP_EQ_SZ_SILENT() - Compare two size_t values, silent variant.
@@ -907,7 +883,7 @@ const char *tst_errno_names(char *buf, const int *exp_errs, int exp_errs_cnt);
  * success, only prints :c:enum:`TFAIL <tst_res_flags>` on failure.
  */
 #define TST_EXP_EQ_SZ_SILENT(VAL_A, VAL_B) \
-	TST_EXP_EQ_SILENT_(VAL_A, #VAL_A, VAL_B, #VAL_B, size_t, "%zu")
+	TST_EXP_EQ_(VAL_A, #VAL_A, VAL_B, #VAL_B, size_t, "%zu", 1)
 
 /**
  * TST_EXP_EQ_SSZ() - Compare two ssize_t values.
@@ -917,15 +893,8 @@ const char *tst_errno_names(char *buf, const int *exp_errs, int exp_errs_cnt);
  *
  * Reports a pass if values are equal and a fail otherwise.
  */
-#define TST_EXP_EQ_SSZ(VAL_A, VAL_B) do {                                      \
-	TST_EXP_EQ_SILENT_(VAL_A, #VAL_A, VAL_B, #VAL_B, size_t, "%zi");       \
-								               \
-	if (TST_PASS) {                                                        \
-		tst_res_(__FILE__, __LINE__, TPASS,                            \
-			#VAL_A " == " #VAL_B " (%zi)",                         \
-			(ssize_t)VAL_A);                                        \
-	}                                                                      \
-} while (0)
+#define TST_EXP_EQ_SSZ(VAL_A, VAL_B) \
+	TST_EXP_EQ_(VAL_A, #VAL_A, VAL_B, #VAL_B, ssize_t, "%zi", 0)
 
 /**
  * TST_EXP_EQ_SSZ_SILENT() - Compare two ssize_t values, silent variant.
@@ -937,7 +906,7 @@ const char *tst_errno_names(char *buf, const int *exp_errs, int exp_errs_cnt);
  * success, only prints :c:enum:`TFAIL <tst_res_flags>` on failure.
  */
 #define TST_EXP_EQ_SSZ_SILENT(VAL_A, VAL_B) \
-	TST_EXP_EQ_SILENT_(VAL_A, #VAL_A, VAL_B, #VAL_B, ssize_t, "%zi")
+	TST_EXP_EQ_(VAL_A, #VAL_A, VAL_B, #VAL_B, ssize_t, "%zi", 1)
 
 /**
  * TST_EXP_EQ_STR() - Compare two strings.

base-commit: 5f4d6908fa7d48842f151b18ed4d8a9a76258cd5

Signature · 5 lines
2.56.0.611.g1030f0b940-goog


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp