Export of internal Abseil changes

--
066144400e12616f6771e512427bcd98aa203455 by Abseil Team <absl-team@google.com>:

Internal comment cleanup.

PiperOrigin-RevId: 377368470

--
3ba56d263bd90a9797d12b5ce29edce3fa65917c by Abseil Team <absl-team@google.com>:

Add tests for hash table capacity being unchanged by insert.

PiperOrigin-RevId: 377303140
GitOrigin-RevId: 066144400e12616f6771e512427bcd98aa203455
Change-Id: I2edf14b412e45a2ad490dcf9f06e009c12a60e3e
pull/974/head
Abseil Team 4 years ago committed by Derek Mauro
parent ed53ad03ab
commit 17c954d90d
  1. 5
      absl/container/internal/raw_hash_map.h
  2. 31
      absl/container/internal/raw_hash_set_test.cc
  3. 39
      absl/container/internal/unordered_map_modifiers_test.h
  4. 35
      absl/container/internal/unordered_set_modifiers_test.h

@ -51,8 +51,9 @@ class raw_hash_map : public raw_hash_set<Policy, Hash, Eq, Alloc> {
using key_arg = typename KeyArgImpl::template type<K, key_type>;
static_assert(!std::is_reference<key_type>::value, "");
// TODO(alkis): remove this assertion and verify that reference mapped_type is
// supported.
// TODO(b/187807849): Evaluate whether to support reference mapped_type and
// remove this assertion if/when it is supported.
static_assert(!std::is_reference<mapped_type>::value, "");
using iterator = typename raw_hash_map::raw_hash_set::iterator;

@ -541,6 +541,37 @@ TEST(Table, InsertCollisionAndFindAfterDelete) {
EXPECT_TRUE(t.empty());
}
TEST(Table, InsertWithinCapacity) {
IntTable t;
t.reserve(10);
const size_t original_capacity = t.capacity();
const auto addr = [&](int i) {
return reinterpret_cast<uintptr_t>(&*t.find(i));
};
// Inserting an element does not change capacity.
t.insert(0);
EXPECT_THAT(t.capacity(), original_capacity);
const uintptr_t original_addr_0 = addr(0);
// Inserting another element does not rehash.
t.insert(1);
EXPECT_THAT(t.capacity(), original_capacity);
EXPECT_THAT(addr(0), original_addr_0);
// Inserting lots of duplicate elements does not rehash.
for (int i = 0; i < 100; ++i) {
t.insert(i % 10);
}
EXPECT_THAT(t.capacity(), original_capacity);
EXPECT_THAT(addr(0), original_addr_0);
// Inserting a range of duplicate elements does not rehash.
std::vector<int> dup_range;
for (int i = 0; i < 100; ++i) {
dup_range.push_back(i % 10);
}
t.insert(dup_range.begin(), dup_range.end());
EXPECT_THAT(t.capacity(), original_capacity);
EXPECT_THAT(addr(0), original_addr_0);
}
TEST(Table, LazyEmplace) {
StringTable t;
bool called = false;

@ -81,6 +81,38 @@ TYPED_TEST_P(ModifiersTest, InsertRange) {
ASSERT_THAT(items(m), ::testing::UnorderedElementsAreArray(values));
}
TYPED_TEST_P(ModifiersTest, InsertWithinCapacity) {
using T = hash_internal::GeneratedType<TypeParam>;
using V = typename TypeParam::mapped_type;
T val = hash_internal::Generator<T>()();
TypeParam m;
m.reserve(10);
const size_t original_capacity = m.bucket_count();
m.insert(val);
EXPECT_EQ(m.bucket_count(), original_capacity);
T val2 = {val.first, hash_internal::Generator<V>()()};
m.insert(val2);
EXPECT_EQ(m.bucket_count(), original_capacity);
}
TYPED_TEST_P(ModifiersTest, InsertRangeWithinCapacity) {
#if !defined(__GLIBCXX__)
using T = hash_internal::GeneratedType<TypeParam>;
std::vector<T> base_values;
std::generate_n(std::back_inserter(base_values), 10,
hash_internal::Generator<T>());
std::vector<T> values;
while (values.size() != 100) {
std::copy_n(base_values.begin(), 10, std::back_inserter(values));
}
TypeParam m;
m.reserve(10);
const size_t original_capacity = m.bucket_count();
m.insert(values.begin(), values.end());
EXPECT_EQ(m.bucket_count(), original_capacity);
#endif
}
TYPED_TEST_P(ModifiersTest, InsertOrAssign) {
#ifdef UNORDERED_MAP_CXX17
using std::get;
@ -266,9 +298,10 @@ TYPED_TEST_P(ModifiersTest, Swap) {
// TODO(alkis): Write tests for merge.
REGISTER_TYPED_TEST_CASE_P(ModifiersTest, Clear, Insert, InsertHint,
InsertRange, InsertOrAssign, InsertOrAssignHint,
Emplace, EmplaceHint, TryEmplace, TryEmplaceHint,
Erase, EraseRange, EraseKey, Swap);
InsertRange, InsertWithinCapacity,
InsertRangeWithinCapacity, InsertOrAssign,
InsertOrAssignHint, Emplace, EmplaceHint, TryEmplace,
TryEmplaceHint, Erase, EraseRange, EraseKey, Swap);
template <typename Type>
struct is_unique_ptr : std::false_type {};

@ -74,6 +74,36 @@ TYPED_TEST_P(ModifiersTest, InsertRange) {
ASSERT_THAT(keys(m), ::testing::UnorderedElementsAreArray(values));
}
TYPED_TEST_P(ModifiersTest, InsertWithinCapacity) {
using T = hash_internal::GeneratedType<TypeParam>;
T val = hash_internal::Generator<T>()();
TypeParam m;
m.reserve(10);
const size_t original_capacity = m.bucket_count();
m.insert(val);
EXPECT_EQ(m.bucket_count(), original_capacity);
m.insert(val);
EXPECT_EQ(m.bucket_count(), original_capacity);
}
TYPED_TEST_P(ModifiersTest, InsertRangeWithinCapacity) {
#if !defined(__GLIBCXX__)
using T = hash_internal::GeneratedType<TypeParam>;
std::vector<T> base_values;
std::generate_n(std::back_inserter(base_values), 10,
hash_internal::Generator<T>());
std::vector<T> values;
while (values.size() != 100) {
values.insert(values.end(), base_values.begin(), base_values.end());
}
TypeParam m;
m.reserve(10);
const size_t original_capacity = m.bucket_count();
m.insert(values.begin(), values.end());
EXPECT_EQ(m.bucket_count(), original_capacity);
#endif
}
TYPED_TEST_P(ModifiersTest, Emplace) {
using T = hash_internal::GeneratedType<TypeParam>;
T val = hash_internal::Generator<T>()();
@ -180,8 +210,9 @@ TYPED_TEST_P(ModifiersTest, Swap) {
// TODO(alkis): Write tests for merge.
REGISTER_TYPED_TEST_CASE_P(ModifiersTest, Clear, Insert, InsertHint,
InsertRange, Emplace, EmplaceHint, Erase, EraseRange,
EraseKey, Swap);
InsertRange, InsertWithinCapacity,
InsertRangeWithinCapacity, Emplace, EmplaceHint,
Erase, EraseRange, EraseKey, Swap);
} // namespace container_internal
ABSL_NAMESPACE_END

Loading…
Cancel
Save