1 //===----------------------------------------------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 // UNSUPPORTED: c++98, c++03, c++11
11
12 // <experimental/any>
13
14 // any& operator=(any &&);
15
16 // Test move assignment.
17
18 #include <experimental/any>
19 #include <cassert>
20
21 #include "experimental_any_helpers.h"
22 #include "test_macros.h"
23
24 using std::experimental::any;
25 using std::experimental::any_cast;
26
27 template <class LHS, class RHS>
test_move_assign()28 void test_move_assign() {
29 assert(LHS::count == 0);
30 assert(RHS::count == 0);
31 {
32 LHS const s1(1);
33 any a(s1);
34 RHS const s2(2);
35 any a2(s2);
36
37 assert(LHS::count == 2);
38 assert(RHS::count == 2);
39
40 a = std::move(a2);
41
42 assert(LHS::count == 1);
43 assert(RHS::count == 2);
44
45 assertContains<RHS>(a, 2);
46 assertEmpty<RHS>(a2);
47 }
48 assert(LHS::count == 0);
49 assert(RHS::count == 0);
50 }
51
52 template <class LHS>
test_move_assign_empty()53 void test_move_assign_empty() {
54 assert(LHS::count == 0);
55 {
56 any a;
57 any a2((LHS(1)));
58
59 assert(LHS::count == 1);
60
61 a = std::move(a2);
62
63 assert(LHS::count == 1);
64
65 assertContains<LHS>(a, 1);
66 assertEmpty<LHS>(a2);
67 }
68 assert(LHS::count == 0);
69 {
70 any a((LHS(1)));
71 any a2;
72
73 assert(LHS::count == 1);
74
75 a = std::move(a2);
76
77 assert(LHS::count == 0);
78
79 assertEmpty<LHS>(a);
80 assertEmpty(a2);
81 }
82 assert(LHS::count == 0);
83 }
84
test_move_assign_noexcept()85 void test_move_assign_noexcept() {
86 any a1;
87 any a2;
88 static_assert(
89 noexcept(a1 = std::move(a2))
90 , "any & operator=(any &&) must be noexcept"
91 );
92 }
93
main()94 int main() {
95 test_move_assign_noexcept();
96 test_move_assign<small1, small2>();
97 test_move_assign<large1, large2>();
98 test_move_assign<small, large>();
99 test_move_assign<large, small>();
100 test_move_assign_empty<small>();
101 test_move_assign_empty<large>();
102 }
103