[PowerPC][NFC] Cleanup PPCCTRLoopsVerify pass
[llvm-project.git] / libcxx / test / support / poisoned_hash_helper.h
blob60ce6f9d35b50af2851115431705a9302f533c93
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
12 #include <type_traits>
13 #include <cassert>
15 #include "test_macros.h"
16 #include "test_workarounds.h"
18 #if TEST_STD_VER < 11
19 #error this header may only be used in C++11 or newer
20 #endif
22 template <class ...Args> struct TypeList;
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{});
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);
33 // Test that the specified Hash meets the requirements of a disabled hash.
34 template <class Hash, class Key>
35 void test_hash_disabled();
37 template <class T>
38 void test_hash_disabled_for_type() {
39 return test_hash_disabled<std::hash<T>, T>();
42 namespace PoisonedHashDetail {
43 enum Enum {};
44 enum EnumClass : bool {};
45 struct Class {};
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*
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{});
97 namespace PoisonedHashDetail {
99 template <class T, class = typename T::foo_bar_baz>
100 constexpr bool instantiate(int) { return true; }
101 template <class> constexpr bool instantiate(long) { return true; }
102 template <class T> constexpr bool instantiate() { return instantiate<T>(0); }
104 template <class To>
105 struct ConvertibleToSimple {
106 operator To() const {
107 return To{};
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); }
120 template <class HashExpr,
121 class Res = typename std::result_of<HashExpr>::type>
122 constexpr bool can_hash(int) {
123 return std::is_same<Res, size_t>::value;
125 template <class> constexpr bool can_hash(long) { return false; }
126 template <class T> constexpr bool can_hash() { return can_hash<T>(0); }
128 } // namespace PoisonedHashDetail
130 template <class Hash, class Key, class InputKey>
131 void test_hash_enabled(InputKey const& key) {
132 using namespace PoisonedHashDetail;
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, "");
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
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&&)>(), "");
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>&&)>(), "");
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&&)>(), "");
165 const Hash h{};
166 assert(h(key) == h(key));
170 template <class Hash, class Key>
171 void test_hash_disabled() {
172 using namespace PoisonedHashDetail;
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, "");
181 static_assert(!std::is_function<
182 typename std::remove_pointer<
183 typename std::remove_reference<Hash>::type
184 >::type
185 >::value, "");
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&&)>(), "");
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>&&)>(), "");
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&&)>(), "");
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>();
214 template <class Trait>
215 static void applyTrait() {
216 Trait::template apply<First>();
217 TypeList<Rest...>::template applyTrait<Trait>();
221 template <>
222 struct TypeList<> {
223 template <template <class> class Trait, bool Expect = true>
224 static constexpr bool assertTrait() {
225 return true;
227 template <class Trait>
228 static void applyTrait() {}
232 struct TestLibraryTrait {
233 template <class Type>
234 static void apply() { test_hash_enabled<std::hash<Type>, Type>(); }
237 template <class Types>
238 void test_library_hash_specializations_available(Types) {
239 Types::template applyTrait<TestLibraryTrait >();
242 #endif // SUPPORT_POISONED_HASH_HELPER_H