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