Misha Brukman | 8fb520e | 2009-01-01 02:24:48 +0000 | [diff] [blame] | 1 | //===- llvm/unittest/ADT/DenseMapMap.cpp - DenseMap 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/DenseMap.h" |
Chandler Carruth | e5c7bc6 | 2012-06-16 01:31:33 +0000 | [diff] [blame] | 12 | #include <map> |
Misha Brukman | 8fb520e | 2009-01-01 02:24:48 +0000 | [diff] [blame] | 13 | |
| 14 | using namespace llvm; |
| 15 | |
| 16 | namespace { |
| 17 | |
Chandler Carruth | dd9d38d | 2012-06-17 09:05:09 +0000 | [diff] [blame^] | 18 | uint32_t getTestKey(int i, uint32_t *) { return i; } |
| 19 | uint32_t getTestValue(int i, uint32_t *) { return 42 + i; } |
Chandler Carruth | e5c7bc6 | 2012-06-16 01:31:33 +0000 | [diff] [blame] | 20 | |
Chandler Carruth | dd9d38d | 2012-06-17 09:05:09 +0000 | [diff] [blame^] | 21 | uint32_t *getTestKey(int i, uint32_t **) { |
| 22 | static uint32_t dummy_arr1[8192]; |
| 23 | assert(i < 8192 && "Only support 8192 dummy keys."); |
| 24 | return &dummy_arr1[i]; |
Misha Brukman | 8fb520e | 2009-01-01 02:24:48 +0000 | [diff] [blame] | 25 | } |
Chandler Carruth | dd9d38d | 2012-06-17 09:05:09 +0000 | [diff] [blame^] | 26 | uint32_t *getTestValue(int i, uint32_t **) { |
Chandler Carruth | e5c7bc6 | 2012-06-16 01:31:33 +0000 | [diff] [blame] | 27 | static uint32_t dummy_arr1[8192]; |
| 28 | assert(i < 8192 && "Only support 8192 dummy keys."); |
| 29 | return &dummy_arr1[i]; |
| 30 | } |
| 31 | |
Chandler Carruth | dd9d38d | 2012-06-17 09:05:09 +0000 | [diff] [blame^] | 32 | // Test fixture, with helper functions implemented by forwarding to global |
| 33 | // function overloads selected by component types of the type parameter. This |
| 34 | // allows all of the map implementations to be tested with shared |
| 35 | // implementations of helper routines. |
| 36 | template <typename T> |
| 37 | class DenseMapTest : public ::testing::Test { |
| 38 | protected: |
| 39 | T Map; |
| 40 | |
| 41 | static typename T::key_type *const dummy_key_ptr; |
| 42 | static typename T::mapped_type *const dummy_value_ptr; |
| 43 | |
| 44 | typename T::key_type getKey(int i = 0) { |
| 45 | return getTestKey(i, dummy_key_ptr); |
| 46 | } |
| 47 | typename T::mapped_type getValue(int i = 0) { |
| 48 | return getTestValue(i, dummy_value_ptr); |
| 49 | } |
| 50 | }; |
| 51 | |
| 52 | template <typename T> |
| 53 | typename T::key_type *const DenseMapTest<T>::dummy_key_ptr = 0; |
| 54 | template <typename T> |
| 55 | typename T::mapped_type *const DenseMapTest<T>::dummy_value_ptr = 0; |
Chandler Carruth | e5c7bc6 | 2012-06-16 01:31:33 +0000 | [diff] [blame] | 56 | |
| 57 | // Register these types for testing. |
| 58 | typedef ::testing::Types<DenseMap<uint32_t, uint32_t>, |
Chandler Carruth | dd9d38d | 2012-06-17 09:05:09 +0000 | [diff] [blame^] | 59 | DenseMap<uint32_t *, uint32_t *>, |
| 60 | SmallDenseMap<uint32_t, uint32_t>, |
| 61 | SmallDenseMap<uint32_t *, uint32_t *> |
| 62 | > DenseMapTestTypes; |
Chandler Carruth | e5c7bc6 | 2012-06-16 01:31:33 +0000 | [diff] [blame] | 63 | TYPED_TEST_CASE(DenseMapTest, DenseMapTestTypes); |
| 64 | |
| 65 | // Empty map tests |
| 66 | TYPED_TEST(DenseMapTest, EmptyIntMapTest) { |
Misha Brukman | 8fb520e | 2009-01-01 02:24:48 +0000 | [diff] [blame] | 67 | // Size tests |
Chandler Carruth | e5c7bc6 | 2012-06-16 01:31:33 +0000 | [diff] [blame] | 68 | EXPECT_EQ(0u, this->Map.size()); |
| 69 | EXPECT_TRUE(this->Map.empty()); |
Misha Brukman | 8fb520e | 2009-01-01 02:24:48 +0000 | [diff] [blame] | 70 | |
| 71 | // Iterator tests |
Chandler Carruth | e5c7bc6 | 2012-06-16 01:31:33 +0000 | [diff] [blame] | 72 | EXPECT_TRUE(this->Map.begin() == this->Map.end()); |
Misha Brukman | 8fb520e | 2009-01-01 02:24:48 +0000 | [diff] [blame] | 73 | |
| 74 | // Lookup tests |
Chandler Carruth | e5c7bc6 | 2012-06-16 01:31:33 +0000 | [diff] [blame] | 75 | EXPECT_FALSE(this->Map.count(this->getKey())); |
| 76 | EXPECT_TRUE(this->Map.find(this->getKey()) == this->Map.end()); |
Chandler Carruth | 7027ba9 | 2012-06-16 03:54:11 +0000 | [diff] [blame] | 77 | #ifndef _MSC_VER |
Chandler Carruth | e5c7bc6 | 2012-06-16 01:31:33 +0000 | [diff] [blame] | 78 | EXPECT_EQ(typename TypeParam::mapped_type(), |
| 79 | this->Map.lookup(this->getKey())); |
Chandler Carruth | 7027ba9 | 2012-06-16 03:54:11 +0000 | [diff] [blame] | 80 | #else |
| 81 | // MSVC, at least old versions, cannot parse the typename to disambiguate |
| 82 | // TypeParam::mapped_type as a type. However, because MSVC doesn't implement |
| 83 | // two-phase name lookup, it also doesn't require the typename. Deal with |
| 84 | // this mutual incompatibility through specialized code. |
| 85 | EXPECT_EQ(TypeParam::mapped_type(), |
| 86 | this->Map.lookup(this->getKey())); |
| 87 | #endif |
Misha Brukman | 8fb520e | 2009-01-01 02:24:48 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | // Constant map tests |
Chandler Carruth | e5c7bc6 | 2012-06-16 01:31:33 +0000 | [diff] [blame] | 91 | TYPED_TEST(DenseMapTest, ConstEmptyMapTest) { |
| 92 | const TypeParam &ConstMap = this->Map; |
| 93 | EXPECT_EQ(0u, ConstMap.size()); |
| 94 | EXPECT_TRUE(ConstMap.empty()); |
| 95 | EXPECT_TRUE(ConstMap.begin() == ConstMap.end()); |
Misha Brukman | 8fb520e | 2009-01-01 02:24:48 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | // A map with a single entry |
Chandler Carruth | e5c7bc6 | 2012-06-16 01:31:33 +0000 | [diff] [blame] | 99 | TYPED_TEST(DenseMapTest, SingleEntryMapTest) { |
| 100 | this->Map[this->getKey()] = this->getValue(); |
Misha Brukman | 8fb520e | 2009-01-01 02:24:48 +0000 | [diff] [blame] | 101 | |
| 102 | // Size tests |
Chandler Carruth | e5c7bc6 | 2012-06-16 01:31:33 +0000 | [diff] [blame] | 103 | EXPECT_EQ(1u, this->Map.size()); |
| 104 | EXPECT_FALSE(this->Map.begin() == this->Map.end()); |
| 105 | EXPECT_FALSE(this->Map.empty()); |
Misha Brukman | 8fb520e | 2009-01-01 02:24:48 +0000 | [diff] [blame] | 106 | |
| 107 | // Iterator tests |
Chandler Carruth | e5c7bc6 | 2012-06-16 01:31:33 +0000 | [diff] [blame] | 108 | typename TypeParam::iterator it = this->Map.begin(); |
| 109 | EXPECT_EQ(this->getKey(), it->first); |
| 110 | EXPECT_EQ(this->getValue(), it->second); |
Misha Brukman | 8fb520e | 2009-01-01 02:24:48 +0000 | [diff] [blame] | 111 | ++it; |
Chandler Carruth | e5c7bc6 | 2012-06-16 01:31:33 +0000 | [diff] [blame] | 112 | EXPECT_TRUE(it == this->Map.end()); |
Misha Brukman | 8fb520e | 2009-01-01 02:24:48 +0000 | [diff] [blame] | 113 | |
| 114 | // Lookup tests |
Chandler Carruth | e5c7bc6 | 2012-06-16 01:31:33 +0000 | [diff] [blame] | 115 | EXPECT_TRUE(this->Map.count(this->getKey())); |
| 116 | EXPECT_TRUE(this->Map.find(this->getKey()) == this->Map.begin()); |
| 117 | EXPECT_EQ(this->getValue(), this->Map.lookup(this->getKey())); |
| 118 | EXPECT_EQ(this->getValue(), this->Map[this->getKey()]); |
Misha Brukman | 8fb520e | 2009-01-01 02:24:48 +0000 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | // Test clear() method |
Chandler Carruth | e5c7bc6 | 2012-06-16 01:31:33 +0000 | [diff] [blame] | 122 | TYPED_TEST(DenseMapTest, ClearTest) { |
| 123 | this->Map[this->getKey()] = this->getValue(); |
| 124 | this->Map.clear(); |
Misha Brukman | 8fb520e | 2009-01-01 02:24:48 +0000 | [diff] [blame] | 125 | |
Chandler Carruth | e5c7bc6 | 2012-06-16 01:31:33 +0000 | [diff] [blame] | 126 | EXPECT_EQ(0u, this->Map.size()); |
| 127 | EXPECT_TRUE(this->Map.empty()); |
| 128 | EXPECT_TRUE(this->Map.begin() == this->Map.end()); |
Misha Brukman | 8fb520e | 2009-01-01 02:24:48 +0000 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | // Test erase(iterator) method |
Chandler Carruth | e5c7bc6 | 2012-06-16 01:31:33 +0000 | [diff] [blame] | 132 | TYPED_TEST(DenseMapTest, EraseTest) { |
| 133 | this->Map[this->getKey()] = this->getValue(); |
| 134 | this->Map.erase(this->Map.begin()); |
Misha Brukman | 8fb520e | 2009-01-01 02:24:48 +0000 | [diff] [blame] | 135 | |
Chandler Carruth | e5c7bc6 | 2012-06-16 01:31:33 +0000 | [diff] [blame] | 136 | EXPECT_EQ(0u, this->Map.size()); |
| 137 | EXPECT_TRUE(this->Map.empty()); |
| 138 | EXPECT_TRUE(this->Map.begin() == this->Map.end()); |
Misha Brukman | 8fb520e | 2009-01-01 02:24:48 +0000 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | // Test erase(value) method |
Chandler Carruth | e5c7bc6 | 2012-06-16 01:31:33 +0000 | [diff] [blame] | 142 | TYPED_TEST(DenseMapTest, EraseTest2) { |
| 143 | this->Map[this->getKey()] = this->getValue(); |
| 144 | this->Map.erase(this->getKey()); |
Misha Brukman | 8fb520e | 2009-01-01 02:24:48 +0000 | [diff] [blame] | 145 | |
Chandler Carruth | e5c7bc6 | 2012-06-16 01:31:33 +0000 | [diff] [blame] | 146 | EXPECT_EQ(0u, this->Map.size()); |
| 147 | EXPECT_TRUE(this->Map.empty()); |
| 148 | EXPECT_TRUE(this->Map.begin() == this->Map.end()); |
Misha Brukman | 8fb520e | 2009-01-01 02:24:48 +0000 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | // Test insert() method |
Chandler Carruth | e5c7bc6 | 2012-06-16 01:31:33 +0000 | [diff] [blame] | 152 | TYPED_TEST(DenseMapTest, InsertTest) { |
| 153 | this->Map.insert(std::make_pair(this->getKey(), this->getValue())); |
| 154 | EXPECT_EQ(1u, this->Map.size()); |
| 155 | EXPECT_EQ(this->getValue(), this->Map[this->getKey()]); |
Misha Brukman | 8fb520e | 2009-01-01 02:24:48 +0000 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | // Test copy constructor method |
Chandler Carruth | e5c7bc6 | 2012-06-16 01:31:33 +0000 | [diff] [blame] | 159 | TYPED_TEST(DenseMapTest, CopyConstructorTest) { |
| 160 | this->Map[this->getKey()] = this->getValue(); |
| 161 | TypeParam copyMap(this->Map); |
Misha Brukman | 8fb520e | 2009-01-01 02:24:48 +0000 | [diff] [blame] | 162 | |
| 163 | EXPECT_EQ(1u, copyMap.size()); |
Chandler Carruth | e5c7bc6 | 2012-06-16 01:31:33 +0000 | [diff] [blame] | 164 | EXPECT_EQ(this->getValue(), copyMap[this->getKey()]); |
Misha Brukman | 8fb520e | 2009-01-01 02:24:48 +0000 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | // Test assignment operator method |
Chandler Carruth | e5c7bc6 | 2012-06-16 01:31:33 +0000 | [diff] [blame] | 168 | TYPED_TEST(DenseMapTest, AssignmentTest) { |
| 169 | this->Map[this->getKey()] = this->getValue(); |
| 170 | TypeParam copyMap = this->Map; |
Misha Brukman | 8fb520e | 2009-01-01 02:24:48 +0000 | [diff] [blame] | 171 | |
| 172 | EXPECT_EQ(1u, copyMap.size()); |
Chandler Carruth | e5c7bc6 | 2012-06-16 01:31:33 +0000 | [diff] [blame] | 173 | EXPECT_EQ(this->getValue(), copyMap[this->getKey()]); |
Misha Brukman | 8fb520e | 2009-01-01 02:24:48 +0000 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | // A more complex iteration test |
Chandler Carruth | e5c7bc6 | 2012-06-16 01:31:33 +0000 | [diff] [blame] | 177 | TYPED_TEST(DenseMapTest, IterationTest) { |
Misha Brukman | 8fb520e | 2009-01-01 02:24:48 +0000 | [diff] [blame] | 178 | bool visited[100]; |
Chandler Carruth | e5c7bc6 | 2012-06-16 01:31:33 +0000 | [diff] [blame] | 179 | std::map<typename TypeParam::key_type, unsigned> visitedIndex; |
Misha Brukman | 8fb520e | 2009-01-01 02:24:48 +0000 | [diff] [blame] | 180 | |
| 181 | // Insert 100 numbers into the map |
| 182 | for (int i = 0; i < 100; ++i) { |
| 183 | visited[i] = false; |
Chandler Carruth | e5c7bc6 | 2012-06-16 01:31:33 +0000 | [diff] [blame] | 184 | visitedIndex[this->getKey(i)] = i; |
| 185 | |
| 186 | this->Map[this->getKey(i)] = this->getValue(i); |
Misha Brukman | 8fb520e | 2009-01-01 02:24:48 +0000 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | // Iterate over all numbers and mark each one found. |
Chandler Carruth | e5c7bc6 | 2012-06-16 01:31:33 +0000 | [diff] [blame] | 190 | for (typename TypeParam::iterator it = this->Map.begin(); |
| 191 | it != this->Map.end(); ++it) |
| 192 | visited[visitedIndex[it->first]] = true; |
Misha Brukman | 8fb520e | 2009-01-01 02:24:48 +0000 | [diff] [blame] | 193 | |
| 194 | // Ensure every number was visited. |
Chandler Carruth | e5c7bc6 | 2012-06-16 01:31:33 +0000 | [diff] [blame] | 195 | for (int i = 0; i < 100; ++i) |
Misha Brukman | c870bb5 | 2009-01-07 21:13:53 +0000 | [diff] [blame] | 196 | ASSERT_TRUE(visited[i]) << "Entry #" << i << " was never visited"; |
Misha Brukman | 8fb520e | 2009-01-01 02:24:48 +0000 | [diff] [blame] | 197 | } |
| 198 | |
Jeffrey Yasskin | 81cf432 | 2009-11-10 01:02:17 +0000 | [diff] [blame] | 199 | // const_iterator test |
Chandler Carruth | e5c7bc6 | 2012-06-16 01:31:33 +0000 | [diff] [blame] | 200 | TYPED_TEST(DenseMapTest, ConstIteratorTest) { |
Jeffrey Yasskin | 81cf432 | 2009-11-10 01:02:17 +0000 | [diff] [blame] | 201 | // Check conversion from iterator to const_iterator. |
Chandler Carruth | e5c7bc6 | 2012-06-16 01:31:33 +0000 | [diff] [blame] | 202 | typename TypeParam::iterator it = this->Map.begin(); |
| 203 | typename TypeParam::const_iterator cit(it); |
Jeffrey Yasskin | 81cf432 | 2009-11-10 01:02:17 +0000 | [diff] [blame] | 204 | EXPECT_TRUE(it == cit); |
| 205 | |
| 206 | // Check copying of const_iterators. |
Chandler Carruth | e5c7bc6 | 2012-06-16 01:31:33 +0000 | [diff] [blame] | 207 | typename TypeParam::const_iterator cit2(cit); |
Jeffrey Yasskin | 81cf432 | 2009-11-10 01:02:17 +0000 | [diff] [blame] | 208 | EXPECT_TRUE(cit == cit2); |
| 209 | } |
| 210 | |
Talin | babd598 | 2012-01-30 06:55:43 +0000 | [diff] [blame] | 211 | // Key traits that allows lookup with either an unsigned or char* key; |
| 212 | // In the latter case, "a" == 0, "b" == 1 and so on. |
| 213 | struct TestDenseMapInfo { |
| 214 | static inline unsigned getEmptyKey() { return ~0; } |
| 215 | static inline unsigned getTombstoneKey() { return ~0U - 1; } |
| 216 | static unsigned getHashValue(const unsigned& Val) { return Val * 37U; } |
| 217 | static unsigned getHashValue(const char* Val) { |
| 218 | return (unsigned)(Val[0] - 'a') * 37U; |
| 219 | } |
| 220 | static bool isEqual(const unsigned& LHS, const unsigned& RHS) { |
| 221 | return LHS == RHS; |
| 222 | } |
| 223 | static bool isEqual(const char* LHS, const unsigned& RHS) { |
| 224 | return (unsigned)(LHS[0] - 'a') == RHS; |
| 225 | } |
| 226 | }; |
| 227 | |
| 228 | // find_as() tests |
Chandler Carruth | e5c7bc6 | 2012-06-16 01:31:33 +0000 | [diff] [blame] | 229 | TEST(DenseMapCustomTest, FindAsTest) { |
Talin | babd598 | 2012-01-30 06:55:43 +0000 | [diff] [blame] | 230 | DenseMap<unsigned, unsigned, TestDenseMapInfo> map; |
| 231 | map[0] = 1; |
| 232 | map[1] = 2; |
| 233 | map[2] = 3; |
| 234 | |
| 235 | // Size tests |
| 236 | EXPECT_EQ(3u, map.size()); |
| 237 | |
| 238 | // Normal lookup tests |
| 239 | EXPECT_EQ(1, map.count(1)); |
| 240 | EXPECT_EQ(1u, map.find(0)->second); |
| 241 | EXPECT_EQ(2u, map.find(1)->second); |
| 242 | EXPECT_EQ(3u, map.find(2)->second); |
| 243 | EXPECT_TRUE(map.find(3) == map.end()); |
| 244 | |
| 245 | // find_as() tests |
| 246 | EXPECT_EQ(1u, map.find_as("a")->second); |
| 247 | EXPECT_EQ(2u, map.find_as("b")->second); |
| 248 | EXPECT_EQ(3u, map.find_as("c")->second); |
| 249 | EXPECT_TRUE(map.find_as("d") == map.end()); |
| 250 | } |
| 251 | |
Misha Brukman | 8fb520e | 2009-01-01 02:24:48 +0000 | [diff] [blame] | 252 | } |