blob: 60ce6f9d35b50af2851115431705a9302f533c93 [file] [log] [blame]
Eric Fiselierf9127592017-01-21 00:02:12 +00001// -*- C++ -*-
2//===----------------------------------------------------------------------===//
3//
Chandler Carruth57b08b02019-01-19 10:56:40 +00004// 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
Eric Fiselierf9127592017-01-21 00:02:12 +00007//
8//===----------------------------------------------------------------------===//
Nico Webercc890632019-08-21 00:14:12 +00009#ifndef SUPPORT_POISONED_HASH_HELPER_H
10#define SUPPORT_POISONED_HASH_HELPER_H
Eric Fiselierf9127592017-01-21 00:02:12 +000011
12#include <type_traits>
13#include <cassert>
14
15#include "test_macros.h"
Eric Fiselier9506f142017-03-22 22:41:41 +000016#include "test_workarounds.h"
Eric Fiselierf9127592017-01-21 00:02:12 +000017
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
Eric Fiselierf9127592017-01-21 00:02:12 +0000149 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
Eric Fiselierd9e16312017-01-21 00:57:29 +0000165 const Hash h{};
Eric Fiselierf9127592017-01-21 00:02:12 +0000166 assert(h(key) == h(key));
167
168}
169
170template <class Hash, class Key>
171void 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
Eric Fiselierf9127592017-01-21 00:02:12 +0000188 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
206template <class First, class ...Rest>
207struct 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
221template <>
222struct 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
232struct TestLibraryTrait {
233 template <class Type>
234 static void apply() { test_hash_enabled<std::hash<Type>, Type>(); }
235};
236
237template <class Types>
238void test_library_hash_specializations_available(Types) {
239 Types::template applyTrait<TestLibraryTrait >();
240}
241
Nico Webercc890632019-08-21 00:14:12 +0000242#endif // SUPPORT_POISONED_HASH_HELPER_H