]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - test/std/utilities/any/any.class/any.modifiers/swap.pass.cpp
Vendor import of libc++ trunk r290819:
[FreeBSD/FreeBSD.git] / test / std / utilities / any / any.class / any.modifiers / swap.pass.cpp
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 // <any>
13
14 // any::swap(any &) noexcept
15
16 // Test swap(large, small) and swap(small, large)
17
18 #include <any>
19 #include <cassert>
20
21 #include "any_helpers.h"
22
23 using std::any;
24 using std::any_cast;
25
26 template <class LHS, class RHS>
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>
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
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
92 void test_self_swap() {
93     {
94         // empty
95         any a;
96         a.swap(a);
97         assertEmpty(a);
98     }
99     { // small
100         using T = small;
101         any a{T{42}};
102         T::reset();
103         a.swap(a);
104         assertContains<T>(a, 42);
105         assert(T::count == 1);
106         assert(T::copied == 0);
107         LIBCPP_ASSERT(T::moved == 0);
108     }
109     assert(small::count == 0);
110     { // large
111         using T = large;
112         any a{T{42}};
113         T::reset();
114         a.swap(a);
115         assertContains<T>(a, 42);
116         assert(T::count == 1);
117         assert(T::copied == 0);
118         LIBCPP_ASSERT(T::moved == 0);
119     }
120     assert(large::count == 0);
121 }
122
123 int main()
124 {
125     test_noexcept();
126     test_swap_empty<small>();
127     test_swap_empty<large>();
128     test_swap<small1, small2>();
129     test_swap<large1, large2>();
130     test_swap<small, large>();
131     test_swap<large, small>();
132     test_self_swap();
133 }