hash_generator_testing.h
Go to the documentation of this file.
00001 // Copyright 2018 The Abseil Authors.
00002 //
00003 // Licensed under the Apache License, Version 2.0 (the "License");
00004 // you may not use this file except in compliance with the License.
00005 // You may obtain a copy of the License at
00006 //
00007 //      https://www.apache.org/licenses/LICENSE-2.0
00008 //
00009 // Unless required by applicable law or agreed to in writing, software
00010 // distributed under the License is distributed on an "AS IS" BASIS,
00011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00012 // See the License for the specific language governing permissions and
00013 // limitations under the License.
00014 //
00015 // Generates random values for testing. Specialized only for the few types we
00016 // care about.
00017 
00018 #ifndef ABSL_CONTAINER_INTERNAL_HASH_GENERATOR_TESTING_H_
00019 #define ABSL_CONTAINER_INTERNAL_HASH_GENERATOR_TESTING_H_
00020 
00021 #include <stdint.h>
00022 #include <algorithm>
00023 #include <iosfwd>
00024 #include <random>
00025 #include <tuple>
00026 #include <type_traits>
00027 #include <utility>
00028 
00029 #include "absl/container/internal/hash_policy_testing.h"
00030 #include "absl/meta/type_traits.h"
00031 #include "absl/strings/string_view.h"
00032 
00033 namespace absl {
00034 namespace container_internal {
00035 namespace hash_internal {
00036 namespace generator_internal {
00037 
00038 template <class Container, class = void>
00039 struct IsMap : std::false_type {};
00040 
00041 template <class Map>
00042 struct IsMap<Map, absl::void_t<typename Map::mapped_type>> : std::true_type {};
00043 
00044 }  // namespace generator_internal
00045 
00046 std::mt19937_64* GetSharedRng();
00047 
00048 enum Enum {
00049   kEnumEmpty,
00050   kEnumDeleted,
00051 };
00052 
00053 enum class EnumClass : uint64_t {
00054   kEmpty,
00055   kDeleted,
00056 };
00057 
00058 inline std::ostream& operator<<(std::ostream& o, const EnumClass& ec) {
00059   return o << static_cast<uint64_t>(ec);
00060 }
00061 
00062 template <class T, class E = void>
00063 struct Generator;
00064 
00065 template <class T>
00066 struct Generator<T, typename std::enable_if<std::is_integral<T>::value>::type> {
00067   T operator()() const {
00068     std::uniform_int_distribution<T> dist;
00069     return dist(*GetSharedRng());
00070   }
00071 };
00072 
00073 template <>
00074 struct Generator<Enum> {
00075   Enum operator()() const {
00076     std::uniform_int_distribution<typename std::underlying_type<Enum>::type>
00077         dist;
00078     while (true) {
00079       auto variate = dist(*GetSharedRng());
00080       if (variate != kEnumEmpty && variate != kEnumDeleted)
00081         return static_cast<Enum>(variate);
00082     }
00083   }
00084 };
00085 
00086 template <>
00087 struct Generator<EnumClass> {
00088   EnumClass operator()() const {
00089     std::uniform_int_distribution<
00090         typename std::underlying_type<EnumClass>::type>
00091         dist;
00092     while (true) {
00093       EnumClass variate = static_cast<EnumClass>(dist(*GetSharedRng()));
00094       if (variate != EnumClass::kEmpty && variate != EnumClass::kDeleted)
00095         return static_cast<EnumClass>(variate);
00096     }
00097   }
00098 };
00099 
00100 template <>
00101 struct Generator<std::string> {
00102   std::string operator()() const;
00103 };
00104 
00105 template <>
00106 struct Generator<absl::string_view> {
00107   absl::string_view operator()() const;
00108 };
00109 
00110 template <>
00111 struct Generator<NonStandardLayout> {
00112   NonStandardLayout operator()() const {
00113     return NonStandardLayout(Generator<std::string>()());
00114   }
00115 };
00116 
00117 template <class K, class V>
00118 struct Generator<std::pair<K, V>> {
00119   std::pair<K, V> operator()() const {
00120     return std::pair<K, V>(Generator<typename std::decay<K>::type>()(),
00121                            Generator<typename std::decay<V>::type>()());
00122   }
00123 };
00124 
00125 template <class... Ts>
00126 struct Generator<std::tuple<Ts...>> {
00127   std::tuple<Ts...> operator()() const {
00128     return std::tuple<Ts...>(Generator<typename std::decay<Ts>::type>()()...);
00129   }
00130 };
00131 
00132 template <class U>
00133 struct Generator<U, absl::void_t<decltype(std::declval<U&>().key()),
00134                                 decltype(std::declval<U&>().value())>>
00135     : Generator<std::pair<
00136           typename std::decay<decltype(std::declval<U&>().key())>::type,
00137           typename std::decay<decltype(std::declval<U&>().value())>::type>> {};
00138 
00139 template <class Container>
00140 using GeneratedType = decltype(
00141     std::declval<const Generator<
00142         typename std::conditional<generator_internal::IsMap<Container>::value,
00143                                   typename Container::value_type,
00144                                   typename Container::key_type>::type>&>()());
00145 
00146 }  // namespace hash_internal
00147 }  // namespace container_internal
00148 }  // namespace absl
00149 
00150 #endif  // ABSL_CONTAINER_INTERNAL_HASH_GENERATOR_TESTING_H_


abseil_cpp
Author(s):
autogenerated on Wed Jun 19 2019 19:42:14