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 | |
Misha Brukman | 8fb520e | 2009-01-01 02:24:48 +0000 | [diff] [blame] | 10 | #include "llvm/ADT/DenseMap.h" |
Chandler Carruth | 3c0d607 | 2017-06-06 11:06:56 +0000 | [diff] [blame] | 11 | #include "gtest/gtest.h" |
Chandler Carruth | e5c7bc6 | 2012-06-16 01:31:33 +0000 | [diff] [blame] | 12 | #include <map> |
Chandler Carruth | 6446d7e | 2012-06-17 10:33:51 +0000 | [diff] [blame] | 13 | #include <set> |
Misha Brukman | 8fb520e | 2009-01-01 02:24:48 +0000 | [diff] [blame] | 14 | |
| 15 | using namespace llvm; |
| 16 | |
| 17 | namespace { |
| 18 | |
Chandler Carruth | dd9d38d | 2012-06-17 09:05:09 +0000 | [diff] [blame] | 19 | uint32_t getTestKey(int i, uint32_t *) { return i; } |
| 20 | uint32_t getTestValue(int i, uint32_t *) { return 42 + i; } |
Chandler Carruth | e5c7bc6 | 2012-06-16 01:31:33 +0000 | [diff] [blame] | 21 | |
Chandler Carruth | dd9d38d | 2012-06-17 09:05:09 +0000 | [diff] [blame] | 22 | uint32_t *getTestKey(int i, uint32_t **) { |
| 23 | static uint32_t dummy_arr1[8192]; |
| 24 | assert(i < 8192 && "Only support 8192 dummy keys."); |
| 25 | return &dummy_arr1[i]; |
Misha Brukman | 8fb520e | 2009-01-01 02:24:48 +0000 | [diff] [blame] | 26 | } |
Chandler Carruth | dd9d38d | 2012-06-17 09:05:09 +0000 | [diff] [blame] | 27 | uint32_t *getTestValue(int i, uint32_t **) { |
Chandler Carruth | e5c7bc6 | 2012-06-16 01:31:33 +0000 | [diff] [blame] | 28 | static uint32_t dummy_arr1[8192]; |
| 29 | assert(i < 8192 && "Only support 8192 dummy keys."); |
| 30 | return &dummy_arr1[i]; |
| 31 | } |
| 32 | |
Adrian Prantl | 26b584c | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 33 | /// A test class that tries to check that construction and destruction |
Chandler Carruth | 6446d7e | 2012-06-17 10:33:51 +0000 | [diff] [blame] | 34 | /// occur correctly. |
| 35 | class CtorTester { |
| 36 | static std::set<CtorTester *> Constructed; |
| 37 | int Value; |
| 38 | |
| 39 | public: |
| 40 | explicit CtorTester(int Value = 0) : Value(Value) { |
| 41 | EXPECT_TRUE(Constructed.insert(this).second); |
| 42 | } |
| 43 | CtorTester(uint32_t Value) : Value(Value) { |
| 44 | EXPECT_TRUE(Constructed.insert(this).second); |
| 45 | } |
| 46 | CtorTester(const CtorTester &Arg) : Value(Arg.Value) { |
| 47 | EXPECT_TRUE(Constructed.insert(this).second); |
| 48 | } |
David Blaikie | 5edbe7e | 2015-03-04 07:57:45 +0000 | [diff] [blame] | 49 | CtorTester &operator=(const CtorTester &) = default; |
Chandler Carruth | 6446d7e | 2012-06-17 10:33:51 +0000 | [diff] [blame] | 50 | ~CtorTester() { |
| 51 | EXPECT_EQ(1u, Constructed.erase(this)); |
| 52 | } |
| 53 | operator uint32_t() const { return Value; } |
| 54 | |
| 55 | int getValue() const { return Value; } |
| 56 | bool operator==(const CtorTester &RHS) const { return Value == RHS.Value; } |
| 57 | }; |
| 58 | |
| 59 | std::set<CtorTester *> CtorTester::Constructed; |
| 60 | |
| 61 | struct CtorTesterMapInfo { |
| 62 | static inline CtorTester getEmptyKey() { return CtorTester(-1); } |
| 63 | static inline CtorTester getTombstoneKey() { return CtorTester(-2); } |
| 64 | static unsigned getHashValue(const CtorTester &Val) { |
| 65 | return Val.getValue() * 37u; |
| 66 | } |
| 67 | static bool isEqual(const CtorTester &LHS, const CtorTester &RHS) { |
| 68 | return LHS == RHS; |
| 69 | } |
| 70 | }; |
| 71 | |
| 72 | CtorTester getTestKey(int i, CtorTester *) { return CtorTester(i); } |
| 73 | CtorTester getTestValue(int i, CtorTester *) { return CtorTester(42 + i); } |
| 74 | |
Chandler Carruth | dd9d38d | 2012-06-17 09:05:09 +0000 | [diff] [blame] | 75 | // Test fixture, with helper functions implemented by forwarding to global |
| 76 | // function overloads selected by component types of the type parameter. This |
| 77 | // allows all of the map implementations to be tested with shared |
| 78 | // implementations of helper routines. |
| 79 | template <typename T> |
| 80 | class DenseMapTest : public ::testing::Test { |
| 81 | protected: |
| 82 | T Map; |
| 83 | |
| 84 | static typename T::key_type *const dummy_key_ptr; |
| 85 | static typename T::mapped_type *const dummy_value_ptr; |
| 86 | |
| 87 | typename T::key_type getKey(int i = 0) { |
| 88 | return getTestKey(i, dummy_key_ptr); |
| 89 | } |
| 90 | typename T::mapped_type getValue(int i = 0) { |
| 91 | return getTestValue(i, dummy_value_ptr); |
| 92 | } |
| 93 | }; |
| 94 | |
| 95 | template <typename T> |
Craig Topper | b177041 | 2014-06-08 22:29:17 +0000 | [diff] [blame] | 96 | typename T::key_type *const DenseMapTest<T>::dummy_key_ptr = nullptr; |
Chandler Carruth | dd9d38d | 2012-06-17 09:05:09 +0000 | [diff] [blame] | 97 | template <typename T> |
Craig Topper | b177041 | 2014-06-08 22:29:17 +0000 | [diff] [blame] | 98 | typename T::mapped_type *const DenseMapTest<T>::dummy_value_ptr = nullptr; |
Chandler Carruth | e5c7bc6 | 2012-06-16 01:31:33 +0000 | [diff] [blame] | 99 | |
| 100 | // Register these types for testing. |
| 101 | typedef ::testing::Types<DenseMap<uint32_t, uint32_t>, |
Chandler Carruth | dd9d38d | 2012-06-17 09:05:09 +0000 | [diff] [blame] | 102 | DenseMap<uint32_t *, uint32_t *>, |
Chandler Carruth | 6446d7e | 2012-06-17 10:33:51 +0000 | [diff] [blame] | 103 | DenseMap<CtorTester, CtorTester, CtorTesterMapInfo>, |
Chandler Carruth | dd9d38d | 2012-06-17 09:05:09 +0000 | [diff] [blame] | 104 | SmallDenseMap<uint32_t, uint32_t>, |
Chandler Carruth | 6446d7e | 2012-06-17 10:33:51 +0000 | [diff] [blame] | 105 | SmallDenseMap<uint32_t *, uint32_t *>, |
| 106 | SmallDenseMap<CtorTester, CtorTester, 4, |
| 107 | CtorTesterMapInfo> |
Chandler Carruth | dd9d38d | 2012-06-17 09:05:09 +0000 | [diff] [blame] | 108 | > DenseMapTestTypes; |
Chandler Carruth | e5c7bc6 | 2012-06-16 01:31:33 +0000 | [diff] [blame] | 109 | TYPED_TEST_CASE(DenseMapTest, DenseMapTestTypes); |
| 110 | |
| 111 | // Empty map tests |
| 112 | TYPED_TEST(DenseMapTest, EmptyIntMapTest) { |
Misha Brukman | 8fb520e | 2009-01-01 02:24:48 +0000 | [diff] [blame] | 113 | // Size tests |
Chandler Carruth | e5c7bc6 | 2012-06-16 01:31:33 +0000 | [diff] [blame] | 114 | EXPECT_EQ(0u, this->Map.size()); |
| 115 | EXPECT_TRUE(this->Map.empty()); |
Misha Brukman | 8fb520e | 2009-01-01 02:24:48 +0000 | [diff] [blame] | 116 | |
| 117 | // Iterator tests |
Chandler Carruth | e5c7bc6 | 2012-06-16 01:31:33 +0000 | [diff] [blame] | 118 | EXPECT_TRUE(this->Map.begin() == this->Map.end()); |
Misha Brukman | 8fb520e | 2009-01-01 02:24:48 +0000 | [diff] [blame] | 119 | |
| 120 | // Lookup tests |
Chandler Carruth | e5c7bc6 | 2012-06-16 01:31:33 +0000 | [diff] [blame] | 121 | EXPECT_FALSE(this->Map.count(this->getKey())); |
| 122 | EXPECT_TRUE(this->Map.find(this->getKey()) == this->Map.end()); |
Reid Kleckner | f2df02b | 2014-02-13 19:51:13 +0000 | [diff] [blame] | 123 | #if !defined(_MSC_VER) || defined(__clang__) |
Chandler Carruth | e5c7bc6 | 2012-06-16 01:31:33 +0000 | [diff] [blame] | 124 | EXPECT_EQ(typename TypeParam::mapped_type(), |
| 125 | this->Map.lookup(this->getKey())); |
Chandler Carruth | 7027ba9 | 2012-06-16 03:54:11 +0000 | [diff] [blame] | 126 | #else |
| 127 | // MSVC, at least old versions, cannot parse the typename to disambiguate |
| 128 | // TypeParam::mapped_type as a type. However, because MSVC doesn't implement |
| 129 | // two-phase name lookup, it also doesn't require the typename. Deal with |
| 130 | // this mutual incompatibility through specialized code. |
| 131 | EXPECT_EQ(TypeParam::mapped_type(), |
| 132 | this->Map.lookup(this->getKey())); |
| 133 | #endif |
Misha Brukman | 8fb520e | 2009-01-01 02:24:48 +0000 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | // Constant map tests |
Chandler Carruth | e5c7bc6 | 2012-06-16 01:31:33 +0000 | [diff] [blame] | 137 | TYPED_TEST(DenseMapTest, ConstEmptyMapTest) { |
| 138 | const TypeParam &ConstMap = this->Map; |
| 139 | EXPECT_EQ(0u, ConstMap.size()); |
| 140 | EXPECT_TRUE(ConstMap.empty()); |
| 141 | EXPECT_TRUE(ConstMap.begin() == ConstMap.end()); |
Misha Brukman | 8fb520e | 2009-01-01 02:24:48 +0000 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | // A map with a single entry |
Chandler Carruth | e5c7bc6 | 2012-06-16 01:31:33 +0000 | [diff] [blame] | 145 | TYPED_TEST(DenseMapTest, SingleEntryMapTest) { |
| 146 | this->Map[this->getKey()] = this->getValue(); |
Misha Brukman | 8fb520e | 2009-01-01 02:24:48 +0000 | [diff] [blame] | 147 | |
| 148 | // Size tests |
Chandler Carruth | e5c7bc6 | 2012-06-16 01:31:33 +0000 | [diff] [blame] | 149 | EXPECT_EQ(1u, this->Map.size()); |
| 150 | EXPECT_FALSE(this->Map.begin() == this->Map.end()); |
| 151 | EXPECT_FALSE(this->Map.empty()); |
Misha Brukman | 8fb520e | 2009-01-01 02:24:48 +0000 | [diff] [blame] | 152 | |
| 153 | // Iterator tests |
Chandler Carruth | e5c7bc6 | 2012-06-16 01:31:33 +0000 | [diff] [blame] | 154 | typename TypeParam::iterator it = this->Map.begin(); |
| 155 | EXPECT_EQ(this->getKey(), it->first); |
| 156 | EXPECT_EQ(this->getValue(), it->second); |
Misha Brukman | 8fb520e | 2009-01-01 02:24:48 +0000 | [diff] [blame] | 157 | ++it; |
Chandler Carruth | e5c7bc6 | 2012-06-16 01:31:33 +0000 | [diff] [blame] | 158 | EXPECT_TRUE(it == this->Map.end()); |
Misha Brukman | 8fb520e | 2009-01-01 02:24:48 +0000 | [diff] [blame] | 159 | |
| 160 | // Lookup tests |
Chandler Carruth | e5c7bc6 | 2012-06-16 01:31:33 +0000 | [diff] [blame] | 161 | EXPECT_TRUE(this->Map.count(this->getKey())); |
| 162 | EXPECT_TRUE(this->Map.find(this->getKey()) == this->Map.begin()); |
| 163 | EXPECT_EQ(this->getValue(), this->Map.lookup(this->getKey())); |
| 164 | EXPECT_EQ(this->getValue(), this->Map[this->getKey()]); |
Misha Brukman | 8fb520e | 2009-01-01 02:24:48 +0000 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | // Test clear() method |
Chandler Carruth | e5c7bc6 | 2012-06-16 01:31:33 +0000 | [diff] [blame] | 168 | TYPED_TEST(DenseMapTest, ClearTest) { |
| 169 | this->Map[this->getKey()] = this->getValue(); |
| 170 | this->Map.clear(); |
Misha Brukman | 8fb520e | 2009-01-01 02:24:48 +0000 | [diff] [blame] | 171 | |
Chandler Carruth | e5c7bc6 | 2012-06-16 01:31:33 +0000 | [diff] [blame] | 172 | EXPECT_EQ(0u, this->Map.size()); |
| 173 | EXPECT_TRUE(this->Map.empty()); |
| 174 | EXPECT_TRUE(this->Map.begin() == this->Map.end()); |
Misha Brukman | 8fb520e | 2009-01-01 02:24:48 +0000 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | // Test erase(iterator) method |
Chandler Carruth | e5c7bc6 | 2012-06-16 01:31:33 +0000 | [diff] [blame] | 178 | TYPED_TEST(DenseMapTest, EraseTest) { |
| 179 | this->Map[this->getKey()] = this->getValue(); |
| 180 | this->Map.erase(this->Map.begin()); |
Misha Brukman | 8fb520e | 2009-01-01 02:24:48 +0000 | [diff] [blame] | 181 | |
Chandler Carruth | e5c7bc6 | 2012-06-16 01:31:33 +0000 | [diff] [blame] | 182 | EXPECT_EQ(0u, this->Map.size()); |
| 183 | EXPECT_TRUE(this->Map.empty()); |
| 184 | EXPECT_TRUE(this->Map.begin() == this->Map.end()); |
Misha Brukman | 8fb520e | 2009-01-01 02:24:48 +0000 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | // Test erase(value) method |
Chandler Carruth | e5c7bc6 | 2012-06-16 01:31:33 +0000 | [diff] [blame] | 188 | TYPED_TEST(DenseMapTest, EraseTest2) { |
| 189 | this->Map[this->getKey()] = this->getValue(); |
| 190 | this->Map.erase(this->getKey()); |
Misha Brukman | 8fb520e | 2009-01-01 02:24:48 +0000 | [diff] [blame] | 191 | |
Chandler Carruth | e5c7bc6 | 2012-06-16 01:31:33 +0000 | [diff] [blame] | 192 | EXPECT_EQ(0u, this->Map.size()); |
| 193 | EXPECT_TRUE(this->Map.empty()); |
| 194 | EXPECT_TRUE(this->Map.begin() == this->Map.end()); |
Misha Brukman | 8fb520e | 2009-01-01 02:24:48 +0000 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | // Test insert() method |
Chandler Carruth | e5c7bc6 | 2012-06-16 01:31:33 +0000 | [diff] [blame] | 198 | TYPED_TEST(DenseMapTest, InsertTest) { |
| 199 | this->Map.insert(std::make_pair(this->getKey(), this->getValue())); |
| 200 | EXPECT_EQ(1u, this->Map.size()); |
| 201 | EXPECT_EQ(this->getValue(), this->Map[this->getKey()]); |
Misha Brukman | 8fb520e | 2009-01-01 02:24:48 +0000 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | // Test copy constructor method |
Chandler Carruth | e5c7bc6 | 2012-06-16 01:31:33 +0000 | [diff] [blame] | 205 | TYPED_TEST(DenseMapTest, CopyConstructorTest) { |
| 206 | this->Map[this->getKey()] = this->getValue(); |
| 207 | TypeParam copyMap(this->Map); |
Misha Brukman | 8fb520e | 2009-01-01 02:24:48 +0000 | [diff] [blame] | 208 | |
| 209 | EXPECT_EQ(1u, copyMap.size()); |
Chandler Carruth | e5c7bc6 | 2012-06-16 01:31:33 +0000 | [diff] [blame] | 210 | EXPECT_EQ(this->getValue(), copyMap[this->getKey()]); |
Misha Brukman | 8fb520e | 2009-01-01 02:24:48 +0000 | [diff] [blame] | 211 | } |
| 212 | |
Duncan P. N. Exon Smith | e8aecce | 2014-02-25 23:35:13 +0000 | [diff] [blame] | 213 | // Test copy constructor method where SmallDenseMap isn't small. |
| 214 | TYPED_TEST(DenseMapTest, CopyConstructorNotSmallTest) { |
| 215 | for (int Key = 0; Key < 5; ++Key) |
| 216 | this->Map[this->getKey(Key)] = this->getValue(Key); |
| 217 | TypeParam copyMap(this->Map); |
| 218 | |
| 219 | EXPECT_EQ(5u, copyMap.size()); |
| 220 | for (int Key = 0; Key < 5; ++Key) |
| 221 | EXPECT_EQ(this->getValue(Key), copyMap[this->getKey(Key)]); |
| 222 | } |
| 223 | |
| 224 | // Test copying from a default-constructed map. |
| 225 | TYPED_TEST(DenseMapTest, CopyConstructorFromDefaultTest) { |
| 226 | TypeParam copyMap(this->Map); |
| 227 | |
| 228 | EXPECT_TRUE(copyMap.empty()); |
| 229 | } |
| 230 | |
| 231 | // Test copying from an empty map where SmallDenseMap isn't small. |
| 232 | TYPED_TEST(DenseMapTest, CopyConstructorFromEmptyTest) { |
| 233 | for (int Key = 0; Key < 5; ++Key) |
| 234 | this->Map[this->getKey(Key)] = this->getValue(Key); |
| 235 | this->Map.clear(); |
| 236 | TypeParam copyMap(this->Map); |
| 237 | |
| 238 | EXPECT_TRUE(copyMap.empty()); |
| 239 | } |
| 240 | |
Misha Brukman | 8fb520e | 2009-01-01 02:24:48 +0000 | [diff] [blame] | 241 | // Test assignment operator method |
Chandler Carruth | e5c7bc6 | 2012-06-16 01:31:33 +0000 | [diff] [blame] | 242 | TYPED_TEST(DenseMapTest, AssignmentTest) { |
| 243 | this->Map[this->getKey()] = this->getValue(); |
| 244 | TypeParam copyMap = this->Map; |
Misha Brukman | 8fb520e | 2009-01-01 02:24:48 +0000 | [diff] [blame] | 245 | |
| 246 | EXPECT_EQ(1u, copyMap.size()); |
Chandler Carruth | e5c7bc6 | 2012-06-16 01:31:33 +0000 | [diff] [blame] | 247 | EXPECT_EQ(this->getValue(), copyMap[this->getKey()]); |
Andrew Trick | e85047e | 2014-08-04 22:18:25 +0000 | [diff] [blame] | 248 | |
| 249 | // test self-assignment. |
Roman Lebedev | 6bc5d61 | 2018-04-07 10:37:18 +0000 | [diff] [blame] | 250 | copyMap = static_cast<TypeParam &>(copyMap); |
Andrew Trick | e85047e | 2014-08-04 22:18:25 +0000 | [diff] [blame] | 251 | EXPECT_EQ(1u, copyMap.size()); |
| 252 | EXPECT_EQ(this->getValue(), copyMap[this->getKey()]); |
Misha Brukman | 8fb520e | 2009-01-01 02:24:48 +0000 | [diff] [blame] | 253 | } |
| 254 | |
Michael Gottesman | 23a49a6 | 2015-10-31 05:23:53 +0000 | [diff] [blame] | 255 | TYPED_TEST(DenseMapTest, AssignmentTestNotSmall) { |
| 256 | for (int Key = 0; Key < 5; ++Key) |
| 257 | this->Map[this->getKey(Key)] = this->getValue(Key); |
| 258 | TypeParam copyMap = this->Map; |
| 259 | |
| 260 | EXPECT_EQ(5u, copyMap.size()); |
| 261 | for (int Key = 0; Key < 5; ++Key) |
| 262 | EXPECT_EQ(this->getValue(Key), copyMap[this->getKey(Key)]); |
| 263 | |
| 264 | // test self-assignment. |
Roman Lebedev | 6bc5d61 | 2018-04-07 10:37:18 +0000 | [diff] [blame] | 265 | copyMap = static_cast<TypeParam &>(copyMap); |
Michael Gottesman | 23a49a6 | 2015-10-31 05:23:53 +0000 | [diff] [blame] | 266 | EXPECT_EQ(5u, copyMap.size()); |
| 267 | for (int Key = 0; Key < 5; ++Key) |
| 268 | EXPECT_EQ(this->getValue(Key), copyMap[this->getKey(Key)]); |
| 269 | } |
| 270 | |
Chandler Carruth | 8dffa4a | 2012-06-17 11:28:13 +0000 | [diff] [blame] | 271 | // Test swap method |
| 272 | TYPED_TEST(DenseMapTest, SwapTest) { |
| 273 | this->Map[this->getKey()] = this->getValue(); |
| 274 | TypeParam otherMap; |
| 275 | |
| 276 | this->Map.swap(otherMap); |
| 277 | EXPECT_EQ(0u, this->Map.size()); |
| 278 | EXPECT_TRUE(this->Map.empty()); |
| 279 | EXPECT_EQ(1u, otherMap.size()); |
| 280 | EXPECT_EQ(this->getValue(), otherMap[this->getKey()]); |
| 281 | |
| 282 | this->Map.swap(otherMap); |
| 283 | EXPECT_EQ(0u, otherMap.size()); |
| 284 | EXPECT_TRUE(otherMap.empty()); |
| 285 | EXPECT_EQ(1u, this->Map.size()); |
| 286 | EXPECT_EQ(this->getValue(), this->Map[this->getKey()]); |
| 287 | |
| 288 | // Make this more interesting by inserting 100 numbers into the map. |
| 289 | for (int i = 0; i < 100; ++i) |
| 290 | this->Map[this->getKey(i)] = this->getValue(i); |
| 291 | |
| 292 | this->Map.swap(otherMap); |
| 293 | EXPECT_EQ(0u, this->Map.size()); |
| 294 | EXPECT_TRUE(this->Map.empty()); |
| 295 | EXPECT_EQ(100u, otherMap.size()); |
| 296 | for (int i = 0; i < 100; ++i) |
| 297 | EXPECT_EQ(this->getValue(i), otherMap[this->getKey(i)]); |
| 298 | |
| 299 | this->Map.swap(otherMap); |
| 300 | EXPECT_EQ(0u, otherMap.size()); |
| 301 | EXPECT_TRUE(otherMap.empty()); |
| 302 | EXPECT_EQ(100u, this->Map.size()); |
| 303 | for (int i = 0; i < 100; ++i) |
| 304 | EXPECT_EQ(this->getValue(i), this->Map[this->getKey(i)]); |
| 305 | } |
| 306 | |
Misha Brukman | 8fb520e | 2009-01-01 02:24:48 +0000 | [diff] [blame] | 307 | // A more complex iteration test |
Chandler Carruth | e5c7bc6 | 2012-06-16 01:31:33 +0000 | [diff] [blame] | 308 | TYPED_TEST(DenseMapTest, IterationTest) { |
Misha Brukman | 8fb520e | 2009-01-01 02:24:48 +0000 | [diff] [blame] | 309 | bool visited[100]; |
Chandler Carruth | e5c7bc6 | 2012-06-16 01:31:33 +0000 | [diff] [blame] | 310 | std::map<typename TypeParam::key_type, unsigned> visitedIndex; |
Misha Brukman | 8fb520e | 2009-01-01 02:24:48 +0000 | [diff] [blame] | 311 | |
| 312 | // Insert 100 numbers into the map |
| 313 | for (int i = 0; i < 100; ++i) { |
| 314 | visited[i] = false; |
Chandler Carruth | e5c7bc6 | 2012-06-16 01:31:33 +0000 | [diff] [blame] | 315 | visitedIndex[this->getKey(i)] = i; |
| 316 | |
| 317 | this->Map[this->getKey(i)] = this->getValue(i); |
Misha Brukman | 8fb520e | 2009-01-01 02:24:48 +0000 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | // Iterate over all numbers and mark each one found. |
Chandler Carruth | e5c7bc6 | 2012-06-16 01:31:33 +0000 | [diff] [blame] | 321 | for (typename TypeParam::iterator it = this->Map.begin(); |
| 322 | it != this->Map.end(); ++it) |
| 323 | visited[visitedIndex[it->first]] = true; |
Misha Brukman | 8fb520e | 2009-01-01 02:24:48 +0000 | [diff] [blame] | 324 | |
| 325 | // Ensure every number was visited. |
Chandler Carruth | e5c7bc6 | 2012-06-16 01:31:33 +0000 | [diff] [blame] | 326 | for (int i = 0; i < 100; ++i) |
Misha Brukman | c870bb5 | 2009-01-07 21:13:53 +0000 | [diff] [blame] | 327 | ASSERT_TRUE(visited[i]) << "Entry #" << i << " was never visited"; |
Misha Brukman | 8fb520e | 2009-01-01 02:24:48 +0000 | [diff] [blame] | 328 | } |
| 329 | |
Jeffrey Yasskin | 81cf432 | 2009-11-10 01:02:17 +0000 | [diff] [blame] | 330 | // const_iterator test |
Chandler Carruth | e5c7bc6 | 2012-06-16 01:31:33 +0000 | [diff] [blame] | 331 | TYPED_TEST(DenseMapTest, ConstIteratorTest) { |
Jeffrey Yasskin | 81cf432 | 2009-11-10 01:02:17 +0000 | [diff] [blame] | 332 | // Check conversion from iterator to const_iterator. |
Chandler Carruth | e5c7bc6 | 2012-06-16 01:31:33 +0000 | [diff] [blame] | 333 | typename TypeParam::iterator it = this->Map.begin(); |
| 334 | typename TypeParam::const_iterator cit(it); |
Jeffrey Yasskin | 81cf432 | 2009-11-10 01:02:17 +0000 | [diff] [blame] | 335 | EXPECT_TRUE(it == cit); |
| 336 | |
| 337 | // Check copying of const_iterators. |
Chandler Carruth | e5c7bc6 | 2012-06-16 01:31:33 +0000 | [diff] [blame] | 338 | typename TypeParam::const_iterator cit2(cit); |
Jeffrey Yasskin | 81cf432 | 2009-11-10 01:02:17 +0000 | [diff] [blame] | 339 | EXPECT_TRUE(cit == cit2); |
| 340 | } |
| 341 | |
Mehdi Amini | 9ed5a82 | 2016-03-25 05:57:52 +0000 | [diff] [blame] | 342 | namespace { |
| 343 | // Simple class that counts how many moves and copy happens when growing a map |
| 344 | struct CountCopyAndMove { |
| 345 | static int Move; |
| 346 | static int Copy; |
| 347 | CountCopyAndMove() {} |
| 348 | |
| 349 | CountCopyAndMove(const CountCopyAndMove &) { Copy++; } |
| 350 | CountCopyAndMove &operator=(const CountCopyAndMove &) { |
| 351 | Copy++; |
| 352 | return *this; |
| 353 | } |
| 354 | CountCopyAndMove(CountCopyAndMove &&) { Move++; } |
| 355 | CountCopyAndMove &operator=(const CountCopyAndMove &&) { |
| 356 | Move++; |
| 357 | return *this; |
| 358 | } |
| 359 | }; |
| 360 | int CountCopyAndMove::Copy = 0; |
| 361 | int CountCopyAndMove::Move = 0; |
| 362 | |
| 363 | } // anonymous namespace |
| 364 | |
Lang Hames | cb8b3a2 | 2018-10-15 15:26:47 +0000 | [diff] [blame] | 365 | // Test initializer list construction. |
| 366 | TEST(DenseMapCustomTest, InitializerList) { |
| 367 | DenseMap<int, int> M({{0, 0}, {0, 1}, {1, 2}}); |
| 368 | EXPECT_EQ(2u, M.size()); |
| 369 | EXPECT_EQ(1u, M.count(0)); |
| 370 | EXPECT_EQ(0, M[0]); |
| 371 | EXPECT_EQ(1u, M.count(1)); |
| 372 | EXPECT_EQ(2, M[1]); |
| 373 | } |
| 374 | |
| 375 | // Test initializer list construction. |
| 376 | TEST(DenseMapCustomTest, EqualityComparison) { |
| 377 | DenseMap<int, int> M1({{0, 0}, {1, 2}}); |
| 378 | DenseMap<int, int> M2({{0, 0}, {1, 2}}); |
| 379 | DenseMap<int, int> M3({{0, 0}, {1, 3}}); |
| 380 | |
| 381 | EXPECT_EQ(M1, M2); |
| 382 | EXPECT_NE(M1, M3); |
| 383 | } |
| 384 | |
Mehdi Amini | 9ed5a82 | 2016-03-25 05:57:52 +0000 | [diff] [blame] | 385 | // Test for the default minimum size of a DenseMap |
| 386 | TEST(DenseMapCustomTest, DefaultMinReservedSizeTest) { |
| 387 | // IF THIS VALUE CHANGE, please update InitialSizeTest, InitFromIterator, and |
| 388 | // ReserveTest as well! |
| 389 | const int ExpectedInitialBucketCount = 64; |
| 390 | // Formula from DenseMap::getMinBucketToReserveForEntries() |
| 391 | const int ExpectedMaxInitialEntries = ExpectedInitialBucketCount * 3 / 4 - 1; |
| 392 | |
| 393 | DenseMap<int, CountCopyAndMove> Map; |
| 394 | // Will allocate 64 buckets |
| 395 | Map.reserve(1); |
| 396 | unsigned MemorySize = Map.getMemorySize(); |
| 397 | CountCopyAndMove::Copy = 0; |
| 398 | CountCopyAndMove::Move = 0; |
| 399 | for (int i = 0; i < ExpectedMaxInitialEntries; ++i) |
Mehdi Amini | 64f9238 | 2016-03-25 23:25:06 +0000 | [diff] [blame] | 400 | Map.insert(std::pair<int, CountCopyAndMove>(std::piecewise_construct, |
| 401 | std::forward_as_tuple(i), |
| 402 | std::forward_as_tuple())); |
Mehdi Amini | 9ed5a82 | 2016-03-25 05:57:52 +0000 | [diff] [blame] | 403 | // Check that we didn't grow |
| 404 | EXPECT_EQ(MemorySize, Map.getMemorySize()); |
| 405 | // Check that move was called the expected number of times |
Mehdi Amini | 64f9238 | 2016-03-25 23:25:06 +0000 | [diff] [blame] | 406 | EXPECT_EQ(ExpectedMaxInitialEntries, CountCopyAndMove::Move); |
Malcolm Parsons | 5fc9628 | 2018-01-24 10:33:39 +0000 | [diff] [blame] | 407 | // Check that no copy occurred |
Mehdi Amini | 9ed5a82 | 2016-03-25 05:57:52 +0000 | [diff] [blame] | 408 | EXPECT_EQ(0, CountCopyAndMove::Copy); |
| 409 | |
| 410 | // Adding one extra element should grow the map |
Mehdi Amini | 64f9238 | 2016-03-25 23:25:06 +0000 | [diff] [blame] | 411 | Map.insert(std::pair<int, CountCopyAndMove>( |
| 412 | std::piecewise_construct, |
| 413 | std::forward_as_tuple(ExpectedMaxInitialEntries), |
| 414 | std::forward_as_tuple())); |
Mehdi Amini | 9ed5a82 | 2016-03-25 05:57:52 +0000 | [diff] [blame] | 415 | // Check that we grew |
| 416 | EXPECT_NE(MemorySize, Map.getMemorySize()); |
| 417 | // Check that move was called the expected number of times |
Mehdi Amini | 4f6a396 | 2016-03-25 15:46:14 +0000 | [diff] [blame] | 418 | // This relies on move-construction elision, and cannot be reliably tested. |
| 419 | // EXPECT_EQ(ExpectedMaxInitialEntries + 2, CountCopyAndMove::Move); |
Malcolm Parsons | 5fc9628 | 2018-01-24 10:33:39 +0000 | [diff] [blame] | 420 | // Check that no copy occurred |
Mehdi Amini | 9ed5a82 | 2016-03-25 05:57:52 +0000 | [diff] [blame] | 421 | EXPECT_EQ(0, CountCopyAndMove::Copy); |
| 422 | } |
| 423 | |
| 424 | // Make sure creating the map with an initial size of N actually gives us enough |
| 425 | // buckets to insert N items without increasing allocation size. |
| 426 | TEST(DenseMapCustomTest, InitialSizeTest) { |
| 427 | // Test a few different sizes, 48 is *not* a random choice: we need a value |
| 428 | // that is 2/3 of a power of two to stress the grow() condition, and the power |
| 429 | // of two has to be at least 64 because of minimum size allocation in the |
| 430 | // DenseMap (see DefaultMinReservedSizeTest). 66 is a value just above the |
| 431 | // 64 default init. |
| 432 | for (auto Size : {1, 2, 48, 66}) { |
| 433 | DenseMap<int, CountCopyAndMove> Map(Size); |
| 434 | unsigned MemorySize = Map.getMemorySize(); |
| 435 | CountCopyAndMove::Copy = 0; |
| 436 | CountCopyAndMove::Move = 0; |
| 437 | for (int i = 0; i < Size; ++i) |
Mehdi Amini | 64f9238 | 2016-03-25 23:25:06 +0000 | [diff] [blame] | 438 | Map.insert(std::pair<int, CountCopyAndMove>(std::piecewise_construct, |
| 439 | std::forward_as_tuple(i), |
| 440 | std::forward_as_tuple())); |
Mehdi Amini | 9ed5a82 | 2016-03-25 05:57:52 +0000 | [diff] [blame] | 441 | // Check that we didn't grow |
| 442 | EXPECT_EQ(MemorySize, Map.getMemorySize()); |
| 443 | // Check that move was called the expected number of times |
Mehdi Amini | 64f9238 | 2016-03-25 23:25:06 +0000 | [diff] [blame] | 444 | EXPECT_EQ(Size, CountCopyAndMove::Move); |
Malcolm Parsons | 5fc9628 | 2018-01-24 10:33:39 +0000 | [diff] [blame] | 445 | // Check that no copy occurred |
Mehdi Amini | 9ed5a82 | 2016-03-25 05:57:52 +0000 | [diff] [blame] | 446 | EXPECT_EQ(0, CountCopyAndMove::Copy); |
| 447 | } |
| 448 | } |
| 449 | |
| 450 | // Make sure creating the map with a iterator range does not trigger grow() |
| 451 | TEST(DenseMapCustomTest, InitFromIterator) { |
| 452 | std::vector<std::pair<int, CountCopyAndMove>> Values; |
| 453 | // The size is a random value greater than 64 (hardcoded DenseMap min init) |
| 454 | const int Count = 65; |
| 455 | for (int i = 0; i < Count; i++) |
| 456 | Values.emplace_back(i, CountCopyAndMove()); |
| 457 | |
| 458 | CountCopyAndMove::Move = 0; |
| 459 | CountCopyAndMove::Copy = 0; |
| 460 | DenseMap<int, CountCopyAndMove> Map(Values.begin(), Values.end()); |
Malcolm Parsons | 5fc9628 | 2018-01-24 10:33:39 +0000 | [diff] [blame] | 461 | // Check that no move occurred |
Mehdi Amini | 9ed5a82 | 2016-03-25 05:57:52 +0000 | [diff] [blame] | 462 | EXPECT_EQ(0, CountCopyAndMove::Move); |
| 463 | // Check that copy was called the expected number of times |
| 464 | EXPECT_EQ(Count, CountCopyAndMove::Copy); |
| 465 | } |
| 466 | |
| 467 | // Make sure reserve actually gives us enough buckets to insert N items |
Fiona Glaser | 33ae313 | 2016-03-15 01:50:46 +0000 | [diff] [blame] | 468 | // without increasing allocation size. |
Mehdi Amini | 9ed5a82 | 2016-03-25 05:57:52 +0000 | [diff] [blame] | 469 | TEST(DenseMapCustomTest, ReserveTest) { |
| 470 | // Test a few different size, 48 is *not* a random choice: we need a value |
| 471 | // that is 2/3 of a power of two to stress the grow() condition, and the power |
| 472 | // of two has to be at least 64 because of minimum size allocation in the |
| 473 | // DenseMap (see DefaultMinReservedSizeTest). 66 is a value just above the |
| 474 | // 64 default init. |
| 475 | for (auto Size : {1, 2, 48, 66}) { |
| 476 | DenseMap<int, CountCopyAndMove> Map; |
Mehdi Amini | d4501ff | 2016-03-22 07:35:51 +0000 | [diff] [blame] | 477 | Map.reserve(Size); |
Fiona Glaser | 33ae313 | 2016-03-15 01:50:46 +0000 | [diff] [blame] | 478 | unsigned MemorySize = Map.getMemorySize(); |
Mehdi Amini | 9ed5a82 | 2016-03-25 05:57:52 +0000 | [diff] [blame] | 479 | CountCopyAndMove::Copy = 0; |
| 480 | CountCopyAndMove::Move = 0; |
| 481 | for (int i = 0; i < Size; ++i) |
Mehdi Amini | 64f9238 | 2016-03-25 23:25:06 +0000 | [diff] [blame] | 482 | Map.insert(std::pair<int, CountCopyAndMove>(std::piecewise_construct, |
| 483 | std::forward_as_tuple(i), |
| 484 | std::forward_as_tuple())); |
Mehdi Amini | 9ed5a82 | 2016-03-25 05:57:52 +0000 | [diff] [blame] | 485 | // Check that we didn't grow |
| 486 | EXPECT_EQ(MemorySize, Map.getMemorySize()); |
| 487 | // Check that move was called the expected number of times |
Mehdi Amini | 64f9238 | 2016-03-25 23:25:06 +0000 | [diff] [blame] | 488 | EXPECT_EQ(Size, CountCopyAndMove::Move); |
Malcolm Parsons | 5fc9628 | 2018-01-24 10:33:39 +0000 | [diff] [blame] | 489 | // Check that no copy occurred |
Mehdi Amini | 9ed5a82 | 2016-03-25 05:57:52 +0000 | [diff] [blame] | 490 | EXPECT_EQ(0, CountCopyAndMove::Copy); |
Fiona Glaser | 33ae313 | 2016-03-15 01:50:46 +0000 | [diff] [blame] | 491 | } |
| 492 | } |
| 493 | |
Chandler Carruth | 9ece8eb | 2015-06-24 10:06:29 +0000 | [diff] [blame] | 494 | // Make sure DenseMap works with StringRef keys. |
| 495 | TEST(DenseMapCustomTest, StringRefTest) { |
| 496 | DenseMap<StringRef, int> M; |
| 497 | |
| 498 | M["a"] = 1; |
| 499 | M["b"] = 2; |
| 500 | M["c"] = 3; |
| 501 | |
| 502 | EXPECT_EQ(3u, M.size()); |
| 503 | EXPECT_EQ(1, M.lookup("a")); |
| 504 | EXPECT_EQ(2, M.lookup("b")); |
| 505 | EXPECT_EQ(3, M.lookup("c")); |
| 506 | |
| 507 | EXPECT_EQ(0, M.lookup("q")); |
| 508 | |
| 509 | // Test the empty string, spelled various ways. |
| 510 | EXPECT_EQ(0, M.lookup("")); |
| 511 | EXPECT_EQ(0, M.lookup(StringRef())); |
| 512 | EXPECT_EQ(0, M.lookup(StringRef("a", 0))); |
| 513 | M[""] = 42; |
| 514 | EXPECT_EQ(42, M.lookup("")); |
| 515 | EXPECT_EQ(42, M.lookup(StringRef())); |
| 516 | EXPECT_EQ(42, M.lookup(StringRef("a", 0))); |
| 517 | } |
| 518 | |
Talin | babd598 | 2012-01-30 06:55:43 +0000 | [diff] [blame] | 519 | // Key traits that allows lookup with either an unsigned or char* key; |
| 520 | // In the latter case, "a" == 0, "b" == 1 and so on. |
| 521 | struct TestDenseMapInfo { |
| 522 | static inline unsigned getEmptyKey() { return ~0; } |
| 523 | static inline unsigned getTombstoneKey() { return ~0U - 1; } |
| 524 | static unsigned getHashValue(const unsigned& Val) { return Val * 37U; } |
| 525 | static unsigned getHashValue(const char* Val) { |
| 526 | return (unsigned)(Val[0] - 'a') * 37U; |
| 527 | } |
| 528 | static bool isEqual(const unsigned& LHS, const unsigned& RHS) { |
| 529 | return LHS == RHS; |
| 530 | } |
| 531 | static bool isEqual(const char* LHS, const unsigned& RHS) { |
| 532 | return (unsigned)(LHS[0] - 'a') == RHS; |
| 533 | } |
| 534 | }; |
| 535 | |
| 536 | // find_as() tests |
Chandler Carruth | e5c7bc6 | 2012-06-16 01:31:33 +0000 | [diff] [blame] | 537 | TEST(DenseMapCustomTest, FindAsTest) { |
Talin | babd598 | 2012-01-30 06:55:43 +0000 | [diff] [blame] | 538 | DenseMap<unsigned, unsigned, TestDenseMapInfo> map; |
| 539 | map[0] = 1; |
| 540 | map[1] = 2; |
| 541 | map[2] = 3; |
| 542 | |
| 543 | // Size tests |
| 544 | EXPECT_EQ(3u, map.size()); |
| 545 | |
| 546 | // Normal lookup tests |
David Blaikie | 20dead8 | 2014-06-20 19:54:13 +0000 | [diff] [blame] | 547 | EXPECT_EQ(1u, map.count(1)); |
Talin | babd598 | 2012-01-30 06:55:43 +0000 | [diff] [blame] | 548 | EXPECT_EQ(1u, map.find(0)->second); |
| 549 | EXPECT_EQ(2u, map.find(1)->second); |
| 550 | EXPECT_EQ(3u, map.find(2)->second); |
| 551 | EXPECT_TRUE(map.find(3) == map.end()); |
| 552 | |
| 553 | // find_as() tests |
| 554 | EXPECT_EQ(1u, map.find_as("a")->second); |
| 555 | EXPECT_EQ(2u, map.find_as("b")->second); |
| 556 | EXPECT_EQ(3u, map.find_as("c")->second); |
| 557 | EXPECT_TRUE(map.find_as("d") == map.end()); |
| 558 | } |
| 559 | |
Pete Cooper | fbaf206 | 2012-10-23 18:47:35 +0000 | [diff] [blame] | 560 | struct ContiguousDenseMapInfo { |
| 561 | static inline unsigned getEmptyKey() { return ~0; } |
| 562 | static inline unsigned getTombstoneKey() { return ~0U - 1; } |
| 563 | static unsigned getHashValue(const unsigned& Val) { return Val; } |
| 564 | static bool isEqual(const unsigned& LHS, const unsigned& RHS) { |
| 565 | return LHS == RHS; |
| 566 | } |
| 567 | }; |
| 568 | |
| 569 | // Test that filling a small dense map with exactly the number of elements in |
| 570 | // the map grows to have enough space for an empty bucket. |
| 571 | TEST(DenseMapCustomTest, SmallDenseMapGrowTest) { |
| 572 | SmallDenseMap<unsigned, unsigned, 32, ContiguousDenseMapInfo> map; |
| 573 | // Add some number of elements, then delete a few to leave us some tombstones. |
| 574 | // If we just filled the map with 32 elements we'd grow because of not enough |
| 575 | // tombstones which masks the issue here. |
| 576 | for (unsigned i = 0; i < 20; ++i) |
| 577 | map[i] = i + 1; |
| 578 | for (unsigned i = 0; i < 10; ++i) |
| 579 | map.erase(i); |
| 580 | for (unsigned i = 20; i < 32; ++i) |
| 581 | map[i] = i + 1; |
| 582 | |
| 583 | // Size tests |
| 584 | EXPECT_EQ(22u, map.size()); |
| 585 | |
| 586 | // Try to find an element which doesn't exist. There was a bug in |
| 587 | // SmallDenseMap which led to a map with num elements == small capacity not |
| 588 | // having an empty bucket any more. Finding an element not in the map would |
| 589 | // therefore never terminate. |
| 590 | EXPECT_TRUE(map.find(32) == map.end()); |
| 591 | } |
| 592 | |
Benjamin Kramer | 1a324fd | 2016-07-21 13:37:53 +0000 | [diff] [blame] | 593 | TEST(DenseMapCustomTest, TryEmplaceTest) { |
| 594 | DenseMap<int, std::unique_ptr<int>> Map; |
| 595 | std::unique_ptr<int> P(new int(2)); |
| 596 | auto Try1 = Map.try_emplace(0, new int(1)); |
| 597 | EXPECT_TRUE(Try1.second); |
| 598 | auto Try2 = Map.try_emplace(0, std::move(P)); |
| 599 | EXPECT_FALSE(Try2.second); |
| 600 | EXPECT_EQ(Try1.first, Try2.first); |
| 601 | EXPECT_NE(nullptr, P); |
| 602 | } |
Daniel Berlin | 5e2cfa2 | 2017-03-10 00:25:26 +0000 | [diff] [blame] | 603 | |
| 604 | TEST(DenseMapCustomTest, ConstTest) { |
| 605 | // Test that const pointers work okay for count and find, even when the |
| 606 | // underlying map is a non-const pointer. |
| 607 | DenseMap<int *, int> Map; |
| 608 | int A; |
| 609 | int *B = &A; |
| 610 | const int *C = &A; |
| 611 | Map.insert({B, 0}); |
| 612 | EXPECT_EQ(Map.count(B), 1u); |
| 613 | EXPECT_EQ(Map.count(C), 1u); |
| 614 | EXPECT_NE(Map.find(B), Map.end()); |
| 615 | EXPECT_NE(Map.find(C), Map.end()); |
| 616 | } |
Misha Brukman | 8fb520e | 2009-01-01 02:24:48 +0000 | [diff] [blame] | 617 | } |