bitset_test.cc 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. #include "src/core/lib/gprpp/bitset.h"
  15. #include <random>
  16. #include <gtest/gtest.h>
  17. namespace grpc_core {
  18. namespace testing {
  19. // Stand in type to make the size to test a type
  20. template <size_t K>
  21. struct Size {
  22. static constexpr size_t kBits = K;
  23. };
  24. using TestSizes = ::testing::Types<
  25. // All sizes up to 17 bits
  26. Size<1>, Size<2>, Size<3>, Size<4>, Size<5>, Size<6>, Size<7>, Size<8>,
  27. Size<9>, Size<10>, Size<11>, Size<12>, Size<13>, Size<14>, Size<15>,
  28. Size<16>, Size<17>,
  29. // Values around 32 bits
  30. Size<24>, Size<25>, Size<26>, Size<27>, Size<28>, Size<29>, Size<30>,
  31. Size<31>, Size<32>, Size<33>,
  32. // Values around 48 bits
  33. Size<47>, Size<48>, Size<49>,
  34. // Values around 64 bits
  35. Size<62>, Size<63>, Size<64>, Size<65>, Size<66>,
  36. // Values around 96 bits
  37. Size<95>, Size<96>, Size<97>,
  38. // Silly numbers of bits
  39. Size<1024>, Size<4000>, Size<4321> >;
  40. template <typename S>
  41. struct BitSetTest : public ::testing::Test {};
  42. TYPED_TEST_SUITE(BitSetTest, TestSizes);
  43. TYPED_TEST(BitSetTest, NoneAtInit) {
  44. BitSet<TypeParam::kBits> b;
  45. EXPECT_TRUE(b.none());
  46. }
  47. TYPED_TEST(BitSetTest, OneBit) {
  48. constexpr size_t kBits = TypeParam::kBits;
  49. for (size_t i = 0; i < kBits; i++) {
  50. BitSet<kBits> b;
  51. b.set(i);
  52. EXPECT_FALSE(b.none());
  53. for (size_t j = 0; j < kBits; j++) {
  54. EXPECT_EQ(b.is_set(j), i == j);
  55. }
  56. }
  57. }
  58. TYPED_TEST(BitSetTest, AllSet) {
  59. constexpr size_t kBits = TypeParam::kBits;
  60. BitSet<kBits> b;
  61. for (size_t i = 0; i < kBits; i++) {
  62. EXPECT_FALSE(b.all());
  63. b.set(i);
  64. }
  65. EXPECT_TRUE(b.all());
  66. }
  67. TYPED_TEST(BitSetTest, Count) {
  68. constexpr size_t kBits = TypeParam::kBits;
  69. BitSet<kBits> b;
  70. std::set<size_t> bits_set;
  71. std::random_device rd;
  72. std::uniform_int_distribution<size_t> dist(0, kBits - 1);
  73. for (size_t i = 0; i < 4 * kBits; i++) {
  74. size_t bit = dist(rd);
  75. bits_set.insert(bit);
  76. b.set(bit);
  77. EXPECT_EQ(b.count(), bits_set.size());
  78. }
  79. }
  80. TEST(EmptyBitSet, Empty) {
  81. BitSet<0> b;
  82. EXPECT_TRUE(b.all());
  83. EXPECT_TRUE(b.none());
  84. EXPECT_EQ(b.count(), 0);
  85. }
  86. } // namespace testing
  87. } // namespace grpc_core
  88. int main(int argc, char** argv) {
  89. ::testing::InitGoogleTest(&argc, argv);
  90. return RUN_ALL_TESTS();
  91. }