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 // UNSUPPORTED: c++03, c++11, c++14, c++17
10 // UNSUPPORTED: GCC-ALWAYS_INLINE-FIXME
11 
12 // <algorithm>
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 <deque>
27 #include <ranges>
28 #include <vector>
29 
30 #include "almost_satisfies_types.h"
31 #include "MoveOnly.h"
32 #include "test_iterators.h"
33 
34 template <class In, class Out = In, class Sent = sentinel_wrapper<In>>
35 concept HasMoveBackwardIt = requires(In in, Sent sent, Out out) { std::ranges::move_backward(in, sent, out); };
36 
37 static_assert(HasMoveBackwardIt<int*>);
38 static_assert(!HasMoveBackwardIt<InputIteratorNotDerivedFrom>);
39 static_assert(!HasMoveBackwardIt<InputIteratorNotIndirectlyReadable>);
40 static_assert(!HasMoveBackwardIt<InputIteratorNotInputOrOutputIterator>);
41 static_assert(!HasMoveBackwardIt<int*, WeaklyIncrementableNotMovable>);
42 struct NotIndirectlyCopyable {};
43 static_assert(!HasMoveBackwardIt<int*, NotIndirectlyCopyable*>);
44 static_assert(!HasMoveBackwardIt<int*, int*, SentinelForNotSemiregular>);
45 static_assert(!HasMoveBackwardIt<int*, int*, SentinelForNotWeaklyEqualityComparableWith>);
46 
47 template <class Range, class Out>
48 concept HasMoveBackwardR = requires(Range range, Out out) { std::ranges::move_backward(range, out); };
49 
50 static_assert(HasMoveBackwardR<std::array<int, 10>, int*>);
51 static_assert(!HasMoveBackwardR<InputRangeNotDerivedFrom, int*>);
52 static_assert(!HasMoveBackwardR<InputRangeNotIndirectlyReadable, int*>);
53 static_assert(!HasMoveBackwardR<InputRangeNotInputOrOutputIterator, int*>);
54 static_assert(!HasMoveBackwardR<WeaklyIncrementableNotMovable, int*>);
55 static_assert(!HasMoveBackwardR<UncheckedRange<NotIndirectlyCopyable*>, int*>);
56 static_assert(!HasMoveBackwardR<InputRangeNotSentinelSemiregular, int*>);
57 static_assert(!HasMoveBackwardR<InputRangeNotSentinelEqualityComparableWith, int*>);
58 static_assert(!HasMoveBackwardR<UncheckedRange<int*>, WeaklyIncrementableNotMovable>);
59 
60 static_assert(std::is_same_v<std::ranges::copy_result<int, long>, std::ranges::in_out_result<int, long>>);
61 
62 template <class In, class Out, class Sent, int N>
63 constexpr void test(std::array<int, N> in) {
64   {
65     std::array<int, N> out;
66     std::same_as<std::ranges::in_out_result<In, Out>> decltype(auto) ret =
67       std::ranges::move_backward(In(in.data()), Sent(In(in.data() + in.size())), Out(out.data() + out.size()));
68     assert(in == out);
69     assert(base(ret.in) == in.data() + in.size());
70     assert(base(ret.out) == out.data());
71   }
72   {
73     std::array<int, N> out;
74     auto range = std::ranges::subrange(In(in.data()), Sent(In(in.data() + in.size())));
75     std::same_as<std::ranges::in_out_result<In, Out>> decltype(auto) ret =
76         std::ranges::move_backward(range, Out(out.data() + out.size()));
77     assert(in == out);
78     assert(base(ret.in) == in.data() + in.size());
79     assert(base(ret.out) == out.data());
80   }
81 }
82 
83 template <class In, class Out, class Sent = In>
84 constexpr void test_iterators() {
85   // simple test
86   test<In, Out, Sent, 4>({1, 2, 3, 4});
87   // check that an empty range works
88   test<In, Out, Sent, 0>({});
89 }
90 
91 template <class InContainer, class OutContainer, class In, class Out, class Sent = In>
92 constexpr void test_containers() {
93   {
94     InContainer in {1, 2, 3, 4};
95     OutContainer out(4);
96     std::same_as<std::ranges::in_out_result<In, Out>> auto ret =
97       std::ranges::move_backward(In(in.begin()), Sent(In(in.end())), Out(out.end()));
98     assert(std::ranges::equal(in, out));
99     assert(base(ret.in) == in.end());
100     assert(base(ret.out) == out.begin());
101   }
102   {
103     InContainer in {1, 2, 3, 4};
104     OutContainer out(4);
105     auto range = std::ranges::subrange(In(in.begin()), Sent(In(in.end())));
106     std::same_as<std::ranges::in_out_result<In, Out>> auto ret = std::ranges::move_backward(range, Out(out.end()));
107     assert(std::ranges::equal(in, out));
108     assert(base(ret.in) == in.end());
109     assert(base(ret.out) == out.begin());
110   }
111 }
112 
113 template <template <class> class InIter, template <class> class OutIter>
114 constexpr void test_sentinels() {
115   test_iterators<InIter<int*>, OutIter<int*>, InIter<int*>>();
116   test_iterators<InIter<int*>, OutIter<int*>, sentinel_wrapper<InIter<int*>>>();
117   test_iterators<InIter<int*>, OutIter<int*>, sized_sentinel<InIter<int*>>>();
118 
119   if (!std::is_constant_evaluated()) {
120     if constexpr (!std::is_same_v<InIter<int*>, contiguous_iterator<int*>> &&
121                   !std::is_same_v<OutIter<int*>, contiguous_iterator<int*>> &&
122                   !std::is_same_v<InIter<int*>, ContiguousProxyIterator<int*>> &&
123                   !std::is_same_v<OutIter<int*>, ContiguousProxyIterator<int*>>) {
124       test_containers<std::deque<int>,
125                       std::deque<int>,
126                       InIter<std::deque<int>::iterator>,
127                       OutIter<std::deque<int>::iterator>>();
128       test_containers<std::deque<int>,
129                       std::vector<int>,
130                       InIter<std::deque<int>::iterator>,
131                       OutIter<std::vector<int>::iterator>>();
132       test_containers<std::vector<int>,
133                       std::deque<int>,
134                       InIter<std::vector<int>::iterator>,
135                       OutIter<std::deque<int>::iterator>>();
136       test_containers<std::vector<int>,
137                       std::vector<int>,
138                       InIter<std::vector<int>::iterator>,
139                       OutIter<std::vector<int>::iterator>>();
140     }
141   }
142 }
143 
144 template <template <class> class Out>
145 constexpr void test_in_iterators() {
146   test_sentinels<bidirectional_iterator, Out>();
147   test_sentinels<random_access_iterator, Out>();
148   test_sentinels<contiguous_iterator, Out>();
149   test_sentinels<std::type_identity_t, Out>();
150 }
151 
152 template <template <class> class Out>
153 constexpr void test_proxy_in_iterators() {
154   test_sentinels<BidirectionalProxyIterator, Out>();
155   test_sentinels<RandomAccessProxyIterator, Out>();
156   test_sentinels<ContiguousProxyIterator, Out>();
157   test_sentinels<ProxyIterator, Out>();
158 }
159 
160 struct IteratorWithMoveIter {
161   using value_type = int;
162   using difference_type = int;
163   explicit IteratorWithMoveIter() = default;
164   int* ptr;
165   constexpr IteratorWithMoveIter(int* ptr_) : ptr(ptr_) {}
166 
167   constexpr int& operator*() const; // iterator with iter_move should not be dereferenced
168 
169   constexpr IteratorWithMoveIter& operator++() { ++ptr; return *this; }
170   constexpr IteratorWithMoveIter operator++(int) { auto ret = *this; ++*this; return ret; }
171 
172   constexpr IteratorWithMoveIter& operator--() { --ptr; return *this; }
173   constexpr IteratorWithMoveIter operator--(int) { auto ret = *this; --*this; return ret; }
174 
175   friend constexpr int iter_move(const IteratorWithMoveIter&) { return 42; }
176 
177   constexpr bool operator==(const IteratorWithMoveIter& other) const = default;
178 };
179 
180 constexpr bool test() {
181   test_in_iterators<bidirectional_iterator>();
182   test_in_iterators<random_access_iterator>();
183   test_in_iterators<contiguous_iterator>();
184   test_in_iterators<std::type_identity_t>();
185 
186   test_proxy_in_iterators<BidirectionalProxyIterator>();
187   test_proxy_in_iterators<RandomAccessProxyIterator>();
188   test_proxy_in_iterators<ContiguousProxyIterator>();
189   test_proxy_in_iterators<ProxyIterator>();
190 
191   { // check that a move-only type works
192     {
193       MoveOnly a[] = {1, 2, 3};
194       MoveOnly b[3];
195       std::ranges::move_backward(a, std::end(b));
196       assert(b[0].get() == 1);
197       assert(b[1].get() == 2);
198       assert(b[2].get() == 3);
199     }
200     {
201       MoveOnly a[] = {1, 2, 3};
202       MoveOnly b[3];
203       std::ranges::move_backward(std::begin(a), std::end(a), std::end(b));
204       assert(b[0].get() == 1);
205       assert(b[1].get() == 2);
206       assert(b[2].get() == 3);
207     }
208   }
209 
210   { // check that a move-only type works for ProxyIterator
211     {
212       MoveOnly a[] = {1, 2, 3};
213       MoveOnly b[3];
214       ProxyRange proxyA{a};
215       ProxyRange proxyB{b};
216       std::ranges::move_backward(proxyA, std::ranges::next(proxyB.begin(), std::end(proxyB)));
217       assert(b[0].get() == 1);
218       assert(b[1].get() == 2);
219       assert(b[2].get() == 3);
220     }
221     {
222       MoveOnly a[] = {1, 2, 3};
223       MoveOnly b[3];
224       ProxyRange proxyA{a};
225       ProxyRange proxyB{b};
226       std::ranges::move_backward(std::begin(proxyA), std::end(proxyA),  std::ranges::next(proxyB.begin(), std::end(proxyB)));
227       assert(b[0].get() == 1);
228       assert(b[1].get() == 2);
229       assert(b[2].get() == 3);
230     }
231   }
232 
233   { // check that ranges::dangling is returned
234     std::array<int, 4> out;
235     std::same_as<std::ranges::in_out_result<std::ranges::dangling, int*>> auto ret =
236       std::ranges::move_backward(std::array {1, 2, 3, 4}, out.data() + out.size());
237     assert(ret.out == out.data());
238     assert((out == std::array{1, 2, 3, 4}));
239   }
240 
241   { // check that an iterator is returned with a borrowing range
242     std::array in {1, 2, 3, 4};
243     std::array<int, 4> out;
244     std::same_as<std::ranges::in_out_result<int*, int*>> auto ret =
245         std::ranges::move_backward(std::views::all(in), out.data() + out.size());
246     assert(ret.in == in.data() + in.size());
247     assert(ret.out == out.data());
248     assert(in == out);
249   }
250 
251   { // check that every element is moved exactly once
252     struct MoveOnce {
253       bool moved = false;
254       constexpr MoveOnce() = default;
255       constexpr MoveOnce(const MoveOnce& other) = delete;
256       constexpr MoveOnce& operator=(const MoveOnce& other) {
257         assert(!other.moved);
258         moved = true;
259         return *this;
260       }
261     };
262     {
263       std::array<MoveOnce, 4> in {};
264       std::array<MoveOnce, 4> out {};
265       auto ret = std::ranges::move_backward(in.begin(), in.end(), out.end());
266       assert(ret.in == in.end());
267       assert(ret.out == out.begin());
268       assert(std::all_of(out.begin(), out.end(), [](const auto& e) { return e.moved; }));
269     }
270     {
271       std::array<MoveOnce, 4> in {};
272       std::array<MoveOnce, 4> out {};
273       auto ret = std::ranges::move_backward(in, out.end());
274       assert(ret.in == in.end());
275       assert(ret.out == out.begin());
276       assert(std::all_of(out.begin(), out.end(), [](const auto& e) { return e.moved; }));
277     }
278   }
279 
280   { // check that the range is moved backwards
281     struct OnlyBackwardsMovable {
282       OnlyBackwardsMovable* next = nullptr;
283       bool canMove = false;
284       OnlyBackwardsMovable() = default;
285       constexpr OnlyBackwardsMovable& operator=(const OnlyBackwardsMovable&) {
286         assert(canMove);
287         if (next != nullptr)
288           next->canMove = true;
289         return *this;
290       }
291     };
292     {
293       std::array<OnlyBackwardsMovable, 3> in {};
294       std::array<OnlyBackwardsMovable, 3> out {};
295       out[1].next = &out[0];
296       out[2].next = &out[1];
297       out[2].canMove = true;
298       auto ret = std::ranges::move_backward(in, out.end());
299       assert(ret.in == in.end());
300       assert(ret.out == out.begin());
301       assert(out[0].canMove);
302       assert(out[1].canMove);
303       assert(out[2].canMove);
304     }
305     {
306       std::array<OnlyBackwardsMovable, 3> in {};
307       std::array<OnlyBackwardsMovable, 3> out {};
308       out[1].next = &out[0];
309       out[2].next = &out[1];
310       out[2].canMove = true;
311       auto ret = std::ranges::move_backward(in.begin(), in.end(), out.end());
312       assert(ret.in == in.end());
313       assert(ret.out == out.begin());
314       assert(out[0].canMove);
315       assert(out[1].canMove);
316       assert(out[2].canMove);
317     }
318   }
319 
320   { // check that iter_move is used properly
321     {
322       int a[] = {1, 2, 3, 4};
323       std::array<int, 4> b;
324       auto ret = std::ranges::move_backward(IteratorWithMoveIter(a), IteratorWithMoveIter(a + 4), b.data() + b.size());
325       assert(ret.in == a + 4);
326       assert(ret.out == b.data());
327       assert((b == std::array {42, 42, 42, 42}));
328     }
329     {
330       int a[] = {1, 2, 3, 4};
331       std::array<int, 4> b;
332       auto range = std::ranges::subrange(IteratorWithMoveIter(a), IteratorWithMoveIter(a + 4));
333       auto ret = std::ranges::move_backward(range, b.data() + b.size());
334       assert(ret.in == a + 4);
335       assert(ret.out == b.data());
336       assert((b == std::array {42, 42, 42, 42}));
337     }
338   }
339 
340   return true;
341 }
342 
343 int main(int, char**) {
344   test();
345   static_assert(test());
346 
347   return 0;
348 }
349