Stuart Hastings | f0e4cac | 2009-05-01 20:47:53 +0000 | [diff] [blame] | 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 | |
Craig Topper | dbf5457 | 2012-09-15 18:45:38 +0000 | [diff] [blame] | 10 | #include "llvm/ADT/DenseSet.h" |
Vitaly Buka | 7326421 | 2016-10-05 20:36:39 +0000 | [diff] [blame] | 11 | #include "gtest/gtest.h" |
| 12 | #include <type_traits> |
Stuart Hastings | f0e4cac | 2009-05-01 20:47:53 +0000 | [diff] [blame] | 13 | |
| 14 | using namespace llvm; |
| 15 | |
| 16 | namespace { |
| 17 | |
David Blaikie | 73d3815 | 2018-09-20 23:11:27 +0000 | [diff] [blame] | 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 | |
Stuart Hastings | f0e4cac | 2009-05-01 20:47:53 +0000 | [diff] [blame] | 25 | // Test hashing with a set of only two entries. |
Vitaly Buka | 7326421 | 2016-10-05 20:36:39 +0000 | [diff] [blame] | 26 | TEST(DenseSetTest, DoubleEntrySetTest) { |
Stuart Hastings | f0e4cac | 2009-05-01 20:47:53 +0000 | [diff] [blame] | 27 | llvm::DenseSet<unsigned> set(2); |
| 28 | set.insert(0); |
| 29 | set.insert(1); |
| 30 | // Original failure was an infinite loop in this call: |
David Blaikie | 20dead8 | 2014-06-20 19:54:13 +0000 | [diff] [blame] | 31 | EXPECT_EQ(0u, set.count(2)); |
Stuart Hastings | f0e4cac | 2009-05-01 20:47:53 +0000 | [diff] [blame] | 32 | } |
| 33 | |
Lang Hames | 63b14ba | 2014-10-19 19:36:33 +0000 | [diff] [blame] | 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 | |
Vitaly Buka | 7326421 | 2016-10-05 20:36:39 +0000 | [diff] [blame] | 49 | // Test fixture |
| 50 | template <typename T> class DenseSetTest : public testing::Test { |
| 51 | protected: |
| 52 | T Set = GetTestSet(); |
Lang Hames | 63b14ba | 2014-10-19 19:36:33 +0000 | [diff] [blame] | 53 | |
Vitaly Buka | 7326421 | 2016-10-05 20:36:39 +0000 | [diff] [blame] | 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>, |
Justin Lebar | 6461aab | 2016-10-17 22:24:28 +0000 | [diff] [blame] | 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>> |
Vitaly Buka | 7326421 | 2016-10-05 20:36:39 +0000 | [diff] [blame] | 71 | DenseSetTestTypes; |
| 72 | TYPED_TEST_CASE(DenseSetTest, DenseSetTestTypes); |
| 73 | |
Justin Lebar | afb3618 | 2016-10-17 22:24:32 +0000 | [diff] [blame] | 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 | |
Lang Hames | fcb831d | 2018-10-15 18:34:36 +0000 | [diff] [blame] | 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 | |
Dean Michael Berris | d0917b6 | 2017-01-24 05:29:40 +0000 | [diff] [blame] | 91 | TYPED_TEST(DenseSetTest, ConstIteratorComparison) { |
Dean Michael Berris | da6e660e | 2017-01-24 04:11:18 +0000 | [diff] [blame] | 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 | |
Dean Michael Berris | d0917b6 | 2017-01-24 05:29:40 +0000 | [diff] [blame] | 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 | |
Justin Lebar | afb3618 | 2016-10-17 22:24:32 +0000 | [diff] [blame] | 107 | TYPED_TEST(DenseSetTest, EmptyInitializerList) { |
| 108 | TypeParam set({}); |
| 109 | EXPECT_EQ(0u, set.size()); |
| 110 | EXPECT_EQ(0u, set.count(0)); |
| 111 | } |
| 112 | |
Vitaly Buka | 7326421 | 2016-10-05 20:36:39 +0000 | [diff] [blame] | 113 | TYPED_TEST(DenseSetTest, FindAsTest) { |
| 114 | auto &set = this->Set; |
Lang Hames | 63b14ba | 2014-10-19 19:36:33 +0000 | [diff] [blame] | 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 | |
Lang Hames | cb8b3a2 | 2018-10-15 15:26:47 +0000 | [diff] [blame] | 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 | |
Mehdi Amini | 568382f | 2016-08-13 20:42:19 +0000 | [diff] [blame] | 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); |
Malcolm Parsons | 5fc9628 | 2018-01-24 10:33:39 +0000 | [diff] [blame] | 208 | // Check that no copy occurred |
Mehdi Amini | 568382f | 2016-08-13 20:42:19 +0000 | [diff] [blame] | 209 | EXPECT_EQ(0, CountCopyAndMove::Copy); |
| 210 | } |
| 211 | } |
Daniel Berlin | 5e2cfa2 | 2017-03-10 00:25:26 +0000 | [diff] [blame] | 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 | } |
Stuart Hastings | f0e4cac | 2009-05-01 20:47:53 +0000 | [diff] [blame] | 225 | } |