Commit f16e457b by Abseil Team Committed by Copybara-Service

Unit-tests to verify ABSL raw_hash_set does not double-hash in prod

PiperOrigin-RevId: 590337123
Change-Id: Ib98bbb5a5dadbce5e891567038e016f4da2efc0b
parent ad0a6d2f
......@@ -682,13 +682,17 @@ cc_test(
":hash_function_defaults",
":hash_policy_testing",
":hashtable_debug",
":hashtablez_sampler",
":raw_hash_set",
":test_allocator",
"//absl/base",
"//absl/base:config",
"//absl/base:core_headers",
"//absl/base:prefetch",
"//absl/hash",
"//absl/log",
"//absl/memory",
"//absl/meta:type_traits",
"//absl/strings",
"@com_google_googletest//:gtest",
"@com_google_googletest//:gtest_main",
......
......@@ -745,14 +745,18 @@ absl_cc_test(
absl::core_headers
absl::flat_hash_map
absl::flat_hash_set
absl::hash
absl::hash_function_defaults
absl::hash_policy_testing
absl::hashtable_debug
absl::hashtablez_sampler
absl::log
absl::memory
absl::prefetch
absl::raw_hash_set
absl::strings
absl::test_allocator
absl::type_traits
GTest::gmock_main
)
......
......@@ -49,8 +49,12 @@
#include "absl/container/internal/hash_function_defaults.h"
#include "absl/container/internal/hash_policy_testing.h"
#include "absl/container/internal/hashtable_debug.h"
#include "absl/container/internal/hashtablez_sampler.h"
#include "absl/container/internal/test_allocator.h"
#include "absl/hash/hash.h"
#include "absl/log/log.h"
#include "absl/memory/memory.h"
#include "absl/meta/type_traits.h"
#include "absl/strings/string_view.h"
namespace absl {
......@@ -2594,6 +2598,69 @@ using RawHashSetAlloc = raw_hash_set<IntPolicy, hash_default_hash<int64_t>,
TEST(Table, AllocatorPropagation) { TestAllocPropagation<RawHashSetAlloc>(); }
struct CountedHash {
size_t operator()(int value) const {
++count;
return static_cast<size_t>(value);
}
mutable int count = 0;
};
struct CountedHashIntTable
: raw_hash_set<IntPolicy, CountedHash, std::equal_to<int>,
std::allocator<int>> {
using Base = typename CountedHashIntTable::raw_hash_set;
using Base::Base;
};
TEST(Table, CountedHash) {
// Verify that raw_hash_set does not compute redundant hashes.
#ifdef NDEBUG
constexpr bool kExpectMinimumHashes = true;
#else
constexpr bool kExpectMinimumHashes = false;
#endif
if (!kExpectMinimumHashes) {
GTEST_SKIP() << "Only run under NDEBUG: `assert` statements may cause "
"redundant hashing.";
}
using Table = CountedHashIntTable;
auto HashCount = [](const Table& t) { return t.hash_function().count; };
{
Table t;
EXPECT_EQ(HashCount(t), 0);
}
{
Table t;
t.insert(1);
EXPECT_EQ(HashCount(t), 1);
t.erase(1);
EXPECT_EQ(HashCount(t), 2);
}
{
Table t;
t.insert(3);
EXPECT_EQ(HashCount(t), 1);
auto node = t.extract(3);
EXPECT_EQ(HashCount(t), 2);
t.insert(std::move(node));
EXPECT_EQ(HashCount(t), 3);
}
{
Table t;
t.emplace(5);
EXPECT_EQ(HashCount(t), 1);
}
{
Table src;
src.insert(7);
Table dst;
dst.merge(src);
EXPECT_EQ(HashCount(dst), 1);
}
}
} // namespace
} // namespace container_internal
ABSL_NAMESPACE_END
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment