]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - tests/sys/sys/bitset_test.c
bitset: Reimplement BIT_FOREACH_IS(SET|CLR)
[FreeBSD/FreeBSD.git] / tests / sys / sys / bitset_test.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2021 The FreeBSD Foundation
5  *
6  * This software was developed by Mark Johnston under sponsorship from
7  * the FreeBSD Foundation.
8  */
9
10 #include <sys/types.h>
11 #include <sys/_bitset.h>
12 #include <sys/bitset.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15
16 #include <atf-c.h>
17
18 BITSET_DEFINE(bs256, 256);
19
20 ATF_TC_WITHOUT_HEAD(bit_foreach);
21 ATF_TC_BODY(bit_foreach, tc)
22 {
23         struct bs256 bs0, bs1, bsrand;
24         int setc, clrc, i;
25
26 #define _BIT_FOREACH_COUNT(s, bs) do {                                  \
27         int prev = -1;                                                  \
28         setc = clrc = 0;                                                \
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);                            \
33                 setc++;                                                 \
34                 prev = i;                                               \
35         }                                                               \
36         prev = -1;                                                      \
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);                                \
41                 clrc++;                                                 \
42                 prev = i;                                               \
43         }                                                               \
44 } while (0)
45
46         /*
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.
49          */
50
51         BIT_FILL(256, &bs1);
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);
55
56         BIT_ZERO(256, &bs0);
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);
60
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",
67             setc, clrc);
68
69         /*
70          * Try to verify that we can safely clear bits in the set while
71          * iterating.
72          */
73         BIT_FOREACH_ISSET(256, i, &bsrand) {
74                 ATF_REQUIRE(setc-- > 0);
75                 BIT_CLR(256, i, &bsrand);
76         }
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);
80
81 #undef _BIT_FOREACH_COUNT
82 }
83
84 ATF_TP_ADD_TCS(tp)
85 {
86         ATF_TP_ADD_TC(tp, bit_foreach);
87         return (atf_no_error());
88 }