2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2021 The FreeBSD Foundation
6 * This software was developed by Mark Johnston under sponsorship from
7 * the FreeBSD Foundation.
10 #include <sys/types.h>
11 #include <sys/_bitset.h>
12 #include <sys/bitset.h>
18 BITSET_DEFINE(bs256, 256);
20 ATF_TC_WITHOUT_HEAD(bit_foreach);
21 ATF_TC_BODY(bit_foreach, tc)
23 struct bs256 bs0, bs1, bsrand;
26 #define _BIT_FOREACH_COUNT(s, bs) do { \
29 BIT_FOREACH_ISSET((s), i, (bs)) { \
30 ATF_REQUIRE_MSG(prev < i, "incorrect bit ordering"); \
31 ATF_REQUIRE_MSG(BIT_ISSET((s), i, (bs)), \
32 "bit %d is not set", i); \
37 BIT_FOREACH_ISCLR((s), i, (bs)) { \
38 ATF_REQUIRE_MSG(prev < i, "incorrect bit ordering"); \
39 ATF_REQUIRE_MSG(!BIT_ISSET((s), i, (bs)), \
40 "bit %d is set", i); \
47 * Create several bitsets, and for each one count the number
48 * of set and clear bits and make sure they match what we expect.
52 _BIT_FOREACH_COUNT(256, &bs1);
53 ATF_REQUIRE_MSG(setc == 256, "incorrect set count %d", setc);
54 ATF_REQUIRE_MSG(clrc == 0, "incorrect clear count %d", clrc);
57 _BIT_FOREACH_COUNT(256, &bs0);
58 ATF_REQUIRE_MSG(setc == 0, "incorrect set count %d", setc);
59 ATF_REQUIRE_MSG(clrc == 256, "incorrect clear count %d", clrc);
61 BIT_ZERO(256, &bsrand);
62 for (i = 0; i < 256; i++)
63 if (random() % 2 != 0)
64 BIT_SET(256, i, &bsrand);
65 _BIT_FOREACH_COUNT(256, &bsrand);
66 ATF_REQUIRE_MSG(setc + clrc == 256, "incorrect counts %d, %d",
70 * Try to verify that we can safely clear bits in the set while
73 BIT_FOREACH_ISSET(256, i, &bsrand) {
74 ATF_REQUIRE(setc-- > 0);
75 BIT_CLR(256, i, &bsrand);
77 _BIT_FOREACH_COUNT(256, &bsrand);
78 ATF_REQUIRE_MSG(setc == 0, "incorrect set count %d", setc);
79 ATF_REQUIRE_MSG(clrc == 256, "incorrect clear count %d", clrc);
81 #undef _BIT_FOREACH_COUNT
86 ATF_TP_ADD_TC(tp, bit_foreach);
87 return (atf_no_error());