xref: /llvm-project/libcxx/test/std/utilities/any/any.class/any.assign/move.pass.cpp (revision ec350ad418a24f70c88758259c774a1e11c06d74)
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++03, c++11, c++14
10 
11 // <any>
12 
13 // any& operator=(any &&);
14 
15 // Test move assignment.
16 
17 #include <any>
18 #include <cassert>
19 
20 #include "any_helpers.h"
21 #include "test_macros.h"
22 
23 template <class LHS, class RHS>
test_move_assign()24 void test_move_assign() {
25     assert(LHS::count == 0);
26     assert(RHS::count == 0);
27     {
28         LHS const s1(1);
29         std::any a = s1;
30         RHS const s2(2);
31         std::any a2 = s2;
32 
33         assert(LHS::count == 2);
34         assert(RHS::count == 2);
35 
36         a = std::move(a2);
37 
38         assert(LHS::count == 1);
39         assert(RHS::count == 2 + a2.has_value());
40         LIBCPP_ASSERT(RHS::count == 2); // libc++ leaves the object empty
41 
42         assertContains<RHS>(a, 2);
43         if (a2.has_value())
44             assertContains<RHS>(a2, 0);
45         LIBCPP_ASSERT(!a2.has_value());
46     }
47     assert(LHS::count == 0);
48     assert(RHS::count == 0);
49 }
50 
51 template <class LHS>
test_move_assign_empty()52 void test_move_assign_empty() {
53     assert(LHS::count == 0);
54     {
55         std::any a;
56         std::any a2 = LHS(1);
57 
58         assert(LHS::count == 1);
59 
60         a = std::move(a2);
61 
62         assert(LHS::count == 1 + a2.has_value());
63         LIBCPP_ASSERT(LHS::count == 1);
64 
65         assertContains<LHS>(a, 1);
66         if (a2.has_value())
67             assertContains<LHS>(a2, 0);
68         LIBCPP_ASSERT(!a2.has_value());
69     }
70     assert(LHS::count == 0);
71     {
72         std::any a = LHS(1);
73         std::any a2;
74 
75         assert(LHS::count == 1);
76 
77         a = std::move(a2);
78 
79         assert(LHS::count == 0);
80 
81         assertEmpty<LHS>(a);
82         assertEmpty(a2);
83     }
84     assert(LHS::count == 0);
85 }
86 
test_move_assign_noexcept()87 void test_move_assign_noexcept() {
88     std::any a1;
89     std::any a2;
90     ASSERT_NOEXCEPT(a1 = std::move(a2));
91 }
92 
main(int,char **)93 int main(int, char**) {
94     test_move_assign_noexcept();
95     test_move_assign<small1, small2>();
96     test_move_assign<large1, large2>();
97     test_move_assign<small, large>();
98     test_move_assign<large, small>();
99     test_move_assign_empty<small>();
100     test_move_assign_empty<large>();
101 
102   return 0;
103 }
104