xref: /llvm-project/libcxx/test/std/algorithms/alg.modifying.operations/alg.swap/swap_ranges.pass.cpp (revision 57b08b0944046a6a57ee9b7b479181f548a5b9b4)
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 // <algorithm>
10 
11 // template<ForwardIterator Iter1, ForwardIterator Iter2>
12 //   requires HasSwap<Iter1::reference, Iter2::reference>
13 //   Iter2
14 //   swap_ranges(Iter1 first1, Iter1 last1, Iter2 first2);
15 
16 #include <algorithm>
17 #include <cassert>
18 #include <memory>
19 
20 #include "test_macros.h"
21 #include "test_iterators.h"
22 
23 template<class Iter1, class Iter2>
24 void
25 test()
26 {
27     int i[3] = {1, 2, 3};
28     int j[3] = {4, 5, 6};
29     Iter2 r = std::swap_ranges(Iter1(i), Iter1(i+3), Iter2(j));
30     assert(base(r) == j+3);
31     assert(i[0] == 4);
32     assert(i[1] == 5);
33     assert(i[2] == 6);
34     assert(j[0] == 1);
35     assert(j[1] == 2);
36     assert(j[2] == 3);
37 }
38 
39 #if TEST_STD_VER >= 11
40 template<class Iter1, class Iter2>
41 void
42 test1()
43 {
44     std::unique_ptr<int> i[3];
45     for (int k = 0; k < 3; ++k)
46         i[k].reset(new int(k+1));
47     std::unique_ptr<int> j[3];
48     for (int k = 0; k < 3; ++k)
49         j[k].reset(new int(k+4));
50     Iter2 r = std::swap_ranges(Iter1(i), Iter1(i+3), Iter2(j));
51     assert(base(r) == j+3);
52     assert(*i[0] == 4);
53     assert(*i[1] == 5);
54     assert(*i[2] == 6);
55     assert(*j[0] == 1);
56     assert(*j[1] == 2);
57     assert(*j[2] == 3);
58 }
59 #endif // TEST_STD_VER >= 11
60 
61 void test2()
62 {
63     {
64     int src[2][2]      = {{0, 1}, {2, 3}};
65     decltype(src) dest = {{9, 8}, {7, 6}};
66 
67     std::swap(src, dest);
68 
69     assert ( src[0][0] == 9 );
70     assert ( src[0][1] == 8 );
71     assert ( src[1][0] == 7 );
72     assert ( src[1][1] == 6 );
73 
74     assert ( dest[0][0] == 0 );
75     assert ( dest[0][1] == 1 );
76     assert ( dest[1][0] == 2 );
77     assert ( dest[1][1] == 3 );
78     }
79 
80     {
81     int src[3][3]      = {{0, 1, 2}, {3, 4, 5}, {6, 7, 8}};
82     decltype(src) dest = {{9, 8, 7}, {6, 5, 4}, {3, 2, 1}};
83 
84     std::swap(src, dest);
85 
86     assert ( src[0][0] == 9 );
87     assert ( src[0][1] == 8 );
88     assert ( src[0][2] == 7 );
89     assert ( src[1][0] == 6 );
90     assert ( src[1][1] == 5 );
91     assert ( src[1][2] == 4 );
92     assert ( src[2][0] == 3 );
93     assert ( src[2][1] == 2 );
94     assert ( src[2][2] == 1 );
95 
96     assert ( dest[0][0] == 0 );
97     assert ( dest[0][1] == 1 );
98     assert ( dest[0][2] == 2 );
99     assert ( dest[1][0] == 3 );
100     assert ( dest[1][1] == 4 );
101     assert ( dest[1][2] == 5 );
102     assert ( dest[2][0] == 6 );
103     assert ( dest[2][1] == 7 );
104     assert ( dest[2][2] == 8 );
105     }
106 }
107 
108 int main()
109 {
110     test<forward_iterator<int*>, forward_iterator<int*> >();
111     test<forward_iterator<int*>, bidirectional_iterator<int*> >();
112     test<forward_iterator<int*>, random_access_iterator<int*> >();
113     test<forward_iterator<int*>, int*>();
114 
115     test<bidirectional_iterator<int*>, forward_iterator<int*> >();
116     test<bidirectional_iterator<int*>, bidirectional_iterator<int*> >();
117     test<bidirectional_iterator<int*>, random_access_iterator<int*> >();
118     test<bidirectional_iterator<int*>, int*>();
119 
120     test<random_access_iterator<int*>, forward_iterator<int*> >();
121     test<random_access_iterator<int*>, bidirectional_iterator<int*> >();
122     test<random_access_iterator<int*>, random_access_iterator<int*> >();
123     test<random_access_iterator<int*>, int*>();
124 
125     test<int*, forward_iterator<int*> >();
126     test<int*, bidirectional_iterator<int*> >();
127     test<int*, random_access_iterator<int*> >();
128     test<int*, int*>();
129 
130 #if TEST_STD_VER >= 11
131     test1<forward_iterator<std::unique_ptr<int>*>, forward_iterator<std::unique_ptr<int>*> >();
132     test1<forward_iterator<std::unique_ptr<int>*>, bidirectional_iterator<std::unique_ptr<int>*> >();
133     test1<forward_iterator<std::unique_ptr<int>*>, random_access_iterator<std::unique_ptr<int>*> >();
134     test1<forward_iterator<std::unique_ptr<int>*>, std::unique_ptr<int>*>();
135 
136     test1<bidirectional_iterator<std::unique_ptr<int>*>, forward_iterator<std::unique_ptr<int>*> >();
137     test1<bidirectional_iterator<std::unique_ptr<int>*>, bidirectional_iterator<std::unique_ptr<int>*> >();
138     test1<bidirectional_iterator<std::unique_ptr<int>*>, random_access_iterator<std::unique_ptr<int>*> >();
139     test1<bidirectional_iterator<std::unique_ptr<int>*>, std::unique_ptr<int>*>();
140 
141     test1<random_access_iterator<std::unique_ptr<int>*>, forward_iterator<std::unique_ptr<int>*> >();
142     test1<random_access_iterator<std::unique_ptr<int>*>, bidirectional_iterator<std::unique_ptr<int>*> >();
143     test1<random_access_iterator<std::unique_ptr<int>*>, random_access_iterator<std::unique_ptr<int>*> >();
144     test1<random_access_iterator<std::unique_ptr<int>*>, std::unique_ptr<int>*>();
145 
146     test1<std::unique_ptr<int>*, forward_iterator<std::unique_ptr<int>*> >();
147     test1<std::unique_ptr<int>*, bidirectional_iterator<std::unique_ptr<int>*> >();
148     test1<std::unique_ptr<int>*, random_access_iterator<std::unique_ptr<int>*> >();
149     test1<std::unique_ptr<int>*, std::unique_ptr<int>*>();
150 #endif // TEST_STD_VER >= 11
151 
152     test2();
153 }
154