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 StringMapTest : public testing::Test {
19 StringMap<uint32_t> testMap;
21 static const char testKey[];
22 static const uint32_t testValue;
23 static const char* testKeyFirst;
24 static const char* testKeyLast;
25 static const std::string testKeyStr;
27 void assertEmptyMap() {
29 EXPECT_EQ(0u, testMap.size());
30 EXPECT_TRUE(testMap.empty());
33 EXPECT_TRUE(testMap.begin() == testMap.end());
36 EXPECT_EQ(0u, testMap.count(testKey));
37 EXPECT_EQ(0u, testMap.count(testKeyFirst, testKeyLast));
38 EXPECT_EQ(0u, testMap.count(testKeyStr));
39 EXPECT_TRUE(testMap.find(testKey) == testMap.end());
40 EXPECT_TRUE(testMap.find(testKeyFirst, testKeyLast) == testMap.end());
41 EXPECT_TRUE(testMap.find(testKeyStr) == testMap.end());
44 void assertSingleItemMap() {
46 EXPECT_EQ(1u, testMap.size());
47 EXPECT_FALSE(testMap.begin() == testMap.end());
48 EXPECT_FALSE(testMap.empty());
51 StringMap<uint32_t>::iterator it = testMap.begin();
52 EXPECT_STREQ(testKey, it->first());
53 EXPECT_EQ(testValue, it->second);
55 EXPECT_TRUE(it == testMap.end());
58 EXPECT_EQ(1u, testMap.count(testKey));
59 EXPECT_EQ(1u, testMap.count(testKeyFirst, testKeyLast));
60 EXPECT_EQ(1u, testMap.count(testKeyStr));
61 EXPECT_TRUE(testMap.find(testKey) == testMap.begin());
62 EXPECT_TRUE(testMap.find(testKeyFirst, testKeyLast) == testMap.begin());
63 EXPECT_TRUE(testMap.find(testKeyStr) == testMap.begin());
67 const char StringMapTest::testKey[] = "key";
68 const uint32_t StringMapTest::testValue = 1u;
69 const char* StringMapTest::testKeyFirst = testKey;
70 const char* StringMapTest::testKeyLast = testKey + sizeof(testKey) - 1;
71 const std::string StringMapTest::testKeyStr(testKey);
74 TEST_F(StringMapTest, EmptyMapTest) {
75 SCOPED_TRACE("EmptyMapTest");
80 TEST_F(StringMapTest, ConstEmptyMapTest) {
81 const StringMap<uint32_t>& constTestMap = testMap;
84 EXPECT_EQ(0u, constTestMap.size());
85 EXPECT_TRUE(constTestMap.empty());
88 EXPECT_TRUE(constTestMap.begin() == constTestMap.end());
91 EXPECT_EQ(0u, constTestMap.count(testKey));
92 EXPECT_EQ(0u, constTestMap.count(testKeyFirst, testKeyLast));
93 EXPECT_EQ(0u, constTestMap.count(testKeyStr));
94 EXPECT_TRUE(constTestMap.find(testKey) == constTestMap.end());
95 EXPECT_TRUE(constTestMap.find(testKeyFirst, testKeyLast) ==
97 EXPECT_TRUE(constTestMap.find(testKeyStr) == constTestMap.end());
100 // A map with a single entry
101 TEST_F(StringMapTest, SingleEntryMapTest) {
102 SCOPED_TRACE("SingleEntryMapTest");
103 testMap[testKey] = testValue;
104 assertSingleItemMap();
107 // Test clear() method
108 TEST_F(StringMapTest, ClearTest) {
109 SCOPED_TRACE("ClearTest");
110 testMap[testKey] = testValue;
115 // Test erase(iterator) method
116 TEST_F(StringMapTest, EraseIteratorTest) {
117 SCOPED_TRACE("EraseIteratorTest");
118 testMap[testKey] = testValue;
119 testMap.erase(testMap.begin());
123 // Test erase(value) method
124 TEST_F(StringMapTest, EraseValueTest) {
125 SCOPED_TRACE("EraseValueTest");
126 testMap[testKey] = testValue;
127 testMap.erase(testKey);
131 // Test inserting two values and erasing one
132 TEST_F(StringMapTest, InsertAndEraseTest) {
133 SCOPED_TRACE("InsertAndEraseTest");
134 testMap[testKey] = testValue;
135 testMap["otherKey"] = 2;
136 testMap.erase("otherKey");
137 assertSingleItemMap();
140 // Test StringMapEntry::Create() method.
141 // DISABLED because this fails without a StringMapEntryInitializer, and
142 // I can't get it to compile with one.
143 TEST_F(StringMapTest, DISABLED_StringMapEntryTest) {
144 StringMap<uint32_t>::value_type* entry =
145 StringMap<uint32_t>::value_type::Create(
146 testKeyFirst, testKeyLast, 1u);
147 EXPECT_STREQ(testKey, entry->first());
148 EXPECT_EQ(1u, entry->second);
151 // Test insert() method
152 // DISABLED because this fails without a StringMapEntryInitializer, and
153 // I can't get it to compile with one.
154 TEST_F(StringMapTest, DISABLED_InsertTest) {
155 SCOPED_TRACE("InsertTest");
157 StringMap<uint32_t>::value_type::Create(
158 testKeyFirst, testKeyLast, testMap.getAllocator(), 1u));
159 assertSingleItemMap();
162 // A more complex iteration test
163 TEST_F(StringMapTest, IterationTest) {
166 // Insert 100 numbers into the map
167 for (int i = 0; i < 100; ++i) {
168 std::stringstream ss;
170 testMap[ss.str()] = i;
174 // Iterate over all numbers and mark each one found.
175 for (StringMap<uint32_t>::iterator it = testMap.begin();
176 it != testMap.end(); ++it) {
177 std::stringstream ss;
178 ss << "key_" << it->second;
179 ASSERT_STREQ(ss.str().c_str(), it->first());
180 visited[it->second] = true;
183 // Ensure every number was visited.
184 for (int i = 0; i < 100; ++i) {
185 ASSERT_TRUE(visited[i]) << "Entry #" << i << " was never visited";