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<input_iterator I, sentinel_for<I> S, weakly_incrementable O> 14 // requires indirectly_movable<I, O> 15 // constexpr ranges::move_result<I, O> 16 // ranges::move(I first, S last, O result); 17 // template<input_range R, weakly_incrementable O> 18 // requires indirectly_movable<iterator_t<R>, O> 19 // constexpr ranges::move_result<borrowed_iterator_t<R>, O> 20 // ranges::move(R&& r, O 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 HasMoveIt = requires(In in, Sent sent, Out out) { std::ranges::move(in, sent, out); }; 33 34 static_assert(HasMoveIt<int*>); 35 static_assert(!HasMoveIt<InputIteratorNotDerivedFrom>); 36 static_assert(!HasMoveIt<InputIteratorNotIndirectlyReadable>); 37 static_assert(!HasMoveIt<InputIteratorNotInputOrOutputIterator>); 38 static_assert(!HasMoveIt<int*, WeaklyIncrementableNotMovable>); 39 struct NotIndirectlyMovable {}; 40 static_assert(!HasMoveIt<int*, NotIndirectlyMovable*>); 41 static_assert(!HasMoveIt<int*, int*, SentinelForNotSemiregular>); 42 static_assert(!HasMoveIt<int*, int*, SentinelForNotWeaklyEqualityComparableWith>); 43 44 template <class Range, class Out> 45 concept HasMoveR = requires(Range range, Out out) { std::ranges::move(range, out); }; 46 47 static_assert(HasMoveR<std::array<int, 10>, int*>); 48 static_assert(!HasMoveR<InputRangeNotDerivedFrom, int*>); 49 static_assert(!HasMoveR<InputRangeNotIndirectlyReadable, int*>); 50 static_assert(!HasMoveR<InputRangeNotInputOrOutputIterator, int*>); 51 static_assert(!HasMoveR<WeaklyIncrementableNotMovable, int*>); 52 static_assert(!HasMoveR<UncheckedRange<NotIndirectlyMovable*>, int*>); 53 static_assert(!HasMoveR<InputRangeNotSentinelSemiregular, int*>); 54 static_assert(!HasMoveR<InputRangeNotSentinelEqualityComparableWith, int*>); 55 static_assert(!HasMoveR<UncheckedRange<int*>, WeaklyIncrementableNotMovable>); 56 57 static_assert(std::is_same_v<std::ranges::move_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(In(in.data()), Sent(In(in.data() + in.size())), Out(out.data())); 65 assert(in == out); 66 assert(base(ret.in) == in.data() + in.size()); 67 assert(base(ret.out) == out.data() + out.size()); 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(range, Out(out.data())); 74 assert(in == out); 75 assert(base(ret.in) == in.data() + in.size()); 76 assert(base(ret.out) == out.data() + out.size()); 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 Out> 89 constexpr void test_in_iterators() { 90 test_iterators<cpp20_input_iterator<int*>, Out, sentinel_wrapper<cpp20_input_iterator<int*>>>(); 91 test_iterators<forward_iterator<int*>, Out>(); 92 test_iterators<bidirectional_iterator<int*>, Out>(); 93 test_iterators<random_access_iterator<int*>, Out>(); 94 test_iterators<contiguous_iterator<int*>, Out>(); 95 test_iterators<int*, Out>(); 96 } 97 98 template <class Out> 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; 112 constexpr IteratorWithMoveIter(int* ptr_) : ptr(ptr_) {} 113 114 constexpr int& operator*() const; // iterator with iter_move should not be dereferenced 115 116 constexpr IteratorWithMoveIter& operator++() { ++ptr; return *this; } 117 constexpr IteratorWithMoveIter operator++(int) { auto ret = *this; ++*this; return ret; } 118 119 friend constexpr int iter_move(const IteratorWithMoveIter&) { return 42; } 120 121 constexpr bool operator==(const IteratorWithMoveIter& other) const = default; 122 }; 123 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 test_in_iterators<int*>(); 134 135 test_proxy_in_iterators<ProxyIterator<cpp20_input_iterator<int*>>>(); 136 test_proxy_in_iterators<ProxyIterator<forward_iterator<int*>>>(); 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(a, std::begin(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(std::begin(a), std::end(a), std::begin(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(proxyA, std::begin(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(std::begin(proxyA), std::end(proxyA), std::begin(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*>> decltype(auto) ret = 186 std::ranges::move(std::array {1, 2, 3, 4}, out.data()); 187 assert(ret.out == out.data() + 4); 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*>> decltype(auto) ret = 195 std::ranges::move(std::views::all(in), out.data()); 196 assert(ret.in == in.data() + 4); 197 assert(ret.out == out.data() + 4); 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=(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(in.begin(), in.end(), out.begin()); 216 assert(ret.in == in.end()); 217 assert(ret.out == out.end()); 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(in, out.begin()); 224 assert(ret.in == in.end()); 225 assert(ret.out == out.end()); 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 forwards 231 struct OnlyForwardsMovable { 232 OnlyForwardsMovable* next = nullptr; 233 bool canMove = false; 234 OnlyForwardsMovable() = default; 235 constexpr OnlyForwardsMovable& operator=(OnlyForwardsMovable&&) { 236 assert(canMove); 237 if (next != nullptr) 238 next->canMove = true; 239 return *this; 240 } 241 }; 242 { 243 std::array<OnlyForwardsMovable, 3> in {}; 244 std::array<OnlyForwardsMovable, 3> out {}; 245 out[0].next = &out[1]; 246 out[1].next = &out[2]; 247 out[0].canMove = true; 248 auto ret = std::ranges::move(in.begin(), in.end(), out.begin()); 249 assert(ret.in == in.end()); 250 assert(ret.out == out.end()); 251 assert(out[0].canMove); 252 assert(out[1].canMove); 253 assert(out[2].canMove); 254 } 255 { 256 std::array<OnlyForwardsMovable, 3> in {}; 257 std::array<OnlyForwardsMovable, 3> out {}; 258 out[0].next = &out[1]; 259 out[1].next = &out[2]; 260 out[0].canMove = true; 261 auto ret = std::ranges::move(in, out.begin()); 262 assert(ret.in == in.end()); 263 assert(ret.out == out.end()); 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(IteratorWithMoveIter(a), IteratorWithMoveIter(a + 4), b.data()); 275 assert(ret.in == a + 4); 276 assert(ret.out == b.data() + 4); 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(range, b.data()); 284 assert(ret.in == a + 4); 285 assert(ret.out == b.data() + 4); 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