Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / test / std / utilities / utility / utility.swap / swap_array.pass.cpp
blob3d562ddd3fc61c52a0eebd3b39befae1fa19a8fb
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 // <utility>
11 // template<ValueType T, size_t N>
12 // requires Swappable<T>
13 // void
14 // swap(T (&a)[N], T (&b)[N]);
16 #include <algorithm>
17 #include <cassert>
18 #include <memory>
19 #include <type_traits>
20 #include <utility>
22 #include "test_macros.h"
25 #if TEST_STD_VER >= 11
26 struct CopyOnly {
27 CopyOnly() {}
28 CopyOnly(CopyOnly const&) noexcept {}
29 CopyOnly& operator=(CopyOnly const&) { return *this; }
33 struct NoexceptMoveOnly {
34 NoexceptMoveOnly() {}
35 NoexceptMoveOnly(NoexceptMoveOnly&&) noexcept {}
36 NoexceptMoveOnly& operator=(NoexceptMoveOnly&&) noexcept { return *this; }
39 struct NotMoveConstructible {
40 NotMoveConstructible() {}
41 NotMoveConstructible& operator=(NotMoveConstructible&&) { return *this; }
42 private:
43 NotMoveConstructible(NotMoveConstructible&&);
46 template <class Tp>
47 auto can_swap_test(int) -> decltype(std::swap(std::declval<Tp>(), std::declval<Tp>()));
49 template <class Tp>
50 auto can_swap_test(...) -> std::false_type;
52 template <class Tp>
53 constexpr bool can_swap() {
54 return std::is_same<decltype(can_swap_test<Tp>(0)), void>::value;
56 #endif
58 #if TEST_STD_VER > 17
59 constexpr bool test_swap_constexpr()
61 int i[3] = {1, 2, 3};
62 int j[3] = {4, 5, 6};
63 std::swap(i, j);
64 return i[0] == 4 &&
65 i[1] == 5 &&
66 i[2] == 6 &&
67 j[0] == 1 &&
68 j[1] == 2 &&
69 j[2] == 3;
71 #endif // TEST_STD_VER > 17
73 int main(int, char**)
76 int i[3] = {1, 2, 3};
77 int j[3] = {4, 5, 6};
78 std::swap(i, j);
79 assert(i[0] == 4);
80 assert(i[1] == 5);
81 assert(i[2] == 6);
82 assert(j[0] == 1);
83 assert(j[1] == 2);
84 assert(j[2] == 3);
86 #if TEST_STD_VER >= 11
88 std::unique_ptr<int> i[3];
89 for (int k = 0; k < 3; ++k)
90 i[k].reset(new int(k+1));
91 std::unique_ptr<int> j[3];
92 for (int k = 0; k < 3; ++k)
93 j[k].reset(new int(k+4));
94 std::swap(i, j);
95 assert(*i[0] == 4);
96 assert(*i[1] == 5);
97 assert(*i[2] == 6);
98 assert(*j[0] == 1);
99 assert(*j[1] == 2);
100 assert(*j[2] == 3);
103 using CA = CopyOnly[42];
104 using MA = NoexceptMoveOnly[42];
105 using NA = NotMoveConstructible[42];
106 static_assert(can_swap<CA&>(), "");
107 static_assert(can_swap<MA&>(), "");
108 static_assert(!can_swap<NA&>(), "");
110 CA ca;
111 MA ma;
112 static_assert(!noexcept(std::swap(ca, ca)), "");
113 static_assert(noexcept(std::swap(ma, ma)), "");
115 #endif
117 #if TEST_STD_VER > 17
118 static_assert(test_swap_constexpr());
119 #endif // TEST_STD_VER > 17
121 return 0;