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