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 "gtest/gtest.h"
11 #include "llvm/ADT/StringMap.h"
12 #include "llvm/Support/DataTypes.h"
18 class StringMapTest
: public testing::Test
{
20 StringMap
<uint32_t> testMap
;
22 static const char testKey
[];
23 static const uint32_t testValue
;
24 static const char* testKeyFirst
;
25 static size_t testKeyLength
;
26 static const std::string testKeyStr
;
28 void assertEmptyMap() {
30 EXPECT_EQ(0u, testMap
.size());
31 EXPECT_TRUE(testMap
.empty());
34 EXPECT_TRUE(testMap
.begin() == testMap
.end());
37 EXPECT_EQ(0u, testMap
.count(testKey
));
38 EXPECT_EQ(0u, testMap
.count(StringRef(testKeyFirst
, testKeyLength
)));
39 EXPECT_EQ(0u, testMap
.count(testKeyStr
));
40 EXPECT_TRUE(testMap
.find(testKey
) == testMap
.end());
41 EXPECT_TRUE(testMap
.find(StringRef(testKeyFirst
, testKeyLength
)) ==
43 EXPECT_TRUE(testMap
.find(testKeyStr
) == testMap
.end());
46 void assertSingleItemMap() {
48 EXPECT_EQ(1u, testMap
.size());
49 EXPECT_FALSE(testMap
.begin() == testMap
.end());
50 EXPECT_FALSE(testMap
.empty());
53 StringMap
<uint32_t>::iterator it
= testMap
.begin();
54 EXPECT_STREQ(testKey
, it
->first());
55 EXPECT_EQ(testValue
, it
->second
);
57 EXPECT_TRUE(it
== testMap
.end());
60 EXPECT_EQ(1u, testMap
.count(testKey
));
61 EXPECT_EQ(1u, testMap
.count(StringRef(testKeyFirst
, testKeyLength
)));
62 EXPECT_EQ(1u, testMap
.count(testKeyStr
));
63 EXPECT_TRUE(testMap
.find(testKey
) == testMap
.begin());
64 EXPECT_TRUE(testMap
.find(StringRef(testKeyFirst
, testKeyLength
)) ==
66 EXPECT_TRUE(testMap
.find(testKeyStr
) == testMap
.begin());
70 const char StringMapTest::testKey
[] = "key";
71 const uint32_t StringMapTest::testValue
= 1u;
72 const char* StringMapTest::testKeyFirst
= testKey
;
73 size_t StringMapTest::testKeyLength
= sizeof(testKey
) - 1;
74 const std::string
StringMapTest::testKeyStr(testKey
);
77 TEST_F(StringMapTest
, EmptyMapTest
) {
78 SCOPED_TRACE("EmptyMapTest");
82 // Constant map tests.
83 TEST_F(StringMapTest
, ConstEmptyMapTest
) {
84 const StringMap
<uint32_t>& constTestMap
= testMap
;
87 EXPECT_EQ(0u, constTestMap
.size());
88 EXPECT_TRUE(constTestMap
.empty());
91 EXPECT_TRUE(constTestMap
.begin() == constTestMap
.end());
94 EXPECT_EQ(0u, constTestMap
.count(testKey
));
95 EXPECT_EQ(0u, constTestMap
.count(StringRef(testKeyFirst
, testKeyLength
)));
96 EXPECT_EQ(0u, constTestMap
.count(testKeyStr
));
97 EXPECT_TRUE(constTestMap
.find(testKey
) == constTestMap
.end());
98 EXPECT_TRUE(constTestMap
.find(StringRef(testKeyFirst
, testKeyLength
)) ==
100 EXPECT_TRUE(constTestMap
.find(testKeyStr
) == constTestMap
.end());
103 // A map with a single entry.
104 TEST_F(StringMapTest
, SingleEntryMapTest
) {
105 SCOPED_TRACE("SingleEntryMapTest");
106 testMap
[testKey
] = testValue
;
107 assertSingleItemMap();
110 // Test clear() method.
111 TEST_F(StringMapTest
, ClearTest
) {
112 SCOPED_TRACE("ClearTest");
113 testMap
[testKey
] = testValue
;
118 // Test erase(iterator) method.
119 TEST_F(StringMapTest
, EraseIteratorTest
) {
120 SCOPED_TRACE("EraseIteratorTest");
121 testMap
[testKey
] = testValue
;
122 testMap
.erase(testMap
.begin());
126 // Test erase(value) method.
127 TEST_F(StringMapTest
, EraseValueTest
) {
128 SCOPED_TRACE("EraseValueTest");
129 testMap
[testKey
] = testValue
;
130 testMap
.erase(testKey
);
134 // Test inserting two values and erasing one.
135 TEST_F(StringMapTest
, InsertAndEraseTest
) {
136 SCOPED_TRACE("InsertAndEraseTest");
137 testMap
[testKey
] = testValue
;
138 testMap
["otherKey"] = 2;
139 testMap
.erase("otherKey");
140 assertSingleItemMap();
143 // A more complex iteration test.
144 TEST_F(StringMapTest
, IterationTest
) {
147 // Insert 100 numbers into the map
148 for (int i
= 0; i
< 100; ++i
) {
149 std::stringstream ss
;
151 testMap
[ss
.str()] = i
;
155 // Iterate over all numbers and mark each one found.
156 for (StringMap
<uint32_t>::iterator it
= testMap
.begin();
157 it
!= testMap
.end(); ++it
) {
158 std::stringstream ss
;
159 ss
<< "key_" << it
->second
;
160 ASSERT_STREQ(ss
.str().c_str(), it
->first());
161 visited
[it
->second
] = true;
164 // Ensure every number was visited.
165 for (int i
= 0; i
< 100; ++i
) {
166 ASSERT_TRUE(visited
[i
]) << "Entry #" << i
<< " was never visited";
170 } // end anonymous namespace
175 class StringMapEntryInitializer
<uint32_t> {
177 template <typename InitTy
>
178 static void Initialize(StringMapEntry
<uint32_t> &T
, InitTy InitVal
) {
183 } // end llvm namespace
187 // Test StringMapEntry::Create() method.
188 TEST_F(StringMapTest
, StringMapEntryTest
) {
189 StringMap
<uint32_t>::value_type
* entry
=
190 StringMap
<uint32_t>::value_type::Create(
191 testKeyFirst
, testKeyFirst
+ testKeyLength
, 1u);
192 EXPECT_STREQ(testKey
, entry
->first());
193 EXPECT_EQ(1u, entry
->second
);
197 // Test insert() method.
198 TEST_F(StringMapTest
, InsertTest
) {
199 SCOPED_TRACE("InsertTest");
201 StringMap
<uint32_t>::value_type::Create(
202 testKeyFirst
, testKeyFirst
+ testKeyLength
,
203 testMap
.getAllocator(), 1u));
204 assertSingleItemMap();
207 } // end anonymous namespace