Debian Codegraph @

tests/hash_generator_testing.h

raw
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
18 #ifndef PHMAP_PRIV_HASH_GENERATOR_TESTING_H_
19 #define PHMAP_PRIV_HASH_GENERATOR_TESTING_H_
20
21 #include <stdint.h>
22 #include <algorithm>
23 #include <iosfwd>
24 #include <random>
25 #include <tuple>
26 #include <type_traits>
27 #include <utility>
28 #include <string>
29 #include <deque>
30 #include <functional>
31
32 #if PHMAP_HAVE_STD_STRING_VIEW
33 #include <string_view>
34 #endif
35
36 #include "hash_policy_testing.h"
37
38 namespace phmap {
39 namespace priv {
40 namespace hash_internal {
41 namespace generator_internal {
42
43 template <class Container, class = void>
44 struct IsMap : std::false_type {};
45
46 template <class Map>
47 struct IsMap<Map, phmap::void_t<typename Map::mapped_type>> : std::true_type {};
48
49 } // namespace generator_internal
50
51 namespace
52 {
53 class RandomDeviceSeedSeq {
54 public:
55 using result_type = typename std::random_device::result_type;
56
57 template <class Iterator>
58 void generate(Iterator start, Iterator end) {
59 while (start != end) {
60 *start = gen_();
61 ++start;
62 }
63 }
64
65 private:
66 std::random_device gen_;
67 };
68 } // namespace
69
70 std::mt19937_64* GetSharedRng(); // declaration
71
72 std::mt19937_64* GetSharedRng() {
73 RandomDeviceSeedSeq seed_seq;
74 static auto* rng = new std::mt19937_64(seed_seq);
75 return rng;
76 }
77
78
79 enum Enum {
80 kEnumEmpty,
81 kEnumDeleted,
82 };
83
84 enum class EnumClass : uint64_t {
85 kEmpty,
86 kDeleted,
87 };
88
89 inline std::ostream& operator<<(std::ostream& o, const EnumClass& ec) {
90 return o << static_cast<uint64_t>(ec);
91 }
92
93 template <class T, class E = void>
94 struct Generator;
95
96 template <class T>
97 struct Generator<T, typename std::enable_if<std::is_integral<T>::value>::type> {
98 T operator()() const {
99 std::uniform_int_distribution<T> dist;
100 return dist(*GetSharedRng());
101 }
102 };
103
104 template <>
105 struct Generator<Enum> {
106 Enum operator()() const {
107 std::uniform_int_distribution<typename std::underlying_type<Enum>::type> dist;
108
109 while (true) {
110 auto variate = dist(*GetSharedRng());
111 if (variate != kEnumEmpty && variate != kEnumDeleted)
112 return static_cast<Enum>(variate);
113 }
114 }
115 };
116
117 template <>
118 struct Generator<EnumClass> {
119 EnumClass operator()() const {
120 std::uniform_int_distribution<
121 typename std::underlying_type<EnumClass>::type> dist;
122 while (true) {
123 EnumClass variate = static_cast<EnumClass>(dist(*GetSharedRng()));
124 if (variate != EnumClass::kEmpty && variate != EnumClass::kDeleted)
125 return static_cast<EnumClass>(variate);
126 }
127 }
128 };
129
130 template <>
131 struct Generator<std::string> {
132 std::string operator()() const {
133 // NOLINTNEXTLINE(runtime/int)
134 std::uniform_int_distribution<short> chars(0x20, 0x7E);
135 std::string res;
136 res.resize(32);
137 std::generate(res.begin(), res.end(),
138 [&]() { return (char)chars(*GetSharedRng()); });
139 return res;
140 }
141 };
142
143 #if PHMAP_HAVE_STD_STRING_VIEW
144 template <>
145 struct Generator<std::string_view> {
146 std::string_view operator()() const {
147 static auto* arena = new std::deque<std::string>();
148 // NOLINTNEXTLINE(runtime/int)
149 std::uniform_int_distribution<short> chars(0x20, 0x7E);
150 arena->emplace_back();
151 auto& res = arena->back();
152 res.resize(32);
153 std::generate(res.begin(), res.end(),
154 [&]() { return (char)chars(*GetSharedRng()); });
155 return res;
156 }
157 };
158 #endif
159
160 template <>
161 struct Generator<NonStandardLayout> {
162 NonStandardLayout operator()() const {
163 return NonStandardLayout(Generator<std::string>()());
164 }
165 };
166
167 template <class K, class V>
168 struct Generator<std::pair<K, V>> {
169 std::pair<K, V> operator()() const {
170 return std::pair<K, V>(Generator<typename std::decay<K>::type>()(),
171 Generator<typename std::decay<V>::type>()());
172 }
173 };
174
175 template <class... Ts>
176 struct Generator<std::tuple<Ts...>> {
177 std::tuple<Ts...> operator()() const {
178 return std::tuple<Ts...>(Generator<typename std::decay<Ts>::type>()()...);
179 }
180 };
181
182 template <class U>
183 struct Generator<U, phmap::void_t<decltype(std::declval<U&>().key()),
184 decltype(std::declval<U&>().value())>>
185 : Generator<std::pair<
186 typename std::decay<decltype(std::declval<U&>().key())>::type,
187 typename std::decay<decltype(std::declval<U&>().value())>::type>> {};
188
189 template <class Container>
190 using GeneratedType = decltype(
191 std::declval<const Generator<
192 typename std::conditional<generator_internal::IsMap<Container>::value,
193 typename Container::value_type,
194 typename Container::key_type>::type>&>()());
195
196 } // namespace hash_internal
197 } // namespace priv
198 } // namespace phmap
199
200 namespace std
201 {
202 using phmap::priv::hash_internal::EnumClass;
203 using phmap::priv::hash_internal::Enum;
204
205 template<>
206 struct hash<EnumClass>
207 {
208 std::size_t operator()(EnumClass const &p) const { return (std::size_t)p; }
209 };
210 template<>
211 struct hash<Enum>
212 {
213 std::size_t operator()(Enum const &p) const { return (std::size_t)p; }
214 };
215 }
216
217
218 #endif // PHMAP_PRIV_HASH_GENERATOR_TESTING_H_