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 InIter, OutputIterator<auto, InIter::reference> OutIter>
13 // constexpr OutIter // constexpr after C++17
14 // rotate_copy(InIter first, InIter middle, InIter last, OutIter result);
15
16 #include <algorithm>
17 #include <cassert>
18
19 #include "test_macros.h"
20 #include "test_iterators.h"
21
22 // #if TEST_STD_VER > 17
23 // TEST_CONSTEXPR bool test_constexpr() {
24 // int ia[] = {1, 3, 5, 2, 5, 6};
25 // int ib[std::size(ia)] = {0};
26 //
27 // const size_t N = 2;
28 // const auto middle = std::begin(ia) + N;
29 // auto it = std::rotate_copy(std::begin(ia), middle, std::end(ia), std::begin(ib));
30 //
31 // return std::distance(std::begin(ib), it) == std::size(ia)
32 // && std::equal (std::begin(ia), middle, std::begin(ib) + std::size(ia) - N)
33 // && std::equal (middle, std::end(ia), std::begin(ib))
34 // ;
35 // }
36 // #endif
37
38 template <class InIter, class OutIter>
39 void
test()40 test()
41 {
42 int ia[] = {0, 1, 2, 3};
43 const unsigned sa = sizeof(ia)/sizeof(ia[0]);
44 int ib[sa] = {0};
45
46 OutIter r = std::rotate_copy(InIter(ia), InIter(ia), InIter(ia), OutIter(ib));
47 assert(base(r) == ib);
48
49 r = std::rotate_copy(InIter(ia), InIter(ia), InIter(ia+1), OutIter(ib));
50 assert(base(r) == ib+1);
51 assert(ib[0] == 0);
52
53 r = std::rotate_copy(InIter(ia), InIter(ia+1), InIter(ia+1), OutIter(ib));
54 assert(base(r) == ib+1);
55 assert(ib[0] == 0);
56
57 r = std::rotate_copy(InIter(ia), InIter(ia), InIter(ia+2), OutIter(ib));
58 assert(base(r) == ib+2);
59 assert(ib[0] == 0);
60 assert(ib[1] == 1);
61
62 r = std::rotate_copy(InIter(ia), InIter(ia+1), InIter(ia+2), OutIter(ib));
63 assert(base(r) == ib+2);
64 assert(ib[0] == 1);
65 assert(ib[1] == 0);
66
67 r = std::rotate_copy(InIter(ia), InIter(ia+2), InIter(ia+2), OutIter(ib));
68 assert(base(r) == ib+2);
69 assert(ib[0] == 0);
70 assert(ib[1] == 1);
71
72 r = std::rotate_copy(InIter(ia), InIter(ia), InIter(ia+3), OutIter(ib));
73 assert(base(r) == ib+3);
74 assert(ib[0] == 0);
75 assert(ib[1] == 1);
76 assert(ib[2] == 2);
77
78 r = std::rotate_copy(InIter(ia), InIter(ia+1), InIter(ia+3), OutIter(ib));
79 assert(base(r) == ib+3);
80 assert(ib[0] == 1);
81 assert(ib[1] == 2);
82 assert(ib[2] == 0);
83
84 r = std::rotate_copy(InIter(ia), InIter(ia+2), InIter(ia+3), OutIter(ib));
85 assert(base(r) == ib+3);
86 assert(ib[0] == 2);
87 assert(ib[1] == 0);
88 assert(ib[2] == 1);
89
90 r = std::rotate_copy(InIter(ia), InIter(ia+3), InIter(ia+3), OutIter(ib));
91 assert(base(r) == ib+3);
92 assert(ib[0] == 0);
93 assert(ib[1] == 1);
94 assert(ib[2] == 2);
95
96 r = std::rotate_copy(InIter(ia), InIter(ia), InIter(ia+4), OutIter(ib));
97 assert(base(r) == ib+4);
98 assert(ib[0] == 0);
99 assert(ib[1] == 1);
100 assert(ib[2] == 2);
101 assert(ib[3] == 3);
102
103 r = std::rotate_copy(InIter(ia), InIter(ia+1), InIter(ia+4), OutIter(ib));
104 assert(base(r) == ib+4);
105 assert(ib[0] == 1);
106 assert(ib[1] == 2);
107 assert(ib[2] == 3);
108 assert(ib[3] == 0);
109
110 r = std::rotate_copy(InIter(ia), InIter(ia+2), InIter(ia+4), OutIter(ib));
111 assert(base(r) == ib+4);
112 assert(ib[0] == 2);
113 assert(ib[1] == 3);
114 assert(ib[2] == 0);
115 assert(ib[3] == 1);
116
117 r = std::rotate_copy(InIter(ia), InIter(ia+3), InIter(ia+4), OutIter(ib));
118 assert(base(r) == ib+4);
119 assert(ib[0] == 3);
120 assert(ib[1] == 0);
121 assert(ib[2] == 1);
122 assert(ib[3] == 2);
123
124 r = std::rotate_copy(InIter(ia), InIter(ia+4), InIter(ia+4), OutIter(ib));
125 assert(base(r) == ib+4);
126 assert(ib[0] == 0);
127 assert(ib[1] == 1);
128 assert(ib[2] == 2);
129 assert(ib[3] == 3);
130 }
131
main()132 int main()
133 {
134 test<bidirectional_iterator<const int*>, output_iterator<int*> >();
135 test<bidirectional_iterator<const int*>, forward_iterator<int*> >();
136 test<bidirectional_iterator<const int*>, bidirectional_iterator<int*> >();
137 test<bidirectional_iterator<const int*>, random_access_iterator<int*> >();
138 test<bidirectional_iterator<const int*>, int*>();
139
140 test<random_access_iterator<const int*>, output_iterator<int*> >();
141 test<random_access_iterator<const int*>, forward_iterator<int*> >();
142 test<random_access_iterator<const int*>, bidirectional_iterator<int*> >();
143 test<random_access_iterator<const int*>, random_access_iterator<int*> >();
144 test<random_access_iterator<const int*>, int*>();
145
146 test<const int*, output_iterator<int*> >();
147 test<const int*, forward_iterator<int*> >();
148 test<const int*, bidirectional_iterator<int*> >();
149 test<const int*, random_access_iterator<int*> >();
150 test<const int*, int*>();
151
152 // #if TEST_STD_VER > 17
153 // static_assert(test_constexpr());
154 // #endif
155 }
156