public inbox for ltp@lists.linux.it
 help / color / mirror / Atom feed
From: Cyril Hrubis <chrubis@suse.cz>
To: ltp@lists.linux.it
Subject: [LTP] [PATCH 01/10] lib: Introduce more TEST_* macros
Date: Fri, 13 Nov 2020 14:14:19 +0100	[thread overview]
Message-ID: <20201113131428.13199-2-chrubis@suse.cz> (raw)
In-Reply-To: <20201113131428.13199-1-chrubis@suse.cz>

In order to simplify common return value checks.

Signed-off-by: Cyril Hrubis <chrubis@suse.cz>
---
 include/tst_test.h               |  28 +------
 include/tst_test_macros.h        | 131 +++++++++++++++++++++++++++++++
 lib/newlib_tests/.gitignore      |   3 +
 lib/newlib_tests/test_macros01.c |  40 ++++++++++
 lib/newlib_tests/test_macros02.c |  42 ++++++++++
 lib/newlib_tests/test_macros03.c |  40 ++++++++++
 lib/tst_test.c                   |   1 +
 7 files changed, 258 insertions(+), 27 deletions(-)
 create mode 100644 include/tst_test_macros.h
 create mode 100644 lib/newlib_tests/test_macros01.c
 create mode 100644 lib/newlib_tests/test_macros02.c
 create mode 100644 lib/newlib_tests/test_macros03.c

diff --git a/include/tst_test.h b/include/tst_test.h
index c91d3f18a..6d188a650 100644
--- a/include/tst_test.h
+++ b/include/tst_test.h
@@ -18,6 +18,7 @@
 
 #include "tst_common.h"
 #include "tst_res_flags.h"
+#include "tst_test_macros.h"
 #include "tst_checkpoint.h"
 #include "tst_device.h"
 #include "tst_mkfs.h"
@@ -269,33 +270,6 @@ void tst_run_tcases(int argc, char *argv[], struct tst_test *self)
  */
 void tst_reinit(void);
 
-//TODO Clean?
-#define TEST(SCALL) \
-	do { \
-		errno = 0; \
-		TST_RET = SCALL; \
-		TST_ERR = errno; \
-	} while (0)
-
-#define TEST_VOID(SCALL) \
-	do { \
-		errno = 0; \
-		SCALL; \
-		TST_ERR = errno; \
-	} while (0)
-
-extern long TST_RET;
-extern int TST_ERR;
-
-extern void *TST_RET_PTR;
-
-#define TESTPTR(SCALL) \
-	do { \
-		errno = 0; \
-		TST_RET_PTR = (void*)SCALL; \
-		TST_ERR = errno; \
-	} while (0)
-
 /*
  * Functions to convert ERRNO to its name and SIGNAL to its name.
  */
