[flang][runtime] Make defined formatted I/O process format elementally (#74150)
[llvm-project.git] / libcxx / test / support / any_helpers.h
blobdae3ca3c90f58348d46cb925cf829ee0461eeb2d
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 //===----------------------------------------------------------------------===//
9 #ifndef ANY_HELPERS_H
10 #define ANY_HELPERS_H
12 #include <typeinfo>
13 #include <type_traits>
14 #include <cassert>
16 namespace std { namespace experimental {} }
18 #include "test_macros.h"
19 #include "type_id.h"
21 #if !defined(TEST_HAS_NO_RTTI)
22 #define RTTI_ASSERT(X) assert(X)
23 #else
24 #define RTTI_ASSERT(X)
25 #endif
27 template <class T>
28 struct IsSmallObject
29 : public std::integral_constant<bool
30 , sizeof(T) <= sizeof(std::any) - sizeof(void*)
31 && std::alignment_of<void*>::value
32 % std::alignment_of<T>::value == 0
33 && std::is_nothrow_move_constructible<T>::value
35 {};
37 template <class T>
38 bool containsType(std::any const& a) {
39 #if !defined(TEST_HAS_NO_RTTI)
40 return a.type() == typeid(T);
41 #else
42 return a.has_value() && std::any_cast<T>(&a) != nullptr;
43 #endif
46 // Return 'true' if 'Type' will be considered a small type by 'any'
47 template <class Type>
48 bool isSmallType() {
49 return IsSmallObject<Type>::value;
52 // Assert that an object is empty. If the object used to contain an object
53 // of type 'LastType' check that it can no longer be accessed.
54 template <class LastType = int>
55 void assertEmpty(std::any const& a) {
56 assert(!a.has_value());
57 RTTI_ASSERT(a.type() == typeid(void));
58 assert(std::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 assert(a.has_value());
93 assert(containsType<Type>(a));
94 std::any_cast<Type&>(a).value = value;
97 // A test type that will trigger the small object optimization within 'any'.
98 template <int Dummy = 0>
99 struct small_type
101 static int count;
102 static int copied;
103 static int moved;
104 static int const_copied;
105 static int non_const_copied;
107 static void reset() {
108 small_type::copied = 0;
109 small_type::moved = 0;
110 small_type::const_copied = 0;
111 small_type::non_const_copied = 0;
114 int value;
116 explicit small_type(int val = 0) : value(val) {
117 ++count;
119 explicit small_type(int, int val, int) : value(val) {
120 ++count;
122 small_type(std::initializer_list<int> il) : value(*il.begin()) {
123 ++count;
126 small_type(small_type const & other) noexcept {
127 value = other.value;
128 ++count;
129 ++copied;
130 ++const_copied;
133 small_type(small_type& other) noexcept {
134 value = other.value;
135 ++count;
136 ++copied;
137 ++non_const_copied;
140 small_type(small_type && other) noexcept {
141 value = other.value;
142 other.value = 0;
143 ++count;
144 ++moved;
147 ~small_type() {
148 value = -1;
149 --count;
152 private:
153 small_type& operator=(small_type const&) = delete;
154 small_type& operator=(small_type&&) = delete;
157 template <int Dummy>
158 int small_type<Dummy>::count = 0;
160 template <int Dummy>
161 int small_type<Dummy>::copied = 0;
163 template <int Dummy>
164 int small_type<Dummy>::moved = 0;
166 template <int Dummy>
167 int small_type<Dummy>::const_copied = 0;
169 template <int Dummy>
170 int small_type<Dummy>::non_const_copied = 0;
172 typedef small_type<> small;
173 typedef small_type<1> small1;
174 typedef small_type<2> small2;
177 // A test type that will NOT trigger the small object optimization in any.
178 template <int Dummy = 0>
179 struct large_type
181 static int count;
182 static int copied;
183 static int moved;
184 static int const_copied;
185 static int non_const_copied;
187 static void reset() {
188 large_type::copied = 0;
189 large_type::moved = 0;
190 large_type::const_copied = 0;
191 large_type::non_const_copied = 0;
194 int value;
196 large_type(int val = 0) : value(val) {
197 ++count;
198 data[0] = 0;
200 large_type(int, int val, int) : value(val) {
201 ++count;
202 data[0] = 0;
204 large_type(std::initializer_list<int> il) : value(*il.begin()) {
205 ++count;
207 large_type(large_type const & other) {
208 value = other.value;
209 ++count;
210 ++copied;
211 ++const_copied;
214 large_type(large_type & other) {
215 value = other.value;
216 ++count;
217 ++copied;
218 ++non_const_copied;
221 large_type(large_type && other) {
222 value = other.value;
223 other.value = 0;
224 ++count;
225 ++moved;
228 ~large_type() {
229 value = 0;
230 --count;
233 private:
234 large_type& operator=(large_type const&) = delete;
235 large_type& operator=(large_type &&) = delete;
236 int data[10];
239 template <int Dummy>
240 int large_type<Dummy>::count = 0;
242 template <int Dummy>
243 int large_type<Dummy>::copied = 0;
245 template <int Dummy>
246 int large_type<Dummy>::moved = 0;
248 template <int Dummy>
249 int large_type<Dummy>::const_copied = 0;
251 template <int Dummy>
252 int large_type<Dummy>::non_const_copied = 0;
254 typedef large_type<> large;
255 typedef large_type<1> large1;
256 typedef large_type<2> large2;
258 // The exception type thrown by 'small_throws_on_copy', 'large_throws_on_copy'
259 // and 'throws_on_move'.
260 struct my_any_exception {};
262 void throwMyAnyExpression() {
263 #if !defined(TEST_HAS_NO_EXCEPTIONS)
264 throw my_any_exception();
265 #else
266 assert(false && "Exceptions are disabled");
267 #endif
270 // A test type that will trigger the small object optimization within 'any'.
271 // this type throws if it is copied.
272 struct small_throws_on_copy
274 static int count;
275 static int copied;
276 static int moved;
277 static void reset() { count = copied = moved = 0; }
278 int value;
280 explicit small_throws_on_copy(int val = 0) : value(val) {
281 ++count;
283 explicit small_throws_on_copy(int, int val, int) : value(val) {
284 ++count;
286 small_throws_on_copy(small_throws_on_copy const &) {
287 throwMyAnyExpression();
290 small_throws_on_copy(small_throws_on_copy && other) throw() {
291 value = other.value;
292 ++count; ++moved;
295 ~small_throws_on_copy() {
296 --count;
298 private:
299 small_throws_on_copy& operator=(small_throws_on_copy const&) = delete;
300 small_throws_on_copy& operator=(small_throws_on_copy &&) = delete;
303 int small_throws_on_copy::count = 0;
304 int small_throws_on_copy::copied = 0;
305 int small_throws_on_copy::moved = 0;
308 // A test type that will NOT trigger the small object optimization within 'any'.
309 // this type throws if it is copied.
310 struct large_throws_on_copy
312 static int count;
313 static int copied;
314 static int moved;
315 static void reset() { count = copied = moved = 0; }
316 int value = 0;
318 explicit large_throws_on_copy(int val = 0) : value(val) {
319 data[0] = 0;
320 ++count;
322 explicit large_throws_on_copy(int, int val, int) : value(val) {
323 data[0] = 0;
324 ++count;
326 large_throws_on_copy(large_throws_on_copy const &) {
327 throwMyAnyExpression();
330 large_throws_on_copy(large_throws_on_copy && other) throw() {
331 value = other.value;
332 ++count; ++moved;
335 ~large_throws_on_copy() {
336 --count;
339 private:
340 large_throws_on_copy& operator=(large_throws_on_copy const&) = delete;
341 large_throws_on_copy& operator=(large_throws_on_copy &&) = delete;
342 int data[10];
345 int large_throws_on_copy::count = 0;
346 int large_throws_on_copy::copied = 0;
347 int large_throws_on_copy::moved = 0;
349 // A test type that throws when it is moved. This object will NOT trigger
350 // the small object optimization in 'any'.
351 struct throws_on_move
353 static int count;
354 static int copied;
355 static int moved;
356 static void reset() { count = copied = moved = 0; }
357 int value;
359 explicit throws_on_move(int val = 0) : value(val) { ++count; }
360 explicit throws_on_move(int, int val, int) : value(val) { ++count; }
361 throws_on_move(throws_on_move const & other) {
362 value = other.value;
363 ++count; ++copied;
366 throws_on_move(throws_on_move &&) {
367 throwMyAnyExpression();
370 ~throws_on_move() {
371 --count;
373 private:
374 throws_on_move& operator=(throws_on_move const&) = delete;
375 throws_on_move& operator=(throws_on_move &&) = delete;
378 int throws_on_move::count = 0;
379 int throws_on_move::copied = 0;
380 int throws_on_move::moved = 0;
382 struct small_tracked_t {
383 small_tracked_t()
384 : arg_types(&makeArgumentID<>()) {}
385 small_tracked_t(small_tracked_t const&) noexcept
386 : arg_types(&makeArgumentID<small_tracked_t const&>()) {}
387 small_tracked_t(small_tracked_t &&) noexcept
388 : arg_types(&makeArgumentID<small_tracked_t &&>()) {}
389 template <class ...Args>
390 explicit small_tracked_t(Args&&...)
391 : arg_types(&makeArgumentID<Args...>()) {}
392 template <class ...Args>
393 explicit small_tracked_t(std::initializer_list<int>, Args&&...)
394 : arg_types(&makeArgumentID<std::initializer_list<int>, Args...>()) {}
396 TypeID const* arg_types;
398 static_assert(IsSmallObject<small_tracked_t>::value, "must be small");
400 struct large_tracked_t {
401 large_tracked_t()
402 : arg_types(&makeArgumentID<>()) { dummy[0] = 42; }
403 large_tracked_t(large_tracked_t const&) noexcept
404 : arg_types(&makeArgumentID<large_tracked_t const&>()) {}
405 large_tracked_t(large_tracked_t &&) noexcept
406 : arg_types(&makeArgumentID<large_tracked_t &&>()) {}
407 template <class ...Args>
408 explicit large_tracked_t(Args&&...)
409 : arg_types(&makeArgumentID<Args...>()) {}
410 template <class ...Args>
411 explicit large_tracked_t(std::initializer_list<int>, Args&&...)
412 : arg_types(&makeArgumentID<std::initializer_list<int>, Args...>()) {}
414 TypeID const* arg_types;
415 int dummy[sizeof(std::any) / sizeof(int) + 1];
418 static_assert(!IsSmallObject<large_tracked_t>::value, "must not be small");
421 template <class Type, class ...Args>
422 void assertArgsMatch(std::any const& a) {
423 assert(a.has_value());
424 assert(containsType<Type>(a));
425 assert(std::any_cast<Type const &>(a).arg_types == &makeArgumentID<Args...>());
429 #endif