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