git: 670be460e408 - main - bitstring_test: Add regression tests for bit_ff(c|s)_area_at()

From: Mark Johnston <markj_at_FreeBSD.org>
Date: Fri, 20 May 2022 14:36:35 UTC
The branch main has been updated by markj:

URL: https://cgit.FreeBSD.org/src/commit/?id=670be460e4089038aadb47917bd71fe07d2cac21

commit 670be460e4089038aadb47917bd71fe07d2cac21
Author:     Mark Johnston <markj@FreeBSD.org>
AuthorDate: 2022-05-20 14:18:10 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2022-05-20 14:36:05 +0000

    bitstring_test: Add regression tests for bit_ff(c|s)_area_at()
    
    Validate the cases where a match can be found immediately and where no
    match can be found.  This extends the existing test cases and is enough
    to catch the bug fixed in commit 6e7a585348d5 ("bitstring: fix ff_area()
    when start!=0").
    
    Reviewed by:    dougm
    MFC after:      1 week
    Sponsored by:   The FreeBSD Foundation
    Differential Revision:  https://reviews.freebsd.org/D35259
---
 tests/sys/sys/bitstring_test.c | 73 ++++++++++++++++++++++++++++++++++--------
 1 file changed, 60 insertions(+), 13 deletions(-)

diff --git a/tests/sys/sys/bitstring_test.c b/tests/sys/sys/bitstring_test.c
index 97aa97a680f0..68f394c88d7c 100644
--- a/tests/sys/sys/bitstring_test.c
+++ b/tests/sys/sys/bitstring_test.c
@@ -29,6 +29,7 @@
  *
  * $FreeBSD$
  */
+
 #include <sys/param.h>
 
 #include <bitstring.h>
@@ -321,26 +322,72 @@ BITSTRING_TC_DEFINE(bit_ffc_at)
 			nbits, memloc, nbits + 3, found_clear_bit);
 }
 
-BITSTRING_TC_DEFINE(bit_ffc_area_no_match)
+BITSTRING_TC_DEFINE(bit_ffc_area_at_all_or_nothing)
 /* bitstr_t *bitstr, int nbits, const char *memloc */
 {
-	int found_clear_bits;
+	int found;
 
-	memset(bitstr, 0xFF, bitstr_size(nbits));
-	bit_ffc_area(bitstr, nbits, 2, &found_clear_bits);
-	ATF_REQUIRE_EQ_MSG(-1, found_clear_bits,
-		"bit_ffc_area_%d_%s: Failed all set bits.", nbits, memloc);
+	memset(bitstr, 0, bitstr_size(nbits));
+	if (nbits % _BITSTR_BITS != 0)
+		bit_nset(bitstr, nbits, roundup2(nbits, _BITSTR_BITS) - 1);
+
+	for (int start = 0; start < nbits; start++) {
+		for (int size = 1; size < nbits - start; size++) {
+			bit_ffc_area_at(bitstr, start, nbits, size, &found);
+			ATF_REQUIRE_EQ_MSG(start, found,
+			    "bit_ffc_area_at_%d_%s: "
+			    "Did not find %d clear bits at %d",
+			    nbits, memloc, size, start);
+		}
+	}
+
+	memset(bitstr, 0xff, bitstr_size(nbits));
+	if (nbits % _BITSTR_BITS != 0)
+		bit_nclear(bitstr, nbits, roundup2(nbits, _BITSTR_BITS) - 1);
+
+	for (int start = 0; start < nbits; start++) {
+		for (int size = 1; size < nbits - start; size++) {
+			bit_ffc_area_at(bitstr, start, nbits, size, &found);
+			ATF_REQUIRE_EQ_MSG(-1, found,
+			    "bit_ffc_area_at_%d_%s: "
+			    "Found %d clear bits at %d",
+			    nbits, memloc, size, start);
+		}
+	}
 }
 
-BITSTRING_TC_DEFINE(bit_ffs_area_no_match)
+BITSTRING_TC_DEFINE(bit_ffs_area_at_all_or_nothing)
 /* bitstr_t *bitstr, int nbits, const char *memloc */
 {
-	int found_clear_bits;
+	int found;
 
 	memset(bitstr, 0, bitstr_size(nbits));
-	bit_ffs_area(bitstr, nbits, 2, &found_clear_bits);
-	ATF_REQUIRE_EQ_MSG(-1, found_clear_bits,
-		"bit_ffs_area_%d_%s: Failed all clear bits.", nbits, memloc);
+	if (nbits % _BITSTR_BITS != 0)
+		bit_nset(bitstr, nbits, roundup2(nbits, _BITSTR_BITS) - 1);
+
+	for (int start = 0; start < nbits; start++) {
+		for (int size = 1; size < nbits - start; size++) {
+			bit_ffs_area_at(bitstr, start, nbits, size, &found);
+			ATF_REQUIRE_EQ_MSG(-1, found,
+			    "bit_ffs_area_at_%d_%s: "
+			    "Found %d set bits at %d",
+			    nbits, memloc, size, start);
+		}
+	}
+
+	memset(bitstr, 0xff, bitstr_size(nbits));
+	if (nbits % _BITSTR_BITS != 0)
+		bit_nclear(bitstr, nbits, roundup2(nbits, _BITSTR_BITS) - 1);
+
+	for (int start = 0; start < nbits; start++) {
+		for (int size = 1; size < nbits - start; size++) {
+			bit_ffs_area_at(bitstr, start, nbits, size, &found);
+			ATF_REQUIRE_EQ_MSG(start, found,
+			    "bit_ffs_area_at_%d_%s: "
+			    "Did not find %d set bits at %d",
+			    nbits, memloc, size, start);
+		}
+	}
 }
 
 ATF_TC_WITHOUT_HEAD(bit_ffs_area);
@@ -833,8 +880,8 @@ ATF_TP_ADD_TCS(tp)
 	BITSTRING_TC_ADD(tp, bit_nclear);
 	BITSTRING_TC_ADD(tp, bit_nset);
 	BITSTRING_TC_ADD(tp, bit_count);
-	BITSTRING_TC_ADD(tp, bit_ffs_area_no_match);
-	BITSTRING_TC_ADD(tp, bit_ffc_area_no_match);
+	BITSTRING_TC_ADD(tp, bit_ffs_area_at_all_or_nothing);
+	BITSTRING_TC_ADD(tp, bit_ffc_area_at_all_or_nothing);
 	BITSTRING_TC_ADD(tp, bit_foreach);
 	BITSTRING_TC_ADD(tp, bit_foreach_at);
 	BITSTRING_TC_ADD(tp, bit_foreach_unset);