From dcd54dcda7bc83592ad40d9f74bea34b23a5f866 Mon Sep 17 00:00:00 2001 From: greg Date: Sun, 24 Mar 2019 19:27:21 -0400 Subject: [PATCH] use macros to avoid duplicating the tests --- tests/flat_hash_map_test.cc | 47 ++--- tests/flat_hash_set_test.cc | 27 +-- tests/node_hash_map_test.cc | 31 ++-- tests/node_hash_set_test.cc | 31 ++-- tests/parallel_flat_hash_map_test.cc | 248 +-------------------------- tests/parallel_flat_hash_set_test.cc | 129 +------------- 6 files changed, 84 insertions(+), 429 deletions(-) diff --git a/tests/flat_hash_map_test.cc b/tests/flat_hash_map_test.cc index 810d3f3..478bcbb 100644 --- a/tests/flat_hash_map_test.cc +++ b/tests/flat_hash_map_test.cc @@ -12,6 +12,11 @@ // See the License for the specific language governing permissions and // limitations under the License. +#ifndef THIS_HASH_MAP + #define THIS_HASH_MAP flat_hash_map + #define THIS_TEST_NAME FlatHashMap +#endif + #include "parallel_hashmap/phmap.h" #if defined(PHMAP_HAVE_STD_ANY) @@ -34,7 +39,7 @@ using ::testing::Pair; using ::testing::UnorderedElementsAre; template -using Map = flat_hash_map>>; static_assert(!std::is_standard_layout(), ""); @@ -44,12 +49,12 @@ using MapTypes = Map, Map, Map, Map>; -INSTANTIATE_TYPED_TEST_SUITE_P(FlatHashMap, ConstructorTest, MapTypes); -INSTANTIATE_TYPED_TEST_SUITE_P(FlatHashMap, LookupTest, MapTypes); -INSTANTIATE_TYPED_TEST_SUITE_P(FlatHashMap, MembersTest, MapTypes); -INSTANTIATE_TYPED_TEST_SUITE_P(FlatHashMap, ModifiersTest, MapTypes); +INSTANTIATE_TYPED_TEST_SUITE_P(THIS_TEST_NAME, ConstructorTest, MapTypes); +INSTANTIATE_TYPED_TEST_SUITE_P(THIS_TEST_NAME, LookupTest, MapTypes); +INSTANTIATE_TYPED_TEST_SUITE_P(THIS_TEST_NAME, MembersTest, MapTypes); +INSTANTIATE_TYPED_TEST_SUITE_P(THIS_TEST_NAME, ModifiersTest, MapTypes); -TEST(FlatHashMap, StandardLayout) { +TEST(THIS_TEST_NAME, StandardLayout) { struct Int { explicit Int(size_t value) : value(value) {} Int() : value(0) { ADD_FAILURE(); } @@ -67,14 +72,14 @@ TEST(FlatHashMap, StandardLayout) { // Verify that neither the key nor the value get default-constructed or // copy-constructed. { - flat_hash_map m; + THIS_HASH_MAP m; m.try_emplace(Int(1), Int(2)); m.try_emplace(Int(3), Int(4)); m.erase(Int(1)); m.rehash(2 * m.bucket_count()); } { - flat_hash_map m; + THIS_HASH_MAP m; m.try_emplace(Int(1), Int(2)); m.try_emplace(Int(3), Int(4)); m.erase(Int(1)); @@ -85,12 +90,12 @@ TEST(FlatHashMap, StandardLayout) { // gcc becomes unhappy if this is inside the method, so pull it out here. struct balast {}; -TEST(FlatHashMap, IteratesMsan) { +TEST(THIS_TEST_NAME, IteratesMsan) { // Because SwissTable randomizes on pointer addresses, we keep old tables // around to ensure we don't reuse old memory. - std::vector> garbage; + std::vector> garbage; for (int i = 0; i < 100; ++i) { - phmap::flat_hash_map t; + phmap::THIS_HASH_MAP t; for (int j = 0; j < 100; ++j) { t[j]; for (const auto& p : t) EXPECT_THAT(p, Pair(_, _)); @@ -138,12 +143,12 @@ struct Eq { } }; -TEST(FlatHashMap, LazyKeyPattern) { +TEST(THIS_TEST_NAME, LazyKeyPattern) { // hashes are only guaranteed in opt mode, we use assertions to track internal // state that can cause extra calls to hash. int conversions = 0; int hashes = 0; - flat_hash_map m(0, Hash{&hashes}); + THIS_HASH_MAP m(0, Hash{&hashes}); m.reserve(3); m[LazyInt(1, &conversions)] = 1; @@ -175,12 +180,12 @@ TEST(FlatHashMap, LazyKeyPattern) { #endif } -TEST(FlatHashMap, BitfieldArgument) { +TEST(THIS_TEST_NAME, BitfieldArgument) { union { int n : 1; }; n = 0; - flat_hash_map m; + THIS_HASH_MAP m; m.erase(n); m.count(n); m.prefetch(n); @@ -195,10 +200,10 @@ TEST(FlatHashMap, BitfieldArgument) { m[n]; } -TEST(FlatHashMap, MergeExtractInsert) { - // We can't test mutable keys, or non-copyable keys with flat_hash_map. +TEST(THIS_TEST_NAME, MergeExtractInsert) { + // We can't test mutable keys, or non-copyable keys with THIS_HASH_MAP. // Test that the nodes have the proper API. - phmap::flat_hash_map m = {{1, 7}, {2, 9}}; + phmap::THIS_HASH_MAP m = {{1, 7}, {2, 9}}; auto node = m.extract(1); EXPECT_TRUE(node); EXPECT_EQ(node.key(), 1); @@ -210,8 +215,8 @@ TEST(FlatHashMap, MergeExtractInsert) { EXPECT_THAT(m, UnorderedElementsAre(Pair(1, 17), Pair(2, 9))); } #if !defined(__ANDROID__) && !defined(__APPLE__) && !defined(__EMSCRIPTEN__) && defined(PHMAP_HAVE_STD_ANY) -TEST(FlatHashMap, Any) { - phmap::flat_hash_map m; +TEST(THIS_TEST_NAME, Any) { + phmap::THIS_HASH_MAP m; m.emplace(1, 7); auto it = m.find(1); ASSERT_NE(it, m.end()); @@ -234,7 +239,7 @@ TEST(FlatHashMap, Any) { struct E { bool operator()(const std::any&, const std::any&) const { return true; } }; - phmap::flat_hash_map m2; + phmap::THIS_HASH_MAP m2; m2.emplace(1, 7); auto it2 = m2.find(1); ASSERT_NE(it2, m2.end()); diff --git a/tests/flat_hash_set_test.cc b/tests/flat_hash_set_test.cc index 54481a2..e275ec1 100644 --- a/tests/flat_hash_set_test.cc +++ b/tests/flat_hash_set_test.cc @@ -12,6 +12,11 @@ // See the License for the specific language governing permissions and // limitations under the License. +#ifndef THIS_HASH_SET + #define THIS_HASH_SET flat_hash_set + #define THIS_TEST_NAME FlatHashSet +#endif + #include "parallel_hashmap/phmap.h" #include @@ -34,30 +39,30 @@ using ::testing::UnorderedElementsAreArray; template using Set = - phmap::flat_hash_set>; + phmap::THIS_HASH_SET>; using SetTypes = ::testing::Types, Set, Set, Set>; -INSTANTIATE_TYPED_TEST_SUITE_P(FlatHashSet, ConstructorTest, SetTypes); -INSTANTIATE_TYPED_TEST_SUITE_P(FlatHashSet, LookupTest, SetTypes); -INSTANTIATE_TYPED_TEST_SUITE_P(FlatHashSet, MembersTest, SetTypes); -INSTANTIATE_TYPED_TEST_SUITE_P(FlatHashSet, ModifiersTest, SetTypes); +INSTANTIATE_TYPED_TEST_SUITE_P(THIS_TEST_NAME, ConstructorTest, SetTypes); +INSTANTIATE_TYPED_TEST_SUITE_P(THIS_TEST_NAME, LookupTest, SetTypes); +INSTANTIATE_TYPED_TEST_SUITE_P(THIS_TEST_NAME, MembersTest, SetTypes); +INSTANTIATE_TYPED_TEST_SUITE_P(THIS_TEST_NAME, ModifiersTest, SetTypes); #if PHMAP_HAVE_STD_STRING_VIEW -TEST(FlatHashSet, EmplaceString) { +TEST(THIS_TEST_NAME, EmplaceString) { std::vector v = {"a", "b"}; - phmap::flat_hash_set hs(v.begin(), v.end()); + phmap::THIS_HASH_SET hs(v.begin(), v.end()); EXPECT_THAT(hs, UnorderedElementsAreArray(v)); } #endif -TEST(FlatHashSet, BitfieldArgument) { +TEST(THIS_TEST_NAME, BitfieldArgument) { union { int n : 1; }; n = 0; - phmap::flat_hash_set s = {n}; + phmap::THIS_HASH_SET s = {n}; s.insert(n); s.insert(s.end(), n); s.insert({n}); @@ -69,7 +74,7 @@ TEST(FlatHashSet, BitfieldArgument) { s.equal_range(n); } -TEST(FlatHashSet, MergeExtractInsert) { +TEST(THIS_TEST_NAME, MergeExtractInsert) { struct Hash { size_t operator()(const std::unique_ptr& p) const { return *p; } }; @@ -79,7 +84,7 @@ TEST(FlatHashSet, MergeExtractInsert) { return *a == *b; } }; - phmap::flat_hash_set, Hash, Eq> set1, set2; + phmap::THIS_HASH_SET, Hash, Eq> set1, set2; set1.insert(phmap::make_unique(7)); set1.insert(phmap::make_unique(17)); diff --git a/tests/node_hash_map_test.cc b/tests/node_hash_map_test.cc index cf1ec13..102ce5b 100644 --- a/tests/node_hash_map_test.cc +++ b/tests/node_hash_map_test.cc @@ -12,6 +12,11 @@ // See the License for the specific language governing permissions and // limitations under the License. +#ifndef THIS_HASH_MAP + #define THIS_HASH_MAP node_hash_map + #define THIS_TEST_NAME NodeHashMap +#endif + #include "parallel_hashmap/phmap.h" #include "tracked.h" @@ -29,20 +34,20 @@ using ::testing::Pair; using ::testing::UnorderedElementsAre; using MapTypes = ::testing::Types< - phmap::node_hash_map>>, - phmap::node_hash_map>>>; -INSTANTIATE_TYPED_TEST_SUITE_P(NodeHashMap, ConstructorTest, MapTypes); -INSTANTIATE_TYPED_TEST_SUITE_P(NodeHashMap, LookupTest, MapTypes); -INSTANTIATE_TYPED_TEST_SUITE_P(NodeHashMap, MembersTest, MapTypes); -INSTANTIATE_TYPED_TEST_SUITE_P(NodeHashMap, ModifiersTest, MapTypes); +INSTANTIATE_TYPED_TEST_SUITE_P(THIS_TEST_NAME, ConstructorTest, MapTypes); +INSTANTIATE_TYPED_TEST_SUITE_P(THIS_TEST_NAME, LookupTest, MapTypes); +INSTANTIATE_TYPED_TEST_SUITE_P(THIS_TEST_NAME, MembersTest, MapTypes); +INSTANTIATE_TYPED_TEST_SUITE_P(THIS_TEST_NAME, ModifiersTest, MapTypes); -using M = phmap::node_hash_map>; +using M = phmap::THIS_HASH_MAP>; -TEST(NodeHashMap, Emplace) { +TEST(THIS_TEST_NAME, Emplace) { M m; Tracked t(53); m.emplace("a", t); @@ -110,10 +115,10 @@ TEST(NodeHashMap, Emplace) { ASSERT_EQ(7, t.num_copies()); } -TEST(NodeHashMap, AssignRecursive) { +TEST(THIS_TEST_NAME, AssignRecursive) { struct Tree { // Verify that unordered_map can be instantiated. - phmap::node_hash_map children; + phmap::THIS_HASH_MAP children; }; Tree root; const Tree& child = root.children.emplace().first->second; @@ -133,7 +138,7 @@ TEST(FlatHashMap, MoveOnlyKey) { struct Hash { size_t operator()(const Key&) const { return 0; } }; - phmap::node_hash_map m; + phmap::THIS_HASH_MAP m; m[Key()]; } @@ -155,8 +160,8 @@ struct NonMovableKeyEq { bool operator()(const NonMovableKey& a, int b) const { return a.i == b; } }; -TEST(NodeHashMap, MergeExtractInsert) { - phmap::node_hash_map +TEST(THIS_TEST_NAME, MergeExtractInsert) { + phmap::THIS_HASH_MAP set1, set2; set1.emplace(std::piecewise_construct, std::make_tuple(7), std::make_tuple(-7)); diff --git a/tests/node_hash_set_test.cc b/tests/node_hash_set_test.cc index 3a08821..ed7446d 100644 --- a/tests/node_hash_set_test.cc +++ b/tests/node_hash_set_test.cc @@ -12,6 +12,11 @@ // See the License for the specific language governing permissions and // limitations under the License. +#ifndef THIS_HASH_SET + #define THIS_HASH_SET node_hash_set + #define THIS_TEST_NAME NodeHashSet +#endif + #include "parallel_hashmap/phmap.h" #include "unordered_set_constructor_test.h" @@ -28,25 +33,25 @@ using ::testing::Pointee; using ::testing::UnorderedElementsAre; using SetTypes = ::testing::Types< - node_hash_set>, - node_hash_set>, + THIS_HASH_SET>, - node_hash_set>, - node_hash_set>, + THIS_HASH_SET>>; -INSTANTIATE_TYPED_TEST_SUITE_P(NodeHashSet, ConstructorTest, SetTypes); -INSTANTIATE_TYPED_TEST_SUITE_P(NodeHashSet, LookupTest, SetTypes); -INSTANTIATE_TYPED_TEST_SUITE_P(NodeHashSet, MembersTest, SetTypes); -INSTANTIATE_TYPED_TEST_SUITE_P(NodeHashSet, ModifiersTest, SetTypes); +INSTANTIATE_TYPED_TEST_SUITE_P(THIS_TEST_NAME, ConstructorTest, SetTypes); +INSTANTIATE_TYPED_TEST_SUITE_P(THIS_TEST_NAME, LookupTest, SetTypes); +INSTANTIATE_TYPED_TEST_SUITE_P(THIS_TEST_NAME, MembersTest, SetTypes); +INSTANTIATE_TYPED_TEST_SUITE_P(THIS_TEST_NAME, ModifiersTest, SetTypes); -TEST(NodeHashSet, MoveableNotCopyableCompiles) { - node_hash_set> t; - node_hash_set> u; +TEST(THIS_TEST_NAME, MoveableNotCopyableCompiles) { + THIS_HASH_SET> t; + THIS_HASH_SET> u; u = std::move(t); } -TEST(NodeHashSet, MergeExtractInsert) { +TEST(THIS_TEST_NAME, MergeExtractInsert) { struct Hash { size_t operator()(const std::unique_ptr& p) const { return *p; } }; @@ -56,7 +61,7 @@ TEST(NodeHashSet, MergeExtractInsert) { return *a == *b; } }; - phmap::node_hash_set, Hash, Eq> set1, set2; + phmap::THIS_HASH_SET, Hash, Eq> set1, set2; set1.insert(phmap::make_unique(7)); set1.insert(phmap::make_unique(17)); diff --git a/tests/parallel_flat_hash_map_test.cc b/tests/parallel_flat_hash_map_test.cc index 4ffa409..d1e6288 100644 --- a/tests/parallel_flat_hash_map_test.cc +++ b/tests/parallel_flat_hash_map_test.cc @@ -1,246 +1,4 @@ -// Copyright 2018 The Abseil Authors. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// https://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. +#define THIS_HASH_MAP parallel_flat_hash_map +#define THIS_TEST_NAME ParallelFlatHashMap -#include "parallel_hashmap/phmap.h" - -#if defined(PHMAP_HAVE_STD_ANY) - #include -#endif - -#include "hash_generator_testing.h" -#include "unordered_map_constructor_test.h" -#include "unordered_map_lookup_test.h" -#include "unordered_map_members_test.h" -#include "unordered_map_modifiers_test.h" - -namespace phmap { -namespace container_internal { -namespace { -using ::phmap::container_internal::hash_internal::Enum; -using ::phmap::container_internal::hash_internal::EnumClass; -using ::testing::_; -using ::testing::Pair; -using ::testing::UnorderedElementsAre; - -template -using Map = - parallel_flat_hash_map>; - -static_assert(!std::is_standard_layout(), ""); - -using MapTypes = - ::testing::Types, Map, Map, - Map, Map, - Map>; - -INSTANTIATE_TYPED_TEST_SUITE_P(ParallelFlatHashMap, ConstructorTest, MapTypes); -INSTANTIATE_TYPED_TEST_SUITE_P(ParallelFlatHashMap, LookupTest, MapTypes); -INSTANTIATE_TYPED_TEST_SUITE_P(ParallelFlatHashMap, ModifiersTest, MapTypes); - -TEST(ParallelFlatHashMap, StandardLayout) { - struct Int { - explicit Int(size_t value) : value(value) {} - Int() : value(0) { ADD_FAILURE(); } - Int(const Int& other) : value(other.value) { ADD_FAILURE(); } - Int(Int&&) = default; - bool operator==(const Int& other) const { return value == other.value; } - size_t value; - }; - static_assert(std::is_standard_layout(), ""); - - struct Hash { - size_t operator()(const Int& obj) const { return obj.value; } - }; - - // Verify that neither the key nor the value get default-constructed or - // copy-constructed. - { - parallel_flat_hash_map m; - m.try_emplace(Int(1), Int(2)); - m.try_emplace(Int(3), Int(4)); - m.erase(Int(1)); - m.rehash(2 * m.bucket_count()); - } - { - parallel_flat_hash_map m; - m.try_emplace(Int(1), Int(2)); - m.try_emplace(Int(3), Int(4)); - m.erase(Int(1)); - m.clear(); - } -} - -// gcc becomes unhappy if this is inside the method, so pull it out here. -struct balast {}; - -TEST(ParallelFlatHashMap, IteratesMsan) { - // Because SwissTable randomizes on pointer addresses, we keep old tables - // around to ensure we don't reuse old memory. - std::vector> garbage; - for (int i = 0; i < 100; ++i) { - phmap::parallel_flat_hash_map t; - for (int j = 0; j < 100; ++j) { - t[j]; - for (const auto& p : t) EXPECT_THAT(p, Pair(_, _)); - } - garbage.push_back(std::move(t)); - } -} - -// Demonstration of the "Lazy Key" pattern. This uses heterogeneous insert to -// avoid creating expensive key elements when the item is already present in the -// map. -struct LazyInt { - explicit LazyInt(size_t value, int* tracker) - : value(value), tracker(tracker) {} - - explicit operator size_t() const { - ++*tracker; - return value; - } - - size_t value; - int* tracker; -}; - -struct Hash { - using is_transparent = void; - int* tracker; - size_t operator()(size_t obj) const { - ++*tracker; - return obj; - } - size_t operator()(const LazyInt& obj) const { - ++*tracker; - return obj.value; - } -}; - -struct Eq { - using is_transparent = void; - bool operator()(size_t lhs, size_t rhs) const { - return lhs == rhs; - } - bool operator()(size_t lhs, const LazyInt& rhs) const { - return lhs == rhs.value; - } -}; - -TEST(ParallelFlatHashMap, LazyKeyPattern) { - // hashes are only guaranteed in opt mode, we use assertions to track internal - // state that can cause extra calls to hash. - int conversions = 0; - int hashes = 0; - parallel_flat_hash_map m(0, Hash{&hashes}); - m.reserve(3); - - m[LazyInt(1, &conversions)] = 1; - EXPECT_THAT(m, UnorderedElementsAre(Pair(1, 1))); - EXPECT_EQ(conversions, 1); -#ifdef NDEBUG - EXPECT_EQ(hashes, 1); -#endif - - m[LazyInt(1, &conversions)] = 2; - EXPECT_THAT(m, UnorderedElementsAre(Pair(1, 2))); - EXPECT_EQ(conversions, 1); -#ifdef NDEBUG - EXPECT_EQ(hashes, 2); -#endif - - m.try_emplace(LazyInt(2, &conversions), 3); - EXPECT_THAT(m, UnorderedElementsAre(Pair(1, 2), Pair(2, 3))); - EXPECT_EQ(conversions, 2); -#ifdef NDEBUG - EXPECT_EQ(hashes, 3); -#endif - - m.try_emplace(LazyInt(2, &conversions), 4); - EXPECT_THAT(m, UnorderedElementsAre(Pair(1, 2), Pair(2, 3))); - EXPECT_EQ(conversions, 2); -#ifdef NDEBUG - EXPECT_EQ(hashes, 4); -#endif -} - -TEST(ParallelFlatHashMap, BitfieldArgument) { - union { - int n : 1; - }; - n = 0; - parallel_flat_hash_map m; - m.erase(n); - m.count(n); - m.prefetch(n); - m.find(n); - m.contains(n); - m.equal_range(n); - m.insert_or_assign(n, n); - m.insert_or_assign(m.end(), n, n); - m.try_emplace(n); - m.try_emplace(m.end(), n); - m.at(n); - m[n]; -} - -TEST(ParallelFlatHashMap, MergeExtractInsert) { - // We can't test mutable keys, or non-copyable keys with parallel_flat_hash_map. - // Test that the nodes have the proper API. - phmap::parallel_flat_hash_map m = {{1, 7}, {2, 9}}; - auto node = m.extract(1); - EXPECT_TRUE(node); - EXPECT_EQ(node.key(), 1); - EXPECT_EQ(node.mapped(), 7); - EXPECT_THAT(m, UnorderedElementsAre(Pair(2, 9))); - - node.mapped() = 17; - m.insert(std::move(node)); - EXPECT_THAT(m, UnorderedElementsAre(Pair(1, 17), Pair(2, 9))); -} -#if !defined(__ANDROID__) && !defined(__APPLE__) && !defined(__EMSCRIPTEN__) && defined(PHMAP_HAVE_STD_ANY) -TEST(ParallelFlatHashMap, Any) { - phmap::parallel_flat_hash_map m; - m.emplace(1, 7); - auto it = m.find(1); - ASSERT_NE(it, m.end()); - EXPECT_EQ(7, std::any_cast(it->second)); - - m.emplace(std::piecewise_construct, std::make_tuple(2), std::make_tuple(8)); - it = m.find(2); - ASSERT_NE(it, m.end()); - EXPECT_EQ(8, std::any_cast(it->second)); - - m.emplace(std::piecewise_construct, std::make_tuple(3), - std::make_tuple(std::any(9))); - it = m.find(3); - ASSERT_NE(it, m.end()); - EXPECT_EQ(9, std::any_cast(it->second)); - - struct H { - size_t operator()(const std::any&) const { return 0; } - }; - struct E { - bool operator()(const std::any&, const std::any&) const { return true; } - }; - phmap::parallel_flat_hash_map m2; - m2.emplace(1, 7); - auto it2 = m2.find(1); - ASSERT_NE(it2, m2.end()); - EXPECT_EQ(7, it2->second); -} -#endif // __ANDROID__ - -} // namespace -} // namespace container_internal -} // namespace phmap +#include "flat_hash_map_test.cc" diff --git a/tests/parallel_flat_hash_set_test.cc b/tests/parallel_flat_hash_set_test.cc index 15981aa..3c193de 100644 --- a/tests/parallel_flat_hash_set_test.cc +++ b/tests/parallel_flat_hash_set_test.cc @@ -1,127 +1,4 @@ -// Copyright 2018 The Abseil Authors. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// https://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. +#define THIS_HASH_SET parallel_flat_hash_set +#define THIS_TEST_NAME ParallelFlatHashSet -#include "parallel_hashmap/phmap.h" - -#include - -#include "hash_generator_testing.h" -#include "unordered_set_constructor_test.h" -#include "unordered_set_lookup_test.h" -#include "unordered_set_members_test.h" -#include "unordered_set_modifiers_test.h" - -namespace phmap { -namespace container_internal { -namespace { - -using ::phmap::container_internal::hash_internal::Enum; -using ::phmap::container_internal::hash_internal::EnumClass; -using ::testing::Pointee; -using ::testing::UnorderedElementsAre; -using ::testing::UnorderedElementsAreArray; - -template -using Set = - phmap::parallel_flat_hash_set>; - -using SetTypes = - ::testing::Types, Set, Set, Set>; - -INSTANTIATE_TYPED_TEST_SUITE_P(ParallelFlatHashSet, ConstructorTest, SetTypes); -INSTANTIATE_TYPED_TEST_SUITE_P(ParallelFlatHashSet, LookupTest, SetTypes); -INSTANTIATE_TYPED_TEST_SUITE_P(ParallelFlatHashSet, ModifiersTest, SetTypes); - -#if PHMAP_HAVE_STD_STRING_VIEW -TEST(ParallelFlatHashSet, EmplaceString) { - std::vector v = {"a", "b"}; - phmap::parallel_flat_hash_set hs(v.begin(), v.end()); - EXPECT_THAT(hs, UnorderedElementsAreArray(v)); -} -#endif - -TEST(ParallelFlatHashSet, BitfieldArgument) { - union { - int n : 1; - }; - n = 0; - phmap::parallel_flat_hash_set s = {n}; - s.insert(n); - s.insert(s.end(), n); - s.insert({n}); - s.erase(n); - s.count(n); - s.prefetch(n); - s.find(n); - s.contains(n); - s.equal_range(n); -} - -TEST(ParallelFlatHashSet, MergeExtractInsert) { - struct Hash { - size_t operator()(const std::unique_ptr& p) const { return *p; } - }; - struct Eq { - bool operator()(const std::unique_ptr& a, - const std::unique_ptr& b) const { - return *a == *b; - } - }; - phmap::parallel_flat_hash_set, Hash, Eq> set1, set2; - set1.insert(phmap::make_unique(7)); - set1.insert(phmap::make_unique(17)); - - set2.insert(phmap::make_unique(7)); - set2.insert(phmap::make_unique(19)); - - EXPECT_THAT(set1, UnorderedElementsAre(Pointee(7), Pointee(17))); - EXPECT_THAT(set2, UnorderedElementsAre(Pointee(7), Pointee(19))); - - set1.merge(set2); - - EXPECT_THAT(set1, UnorderedElementsAre(Pointee(7), Pointee(17), Pointee(19))); - EXPECT_THAT(set2, UnorderedElementsAre(Pointee(7))); - - auto node = set1.extract(phmap::make_unique(7)); - EXPECT_TRUE(node); - EXPECT_THAT(node.value(), Pointee(7)); - EXPECT_THAT(set1, UnorderedElementsAre(Pointee(17), Pointee(19))); - - auto insert_result = set2.insert(std::move(node)); - EXPECT_FALSE(node); - EXPECT_FALSE(insert_result.inserted); - EXPECT_TRUE(insert_result.node); - EXPECT_THAT(insert_result.node.value(), Pointee(7)); - EXPECT_EQ(**insert_result.position, 7); - EXPECT_NE(insert_result.position->get(), insert_result.node.value().get()); - EXPECT_THAT(set2, UnorderedElementsAre(Pointee(7))); - - node = set1.extract(phmap::make_unique(17)); - EXPECT_TRUE(node); - EXPECT_THAT(node.value(), Pointee(17)); - EXPECT_THAT(set1, UnorderedElementsAre(Pointee(19))); - - node.value() = phmap::make_unique(23); - - insert_result = set2.insert(std::move(node)); - EXPECT_FALSE(node); - EXPECT_TRUE(insert_result.inserted); - EXPECT_FALSE(insert_result.node); - EXPECT_EQ(**insert_result.position, 23); - EXPECT_THAT(set2, UnorderedElementsAre(Pointee(7), Pointee(23))); -} - -} // namespace -} // namespace container_internal -} // namespace phmap +#include "flat_hash_set_test.cc"