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