425 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9// <algorithm>10 11// UNSUPPORTED: c++03, c++11, c++14, c++1712// UNSUPPORTED: GCC-ALWAYS_INLINE-FIXME13 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 <iterator>28#include <ranges>29#include <vector>30 31#include "almost_satisfies_types.h"32#include "MoveOnly.h"33#include "test_iterators.h"34#include "test_macros.h"35 36template <class In, class Out = In, class Sent = sentinel_wrapper<In>>37concept HasMoveIt = requires(In in, Sent sent, Out out) { std::ranges::move(in, sent, out); };38 39static_assert(HasMoveIt<int*>);40static_assert(!HasMoveIt<InputIteratorNotDerivedFrom>);41static_assert(!HasMoveIt<InputIteratorNotIndirectlyReadable>);42static_assert(!HasMoveIt<InputIteratorNotInputOrOutputIterator>);43static_assert(!HasMoveIt<int*, WeaklyIncrementableNotMovable>);44struct NotIndirectlyMovable {};45static_assert(!HasMoveIt<int*, NotIndirectlyMovable*>);46static_assert(!HasMoveIt<int*, int*, SentinelForNotSemiregular>);47static_assert(!HasMoveIt<int*, int*, SentinelForNotWeaklyEqualityComparableWith>);48 49template <class Range, class Out>50concept HasMoveR = requires(Range range, Out out) { std::ranges::move(range, out); };51 52static_assert(HasMoveR<std::array<int, 10>, int*>);53static_assert(!HasMoveR<InputRangeNotDerivedFrom, int*>);54static_assert(!HasMoveR<InputRangeNotIndirectlyReadable, int*>);55static_assert(!HasMoveR<InputRangeNotInputOrOutputIterator, int*>);56static_assert(!HasMoveR<WeaklyIncrementableNotMovable, int*>);57static_assert(!HasMoveR<UncheckedRange<NotIndirectlyMovable*>, int*>);58static_assert(!HasMoveR<InputRangeNotSentinelSemiregular, int*>);59static_assert(!HasMoveR<InputRangeNotSentinelEqualityComparableWith, int*>);60static_assert(!HasMoveR<UncheckedRange<int*>, WeaklyIncrementableNotMovable>);61 62static_assert(std::is_same_v<std::ranges::move_result<int, long>, std::ranges::in_out_result<int, long>>);63 64template <class In, class Out, class Sent, int N>65constexpr void test(std::array<int, N> in) {66 {67 std::array<int, N> out;68 std::same_as<std::ranges::in_out_result<In, Out>> decltype(auto) ret =69 std::ranges::move(In(in.data()), Sent(In(in.data() + in.size())), Out(out.data()));70 assert(in == out);71 assert(base(ret.in) == in.data() + in.size());72 assert(base(ret.out) == out.data() + out.size());73 }74 {75 std::array<int, N> out;76 auto range = std::ranges::subrange(In(in.data()), Sent(In(in.data() + in.size())));77 std::same_as<std::ranges::in_out_result<In, Out>> decltype(auto) ret = std::ranges::move(range, Out(out.data()));78 assert(in == out);79 assert(base(ret.in) == in.data() + in.size());80 assert(base(ret.out) == out.data() + out.size());81 }82}83 84template <class InContainer, class OutContainer, class In, class Out, class Sent = In>85constexpr void test_containers() {86 {87 InContainer in{1, 2, 3, 4};88 OutContainer out(4);89 std::same_as<std::ranges::in_out_result<In, Out>> auto ret =90 std::ranges::move(In(in.begin()), Sent(In(in.end())), Out(out.begin()));91 assert(std::ranges::equal(in, out));92 assert(base(ret.in) == in.end());93 assert(base(ret.out) == out.end());94 }95 {96 InContainer in{1, 2, 3, 4};97 OutContainer out(4);98 auto range = std::ranges::subrange(In(in.begin()), Sent(In(in.end())));99 std::same_as<std::ranges::in_out_result<In, Out>> auto ret = std::ranges::move(range, Out(out.begin()));100 assert(std::ranges::equal(in, out));101 assert(base(ret.in) == in.end());102 assert(base(ret.out) == out.end());103 }104}105 106template <class In, class Out, class Sent = In>107constexpr void test_iterators() {108 // simple test109 test<In, Out, Sent, 4>({1, 2, 3, 4});110 // check that an empty range works111 test<In, Out, Sent, 0>({});112}113 114template <template <class> class In, template <class> class Out>115constexpr void test_sentinels() {116 test_iterators<In<int*>, Out<int*>>();117 test_iterators<In<int*>, Out<int*>, sized_sentinel<In<int*>>>();118 test_iterators<In<int*>, Out<int*>, sentinel_wrapper<In<int*>>>();119 120 if (!std::is_constant_evaluated()) {121 if constexpr (!std::is_same_v<In<int*>, contiguous_iterator<int*>> &&122 !std::is_same_v<Out<int*>, contiguous_iterator<int*>> &&123 !std::is_same_v<In<int*>, ContiguousProxyIterator<int*>> &&124 !std::is_same_v<Out<int*>, ContiguousProxyIterator<int*>>) {125 test_containers<std::deque<int>,126 std::deque<int>,127 In<std::deque<int>::iterator>,128 Out<std::deque<int>::iterator>>();129 test_containers<std::deque<int>,130 std::vector<int>,131 In<std::deque<int>::iterator>,132 Out<std::vector<int>::iterator>>();133 test_containers<std::vector<int>,134 std::deque<int>,135 In<std::vector<int>::iterator>,136 Out<std::deque<int>::iterator>>();137 test_containers<std::vector<int>,138 std::vector<int>,139 In<std::vector<int>::iterator>,140 Out<std::vector<int>::iterator>>();141 }142 }143}144 145template <template <class> class Out>146constexpr void test_in_iterators() {147 test_iterators<cpp20_input_iterator<int*>, Out<int*>, sentinel_wrapper<cpp20_input_iterator<int*>>>();148 test_sentinels<forward_iterator, Out>();149 test_sentinels<bidirectional_iterator, Out>();150 test_sentinels<random_access_iterator, Out>();151 test_sentinels<contiguous_iterator, Out>();152 test_sentinels<std::type_identity_t, Out>();153}154 155template <template <class> class Out>156constexpr void test_proxy_in_iterators() {157 test_iterators<ProxyIterator<cpp20_input_iterator<int*>>,158 Out<int*>,159 sentinel_wrapper<ProxyIterator<cpp20_input_iterator<int*>>>>();160 test_sentinels<ForwardProxyIterator, Out>();161 test_sentinels<BidirectionalProxyIterator, Out>();162 test_sentinels<RandomAccessProxyIterator, Out>();163 test_sentinels<ContiguousProxyIterator, Out>();164 test_sentinels<ProxyIterator, Out>();165}166 167struct IteratorWithMoveIter {168 using value_type = int;169 using difference_type = int;170 explicit IteratorWithMoveIter() = default;171 int* ptr;172 constexpr IteratorWithMoveIter(int* ptr_) : ptr(ptr_) {}173 174 constexpr int& operator*() const; // iterator with iter_move should not be dereferenced175 176 constexpr IteratorWithMoveIter& operator++() {177 ++ptr;178 return *this;179 }180 constexpr IteratorWithMoveIter operator++(int) {181 auto ret = *this;182 ++*this;183 return ret;184 }185 186 friend constexpr int iter_move(const IteratorWithMoveIter&) { return 42; }187 188 constexpr bool operator==(const IteratorWithMoveIter& other) const = default;189};190 191#if TEST_STD_VER >= 23192constexpr bool test_vector_bool(std::size_t N) {193 std::vector<bool> v(N, false);194 for (std::size_t i = 0; i < N; i += 2)195 v[i] = true;196 197 { // Test move with aligned bytes198 std::vector<bool> in{v};199 std::vector<bool> out(N);200 std::ranges::move(in, out.begin());201 assert(out == v);202 }203 { // Test move with unaligned bytes204 std::vector<bool> in{v};205 std::vector<bool> out(N);206 std::ranges::move(std::views::counted(in.begin() + 4, N - 4), out.begin());207 assert(std::ranges::equal(v | std::views::drop(4), out | std::views::take(N - 4)));208 }209 210 return true;211}212#endif213 214// cpp17_intput_iterator has a defaulted template argument215template <class Iter>216using Cpp17InIter = cpp17_input_iterator<Iter>;217 218constexpr bool test() {219 test_in_iterators<cpp17_output_iterator>();220 test_in_iterators<cpp20_output_iterator>();221 test_in_iterators<Cpp17InIter>();222 test_in_iterators<cpp20_input_iterator>();223 test_in_iterators<forward_iterator>();224 test_in_iterators<bidirectional_iterator>();225 test_in_iterators<random_access_iterator>();226 test_in_iterators<contiguous_iterator>();227 test_in_iterators<std::type_identity_t>();228 229 test_proxy_in_iterators<Cpp20InputProxyIterator>();230 test_proxy_in_iterators<ForwardProxyIterator>();231 test_proxy_in_iterators<BidirectionalProxyIterator>();232 test_proxy_in_iterators<RandomAccessProxyIterator>();233 test_proxy_in_iterators<ContiguousProxyIterator>();234 test_proxy_in_iterators<ProxyIterator>();235 236 { // check that a move-only type works237 // When non-trivial238 {239 MoveOnly a[] = {1, 2, 3};240 MoveOnly b[3];241 std::ranges::move(a, std::begin(b));242 assert(b[0].get() == 1);243 assert(b[1].get() == 2);244 assert(b[2].get() == 3);245 }246 {247 MoveOnly a[] = {1, 2, 3};248 MoveOnly b[3];249 std::ranges::move(std::begin(a), std::end(a), std::begin(b));250 assert(b[0].get() == 1);251 assert(b[1].get() == 2);252 assert(b[2].get() == 3);253 }254 255 // When trivial256 {257 TrivialMoveOnly a[] = {1, 2, 3};258 TrivialMoveOnly b[3];259 std::ranges::move(a, std::begin(b));260 assert(b[0].get() == 1);261 assert(b[1].get() == 2);262 assert(b[2].get() == 3);263 }264 {265 TrivialMoveOnly a[] = {1, 2, 3};266 TrivialMoveOnly b[3];267 std::ranges::move(std::begin(a), std::end(a), std::begin(b));268 assert(b[0].get() == 1);269 assert(b[1].get() == 2);270 assert(b[2].get() == 3);271 }272 }273 274 { // check that a move-only type works for ProxyIterator275 {276 MoveOnly a[] = {1, 2, 3};277 MoveOnly b[3];278 ProxyRange proxyA{a};279 ProxyRange proxyB{b};280 std::ranges::move(proxyA, std::begin(proxyB));281 assert(b[0].get() == 1);282 assert(b[1].get() == 2);283 assert(b[2].get() == 3);284 }285 {286 MoveOnly a[] = {1, 2, 3};287 MoveOnly b[3];288 ProxyRange proxyA{a};289 ProxyRange proxyB{b};290 std::ranges::move(std::begin(proxyA), std::end(proxyA), std::begin(proxyB));291 assert(b[0].get() == 1);292 assert(b[1].get() == 2);293 assert(b[2].get() == 3);294 }295 }296 297 { // check that ranges::dangling is returned298 std::array<int, 4> out;299 std::same_as<std::ranges::in_out_result<std::ranges::dangling, int*>> decltype(auto) ret =300 std::ranges::move(std::array{1, 2, 3, 4}, out.data());301 assert(ret.out == out.data() + 4);302 assert((out == std::array{1, 2, 3, 4}));303 }304 305 { // check that an iterator is returned with a borrowing range306 std::array in{1, 2, 3, 4};307 std::array<int, 4> out;308 std::same_as<std::ranges::in_out_result<std::array<int, 4>::iterator, int*>> decltype(auto) ret =309 std::ranges::move(std::views::all(in), out.data());310 assert(ret.in == in.end());311 assert(ret.out == out.data() + 4);312 assert(in == out);313 }314 315 { // check that every element is moved exactly once316 struct MoveOnce {317 bool moved = false;318 constexpr MoveOnce() = default;319 constexpr MoveOnce(const MoveOnce& other) = delete;320 constexpr MoveOnce& operator=(MoveOnce&& other) {321 assert(!other.moved);322 moved = true;323 return *this;324 }325 };326 {327 std::array<MoveOnce, 4> in{};328 std::array<MoveOnce, 4> out{};329 auto ret = std::ranges::move(in.begin(), in.end(), out.begin());330 assert(ret.in == in.end());331 assert(ret.out == out.end());332 assert(std::all_of(out.begin(), out.end(), [](const auto& e) { return e.moved; }));333 }334 {335 std::array<MoveOnce, 4> in{};336 std::array<MoveOnce, 4> out{};337 auto ret = std::ranges::move(in, out.begin());338 assert(ret.in == in.end());339 assert(ret.out == out.end());340 assert(std::all_of(out.begin(), out.end(), [](const auto& e) { return e.moved; }));341 }342 }343 344 { // check that the range is moved forwards345 struct OnlyForwardsMovable {346 OnlyForwardsMovable* next = nullptr;347 bool canMove = false;348 OnlyForwardsMovable() = default;349 constexpr OnlyForwardsMovable& operator=(OnlyForwardsMovable&&) {350 assert(canMove);351 if (next != nullptr)352 next->canMove = true;353 return *this;354 }355 };356 {357 std::array<OnlyForwardsMovable, 3> in{};358 std::array<OnlyForwardsMovable, 3> out{};359 out[0].next = &out[1];360 out[1].next = &out[2];361 out[0].canMove = true;362 auto ret = std::ranges::move(in.begin(), in.end(), out.begin());363 assert(ret.in == in.end());364 assert(ret.out == out.end());365 assert(out[0].canMove);366 assert(out[1].canMove);367 assert(out[2].canMove);368 }369 {370 std::array<OnlyForwardsMovable, 3> in{};371 std::array<OnlyForwardsMovable, 3> out{};372 out[0].next = &out[1];373 out[1].next = &out[2];374 out[0].canMove = true;375 auto ret = std::ranges::move(in, out.begin());376 assert(ret.in == in.end());377 assert(ret.out == out.end());378 assert(out[0].canMove);379 assert(out[1].canMove);380 assert(out[2].canMove);381 }382 }383 384 { // check that iter_move is used properly385 {386 int a[] = {1, 2, 3, 4};387 std::array<int, 4> b;388 auto ret = std::ranges::move(IteratorWithMoveIter(a), IteratorWithMoveIter(a + 4), b.data());389 assert(ret.in == a + 4);390 assert(ret.out == b.data() + 4);391 assert((b == std::array{42, 42, 42, 42}));392 }393 {394 int a[] = {1, 2, 3, 4};395 std::array<int, 4> b;396 auto range = std::ranges::subrange(IteratorWithMoveIter(a), IteratorWithMoveIter(a + 4));397 auto ret = std::ranges::move(range, b.data());398 assert(ret.in == a + 4);399 assert(ret.out == b.data() + 4);400 assert((b == std::array{42, 42, 42, 42}));401 }402 }403 404#if TEST_STD_VER >= 23405 { // Test vector<bool>::iterator optimization406 assert(test_vector_bool(8));407 assert(test_vector_bool(19));408 assert(test_vector_bool(32));409 assert(test_vector_bool(49));410 assert(test_vector_bool(64));411 assert(test_vector_bool(199));412 assert(test_vector_bool(256));413 }414#endif415 416 return true;417}418 419int main(int, char**) {420 test();421 static_assert(test());422 423 return 0;424}425