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