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