All of lore.kernel.org
 help / color / mirror / Atom feed
From: Tamir Duberstein <tamird@gmail.com>
To: David Gow <davidgow@google.com>, Petr Mladek <pmladek@suse.com>,
	 Steven Rostedt <rostedt@goodmis.org>,
	 Andy Shevchenko <andriy.shevchenko@linux.intel.com>,
	 Rasmus Villemoes <linux@rasmusvillemoes.dk>,
	 Sergey Senozhatsky <senozhatsky@chromium.org>,
	 Andrew Morton <akpm@linux-foundation.org>,
	Shuah Khan <shuah@kernel.org>
Cc: linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org,
	 Tamir Duberstein <tamird@gmail.com>
Subject: [PATCH 2/2] printf: break kunit into test cases
Date: Tue, 04 Feb 2025 14:36:38 -0500	[thread overview]
Message-ID: <20250204-printf-kunit-convert-v1-2-ecf1b846a4de@gmail.com> (raw)
In-Reply-To: <20250204-printf-kunit-convert-v1-0-ecf1b846a4de@gmail.com>

Use `suite_{init,exit}` and move all tests into `printf_test_cases`.
This gives us nicer output in the event of a failure.

Combine `plain_format` and `plain_hash` into `hash_pointer` since
they're testing the same scenario.

Signed-off-by: Tamir Duberstein <tamird@gmail.com>
---
 lib/printf_kunit.c | 196 ++++++++++++++++++-----------------------------------
 1 file changed, 65 insertions(+), 131 deletions(-)

diff --git a/lib/printf_kunit.c b/lib/printf_kunit.c
index e889aca69eba..e88b70ad3830 100644
--- a/lib/printf_kunit.c
+++ b/lib/printf_kunit.c
@@ -180,29 +180,6 @@ test_string(struct kunit *test)
 #define ZEROS "00000000"	/* hex 32 zero bits */
 #define ONES "ffffffff"		/* hex 32 one bits */
 
-static int
-plain_format(void)
-{
-	char buf[PLAIN_BUF_SIZE];
-	int nchars;
-
-	nchars = snprintf(buf, PLAIN_BUF_SIZE, "%p", PTR);
-
-	if (nchars != PTR_WIDTH)
-		return -1;
-
-	if (strncmp(buf, PTR_VAL_NO_CRNG, PTR_WIDTH) == 0) {
-		pr_warn("crng possibly not yet initialized. plain 'p' buffer contains \"%s\"",
-			PTR_VAL_NO_CRNG);
-		return 0;
-	}
-
-	if (strncmp(buf, ZEROS, strlen(ZEROS)) != 0)
-		return -1;
-
-	return 0;
-}
-
 #else
 
 #define PTR_WIDTH 8
@@ -212,79 +189,44 @@ plain_format(void)
 #define ZEROS ""
 #define ONES ""
 
-static int
-plain_format(void)
-{
-	/* Format is implicitly tested for 32 bit machines by plain_hash() */
-	return 0;
-}
-
 #endif	/* BITS_PER_LONG == 64 */
 
-static int
-plain_hash_to_buffer(const void *p, char *buf, size_t len)
+static void
+plain_hash_to_buffer(struct kunit *test, const void *p, char *buf, size_t len)
 {
-	int nchars;
-
-	nchars = snprintf(buf, len, "%p", p);
-
-	if (nchars != PTR_WIDTH)
-		return -1;
+	KUNIT_ASSERT_EQ(test, snprintf(buf, len, "%p", p), PTR_WIDTH);
 
 	if (strncmp(buf, PTR_VAL_NO_CRNG, PTR_WIDTH) == 0) {
 		pr_warn("crng possibly not yet initialized. plain 'p' buffer contains \"%s\"",
 			PTR_VAL_NO_CRNG);
-		return 0;
 	}
-
-	return 0;
 }
 
