1 //===----------------------------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // UNSUPPORTED: c++98, c++03, c++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
21 // any& operator=(any const &);
23 // Test copy assignment
25 #include <experimental/any>
28 #include "experimental_any_helpers.h"
29 #include "count_new.hpp"
30 #include "test_macros.h"
32 using std::experimental::any;
33 using std::experimental::any_cast;
35 template <class LHS, class RHS>
36 void test_copy_assign() {
37 assert(LHS::count == 0);
38 assert(RHS::count == 0);
43 any const rhs(RHS(2));
45 assert(LHS::count == 1);
46 assert(RHS::count == 1);
47 assert(RHS::copied == 0);
51 assert(RHS::copied == 1);
52 assert(LHS::count == 0);
53 assert(RHS::count == 2);
55 assertContains<RHS>(lhs, 2);
56 assertContains<RHS>(rhs, 2);
58 assert(LHS::count == 0);
59 assert(RHS::count == 0);
63 void test_copy_assign_empty() {
64 assert(LHS::count == 0);
68 any const rhs(LHS(42));
70 assert(LHS::count == 1);
71 assert(LHS::copied == 0);
75 assert(LHS::copied == 1);
76 assert(LHS::count == 2);
78 assertContains<LHS>(lhs, 42);
79 assertContains<LHS>(rhs, 42);
81 assert(LHS::count == 0);
87 assert(LHS::count == 1);
88 assert(LHS::copied == 0);
92 assert(LHS::copied == 0);
93 assert(LHS::count == 0);
95 assertEmpty<LHS>(lhs);
98 assert(LHS::count == 0);
101 void test_copy_assign_self() {
107 assert(globalMemCounter.checkOutstandingNewEq(0));
109 assert(globalMemCounter.checkOutstandingNewEq(0));
113 assert(small::count == 1);
117 assert(small::count == 1);
118 assertContains<small>(a, 1);
119 assert(globalMemCounter.checkOutstandingNewEq(0));
121 assert(small::count == 0);
122 assert(globalMemCounter.checkOutstandingNewEq(0));
126 assert(large::count == 1);
130 assert(large::count == 1);
131 assertContains<large>(a, 1);
132 assert(globalMemCounter.checkOutstandingNewEq(1));
134 assert(large::count == 0);
135 assert(globalMemCounter.checkOutstandingNewEq(0));
139 void test_copy_assign_throws()
141 #if !defined(TEST_HAS_NO_EXCEPTIONS)
143 [](any& lhs, any const& rhs) {
147 } catch (my_any_exception const &) {
153 // const lvalue to empty
156 any const rhs((Tp(1)));
157 assert(Tp::count == 1);
161 assert(Tp::count == 1);
162 assertEmpty<Tp>(lhs);
163 assertContains<Tp>(rhs);
167 any const rhs((Tp(1)));
168 assert(small::count == 1);
169 assert(Tp::count == 1);
173 assert(small::count == 1);
174 assert(Tp::count == 1);
175 assertContains<small>(lhs, 2);
176 assertContains<Tp>(rhs);
180 any const rhs((Tp(1)));
181 assert(large::count == 1);
182 assert(Tp::count == 1);
186 assert(large::count == 1);
187 assert(Tp::count == 1);
188 assertContains<large>(lhs, 2);
189 assertContains<Tp>(rhs);
195 test_copy_assign<small1, small2>();
196 test_copy_assign<large1, large2>();
197 test_copy_assign<small, large>();
198 test_copy_assign<large, small>();
199 test_copy_assign_empty<small>();
200 test_copy_assign_empty<large>();
201 test_copy_assign_self();
202 test_copy_assign_throws<small_throws_on_copy>();
203 test_copy_assign_throws<large_throws_on_copy>();