1 // Copyright 2018 The Abseil Authors.
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
7 // https://www.apache.org/licenses/LICENSE-2.0
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.
15 // Generates random values for testing. Specialized only for the few types we
18 #ifndef ABSL_CONTAINER_INTERNAL_HASH_GENERATOR_TESTING_H_
19 #define ABSL_CONTAINER_INTERNAL_HASH_GENERATOR_TESTING_H_
26 #include <type_traits>
29 #include "absl/container/internal/hash_policy_testing.h"
30 #include "absl/meta/type_traits.h"
31 #include "absl/strings/string_view.h"
34 namespace container_internal {
35 namespace hash_internal {
36 namespace generator_internal {
38 template <class Container, class = void>
39 struct IsMap : std::false_type {};
42 struct IsMap<Map, absl::void_t<typename Map::mapped_type>> : std::true_type {};
44 } // namespace generator_internal
46 std::mt19937_64* GetSharedRng();
53 enum class EnumClass : uint64_t {
58 inline std::ostream& operator<<(std::ostream& o, const EnumClass& ec) {
59 return o << static_cast<uint64_t>(ec);
62 template <class T, class E = void>
66 struct Generator<T, typename std::enable_if<std::is_integral<T>::value>::type> {
67 T operator()() const {
68 std::uniform_int_distribution<T> dist;
69 return dist(*GetSharedRng());
74 struct Generator<Enum> {
75 Enum operator()() const {
76 std::uniform_int_distribution<typename std::underlying_type<Enum>::type>
79 auto variate = dist(*GetSharedRng());
80 if (variate != kEnumEmpty && variate != kEnumDeleted)
81 return static_cast<Enum>(variate);
87 struct Generator<EnumClass> {
88 EnumClass operator()() const {
89 std::uniform_int_distribution<
90 typename std::underlying_type<EnumClass>::type>
93 EnumClass variate = static_cast<EnumClass>(dist(*GetSharedRng()));
94 if (variate != EnumClass::kEmpty && variate != EnumClass::kDeleted)
95 return static_cast<EnumClass>(variate);
101 struct Generator<std::string> {
102 std::string operator()() const;
106 struct Generator<absl::string_view> {
107 absl::string_view operator()() const;
111 struct Generator<NonStandardLayout> {
112 NonStandardLayout operator()() const {
113 return NonStandardLayout(Generator<std::string>()());
117 template <class K, class V>
118 struct Generator<std::pair<K, V>> {
119 std::pair<K, V> operator()() const {
120 return std::pair<K, V>(Generator<typename std::decay<K>::type>()(),
121 Generator<typename std::decay<V>::type>()());
125 template <class... Ts>
126 struct Generator<std::tuple<Ts...>> {
127 std::tuple<Ts...> operator()() const {
128 return std::tuple<Ts...>(Generator<typename std::decay<Ts>::type>()()...);
133 struct Generator<U, absl::void_t<decltype(std::declval<U&>().key()),
134 decltype(std::declval<U&>().value())>>
135 : Generator<std::pair<
136 typename std::decay<decltype(std::declval<U&>().key())>::type,
137 typename std::decay<decltype(std::declval<U&>().value())>::type>> {};
139 template <class Container>
140 using GeneratedType = decltype(
141 std::declval<const Generator<
142 typename std::conditional<generator_internal::IsMap<Container>::value,
143 typename Container::value_type,
144 typename Container::key_type>::type>&>()());
146 } // namespace hash_internal
147 } // namespace container_internal
150 #endif // ABSL_CONTAINER_INTERNAL_HASH_GENERATOR_TESTING_H_