/Users/deen/code/yugabyte-db/src/yb/util/uint_set-test.cc
Line | Count | Source |
1 | | // |
2 | | // Copyright (c) YugaByte, Inc. |
3 | | // |
4 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
5 | | // in compliance with the License. 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 distributed under the License |
10 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
11 | | // or implied. See the License for the specific language governing permissions and limitations |
12 | | // under the License. |
13 | | // |
14 | | |
15 | | #include <gtest/gtest.h> |
16 | | |
17 | | #include "yb/util/proto_container_test.pb.h" |
18 | | #include "yb/util/random.h" |
19 | | #include "yb/util/result.h" |
20 | | #include "yb/util/test_macros.h" |
21 | | #include "yb/util/test_util.h" |
22 | | #include "yb/util/uint_set.h" |
23 | | |
24 | | namespace yb { |
25 | | |
26 | | constexpr uint32_t kNumRandomToVerify = 10000; |
27 | | class UnsignedIntSetTest : public YBTest { |
28 | | protected: |
29 | 705 | CHECKED_STATUS SetRange(uint32_t lo, uint32_t hi) { |
30 | 705 | RETURN_NOT_OK(set_.SetRange(lo, hi)); |
31 | 11.2M | for (auto i = lo; i <= hi; ++i) { |
32 | 11.2M | state_.insert(i); |
33 | 11.2M | } |
34 | 705 | max_ = std::max(max_, hi); |
35 | 705 | return Status::OK(); |
36 | 705 | } |
37 | | |
38 | 1.05k | uint32_t GetMaxIndexToCheck() const { |
39 | 1.05k | constexpr uint32_t kBufferOverMaxToCheck = 100; |
40 | 1.05k | return max_ + kBufferOverMaxToCheck; |
41 | 1.05k | } |
42 | | |
43 | 4 | void VerifyState() const { |
44 | 4 | EXPECT_EQ(state_.empty(), set_.IsEmpty()); |
45 | 501 | for (uint32_t i = 0; i <= GetMaxIndexToCheck(); ++i) { |
46 | 994 | EXPECT_EQ(set_.Test(i), state_.find(i) != state_.end()) << i; |
47 | 497 | } |
48 | 4 | } |
49 | | |
50 | 692 | void RandomVerifyState() const { |
51 | 692 | EXPECT_EQ(state_.empty(), set_.IsEmpty()); |
52 | 44.6M | for (const auto& elem : state_) { |
53 | 44.6M | EXPECT_TRUE(set_.Test(elem)); |
54 | 44.6M | } |
55 | | |
56 | 692 | Random rng(29203); |
57 | 6.92M | for (size_t i = 0; i < kNumRandomToVerify; ++i) { |
58 | 6.92M | auto random_idx = rng.Next32(); |
59 | 13.8M | EXPECT_EQ(set_.Test(random_idx), state_.find(random_idx) != state_.end()) << i; |
60 | 6.92M | } |
61 | 692 | } |
62 | | |
63 | | UnsignedIntSet<uint32_t> set_; |
64 | | std::set<uint32_t> state_; |
65 | | uint32_t max_ = 0; |
66 | | }; |
67 | | |
68 | 1 | TEST_F(UnsignedIntSetTest, BasicSet) { |
69 | 1 | ASSERT_OK(SetRange(10, 21)); |
70 | 1 | ASSERT_OK(SetRange(24, 29)); |
71 | 1 | VerifyState(); |
72 | 1 | } |
73 | | |
74 | 1 | TEST_F(UnsignedIntSetTest, JoinRanges) { |
75 | 1 | ASSERT_OK(SetRange(10, 21)); |
76 | 1 | ASSERT_OK(SetRange(22, 29)); |
77 | 1 | VerifyState(); |
78 | 1 | } |
79 | | |
80 | 1 | TEST_F(UnsignedIntSetTest, SingleElemRange) { |
81 | 1 | ASSERT_OK(SetRange(10, 10)); |
82 | 1 | VerifyState(); |
83 | 1 | } |
84 | | |
85 | 1 | TEST_F(UnsignedIntSetTest, OverlappingRange) { |
86 | 1 | ASSERT_OK(SetRange(10, 21)); |
87 | 1 | ASSERT_OK(SetRange(15, 25)); |
88 | 1 | VerifyState(); |
89 | 1 | } |
90 | | |
91 | | class UnsignedIntSetEncodeDecodeTest : public UnsignedIntSetTest { |
92 | | protected: |
93 | 5 | void VerifyCopy() const { |
94 | 5 | auto copy = ASSERT_RESULT(GetCopy()); |
95 | | |
96 | 554 | for (uint32_t i = 0; i <= GetMaxIndexToCheck(); ++i) { |
97 | 549 | EXPECT_EQ(set_.Test(i), copy.Test(i)); |
98 | 549 | } |
99 | 5 | } |
100 | | |
101 | 692 | void RandomVerifyCopy() const { |
102 | 692 | auto copy = ASSERT_RESULT(GetCopy()); |
103 | | |
104 | 692 | EXPECT_EQ(state_.empty(), set_.IsEmpty()); |
105 | 44.6M | for (const auto& elem : state_) { |
106 | 44.6M | EXPECT_EQ(set_.Test(elem), copy.Test(elem)); |
107 | 44.6M | } |
108 | | |
109 | 692 | Random rng(29203); |
110 | 6.92M | for (size_t i = 0; i < kNumRandomToVerify; ++i) { |
111 | 6.92M | auto random_idx = rng.Next32(); |
112 | 13.8M | EXPECT_EQ(set_.Test(random_idx), copy.Test(random_idx)) << i; |
113 | 6.92M | } |
114 | 692 | } |
115 | | |
116 | | private: |
117 | 697 | Result<UnsignedIntSet<uint32_t>> GetCopy() const { |
118 | 697 | UnsignedIntSetTestPB pb; |
119 | 697 | set_.ToPB(pb.mutable_set()); |
120 | 697 | return UnsignedIntSet<uint32_t>::FromPB(pb.set()); |
121 | 697 | } |
122 | | }; |
123 | | |
124 | 1 | TEST_F(UnsignedIntSetEncodeDecodeTest, EncodeDecode) { |
125 | 1 | ASSERT_OK(SetRange(10, 21)); |
126 | 1 | ASSERT_OK(SetRange(24, 29)); |
127 | 1 | VerifyCopy(); |
128 | 1 | } |
129 | | |
130 | 1 | TEST_F(UnsignedIntSetEncodeDecodeTest, HasZeroRangeSet) { |
131 | 1 | ASSERT_OK(SetRange(0, 10)); |
132 | 1 | VerifyCopy(); |
133 | 1 | } |
134 | | |
135 | 1 | TEST_F(UnsignedIntSetEncodeDecodeTest, HasZeroOnlySet) { |
136 | 1 | ASSERT_OK(SetRange(0, 0)); |
137 | 1 | VerifyCopy(); |
138 | 1 | } |
139 | | |
140 | 1 | TEST_F(UnsignedIntSetEncodeDecodeTest, HasNoneSet) { |
141 | 1 | VerifyCopy(); |
142 | 1 | } |
143 | | |
144 | 1 | TEST_F(UnsignedIntSetEncodeDecodeTest, MultipleSetRanges) { |
145 | 1 | ASSERT_OK(SetRange(2, 3)); |
146 | 1 | ASSERT_OK(SetRange(5, 5)); |
147 | 1 | VerifyCopy(); |
148 | 1 | } |
149 | | |
150 | 1 | TEST_F(UnsignedIntSetEncodeDecodeTest, Random) { |
151 | 1 | constexpr int kNumIters = 10; |
152 | 1 | constexpr int kMinNumIntervals = 10; |
153 | 1 | constexpr int kMaxNumIntervals = 100; |
154 | 1 | uint16_t kMaxValue = std::numeric_limits<uint16_t>::max(); |
155 | 1 | Random rng(2813308004); |
156 | | |
157 | 11 | for (int i = 0; i < kNumIters; ++i) { |
158 | 10 | UnsignedIntSet<uint16_t> set; |
159 | 10 | auto num_ranges = kMinNumIntervals + rng.Uniform(kMaxNumIntervals); |
160 | 702 | for (size_t range_idx = 0; range_idx < num_ranges; ++range_idx) { |
161 | 692 | uint16_t lo = rng.Uniform(kMaxValue - 1); |
162 | 692 | uint16_t hi = lo + rng.Uniform(kMaxValue - lo); |
163 | 692 | ASSERT_OK(SetRange(lo, hi)); |
164 | 692 | RandomVerifyState(); |
165 | 692 | RandomVerifyCopy(); |
166 | 692 | } |
167 | 10 | } |
168 | 1 | } |
169 | | |
170 | | } // namespace yb |