1 //===- llvm/unittest/ADT/StringMapMap.cpp - StringMap unit tests ----------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "llvm/ADT/StringMap.h" 11 #include "llvm/ADT/Twine.h" 12 #include "llvm/Support/DataTypes.h" 13 #include "gtest/gtest.h" 14 #include <tuple> 15 using namespace llvm; 16 17 namespace { 18 19 // Test fixture 20 class StringMapTest : public testing::Test { 21 protected: 22 StringMap<uint32_t> testMap; 23 24 static const char testKey[]; 25 static const uint32_t testValue; 26 static const char* testKeyFirst; 27 static size_t testKeyLength; 28 static const std::string testKeyStr; 29 30 void assertEmptyMap() { 31 // Size tests 32 EXPECT_EQ(0u, testMap.size()); 33 EXPECT_TRUE(testMap.empty()); 34 35 // Iterator tests 36 EXPECT_TRUE(testMap.begin() == testMap.end()); 37 38 // Lookup tests 39 EXPECT_EQ(0u, testMap.count(testKey)); 40 EXPECT_EQ(0u, testMap.count(StringRef(testKeyFirst, testKeyLength))); 41 EXPECT_EQ(0u, testMap.count(testKeyStr)); 42 EXPECT_TRUE(testMap.find(testKey) == testMap.end()); 43 EXPECT_TRUE(testMap.find(StringRef(testKeyFirst, testKeyLength)) == 44 testMap.end()); 45 EXPECT_TRUE(testMap.find(testKeyStr) == testMap.end()); 46 } 47 48 void assertSingleItemMap() { 49 // Size tests 50 EXPECT_EQ(1u, testMap.size()); 51 EXPECT_FALSE(testMap.begin() == testMap.end()); 52 EXPECT_FALSE(testMap.empty()); 53 54 // Iterator tests 55 StringMap<uint32_t>::iterator it = testMap.begin(); 56 EXPECT_STREQ(testKey, it->first().data()); 57 EXPECT_EQ(testValue, it->second); 58 ++it; 59 EXPECT_TRUE(it == testMap.end()); 60 61 // Lookup tests 62 EXPECT_EQ(1u, testMap.count(testKey)); 63 EXPECT_EQ(1u, testMap.count(StringRef(testKeyFirst, testKeyLength))); 64 EXPECT_EQ(1u, testMap.count(testKeyStr)); 65 EXPECT_TRUE(testMap.find(testKey) == testMap.begin()); 66 EXPECT_TRUE(testMap.find(StringRef(testKeyFirst, testKeyLength)) == 67 testMap.begin()); 68 EXPECT_TRUE(testMap.find(testKeyStr) == testMap.begin()); 69 } 70 }; 71 72 const char StringMapTest::testKey[] = "key"; 73 const uint32_t StringMapTest::testValue = 1u; 74 const char* StringMapTest::testKeyFirst = testKey; 75 size_t StringMapTest::testKeyLength = sizeof(testKey) - 1; 76 const std::string StringMapTest::testKeyStr(testKey); 77 78 // Empty map tests. 79 TEST_F(StringMapTest, EmptyMapTest) { 80 assertEmptyMap(); 81 } 82 83 // Constant map tests. 84 TEST_F(StringMapTest, ConstEmptyMapTest) { 85 const StringMap<uint32_t>& constTestMap = testMap; 86 87 // Size tests 88 EXPECT_EQ(0u, constTestMap.size()); 89 EXPECT_TRUE(constTestMap.empty()); 90 91 // Iterator tests 92 EXPECT_TRUE(constTestMap.begin() == constTestMap.end()); 93 94 // Lookup tests 95 EXPECT_EQ(0u, constTestMap.count(testKey)); 96 EXPECT_EQ(0u, constTestMap.count(StringRef(testKeyFirst, testKeyLength))); 97 EXPECT_EQ(0u, constTestMap.count(testKeyStr)); 98 EXPECT_TRUE(constTestMap.find(testKey) == constTestMap.end()); 99 EXPECT_TRUE(constTestMap.find(StringRef(testKeyFirst, testKeyLength)) == 100 constTestMap.end()); 101 EXPECT_TRUE(constTestMap.find(testKeyStr) == constTestMap.end()); 102 } 103 104 // A map with a single entry. 105 TEST_F(StringMapTest, SingleEntryMapTest) { 106 testMap[testKey] = testValue; 107 assertSingleItemMap(); 108 } 109 110 // Test clear() method. 111 TEST_F(StringMapTest, ClearTest) { 112 testMap[testKey] = testValue; 113 testMap.clear(); 114 assertEmptyMap(); 115 } 116 117 // Test erase(iterator) method. 118 TEST_F(StringMapTest, EraseIteratorTest) { 119 testMap[testKey] = testValue; 120 testMap.erase(testMap.begin()); 121 assertEmptyMap(); 122 } 123 124 // Test erase(value) method. 125 TEST_F(StringMapTest, EraseValueTest) { 126 testMap[testKey] = testValue; 127 testMap.erase(testKey); 128 assertEmptyMap(); 129 } 130 131 // Test inserting two values and erasing one. 132 TEST_F(StringMapTest, InsertAndEraseTest) { 133 testMap[testKey] = testValue; 134 testMap["otherKey"] = 2; 135 testMap.erase("otherKey"); 136 assertSingleItemMap(); 137 } 138 139 TEST_F(StringMapTest, SmallFullMapTest) { 140 // StringMap has a tricky corner case when the map is small (<8 buckets) and 141 // it fills up through a balanced pattern of inserts and erases. This can 142 // lead to inf-loops in some cases (PR13148) so we test it explicitly here. 143 llvm::StringMap<int> Map(2); 144 145 Map["eins"] = 1; 146 Map["zwei"] = 2; 147 Map["drei"] = 3; 148 Map.erase("drei"); 149 Map.erase("eins"); 150 Map["veir"] = 4; 151 Map["funf"] = 5; 152 153 EXPECT_EQ(3u, Map.size()); 154 EXPECT_EQ(0, Map.lookup("eins")); 155 EXPECT_EQ(2, Map.lookup("zwei")); 156 EXPECT_EQ(0, Map.lookup("drei")); 157 EXPECT_EQ(4, Map.lookup("veir")); 158 EXPECT_EQ(5, Map.lookup("funf")); 159 } 160 161 TEST_F(StringMapTest, CopyCtorTest) { 162 llvm::StringMap<int> Map; 163 164 Map["eins"] = 1; 165 Map["zwei"] = 2; 166 Map["drei"] = 3; 167 Map.erase("drei"); 168 Map.erase("eins"); 169 Map["veir"] = 4; 170 Map["funf"] = 5; 171 172 EXPECT_EQ(3u, Map.size()); 173 EXPECT_EQ(0, Map.lookup("eins")); 174 EXPECT_EQ(2, Map.lookup("zwei")); 175 EXPECT_EQ(0, Map.lookup("drei")); 176 EXPECT_EQ(4, Map.lookup("veir")); 177 EXPECT_EQ(5, Map.lookup("funf")); 178 179 llvm::StringMap<int> Map2(Map); 180 EXPECT_EQ(3u, Map2.size()); 181 EXPECT_EQ(0, Map2.lookup("eins")); 182 EXPECT_EQ(2, Map2.lookup("zwei")); 183 EXPECT_EQ(0, Map2.lookup("drei")); 184 EXPECT_EQ(4, Map2.lookup("veir")); 185 EXPECT_EQ(5, Map2.lookup("funf")); 186 } 187 188 // A more complex iteration test. 189 TEST_F(StringMapTest, IterationTest) { 190 bool visited[100]; 191 192 // Insert 100 numbers into the map 193 for (int i = 0; i < 100; ++i) { 194 std::stringstream ss; 195 ss << "key_" << i; 196 testMap[ss.str()] = i; 197 visited[i] = false; 198 } 199 200 // Iterate over all numbers and mark each one found. 201 for (StringMap<uint32_t>::iterator it = testMap.begin(); 202 it != testMap.end(); ++it) { 203 std::stringstream ss; 204 ss << "key_" << it->second; 205 ASSERT_STREQ(ss.str().c_str(), it->first().data()); 206 visited[it->second] = true; 207 } 208 209 // Ensure every number was visited. 210 for (int i = 0; i < 100; ++i) { 211 ASSERT_TRUE(visited[i]) << "Entry #" << i << " was never visited"; 212 } 213 } 214 215 // Test StringMapEntry::Create() method. 216 TEST_F(StringMapTest, StringMapEntryTest) { 217 StringMap<uint32_t>::value_type* entry = 218 StringMap<uint32_t>::value_type::Create( 219 StringRef(testKeyFirst, testKeyLength), 1u); 220 EXPECT_STREQ(testKey, entry->first().data()); 221 EXPECT_EQ(1u, entry->second); 222 free(entry); 223 } 224 225 // Test insert() method. 226 TEST_F(StringMapTest, InsertTest) { 227 SCOPED_TRACE("InsertTest"); 228 testMap.insert( 229 StringMap<uint32_t>::value_type::Create( 230 StringRef(testKeyFirst, testKeyLength), 231 testMap.getAllocator(), 1u)); 232 assertSingleItemMap(); 233 } 234 235 // Test insert(pair<K, V>) method 236 TEST_F(StringMapTest, InsertPairTest) { 237 bool Inserted; 238 StringMap<uint32_t>::iterator NewIt; 239 std::tie(NewIt, Inserted) = 240 testMap.insert(std::make_pair(testKeyFirst, testValue)); 241 EXPECT_EQ(1u, testMap.size()); 242 EXPECT_EQ(testValue, testMap[testKeyFirst]); 243 EXPECT_EQ(testKeyFirst, NewIt->first()); 244 EXPECT_EQ(testValue, NewIt->second); 245 EXPECT_TRUE(Inserted); 246 247 StringMap<uint32_t>::iterator ExistingIt; 248 std::tie(ExistingIt, Inserted) = 249 testMap.insert(std::make_pair(testKeyFirst, testValue + 1)); 250 EXPECT_EQ(1u, testMap.size()); 251 EXPECT_EQ(testValue, testMap[testKeyFirst]); 252 EXPECT_FALSE(Inserted); 253 EXPECT_EQ(NewIt, ExistingIt); 254 } 255 256 // Test insert(pair<K, V>) method when rehashing occurs 257 TEST_F(StringMapTest, InsertRehashingPairTest) { 258 // Check that the correct iterator is returned when the inserted element is 259 // moved to a different bucket during internal rehashing. This depends on 260 // the particular key, and the implementation of StringMap and HashString. 261 // Changes to those might result in this test not actually checking that. 262 StringMap<uint32_t> t(0); 263 EXPECT_EQ(0u, t.getNumBuckets()); 264 265 StringMap<uint32_t>::iterator It = 266 t.insert(std::make_pair("abcdef", 42)).first; 267 EXPECT_EQ(16u, t.getNumBuckets()); 268 EXPECT_EQ("abcdef", It->first()); 269 EXPECT_EQ(42u, It->second); 270 } 271 272 // Create a non-default constructable value 273 struct StringMapTestStruct { 274 StringMapTestStruct(int i) : i(i) {} 275 StringMapTestStruct() = delete; 276 int i; 277 }; 278 279 TEST_F(StringMapTest, NonDefaultConstructable) { 280 StringMap<StringMapTestStruct> t; 281 t.insert(std::make_pair("Test", StringMapTestStruct(123))); 282 StringMap<StringMapTestStruct>::iterator iter = t.find("Test"); 283 ASSERT_NE(iter, t.end()); 284 ASSERT_EQ(iter->second.i, 123); 285 } 286 287 struct Immovable { 288 Immovable() {} 289 Immovable(Immovable&&) = delete; // will disable the other special members 290 }; 291 292 struct MoveOnly { 293 int i; 294 MoveOnly(int i) : i(i) {} 295 MoveOnly(const Immovable&) : i(0) {} 296 MoveOnly(MoveOnly &&RHS) : i(RHS.i) {} 297 MoveOnly &operator=(MoveOnly &&RHS) { 298 i = RHS.i; 299 return *this; 300 } 301 302 private: 303 MoveOnly(const MoveOnly &) = delete; 304 MoveOnly &operator=(const MoveOnly &) = delete; 305 }; 306 307 TEST_F(StringMapTest, MoveOnly) { 308 StringMap<MoveOnly> t; 309 t.insert(std::make_pair("Test", MoveOnly(42))); 310 StringRef Key = "Test"; 311 StringMapEntry<MoveOnly>::Create(Key, MoveOnly(42)) 312 ->Destroy(); 313 } 314 315 TEST_F(StringMapTest, CtorArg) { 316 StringRef Key = "Test"; 317 StringMapEntry<MoveOnly>::Create(Key, Immovable()) 318 ->Destroy(); 319 } 320 321 TEST_F(StringMapTest, MoveConstruct) { 322 StringMap<int> A; 323 A["x"] = 42; 324 StringMap<int> B = std::move(A); 325 ASSERT_EQ(A.size(), 0u); 326 ASSERT_EQ(B.size(), 1u); 327 ASSERT_EQ(B["x"], 42); 328 ASSERT_EQ(B.count("y"), 0u); 329 } 330 331 TEST_F(StringMapTest, MoveAssignment) { 332 StringMap<int> A; 333 A["x"] = 42; 334 StringMap<int> B; 335 B["y"] = 117; 336 A = std::move(B); 337 ASSERT_EQ(A.size(), 1u); 338 ASSERT_EQ(B.size(), 0u); 339 ASSERT_EQ(A["y"], 117); 340 ASSERT_EQ(B.count("x"), 0u); 341 } 342 343 struct Countable { 344 int &InstanceCount; 345 int Number; 346 Countable(int Number, int &InstanceCount) 347 : InstanceCount(InstanceCount), Number(Number) { 348 ++InstanceCount; 349 } 350 Countable(Countable &&C) : InstanceCount(C.InstanceCount), Number(C.Number) { 351 ++InstanceCount; 352 C.Number = -1; 353 } 354 Countable(const Countable &C) 355 : InstanceCount(C.InstanceCount), Number(C.Number) { 356 ++InstanceCount; 357 } 358 Countable &operator=(Countable C) { 359 Number = C.Number; 360 return *this; 361 } 362 ~Countable() { --InstanceCount; } 363 }; 364 365 TEST_F(StringMapTest, MoveDtor) { 366 int InstanceCount = 0; 367 StringMap<Countable> A; 368 A.insert(std::make_pair("x", Countable(42, InstanceCount))); 369 ASSERT_EQ(InstanceCount, 1); 370 auto I = A.find("x"); 371 ASSERT_NE(I, A.end()); 372 ASSERT_EQ(I->second.Number, 42); 373 374 StringMap<Countable> B; 375 B = std::move(A); 376 ASSERT_EQ(InstanceCount, 1); 377 ASSERT_TRUE(A.empty()); 378 I = B.find("x"); 379 ASSERT_NE(I, B.end()); 380 ASSERT_EQ(I->second.Number, 42); 381 382 B = StringMap<Countable>(); 383 ASSERT_EQ(InstanceCount, 0); 384 ASSERT_TRUE(B.empty()); 385 } 386 387 namespace { 388 // Simple class that counts how many moves and copy happens when growing a map 389 struct CountCtorCopyAndMove { 390 static unsigned Ctor; 391 static unsigned Move; 392 static unsigned Copy; 393 int Data = 0; 394 CountCtorCopyAndMove(int Data) : Data(Data) { Ctor++; } 395 CountCtorCopyAndMove() { Ctor++; } 396 397 CountCtorCopyAndMove(const CountCtorCopyAndMove &) { Copy++; } 398 CountCtorCopyAndMove &operator=(const CountCtorCopyAndMove &) { 399 Copy++; 400 return *this; 401 } 402 CountCtorCopyAndMove(CountCtorCopyAndMove &&) { Move++; } 403 CountCtorCopyAndMove &operator=(const CountCtorCopyAndMove &&) { 404 Move++; 405 return *this; 406 } 407 }; 408 unsigned CountCtorCopyAndMove::Copy = 0; 409 unsigned CountCtorCopyAndMove::Move = 0; 410 unsigned CountCtorCopyAndMove::Ctor = 0; 411 412 } // anonymous namespace 413 414 // Make sure creating the map with an initial size of N actually gives us enough 415 // buckets to insert N items without increasing allocation size. 416 TEST(StringMapCustomTest, InitialSizeTest) { 417 // 1 is an "edge value", 32 is an arbitrary power of two, and 67 is an 418 // arbitrary prime, picked without any good reason. 419 for (auto Size : {1, 32, 67}) { 420 StringMap<CountCtorCopyAndMove> Map(Size); 421 auto NumBuckets = Map.getNumBuckets(); 422 CountCtorCopyAndMove::Move = 0; 423 CountCtorCopyAndMove::Copy = 0; 424 for (int i = 0; i < Size; ++i) 425 Map.insert(std::pair<std::string, CountCtorCopyAndMove>( 426 std::piecewise_construct, std::forward_as_tuple(Twine(i).str()), 427 std::forward_as_tuple(i))); 428 // After the inital move, the map will move the Elts in the Entry. 429 EXPECT_EQ((unsigned)Size * 2, CountCtorCopyAndMove::Move); 430 // We copy once the pair from the Elts vector 431 EXPECT_EQ(0u, CountCtorCopyAndMove::Copy); 432 // Check that the map didn't grow 433 EXPECT_EQ(Map.getNumBuckets(), NumBuckets); 434 } 435 } 436 437 TEST(StringMapCustomTest, BracketOperatorCtor) { 438 StringMap<CountCtorCopyAndMove> Map; 439 CountCtorCopyAndMove::Ctor = 0; 440 Map["abcd"]; 441 EXPECT_EQ(1u, CountCtorCopyAndMove::Ctor); 442 // Test that operator[] does not create a value when it is already in the map 443 CountCtorCopyAndMove::Ctor = 0; 444 Map["abcd"]; 445 EXPECT_EQ(0u, CountCtorCopyAndMove::Ctor); 446 } 447 448 namespace { 449 struct NonMoveableNonCopyableType { 450 int Data = 0; 451 NonMoveableNonCopyableType() = default; 452 NonMoveableNonCopyableType(int Data) : Data(Data) {} 453 NonMoveableNonCopyableType(const NonMoveableNonCopyableType &) = delete; 454 NonMoveableNonCopyableType(NonMoveableNonCopyableType &&) = delete; 455 }; 456 } 457 458 // Test that we can "emplace" an element in the map without involving map/move 459 TEST(StringMapCustomTest, EmplaceTest) { 460 StringMap<NonMoveableNonCopyableType> Map; 461 Map.emplace_second("abcd", 42); 462 EXPECT_EQ(1u, Map.count("abcd")); 463 EXPECT_EQ(42, Map["abcd"].Data); 464 } 465 466 } // end anonymous namespace 467