[PowerPC][NFC] Cleanup PPCCTRLoopsVerify pass
[llvm-project.git] / libcxx / test / support / any_helpers.h
blob6bdc3fd4336e7332c6091422c3a6763b54d7a727
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 #ifndef ANY_HELPERS_H
9 #define ANY_HELPERS_H
11 #include <typeinfo>
12 #include <type_traits>
13 #include <cassert>
15 namespace std { namespace experimental {} }
17 #include "test_macros.h"
18 #include "type_id.h"
20 #if !defined(TEST_HAS_NO_RTTI)
21 #define RTTI_ASSERT(X) assert(X)
22 #else
23 #define RTTI_ASSERT(X)
24 #endif
26 template <class T>
27 struct IsSmallObject
28 : public std::integral_constant<bool
29 , sizeof(T) <= sizeof(std::any) - sizeof(void*)
30 && std::alignment_of<void*>::value
31 % std::alignment_of<T>::value == 0
32 && std::is_nothrow_move_constructible<T>::value
34 {};
36 template <class T>
37 bool containsType(std::any const& a) {
38 #if !defined(TEST_HAS_NO_RTTI)
39 return a.type() == typeid(T);
40 #else
41 return a.has_value() && std::any_cast<T>(&a) != nullptr;
42 #endif
45 // Return 'true' if 'Type' will be considered a small type by 'any'
46 template <class Type>
47 bool isSmallType() {
48 return IsSmallObject<Type>::value;
51 // Assert that an object is empty. If the object used to contain an object
52 // of type 'LastType' check that it can no longer be accessed.
53 template <class LastType = int>
54 void assertEmpty(std::any const& a) {
55 using namespace std;
56 assert(!a.has_value());
57 RTTI_ASSERT(a.type() == typeid(void));
58 assert(any_cast<LastType const>(&a) == nullptr);
61 template <class Type>
62 constexpr auto has_value_member(int) -> decltype(std::declval<Type&>().value, true)
63 { return true; }
64 template <class> constexpr bool has_value_member(long) { return false; }
67 // Assert that an 'any' object stores the specified 'Type' and 'value'.
68 template <class Type>
69 std::enable_if_t<has_value_member<Type>(0)>
70 _LIBCPP_AVAILABILITY_THROW_BAD_ANY_CAST
71 assertContains(std::any const& a, int value) {
72 assert(a.has_value());
73 assert(containsType<Type>(a));
74 assert(std::any_cast<Type const &>(a).value == value);
77 template <class Type, class Value>
78 std::enable_if_t<!has_value_member<Type>(0)>
79 _LIBCPP_AVAILABILITY_THROW_BAD_ANY_CAST
80 assertContains(std::any const& a, Value value) {
81 assert(a.has_value());
82 assert(containsType<Type>(a));
83 assert(std::any_cast<Type const &>(a) == value);
87 // Modify the value of a "test type" stored within an any to the specified
88 // 'value'.
89 template <class Type>
90 _LIBCPP_AVAILABILITY_THROW_BAD_ANY_CAST
91 void modifyValue(std::any& a, int value) {
92 using namespace std;
93 using namespace std::experimental;
94 assert(a.has_value());
95 assert(containsType<Type>(a));
96 any_cast<Type&>(a).value = value;
99 // A test type that will trigger the small object optimization within 'any'.
100 template <int Dummy = 0>
101 struct small_type
103 static int count;
104 static int copied;
105 static int moved;
106 static int const_copied;
107 static int non_const_copied;
109 static void reset() {
110 small_type::copied = 0;
111 small_type::moved = 0;
112 small_type::const_copied = 0;
113 small_type::non_const_copied = 0;
116 int value;
118 explicit small_type(int val = 0) : value(val) {
119 ++count;
121 explicit small_type(int, int val, int) : value(val) {
122 ++count;
124 small_type(std::initializer_list<int> il) : value(*il.begin()) {
125 ++count;
128 small_type(small_type const & other) noexcept {
129 value = other.value;
130 ++count;
131 ++copied;
132 ++const_copied;
135 small_type(small_type& other) noexcept {
136 value = other.value;
137 ++count;
138 ++copied;
139 ++non_const_copied;
142 small_type(small_type && other) noexcept {
143 value = other.value;
144 other.value = 0;
145 ++count;
146 ++moved;
149 ~small_type() {
150 value = -1;
151 --count;
154 private:
155 small_type& operator=(small_type const&) = delete;
156 small_type& operator=(small_type&&) = delete;
159 template <int Dummy>
160 int small_type<Dummy>::count = 0;
162 template <int Dummy>
163 int small_type<Dummy>::copied = 0;
165 template <int Dummy>
166 int small_type<Dummy>::moved = 0;
168 template <int Dummy>
169 int small_type<Dummy>::const_copied = 0;
171 template <int Dummy>
172 int small_type<Dummy>::non_const_copied = 0;
174 typedef small_type<> small;
175 typedef small_type<1> small1;
176 typedef small_type<2> small2;
179 // A test type that will NOT trigger the small object optimization in any.
180 template <int Dummy = 0>
181 struct large_type
183 static int count;
184 static int copied;
185 static int moved;
186 static int const_copied;
187 static int non_const_copied;
189 static void reset() {
190 large_type::copied = 0;
191 large_type::moved = 0;
192 large_type::const_copied = 0;
193 large_type::non_const_copied = 0;
196 int value;
198 large_type(int val = 0) : value(val) {
199 ++count;
200 data[0] = 0;
202 large_type(int, int val, int) : value(val) {
203 ++count;
204 data[0] = 0;
206 large_type(std::initializer_list<int> il) : value(*il.begin()) {
207 ++count;
209 large_type(large_type const & other) {
210 value = other.value;
211 ++count;
212 ++copied;
213 ++const_copied;
216 large_type(large_type & other) {
217 value = other.value;
218 ++count;
219 ++copied;
220 ++non_const_copied;
223 large_type(large_type && other) {
224 value = other.value;
225 other.value = 0;
226 ++count;
227 ++moved;
230 ~large_type() {
231 value = 0;
232 --count;
235 private:
236 large_type& operator=(large_type const&) = delete;
237 large_type& operator=(large_type &&) = delete;
238 int data[10];
241 template <int Dummy>
242 int large_type<Dummy>::count = 0;
244 template <int Dummy>
245 int large_type<Dummy>::copied = 0;
247 template <int Dummy>
248 int large_type<Dummy>::moved = 0;
250 template <int Dummy>
251 int large_type<Dummy>::const_copied = 0;
253 template <int Dummy>
254 int large_type<Dummy>::non_const_copied = 0;
256 typedef large_type<> large;
257 typedef large_type<1> large1;
258 typedef large_type<2> large2;
260 // The exception type thrown by 'small_throws_on_copy', 'large_throws_on_copy'
261 // and 'throws_on_move'.
262 struct my_any_exception {};
264 void throwMyAnyExpression() {
265 #if !defined(TEST_HAS_NO_EXCEPTIONS)
266 throw my_any_exception();
267 #else
268 assert(false && "Exceptions are disabled");
269 #endif
272 // A test type that will trigger the small object optimization within 'any'.
273 // this type throws if it is copied.
274 struct small_throws_on_copy
276 static int count;
277 static int copied;
278 static int moved;
279 static void reset() { count = copied = moved = 0; }
280 int value;
282 explicit small_throws_on_copy(int val = 0) : value(val) {
283 ++count;
285 explicit small_throws_on_copy(int, int val, int) : value(val) {
286 ++count;
288 small_throws_on_copy(small_throws_on_copy const &) {
289 throwMyAnyExpression();
292 small_throws_on_copy(small_throws_on_copy && other) throw() {
293 value = other.value;
294 ++count; ++moved;
297 ~small_throws_on_copy() {
298 --count;
300 private:
301 small_throws_on_copy& operator=(small_throws_on_copy const&) = delete;
302 small_throws_on_copy& operator=(small_throws_on_copy &&) = delete;
305 int small_throws_on_copy::count = 0;
306 int small_throws_on_copy::copied = 0;
307 int small_throws_on_copy::moved = 0;
310 // A test type that will NOT trigger the small object optimization within 'any'.
311 // this type throws if it is copied.
312 struct large_throws_on_copy
314 static int count;
315 static int copied;
316 static int moved;
317 static void reset() { count = copied = moved = 0; }
318 int value = 0;
320 explicit large_throws_on_copy(int val = 0) : value(val) {
321 data[0] = 0;
322 ++count;
324 explicit large_throws_on_copy(int, int val, int) : value(val) {
325 data[0] = 0;
326 ++count;
328 large_throws_on_copy(large_throws_on_copy const &) {
329 throwMyAnyExpression();
332 large_throws_on_copy(large_throws_on_copy && other) throw() {
333 value = other.value;
334 ++count; ++moved;
337 ~large_throws_on_copy() {
338 --count;
341 private:
342 large_throws_on_copy& operator=(large_throws_on_copy const&) = delete;
343 large_throws_on_copy& operator=(large_throws_on_copy &&) = delete;
344 int data[10];
347 int large_throws_on_copy::count = 0;
348 int large_throws_on_copy::copied = 0;
349 int large_throws_on_copy::moved = 0;
351 // A test type that throws when it is moved. This object will NOT trigger
352 // the small object optimization in 'any'.
353 struct throws_on_move
355 static int count;
356 static int copied;
357 static int moved;
358 static void reset() { count = copied = moved = 0; }
359 int value;
361 explicit throws_on_move(int val = 0) : value(val) { ++count; }
362 explicit throws_on_move(int, int val, int) : value(val) { ++count; }
363 throws_on_move(throws_on_move const & other) {
364 value = other.value;
365 ++count; ++copied;
368 throws_on_move(throws_on_move &&) {
369 throwMyAnyExpression();
372 ~throws_on_move() {
373 --count;
375 private:
376 throws_on_move& operator=(throws_on_move const&) = delete;
377 throws_on_move& operator=(throws_on_move &&) = delete;
380 int throws_on_move::count = 0;
381 int throws_on_move::copied = 0;
382 int throws_on_move::moved = 0;
384 struct small_tracked_t {
385 small_tracked_t()
386 : arg_types(&makeArgumentID<>()) {}
387 small_tracked_t(small_tracked_t const&) noexcept
388 : arg_types(&makeArgumentID<small_tracked_t const&>()) {}
389 small_tracked_t(small_tracked_t &&) noexcept
390 : arg_types(&makeArgumentID<small_tracked_t &&>()) {}
391 template <class ...Args>
392 explicit small_tracked_t(Args&&...)
393 : arg_types(&makeArgumentID<Args...>()) {}
394 template <class ...Args>
395 explicit small_tracked_t(std::initializer_list<int>, Args&&...)
396 : arg_types(&makeArgumentID<std::initializer_list<int>, Args...>()) {}
398 TypeID const* arg_types;
400 static_assert(IsSmallObject<small_tracked_t>::value, "must be small");
402 struct large_tracked_t {
403 large_tracked_t()
404 : arg_types(&makeArgumentID<>()) { dummy[0] = 42; }
405 large_tracked_t(large_tracked_t const&) noexcept
406 : arg_types(&makeArgumentID<large_tracked_t const&>()) {}
407 large_tracked_t(large_tracked_t &&) noexcept
408 : arg_types(&makeArgumentID<large_tracked_t &&>()) {}
409 template <class ...Args>
410 explicit large_tracked_t(Args&&...)
411 : arg_types(&makeArgumentID<Args...>()) {}
412 template <class ...Args>
413 explicit large_tracked_t(std::initializer_list<int>, Args&&...)
414 : arg_types(&makeArgumentID<std::initializer_list<int>, Args...>()) {}
416 TypeID const* arg_types;
417 int dummy[sizeof(std::any) / sizeof(int) + 1];
420 static_assert(!IsSmallObject<large_tracked_t>::value, "must not be small");
423 template <class Type, class ...Args>
424 void assertArgsMatch(std::any const& a) {
425 using namespace std;
426 using namespace std::experimental;
427 assert(a.has_value());
428 assert(containsType<Type>(a));
429 assert(any_cast<Type const &>(a).arg_types == &makeArgumentID<Args...>());
433 #endif