1 //===- llvm/unittest/ADT/StringMapMap.cpp - StringMap unit tests ----------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "llvm/ADT/StringMap.h"
10 #include "llvm/ADT/StringSet.h"
11 #include "llvm/ADT/Twine.h"
12 #include "llvm/Support/DataTypes.h"
13 #include "gtest/gtest.h"
21 class StringMapTest
: public testing::Test
{
23 StringMap
<uint32_t> testMap
;
25 static const char testKey
[];
26 static const uint32_t testValue
;
27 static const char* testKeyFirst
;
28 static size_t testKeyLength
;
29 static const std::string testKeyStr
;
31 void assertEmptyMap() {
33 EXPECT_EQ(0u, testMap
.size());
34 EXPECT_TRUE(testMap
.empty());
37 EXPECT_TRUE(testMap
.begin() == testMap
.end());
40 EXPECT_EQ(0u, testMap
.count(testKey
));
41 EXPECT_EQ(0u, testMap
.count(StringRef(testKeyFirst
, testKeyLength
)));
42 EXPECT_EQ(0u, testMap
.count(testKeyStr
));
43 EXPECT_TRUE(testMap
.find(testKey
) == testMap
.end());
44 EXPECT_TRUE(testMap
.find(StringRef(testKeyFirst
, testKeyLength
)) ==
46 EXPECT_TRUE(testMap
.find(testKeyStr
) == testMap
.end());
49 void assertSingleItemMap() {
51 EXPECT_EQ(1u, testMap
.size());
52 EXPECT_FALSE(testMap
.begin() == testMap
.end());
53 EXPECT_FALSE(testMap
.empty());
56 StringMap
<uint32_t>::iterator it
= testMap
.begin();
57 EXPECT_STREQ(testKey
, it
->first().data());
58 EXPECT_EQ(testValue
, it
->second
);
60 EXPECT_TRUE(it
== testMap
.end());
63 EXPECT_EQ(1u, testMap
.count(testKey
));
64 EXPECT_EQ(1u, testMap
.count(StringRef(testKeyFirst
, testKeyLength
)));
65 EXPECT_EQ(1u, testMap
.count(testKeyStr
));
66 EXPECT_TRUE(testMap
.find(testKey
) == testMap
.begin());
67 EXPECT_TRUE(testMap
.find(StringRef(testKeyFirst
, testKeyLength
)) ==
69 EXPECT_TRUE(testMap
.find(testKeyStr
) == testMap
.begin());
73 const char StringMapTest::testKey
[] = "key";
74 const uint32_t StringMapTest::testValue
= 1u;
75 const char* StringMapTest::testKeyFirst
= testKey
;
76 size_t StringMapTest::testKeyLength
= sizeof(testKey
) - 1;
77 const std::string
StringMapTest::testKeyStr(testKey
);
80 TEST_F(StringMapTest
, EmptyMapTest
) {
84 // Constant map tests.
85 TEST_F(StringMapTest
, ConstEmptyMapTest
) {
86 const StringMap
<uint32_t>& constTestMap
= testMap
;
89 EXPECT_EQ(0u, constTestMap
.size());
90 EXPECT_TRUE(constTestMap
.empty());
93 EXPECT_TRUE(constTestMap
.begin() == constTestMap
.end());
96 EXPECT_EQ(0u, constTestMap
.count(testKey
));
97 EXPECT_EQ(0u, constTestMap
.count(StringRef(testKeyFirst
, testKeyLength
)));
98 EXPECT_EQ(0u, constTestMap
.count(testKeyStr
));
99 EXPECT_TRUE(constTestMap
.find(testKey
) == constTestMap
.end());
100 EXPECT_TRUE(constTestMap
.find(StringRef(testKeyFirst
, testKeyLength
)) ==
102 EXPECT_TRUE(constTestMap
.find(testKeyStr
) == constTestMap
.end());
105 // A map with a single entry.
106 TEST_F(StringMapTest
, SingleEntryMapTest
) {
107 testMap
[testKey
] = testValue
;
108 assertSingleItemMap();
111 // Test clear() method.
112 TEST_F(StringMapTest
, ClearTest
) {
113 testMap
[testKey
] = testValue
;
118 // Test erase(iterator) method.
119 TEST_F(StringMapTest
, EraseIteratorTest
) {
120 testMap
[testKey
] = testValue
;
121 testMap
.erase(testMap
.begin());
125 // Test erase(value) method.
126 TEST_F(StringMapTest
, EraseValueTest
) {
127 testMap
[testKey
] = testValue
;
128 testMap
.erase(testKey
);
132 // Test inserting two values and erasing one.
133 TEST_F(StringMapTest
, InsertAndEraseTest
) {
134 testMap
[testKey
] = testValue
;
135 testMap
["otherKey"] = 2;
136 testMap
.erase("otherKey");
137 assertSingleItemMap();
140 TEST_F(StringMapTest
, SmallFullMapTest
) {
141 // StringMap has a tricky corner case when the map is small (<8 buckets) and
142 // it fills up through a balanced pattern of inserts and erases. This can
143 // lead to inf-loops in some cases (PR13148) so we test it explicitly here.
144 llvm::StringMap
<int> Map(2);
154 EXPECT_EQ(3u, Map
.size());
155 EXPECT_EQ(0, Map
.lookup("eins"));
156 EXPECT_EQ(2, Map
.lookup("zwei"));
157 EXPECT_EQ(0, Map
.lookup("drei"));
158 EXPECT_EQ(4, Map
.lookup("veir"));
159 EXPECT_EQ(5, Map
.lookup("funf"));
162 TEST_F(StringMapTest
, CopyCtorTest
) {
163 llvm::StringMap
<int> Map
;
173 EXPECT_EQ(3u, Map
.size());
174 EXPECT_EQ(0, Map
.lookup("eins"));
175 EXPECT_EQ(2, Map
.lookup("zwei"));
176 EXPECT_EQ(0, Map
.lookup("drei"));
177 EXPECT_EQ(4, Map
.lookup("veir"));
178 EXPECT_EQ(5, Map
.lookup("funf"));
180 llvm::StringMap
<int> Map2(Map
);
181 EXPECT_EQ(3u, Map2
.size());
182 EXPECT_EQ(0, Map2
.lookup("eins"));
183 EXPECT_EQ(2, Map2
.lookup("zwei"));
184 EXPECT_EQ(0, Map2
.lookup("drei"));
185 EXPECT_EQ(4, Map2
.lookup("veir"));
186 EXPECT_EQ(5, Map2
.lookup("funf"));
189 // A more complex iteration test.
190 TEST_F(StringMapTest
, IterationTest
) {
193 // Insert 100 numbers into the map
194 for (int i
= 0; i
< 100; ++i
) {
195 std::stringstream ss
;
197 testMap
[ss
.str()] = i
;
201 // Iterate over all numbers and mark each one found.
202 for (StringMap
<uint32_t>::iterator it
= testMap
.begin();
203 it
!= testMap
.end(); ++it
) {
204 std::stringstream ss
;
205 ss
<< "key_" << it
->second
;
206 ASSERT_STREQ(ss
.str().c_str(), it
->first().data());
207 visited
[it
->second
] = true;
210 // Ensure every number was visited.
211 for (int i
= 0; i
< 100; ++i
) {
212 ASSERT_TRUE(visited
[i
]) << "Entry #" << i
<< " was never visited";
216 // Test StringMapEntry::Create() method.
217 TEST_F(StringMapTest
, StringMapEntryTest
) {
218 StringMap
<uint32_t>::value_type
* entry
=
219 StringMap
<uint32_t>::value_type::Create(
220 StringRef(testKeyFirst
, testKeyLength
), 1u);
221 EXPECT_STREQ(testKey
, entry
->first().data());
222 EXPECT_EQ(1u, entry
->second
);
226 // Test insert() method.
227 TEST_F(StringMapTest
, InsertTest
) {
228 SCOPED_TRACE("InsertTest");
230 StringMap
<uint32_t>::value_type::Create(
231 StringRef(testKeyFirst
, testKeyLength
),
232 testMap
.getAllocator(), 1u));
233 assertSingleItemMap();
236 // Test insert(pair<K, V>) method
237 TEST_F(StringMapTest
, InsertPairTest
) {
239 StringMap
<uint32_t>::iterator NewIt
;
240 std::tie(NewIt
, Inserted
) =
241 testMap
.insert(std::make_pair(testKeyFirst
, testValue
));
242 EXPECT_EQ(1u, testMap
.size());
243 EXPECT_EQ(testValue
, testMap
[testKeyFirst
]);
244 EXPECT_EQ(testKeyFirst
, NewIt
->first());
245 EXPECT_EQ(testValue
, NewIt
->second
);
246 EXPECT_TRUE(Inserted
);
248 StringMap
<uint32_t>::iterator ExistingIt
;
249 std::tie(ExistingIt
, Inserted
) =
250 testMap
.insert(std::make_pair(testKeyFirst
, testValue
+ 1));
251 EXPECT_EQ(1u, testMap
.size());
252 EXPECT_EQ(testValue
, testMap
[testKeyFirst
]);
253 EXPECT_FALSE(Inserted
);
254 EXPECT_EQ(NewIt
, ExistingIt
);
257 // Test insert(pair<K, V>) method when rehashing occurs
258 TEST_F(StringMapTest
, InsertRehashingPairTest
) {
259 // Check that the correct iterator is returned when the inserted element is
260 // moved to a different bucket during internal rehashing. This depends on
261 // the particular key, and the implementation of StringMap and HashString.
262 // Changes to those might result in this test not actually checking that.
263 StringMap
<uint32_t> t(0);
264 EXPECT_EQ(0u, t
.getNumBuckets());
266 StringMap
<uint32_t>::iterator It
=
267 t
.insert(std::make_pair("abcdef", 42)).first
;
268 EXPECT_EQ(16u, t
.getNumBuckets());
269 EXPECT_EQ("abcdef", It
->first());
270 EXPECT_EQ(42u, It
->second
);
273 TEST_F(StringMapTest
, IterMapKeys
) {
280 auto Keys
= to_vector
<4>(Map
.keys());
283 SmallVector
<StringRef
, 4> Expected
= {"A", "B", "C", "D"};
284 EXPECT_EQ(Expected
, Keys
);
287 TEST_F(StringMapTest
, IterSetKeys
) {
294 auto Keys
= to_vector
<4>(Set
.keys());
297 SmallVector
<StringRef
, 4> Expected
= {"A", "B", "C", "D"};
298 EXPECT_EQ(Expected
, Keys
);
301 // Create a non-default constructable value
302 struct StringMapTestStruct
{
303 StringMapTestStruct(int i
) : i(i
) {}
304 StringMapTestStruct() = delete;
308 TEST_F(StringMapTest
, NonDefaultConstructable
) {
309 StringMap
<StringMapTestStruct
> t
;
310 t
.insert(std::make_pair("Test", StringMapTestStruct(123)));
311 StringMap
<StringMapTestStruct
>::iterator iter
= t
.find("Test");
312 ASSERT_NE(iter
, t
.end());
313 ASSERT_EQ(iter
->second
.i
, 123);
318 Immovable(Immovable
&&) = delete; // will disable the other special members
323 MoveOnly(int i
) : i(i
) {}
324 MoveOnly(const Immovable
&) : i(0) {}
325 MoveOnly(MoveOnly
&&RHS
) : i(RHS
.i
) {}
326 MoveOnly
&operator=(MoveOnly
&&RHS
) {
332 MoveOnly(const MoveOnly
&) = delete;
333 MoveOnly
&operator=(const MoveOnly
&) = delete;
336 TEST_F(StringMapTest
, MoveOnly
) {
337 StringMap
<MoveOnly
> t
;
338 t
.insert(std::make_pair("Test", MoveOnly(42)));
339 StringRef Key
= "Test";
340 StringMapEntry
<MoveOnly
>::Create(Key
, MoveOnly(42))
344 TEST_F(StringMapTest
, CtorArg
) {
345 StringRef Key
= "Test";
346 StringMapEntry
<MoveOnly
>::Create(Key
, Immovable())
350 TEST_F(StringMapTest
, MoveConstruct
) {
353 StringMap
<int> B
= std::move(A
);
354 ASSERT_EQ(A
.size(), 0u);
355 ASSERT_EQ(B
.size(), 1u);
356 ASSERT_EQ(B
["x"], 42);
357 ASSERT_EQ(B
.count("y"), 0u);
360 TEST_F(StringMapTest
, MoveAssignment
) {
366 ASSERT_EQ(A
.size(), 1u);
367 ASSERT_EQ(B
.size(), 0u);
368 ASSERT_EQ(A
["y"], 117);
369 ASSERT_EQ(B
.count("x"), 0u);
375 Countable(int Number
, int &InstanceCount
)
376 : InstanceCount(InstanceCount
), Number(Number
) {
379 Countable(Countable
&&C
) : InstanceCount(C
.InstanceCount
), Number(C
.Number
) {
383 Countable(const Countable
&C
)
384 : InstanceCount(C
.InstanceCount
), Number(C
.Number
) {
387 Countable
&operator=(Countable C
) {
391 ~Countable() { --InstanceCount
; }
394 TEST_F(StringMapTest
, MoveDtor
) {
395 int InstanceCount
= 0;
396 StringMap
<Countable
> A
;
397 A
.insert(std::make_pair("x", Countable(42, InstanceCount
)));
398 ASSERT_EQ(InstanceCount
, 1);
399 auto I
= A
.find("x");
400 ASSERT_NE(I
, A
.end());
401 ASSERT_EQ(I
->second
.Number
, 42);
403 StringMap
<Countable
> B
;
405 ASSERT_EQ(InstanceCount
, 1);
406 ASSERT_TRUE(A
.empty());
408 ASSERT_NE(I
, B
.end());
409 ASSERT_EQ(I
->second
.Number
, 42);
411 B
= StringMap
<Countable
>();
412 ASSERT_EQ(InstanceCount
, 0);
413 ASSERT_TRUE(B
.empty());
417 // Simple class that counts how many moves and copy happens when growing a map
418 struct CountCtorCopyAndMove
{
419 static unsigned Ctor
;
420 static unsigned Move
;
421 static unsigned Copy
;
423 CountCtorCopyAndMove(int Data
) : Data(Data
) { Ctor
++; }
424 CountCtorCopyAndMove() { Ctor
++; }
426 CountCtorCopyAndMove(const CountCtorCopyAndMove
&) { Copy
++; }
427 CountCtorCopyAndMove
&operator=(const CountCtorCopyAndMove
&) {
431 CountCtorCopyAndMove(CountCtorCopyAndMove
&&) { Move
++; }
432 CountCtorCopyAndMove
&operator=(const CountCtorCopyAndMove
&&) {
437 unsigned CountCtorCopyAndMove::Copy
= 0;
438 unsigned CountCtorCopyAndMove::Move
= 0;
439 unsigned CountCtorCopyAndMove::Ctor
= 0;
441 } // anonymous namespace
443 // Make sure creating the map with an initial size of N actually gives us enough
444 // buckets to insert N items without increasing allocation size.
445 TEST(StringMapCustomTest
, InitialSizeTest
) {
446 // 1 is an "edge value", 32 is an arbitrary power of two, and 67 is an
447 // arbitrary prime, picked without any good reason.
448 for (auto Size
: {1, 32, 67}) {
449 StringMap
<CountCtorCopyAndMove
> Map(Size
);
450 auto NumBuckets
= Map
.getNumBuckets();
451 CountCtorCopyAndMove::Move
= 0;
452 CountCtorCopyAndMove::Copy
= 0;
453 for (int i
= 0; i
< Size
; ++i
)
454 Map
.insert(std::pair
<std::string
, CountCtorCopyAndMove
>(
455 std::piecewise_construct
, std::forward_as_tuple(Twine(i
).str()),
456 std::forward_as_tuple(i
)));
457 // After the initial move, the map will move the Elts in the Entry.
458 EXPECT_EQ((unsigned)Size
* 2, CountCtorCopyAndMove::Move
);
459 // We copy once the pair from the Elts vector
460 EXPECT_EQ(0u, CountCtorCopyAndMove::Copy
);
461 // Check that the map didn't grow
462 EXPECT_EQ(Map
.getNumBuckets(), NumBuckets
);
466 TEST(StringMapCustomTest
, BracketOperatorCtor
) {
467 StringMap
<CountCtorCopyAndMove
> Map
;
468 CountCtorCopyAndMove::Ctor
= 0;
470 EXPECT_EQ(1u, CountCtorCopyAndMove::Ctor
);
471 // Test that operator[] does not create a value when it is already in the map
472 CountCtorCopyAndMove::Ctor
= 0;
474 EXPECT_EQ(0u, CountCtorCopyAndMove::Ctor
);
478 struct NonMoveableNonCopyableType
{
480 NonMoveableNonCopyableType() = default;
481 NonMoveableNonCopyableType(int Data
) : Data(Data
) {}
482 NonMoveableNonCopyableType(const NonMoveableNonCopyableType
&) = delete;
483 NonMoveableNonCopyableType(NonMoveableNonCopyableType
&&) = delete;
487 // Test that we can "emplace" an element in the map without involving map/move
488 TEST(StringMapCustomTest
, EmplaceTest
) {
489 StringMap
<NonMoveableNonCopyableType
> Map
;
490 Map
.try_emplace("abcd", 42);
491 EXPECT_EQ(1u, Map
.count("abcd"));
492 EXPECT_EQ(42, Map
["abcd"].Data
);
495 // Test that StringMapEntryBase can handle size_t wide sizes.
496 TEST(StringMapCustomTest
, StringMapEntryBaseSize
) {
499 // Test that the entry can represent max-unsigned.
500 if (sizeof(size_t) <= sizeof(unsigned))
501 LargeValue
= std::numeric_limits
<unsigned>::max();
503 LargeValue
= std::numeric_limits
<unsigned>::max() + 1ULL;
504 StringMapEntryBase
LargeBase(LargeValue
);
505 EXPECT_EQ(LargeValue
, LargeBase
.getKeyLength());
507 // Test that the entry can hold at least max size_t.
508 LargeValue
= std::numeric_limits
<size_t>::max();
509 StringMapEntryBase
LargerBase(LargeValue
);
510 LargeValue
= std::numeric_limits
<size_t>::max();
511 EXPECT_EQ(LargeValue
, LargerBase
.getKeyLength());
514 // Test that StringMapEntry can handle size_t wide sizes.
515 TEST(StringMapCustomTest
, StringMapEntrySize
) {
518 // Test that the entry can represent max-unsigned.
519 if (sizeof(size_t) <= sizeof(unsigned))
520 LargeValue
= std::numeric_limits
<unsigned>::max();
522 LargeValue
= std::numeric_limits
<unsigned>::max() + 1ULL;
523 StringMapEntry
<int> LargeEntry(LargeValue
);
524 StringRef Key
= LargeEntry
.getKey();
525 EXPECT_EQ(LargeValue
, Key
.size());
527 // Test that the entry can hold at least max size_t.
528 LargeValue
= std::numeric_limits
<size_t>::max();
529 StringMapEntry
<int> LargerEntry(LargeValue
);
530 Key
= LargerEntry
.getKey();
531 EXPECT_EQ(LargeValue
, Key
.size());
534 } // end anonymous namespace