blob: 7368e2ed0e019d743fce2025deda00d722126bb3 [file] [log] [blame]
Stuart Hastingsf0e4cac2009-05-01 20:47:53 +00001//===- llvm/unittest/ADT/DenseSetTest.cpp - DenseSet 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
Craig Topperdbf54572012-09-15 18:45:38 +000010#include "llvm/ADT/DenseSet.h"
Vitaly Buka73264212016-10-05 20:36:39 +000011#include "gtest/gtest.h"
12#include <type_traits>
Stuart Hastingsf0e4cac2009-05-01 20:47:53 +000013
14using namespace llvm;
15
16namespace {
17
David Blaikie73d38152018-09-20 23:11:27 +000018static_assert(std::is_const<std::remove_pointer<
19 DenseSet<int>::const_iterator::pointer>::type>::value,
20 "Iterator pointer type should be const");
21static_assert(std::is_const<std::remove_reference<
22 DenseSet<int>::const_iterator::reference>::type>::value,
23 "Iterator reference type should be const");
24
Stuart Hastingsf0e4cac2009-05-01 20:47:53 +000025// Test hashing with a set of only two entries.
Vitaly Buka73264212016-10-05 20:36:39 +000026TEST(DenseSetTest, DoubleEntrySetTest) {
Stuart Hastingsf0e4cac2009-05-01 20:47:53 +000027 llvm::DenseSet<unsigned> set(2);
28 set.insert(0);
29 set.insert(1);
30 // Original failure was an infinite loop in this call:
David Blaikie20dead82014-06-20 19:54:13 +000031 EXPECT_EQ(0u, set.count(2));
Stuart Hastingsf0e4cac2009-05-01 20:47:53 +000032}
33
Lang Hames63b14ba2014-10-19 19:36:33 +000034struct TestDenseSetInfo {
35 static inline unsigned getEmptyKey() { return ~0; }
36 static inline unsigned getTombstoneKey() { return ~0U - 1; }
37 static unsigned getHashValue(const unsigned& Val) { return Val * 37U; }
38 static unsigned getHashValue(const char* Val) {
39 return (unsigned)(Val[0] - 'a') * 37U;
40 }
41 static bool isEqual(const unsigned& LHS, const unsigned& RHS) {
42 return LHS == RHS;
43 }
44 static bool isEqual(const char* LHS, const unsigned& RHS) {
45 return (unsigned)(LHS[0] - 'a') == RHS;
46 }
47};
48
Vitaly Buka73264212016-10-05 20:36:39 +000049// Test fixture
50template <typename T> class DenseSetTest : public testing::Test {
51protected:
52 T Set = GetTestSet();
Lang Hames63b14ba2014-10-19 19:36:33 +000053
Vitaly Buka73264212016-10-05 20:36:39 +000054private:
55 static T GetTestSet() {
56 typename std::remove_const<T>::type Set;
57 Set.insert(0);
58 Set.insert(1);
59 Set.insert(2);
60 return Set;
61 }
62};
63
64// Register these types for testing.
65typedef ::testing::Types<DenseSet<unsigned, TestDenseSetInfo>,
Justin Lebar6461aab2016-10-17 22:24:28 +000066 const DenseSet<unsigned, TestDenseSetInfo>,
67 SmallDenseSet<unsigned, 1, TestDenseSetInfo>,
68 SmallDenseSet<unsigned, 4, TestDenseSetInfo>,
69 const SmallDenseSet<unsigned, 4, TestDenseSetInfo>,
70 SmallDenseSet<unsigned, 64, TestDenseSetInfo>>
Vitaly Buka73264212016-10-05 20:36:39 +000071 DenseSetTestTypes;
72TYPED_TEST_CASE(DenseSetTest, DenseSetTestTypes);
73
Justin Lebarafb36182016-10-17 22:24:32 +000074TYPED_TEST(DenseSetTest, InitializerList) {
75 TypeParam set({1, 2, 1, 4});
76 EXPECT_EQ(3u, set.size());
77 EXPECT_EQ(1u, set.count(1));
78 EXPECT_EQ(1u, set.count(2));
79 EXPECT_EQ(1u, set.count(4));
80 EXPECT_EQ(0u, set.count(3));
81}
82
Lang Hamesfcb831d2018-10-15 18:34:36 +000083TYPED_TEST(DenseSetTest, InitializerListWithNonPowerOfTwoLength) {
84 TypeParam set({1, 2, 3});
85 EXPECT_EQ(3u, set.size());
86 EXPECT_EQ(1u, set.count(1));
87 EXPECT_EQ(1u, set.count(2));
88 EXPECT_EQ(1u, set.count(3));
89}
90
Dean Michael Berrisd0917b62017-01-24 05:29:40 +000091TYPED_TEST(DenseSetTest, ConstIteratorComparison) {
Dean Michael Berrisda6e660e2017-01-24 04:11:18 +000092 TypeParam set({1});
93 const TypeParam &cset = set;
94 EXPECT_EQ(set.begin(), cset.begin());
95 EXPECT_EQ(set.end(), cset.end());
96 EXPECT_NE(set.end(), cset.begin());
97 EXPECT_NE(set.begin(), cset.end());
98}
99
Dean Michael Berrisd0917b62017-01-24 05:29:40 +0000100TYPED_TEST(DenseSetTest, DefaultConstruction) {
101 typename TypeParam::iterator I, J;
102 typename TypeParam::const_iterator CI, CJ;
103 EXPECT_EQ(I, J);
104 EXPECT_EQ(CI, CJ);
105}
106
Justin Lebarafb36182016-10-17 22:24:32 +0000107TYPED_TEST(DenseSetTest, EmptyInitializerList) {
108 TypeParam set({});
109 EXPECT_EQ(0u, set.size());
110 EXPECT_EQ(0u, set.count(0));
111}
112
Vitaly Buka73264212016-10-05 20:36:39 +0000113TYPED_TEST(DenseSetTest, FindAsTest) {
114 auto &set = this->Set;
Lang Hames63b14ba2014-10-19 19:36:33 +0000115 // Size tests
116 EXPECT_EQ(3u, set.size());
117
118 // Normal lookup tests
119 EXPECT_EQ(1u, set.count(1));
120 EXPECT_EQ(0u, *set.find(0));
121 EXPECT_EQ(1u, *set.find(1));
122 EXPECT_EQ(2u, *set.find(2));
123 EXPECT_TRUE(set.find(3) == set.end());
124
125 // find_as() tests
126 EXPECT_EQ(0u, *set.find_as("a"));
127 EXPECT_EQ(1u, *set.find_as("b"));
128 EXPECT_EQ(2u, *set.find_as("c"));
129 EXPECT_TRUE(set.find_as("d") == set.end());
130}
131
Lang Hamescb8b3a22018-10-15 15:26:47 +0000132TYPED_TEST(DenseSetTest, EqualityComparisonTest) {
133 TypeParam set1({1, 2, 3, 4});
134 TypeParam set2({4, 3, 2, 1});
135 TypeParam set3({2, 3, 4, 5});
136
137 EXPECT_EQ(set1, set2);
138 EXPECT_NE(set1, set3);
139}
140
Mehdi Amini568382f2016-08-13 20:42:19 +0000141// Simple class that counts how many moves and copy happens when growing a map
142struct CountCopyAndMove {
143 static int Move;
144 static int Copy;
145 int Value;
146 CountCopyAndMove(int Value) : Value(Value) {}
147
148 CountCopyAndMove(const CountCopyAndMove &RHS) {
149 Value = RHS.Value;
150 Copy++;
151 }
152 CountCopyAndMove &operator=(const CountCopyAndMove &RHS) {
153 Value = RHS.Value;
154 Copy++;
155 return *this;
156 }
157 CountCopyAndMove(CountCopyAndMove &&RHS) {
158 Value = RHS.Value;
159 Move++;
160 }
161 CountCopyAndMove &operator=(const CountCopyAndMove &&RHS) {
162 Value = RHS.Value;
163 Move++;
164 return *this;
165 }
166};
167int CountCopyAndMove::Copy = 0;
168int CountCopyAndMove::Move = 0;
169} // anonymous namespace
170
171namespace llvm {
172// Specialization required to insert a CountCopyAndMove into a DenseSet.
173template <> struct DenseMapInfo<CountCopyAndMove> {
174 static inline CountCopyAndMove getEmptyKey() { return CountCopyAndMove(-1); };
175 static inline CountCopyAndMove getTombstoneKey() {
176 return CountCopyAndMove(-2);
177 };
178 static unsigned getHashValue(const CountCopyAndMove &Val) {
179 return Val.Value;
180 }
181 static bool isEqual(const CountCopyAndMove &LHS,
182 const CountCopyAndMove &RHS) {
183 return LHS.Value == RHS.Value;
184 }
185};
186}
187
188namespace {
189// Make sure reserve actually gives us enough buckets to insert N items
190// without increasing allocation size.
191TEST(DenseSetCustomTest, ReserveTest) {
192 // Test a few different size, 48 is *not* a random choice: we need a value
193 // that is 2/3 of a power of two to stress the grow() condition, and the power
194 // of two has to be at least 64 because of minimum size allocation in the
195 // DenseMa. 66 is a value just above the 64 default init.
196 for (auto Size : {1, 2, 48, 66}) {
197 DenseSet<CountCopyAndMove> Set;
198 Set.reserve(Size);
199 unsigned MemorySize = Set.getMemorySize();
200 CountCopyAndMove::Copy = 0;
201 CountCopyAndMove::Move = 0;
202 for (int i = 0; i < Size; ++i)
203 Set.insert(CountCopyAndMove(i));
204 // Check that we didn't grow
205 EXPECT_EQ(MemorySize, Set.getMemorySize());
206 // Check that move was called the expected number of times
207 EXPECT_EQ(Size, CountCopyAndMove::Move);
Malcolm Parsons5fc96282018-01-24 10:33:39 +0000208 // Check that no copy occurred
Mehdi Amini568382f2016-08-13 20:42:19 +0000209 EXPECT_EQ(0, CountCopyAndMove::Copy);
210 }
211}
Daniel Berlin5e2cfa22017-03-10 00:25:26 +0000212TEST(DenseSetCustomTest, ConstTest) {
213 // Test that const pointers work okay for count and find, even when the
214 // underlying map is a non-const pointer.
215 DenseSet<int *> Map;
216 int A;
217 int *B = &A;
218 const int *C = &A;
219 Map.insert(B);
220 EXPECT_EQ(Map.count(B), 1u);
221 EXPECT_EQ(Map.count(C), 1u);
222 EXPECT_NE(Map.find(B), Map.end());
223 EXPECT_NE(Map.find(C), Map.end());
224}
Stuart Hastingsf0e4cac2009-05-01 20:47:53 +0000225}