hash_policy_testing.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. // Copyright 2018 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. //
  15. // Utilities to help tests verify that hash tables properly handle stateful
  16. // allocators and hash functions.
  17. #ifndef Y_ABSL_CONTAINER_INTERNAL_HASH_POLICY_TESTING_H_
  18. #define Y_ABSL_CONTAINER_INTERNAL_HASH_POLICY_TESTING_H_
  19. #include <cstdlib>
  20. #include <limits>
  21. #include <memory>
  22. #include <ostream>
  23. #include <type_traits>
  24. #include <utility>
  25. #include <vector>
  26. #include "y_absl/hash/hash.h"
  27. #include "y_absl/strings/string_view.h"
  28. namespace y_absl {
  29. Y_ABSL_NAMESPACE_BEGIN
  30. namespace container_internal {
  31. namespace hash_testing_internal {
  32. template <class Derived>
  33. struct WithId {
  34. WithId() : id_(next_id<Derived>()) {}
  35. WithId(const WithId& that) : id_(that.id_) {}
  36. WithId(WithId&& that) : id_(that.id_) { that.id_ = 0; }
  37. WithId& operator=(const WithId& that) {
  38. id_ = that.id_;
  39. return *this;
  40. }
  41. WithId& operator=(WithId&& that) {
  42. id_ = that.id_;
  43. that.id_ = 0;
  44. return *this;
  45. }
  46. size_t id() const { return id_; }
  47. friend bool operator==(const WithId& a, const WithId& b) {
  48. return a.id_ == b.id_;
  49. }
  50. friend bool operator!=(const WithId& a, const WithId& b) { return !(a == b); }
  51. protected:
  52. explicit WithId(size_t id) : id_(id) {}
  53. private:
  54. size_t id_;
  55. template <class T>
  56. static size_t next_id() {
  57. // 0 is reserved for moved from state.
  58. static size_t gId = 1;
  59. return gId++;
  60. }
  61. };
  62. } // namespace hash_testing_internal
  63. struct NonStandardLayout {
  64. NonStandardLayout() {}
  65. explicit NonStandardLayout(TString s) : value(std::move(s)) {}
  66. virtual ~NonStandardLayout() {}
  67. friend bool operator==(const NonStandardLayout& a,
  68. const NonStandardLayout& b) {
  69. return a.value == b.value;
  70. }
  71. friend bool operator!=(const NonStandardLayout& a,
  72. const NonStandardLayout& b) {
  73. return a.value != b.value;
  74. }
  75. template <typename H>
  76. friend H AbslHashValue(H h, const NonStandardLayout& v) {
  77. return H::combine(std::move(h), v.value);
  78. }
  79. TString value;
  80. };
  81. struct StatefulTestingHash
  82. : y_absl::container_internal::hash_testing_internal::WithId<
  83. StatefulTestingHash> {
  84. template <class T>
  85. size_t operator()(const T& t) const {
  86. return y_absl::Hash<T>{}(t);
  87. }
  88. };
  89. struct StatefulTestingEqual
  90. : y_absl::container_internal::hash_testing_internal::WithId<
  91. StatefulTestingEqual> {
  92. template <class T, class U>
  93. bool operator()(const T& t, const U& u) const {
  94. return t == u;
  95. }
  96. };
  97. // It is expected that Alloc() == Alloc() for all allocators so we cannot use
  98. // WithId base. We need to explicitly assign ids.
  99. template <class T = int>
  100. struct Alloc : std::allocator<T> {
  101. using propagate_on_container_swap = std::true_type;
  102. // Using old paradigm for this to ensure compatibility.
  103. explicit Alloc(size_t id = 0) : id_(id) {}
  104. Alloc(const Alloc&) = default;
  105. Alloc& operator=(const Alloc&) = default;
  106. template <class U>
  107. Alloc(const Alloc<U>& that) : std::allocator<T>(that), id_(that.id()) {}
  108. template <class U>
  109. struct rebind {
  110. using other = Alloc<U>;
  111. };
  112. size_t id() const { return id_; }
  113. friend bool operator==(const Alloc& a, const Alloc& b) {
  114. return a.id_ == b.id_;
  115. }
  116. friend bool operator!=(const Alloc& a, const Alloc& b) { return !(a == b); }
  117. private:
  118. size_t id_ = (std::numeric_limits<size_t>::max)();
  119. };
  120. template <class Map>
  121. auto items(const Map& m) -> std::vector<
  122. std::pair<typename Map::key_type, typename Map::mapped_type>> {
  123. using std::get;
  124. std::vector<std::pair<typename Map::key_type, typename Map::mapped_type>> res;
  125. res.reserve(m.size());
  126. for (const auto& v : m) res.emplace_back(get<0>(v), get<1>(v));
  127. return res;
  128. }
  129. template <class Set>
  130. auto keys(const Set& s)
  131. -> std::vector<typename std::decay<typename Set::key_type>::type> {
  132. std::vector<typename std::decay<typename Set::key_type>::type> res;
  133. res.reserve(s.size());
  134. for (const auto& v : s) res.emplace_back(v);
  135. return res;
  136. }
  137. } // namespace container_internal
  138. Y_ABSL_NAMESPACE_END
  139. } // namespace y_absl
  140. // Y_ABSL_UNORDERED_SUPPORTS_ALLOC_CTORS is false for glibcxx versions
  141. // where the unordered containers are missing certain constructors that
  142. // take allocator arguments. This test is defined ad-hoc for the platforms
  143. // we care about (notably Crosstool 17) because libstdcxx's useless
  144. // versioning scheme precludes a more principled solution.
  145. // From GCC-4.9 Changelog: (src: https://gcc.gnu.org/gcc-4.9/changes.html)
  146. // "the unordered associative containers in <unordered_map> and <unordered_set>
  147. // meet the allocator-aware container requirements;"
  148. #if defined(__GLIBCXX__) && __GLIBCXX__ <= 20140425
  149. #define Y_ABSL_UNORDERED_SUPPORTS_ALLOC_CTORS 0
  150. #else
  151. #define Y_ABSL_UNORDERED_SUPPORTS_ALLOC_CTORS 1
  152. #endif
  153. #endif // Y_ABSL_CONTAINER_INTERNAL_HASH_POLICY_TESTING_H_