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

source code of libcxx/test/std/utilities/any/any.class/any.modifiers/swap.pass.cpp