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