| 1 | /* Copyright (c) 2020, 2026, Oracle and/or its affiliates. |
| 2 | |
| 3 | This program is free software; you can redistribute it and/or modify |
| 4 | it under the terms of the GNU General Public License, version 2.0, |
| 5 | as published by the Free Software Foundation. |
| 6 | |
| 7 | This program is designed to work with certain software (including |
| 8 | but not limited to OpenSSL) that is licensed under separate terms, |
| 9 | as designated in a particular file or component or in included license |
| 10 | documentation. The authors of MySQL hereby grant you an additional |
| 11 | permission to link the program and your derivative works with the |
| 12 | separately licensed software that they have either included with |
| 13 | the program or referenced in the documentation. |
| 14 | |
| 15 | This program is distributed in the hope that it will be useful, |
| 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | GNU General Public License, version 2.0, for more details. |
| 19 | |
| 20 | You should have received a copy of the GNU General Public License |
| 21 | along with this program; if not, write to the Free Software |
| 22 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ |
| 23 | |
| 24 | #include <gmock/gmock.h> |
| 25 | #include <memory> |
| 26 | #include <vector> |
| 27 | #include "my_alloc.h" |
| 28 | #include "sql/join_optimizer/overflow_bitset.h" |
| 29 | |
| 30 | using std::vector; |
| 31 | |
| 32 | TEST(OverflowBitsetTest, ZeroInitialize) { |
| 33 | OverflowBitset s; |
| 34 | if (sizeof(void *) == 8) { |
| 35 | EXPECT_EQ(63, s.capacity()); |
| 36 | } else { |
| 37 | EXPECT_EQ(31, s.capacity()); |
| 38 | } |
| 39 | for (unsigned i = 0; i < s.capacity(); ++i) { |
| 40 | EXPECT_FALSE(IsBitSet(i, s)); |
| 41 | } |
| 42 | EXPECT_TRUE(s.is_inline()); |
| 43 | } |
| 44 | |
| 45 | TEST(OverflowBitsetTest, InitializeFromInt) { |
| 46 | OverflowBitset s{0x7eadbeef}; |
| 47 | for (int i = 0; i < 31; ++i) { |
| 48 | EXPECT_EQ(IsBitSet(i, s), IsBitSet(i, 0x7eadbeef)); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | TEST(OverflowBitsetTest, TrivialCopy) { |
| 53 | OverflowBitset s{0x7eadbeef}; |
| 54 | OverflowBitset t(s); |
| 55 | for (int i = 0; i < 31; ++i) { |
| 56 | EXPECT_EQ(IsBitSet(i, s), IsBitSet(i, t)); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | TEST(OverflowBitsetTest, MutateInline) { |
| 61 | MEM_ROOT mem_root; |
| 62 | MutableOverflowBitset s{&mem_root, 30}; |
| 63 | for (int i = 0; i < 30; ++i) { |
| 64 | if (i % 3 == 0) { |
| 65 | s.SetBit(i); |
| 66 | } |
| 67 | } |
| 68 | s.ClearBits(2, 9); |
| 69 | s.ClearBit(27); |
| 70 | |
| 71 | OverflowBitset cs = std::move(s); |
| 72 | for (int i = 0; i < 30; ++i) { |
| 73 | SCOPED_TRACE(i); |
| 74 | EXPECT_EQ(i % 3 == 0 && (i < 2 || i >= 9) && (i != 27), IsBitSet(i, cs)); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | TEST(OverflowBitsetTest, MutateOverflow) { |
| 79 | MEM_ROOT mem_root; |
| 80 | MutableOverflowBitset s{&mem_root, 200}; |
| 81 | for (int i = 0; i < 200; ++i) { |
| 82 | if (i % 3 == 0) { |
| 83 | s.SetBit(i); |
| 84 | } |
| 85 | } |
| 86 | s.ClearBits(2, 9); |
| 87 | s.ClearBits(60, 150); |
| 88 | s.ClearBit(42); |
| 89 | |
| 90 | OverflowBitset cs = std::move(s); |
| 91 | for (int i = 0; i < 200; ++i) { |
| 92 | SCOPED_TRACE(i); |
| 93 | EXPECT_EQ( |
| 94 | i % 3 == 0 && (i < 2 || i >= 9) && (i < 60 || i >= 150) && (i != 42), |
| 95 | IsBitSet(i, cs)); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | TEST(OverflowBitsetTest, AndOrXor) { // Also tests Clone. |
| 100 | for (int size : {63, 64, 200}) { |
| 101 | SCOPED_TRACE(size); |
| 102 | |
| 103 | MEM_ROOT mem_root; |
| 104 | MutableOverflowBitset s1{&mem_root, static_cast<size_t>(size)}; |
| 105 | MutableOverflowBitset s2{&mem_root, static_cast<size_t>(size)}; |
| 106 | for (int i = 0; i < size; ++i) { |
| 107 | if (i % 3 == 0) { |
| 108 | s1.SetBit(i); |
| 109 | } |
| 110 | if (i % 5 == 0) { |
| 111 | s2.SetBit(i); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | OverflowBitset ors = |
| 116 | OverflowBitset::Or(&mem_root, s1.Clone(&mem_root), s2.Clone(&mem_root)); |
| 117 | OverflowBitset ands = OverflowBitset::And(&mem_root, s1.Clone(&mem_root), |
| 118 | s2.Clone(&mem_root)); |
| 119 | OverflowBitset xors = OverflowBitset::Xor(&mem_root, s1.Clone(&mem_root), |
| 120 | s2.Clone(&mem_root)); |
| 121 | |
| 122 | for (int i = 0; i < size; ++i) { |
| 123 | const bool v1 = (i % 3 == 0); |
| 124 | const bool v2 = (i % 5 == 0); |
| 125 | |
| 126 | SCOPED_TRACE(i); |
| 127 | EXPECT_EQ(v1 | v2, IsBitSet(i, ors)); |
| 128 | EXPECT_EQ(v1 & v2, IsBitSet(i, ands)); |
| 129 | EXPECT_EQ(v1 ^ v2, IsBitSet(i, xors)); |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | TEST(OverflowBitsetTest, BitsSetInInline) { |
| 135 | OverflowBitset s{0x1005}; |
| 136 | vector<int> ret; |
| 137 | for (int bit_num : BitsSetIn(s)) { |
| 138 | ret.push_back(bit_num); |
| 139 | } |
| 140 | EXPECT_THAT(ret, testing::ElementsAre(0, 2, 12)); |
| 141 | } |
| 142 | |
| 143 | TEST(OverflowBitsetTest, BitsSetInOverflow) { |
| 144 | MEM_ROOT mem_root; |
| 145 | MutableOverflowBitset s{&mem_root, 200}; |
| 146 | s.SetBit(100); |
| 147 | s.SetBit(180); |
| 148 | s.SetBit(181); |
| 149 | s.SetBit(199); |
| 150 | |
| 151 | vector<int> ret; |
| 152 | for (int bit_num : BitsSetIn(std::move(s))) { |
| 153 | ret.push_back(bit_num); |
| 154 | } |
| 155 | EXPECT_THAT(ret, testing::ElementsAre(100, 180, 181, 199)); |
| 156 | } |
| 157 | |
| 158 | TEST(OverflowBitsetTest, BitsSetInBothInline) { |
| 159 | OverflowBitset s{0x1005}, t{0x1204}; |
| 160 | vector<int> ret; |
| 161 | for (int bit_num : BitsSetInBoth(s, t)) { |
| 162 | ret.push_back(bit_num); |
| 163 | } |
| 164 | EXPECT_THAT(ret, testing::ElementsAre(2, 12)); |
| 165 | } |
| 166 | |
| 167 | TEST(OverflowBitsetTest, BitsSetInBothOverflow) { |
| 168 | MEM_ROOT mem_root; |
| 169 | MutableOverflowBitset s{&mem_root, 200}; |
| 170 | s.SetBit(100); |
| 171 | s.SetBit(180); |
| 172 | s.SetBit(181); |
| 173 | s.SetBit(199); |
| 174 | MutableOverflowBitset t{&mem_root, 200}; |
| 175 | t.SetBit(100); |
| 176 | t.SetBit(181); |
| 177 | |
| 178 | vector<int> ret; |
| 179 | for (int bit_num : BitsSetInBoth(std::move(s), std::move(t))) { |
| 180 | ret.push_back(bit_num); |
| 181 | } |
| 182 | EXPECT_THAT(ret, testing::ElementsAre(100, 181)); |
| 183 | } |
| 184 | |
| 185 | TEST(OverflowBitsetTest, OverlapsInline) { |
| 186 | OverflowBitset s1{0x1005}; |
| 187 | OverflowBitset s2{0x0150}; |
| 188 | OverflowBitset s3{0xffff}; |
| 189 | |
| 190 | EXPECT_FALSE(Overlaps(s1, s2)); |
| 191 | EXPECT_TRUE(Overlaps(s2, s3)); |
| 192 | EXPECT_TRUE(Overlaps(s1, s3)); |
| 193 | |
| 194 | // Nothing overlaps with the empty set. |
| 195 | OverflowBitset s4{0}; |
| 196 | EXPECT_FALSE(Overlaps(s1, s4)); |
| 197 | EXPECT_FALSE(Overlaps(s2, s4)); |
| 198 | EXPECT_FALSE(Overlaps(s3, s4)); |
| 199 | EXPECT_FALSE(Overlaps(s4, s1)); |
| 200 | EXPECT_FALSE(Overlaps(s4, s2)); |
| 201 | EXPECT_FALSE(Overlaps(s4, s3)); |
| 202 | } |
| 203 | |
| 204 | TEST(OverflowBitsetTest, OverlapsOverflow) { |
| 205 | MEM_ROOT mem_root; |
| 206 | MutableOverflowBitset s1_tmp{&mem_root, 200}; |
| 207 | MutableOverflowBitset s2_tmp{&mem_root, 200}; |
| 208 | MutableOverflowBitset s3_tmp{&mem_root, 200}; |
| 209 | |
| 210 | s1_tmp.SetBit(1); |
| 211 | s1_tmp.SetBit(100); |
| 212 | s2_tmp.SetBit(60); |
| 213 | s2_tmp.SetBit(160); |
| 214 | s3_tmp.SetBit(1); |
| 215 | s3_tmp.SetBit(160); |
| 216 | |
| 217 | OverflowBitset s1 = std::move(s1_tmp); |
| 218 | OverflowBitset s2 = std::move(s2_tmp); |
| 219 | OverflowBitset s3 = std::move(s3_tmp); |
| 220 | EXPECT_FALSE(Overlaps(s1, s2)); |
| 221 | EXPECT_TRUE(Overlaps(s2, s3)); |
| 222 | EXPECT_TRUE(Overlaps(s1, s3)); |
| 223 | } |
| 224 | |
| 225 | TEST(OverflowBitsetTest, IsSubsetInline) { |
| 226 | OverflowBitset s1{0x1005}; |
| 227 | OverflowBitset s2{0x0150}; |
| 228 | OverflowBitset s3{0xffff}; |
| 229 | |
| 230 | EXPECT_TRUE(IsSubset(s1, s1)); |
| 231 | EXPECT_FALSE(IsSubset(s1, s2)); |
| 232 | EXPECT_TRUE(IsSubset(s1, s3)); |
| 233 | |
| 234 | EXPECT_FALSE(IsSubset(s2, s1)); |
| 235 | EXPECT_TRUE(IsSubset(s2, s2)); |
| 236 | EXPECT_TRUE(IsSubset(s2, s3)); |
| 237 | |
| 238 | EXPECT_FALSE(IsSubset(s3, s1)); |
| 239 | EXPECT_FALSE(IsSubset(s3, s2)); |
| 240 | EXPECT_TRUE(IsSubset(s3, s3)); |
| 241 | } |
| 242 | |
| 243 | TEST(OverflowBitsetTest, IsSubsetOverflow) { |
| 244 | MEM_ROOT mem_root; |
| 245 | MutableOverflowBitset s1_tmp{&mem_root, 200}; |
| 246 | MutableOverflowBitset s2_tmp{&mem_root, 200}; |
| 247 | MutableOverflowBitset s3_tmp{&mem_root, 200}; |
| 248 | |
| 249 | s1_tmp.SetBit(1); |
| 250 | s1_tmp.SetBit(100); |
| 251 | |
| 252 | s2_tmp.SetBit(60); |
| 253 | s2_tmp.SetBit(160); |
| 254 | |
| 255 | s3_tmp.SetBit(1); |
| 256 | s3_tmp.SetBit(60); |
| 257 | s3_tmp.SetBit(100); |
| 258 | s3_tmp.SetBit(160); |
| 259 | |
| 260 | OverflowBitset s1 = std::move(s1_tmp); |
| 261 | OverflowBitset s2 = std::move(s2_tmp); |
| 262 | OverflowBitset s3 = std::move(s3_tmp); |
| 263 | |
| 264 | EXPECT_TRUE(IsSubset(s1, s1)); |
| 265 | EXPECT_FALSE(IsSubset(s1, s2)); |
| 266 | EXPECT_TRUE(IsSubset(s1, s3)); |
| 267 | |
| 268 | EXPECT_FALSE(IsSubset(s2, s1)); |
| 269 | EXPECT_TRUE(IsSubset(s2, s2)); |
| 270 | EXPECT_TRUE(IsSubset(s2, s3)); |
| 271 | |
| 272 | EXPECT_FALSE(IsSubset(s3, s1)); |
| 273 | EXPECT_FALSE(IsSubset(s3, s2)); |
| 274 | EXPECT_TRUE(IsSubset(s3, s3)); |
| 275 | } |
| 276 | |
| 277 | TEST(OverflowBitsetTest, IsEmptyInline) { |
| 278 | EXPECT_TRUE(IsEmpty(OverflowBitset{})); |
| 279 | EXPECT_TRUE(IsEmpty(OverflowBitset{0})); |
| 280 | EXPECT_FALSE(IsEmpty(OverflowBitset{1})); |
| 281 | } |
| 282 | |
| 283 | TEST(OverflowBitsetTest, IsEmptyOverflow) { |
| 284 | MEM_ROOT mem_root; |
| 285 | MutableOverflowBitset s1{&mem_root, 200}; |
| 286 | EXPECT_TRUE(IsEmpty(std::move(s1))); |
| 287 | |
| 288 | MutableOverflowBitset s2{&mem_root, 200}; |
| 289 | s2.SetBit(186); |
| 290 | EXPECT_FALSE(IsEmpty(std::move(s2))); |
| 291 | } |
| 292 | |
| 293 | TEST(OverflowBitsetTest, PopulationCountInline) { |
| 294 | MEM_ROOT mem_root; |
| 295 | MutableOverflowBitset s{&mem_root, 30}; |
| 296 | for (int i = 0; i < 30; ++i) { |
| 297 | if (i % 3 == 0) { |
| 298 | s.SetBit(i); |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | EXPECT_EQ(10, PopulationCount(std::move(s))); |
| 303 | } |
| 304 | |
| 305 | TEST(OverflowBitsetTest, PopulationCountOverflow) { |
| 306 | MEM_ROOT mem_root; |
| 307 | MutableOverflowBitset s{&mem_root, 200}; |
| 308 | for (int i = 0; i < 200; ++i) { |
| 309 | if (i % 3 == 0) { |
| 310 | s.SetBit(i); |
| 311 | } |
| 312 | } |
| 313 | EXPECT_EQ(67, PopulationCount(std::move(s))); |
| 314 | } |