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