Add PR check to suggest alternatives to using undef (#118506)
[llvm-project.git] / libcxx / test / std / algorithms / alg.modifying.operations / alg.swap / swap_ranges.pass.cpp
blob8bfbcd755e39f74322f698a5052efaab67e9a37e
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 // <algorithm>
11 // template<ForwardIterator Iter1, ForwardIterator Iter2>
12 // requires HasSwap<Iter1::reference, Iter2::reference>
13 // Iter2
14 // swap_ranges(Iter1 first1, Iter1 last1, Iter2 first2);
16 #include <algorithm>
17 #include <cassert>
18 #include <memory>
19 #include <utility>
21 #include "test_macros.h"
22 #include "test_iterators.h"
24 template<class Iter1, class Iter2>
25 void
26 test()
28 int i[3] = {1, 2, 3};
29 int j[3] = {4, 5, 6};
30 Iter2 r = std::swap_ranges(Iter1(i), Iter1(i+3), Iter2(j));
31 assert(base(r) == j+3);
32 assert(i[0] == 4);
33 assert(i[1] == 5);
34 assert(i[2] == 6);
35 assert(j[0] == 1);
36 assert(j[1] == 2);
37 assert(j[2] == 3);
40 #if TEST_STD_VER >= 11
41 template<class Iter1, class Iter2>
42 void
43 test1()
45 std::unique_ptr<int> i[3];
46 for (int k = 0; k < 3; ++k)
47 i[k].reset(new int(k+1));
48 std::unique_ptr<int> j[3];
49 for (int k = 0; k < 3; ++k)
50 j[k].reset(new int(k+4));
51 Iter2 r = std::swap_ranges(Iter1(i), Iter1(i+3), Iter2(j));
52 assert(base(r) == j+3);
53 assert(*i[0] == 4);
54 assert(*i[1] == 5);
55 assert(*i[2] == 6);
56 assert(*j[0] == 1);
57 assert(*j[1] == 2);
58 assert(*j[2] == 3);
60 #endif // TEST_STD_VER >= 11
62 void test2()
65 int src[2][2] = {{0, 1}, {2, 3}};
66 decltype(src) dest = {{9, 8}, {7, 6}};
68 std::swap(src, dest);
70 assert ( src[0][0] == 9 );
71 assert ( src[0][1] == 8 );
72 assert ( src[1][0] == 7 );
73 assert ( src[1][1] == 6 );
75 assert ( dest[0][0] == 0 );
76 assert ( dest[0][1] == 1 );
77 assert ( dest[1][0] == 2 );
78 assert ( dest[1][1] == 3 );
82 int src[3][3] = {{0, 1, 2}, {3, 4, 5}, {6, 7, 8}};
83 decltype(src) dest = {{9, 8, 7}, {6, 5, 4}, {3, 2, 1}};
85 std::swap(src, dest);
87 assert ( src[0][0] == 9 );
88 assert ( src[0][1] == 8 );
89 assert ( src[0][2] == 7 );
90 assert ( src[1][0] == 6 );
91 assert ( src[1][1] == 5 );
92 assert ( src[1][2] == 4 );
93 assert ( src[2][0] == 3 );
94 assert ( src[2][1] == 2 );
95 assert ( src[2][2] == 1 );
97 assert ( dest[0][0] == 0 );
98 assert ( dest[0][1] == 1 );
99 assert ( dest[0][2] == 2 );
100 assert ( dest[1][0] == 3 );
101 assert ( dest[1][1] == 4 );
102 assert ( dest[1][2] == 5 );
103 assert ( dest[2][0] == 6 );
104 assert ( dest[2][1] == 7 );
105 assert ( dest[2][2] == 8 );
109 #if TEST_STD_VER > 17
110 constexpr bool test_swap_constexpr()
112 int i[3] = {1, 2, 3};
113 int j[3] = {4, 5, 6};
114 std::swap_ranges(i, i+3, j);
115 return i[0] == 4 &&
116 i[1] == 5 &&
117 i[2] == 6 &&
118 j[0] == 1 &&
119 j[1] == 2 &&
120 j[2] == 3;
122 #endif // TEST_STD_VER > 17
124 int main(int, char**)
126 test<forward_iterator<int*>, forward_iterator<int*> >();
127 test<forward_iterator<int*>, bidirectional_iterator<int*> >();
128 test<forward_iterator<int*>, random_access_iterator<int*> >();
129 test<forward_iterator<int*>, int*>();
131 test<bidirectional_iterator<int*>, forward_iterator<int*> >();
132 test<bidirectional_iterator<int*>, bidirectional_iterator<int*> >();
133 test<bidirectional_iterator<int*>, random_access_iterator<int*> >();
134 test<bidirectional_iterator<int*>, int*>();
136 test<random_access_iterator<int*>, forward_iterator<int*> >();
137 test<random_access_iterator<int*>, bidirectional_iterator<int*> >();
138 test<random_access_iterator<int*>, random_access_iterator<int*> >();
139 test<random_access_iterator<int*>, int*>();
141 test<int*, forward_iterator<int*> >();
142 test<int*, bidirectional_iterator<int*> >();
143 test<int*, random_access_iterator<int*> >();
144 test<int*, int*>();
146 #if TEST_STD_VER >= 11
147 test1<forward_iterator<std::unique_ptr<int>*>, forward_iterator<std::unique_ptr<int>*> >();
148 test1<forward_iterator<std::unique_ptr<int>*>, bidirectional_iterator<std::unique_ptr<int>*> >();
149 test1<forward_iterator<std::unique_ptr<int>*>, random_access_iterator<std::unique_ptr<int>*> >();
150 test1<forward_iterator<std::unique_ptr<int>*>, std::unique_ptr<int>*>();
152 test1<bidirectional_iterator<std::unique_ptr<int>*>, forward_iterator<std::unique_ptr<int>*> >();
153 test1<bidirectional_iterator<std::unique_ptr<int>*>, bidirectional_iterator<std::unique_ptr<int>*> >();
154 test1<bidirectional_iterator<std::unique_ptr<int>*>, random_access_iterator<std::unique_ptr<int>*> >();
155 test1<bidirectional_iterator<std::unique_ptr<int>*>, std::unique_ptr<int>*>();
157 test1<random_access_iterator<std::unique_ptr<int>*>, forward_iterator<std::unique_ptr<int>*> >();
158 test1<random_access_iterator<std::unique_ptr<int>*>, bidirectional_iterator<std::unique_ptr<int>*> >();
159 test1<random_access_iterator<std::unique_ptr<int>*>, random_access_iterator<std::unique_ptr<int>*> >();
160 test1<random_access_iterator<std::unique_ptr<int>*>, std::unique_ptr<int>*>();
162 test1<std::unique_ptr<int>*, forward_iterator<std::unique_ptr<int>*> >();
163 test1<std::unique_ptr<int>*, bidirectional_iterator<std::unique_ptr<int>*> >();
164 test1<std::unique_ptr<int>*, random_access_iterator<std::unique_ptr<int>*> >();
165 test1<std::unique_ptr<int>*, std::unique_ptr<int>*>();
166 #endif // TEST_STD_VER >= 11
168 #if TEST_STD_VER > 17
169 static_assert(test_swap_constexpr());
170 #endif // TEST_STD_VER > 17
172 test2();
174 return 0;