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