llvm.org GIT mirror llvm / 8bb5e99
* Added unittests for StringMap * Fixed but in StringMap::clear() * Removed trailing whitespace Original patch by Talin. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@61914 91177308-0d34-0410-b5e6-96231b3b80d8 Misha Brukman 10 years ago
2 changed file(s) with 193 addition(s) and 2 deletion(s). Raw diff Collapse all Expand all
335335 // clear - Empties out the StringMap
336336 void clear() {
337337 if (empty()) return;
338
338
339339 // Zap all values, resetting the keys back to non-present (not tombstone),
340340 // which is safe because we're removing all elements.
341341 for (ItemBucket *I = TheTable, *E = TheTable+NumBuckets; I != E; ++I) {
344344 I->Item = 0;
345345 }
346346 }
347
348 NumItems = 0;
347349 }
348350
349351 /// GetOrCreateValue - Look up the specified key in the table. If a value
420422 StringMapImpl::ItemBucket *Ptr;
421423 public:
422424 typedef StringMapEntry value_type;
423
425
424426 explicit StringMapConstIterator(StringMapImpl::ItemBucket *Bucket,
425427 bool NoAdvance = false)
426428 : Ptr(Bucket) {
0 //===- llvm/unittest/ADT/StringMapMap.cpp - StringMap unit tests -*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "gtest/gtest.h"
10 #include "llvm/ADT/StringMap.h"
11 using namespace llvm;
12
13 namespace {
14
15 // Test fixture
16 class StringMapTest : public testing::Test {
17 protected:
18 StringMap testMap;
19
20 static const char testKey[];
21 static const uint32_t testValue;
22 static const char* testKeyFirst;
23 static const char* testKeyLast;
24 static const std::string testKeyStr;
25
26 void assertEmptyMap() {
27 // Size tests
28 EXPECT_EQ(0u, testMap.size());
29 EXPECT_TRUE(testMap.empty());
30
31 // Iterator tests
32 EXPECT_TRUE(testMap.begin() == testMap.end());
33
34 // Lookup tests
35 EXPECT_EQ(0u, testMap.count(testKey));
36 EXPECT_EQ(0u, testMap.count(testKeyFirst, testKeyLast));
37 EXPECT_EQ(0u, testMap.count(testKeyStr));
38 EXPECT_TRUE(testMap.find(testKey) == testMap.end());
39 EXPECT_TRUE(testMap.find(testKeyFirst, testKeyLast) == testMap.end());
40 EXPECT_TRUE(testMap.find(testKeyStr) == testMap.end());
41 }
42
43 void assertSingleItemMap() {
44 // Size tests
45 EXPECT_EQ(1u, testMap.size());
46 EXPECT_FALSE(testMap.begin() == testMap.end());
47 EXPECT_FALSE(testMap.empty());
48
49 // Iterator tests
50 StringMap::iterator it = testMap.begin();
51 EXPECT_STREQ(testKey, it->first());
52 EXPECT_EQ(testValue, it->second);
53 ++it;
54 EXPECT_TRUE(it == testMap.end());
55
56 // Lookup tests
57 EXPECT_EQ(1u, testMap.count(testKey));
58 EXPECT_EQ(1u, testMap.count(testKeyFirst, testKeyLast));
59 EXPECT_EQ(1u, testMap.count(testKeyStr));
60 EXPECT_TRUE(testMap.find(testKey) == testMap.begin());
61 EXPECT_TRUE(testMap.find(testKeyFirst, testKeyLast) == testMap.begin());
62 EXPECT_TRUE(testMap.find(testKeyStr) == testMap.begin());
63 }
64 };
65
66 const char StringMapTest::testKey[] = "key";
67 const uint32_t StringMapTest::testValue = 1u;
68 const char* StringMapTest::testKeyFirst = testKey;
69 const char* StringMapTest::testKeyLast = testKey + sizeof(testKey) - 1;
70 const std::string StringMapTest::testKeyStr(testKey);
71
72 // Empty map tests
73 TEST_F(StringMapTest, EmptyMapTest) {
74 SCOPED_TRACE("EmptyMapTest");
75 assertEmptyMap();
76 }
77
78 // Constant map tests
79 TEST_F(StringMapTest, ConstEmptyMapTest) {
80 const StringMap& constTestMap = testMap;
81
82 // Size tests
83 EXPECT_EQ(0u, constTestMap.size());
84 EXPECT_TRUE(constTestMap.empty());
85
86 // Iterator tests
87 EXPECT_TRUE(constTestMap.begin() == constTestMap.end());
88
89 // Lookup tests
90 EXPECT_EQ(0u, constTestMap.count(testKey));
91 EXPECT_EQ(0u, constTestMap.count(testKeyFirst, testKeyLast));
92 EXPECT_EQ(0u, constTestMap.count(testKeyStr));
93 EXPECT_TRUE(constTestMap.find(testKey) == constTestMap.end());
94 EXPECT_TRUE(constTestMap.find(testKeyFirst, testKeyLast) ==
95 constTestMap.end());
96 EXPECT_TRUE(constTestMap.find(testKeyStr) == constTestMap.end());
97 }
98
99 // A map with a single entry
100 TEST_F(StringMapTest, SingleEntryMapTest) {
101 SCOPED_TRACE("SingleEntryMapTest");
102 testMap[testKey] = testValue;
103 assertSingleItemMap();
104 }
105
106 // Test clear() method
107 TEST_F(StringMapTest, ClearTest) {
108 SCOPED_TRACE("ClearTest");
109 testMap[testKey] = testValue;
110 testMap.clear();
111 assertEmptyMap();
112 }
113
114 // Test erase(iterator) method
115 TEST_F(StringMapTest, EraseIteratorTest) {
116 SCOPED_TRACE("EraseIteratorTest");
117 testMap[testKey] = testValue;
118 testMap.erase(testMap.begin());
119 assertEmptyMap();
120 }
121
122 // Test erase(value) method
123 TEST_F(StringMapTest, EraseValueTest) {
124 SCOPED_TRACE("EraseValueTest");
125 testMap[testKey] = testValue;
126 testMap.erase(testKey);
127 assertEmptyMap();
128 }
129
130 // Test inserting two values and erasing one
131 TEST_F(StringMapTest, InsertAndEraseTest) {
132 SCOPED_TRACE("InsertAndEraseTest");
133 testMap[testKey] = testValue;
134 testMap["otherKey"] = 2;
135 testMap.erase("otherKey");
136 assertSingleItemMap();
137 }
138
139 // Test StringMapEntry::Create() method.
140 // DISABLED because this fails without a StringMapEntryInitializer, and
141 // I can't get it to compile with one.
142 TEST_F(StringMapTest, DISABLED_StringMapEntryTest) {
143 StringMap::value_type* entry =
144 StringMap::value_type::Create(
145 testKeyFirst, testKeyLast, 1u);
146 EXPECT_STREQ(testKey, entry->first());
147 EXPECT_EQ(1u, entry->second);
148 }
149
150 // Test insert() method
151 // DISABLED because this fails without a StringMapEntryInitializer, and
152 // I can't get it to compile with one.
153 TEST_F(StringMapTest, DISABLED_InsertTest) {
154 SCOPED_TRACE("InsertTest");
155 testMap.insert(
156 StringMap::value_type::Create(
157 testKeyFirst, testKeyLast, testMap.getAllocator(), 1u));
158 assertSingleItemMap();
159 }
160
161 // A more complex iteration test
162 TEST_F(StringMapTest, IterationTest) {
163 bool visited[100];
164
165 // Insert 100 numbers into the map
166 for (int i = 0; i < 100; ++i) {
167 std::stringstream ss;
168 ss << "key_" << i;
169 testMap[ss.str()] = i;
170 visited[i] = false;
171 }
172
173 // Iterate over all numbers and mark each one found.
174 for (StringMap::iterator it = testMap.begin();
175 it != testMap.end(); ++it) {
176 std::stringstream ss;
177 ss << "key_" << it->second;
178 ASSERT_STREQ(ss.str().c_str(), it->first());
179 visited[it->second] = true;
180 }
181
182 // Ensure every number was visited.
183 for (int i = 0; i < 100; ++i) {
184 ASSERT_TRUE(visited[i]) << "Entry #" << i << " was never visited";
185 }
186 }
187
188 }