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>
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());
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());
77     assert(base(ret.out) == out.data());
78   }
79 }
80 
81 template <class In, class Out, class Sent = In>
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>
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 struct IteratorWithMoveIter {
98   using value_type = int;
99   using difference_type = int;
100   explicit IteratorWithMoveIter() = default;
101   int* ptr;
102   constexpr IteratorWithMoveIter(int* ptr_) : ptr(ptr_) {}
103 
104   constexpr int& operator*() const; // iterator with iter_move should not be dereferenced
105 
106   constexpr IteratorWithMoveIter& operator++() { ++ptr; return *this; }
107   constexpr IteratorWithMoveIter operator++(int) { auto ret = *this; ++*this; return ret; }
108 
109   constexpr IteratorWithMoveIter& operator--() { --ptr; return *this; }
110   constexpr IteratorWithMoveIter operator--(int) { auto ret = *this; --*this; return ret; }
111 
112   friend constexpr int iter_move(const IteratorWithMoveIter&) { return 42; }
113 
114   constexpr bool operator==(const IteratorWithMoveIter& other) const = default;
115 };
116 
117 constexpr bool test() {
118   test_in_iterators<bidirectional_iterator<int*>>();
119   test_in_iterators<random_access_iterator<int*>>();
120   test_in_iterators<contiguous_iterator<int*>>();
121 
122   { // check that a move-only type works
123     {
124       MoveOnly a[] = {1, 2, 3};
125       MoveOnly b[3];
126       std::ranges::move_backward(a, std::end(b));
127       assert(b[0].get() == 1);
128       assert(b[1].get() == 2);
129       assert(b[2].get() == 3);
130     }
131     {
132       MoveOnly a[] = {1, 2, 3};
133       MoveOnly b[3];
134       std::ranges::move_backward(std::begin(a), std::end(a), std::end(b));
135       assert(b[0].get() == 1);
136       assert(b[1].get() == 2);
137       assert(b[2].get() == 3);
138     }
139   }
140 
141   { // check that ranges::dangling is returned
142     std::array<int, 4> out;
143     std::same_as<std::ranges::in_out_result<std::ranges::dangling, int*>> auto ret =
144       std::ranges::move_backward(std::array {1, 2, 3, 4}, out.data() + out.size());
145     assert(ret.out == out.data());
146     assert((out == std::array{1, 2, 3, 4}));
147   }
148 
149   { // check that an iterator is returned with a borrowing range
150     std::array in {1, 2, 3, 4};
151     std::array<int, 4> out;
152     std::same_as<std::ranges::in_out_result<int*, int*>> auto ret =
153         std::ranges::move_backward(std::views::all(in), out.data() + out.size());
154     assert(ret.in == in.data());
155     assert(ret.out == out.data());
156     assert(in == out);
157   }
158 
159   { // check that every element is moved exactly once
160     struct MoveOnce {
161       bool moved = false;
162       constexpr MoveOnce() = default;
163       constexpr MoveOnce(const MoveOnce& other) = delete;
164       constexpr MoveOnce& operator=(const MoveOnce& other) {
165         assert(!other.moved);
166         moved = true;
167         return *this;
168       }
169     };
170     {
171       std::array<MoveOnce, 4> in {};
172       std::array<MoveOnce, 4> out {};
173       auto ret = std::ranges::move_backward(in.begin(), in.end(), out.end());
174       assert(ret.in == in.begin());
175       assert(ret.out == out.begin());
176       assert(std::all_of(out.begin(), out.end(), [](const auto& e) { return e.moved; }));
177     }
178     {
179       std::array<MoveOnce, 4> in {};
180       std::array<MoveOnce, 4> out {};
181       auto ret = std::ranges::move_backward(in, out.end());
182       assert(ret.in == in.begin());
183       assert(ret.out == out.begin());
184       assert(std::all_of(out.begin(), out.end(), [](const auto& e) { return e.moved; }));
185     }
186   }
187 
188   { // check that the range is moved backwards
189     struct OnlyBackwardsMovable {
190       OnlyBackwardsMovable* next = nullptr;
191       bool canMove = false;
192       OnlyBackwardsMovable() = default;
193       constexpr OnlyBackwardsMovable& operator=(const OnlyBackwardsMovable&) {
194         assert(canMove);
195         if (next != nullptr)
196           next->canMove = true;
197         return *this;
198       }
199     };
200     {
201       std::array<OnlyBackwardsMovable, 3> in {};
202       std::array<OnlyBackwardsMovable, 3> out {};
203       out[1].next = &out[0];
204       out[2].next = &out[1];
205       out[2].canMove = true;
206       auto ret = std::ranges::move_backward(in, out.end());
207       assert(ret.in == in.begin());
208       assert(ret.out == out.begin());
209       assert(out[0].canMove);
210       assert(out[1].canMove);
211       assert(out[2].canMove);
212     }
213     {
214       std::array<OnlyBackwardsMovable, 3> in {};
215       std::array<OnlyBackwardsMovable, 3> out {};
216       out[1].next = &out[0];
217       out[2].next = &out[1];
218       out[2].canMove = true;
219       auto ret = std::ranges::move_backward(in.begin(), in.end(), out.end());
220       assert(ret.in == in.begin());
221       assert(ret.out == out.begin());
222       assert(out[0].canMove);
223       assert(out[1].canMove);
224       assert(out[2].canMove);
225     }
226   }
227 
228   { // check that iter_move is used properly
229     {
230       int a[] = {1, 2, 3, 4};
231       std::array<int, 4> b;
232       auto ret = std::ranges::move_backward(IteratorWithMoveIter(a), IteratorWithMoveIter(a + 4), b.data() + b.size());
233       assert(ret.in == a);
234       assert(ret.out == b.data());
235       assert((b == std::array {42, 42, 42, 42}));
236     }
237     {
238       int a[] = {1, 2, 3, 4};
239       std::array<int, 4> b;
240       auto range = std::ranges::subrange(IteratorWithMoveIter(a), IteratorWithMoveIter(a + 4));
241       auto ret = std::ranges::move_backward(range, b.data() + b.size());
242       assert(ret.in == a);
243       assert(ret.out == b.data());
244       assert((b == std::array {42, 42, 42, 42}));
245     }
246   }
247 
248   return true;
249 }
250 
251 int main(int, char**) {
252   test();
253   static_assert(test());
254 
255   return 0;
256 }
257