public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: David Laight <david.laight.linux@gmail.com>
To: linux-kernel@vger.kernel.org, Andrew Morton <akpm@linux-foundation.org>
Cc: David Laight <david.laight.linux@gmail.com>,
	Arnd Bergmann <arnd@arndb.de>,
	Linus Torvalds <torvalds@linux-foundation.org>,
	Christophe Leroy <christophe.leroy@c-s.fr>,
	Andy Shevchenko <andriy.shevchenko@linux.intel.com>,
	Rasmus Villemoes <linux@rasmusvillemoes.dk>,
	nnac123@linux.ibm.com, horms@kernel.org
Subject: [PATCH next 3/8] test_hexdump: Use longer variable names
Date: Sat,  8 Mar 2025 09:34:47 +0000	[thread overview]
Message-ID: <20250308093452.3742-4-david.laight.linux@gmail.com> (raw)
In-Reply-To: <20250308093452.3742-1-david.laight.linux@gmail.com>

We're not so short of memory space for the compiler symbol table
that variable names have to be limited to 2 characters.
Use groupsize and rowsize (not gs and rs).
There is also no need to copy the function parameters to locals.
Change test_hexdump_prepare_test() (the function that gernerate the
sample output) to return the length - saves calculating it separately.

Test reports 'all 1184 tests passed'.

Signed-off-by: David Laight <david.laight.linux@gmail.com>
---
 lib/test_hexdump.c | 81 +++++++++++++++++++++-------------------------
 1 file changed, 36 insertions(+), 45 deletions(-)

