00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include "absl/container/node_hash_set.h"
00016
00017 #include "absl/container/internal/unordered_set_constructor_test.h"
00018 #include "absl/container/internal/unordered_set_lookup_test.h"
00019 #include "absl/container/internal/unordered_set_members_test.h"
00020 #include "absl/container/internal/unordered_set_modifiers_test.h"
00021
00022 namespace absl {
00023 namespace container_internal {
00024 namespace {
00025 using ::absl::container_internal::hash_internal::Enum;
00026 using ::absl::container_internal::hash_internal::EnumClass;
00027 using ::testing::Pointee;
00028 using ::testing::UnorderedElementsAre;
00029
00030 using SetTypes = ::testing::Types<
00031 node_hash_set<int, StatefulTestingHash, StatefulTestingEqual, Alloc<int>>,
00032 node_hash_set<std::string, StatefulTestingHash, StatefulTestingEqual,
00033 Alloc<std::string>>,
00034 node_hash_set<Enum, StatefulTestingHash, StatefulTestingEqual, Alloc<Enum>>,
00035 node_hash_set<EnumClass, StatefulTestingHash, StatefulTestingEqual,
00036 Alloc<EnumClass>>>;
00037
00038 INSTANTIATE_TYPED_TEST_SUITE_P(NodeHashSet, ConstructorTest, SetTypes);
00039 INSTANTIATE_TYPED_TEST_SUITE_P(NodeHashSet, LookupTest, SetTypes);
00040 INSTANTIATE_TYPED_TEST_SUITE_P(NodeHashSet, MembersTest, SetTypes);
00041 INSTANTIATE_TYPED_TEST_SUITE_P(NodeHashSet, ModifiersTest, SetTypes);
00042
00043 TEST(NodeHashSet, MoveableNotCopyableCompiles) {
00044 node_hash_set<std::unique_ptr<void*>> t;
00045 node_hash_set<std::unique_ptr<void*>> u;
00046 u = std::move(t);
00047 }
00048
00049 TEST(NodeHashSet, MergeExtractInsert) {
00050 struct Hash {
00051 size_t operator()(const std::unique_ptr<int>& p) const { return *p; }
00052 };
00053 struct Eq {
00054 bool operator()(const std::unique_ptr<int>& a,
00055 const std::unique_ptr<int>& b) const {
00056 return *a == *b;
00057 }
00058 };
00059 absl::node_hash_set<std::unique_ptr<int>, Hash, Eq> set1, set2;
00060 set1.insert(absl::make_unique<int>(7));
00061 set1.insert(absl::make_unique<int>(17));
00062
00063 set2.insert(absl::make_unique<int>(7));
00064 set2.insert(absl::make_unique<int>(19));
00065
00066 EXPECT_THAT(set1, UnorderedElementsAre(Pointee(7), Pointee(17)));
00067 EXPECT_THAT(set2, UnorderedElementsAre(Pointee(7), Pointee(19)));
00068
00069 set1.merge(set2);
00070
00071 EXPECT_THAT(set1, UnorderedElementsAre(Pointee(7), Pointee(17), Pointee(19)));
00072 EXPECT_THAT(set2, UnorderedElementsAre(Pointee(7)));
00073
00074 auto node = set1.extract(absl::make_unique<int>(7));
00075 EXPECT_TRUE(node);
00076 EXPECT_THAT(node.value(), Pointee(7));
00077 EXPECT_THAT(set1, UnorderedElementsAre(Pointee(17), Pointee(19)));
00078
00079 auto insert_result = set2.insert(std::move(node));
00080 EXPECT_FALSE(node);
00081 EXPECT_FALSE(insert_result.inserted);
00082 EXPECT_TRUE(insert_result.node);
00083 EXPECT_THAT(insert_result.node.value(), Pointee(7));
00084 EXPECT_EQ(**insert_result.position, 7);
00085 EXPECT_NE(insert_result.position->get(), insert_result.node.value().get());
00086 EXPECT_THAT(set2, UnorderedElementsAre(Pointee(7)));
00087
00088 node = set1.extract(absl::make_unique<int>(17));
00089 EXPECT_TRUE(node);
00090 EXPECT_THAT(node.value(), Pointee(17));
00091 EXPECT_THAT(set1, UnorderedElementsAre(Pointee(19)));
00092
00093 node.value() = absl::make_unique<int>(23);
00094
00095 insert_result = set2.insert(std::move(node));
00096 EXPECT_FALSE(node);
00097 EXPECT_TRUE(insert_result.inserted);
00098 EXPECT_FALSE(insert_result.node);
00099 EXPECT_EQ(**insert_result.position, 23);
00100 EXPECT_THAT(set2, UnorderedElementsAre(Pointee(7), Pointee(23)));
00101 }
00102
00103 }
00104 }
00105 }