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