bitset_test.cc
Go to the documentation of this file.
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 
16 
17 #include <random>
18 
19 #include <gtest/gtest.h>
20 
21 namespace grpc_core {
22 namespace testing {
23 
24 // Stand in type to make the size to test a type
25 template <size_t K>
26 struct Size {
27  static constexpr size_t kBits = K;
28 };
29 
31  // All sizes up to 17 bits
35  // Values around 32 bits
38  // Values around 48 bits
40  // Values around 64 bits
42  // Values around 96 bits
44  // Silly numbers of bits
46 
47 template <typename S>
48 struct BitSetTest : public ::testing::Test {};
49 
51 
52 TYPED_TEST(BitSetTest, NoneAtInit) {
54  EXPECT_TRUE(b.none());
55 }
56 
58  constexpr size_t kBits = TypeParam::kBits;
59  for (size_t i = 0; i < kBits; i++) {
61  b.set(i);
62  EXPECT_FALSE(b.none());
63  for (size_t j = 0; j < kBits; j++) {
64  EXPECT_EQ(b.is_set(j), i == j);
65  }
66  }
67 }
68 
70  constexpr size_t kBits = TypeParam::kBits;
72  for (size_t i = 0; i < kBits; i++) {
73  EXPECT_FALSE(b.all());
74  b.set(i);
75  }
76  EXPECT_TRUE(b.all());
77 }
78 
80  constexpr size_t kBits = TypeParam::kBits;
82  std::set<size_t> bits_set;
83  std::random_device rd;
84  std::uniform_int_distribution<size_t> dist(0, kBits - 1);
85  for (size_t i = 0; i < 4 * kBits; i++) {
86  size_t bit = dist(rd);
87  bits_set.insert(bit);
88  b.set(bit);
89  EXPECT_EQ(b.count(), bits_set.size());
90  }
91 }
92 
93 TEST(ToIntTest, ToInt) {
94  auto make_bitset = [](bool b0, bool b1, bool b2) {
95  BitSet<3> b;
96  b.set(0, b0);
97  b.set(1, b1);
98  b.set(2, b2);
99  return b;
100  };
101  EXPECT_EQ(make_bitset(false, false, false).ToInt<uint32_t>(), 0);
102  EXPECT_EQ(make_bitset(true, false, false).ToInt<uint32_t>(), 1);
103  EXPECT_EQ(make_bitset(false, true, false).ToInt<uint32_t>(), 2);
104  EXPECT_EQ(make_bitset(true, true, false).ToInt<uint32_t>(), 3);
105  EXPECT_EQ(make_bitset(false, false, true).ToInt<uint32_t>(), 4);
106  EXPECT_EQ(make_bitset(true, false, true).ToInt<uint32_t>(), 5);
107  EXPECT_EQ(make_bitset(false, true, true).ToInt<uint32_t>(), 6);
108  EXPECT_EQ(make_bitset(true, true, true).ToInt<uint32_t>(), 7);
109 }
110 
111 TEST(EmptyBitSet, Empty) {
112  BitSet<0> b;
113  EXPECT_TRUE(b.all());
114  EXPECT_TRUE(b.none());
115  EXPECT_EQ(b.count(), 0);
116 }
117 
118 } // namespace testing
119 } // namespace grpc_core
120 
121 int main(int argc, char** argv) {
122  ::testing::InitGoogleTest(&argc, argv);
123  return RUN_ALL_TESTS();
124 }
EXPECT_FALSE
#define EXPECT_FALSE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1970
testing
Definition: aws_request_signer_test.cc:25
grpc_core
Definition: call_metric_recorder.h:31
testing::Test
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:402
EXPECT_EQ
#define EXPECT_EQ(a, b)
Definition: iomgr/time_averaged_stats_test.cc:27
testing::internal::ProxyTypeList
Definition: googletest/googletest/include/gtest/internal/gtest-type-util.h:155
grpc_core::testing::TYPED_TEST_SUITE
TYPED_TEST_SUITE(BitSetTest, TestSizes)
b2
T::second_type b2
Definition: abseil-cpp/absl/container/internal/hash_function_defaults_test.cc:308
grpc_core::testing::Size::kBits
static constexpr size_t kBits
Definition: bitset_test.cc:27
RUN_ALL_TESTS
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2471
b
uint64_t b
Definition: abseil-cpp/absl/container/internal/layout_test.cc:53
Empty
Definition: abseil-cpp/absl/container/internal/compressed_tuple_test.cc:33
grpc_core::BitSet< 0 >
Definition: bitset.h:200
b1
T::second_type b1
Definition: abseil-cpp/absl/container/internal/hash_function_defaults_test.cc:306
testing::InitGoogleTest
GTEST_API_ void InitGoogleTest(int *argc, char **argv)
Definition: bloaty/third_party/googletest/googletest/src/gtest.cc:6106
grpc_core::testing::TYPED_TEST
TYPED_TEST(BitSetTest, NoneAtInit)
Definition: bitset_test.cc:52
grpc_core::BitSet
Definition: bitset.h:85
grpc_core::testing::Size
Definition: bitset_test.cc:26
grpc_core::testing::BitSetTest
Definition: bitset_test.cc:48
grpc_core::testing::TEST
TEST(ServiceConfigParserTest, DoubleRegistration)
Definition: service_config_test.cc:448
EXPECT_TRUE
#define EXPECT_TRUE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1967
google::protobuf::python::descriptor::Count
static PyObject * Count(PyContainer *self, PyObject *item)
Definition: bloaty/third_party/protobuf/python/google/protobuf/pyext/descriptor_containers.cc:694
main
int main(int argc, char **argv)
Definition: bitset_test.cc:121
bitset.h
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230


grpc
Author(s):
autogenerated on Thu Mar 13 2025 02:58:39