hash_generator_testing.h
Go to the documentation of this file.
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 ABSL_CONTAINER_INTERNAL_HASH_GENERATOR_TESTING_H_
19 #define ABSL_CONTAINER_INTERNAL_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 
30 #include "absl/meta/type_traits.h"
32 
33 namespace absl {
34 namespace container_internal {
35 namespace hash_internal {
36 namespace generator_internal {
37 
38 template <class Container, class = void>
39 struct IsMap : std::false_type {};
40 
41 template <class Map>
42 struct IsMap<Map, absl::void_t<typename Map::mapped_type>> : std::true_type {};
43 
44 } // namespace generator_internal
45 
46 std::mt19937_64* GetSharedRng();
47 
48 enum Enum {
51 };
52 
53 enum class EnumClass : uint64_t {
54  kEmpty,
55  kDeleted,
56 };
57 
58 inline std::ostream& operator<<(std::ostream& o, const EnumClass& ec) {
59  return o << static_cast<uint64_t>(ec);
60 }
61 
62 template <class T, class E = void>
63 struct Generator;
64 
65 template <class T>
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());
70  }
71 };
72 
73 template <>
74 struct Generator<Enum> {
75  Enum operator()() const {
76  std::uniform_int_distribution<typename std::underlying_type<Enum>::type>
77  dist;
78  while (true) {
79  auto variate = dist(*GetSharedRng());
80  if (variate != kEnumEmpty && variate != kEnumDeleted)
81  return static_cast<Enum>(variate);
82  }
83  }
84 };
85 
86 template <>
89  std::uniform_int_distribution<
90  typename std::underlying_type<EnumClass>::type>
91  dist;
92  while (true) {
93  EnumClass variate = static_cast<EnumClass>(dist(*GetSharedRng()));
94  if (variate != EnumClass::kEmpty && variate != EnumClass::kDeleted)
95  return static_cast<EnumClass>(variate);
96  }
97  }
98 };
99 
100 template <>
101 struct Generator<std::string> {
102  std::string operator()() const;
103 };
104 
105 template <>
107  absl::string_view operator()() const;
108 };
109 
110 template <>
114  }
115 };
116 
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>()(),
122  }
123 };
124 
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>()()...);
129  }
130 };
131 
132 template <class U>
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>> {};
138 
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>&>()());
145 
146 } // namespace hash_internal
147 } // namespace container_internal
148 } // namespace absl
149 
150 #endif // ABSL_CONTAINER_INTERNAL_HASH_GENERATOR_TESTING_H_
Definition: algorithm.h:29
std::pair< std::string, std::string > pair
UnboundConversion o
Definition: parser_test.cc:86
typename type_traits_internal::VoidTImpl< Ts... >::type void_t
Definition: type_traits.h:171
std::ostream & operator<<(std::ostream &o, const EnumClass &ec)
decltype(std::declval< const Generator< typename std::conditional< generator_internal::IsMap< Container >::value, typename Container::value_type, typename Container::key_type >::type > & >()()) GeneratedType


abseil_cpp
Author(s):
autogenerated on Mon Feb 28 2022 21:31:18