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++03, c++11, c++14
10 
11 // Throwing bad_any_cast is supported starting in macosx10.13
12 // XFAIL: use_system_cxx_lib && target={{.+}}-apple-macosx10.{{9|10|11|12}} && !no-exceptions
13 
14 // <any>
15 
16 // any::swap(any &) noexcept
17 
18 // Test swap(large, small) and swap(small, large)
19 
20 #include <any>
21 #include <cassert>
22 
23 #include "test_macros.h"
24 #include "any_helpers.h"
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         std::any a1 = LHS(1);
32         std::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         std::any a1 = Tp(1);
55         std::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         std::any a1 = Tp(1);
68         std::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     std::any a1;
85     std::any a2;
86     ASSERT_NOEXCEPT(a1.swap(a2));
87 }
88 
test_self_swap()89 void test_self_swap() {
90     {
91         // empty
92         std::any a;
93         a.swap(a);
94         assertEmpty(a);
95     }
96     { // small
97         using T = small;
98         std::any a = T(42);
99         T::reset();
100         a.swap(a);
101         assertContains<T>(a, 42);
102         assert(T::count == 1);
103         assert(T::copied == 0);
104         LIBCPP_ASSERT(T::moved == 0);
105     }
106     assert(small::count == 0);
107     { // large
108         using T = large;
109         std::any a = T(42);
110         T::reset();
111         a.swap(a);
112         assertContains<T>(a, 42);
113         assert(T::count == 1);
114         assert(T::copied == 0);
115         LIBCPP_ASSERT(T::moved == 0);
116     }
117     assert(large::count == 0);
118 }
119 
main(int,char **)120 int main(int, char**)
121 {
122     test_noexcept();
123     test_swap_empty<small>();
124     test_swap_empty<large>();
125     test_swap<small1, small2>();
126     test_swap<large1, large2>();
127     test_swap<small, large>();
128     test_swap<large, small>();
129     test_self_swap();
130 
131   return 0;
132 }
133