xref: /llvm-project/llvm/unittests/ADT/StringMapTest.cpp (revision e32cd94b47156efadb7afd768073d662bd3a22d1)
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 //===----------------------------------------------------------------------===//
9 
10 #include "gtest/gtest.h"
11 #include "llvm/ADT/StringMap.h"
12 #include "llvm/Support/DataTypes.h"
13 using namespace llvm;
14 
15 namespace {
16 
17 // Test fixture
18 class StringMapTest : public testing::Test {
19 protected:
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 
68 const char StringMapTest::testKey[] = "key";
69 const uint32_t StringMapTest::testValue = 1u;
70 const char* StringMapTest::testKeyFirst = testKey;
71 const char* StringMapTest::testKeyLast = testKey + sizeof(testKey) - 1;
72 const std::string StringMapTest::testKeyStr(testKey);
73 
74 // Empty map tests.
75 TEST_F(StringMapTest, EmptyMapTest) {
76   SCOPED_TRACE("EmptyMapTest");
77   assertEmptyMap();
78 }
79 
80 // Constant map tests.
81 TEST_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) ==
97               constTestMap.end());
98   EXPECT_TRUE(constTestMap.find(testKeyStr) == constTestMap.end());
99 }
100 
101 // A map with a single entry.
102 TEST_F(StringMapTest, SingleEntryMapTest) {
103   SCOPED_TRACE("SingleEntryMapTest");
104   testMap[testKey] = testValue;
105   assertSingleItemMap();
106 }
107 
108 // Test clear() method.
109 TEST_F(StringMapTest, ClearTest) {
110   SCOPED_TRACE("ClearTest");
111   testMap[testKey] = testValue;
112   testMap.clear();
113   assertEmptyMap();
114 }
115 
116 // Test erase(iterator) method.
117 TEST_F(StringMapTest, EraseIteratorTest) {
118   SCOPED_TRACE("EraseIteratorTest");
119   testMap[testKey] = testValue;
120   testMap.erase(testMap.begin());
121   assertEmptyMap();
122 }
123 
124 // Test erase(value) method.
125 TEST_F(StringMapTest, EraseValueTest) {
126   SCOPED_TRACE("EraseValueTest");
127   testMap[testKey] = testValue;
128   testMap.erase(testKey);
129   assertEmptyMap();
130 }
131 
132 // Test inserting two values and erasing one.
133 TEST_F(StringMapTest, InsertAndEraseTest) {
134   SCOPED_TRACE("InsertAndEraseTest");
135   testMap[testKey] = testValue;
136   testMap["otherKey"] = 2;
137   testMap.erase("otherKey");
138   assertSingleItemMap();
139 }
140 
141 // A more complex iteration test.
142 TEST_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 
168 } // end anonymous namespace
169 
170 namespace llvm {
171 
172 template <>
173 class StringMapEntryInitializer<uint32_t> {
174 public:
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 
183 namespace {
184 
185 // Test StringMapEntry::Create() method.
186 TEST_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);
192 }
193 
194 // Test insert() method.
195 TEST_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
204