25 namespace container_internal {
30 using ::testing::Pair;
31 using ::testing::UnorderedElementsAre;
33 template <
class K,
class V>
34 using Map = flat_hash_map<K, V, StatefulTestingHash, StatefulTestingEqual,
35 Alloc<std::pair<const K, V>>>;
37 static_assert(!std::is_standard_layout<NonStandardLayout>(),
"");
40 ::testing::Types<Map<int, int>, Map<std::string, int>,
41 Map<Enum, std::string>, Map<EnumClass, int>,
42 Map<int, NonStandardLayout>, Map<NonStandardLayout, int>>;
44 INSTANTIATE_TYPED_TEST_SUITE_P(FlatHashMap, ConstructorTest, MapTypes);
45 INSTANTIATE_TYPED_TEST_SUITE_P(FlatHashMap, LookupTest, MapTypes);
46 INSTANTIATE_TYPED_TEST_SUITE_P(FlatHashMap, MembersTest, MapTypes);
47 INSTANTIATE_TYPED_TEST_SUITE_P(FlatHashMap, ModifiersTest, MapTypes);
49 TEST(FlatHashMap, StandardLayout) {
51 explicit Int(
size_t value) : value(value) {}
52 Int() :
value(0) { ADD_FAILURE(); }
53 Int(
const Int& other) :
value(other.
value) { ADD_FAILURE(); }
55 bool operator==(
const Int& other)
const {
return value == other.value; }
58 static_assert(std::is_standard_layout<Int>(),
"");
61 size_t operator()(
const Int& obj)
const {
return obj.value; }
67 flat_hash_map<Int, Int, Hash> m;
68 m.try_emplace(Int(1), Int(2));
69 m.try_emplace(Int(3), Int(4));
71 m.rehash(2 * m.bucket_count());
74 flat_hash_map<Int, Int, Hash> m;
75 m.try_emplace(Int(1), Int(2));
76 m.try_emplace(Int(3), Int(4));
85 TEST(FlatHashMap, IteratesMsan) {
88 std::vector<absl::flat_hash_map<int, balast>> garbage;
89 for (
int i = 0;
i < 100; ++
i) {
91 for (
int j = 0; j < 100; ++j) {
93 for (
const auto& p : t) EXPECT_THAT(p, Pair(_, _));
104 : value(value), tracker(tracker) {}
106 explicit operator size_t()
const {
116 using is_transparent = void;
118 size_t operator()(
size_t obj)
const {
122 size_t operator()(
const LazyInt& obj)
const {
129 using is_transparent = void;
130 bool operator()(
size_t lhs,
size_t rhs)
const {
133 bool operator()(
size_t lhs,
const LazyInt& rhs)
const {
134 return lhs == rhs.value;
138 TEST(FlatHashMap, LazyKeyPattern) {
143 flat_hash_map<size_t, size_t, Hash, Eq> m(0,
Hash{&hashes});
146 m[LazyInt(1, &conversions)] = 1;
147 EXPECT_THAT(m, UnorderedElementsAre(Pair(1, 1)));
148 EXPECT_EQ(conversions, 1);
150 EXPECT_EQ(hashes, 1);
153 m[LazyInt(1, &conversions)] = 2;
154 EXPECT_THAT(m, UnorderedElementsAre(Pair(1, 2)));
155 EXPECT_EQ(conversions, 1);
157 EXPECT_EQ(hashes, 2);
160 m.try_emplace(LazyInt(2, &conversions), 3);
161 EXPECT_THAT(m, UnorderedElementsAre(Pair(1, 2), Pair(2, 3)));
162 EXPECT_EQ(conversions, 2);
164 EXPECT_EQ(hashes, 3);
167 m.try_emplace(LazyInt(2, &conversions), 4);
168 EXPECT_THAT(m, UnorderedElementsAre(Pair(1, 2), Pair(2, 3)));
169 EXPECT_EQ(conversions, 2);
171 EXPECT_EQ(hashes, 4);
175 TEST(FlatHashMap, BitfieldArgument) {
180 flat_hash_map<int, int> m;
187 m.insert_or_assign(
n,
n);
188 m.insert_or_assign(m.end(),
n,
n);
190 m.try_emplace(m.end(),
n);
195 TEST(FlatHashMap, MergeExtractInsert) {
201 EXPECT_EQ(node.key(), 1);
202 EXPECT_EQ(node.mapped(), 7);
203 EXPECT_THAT(m, UnorderedElementsAre(Pair(2, 9)));
207 EXPECT_THAT(m, UnorderedElementsAre(Pair(1, 17), Pair(2, 9)));
210 #if (defined(ABSL_HAVE_STD_ANY) || !defined(_LIBCPP_VERSION)) && \ 211 !defined(__EMSCRIPTEN__) 212 TEST(FlatHashMap, Any) {
216 ASSERT_NE(it, m.
end());
217 EXPECT_EQ(7, absl::any_cast<int>(it->second));
219 m.
emplace(std::piecewise_construct, std::make_tuple(2), std::make_tuple(8));
221 ASSERT_NE(it, m.
end());
222 EXPECT_EQ(8, absl::any_cast<int>(it->second));
224 m.
emplace(std::piecewise_construct, std::make_tuple(3),
227 ASSERT_NE(it, m.
end());
228 EXPECT_EQ(9, absl::any_cast<int>(it->second));
231 size_t operator()(
const absl::any&)
const {
return 0; }
238 auto it2 = m2.
find(1);
239 ASSERT_NE(it2, m2.
end());
240 EXPECT_EQ(7, it2->second);
242 #endif // (defined(ABSL_HAVE_STD_ANY) || !defined(_LIBCPP_VERSION)) && std::pair< iterator, bool > emplace(Args &&... args)
TEST(NotificationTest, SanityTest)
node_type extract(const_iterator position)
bool operator==(const absl::InlinedVector< T, N, A > &a, const absl::InlinedVector< T, N, A > &b)
std::pair< iterator, bool > insert(T &&value)
constexpr absl::remove_reference_t< T > && move(T &&t) noexcept
iterator find(const key_arg< K > &key, size_t hash)