Home | History | Annotate | Download | only in any.modifiers
      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
     11 
     12 // XFAIL: availability=macosx
     13 
     14 // <experimental/any>
     15 
     16 // any::swap(any &) noexcept
     17 
     18 // Test swap(large, small) and swap(small, large)
     19 
     20 #include <experimental/any>
     21 #include <cassert>
     22 
     23 #include "experimental_any_helpers.h"
     24 
     25 using std::experimental::any;
     26 using std::experimental::any_cast;
     27 
     28 template <class LHS, class RHS>
     29 void test_swap() {
     30     assert(LHS::count == 0);
     31     assert(RHS::count == 0);
     32     {
     33         any a1((LHS(1)));
     34         any a2(RHS{2});
     35         assert(LHS::count == 1);
     36         assert(RHS::count == 1);
     37 
     38         a1.swap(a2);
     39 
     40         assert(LHS::count == 1);
     41         assert(RHS::count == 1);
     42 
     43         assertContains<RHS>(a1, 2);
     44         assertContains<LHS>(a2, 1);
     45     }
     46     assert(LHS::count == 0);
     47     assert(RHS::count == 0);
     48     assert(LHS::copied == 0);
     49     assert(RHS::copied == 0);
     50 }
     51 
     52 template <class Tp>
     53 void test_swap_empty() {
     54     assert(Tp::count == 0);
     55     {
     56         any a1((Tp(1)));
     57         any a2;
     58         assert(Tp::count == 1);
     59 
     60         a1.swap(a2);
     61 
     62         assert(Tp::count == 1);
     63 
     64         assertContains<Tp>(a2, 1);
     65         assertEmpty(a1);
     66     }
     67     assert(Tp::count == 0);
     68     {
     69         any a1((Tp(1)));
     70         any a2;
     71         assert(Tp::count == 1);
     72 
     73         a2.swap(a1);
     74 
     75         assert(Tp::count == 1);
     76 
     77         assertContains<Tp>(a2, 1);
     78         assertEmpty(a1);
     79     }
     80     assert(Tp::count == 0);
     81     assert(Tp::copied == 0);
     82 }
     83 
     84 void test_noexcept()
     85 {
     86     any a1;
     87     any a2;
     88     static_assert(
     89         noexcept(a1.swap(a2))
     90       , "any::swap(any&) must be noexcept"
     91       );
     92 }
     93 
     94 int main()
     95 {
     96     test_noexcept();
     97     test_swap_empty<small>();
     98     test_swap_empty<large>();
     99     test_swap<small1, small2>();
    100     test_swap<large1, large2>();
    101     test_swap<small, large>();
    102     test_swap<large, small>();
    103 }
    104