Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 28 Dec 2023 17:20:14 GMT
From:      Robert Clausecker <fuz@FreeBSD.org>
To:        src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org
Subject:   git: 08ad297599ee - stable/14 - lib/libc/tests/string/strcspn_test.c: add test for correct match order
Message-ID:  <202312281720.3BSHKECA080034@gitrepo.freebsd.org>

next in thread | raw e-mail | index | archive | help
The branch stable/14 has been updated by fuz:

URL: https://cgit.FreeBSD.org/src/commit/?id=08ad297599ee6c40b4346699dac81d71f1933f18

commit 08ad297599ee6c40b4346699dac81d71f1933f18
Author:     Robert Clausecker <fuz@FreeBSD.org>
AuthorDate: 2023-12-19 16:27:24 +0000
Commit:     Robert Clausecker <fuz@FreeBSD.org>
CommitDate: 2023-12-28 17:02:41 +0000

    lib/libc/tests/string/strcspn_test.c: add test for correct match order
    
    This new unit test verifies that if there are multiple
    matches, the first match is returned, ignoring later
    matches.
    
    Approved by:    mjg (blanket, via IRC)
    MFC after:      1 week
    MFC to:         stable/14
    
    (cherry picked from commit a0ecf2224ea35d029d33541878f0eee42f5fd84f)
---
 lib/libc/tests/string/strcspn_test.c | 51 +++++++++++++++++++++++++++++++++++-
 1 file changed, 50 insertions(+), 1 deletion(-)

diff --git a/lib/libc/tests/string/strcspn_test.c b/lib/libc/tests/string/strcspn_test.c
index 6cb2a04d3e64..293fc2bc8d4e 100644
--- a/lib/libc/tests/string/strcspn_test.c
+++ b/lib/libc/tests/string/strcspn_test.c
@@ -150,7 +150,7 @@ ATF_TC_BODY(set_alignments, tc)
 #ifndef STRSPN
 /* test all positions in which set could match buf */
 static void
-test_match_positions(char *buf,  char *set, size_t buflen, size_t setlen)
+test_match_positions(char *buf, char *set, size_t buflen, size_t setlen)
 {
 	size_t i, j, outcome;
 
@@ -206,6 +206,54 @@ ATF_TC_BODY(match_positions, tc)
 	test_match_positions(buf, set, 16, 8);
 	test_match_positions(buf, set, 8, 8);
 }
+
+/* if there are two matches, check that the earlier match is taken */
+static void
+test_match_order(char *buf, char *set, size_t buflen, size_t setlen)
+{
+	size_t i, j, k, l, outcome;
+
+	memset(buf, '-', buflen);
+
+	for (i = 0; i < setlen; i++)
+		set[i] = 'A' + i;
+
+	buf[buflen] = '\0';
+	set[setlen] = '\0';
+
+	for (i = 0; i < setlen; i++)
+		for (j = 0; j < setlen; j++)
+			for (k = 0; k + 1 < buflen; k++)
+				for (l = k + 1; l < buflen; l++) {
+					buf[k] = set[i];
+					buf[l] = set[j];
+					outcome = strcspn(buf, set);
+					ATF_CHECK_EQ_MSG(k, outcome,
+					    "strcspn(\"%s\", \"%s\") = %zu != %zu",
+					    buf, set, outcome, k);
+					buf[k] = '-';
+					buf[l] = '-';
+				}
+}
+
+ATF_TC_WITHOUT_HEAD(match_order);
+ATF_TC_BODY(match_order, tc)
+{
+	char buf[33], set[65];
+
+	test_match_order(buf, set, 32, 64);
+	test_match_order(buf, set, 16, 64);
+	test_match_order(buf, set, 8, 64);
+	test_match_order(buf, set, 32, 32);
+	test_match_order(buf, set, 16, 32);
+	test_match_order(buf, set, 8, 32);
+	test_match_order(buf, set, 32, 16);
+	test_match_order(buf, set, 16, 16);
+	test_match_order(buf, set, 8, 16);
+	test_match_order(buf, set, 32, 8);
+	test_match_order(buf, set, 16, 8);
+	test_match_order(buf, set, 8, 8);
+}
 #endif /* !defined(STRSPN) */
 
 ATF_TP_ADD_TCS(tp)
@@ -214,6 +262,7 @@ ATF_TP_ADD_TCS(tp)
 	ATF_TP_ADD_TC(tp, set_alignments);
 #ifndef STRSPN
 	ATF_TP_ADD_TC(tp, match_positions);
+	ATF_TP_ADD_TC(tp, match_order);
 #endif
 
 	return (atf_no_error());



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?202312281720.3BSHKECA080034>