-static int
-plain_hash(void)
+static void
+hash_pointer(struct kunit *test)
 {
-	char buf[PLAIN_BUF_SIZE];
-	int ret;
+	if (no_hash_pointers)
+		kunit_skip(test, "hash pointers disabled");
 
-	ret = plain_hash_to_buffer(PTR, buf, PLAIN_BUF_SIZE);
-	if (ret)
-		return ret;
+	char buf[PLAIN_BUF_SIZE];
 
-	if (strncmp(buf, PTR_STR, PTR_WIDTH) == 0)
-		return -1;
+	plain_hash_to_buffer(test, PTR, buf, PLAIN_BUF_SIZE);
 
-	return 0;
-}
-
-/*
- * We can't use test() to test %p because we don't know what output to expect
- * after an address is hashed.
- */
-static void
-plain(struct kunit *test)
-{
-	if (no_hash_pointers) {
-		pr_warn("skipping plain 'p' tests");
-		return;
-	}
+	/*
+	 * We can't use test() to test %p because we don't know what output to expect
+	 * after an address is hashed.
+	 */
 
-	KUNIT_EXPECT_FALSE(test, plain_hash());
-	KUNIT_EXPECT_FALSE(test, plain_format());
+	KUNIT_EXPECT_MEMEQ(test, buf, ZEROS, strlen(ZEROS));
+	KUNIT_EXPECT_MEMNEQ(test, buf+strlen(ZEROS), PTR_STR, PTR_WIDTH);
 }
 
 static void
 test_hashed(struct kunit *test, const char *fmt, const void *p)
 {
 	char buf[PLAIN_BUF_SIZE];
-	int ret;
 
-	/*
-	 * No need to increase failed test counter since this is assumed
-	 * to be called after plain().
-	 */
-	ret = plain_hash_to_buffer(p, buf, PLAIN_BUF_SIZE);
-	if (ret)
-		return;
+	plain_hash_to_buffer(test, p, buf, PLAIN_BUF_SIZE);
 
 	tc(test, buf, fmt, p);
 }
@@ -322,12 +264,12 @@ invalid_pointer(struct kunit *test)
 }
 
 static void
-symbol_ptr(void)
+symbol_ptr(struct kunit *test)
 {
 }
 
 static void
-kernel_ptr(void)
+kernel_ptr(struct kunit *test)
 {
 	/* We can't test this without access to kptr_restrict. */
 }
@@ -398,12 +340,12 @@ struct_range(struct kunit *test)
 }
 
 static void
-addr(void)
+addr(struct kunit *test)
 {
 }
 
 static void
-escaped_str(void)
+escaped_str(struct kunit *test)
 {
 }
 
@@ -446,15 +388,8 @@ ip4(struct kunit *test)
 }
 
 static void
-ip6(void)
-{
-}
-
-static void
-ip(struct kunit *test)
+ip6(struct kunit *test)
 {
-	ip4(test);
-	ip6();
 }
 
 static void
@@ -506,7 +441,7 @@ dentry(struct kunit *test)
 }
 
 static void
-struct_va_format(void)
+struct_va_format(struct kunit *test)
 {
 }
 
@@ -545,7 +480,7 @@ time_and_date(struct kunit *test)
 }
 
 static void
-struct_clk(void)
+struct_clk(struct kunit *test)
 {
 }
 
@@ -587,7 +522,7 @@ bitmap(struct kunit *test)
 }
 
 static void
-netdev_features(void)
+netdev_features(struct kunit *test)
 {
 }
 
@@ -712,8 +647,7 @@ static void fwnode_pointer(struct kunit *test)
 
 	rval = software_node_register_node_group(group);
 	if (rval) {
-		pr_warn("cannot register softnodes; rval %d\n", rval);
-		return;
+		kunit_skip(test, "cannot register softnodes; rval %d\n", rval);
 	}
 
 	tc(test, full_name_second, "%pfw", software_node_fwnode(&second));
@@ -762,57 +696,57 @@ errptr(struct kunit *test)
 #endif
 }
 
