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");
79 // Constant map tests.
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 // A more complex iteration test.
141 TEST_F(StringMapTest, IterationTest) {
144 // Insert 100 numbers into the map
145 for (int i = 0; i < 100; ++i) {
146 std::stringstream ss;
148 testMap[ss.str()] = i;
152 // Iterate over all numbers and mark each one found.
153 for (StringMap<uint32_t>::iterator it = testMap.begin();
154 it != testMap.end(); ++it) {
155 std::stringstream ss;
156 ss << "key_" << it->second;
157 ASSERT_STREQ(ss.str().c_str(), it->first());
158 visited[it->second] = true;
161 // Ensure every number was visited.
162 for (int i = 0; i < 100; ++i) {
163 ASSERT_TRUE(visited[i]) << "Entry #" << i << " was never visited";
167 } // end anonymous namespace
172 class StringMapEntryInitializer<uint32_t> {
174 template <typename InitTy>
175 static void Initialize(StringMapEntry<uint32_t> &T, InitTy InitVal) {
180 } // end llvm namespace
184 // Test StringMapEntry::Create() method.
185 TEST_F(StringMapTest, StringMapEntryTest) {
186 StringMap<uint32_t>::value_type* entry =
187 StringMap<uint32_t>::value_type::Create(
188 testKeyFirst, testKeyLast, 1u);
189 EXPECT_STREQ(testKey, entry->first());
190 EXPECT_EQ(1u, entry->second);
193 // Test insert() method.
194 TEST_F(StringMapTest, InsertTest) {
195 SCOPED_TRACE("InsertTest");
197 StringMap<uint32_t>::value_type::Create(
198 testKeyFirst, testKeyLast, testMap.getAllocator(), 1u));
199 assertSingleItemMap();
202 } // end anonymous namespace