xref: /llvm-project/llvm/unittests/ADT/DenseSetTest.cpp (revision 28332c415c323d3d7db1031bd39f62498f18486e)
1 //===- 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 
10 #include "llvm/ADT/DenseSet.h"
11 #include "gtest/gtest.h"
12 #include <type_traits>
13 
14 using namespace llvm;
15 
16 namespace {
17 
18 static_assert(std::is_const<std::remove_pointer<
19                   DenseSet<int>::const_iterator::pointer>::type>::value,
20               "Iterator pointer type should be const");
21 static_assert(std::is_const<std::remove_reference<
22                   DenseSet<int>::const_iterator::reference>::type>::value,
23               "Iterator reference type should be const");
24 
25 // Test hashing with a set of only two entries.
26 TEST(DenseSetTest, DoubleEntrySetTest) {
27   llvm::DenseSet<unsigned> set(2);
28   set.insert(0);
29   set.insert(1);
30   // Original failure was an infinite loop in this call:
31   EXPECT_EQ(0u, set.count(2));
32 }
33 
34 struct 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 
49 // Test fixture
50 template <typename T> class DenseSetTest : public testing::Test {
51 protected:
52   T Set = GetTestSet();
53 
54 private:
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.
65 typedef ::testing::Types<DenseSet<unsigned, TestDenseSetInfo>,
66                          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>>
71     DenseSetTestTypes;
72 TYPED_TEST_CASE(DenseSetTest, DenseSetTestTypes);
73 
74 TYPED_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 
83 TYPED_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 
91 TYPED_TEST(DenseSetTest, ConstIteratorComparison) {
92   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 
100 TYPED_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 
107 TYPED_TEST(DenseSetTest, EmptyInitializerList) {
108   TypeParam set({});
109   EXPECT_EQ(0u, set.size());
110   EXPECT_EQ(0u, set.count(0));
111 }
112 
113 TYPED_TEST(DenseSetTest, FindAsTest) {
114   auto &set = this->Set;
115   // 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 
132 TYPED_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 
141 // Simple class that counts how many moves and copy happens when growing a map
142 struct 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 };
167 int CountCopyAndMove::Copy = 0;
168 int CountCopyAndMove::Move = 0;
169 } // anonymous namespace
170 
171 namespace llvm {
172 // Specialization required to insert a CountCopyAndMove into a DenseSet.
173 template <> 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 
188 namespace {
189 // Make sure reserve actually gives us enough buckets to insert N items
190 // without increasing allocation size.
191 TEST(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);
208     // Check that no copy occurred
209     EXPECT_EQ(0, CountCopyAndMove::Copy);
210   }
211 }
212 TEST(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 }
225 }
226