• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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, c++14
11 
12 // <any>
13 
14 // any& operator=(any &&);
15 
16 // Test move assignment.
17 
18 #include <any>
19 #include <cassert>
20 
21 #include "any_helpers.h"
22 #include "test_macros.h"
23 
24 using std::any;
25 using std::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 + a2.has_value());
44         LIBCPP_ASSERT(RHS::count == 2); // libc++ leaves the object empty
45 
46         assertContains<RHS>(a, 2);
47         if (a2.has_value())
48             assertContains<RHS>(a2, 0);
49         LIBCPP_ASSERT(!a2.has_value());
50     }
51     assert(LHS::count == 0);
52     assert(RHS::count == 0);
53 }
54 
55 template <class LHS>
test_move_assign_empty()56 void test_move_assign_empty() {
57     assert(LHS::count == 0);
58     {
59         any a;
60         any a2((LHS(1)));
61 
62         assert(LHS::count == 1);
63 
64         a = std::move(a2);
65 
66         assert(LHS::count == 1 + a2.has_value());
67         LIBCPP_ASSERT(LHS::count == 1);
68 
69         assertContains<LHS>(a, 1);
70         if (a2.has_value())
71             assertContains<LHS>(a2, 0);
72         LIBCPP_ASSERT(!a2.has_value());
73     }
74     assert(LHS::count == 0);
75     {
76         any a((LHS(1)));
77         any a2;
78 
79         assert(LHS::count == 1);
80 
81         a = std::move(a2);
82 
83         assert(LHS::count == 0);
84 
85         assertEmpty<LHS>(a);
86         assertEmpty(a2);
87     }
88     assert(LHS::count == 0);
89 }
90 
test_move_assign_noexcept()91 void test_move_assign_noexcept() {
92     any a1;
93     any a2;
94     static_assert(
95         noexcept(a1 = std::move(a2))
96       , "any & operator=(any &&) must be noexcept"
97       );
98 }
99 
main()100 int main() {
101     test_move_assign_noexcept();
102     test_move_assign<small1, small2>();
103     test_move_assign<large1, large2>();
104     test_move_assign<small, large>();
105     test_move_assign<large, small>();
106     test_move_assign_empty<small>();
107     test_move_assign_empty<large>();
108 }
109