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 // Throwing bad_any_cast is supported starting in macosx10.13
12 // XFAIL: use_system_cxx_lib && target={{.+}}-apple-macosx10.{{9|10|11|12}} && !no-exceptions
13
14 // <any>
15
16 // any& operator=(const any&);
17
18 // Test copy assignment
19
20 #include <any>
21 #include <cassert>
22
23 #include "any_helpers.h"
24 #include "count_new.h"
25 #include "test_macros.h"
26
27 template <class LHS, class RHS>
test_copy_assign()28 void test_copy_assign() {
29 assert(LHS::count == 0);
30 assert(RHS::count == 0);
31 LHS::reset();
32 RHS::reset();
33 {
34 std::any lhs = LHS(1);
35 const std::any rhs = RHS(2);
36
37 assert(LHS::count == 1);
38 assert(RHS::count == 1);
39 assert(RHS::copied == 0);
40
41 lhs = rhs;
42
43 assert(RHS::copied == 1);
44 assert(LHS::count == 0);
45 assert(RHS::count == 2);
46
47 assertContains<RHS>(lhs, 2);
48 assertContains<RHS>(rhs, 2);
49 }
50 assert(LHS::count == 0);
51 assert(RHS::count == 0);
52 }
53
54 template <class LHS>
test_copy_assign_empty()55 void test_copy_assign_empty() {
56 assert(LHS::count == 0);
57 LHS::reset();
58 {
59 std::any lhs;
60 const std::any rhs = LHS(42);
61
62 assert(LHS::count == 1);
63 assert(LHS::copied == 0);
64
65 lhs = rhs;
66
67 assert(LHS::copied == 1);
68 assert(LHS::count == 2);
69
70 assertContains<LHS>(lhs, 42);
71 assertContains<LHS>(rhs, 42);
72 }
73 assert(LHS::count == 0);
74 LHS::reset();
75 {
76 std::any lhs = LHS(1);
77 const std::any rhs;
78
79 assert(LHS::count == 1);
80 assert(LHS::copied == 0);
81
82 lhs = rhs;
83
84 assert(LHS::copied == 0);
85 assert(LHS::count == 0);
86
87 assertEmpty<LHS>(lhs);
88 assertEmpty(rhs);
89 }
90 assert(LHS::count == 0);
91 }
92
test_copy_assign_self()93 void test_copy_assign_self() {
94 // empty
95 {
96 std::any a;
97 a = (std::any&)a;
98 assertEmpty(a);
99 assert(globalMemCounter.checkOutstandingNewEq(0));
100 }
101 assert(globalMemCounter.checkOutstandingNewEq(0));
102 // small
103 {
104 std::any a = small(1);
105 assert(small::count == 1);
106
107 a = (std::any&)a;
108
109 assert(small::count == 1);
110 assertContains<small>(a, 1);
111 assert(globalMemCounter.checkOutstandingNewEq(0));
112 }
113 assert(small::count == 0);
114 assert(globalMemCounter.checkOutstandingNewEq(0));
115 // large
116 {
117 std::any a = large(1);
118 assert(large::count == 1);
119
120 a = (std::any&)a;
121
122 assert(large::count == 1);
123 assertContains<large>(a, 1);
124 assert(globalMemCounter.checkOutstandingNewEq(1));
125 }
126 assert(large::count == 0);
127 assert(globalMemCounter.checkOutstandingNewEq(0));
128 }
129
130 template <class Tp>
test_copy_assign_throws()131 void test_copy_assign_throws()
132 {
133 #if !defined(TEST_HAS_NO_EXCEPTIONS)
134 auto try_throw =
135 [](std::any& lhs, const std::any& rhs) {
136 try {
137 lhs = rhs;
138 assert(false);
139 } catch (const my_any_exception&) {
140 // do nothing
141 } catch (...) {
142 assert(false);
143 }
144 };
145 // const lvalue to empty
146 {
147 std::any lhs;
148 const std::any rhs = Tp(1);
149 assert(Tp::count == 1);
150
151 try_throw(lhs, rhs);
152
153 assert(Tp::count == 1);
154 assertEmpty<Tp>(lhs);
155 assertContains<Tp>(rhs, 1);
156 }
157 {
158 std::any lhs = small(2);
159 const std::any rhs = Tp(1);
160 assert(small::count == 1);
161 assert(Tp::count == 1);
162
163 try_throw(lhs, rhs);
164
165 assert(small::count == 1);
166 assert(Tp::count == 1);
167 assertContains<small>(lhs, 2);
168 assertContains<Tp>(rhs, 1);
169 }
170 {
171 std::any lhs = large(2);
172 const std::any rhs = Tp(1);
173 assert(large::count == 1);
174 assert(Tp::count == 1);
175
176 try_throw(lhs, rhs);
177
178 assert(large::count == 1);
179 assert(Tp::count == 1);
180 assertContains<large>(lhs, 2);
181 assertContains<Tp>(rhs, 1);
182 }
183 #endif
184 }
185
main(int,char **)186 int main(int, char**) {
187 globalMemCounter.reset();
188 test_copy_assign<small1, small2>();
189 test_copy_assign<large1, large2>();
190 test_copy_assign<small, large>();
191 test_copy_assign<large, small>();
192 test_copy_assign_empty<small>();
193 test_copy_assign_empty<large>();
194 test_copy_assign_self();
195 test_copy_assign_throws<small_throws_on_copy>();
196 test_copy_assign_throws<large_throws_on_copy>();
197
198 return 0;
199 }
200