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