1 //===- llvm/unittest/ADT/DenseMapMap.cpp - DenseMap unit tests --*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "llvm/ADT/DenseMap.h" 10 #include "llvm/ADT/DenseMapInfo.h" 11 #include "gmock/gmock.h" 12 #include "gtest/gtest.h" 13 #include <map> 14 #include <set> 15 #include <utility> 16 #include <variant> 17 18 using namespace llvm; 19 20 namespace { 21 22 uint32_t getTestKey(int i, uint32_t *) { return i; } 23 uint32_t getTestValue(int i, uint32_t *) { return 42 + i; } 24 25 uint32_t *getTestKey(int i, uint32_t **) { 26 static uint32_t dummy_arr1[8192]; 27 assert(i < 8192 && "Only support 8192 dummy keys."); 28 return &dummy_arr1[i]; 29 } 30 uint32_t *getTestValue(int i, uint32_t **) { 31 static uint32_t dummy_arr1[8192]; 32 assert(i < 8192 && "Only support 8192 dummy keys."); 33 return &dummy_arr1[i]; 34 } 35 36 /// A test class that tries to check that construction and destruction 37 /// occur correctly. 38 class CtorTester { 39 static std::set<CtorTester *> Constructed; 40 int Value; 41 42 public: 43 explicit CtorTester(int Value = 0) : Value(Value) { 44 EXPECT_TRUE(Constructed.insert(this).second); 45 } 46 CtorTester(uint32_t Value) : Value(Value) { 47 EXPECT_TRUE(Constructed.insert(this).second); 48 } 49 CtorTester(const CtorTester &Arg) : Value(Arg.Value) { 50 EXPECT_TRUE(Constructed.insert(this).second); 51 } 52 CtorTester &operator=(const CtorTester &) = default; 53 ~CtorTester() { 54 EXPECT_EQ(1u, Constructed.erase(this)); 55 } 56 operator uint32_t() const { return Value; } 57 58 int getValue() const { return Value; } 59 bool operator==(const CtorTester &RHS) const { return Value == RHS.Value; } 60 }; 61 62 std::set<CtorTester *> CtorTester::Constructed; 63 64 struct CtorTesterMapInfo { 65 static inline CtorTester getEmptyKey() { return CtorTester(-1); } 66 static inline CtorTester getTombstoneKey() { return CtorTester(-2); } 67 static unsigned getHashValue(const CtorTester &Val) { 68 return Val.getValue() * 37u; 69 } 70 static bool isEqual(const CtorTester &LHS, const CtorTester &RHS) { 71 return LHS == RHS; 72 } 73 }; 74 75 CtorTester getTestKey(int i, CtorTester *) { return CtorTester(i); } 76 CtorTester getTestValue(int i, CtorTester *) { return CtorTester(42 + i); } 77 78 // Test fixture, with helper functions implemented by forwarding to global 79 // function overloads selected by component types of the type parameter. This 80 // allows all of the map implementations to be tested with shared 81 // implementations of helper routines. 82 template <typename T> 83 class DenseMapTest : public ::testing::Test { 84 protected: 85 T Map; 86 87 static typename T::key_type *const dummy_key_ptr; 88 static typename T::mapped_type *const dummy_value_ptr; 89 90 typename T::key_type getKey(int i = 0) { 91 return getTestKey(i, dummy_key_ptr); 92 } 93 typename T::mapped_type getValue(int i = 0) { 94 return getTestValue(i, dummy_value_ptr); 95 } 96 }; 97 98 template <typename T> 99 typename T::key_type *const DenseMapTest<T>::dummy_key_ptr = nullptr; 100 template <typename T> 101 typename T::mapped_type *const DenseMapTest<T>::dummy_value_ptr = nullptr; 102 103 // Register these types for testing. 104 typedef ::testing::Types<DenseMap<uint32_t, uint32_t>, 105 DenseMap<uint32_t *, uint32_t *>, 106 DenseMap<CtorTester, CtorTester, CtorTesterMapInfo>, 107 SmallDenseMap<uint32_t, uint32_t>, 108 SmallDenseMap<uint32_t *, uint32_t *>, 109 SmallDenseMap<CtorTester, CtorTester, 4, 110 CtorTesterMapInfo> 111 > DenseMapTestTypes; 112 TYPED_TEST_SUITE(DenseMapTest, DenseMapTestTypes, ); 113 114 // Empty map tests 115 TYPED_TEST(DenseMapTest, EmptyIntMapTest) { 116 // Size tests 117 EXPECT_EQ(0u, this->Map.size()); 118 EXPECT_TRUE(this->Map.empty()); 119 120 // Iterator tests 121 EXPECT_TRUE(this->Map.begin() == this->Map.end()); 122 123 // Lookup tests 124 EXPECT_FALSE(this->Map.count(this->getKey())); 125 EXPECT_TRUE(this->Map.find(this->getKey()) == this->Map.end()); 126 EXPECT_EQ(typename TypeParam::mapped_type(), 127 this->Map.lookup(this->getKey())); 128 } 129 130 // Constant map tests 131 TYPED_TEST(DenseMapTest, ConstEmptyMapTest) { 132 const TypeParam &ConstMap = this->Map; 133 EXPECT_EQ(0u, ConstMap.size()); 134 EXPECT_TRUE(ConstMap.empty()); 135 EXPECT_TRUE(ConstMap.begin() == ConstMap.end()); 136 } 137 138 // A map with a single entry 139 TYPED_TEST(DenseMapTest, SingleEntryMapTest) { 140 this->Map[this->getKey()] = this->getValue(); 141 142 // Size tests 143 EXPECT_EQ(1u, this->Map.size()); 144 EXPECT_FALSE(this->Map.begin() == this->Map.end()); 145 EXPECT_FALSE(this->Map.empty()); 146 147 // Iterator tests 148 typename TypeParam::iterator it = this->Map.begin(); 149 EXPECT_EQ(this->getKey(), it->first); 150 EXPECT_EQ(this->getValue(), it->second); 151 ++it; 152 EXPECT_TRUE(it == this->Map.end()); 153 154 // Lookup tests 155 EXPECT_TRUE(this->Map.count(this->getKey())); 156 EXPECT_TRUE(this->Map.find(this->getKey()) == this->Map.begin()); 157 EXPECT_EQ(this->getValue(), this->Map.lookup(this->getKey())); 158 EXPECT_EQ(this->getValue(), this->Map[this->getKey()]); 159 } 160 161 // Test clear() method 162 TYPED_TEST(DenseMapTest, ClearTest) { 163 this->Map[this->getKey()] = this->getValue(); 164 this->Map.clear(); 165 166 EXPECT_EQ(0u, this->Map.size()); 167 EXPECT_TRUE(this->Map.empty()); 168 EXPECT_TRUE(this->Map.begin() == this->Map.end()); 169 } 170 171 // Test erase(iterator) method 172 TYPED_TEST(DenseMapTest, EraseTest) { 173 this->Map[this->getKey()] = this->getValue(); 174 this->Map.erase(this->Map.begin()); 175 176 EXPECT_EQ(0u, this->Map.size()); 177 EXPECT_TRUE(this->Map.empty()); 178 EXPECT_TRUE(this->Map.begin() == this->Map.end()); 179 } 180 181 // Test erase(value) method 182 TYPED_TEST(DenseMapTest, EraseTest2) { 183 this->Map[this->getKey()] = this->getValue(); 184 this->Map.erase(this->getKey()); 185 186 EXPECT_EQ(0u, this->Map.size()); 187 EXPECT_TRUE(this->Map.empty()); 188 EXPECT_TRUE(this->Map.begin() == this->Map.end()); 189 } 190 191 // Test insert() method 192 TYPED_TEST(DenseMapTest, InsertTest) { 193 this->Map.insert(std::make_pair(this->getKey(), this->getValue())); 194 EXPECT_EQ(1u, this->Map.size()); 195 EXPECT_EQ(this->getValue(), this->Map[this->getKey()]); 196 } 197 198 // Test copy constructor method 199 TYPED_TEST(DenseMapTest, CopyConstructorTest) { 200 this->Map[this->getKey()] = this->getValue(); 201 TypeParam copyMap(this->Map); 202 203 EXPECT_EQ(1u, copyMap.size()); 204 EXPECT_EQ(this->getValue(), copyMap[this->getKey()]); 205 } 206 207 // Test copy constructor method where SmallDenseMap isn't small. 208 TYPED_TEST(DenseMapTest, CopyConstructorNotSmallTest) { 209 for (int Key = 0; Key < 5; ++Key) 210 this->Map[this->getKey(Key)] = this->getValue(Key); 211 TypeParam copyMap(this->Map); 212 213 EXPECT_EQ(5u, copyMap.size()); 214 for (int Key = 0; Key < 5; ++Key) 215 EXPECT_EQ(this->getValue(Key), copyMap[this->getKey(Key)]); 216 } 217 218 // Test copying from a default-constructed map. 219 TYPED_TEST(DenseMapTest, CopyConstructorFromDefaultTest) { 220 TypeParam copyMap(this->Map); 221 222 EXPECT_TRUE(copyMap.empty()); 223 } 224 225 // Test copying from an empty map where SmallDenseMap isn't small. 226 TYPED_TEST(DenseMapTest, CopyConstructorFromEmptyTest) { 227 for (int Key = 0; Key < 5; ++Key) 228 this->Map[this->getKey(Key)] = this->getValue(Key); 229 this->Map.clear(); 230 TypeParam copyMap(this->Map); 231 232 EXPECT_TRUE(copyMap.empty()); 233 } 234 235 // Test assignment operator method 236 TYPED_TEST(DenseMapTest, AssignmentTest) { 237 this->Map[this->getKey()] = this->getValue(); 238 TypeParam copyMap = this->Map; 239 240 EXPECT_EQ(1u, copyMap.size()); 241 EXPECT_EQ(this->getValue(), copyMap[this->getKey()]); 242 243 // test self-assignment. 244 copyMap = static_cast<TypeParam &>(copyMap); 245 EXPECT_EQ(1u, copyMap.size()); 246 EXPECT_EQ(this->getValue(), copyMap[this->getKey()]); 247 } 248 249 TYPED_TEST(DenseMapTest, AssignmentTestNotSmall) { 250 for (int Key = 0; Key < 5; ++Key) 251 this->Map[this->getKey(Key)] = this->getValue(Key); 252 TypeParam copyMap = this->Map; 253 254 EXPECT_EQ(5u, copyMap.size()); 255 for (int Key = 0; Key < 5; ++Key) 256 EXPECT_EQ(this->getValue(Key), copyMap[this->getKey(Key)]); 257 258 // test self-assignment. 259 copyMap = static_cast<TypeParam &>(copyMap); 260 EXPECT_EQ(5u, copyMap.size()); 261 for (int Key = 0; Key < 5; ++Key) 262 EXPECT_EQ(this->getValue(Key), copyMap[this->getKey(Key)]); 263 } 264 265 // Test swap method 266 TYPED_TEST(DenseMapTest, SwapTest) { 267 this->Map[this->getKey()] = this->getValue(); 268 TypeParam otherMap; 269 270 this->Map.swap(otherMap); 271 EXPECT_EQ(0u, this->Map.size()); 272 EXPECT_TRUE(this->Map.empty()); 273 EXPECT_EQ(1u, otherMap.size()); 274 EXPECT_EQ(this->getValue(), otherMap[this->getKey()]); 275 276 this->Map.swap(otherMap); 277 EXPECT_EQ(0u, otherMap.size()); 278 EXPECT_TRUE(otherMap.empty()); 279 EXPECT_EQ(1u, this->Map.size()); 280 EXPECT_EQ(this->getValue(), this->Map[this->getKey()]); 281 282 // Make this more interesting by inserting 100 numbers into the map. 283 for (int i = 0; i < 100; ++i) 284 this->Map[this->getKey(i)] = this->getValue(i); 285 286 this->Map.swap(otherMap); 287 EXPECT_EQ(0u, this->Map.size()); 288 EXPECT_TRUE(this->Map.empty()); 289 EXPECT_EQ(100u, otherMap.size()); 290 for (int i = 0; i < 100; ++i) 291 EXPECT_EQ(this->getValue(i), otherMap[this->getKey(i)]); 292 293 this->Map.swap(otherMap); 294 EXPECT_EQ(0u, otherMap.size()); 295 EXPECT_TRUE(otherMap.empty()); 296 EXPECT_EQ(100u, this->Map.size()); 297 for (int i = 0; i < 100; ++i) 298 EXPECT_EQ(this->getValue(i), this->Map[this->getKey(i)]); 299 } 300 301 // A more complex iteration test 302 TYPED_TEST(DenseMapTest, IterationTest) { 303 bool visited[100]; 304 std::map<typename TypeParam::key_type, unsigned> visitedIndex; 305 306 // Insert 100 numbers into the map 307 for (int i = 0; i < 100; ++i) { 308 visited[i] = false; 309 visitedIndex[this->getKey(i)] = i; 310 311 this->Map[this->getKey(i)] = this->getValue(i); 312 } 313 314 // Iterate over all numbers and mark each one found. 315 for (typename TypeParam::iterator it = this->Map.begin(); 316 it != this->Map.end(); ++it) 317 visited[visitedIndex[it->first]] = true; 318 319 // Ensure every number was visited. 320 for (int i = 0; i < 100; ++i) 321 ASSERT_TRUE(visited[i]) << "Entry #" << i << " was never visited"; 322 } 323 324 // const_iterator test 325 TYPED_TEST(DenseMapTest, ConstIteratorTest) { 326 // Check conversion from iterator to const_iterator. 327 typename TypeParam::iterator it = this->Map.begin(); 328 typename TypeParam::const_iterator cit(it); 329 EXPECT_TRUE(it == cit); 330 331 // Check copying of const_iterators. 332 typename TypeParam::const_iterator cit2(cit); 333 EXPECT_TRUE(cit == cit2); 334 } 335 336 namespace { 337 // Simple class that counts how many moves and copy happens when growing a map 338 struct CountCopyAndMove { 339 static int Move; 340 static int Copy; 341 CountCopyAndMove() {} 342 343 CountCopyAndMove(const CountCopyAndMove &) { Copy++; } 344 CountCopyAndMove &operator=(const CountCopyAndMove &) { 345 Copy++; 346 return *this; 347 } 348 CountCopyAndMove(CountCopyAndMove &&) { Move++; } 349 CountCopyAndMove &operator=(const CountCopyAndMove &&) { 350 Move++; 351 return *this; 352 } 353 }; 354 int CountCopyAndMove::Copy = 0; 355 int CountCopyAndMove::Move = 0; 356 357 } // anonymous namespace 358 359 // Test initializer list construction. 360 TEST(DenseMapCustomTest, InitializerList) { 361 DenseMap<int, int> M({{0, 0}, {0, 1}, {1, 2}}); 362 EXPECT_EQ(2u, M.size()); 363 EXPECT_EQ(1u, M.count(0)); 364 EXPECT_EQ(0, M[0]); 365 EXPECT_EQ(1u, M.count(1)); 366 EXPECT_EQ(2, M[1]); 367 } 368 369 // Test initializer list construction. 370 TEST(DenseMapCustomTest, EqualityComparison) { 371 DenseMap<int, int> M1({{0, 0}, {1, 2}}); 372 DenseMap<int, int> M2({{0, 0}, {1, 2}}); 373 DenseMap<int, int> M3({{0, 0}, {1, 3}}); 374 375 EXPECT_EQ(M1, M2); 376 EXPECT_NE(M1, M3); 377 } 378 379 // Test for the default minimum size of a DenseMap 380 TEST(DenseMapCustomTest, DefaultMinReservedSizeTest) { 381 // IF THIS VALUE CHANGE, please update InitialSizeTest, InitFromIterator, and 382 // ReserveTest as well! 383 const int ExpectedInitialBucketCount = 64; 384 // Formula from DenseMap::getMinBucketToReserveForEntries() 385 const int ExpectedMaxInitialEntries = ExpectedInitialBucketCount * 3 / 4 - 1; 386 387 DenseMap<int, CountCopyAndMove> Map; 388 // Will allocate 64 buckets 389 Map.reserve(1); 390 unsigned MemorySize = Map.getMemorySize(); 391 CountCopyAndMove::Copy = 0; 392 CountCopyAndMove::Move = 0; 393 for (int i = 0; i < ExpectedMaxInitialEntries; ++i) 394 Map.insert(std::pair<int, CountCopyAndMove>(std::piecewise_construct, 395 std::forward_as_tuple(i), 396 std::forward_as_tuple())); 397 // Check that we didn't grow 398 EXPECT_EQ(MemorySize, Map.getMemorySize()); 399 // Check that move was called the expected number of times 400 EXPECT_EQ(ExpectedMaxInitialEntries, CountCopyAndMove::Move); 401 // Check that no copy occurred 402 EXPECT_EQ(0, CountCopyAndMove::Copy); 403 404 // Adding one extra element should grow the map 405 Map.insert(std::pair<int, CountCopyAndMove>( 406 std::piecewise_construct, 407 std::forward_as_tuple(ExpectedMaxInitialEntries), 408 std::forward_as_tuple())); 409 // Check that we grew 410 EXPECT_NE(MemorySize, Map.getMemorySize()); 411 // Check that move was called the expected number of times 412 // This relies on move-construction elision, and cannot be reliably tested. 413 // EXPECT_EQ(ExpectedMaxInitialEntries + 2, CountCopyAndMove::Move); 414 // Check that no copy occurred 415 EXPECT_EQ(0, CountCopyAndMove::Copy); 416 } 417 418 // Make sure creating the map with an initial size of N actually gives us enough 419 // buckets to insert N items without increasing allocation size. 420 TEST(DenseMapCustomTest, InitialSizeTest) { 421 // Test a few different sizes, 48 is *not* a random choice: we need a value 422 // that is 2/3 of a power of two to stress the grow() condition, and the power 423 // of two has to be at least 64 because of minimum size allocation in the 424 // DenseMap (see DefaultMinReservedSizeTest). 66 is a value just above the 425 // 64 default init. 426 for (auto Size : {1, 2, 48, 66}) { 427 DenseMap<int, CountCopyAndMove> Map(Size); 428 unsigned MemorySize = Map.getMemorySize(); 429 CountCopyAndMove::Copy = 0; 430 CountCopyAndMove::Move = 0; 431 for (int i = 0; i < Size; ++i) 432 Map.insert(std::pair<int, CountCopyAndMove>(std::piecewise_construct, 433 std::forward_as_tuple(i), 434 std::forward_as_tuple())); 435 // Check that we didn't grow 436 EXPECT_EQ(MemorySize, Map.getMemorySize()); 437 // Check that move was called the expected number of times 438 EXPECT_EQ(Size, CountCopyAndMove::Move); 439 // Check that no copy occurred 440 EXPECT_EQ(0, CountCopyAndMove::Copy); 441 } 442 } 443 444 // Make sure creating the map with a iterator range does not trigger grow() 445 TEST(DenseMapCustomTest, InitFromIterator) { 446 std::vector<std::pair<int, CountCopyAndMove>> Values; 447 // The size is a random value greater than 64 (hardcoded DenseMap min init) 448 const int Count = 65; 449 for (int i = 0; i < Count; i++) 450 Values.emplace_back(i, CountCopyAndMove()); 451 452 CountCopyAndMove::Move = 0; 453 CountCopyAndMove::Copy = 0; 454 DenseMap<int, CountCopyAndMove> Map(Values.begin(), Values.end()); 455 // Check that no move occurred 456 EXPECT_EQ(0, CountCopyAndMove::Move); 457 // Check that copy was called the expected number of times 458 EXPECT_EQ(Count, CountCopyAndMove::Copy); 459 } 460 461 // Make sure reserve actually gives us enough buckets to insert N items 462 // without increasing allocation size. 463 TEST(DenseMapCustomTest, ReserveTest) { 464 // Test a few different size, 48 is *not* a random choice: we need a value 465 // that is 2/3 of a power of two to stress the grow() condition, and the power 466 // of two has to be at least 64 because of minimum size allocation in the 467 // DenseMap (see DefaultMinReservedSizeTest). 66 is a value just above the 468 // 64 default init. 469 for (auto Size : {1, 2, 48, 66}) { 470 DenseMap<int, CountCopyAndMove> Map; 471 Map.reserve(Size); 472 unsigned MemorySize = Map.getMemorySize(); 473 CountCopyAndMove::Copy = 0; 474 CountCopyAndMove::Move = 0; 475 for (int i = 0; i < Size; ++i) 476 Map.insert(std::pair<int, CountCopyAndMove>(std::piecewise_construct, 477 std::forward_as_tuple(i), 478 std::forward_as_tuple())); 479 // Check that we didn't grow 480 EXPECT_EQ(MemorySize, Map.getMemorySize()); 481 // Check that move was called the expected number of times 482 EXPECT_EQ(Size, CountCopyAndMove::Move); 483 // Check that no copy occurred 484 EXPECT_EQ(0, CountCopyAndMove::Copy); 485 } 486 } 487 488 // Make sure DenseMap works with StringRef keys. 489 TEST(DenseMapCustomTest, StringRefTest) { 490 DenseMap<StringRef, int> M; 491 492 M["a"] = 1; 493 M["b"] = 2; 494 M["c"] = 3; 495 496 EXPECT_EQ(3u, M.size()); 497 EXPECT_EQ(1, M.lookup("a")); 498 EXPECT_EQ(2, M.lookup("b")); 499 EXPECT_EQ(3, M.lookup("c")); 500 501 EXPECT_EQ(0, M.lookup("q")); 502 503 // Test the empty string, spelled various ways. 504 EXPECT_EQ(0, M.lookup("")); 505 EXPECT_EQ(0, M.lookup(StringRef())); 506 EXPECT_EQ(0, M.lookup(StringRef("a", 0))); 507 M[""] = 42; 508 EXPECT_EQ(42, M.lookup("")); 509 EXPECT_EQ(42, M.lookup(StringRef())); 510 EXPECT_EQ(42, M.lookup(StringRef("a", 0))); 511 } 512 513 // Key traits that allows lookup with either an unsigned or char* key; 514 // In the latter case, "a" == 0, "b" == 1 and so on. 515 struct TestDenseMapInfo { 516 static inline unsigned getEmptyKey() { return ~0; } 517 static inline unsigned getTombstoneKey() { return ~0U - 1; } 518 static unsigned getHashValue(const unsigned& Val) { return Val * 37U; } 519 static unsigned getHashValue(const char* Val) { 520 return (unsigned)(Val[0] - 'a') * 37U; 521 } 522 static bool isEqual(const unsigned& LHS, const unsigned& RHS) { 523 return LHS == RHS; 524 } 525 static bool isEqual(const char* LHS, const unsigned& RHS) { 526 return (unsigned)(LHS[0] - 'a') == RHS; 527 } 528 }; 529 530 // find_as() tests 531 TEST(DenseMapCustomTest, FindAsTest) { 532 DenseMap<unsigned, unsigned, TestDenseMapInfo> map; 533 map[0] = 1; 534 map[1] = 2; 535 map[2] = 3; 536 537 // Size tests 538 EXPECT_EQ(3u, map.size()); 539 540 // Normal lookup tests 541 EXPECT_EQ(1u, map.count(1)); 542 EXPECT_EQ(1u, map.find(0)->second); 543 EXPECT_EQ(2u, map.find(1)->second); 544 EXPECT_EQ(3u, map.find(2)->second); 545 EXPECT_TRUE(map.find(3) == map.end()); 546 547 // find_as() tests 548 EXPECT_EQ(1u, map.find_as("a")->second); 549 EXPECT_EQ(2u, map.find_as("b")->second); 550 EXPECT_EQ(3u, map.find_as("c")->second); 551 EXPECT_TRUE(map.find_as("d") == map.end()); 552 } 553 554 TEST(DenseMapCustomTest, SmallDenseMapInitializerList) { 555 SmallDenseMap<int, int> M = {{0, 0}, {0, 1}, {1, 2}}; 556 EXPECT_EQ(2u, M.size()); 557 EXPECT_EQ(1u, M.count(0)); 558 EXPECT_EQ(0, M[0]); 559 EXPECT_EQ(1u, M.count(1)); 560 EXPECT_EQ(2, M[1]); 561 } 562 563 struct ContiguousDenseMapInfo { 564 static inline unsigned getEmptyKey() { return ~0; } 565 static inline unsigned getTombstoneKey() { return ~0U - 1; } 566 static unsigned getHashValue(const unsigned& Val) { return Val; } 567 static bool isEqual(const unsigned& LHS, const unsigned& RHS) { 568 return LHS == RHS; 569 } 570 }; 571 572 // Test that filling a small dense map with exactly the number of elements in 573 // the map grows to have enough space for an empty bucket. 574 TEST(DenseMapCustomTest, SmallDenseMapGrowTest) { 575 SmallDenseMap<unsigned, unsigned, 32, ContiguousDenseMapInfo> map; 576 // Add some number of elements, then delete a few to leave us some tombstones. 577 // If we just filled the map with 32 elements we'd grow because of not enough 578 // tombstones which masks the issue here. 579 for (unsigned i = 0; i < 20; ++i) 580 map[i] = i + 1; 581 for (unsigned i = 0; i < 10; ++i) 582 map.erase(i); 583 for (unsigned i = 20; i < 32; ++i) 584 map[i] = i + 1; 585 586 // Size tests 587 EXPECT_EQ(22u, map.size()); 588 589 // Try to find an element which doesn't exist. There was a bug in 590 // SmallDenseMap which led to a map with num elements == small capacity not 591 // having an empty bucket any more. Finding an element not in the map would 592 // therefore never terminate. 593 EXPECT_TRUE(map.find(32) == map.end()); 594 } 595 596 TEST(DenseMapCustomTest, LargeSmallDenseMapCompaction) { 597 SmallDenseMap<unsigned, unsigned, 128, ContiguousDenseMapInfo> map; 598 // Fill to < 3/4 load. 599 for (unsigned i = 0; i < 95; ++i) 600 map[i] = i; 601 // And erase, leaving behind tombstones. 602 for (unsigned i = 0; i < 95; ++i) 603 map.erase(i); 604 // Fill further, so that less than 1/8 are empty, but still below 3/4 load. 605 for (unsigned i = 95; i < 128; ++i) 606 map[i] = i; 607 608 EXPECT_EQ(33u, map.size()); 609 // Similar to the previous test, check for a non-existing element, as an 610 // indirect check that tombstones have been removed. 611 EXPECT_TRUE(map.find(0) == map.end()); 612 } 613 614 TEST(DenseMapCustomTest, SmallDenseMapWithNumBucketsNonPowerOf2) { 615 // Is not power of 2. 616 const unsigned NumInitBuckets = 33; 617 // Power of 2 less then NumInitBuckets. 618 constexpr unsigned InlineBuckets = 4; 619 // Constructor should not trigger assert. 620 SmallDenseMap<int, int, InlineBuckets> map(NumInitBuckets); 621 } 622 623 TEST(DenseMapCustomTest, TryEmplaceTest) { 624 DenseMap<int, std::unique_ptr<int>> Map; 625 std::unique_ptr<int> P(new int(2)); 626 auto Try1 = Map.try_emplace(0, new int(1)); 627 EXPECT_TRUE(Try1.second); 628 auto Try2 = Map.try_emplace(0, std::move(P)); 629 EXPECT_FALSE(Try2.second); 630 EXPECT_EQ(Try1.first, Try2.first); 631 EXPECT_NE(nullptr, P); 632 } 633 634 TEST(DenseMapCustomTest, ConstTest) { 635 // Test that const pointers work okay for count and find, even when the 636 // underlying map is a non-const pointer. 637 DenseMap<int *, int> Map; 638 int A; 639 int *B = &A; 640 const int *C = &A; 641 Map.insert({B, 0}); 642 EXPECT_EQ(Map.count(B), 1u); 643 EXPECT_EQ(Map.count(C), 1u); 644 EXPECT_NE(Map.find(B), Map.end()); 645 EXPECT_NE(Map.find(C), Map.end()); 646 } 647 648 struct IncompleteStruct; 649 650 TEST(DenseMapCustomTest, OpaquePointerKey) { 651 // Test that we can use a pointer to an incomplete type as a DenseMap key. 652 // This is an important build time optimization, since many classes have 653 // DenseMap members. 654 DenseMap<IncompleteStruct *, int> Map; 655 int Keys[3] = {0, 0, 0}; 656 IncompleteStruct *K1 = reinterpret_cast<IncompleteStruct *>(&Keys[0]); 657 IncompleteStruct *K2 = reinterpret_cast<IncompleteStruct *>(&Keys[1]); 658 IncompleteStruct *K3 = reinterpret_cast<IncompleteStruct *>(&Keys[2]); 659 Map.insert({K1, 1}); 660 Map.insert({K2, 2}); 661 Map.insert({K3, 3}); 662 EXPECT_EQ(Map.count(K1), 1u); 663 EXPECT_EQ(Map[K1], 1); 664 EXPECT_EQ(Map[K2], 2); 665 EXPECT_EQ(Map[K3], 3); 666 Map.clear(); 667 EXPECT_EQ(Map.find(K1), Map.end()); 668 EXPECT_EQ(Map.find(K2), Map.end()); 669 EXPECT_EQ(Map.find(K3), Map.end()); 670 } 671 } // namespace 672 673 namespace { 674 struct A { 675 A(int value) : value(value) {} 676 int value; 677 }; 678 struct B : public A { 679 using A::A; 680 }; 681 } // namespace 682 683 namespace llvm { 684 template <typename T> 685 struct DenseMapInfo<T, std::enable_if_t<std::is_base_of_v<A, T>>> { 686 static inline T getEmptyKey() { return {static_cast<int>(~0)}; } 687 static inline T getTombstoneKey() { return {static_cast<int>(~0U - 1)}; } 688 static unsigned getHashValue(const T &Val) { return Val.value; } 689 static bool isEqual(const T &LHS, const T &RHS) { 690 return LHS.value == RHS.value; 691 } 692 }; 693 } // namespace llvm 694 695 namespace { 696 TEST(DenseMapCustomTest, SFINAEMapInfo) { 697 // Test that we can use a pointer to an incomplete type as a DenseMap key. 698 // This is an important build time optimization, since many classes have 699 // DenseMap members. 700 DenseMap<B, int> Map; 701 B Keys[3] = {{0}, {1}, {2}}; 702 Map.insert({Keys[0], 1}); 703 Map.insert({Keys[1], 2}); 704 Map.insert({Keys[2], 3}); 705 EXPECT_EQ(Map.count(Keys[0]), 1u); 706 EXPECT_EQ(Map[Keys[0]], 1); 707 EXPECT_EQ(Map[Keys[1]], 2); 708 EXPECT_EQ(Map[Keys[2]], 3); 709 Map.clear(); 710 EXPECT_EQ(Map.find(Keys[0]), Map.end()); 711 EXPECT_EQ(Map.find(Keys[1]), Map.end()); 712 EXPECT_EQ(Map.find(Keys[2]), Map.end()); 713 } 714 715 TEST(DenseMapCustomTest, VariantSupport) { 716 using variant = std::variant<int, int>; 717 DenseMap<variant, int> Map; 718 variant Keys[] = { 719 variant(std::in_place_index<0>, 1), 720 variant(std::in_place_index<1>, 1), 721 }; 722 Map.try_emplace(Keys[0], 0); 723 Map.try_emplace(Keys[1], 1); 724 EXPECT_THAT(Map, testing::SizeIs(2)); 725 EXPECT_NE(DenseMapInfo<variant>::getHashValue(Keys[0]), 726 DenseMapInfo<variant>::getHashValue(Keys[1])); 727 } 728 } // namespace 729