| 1 |
// Copyright 2018 The Abseil Authors. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
#ifndef THIS_HASH_SET |
| 16 |
#define THIS_HASH_SET node_hash_set |
| 17 |
#define THIS_TEST_NAME NodeHashSet |
| 18 |
#endif |
| 19 |
|
| 20 |
#include "parallel_hashmap/phmap.h" |
| 21 |
|
| 22 |
#include "unordered_set_constructor_test.h" |
| 23 |
#include "unordered_set_lookup_test.h" |
| 24 |
#include "unordered_set_members_test.h" |
| 25 |
#include "unordered_set_modifiers_test.h" |
| 26 |
|
| 27 |
namespace phmap { |
| 28 |
namespace priv { |
| 29 |
namespace { |
| 30 |
using ::phmap::priv::hash_internal::Enum; |
| 31 |
using ::phmap::priv::hash_internal::EnumClass; |
| 32 |
using ::testing::Pointee; |
| 33 |
using ::testing::UnorderedElementsAre; |
| 34 |
|
| 35 |
using SetTypes = ::testing::Types< |
| 36 |
THIS_HASH_SET<int, StatefulTestingHash, StatefulTestingEqual, Alloc<int>>, |
| 37 |
THIS_HASH_SET<std::string, StatefulTestingHash, StatefulTestingEqual, |
| 38 |
Alloc<std::string>>, |
| 39 |
THIS_HASH_SET<Enum, StatefulTestingHash, StatefulTestingEqual, Alloc<Enum>>, |
| 40 |
THIS_HASH_SET<EnumClass, StatefulTestingHash, StatefulTestingEqual, |
| 41 |
Alloc<EnumClass>>>; |
| 42 |
|
| 43 |
INSTANTIATE_TYPED_TEST_SUITE_P(THIS_TEST_NAME, ConstructorTest, SetTypes); |
| 44 |
INSTANTIATE_TYPED_TEST_SUITE_P(THIS_TEST_NAME, LookupTest, SetTypes); |
| 45 |
INSTANTIATE_TYPED_TEST_SUITE_P(THIS_TEST_NAME, MembersTest, SetTypes); |
| 46 |
INSTANTIATE_TYPED_TEST_SUITE_P(THIS_TEST_NAME, ModifiersTest, SetTypes); |
| 47 |
|
| 48 |
TEST(THIS_TEST_NAME, MoveableNotCopyableCompiles) { |
| 49 |
THIS_HASH_SET<std::unique_ptr<void*>> t; |
| 50 |
THIS_HASH_SET<std::unique_ptr<void*>> u; |
| 51 |
u = std::move(t); |
| 52 |
} |
| 53 |
|
| 54 |
TEST(THIS_TEST_NAME, MergeExtractInsert) { |
| 55 |
struct Hash { |
| 56 |
size_t operator()(const std::unique_ptr<int>& p) const { return *p; } |
| 57 |
}; |
| 58 |
struct Eq { |
| 59 |
bool operator()(const std::unique_ptr<int>& a, |
| 60 |
const std::unique_ptr<int>& b) const { |
| 61 |
return *a == *b; |
| 62 |
} |
| 63 |
}; |
| 64 |
phmap::THIS_HASH_SET<std::unique_ptr<int>, Hash, Eq> set1, set2; |
| 65 |
set1.insert(phmap::make_unique<int>(7)); |
| 66 |
set1.insert(phmap::make_unique<int>(17)); |
| 67 |
|
| 68 |
set2.insert(phmap::make_unique<int>(7)); |
| 69 |
set2.insert(phmap::make_unique<int>(19)); |
| 70 |
|
| 71 |
EXPECT_THAT(set1, UnorderedElementsAre(Pointee(7), Pointee(17))); |
| 72 |
EXPECT_THAT(set2, UnorderedElementsAre(Pointee(7), Pointee(19))); |
| 73 |
|
| 74 |
set1.merge(set2); |
| 75 |
|
| 76 |
EXPECT_THAT(set1, UnorderedElementsAre(Pointee(7), Pointee(17), Pointee(19))); |
| 77 |
EXPECT_THAT(set2, UnorderedElementsAre(Pointee(7))); |
| 78 |
|
| 79 |
auto node = set1.extract(phmap::make_unique<int>(7)); |
| 80 |
EXPECT_TRUE(node); |
| 81 |
EXPECT_THAT(node.value(), Pointee(7)); |
| 82 |
EXPECT_THAT(set1, UnorderedElementsAre(Pointee(17), Pointee(19))); |
| 83 |
|
| 84 |
auto insert_result = set2.insert(std::move(node)); |
| 85 |
EXPECT_FALSE(node); |
| 86 |
EXPECT_FALSE(insert_result.inserted); |
| 87 |
EXPECT_TRUE(insert_result.node); |
| 88 |
EXPECT_THAT(insert_result.node.value(), Pointee(7)); |
| 89 |
EXPECT_EQ(**insert_result.position, 7); |
| 90 |
EXPECT_NE(insert_result.position->get(), insert_result.node.value().get()); |
| 91 |
EXPECT_THAT(set2, UnorderedElementsAre(Pointee(7))); |
| 92 |
|
| 93 |
node = set1.extract(phmap::make_unique<int>(17)); |
| 94 |
EXPECT_TRUE(node); |
| 95 |
EXPECT_THAT(node.value(), Pointee(17)); |
| 96 |
EXPECT_THAT(set1, UnorderedElementsAre(Pointee(19))); |
| 97 |
|
| 98 |
node.value() = phmap::make_unique<int>(23); |
| 99 |
|
| 100 |
insert_result = set2.insert(std::move(node)); |
| 101 |
EXPECT_FALSE(node); |
| 102 |
EXPECT_TRUE(insert_result.inserted); |
| 103 |
EXPECT_FALSE(insert_result.node); |
| 104 |
EXPECT_EQ(**insert_result.position, 23); |
| 105 |
EXPECT_THAT(set2, UnorderedElementsAre(Pointee(7), Pointee(23))); |
| 106 |
} |
| 107 |
|
| 108 |
} // namespace |
| 109 |
} // namespace priv |
| 110 |
} // namespace phmap |