hash_generator_testing.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. // Generates random values for testing. Specialized only for the few types we
  16. // care about.
  17. #ifndef ABSL_CONTAINER_INTERNAL_HASH_GENERATOR_TESTING_H_
  18. #define ABSL_CONTAINER_INTERNAL_HASH_GENERATOR_TESTING_H_
  19. #include <stdint.h>
  20. #include <algorithm>
  21. #include <cassert>
  22. #include <iosfwd>
  23. #include <random>
  24. #include <tuple>
  25. #include <type_traits>
  26. #include <utility>
  27. #include <vector>
  28. #include "absl/container/internal/hash_policy_testing.h"
  29. #include "absl/memory/memory.h"
  30. #include "absl/meta/type_traits.h"
  31. #include "absl/strings/string_view.h"
  32. namespace absl {
  33. ABSL_NAMESPACE_BEGIN
  34. namespace container_internal {
  35. namespace hash_internal {
  36. namespace generator_internal {
  37. template <class Container, class = void>
  38. struct IsMap : std::false_type {};
  39. template <class Map>
  40. struct IsMap<Map, absl::void_t<typename Map::mapped_type>> : std::true_type {};
  41. } // namespace generator_internal
  42. std::mt19937_64* GetSharedRng();
  43. enum Enum {
  44. kEnumEmpty,
  45. kEnumDeleted,
  46. };
  47. enum class EnumClass : uint64_t {
  48. kEmpty,
  49. kDeleted,
  50. };
  51. inline std::ostream& operator<<(std::ostream& o, const EnumClass& ec) {
  52. return o << static_cast<uint64_t>(ec);
  53. }
  54. template <class T, class E = void>
  55. struct Generator;
  56. template <class T>
  57. struct Generator<T, typename std::enable_if<std::is_integral<T>::value>::type> {
  58. T operator()() const {
  59. std::uniform_int_distribution<T> dist;
  60. return dist(*GetSharedRng());
  61. }
  62. };
  63. template <>
  64. struct Generator<Enum> {
  65. Enum operator()() const {
  66. std::uniform_int_distribution<typename std::underlying_type<Enum>::type>
  67. dist;
  68. while (true) {
  69. auto variate = dist(*GetSharedRng());
  70. if (variate != kEnumEmpty && variate != kEnumDeleted)
  71. return static_cast<Enum>(variate);
  72. }
  73. }
  74. };
  75. template <>
  76. struct Generator<EnumClass> {
  77. EnumClass operator()() const {
  78. std::uniform_int_distribution<
  79. typename std::underlying_type<EnumClass>::type>
  80. dist;
  81. while (true) {
  82. EnumClass variate = static_cast<EnumClass>(dist(*GetSharedRng()));
  83. if (variate != EnumClass::kEmpty && variate != EnumClass::kDeleted)
  84. return static_cast<EnumClass>(variate);
  85. }
  86. }
  87. };
  88. template <>
  89. struct Generator<std::string> {
  90. std::string operator()() const;
  91. };
  92. template <>
  93. struct Generator<absl::string_view> {
  94. absl::string_view operator()() const;
  95. };
  96. template <>
  97. struct Generator<NonStandardLayout> {
  98. NonStandardLayout operator()() const {
  99. return NonStandardLayout(Generator<std::string>()());
  100. }
  101. };
  102. template <class K, class V>
  103. struct Generator<std::pair<K, V>> {
  104. std::pair<K, V> operator()() const {
  105. return std::pair<K, V>(Generator<typename std::decay<K>::type>()(),
  106. Generator<typename std::decay<V>::type>()());
  107. }
  108. };
  109. template <class... Ts>
  110. struct Generator<std::tuple<Ts...>> {
  111. std::tuple<Ts...> operator()() const {
  112. return std::tuple<Ts...>(Generator<typename std::decay<Ts>::type>()()...);
  113. }
  114. };
  115. template <class T>
  116. struct Generator<std::unique_ptr<T>> {
  117. std::unique_ptr<T> operator()() const {
  118. return absl::make_unique<T>(Generator<T>()());
  119. }
  120. };
  121. template <class U>
  122. struct Generator<U, absl::void_t<decltype(std::declval<U&>().key()),
  123. decltype(std::declval<U&>().value())>>
  124. : Generator<std::pair<
  125. typename std::decay<decltype(std::declval<U&>().key())>::type,
  126. typename std::decay<decltype(std::declval<U&>().value())>::type>> {};
  127. template <class Container>
  128. using GeneratedType = decltype(
  129. std::declval<const Generator<
  130. typename std::conditional<generator_internal::IsMap<Container>::value,
  131. typename Container::value_type,
  132. typename Container::key_type>::type>&>()());
  133. // Naive wrapper that performs a linear search of previous values.
  134. // Beware this is O(SQR), which is reasonable for smaller kMaxValues.
  135. template <class T, size_t kMaxValues = 64, class E = void>
  136. struct UniqueGenerator {
  137. Generator<T, E> gen;
  138. std::vector<T> values;
  139. T operator()() {
  140. assert(values.size() < kMaxValues);
  141. for (;;) {
  142. T value = gen();
  143. if (std::find(values.begin(), values.end(), value) == values.end()) {
  144. values.push_back(value);
  145. return value;
  146. }
  147. }
  148. }
  149. };
  150. } // namespace hash_internal
  151. } // namespace container_internal
  152. ABSL_NAMESPACE_END
  153. } // namespace absl
  154. #endif // ABSL_CONTAINER_INTERNAL_HASH_GENERATOR_TESTING_H_