blob: ea91348a5bdf730e92085d5778ad1c78b6de18bc [file] [log] [blame]
Bill Wendling1ed36632009-01-08 07:35:39 +00001//===- llvm/unittest/ADT/StringMapMap.cpp - StringMap unit tests ----------===//
Misha Brukman8bb5e992009-01-08 04:48:20 +00002//
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//===----------------------------------------------------------------------===//
9
10#include "gtest/gtest.h"
11#include "llvm/ADT/StringMap.h"
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000012#include "llvm/Support/DataTypes.h"
Misha Brukman8bb5e992009-01-08 04:48:20 +000013using namespace llvm;
14
15namespace {
16
17// Test fixture
18class StringMapTest : public testing::Test {
19protected:
20 StringMap<uint32_t> testMap;
21
22 static const char testKey[];
23 static const uint32_t testValue;
24 static const char* testKeyFirst;
Daniel Dunbar6316fbc2009-07-23 18:17:34 +000025 static size_t testKeyLength;
Misha Brukman8bb5e992009-01-08 04:48:20 +000026 static const std::string testKeyStr;
27
28 void assertEmptyMap() {
29 // Size tests
30 EXPECT_EQ(0u, testMap.size());
31 EXPECT_TRUE(testMap.empty());
32
33 // Iterator tests
34 EXPECT_TRUE(testMap.begin() == testMap.end());
35
36 // Lookup tests
37 EXPECT_EQ(0u, testMap.count(testKey));
Daniel Dunbar6316fbc2009-07-23 18:17:34 +000038 EXPECT_EQ(0u, testMap.count(StringRef(testKeyFirst, testKeyLength)));
Misha Brukman8bb5e992009-01-08 04:48:20 +000039 EXPECT_EQ(0u, testMap.count(testKeyStr));
40 EXPECT_TRUE(testMap.find(testKey) == testMap.end());
Daniel Dunbar6316fbc2009-07-23 18:17:34 +000041 EXPECT_TRUE(testMap.find(StringRef(testKeyFirst, testKeyLength)) ==
42 testMap.end());
Misha Brukman8bb5e992009-01-08 04:48:20 +000043 EXPECT_TRUE(testMap.find(testKeyStr) == testMap.end());
44 }
45
46 void assertSingleItemMap() {
47 // Size tests
48 EXPECT_EQ(1u, testMap.size());
49 EXPECT_FALSE(testMap.begin() == testMap.end());
50 EXPECT_FALSE(testMap.empty());
51
52 // Iterator tests
53 StringMap<uint32_t>::iterator it = testMap.begin();
54 EXPECT_STREQ(testKey, it->first());
55 EXPECT_EQ(testValue, it->second);
56 ++it;
57 EXPECT_TRUE(it == testMap.end());
58
59 // Lookup tests
60 EXPECT_EQ(1u, testMap.count(testKey));
Daniel Dunbar6316fbc2009-07-23 18:17:34 +000061 EXPECT_EQ(1u, testMap.count(StringRef(testKeyFirst, testKeyLength)));
Misha Brukman8bb5e992009-01-08 04:48:20 +000062 EXPECT_EQ(1u, testMap.count(testKeyStr));
63 EXPECT_TRUE(testMap.find(testKey) == testMap.begin());
Daniel Dunbar6316fbc2009-07-23 18:17:34 +000064 EXPECT_TRUE(testMap.find(StringRef(testKeyFirst, testKeyLength)) ==
65 testMap.begin());
Misha Brukman8bb5e992009-01-08 04:48:20 +000066 EXPECT_TRUE(testMap.find(testKeyStr) == testMap.begin());
67 }
68};
69
70const char StringMapTest::testKey[] = "key";
71const uint32_t StringMapTest::testValue = 1u;
72const char* StringMapTest::testKeyFirst = testKey;
Daniel Dunbar6316fbc2009-07-23 18:17:34 +000073size_t StringMapTest::testKeyLength = sizeof(testKey) - 1;
Misha Brukman8bb5e992009-01-08 04:48:20 +000074const std::string StringMapTest::testKeyStr(testKey);
75
Bill Wendling6b223d72009-01-08 09:31:36 +000076// Empty map tests.
Misha Brukman8bb5e992009-01-08 04:48:20 +000077TEST_F(StringMapTest, EmptyMapTest) {
78 SCOPED_TRACE("EmptyMapTest");
79 assertEmptyMap();
80}
81
Bill Wendling6b223d72009-01-08 09:31:36 +000082// Constant map tests.
Misha Brukman8bb5e992009-01-08 04:48:20 +000083TEST_F(StringMapTest, ConstEmptyMapTest) {
84 const StringMap<uint32_t>& constTestMap = testMap;
85
86 // Size tests
87 EXPECT_EQ(0u, constTestMap.size());
88 EXPECT_TRUE(constTestMap.empty());
89
90 // Iterator tests
91 EXPECT_TRUE(constTestMap.begin() == constTestMap.end());
92
93 // Lookup tests
94 EXPECT_EQ(0u, constTestMap.count(testKey));
Daniel Dunbar6316fbc2009-07-23 18:17:34 +000095 EXPECT_EQ(0u, constTestMap.count(StringRef(testKeyFirst, testKeyLength)));
Misha Brukman8bb5e992009-01-08 04:48:20 +000096 EXPECT_EQ(0u, constTestMap.count(testKeyStr));
97 EXPECT_TRUE(constTestMap.find(testKey) == constTestMap.end());
Daniel Dunbar6316fbc2009-07-23 18:17:34 +000098 EXPECT_TRUE(constTestMap.find(StringRef(testKeyFirst, testKeyLength)) ==
Bill Wendling6b223d72009-01-08 09:31:36 +000099 constTestMap.end());
Misha Brukman8bb5e992009-01-08 04:48:20 +0000100 EXPECT_TRUE(constTestMap.find(testKeyStr) == constTestMap.end());
101}
102
Bill Wendling6b223d72009-01-08 09:31:36 +0000103// A map with a single entry.
Misha Brukman8bb5e992009-01-08 04:48:20 +0000104TEST_F(StringMapTest, SingleEntryMapTest) {
105 SCOPED_TRACE("SingleEntryMapTest");
106 testMap[testKey] = testValue;
107 assertSingleItemMap();
108}
109
Bill Wendling6b223d72009-01-08 09:31:36 +0000110// Test clear() method.
Misha Brukman8bb5e992009-01-08 04:48:20 +0000111TEST_F(StringMapTest, ClearTest) {
112 SCOPED_TRACE("ClearTest");
113 testMap[testKey] = testValue;
114 testMap.clear();
115 assertEmptyMap();
116}
117
Bill Wendling6b223d72009-01-08 09:31:36 +0000118// Test erase(iterator) method.
Misha Brukman8bb5e992009-01-08 04:48:20 +0000119TEST_F(StringMapTest, EraseIteratorTest) {
120 SCOPED_TRACE("EraseIteratorTest");
121 testMap[testKey] = testValue;
122 testMap.erase(testMap.begin());
123 assertEmptyMap();
124}
125
Bill Wendling6b223d72009-01-08 09:31:36 +0000126// Test erase(value) method.
Misha Brukman8bb5e992009-01-08 04:48:20 +0000127TEST_F(StringMapTest, EraseValueTest) {
128 SCOPED_TRACE("EraseValueTest");
129 testMap[testKey] = testValue;
130 testMap.erase(testKey);
131 assertEmptyMap();
132}
133
Bill Wendling6b223d72009-01-08 09:31:36 +0000134// Test inserting two values and erasing one.
Misha Brukman8bb5e992009-01-08 04:48:20 +0000135TEST_F(StringMapTest, InsertAndEraseTest) {
136 SCOPED_TRACE("InsertAndEraseTest");
137 testMap[testKey] = testValue;
138 testMap["otherKey"] = 2;
139 testMap.erase("otherKey");
140 assertSingleItemMap();
141}
142
Bill Wendling6b223d72009-01-08 09:31:36 +0000143// A more complex iteration test.
Misha Brukman8bb5e992009-01-08 04:48:20 +0000144TEST_F(StringMapTest, IterationTest) {
145 bool visited[100];
146
147 // Insert 100 numbers into the map
148 for (int i = 0; i < 100; ++i) {
149 std::stringstream ss;
150 ss << "key_" << i;
151 testMap[ss.str()] = i;
152 visited[i] = false;
153 }
154
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;
162 }
163
164 // Ensure every number was visited.
165 for (int i = 0; i < 100; ++i) {
166 ASSERT_TRUE(visited[i]) << "Entry #" << i << " was never visited";
167 }
168}
169
Bill Wendling6b223d72009-01-08 09:31:36 +0000170} // end anonymous namespace
171
172namespace llvm {
173
174template <>
175class StringMapEntryInitializer<uint32_t> {
176public:
177 template <typename InitTy>
178 static void Initialize(StringMapEntry<uint32_t> &T, InitTy InitVal) {
179 T.second = InitVal;
180 }
181};
182
183} // end llvm namespace
184
185namespace {
186
187// Test StringMapEntry::Create() method.
188TEST_F(StringMapTest, StringMapEntryTest) {
189 StringMap<uint32_t>::value_type* entry =
190 StringMap<uint32_t>::value_type::Create(
Daniel Dunbar6316fbc2009-07-23 18:17:34 +0000191 testKeyFirst, testKeyFirst + testKeyLength, 1u);
Bill Wendling6b223d72009-01-08 09:31:36 +0000192 EXPECT_STREQ(testKey, entry->first());
193 EXPECT_EQ(1u, entry->second);
Jeffrey Yasskind8735352010-02-11 07:16:13 +0000194 free(entry);
Misha Brukman8bb5e992009-01-08 04:48:20 +0000195}
Bill Wendling6b223d72009-01-08 09:31:36 +0000196
197// Test insert() method.
198TEST_F(StringMapTest, InsertTest) {
199 SCOPED_TRACE("InsertTest");
200 testMap.insert(
201 StringMap<uint32_t>::value_type::Create(
Daniel Dunbar6316fbc2009-07-23 18:17:34 +0000202 testKeyFirst, testKeyFirst + testKeyLength,
203 testMap.getAllocator(), 1u));
Bill Wendling6b223d72009-01-08 09:31:36 +0000204 assertSingleItemMap();
205}
206
207} // end anonymous namespace