2 //===----------------------------------------------------------------------===//
4 // The LLVM Compiler Infrastructure
6 // This file is dual licensed under the MIT and the University of Illinois Open
7 // Source Licenses. See LICENSE.TXT for details.
9 //===----------------------------------------------------------------------===//
10 #ifndef SUPPORT_POISONED_HASH_HELPER_HPP
11 #define SUPPORT_POISONED_HASH_HELPER_HPP
13 #include <type_traits>
16 #include "test_macros.h"
17 #include "test_workarounds.h"
20 #error this header may only be used in C++11 or newer
23 template <class ...Args> struct TypeList;
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{});
29 template <class T, class InputKey = T>
30 void test_hash_enabled_for_type(InputKey const& key = InputKey{}) {
31 return test_hash_enabled<std::hash<T>, T, InputKey>(key);
34 // Test that the specified Hash meets the requirements of a disabled hash.
35 template <class Hash, class Key>
36 void test_hash_disabled();
39 void test_hash_disabled_for_type() {
40 return test_hash_disabled<std::hash<T>, T>();
43 namespace PoisonedHashDetail {
45 enum EnumClass : bool {};
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 !defined(TEST_WORKAROUND_C1XX_BROKEN_NULLPTR_CONVERSION_OPERATOR)
63 #ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
75 #ifndef _LIBCPP_HAS_NO_INT128
82 #if TEST_STD_VER >= 14
84 PoisonedHashDetail::Enum,
85 PoisonedHashDetail::EnumClass,
90 PoisonedHashDetail::Class*
94 // Test that each of the library hash specializations for arithmetic types,
95 // enum types, and pointer types are available and enabled.
96 template <class Types = LibraryHashTypes>
97 void test_library_hash_specializations_available(Types = Types{});
100 namespace PoisonedHashDetail {
102 template <class T, class = typename T::foo_bar_baz>
103 constexpr bool instantiate(int) { return true; }
104 template <class> constexpr bool instantiate(long) { return true; }
105 template <class T> constexpr bool instantiate() { return instantiate<T>(0); }
108 struct ConvertibleToSimple {
109 operator To() const {
115 struct ConvertibleTo {
117 operator To&() & { return to; }
118 operator To const&() const & { return to; }
119 operator To&&() && { return std::move(to); }
120 operator To const&&() const && { return std::move(to); }
123 template <class HashExpr,
124 class Res = typename std::result_of<HashExpr>::type>
125 constexpr bool can_hash(int) {
126 return std::is_same<Res, size_t>::value;
128 template <class> constexpr bool can_hash(long) { return false; }
129 template <class T> constexpr bool can_hash() { return can_hash<T>(0); }
131 } // namespace PoisonedHashDetail
133 template <class Hash, class Key, class InputKey>
134 void test_hash_enabled(InputKey const& key) {
135 using namespace PoisonedHashDetail;
137 static_assert(std::is_destructible<Hash>::value, "");
138 // Enabled hash requirements
139 static_assert(std::is_default_constructible<Hash>::value, "");
140 static_assert(std::is_copy_constructible<Hash>::value, "");
141 static_assert(std::is_move_constructible<Hash>::value, "");
142 static_assert(std::is_copy_assignable<Hash>::value, "");
143 static_assert(std::is_move_assignable<Hash>::value, "");
145 #if TEST_STD_VER > 14
146 static_assert(std::is_swappable<Hash>::value, "");
147 #elif defined(_LIBCPP_VERSION)
148 static_assert(std::__is_swappable<Hash>::value, "");
151 // Hashable requirements
152 using CKey = ConvertibleTo<Key>;
153 static_assert(can_hash<Hash(Key&)>(), "");
154 static_assert(can_hash<Hash(Key const&)>(), "");
155 static_assert(can_hash<Hash(Key&&)>(), "");
156 static_assert(can_hash<Hash const&(Key&)>(), "");
157 static_assert(can_hash<Hash const&(Key const&)>(), "");
158 static_assert(can_hash<Hash const&(Key&&)>(), "");
160 static_assert(can_hash<Hash(ConvertibleToSimple<Key>&)>(), "");
161 static_assert(can_hash<Hash(ConvertibleToSimple<Key> const&)>(), "");
162 static_assert(can_hash<Hash(ConvertibleToSimple<Key>&&)>(), "");
164 static_assert(can_hash<Hash(ConvertibleTo<Key>&)>(), "");
165 static_assert(can_hash<Hash(ConvertibleTo<Key> const&)>(), "");
166 static_assert(can_hash<Hash(ConvertibleTo<Key> &&)>(), "");
167 static_assert(can_hash<Hash(ConvertibleTo<Key> const&&)>(), "");
170 assert(h(key) == h(key));
174 template <class Hash, class Key>
175 void test_hash_disabled() {
176 using namespace PoisonedHashDetail;
178 // Disabled hash requirements
179 static_assert(!std::is_default_constructible<Hash>::value, "");
180 static_assert(!std::is_copy_constructible<Hash>::value, "");
181 static_assert(!std::is_move_constructible<Hash>::value, "");
182 static_assert(!std::is_copy_assignable<Hash>::value, "");
183 static_assert(!std::is_move_assignable<Hash>::value, "");
185 static_assert(!std::is_function<
186 typename std::remove_pointer<
187 typename std::remove_reference<Hash>::type
191 // Hashable requirements
192 using CKey = ConvertibleTo<Key>;
193 static_assert(!can_hash<Hash(Key&)>(), "");
194 static_assert(!can_hash<Hash(Key const&)>(), "");
195 static_assert(!can_hash<Hash(Key&&)>(), "");
196 static_assert(!can_hash<Hash const&(Key&)>(), "");
197 static_assert(!can_hash<Hash const&(Key const&)>(), "");
198 static_assert(!can_hash<Hash const&(Key&&)>(), "");
200 static_assert(!can_hash<Hash(ConvertibleToSimple<Key>&)>(), "");
201 static_assert(!can_hash<Hash(ConvertibleToSimple<Key> const&)>(), "");
202 static_assert(!can_hash<Hash(ConvertibleToSimple<Key>&&)>(), "");
204 static_assert(!can_hash<Hash(ConvertibleTo<Key>&)>(), "");
205 static_assert(!can_hash<Hash(ConvertibleTo<Key> const&)>(), "");
206 static_assert(!can_hash<Hash(ConvertibleTo<Key> &&)>(), "");
207 static_assert(!can_hash<Hash(ConvertibleTo<Key> const&&)>(), "");
211 template <class First, class ...Rest>
212 struct TypeList<First, Rest...> {
213 template <template <class> class Trait, bool Expect = true>
214 static constexpr bool assertTrait() {
215 static_assert(Trait<First>::value == Expect, "");
216 return TypeList<Rest...>::template assertTrait<Trait, Expect>();
219 template <class Trait>
220 static void applyTrait() {
221 Trait::template apply<First>();
222 TypeList<Rest...>::template applyTrait<Trait>();
228 template <template <class> class Trait, bool Expect = true>
229 static constexpr bool assertTrait() {
232 template <class Trait>
233 static void applyTrait() {}
237 struct TestLibraryTrait {
238 template <class Type>
239 static void apply() { test_hash_enabled<std::hash<Type>, Type>(); }
242 template <class Types>
243 void test_library_hash_specializations_available(Types) {
244 Types::template applyTrait<TestLibraryTrait >();
247 #endif // SUPPORT_POISONED_HASH_HELPER_HPP