diff --git a/include/tst_test_macros.h b/include/tst_test_macros.h
new file mode 100644
index 000000000..69de2ce3d
--- /dev/null
+++ b/include/tst_test_macros.h
@@ -0,0 +1,131 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2015-2020 Cyril Hrubis <chrubis@suse.cz>
+ */
+
+#ifndef TST_TEST_MACROS_H__
+#define TST_TEST_MACROS_H__
+
+#define TEST(SCALL) \
+	do { \
+		errno = 0; \
+		TST_RET = SCALL; \
+		TST_ERR = errno; \
+	} while (0)
+
+#define TEST_VOID(SCALL) \
+	do { \
+		errno = 0; \
+		SCALL; \
+		TST_ERR = errno; \
+	} while (0)
+
+extern long TST_RET;
+extern int TST_ERR;
+extern int TST_PASS;
+
+extern void *TST_RET_PTR;
+
+#define TESTPTR(SCALL) \
+	do { \
+		errno = 0; \
+		TST_RET_PTR = (void*)SCALL; \
+		TST_ERR = errno; \
+	} while (0)
+
+
+#define TEST_2(_1, _2, ...) _2
+
+#define TEST_FMT_(FMT, _1, ...) FMT, ##__VA_ARGS__
+
+#define TEST_MSG(RES, FMT, SCALL, ...) \
+	tst_res_(__FILE__, __LINE__, RES, \
+		TEST_FMT_(TEST_2(dummy, ##__VA_ARGS__, SCALL) FMT, __VA_ARGS__))
+
+#define TEST_MSGP(RES, FMT, PAR, SCALL, ...) \
+	tst_res_(__FILE__, __LINE__, RES, \
+		TEST_FMT_(TEST_2(dummy, ##__VA_ARGS__, SCALL) FMT, __VA_ARGS__), PAR)
+
+#define TEST_FD(SCALL, ...)                                                    \
+	do {                                                                   \
+		TEST(SCALL);                                                   \
+		                                                               \
+		TST_PASS = 0;                                                  \
+		                                                               \
+		if (TST_RET == -1) {                                           \
+			TEST_MSG(TFAIL | TTERRNO, " failed",                   \
+			         #SCALL, ##__VA_ARGS__);                       \
+		        break;                                                 \
+		}                                                              \
+		                                                               \
+		if (TST_RET < 0) {                                             \
+			TEST_MSGP(TFAIL | TTERRNO, " invalid retval %ld",      \
+				  TST_RET, #SCALL, ##__VA_ARGS__);             \
+			break;                                                 \
+		}                                                              \
+                                                                               \
+		TEST_MSGP(TPASS, " returned fd %ld", TST_RET,                  \
+		          #SCALL, ##__VA_ARGS__);                              \
+                                                                               \
+                TST_PASS = 1;                                                  \
+                                                                               \
+	} while (0)
+
+#define TEST_PASS(SCALL, ...)                                                  \
+	do {                                                                   \
+		TEST(SCALL);                                                   \
+		                                                               \
+		TST_PASS = 0;                                                  \
+		                                                               \
+		if (TST_RET == -1) {                                           \
+			TEST_MSG(TFAIL | TTERRNO, " failed",                   \
+			         #SCALL, ##__VA_ARGS__);                       \
+		        break;                                                 \
+		}                                                              \
+		                                                               \
+		if (TST_RET != 0) {                                            \
+			TEST_MSGP(TFAIL | TTERRNO, " invalid retval %ld",      \
+                                  TST_RET, #SCALL, ##__VA_ARGS__);             \
+			break;                                                 \
+		}                                                              \
+                                                                               \
+		TEST_MSG(TPASS, " passed", #SCALL, ##__VA_ARGS__);             \
+                                                                               \
+                TST_PASS = 1;                                                  \
+                                                                               \
+	} while (0)
+
+
+#define TEST_FAIL(SCALL, ERRNO, ...)                                           \
+	do {                                                                   \
+		TEST(SCALL);                                                   \
+		                                                               \
+		TST_PASS = 0;                                                  \
+		                                                               \
+		if (TST_RET == 0) {                                            \
+			TEST_MSG(TFAIL | TTERRNO, " succeeded",                \
+			         #SCALL, ##__VA_ARGS__);                       \
+		        break;                                                 \
+		}                                                              \
+		                                                               \
+		if (TST_RET != -1) {                                           \
+			TEST_MSGP(TFAIL | TTERRNO, " invalid retval %ld",      \
+                                  TST_RET, #SCALL, ##__VA_ARGS__);             \
+			break;                                                 \
+		}                                                              \
+		                                                               \
+		if (ERRNO) {                                                   \
+			if (TST_ERR == ERRNO) {                                \
+				TEST_MSG(TPASS | TERRNO, "",                   \
+				         #SCALL, ##__VA_ARGS__);               \
+				TST_PASS = 1;                                  \
+			} else {                                               \
+				TEST_MSGP(TFAIL | TERRNO, " expected %s",      \
+				          tst_strerrno(ERRNO),                 \
+				          #SCALL, ##__VA_ARGS__);              \
+			}                                                      \
+		}                                                              \
+	} while (0)
+
+
+#endif	/* TST_TEST_MACROS_H__ */
diff --git a/lib/newlib_tests/.gitignore b/lib/newlib_tests/.gitignore
index ac1d19be0..a5c652c99 100644
--- a/lib/newlib_tests/.gitignore
+++ b/lib/newlib_tests/.gitignore
@@ -36,3 +36,6 @@ test_kconfig02
 variant
 test_guarded_buf
 tst_bool_expr
+test_macros01
+test_macros02
+test_macros03
diff --git a/lib/newlib_tests/test_macros01.c b/lib/newlib_tests/test_macros01.c
new file mode 100644
index 000000000..8b315b30f
--- /dev/null
+++ b/lib/newlib_tests/test_macros01.c
@@ -0,0 +1,40 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2020 Cyril Hrubis <chrubis@suse.cz>
+ */
+
+/*
+ * Test macros.
+ */
+
+#include "tst_test.h"
+
+static int fail_fd(void)
+{
+	errno = EINVAL;
+	return -1;
+}
+
+static int pass_fd(void)
+{
+	return 42;
+}
+
+static int inval_val(void)
+{
+	return -42;
+}
+
+static void do_test(void)
+{
+	TEST_FD(fail_fd(), "TEST DESCRIPTION");
+	tst_res(TINFO, "TST_PASS = %i", TST_PASS);
+	TEST_FD(pass_fd(), "%s", "TEST DESCRIPTION PARAM");
+	tst_res(TINFO, "TST_PASS = %i", TST_PASS);
+	TEST_FD(inval_val());
+	tst_res(TINFO, "TST_PASS = %i", TST_PASS);
+}
+
+static struct tst_test test = {
+	.test_all = do_test,
+};
diff --git a/lib/newlib_tests/test_macros02.c b/lib/newlib_tests/test_macros02.c
new file mode 100644
index 000000000..41b685a36
--- /dev/null
+++ b/lib/newlib_tests/test_macros02.c
@@ -0,0 +1,42 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2020 Cyril Hrubis <chrubis@suse.cz>
+ */
+
+/*
+ * Test macros.
+ */
+
+#include "tst_test.h"
+
+static int fail_fn(void)
+{
+	errno = EINVAL;
+	return -1;
+}
+
+static int pass_fn(void)
+{
+	return 0;
+}
+
+static int inval_ret_fn(void)
+{
+	return 42;
+}
+
+static void do_test(void)
+{
+	TEST_FAIL(fail_fn(), EINVAL);
+	tst_res(TINFO, "TST_PASS = %i", TST_PASS);
+	TEST_FAIL(fail_fn(), ENOTTY);
+	tst_res(TINFO, "TST_PASS = %i", TST_PASS);
+	TEST_FAIL(pass_fn(), ENOTTY);
+	tst_res(TINFO, "TST_PASS = %i", TST_PASS);
+	TEST_FAIL(inval_ret_fn(), ENOTTY, "TEST DESCRIPTION");
+	tst_res(TINFO, "TST_PASS = %i", TST_PASS);
+}
+
+static struct tst_test test = {
+	.test_all = do_test,
+};
diff --git a/lib/newlib_tests/test_macros03.c b/lib/newlib_tests/test_macros03.c
new file mode 100644
index 000000000..731715308
--- /dev/null
+++ b/lib/newlib_tests/test_macros03.c
@@ -0,0 +1,40 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2020 Cyril Hrubis <chrubis@suse.cz>
+ */
+
+/*
+ * Test macros.
+ */
+
+#include "tst_test.h"
+
+static int fail_fn(void)
+{
+	errno = EINVAL;
+	return -1;
+}
+
+static int pass_fn(void)
+{
+	return 0;
+}
+
+static int inval_ret_fn(void)
+{
+	return 42;
+}
+
+static void do_test(void)
+{
+	TEST_PASS(fail_fn());
+	tst_res(TINFO, "TST_PASS = %i", TST_PASS);
+	TEST_PASS(pass_fn(), "TEST DESCRIPTION");
+	tst_res(TINFO, "TST_PASS = %i", TST_PASS);
+	TEST_PASS(inval_ret_fn());
+	tst_res(TINFO, "TST_PASS = %i", TST_PASS);
+}
+
+static struct tst_test test = {
+	.test_all = do_test,
+};
diff --git a/lib/tst_test.c b/lib/tst_test.c
index 535c0ff4c..6700a4eef 100644
--- a/lib/tst_test.c
+++ b/lib/tst_test.c
@@ -74,6 +74,7 @@ const char *tst_ipc_path = ipc_path;
 static char shm_path[1024];
 
 int TST_ERR;
+int TST_PASS;
 long TST_RET;
 
 static void do_cleanup(void);
-- 
2.26.2


  reply	other threads:[~2020-11-13 13:14 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-11-13 13:14 [LTP] [PATCH 00/10] Introduce TEST_MACROS Cyril Hrubis
2020-11-13 13:14 ` Cyril Hrubis [this message]
2020-11-13 20:28   ` [LTP] [PATCH 01/10] lib: Introduce more TEST_* macros Petr Vorel
2020-11-16  8:41     ` Li Wang
2020-11-19 13:08       ` Cyril Hrubis
2020-11-25 13:18         ` Cyril Hrubis
2020-11-25 13:33           ` Li Wang
2020-11-25 15:57             ` Cyril Hrubis
2020-11-26  2:17               ` Li Wang
2020-11-19 13:09     ` Cyril Hrubis
2020-11-25 16:54   ` Martin Doucha
2020-12-03 12:30     ` Cyril Hrubis
2020-12-03 14:34       ` Martin Doucha
2020-11-13 13:14 ` [LTP] [PATCH 02/10] syscalls/uname: Make use of TEST_MACROS Cyril Hrubis
2020-11-13 13:14 ` [LTP] [PATCH 03/10] syscalls/accept: " Cyril Hrubis
2020-11-13 13:14 ` [LTP] [PATCH 04/10] syscalls/access: " Cyril Hrubis
2020-11-13 13:14 ` [LTP] [PATCH 05/10] syscalls/bind: " Cyril Hrubis
2020-11-13 13:14 ` [LTP] [PATCH 06/10] syscalls/brk01: " Cyril Hrubis
2020-11-13 13:14 ` [LTP] [PATCH 07/10] syscalls/cacheflush: " Cyril Hrubis
2020-11-13 13:14 ` [LTP] [PATCH 08/10] syscalls/capget: " Cyril Hrubis
2020-11-13 13:14 ` [LTP] [PATCH 09/10] syscalls/capset: " Cyril Hrubis
2020-11-13 13:14 ` [LTP] [PATCH 10/10] syscalls/open: " Cyril Hrubis

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20201113131428.13199-2-chrubis@suse.cz \
    --to=chrubis@suse.cz \
    --cc=ltp@lists.linux.it \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox