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