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 // UNSUPPORTED: c++03, c++11, c++14, c++17
12 // UNSUPPORTED: libcpp-has-no-incomplete-ranges
13
14 // template<bidirectional_iterator I1, sentinel_for<I1> S1, bidirectional_iterator I2>
15 // requires indirectly_movable<I1, I2>
16 // constexpr ranges::move_backward_result<I1, I2>
17 // ranges::move_backward(I1 first, S1 last, I2 result);
18 // template<bidirectional_range R, bidirectional_iterator I>
19 // requires indirectly_movable<iterator_t<R>, I>
20 // constexpr ranges::move_backward_result<borrowed_iterator_t<R>, I>
21 // ranges::move_backward(R&& r, I result);
22
23 #include <algorithm>
24 #include <array>
25 #include <cassert>
26 #include <ranges>
27
28 #include "almost_satisfies_types.h"
29 #include "MoveOnly.h"
30 #include "test_iterators.h"
31
32 template <class In, class Out = In, class Sent = sentinel_wrapper<In>>
33 concept HasMoveBackwardIt = requires(In in, Sent sent, Out out) { std::ranges::move_backward(in, sent, out); };
34
35 static_assert(HasMoveBackwardIt<int*>);
36 static_assert(!HasMoveBackwardIt<InputIteratorNotDerivedFrom>);
37 static_assert(!HasMoveBackwardIt<InputIteratorNotIndirectlyReadable>);
38 static_assert(!HasMoveBackwardIt<InputIteratorNotInputOrOutputIterator>);
39 static_assert(!HasMoveBackwardIt<int*, WeaklyIncrementableNotMovable>);
40 struct NotIndirectlyCopyable {};
41 static_assert(!HasMoveBackwardIt<int*, NotIndirectlyCopyable*>);
42 static_assert(!HasMoveBackwardIt<int*, int*, SentinelForNotSemiregular>);
43 static_assert(!HasMoveBackwardIt<int*, int*, SentinelForNotWeaklyEqualityComparableWith>);
44
45 template <class Range, class Out>
46 concept HasMoveBackwardR = requires(Range range, Out out) { std::ranges::move_backward(range, out); };
47
48 static_assert(HasMoveBackwardR<std::array<int, 10>, int*>);
49 static_assert(!HasMoveBackwardR<InputRangeNotDerivedFrom, int*>);
50 static_assert(!HasMoveBackwardR<InputRangeNotIndirectlyReadable, int*>);
51 static_assert(!HasMoveBackwardR<InputRangeNotInputOrOutputIterator, int*>);
52 static_assert(!HasMoveBackwardR<WeaklyIncrementableNotMovable, int*>);
53 static_assert(!HasMoveBackwardR<UncheckedRange<NotIndirectlyCopyable*>, int*>);
54 static_assert(!HasMoveBackwardR<InputRangeNotSentinelSemiregular, int*>);
55 static_assert(!HasMoveBackwardR<InputRangeNotSentinelEqualityComparableWith, int*>);
56 static_assert(!HasMoveBackwardR<UncheckedRange<int*>, WeaklyIncrementableNotMovable>);
57
58 static_assert(std::is_same_v<std::ranges::copy_result<int, long>, std::ranges::in_out_result<int, long>>);
59
60 template <class In, class Out, class Sent, int N>
test(std::array<int,N> in)61 constexpr void test(std::array<int, N> in) {
62 {
63 std::array<int, N> out;
64 std::same_as<std::ranges::in_out_result<In, Out>> decltype(auto) ret =
65 std::ranges::move_backward(In(in.data()), Sent(In(in.data() + in.size())), Out(out.data() + out.size()));
66 assert(in == out);
67 assert(base(ret.in) == in.data() + in.size());
68 assert(base(ret.out) == out.data());
69 }
70 {
71 std::array<int, N> out;
72 auto range = std::ranges::subrange(In(in.data()), Sent(In(in.data() + in.size())));
73 std::same_as<std::ranges::in_out_result<In, Out>> decltype(auto) ret =
74 std::ranges::move_backward(range, Out(out.data() + out.size()));
75 assert(in == out);
76 assert(base(ret.in) == in.data() + in.size());
77 assert(base(ret.out) == out.data());
78 }
79 }
80
81 template <class In, class Out, class Sent = In>
test_iterators()82 constexpr void test_iterators() {
83 // simple test
84 test<In, Out, Sent, 4>({1, 2, 3, 4});
85 // check that an empty range works
86 test<In, Out, Sent, 0>({});
87 }
88
89 template <class Out>
test_in_iterators()90 constexpr void test_in_iterators() {
91 test_iterators<bidirectional_iterator<int*>, Out, sentinel_wrapper<bidirectional_iterator<int*>>>();
92 test_iterators<bidirectional_iterator<int*>, Out>();
93 test_iterators<random_access_iterator<int*>, Out>();
94 test_iterators<contiguous_iterator<int*>, Out>();
95 }
96
97 template <class Out>
test_proxy_in_iterators()98 constexpr void test_proxy_in_iterators() {
99 test_iterators<ProxyIterator<bidirectional_iterator<int*>>, Out, sentinel_wrapper<ProxyIterator<bidirectional_iterator<int*>>>>();
100 test_iterators<ProxyIterator<bidirectional_iterator<int*>>, Out>();
101 test_iterators<ProxyIterator<random_access_iterator<int*>>, Out>();
102 test_iterators<ProxyIterator<contiguous_iterator<int*>>, Out>();
103 }
104
105 struct IteratorWithMoveIter {
106 using value_type = int;
107 using difference_type = int;
108 explicit IteratorWithMoveIter() = default;
109 int* ptr;
IteratorWithMoveIterIteratorWithMoveIter110 constexpr IteratorWithMoveIter(int* ptr_) : ptr(ptr_) {}
111
112 constexpr int& operator*() const; // iterator with iter_move should not be dereferenced
113
operator ++IteratorWithMoveIter114 constexpr IteratorWithMoveIter& operator++() { ++ptr; return *this; }
operator ++IteratorWithMoveIter115 constexpr IteratorWithMoveIter operator++(int) { auto ret = *this; ++*this; return ret; }
116
operator --IteratorWithMoveIter117 constexpr IteratorWithMoveIter& operator--() { --ptr; return *this; }
operator --IteratorWithMoveIter118 constexpr IteratorWithMoveIter operator--(int) { auto ret = *this; --*this; return ret; }
119
iter_move(const IteratorWithMoveIter &)120 friend constexpr int iter_move(const IteratorWithMoveIter&) { return 42; }
121
122 constexpr bool operator==(const IteratorWithMoveIter& other) const = default;
123 };
124
test()125 constexpr bool test() {
126 test_in_iterators<bidirectional_iterator<int*>>();
127 test_in_iterators<random_access_iterator<int*>>();
128 test_in_iterators<contiguous_iterator<int*>>();
129
130 test_proxy_in_iterators<ProxyIterator<bidirectional_iterator<int*>>>();
131 test_proxy_in_iterators<ProxyIterator<random_access_iterator<int*>>>();
132 test_proxy_in_iterators<ProxyIterator<contiguous_iterator<int*>>>();
133
134 { // check that a move-only type works
135 {
136 MoveOnly a[] = {1, 2, 3};
137 MoveOnly b[3];
138 std::ranges::move_backward(a, std::end(b));
139 assert(b[0].get() == 1);
140 assert(b[1].get() == 2);
141 assert(b[2].get() == 3);
142 }
143 {
144 MoveOnly a[] = {1, 2, 3};
145 MoveOnly b[3];
146 std::ranges::move_backward(std::begin(a), std::end(a), std::end(b));
147 assert(b[0].get() == 1);
148 assert(b[1].get() == 2);
149 assert(b[2].get() == 3);
150 }
151 }
152
153 { // check that a move-only type works for ProxyIterator
154 {
155 MoveOnly a[] = {1, 2, 3};
156 MoveOnly b[3];
157 ProxyRange proxyA{a};
158 ProxyRange proxyB{b};
159 std::ranges::move_backward(proxyA, std::ranges::next(proxyB.begin(), std::end(proxyB)));
160 assert(b[0].get() == 1);
161 assert(b[1].get() == 2);
162 assert(b[2].get() == 3);
163 }
164 {
165 MoveOnly a[] = {1, 2, 3};
166 MoveOnly b[3];
167 ProxyRange proxyA{a};
168 ProxyRange proxyB{b};
169 std::ranges::move_backward(std::begin(proxyA), std::end(proxyA), std::ranges::next(proxyB.begin(), std::end(proxyB)));
170 assert(b[0].get() == 1);
171 assert(b[1].get() == 2);
172 assert(b[2].get() == 3);
173 }
174 }
175
176 { // check that ranges::dangling is returned
177 std::array<int, 4> out;
178 std::same_as<std::ranges::in_out_result<std::ranges::dangling, int*>> auto ret =
179 std::ranges::move_backward(std::array {1, 2, 3, 4}, out.data() + out.size());
180 assert(ret.out == out.data());
181 assert((out == std::array{1, 2, 3, 4}));
182 }
183
184 { // check that an iterator is returned with a borrowing range
185 std::array in {1, 2, 3, 4};
186 std::array<int, 4> out;
187 std::same_as<std::ranges::in_out_result<int*, int*>> auto ret =
188 std::ranges::move_backward(std::views::all(in), out.data() + out.size());
189 assert(ret.in == in.data() + in.size());
190 assert(ret.out == out.data());
191 assert(in == out);
192 }
193
194 { // check that every element is moved exactly once
195 struct MoveOnce {
196 bool moved = false;
197 constexpr MoveOnce() = default;
198 constexpr MoveOnce(const MoveOnce& other) = delete;
199 constexpr MoveOnce& operator=(const MoveOnce& other) {
200 assert(!other.moved);
201 moved = true;
202 return *this;
203 }
204 };
205 {
206 std::array<MoveOnce, 4> in {};
207 std::array<MoveOnce, 4> out {};
208 auto ret = std::ranges::move_backward(in.begin(), in.end(), out.end());
209 assert(ret.in == in.end());
210 assert(ret.out == out.begin());
211 assert(std::all_of(out.begin(), out.end(), [](const auto& e) { return e.moved; }));
212 }
213 {
214 std::array<MoveOnce, 4> in {};
215 std::array<MoveOnce, 4> out {};
216 auto ret = std::ranges::move_backward(in, out.end());
217 assert(ret.in == in.end());
218 assert(ret.out == out.begin());
219 assert(std::all_of(out.begin(), out.end(), [](const auto& e) { return e.moved; }));
220 }
221 }
222
223 { // check that the range is moved backwards
224 struct OnlyBackwardsMovable {
225 OnlyBackwardsMovable* next = nullptr;
226 bool canMove = false;
227 OnlyBackwardsMovable() = default;
228 constexpr OnlyBackwardsMovable& operator=(const OnlyBackwardsMovable&) {
229 assert(canMove);
230 if (next != nullptr)
231 next->canMove = true;
232 return *this;
233 }
234 };
235 {
236 std::array<OnlyBackwardsMovable, 3> in {};
237 std::array<OnlyBackwardsMovable, 3> out {};
238 out[1].next = &out[0];
239 out[2].next = &out[1];
240 out[2].canMove = true;
241 auto ret = std::ranges::move_backward(in, out.end());
242 assert(ret.in == in.end());
243 assert(ret.out == out.begin());
244 assert(out[0].canMove);
245 assert(out[1].canMove);
246 assert(out[2].canMove);
247 }
248 {
249 std::array<OnlyBackwardsMovable, 3> in {};
250 std::array<OnlyBackwardsMovable, 3> out {};
251 out[1].next = &out[0];
252 out[2].next = &out[1];
253 out[2].canMove = true;
254 auto ret = std::ranges::move_backward(in.begin(), in.end(), out.end());
255 assert(ret.in == in.end());
256 assert(ret.out == out.begin());
257 assert(out[0].canMove);
258 assert(out[1].canMove);
259 assert(out[2].canMove);
260 }
261 }
262
263 { // check that iter_move is used properly
264 {
265 int a[] = {1, 2, 3, 4};
266 std::array<int, 4> b;
267 auto ret = std::ranges::move_backward(IteratorWithMoveIter(a), IteratorWithMoveIter(a + 4), b.data() + b.size());
268 assert(ret.in == a + 4);
269 assert(ret.out == b.data());
270 assert((b == std::array {42, 42, 42, 42}));
271 }
272 {
273 int a[] = {1, 2, 3, 4};
274 std::array<int, 4> b;
275 auto range = std::ranges::subrange(IteratorWithMoveIter(a), IteratorWithMoveIter(a + 4));
276 auto ret = std::ranges::move_backward(range, b.data() + b.size());
277 assert(ret.in == a + 4);
278 assert(ret.out == b.data());
279 assert((b == std::array {42, 42, 42, 42}));
280 }
281 }
282
283 return true;
284 }
285
main(int,char **)286 int main(int, char**) {
287 test();
288 static_assert(test());
289
290 return 0;
291 }
292