Skip to content

Commit

Permalink
Add test cases for absl::Hash:
Browse files Browse the repository at this point in the history
- That hashes are consistent for the same int value across different int types.
- That hashes of vectors of strings are unequal even when their concatenations are equal.
- That FragmentedCord hashes works as intended for small Cords.

PiperOrigin-RevId: 707592036
Change-Id: Ifb440245ce94bf9e624f64f9e370c0918c0b3f54
  • Loading branch information
ezbr authored and copybara-github committed Dec 18, 2024
1 parent 553c74b commit d6a75d9
Showing 1 changed file with 33 additions and 18 deletions.
51 changes: 33 additions & 18 deletions absl/hash/hash_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,15 @@ TEST(HashValueTest, EnumAndBool) {
std::make_tuple(true, false)));
}

TEST(HashValueTest, HashConsistentAcrossIntTypes){
std::vector<size_t> hashes = {
absl::Hash<int8_t>{}(1), absl::Hash<uint8_t>{}(1),
absl::Hash<int16_t>{}(1), absl::Hash<uint16_t>{}(1),
absl::Hash<int32_t>{}(1), absl::Hash<uint32_t>{}(1),
absl::Hash<int64_t>{}(1), absl::Hash<uint64_t>{}(1)};
EXPECT_THAT(hashes, testing::Each(absl::Hash<int>{}(1)));
}

TEST(HashValueTest, FloatingPoint) {
EXPECT_TRUE((is_hashable<float>::value));
EXPECT_TRUE((is_hashable<double>::value));
Expand Down Expand Up @@ -303,7 +312,7 @@ TEST(HashValueTest, CombineContiguousWorks) {

struct DummyDeleter {
template <typename T>
void operator() (T* ptr) {}
void operator() (T*) {}
};

struct SmartPointerEq {
Expand Down Expand Up @@ -421,33 +430,39 @@ TEST(HashValueTest, Strings) {
const std::string large = std::string(2048, 'x'); // multiple of chunk size
const std::string huge = std::string(5000, 'a'); // not a multiple

EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly(std::make_tuple( //
std::string(), absl::string_view(), absl::Cord(), //
std::string(""), absl::string_view(""), absl::Cord(""), //
std::string(small), absl::string_view(small), absl::Cord(small), //
std::string(dup), absl::string_view(dup), absl::Cord(dup), //
std::string(large), absl::string_view(large), absl::Cord(large), //
std::string(huge), absl::string_view(huge), FlatCord(huge), //
EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly(std::make_tuple(
std::string(), absl::string_view(), absl::Cord(), std::string(""),
absl::string_view(""), absl::Cord(""), std::string(small),
absl::string_view(small), absl::Cord(small), FragmentedCord(small),
std::string(dup), absl::string_view(dup), absl::Cord(dup),
std::string(large), absl::string_view(large), absl::Cord(large),
std::string(huge), absl::string_view(huge), FlatCord(huge),
FragmentedCord(huge))));

// Also check that nested types maintain the same hash.
const WrapInTuple t{};
EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly(std::make_tuple( //
t(std::string()), t(absl::string_view()), t(absl::Cord()), //
t(std::string("")), t(absl::string_view("")), t(absl::Cord("")), //
t(std::string(small)), t(absl::string_view(small)), //
t(absl::Cord(small)), //
t(std::string(dup)), t(absl::string_view(dup)), t(absl::Cord(dup)), //
t(std::string(large)), t(absl::string_view(large)), //
t(absl::Cord(large)), //
t(std::string(huge)), t(absl::string_view(huge)), //
t(FlatCord(huge)), t(FragmentedCord(huge)))));
EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly(std::make_tuple(
t(std::string()), t(absl::string_view()), t(absl::Cord()),
t(std::string("")), t(absl::string_view("")), t(absl::Cord("")),
t(std::string(small)), t(absl::string_view(small)), t(absl::Cord(small)),
t(FragmentedCord(small)), t(std::string(dup)), t(absl::string_view(dup)),
t(absl::Cord(dup)), t(std::string(large)), t(absl::string_view(large)),
t(absl::Cord(large)), t(std::string(huge)), t(absl::string_view(huge)),
t(FlatCord(huge)), t(FragmentedCord(huge)))));

// Make sure that hashing a `const char*` does not use its string-value.
EXPECT_NE(SpyHash(static_cast<const char*>("ABC")),
SpyHash(absl::string_view("ABC")));
}

TEST(HashValueTest, StringsVector) {
using Vec = std::vector<std::string>;
EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly(std::make_tuple(
Vec{"abc", "def"}, Vec{"abcde", "f"},
Vec{"abcdefghijklmnopqrstuvwxyz", "ABCDEFGHIJKLMNOPQRSTUVWXYZ"},
Vec{"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXY", "Z"})));
}

TEST(HashValueTest, WString) {
EXPECT_TRUE((is_hashable<std::wstring>::value));

Expand Down

0 comments on commit d6a75d9

Please sign in to comment.