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, c++14
14 // any& operator=(any const &);
16 // Test copy assignment
21 #include "any_helpers.h"
22 #include "count_new.hpp"
23 #include "test_macros.h"
28 template <class LHS, class RHS>
29 void test_copy_assign() {
30 assert(LHS::count == 0);
31 assert(RHS::count == 0);
36 any const rhs(RHS(2));
38 assert(LHS::count == 1);
39 assert(RHS::count == 1);
40 assert(RHS::copied == 0);
44 assert(RHS::copied == 1);
45 assert(LHS::count == 0);
46 assert(RHS::count == 2);
48 assertContains<RHS>(lhs, 2);
49 assertContains<RHS>(rhs, 2);
51 assert(LHS::count == 0);
52 assert(RHS::count == 0);
56 void test_copy_assign_empty() {
57 assert(LHS::count == 0);
61 any const rhs(LHS(42));
63 assert(LHS::count == 1);
64 assert(LHS::copied == 0);
68 assert(LHS::copied == 1);
69 assert(LHS::count == 2);
71 assertContains<LHS>(lhs, 42);
72 assertContains<LHS>(rhs, 42);
74 assert(LHS::count == 0);
80 assert(LHS::count == 1);
81 assert(LHS::copied == 0);
85 assert(LHS::copied == 0);
86 assert(LHS::count == 0);
88 assertEmpty<LHS>(lhs);
91 assert(LHS::count == 0);
94 void test_copy_assign_self() {
100 assert(globalMemCounter.checkOutstandingNewEq(0));
102 assert(globalMemCounter.checkOutstandingNewEq(0));
106 assert(small::count == 1);
110 assert(small::count == 1);
111 assertContains<small>(a, 1);
112 assert(globalMemCounter.checkOutstandingNewEq(0));
114 assert(small::count == 0);
115 assert(globalMemCounter.checkOutstandingNewEq(0));
119 assert(large::count == 1);
123 assert(large::count == 1);
124 assertContains<large>(a, 1);
125 assert(globalMemCounter.checkOutstandingNewEq(1));
127 assert(large::count == 0);
128 assert(globalMemCounter.checkOutstandingNewEq(0));
132 void test_copy_assign_throws()
134 #if !defined(TEST_HAS_NO_EXCEPTIONS)
136 [](any& lhs, any const& rhs) {
140 } catch (my_any_exception const &) {
146 // const lvalue to empty
149 any const rhs((Tp(1)));
150 assert(Tp::count == 1);
154 assert(Tp::count == 1);
155 assertEmpty<Tp>(lhs);
156 assertContains<Tp>(rhs, 1);
160 any const rhs((Tp(1)));
161 assert(small::count == 1);
162 assert(Tp::count == 1);
166 assert(small::count == 1);
167 assert(Tp::count == 1);
168 assertContains<small>(lhs, 2);
169 assertContains<Tp>(rhs, 1);
173 any const rhs((Tp(1)));
174 assert(large::count == 1);
175 assert(Tp::count == 1);
179 assert(large::count == 1);
180 assert(Tp::count == 1);
181 assertContains<large>(lhs, 2);
182 assertContains<Tp>(rhs, 1);
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>();