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