blob: 0a5675a2c5a99509a1dfe9bd1f0eb75bb661941a [file] [log] [blame]
Eric Fiselierf9127592017-01-21 00:02:12 +00001// -*- 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
22template <class ...Args> struct TypeList;
23
24// Test that the specified Hash meets the requirements of an enabled hash
25template <class Hash, class Key, class InputKey = Key>
26void test_hash_enabled(InputKey const& key = InputKey{});
27
28template <class T, class InputKey = T>
29void 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.
34template <class Hash, class Key>
35void test_hash_disabled();
36
37template <class T>
38void test_hash_disabled_for_type() {
39 return test_hash_disabled<std::hash<T>, T>();
40}
41
42namespace 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.
51using 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.
93template <class Types = LibraryHashTypes>
94void test_library_hash_specializations_available(Types = Types{});
95
96
97namespace PoisonedHashDetail {
98
99template <class T, class = typename T::foo_bar_baz>
100constexpr bool instantiate(int) { return true; }
101template <class> constexpr bool instantiate(long) { return true; }
102template <class T> constexpr bool instantiate() { return instantiate<T>(0); }
103
104template <class To>
105struct ConvertibleToSimple {
106 operator To() const {
107 return To{};
108 }
109};
110
111template <class To>
112struct 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
120template <class HashExpr,
121 class Res = typename std::result_of<HashExpr>::type>
122constexpr bool can_hash(int) {
123 return std::is_same<Res, size_t>::value;
124}
125template <class> constexpr bool can_hash(long) { return false; }
126template <class T> constexpr bool can_hash() { return can_hash<T>(0); }
127
128} // namespace PoisonedHashDetail
129
130template <class Hash, class Key, class InputKey>
131void 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
171template <class Hash, class Key>
172void 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
208template <class First, class ...Rest>
209struct TypeList<First, Rest...> {
210 template <template <class> class Trait, bool Expect = true>
211 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>
217 static void applyTrait() {
218 Trait::template apply<First>();
219 TypeList<Rest...>::template applyTrait<Trait>();
220 }
221};
222
223template <>
224struct TypeList<> {
225 template <template <class> class Trait, bool Expect = true>
226 static constexpr bool assertTrait() {
227 return true;
228 }
229 template <class Trait>
230 static void applyTrait() {}
231};
232
233
234struct TestLibraryTrait {
235 template <class Type>
236 static void apply() { test_hash_enabled<std::hash<Type>, Type>(); }
237};
238
239template <class Types>
240void test_library_hash_specializations_available(Types) {
241 Types::template applyTrait<TestLibraryTrait >();
242}
243
244#endif // SUPPORT_POISONED_HASH_HELPER_HPP