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