1 //===----------------------------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 // UNSUPPORTED: c++98, c++03, c++11, c++14
11 
12 // XFAIL: with_system_cxx_lib=macosx10.12
13 // XFAIL: with_system_cxx_lib=macosx10.11
14 // XFAIL: with_system_cxx_lib=macosx10.10
15 // XFAIL: with_system_cxx_lib=macosx10.9
16 // XFAIL: with_system_cxx_lib=macosx10.7
17 // XFAIL: with_system_cxx_lib=macosx10.8
18 
19 // <any>
20 
21 // template <class T, class ...Args> any make_any(Args&&...);
22 // template <class T, class U, class ...Args>
23 // any make_any(initializer_list<U>, Args&&...);
24 
25 #include <any>
26 #include <cassert>
27 
28 #include "any_helpers.h"
29 #include "count_new.hpp"
30 #include "test_macros.h"
31 
32 using std::any;
33 using std::any_cast;
34 
35 
36 template <class Type>
37 void test_make_any_type() {
38     // constructing from a small type should perform no allocations.
39     DisableAllocationGuard g(isSmallType<Type>()); ((void)g);
40     assert(Type::count == 0);
41     Type::reset();
42     {
43         any a = std::make_any<Type>();
44 
45         assert(Type::count == 1);
46         assert(Type::copied == 0);
47         assert(Type::moved == 0);
48         assertContains<Type>(a, 0);
49     }
50     assert(Type::count == 0);
51     Type::reset();
52     {
53         any a = std::make_any<Type>(101);
54 
55         assert(Type::count == 1);
56         assert(Type::copied == 0);
57         assert(Type::moved == 0);
58         assertContains<Type>(a, 101);
59     }
60     assert(Type::count == 0);
61     Type::reset();
62     {
63         any a = std::make_any<Type>(-1, 42, -1);
64 
65         assert(Type::count == 1);
66         assert(Type::copied == 0);
67         assert(Type::moved == 0);
68         assertContains<Type>(a, 42);
69     }
70     assert(Type::count == 0);
71     Type::reset();
72 }
73 
74 template <class Type>
75 void test_make_any_type_tracked() {
76     // constructing from a small type should perform no allocations.
77     DisableAllocationGuard g(isSmallType<Type>()); ((void)g);
78     {
79         any a = std::make_any<Type>();
80         assertArgsMatch<Type>(a);
81     }
82     {
83         any a = std::make_any<Type>(-1, 42, -1);
84         assertArgsMatch<Type, int, int, int>(a);
85     }
86     // initializer_list constructor tests
87     {
88         any a = std::make_any<Type>({-1, 42, -1});
89         assertArgsMatch<Type, std::initializer_list<int>>(a);
90     }
91     {
92         int x = 42;
93         any a  = std::make_any<Type>({-1, 42, -1}, x);
94         assertArgsMatch<Type, std::initializer_list<int>, int&>(a);
95     }
96 }
97 
98 #ifndef TEST_HAS_NO_EXCEPTIONS
99 
100 struct SmallThrows {
101   SmallThrows(int) { throw 42; }
102   SmallThrows(std::initializer_list<int>, int) { throw 42; }
103 };
104 static_assert(IsSmallObject<SmallThrows>::value, "");
105 
106 struct LargeThrows {
107   LargeThrows(int) { throw 42; }
108   LargeThrows(std::initializer_list<int>, int) { throw 42; }
109   int data[sizeof(std::any)];
110 };
111 static_assert(!IsSmallObject<LargeThrows>::value, "");
112 
113 template <class Type>
114 void test_make_any_throws()
115 {
116     {
117         try {
118             std::make_any<Type>(101);
119             assert(false);
120         } catch (int const&) {
121         }
122     }
123     {
124         try {
125             std::make_any<Type>({1, 2, 3}, 101);
126             assert(false);
127         } catch (int const&) {
128         }
129     }
130 }
131 
132 #endif
133 
134 int main() {
135     test_make_any_type<small>();
136     test_make_any_type<large>();
137     test_make_any_type<small_throws_on_copy>();
138     test_make_any_type<large_throws_on_copy>();
139     test_make_any_type<throws_on_move>();
140     test_make_any_type_tracked<small_tracked_t>();
141     test_make_any_type_tracked<large_tracked_t>();
142 #ifndef TEST_HAS_NO_EXCEPTIONS
143     test_make_any_throws<SmallThrows>();
144     test_make_any_throws<LargeThrows>();
145 
146 #endif
147 }
148