18 #include <type_traits> 21 #include "gtest/gtest.h" 25 namespace container_internal {
28 using ::testing::Types;
31 hash_default_eq<int32_t>
eq;
32 EXPECT_TRUE(
eq(1, 1u));
33 EXPECT_TRUE(
eq(1,
char{1}));
34 EXPECT_TRUE(
eq(1,
true));
35 EXPECT_TRUE(
eq(1,
double{1.1}));
36 EXPECT_FALSE(
eq(1,
char{2}));
37 EXPECT_FALSE(
eq(1, 2u));
38 EXPECT_FALSE(
eq(1,
false));
39 EXPECT_FALSE(
eq(1, 2.));
43 hash_default_hash<int32_t>
hash;
45 EXPECT_EQ(h,
hash(1u));
46 EXPECT_EQ(h,
hash(
char{1}));
47 EXPECT_EQ(h,
hash(
true));
48 EXPECT_EQ(h,
hash(
double{1.1}));
49 EXPECT_NE(h,
hash(2u));
50 EXPECT_NE(h,
hash(
char{2}));
51 EXPECT_NE(h,
hash(
false));
52 EXPECT_NE(h,
hash(2.));
59 EXPECT_TRUE(
eq(MyEnum::A, MyEnum::A));
60 EXPECT_FALSE(
eq(MyEnum::A, MyEnum::B));
69 EXPECT_NE(h,
hash(MyEnum::D));
73 using StringTypes = ::testing::Types<std::string, absl::string_view>;
76 struct EqString : ::testing::Test {
80 TYPED_TEST_SUITE(EqString, StringTypes);
83 struct HashString : ::testing::Test {
87 TYPED_TEST_SUITE(HashString, StringTypes);
89 TYPED_TEST(EqString, Works) {
91 EXPECT_TRUE(
eq(
"a",
"a"));
93 EXPECT_TRUE(
eq(
"a", std::string(
"a")));
94 EXPECT_FALSE(
eq(
"a",
"b"));
96 EXPECT_FALSE(
eq(
"a", std::string(
"b")));
99 TYPED_TEST(HashString, Works) {
103 EXPECT_EQ(h,
hash(std::string(
"a")));
105 EXPECT_NE(h,
hash(std::string(
"b")));
110 void operator()(
const T*
ptr)
const {}
114 ::testing::Types<const int*, int*, std::unique_ptr<const int>,
115 std::unique_ptr<const int, NoDeleter>,
116 std::unique_ptr<int>, std::unique_ptr<int, NoDeleter>,
117 std::shared_ptr<const int>, std::shared_ptr<int>>;
120 struct EqPointer : ::testing::Test {
121 hash_default_eq<T>
key_eq;
124 TYPED_TEST_SUITE(EqPointer, PointerTypes);
127 struct HashPointer : ::testing::Test {
128 hash_default_hash<T>
hasher;
131 TYPED_TEST_SUITE(HashPointer, PointerTypes);
133 TYPED_TEST(EqPointer, Works) {
136 auto sptr = std::make_shared<int>();
137 std::shared_ptr<const int> csptr = sptr;
138 int*
ptr = sptr.get();
139 const int* cptr =
ptr;
140 std::unique_ptr<int, NoDeleter> uptr(ptr);
141 std::unique_ptr<const int, NoDeleter> cuptr(ptr);
143 EXPECT_TRUE(
eq(ptr, cptr));
144 EXPECT_TRUE(
eq(ptr, sptr));
145 EXPECT_TRUE(
eq(ptr, uptr));
146 EXPECT_TRUE(
eq(ptr, csptr));
147 EXPECT_TRUE(
eq(ptr, cuptr));
148 EXPECT_FALSE(
eq(&dummy, cptr));
149 EXPECT_FALSE(
eq(&dummy, sptr));
150 EXPECT_FALSE(
eq(&dummy, uptr));
151 EXPECT_FALSE(
eq(&dummy, csptr));
152 EXPECT_FALSE(
eq(&dummy, cuptr));
157 struct Derived : Base {};
159 hash_default_hash<Base*>
hasher;
164 EXPECT_EQ(
hasher(static_cast<Base*>(&derived)),
hasher(&derived));
166 auto dp = std::make_shared<Derived>();
167 EXPECT_EQ(
hasher(static_cast<Base*>(dp.get())),
hasher(dp));
171 using Func = int (*)();
172 hash_default_hash<Func>
hasher;
173 hash_default_eq<Func>
eq;
175 Func p1 = [] {
return 1; }, p2 = [] {
return 2; };
177 EXPECT_TRUE(
eq(p1, p1));
180 EXPECT_FALSE(
eq(p1, p2));
183 TYPED_TEST(HashPointer, Works) {
186 auto sptr = std::make_shared<int>();
187 std::shared_ptr<const int> csptr = sptr;
188 int* ptr = sptr.get();
189 const int* cptr =
ptr;
190 std::unique_ptr<int, NoDeleter> uptr(ptr);
191 std::unique_ptr<const int, NoDeleter> cuptr(ptr);
198 EXPECT_NE(
hash(&dummy),
hash(cptr));
199 EXPECT_NE(
hash(&dummy),
hash(sptr));
200 EXPECT_NE(
hash(&dummy),
hash(uptr));
201 EXPECT_NE(
hash(&dummy),
hash(csptr));
202 EXPECT_NE(
hash(&dummy),
hash(cuptr));
207 using StringTypesCartesianProduct = Types<
210 std::pair<absl::string_view, std::string>,
211 std::pair<absl::string_view, absl::string_view>,
212 std::pair<absl::string_view, const char*>>;
215 constexpr
char kFirstString[] =
"abc123";
216 constexpr
char kSecondString[] =
"ijk456";
218 template <
typename T>
219 struct StringLikeTest :
public ::testing::Test {
220 typename T::first_type
a1{kFirstString};
221 typename T::second_type
b1{kFirstString};
222 typename T::first_type
a2{kSecondString};
223 typename T::second_type
b2{kSecondString};
224 hash_default_eq<typename T::first_type>
eq;
225 hash_default_hash<typename T::first_type>
hash;
228 TYPED_TEST_CASE_P(StringLikeTest);
231 EXPECT_TRUE(this->
eq(this->
a1, this->
b1));
232 EXPECT_TRUE(this->
eq(this->
b1, this->
a1));
236 EXPECT_FALSE(this->
eq(this->
a1, this->
b2));
237 EXPECT_FALSE(this->
eq(this->
b2, this->
a1));
247 TYPED_TEST_SUITE(StringLikeTest, StringTypesCartesianProduct);
272 template <
class E = Hashable<H>,
273 class =
typename std::enable_if<E::HashableBy(kStd)>::type>
281 namespace container_internal {
285 size_t Hash(
const T&
v) {
286 return hash_default_hash<T>()(v);
289 TEST(Delegate, HashDispatch) {
hash_default_eq< T > key_eq
TEST(NotificationTest, SanityTest)
typename container_internal::HashEq< T >::Hash hash_default_hash
hash_default_hash< T > hasher
hash_default_hash< typename T::first_type > hash
TYPED_TEST_P(ConstructorTest, NoArgs)
typename container_internal::HashEq< T >::Eq hash_default_eq
size_t operator()(E) const
hash_default_eq< typename T::first_type > eq
absl::hash_internal::Hash< T > Hash
static constexpr bool HashableBy(Hash h)