diff --git a/lib/test_hexdump.c b/lib/test_hexdump.c
index e142b11c36c6..743ea5c78f9e 100644
--- a/lib/test_hexdump.c
+++ b/lib/test_hexdump.c
@@ -22,32 +22,30 @@ static const unsigned char data_b[] = {
 static unsigned total_tests __initdata;
 static unsigned failed_tests __initdata;
 
-static void __init test_hexdump_prepare_test(size_t len, size_t rowsize,
-					     size_t groupsize, char *test,
-					     size_t testlen, bool ascii)
+static size_t __init test_hexdump_prepare_test(size_t len, size_t rowsize,
+					       size_t groupsize, char *test,
+					       size_t testlen, bool ascii)
 {
 	char *p;
-	size_t l = len;
-	size_t gs = groupsize, rs = rowsize;
-	size_t bs, i, j;
+	size_t byteswap, i, j;
 
-	if (rs != 16 && rs != 32)
-		rs = 16;
+	if (rowsize != 16 && rowsize != 32)
+		rowsize = 16;
 
-	if (l > rs)
-		l = rs;
+	if (len > rowsize)
+		len = rowsize;
 
-	if (!is_power_of_2(gs) || gs > 8 || (len % gs != 0))
-		gs = 1;
-	bs = IS_ENABLED(CONFIG_CPU_BIG_ENDIAN) ? 0 : gs - 1;
+	if (!is_power_of_2(groupsize) || groupsize > 8 || (len % groupsize != 0))
+		groupsize = 1;
+	byteswap = IS_ENABLED(CONFIG_CPU_BIG_ENDIAN) ? 0 : groupsize - 1;
 
 	/* hex dump */
 	p = test;
-	for (i = 0, j = 0; i < l; i++) {
-		unsigned char b = data_b[i ^ bs];
+	for (i = 0, j = 0; i < len; i++) {
+		unsigned char b = data_b[i ^ byteswap];
 		*p++ = "0123456789abcdef"[b >> 4];
 		*p++ = "0123456789abcdef"[b & 15];
-		if (++j == gs) {
+		if (++j == groupsize) {
 			j = 0;
 			*p++ = ' ';
 		}
@@ -59,15 +57,16 @@ static void __init test_hexdump_prepare_test(size_t len, size_t rowsize,
 	if (ascii) {
 		do {
 			*p++ = ' ';
-		} while (p < test + rs * 2 + rs / gs + 1);
+		} while (p < test + rowsize * 2 + rowsize / groupsize + 1);
 
-		for (i = 0; i < l; i++) {
+		for (i = 0; i < len; i++) {
 			unsigned char b = data_b[i];
 			*p++ = (isascii(b) && isprint(b)) ? b : '.';
 		}
 	}
 
 	*p = '\0';
+	return p - test;
 }
 
 #define TEST_HEXDUMP_BUF_SIZE		(32 * 3 + 2 + 32 + 1)
@@ -113,44 +112,35 @@ static void __init test_hexdump_overflow(size_t buflen, size_t len,
 {
 	char test[TEST_HEXDUMP_BUF_SIZE];
 	char buf[TEST_HEXDUMP_BUF_SIZE];
-	size_t rs = rowsize, gs = groupsize;
-	size_t ae, he, e, f, r;
-	bool a;
+	size_t expected;
+	size_t f, result;
+	bool ok;
 
 	total_tests++;
 
 	memset(buf, FILL_CHAR, sizeof(buf));
 
-	r = hex_dump_to_buffer(data_b, len, rs, gs, buf, buflen, ascii);
+	result = hex_dump_to_buffer(data_b, len, rowsize, groupsize, buf,
+				    buflen, ascii);
 
-	/*
-	 * Caller must provide the data length multiple of groupsize. The
-	 * calculations below are made with that assumption in mind.
-	 */
-	ae = rs * 2 /* hex */ + rs / gs /* spaces */ + 1 /* space */ + len /* ascii */;
-	he = (gs * 2 /* hex */ + 1 /* space */) * len / gs - 1 /* no trailing space */;
+	/* Test output is generated into a 'long enough' buffer */
+	expected = test_hexdump_prepare_test(len, rowsize, groupsize, test,
+					     sizeof(test), ascii);
 
-	if (ascii)
-		e = ae;
-	else
-		e = he;
-
-	f = min(e + 1, buflen);
-	if (buflen) {
-		test_hexdump_prepare_test(len, rs, gs, test, sizeof(test), ascii);
+	f = min(expected + 1, buflen);
+	if (f)
 		test[f - 1] = '\0';
-	}
 	memset(test + f, FILL_CHAR, sizeof(test) - f);
 
-	a = r == e && !memcmp(test, buf, TEST_HEXDUMP_BUF_SIZE);
+	ok = result == expected && !memcmp(test, buf, TEST_HEXDUMP_BUF_SIZE);
 
 	buf[sizeof(buf) - 1] = '\0';
 
-	if (!a) {
+	if (!ok) {
 		pr_err("Len: %zu buflen: %zu strlen: %zu\n",
 			len, buflen, strnlen(buf, sizeof(buf)));
-		pr_err("Result: %zu '%s'\n", r, buf);
-		pr_err("Expect: %zu '%s'\n", e, test);
+		pr_err("Result: %zu '%s'\n", result, buf);
+		pr_err("Expect: %zu '%s'\n", expected, test);
 		failed_tests++;
 	}
 }
@@ -158,13 +148,14 @@ static void __init test_hexdump_overflow(size_t buflen, size_t len,
 static void __init test_hexdump_overflow_set(size_t buflen, bool ascii)
 {
 	unsigned int i = 0;
-	size_t rs = get_random_u32_inclusive(1, 2) * 16;
+	size_t rowsize = get_random_u32_inclusive(1, 2) * 16;
 
 	do {
-		size_t gs = 1 << i;
-		size_t len = get_random_u32_below(rs) + gs;
+		size_t groupsize = 1 << i;
+		size_t len = get_random_u32_below(rowsize) + groupsize;
 
-		test_hexdump_overflow(buflen, rounddown(len, gs), rs, gs, ascii);
+		test_hexdump_overflow(buflen, rounddown(len, groupsize),
+				      rowsize, groupsize, ascii);
 	} while (i++ < 3);
 }
 
-- 
2.39.5


  parent reply	other threads:[~2025-03-08  9:35 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-03-08  9:34 [PATCH next 0/8] test_hexdump: Improve test coverage David Laight
2025-03-08  9:34 ` [PATCH next 1/8] test_hexdump: Change rowsize and groupsize to size_t David Laight
2025-03-10  8:28   ` Andy Shevchenko
2025-03-08  9:34 ` [PATCH next 2/8] test_hexdump: Create test output data from the binary input data buffer David Laight
2025-03-10  8:31   ` Andy Shevchenko
2025-03-12 19:28     ` David Laight
2025-03-12 19:36       ` Andy Shevchenko
2025-03-13  9:17         ` David Laight
2025-03-08  9:34 ` David Laight [this message]
2025-03-08  9:34 ` [PATCH next 4/8] test_hexdump: Check for buffer overrun of sample output buffer David Laight
2025-03-10  9:01   ` Andy Shevchenko
2025-03-12 19:37     ` David Laight
2025-03-08  9:34 ` [PATCH next 5/8] test_hexdump: Fix sample output if zero bytes requested David Laight
2025-03-10  9:03   ` Andy Shevchenko
2025-03-12 19:40     ` David Laight
2025-03-08  9:34 ` [PATCH next 6/8] test_hexdump: If a test fails print all the parameters David Laight
2025-03-08  9:34 ` [PATCH next 7/8] test_hexdump: Run fixed not random tests David Laight
2025-03-08  9:34 ` [PATCH next 8/8] test_hexdump: Test all 256 byte values David Laight

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=20250308093452.3742-4-david.laight.linux@gmail.com \
    --to=david.laight.linux@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=andriy.shevchenko@linux.intel.com \
    --cc=arnd@arndb.de \
    --cc=christophe.leroy@c-s.fr \
    --cc=horms@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@rasmusvillemoes.dk \
    --cc=nnac123@linux.ibm.com \
    --cc=torvalds@linux-foundation.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox