[libc++][doc] Updates format status.
[llvm-project.git] / libcxx / test / std / containers / sequences / array / array.special / swap.pass.cpp
blob94974295dfc1081cbca5251f478c683eae848687
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 //===----------------------------------------------------------------------===//
9 // <array>
11 // template <class T, size_t N> void swap(array<T,N>& x, array<T,N>& y);
13 #include <array>
14 #include <cassert>
15 #include <utility>
17 #include "test_macros.h"
19 struct NonSwappable {
20 TEST_CONSTEXPR NonSwappable() { }
21 private:
22 NonSwappable(NonSwappable const&);
23 NonSwappable& operator=(NonSwappable const&);
26 template <class Tp>
27 decltype(swap(std::declval<Tp>(), std::declval<Tp>()))
28 can_swap_imp(int);
30 template <class Tp>
31 std::false_type can_swap_imp(...);
33 template <class Tp>
34 struct can_swap : std::is_same<decltype(can_swap_imp<Tp>(0)), void> { };
36 TEST_CONSTEXPR_CXX20 bool tests()
39 typedef double T;
40 typedef std::array<T, 3> C;
41 C c1 = {1, 2, 3.5};
42 C c2 = {4, 5, 6.5};
43 swap(c1, c2);
44 assert(c1.size() == 3);
45 assert(c1[0] == 4);
46 assert(c1[1] == 5);
47 assert(c1[2] == 6.5);
48 assert(c2.size() == 3);
49 assert(c2[0] == 1);
50 assert(c2[1] == 2);
51 assert(c2[2] == 3.5);
54 typedef double T;
55 typedef std::array<T, 0> C;
56 C c1 = {};
57 C c2 = {};
58 swap(c1, c2);
59 assert(c1.size() == 0);
60 assert(c2.size() == 0);
63 typedef NonSwappable T;
64 typedef std::array<T, 0> C0;
65 static_assert(can_swap<C0&>::value, "");
66 C0 l = {};
67 C0 r = {};
68 swap(l, r);
69 #if TEST_STD_VER >= 11
70 static_assert(noexcept(swap(l, r)), "");
71 #endif
73 #if TEST_STD_VER >= 11
75 // NonSwappable is still considered swappable in C++03 because there
76 // is no access control SFINAE.
77 typedef NonSwappable T;
78 typedef std::array<T, 42> C1;
79 static_assert(!can_swap<C1&>::value, "");
81 #endif
83 return true;
86 int main(int, char**)
88 tests();
89 #if TEST_STD_VER >= 20
90 static_assert(tests(), "");
91 #endif
92 return 0;