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
main()65 int main()
66 {
67 test<forward_iterator<int*>, forward_iterator<int*> >();
68 test<forward_iterator<int*>, bidirectional_iterator<int*> >();
69 test<forward_iterator<int*>, random_access_iterator<int*> >();
70 test<forward_iterator<int*>, int*>();
71
72 test<bidirectional_iterator<int*>, forward_iterator<int*> >();
73 test<bidirectional_iterator<int*>, bidirectional_iterator<int*> >();
74 test<bidirectional_iterator<int*>, random_access_iterator<int*> >();
75 test<bidirectional_iterator<int*>, int*>();
76
77 test<random_access_iterator<int*>, forward_iterator<int*> >();
78 test<random_access_iterator<int*>, bidirectional_iterator<int*> >();
79 test<random_access_iterator<int*>, random_access_iterator<int*> >();
80 test<random_access_iterator<int*>, int*>();
81
82 test<int*, forward_iterator<int*> >();
83 test<int*, bidirectional_iterator<int*> >();
84 test<int*, random_access_iterator<int*> >();
85 test<int*, int*>();
86
87 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
88
89 test1<forward_iterator<std::unique_ptr<int>*>, forward_iterator<std::unique_ptr<int>*> >();
90 test1<forward_iterator<std::unique_ptr<int>*>, bidirectional_iterator<std::unique_ptr<int>*> >();
91 test1<forward_iterator<std::unique_ptr<int>*>, random_access_iterator<std::unique_ptr<int>*> >();
92 test1<forward_iterator<std::unique_ptr<int>*>, std::unique_ptr<int>*>();
93
94 test1<bidirectional_iterator<std::unique_ptr<int>*>, forward_iterator<std::unique_ptr<int>*> >();
95 test1<bidirectional_iterator<std::unique_ptr<int>*>, bidirectional_iterator<std::unique_ptr<int>*> >();
96 test1<bidirectional_iterator<std::unique_ptr<int>*>, random_access_iterator<std::unique_ptr<int>*> >();
97 test1<bidirectional_iterator<std::unique_ptr<int>*>, std::unique_ptr<int>*>();
98
99 test1<random_access_iterator<std::unique_ptr<int>*>, forward_iterator<std::unique_ptr<int>*> >();
100 test1<random_access_iterator<std::unique_ptr<int>*>, bidirectional_iterator<std::unique_ptr<int>*> >();
101 test1<random_access_iterator<std::unique_ptr<int>*>, random_access_iterator<std::unique_ptr<int>*> >();
102 test1<random_access_iterator<std::unique_ptr<int>*>, std::unique_ptr<int>*>();
103
104 test1<std::unique_ptr<int>*, forward_iterator<std::unique_ptr<int>*> >();
105 test1<std::unique_ptr<int>*, bidirectional_iterator<std::unique_ptr<int>*> >();
106 test1<std::unique_ptr<int>*, random_access_iterator<std::unique_ptr<int>*> >();
107 test1<std::unique_ptr<int>*, std::unique_ptr<int>*>();
108
109 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
110 }
111