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 #include "test_workarounds.h"
18
19 #if TEST_STD_VER < 11
20 #error this header may only be used in C++11 or newer
21 #endif
22
23 template <class ...Args> struct TypeList;
24
25 // Test that the specified Hash meets the requirements of an enabled hash
26 template <class Hash, class Key, class InputKey = Key>
27 void test_hash_enabled(InputKey const& key = InputKey{});
28
29 template <class T, class InputKey = T>
test_hash_enabled_for_type(InputKey const & key=InputKey{})30 void test_hash_enabled_for_type(InputKey const& key = InputKey{}) {
31 return test_hash_enabled<std::hash<T>, T, InputKey>(key);
32 }
33
34 // Test that the specified Hash meets the requirements of a disabled hash.
35 template <class Hash, class Key>
36 void test_hash_disabled();
37
38 template <class T>
test_hash_disabled_for_type()39 void test_hash_disabled_for_type() {
40 return test_hash_disabled<std::hash<T>, T>();
41 }
42
43 namespace PoisonedHashDetail {
44 enum Enum {};
45 enum EnumClass : bool {};
46 struct Class {};
47 }
48
49 // Each header that declares the template hash provides enabled
50 // specializations of hash for nullptr t and all cv-unqualified
51 // arithmetic, enumeration, and pointer types.
52 using LibraryHashTypes = TypeList<
53 #if TEST_STD_VER > 14
54 decltype(nullptr),
55 #endif
56 bool,
57 char,
58 signed char,
59 unsigned char,
60 wchar_t,
61 #ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
62 char16_t,
63 char32_t,
64 #endif
65 short,
66 unsigned short,
67 int,
68 unsigned int,
69 long,
70 unsigned long,
71 long long,
72 unsigned long long,
73 #ifndef _LIBCPP_HAS_NO_INT128
74 __int128_t,
75 __uint128_t,
76 #endif
77 float,
78 double,
79 long double,
80 #if TEST_STD_VER >= 14
81 // Enum types
82 PoisonedHashDetail::Enum,
83 PoisonedHashDetail::EnumClass,
84 #endif
85 // pointer types
86 void*,
87 void const*,
88 PoisonedHashDetail::Class*
89 >;
90
91
92 // Test that each of the library hash specializations for arithmetic types,
93 // enum types, and pointer types are available and enabled.
94 template <class Types = LibraryHashTypes>
95 void test_library_hash_specializations_available(Types = Types{});
96
97
98 namespace PoisonedHashDetail {
99
100 template <class T, class = typename T::foo_bar_baz>
instantiate(int)101 constexpr bool instantiate(int) { return true; }
instantiate(long)102 template <class> constexpr bool instantiate(long) { return true; }
instantiate()103 template <class T> constexpr bool instantiate() { return instantiate<T>(0); }
104
105 template <class To>
106 struct ConvertibleToSimple {
operator ToPoisonedHashDetail::ConvertibleToSimple107 operator To() const {
108 return To{};
109 }
110 };
111
112 template <class To>
113 struct ConvertibleTo {
114 To to{};
operator To&PoisonedHashDetail::ConvertibleTo115 operator To&() & { return to; }
operator To const&PoisonedHashDetail::ConvertibleTo116 operator To const&() const & { return to; }
operator To&&PoisonedHashDetail::ConvertibleTo117 operator To&&() && { return std::move(to); }
operator To const&&PoisonedHashDetail::ConvertibleTo118 operator To const&&() const && { return std::move(to); }
119 };
120
121 template <class HashExpr,
122 class Res = typename std::result_of<HashExpr>::type>
can_hash(int)123 constexpr bool can_hash(int) {
124 return std::is_same<Res, size_t>::value;
125 }
can_hash(long)126 template <class> constexpr bool can_hash(long) { return false; }
can_hash()127 template <class T> constexpr bool can_hash() { return can_hash<T>(0); }
128
129 } // namespace PoisonedHashDetail
130
131 template <class Hash, class Key, class InputKey>
test_hash_enabled(InputKey const & key)132 void test_hash_enabled(InputKey const& key) {
133 using namespace PoisonedHashDetail;
134
135 static_assert(std::is_destructible<Hash>::value, "");
136 // Enabled hash requirements
137 static_assert(std::is_default_constructible<Hash>::value, "");
138 static_assert(std::is_copy_constructible<Hash>::value, "");
139 static_assert(std::is_move_constructible<Hash>::value, "");
140 static_assert(std::is_copy_assignable<Hash>::value, "");
141 static_assert(std::is_move_assignable<Hash>::value, "");
142
143 #if TEST_STD_VER > 14
144 static_assert(std::is_swappable<Hash>::value, "");
145 #elif defined(_LIBCPP_VERSION)
146 static_assert(std::__is_swappable<Hash>::value, "");
147 #endif
148
149 // Hashable requirements
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 static_assert(!can_hash<Hash(Key&)>(), "");
190 static_assert(!can_hash<Hash(Key const&)>(), "");
191 static_assert(!can_hash<Hash(Key&&)>(), "");
192 static_assert(!can_hash<Hash const&(Key&)>(), "");
193 static_assert(!can_hash<Hash const&(Key const&)>(), "");
194 static_assert(!can_hash<Hash const&(Key&&)>(), "");
195
196 static_assert(!can_hash<Hash(ConvertibleToSimple<Key>&)>(), "");
197 static_assert(!can_hash<Hash(ConvertibleToSimple<Key> const&)>(), "");
198 static_assert(!can_hash<Hash(ConvertibleToSimple<Key>&&)>(), "");
199
200 static_assert(!can_hash<Hash(ConvertibleTo<Key>&)>(), "");
201 static_assert(!can_hash<Hash(ConvertibleTo<Key> const&)>(), "");
202 static_assert(!can_hash<Hash(ConvertibleTo<Key> &&)>(), "");
203 static_assert(!can_hash<Hash(ConvertibleTo<Key> const&&)>(), "");
204 }
205
206
207 template <class First, class ...Rest>
208 struct TypeList<First, Rest...> {
209 template <template <class> class Trait, bool Expect = true>
assertTraitTypeList210 static constexpr bool assertTrait() {
211 static_assert(Trait<First>::value == Expect, "");
212 return TypeList<Rest...>::template assertTrait<Trait, Expect>();
213 }
214
215 template <class Trait>
applyTraitTypeList216 static void applyTrait() {
217 Trait::template apply<First>();
218 TypeList<Rest...>::template applyTrait<Trait>();
219 }
220 };
221
222 template <>
223 struct TypeList<> {
224 template <template <class> class Trait, bool Expect = true>
assertTraitTypeList225 static constexpr bool assertTrait() {
226 return true;
227 }
228 template <class Trait>
applyTraitTypeList229 static void applyTrait() {}
230 };
231
232
233 struct TestLibraryTrait {
234 template <class Type>
applyTestLibraryTrait235 static void apply() { test_hash_enabled<std::hash<Type>, Type>(); }
236 };
237
238 template <class Types>
test_library_hash_specializations_available(Types)239 void test_library_hash_specializations_available(Types) {
240 Types::template applyTrait<TestLibraryTrait >();
241 }
242
243 #endif // SUPPORT_POISONED_HASH_HELPER_HPP
244