bf29470384
-- bdce7e57e9e886eff1114d0266781b443f7ec639 by Derek Mauro <dmauro@google.com>: Change {Get|Set}EnvironmentVariable to {Get|Set}EnvironmentVariableA for compatibility with /DUNICODE. PiperOrigin-RevId: 239229514 -- 2276ed502326a044a84060d34eb19d499e3a3be2 by Derek Mauro <dmauro@google.com>: Import of CCTZ from GitHub. PiperOrigin-RevId: 239228622 -- a462efb970ff43b08a362ef2343fb75ac1295a50 by Derek Mauro <dmauro@google.com>: Adding linking of CoreFoundation to CMakeLists in absl/time. Import https://github.com/abseil/abseil-cpp/pull/280. Fix #283 PiperOrigin-RevId: 239220785 -- fc23327b97f940c682aae1956cf7a1bf87f88c06 by Derek Mauro <dmauro@google.com>: Add hermetic test script that uses Docker to build with a very recent version of gcc (8.3.0 today) with libstdc++ and bazel. PiperOrigin-RevId: 239220448 -- 418c08a8f6a53e63b84e39473035774417ca3aa7 by Derek Mauro <dmauro@google.com>: Disable part of the variant exeception safety test on move assignment when using versions of libstd++ that contain a bug. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87431#c7 PiperOrigin-RevId: 239062455 -- 799722217aeda79679577843c91d5be62cbcbb42 by Matt Calabrese <calabrese@google.com>: Add internal-only IsSwappable traits corresponding to std::is_swappable and std::is_nothrow_swappable, which are used with the swap implementations of optional and variant. PiperOrigin-RevId: 239049448 -- aa46a036038a3de5c68ac5e5d3b4bf76f818d2ea by CJ Johnson <johnsoncj@google.com>: Make InlinedVectorStorage constructor explicit PiperOrigin-RevId: 239044361 -- 17949715b3aa21c794701f69f2154e91b6acabc3 by CJ Johnson <johnsoncj@google.com>: Add absl namesapce to internal/inlined_vector.h PiperOrigin-RevId: 239030789 -- 834628325953078cc08ed10d23bb8890e5bec897 by Derek Mauro <dmauro@google.com>: Add test script that uses Docker to build Abseil with gcc-4.8, libstdc++, and cmake. PiperOrigin-RevId: 239028433 -- 80fe24149ed73ed2ced995ad1e372fb060c60427 by CJ Johnson <johnsoncj@google.com>: Factors data members of InlinedVector into an impl type called InlinedVectorStorage so that (in future changes) the contents of a vector can be grouped together with a single pointer. PiperOrigin-RevId: 239021086 -- 585331436d5d4d79f845e45dcf79d918a0dc6169 by Derek Mauro <dmauro@google.com>: Add -Wno-missing-field-initializers to gcc compiler flags. gcc-4.x has spurious missing field initializer warnings. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=36750 PiperOrigin-RevId: 239017217 -- 94602fe4e33ee3a552a7f2939c0f57a992f55075 by Abseil Team <absl-team@google.com>: Formatting fixes. PiperOrigin-RevId: 238983038 -- a1c1b63c08505574e0a8c491561840cecb2bb93e by Derek Mauro <dmauro@google.com>: Add hermetic test script that uses Docker to build with a very recent version of clang with libc++ and bazel. PiperOrigin-RevId: 238669118 -- e525f8d20bc2f79a0d69336b902f63858f3bff9d by Derek Mauro <dmauro@google.com>: Disable the test optionalTest.InPlaceTSFINAEBug until libc++ is updated. PiperOrigin-RevId: 238661703 -- f99a2a0b5ec424a059678f7f226600f137b4c74e by Derek Mauro <dmauro@google.com>: Correct the check for the FlatHashMap-Any test bug (list conditions instead of platforms when possible) PiperOrigin-RevId: 238653344 -- 777928035dbcbf39f361eb7d10dc3696822f692f by Jon Cohen <cohenjon@google.com>: Add install rules for Abseil CMake. These are attempted to be limited to in-project installation. This serves two purposes -- first it's morally the same as using Abseil in-source, except you don't have to rebuild us every time. Second, the presence of an install rule makes life massively simpler for package manager maintainers. Currently this doesn't install absl tests or testonly libraries. This can be added in a follow-up patch. Fixes #38, Fixes #80, Closes #182 PiperOrigin-RevId: 238645836 -- ded1c6ce697c191b7a6ff14572b3e6d183117b2c by Derek Mauro <dmauro@google.com>: Add hermetic test script that uses Docker to build with a very recent version of clang with libstdc++ and bazel. PiperOrigin-RevId: 238517815 GitOrigin-RevId: bdce7e57e9e886eff1114d0266781b443f7ec639 Change-Id: I6f745869cb8ef63851891ccac05ae9a7dd241c4f
247 lines
7 KiB
C++
247 lines
7 KiB
C++
// 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.
|
|
|
|
#include "absl/container/flat_hash_map.h"
|
|
|
|
#include "absl/container/internal/hash_generator_testing.h"
|
|
#include "absl/container/internal/unordered_map_constructor_test.h"
|
|
#include "absl/container/internal/unordered_map_lookup_test.h"
|
|
#include "absl/container/internal/unordered_map_members_test.h"
|
|
#include "absl/container/internal/unordered_map_modifiers_test.h"
|
|
#include "absl/types/any.h"
|
|
|
|
namespace absl {
|
|
namespace container_internal {
|
|
namespace {
|
|
using ::absl::container_internal::hash_internal::Enum;
|
|
using ::absl::container_internal::hash_internal::EnumClass;
|
|
using ::testing::_;
|
|
using ::testing::Pair;
|
|
using ::testing::UnorderedElementsAre;
|
|
|
|
template <class K, class V>
|
|
using Map = flat_hash_map<K, V, StatefulTestingHash, StatefulTestingEqual,
|
|
Alloc<std::pair<const K, V>>>;
|
|
|
|
static_assert(!std::is_standard_layout<NonStandardLayout>(), "");
|
|
|
|
using MapTypes =
|
|
::testing::Types<Map<int, int>, Map<std::string, int>,
|
|
Map<Enum, std::string>, Map<EnumClass, int>,
|
|
Map<int, NonStandardLayout>, Map<NonStandardLayout, int>>;
|
|
|
|
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);
|
|
|
|
TEST(FlatHashMap, 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<Int>(), "");
|
|
|
|
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.
|
|
{
|
|
flat_hash_map<Int, Int, Hash> 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<Int, Int, Hash> 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(FlatHashMap, IteratesMsan) {
|
|
// Because SwissTable randomizes on pointer addresses, we keep old tables
|
|
// around to ensure we don't reuse old memory.
|
|
std::vector<absl::flat_hash_map<int, balast>> garbage;
|
|
for (int i = 0; i < 100; ++i) {
|
|
absl::flat_hash_map<int, balast> 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(FlatHashMap, 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<size_t, size_t, Hash, Eq> 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(FlatHashMap, BitfieldArgument) {
|
|
union {
|
|
int n : 1;
|
|
};
|
|
n = 0;
|
|
flat_hash_map<int, int> 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(FlatHashMap, MergeExtractInsert) {
|
|
// We can't test mutable keys, or non-copyable keys with flat_hash_map.
|
|
// Test that the nodes have the proper API.
|
|
absl::flat_hash_map<int, int> 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(ABSL_HAVE_STD_ANY) || !defined(_LIBCPP_VERSION)) && \
|
|
!defined(__EMSCRIPTEN__)
|
|
TEST(FlatHashMap, Any) {
|
|
absl::flat_hash_map<int, absl::any> m;
|
|
m.emplace(1, 7);
|
|
auto it = m.find(1);
|
|
ASSERT_NE(it, m.end());
|
|
EXPECT_EQ(7, absl::any_cast<int>(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, absl::any_cast<int>(it->second));
|
|
|
|
m.emplace(std::piecewise_construct, std::make_tuple(3),
|
|
std::make_tuple(absl::any(9)));
|
|
it = m.find(3);
|
|
ASSERT_NE(it, m.end());
|
|
EXPECT_EQ(9, absl::any_cast<int>(it->second));
|
|
|
|
struct H {
|
|
size_t operator()(const absl::any&) const { return 0; }
|
|
};
|
|
struct E {
|
|
bool operator()(const absl::any&, const absl::any&) const { return true; }
|
|
};
|
|
absl::flat_hash_map<absl::any, int, H, E> m2;
|
|
m2.emplace(1, 7);
|
|
auto it2 = m2.find(1);
|
|
ASSERT_NE(it2, m2.end());
|
|
EXPECT_EQ(7, it2->second);
|
|
}
|
|
#endif // (defined(ABSL_HAVE_STD_ANY) || !defined(_LIBCPP_VERSION)) &&
|
|
// !defined(__EMSCRIPTEN__)
|
|
|
|
} // namespace
|
|
} // namespace container_internal
|
|
} // namespace absl
|