hash_test.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright 2023 The Abseil Authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // Common code shared between absl/hash/hash_test.cc and
  15. // absl/hash/hash_instantiated_test.cc.
  16. #ifndef ABSL_HASH_INTERNAL_HASH_TEST_H_
  17. #define ABSL_HASH_INTERNAL_HASH_TEST_H_
  18. #include <type_traits>
  19. #include <utility>
  20. #include "absl/base/config.h"
  21. #include "absl/hash/hash.h"
  22. namespace absl {
  23. ABSL_NAMESPACE_BEGIN
  24. namespace hash_test_internal {
  25. // Utility wrapper of T for the purposes of testing the `AbslHash` type erasure
  26. // mechanism. `TypeErasedValue<T>` can be constructed with a `T`, and can
  27. // be compared and hashed. However, all hashing goes through the hashing
  28. // type-erasure framework.
  29. template <typename T>
  30. class TypeErasedValue {
  31. public:
  32. TypeErasedValue() = default;
  33. TypeErasedValue(const TypeErasedValue&) = default;
  34. TypeErasedValue(TypeErasedValue&&) = default;
  35. explicit TypeErasedValue(const T& n) : n_(n) {}
  36. template <typename H>
  37. friend H AbslHashValue(H hash_state, const TypeErasedValue& v) {
  38. v.HashValue(absl::HashState::Create(&hash_state));
  39. return hash_state;
  40. }
  41. void HashValue(absl::HashState state) const {
  42. absl::HashState::combine(std::move(state), n_);
  43. }
  44. bool operator==(const TypeErasedValue& rhs) const { return n_ == rhs.n_; }
  45. bool operator!=(const TypeErasedValue& rhs) const { return !(*this == rhs); }
  46. private:
  47. T n_;
  48. };
  49. // A TypeErasedValue refinement, for containers. It exposes the wrapped
  50. // `value_type` and is constructible from an initializer list.
  51. template <typename T>
  52. class TypeErasedContainer : public TypeErasedValue<T> {
  53. public:
  54. using value_type = typename T::value_type;
  55. TypeErasedContainer() = default;
  56. TypeErasedContainer(const TypeErasedContainer&) = default;
  57. TypeErasedContainer(TypeErasedContainer&&) = default;
  58. explicit TypeErasedContainer(const T& n) : TypeErasedValue<T>(n) {}
  59. TypeErasedContainer(std::initializer_list<value_type> init_list)
  60. : TypeErasedContainer(T(init_list.begin(), init_list.end())) {}
  61. // one-argument constructor of value type T, to appease older toolchains that
  62. // get confused by one-element initializer lists in some contexts
  63. explicit TypeErasedContainer(const value_type& v)
  64. : TypeErasedContainer(T(&v, &v + 1)) {}
  65. };
  66. // Helper trait to verify if T is hashable. We use absl::Hash's poison status to
  67. // detect it.
  68. template <typename T>
  69. using is_hashable = std::is_default_constructible<absl::Hash<T>>;
  70. } // namespace hash_test_internal
  71. ABSL_NAMESPACE_END
  72. } // namespace absl
  73. #endif // ABSL_HASH_INTERNAL_HASH_TEST_H_