hash_function_defaults_test.cc
Go to the documentation of this file.
00001 // Copyright 2018 The Abseil Authors.
00002 //
00003 // Licensed under the Apache License, Version 2.0 (the "License");
00004 // you may not use this file except in compliance with the License.
00005 // You may obtain a copy of the License at
00006 //
00007 //      https://www.apache.org/licenses/LICENSE-2.0
00008 //
00009 // Unless required by applicable law or agreed to in writing, software
00010 // distributed under the License is distributed on an "AS IS" BASIS,
00011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00012 // See the License for the specific language governing permissions and
00013 // limitations under the License.
00014 
00015 #include "absl/container/internal/hash_function_defaults.h"
00016 
00017 #include <functional>
00018 #include <type_traits>
00019 #include <utility>
00020 
00021 #include "gtest/gtest.h"
00022 #include "absl/strings/string_view.h"
00023 
00024 namespace absl {
00025 namespace container_internal {
00026 namespace {
00027 
00028 using ::testing::Types;
00029 
00030 TEST(Eq, Int32) {
00031   hash_default_eq<int32_t> eq;
00032   EXPECT_TRUE(eq(1, 1u));
00033   EXPECT_TRUE(eq(1, char{1}));
00034   EXPECT_TRUE(eq(1, true));
00035   EXPECT_TRUE(eq(1, double{1.1}));
00036   EXPECT_FALSE(eq(1, char{2}));
00037   EXPECT_FALSE(eq(1, 2u));
00038   EXPECT_FALSE(eq(1, false));
00039   EXPECT_FALSE(eq(1, 2.));
00040 }
00041 
00042 TEST(Hash, Int32) {
00043   hash_default_hash<int32_t> hash;
00044   auto h = hash(1);
00045   EXPECT_EQ(h, hash(1u));
00046   EXPECT_EQ(h, hash(char{1}));
00047   EXPECT_EQ(h, hash(true));
00048   EXPECT_EQ(h, hash(double{1.1}));
00049   EXPECT_NE(h, hash(2u));
00050   EXPECT_NE(h, hash(char{2}));
00051   EXPECT_NE(h, hash(false));
00052   EXPECT_NE(h, hash(2.));
00053 }
00054 
00055 enum class MyEnum { A, B, C, D };
00056 
00057 TEST(Eq, Enum) {
00058   hash_default_eq<MyEnum> eq;
00059   EXPECT_TRUE(eq(MyEnum::A, MyEnum::A));
00060   EXPECT_FALSE(eq(MyEnum::A, MyEnum::B));
00061 }
00062 
00063 TEST(Hash, Enum) {
00064   hash_default_hash<MyEnum> hash;
00065 
00066   for (MyEnum e : {MyEnum::A, MyEnum::B, MyEnum::C}) {
00067     auto h = hash(e);
00068     EXPECT_EQ(h, hash_default_hash<int>{}(static_cast<int>(e)));
00069     EXPECT_NE(h, hash(MyEnum::D));
00070   }
00071 }
00072 
00073 using StringTypes = ::testing::Types<std::string, absl::string_view>;
00074 
00075 template <class T>
00076 struct EqString : ::testing::Test {
00077   hash_default_eq<T> key_eq;
00078 };
00079 
00080 TYPED_TEST_SUITE(EqString, StringTypes);
00081 
00082 template <class T>
00083 struct HashString : ::testing::Test {
00084   hash_default_hash<T> hasher;
00085 };
00086 
00087 TYPED_TEST_SUITE(HashString, StringTypes);
00088 
00089 TYPED_TEST(EqString, Works) {
00090   auto eq = this->key_eq;
00091   EXPECT_TRUE(eq("a", "a"));
00092   EXPECT_TRUE(eq("a", absl::string_view("a")));
00093   EXPECT_TRUE(eq("a", std::string("a")));
00094   EXPECT_FALSE(eq("a", "b"));
00095   EXPECT_FALSE(eq("a", absl::string_view("b")));
00096   EXPECT_FALSE(eq("a", std::string("b")));
00097 }
00098 
00099 TYPED_TEST(HashString, Works) {
00100   auto hash = this->hasher;
00101   auto h = hash("a");
00102   EXPECT_EQ(h, hash(absl::string_view("a")));
00103   EXPECT_EQ(h, hash(std::string("a")));
00104   EXPECT_NE(h, hash(absl::string_view("b")));
00105   EXPECT_NE(h, hash(std::string("b")));
00106 }
00107 
00108 struct NoDeleter {
00109   template <class T>
00110   void operator()(const T* ptr) const {}
00111 };
00112 
00113 using PointerTypes =
00114     ::testing::Types<const int*, int*, std::unique_ptr<const int>,
00115                      std::unique_ptr<const int, NoDeleter>,
00116                      std::unique_ptr<int>, std::unique_ptr<int, NoDeleter>,
00117                      std::shared_ptr<const int>, std::shared_ptr<int>>;
00118 
00119 template <class T>
00120 struct EqPointer : ::testing::Test {
00121   hash_default_eq<T> key_eq;
00122 };
00123 
00124 TYPED_TEST_SUITE(EqPointer, PointerTypes);
00125 
00126 template <class T>
00127 struct HashPointer : ::testing::Test {
00128   hash_default_hash<T> hasher;
00129 };
00130 
00131 TYPED_TEST_SUITE(HashPointer, PointerTypes);
00132 
00133 TYPED_TEST(EqPointer, Works) {
00134   int dummy;
00135   auto eq = this->key_eq;
00136   auto sptr = std::make_shared<int>();
00137   std::shared_ptr<const int> csptr = sptr;
00138   int* ptr = sptr.get();
00139   const int* cptr = ptr;
00140   std::unique_ptr<int, NoDeleter> uptr(ptr);
00141   std::unique_ptr<const int, NoDeleter> cuptr(ptr);
00142 
00143   EXPECT_TRUE(eq(ptr, cptr));
00144   EXPECT_TRUE(eq(ptr, sptr));
00145   EXPECT_TRUE(eq(ptr, uptr));
00146   EXPECT_TRUE(eq(ptr, csptr));
00147   EXPECT_TRUE(eq(ptr, cuptr));
00148   EXPECT_FALSE(eq(&dummy, cptr));
00149   EXPECT_FALSE(eq(&dummy, sptr));
00150   EXPECT_FALSE(eq(&dummy, uptr));
00151   EXPECT_FALSE(eq(&dummy, csptr));
00152   EXPECT_FALSE(eq(&dummy, cuptr));
00153 }
00154 
00155 TEST(Hash, DerivedAndBase) {
00156   struct Base {};
00157   struct Derived : Base {};
00158 
00159   hash_default_hash<Base*> hasher;
00160 
00161   Base base;
00162   Derived derived;
00163   EXPECT_NE(hasher(&base), hasher(&derived));
00164   EXPECT_EQ(hasher(static_cast<Base*>(&derived)), hasher(&derived));
00165 
00166   auto dp = std::make_shared<Derived>();
00167   EXPECT_EQ(hasher(static_cast<Base*>(dp.get())), hasher(dp));
00168 }
00169 
00170 TEST(Hash, FunctionPointer) {
00171   using Func = int (*)();
00172   hash_default_hash<Func> hasher;
00173   hash_default_eq<Func> eq;
00174 
00175   Func p1 = [] { return 1; }, p2 = [] { return 2; };
00176   EXPECT_EQ(hasher(p1), hasher(p1));
00177   EXPECT_TRUE(eq(p1, p1));
00178 
00179   EXPECT_NE(hasher(p1), hasher(p2));
00180   EXPECT_FALSE(eq(p1, p2));
00181 }
00182 
00183 TYPED_TEST(HashPointer, Works) {
00184   int dummy;
00185   auto hash = this->hasher;
00186   auto sptr = std::make_shared<int>();
00187   std::shared_ptr<const int> csptr = sptr;
00188   int* ptr = sptr.get();
00189   const int* cptr = ptr;
00190   std::unique_ptr<int, NoDeleter> uptr(ptr);
00191   std::unique_ptr<const int, NoDeleter> cuptr(ptr);
00192 
00193   EXPECT_EQ(hash(ptr), hash(cptr));
00194   EXPECT_EQ(hash(ptr), hash(sptr));
00195   EXPECT_EQ(hash(ptr), hash(uptr));
00196   EXPECT_EQ(hash(ptr), hash(csptr));
00197   EXPECT_EQ(hash(ptr), hash(cuptr));
00198   EXPECT_NE(hash(&dummy), hash(cptr));
00199   EXPECT_NE(hash(&dummy), hash(sptr));
00200   EXPECT_NE(hash(&dummy), hash(uptr));
00201   EXPECT_NE(hash(&dummy), hash(csptr));
00202   EXPECT_NE(hash(&dummy), hash(cuptr));
00203 }
00204 
00205 // Cartesian product of (std::string, absl::string_view)
00206 // with (std::string, absl::string_view, const char*).
00207 using StringTypesCartesianProduct = Types<
00208     // clang-format off
00209 
00210     std::pair<absl::string_view, std::string>,
00211     std::pair<absl::string_view, absl::string_view>,
00212     std::pair<absl::string_view, const char*>>;
00213 // clang-format on
00214 
00215 constexpr char kFirstString[] = "abc123";
00216 constexpr char kSecondString[] = "ijk456";
00217 
00218 template <typename T>
00219 struct StringLikeTest : public ::testing::Test {
00220   typename T::first_type a1{kFirstString};
00221   typename T::second_type b1{kFirstString};
00222   typename T::first_type a2{kSecondString};
00223   typename T::second_type b2{kSecondString};
00224   hash_default_eq<typename T::first_type> eq;
00225   hash_default_hash<typename T::first_type> hash;
00226 };
00227 
00228 TYPED_TEST_CASE_P(StringLikeTest);
00229 
00230 TYPED_TEST_P(StringLikeTest, Eq) {
00231   EXPECT_TRUE(this->eq(this->a1, this->b1));
00232   EXPECT_TRUE(this->eq(this->b1, this->a1));
00233 }
00234 
00235 TYPED_TEST_P(StringLikeTest, NotEq) {
00236   EXPECT_FALSE(this->eq(this->a1, this->b2));
00237   EXPECT_FALSE(this->eq(this->b2, this->a1));
00238 }
00239 
00240 TYPED_TEST_P(StringLikeTest, HashEq) {
00241   EXPECT_EQ(this->hash(this->a1), this->hash(this->b1));
00242   EXPECT_EQ(this->hash(this->a2), this->hash(this->b2));
00243   // It would be a poor hash function which collides on these strings.
00244   EXPECT_NE(this->hash(this->a1), this->hash(this->b2));
00245 }
00246 
00247 TYPED_TEST_SUITE(StringLikeTest, StringTypesCartesianProduct);
00248 
00249 }  // namespace
00250 }  // namespace container_internal
00251 }  // namespace absl
00252 
00253 enum Hash : size_t {
00254   kStd = 0x2,       // std::hash
00255 #ifdef _MSC_VER
00256   kExtension = kStd,  // In MSVC, std::hash == ::hash
00257 #else                 // _MSC_VER
00258   kExtension = 0x4,  // ::hash (GCC extension)
00259 #endif                // _MSC_VER
00260 };
00261 
00262 // H is a bitmask of Hash enumerations.
00263 // Hashable<H> is hashable via all means specified in H.
00264 template <int H>
00265 struct Hashable {
00266   static constexpr bool HashableBy(Hash h) { return h & H; }
00267 };
00268 
00269 namespace std {
00270 template <int H>
00271 struct hash<Hashable<H>> {
00272   template <class E = Hashable<H>,
00273             class = typename std::enable_if<E::HashableBy(kStd)>::type>
00274   size_t operator()(E) const {
00275     return kStd;
00276   }
00277 };
00278 }  // namespace std
00279 
00280 namespace absl {
00281 namespace container_internal {
00282 namespace {
00283 
00284 template <class T>
00285 size_t Hash(const T& v) {
00286   return hash_default_hash<T>()(v);
00287 }
00288 
00289 TEST(Delegate, HashDispatch) {
00290   EXPECT_EQ(Hash(kStd), Hash(Hashable<kStd>()));
00291 }
00292 
00293 }  // namespace
00294 }  // namespace container_internal
00295 }  // namespace absl


abseil_cpp
Author(s):
autogenerated on Wed Jun 19 2019 19:42:14