blob: 66e04933adfc329ae875c28f685a45c0615e4f13 [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();
Chris Lattnerd7c02732011-07-14 18:31:43 +000054 EXPECT_STREQ(testKey, it->first().data());
Misha Brukman8bb5e992009-01-08 04:48:20 +000055 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) {
Misha Brukman8bb5e992009-01-08 04:48:20 +000078 assertEmptyMap();
79}
80
Bill Wendling6b223d72009-01-08 09:31:36 +000081// Constant map tests.
Misha Brukman8bb5e992009-01-08 04:48:20 +000082TEST_F(StringMapTest, ConstEmptyMapTest) {
83 const StringMap<uint32_t>& constTestMap = testMap;
84
85 // Size tests
86 EXPECT_EQ(0u, constTestMap.size());
87 EXPECT_TRUE(constTestMap.empty());
88
89 // Iterator tests
90 EXPECT_TRUE(constTestMap.begin() == constTestMap.end());
91
92 // Lookup tests
93 EXPECT_EQ(0u, constTestMap.count(testKey));
Daniel Dunbar6316fbc2009-07-23 18:17:34 +000094 EXPECT_EQ(0u, constTestMap.count(StringRef(testKeyFirst, testKeyLength)));
Misha Brukman8bb5e992009-01-08 04:48:20 +000095 EXPECT_EQ(0u, constTestMap.count(testKeyStr));
96 EXPECT_TRUE(constTestMap.find(testKey) == constTestMap.end());
Daniel Dunbar6316fbc2009-07-23 18:17:34 +000097 EXPECT_TRUE(constTestMap.find(StringRef(testKeyFirst, testKeyLength)) ==
Bill Wendling6b223d72009-01-08 09:31:36 +000098 constTestMap.end());
Misha Brukman8bb5e992009-01-08 04:48:20 +000099 EXPECT_TRUE(constTestMap.find(testKeyStr) == constTestMap.end());
100}
101
Bill Wendling6b223d72009-01-08 09:31:36 +0000102// A map with a single entry.
Misha Brukman8bb5e992009-01-08 04:48:20 +0000103TEST_F(StringMapTest, SingleEntryMapTest) {
Misha Brukman8bb5e992009-01-08 04:48:20 +0000104 testMap[testKey] = testValue;
105 assertSingleItemMap();
106}
107
Bill Wendling6b223d72009-01-08 09:31:36 +0000108// Test clear() method.
Misha Brukman8bb5e992009-01-08 04:48:20 +0000109TEST_F(StringMapTest, ClearTest) {
Misha Brukman8bb5e992009-01-08 04:48:20 +0000110 testMap[testKey] = testValue;
111 testMap.clear();
112 assertEmptyMap();
113}
114
Bill Wendling6b223d72009-01-08 09:31:36 +0000115// Test erase(iterator) method.
Misha Brukman8bb5e992009-01-08 04:48:20 +0000116TEST_F(StringMapTest, EraseIteratorTest) {
Misha Brukman8bb5e992009-01-08 04:48:20 +0000117 testMap[testKey] = testValue;
118 testMap.erase(testMap.begin());
119 assertEmptyMap();
120}
121
Bill Wendling6b223d72009-01-08 09:31:36 +0000122// Test erase(value) method.
Misha Brukman8bb5e992009-01-08 04:48:20 +0000123TEST_F(StringMapTest, EraseValueTest) {
Misha Brukman8bb5e992009-01-08 04:48:20 +0000124 testMap[testKey] = testValue;
125 testMap.erase(testKey);
126 assertEmptyMap();
127}
128
Bill Wendling6b223d72009-01-08 09:31:36 +0000129// Test inserting two values and erasing one.
Misha Brukman8bb5e992009-01-08 04:48:20 +0000130TEST_F(StringMapTest, InsertAndEraseTest) {
Misha Brukman8bb5e992009-01-08 04:48:20 +0000131 testMap[testKey] = testValue;
132 testMap["otherKey"] = 2;
133 testMap.erase("otherKey");
134 assertSingleItemMap();
135}
136
Bill Wendling6b223d72009-01-08 09:31:36 +0000137// A more complex iteration test.
Misha Brukman8bb5e992009-01-08 04:48:20 +0000138TEST_F(StringMapTest, IterationTest) {
139 bool visited[100];
140
141 // Insert 100 numbers into the map
142 for (int i = 0; i < 100; ++i) {
143 std::stringstream ss;
144 ss << "key_" << i;
145 testMap[ss.str()] = i;
146 visited[i] = false;
147 }
148
149 // Iterate over all numbers and mark each one found.
150 for (StringMap<uint32_t>::iterator it = testMap.begin();
151 it != testMap.end(); ++it) {
152 std::stringstream ss;
153 ss << "key_" << it->second;
Chris Lattnerd7c02732011-07-14 18:31:43 +0000154 ASSERT_STREQ(ss.str().c_str(), it->first().data());
Misha Brukman8bb5e992009-01-08 04:48:20 +0000155 visited[it->second] = true;
156 }
157
158 // Ensure every number was visited.
159 for (int i = 0; i < 100; ++i) {
160 ASSERT_TRUE(visited[i]) << "Entry #" << i << " was never visited";
161 }
162}
163
Bill Wendling6b223d72009-01-08 09:31:36 +0000164} // end anonymous namespace
165
166namespace llvm {
167
168template <>
169class StringMapEntryInitializer<uint32_t> {
170public:
171 template <typename InitTy>
172 static void Initialize(StringMapEntry<uint32_t> &T, InitTy InitVal) {
173 T.second = InitVal;
174 }
175};
176
177} // end llvm namespace
178
179namespace {
180
181// Test StringMapEntry::Create() method.
182TEST_F(StringMapTest, StringMapEntryTest) {
183 StringMap<uint32_t>::value_type* entry =
184 StringMap<uint32_t>::value_type::Create(
Daniel Dunbar6316fbc2009-07-23 18:17:34 +0000185 testKeyFirst, testKeyFirst + testKeyLength, 1u);
Chris Lattnerd7c02732011-07-14 18:31:43 +0000186 EXPECT_STREQ(testKey, entry->first().data());
Bill Wendling6b223d72009-01-08 09:31:36 +0000187 EXPECT_EQ(1u, entry->second);
Jeffrey Yasskind8735352010-02-11 07:16:13 +0000188 free(entry);
Misha Brukman8bb5e992009-01-08 04:48:20 +0000189}
Bill Wendling6b223d72009-01-08 09:31:36 +0000190
191// Test insert() method.
192TEST_F(StringMapTest, InsertTest) {
193 SCOPED_TRACE("InsertTest");
194 testMap.insert(
195 StringMap<uint32_t>::value_type::Create(
Daniel Dunbar6316fbc2009-07-23 18:17:34 +0000196 testKeyFirst, testKeyFirst + testKeyLength,
197 testMap.getAllocator(), 1u));
Bill Wendling6b223d72009-01-08 09:31:36 +0000198 assertSingleItemMap();
199}
200
201} // end anonymous namespace