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"
17 class StringMapEntryInitializer<uint32_t> {
19 template <typename InitTy>
20 static void Initialize(StringMapEntry<uint32_t> &T, InitTy InitVal) {
30 class StringMapTest : public testing::Test {
32 StringMap<uint32_t> testMap;
34 static const char testKey[];
35 static const uint32_t testValue;
36 static const char* testKeyFirst;
37 static const char* testKeyLast;
38 static const std::string testKeyStr;
40 void assertEmptyMap() {
42 EXPECT_EQ(0u, testMap.size());
43 EXPECT_TRUE(testMap.empty());
46 EXPECT_TRUE(testMap.begin() == testMap.end());
49 EXPECT_EQ(0u, testMap.count(testKey));
50 EXPECT_EQ(0u, testMap.count(testKeyFirst, testKeyLast));
51 EXPECT_EQ(0u, testMap.count(testKeyStr));
52 EXPECT_TRUE(testMap.find(testKey) == testMap.end());
53 EXPECT_TRUE(testMap.find(testKeyFirst, testKeyLast) == testMap.end());
54 EXPECT_TRUE(testMap.find(testKeyStr) == testMap.end());
57 void assertSingleItemMap() {
59 EXPECT_EQ(1u, testMap.size());
60 EXPECT_FALSE(testMap.begin() == testMap.end());
61 EXPECT_FALSE(testMap.empty());
64 StringMap<uint32_t>::iterator it = testMap.begin();
65 EXPECT_STREQ(testKey, it->first());
66 EXPECT_EQ(testValue, it->second);
68 EXPECT_TRUE(it == testMap.end());
71 EXPECT_EQ(1u, testMap.count(testKey));
72 EXPECT_EQ(1u, testMap.count(testKeyFirst, testKeyLast));
73 EXPECT_EQ(1u, testMap.count(testKeyStr));
74 EXPECT_TRUE(testMap.find(testKey) == testMap.begin());
75 EXPECT_TRUE(testMap.find(testKeyFirst, testKeyLast) == testMap.begin());
76 EXPECT_TRUE(testMap.find(testKeyStr) == testMap.begin());
80 const char StringMapTest::testKey[] = "key";
81 const uint32_t StringMapTest::testValue = 1u;
82 const char* StringMapTest::testKeyFirst = testKey;
83 const char* StringMapTest::testKeyLast = testKey + sizeof(testKey) - 1;
84 const std::string StringMapTest::testKeyStr(testKey);
87 TEST_F(StringMapTest, EmptyMapTest) {
88 SCOPED_TRACE("EmptyMapTest");
93 TEST_F(StringMapTest, ConstEmptyMapTest) {
94 const StringMap<uint32_t>& constTestMap = testMap;
97 EXPECT_EQ(0u, constTestMap.size());
98 EXPECT_TRUE(constTestMap.empty());
101 EXPECT_TRUE(constTestMap.begin() == constTestMap.end());
104 EXPECT_EQ(0u, constTestMap.count(testKey));
105 EXPECT_EQ(0u, constTestMap.count(testKeyFirst, testKeyLast));
106 EXPECT_EQ(0u, constTestMap.count(testKeyStr));
107 EXPECT_TRUE(constTestMap.find(testKey) == constTestMap.end());
108 EXPECT_TRUE(constTestMap.find(testKeyFirst, testKeyLast) ==
110 EXPECT_TRUE(constTestMap.find(testKeyStr) == constTestMap.end());
113 // A map with a single entry
114 TEST_F(StringMapTest, SingleEntryMapTest) {
115 SCOPED_TRACE("SingleEntryMapTest");
116 testMap[testKey] = testValue;
117 assertSingleItemMap();
120 // Test clear() method
121 TEST_F(StringMapTest, ClearTest) {
122 SCOPED_TRACE("ClearTest");
123 testMap[testKey] = testValue;
128 // Test erase(iterator) method
129 TEST_F(StringMapTest, EraseIteratorTest) {
130 SCOPED_TRACE("EraseIteratorTest");
131 testMap[testKey] = testValue;
132 testMap.erase(testMap.begin());
136 // Test erase(value) method
137 TEST_F(StringMapTest, EraseValueTest) {
138 SCOPED_TRACE("EraseValueTest");
139 testMap[testKey] = testValue;
140 testMap.erase(testKey);
144 // Test inserting two values and erasing one
145 TEST_F(StringMapTest, InsertAndEraseTest) {
146 SCOPED_TRACE("InsertAndEraseTest");
147 testMap[testKey] = testValue;
148 testMap["otherKey"] = 2;
149 testMap.erase("otherKey");
150 assertSingleItemMap();
153 // Test StringMapEntry::Create() method.
154 // DISABLED because this fails without a StringMapEntryInitializer, and
155 // I can't get it to compile with one.
156 TEST_F(StringMapTest, StringMapEntryTest) {
158 StringMap<uint32_t>::value_type* entry =
159 StringMap<uint32_t>::value_type::Create(
160 testKeyFirst, testKeyLast, A, 1u);
161 EXPECT_STREQ(testKey, entry->first());
162 EXPECT_EQ(1u, entry->second);
165 // Test insert() method
166 // DISABLED because this fails without a StringMapEntryInitializer, and
167 // I can't get it to compile with one.
168 TEST_F(StringMapTest, InsertTest) {
169 SCOPED_TRACE("InsertTest");
171 StringMap<uint32_t>::value_type::Create(
172 testKeyFirst, testKeyLast, testMap.getAllocator(), 1u));
173 assertSingleItemMap();
176 // A more complex iteration test
177 TEST_F(StringMapTest, IterationTest) {
180 // Insert 100 numbers into the map
181 for (int i = 0; i < 100; ++i) {
182 std::stringstream ss;
184 testMap[ss.str()] = i;
188 // Iterate over all numbers and mark each one found.
189 for (StringMap<uint32_t>::iterator it = testMap.begin();
190 it != testMap.end(); ++it) {
191 std::stringstream ss;
192 ss << "key_" << it->second;
193 ASSERT_STREQ(ss.str().c_str(), it->first());
194 visited[it->second] = true;
197 // Ensure every number was visited.
198 for (int i = 0; i < 100; ++i) {
199 ASSERT_TRUE(visited[i]) << "Entry #" << i << " was never visited";