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: availability=macosx10.13
13 // XFAIL: availability=macosx10.12
14 // XFAIL: availability=macosx10.11
15 // XFAIL: availability=macosx10.10
16 // XFAIL: availability=macosx10.9
17 // XFAIL: availability=macosx10.8
18 // XFAIL: availability=macosx10.7
19
20 // <any>
21
22 // any::swap(any &) noexcept
23
24 // Test swap(large, small) and swap(small, large)
25
26 #include <any>
27 #include <cassert>
28
29 #include "any_helpers.h"
30
31 using std::any;
32 using std::any_cast;
33
34 template <class LHS, class RHS>
test_swap()35 void test_swap() {
36 assert(LHS::count == 0);
37 assert(RHS::count == 0);
38 {
39 any a1((LHS(1)));
40 any a2(RHS{2});
41 assert(LHS::count == 1);
42 assert(RHS::count == 1);
43
44 a1.swap(a2);
45
46 assert(LHS::count == 1);
47 assert(RHS::count == 1);
48
49 assertContains<RHS>(a1, 2);
50 assertContains<LHS>(a2, 1);
51 }
52 assert(LHS::count == 0);
53 assert(RHS::count == 0);
54 assert(LHS::copied == 0);
55 assert(RHS::copied == 0);
56 }
57
58 template <class Tp>
test_swap_empty()59 void test_swap_empty() {
60 assert(Tp::count == 0);
61 {
62 any a1((Tp(1)));
63 any a2;
64 assert(Tp::count == 1);
65
66 a1.swap(a2);
67
68 assert(Tp::count == 1);
69
70 assertContains<Tp>(a2, 1);
71 assertEmpty(a1);
72 }
73 assert(Tp::count == 0);
74 {
75 any a1((Tp(1)));
76 any a2;
77 assert(Tp::count == 1);
78
79 a2.swap(a1);
80
81 assert(Tp::count == 1);
82
83 assertContains<Tp>(a2, 1);
84 assertEmpty(a1);
85 }
86 assert(Tp::count == 0);
87 assert(Tp::copied == 0);
88 }
89
test_noexcept()90 void test_noexcept()
91 {
92 any a1;
93 any a2;
94 static_assert(
95 noexcept(a1.swap(a2))
96 , "any::swap(any&) must be noexcept"
97 );
98 }
99
test_self_swap()100 void test_self_swap() {
101 {
102 // empty
103 any a;
104 a.swap(a);
105 assertEmpty(a);
106 }
107 { // small
108 using T = small;
109 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(small::count == 0);
118 { // large
119 using T = large;
120 any a{T{42}};
121 T::reset();
122 a.swap(a);
123 assertContains<T>(a, 42);
124 assert(T::count == 1);
125 assert(T::copied == 0);
126 LIBCPP_ASSERT(T::moved == 0);
127 }
128 assert(large::count == 0);
129 }
130
main()131 int main()
132 {
133 test_noexcept();
134 test_swap_empty<small>();
135 test_swap_empty<large>();
136 test_swap<small1, small2>();
137 test_swap<large1, large2>();
138 test_swap<small, large>();
139 test_swap<large, small>();
140 test_self_swap();
141 }
142