//===----------------------------------------------------------------------===// // // The LLVM Compiler Infrastructure // // This file is dual licensed under the MIT and the University of Illinois Open // Source Licenses. See LICENSE.TXT for details. // //===----------------------------------------------------------------------===// // UNSUPPORTED: c++98, c++03, c++11, c++14 // XFAIL: availability=macosx10.13 // XFAIL: availability=macosx10.12 // XFAIL: availability=macosx10.11 // XFAIL: availability=macosx10.10 // XFAIL: availability=macosx10.9 // XFAIL: availability=macosx10.8 // XFAIL: availability=macosx10.7 // // template any make_any(Args&&...); // template // any make_any(initializer_list, Args&&...); #include #include #include "any_helpers.h" #include "count_new.hpp" #include "test_macros.h" using std::any; using std::any_cast; template void test_make_any_type() { // constructing from a small type should perform no allocations. DisableAllocationGuard g(isSmallType()); ((void)g); assert(Type::count == 0); Type::reset(); { any a = std::make_any(); assert(Type::count == 1); assert(Type::copied == 0); assert(Type::moved == 0); assertContains(a, 0); } assert(Type::count == 0); Type::reset(); { any a = std::make_any(101); assert(Type::count == 1); assert(Type::copied == 0); assert(Type::moved == 0); assertContains(a, 101); } assert(Type::count == 0); Type::reset(); { any a = std::make_any(-1, 42, -1); assert(Type::count == 1); assert(Type::copied == 0); assert(Type::moved == 0); assertContains(a, 42); } assert(Type::count == 0); Type::reset(); } template void test_make_any_type_tracked() { // constructing from a small type should perform no allocations. DisableAllocationGuard g(isSmallType()); ((void)g); { any a = std::make_any(); assertArgsMatch(a); } { any a = std::make_any(-1, 42, -1); assertArgsMatch(a); } // initializer_list constructor tests { any a = std::make_any({-1, 42, -1}); assertArgsMatch>(a); } { int x = 42; any a = std::make_any({-1, 42, -1}, x); assertArgsMatch, int&>(a); } } #ifndef TEST_HAS_NO_EXCEPTIONS struct SmallThrows { SmallThrows(int) { throw 42; } SmallThrows(std::initializer_list, int) { throw 42; } }; static_assert(IsSmallObject::value, ""); struct LargeThrows { LargeThrows(int) { throw 42; } LargeThrows(std::initializer_list, int) { throw 42; } int data[sizeof(std::any)]; }; static_assert(!IsSmallObject::value, ""); template void test_make_any_throws() { { try { TEST_IGNORE_NODISCARD std::make_any(101); assert(false); } catch (int const&) { } } { try { TEST_IGNORE_NODISCARD std::make_any({1, 2, 3}, 101); assert(false); } catch (int const&) { } } } #endif int main() { test_make_any_type(); test_make_any_type(); test_make_any_type(); test_make_any_type(); test_make_any_type(); test_make_any_type_tracked(); test_make_any_type_tracked(); #ifndef TEST_HAS_NO_EXCEPTIONS test_make_any_throws(); test_make_any_throws(); #endif }