Bill Wendling | 1ed3663 | 2009-01-08 07:35:39 +0000 | [diff] [blame] | 1 | //===- llvm/unittest/ADT/StringMapMap.cpp - StringMap unit tests ----------===// |
Misha Brukman | 8bb5e99 | 2009-01-08 04:48:20 +0000 | [diff] [blame] | 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" |
Michael J. Spencer | 1f6efa3 | 2010-11-29 18:16:10 +0000 | [diff] [blame^] | 12 | #include "llvm/Support/DataTypes.h" |
Misha Brukman | 8bb5e99 | 2009-01-08 04:48:20 +0000 | [diff] [blame] | 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; |
Daniel Dunbar | 6316fbc | 2009-07-23 18:17:34 +0000 | [diff] [blame] | 25 | static size_t testKeyLength; |
Misha Brukman | 8bb5e99 | 2009-01-08 04:48:20 +0000 | [diff] [blame] | 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)); |
Daniel Dunbar | 6316fbc | 2009-07-23 18:17:34 +0000 | [diff] [blame] | 38 | EXPECT_EQ(0u, testMap.count(StringRef(testKeyFirst, testKeyLength))); |
Misha Brukman | 8bb5e99 | 2009-01-08 04:48:20 +0000 | [diff] [blame] | 39 | EXPECT_EQ(0u, testMap.count(testKeyStr)); |
| 40 | EXPECT_TRUE(testMap.find(testKey) == testMap.end()); |
Daniel Dunbar | 6316fbc | 2009-07-23 18:17:34 +0000 | [diff] [blame] | 41 | EXPECT_TRUE(testMap.find(StringRef(testKeyFirst, testKeyLength)) == |
| 42 | testMap.end()); |
Misha Brukman | 8bb5e99 | 2009-01-08 04:48:20 +0000 | [diff] [blame] | 43 | EXPECT_TRUE(testMap.find(testKeyStr) == testMap.end()); |
| 44 | } |
| 45 | |
| 46 | void assertSingleItemMap() { |
| 47 | // Size tests |
| 48 | EXPECT_EQ(1u, testMap.size()); |
| 49 | EXPECT_FALSE(testMap.begin() == testMap.end()); |
| 50 | EXPECT_FALSE(testMap.empty()); |
| 51 | |
| 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()); |
| 58 | |
| 59 | // Lookup tests |
| 60 | EXPECT_EQ(1u, testMap.count(testKey)); |
Daniel Dunbar | 6316fbc | 2009-07-23 18:17:34 +0000 | [diff] [blame] | 61 | EXPECT_EQ(1u, testMap.count(StringRef(testKeyFirst, testKeyLength))); |
Misha Brukman | 8bb5e99 | 2009-01-08 04:48:20 +0000 | [diff] [blame] | 62 | EXPECT_EQ(1u, testMap.count(testKeyStr)); |
| 63 | EXPECT_TRUE(testMap.find(testKey) == testMap.begin()); |
Daniel Dunbar | 6316fbc | 2009-07-23 18:17:34 +0000 | [diff] [blame] | 64 | EXPECT_TRUE(testMap.find(StringRef(testKeyFirst, testKeyLength)) == |
| 65 | testMap.begin()); |
Misha Brukman | 8bb5e99 | 2009-01-08 04:48:20 +0000 | [diff] [blame] | 66 | EXPECT_TRUE(testMap.find(testKeyStr) == testMap.begin()); |
| 67 | } |
| 68 | }; |
| 69 | |
| 70 | const char StringMapTest::testKey[] = "key"; |
| 71 | const uint32_t StringMapTest::testValue = 1u; |
| 72 | const char* StringMapTest::testKeyFirst = testKey; |
Daniel Dunbar | 6316fbc | 2009-07-23 18:17:34 +0000 | [diff] [blame] | 73 | size_t StringMapTest::testKeyLength = sizeof(testKey) - 1; |
Misha Brukman | 8bb5e99 | 2009-01-08 04:48:20 +0000 | [diff] [blame] | 74 | const std::string StringMapTest::testKeyStr(testKey); |
| 75 | |
Bill Wendling | 6b223d7 | 2009-01-08 09:31:36 +0000 | [diff] [blame] | 76 | // Empty map tests. |
Misha Brukman | 8bb5e99 | 2009-01-08 04:48:20 +0000 | [diff] [blame] | 77 | TEST_F(StringMapTest, EmptyMapTest) { |
| 78 | SCOPED_TRACE("EmptyMapTest"); |
| 79 | assertEmptyMap(); |
| 80 | } |
| 81 | |
Bill Wendling | 6b223d7 | 2009-01-08 09:31:36 +0000 | [diff] [blame] | 82 | // Constant map tests. |
Misha Brukman | 8bb5e99 | 2009-01-08 04:48:20 +0000 | [diff] [blame] | 83 | TEST_F(StringMapTest, ConstEmptyMapTest) { |
| 84 | const StringMap<uint32_t>& constTestMap = testMap; |
| 85 | |
| 86 | // Size tests |
| 87 | EXPECT_EQ(0u, constTestMap.size()); |
| 88 | EXPECT_TRUE(constTestMap.empty()); |
| 89 | |
| 90 | // Iterator tests |
| 91 | EXPECT_TRUE(constTestMap.begin() == constTestMap.end()); |
| 92 | |
| 93 | // Lookup tests |
| 94 | EXPECT_EQ(0u, constTestMap.count(testKey)); |
Daniel Dunbar | 6316fbc | 2009-07-23 18:17:34 +0000 | [diff] [blame] | 95 | EXPECT_EQ(0u, constTestMap.count(StringRef(testKeyFirst, testKeyLength))); |
Misha Brukman | 8bb5e99 | 2009-01-08 04:48:20 +0000 | [diff] [blame] | 96 | EXPECT_EQ(0u, constTestMap.count(testKeyStr)); |
| 97 | EXPECT_TRUE(constTestMap.find(testKey) == constTestMap.end()); |
Daniel Dunbar | 6316fbc | 2009-07-23 18:17:34 +0000 | [diff] [blame] | 98 | EXPECT_TRUE(constTestMap.find(StringRef(testKeyFirst, testKeyLength)) == |
Bill Wendling | 6b223d7 | 2009-01-08 09:31:36 +0000 | [diff] [blame] | 99 | constTestMap.end()); |
Misha Brukman | 8bb5e99 | 2009-01-08 04:48:20 +0000 | [diff] [blame] | 100 | EXPECT_TRUE(constTestMap.find(testKeyStr) == constTestMap.end()); |
| 101 | } |
| 102 | |
Bill Wendling | 6b223d7 | 2009-01-08 09:31:36 +0000 | [diff] [blame] | 103 | // A map with a single entry. |
Misha Brukman | 8bb5e99 | 2009-01-08 04:48:20 +0000 | [diff] [blame] | 104 | TEST_F(StringMapTest, SingleEntryMapTest) { |
| 105 | SCOPED_TRACE("SingleEntryMapTest"); |
| 106 | testMap[testKey] = testValue; |
| 107 | assertSingleItemMap(); |
| 108 | } |
| 109 | |
Bill Wendling | 6b223d7 | 2009-01-08 09:31:36 +0000 | [diff] [blame] | 110 | // Test clear() method. |
Misha Brukman | 8bb5e99 | 2009-01-08 04:48:20 +0000 | [diff] [blame] | 111 | TEST_F(StringMapTest, ClearTest) { |
| 112 | SCOPED_TRACE("ClearTest"); |
| 113 | testMap[testKey] = testValue; |
| 114 | testMap.clear(); |
| 115 | assertEmptyMap(); |
| 116 | } |
| 117 | |
Bill Wendling | 6b223d7 | 2009-01-08 09:31:36 +0000 | [diff] [blame] | 118 | // Test erase(iterator) method. |
Misha Brukman | 8bb5e99 | 2009-01-08 04:48:20 +0000 | [diff] [blame] | 119 | TEST_F(StringMapTest, EraseIteratorTest) { |
| 120 | SCOPED_TRACE("EraseIteratorTest"); |
| 121 | testMap[testKey] = testValue; |
| 122 | testMap.erase(testMap.begin()); |
| 123 | assertEmptyMap(); |
| 124 | } |
| 125 | |
Bill Wendling | 6b223d7 | 2009-01-08 09:31:36 +0000 | [diff] [blame] | 126 | // Test erase(value) method. |
Misha Brukman | 8bb5e99 | 2009-01-08 04:48:20 +0000 | [diff] [blame] | 127 | TEST_F(StringMapTest, EraseValueTest) { |
| 128 | SCOPED_TRACE("EraseValueTest"); |
| 129 | testMap[testKey] = testValue; |
| 130 | testMap.erase(testKey); |
| 131 | assertEmptyMap(); |
| 132 | } |
| 133 | |
Bill Wendling | 6b223d7 | 2009-01-08 09:31:36 +0000 | [diff] [blame] | 134 | // Test inserting two values and erasing one. |
Misha Brukman | 8bb5e99 | 2009-01-08 04:48:20 +0000 | [diff] [blame] | 135 | TEST_F(StringMapTest, InsertAndEraseTest) { |
| 136 | SCOPED_TRACE("InsertAndEraseTest"); |
| 137 | testMap[testKey] = testValue; |
| 138 | testMap["otherKey"] = 2; |
| 139 | testMap.erase("otherKey"); |
| 140 | assertSingleItemMap(); |
| 141 | } |
| 142 | |
Bill Wendling | 6b223d7 | 2009-01-08 09:31:36 +0000 | [diff] [blame] | 143 | // A more complex iteration test. |
Misha Brukman | 8bb5e99 | 2009-01-08 04:48:20 +0000 | [diff] [blame] | 144 | TEST_F(StringMapTest, IterationTest) { |
| 145 | bool visited[100]; |
| 146 | |
| 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; |
| 153 | } |
| 154 | |
| 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; |
| 162 | } |
| 163 | |
| 164 | // Ensure every number was visited. |
| 165 | for (int i = 0; i < 100; ++i) { |
| 166 | ASSERT_TRUE(visited[i]) << "Entry #" << i << " was never visited"; |
| 167 | } |
| 168 | } |
| 169 | |
Bill Wendling | 6b223d7 | 2009-01-08 09:31:36 +0000 | [diff] [blame] | 170 | } // end anonymous namespace |
| 171 | |
| 172 | namespace llvm { |
| 173 | |
| 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; |
| 180 | } |
| 181 | }; |
| 182 | |
| 183 | } // end llvm namespace |
| 184 | |
| 185 | namespace { |
| 186 | |
| 187 | // Test StringMapEntry::Create() method. |
| 188 | TEST_F(StringMapTest, StringMapEntryTest) { |
| 189 | StringMap<uint32_t>::value_type* entry = |
| 190 | StringMap<uint32_t>::value_type::Create( |
Daniel Dunbar | 6316fbc | 2009-07-23 18:17:34 +0000 | [diff] [blame] | 191 | testKeyFirst, testKeyFirst + testKeyLength, 1u); |
Bill Wendling | 6b223d7 | 2009-01-08 09:31:36 +0000 | [diff] [blame] | 192 | EXPECT_STREQ(testKey, entry->first()); |
| 193 | EXPECT_EQ(1u, entry->second); |
Jeffrey Yasskin | d873535 | 2010-02-11 07:16:13 +0000 | [diff] [blame] | 194 | free(entry); |
Misha Brukman | 8bb5e99 | 2009-01-08 04:48:20 +0000 | [diff] [blame] | 195 | } |
Bill Wendling | 6b223d7 | 2009-01-08 09:31:36 +0000 | [diff] [blame] | 196 | |
| 197 | // Test insert() method. |
| 198 | TEST_F(StringMapTest, InsertTest) { |
| 199 | SCOPED_TRACE("InsertTest"); |
| 200 | testMap.insert( |
| 201 | StringMap<uint32_t>::value_type::Create( |
Daniel Dunbar | 6316fbc | 2009-07-23 18:17:34 +0000 | [diff] [blame] | 202 | testKeyFirst, testKeyFirst + testKeyLength, |
| 203 | testMap.getAllocator(), 1u)); |
Bill Wendling | 6b223d7 | 2009-01-08 09:31:36 +0000 | [diff] [blame] | 204 | assertSingleItemMap(); |
| 205 | } |
| 206 | |
| 207 | } // end anonymous namespace |