Stuart Hastings | e54523d | 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 | bc40d7e | 2012-09-15 18:45:38 +0000 | [diff] [blame] | 10 | #include "llvm/ADT/DenseSet.h" |
Vitaly Buka | f12b1c7 | 2016-10-05 20:36:39 +0000 | [diff] [blame] | 11 | #include "gtest/gtest.h" |
| 12 | #include <type_traits> |
Stuart Hastings | e54523d | 2009-05-01 20:47:53 +0000 | [diff] [blame] | 13 | |
| 14 | using namespace llvm; |
| 15 | |
| 16 | namespace { |
| 17 | |
Stuart Hastings | e54523d | 2009-05-01 20:47:53 +0000 | [diff] [blame] | 18 | // Test hashing with a set of only two entries. |
Vitaly Buka | f12b1c7 | 2016-10-05 20:36:39 +0000 | [diff] [blame] | 19 | TEST(DenseSetTest, DoubleEntrySetTest) { |
Stuart Hastings | e54523d | 2009-05-01 20:47:53 +0000 | [diff] [blame] | 20 | llvm::DenseSet<unsigned> set(2); |
| 21 | set.insert(0); |
| 22 | set.insert(1); |
| 23 | // Original failure was an infinite loop in this call: |
David Blaikie | 7c8d139 | 2014-06-20 19:54:13 +0000 | [diff] [blame] | 24 | EXPECT_EQ(0u, set.count(2)); |
Stuart Hastings | e54523d | 2009-05-01 20:47:53 +0000 | [diff] [blame] | 25 | } |
| 26 | |
Lang Hames | b27a3b0 | 2014-10-19 19:36:33 +0000 | [diff] [blame] | 27 | struct TestDenseSetInfo { |
| 28 | static inline unsigned getEmptyKey() { return ~0; } |
| 29 | static inline unsigned getTombstoneKey() { return ~0U - 1; } |
| 30 | static unsigned getHashValue(const unsigned& Val) { return Val * 37U; } |
| 31 | static unsigned getHashValue(const char* Val) { |
| 32 | return (unsigned)(Val[0] - 'a') * 37U; |
| 33 | } |
| 34 | static bool isEqual(const unsigned& LHS, const unsigned& RHS) { |
| 35 | return LHS == RHS; |
| 36 | } |
| 37 | static bool isEqual(const char* LHS, const unsigned& RHS) { |
| 38 | return (unsigned)(LHS[0] - 'a') == RHS; |
| 39 | } |
| 40 | }; |
| 41 | |
Vitaly Buka | f12b1c7 | 2016-10-05 20:36:39 +0000 | [diff] [blame] | 42 | // Test fixture |
| 43 | template <typename T> class DenseSetTest : public testing::Test { |
| 44 | protected: |
| 45 | T Set = GetTestSet(); |
Lang Hames | b27a3b0 | 2014-10-19 19:36:33 +0000 | [diff] [blame] | 46 | |
Vitaly Buka | f12b1c7 | 2016-10-05 20:36:39 +0000 | [diff] [blame] | 47 | private: |
| 48 | static T GetTestSet() { |
| 49 | typename std::remove_const<T>::type Set; |
| 50 | Set.insert(0); |
| 51 | Set.insert(1); |
| 52 | Set.insert(2); |
| 53 | return Set; |
| 54 | } |
| 55 | }; |
| 56 | |
| 57 | // Register these types for testing. |
| 58 | typedef ::testing::Types<DenseSet<unsigned, TestDenseSetInfo>, |
Justin Lebar | 4bccb58 | 2016-10-17 22:24:28 +0000 | [diff] [blame] | 59 | const DenseSet<unsigned, TestDenseSetInfo>, |
| 60 | SmallDenseSet<unsigned, 1, TestDenseSetInfo>, |
| 61 | SmallDenseSet<unsigned, 4, TestDenseSetInfo>, |
| 62 | const SmallDenseSet<unsigned, 4, TestDenseSetInfo>, |
| 63 | SmallDenseSet<unsigned, 64, TestDenseSetInfo>> |
Vitaly Buka | f12b1c7 | 2016-10-05 20:36:39 +0000 | [diff] [blame] | 64 | DenseSetTestTypes; |
| 65 | TYPED_TEST_CASE(DenseSetTest, DenseSetTestTypes); |
| 66 | |
Justin Lebar | 61b9b6a | 2016-10-17 22:24:32 +0000 | [diff] [blame] | 67 | TYPED_TEST(DenseSetTest, InitializerList) { |
| 68 | TypeParam set({1, 2, 1, 4}); |
| 69 | EXPECT_EQ(3u, set.size()); |
| 70 | EXPECT_EQ(1u, set.count(1)); |
| 71 | EXPECT_EQ(1u, set.count(2)); |
| 72 | EXPECT_EQ(1u, set.count(4)); |
| 73 | EXPECT_EQ(0u, set.count(3)); |
| 74 | } |
| 75 | |
Dean Michael Berris | 6944037 | 2017-01-24 05:29:40 +0000 | [diff] [blame] | 76 | TYPED_TEST(DenseSetTest, ConstIteratorComparison) { |
Dean Michael Berris | 227c6eb | 2017-01-24 04:11:18 +0000 | [diff] [blame] | 77 | TypeParam set({1}); |
| 78 | const TypeParam &cset = set; |
| 79 | EXPECT_EQ(set.begin(), cset.begin()); |
| 80 | EXPECT_EQ(set.end(), cset.end()); |
| 81 | EXPECT_NE(set.end(), cset.begin()); |
| 82 | EXPECT_NE(set.begin(), cset.end()); |
| 83 | } |
| 84 | |
Dean Michael Berris | 6944037 | 2017-01-24 05:29:40 +0000 | [diff] [blame] | 85 | TYPED_TEST(DenseSetTest, DefaultConstruction) { |
| 86 | typename TypeParam::iterator I, J; |
| 87 | typename TypeParam::const_iterator CI, CJ; |
| 88 | EXPECT_EQ(I, J); |
| 89 | EXPECT_EQ(CI, CJ); |
| 90 | } |
| 91 | |
Justin Lebar | 61b9b6a | 2016-10-17 22:24:32 +0000 | [diff] [blame] | 92 | TYPED_TEST(DenseSetTest, EmptyInitializerList) { |
| 93 | TypeParam set({}); |
| 94 | EXPECT_EQ(0u, set.size()); |
| 95 | EXPECT_EQ(0u, set.count(0)); |
| 96 | } |
| 97 | |
Vitaly Buka | f12b1c7 | 2016-10-05 20:36:39 +0000 | [diff] [blame] | 98 | TYPED_TEST(DenseSetTest, FindAsTest) { |
| 99 | auto &set = this->Set; |
Lang Hames | b27a3b0 | 2014-10-19 19:36:33 +0000 | [diff] [blame] | 100 | // Size tests |
| 101 | EXPECT_EQ(3u, set.size()); |
| 102 | |
| 103 | // Normal lookup tests |
| 104 | EXPECT_EQ(1u, set.count(1)); |
| 105 | EXPECT_EQ(0u, *set.find(0)); |
| 106 | EXPECT_EQ(1u, *set.find(1)); |
| 107 | EXPECT_EQ(2u, *set.find(2)); |
| 108 | EXPECT_TRUE(set.find(3) == set.end()); |
| 109 | |
| 110 | // find_as() tests |
| 111 | EXPECT_EQ(0u, *set.find_as("a")); |
| 112 | EXPECT_EQ(1u, *set.find_as("b")); |
| 113 | EXPECT_EQ(2u, *set.find_as("c")); |
| 114 | EXPECT_TRUE(set.find_as("d") == set.end()); |
| 115 | } |
| 116 | |
Mehdi Amini | fa0f96b | 2016-08-13 20:42:19 +0000 | [diff] [blame] | 117 | // Simple class that counts how many moves and copy happens when growing a map |
| 118 | struct CountCopyAndMove { |
| 119 | static int Move; |
| 120 | static int Copy; |
| 121 | int Value; |
| 122 | CountCopyAndMove(int Value) : Value(Value) {} |
| 123 | |
| 124 | CountCopyAndMove(const CountCopyAndMove &RHS) { |
| 125 | Value = RHS.Value; |
| 126 | Copy++; |
| 127 | } |
| 128 | CountCopyAndMove &operator=(const CountCopyAndMove &RHS) { |
| 129 | Value = RHS.Value; |
| 130 | Copy++; |
| 131 | return *this; |
| 132 | } |
| 133 | CountCopyAndMove(CountCopyAndMove &&RHS) { |
| 134 | Value = RHS.Value; |
| 135 | Move++; |
| 136 | } |
| 137 | CountCopyAndMove &operator=(const CountCopyAndMove &&RHS) { |
| 138 | Value = RHS.Value; |
| 139 | Move++; |
| 140 | return *this; |
| 141 | } |
| 142 | }; |
| 143 | int CountCopyAndMove::Copy = 0; |
| 144 | int CountCopyAndMove::Move = 0; |
| 145 | } // anonymous namespace |
| 146 | |
| 147 | namespace llvm { |
| 148 | // Specialization required to insert a CountCopyAndMove into a DenseSet. |
| 149 | template <> struct DenseMapInfo<CountCopyAndMove> { |
| 150 | static inline CountCopyAndMove getEmptyKey() { return CountCopyAndMove(-1); }; |
| 151 | static inline CountCopyAndMove getTombstoneKey() { |
| 152 | return CountCopyAndMove(-2); |
| 153 | }; |
| 154 | static unsigned getHashValue(const CountCopyAndMove &Val) { |
| 155 | return Val.Value; |
| 156 | } |
| 157 | static bool isEqual(const CountCopyAndMove &LHS, |
| 158 | const CountCopyAndMove &RHS) { |
| 159 | return LHS.Value == RHS.Value; |
| 160 | } |
| 161 | }; |
| 162 | } |
| 163 | |
| 164 | namespace { |
| 165 | // Make sure reserve actually gives us enough buckets to insert N items |
| 166 | // without increasing allocation size. |
| 167 | TEST(DenseSetCustomTest, ReserveTest) { |
| 168 | // Test a few different size, 48 is *not* a random choice: we need a value |
| 169 | // that is 2/3 of a power of two to stress the grow() condition, and the power |
| 170 | // of two has to be at least 64 because of minimum size allocation in the |
| 171 | // DenseMa. 66 is a value just above the 64 default init. |
| 172 | for (auto Size : {1, 2, 48, 66}) { |
| 173 | DenseSet<CountCopyAndMove> Set; |
| 174 | Set.reserve(Size); |
| 175 | unsigned MemorySize = Set.getMemorySize(); |
| 176 | CountCopyAndMove::Copy = 0; |
| 177 | CountCopyAndMove::Move = 0; |
| 178 | for (int i = 0; i < Size; ++i) |
| 179 | Set.insert(CountCopyAndMove(i)); |
| 180 | // Check that we didn't grow |
| 181 | EXPECT_EQ(MemorySize, Set.getMemorySize()); |
| 182 | // Check that move was called the expected number of times |
| 183 | EXPECT_EQ(Size, CountCopyAndMove::Move); |
| 184 | // Check that no copy occured |
| 185 | EXPECT_EQ(0, CountCopyAndMove::Copy); |
| 186 | } |
| 187 | } |
Daniel Berlin | 04d9e74 | 2017-03-10 00:25:26 +0000 | [diff] [blame] | 188 | TEST(DenseSetCustomTest, ConstTest) { |
| 189 | // Test that const pointers work okay for count and find, even when the |
| 190 | // underlying map is a non-const pointer. |
| 191 | DenseSet<int *> Map; |
| 192 | int A; |
| 193 | int *B = &A; |
| 194 | const int *C = &A; |
| 195 | Map.insert(B); |
| 196 | EXPECT_EQ(Map.count(B), 1u); |
| 197 | EXPECT_EQ(Map.count(C), 1u); |
| 198 | EXPECT_NE(Map.find(B), Map.end()); |
| 199 | EXPECT_NE(Map.find(C), Map.end()); |
| 200 | } |
Stuart Hastings | e54523d | 2009-05-01 20:47:53 +0000 | [diff] [blame] | 201 | } |