diff --git a/absl/hash/hash_test.cc b/absl/hash/hash_test.cc index 2b01196ec7c..b4e49992c73 100644 --- a/absl/hash/hash_test.cc +++ b/absl/hash/hash_test.cc @@ -119,6 +119,15 @@ TEST(HashValueTest, EnumAndBool) { std::make_tuple(true, false))); } +TEST(HashValueTest, HashConsistentAcrossIntTypes){ + std::vector hashes = { + absl::Hash{}(1), absl::Hash{}(1), + absl::Hash{}(1), absl::Hash{}(1), + absl::Hash{}(1), absl::Hash{}(1), + absl::Hash{}(1), absl::Hash{}(1)}; + EXPECT_THAT(hashes, testing::Each(absl::Hash{}(1))); +} + TEST(HashValueTest, FloatingPoint) { EXPECT_TRUE((is_hashable::value)); EXPECT_TRUE((is_hashable::value)); @@ -303,7 +312,7 @@ TEST(HashValueTest, CombineContiguousWorks) { struct DummyDeleter { template - void operator() (T* ptr) {} + void operator() (T*) {} }; struct SmartPointerEq { @@ -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("ABC")), SpyHash(absl::string_view("ABC"))); } +TEST(HashValueTest, StringsVector) { + using Vec = std::vector; + 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::value));