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::swap(any &) noexcept
16 
17 // Test swap(large, small) and swap(small, large)
18 
19 #include <any>
20 #include <cassert>
21 
22 #include "test_macros.h"
23 #include "any_helpers.h"
24 
25 using std::any;
26 using std::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 void test_self_swap() {
95     {
96         // empty
97         any a;
98         a.swap(a);
99         assertEmpty(a);
100     }
101     { // small
102         using T = small;
103         any a{T{42}};
104         T::reset();
105         a.swap(a);
106         assertContains<T>(a, 42);
107         assert(T::count == 1);
108         assert(T::copied == 0);
109         LIBCPP_ASSERT(T::moved == 0);
110     }
111     assert(small::count == 0);
112     { // large
113         using T = large;
114         any a{T{42}};
115         T::reset();
116         a.swap(a);
117         assertContains<T>(a, 42);
118         assert(T::count == 1);
119         assert(T::copied == 0);
120         LIBCPP_ASSERT(T::moved == 0);
121     }
122     assert(large::count == 0);
123 }
124 
125 int main(int, char**)
126 {
127     test_noexcept();
128     test_swap_empty<small>();
129     test_swap_empty<large>();
130     test_swap<small1, small2>();
131     test_swap<large1, large2>();
132     test_swap<small, large>();
133     test_swap<large, small>();
134     test_self_swap();
135 
136   return 0;
137 }
138