00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include "absl/container/flat_hash_set.h"
00016
00017 #include <vector>
00018
00019 #include "absl/container/internal/hash_generator_testing.h"
00020 #include "absl/container/internal/unordered_set_constructor_test.h"
00021 #include "absl/container/internal/unordered_set_lookup_test.h"
00022 #include "absl/container/internal/unordered_set_members_test.h"
00023 #include "absl/container/internal/unordered_set_modifiers_test.h"
00024 #include "absl/memory/memory.h"
00025 #include "absl/strings/string_view.h"
00026
00027 namespace absl {
00028 namespace container_internal {
00029 namespace {
00030
00031 using ::absl::container_internal::hash_internal::Enum;
00032 using ::absl::container_internal::hash_internal::EnumClass;
00033 using ::testing::Pointee;
00034 using ::testing::UnorderedElementsAre;
00035 using ::testing::UnorderedElementsAreArray;
00036
00037 template <class T>
00038 using Set =
00039 absl::flat_hash_set<T, StatefulTestingHash, StatefulTestingEqual, Alloc<T>>;
00040
00041 using SetTypes =
00042 ::testing::Types<Set<int>, Set<std::string>, Set<Enum>, Set<EnumClass>>;
00043
00044 INSTANTIATE_TYPED_TEST_SUITE_P(FlatHashSet, ConstructorTest, SetTypes);
00045 INSTANTIATE_TYPED_TEST_SUITE_P(FlatHashSet, LookupTest, SetTypes);
00046 INSTANTIATE_TYPED_TEST_SUITE_P(FlatHashSet, MembersTest, SetTypes);
00047 INSTANTIATE_TYPED_TEST_SUITE_P(FlatHashSet, ModifiersTest, SetTypes);
00048
00049 TEST(FlatHashSet, EmplaceString) {
00050 std::vector<std::string> v = {"a", "b"};
00051 absl::flat_hash_set<absl::string_view> hs(v.begin(), v.end());
00052 EXPECT_THAT(hs, UnorderedElementsAreArray(v));
00053 }
00054
00055 TEST(FlatHashSet, BitfieldArgument) {
00056 union {
00057 int n : 1;
00058 };
00059 n = 0;
00060 absl::flat_hash_set<int> s = {n};
00061 s.insert(n);
00062 s.insert(s.end(), n);
00063 s.insert({n});
00064 s.erase(n);
00065 s.count(n);
00066 s.prefetch(n);
00067 s.find(n);
00068 s.contains(n);
00069 s.equal_range(n);
00070 }
00071
00072 TEST(FlatHashSet, MergeExtractInsert) {
00073 struct Hash {
00074 size_t operator()(const std::unique_ptr<int>& p) const { return *p; }
00075 };
00076 struct Eq {
00077 bool operator()(const std::unique_ptr<int>& a,
00078 const std::unique_ptr<int>& b) const {
00079 return *a == *b;
00080 }
00081 };
00082 absl::flat_hash_set<std::unique_ptr<int>, Hash, Eq> set1, set2;
00083 set1.insert(absl::make_unique<int>(7));
00084 set1.insert(absl::make_unique<int>(17));
00085
00086 set2.insert(absl::make_unique<int>(7));
00087 set2.insert(absl::make_unique<int>(19));
00088
00089 EXPECT_THAT(set1, UnorderedElementsAre(Pointee(7), Pointee(17)));
00090 EXPECT_THAT(set2, UnorderedElementsAre(Pointee(7), Pointee(19)));
00091
00092 set1.merge(set2);
00093
00094 EXPECT_THAT(set1, UnorderedElementsAre(Pointee(7), Pointee(17), Pointee(19)));
00095 EXPECT_THAT(set2, UnorderedElementsAre(Pointee(7)));
00096
00097 auto node = set1.extract(absl::make_unique<int>(7));
00098 EXPECT_TRUE(node);
00099 EXPECT_THAT(node.value(), Pointee(7));
00100 EXPECT_THAT(set1, UnorderedElementsAre(Pointee(17), Pointee(19)));
00101
00102 auto insert_result = set2.insert(std::move(node));
00103 EXPECT_FALSE(node);
00104 EXPECT_FALSE(insert_result.inserted);
00105 EXPECT_TRUE(insert_result.node);
00106 EXPECT_THAT(insert_result.node.value(), Pointee(7));
00107 EXPECT_EQ(**insert_result.position, 7);
00108 EXPECT_NE(insert_result.position->get(), insert_result.node.value().get());
00109 EXPECT_THAT(set2, UnorderedElementsAre(Pointee(7)));
00110
00111 node = set1.extract(absl::make_unique<int>(17));
00112 EXPECT_TRUE(node);
00113 EXPECT_THAT(node.value(), Pointee(17));
00114 EXPECT_THAT(set1, UnorderedElementsAre(Pointee(19)));
00115
00116 node.value() = absl::make_unique<int>(23);
00117
00118 insert_result = set2.insert(std::move(node));
00119 EXPECT_FALSE(node);
00120 EXPECT_TRUE(insert_result.inserted);
00121 EXPECT_FALSE(insert_result.node);
00122 EXPECT_EQ(**insert_result.position, 23);
00123 EXPECT_THAT(set2, UnorderedElementsAre(Pointee(7), Pointee(23)));
00124 }
00125
00126 }
00127 }
00128 }