Fix comment to use llvm 2.x syntax.
[llvm/stm8.git] / unittests / ADT / StringMapTest.cpp
blobea91348a5bdf730e92085d5778ad1c78b6de18bc
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 #include "llvm/Support/DataTypes.h"
13 using namespace llvm;
15 namespace {
17 // Test fixture
18 class StringMapTest : public testing::Test {
19 protected:
20 StringMap<uint32_t> testMap;
22 static const char testKey[];
23 static const uint32_t testValue;
24 static const char* testKeyFirst;
25 static size_t testKeyLength;
26 static const std::string testKeyStr;
28 void assertEmptyMap() {
29 // Size tests
30 EXPECT_EQ(0u, testMap.size());
31 EXPECT_TRUE(testMap.empty());
33 // Iterator tests
34 EXPECT_TRUE(testMap.begin() == testMap.end());
36 // Lookup tests
37 EXPECT_EQ(0u, testMap.count(testKey));
38 EXPECT_EQ(0u, testMap.count(StringRef(testKeyFirst, testKeyLength)));
39 EXPECT_EQ(0u, testMap.count(testKeyStr));
40 EXPECT_TRUE(testMap.find(testKey) == testMap.end());
41 EXPECT_TRUE(testMap.find(StringRef(testKeyFirst, testKeyLength)) ==
42 testMap.end());
43 EXPECT_TRUE(testMap.find(testKeyStr) == testMap.end());
46 void assertSingleItemMap() {
47 // Size tests
48 EXPECT_EQ(1u, testMap.size());
49 EXPECT_FALSE(testMap.begin() == testMap.end());
50 EXPECT_FALSE(testMap.empty());
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());
59 // Lookup tests
60 EXPECT_EQ(1u, testMap.count(testKey));
61 EXPECT_EQ(1u, testMap.count(StringRef(testKeyFirst, testKeyLength)));
62 EXPECT_EQ(1u, testMap.count(testKeyStr));
63 EXPECT_TRUE(testMap.find(testKey) == testMap.begin());
64 EXPECT_TRUE(testMap.find(StringRef(testKeyFirst, testKeyLength)) ==
65 testMap.begin());
66 EXPECT_TRUE(testMap.find(testKeyStr) == testMap.begin());
70 const char StringMapTest::testKey[] = "key";
71 const uint32_t StringMapTest::testValue = 1u;
72 const char* StringMapTest::testKeyFirst = testKey;
73 size_t StringMapTest::testKeyLength = sizeof(testKey) - 1;
74 const std::string StringMapTest::testKeyStr(testKey);
76 // Empty map tests.
77 TEST_F(StringMapTest, EmptyMapTest) {
78 SCOPED_TRACE("EmptyMapTest");
79 assertEmptyMap();
82 // Constant map tests.
83 TEST_F(StringMapTest, ConstEmptyMapTest) {
84 const StringMap<uint32_t>& constTestMap = testMap;
86 // Size tests
87 EXPECT_EQ(0u, constTestMap.size());
88 EXPECT_TRUE(constTestMap.empty());
90 // Iterator tests
91 EXPECT_TRUE(constTestMap.begin() == constTestMap.end());
93 // Lookup tests
94 EXPECT_EQ(0u, constTestMap.count(testKey));
95 EXPECT_EQ(0u, constTestMap.count(StringRef(testKeyFirst, testKeyLength)));
96 EXPECT_EQ(0u, constTestMap.count(testKeyStr));
97 EXPECT_TRUE(constTestMap.find(testKey) == constTestMap.end());
98 EXPECT_TRUE(constTestMap.find(StringRef(testKeyFirst, testKeyLength)) ==
99 constTestMap.end());
100 EXPECT_TRUE(constTestMap.find(testKeyStr) == constTestMap.end());
103 // A map with a single entry.
104 TEST_F(StringMapTest, SingleEntryMapTest) {
105 SCOPED_TRACE("SingleEntryMapTest");
106 testMap[testKey] = testValue;
107 assertSingleItemMap();
110 // Test clear() method.
111 TEST_F(StringMapTest, ClearTest) {
112 SCOPED_TRACE("ClearTest");
113 testMap[testKey] = testValue;
114 testMap.clear();
115 assertEmptyMap();
118 // Test erase(iterator) method.
119 TEST_F(StringMapTest, EraseIteratorTest) {
120 SCOPED_TRACE("EraseIteratorTest");
121 testMap[testKey] = testValue;
122 testMap.erase(testMap.begin());
123 assertEmptyMap();
126 // Test erase(value) method.
127 TEST_F(StringMapTest, EraseValueTest) {
128 SCOPED_TRACE("EraseValueTest");
129 testMap[testKey] = testValue;
130 testMap.erase(testKey);
131 assertEmptyMap();
134 // Test inserting two values and erasing one.
135 TEST_F(StringMapTest, InsertAndEraseTest) {
136 SCOPED_TRACE("InsertAndEraseTest");
137 testMap[testKey] = testValue;
138 testMap["otherKey"] = 2;
139 testMap.erase("otherKey");
140 assertSingleItemMap();
143 // A more complex iteration test.
144 TEST_F(StringMapTest, IterationTest) {
145 bool visited[100];
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;
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;
164 // Ensure every number was visited.
165 for (int i = 0; i < 100; ++i) {
166 ASSERT_TRUE(visited[i]) << "Entry #" << i << " was never visited";
170 } // end anonymous namespace
172 namespace llvm {
174 template <>
175 class StringMapEntryInitializer<uint32_t> {
176 public:
177 template <typename InitTy>
178 static void Initialize(StringMapEntry<uint32_t> &T, InitTy InitVal) {
179 T.second = InitVal;
183 } // end llvm namespace
185 namespace {
187 // Test StringMapEntry::Create() method.
188 TEST_F(StringMapTest, StringMapEntryTest) {
189 StringMap<uint32_t>::value_type* entry =
190 StringMap<uint32_t>::value_type::Create(
191 testKeyFirst, testKeyFirst + testKeyLength, 1u);
192 EXPECT_STREQ(testKey, entry->first());
193 EXPECT_EQ(1u, entry->second);
194 free(entry);
197 // Test insert() method.
198 TEST_F(StringMapTest, InsertTest) {
199 SCOPED_TRACE("InsertTest");
200 testMap.insert(
201 StringMap<uint32_t>::value_type::Create(
202 testKeyFirst, testKeyFirst + testKeyLength,
203 testMap.getAllocator(), 1u));
204 assertSingleItemMap();
207 } // end anonymous namespace