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 using namespace llvm; 13 14 namespace llvm { 15 16 template <> 17 class StringMapEntryInitializer<uint32_t> { 18 public: 19 template <typename InitTy> 20 static void Initialize(StringMapEntry<uint32_t> &T, InitTy InitVal) { 21 T.second = InitVal; 22 } 23 }; 24 25 } 26 27 namespace { 28 29 // Test fixture 30 class StringMapTest : public testing::Test { 31 protected: 32 StringMap<uint32_t> testMap; 33 34 static const char testKey[]; 35 static const uint32_t testValue; 36 static const char* testKeyFirst; 37 static const char* testKeyLast; 38 static const std::string testKeyStr; 39 40 void assertEmptyMap() { 41 // Size tests 42 EXPECT_EQ(0u, testMap.size()); 43 EXPECT_TRUE(testMap.empty()); 44 45 // Iterator tests 46 EXPECT_TRUE(testMap.begin() == testMap.end()); 47 48 // Lookup tests 49 EXPECT_EQ(0u, testMap.count(testKey)); 50 EXPECT_EQ(0u, testMap.count(testKeyFirst, testKeyLast)); 51 EXPECT_EQ(0u, testMap.count(testKeyStr)); 52 EXPECT_TRUE(testMap.find(testKey) == testMap.end()); 53 EXPECT_TRUE(testMap.find(testKeyFirst, testKeyLast) == testMap.end()); 54 EXPECT_TRUE(testMap.find(testKeyStr) == testMap.end()); 55 } 56 57 void assertSingleItemMap() { 58 // Size tests 59 EXPECT_EQ(1u, testMap.size()); 60 EXPECT_FALSE(testMap.begin() == testMap.end()); 61 EXPECT_FALSE(testMap.empty()); 62 63 // Iterator tests 64 StringMap<uint32_t>::iterator it = testMap.begin(); 65 EXPECT_STREQ(testKey, it->first()); 66 EXPECT_EQ(testValue, it->second); 67 ++it; 68 EXPECT_TRUE(it == testMap.end()); 69 70 // Lookup tests 71 EXPECT_EQ(1u, testMap.count(testKey)); 72 EXPECT_EQ(1u, testMap.count(testKeyFirst, testKeyLast)); 73 EXPECT_EQ(1u, testMap.count(testKeyStr)); 74 EXPECT_TRUE(testMap.find(testKey) == testMap.begin()); 75 EXPECT_TRUE(testMap.find(testKeyFirst, testKeyLast) == testMap.begin()); 76 EXPECT_TRUE(testMap.find(testKeyStr) == testMap.begin()); 77 } 78 }; 79 80 const char StringMapTest::testKey[] = "key"; 81 const uint32_t StringMapTest::testValue = 1u; 82 const char* StringMapTest::testKeyFirst = testKey; 83 const char* StringMapTest::testKeyLast = testKey + sizeof(testKey) - 1; 84 const std::string StringMapTest::testKeyStr(testKey); 85 86 // Empty map tests 87 TEST_F(StringMapTest, EmptyMapTest) { 88 SCOPED_TRACE("EmptyMapTest"); 89 assertEmptyMap(); 90 } 91 92 // Constant map tests 93 TEST_F(StringMapTest, ConstEmptyMapTest) { 94 const StringMap<uint32_t>& constTestMap = testMap; 95 96 // Size tests 97 EXPECT_EQ(0u, constTestMap.size()); 98 EXPECT_TRUE(constTestMap.empty()); 99 100 // Iterator tests 101 EXPECT_TRUE(constTestMap.begin() == constTestMap.end()); 102 103 // Lookup tests 104 EXPECT_EQ(0u, constTestMap.count(testKey)); 105 EXPECT_EQ(0u, constTestMap.count(testKeyFirst, testKeyLast)); 106 EXPECT_EQ(0u, constTestMap.count(testKeyStr)); 107 EXPECT_TRUE(constTestMap.find(testKey) == constTestMap.end()); 108 EXPECT_TRUE(constTestMap.find(testKeyFirst, testKeyLast) == 109 constTestMap.end()); 110 EXPECT_TRUE(constTestMap.find(testKeyStr) == constTestMap.end()); 111 } 112 113 // A map with a single entry 114 TEST_F(StringMapTest, SingleEntryMapTest) { 115 SCOPED_TRACE("SingleEntryMapTest"); 116 testMap[testKey] = testValue; 117 assertSingleItemMap(); 118 } 119 120 // Test clear() method 121 TEST_F(StringMapTest, ClearTest) { 122 SCOPED_TRACE("ClearTest"); 123 testMap[testKey] = testValue; 124 testMap.clear(); 125 assertEmptyMap(); 126 } 127 128 // Test erase(iterator) method 129 TEST_F(StringMapTest, EraseIteratorTest) { 130 SCOPED_TRACE("EraseIteratorTest"); 131 testMap[testKey] = testValue; 132 testMap.erase(testMap.begin()); 133 assertEmptyMap(); 134 } 135 136 // Test erase(value) method 137 TEST_F(StringMapTest, EraseValueTest) { 138 SCOPED_TRACE("EraseValueTest"); 139 testMap[testKey] = testValue; 140 testMap.erase(testKey); 141 assertEmptyMap(); 142 } 143 144 // Test inserting two values and erasing one 145 TEST_F(StringMapTest, InsertAndEraseTest) { 146 SCOPED_TRACE("InsertAndEraseTest"); 147 testMap[testKey] = testValue; 148 testMap["otherKey"] = 2; 149 testMap.erase("otherKey"); 150 assertSingleItemMap(); 151 } 152 153 // Test StringMapEntry::Create() method. 154 // DISABLED because this fails without a StringMapEntryInitializer, and 155 // I can't get it to compile with one. 156 TEST_F(StringMapTest, StringMapEntryTest) { 157 MallocAllocator A; 158 StringMap<uint32_t>::value_type* entry = 159 StringMap<uint32_t>::value_type::Create( 160 testKeyFirst, testKeyLast, A, 1u); 161 EXPECT_STREQ(testKey, entry->first()); 162 EXPECT_EQ(1u, entry->second); 163 } 164 165 // Test insert() method 166 // DISABLED because this fails without a StringMapEntryInitializer, and 167 // I can't get it to compile with one. 168 TEST_F(StringMapTest, InsertTest) { 169 SCOPED_TRACE("InsertTest"); 170 testMap.insert( 171 StringMap<uint32_t>::value_type::Create( 172 testKeyFirst, testKeyLast, testMap.getAllocator(), 1u)); 173 assertSingleItemMap(); 174 } 175 176 // A more complex iteration test 177 TEST_F(StringMapTest, IterationTest) { 178 bool visited[100]; 179 180 // Insert 100 numbers into the map 181 for (int i = 0; i < 100; ++i) { 182 std::stringstream ss; 183 ss << "key_" << i; 184 testMap[ss.str()] = i; 185 visited[i] = false; 186 } 187 188 // Iterate over all numbers and mark each one found. 189 for (StringMap<uint32_t>::iterator it = testMap.begin(); 190 it != testMap.end(); ++it) { 191 std::stringstream ss; 192 ss << "key_" << it->second; 193 ASSERT_STREQ(ss.str().c_str(), it->first()); 194 visited[it->second] = true; 195 } 196 197 // Ensure every number was visited. 198 for (int i = 0; i < 100; ++i) { 199 ASSERT_TRUE(visited[i]) << "Entry #" << i << " was never visited"; 200 } 201 } 202 203 } 204