1 //===----------------------------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
13 #include <type_traits>
16 namespace std { namespace experimental {} }
18 #include "test_macros.h"
21 #if !defined(TEST_HAS_NO_RTTI)
22 #define RTTI_ASSERT(X) assert(X)
24 #define RTTI_ASSERT(X)
29 : public std::integral_constant<bool
30 , sizeof(T) <= (sizeof(void*)*3)
31 && std::alignment_of<void*>::value
32 % std::alignment_of<T>::value == 0
33 && std::is_nothrow_move_constructible<T>::value
38 bool containsType(std::any const& a) {
39 #if !defined(TEST_HAS_NO_RTTI)
40 return a.type() == typeid(T);
42 return a.has_value() && std::any_cast<T>(&a) != nullptr;
46 // Return 'true' if 'Type' will be considered a small type by 'any'
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) {
57 assert(!a.has_value());
58 RTTI_ASSERT(a.type() == typeid(void));
59 assert(any_cast<LastType const>(&a) == nullptr);
63 constexpr auto has_value_member(int) -> decltype(std::declval<Type&>().value, true)
65 template <class> constexpr bool has_value_member(long) { return false; }
68 // Assert that an 'any' object stores the specified 'Type' and 'value'.
70 std::enable_if_t<has_value_member<Type>(0)>
71 _LIBCPP_AVAILABILITY_THROW_BAD_ANY_CAST
72 assertContains(std::any const& a, int value) {
73 assert(a.has_value());
74 assert(containsType<Type>(a));
75 assert(std::any_cast<Type const &>(a).value == value);
78 template <class Type, class Value>
79 std::enable_if_t<!has_value_member<Type>(0)>
80 _LIBCPP_AVAILABILITY_THROW_BAD_ANY_CAST
81 assertContains(std::any const& a, Value value) {
82 assert(a.has_value());
83 assert(containsType<Type>(a));
84 assert(std::any_cast<Type const &>(a) == value);
88 // Modify the value of a "test type" stored within an any to the specified
91 _LIBCPP_AVAILABILITY_THROW_BAD_ANY_CAST
92 void modifyValue(std::any& a, int value) {
94 using namespace std::experimental;
95 assert(a.has_value());
96 assert(containsType<Type>(a));
97 any_cast<Type&>(a).value = value;
100 // A test type that will trigger the small object optimization within 'any'.
101 template <int Dummy = 0>
107 static int const_copied;
108 static int non_const_copied;
110 static void reset() {
111 small_type::copied = 0;
112 small_type::moved = 0;
113 small_type::const_copied = 0;
114 small_type::non_const_copied = 0;
119 explicit small_type(int val = 0) : value(val) {
122 explicit small_type(int, int val, int) : value(val) {
125 small_type(std::initializer_list<int> il) : value(*il.begin()) {
129 small_type(small_type const & other) noexcept {
136 small_type(small_type& other) noexcept {
143 small_type(small_type && other) noexcept {
156 small_type& operator=(small_type const&) = delete;
157 small_type& operator=(small_type&&) = delete;
161 int small_type<Dummy>::count = 0;
164 int small_type<Dummy>::copied = 0;
167 int small_type<Dummy>::moved = 0;
170 int small_type<Dummy>::const_copied = 0;
173 int small_type<Dummy>::non_const_copied = 0;
175 typedef small_type<> small;
176 typedef small_type<1> small1;
177 typedef small_type<2> small2;
180 // A test type that will NOT trigger the small object optimization in any.
181 template <int Dummy = 0>
187 static int const_copied;
188 static int non_const_copied;
190 static void reset() {
191 large_type::copied = 0;
192 large_type::moved = 0;
193 large_type::const_copied = 0;
194 large_type::non_const_copied = 0;
199 large_type(int val = 0) : value(val) {
203 large_type(int, int val, int) : value(val) {
207 large_type(std::initializer_list<int> il) : value(*il.begin()) {
210 large_type(large_type const & other) {
217 large_type(large_type & other) {
224 large_type(large_type && other) {
237 large_type& operator=(large_type const&) = delete;
238 large_type& operator=(large_type &&) = delete;
243 int large_type<Dummy>::count = 0;
246 int large_type<Dummy>::copied = 0;
249 int large_type<Dummy>::moved = 0;
252 int large_type<Dummy>::const_copied = 0;
255 int large_type<Dummy>::non_const_copied = 0;
257 typedef large_type<> large;
258 typedef large_type<1> large1;
259 typedef large_type<2> large2;
261 // The exception type thrown by 'small_throws_on_copy', 'large_throws_on_copy'
262 // and 'throws_on_move'.
263 struct my_any_exception {};
265 void throwMyAnyExpression() {
266 #if !defined(TEST_HAS_NO_EXCEPTIONS)
267 throw my_any_exception();
269 assert(false && "Exceptions are disabled");
273 // A test type that will trigger the small object optimization within 'any'.
274 // this type throws if it is copied.
275 struct small_throws_on_copy
280 static void reset() { count = copied = moved = 0; }
283 explicit small_throws_on_copy(int val = 0) : value(val) {
286 explicit small_throws_on_copy(int, int val, int) : value(val) {
289 small_throws_on_copy(small_throws_on_copy const &) {
290 throwMyAnyExpression();
293 small_throws_on_copy(small_throws_on_copy && other) throw() {
298 ~small_throws_on_copy() {
302 small_throws_on_copy& operator=(small_throws_on_copy const&) = delete;
303 small_throws_on_copy& operator=(small_throws_on_copy &&) = delete;
306 int small_throws_on_copy::count = 0;
307 int small_throws_on_copy::copied = 0;
308 int small_throws_on_copy::moved = 0;
311 // A test type that will NOT trigger the small object optimization within 'any'.
312 // this type throws if it is copied.
313 struct large_throws_on_copy
318 static void reset() { count = copied = moved = 0; }
321 explicit large_throws_on_copy(int val = 0) : value(val) {
325 explicit large_throws_on_copy(int, int val, int) : value(val) {
329 large_throws_on_copy(large_throws_on_copy const &) {
330 throwMyAnyExpression();
333 large_throws_on_copy(large_throws_on_copy && other) throw() {
338 ~large_throws_on_copy() {
343 large_throws_on_copy& operator=(large_throws_on_copy const&) = delete;
344 large_throws_on_copy& operator=(large_throws_on_copy &&) = delete;
348 int large_throws_on_copy::count = 0;
349 int large_throws_on_copy::copied = 0;
350 int large_throws_on_copy::moved = 0;
352 // A test type that throws when it is moved. This object will NOT trigger
353 // the small object optimization in 'any'.
354 struct throws_on_move
359 static void reset() { count = copied = moved = 0; }
362 explicit throws_on_move(int val = 0) : value(val) { ++count; }
363 explicit throws_on_move(int, int val, int) : value(val) { ++count; }
364 throws_on_move(throws_on_move const & other) {
369 throws_on_move(throws_on_move &&) {
370 throwMyAnyExpression();
377 throws_on_move& operator=(throws_on_move const&) = delete;
378 throws_on_move& operator=(throws_on_move &&) = delete;
381 int throws_on_move::count = 0;
382 int throws_on_move::copied = 0;
383 int throws_on_move::moved = 0;
385 struct small_tracked_t {
387 : arg_types(&makeArgumentID<>()) {}
388 small_tracked_t(small_tracked_t const&) noexcept
389 : arg_types(&makeArgumentID<small_tracked_t const&>()) {}
390 small_tracked_t(small_tracked_t &&) noexcept
391 : arg_types(&makeArgumentID<small_tracked_t &&>()) {}
392 template <class ...Args>
393 explicit small_tracked_t(Args&&...)
394 : arg_types(&makeArgumentID<Args...>()) {}
395 template <class ...Args>
396 explicit small_tracked_t(std::initializer_list<int>, Args&&...)
397 : arg_types(&makeArgumentID<std::initializer_list<int>, Args...>()) {}
399 TypeID const* arg_types;
401 static_assert(IsSmallObject<small_tracked_t>::value, "must be small");
403 struct large_tracked_t {
405 : arg_types(&makeArgumentID<>()) { dummy[0] = 42; }
406 large_tracked_t(large_tracked_t const&) noexcept
407 : arg_types(&makeArgumentID<large_tracked_t const&>()) {}
408 large_tracked_t(large_tracked_t &&) noexcept
409 : arg_types(&makeArgumentID<large_tracked_t &&>()) {}
410 template <class ...Args>
411 explicit large_tracked_t(Args&&...)
412 : arg_types(&makeArgumentID<Args...>()) {}
413 template <class ...Args>
414 explicit large_tracked_t(std::initializer_list<int>, Args&&...)
415 : arg_types(&makeArgumentID<std::initializer_list<int>, Args...>()) {}
417 TypeID const* arg_types;
421 static_assert(!IsSmallObject<large_tracked_t>::value, "must be small");
424 template <class Type, class ...Args>
425 void assertArgsMatch(std::any const& a) {
427 using namespace std::experimental;
428 assert(a.has_value());
429 assert(containsType<Type>(a));
430 assert(any_cast<Type const &>(a).arg_types == &makeArgumentID<Args...>());