[PATCH v3 2/4] test_macros: Add TST_EXP_PASS_OR_FAIL()

Petr Vorel <[email protected]>
Newsgroups gmane.linux.ltp
Message-ID <[email protected]>
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 <[email protected]>
---
Please double check if TST_MSGP_() and TST_EXP_PASS_PTR_() in
TST_EXP_PASS_() should really use SSCALL instead of #SCALL.

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

diff --git a/include/tst_test_macros.h b/include/tst_test_macros.h
index 65ec873444..12bd9d47ed 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().
@@ -347,11 +347,14 @@ 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__);            \
+		TST_EXP_PASS_SILENT_(SCALL, SSCALL, ##__VA_ARGS__);            \
 		                                                               \
 		if (TST_PASS)                                                  \
-			TST_MSG_(TPASS, " passed", #SCALL, ##__VA_ARGS__);     \
+			TST_MSG_(TPASS, " passed", SSCALL, ##__VA_ARGS__);     \
 	} while (0)                                                            \
 
 #define TST_EXP_PASS_PTR_(SCALL, SSCALL, FAIL_PTR_VAL, ...)                    \
@@ -360,9 +363,32 @@ extern int TST_PASS;
 					FAIL_PTR_VAL, ##__VA_ARGS__);          \
 		                                                               \
 		if (TST_PASS)                                                  \
-			TST_MSG_(TPASS, " passed", #SCALL, ##__VA_ARGS__);     \
+			TST_MSG_(TPASS, " passed", SSCALL, ##__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, ...)                               \
+	({                                                                     \
+		if (ERRNO)                                                     \
+			TST_EXP_FAIL_(SCALL, #SCALL, ERRNO, ##__VA_ARGS__);    \
+		else                                                           \
+			TST_EXP_PASS_(SCALL, #SCALL, ##__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.55.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.