blob: db00f8cf8e57aa7178d8e9dbe64d6e0d5e200d4d [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 }
David Blaikie54a7ca32015-03-04 07:57:45 +000049 CtorTester &operator=(const CtorTester &) = default;
Chandler Carrutha1be8422012-06-17 10:33:51 +000050 ~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
59std::set<CtorTester *> CtorTester::Constructed;
60
61struct 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
72CtorTester getTestKey(int i, CtorTester *) { return CtorTester(i); }
73CtorTester getTestValue(int i, CtorTester *) { return CtorTester(42 + i); }
74
Chandler Carruth20dd8382012-06-17 09:05:09 +000075// 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.
79template <typename T>
80class DenseMapTest : public ::testing::Test {
81protected:
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
95template <typename T>
Craig Topper66f09ad2014-06-08 22:29:17 +000096typename T::key_type *const DenseMapTest<T>::dummy_key_ptr = nullptr;
Chandler Carruth20dd8382012-06-17 09:05:09 +000097template <typename T>
Craig Topper66f09ad2014-06-08 22:29:17 +000098typename T::mapped_type *const DenseMapTest<T>::dummy_value_ptr = nullptr;
Chandler Carruth2b50c402012-06-16 01:31:33 +000099
100// Register these types for testing.
101typedef ::testing::Types<DenseMap<uint32_t, uint32_t>,
Chandler Carruth20dd8382012-06-17 09:05:09 +0000102 DenseMap<uint32_t *, uint32_t *>,
Chandler Carrutha1be8422012-06-17 10:33:51 +0000103 DenseMap<CtorTester, CtorTester, CtorTesterMapInfo>,
Chandler Carruth20dd8382012-06-17 09:05:09 +0000104 SmallDenseMap<uint32_t, uint32_t>,
Chandler Carrutha1be8422012-06-17 10:33:51 +0000105 SmallDenseMap<uint32_t *, uint32_t *>,
106 SmallDenseMap<CtorTester, CtorTester, 4,
107 CtorTesterMapInfo>
Chandler Carruth20dd8382012-06-17 09:05:09 +0000108 > DenseMapTestTypes;
Chandler Carruth2b50c402012-06-16 01:31:33 +0000109TYPED_TEST_CASE(DenseMapTest, DenseMapTestTypes);
110
111// Empty map tests
112TYPED_TEST(DenseMapTest, EmptyIntMapTest) {
Misha Brukmanbcf15382009-01-01 02:24:48 +0000113 // Size tests
Chandler Carruth2b50c402012-06-16 01:31:33 +0000114 EXPECT_EQ(0u, this->Map.size());
115 EXPECT_TRUE(this->Map.empty());
Misha Brukmanbcf15382009-01-01 02:24:48 +0000116
117 // Iterator tests
Chandler Carruth2b50c402012-06-16 01:31:33 +0000118 EXPECT_TRUE(this->Map.begin() == this->Map.end());
Misha Brukmanbcf15382009-01-01 02:24:48 +0000119
120 // Lookup tests
Chandler Carruth2b50c402012-06-16 01:31:33 +0000121 EXPECT_FALSE(this->Map.count(this->getKey()));
122 EXPECT_TRUE(this->Map.find(this->getKey()) == this->Map.end());
Reid Kleckner0f679172014-02-13 19:51:13 +0000123#if !defined(_MSC_VER) || defined(__clang__)
Chandler Carruth2b50c402012-06-16 01:31:33 +0000124 EXPECT_EQ(typename TypeParam::mapped_type(),
125 this->Map.lookup(this->getKey()));
Chandler Carrutha68dcb42012-06-16 03:54:11 +0000126#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 Brukmanbcf15382009-01-01 02:24:48 +0000134}
135
136// Constant map tests
Chandler Carruth2b50c402012-06-16 01:31:33 +0000137TYPED_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 Brukmanbcf15382009-01-01 02:24:48 +0000142}
143
144// A map with a single entry
Chandler Carruth2b50c402012-06-16 01:31:33 +0000145TYPED_TEST(DenseMapTest, SingleEntryMapTest) {
146 this->Map[this->getKey()] = this->getValue();
Misha Brukmanbcf15382009-01-01 02:24:48 +0000147
148 // Size tests
Chandler Carruth2b50c402012-06-16 01:31:33 +0000149 EXPECT_EQ(1u, this->Map.size());
150 EXPECT_FALSE(this->Map.begin() == this->Map.end());
151 EXPECT_FALSE(this->Map.empty());
Misha Brukmanbcf15382009-01-01 02:24:48 +0000152
153 // Iterator tests
Chandler Carruth2b50c402012-06-16 01:31:33 +0000154 typename TypeParam::iterator it = this->Map.begin();
155 EXPECT_EQ(this->getKey(), it->first);
156 EXPECT_EQ(this->getValue(), it->second);
Misha Brukmanbcf15382009-01-01 02:24:48 +0000157 ++it;
Chandler Carruth2b50c402012-06-16 01:31:33 +0000158 EXPECT_TRUE(it == this->Map.end());
Misha Brukmanbcf15382009-01-01 02:24:48 +0000159
160 // Lookup tests
Chandler Carruth2b50c402012-06-16 01:31:33 +0000161 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 Brukmanbcf15382009-01-01 02:24:48 +0000165}
166
167// Test clear() method
Chandler Carruth2b50c402012-06-16 01:31:33 +0000168TYPED_TEST(DenseMapTest, ClearTest) {
169 this->Map[this->getKey()] = this->getValue();
170 this->Map.clear();
Misha Brukmanbcf15382009-01-01 02:24:48 +0000171
Chandler Carruth2b50c402012-06-16 01:31:33 +0000172 EXPECT_EQ(0u, this->Map.size());
173 EXPECT_TRUE(this->Map.empty());
174 EXPECT_TRUE(this->Map.begin() == this->Map.end());
Misha Brukmanbcf15382009-01-01 02:24:48 +0000175}
176
177// Test erase(iterator) method
Chandler Carruth2b50c402012-06-16 01:31:33 +0000178TYPED_TEST(DenseMapTest, EraseTest) {
179 this->Map[this->getKey()] = this->getValue();
180 this->Map.erase(this->Map.begin());
Misha Brukmanbcf15382009-01-01 02:24:48 +0000181
Chandler Carruth2b50c402012-06-16 01:31:33 +0000182 EXPECT_EQ(0u, this->Map.size());
183 EXPECT_TRUE(this->Map.empty());
184 EXPECT_TRUE(this->Map.begin() == this->Map.end());
Misha Brukmanbcf15382009-01-01 02:24:48 +0000185}
186
187// Test erase(value) method
Chandler Carruth2b50c402012-06-16 01:31:33 +0000188TYPED_TEST(DenseMapTest, EraseTest2) {
189 this->Map[this->getKey()] = this->getValue();
190 this->Map.erase(this->getKey());
Misha Brukmanbcf15382009-01-01 02:24:48 +0000191
Chandler Carruth2b50c402012-06-16 01:31:33 +0000192 EXPECT_EQ(0u, this->Map.size());
193 EXPECT_TRUE(this->Map.empty());
194 EXPECT_TRUE(this->Map.begin() == this->Map.end());
Misha Brukmanbcf15382009-01-01 02:24:48 +0000195}
196
197// Test insert() method
Chandler Carruth2b50c402012-06-16 01:31:33 +0000198TYPED_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 Brukmanbcf15382009-01-01 02:24:48 +0000202}
203
204// Test copy constructor method
Chandler Carruth2b50c402012-06-16 01:31:33 +0000205TYPED_TEST(DenseMapTest, CopyConstructorTest) {
206 this->Map[this->getKey()] = this->getValue();
207 TypeParam copyMap(this->Map);
Misha Brukmanbcf15382009-01-01 02:24:48 +0000208
209 EXPECT_EQ(1u, copyMap.size());
Chandler Carruth2b50c402012-06-16 01:31:33 +0000210 EXPECT_EQ(this->getValue(), copyMap[this->getKey()]);
Misha Brukmanbcf15382009-01-01 02:24:48 +0000211}
212
Duncan P. N. Exon Smith0d64f8b2014-02-25 23:35:13 +0000213// Test copy constructor method where SmallDenseMap isn't small.
214TYPED_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.
225TYPED_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.
232TYPED_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 Brukmanbcf15382009-01-01 02:24:48 +0000241// Test assignment operator method
Chandler Carruth2b50c402012-06-16 01:31:33 +0000242TYPED_TEST(DenseMapTest, AssignmentTest) {
243 this->Map[this->getKey()] = this->getValue();
244 TypeParam copyMap = this->Map;
Misha Brukmanbcf15382009-01-01 02:24:48 +0000245
246 EXPECT_EQ(1u, copyMap.size());
Chandler Carruth2b50c402012-06-16 01:31:33 +0000247 EXPECT_EQ(this->getValue(), copyMap[this->getKey()]);
Andrew Trickfd4f32b2014-08-04 22:18:25 +0000248
249 // test self-assignment.
250 copyMap = copyMap;
251 EXPECT_EQ(1u, copyMap.size());
252 EXPECT_EQ(this->getValue(), copyMap[this->getKey()]);
Misha Brukmanbcf15382009-01-01 02:24:48 +0000253}
254
Michael Gottesmanef711c12015-10-31 05:23:53 +0000255TYPED_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.
265 copyMap = copyMap;
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 Carruth4de807a2012-06-17 11:28:13 +0000271// Test swap method
272TYPED_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 Brukmanbcf15382009-01-01 02:24:48 +0000307// A more complex iteration test
Chandler Carruth2b50c402012-06-16 01:31:33 +0000308TYPED_TEST(DenseMapTest, IterationTest) {
Misha Brukmanbcf15382009-01-01 02:24:48 +0000309 bool visited[100];
Chandler Carruth2b50c402012-06-16 01:31:33 +0000310 std::map<typename TypeParam::key_type, unsigned> visitedIndex;
Misha Brukmanbcf15382009-01-01 02:24:48 +0000311
312 // Insert 100 numbers into the map
313 for (int i = 0; i < 100; ++i) {
314 visited[i] = false;
Chandler Carruth2b50c402012-06-16 01:31:33 +0000315 visitedIndex[this->getKey(i)] = i;
316
317 this->Map[this->getKey(i)] = this->getValue(i);
Misha Brukmanbcf15382009-01-01 02:24:48 +0000318 }
319
320 // Iterate over all numbers and mark each one found.
Chandler Carruth2b50c402012-06-16 01:31:33 +0000321 for (typename TypeParam::iterator it = this->Map.begin();
322 it != this->Map.end(); ++it)
323 visited[visitedIndex[it->first]] = true;
Misha Brukmanbcf15382009-01-01 02:24:48 +0000324
325 // Ensure every number was visited.
Chandler Carruth2b50c402012-06-16 01:31:33 +0000326 for (int i = 0; i < 100; ++i)
Misha Brukman204b1d22009-01-07 21:13:53 +0000327 ASSERT_TRUE(visited[i]) << "Entry #" << i << " was never visited";
Misha Brukmanbcf15382009-01-01 02:24:48 +0000328}
329
Jeffrey Yasskinb40d3f72009-11-10 01:02:17 +0000330// const_iterator test
Chandler Carruth2b50c402012-06-16 01:31:33 +0000331TYPED_TEST(DenseMapTest, ConstIteratorTest) {
Jeffrey Yasskinb40d3f72009-11-10 01:02:17 +0000332 // Check conversion from iterator to const_iterator.
Chandler Carruth2b50c402012-06-16 01:31:33 +0000333 typename TypeParam::iterator it = this->Map.begin();
334 typename TypeParam::const_iterator cit(it);
Jeffrey Yasskinb40d3f72009-11-10 01:02:17 +0000335 EXPECT_TRUE(it == cit);
336
337 // Check copying of const_iterators.
Chandler Carruth2b50c402012-06-16 01:31:33 +0000338 typename TypeParam::const_iterator cit2(cit);
Jeffrey Yasskinb40d3f72009-11-10 01:02:17 +0000339 EXPECT_TRUE(cit == cit2);
340}
341
Mehdi Amini05eca802016-03-25 05:57:52 +0000342namespace {
343// Simple class that counts how many moves and copy happens when growing a map
344struct 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};
360int CountCopyAndMove::Copy = 0;
361int CountCopyAndMove::Move = 0;
362
363} // anonymous namespace
364
365// Test for the default minimum size of a DenseMap
366TEST(DenseMapCustomTest, DefaultMinReservedSizeTest) {
367 // IF THIS VALUE CHANGE, please update InitialSizeTest, InitFromIterator, and
368 // ReserveTest as well!
369 const int ExpectedInitialBucketCount = 64;
370 // Formula from DenseMap::getMinBucketToReserveForEntries()
371 const int ExpectedMaxInitialEntries = ExpectedInitialBucketCount * 3 / 4 - 1;
372
373 DenseMap<int, CountCopyAndMove> Map;
374 // Will allocate 64 buckets
375 Map.reserve(1);
376 unsigned MemorySize = Map.getMemorySize();
377 CountCopyAndMove::Copy = 0;
378 CountCopyAndMove::Move = 0;
379 for (int i = 0; i < ExpectedMaxInitialEntries; ++i)
Mehdi Amini41298972016-03-25 23:25:06 +0000380 Map.insert(std::pair<int, CountCopyAndMove>(std::piecewise_construct,
381 std::forward_as_tuple(i),
382 std::forward_as_tuple()));
Mehdi Amini05eca802016-03-25 05:57:52 +0000383 // Check that we didn't grow
384 EXPECT_EQ(MemorySize, Map.getMemorySize());
385 // Check that move was called the expected number of times
Mehdi Amini41298972016-03-25 23:25:06 +0000386 EXPECT_EQ(ExpectedMaxInitialEntries, CountCopyAndMove::Move);
Mehdi Amini05eca802016-03-25 05:57:52 +0000387 // Check that no copy occured
388 EXPECT_EQ(0, CountCopyAndMove::Copy);
389
390 // Adding one extra element should grow the map
Mehdi Amini41298972016-03-25 23:25:06 +0000391 Map.insert(std::pair<int, CountCopyAndMove>(
392 std::piecewise_construct,
393 std::forward_as_tuple(ExpectedMaxInitialEntries),
394 std::forward_as_tuple()));
Mehdi Amini05eca802016-03-25 05:57:52 +0000395 // Check that we grew
396 EXPECT_NE(MemorySize, Map.getMemorySize());
397 // Check that move was called the expected number of times
Mehdi Amini9706dcf2016-03-25 15:46:14 +0000398 // This relies on move-construction elision, and cannot be reliably tested.
399 // EXPECT_EQ(ExpectedMaxInitialEntries + 2, CountCopyAndMove::Move);
Mehdi Amini05eca802016-03-25 05:57:52 +0000400 // Check that no copy occured
401 EXPECT_EQ(0, CountCopyAndMove::Copy);
402}
403
404// Make sure creating the map with an initial size of N actually gives us enough
405// buckets to insert N items without increasing allocation size.
406TEST(DenseMapCustomTest, InitialSizeTest) {
407 // Test a few different sizes, 48 is *not* a random choice: we need a value
408 // that is 2/3 of a power of two to stress the grow() condition, and the power
409 // of two has to be at least 64 because of minimum size allocation in the
410 // DenseMap (see DefaultMinReservedSizeTest). 66 is a value just above the
411 // 64 default init.
412 for (auto Size : {1, 2, 48, 66}) {
413 DenseMap<int, CountCopyAndMove> Map(Size);
414 unsigned MemorySize = Map.getMemorySize();
415 CountCopyAndMove::Copy = 0;
416 CountCopyAndMove::Move = 0;
417 for (int i = 0; i < Size; ++i)
Mehdi Amini41298972016-03-25 23:25:06 +0000418 Map.insert(std::pair<int, CountCopyAndMove>(std::piecewise_construct,
419 std::forward_as_tuple(i),
420 std::forward_as_tuple()));
Mehdi Amini05eca802016-03-25 05:57:52 +0000421 // Check that we didn't grow
422 EXPECT_EQ(MemorySize, Map.getMemorySize());
423 // Check that move was called the expected number of times
Mehdi Amini41298972016-03-25 23:25:06 +0000424 EXPECT_EQ(Size, CountCopyAndMove::Move);
Mehdi Amini05eca802016-03-25 05:57:52 +0000425 // Check that no copy occured
426 EXPECT_EQ(0, CountCopyAndMove::Copy);
427 }
428}
429
430// Make sure creating the map with a iterator range does not trigger grow()
431TEST(DenseMapCustomTest, InitFromIterator) {
432 std::vector<std::pair<int, CountCopyAndMove>> Values;
433 // The size is a random value greater than 64 (hardcoded DenseMap min init)
434 const int Count = 65;
435 for (int i = 0; i < Count; i++)
436 Values.emplace_back(i, CountCopyAndMove());
437
438 CountCopyAndMove::Move = 0;
439 CountCopyAndMove::Copy = 0;
440 DenseMap<int, CountCopyAndMove> Map(Values.begin(), Values.end());
441 // Check that no move occured
442 EXPECT_EQ(0, CountCopyAndMove::Move);
443 // Check that copy was called the expected number of times
444 EXPECT_EQ(Count, CountCopyAndMove::Copy);
445}
446
447// Make sure reserve actually gives us enough buckets to insert N items
Fiona Glasera4b1ace2016-03-15 01:50:46 +0000448// without increasing allocation size.
Mehdi Amini05eca802016-03-25 05:57:52 +0000449TEST(DenseMapCustomTest, ReserveTest) {
450 // Test a few different size, 48 is *not* a random choice: we need a value
451 // that is 2/3 of a power of two to stress the grow() condition, and the power
452 // of two has to be at least 64 because of minimum size allocation in the
453 // DenseMap (see DefaultMinReservedSizeTest). 66 is a value just above the
454 // 64 default init.
455 for (auto Size : {1, 2, 48, 66}) {
456 DenseMap<int, CountCopyAndMove> Map;
Mehdi Amini844baa22016-03-22 07:35:51 +0000457 Map.reserve(Size);
Fiona Glasera4b1ace2016-03-15 01:50:46 +0000458 unsigned MemorySize = Map.getMemorySize();
Mehdi Amini05eca802016-03-25 05:57:52 +0000459 CountCopyAndMove::Copy = 0;
460 CountCopyAndMove::Move = 0;
461 for (int i = 0; i < Size; ++i)
Mehdi Amini41298972016-03-25 23:25:06 +0000462 Map.insert(std::pair<int, CountCopyAndMove>(std::piecewise_construct,
463 std::forward_as_tuple(i),
464 std::forward_as_tuple()));
Mehdi Amini05eca802016-03-25 05:57:52 +0000465 // Check that we didn't grow
466 EXPECT_EQ(MemorySize, Map.getMemorySize());
467 // Check that move was called the expected number of times
Mehdi Amini41298972016-03-25 23:25:06 +0000468 EXPECT_EQ(Size, CountCopyAndMove::Move);
Mehdi Amini05eca802016-03-25 05:57:52 +0000469 // Check that no copy occured
470 EXPECT_EQ(0, CountCopyAndMove::Copy);
Fiona Glasera4b1ace2016-03-15 01:50:46 +0000471 }
472}
473
Chandler Carruth59534822015-06-24 10:06:29 +0000474// Make sure DenseMap works with StringRef keys.
475TEST(DenseMapCustomTest, StringRefTest) {
476 DenseMap<StringRef, int> M;
477
478 M["a"] = 1;
479 M["b"] = 2;
480 M["c"] = 3;
481
482 EXPECT_EQ(3u, M.size());
483 EXPECT_EQ(1, M.lookup("a"));
484 EXPECT_EQ(2, M.lookup("b"));
485 EXPECT_EQ(3, M.lookup("c"));
486
487 EXPECT_EQ(0, M.lookup("q"));
488
489 // Test the empty string, spelled various ways.
490 EXPECT_EQ(0, M.lookup(""));
491 EXPECT_EQ(0, M.lookup(StringRef()));
492 EXPECT_EQ(0, M.lookup(StringRef("a", 0)));
493 M[""] = 42;
494 EXPECT_EQ(42, M.lookup(""));
495 EXPECT_EQ(42, M.lookup(StringRef()));
496 EXPECT_EQ(42, M.lookup(StringRef("a", 0)));
497}
498
Rafael Espindolae9f07842016-04-21 12:16:21 +0000499struct CachedHashTest {
500 unsigned Val;
501 unsigned *Counter = nullptr;
502 CachedHashTest(unsigned Val) : Val(Val) {}
503 CachedHashTest(unsigned Val, unsigned *Counter)
504 : Val(Val), Counter(Counter) {}
505};
506}
507namespace llvm {
508template <> struct DenseMapInfo<CachedHashTest> {
509 static CachedHashTest getEmptyKey() { return ~0; }
510 static CachedHashTest getTombstoneKey() { return ~0U - 1; }
511 static unsigned getHashValue(const CachedHashTest &X) {
512 ++*X.Counter;
513 return X.Val;
514 }
515 static bool isEqual(const CachedHashTest &LHS, const CachedHashTest &RHS) {
516 return LHS.Val == RHS.Val;
517 }
518};
519}
520namespace {
521
522TEST(DenseMapCustomTest, CachedHashTest) {
523 unsigned Counter = 0;
524 CachedHashTest Val(0, &Counter);
525 DenseMap<CachedHashTest, int> Map;
526
527 Map[Val] = 0;
528 ASSERT_EQ(1u, Counter);
529
530 Map.reserve(64);
531 ASSERT_EQ(2u, Counter);
532}
533
534// Like above, but now cache the hash.
535TEST(DenseMapCustomTest, CachedHashTest2) {
536 unsigned Counter = 0;
537 CachedHashTest Val(0, &Counter);
538 typedef CachedHash<CachedHashTest> Cached;
539 DenseMap<Cached, int> Map;
540
541 Map[Val] = 0;
542 ASSERT_EQ(1u, Counter);
543
544 Map.reserve(64);
545 ASSERT_EQ(1u, Counter);
546}
547
Talin2a7df512012-01-30 06:55:43 +0000548// Key traits that allows lookup with either an unsigned or char* key;
549// In the latter case, "a" == 0, "b" == 1 and so on.
550struct TestDenseMapInfo {
551 static inline unsigned getEmptyKey() { return ~0; }
552 static inline unsigned getTombstoneKey() { return ~0U - 1; }
553 static unsigned getHashValue(const unsigned& Val) { return Val * 37U; }
554 static unsigned getHashValue(const char* Val) {
555 return (unsigned)(Val[0] - 'a') * 37U;
556 }
557 static bool isEqual(const unsigned& LHS, const unsigned& RHS) {
558 return LHS == RHS;
559 }
560 static bool isEqual(const char* LHS, const unsigned& RHS) {
561 return (unsigned)(LHS[0] - 'a') == RHS;
562 }
563};
564
565// find_as() tests
Chandler Carruth2b50c402012-06-16 01:31:33 +0000566TEST(DenseMapCustomTest, FindAsTest) {
Talin2a7df512012-01-30 06:55:43 +0000567 DenseMap<unsigned, unsigned, TestDenseMapInfo> map;
568 map[0] = 1;
569 map[1] = 2;
570 map[2] = 3;
571
572 // Size tests
573 EXPECT_EQ(3u, map.size());
574
575 // Normal lookup tests
David Blaikie7c8d1392014-06-20 19:54:13 +0000576 EXPECT_EQ(1u, map.count(1));
Talin2a7df512012-01-30 06:55:43 +0000577 EXPECT_EQ(1u, map.find(0)->second);
578 EXPECT_EQ(2u, map.find(1)->second);
579 EXPECT_EQ(3u, map.find(2)->second);
580 EXPECT_TRUE(map.find(3) == map.end());
581
582 // find_as() tests
583 EXPECT_EQ(1u, map.find_as("a")->second);
584 EXPECT_EQ(2u, map.find_as("b")->second);
585 EXPECT_EQ(3u, map.find_as("c")->second);
586 EXPECT_TRUE(map.find_as("d") == map.end());
587}
588
Pete Cooper8ba353d2012-10-23 18:47:35 +0000589struct ContiguousDenseMapInfo {
590 static inline unsigned getEmptyKey() { return ~0; }
591 static inline unsigned getTombstoneKey() { return ~0U - 1; }
592 static unsigned getHashValue(const unsigned& Val) { return Val; }
593 static bool isEqual(const unsigned& LHS, const unsigned& RHS) {
594 return LHS == RHS;
595 }
596};
597
598// Test that filling a small dense map with exactly the number of elements in
599// the map grows to have enough space for an empty bucket.
600TEST(DenseMapCustomTest, SmallDenseMapGrowTest) {
601 SmallDenseMap<unsigned, unsigned, 32, ContiguousDenseMapInfo> map;
602 // Add some number of elements, then delete a few to leave us some tombstones.
603 // If we just filled the map with 32 elements we'd grow because of not enough
604 // tombstones which masks the issue here.
605 for (unsigned i = 0; i < 20; ++i)
606 map[i] = i + 1;
607 for (unsigned i = 0; i < 10; ++i)
608 map.erase(i);
609 for (unsigned i = 20; i < 32; ++i)
610 map[i] = i + 1;
611
612 // Size tests
613 EXPECT_EQ(22u, map.size());
614
615 // Try to find an element which doesn't exist. There was a bug in
616 // SmallDenseMap which led to a map with num elements == small capacity not
617 // having an empty bucket any more. Finding an element not in the map would
618 // therefore never terminate.
619 EXPECT_TRUE(map.find(32) == map.end());
620}
621
Misha Brukmanbcf15382009-01-01 02:24:48 +0000622}