-- 7f6c15aadc4d97e217dd446518dbb4fdc86b36a3 by Derek Mauro <dmauro@google.com>: Upgrade GCC automated testing to use GCC 9.2 and Cmake 3.16.2 PiperOrigin-RevId: 288488783 -- a978cee848d3cf65b0826c981bfd81022fc36660 by Abseil Team <absl-team@google.com>: Removing formatting traits that were only used internally. ON_CALL/EXPECT_CALL do a sufficient job here. PiperOrigin-RevId: 288386509 -- fdec6f40293d5883220f1f0ea1261f7c5b60a66e by Derek Mauro <dmauro@google.com>: Upgrade MacOS tests to use Bazel 2.0.0 PiperOrigin-RevId: 288373298 -- 465865c4123e9481ab50ea0527e92b39519704dd by Derek Mauro <dmauro@google.com>: Changes to support GCC 9 * Fix several -Wredundant-move warnings * Remove FlatHashMap.Any test, which basically doesn't work on any platform any more (see https://cplusplus.github.io/LWG/lwg-active.html#3121) * Fix a constant sign-compare warning * Conditionally compile out the PoisonHash test which doesn't build PiperOrigin-RevId: 288360204 -- 57c4bb07fc58e7dd2a04f3c45027aab5ecaccf25 by Andy Soffer <asoffer@google.com>: Deflaking MockingBitGen test. Because MockingBitGen can return random values, it is inherently flaky. For log-unifrom, 2040 is a common enough value that tests failed unreasonably frequently. Replacing it with a significantly larger value so as to be much less common. 50000 is a good choice because it is (tied for) the least likely to occur randomly from this distribution, but is still in the distribution. PiperOrigin-RevId: 288360112 -- 86f38e4109899d972de353b1c556c018cfe37956 by Matt Calabrese <calabrese@google.com>: Remove construction tests for the internal `CompressedTuple<std::any>` instantiation. This was not guaranteed to work for the reasons that `std::tuple<std::any>` copy construction does not actually work by standard specification (some implementations introduce workarounds for this). In GCC9, `CompressedTuple<std::any>` and `std::tuple<std::any>` both fail for the same reasons, and a proper "fix" requires updating `std::any`, which is out of our control. PiperOrigin-RevId: 288351977 GitOrigin-RevId: 7f6c15aadc4d97e217dd446518dbb4fdc86b36a3 Change-Id: I5d5c62bd297dc0ff1f2970ff076bb5cd088a7e4c
		
			
				
	
	
		
			259 lines
		
	
	
	
		
			7.6 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			259 lines
		
	
	
	
		
			7.6 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 <memory>
 | |
| 
 | |
| #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 {
 | |
| ABSL_NAMESPACE_BEGIN
 | |
| namespace container_internal {
 | |
| namespace {
 | |
| using ::absl::container_internal::hash_internal::Enum;
 | |
| using ::absl::container_internal::hash_internal::EnumClass;
 | |
| using ::testing::_;
 | |
| using ::testing::IsEmpty;
 | |
| 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);
 | |
| 
 | |
| using UniquePtrMapTypes = ::testing::Types<Map<int, std::unique_ptr<int>>>;
 | |
| 
 | |
| INSTANTIATE_TYPED_TEST_SUITE_P(FlatHashMap, UniquePtrModifiersTest,
 | |
|                                UniquePtrMapTypes);
 | |
| 
 | |
| 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)));
 | |
| }
 | |
| 
 | |
| bool FirstIsEven(std::pair<const int, int> p) { return p.first % 2 == 0; }
 | |
| 
 | |
| TEST(FlatHashMap, EraseIf) {
 | |
|   // Erase all elements.
 | |
|   {
 | |
|     flat_hash_map<int, int> s = {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}};
 | |
|     erase_if(s, [](std::pair<const int, int>) { return true; });
 | |
|     EXPECT_THAT(s, IsEmpty());
 | |
|   }
 | |
|   // Erase no elements.
 | |
|   {
 | |
|     flat_hash_map<int, int> s = {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}};
 | |
|     erase_if(s, [](std::pair<const int, int>) { return false; });
 | |
|     EXPECT_THAT(s, UnorderedElementsAre(Pair(1, 1), Pair(2, 2), Pair(3, 3),
 | |
|                                         Pair(4, 4), Pair(5, 5)));
 | |
|   }
 | |
|   // Erase specific elements.
 | |
|   {
 | |
|     flat_hash_map<int, int> s = {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}};
 | |
|     erase_if(s,
 | |
|              [](std::pair<const int, int> kvp) { return kvp.first % 2 == 1; });
 | |
|     EXPECT_THAT(s, UnorderedElementsAre(Pair(2, 2), Pair(4, 4)));
 | |
|   }
 | |
|   // Predicate is function reference.
 | |
|   {
 | |
|     flat_hash_map<int, int> s = {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}};
 | |
|     erase_if(s, FirstIsEven);
 | |
|     EXPECT_THAT(s, UnorderedElementsAre(Pair(1, 1), Pair(3, 3), Pair(5, 5)));
 | |
|   }
 | |
|   // Predicate is function pointer.
 | |
|   {
 | |
|     flat_hash_map<int, int> s = {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}};
 | |
|     erase_if(s, &FirstIsEven);
 | |
|     EXPECT_THAT(s, UnorderedElementsAre(Pair(1, 1), Pair(3, 3), Pair(5, 5)));
 | |
|   }
 | |
| }
 | |
| 
 | |
| }  // namespace
 | |
| }  // namespace container_internal
 | |
| ABSL_NAMESPACE_END
 | |
| }  // namespace absl
 |