151425cb2SMark Johnston /*-
24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
351425cb2SMark Johnston *
451425cb2SMark Johnston * Copyright (c) 2021 The FreeBSD Foundation
551425cb2SMark Johnston *
651425cb2SMark Johnston * This software was developed by Mark Johnston under sponsorship from
751425cb2SMark Johnston * the FreeBSD Foundation.
851425cb2SMark Johnston */
951425cb2SMark Johnston
105e04571cSStefan Eßer #define _WANT_FREEBSD_BITSET
115e04571cSStefan Eßer
1251425cb2SMark Johnston #include <sys/types.h>
1351425cb2SMark Johnston #include <sys/_bitset.h>
1451425cb2SMark Johnston #include <sys/bitset.h>
1551425cb2SMark Johnston #include <stdio.h>
1651425cb2SMark Johnston #include <stdlib.h>
1751425cb2SMark Johnston
1851425cb2SMark Johnston #include <atf-c.h>
1951425cb2SMark Johnston
2051425cb2SMark Johnston BITSET_DEFINE(bs256, 256);
2151425cb2SMark Johnston
2251425cb2SMark Johnston ATF_TC_WITHOUT_HEAD(bit_foreach);
ATF_TC_BODY(bit_foreach,tc)2351425cb2SMark Johnston ATF_TC_BODY(bit_foreach, tc)
2451425cb2SMark Johnston {
2551425cb2SMark Johnston struct bs256 bs0, bs1, bsrand;
2651425cb2SMark Johnston int setc, clrc, i;
2751425cb2SMark Johnston
2851425cb2SMark Johnston #define _BIT_FOREACH_COUNT(s, bs) do { \
2951425cb2SMark Johnston int prev = -1; \
3051425cb2SMark Johnston setc = clrc = 0; \
3151425cb2SMark Johnston BIT_FOREACH_ISSET((s), i, (bs)) { \
3251425cb2SMark Johnston ATF_REQUIRE_MSG(prev < i, "incorrect bit ordering"); \
3351425cb2SMark Johnston ATF_REQUIRE_MSG(BIT_ISSET((s), i, (bs)), \
3451425cb2SMark Johnston "bit %d is not set", i); \
3551425cb2SMark Johnston setc++; \
3651425cb2SMark Johnston prev = i; \
3751425cb2SMark Johnston } \
3851425cb2SMark Johnston prev = -1; \
3951425cb2SMark Johnston BIT_FOREACH_ISCLR((s), i, (bs)) { \
4051425cb2SMark Johnston ATF_REQUIRE_MSG(prev < i, "incorrect bit ordering"); \
4151425cb2SMark Johnston ATF_REQUIRE_MSG(!BIT_ISSET((s), i, (bs)), \
4251425cb2SMark Johnston "bit %d is set", i); \
4351425cb2SMark Johnston clrc++; \
4451425cb2SMark Johnston prev = i; \
4551425cb2SMark Johnston } \
4651425cb2SMark Johnston } while (0)
4751425cb2SMark Johnston
4851425cb2SMark Johnston /*
4951425cb2SMark Johnston * Create several bitsets, and for each one count the number
5051425cb2SMark Johnston * of set and clear bits and make sure they match what we expect.
5151425cb2SMark Johnston */
5251425cb2SMark Johnston
5351425cb2SMark Johnston BIT_FILL(256, &bs1);
5451425cb2SMark Johnston _BIT_FOREACH_COUNT(256, &bs1);
5551425cb2SMark Johnston ATF_REQUIRE_MSG(setc == 256, "incorrect set count %d", setc);
5651425cb2SMark Johnston ATF_REQUIRE_MSG(clrc == 0, "incorrect clear count %d", clrc);
5751425cb2SMark Johnston
5851425cb2SMark Johnston BIT_ZERO(256, &bs0);
5951425cb2SMark Johnston _BIT_FOREACH_COUNT(256, &bs0);
6051425cb2SMark Johnston ATF_REQUIRE_MSG(setc == 0, "incorrect set count %d", setc);
6151425cb2SMark Johnston ATF_REQUIRE_MSG(clrc == 256, "incorrect clear count %d", clrc);
6251425cb2SMark Johnston
6351425cb2SMark Johnston BIT_ZERO(256, &bsrand);
6451425cb2SMark Johnston for (i = 0; i < 256; i++)
6551425cb2SMark Johnston if (random() % 2 != 0)
6651425cb2SMark Johnston BIT_SET(256, i, &bsrand);
6751425cb2SMark Johnston _BIT_FOREACH_COUNT(256, &bsrand);
6851425cb2SMark Johnston ATF_REQUIRE_MSG(setc + clrc == 256, "incorrect counts %d, %d",
6951425cb2SMark Johnston setc, clrc);
7051425cb2SMark Johnston
7151425cb2SMark Johnston /*
7251425cb2SMark Johnston * Try to verify that we can safely clear bits in the set while
7351425cb2SMark Johnston * iterating.
7451425cb2SMark Johnston */
7551425cb2SMark Johnston BIT_FOREACH_ISSET(256, i, &bsrand) {
7651425cb2SMark Johnston ATF_REQUIRE(setc-- > 0);
7751425cb2SMark Johnston BIT_CLR(256, i, &bsrand);
7851425cb2SMark Johnston }
7951425cb2SMark Johnston _BIT_FOREACH_COUNT(256, &bsrand);
8051425cb2SMark Johnston ATF_REQUIRE_MSG(setc == 0, "incorrect set count %d", setc);
8151425cb2SMark Johnston ATF_REQUIRE_MSG(clrc == 256, "incorrect clear count %d", clrc);
8251425cb2SMark Johnston
8351425cb2SMark Johnston #undef _BIT_FOREACH_COUNT
8451425cb2SMark Johnston }
8551425cb2SMark Johnston
ATF_TP_ADD_TCS(tp)8651425cb2SMark Johnston ATF_TP_ADD_TCS(tp)
8751425cb2SMark Johnston {
8851425cb2SMark Johnston ATF_TP_ADD_TC(tp, bit_foreach);
8951425cb2SMark Johnston return (atf_no_error());
9051425cb2SMark Johnston }
91