blob: 75a910a24cab7af8f8f5ec6bef8bbdbf9b986e65 [file] [log] [blame]
Misha Brukmanbcf15382009-01-01 02:24:48 +00001//===- 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 Carruth2b50c402012-06-16 01:31:33 +000012#include <map>
Chandler Carrutha1be8422012-06-17 10:33:51 +000013#include <set>
Misha Brukmanbcf15382009-01-01 02:24:48 +000014
15using namespace llvm;
16
17namespace {
18
Chandler Carruth20dd8382012-06-17 09:05:09 +000019uint32_t getTestKey(int i, uint32_t *) { return i; }
20uint32_t getTestValue(int i, uint32_t *) { return 42 + i; }
Chandler Carruth2b50c402012-06-16 01:31:33 +000021
Chandler Carruth20dd8382012-06-17 09:05:09 +000022uint32_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 Brukmanbcf15382009-01-01 02:24:48 +000026}
Chandler Carruth20dd8382012-06-17 09:05:09 +000027uint32_t *getTestValue(int i, uint32_t **) {
Chandler Carruth2b50c402012-06-16 01:31:33 +000028 static uint32_t dummy_arr1[8192];
29 assert(i < 8192 && "Only support 8192 dummy keys.");
30 return &dummy_arr1[i];
31}
32
Chandler Carrutha1be8422012-06-17 10:33:51 +000033/// \brief A test class that tries to check that construction and destruction
34/// occur correctly.
35class CtorTester {
36 static std::set<CtorTester *> Constructed;
37 int Value;
38
39public:
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 }
49 ~CtorTester() {
50 EXPECT_EQ(1u, Constructed.erase(this));
51 }
52 operator uint32_t() const { return Value; }
53
54 int getValue() const { return Value; }
55 bool operator==(const CtorTester &RHS) const { return Value == RHS.Value; }
56};
57
58std::set<CtorTester *> CtorTester::Constructed;
59
60struct CtorTesterMapInfo {
61 static inline CtorTester getEmptyKey() { return CtorTester(-1); }
62 static inline CtorTester getTombstoneKey() { return CtorTester(-2); }
63 static unsigned getHashValue(const CtorTester &Val) {
64 return Val.getValue() * 37u;
65 }
66 static bool isEqual(const CtorTester &LHS, const CtorTester &RHS) {
67 return LHS == RHS;
68 }
69};
70
71CtorTester getTestKey(int i, CtorTester *) { return CtorTester(i); }
72CtorTester getTestValue(int i, CtorTester *) { return CtorTester(42 + i); }
73
Chandler Carruth20dd8382012-06-17 09:05:09 +000074// Test fixture, with helper functions implemented by forwarding to global
75// function overloads selected by component types of the type parameter. This
76// allows all of the map implementations to be tested with shared
77// implementations of helper routines.
78template <typename T>
79class DenseMapTest : public ::testing::Test {
80protected:
81 T Map;
82
83 static typename T::key_type *const dummy_key_ptr;
84 static typename T::mapped_type *const dummy_value_ptr;
85
86 typename T::key_type getKey(int i = 0) {
87 return getTestKey(i, dummy_key_ptr);
88 }
89 typename T::mapped_type getValue(int i = 0) {
90 return getTestValue(i, dummy_value_ptr);
91 }
92};
93
94template <typename T>
Craig Topper66f09ad2014-06-08 22:29:17 +000095typename T::key_type *const DenseMapTest<T>::dummy_key_ptr = nullptr;
Chandler Carruth20dd8382012-06-17 09:05:09 +000096template <typename T>
Craig Topper66f09ad2014-06-08 22:29:17 +000097typename T::mapped_type *const DenseMapTest<T>::dummy_value_ptr = nullptr;
Chandler Carruth2b50c402012-06-16 01:31:33 +000098
99// Register these types for testing.
100typedef ::testing::Types<DenseMap<uint32_t, uint32_t>,
Chandler Carruth20dd8382012-06-17 09:05:09 +0000101 DenseMap<uint32_t *, uint32_t *>,
Chandler Carrutha1be8422012-06-17 10:33:51 +0000102 DenseMap<CtorTester, CtorTester, CtorTesterMapInfo>,
Chandler Carruth20dd8382012-06-17 09:05:09 +0000103 SmallDenseMap<uint32_t, uint32_t>,
Chandler Carrutha1be8422012-06-17 10:33:51 +0000104 SmallDenseMap<uint32_t *, uint32_t *>,
105 SmallDenseMap<CtorTester, CtorTester, 4,
106 CtorTesterMapInfo>
Chandler Carruth20dd8382012-06-17 09:05:09 +0000107 > DenseMapTestTypes;
Chandler Carruth2b50c402012-06-16 01:31:33 +0000108TYPED_TEST_CASE(DenseMapTest, DenseMapTestTypes);
109
110// Empty map tests
111TYPED_TEST(DenseMapTest, EmptyIntMapTest) {
Misha Brukmanbcf15382009-01-01 02:24:48 +0000112 // Size tests
Chandler Carruth2b50c402012-06-16 01:31:33 +0000113 EXPECT_EQ(0u, this->Map.size());
114 EXPECT_TRUE(this->Map.empty());
Misha Brukmanbcf15382009-01-01 02:24:48 +0000115
116 // Iterator tests
Chandler Carruth2b50c402012-06-16 01:31:33 +0000117 EXPECT_TRUE(this->Map.begin() == this->Map.end());
Misha Brukmanbcf15382009-01-01 02:24:48 +0000118
119 // Lookup tests
Chandler Carruth2b50c402012-06-16 01:31:33 +0000120 EXPECT_FALSE(this->Map.count(this->getKey()));
121 EXPECT_TRUE(this->Map.find(this->getKey()) == this->Map.end());
Reid Kleckner0f679172014-02-13 19:51:13 +0000122#if !defined(_MSC_VER) || defined(__clang__)
Chandler Carruth2b50c402012-06-16 01:31:33 +0000123 EXPECT_EQ(typename TypeParam::mapped_type(),
124 this->Map.lookup(this->getKey()));
Chandler Carrutha68dcb42012-06-16 03:54:11 +0000125#else
126 // MSVC, at least old versions, cannot parse the typename to disambiguate
127 // TypeParam::mapped_type as a type. However, because MSVC doesn't implement
128 // two-phase name lookup, it also doesn't require the typename. Deal with
129 // this mutual incompatibility through specialized code.
130 EXPECT_EQ(TypeParam::mapped_type(),
131 this->Map.lookup(this->getKey()));
132#endif
Misha Brukmanbcf15382009-01-01 02:24:48 +0000133}
134
135// Constant map tests
Chandler Carruth2b50c402012-06-16 01:31:33 +0000136TYPED_TEST(DenseMapTest, ConstEmptyMapTest) {
137 const TypeParam &ConstMap = this->Map;
138 EXPECT_EQ(0u, ConstMap.size());
139 EXPECT_TRUE(ConstMap.empty());
140 EXPECT_TRUE(ConstMap.begin() == ConstMap.end());
Misha Brukmanbcf15382009-01-01 02:24:48 +0000141}
142
143// A map with a single entry
Chandler Carruth2b50c402012-06-16 01:31:33 +0000144TYPED_TEST(DenseMapTest, SingleEntryMapTest) {
145 this->Map[this->getKey()] = this->getValue();
Misha Brukmanbcf15382009-01-01 02:24:48 +0000146
147 // Size tests
Chandler Carruth2b50c402012-06-16 01:31:33 +0000148 EXPECT_EQ(1u, this->Map.size());
149 EXPECT_FALSE(this->Map.begin() == this->Map.end());
150 EXPECT_FALSE(this->Map.empty());
Misha Brukmanbcf15382009-01-01 02:24:48 +0000151
152 // Iterator tests
Chandler Carruth2b50c402012-06-16 01:31:33 +0000153 typename TypeParam::iterator it = this->Map.begin();
154 EXPECT_EQ(this->getKey(), it->first);
155 EXPECT_EQ(this->getValue(), it->second);
Misha Brukmanbcf15382009-01-01 02:24:48 +0000156 ++it;
Chandler Carruth2b50c402012-06-16 01:31:33 +0000157 EXPECT_TRUE(it == this->Map.end());
Misha Brukmanbcf15382009-01-01 02:24:48 +0000158
159 // Lookup tests
Chandler Carruth2b50c402012-06-16 01:31:33 +0000160 EXPECT_TRUE(this->Map.count(this->getKey()));
161 EXPECT_TRUE(this->Map.find(this->getKey()) == this->Map.begin());
162 EXPECT_EQ(this->getValue(), this->Map.lookup(this->getKey()));
163 EXPECT_EQ(this->getValue(), this->Map[this->getKey()]);
Misha Brukmanbcf15382009-01-01 02:24:48 +0000164}
165
166// Test clear() method
Chandler Carruth2b50c402012-06-16 01:31:33 +0000167TYPED_TEST(DenseMapTest, ClearTest) {
168 this->Map[this->getKey()] = this->getValue();
169 this->Map.clear();
Misha Brukmanbcf15382009-01-01 02:24:48 +0000170
Chandler Carruth2b50c402012-06-16 01:31:33 +0000171 EXPECT_EQ(0u, this->Map.size());
172 EXPECT_TRUE(this->Map.empty());
173 EXPECT_TRUE(this->Map.begin() == this->Map.end());
Misha Brukmanbcf15382009-01-01 02:24:48 +0000174}
175
176// Test erase(iterator) method
Chandler Carruth2b50c402012-06-16 01:31:33 +0000177TYPED_TEST(DenseMapTest, EraseTest) {
178 this->Map[this->getKey()] = this->getValue();
179 this->Map.erase(this->Map.begin());
Misha Brukmanbcf15382009-01-01 02:24:48 +0000180
Chandler Carruth2b50c402012-06-16 01:31:33 +0000181 EXPECT_EQ(0u, this->Map.size());
182 EXPECT_TRUE(this->Map.empty());
183 EXPECT_TRUE(this->Map.begin() == this->Map.end());
Misha Brukmanbcf15382009-01-01 02:24:48 +0000184}
185
186// Test erase(value) method
Chandler Carruth2b50c402012-06-16 01:31:33 +0000187TYPED_TEST(DenseMapTest, EraseTest2) {
188 this->Map[this->getKey()] = this->getValue();
189 this->Map.erase(this->getKey());
Misha Brukmanbcf15382009-01-01 02:24:48 +0000190
Chandler Carruth2b50c402012-06-16 01:31:33 +0000191 EXPECT_EQ(0u, this->Map.size());
192 EXPECT_TRUE(this->Map.empty());
193 EXPECT_TRUE(this->Map.begin() == this->Map.end());
Misha Brukmanbcf15382009-01-01 02:24:48 +0000194}
195
196// Test insert() method
Chandler Carruth2b50c402012-06-16 01:31:33 +0000197TYPED_TEST(DenseMapTest, InsertTest) {
198 this->Map.insert(std::make_pair(this->getKey(), this->getValue()));
199 EXPECT_EQ(1u, this->Map.size());
200 EXPECT_EQ(this->getValue(), this->Map[this->getKey()]);
Misha Brukmanbcf15382009-01-01 02:24:48 +0000201}
202
203// Test copy constructor method
Chandler Carruth2b50c402012-06-16 01:31:33 +0000204TYPED_TEST(DenseMapTest, CopyConstructorTest) {
205 this->Map[this->getKey()] = this->getValue();
206 TypeParam copyMap(this->Map);
Misha Brukmanbcf15382009-01-01 02:24:48 +0000207
208 EXPECT_EQ(1u, copyMap.size());
Chandler Carruth2b50c402012-06-16 01:31:33 +0000209 EXPECT_EQ(this->getValue(), copyMap[this->getKey()]);
Misha Brukmanbcf15382009-01-01 02:24:48 +0000210}
211
Duncan P. N. Exon Smith0d64f8b2014-02-25 23:35:13 +0000212// Test copy constructor method where SmallDenseMap isn't small.
213TYPED_TEST(DenseMapTest, CopyConstructorNotSmallTest) {
214 for (int Key = 0; Key < 5; ++Key)
215 this->Map[this->getKey(Key)] = this->getValue(Key);
216 TypeParam copyMap(this->Map);
217
218 EXPECT_EQ(5u, copyMap.size());
219 for (int Key = 0; Key < 5; ++Key)
220 EXPECT_EQ(this->getValue(Key), copyMap[this->getKey(Key)]);
221}
222
223// Test copying from a default-constructed map.
224TYPED_TEST(DenseMapTest, CopyConstructorFromDefaultTest) {
225 TypeParam copyMap(this->Map);
226
227 EXPECT_TRUE(copyMap.empty());
228}
229
230// Test copying from an empty map where SmallDenseMap isn't small.
231TYPED_TEST(DenseMapTest, CopyConstructorFromEmptyTest) {
232 for (int Key = 0; Key < 5; ++Key)
233 this->Map[this->getKey(Key)] = this->getValue(Key);
234 this->Map.clear();
235 TypeParam copyMap(this->Map);
236
237 EXPECT_TRUE(copyMap.empty());
238}
239
Misha Brukmanbcf15382009-01-01 02:24:48 +0000240// Test assignment operator method
Chandler Carruth2b50c402012-06-16 01:31:33 +0000241TYPED_TEST(DenseMapTest, AssignmentTest) {
242 this->Map[this->getKey()] = this->getValue();
243 TypeParam copyMap = this->Map;
Misha Brukmanbcf15382009-01-01 02:24:48 +0000244
245 EXPECT_EQ(1u, copyMap.size());
Chandler Carruth2b50c402012-06-16 01:31:33 +0000246 EXPECT_EQ(this->getValue(), copyMap[this->getKey()]);
Misha Brukmanbcf15382009-01-01 02:24:48 +0000247}
248
Chandler Carruth4de807a2012-06-17 11:28:13 +0000249// Test swap method
250TYPED_TEST(DenseMapTest, SwapTest) {
251 this->Map[this->getKey()] = this->getValue();
252 TypeParam otherMap;
253
254 this->Map.swap(otherMap);
255 EXPECT_EQ(0u, this->Map.size());
256 EXPECT_TRUE(this->Map.empty());
257 EXPECT_EQ(1u, otherMap.size());
258 EXPECT_EQ(this->getValue(), otherMap[this->getKey()]);
259
260 this->Map.swap(otherMap);
261 EXPECT_EQ(0u, otherMap.size());
262 EXPECT_TRUE(otherMap.empty());
263 EXPECT_EQ(1u, this->Map.size());
264 EXPECT_EQ(this->getValue(), this->Map[this->getKey()]);
265
266 // Make this more interesting by inserting 100 numbers into the map.
267 for (int i = 0; i < 100; ++i)
268 this->Map[this->getKey(i)] = this->getValue(i);
269
270 this->Map.swap(otherMap);
271 EXPECT_EQ(0u, this->Map.size());
272 EXPECT_TRUE(this->Map.empty());
273 EXPECT_EQ(100u, otherMap.size());
274 for (int i = 0; i < 100; ++i)
275 EXPECT_EQ(this->getValue(i), otherMap[this->getKey(i)]);
276
277 this->Map.swap(otherMap);
278 EXPECT_EQ(0u, otherMap.size());
279 EXPECT_TRUE(otherMap.empty());
280 EXPECT_EQ(100u, this->Map.size());
281 for (int i = 0; i < 100; ++i)
282 EXPECT_EQ(this->getValue(i), this->Map[this->getKey(i)]);
283}
284
Misha Brukmanbcf15382009-01-01 02:24:48 +0000285// A more complex iteration test
Chandler Carruth2b50c402012-06-16 01:31:33 +0000286TYPED_TEST(DenseMapTest, IterationTest) {
Misha Brukmanbcf15382009-01-01 02:24:48 +0000287 bool visited[100];
Chandler Carruth2b50c402012-06-16 01:31:33 +0000288 std::map<typename TypeParam::key_type, unsigned> visitedIndex;
Misha Brukmanbcf15382009-01-01 02:24:48 +0000289
290 // Insert 100 numbers into the map
291 for (int i = 0; i < 100; ++i) {
292 visited[i] = false;
Chandler Carruth2b50c402012-06-16 01:31:33 +0000293 visitedIndex[this->getKey(i)] = i;
294
295 this->Map[this->getKey(i)] = this->getValue(i);
Misha Brukmanbcf15382009-01-01 02:24:48 +0000296 }
297
298 // Iterate over all numbers and mark each one found.
Chandler Carruth2b50c402012-06-16 01:31:33 +0000299 for (typename TypeParam::iterator it = this->Map.begin();
300 it != this->Map.end(); ++it)
301 visited[visitedIndex[it->first]] = true;
Misha Brukmanbcf15382009-01-01 02:24:48 +0000302
303 // Ensure every number was visited.
Chandler Carruth2b50c402012-06-16 01:31:33 +0000304 for (int i = 0; i < 100; ++i)
Misha Brukman204b1d22009-01-07 21:13:53 +0000305 ASSERT_TRUE(visited[i]) << "Entry #" << i << " was never visited";
Misha Brukmanbcf15382009-01-01 02:24:48 +0000306}
307
Jeffrey Yasskinb40d3f72009-11-10 01:02:17 +0000308// const_iterator test
Chandler Carruth2b50c402012-06-16 01:31:33 +0000309TYPED_TEST(DenseMapTest, ConstIteratorTest) {
Jeffrey Yasskinb40d3f72009-11-10 01:02:17 +0000310 // Check conversion from iterator to const_iterator.
Chandler Carruth2b50c402012-06-16 01:31:33 +0000311 typename TypeParam::iterator it = this->Map.begin();
312 typename TypeParam::const_iterator cit(it);
Jeffrey Yasskinb40d3f72009-11-10 01:02:17 +0000313 EXPECT_TRUE(it == cit);
314
315 // Check copying of const_iterators.
Chandler Carruth2b50c402012-06-16 01:31:33 +0000316 typename TypeParam::const_iterator cit2(cit);
Jeffrey Yasskinb40d3f72009-11-10 01:02:17 +0000317 EXPECT_TRUE(cit == cit2);
318}
319
Talin2a7df512012-01-30 06:55:43 +0000320// Key traits that allows lookup with either an unsigned or char* key;
321// In the latter case, "a" == 0, "b" == 1 and so on.
322struct TestDenseMapInfo {
323 static inline unsigned getEmptyKey() { return ~0; }
324 static inline unsigned getTombstoneKey() { return ~0U - 1; }
325 static unsigned getHashValue(const unsigned& Val) { return Val * 37U; }
326 static unsigned getHashValue(const char* Val) {
327 return (unsigned)(Val[0] - 'a') * 37U;
328 }
329 static bool isEqual(const unsigned& LHS, const unsigned& RHS) {
330 return LHS == RHS;
331 }
332 static bool isEqual(const char* LHS, const unsigned& RHS) {
333 return (unsigned)(LHS[0] - 'a') == RHS;
334 }
335};
336
337// find_as() tests
Chandler Carruth2b50c402012-06-16 01:31:33 +0000338TEST(DenseMapCustomTest, FindAsTest) {
Talin2a7df512012-01-30 06:55:43 +0000339 DenseMap<unsigned, unsigned, TestDenseMapInfo> map;
340 map[0] = 1;
341 map[1] = 2;
342 map[2] = 3;
343
344 // Size tests
345 EXPECT_EQ(3u, map.size());
346
347 // Normal lookup tests
David Blaikie7c8d1392014-06-20 19:54:13 +0000348 EXPECT_EQ(1u, map.count(1));
Talin2a7df512012-01-30 06:55:43 +0000349 EXPECT_EQ(1u, map.find(0)->second);
350 EXPECT_EQ(2u, map.find(1)->second);
351 EXPECT_EQ(3u, map.find(2)->second);
352 EXPECT_TRUE(map.find(3) == map.end());
353
354 // find_as() tests
355 EXPECT_EQ(1u, map.find_as("a")->second);
356 EXPECT_EQ(2u, map.find_as("b")->second);
357 EXPECT_EQ(3u, map.find_as("c")->second);
358 EXPECT_TRUE(map.find_as("d") == map.end());
359}
360
Pete Cooper8ba353d2012-10-23 18:47:35 +0000361struct ContiguousDenseMapInfo {
362 static inline unsigned getEmptyKey() { return ~0; }
363 static inline unsigned getTombstoneKey() { return ~0U - 1; }
364 static unsigned getHashValue(const unsigned& Val) { return Val; }
365 static bool isEqual(const unsigned& LHS, const unsigned& RHS) {
366 return LHS == RHS;
367 }
368};
369
370// Test that filling a small dense map with exactly the number of elements in
371// the map grows to have enough space for an empty bucket.
372TEST(DenseMapCustomTest, SmallDenseMapGrowTest) {
373 SmallDenseMap<unsigned, unsigned, 32, ContiguousDenseMapInfo> map;
374 // Add some number of elements, then delete a few to leave us some tombstones.
375 // If we just filled the map with 32 elements we'd grow because of not enough
376 // tombstones which masks the issue here.
377 for (unsigned i = 0; i < 20; ++i)
378 map[i] = i + 1;
379 for (unsigned i = 0; i < 10; ++i)
380 map.erase(i);
381 for (unsigned i = 20; i < 32; ++i)
382 map[i] = i + 1;
383
384 // Size tests
385 EXPECT_EQ(22u, map.size());
386
387 // Try to find an element which doesn't exist. There was a bug in
388 // SmallDenseMap which led to a map with num elements == small capacity not
389 // having an empty bucket any more. Finding an element not in the map would
390 // therefore never terminate.
391 EXPECT_TRUE(map.find(32) == map.end());
392}
393
Misha Brukmanbcf15382009-01-01 02:24:48 +0000394}