1 //===- llvm/unittest/ADT/DenseMapMap.cpp - DenseMap unit tests --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "gtest/gtest.h"
11 #include "llvm/ADT/DenseMap.h"
19 uint32_t getTestKey(int i, uint32_t *) { return i; }
20 uint32_t getTestValue(int i, uint32_t *) { return 42 + i; }
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];
27 uint32_t *getTestValue(int i, uint32_t **) {
28 static uint32_t dummy_arr1[8192];
29 assert(i < 8192 && "Only support 8192 dummy keys.");
30 return &dummy_arr1[i];
33 /// \brief A test class that tries to check that construction and destruction
36 static std::set<CtorTester *> Constructed;
40 explicit CtorTester(int Value = 0) : Value(Value) {
41 EXPECT_TRUE(Constructed.insert(this).second);
43 CtorTester(uint32_t Value) : Value(Value) {
44 EXPECT_TRUE(Constructed.insert(this).second);
46 CtorTester(const CtorTester &Arg) : Value(Arg.Value) {
47 EXPECT_TRUE(Constructed.insert(this).second);
49 CtorTester &operator=(const CtorTester &) = default;
51 EXPECT_EQ(1u, Constructed.erase(this));
53 operator uint32_t() const { return Value; }
55 int getValue() const { return Value; }
56 bool operator==(const CtorTester &RHS) const { return Value == RHS.Value; }
59 std::set<CtorTester *> CtorTester::Constructed;
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;
67 static bool isEqual(const CtorTester &LHS, const CtorTester &RHS) {
72 CtorTester getTestKey(int i, CtorTester *) { return CtorTester(i); }
73 CtorTester getTestValue(int i, CtorTester *) { return CtorTester(42 + i); }
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.
80 class DenseMapTest : public ::testing::Test {
84 static typename T::key_type *const dummy_key_ptr;
85 static typename T::mapped_type *const dummy_value_ptr;
87 typename T::key_type getKey(int i = 0) {
88 return getTestKey(i, dummy_key_ptr);
90 typename T::mapped_type getValue(int i = 0) {
91 return getTestValue(i, dummy_value_ptr);
96 typename T::key_type *const DenseMapTest<T>::dummy_key_ptr = nullptr;
98 typename T::mapped_type *const DenseMapTest<T>::dummy_value_ptr = nullptr;
100 // Register these types for testing.
101 typedef ::testing::Types<DenseMap<uint32_t, uint32_t>,
102 DenseMap<uint32_t *, uint32_t *>,
103 DenseMap<CtorTester, CtorTester, CtorTesterMapInfo>,
104 SmallDenseMap<uint32_t, uint32_t>,
105 SmallDenseMap<uint32_t *, uint32_t *>,
106 SmallDenseMap<CtorTester, CtorTester, 4,
109 TYPED_TEST_CASE(DenseMapTest, DenseMapTestTypes);
112 TYPED_TEST(DenseMapTest, EmptyIntMapTest) {
114 EXPECT_EQ(0u, this->Map.size());
115 EXPECT_TRUE(this->Map.empty());
118 EXPECT_TRUE(this->Map.begin() == this->Map.end());
121 EXPECT_FALSE(this->Map.count(this->getKey()));
122 EXPECT_TRUE(this->Map.find(this->getKey()) == this->Map.end());
123 #if !defined(_MSC_VER) || defined(__clang__)
124 EXPECT_EQ(typename TypeParam::mapped_type(),
125 this->Map.lookup(this->getKey()));
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()));
136 // Constant map tests
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());
144 // A map with a single entry
145 TYPED_TEST(DenseMapTest, SingleEntryMapTest) {
146 this->Map[this->getKey()] = this->getValue();
149 EXPECT_EQ(1u, this->Map.size());
150 EXPECT_FALSE(this->Map.begin() == this->Map.end());
151 EXPECT_FALSE(this->Map.empty());
154 typename TypeParam::iterator it = this->Map.begin();
155 EXPECT_EQ(this->getKey(), it->first);
156 EXPECT_EQ(this->getValue(), it->second);
158 EXPECT_TRUE(it == this->Map.end());
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()]);
167 // Test clear() method
168 TYPED_TEST(DenseMapTest, ClearTest) {
169 this->Map[this->getKey()] = this->getValue();
172 EXPECT_EQ(0u, this->Map.size());
173 EXPECT_TRUE(this->Map.empty());
174 EXPECT_TRUE(this->Map.begin() == this->Map.end());
177 // Test erase(iterator) method
178 TYPED_TEST(DenseMapTest, EraseTest) {
179 this->Map[this->getKey()] = this->getValue();
180 this->Map.erase(this->Map.begin());
182 EXPECT_EQ(0u, this->Map.size());
183 EXPECT_TRUE(this->Map.empty());
184 EXPECT_TRUE(this->Map.begin() == this->Map.end());
187 // Test erase(value) method
188 TYPED_TEST(DenseMapTest, EraseTest2) {
189 this->Map[this->getKey()] = this->getValue();
190 this->Map.erase(this->getKey());
192 EXPECT_EQ(0u, this->Map.size());
193 EXPECT_TRUE(this->Map.empty());
194 EXPECT_TRUE(this->Map.begin() == this->Map.end());
197 // Test insert() method
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()]);
204 // Test copy constructor method
205 TYPED_TEST(DenseMapTest, CopyConstructorTest) {
206 this->Map[this->getKey()] = this->getValue();
207 TypeParam copyMap(this->Map);
209 EXPECT_EQ(1u, copyMap.size());
210 EXPECT_EQ(this->getValue(), copyMap[this->getKey()]);
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);
219 EXPECT_EQ(5u, copyMap.size());
220 for (int Key = 0; Key < 5; ++Key)
221 EXPECT_EQ(this->getValue(Key), copyMap[this->getKey(Key)]);
224 // Test copying from a default-constructed map.
225 TYPED_TEST(DenseMapTest, CopyConstructorFromDefaultTest) {
226 TypeParam copyMap(this->Map);
228 EXPECT_TRUE(copyMap.empty());
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);
236 TypeParam copyMap(this->Map);
238 EXPECT_TRUE(copyMap.empty());
241 // Test assignment operator method
242 TYPED_TEST(DenseMapTest, AssignmentTest) {
243 this->Map[this->getKey()] = this->getValue();
244 TypeParam copyMap = this->Map;
246 EXPECT_EQ(1u, copyMap.size());
247 EXPECT_EQ(this->getValue(), copyMap[this->getKey()]);
249 // test self-assignment.
251 EXPECT_EQ(1u, copyMap.size());
252 EXPECT_EQ(this->getValue(), copyMap[this->getKey()]);
256 TYPED_TEST(DenseMapTest, SwapTest) {
257 this->Map[this->getKey()] = this->getValue();
260 this->Map.swap(otherMap);
261 EXPECT_EQ(0u, this->Map.size());
262 EXPECT_TRUE(this->Map.empty());
263 EXPECT_EQ(1u, otherMap.size());
264 EXPECT_EQ(this->getValue(), otherMap[this->getKey()]);
266 this->Map.swap(otherMap);
267 EXPECT_EQ(0u, otherMap.size());
268 EXPECT_TRUE(otherMap.empty());
269 EXPECT_EQ(1u, this->Map.size());
270 EXPECT_EQ(this->getValue(), this->Map[this->getKey()]);
272 // Make this more interesting by inserting 100 numbers into the map.
273 for (int i = 0; i < 100; ++i)
274 this->Map[this->getKey(i)] = this->getValue(i);
276 this->Map.swap(otherMap);
277 EXPECT_EQ(0u, this->Map.size());
278 EXPECT_TRUE(this->Map.empty());
279 EXPECT_EQ(100u, otherMap.size());
280 for (int i = 0; i < 100; ++i)
281 EXPECT_EQ(this->getValue(i), otherMap[this->getKey(i)]);
283 this->Map.swap(otherMap);
284 EXPECT_EQ(0u, otherMap.size());
285 EXPECT_TRUE(otherMap.empty());
286 EXPECT_EQ(100u, this->Map.size());
287 for (int i = 0; i < 100; ++i)
288 EXPECT_EQ(this->getValue(i), this->Map[this->getKey(i)]);
291 // A more complex iteration test
292 TYPED_TEST(DenseMapTest, IterationTest) {
294 std::map<typename TypeParam::key_type, unsigned> visitedIndex;
296 // Insert 100 numbers into the map
297 for (int i = 0; i < 100; ++i) {
299 visitedIndex[this->getKey(i)] = i;
301 this->Map[this->getKey(i)] = this->getValue(i);
304 // Iterate over all numbers and mark each one found.
305 for (typename TypeParam::iterator it = this->Map.begin();
306 it != this->Map.end(); ++it)
307 visited[visitedIndex[it->first]] = true;
309 // Ensure every number was visited.
310 for (int i = 0; i < 100; ++i)
311 ASSERT_TRUE(visited[i]) << "Entry #" << i << " was never visited";
314 // const_iterator test
315 TYPED_TEST(DenseMapTest, ConstIteratorTest) {
316 // Check conversion from iterator to const_iterator.
317 typename TypeParam::iterator it = this->Map.begin();
318 typename TypeParam::const_iterator cit(it);
319 EXPECT_TRUE(it == cit);
321 // Check copying of const_iterators.
322 typename TypeParam::const_iterator cit2(cit);
323 EXPECT_TRUE(cit == cit2);
326 // Make sure DenseMap works with StringRef keys.
327 TEST(DenseMapCustomTest, StringRefTest) {
328 DenseMap<StringRef, int> M;
334 EXPECT_EQ(3u, M.size());
335 EXPECT_EQ(1, M.lookup("a"));
336 EXPECT_EQ(2, M.lookup("b"));
337 EXPECT_EQ(3, M.lookup("c"));
339 EXPECT_EQ(0, M.lookup("q"));
341 // Test the empty string, spelled various ways.
342 EXPECT_EQ(0, M.lookup(""));
343 EXPECT_EQ(0, M.lookup(StringRef()));
344 EXPECT_EQ(0, M.lookup(StringRef("a", 0)));
346 EXPECT_EQ(42, M.lookup(""));
347 EXPECT_EQ(42, M.lookup(StringRef()));
348 EXPECT_EQ(42, M.lookup(StringRef("a", 0)));
351 // Key traits that allows lookup with either an unsigned or char* key;
352 // In the latter case, "a" == 0, "b" == 1 and so on.
353 struct TestDenseMapInfo {
354 static inline unsigned getEmptyKey() { return ~0; }
355 static inline unsigned getTombstoneKey() { return ~0U - 1; }
356 static unsigned getHashValue(const unsigned& Val) { return Val * 37U; }
357 static unsigned getHashValue(const char* Val) {
358 return (unsigned)(Val[0] - 'a') * 37U;
360 static bool isEqual(const unsigned& LHS, const unsigned& RHS) {
363 static bool isEqual(const char* LHS, const unsigned& RHS) {
364 return (unsigned)(LHS[0] - 'a') == RHS;
369 TEST(DenseMapCustomTest, FindAsTest) {
370 DenseMap<unsigned, unsigned, TestDenseMapInfo> map;
376 EXPECT_EQ(3u, map.size());
378 // Normal lookup tests
379 EXPECT_EQ(1u, map.count(1));
380 EXPECT_EQ(1u, map.find(0)->second);
381 EXPECT_EQ(2u, map.find(1)->second);
382 EXPECT_EQ(3u, map.find(2)->second);
383 EXPECT_TRUE(map.find(3) == map.end());
386 EXPECT_EQ(1u, map.find_as("a")->second);
387 EXPECT_EQ(2u, map.find_as("b")->second);
388 EXPECT_EQ(3u, map.find_as("c")->second);
389 EXPECT_TRUE(map.find_as("d") == map.end());
392 struct ContiguousDenseMapInfo {
393 static inline unsigned getEmptyKey() { return ~0; }
394 static inline unsigned getTombstoneKey() { return ~0U - 1; }
395 static unsigned getHashValue(const unsigned& Val) { return Val; }
396 static bool isEqual(const unsigned& LHS, const unsigned& RHS) {
401 // Test that filling a small dense map with exactly the number of elements in
402 // the map grows to have enough space for an empty bucket.
403 TEST(DenseMapCustomTest, SmallDenseMapGrowTest) {
404 SmallDenseMap<unsigned, unsigned, 32, ContiguousDenseMapInfo> map;
405 // Add some number of elements, then delete a few to leave us some tombstones.
406 // If we just filled the map with 32 elements we'd grow because of not enough
407 // tombstones which masks the issue here.
408 for (unsigned i = 0; i < 20; ++i)
410 for (unsigned i = 0; i < 10; ++i)
412 for (unsigned i = 20; i < 32; ++i)
416 EXPECT_EQ(22u, map.size());
418 // Try to find an element which doesn't exist. There was a bug in
419 // SmallDenseMap which led to a map with num elements == small capacity not
420 // having an empty bucket any more. Finding an element not in the map would
421 // therefore never terminate.
422 EXPECT_TRUE(map.find(32) == map.end());