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