public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: "Björn Töpel" <bjorn@kernel.org>
To: Palmer Dabbelt <palmer@dabbelt.com>,
	Andy Shevchenko <andy@kernel.org>,
	linux-kernel@vger.kernel.org
Cc: "Palmer Dabbelt" <palmer@rivosinc.com>,
	"Heiko Stuebner" <heiko@sntech.de>,
	linux-riscv@lists.infradead.org,
	"Björn Töpel" <bjorn@rivosinc.com>
Subject: [PATCH v2 2/2] lib/test_string.c: Add strncmp() tests
Date: Tue, 28 Feb 2023 19:42:45 +0100	[thread overview]
Message-ID: <20230228184245.1585775-2-bjorn@kernel.org> (raw)
In-Reply-To: <20230228184245.1585775-1-bjorn@kernel.org>

From: Palmer Dabbelt <palmer@rivosinc.com>

The RISC-V strncmp() fails on some inputs, see the linked thread for
more details. It turns out there were no strncmp() calls in the self
tests, this adds one.

Link: https://lore.kernel.org/all/2801162.88bMQJbFj6@diego/
Reported-by: Heiko Stübner <heiko@sntech.de>
Signed-off-by: Palmer Dabbelt <palmer@rivosinc.com>
Signed-off-by: Björn Töpel <bjorn@rivosinc.com>
---

v1->v2: Added two more tests (pos/neg). (Andy)
        Minor code style issues. (Andy)
        Fixed checkpatch errors.

---
lib/test_string.c | 43 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 43 insertions(+)

diff --git a/lib/test_string.c b/lib/test_string.c
index 550229084c41..b95037eb138b 100644
--- a/lib/test_string.c
+++ b/lib/test_string.c
@@ -209,6 +209,44 @@ static __init int strspn_selftest(void)
 	return 0;
 }
 
+struct strncmp_test {
+	const char *str_a;
+	const char *str_b;
+	unsigned long count;
+	unsigned long max_off;
+	int retval;
+};
+
+static __init int strncmp_selftest(void)
+{
+	size_t i;
+	static const struct strncmp_test tests[] __initconst = {
+		{ "/dev/vda", "/dev/", 5, 4, 0 },
+		{ "/dev/vda", "/dev/vdb", 5, 4, 0 },
+		{ "00000000---11111", "00000000---11112", 12, 4, 0 },
+		{ "ABC", "AB", 3, 0, 67 },
+		{ "ABA", "ABZ", 3, 0, -25 },
+		{ "ABC", "ABC", 3, 0, 0 },
+	};
+
+	for (i = 0; i < ARRAY_SIZE(tests); ++i) {
+		const struct strncmp_test *s = tests + i;
+		size_t off;
+
+		for (off = 0; off <= s->max_off; off++) {
+			int res = strncmp(s->str_a + off, s->str_b + off, s->count - off);
+
+			if (res == 0 && s->retval != 0)
+				return 0x1000 + 0x100*off + 0x10*i + 0x0;
+			if (res > 0 && s->retval <= 0)
+				return 0x1000 + 0x100*off + 0x10*i + 0x1;
+			if (res < 0 && s->retval >= 0)
+				return 0x1000 + 0x100*off + 0x10*i + 0x2;
+		}
+	}
+	return 0;
+}
+
 static __exit void string_selftest_remove(void)
 {
 }
@@ -247,6 +285,11 @@ static __init int string_selftest_init(void)
 	if (subtest)
 		goto fail;
 
+	test = 7;
+	subtest = strncmp_selftest();
+	if (subtest)
+		goto fail;
+
 	pr_info("String selftests succeeded\n");
 	return 0;
 fail:
-- 
2.37.2


  reply	other threads:[~2023-02-28 18:43 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-02-28 18:42 [PATCH v2 1/2] lib/test_string.c: Make definition less dense Björn Töpel
2023-02-28 18:42 ` Björn Töpel [this message]
2023-02-28 19:03 ` Andy Shevchenko
2023-02-28 19:41   ` Björn Töpel

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=20230228184245.1585775-2-bjorn@kernel.org \
    --to=bjorn@kernel.org \
    --cc=andy@kernel.org \
    --cc=bjorn@rivosinc.com \
    --cc=heiko@sntech.de \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-riscv@lists.infradead.org \
    --cc=palmer@dabbelt.com \
    --cc=palmer@rivosinc.com \
    /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