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