• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // -*- C++ -*-
2 //===----------------------------------------------------------------------===//
3 //
4 //                     The LLVM Compiler Infrastructure
5 //
6 // This file is dual licensed under the MIT and the University of Illinois Open
7 // Source Licenses. See LICENSE.TXT for details.
8 //
9 //===----------------------------------------------------------------------===//
10 #ifndef SUPPORT_POISONED_HASH_HELPER_HPP
11 #define SUPPORT_POISONED_HASH_HELPER_HPP
12 
13 #include <type_traits>
14 #include <cassert>
15 
16 #include "test_macros.h"
17 
18 #if TEST_STD_VER < 11
19 #error this header may only be used in C++11 or newer
20 #endif
21 
22 template <class ...Args> struct TypeList;
23 
24 // Test that the specified Hash meets the requirements of an enabled hash
25 template <class Hash, class Key, class InputKey = Key>
26 void test_hash_enabled(InputKey const& key = InputKey{});
27 
28 template <class T, class InputKey = T>
test_hash_enabled_for_type(InputKey const & key=InputKey{})29 void test_hash_enabled_for_type(InputKey const& key = InputKey{}) {
30   return test_hash_enabled<std::hash<T>, T, InputKey>(key);
31 }
32 
33 // Test that the specified Hash meets the requirements of a disabled hash.
34 template <class Hash, class Key>
35 void test_hash_disabled();
36 
37 template <class T>
test_hash_disabled_for_type()38 void test_hash_disabled_for_type() {
39   return test_hash_disabled<std::hash<T>, T>();
40 }
41 
42 namespace PoisonedHashDetail {
43   enum Enum {};
44   enum EnumClass : bool {};
45   struct Class {};
46 }
47 
48 // Each header that declares the template hash provides enabled
49 // specializations of hash for nullptr t and all cv-unqualified
50 // arithmetic, enumeration, and pointer types.
51 using LibraryHashTypes = TypeList<
52 #if TEST_STD_VER > 14
53       decltype(nullptr),
54 #endif
55       bool,
56       char,
57       signed char,
58       unsigned char,
59       wchar_t,
60 #ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
61       char16_t,
62       char32_t,
63 #endif
64       short,
65       unsigned short,
66       int,
67       unsigned int,
68       long,
69       unsigned long,
70       long long,
71       unsigned long long,
72 #ifndef _LIBCPP_HAS_NO_INT128
73       __int128_t,
74       __uint128_t,
75 #endif
76       float,
77       double,
78       long double,
79 #if TEST_STD_VER >= 14
80       // Enum types
81       PoisonedHashDetail::Enum,
82       PoisonedHashDetail::EnumClass,
83 #endif
84       // pointer types
85       void*,
86       void const*,
87       PoisonedHashDetail::Class*
88     >;
89 
90 
91 // Test that each of the library hash specializations for  arithmetic types,
92 // enum types, and pointer types are available and enabled.
93 template <class Types = LibraryHashTypes>
94 void test_library_hash_specializations_available(Types = Types{});
95 
96 
97 namespace PoisonedHashDetail {
98 
99 template <class T, class = typename T::foo_bar_baz>
instantiate(int)100 constexpr bool instantiate(int) { return true; }
instantiate(long)101 template <class> constexpr bool instantiate(long) { return true; }
instantiate()102 template <class T> constexpr bool instantiate() { return instantiate<T>(0); }
103 
104 template <class To>
105 struct ConvertibleToSimple {
operator ToPoisonedHashDetail::ConvertibleToSimple106   operator To() const {
107     return To{};
108   }
109 };
110 
111 template <class To>
112 struct ConvertibleTo {
113   To to{};
operator To&PoisonedHashDetail::ConvertibleTo114   operator To&() & { return to; }
operator To const&PoisonedHashDetail::ConvertibleTo115   operator To const&() const & { return to; }
operator To&&PoisonedHashDetail::ConvertibleTo116   operator To&&() && { return std::move(to); }
operator To const&&PoisonedHashDetail::ConvertibleTo117   operator To const&&() const && { return std::move(to); }
118 };
119 
120 template <class HashExpr,
121          class Res = typename std::result_of<HashExpr>::type>
can_hash(int)122 constexpr bool can_hash(int) {
123   return std::is_same<Res, size_t>::value;
124 }
can_hash(long)125 template <class> constexpr bool can_hash(long) { return false; }
can_hash()126 template <class T> constexpr bool can_hash() { return can_hash<T>(0); }
127 
128 } // namespace PoisonedHashDetail
129 
130 template <class Hash, class Key, class InputKey>
test_hash_enabled(InputKey const & key)131 void test_hash_enabled(InputKey const& key) {
132   using namespace PoisonedHashDetail;
133 
134   static_assert(std::is_destructible<Hash>::value, "");
135   // Enabled hash requirements
136   static_assert(std::is_default_constructible<Hash>::value, "");
137   static_assert(std::is_copy_constructible<Hash>::value, "");
138   static_assert(std::is_move_constructible<Hash>::value, "");
139   static_assert(std::is_copy_assignable<Hash>::value, "");
140   static_assert(std::is_move_assignable<Hash>::value, "");
141 
142 #if TEST_STD_VER > 14
143   static_assert(std::is_swappable<Hash>::value, "");
144 #elif defined(_LIBCPP_VERSION)
145   static_assert(std::__is_swappable<Hash>::value, "");
146 #endif
147 
148   // Hashable requirements
149   using CKey = ConvertibleTo<Key>;
150   static_assert(can_hash<Hash(Key&)>(), "");
151   static_assert(can_hash<Hash(Key const&)>(), "");
152   static_assert(can_hash<Hash(Key&&)>(), "");
153   static_assert(can_hash<Hash const&(Key&)>(), "");
154   static_assert(can_hash<Hash const&(Key const&)>(), "");
155   static_assert(can_hash<Hash const&(Key&&)>(), "");
156 
157   static_assert(can_hash<Hash(ConvertibleToSimple<Key>&)>(), "");
158   static_assert(can_hash<Hash(ConvertibleToSimple<Key> const&)>(), "");
159   static_assert(can_hash<Hash(ConvertibleToSimple<Key>&&)>(), "");
160 
161   static_assert(can_hash<Hash(ConvertibleTo<Key>&)>(), "");
162   static_assert(can_hash<Hash(ConvertibleTo<Key> const&)>(), "");
163   static_assert(can_hash<Hash(ConvertibleTo<Key> &&)>(), "");
164   static_assert(can_hash<Hash(ConvertibleTo<Key> const&&)>(), "");
165 
166   const Hash h{};
167   assert(h(key) == h(key));
168 
169 }
170 
171 template <class Hash, class Key>
test_hash_disabled()172 void test_hash_disabled() {
173   using namespace PoisonedHashDetail;
174 
175   // Disabled hash requirements
176   static_assert(!std::is_default_constructible<Hash>::value, "");
177   static_assert(!std::is_copy_constructible<Hash>::value, "");
178   static_assert(!std::is_move_constructible<Hash>::value, "");
179   static_assert(!std::is_copy_assignable<Hash>::value, "");
180   static_assert(!std::is_move_assignable<Hash>::value, "");
181 
182   static_assert(!std::is_function<
183       typename std::remove_pointer<
184           typename std::remove_reference<Hash>::type
185       >::type
186     >::value, "");
187 
188   // Hashable requirements
189   using CKey = ConvertibleTo<Key>;
190   static_assert(!can_hash<Hash(Key&)>(), "");
191   static_assert(!can_hash<Hash(Key const&)>(), "");
192   static_assert(!can_hash<Hash(Key&&)>(), "");
193   static_assert(!can_hash<Hash const&(Key&)>(), "");
194   static_assert(!can_hash<Hash const&(Key const&)>(), "");
195   static_assert(!can_hash<Hash const&(Key&&)>(), "");
196 
197   static_assert(!can_hash<Hash(ConvertibleToSimple<Key>&)>(), "");
198   static_assert(!can_hash<Hash(ConvertibleToSimple<Key> const&)>(), "");
199   static_assert(!can_hash<Hash(ConvertibleToSimple<Key>&&)>(), "");
200 
201   static_assert(!can_hash<Hash(ConvertibleTo<Key>&)>(), "");
202   static_assert(!can_hash<Hash(ConvertibleTo<Key> const&)>(), "");
203   static_assert(!can_hash<Hash(ConvertibleTo<Key> &&)>(), "");
204   static_assert(!can_hash<Hash(ConvertibleTo<Key> const&&)>(), "");
205 }
206 
207 
208 template <class First, class ...Rest>
209 struct TypeList<First, Rest...> {
210   template <template <class> class Trait, bool Expect = true>
assertTraitTypeList211   static constexpr bool assertTrait() {
212     static_assert(Trait<First>::value == Expect, "");
213     return TypeList<Rest...>::template assertTrait<Trait, Expect>();
214   }
215 
216   template <class Trait>
applyTraitTypeList217   static void applyTrait() {
218     Trait::template apply<First>();
219     TypeList<Rest...>::template applyTrait<Trait>();
220   }
221 };
222 
223 template <>
224 struct TypeList<> {
225   template <template <class> class Trait, bool Expect = true>
assertTraitTypeList226   static constexpr bool assertTrait() {
227     return true;
228   }
229   template <class Trait>
applyTraitTypeList230   static void applyTrait() {}
231 };
232 
233 
234 struct TestLibraryTrait {
235     template <class Type>
applyTestLibraryTrait236     static void apply() { test_hash_enabled<std::hash<Type>, Type>(); }
237 };
238 
239 template <class Types>
test_library_hash_specializations_available(Types)240 void test_library_hash_specializations_available(Types) {
241   Types::template applyTrait<TestLibraryTrait >();
242 }
243 
244 #endif // SUPPORT_POISONED_HASH_HELPER_HPP
245