blob: 7bb2391eff1632bee317072897d824ff71856dd8 [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"
Torok Edwin05717f92009-07-21 07:09:05 +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;
25 static const char* testKeyLast;
26 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));
38 EXPECT_EQ(0u, testMap.count(testKeyFirst, testKeyLast));
39 EXPECT_EQ(0u, testMap.count(testKeyStr));
40 EXPECT_TRUE(testMap.find(testKey) == testMap.end());
41 EXPECT_TRUE(testMap.find(testKeyFirst, testKeyLast) == testMap.end());
42 EXPECT_TRUE(testMap.find(testKeyStr) == testMap.end());
43 }
44
45 void assertSingleItemMap() {
46 // Size tests
47 EXPECT_EQ(1u, testMap.size());
48 EXPECT_FALSE(testMap.begin() == testMap.end());
49 EXPECT_FALSE(testMap.empty());
50
51 // Iterator tests
52 StringMap<uint32_t>::iterator it = testMap.begin();
53 EXPECT_STREQ(testKey, it->first());
54 EXPECT_EQ(testValue, it->second);
55 ++it;
56 EXPECT_TRUE(it == testMap.end());
57
58 // Lookup tests
59 EXPECT_EQ(1u, testMap.count(testKey));
60 EXPECT_EQ(1u, testMap.count(testKeyFirst, testKeyLast));
61 EXPECT_EQ(1u, testMap.count(testKeyStr));
62 EXPECT_TRUE(testMap.find(testKey) == testMap.begin());
63 EXPECT_TRUE(testMap.find(testKeyFirst, testKeyLast) == testMap.begin());
64 EXPECT_TRUE(testMap.find(testKeyStr) == testMap.begin());
65 }
66};
67
68const char StringMapTest::testKey[] = "key";
69const uint32_t StringMapTest::testValue = 1u;
70const char* StringMapTest::testKeyFirst = testKey;
71const char* StringMapTest::testKeyLast = testKey + sizeof(testKey) - 1;
72const std::string StringMapTest::testKeyStr(testKey);
73
Bill Wendling6b223d72009-01-08 09:31:36 +000074// Empty map tests.
Misha Brukman8bb5e992009-01-08 04:48:20 +000075TEST_F(StringMapTest, EmptyMapTest) {
76 SCOPED_TRACE("EmptyMapTest");
77 assertEmptyMap();
78}
79
Bill Wendling6b223d72009-01-08 09:31:36 +000080// Constant map tests.
Misha Brukman8bb5e992009-01-08 04:48:20 +000081TEST_F(StringMapTest, ConstEmptyMapTest) {
82 const StringMap<uint32_t>& constTestMap = testMap;
83
84 // Size tests
85 EXPECT_EQ(0u, constTestMap.size());
86 EXPECT_TRUE(constTestMap.empty());
87
88 // Iterator tests
89 EXPECT_TRUE(constTestMap.begin() == constTestMap.end());
90
91 // Lookup tests
92 EXPECT_EQ(0u, constTestMap.count(testKey));
93 EXPECT_EQ(0u, constTestMap.count(testKeyFirst, testKeyLast));
94 EXPECT_EQ(0u, constTestMap.count(testKeyStr));
95 EXPECT_TRUE(constTestMap.find(testKey) == constTestMap.end());
96 EXPECT_TRUE(constTestMap.find(testKeyFirst, testKeyLast) ==
Bill Wendling6b223d72009-01-08 09:31:36 +000097 constTestMap.end());
Misha Brukman8bb5e992009-01-08 04:48:20 +000098 EXPECT_TRUE(constTestMap.find(testKeyStr) == constTestMap.end());
99}
100
Bill Wendling6b223d72009-01-08 09:31:36 +0000101// A map with a single entry.
Misha Brukman8bb5e992009-01-08 04:48:20 +0000102TEST_F(StringMapTest, SingleEntryMapTest) {
103 SCOPED_TRACE("SingleEntryMapTest");
104 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) {
110 SCOPED_TRACE("ClearTest");
111 testMap[testKey] = testValue;
112 testMap.clear();
113 assertEmptyMap();
114}
115
Bill Wendling6b223d72009-01-08 09:31:36 +0000116// Test erase(iterator) method.
Misha Brukman8bb5e992009-01-08 04:48:20 +0000117TEST_F(StringMapTest, EraseIteratorTest) {
118 SCOPED_TRACE("EraseIteratorTest");
119 testMap[testKey] = testValue;
120 testMap.erase(testMap.begin());
121 assertEmptyMap();
122}
123
Bill Wendling6b223d72009-01-08 09:31:36 +0000124// Test erase(value) method.
Misha Brukman8bb5e992009-01-08 04:48:20 +0000125TEST_F(StringMapTest, EraseValueTest) {
126 SCOPED_TRACE("EraseValueTest");
127 testMap[testKey] = testValue;
128 testMap.erase(testKey);
129 assertEmptyMap();
130}
131
Bill Wendling6b223d72009-01-08 09:31:36 +0000132// Test inserting two values and erasing one.
Misha Brukman8bb5e992009-01-08 04:48:20 +0000133TEST_F(StringMapTest, InsertAndEraseTest) {
134 SCOPED_TRACE("InsertAndEraseTest");
135 testMap[testKey] = testValue;
136 testMap["otherKey"] = 2;
137 testMap.erase("otherKey");
138 assertSingleItemMap();
139}
140
Bill Wendling6b223d72009-01-08 09:31:36 +0000141// A more complex iteration test.
Misha Brukman8bb5e992009-01-08 04:48:20 +0000142TEST_F(StringMapTest, IterationTest) {
143 bool visited[100];
144
145 // Insert 100 numbers into the map
146 for (int i = 0; i < 100; ++i) {
147 std::stringstream ss;
148 ss << "key_" << i;
149 testMap[ss.str()] = i;
150 visited[i] = false;
151 }
152
153 // Iterate over all numbers and mark each one found.
154 for (StringMap<uint32_t>::iterator it = testMap.begin();
155 it != testMap.end(); ++it) {
156 std::stringstream ss;
157 ss << "key_" << it->second;
158 ASSERT_STREQ(ss.str().c_str(), it->first());
159 visited[it->second] = true;
160 }
161
162 // Ensure every number was visited.
163 for (int i = 0; i < 100; ++i) {
164 ASSERT_TRUE(visited[i]) << "Entry #" << i << " was never visited";
165 }
166}
167
Bill Wendling6b223d72009-01-08 09:31:36 +0000168} // end anonymous namespace
169
170namespace llvm {
171
172template <>
173class StringMapEntryInitializer<uint32_t> {
174public:
175 template <typename InitTy>
176 static void Initialize(StringMapEntry<uint32_t> &T, InitTy InitVal) {
177 T.second = InitVal;
178 }
179};
180
181} // end llvm namespace
182
183namespace {
184
185// Test StringMapEntry::Create() method.
186TEST_F(StringMapTest, StringMapEntryTest) {
187 StringMap<uint32_t>::value_type* entry =
188 StringMap<uint32_t>::value_type::Create(
189 testKeyFirst, testKeyLast, 1u);
190 EXPECT_STREQ(testKey, entry->first());
191 EXPECT_EQ(1u, entry->second);
Misha Brukman8bb5e992009-01-08 04:48:20 +0000192}
Bill Wendling6b223d72009-01-08 09:31:36 +0000193
194// Test insert() method.
195TEST_F(StringMapTest, InsertTest) {
196 SCOPED_TRACE("InsertTest");
197 testMap.insert(
198 StringMap<uint32_t>::value_type::Create(
199 testKeyFirst, testKeyLast, testMap.getAllocator(), 1u));
200 assertSingleItemMap();
201}
202
203} // end anonymous namespace