[LTP] [PATCH v2 2/2] test_macros: Add TST_EXP_PASS_OR_FAIL()

Petr Vorel pvorel@suse.cz
Fri Jul 10 15:03:03 CEST 2026


This allows instead of:

	if (err) {
		TST_EXP_FAIL(epoll_ctl(efd, EPOLL_CTL_ADD,
				 fd.fd, &ev), err,
				 "epoll_ctl() on %s", tst_fd_desc(&fd));
	} else {
		TST_EXP_PASS(epoll_ctl(efd, EPOLL_CTL_ADD,
				 fd.fd, &ev),
				 "epoll_ctl() on %s", tst_fd_desc(&fd));
	}

to simplify to just:
	TST_EXP_PASS_OR_FAIL(epoll_ctl(efd, EPOLL_CTL_ADD,
			 fd.fd, &ev), err,
			 "epoll_ctl() on %s", tst_fd_desc(&fd));

That required to add TST_EXP_PASS_().

Add test into test_macros03.c.

+ Fix macro parameter name in TST_EXP_FD_OR_FAIL() (upper case).

Signed-off-by: Petr Vorel <pvorel@suse.cz>
---
Changes in v2:
* Use underscore variant TST_EXP_PASS_ and TST_EXP_FAIL_() (Cyril)
* Add a test (can be done in a separate commit)

Link to v1:
https://lore.kernel.org/ltp/20260710083337.1185184-1-pvorel@suse.cz/

 include/tst_test_macros.h        | 31 ++++++++++++++++++++++++++++++-
 lib/newlib_tests/test_macros03.c |  9 ++++++++-
 2 files changed, 38 insertions(+), 2 deletions(-)

diff --git a/include/tst_test_macros.h b/include/tst_test_macros.h
index e18f1d33f3..3932687904 100644
--- a/include/tst_test_macros.h
+++ b/include/tst_test_macros.h
@@ -177,7 +177,7 @@ extern int TST_PASS;
  * @ERRNO: Expected errno or 0.
  * @...: A printf-like parameters.
  *
- * Expect a file descriptor if errno is 0 otherwise expect a failure with
+ * Expect a file descriptor if ERRNO is 0 otherwise expect a failure with
  * expected errno.
  *
  * Internally it uses TST_EXP_FAIL() and TST_EXP_FD().
@@ -350,6 +350,9 @@ extern int TST_PASS;
  * is converted to a string and used instead.
  */
 #define TST_EXP_PASS(SCALL, ...)                                               \
+	TST_EXP_PASS_(SCALL, #SCALL, ##__VA_ARGS__)
+
+#define TST_EXP_PASS_(SCALL, SSCALL, ...)                                      \
 	do {                                                                   \
 		TST_EXP_PASS_SILENT_(SCALL, #SCALL, ##__VA_ARGS__);            \
 		                                                               \
@@ -366,6 +369,32 @@ extern int TST_PASS;
 			TST_MSG_(TPASS, " passed", #SCALL, ##__VA_ARGS__);     \
 	} while (0)
 
+/**
+ * TST_EXP_PASS_OR_FAIL() - Test syscall and expect it to pass or fail with
+ * expected errno.
+ *
+ * @SCALL: Tested syscall.
+ * @ERRNO: Expected errno or 0.
+ * @...: A printf-like parameters.
+ *
+ * Expect to pass if ERRNO is 0 otherwise expect a failure with
+ * expected errno.
+ *
+ * Internally it uses TST_EXP_FAIL() and TST_EXP_PASS().
+ */
+#define TST_EXP_PASS_OR_FAIL(SCALL, ERRNO, ...)                               \
+	TST_EXP_PASS_OR_FAIL_(SCALL, #SCALL, ERRNO, ##__VA_ARGS__)
+
+#define TST_EXP_PASS_OR_FAIL_(SCALL, SSCALL, ERRNO, ...)                      \
+	({                                                                     \
+		if (ERRNO)                                                     \
+			TST_EXP_FAIL_(SCALL, SSCALL, ERRNO, ##__VA_ARGS__);    \
+		else                                                           \
+			TST_EXP_PASS_(SCALL, SSCALL, ##__VA_ARGS__);           \
+		                                                               \
+		TST_RET;                                                       \
+	})
+
 /**
  * TST_EXP_PASS_PTR_VOID() - Test syscall to return a valid pointer.
  *
diff --git a/lib/newlib_tests/test_macros03.c b/lib/newlib_tests/test_macros03.c
index 19a0ad6fd3..2a281bafcd 100644
--- a/lib/newlib_tests/test_macros03.c
+++ b/lib/newlib_tests/test_macros03.c
@@ -9,9 +9,11 @@
 
 #include "tst_test.h"
 
+#define ERR_ERRNO EINVAL
+
 static int fail_fn(void)
 {
-	errno = EINVAL;
+	errno = ERR_ERRNO;
 	return -1;
 }
 
@@ -42,6 +44,11 @@ static void do_test(void)
 	tst_res(TINFO, "TST_PASS = %i from TST_EXP_PASS_SILENT(pass_fn, ...)", TST_PASS);
 	TST_EXP_PASS_SILENT(inval_ret_fn(), "inval_ret_fn()");
 	tst_res(TINFO, "TST_PASS = %i", TST_PASS);
+
+	tst_res(TINFO, "Testing TST_EXP_PASS_OR_FAIL() macro (pass)");
+	TST_EXP_PASS_OR_FAIL(pass_fn(), 0, "pass_fn()");
+	tst_res(TINFO, "Testing TST_EXP_PASS_OR_FAIL() macro (fail)");
+	TST_EXP_PASS_OR_FAIL(fail_fn(), ERR_ERRNO, "fail_fn()");
 }
 
 static struct tst_test test = {
-- 
2.54.0



More information about the ltp mailing list