1 //===- llvm/unittest/ADT/StringMapMap.cpp - StringMap unit tests ----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/ADT/StringMap.h"
11 #include "llvm/ADT/StringSet.h"
12 #include "llvm/ADT/Twine.h"
13 #include "llvm/Support/DataTypes.h"
14 #include "gtest/gtest.h"
22 class StringMapTest
: public testing::Test
{
24 StringMap
<uint32_t> testMap
;
26 static const char testKey
[];
27 static const uint32_t testValue
;
28 static const char* testKeyFirst
;
29 static size_t testKeyLength
;
30 static const std::string testKeyStr
;
32 void assertEmptyMap() {
34 EXPECT_EQ(0u, testMap
.size());
35 EXPECT_TRUE(testMap
.empty());
38 EXPECT_TRUE(testMap
.begin() == testMap
.end());
41 EXPECT_EQ(0u, testMap
.count(testKey
));
42 EXPECT_EQ(0u, testMap
.count(StringRef(testKeyFirst
, testKeyLength
)));
43 EXPECT_EQ(0u, testMap
.count(testKeyStr
));
44 EXPECT_TRUE(testMap
.find(testKey
) == testMap
.end());
45 EXPECT_TRUE(testMap
.find(StringRef(testKeyFirst
, testKeyLength
)) ==
47 EXPECT_TRUE(testMap
.find(testKeyStr
) == testMap
.end());
50 void assertSingleItemMap() {
52 EXPECT_EQ(1u, testMap
.size());
53 EXPECT_FALSE(testMap
.begin() == testMap
.end());
54 EXPECT_FALSE(testMap
.empty());
57 StringMap
<uint32_t>::iterator it
= testMap
.begin();
58 EXPECT_STREQ(testKey
, it
->first().data());
59 EXPECT_EQ(testValue
, it
->second
);
61 EXPECT_TRUE(it
== testMap
.end());
64 EXPECT_EQ(1u, testMap
.count(testKey
));
65 EXPECT_EQ(1u, testMap
.count(StringRef(testKeyFirst
, testKeyLength
)));
66 EXPECT_EQ(1u, testMap
.count(testKeyStr
));
67 EXPECT_TRUE(testMap
.find(testKey
) == testMap
.begin());
68 EXPECT_TRUE(testMap
.find(StringRef(testKeyFirst
, testKeyLength
)) ==
70 EXPECT_TRUE(testMap
.find(testKeyStr
) == testMap
.begin());
74 const char StringMapTest::testKey
[] = "key";
75 const uint32_t StringMapTest::testValue
= 1u;
76 const char* StringMapTest::testKeyFirst
= testKey
;
77 size_t StringMapTest::testKeyLength
= sizeof(testKey
) - 1;
78 const std::string
StringMapTest::testKeyStr(testKey
);
81 TEST_F(StringMapTest
, EmptyMapTest
) {
85 // Constant map tests.
86 TEST_F(StringMapTest
, ConstEmptyMapTest
) {
87 const StringMap
<uint32_t>& constTestMap
= testMap
;
90 EXPECT_EQ(0u, constTestMap
.size());
91 EXPECT_TRUE(constTestMap
.empty());
94 EXPECT_TRUE(constTestMap
.begin() == constTestMap
.end());
97 EXPECT_EQ(0u, constTestMap
.count(testKey
));
98 EXPECT_EQ(0u, constTestMap
.count(StringRef(testKeyFirst
, testKeyLength
)));
99 EXPECT_EQ(0u, constTestMap
.count(testKeyStr
));
100 EXPECT_TRUE(constTestMap
.find(testKey
) == constTestMap
.end());
101 EXPECT_TRUE(constTestMap
.find(StringRef(testKeyFirst
, testKeyLength
)) ==
103 EXPECT_TRUE(constTestMap
.find(testKeyStr
) == constTestMap
.end());
106 // A map with a single entry.
107 TEST_F(StringMapTest
, SingleEntryMapTest
) {
108 testMap
[testKey
] = testValue
;
109 assertSingleItemMap();
112 // Test clear() method.
113 TEST_F(StringMapTest
, ClearTest
) {
114 testMap
[testKey
] = testValue
;
119 // Test erase(iterator) method.
120 TEST_F(StringMapTest
, EraseIteratorTest
) {
121 testMap
[testKey
] = testValue
;
122 testMap
.erase(testMap
.begin());
126 // Test erase(value) method.
127 TEST_F(StringMapTest
, EraseValueTest
) {
128 testMap
[testKey
] = testValue
;
129 testMap
.erase(testKey
);
133 // Test inserting two values and erasing one.
134 TEST_F(StringMapTest
, InsertAndEraseTest
) {
135 testMap
[testKey
] = testValue
;
136 testMap
["otherKey"] = 2;
137 testMap
.erase("otherKey");
138 assertSingleItemMap();
141 TEST_F(StringMapTest
, SmallFullMapTest
) {
142 // StringMap has a tricky corner case when the map is small (<8 buckets) and
143 // it fills up through a balanced pattern of inserts and erases. This can
144 // lead to inf-loops in some cases (PR13148) so we test it explicitly here.
145 llvm::StringMap
<int> Map(2);
155 EXPECT_EQ(3u, Map
.size());
156 EXPECT_EQ(0, Map
.lookup("eins"));
157 EXPECT_EQ(2, Map
.lookup("zwei"));
158 EXPECT_EQ(0, Map
.lookup("drei"));
159 EXPECT_EQ(4, Map
.lookup("veir"));
160 EXPECT_EQ(5, Map
.lookup("funf"));
163 TEST_F(StringMapTest
, CopyCtorTest
) {
164 llvm::StringMap
<int> Map
;
174 EXPECT_EQ(3u, Map
.size());
175 EXPECT_EQ(0, Map
.lookup("eins"));
176 EXPECT_EQ(2, Map
.lookup("zwei"));
177 EXPECT_EQ(0, Map
.lookup("drei"));
178 EXPECT_EQ(4, Map
.lookup("veir"));
179 EXPECT_EQ(5, Map
.lookup("funf"));
181 llvm::StringMap
<int> Map2(Map
);
182 EXPECT_EQ(3u, Map2
.size());
183 EXPECT_EQ(0, Map2
.lookup("eins"));
184 EXPECT_EQ(2, Map2
.lookup("zwei"));
185 EXPECT_EQ(0, Map2
.lookup("drei"));
186 EXPECT_EQ(4, Map2
.lookup("veir"));
187 EXPECT_EQ(5, Map2
.lookup("funf"));
190 // A more complex iteration test.
191 TEST_F(StringMapTest
, IterationTest
) {
194 // Insert 100 numbers into the map
195 for (int i
= 0; i
< 100; ++i
) {
196 std::stringstream ss
;
198 testMap
[ss
.str()] = i
;
202 // Iterate over all numbers and mark each one found.
203 for (StringMap
<uint32_t>::iterator it
= testMap
.begin();
204 it
!= testMap
.end(); ++it
) {
205 std::stringstream ss
;
206 ss
<< "key_" << it
->second
;
207 ASSERT_STREQ(ss
.str().c_str(), it
->first().data());
208 visited
[it
->second
] = true;
211 // Ensure every number was visited.
212 for (int i
= 0; i
< 100; ++i
) {
213 ASSERT_TRUE(visited
[i
]) << "Entry #" << i
<< " was never visited";
217 // Test StringMapEntry::Create() method.
218 TEST_F(StringMapTest
, StringMapEntryTest
) {
219 StringMap
<uint32_t>::value_type
* entry
=
220 StringMap
<uint32_t>::value_type::Create(
221 StringRef(testKeyFirst
, testKeyLength
), 1u);
222 EXPECT_STREQ(testKey
, entry
->first().data());
223 EXPECT_EQ(1u, entry
->second
);
227 // Test insert() method.
228 TEST_F(StringMapTest
, InsertTest
) {
229 SCOPED_TRACE("InsertTest");
231 StringMap
<uint32_t>::value_type::Create(
232 StringRef(testKeyFirst
, testKeyLength
),
233 testMap
.getAllocator(), 1u));
234 assertSingleItemMap();
237 // Test insert(pair<K, V>) method
238 TEST_F(StringMapTest
, InsertPairTest
) {
240 StringMap
<uint32_t>::iterator NewIt
;
241 std::tie(NewIt
, Inserted
) =
242 testMap
.insert(std::make_pair(testKeyFirst
, testValue
));
243 EXPECT_EQ(1u, testMap
.size());
244 EXPECT_EQ(testValue
, testMap
[testKeyFirst
]);
245 EXPECT_EQ(testKeyFirst
, NewIt
->first());
246 EXPECT_EQ(testValue
, NewIt
->second
);
247 EXPECT_TRUE(Inserted
);
249 StringMap
<uint32_t>::iterator ExistingIt
;
250 std::tie(ExistingIt
, Inserted
) =
251 testMap
.insert(std::make_pair(testKeyFirst
, testValue
+ 1));
252 EXPECT_EQ(1u, testMap
.size());
253 EXPECT_EQ(testValue
, testMap
[testKeyFirst
]);
254 EXPECT_FALSE(Inserted
);
255 EXPECT_EQ(NewIt
, ExistingIt
);
258 // Test insert(pair<K, V>) method when rehashing occurs
259 TEST_F(StringMapTest
, InsertRehashingPairTest
) {
260 // Check that the correct iterator is returned when the inserted element is
261 // moved to a different bucket during internal rehashing. This depends on
262 // the particular key, and the implementation of StringMap and HashString.
263 // Changes to those might result in this test not actually checking that.
264 StringMap
<uint32_t> t(0);
265 EXPECT_EQ(0u, t
.getNumBuckets());
267 StringMap
<uint32_t>::iterator It
=
268 t
.insert(std::make_pair("abcdef", 42)).first
;
269 EXPECT_EQ(16u, t
.getNumBuckets());
270 EXPECT_EQ("abcdef", It
->first());
271 EXPECT_EQ(42u, It
->second
);
274 TEST_F(StringMapTest
, IterMapKeys
) {
281 auto Keys
= to_vector
<4>(Map
.keys());
282 llvm::sort(Keys
.begin(), Keys
.end());
284 SmallVector
<StringRef
, 4> Expected
= {"A", "B", "C", "D"};
285 EXPECT_EQ(Expected
, Keys
);
288 TEST_F(StringMapTest
, IterSetKeys
) {
295 auto Keys
= to_vector
<4>(Set
.keys());
296 llvm::sort(Keys
.begin(), Keys
.end());
298 SmallVector
<StringRef
, 4> Expected
= {"A", "B", "C", "D"};
299 EXPECT_EQ(Expected
, Keys
);
302 // Create a non-default constructable value
303 struct StringMapTestStruct
{
304 StringMapTestStruct(int i
) : i(i
) {}
305 StringMapTestStruct() = delete;
309 TEST_F(StringMapTest
, NonDefaultConstructable
) {
310 StringMap
<StringMapTestStruct
> t
;
311 t
.insert(std::make_pair("Test", StringMapTestStruct(123)));
312 StringMap
<StringMapTestStruct
>::iterator iter
= t
.find("Test");
313 ASSERT_NE(iter
, t
.end());
314 ASSERT_EQ(iter
->second
.i
, 123);
319 Immovable(Immovable
&&) = delete; // will disable the other special members
324 MoveOnly(int i
) : i(i
) {}
325 MoveOnly(const Immovable
&) : i(0) {}
326 MoveOnly(MoveOnly
&&RHS
) : i(RHS
.i
) {}
327 MoveOnly
&operator=(MoveOnly
&&RHS
) {
333 MoveOnly(const MoveOnly
&) = delete;
334 MoveOnly
&operator=(const MoveOnly
&) = delete;
337 TEST_F(StringMapTest
, MoveOnly
) {
338 StringMap
<MoveOnly
> t
;
339 t
.insert(std::make_pair("Test", MoveOnly(42)));
340 StringRef Key
= "Test";
341 StringMapEntry
<MoveOnly
>::Create(Key
, MoveOnly(42))
345 TEST_F(StringMapTest
, CtorArg
) {
346 StringRef Key
= "Test";
347 StringMapEntry
<MoveOnly
>::Create(Key
, Immovable())
351 TEST_F(StringMapTest
, MoveConstruct
) {
354 StringMap
<int> B
= std::move(A
);
355 ASSERT_EQ(A
.size(), 0u);
356 ASSERT_EQ(B
.size(), 1u);
357 ASSERT_EQ(B
["x"], 42);
358 ASSERT_EQ(B
.count("y"), 0u);
361 TEST_F(StringMapTest
, MoveAssignment
) {
367 ASSERT_EQ(A
.size(), 1u);
368 ASSERT_EQ(B
.size(), 0u);
369 ASSERT_EQ(A
["y"], 117);
370 ASSERT_EQ(B
.count("x"), 0u);
376 Countable(int Number
, int &InstanceCount
)
377 : InstanceCount(InstanceCount
), Number(Number
) {
380 Countable(Countable
&&C
) : InstanceCount(C
.InstanceCount
), Number(C
.Number
) {
384 Countable(const Countable
&C
)
385 : InstanceCount(C
.InstanceCount
), Number(C
.Number
) {
388 Countable
&operator=(Countable C
) {
392 ~Countable() { --InstanceCount
; }
395 TEST_F(StringMapTest
, MoveDtor
) {
396 int InstanceCount
= 0;
397 StringMap
<Countable
> A
;
398 A
.insert(std::make_pair("x", Countable(42, InstanceCount
)));
399 ASSERT_EQ(InstanceCount
, 1);
400 auto I
= A
.find("x");
401 ASSERT_NE(I
, A
.end());
402 ASSERT_EQ(I
->second
.Number
, 42);
404 StringMap
<Countable
> B
;
406 ASSERT_EQ(InstanceCount
, 1);
407 ASSERT_TRUE(A
.empty());
409 ASSERT_NE(I
, B
.end());
410 ASSERT_EQ(I
->second
.Number
, 42);
412 B
= StringMap
<Countable
>();
413 ASSERT_EQ(InstanceCount
, 0);
414 ASSERT_TRUE(B
.empty());
418 // Simple class that counts how many moves and copy happens when growing a map
419 struct CountCtorCopyAndMove
{
420 static unsigned Ctor
;
421 static unsigned Move
;
422 static unsigned Copy
;
424 CountCtorCopyAndMove(int Data
) : Data(Data
) { Ctor
++; }
425 CountCtorCopyAndMove() { Ctor
++; }
427 CountCtorCopyAndMove(const CountCtorCopyAndMove
&) { Copy
++; }
428 CountCtorCopyAndMove
&operator=(const CountCtorCopyAndMove
&) {
432 CountCtorCopyAndMove(CountCtorCopyAndMove
&&) { Move
++; }
433 CountCtorCopyAndMove
&operator=(const CountCtorCopyAndMove
&&) {
438 unsigned CountCtorCopyAndMove::Copy
= 0;
439 unsigned CountCtorCopyAndMove::Move
= 0;
440 unsigned CountCtorCopyAndMove::Ctor
= 0;
442 } // anonymous namespace
444 // Make sure creating the map with an initial size of N actually gives us enough
445 // buckets to insert N items without increasing allocation size.
446 TEST(StringMapCustomTest
, InitialSizeTest
) {
447 // 1 is an "edge value", 32 is an arbitrary power of two, and 67 is an
448 // arbitrary prime, picked without any good reason.
449 for (auto Size
: {1, 32, 67}) {
450 StringMap
<CountCtorCopyAndMove
> Map(Size
);
451 auto NumBuckets
= Map
.getNumBuckets();
452 CountCtorCopyAndMove::Move
= 0;
453 CountCtorCopyAndMove::Copy
= 0;
454 for (int i
= 0; i
< Size
; ++i
)
455 Map
.insert(std::pair
<std::string
, CountCtorCopyAndMove
>(
456 std::piecewise_construct
, std::forward_as_tuple(Twine(i
).str()),
457 std::forward_as_tuple(i
)));
458 // After the initial move, the map will move the Elts in the Entry.
459 EXPECT_EQ((unsigned)Size
* 2, CountCtorCopyAndMove::Move
);
460 // We copy once the pair from the Elts vector
461 EXPECT_EQ(0u, CountCtorCopyAndMove::Copy
);
462 // Check that the map didn't grow
463 EXPECT_EQ(Map
.getNumBuckets(), NumBuckets
);
467 TEST(StringMapCustomTest
, BracketOperatorCtor
) {
468 StringMap
<CountCtorCopyAndMove
> Map
;
469 CountCtorCopyAndMove::Ctor
= 0;
471 EXPECT_EQ(1u, CountCtorCopyAndMove::Ctor
);
472 // Test that operator[] does not create a value when it is already in the map
473 CountCtorCopyAndMove::Ctor
= 0;
475 EXPECT_EQ(0u, CountCtorCopyAndMove::Ctor
);
479 struct NonMoveableNonCopyableType
{
481 NonMoveableNonCopyableType() = default;
482 NonMoveableNonCopyableType(int Data
) : Data(Data
) {}
483 NonMoveableNonCopyableType(const NonMoveableNonCopyableType
&) = delete;
484 NonMoveableNonCopyableType(NonMoveableNonCopyableType
&&) = delete;
488 // Test that we can "emplace" an element in the map without involving map/move
489 TEST(StringMapCustomTest
, EmplaceTest
) {
490 StringMap
<NonMoveableNonCopyableType
> Map
;
491 Map
.try_emplace("abcd", 42);
492 EXPECT_EQ(1u, Map
.count("abcd"));
493 EXPECT_EQ(42, Map
["abcd"].Data
);
496 // Test that StringMapEntryBase can handle size_t wide sizes.
497 TEST(StringMapCustomTest
, StringMapEntryBaseSize
) {
500 // Test that the entry can represent max-unsigned.
501 if (sizeof(size_t) <= sizeof(unsigned))
502 LargeValue
= std::numeric_limits
<unsigned>::max();
504 LargeValue
= std::numeric_limits
<unsigned>::max() + 1ULL;
505 StringMapEntryBase
LargeBase(LargeValue
);
506 EXPECT_EQ(LargeValue
, LargeBase
.getKeyLength());
508 // Test that the entry can hold at least max size_t.
509 LargeValue
= std::numeric_limits
<size_t>::max();
510 StringMapEntryBase
LargerBase(LargeValue
);
511 LargeValue
= std::numeric_limits
<size_t>::max();
512 EXPECT_EQ(LargeValue
, LargerBase
.getKeyLength());
515 // Test that StringMapEntry can handle size_t wide sizes.
516 TEST(StringMapCustomTest
, StringMapEntrySize
) {
519 // Test that the entry can represent max-unsigned.
520 if (sizeof(size_t) <= sizeof(unsigned))
521 LargeValue
= std::numeric_limits
<unsigned>::max();
523 LargeValue
= std::numeric_limits
<unsigned>::max() + 1ULL;
524 StringMapEntry
<int> LargeEntry(LargeValue
);
525 StringRef Key
= LargeEntry
.getKey();
526 EXPECT_EQ(LargeValue
, Key
.size());
528 // Test that the entry can hold at least max size_t.
529 LargeValue
= std::numeric_limits
<size_t>::max();
530 StringMapEntry
<int> LargerEntry(LargeValue
);
531 Key
= LargerEntry
.getKey();
532 EXPECT_EQ(LargeValue
, Key
.size());
535 } // end anonymous namespace