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