| 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 flat_hash_set |
| 17 |
#define THIS_TEST_NAME FlatHashSet |
| 18 |
#endif |
| 19 |
|
| 20 |
#include "parallel_hashmap/phmap.h" |
| 21 |
|
| 22 |
#include <vector> |
| 23 |
|
| 24 |
#include "hash_generator_testing.h" |
| 25 |
#include "unordered_set_constructor_test.h" |
| 26 |
#include "unordered_set_lookup_test.h" |
| 27 |
#include "unordered_set_members_test.h" |
| 28 |
#include "unordered_set_modifiers_test.h" |
| 29 |
|
| 30 |
namespace phmap { |
| 31 |
namespace priv { |
| 32 |
namespace { |
| 33 |
|
| 34 |
using ::phmap::priv::hash_internal::Enum; |
| 35 |
using ::phmap::priv::hash_internal::EnumClass; |
| 36 |
using ::testing::Pointee; |
| 37 |
using ::testing::UnorderedElementsAre; |
| 38 |
using ::testing::UnorderedElementsAreArray; |
| 39 |
|
| 40 |
template <class T> |
| 41 |
using Set = |
| 42 |
phmap::THIS_HASH_SET<T, StatefulTestingHash, StatefulTestingEqual, Alloc<T>>; |
| 43 |
|
| 44 |
using SetTypes = |
| 45 |
::testing::Types<Set<int>, Set<std::string>, Set<Enum>, Set<EnumClass>>; |
| 46 |
|
| 47 |
INSTANTIATE_TYPED_TEST_SUITE_P(THIS_TEST_NAME, ConstructorTest, SetTypes); |
| 48 |
INSTANTIATE_TYPED_TEST_SUITE_P(THIS_TEST_NAME, LookupTest, SetTypes); |
| 49 |
INSTANTIATE_TYPED_TEST_SUITE_P(THIS_TEST_NAME, MembersTest, SetTypes); |
| 50 |
INSTANTIATE_TYPED_TEST_SUITE_P(THIS_TEST_NAME, ModifiersTest, SetTypes); |
| 51 |
|
| 52 |
#if PHMAP_HAVE_STD_STRING_VIEW |
| 53 |
TEST(THIS_TEST_NAME, EmplaceString) { |
| 54 |
std::vector<std::string> v = {"a", "b"}; |
| 55 |
phmap::THIS_HASH_SET<std::string_view> hs(v.begin(), v.end()); |
| 56 |
//EXPECT_THAT(hs, UnorderedElementsAreArray(v)); |
| 57 |
} |
| 58 |
#endif |
| 59 |
|
| 60 |
TEST(THIS_TEST_NAME, BitfieldArgument) { |
| 61 |
union { |
| 62 |
int n : 1; |
| 63 |
}; |
| 64 |
n = 0; |
| 65 |
phmap::THIS_HASH_SET<int> s = {n}; |
| 66 |
s.insert(n); |
| 67 |
s.insert(s.end(), n); |
| 68 |
s.insert({n}); |
| 69 |
s.erase(n); |
| 70 |
s.count(n); |
| 71 |
s.prefetch(n); |
| 72 |
s.find(n); |
| 73 |
s.contains(n); |
| 74 |
s.equal_range(n); |
| 75 |
} |
| 76 |
|
| 77 |
TEST(THIS_TEST_NAME, MergeExtractInsert) { |
| 78 |
struct Hash { |
| 79 |
size_t operator()(const std::unique_ptr<int>& p) const { return *p; } |
| 80 |
}; |
| 81 |
struct Eq { |
| 82 |
bool operator()(const std::unique_ptr<int>& a, |
| 83 |
const std::unique_ptr<int>& b) const { |
| 84 |
return *a == *b; |
| 85 |
} |
| 86 |
}; |
| 87 |
phmap::THIS_HASH_SET<std::unique_ptr<int>, Hash, Eq> set1, set2; |
| 88 |
set1.insert(phmap::make_unique<int>(7)); |
| 89 |
set1.insert(phmap::make_unique<int>(17)); |
| 90 |
|
| 91 |
set2.insert(phmap::make_unique<int>(7)); |
| 92 |
set2.insert(phmap::make_unique<int>(19)); |
| 93 |
|
| 94 |
EXPECT_THAT(set1, UnorderedElementsAre(Pointee(7), Pointee(17))); |
| 95 |
EXPECT_THAT(set2, UnorderedElementsAre(Pointee(7), Pointee(19))); |
| 96 |
|
| 97 |
set1.merge(set2); |
| 98 |
|
| 99 |
EXPECT_THAT(set1, UnorderedElementsAre(Pointee(7), Pointee(17), Pointee(19))); |
| 100 |
EXPECT_THAT(set2, UnorderedElementsAre(Pointee(7))); |
| 101 |
|
| 102 |
auto node = set1.extract(phmap::make_unique<int>(7)); |
| 103 |
EXPECT_TRUE(node); |
| 104 |
EXPECT_THAT(node.value(), Pointee(7)); |
| 105 |
EXPECT_THAT(set1, UnorderedElementsAre(Pointee(17), Pointee(19))); |
| 106 |
|
| 107 |
auto insert_result = set2.insert(std::move(node)); |
| 108 |
EXPECT_FALSE(node); |
| 109 |
EXPECT_FALSE(insert_result.inserted); |
| 110 |
EXPECT_TRUE(insert_result.node); |
| 111 |
EXPECT_THAT(insert_result.node.value(), Pointee(7)); |
| 112 |
EXPECT_EQ(**insert_result.position, 7); |
| 113 |
EXPECT_NE(insert_result.position->get(), insert_result.node.value().get()); |
| 114 |
EXPECT_THAT(set2, UnorderedElementsAre(Pointee(7))); |
| 115 |
|
| 116 |
node = set1.extract(phmap::make_unique<int>(17)); |
| 117 |
EXPECT_TRUE(node); |
| 118 |
EXPECT_THAT(node.value(), Pointee(17)); |
| 119 |
EXPECT_THAT(set1, UnorderedElementsAre(Pointee(19))); |
| 120 |
|
| 121 |
node.value() = phmap::make_unique<int>(23); |
| 122 |
|
| 123 |
insert_result = set2.insert(std::move(node)); |
| 124 |
EXPECT_FALSE(node); |
| 125 |
EXPECT_TRUE(insert_result.inserted); |
| 126 |
EXPECT_FALSE(insert_result.node); |
| 127 |
EXPECT_EQ(**insert_result.position, 23); |
| 128 |
EXPECT_THAT(set2, UnorderedElementsAre(Pointee(7), Pointee(23))); |
| 129 |
} |
| 130 |
|
| 131 |
} // namespace |
| 132 |
} // namespace priv |
| 133 |
} // namespace phmap |