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: GCC-ALWAYS_INLINE-FIXME 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 <deque> 27 #include <ranges> 28 #include <vector> 29 30 #include "almost_satisfies_types.h" 31 #include "MoveOnly.h" 32 #include "test_iterators.h" 33 34 template <class In, class Out = In, class Sent = sentinel_wrapper<In>> 35 concept HasMoveIt = requires(In in, Sent sent, Out out) { std::ranges::move(in, sent, out); }; 36 37 static_assert(HasMoveIt<int*>); 38 static_assert(!HasMoveIt<InputIteratorNotDerivedFrom>); 39 static_assert(!HasMoveIt<InputIteratorNotIndirectlyReadable>); 40 static_assert(!HasMoveIt<InputIteratorNotInputOrOutputIterator>); 41 static_assert(!HasMoveIt<int*, WeaklyIncrementableNotMovable>); 42 struct NotIndirectlyMovable {}; 43 static_assert(!HasMoveIt<int*, NotIndirectlyMovable*>); 44 static_assert(!HasMoveIt<int*, int*, SentinelForNotSemiregular>); 45 static_assert(!HasMoveIt<int*, int*, SentinelForNotWeaklyEqualityComparableWith>); 46 47 template <class Range, class Out> 48 concept HasMoveR = requires(Range range, Out out) { std::ranges::move(range, out); }; 49 50 static_assert(HasMoveR<std::array<int, 10>, int*>); 51 static_assert(!HasMoveR<InputRangeNotDerivedFrom, int*>); 52 static_assert(!HasMoveR<InputRangeNotIndirectlyReadable, int*>); 53 static_assert(!HasMoveR<InputRangeNotInputOrOutputIterator, int*>); 54 static_assert(!HasMoveR<WeaklyIncrementableNotMovable, int*>); 55 static_assert(!HasMoveR<UncheckedRange<NotIndirectlyMovable*>, int*>); 56 static_assert(!HasMoveR<InputRangeNotSentinelSemiregular, int*>); 57 static_assert(!HasMoveR<InputRangeNotSentinelEqualityComparableWith, int*>); 58 static_assert(!HasMoveR<UncheckedRange<int*>, WeaklyIncrementableNotMovable>); 59 60 static_assert(std::is_same_v<std::ranges::move_result<int, long>, std::ranges::in_out_result<int, long>>); 61 62 template <class In, class Out, class Sent, int N> 63 constexpr void test(std::array<int, N> in) { 64 { 65 std::array<int, N> out; 66 std::same_as<std::ranges::in_out_result<In, Out>> decltype(auto) ret = 67 std::ranges::move(In(in.data()), Sent(In(in.data() + in.size())), Out(out.data())); 68 assert(in == out); 69 assert(base(ret.in) == in.data() + in.size()); 70 assert(base(ret.out) == out.data() + out.size()); 71 } 72 { 73 std::array<int, N> out; 74 auto range = std::ranges::subrange(In(in.data()), Sent(In(in.data() + in.size()))); 75 std::same_as<std::ranges::in_out_result<In, Out>> decltype(auto) ret = 76 std::ranges::move(range, Out(out.data())); 77 assert(in == out); 78 assert(base(ret.in) == in.data() + in.size()); 79 assert(base(ret.out) == out.data() + out.size()); 80 } 81 } 82 83 template <class InContainer, class OutContainer, class In, class Out, class Sent = In> 84 constexpr void test_containers() { 85 { 86 InContainer in {1, 2, 3, 4}; 87 OutContainer out(4); 88 std::same_as<std::ranges::in_out_result<In, Out>> auto ret = 89 std::ranges::move(In(in.begin()), Sent(In(in.end())), Out(out.begin())); 90 assert(std::ranges::equal(in, out)); 91 assert(base(ret.in) == in.end()); 92 assert(base(ret.out) == out.end()); 93 } 94 { 95 InContainer in {1, 2, 3, 4}; 96 OutContainer out(4); 97 auto range = std::ranges::subrange(In(in.begin()), Sent(In(in.end()))); 98 std::same_as<std::ranges::in_out_result<In, Out>> auto ret = std::ranges::move(range, Out(out.begin())); 99 assert(std::ranges::equal(in, out)); 100 assert(base(ret.in) == in.end()); 101 assert(base(ret.out) == out.end()); 102 } 103 } 104 105 template <class In, class Out, class Sent = In> 106 constexpr void test_iterators() { 107 // simple test 108 test<In, Out, Sent, 4>({1, 2, 3, 4}); 109 // check that an empty range works 110 test<In, Out, Sent, 0>({}); 111 } 112 113 template <template <class> class In, template <class> class Out> 114 constexpr void test_sentinels() { 115 test_iterators<In<int*>, Out<int*>>(); 116 test_iterators<In<int*>, Out<int*>, sized_sentinel<In<int*>>>(); 117 test_iterators<In<int*>, Out<int*>, sentinel_wrapper<In<int*>>>(); 118 119 if (!std::is_constant_evaluated()) { 120 if constexpr (!std::is_same_v<In<int*>, contiguous_iterator<int*>> && 121 !std::is_same_v<Out<int*>, contiguous_iterator<int*>> && 122 !std::is_same_v<In<int*>, ContiguousProxyIterator<int*>> && 123 !std::is_same_v<Out<int*>, ContiguousProxyIterator<int*>>) { 124 test_containers<std::deque<int>, 125 std::deque<int>, 126 In<std::deque<int>::iterator>, 127 Out<std::deque<int>::iterator>>(); 128 test_containers<std::deque<int>, 129 std::vector<int>, 130 In<std::deque<int>::iterator>, 131 Out<std::vector<int>::iterator>>(); 132 test_containers<std::vector<int>, 133 std::deque<int>, 134 In<std::vector<int>::iterator>, 135 Out<std::deque<int>::iterator>>(); 136 test_containers<std::vector<int>, 137 std::vector<int>, 138 In<std::vector<int>::iterator>, 139 Out<std::vector<int>::iterator>>(); 140 } 141 } 142 } 143 144 template <template <class> class Out> 145 constexpr void test_in_iterators() { 146 test_iterators<cpp20_input_iterator<int*>, Out<int*>, sentinel_wrapper<cpp20_input_iterator<int*>>>(); 147 test_sentinels<forward_iterator, Out>(); 148 test_sentinels<bidirectional_iterator, Out>(); 149 test_sentinels<random_access_iterator, Out>(); 150 test_sentinels<contiguous_iterator, Out>(); 151 test_sentinels<std::type_identity_t, Out>(); 152 } 153 154 template <template <class> class Out> 155 constexpr void test_proxy_in_iterators() { 156 test_iterators<ProxyIterator<cpp20_input_iterator<int*>>, 157 Out<int*>, 158 sentinel_wrapper<ProxyIterator<cpp20_input_iterator<int*>>>>(); 159 test_sentinels<ForwardProxyIterator, Out>(); 160 test_sentinels<BidirectionalProxyIterator, Out>(); 161 test_sentinels<RandomAccessProxyIterator, Out>(); 162 test_sentinels<ContiguousProxyIterator, Out>(); 163 test_sentinels<ProxyIterator, Out>(); 164 } 165 166 struct IteratorWithMoveIter { 167 using value_type = int; 168 using difference_type = int; 169 explicit IteratorWithMoveIter() = default; 170 int* ptr; 171 constexpr IteratorWithMoveIter(int* ptr_) : ptr(ptr_) {} 172 173 constexpr int& operator*() const; // iterator with iter_move should not be dereferenced 174 175 constexpr IteratorWithMoveIter& operator++() { ++ptr; return *this; } 176 constexpr IteratorWithMoveIter operator++(int) { auto ret = *this; ++*this; return ret; } 177 178 friend constexpr int iter_move(const IteratorWithMoveIter&) { return 42; } 179 180 constexpr bool operator==(const IteratorWithMoveIter& other) const = default; 181 }; 182 183 // cpp17_intput_iterator has a defaulted template argument 184 template <class Iter> 185 using Cpp17InIter = cpp17_input_iterator<Iter>; 186 187 constexpr bool test() { 188 test_in_iterators<cpp17_output_iterator>(); 189 test_in_iterators<cpp20_output_iterator>(); 190 test_in_iterators<Cpp17InIter>(); 191 test_in_iterators<cpp20_input_iterator>(); 192 test_in_iterators<forward_iterator>(); 193 test_in_iterators<bidirectional_iterator>(); 194 test_in_iterators<random_access_iterator>(); 195 test_in_iterators<contiguous_iterator>(); 196 test_in_iterators<std::type_identity_t>(); 197 198 test_proxy_in_iterators<Cpp20InputProxyIterator>(); 199 test_proxy_in_iterators<ForwardProxyIterator>(); 200 test_proxy_in_iterators<BidirectionalProxyIterator>(); 201 test_proxy_in_iterators<RandomAccessProxyIterator>(); 202 test_proxy_in_iterators<ContiguousProxyIterator>(); 203 test_proxy_in_iterators<ProxyIterator>(); 204 205 { // check that a move-only type works 206 { 207 MoveOnly a[] = {1, 2, 3}; 208 MoveOnly b[3]; 209 std::ranges::move(a, std::begin(b)); 210 assert(b[0].get() == 1); 211 assert(b[1].get() == 2); 212 assert(b[2].get() == 3); 213 } 214 { 215 MoveOnly a[] = {1, 2, 3}; 216 MoveOnly b[3]; 217 std::ranges::move(std::begin(a), std::end(a), std::begin(b)); 218 assert(b[0].get() == 1); 219 assert(b[1].get() == 2); 220 assert(b[2].get() == 3); 221 } 222 } 223 224 { // check that a move-only type works for ProxyIterator 225 { 226 MoveOnly a[] = {1, 2, 3}; 227 MoveOnly b[3]; 228 ProxyRange proxyA{a}; 229 ProxyRange proxyB{b}; 230 std::ranges::move(proxyA, std::begin(proxyB)); 231 assert(b[0].get() == 1); 232 assert(b[1].get() == 2); 233 assert(b[2].get() == 3); 234 } 235 { 236 MoveOnly a[] = {1, 2, 3}; 237 MoveOnly b[3]; 238 ProxyRange proxyA{a}; 239 ProxyRange proxyB{b}; 240 std::ranges::move(std::begin(proxyA), std::end(proxyA), std::begin(proxyB)); 241 assert(b[0].get() == 1); 242 assert(b[1].get() == 2); 243 assert(b[2].get() == 3); 244 } 245 } 246 247 { // check that ranges::dangling is returned 248 std::array<int, 4> out; 249 std::same_as<std::ranges::in_out_result<std::ranges::dangling, int*>> decltype(auto) ret = 250 std::ranges::move(std::array {1, 2, 3, 4}, out.data()); 251 assert(ret.out == out.data() + 4); 252 assert((out == std::array{1, 2, 3, 4})); 253 } 254 255 { // check that an iterator is returned with a borrowing range 256 std::array in {1, 2, 3, 4}; 257 std::array<int, 4> out; 258 std::same_as<std::ranges::in_out_result<int*, int*>> decltype(auto) ret = 259 std::ranges::move(std::views::all(in), out.data()); 260 assert(ret.in == in.data() + 4); 261 assert(ret.out == out.data() + 4); 262 assert(in == out); 263 } 264 265 { // check that every element is moved exactly once 266 struct MoveOnce { 267 bool moved = false; 268 constexpr MoveOnce() = default; 269 constexpr MoveOnce(const MoveOnce& other) = delete; 270 constexpr MoveOnce& operator=(MoveOnce&& other) { 271 assert(!other.moved); 272 moved = true; 273 return *this; 274 } 275 }; 276 { 277 std::array<MoveOnce, 4> in {}; 278 std::array<MoveOnce, 4> out {}; 279 auto ret = std::ranges::move(in.begin(), in.end(), out.begin()); 280 assert(ret.in == in.end()); 281 assert(ret.out == out.end()); 282 assert(std::all_of(out.begin(), out.end(), [](const auto& e) { return e.moved; })); 283 } 284 { 285 std::array<MoveOnce, 4> in {}; 286 std::array<MoveOnce, 4> out {}; 287 auto ret = std::ranges::move(in, out.begin()); 288 assert(ret.in == in.end()); 289 assert(ret.out == out.end()); 290 assert(std::all_of(out.begin(), out.end(), [](const auto& e) { return e.moved; })); 291 } 292 } 293 294 { // check that the range is moved forwards 295 struct OnlyForwardsMovable { 296 OnlyForwardsMovable* next = nullptr; 297 bool canMove = false; 298 OnlyForwardsMovable() = default; 299 constexpr OnlyForwardsMovable& operator=(OnlyForwardsMovable&&) { 300 assert(canMove); 301 if (next != nullptr) 302 next->canMove = true; 303 return *this; 304 } 305 }; 306 { 307 std::array<OnlyForwardsMovable, 3> in {}; 308 std::array<OnlyForwardsMovable, 3> out {}; 309 out[0].next = &out[1]; 310 out[1].next = &out[2]; 311 out[0].canMove = true; 312 auto ret = std::ranges::move(in.begin(), in.end(), out.begin()); 313 assert(ret.in == in.end()); 314 assert(ret.out == out.end()); 315 assert(out[0].canMove); 316 assert(out[1].canMove); 317 assert(out[2].canMove); 318 } 319 { 320 std::array<OnlyForwardsMovable, 3> in {}; 321 std::array<OnlyForwardsMovable, 3> out {}; 322 out[0].next = &out[1]; 323 out[1].next = &out[2]; 324 out[0].canMove = true; 325 auto ret = std::ranges::move(in, out.begin()); 326 assert(ret.in == in.end()); 327 assert(ret.out == out.end()); 328 assert(out[0].canMove); 329 assert(out[1].canMove); 330 assert(out[2].canMove); 331 } 332 } 333 334 { // check that iter_move is used properly 335 { 336 int a[] = {1, 2, 3, 4}; 337 std::array<int, 4> b; 338 auto ret = std::ranges::move(IteratorWithMoveIter(a), IteratorWithMoveIter(a + 4), b.data()); 339 assert(ret.in == a + 4); 340 assert(ret.out == b.data() + 4); 341 assert((b == std::array {42, 42, 42, 42})); 342 } 343 { 344 int a[] = {1, 2, 3, 4}; 345 std::array<int, 4> b; 346 auto range = std::ranges::subrange(IteratorWithMoveIter(a), IteratorWithMoveIter(a + 4)); 347 auto ret = std::ranges::move(range, b.data()); 348 assert(ret.in == a + 4); 349 assert(ret.out == b.data() + 4); 350 assert((b == std::array {42, 42, 42, 42})); 351 } 352 } 353 354 return true; 355 } 356 357 int main(int, char**) { 358 test(); 359 static_assert(test()); 360 361 return 0; 362 } 363