Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 17 Sep 2023 01:54:00 GMT
From:      Robert Clausecker <fuz@FreeBSD.org>
To:        src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org
Subject:   git: 601fd768cfd5 - main - lib/libc/tests/string/strcspn_test.c: extend tests to catch previous bug
Message-ID:  <202309170154.38H1s0cO059592@gitrepo.freebsd.org>

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

URL: https://cgit.FreeBSD.org/src/commit/?id=601fd768cfd599518f7f9d30592763d1279d336d

commit 601fd768cfd599518f7f9d30592763d1279d336d
Author:     Robert Clausecker <fuz@FreeBSD.org>
AuthorDate: 2023-09-12 02:48:58 +0000
Commit:     Robert Clausecker <fuz@FreeBSD.org>
CommitDate: 2023-09-17 00:40:13 +0000

    lib/libc/tests/string/strcspn_test.c: extend tests to catch previous bug
    
    This extends the strcspn() unit tests to catch mistakes in the
    implementation that only appear when a mismatch occurs in a certain
    position of the string against a certain position of the set.
    
    See also:       52d4a4d4e0dedc72bc33082a3f84c2d0fd6f2cbb
    Sponsored by:   The FreeBSD Foundation
    Approved by:    imp
    MFC after:      3 days
    Differential Revision:  https://reviews.freebsd.org/D41821
---
 lib/libc/tests/string/strcspn_test.c | 64 ++++++++++++++++++++++++++++++++++++
 1 file changed, 64 insertions(+)

diff --git a/lib/libc/tests/string/strcspn_test.c b/lib/libc/tests/string/strcspn_test.c
index abacdcbbf18a..722d20025535 100644
--- a/lib/libc/tests/string/strcspn_test.c
+++ b/lib/libc/tests/string/strcspn_test.c
@@ -148,10 +148,74 @@ ATF_TC_BODY(set_alignments, tc)
 	test_set_alignments(buf, 30, NOMATCH);
 }
 
+#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)
+{
+	size_t i, j, outcome;
+
+	memset(buf, '-', buflen);
+
+	for (i = 0; i < setlen; i++)
+		set[i] = 'A' + i;
+
+	buf[buflen] = '\0';
+	set[setlen] = '\0';
+
+	/*
+	 * Check for (mis)match at buffer position i
+	 * against set position j.
+	 */
+	for (i = 0; i < buflen; i++) {
+		for (j = 0; j < setlen; j++) {
+			buf[i] = set[j];
+
+			outcome = strcspn(buf, set);
+			ATF_CHECK_EQ_MSG(i, outcome,
+			    "strcspn(\"%s\", \"%s\") = %zu != %zu",
+			    buf, set, outcome, i);
+		}
+
+		buf[i] = '-';
+	}
+}
+
+ATF_TC_WITHOUT_HEAD(match_positions);
+ATF_TC_BODY(match_positions, tc)
+{
+	char buf[129], set[65];
+
+	test_match_positions(buf, set, 128, 64);
+	test_match_positions(buf, set, 64, 64);
+	test_match_positions(buf, set, 32, 64);
+	test_match_positions(buf, set, 16, 64);
+	test_match_positions(buf, set, 8, 64);
+	test_match_positions(buf, set, 128, 32);
+	test_match_positions(buf, set, 64, 32);
+	test_match_positions(buf, set, 32, 32);
+	test_match_positions(buf, set, 16, 32);
+	test_match_positions(buf, set, 8, 32);
+	test_match_positions(buf, set, 128, 16);
+	test_match_positions(buf, set, 64, 16);
+	test_match_positions(buf, set, 32, 16);
+	test_match_positions(buf, set, 16, 16);
+	test_match_positions(buf, set, 8, 16);
+	test_match_positions(buf, set, 128, 8);
+	test_match_positions(buf, set, 64, 8);
+	test_match_positions(buf, set, 32, 8);
+	test_match_positions(buf, set, 16, 8);
+	test_match_positions(buf, set, 8, 8);
+}
+#endif /* !defined(STRSPN) */
+
 ATF_TP_ADD_TCS(tp)
 {
 	ATF_TP_ADD_TC(tp, buf_alignments);
 	ATF_TP_ADD_TC(tp, set_alignments);
+#ifndef STRSPN
+	ATF_TP_ADD_TC(tp, match_positions);
+#endif
 
 	return (atf_no_error());
 }



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