1 // Copyright 2021 gRPC authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "src/core/lib/gprpp/bitset.h"
16
17 #include <random>
18 #include <set>
19
20 #include "gtest/gtest.h"
21
22 namespace grpc_core {
23 namespace testing {
24
25 // Stand in type to make the size to test a type
26 template <size_t K>
27 struct Size {
28 static constexpr size_t kBits = K;
29 };
30
31 using TestSizes = ::testing::Types<
32 // All sizes up to 17 bits
33 Size<1>, Size<2>, Size<3>, Size<4>, Size<5>, Size<6>, Size<7>, Size<8>,
34 Size<9>, Size<10>, Size<11>, Size<12>, Size<13>, Size<14>, Size<15>,
35 Size<16>, Size<17>,
36 // Values around 32 bits
37 Size<24>, Size<25>, Size<26>, Size<27>, Size<28>, Size<29>, Size<30>,
38 Size<31>, Size<32>, Size<33>,
39 // Values around 48 bits
40 Size<47>, Size<48>, Size<49>,
41 // Values around 64 bits
42 Size<62>, Size<63>, Size<64>, Size<65>, Size<66>,
43 // Values around 96 bits
44 Size<95>, Size<96>, Size<97>,
45 // Silly numbers of bits
46 Size<1024>, Size<4000>, Size<4321> >;
47
48 template <typename S>
49 struct BitSetTest : public ::testing::Test {};
50
51 TYPED_TEST_SUITE(BitSetTest, TestSizes);
52
TYPED_TEST(BitSetTest,NoneAtInit)53 TYPED_TEST(BitSetTest, NoneAtInit) {
54 BitSet<TypeParam::kBits> b;
55 EXPECT_TRUE(b.none());
56 }
57
TYPED_TEST(BitSetTest,OneBit)58 TYPED_TEST(BitSetTest, OneBit) {
59 constexpr size_t kBits = TypeParam::kBits;
60 for (size_t i = 0; i < kBits; i++) {
61 BitSet<kBits> b;
62 b.set(i);
63 EXPECT_FALSE(b.none());
64 for (size_t j = 0; j < kBits; j++) {
65 EXPECT_EQ(b.is_set(j), i == j);
66 }
67 }
68 }
69
TYPED_TEST(BitSetTest,AllSet)70 TYPED_TEST(BitSetTest, AllSet) {
71 constexpr size_t kBits = TypeParam::kBits;
72 BitSet<kBits> b;
73 for (size_t i = 0; i < kBits; i++) {
74 EXPECT_FALSE(b.all());
75 b.set(i);
76 }
77 EXPECT_TRUE(b.all());
78 }
79
TYPED_TEST(BitSetTest,Count)80 TYPED_TEST(BitSetTest, Count) {
81 constexpr size_t kBits = TypeParam::kBits;
82 BitSet<kBits> b;
83 std::set<size_t> bits_set;
84 std::random_device rd;
85 std::uniform_int_distribution<size_t> dist(0, kBits - 1);
86 for (size_t i = 0; i < 4 * kBits; i++) {
87 size_t bit = dist(rd);
88 bits_set.insert(bit);
89 b.set(bit);
90 EXPECT_EQ(b.count(), bits_set.size());
91 }
92 }
93
TEST(ToIntTest,ToInt)94 TEST(ToIntTest, ToInt) {
95 auto make_bitset = [](bool b0, bool b1, bool b2) {
96 BitSet<3> b;
97 b.set(0, b0);
98 b.set(1, b1);
99 b.set(2, b2);
100 return b;
101 };
102 EXPECT_EQ(make_bitset(false, false, false).ToInt<uint32_t>(), 0);
103 EXPECT_EQ(make_bitset(true, false, false).ToInt<uint32_t>(), 1);
104 EXPECT_EQ(make_bitset(false, true, false).ToInt<uint32_t>(), 2);
105 EXPECT_EQ(make_bitset(true, true, false).ToInt<uint32_t>(), 3);
106 EXPECT_EQ(make_bitset(false, false, true).ToInt<uint32_t>(), 4);
107 EXPECT_EQ(make_bitset(true, false, true).ToInt<uint32_t>(), 5);
108 EXPECT_EQ(make_bitset(false, true, true).ToInt<uint32_t>(), 6);
109 EXPECT_EQ(make_bitset(true, true, true).ToInt<uint32_t>(), 7);
110 }
111
TEST(EmptyBitSet,Empty)112 TEST(EmptyBitSet, Empty) {
113 BitSet<0> b;
114 EXPECT_TRUE(b.all());
115 EXPECT_TRUE(b.none());
116 EXPECT_EQ(b.count(), 0);
117 }
118
119 } // namespace testing
120 } // namespace grpc_core
121
main(int argc,char ** argv)122 int main(int argc, char** argv) {
123 ::testing::InitGoogleTest(&argc, argv);
124 return RUN_ALL_TESTS();
125 }
126