-static void
-test_pointer(struct kunit *test)
-{
-	plain(test);
-	null_pointer(test);
-	error_pointer(test);
-	invalid_pointer(test);
-	symbol_ptr();
-	kernel_ptr();
-	struct_resource(test);
-	struct_range(test);
-	addr();
-	escaped_str();
-	hex_string(test);
-	mac(test);
-	ip(test);
-	uuid(test);
-	dentry(test);
-	struct_va_format();
-	time_and_date(test);
-	struct_clk();
-	bitmap(test);
-	netdev_features();
-	flags(test);
-	errptr(test);
-	fwnode_pointer(test);
-	fourcc_pointer(test);
-}
-
-static void printf_test(struct kunit *test)
+static struct kunit_case printf_test_cases[] = {
+	KUNIT_CASE(test_basic),
+	KUNIT_CASE(test_number),
+	KUNIT_CASE(test_string),
+	KUNIT_CASE(hash_pointer),
+	KUNIT_CASE(null_pointer),
+	KUNIT_CASE(error_pointer),
+	KUNIT_CASE(invalid_pointer),
+	KUNIT_CASE(symbol_ptr),
+	KUNIT_CASE(kernel_ptr),
+	KUNIT_CASE(struct_resource),
+	KUNIT_CASE(struct_range),
+	KUNIT_CASE(addr),
+	KUNIT_CASE(escaped_str),
+	KUNIT_CASE(hex_string),
+	KUNIT_CASE(mac),
+	KUNIT_CASE(ip4),
+	KUNIT_CASE(ip6),
+	KUNIT_CASE(uuid),
+	KUNIT_CASE(dentry),
+	KUNIT_CASE(struct_va_format),
+	KUNIT_CASE(time_and_date),
+	KUNIT_CASE(struct_clk),
+	KUNIT_CASE(bitmap),
+	KUNIT_CASE(netdev_features),
+	KUNIT_CASE(flags),
+	KUNIT_CASE(errptr),
+	KUNIT_CASE(fwnode_pointer),
+	KUNIT_CASE(fourcc_pointer),
+	{}
+};
+
+static int printf_suite_init(struct kunit_suite *suite)
 {
 	alloced_buffer = kmalloc(BUF_SIZE + 2*PAD_SIZE, GFP_KERNEL);
 	if (!alloced_buffer)
-		return;
+		return -1;
 	test_buffer = alloced_buffer + PAD_SIZE;
+	return 0;
+}
 
-	test_basic(test);
-	test_number(test);
-	test_string(test);
-	test_pointer(test);
-
+static void printf_suite_exit(struct kunit_suite *suite)
+{
 	kfree(alloced_buffer);
 }
 
-static struct kunit_case printf_test_cases[] = {
-	KUNIT_CASE(printf_test),
-	{}
-};
 
 static struct kunit_suite printf_test_suite = {
 	.name = "printf",
+	.suite_init = printf_suite_init,
+	.suite_exit = printf_suite_exit,
 	.test_cases = printf_test_cases,
 };
 

-- 
2.48.1


  parent reply	other threads:[~2025-02-04 19:36 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-02-04 19:36 [PATCH 0/2] printf: convert self-test to KUnit Tamir Duberstein
2025-02-04 19:36 ` [PATCH 1/2] " Tamir Duberstein
2025-02-05  9:32   ` Geert Uytterhoeven
2025-02-04 19:36 ` Tamir Duberstein [this message]
2025-02-06  9:26 ` [PATCH 0/2] " Rasmus Villemoes
2025-02-06 15:42   ` Tamir Duberstein
2025-02-07  7:55     ` David Gow
2025-02-07 10:01     ` Rasmus Villemoes
2025-02-07 11:27       ` Tamir Duberstein
2025-02-10 11:57         ` Rasmus Villemoes
2025-02-11  7:15           ` David Gow
2025-02-11  8:40             ` Rasmus Villemoes
2025-02-11 11:34               ` Tamir Duberstein
2025-02-11 19:21                 ` Tamir Duberstein
2025-02-12  9:25               ` David Gow

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=20250204-printf-kunit-convert-v1-2-ecf1b846a4de@gmail.com \
    --to=tamird@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=andriy.shevchenko@linux.intel.com \
    --cc=davidgow@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=linux@rasmusvillemoes.dk \
    --cc=pmladek@suse.com \
    --cc=rostedt@goodmis.org \
    --cc=senozhatsky@chromium.org \
    --cc=shuah@kernel.org \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.