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