Set svn:ignore on a slew of +Coverage directories
[llvm/workbench.git] / unittests / ADT / StringMapTest.cpp
blob9f9f2e4c795440ac655ee6c36f69f382e4766256
1 //===- llvm/unittest/ADT/StringMapMap.cpp - StringMap unit tests ----------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
10 #include "gtest/gtest.h"
11 #include "llvm/ADT/StringMap.h"
12 using namespace llvm;
14 namespace {
16 // Test fixture
17 class StringMapTest : public testing::Test {
18 protected:
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() {
28 // Size tests
29 EXPECT_EQ(0u, testMap.size());
30 EXPECT_TRUE(testMap.empty());
32 // Iterator tests
33 EXPECT_TRUE(testMap.begin() == testMap.end());
35 // Lookup tests
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() {
45 // Size tests
46 EXPECT_EQ(1u, testMap.size());
47 EXPECT_FALSE(testMap.begin() == testMap.end());
48 EXPECT_FALSE(testMap.empty());
50 // Iterator tests
51 StringMap<uint32_t>::iterator it = testMap.begin();
52 EXPECT_STREQ(testKey, it->first());
53 EXPECT_EQ(testValue, it->second);
54 ++it;
55 EXPECT_TRUE(it == testMap.end());
57 // Lookup tests
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);
73 // Empty map tests.
74 TEST_F(StringMapTest, EmptyMapTest) {
75 SCOPED_TRACE("EmptyMapTest");
76 assertEmptyMap();
79 // Constant map tests.
80 TEST_F(StringMapTest, ConstEmptyMapTest) {
81 const StringMap<uint32_t>& constTestMap = testMap;
83 // Size tests
84 EXPECT_EQ(0u, constTestMap.size());
85 EXPECT_TRUE(constTestMap.empty());
87 // Iterator tests
88 EXPECT_TRUE(constTestMap.begin() == constTestMap.end());
90 // Lookup tests
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) ==
96 constTestMap.end());
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;
111 testMap.clear();
112 assertEmptyMap();
115 // Test erase(iterator) method.
116 TEST_F(StringMapTest, EraseIteratorTest) {
117 SCOPED_TRACE("EraseIteratorTest");
118 testMap[testKey] = testValue;
119 testMap.erase(testMap.begin());
120 assertEmptyMap();
123 // Test erase(value) method.
124 TEST_F(StringMapTest, EraseValueTest) {
125 SCOPED_TRACE("EraseValueTest");
126 testMap[testKey] = testValue;
127 testMap.erase(testKey);
128 assertEmptyMap();
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) {
142 bool visited[100];
144 // Insert 100 numbers into the map
145 for (int i = 0; i < 100; ++i) {
146 std::stringstream ss;
147 ss << "key_" << i;
148 testMap[ss.str()] = i;
149 visited[i] = false;
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
169 namespace llvm {
171 template <>
172 class StringMapEntryInitializer<uint32_t> {
173 public:
174 template <typename InitTy>
175 static void Initialize(StringMapEntry<uint32_t> &T, InitTy InitVal) {
176 T.second = InitVal;
180 } // end llvm namespace
182 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");
196 testMap.insert(
197 StringMap<uint32_t>::value_type::Create(
198 testKeyFirst, testKeyLast, testMap.getAllocator(), 1u));
199 assertSingleItemMap();
202 } // end anonymous namespace