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 value copy and move 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_assign_value() {
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);
66 assert(LHS::count == 1);
67 assert(RHS::count == 1);
68 assert(RHS::moved == 1);
72 assert(RHS::moved >= 1);
73 assert(RHS::copied == 0);
74 assert(LHS::count == 0);
75 assert(RHS::count == 1);
77 assertContains<RHS>(lhs, 2);
78 assertEmpty<RHS>(rhs);
80 assert(LHS::count == 0);
81 assert(RHS::count == 0);
85 void test_assign_value_empty() {
86 assert(RHS::count == 0);
91 assert(RHS::count == 1);
92 assert(RHS::copied == 0);
96 assert(RHS::count == 2);
97 assert(RHS::copied == 1);
98 assert(RHS::moved >= 0);
99 assertContains<RHS>(lhs, 42);
101 assert(RHS::count == 0);
106 assert(RHS::count == 1);
107 assert(RHS::moved == 0);
109 lhs = std::move(rhs);
111 assert(RHS::count == 2);
112 assert(RHS::copied == 0);
113 assert(RHS::moved >= 1);
114 assertContains<RHS>(lhs, 42);
116 assert(RHS::count == 0);
121 template <class Tp, bool Move = false>
122 void test_assign_throws() {
123 #if !defined(TEST_HAS_NO_EXCEPTIONS)
125 [](any& lhs, auto&& rhs) {
127 Move ? lhs = std::move(rhs)
130 } catch (my_any_exception const &) {
136 // const lvalue to empty
140 assert(Tp::count == 1);
144 assert(Tp::count == 1);
145 assertEmpty<Tp>(lhs);
150 assert(small::count == 1);
151 assert(Tp::count == 1);
155 assert(small::count == 1);
156 assert(Tp::count == 1);
157 assertContains<small>(lhs, 2);
162 assert(large::count == 1);
163 assert(Tp::count == 1);
167 assert(large::count == 1);
168 assert(Tp::count == 1);
169 assertContains<large>(lhs, 2);
175 test_assign_value<small1, small2>();
176 test_assign_value<large1, large2>();
177 test_assign_value<small, large>();
178 test_assign_value<large, small>();
179 test_assign_value_empty<small>();
180 test_assign_value_empty<large>();
181 test_assign_throws<small_throws_on_copy>();
182 test_assign_throws<large_throws_on_copy>();
183 test_assign_throws<throws_on_move, /* Move = */ true>();