1 // -*- C++ -*-
2 //===----------------------------------------------------------------------===//
3 //
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //
8 //===----------------------------------------------------------------------===//
9 #ifndef SUPPORT_POISONED_HASH_HELPER_H
10 #define SUPPORT_POISONED_HASH_HELPER_H
11 
12 #include <type_traits>
13 #include <cassert>
14 
15 #include "test_macros.h"
16 #include "test_workarounds.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>
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 {
ToConvertibleToSimple106   operator To() const {
107     return To{};
108   }
109 };
110 
111 template <class To>
112 struct ConvertibleTo {
113   To to{};
114   operator To&() & { return to; }
115   operator To const&() const & { return to; }
116   operator To&&() && { return std::move(to); }
117   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   static_assert(can_hash<Hash(Key&)>(), "");
150   static_assert(can_hash<Hash(Key const&)>(), "");
151   static_assert(can_hash<Hash(Key&&)>(), "");
152   static_assert(can_hash<Hash const&(Key&)>(), "");
153   static_assert(can_hash<Hash const&(Key const&)>(), "");
154   static_assert(can_hash<Hash const&(Key&&)>(), "");
155 
156   static_assert(can_hash<Hash(ConvertibleToSimple<Key>&)>(), "");
157   static_assert(can_hash<Hash(ConvertibleToSimple<Key> const&)>(), "");
158   static_assert(can_hash<Hash(ConvertibleToSimple<Key>&&)>(), "");
159 
160   static_assert(can_hash<Hash(ConvertibleTo<Key>&)>(), "");
161   static_assert(can_hash<Hash(ConvertibleTo<Key> const&)>(), "");
162   static_assert(can_hash<Hash(ConvertibleTo<Key> &&)>(), "");
163   static_assert(can_hash<Hash(ConvertibleTo<Key> const&&)>(), "");
164 
165   const Hash h{};
166   assert(h(key) == h(key));
167 
168 }
169 
170 template <class Hash, class Key>
test_hash_disabled()171 void test_hash_disabled() {
172   using namespace PoisonedHashDetail;
173 
174   // Disabled hash requirements
175   static_assert(!std::is_default_constructible<Hash>::value, "");
176   static_assert(!std::is_copy_constructible<Hash>::value, "");
177   static_assert(!std::is_move_constructible<Hash>::value, "");
178   static_assert(!std::is_copy_assignable<Hash>::value, "");
179   static_assert(!std::is_move_assignable<Hash>::value, "");
180 
181   static_assert(!std::is_function<
182       typename std::remove_pointer<
183           typename std::remove_reference<Hash>::type
184       >::type
185     >::value, "");
186 
187   // Hashable requirements
188   static_assert(!can_hash<Hash(Key&)>(), "");
189   static_assert(!can_hash<Hash(Key const&)>(), "");
190   static_assert(!can_hash<Hash(Key&&)>(), "");
191   static_assert(!can_hash<Hash const&(Key&)>(), "");
192   static_assert(!can_hash<Hash const&(Key const&)>(), "");
193   static_assert(!can_hash<Hash const&(Key&&)>(), "");
194 
195   static_assert(!can_hash<Hash(ConvertibleToSimple<Key>&)>(), "");
196   static_assert(!can_hash<Hash(ConvertibleToSimple<Key> const&)>(), "");
197   static_assert(!can_hash<Hash(ConvertibleToSimple<Key>&&)>(), "");
198 
199   static_assert(!can_hash<Hash(ConvertibleTo<Key>&)>(), "");
200   static_assert(!can_hash<Hash(ConvertibleTo<Key> const&)>(), "");
201   static_assert(!can_hash<Hash(ConvertibleTo<Key> &&)>(), "");
202   static_assert(!can_hash<Hash(ConvertibleTo<Key> const&&)>(), "");
203 }
204 
205 
206 template <class First, class ...Rest>
207 struct TypeList<First, Rest...> {
208   template <template <class> class Trait, bool Expect = true>
209   static constexpr bool assertTrait() {
210     static_assert(Trait<First>::value == Expect, "");
211     return TypeList<Rest...>::template assertTrait<Trait, Expect>();
212   }
213 
214   template <class Trait>
215   static void applyTrait() {
216     Trait::template apply<First>();
217     TypeList<Rest...>::template applyTrait<Trait>();
218   }
219 };
220 
221 template <>
222 struct TypeList<> {
223   template <template <class> class Trait, bool Expect = true>
224   static constexpr bool assertTrait() {
225     return true;
226   }
227   template <class Trait>
228   static void applyTrait() {}
229 };
230 
231 
232 struct TestLibraryTrait {
233     template <class Type>
234     static void apply() { test_hash_enabled<std::hash<Type>, Type>(); }
235 };
236 
237 template <class Types>
238 void test_library_hash_specializations_available(Types) {
239   Types::template applyTrait<TestLibraryTrait >();
240 }
241 
242 #endif // SUPPORT_POISONED_HASH_HELPER_H
243