2009-05-02 04:47:53 +08:00
|
|
|
//===- llvm/unittest/ADT/DenseSetTest.cpp - DenseSet unit tests --*- C++ -*-===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2009-05-02 04:47:53 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-09-16 02:45:38 +08:00
|
|
|
#include "llvm/ADT/DenseSet.h"
|
2016-10-06 04:36:39 +08:00
|
|
|
#include "gtest/gtest.h"
|
|
|
|
#include <type_traits>
|
2009-05-02 04:47:53 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2018-09-21 07:11:27 +08:00
|
|
|
static_assert(std::is_const<std::remove_pointer<
|
|
|
|
DenseSet<int>::const_iterator::pointer>::type>::value,
|
|
|
|
"Iterator pointer type should be const");
|
|
|
|
static_assert(std::is_const<std::remove_reference<
|
|
|
|
DenseSet<int>::const_iterator::reference>::type>::value,
|
|
|
|
"Iterator reference type should be const");
|
|
|
|
|
2009-05-02 04:47:53 +08:00
|
|
|
// Test hashing with a set of only two entries.
|
2016-10-06 04:36:39 +08:00
|
|
|
TEST(DenseSetTest, DoubleEntrySetTest) {
|
2009-05-02 04:47:53 +08:00
|
|
|
llvm::DenseSet<unsigned> set(2);
|
|
|
|
set.insert(0);
|
|
|
|
set.insert(1);
|
|
|
|
// Original failure was an infinite loop in this call:
|
2014-06-21 03:54:13 +08:00
|
|
|
EXPECT_EQ(0u, set.count(2));
|
2009-05-02 04:47:53 +08:00
|
|
|
}
|
|
|
|
|
2014-10-20 03:36:33 +08:00
|
|
|
struct TestDenseSetInfo {
|
|
|
|
static inline unsigned getEmptyKey() { return ~0; }
|
|
|
|
static inline unsigned getTombstoneKey() { return ~0U - 1; }
|
|
|
|
static unsigned getHashValue(const unsigned& Val) { return Val * 37U; }
|
|
|
|
static unsigned getHashValue(const char* Val) {
|
|
|
|
return (unsigned)(Val[0] - 'a') * 37U;
|
|
|
|
}
|
|
|
|
static bool isEqual(const unsigned& LHS, const unsigned& RHS) {
|
|
|
|
return LHS == RHS;
|
|
|
|
}
|
|
|
|
static bool isEqual(const char* LHS, const unsigned& RHS) {
|
|
|
|
return (unsigned)(LHS[0] - 'a') == RHS;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-10-06 04:36:39 +08:00
|
|
|
// Test fixture
|
|
|
|
template <typename T> class DenseSetTest : public testing::Test {
|
|
|
|
protected:
|
|
|
|
T Set = GetTestSet();
|
|
|
|
|
|
|
|
private:
|
|
|
|
static T GetTestSet() {
|
Use std::foo_t rather than std::foo in LLVM.
Summary: C++14 migration. No functional change.
Reviewers: bkramer, JDevlieghere, lebedev.ri
Subscribers: MatzeB, hiraditya, jkorous, dexonsmith, arphaman, kadircet, lebedev.ri, usaxena95, cfe-commits, llvm-commits
Tags: #clang, #llvm
Differential Revision: https://reviews.llvm.org/D74384
2020-02-11 12:33:08 +08:00
|
|
|
std::remove_const_t<T> Set;
|
2016-10-06 04:36:39 +08:00
|
|
|
Set.insert(0);
|
|
|
|
Set.insert(1);
|
|
|
|
Set.insert(2);
|
|
|
|
return Set;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Register these types for testing.
|
|
|
|
typedef ::testing::Types<DenseSet<unsigned, TestDenseSetInfo>,
|
2016-10-18 06:24:28 +08:00
|
|
|
const DenseSet<unsigned, TestDenseSetInfo>,
|
|
|
|
SmallDenseSet<unsigned, 1, TestDenseSetInfo>,
|
|
|
|
SmallDenseSet<unsigned, 4, TestDenseSetInfo>,
|
|
|
|
const SmallDenseSet<unsigned, 4, TestDenseSetInfo>,
|
|
|
|
SmallDenseSet<unsigned, 64, TestDenseSetInfo>>
|
2016-10-06 04:36:39 +08:00
|
|
|
DenseSetTestTypes;
|
2021-05-17 20:12:11 +08:00
|
|
|
TYPED_TEST_SUITE(DenseSetTest, DenseSetTestTypes, );
|
2014-10-20 03:36:33 +08:00
|
|
|
|
2020-05-02 00:57:18 +08:00
|
|
|
TYPED_TEST(DenseSetTest, Constructor) {
|
|
|
|
constexpr unsigned a[] = {1, 2, 4};
|
|
|
|
TypeParam set(std::begin(a), std::end(a));
|
|
|
|
EXPECT_EQ(3u, set.size());
|
|
|
|
EXPECT_EQ(1u, set.count(1));
|
|
|
|
EXPECT_EQ(1u, set.count(2));
|
|
|
|
EXPECT_EQ(1u, set.count(4));
|
|
|
|
}
|
|
|
|
|
2016-10-18 06:24:32 +08:00
|
|
|
TYPED_TEST(DenseSetTest, InitializerList) {
|
|
|
|
TypeParam set({1, 2, 1, 4});
|
|
|
|
EXPECT_EQ(3u, set.size());
|
|
|
|
EXPECT_EQ(1u, set.count(1));
|
|
|
|
EXPECT_EQ(1u, set.count(2));
|
|
|
|
EXPECT_EQ(1u, set.count(4));
|
|
|
|
EXPECT_EQ(0u, set.count(3));
|
|
|
|
}
|
|
|
|
|
2018-10-16 02:34:36 +08:00
|
|
|
TYPED_TEST(DenseSetTest, InitializerListWithNonPowerOfTwoLength) {
|
|
|
|
TypeParam set({1, 2, 3});
|
|
|
|
EXPECT_EQ(3u, set.size());
|
|
|
|
EXPECT_EQ(1u, set.count(1));
|
|
|
|
EXPECT_EQ(1u, set.count(2));
|
|
|
|
EXPECT_EQ(1u, set.count(3));
|
|
|
|
}
|
|
|
|
|
2017-01-24 13:29:40 +08:00
|
|
|
TYPED_TEST(DenseSetTest, ConstIteratorComparison) {
|
2017-01-24 12:11:18 +08:00
|
|
|
TypeParam set({1});
|
|
|
|
const TypeParam &cset = set;
|
|
|
|
EXPECT_EQ(set.begin(), cset.begin());
|
|
|
|
EXPECT_EQ(set.end(), cset.end());
|
|
|
|
EXPECT_NE(set.end(), cset.begin());
|
|
|
|
EXPECT_NE(set.begin(), cset.end());
|
|
|
|
}
|
|
|
|
|
2017-01-24 13:29:40 +08:00
|
|
|
TYPED_TEST(DenseSetTest, DefaultConstruction) {
|
|
|
|
typename TypeParam::iterator I, J;
|
|
|
|
typename TypeParam::const_iterator CI, CJ;
|
|
|
|
EXPECT_EQ(I, J);
|
|
|
|
EXPECT_EQ(CI, CJ);
|
|
|
|
}
|
|
|
|
|
2016-10-18 06:24:32 +08:00
|
|
|
TYPED_TEST(DenseSetTest, EmptyInitializerList) {
|
|
|
|
TypeParam set({});
|
|
|
|
EXPECT_EQ(0u, set.size());
|
|
|
|
EXPECT_EQ(0u, set.count(0));
|
|
|
|
}
|
|
|
|
|
2016-10-06 04:36:39 +08:00
|
|
|
TYPED_TEST(DenseSetTest, FindAsTest) {
|
|
|
|
auto &set = this->Set;
|
2014-10-20 03:36:33 +08:00
|
|
|
// Size tests
|
|
|
|
EXPECT_EQ(3u, set.size());
|
|
|
|
|
|
|
|
// Normal lookup tests
|
|
|
|
EXPECT_EQ(1u, set.count(1));
|
|
|
|
EXPECT_EQ(0u, *set.find(0));
|
|
|
|
EXPECT_EQ(1u, *set.find(1));
|
|
|
|
EXPECT_EQ(2u, *set.find(2));
|
|
|
|
EXPECT_TRUE(set.find(3) == set.end());
|
|
|
|
|
|
|
|
// find_as() tests
|
|
|
|
EXPECT_EQ(0u, *set.find_as("a"));
|
|
|
|
EXPECT_EQ(1u, *set.find_as("b"));
|
|
|
|
EXPECT_EQ(2u, *set.find_as("c"));
|
|
|
|
EXPECT_TRUE(set.find_as("d") == set.end());
|
|
|
|
}
|
|
|
|
|
2018-10-15 23:26:47 +08:00
|
|
|
TYPED_TEST(DenseSetTest, EqualityComparisonTest) {
|
|
|
|
TypeParam set1({1, 2, 3, 4});
|
|
|
|
TypeParam set2({4, 3, 2, 1});
|
|
|
|
TypeParam set3({2, 3, 4, 5});
|
|
|
|
|
|
|
|
EXPECT_EQ(set1, set2);
|
|
|
|
EXPECT_NE(set1, set3);
|
|
|
|
}
|
|
|
|
|
2016-08-14 04:42:19 +08:00
|
|
|
// Simple class that counts how many moves and copy happens when growing a map
|
|
|
|
struct CountCopyAndMove {
|
|
|
|
static int Move;
|
|
|
|
static int Copy;
|
|
|
|
int Value;
|
|
|
|
CountCopyAndMove(int Value) : Value(Value) {}
|
|
|
|
|
|
|
|
CountCopyAndMove(const CountCopyAndMove &RHS) {
|
|
|
|
Value = RHS.Value;
|
|
|
|
Copy++;
|
|
|
|
}
|
|
|
|
CountCopyAndMove &operator=(const CountCopyAndMove &RHS) {
|
|
|
|
Value = RHS.Value;
|
|
|
|
Copy++;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
CountCopyAndMove(CountCopyAndMove &&RHS) {
|
|
|
|
Value = RHS.Value;
|
|
|
|
Move++;
|
|
|
|
}
|
|
|
|
CountCopyAndMove &operator=(const CountCopyAndMove &&RHS) {
|
|
|
|
Value = RHS.Value;
|
|
|
|
Move++;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
int CountCopyAndMove::Copy = 0;
|
|
|
|
int CountCopyAndMove::Move = 0;
|
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
// Specialization required to insert a CountCopyAndMove into a DenseSet.
|
|
|
|
template <> struct DenseMapInfo<CountCopyAndMove> {
|
|
|
|
static inline CountCopyAndMove getEmptyKey() { return CountCopyAndMove(-1); };
|
|
|
|
static inline CountCopyAndMove getTombstoneKey() {
|
|
|
|
return CountCopyAndMove(-2);
|
|
|
|
};
|
|
|
|
static unsigned getHashValue(const CountCopyAndMove &Val) {
|
|
|
|
return Val.Value;
|
|
|
|
}
|
|
|
|
static bool isEqual(const CountCopyAndMove &LHS,
|
|
|
|
const CountCopyAndMove &RHS) {
|
|
|
|
return LHS.Value == RHS.Value;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
// Make sure reserve actually gives us enough buckets to insert N items
|
|
|
|
// without increasing allocation size.
|
|
|
|
TEST(DenseSetCustomTest, ReserveTest) {
|
|
|
|
// Test a few different size, 48 is *not* a random choice: we need a value
|
|
|
|
// that is 2/3 of a power of two to stress the grow() condition, and the power
|
|
|
|
// of two has to be at least 64 because of minimum size allocation in the
|
|
|
|
// DenseMa. 66 is a value just above the 64 default init.
|
|
|
|
for (auto Size : {1, 2, 48, 66}) {
|
|
|
|
DenseSet<CountCopyAndMove> Set;
|
|
|
|
Set.reserve(Size);
|
|
|
|
unsigned MemorySize = Set.getMemorySize();
|
|
|
|
CountCopyAndMove::Copy = 0;
|
|
|
|
CountCopyAndMove::Move = 0;
|
|
|
|
for (int i = 0; i < Size; ++i)
|
|
|
|
Set.insert(CountCopyAndMove(i));
|
|
|
|
// Check that we didn't grow
|
|
|
|
EXPECT_EQ(MemorySize, Set.getMemorySize());
|
|
|
|
// Check that move was called the expected number of times
|
|
|
|
EXPECT_EQ(Size, CountCopyAndMove::Move);
|
2018-01-24 18:33:39 +08:00
|
|
|
// Check that no copy occurred
|
2016-08-14 04:42:19 +08:00
|
|
|
EXPECT_EQ(0, CountCopyAndMove::Copy);
|
|
|
|
}
|
|
|
|
}
|
2017-03-10 08:25:26 +08:00
|
|
|
TEST(DenseSetCustomTest, ConstTest) {
|
|
|
|
// Test that const pointers work okay for count and find, even when the
|
|
|
|
// underlying map is a non-const pointer.
|
|
|
|
DenseSet<int *> Map;
|
|
|
|
int A;
|
|
|
|
int *B = &A;
|
|
|
|
const int *C = &A;
|
|
|
|
Map.insert(B);
|
|
|
|
EXPECT_EQ(Map.count(B), 1u);
|
|
|
|
EXPECT_EQ(Map.count(C), 1u);
|
2020-07-18 01:40:42 +08:00
|
|
|
EXPECT_TRUE(Map.contains(B));
|
|
|
|
EXPECT_TRUE(Map.contains(C));
|
2017-03-10 08:25:26 +08:00
|
|
|
}
|
2009-05-02 04:47:53 +08:00
|
|
|
}
|