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<bidirectional_iterator I1, sentinel_for<I1> S1, bidirectional_iterator I2> 14 // requires indirectly_movable<I1, I2> 15 // constexpr ranges::move_backward_result<I1, I2> 16 // ranges::move_backward(I1 first, S1 last, I2 result); 17 // template<bidirectional_range R, bidirectional_iterator I> 18 // requires indirectly_movable<iterator_t<R>, I> 19 // constexpr ranges::move_backward_result<borrowed_iterator_t<R>, I> 20 // ranges::move_backward(R&& r, I 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 HasMoveBackwardIt = requires(In in, Sent sent, Out out) { std::ranges::move_backward(in, sent, out); }; 33 34 static_assert(HasMoveBackwardIt<int*>); 35 static_assert(!HasMoveBackwardIt<InputIteratorNotDerivedFrom>); 36 static_assert(!HasMoveBackwardIt<InputIteratorNotIndirectlyReadable>); 37 static_assert(!HasMoveBackwardIt<InputIteratorNotInputOrOutputIterator>); 38 static_assert(!HasMoveBackwardIt<int*, WeaklyIncrementableNotMovable>); 39 struct NotIndirectlyCopyable {}; 40 static_assert(!HasMoveBackwardIt<int*, NotIndirectlyCopyable*>); 41 static_assert(!HasMoveBackwardIt<int*, int*, SentinelForNotSemiregular>); 42 static_assert(!HasMoveBackwardIt<int*, int*, SentinelForNotWeaklyEqualityComparableWith>); 43 44 template <class Range, class Out> 45 concept HasMoveBackwardR = requires(Range range, Out out) { std::ranges::move_backward(range, out); }; 46 47 static_assert(HasMoveBackwardR<std::array<int, 10>, int*>); 48 static_assert(!HasMoveBackwardR<InputRangeNotDerivedFrom, int*>); 49 static_assert(!HasMoveBackwardR<InputRangeNotIndirectlyReadable, int*>); 50 static_assert(!HasMoveBackwardR<InputRangeNotInputOrOutputIterator, int*>); 51 static_assert(!HasMoveBackwardR<WeaklyIncrementableNotMovable, int*>); 52 static_assert(!HasMoveBackwardR<UncheckedRange<NotIndirectlyCopyable*>, int*>); 53 static_assert(!HasMoveBackwardR<InputRangeNotSentinelSemiregular, int*>); 54 static_assert(!HasMoveBackwardR<InputRangeNotSentinelEqualityComparableWith, int*>); 55 static_assert(!HasMoveBackwardR<UncheckedRange<int*>, WeaklyIncrementableNotMovable>); 56 57 static_assert(std::is_same_v<std::ranges::copy_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_backward(In(in.data()), Sent(In(in.data() + in.size())), Out(out.data() + out.size())); 65 assert(in == out); 66 assert(base(ret.in) == in.data() + in.size()); 67 assert(base(ret.out) == out.data()); 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_backward(range, Out(out.data() + out.size())); 74 assert(in == out); 75 assert(base(ret.in) == in.data() + in.size()); 76 assert(base(ret.out) == out.data()); 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<bidirectional_iterator<int*>, Out, sentinel_wrapper<bidirectional_iterator<int*>>>(); 91 test_iterators<bidirectional_iterator<int*>, Out>(); 92 test_iterators<random_access_iterator<int*>, Out>(); 93 test_iterators<contiguous_iterator<int*>, Out>(); 94 } 95 96 template <class Out> 97 constexpr void test_proxy_in_iterators() { 98 test_iterators<ProxyIterator<bidirectional_iterator<int*>>, Out, sentinel_wrapper<ProxyIterator<bidirectional_iterator<int*>>>>(); 99 test_iterators<ProxyIterator<bidirectional_iterator<int*>>, Out>(); 100 test_iterators<ProxyIterator<random_access_iterator<int*>>, Out>(); 101 test_iterators<ProxyIterator<contiguous_iterator<int*>>, Out>(); 102 } 103 104 struct IteratorWithMoveIter { 105 using value_type = int; 106 using difference_type = int; 107 explicit IteratorWithMoveIter() = default; 108 int* ptr; 109 constexpr IteratorWithMoveIter(int* ptr_) : ptr(ptr_) {} 110 111 constexpr int& operator*() const; // iterator with iter_move should not be dereferenced 112 113 constexpr IteratorWithMoveIter& operator++() { ++ptr; return *this; } 114 constexpr IteratorWithMoveIter operator++(int) { auto ret = *this; ++*this; return ret; } 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<bidirectional_iterator<int*>>(); 126 test_in_iterators<random_access_iterator<int*>>(); 127 test_in_iterators<contiguous_iterator<int*>>(); 128 129 test_proxy_in_iterators<ProxyIterator<bidirectional_iterator<int*>>>(); 130 test_proxy_in_iterators<ProxyIterator<random_access_iterator<int*>>>(); 131 test_proxy_in_iterators<ProxyIterator<contiguous_iterator<int*>>>(); 132 133 { // check that a move-only type works 134 { 135 MoveOnly a[] = {1, 2, 3}; 136 MoveOnly b[3]; 137 std::ranges::move_backward(a, std::end(b)); 138 assert(b[0].get() == 1); 139 assert(b[1].get() == 2); 140 assert(b[2].get() == 3); 141 } 142 { 143 MoveOnly a[] = {1, 2, 3}; 144 MoveOnly b[3]; 145 std::ranges::move_backward(std::begin(a), std::end(a), std::end(b)); 146 assert(b[0].get() == 1); 147 assert(b[1].get() == 2); 148 assert(b[2].get() == 3); 149 } 150 } 151 152 { // check that a move-only type works for ProxyIterator 153 { 154 MoveOnly a[] = {1, 2, 3}; 155 MoveOnly b[3]; 156 ProxyRange proxyA{a}; 157 ProxyRange proxyB{b}; 158 std::ranges::move_backward(proxyA, std::ranges::next(proxyB.begin(), std::end(proxyB))); 159 assert(b[0].get() == 1); 160 assert(b[1].get() == 2); 161 assert(b[2].get() == 3); 162 } 163 { 164 MoveOnly a[] = {1, 2, 3}; 165 MoveOnly b[3]; 166 ProxyRange proxyA{a}; 167 ProxyRange proxyB{b}; 168 std::ranges::move_backward(std::begin(proxyA), std::end(proxyA), std::ranges::next(proxyB.begin(), std::end(proxyB))); 169 assert(b[0].get() == 1); 170 assert(b[1].get() == 2); 171 assert(b[2].get() == 3); 172 } 173 } 174 175 { // check that ranges::dangling is returned 176 std::array<int, 4> out; 177 std::same_as<std::ranges::in_out_result<std::ranges::dangling, int*>> auto ret = 178 std::ranges::move_backward(std::array {1, 2, 3, 4}, out.data() + out.size()); 179 assert(ret.out == out.data()); 180 assert((out == std::array{1, 2, 3, 4})); 181 } 182 183 { // check that an iterator is returned with a borrowing range 184 std::array in {1, 2, 3, 4}; 185 std::array<int, 4> out; 186 std::same_as<std::ranges::in_out_result<int*, int*>> auto ret = 187 std::ranges::move_backward(std::views::all(in), out.data() + out.size()); 188 assert(ret.in == in.data() + in.size()); 189 assert(ret.out == out.data()); 190 assert(in == out); 191 } 192 193 { // check that every element is moved exactly once 194 struct MoveOnce { 195 bool moved = false; 196 constexpr MoveOnce() = default; 197 constexpr MoveOnce(const MoveOnce& other) = delete; 198 constexpr MoveOnce& operator=(const MoveOnce& other) { 199 assert(!other.moved); 200 moved = true; 201 return *this; 202 } 203 }; 204 { 205 std::array<MoveOnce, 4> in {}; 206 std::array<MoveOnce, 4> out {}; 207 auto ret = std::ranges::move_backward(in.begin(), in.end(), out.end()); 208 assert(ret.in == in.end()); 209 assert(ret.out == out.begin()); 210 assert(std::all_of(out.begin(), out.end(), [](const auto& e) { return e.moved; })); 211 } 212 { 213 std::array<MoveOnce, 4> in {}; 214 std::array<MoveOnce, 4> out {}; 215 auto ret = std::ranges::move_backward(in, out.end()); 216 assert(ret.in == in.end()); 217 assert(ret.out == out.begin()); 218 assert(std::all_of(out.begin(), out.end(), [](const auto& e) { return e.moved; })); 219 } 220 } 221 222 { // check that the range is moved backwards 223 struct OnlyBackwardsMovable { 224 OnlyBackwardsMovable* next = nullptr; 225 bool canMove = false; 226 OnlyBackwardsMovable() = default; 227 constexpr OnlyBackwardsMovable& operator=(const OnlyBackwardsMovable&) { 228 assert(canMove); 229 if (next != nullptr) 230 next->canMove = true; 231 return *this; 232 } 233 }; 234 { 235 std::array<OnlyBackwardsMovable, 3> in {}; 236 std::array<OnlyBackwardsMovable, 3> out {}; 237 out[1].next = &out[0]; 238 out[2].next = &out[1]; 239 out[2].canMove = true; 240 auto ret = std::ranges::move_backward(in, out.end()); 241 assert(ret.in == in.end()); 242 assert(ret.out == out.begin()); 243 assert(out[0].canMove); 244 assert(out[1].canMove); 245 assert(out[2].canMove); 246 } 247 { 248 std::array<OnlyBackwardsMovable, 3> in {}; 249 std::array<OnlyBackwardsMovable, 3> out {}; 250 out[1].next = &out[0]; 251 out[2].next = &out[1]; 252 out[2].canMove = true; 253 auto ret = std::ranges::move_backward(in.begin(), in.end(), out.end()); 254 assert(ret.in == in.end()); 255 assert(ret.out == out.begin()); 256 assert(out[0].canMove); 257 assert(out[1].canMove); 258 assert(out[2].canMove); 259 } 260 } 261 262 { // check that iter_move is used properly 263 { 264 int a[] = {1, 2, 3, 4}; 265 std::array<int, 4> b; 266 auto ret = std::ranges::move_backward(IteratorWithMoveIter(a), IteratorWithMoveIter(a + 4), b.data() + b.size()); 267 assert(ret.in == a + 4); 268 assert(ret.out == b.data()); 269 assert((b == std::array {42, 42, 42, 42})); 270 } 271 { 272 int a[] = {1, 2, 3, 4}; 273 std::array<int, 4> b; 274 auto range = std::ranges::subrange(IteratorWithMoveIter(a), IteratorWithMoveIter(a + 4)); 275 auto ret = std::ranges::move_backward(range, b.data() + b.size()); 276 assert(ret.in == a + 4); 277 assert(ret.out == b.data()); 278 assert((b == std::array {42, 42, 42, 42})); 279 } 280 } 281 282 return true; 283 } 284 285 int main(int, char**) { 286 test(); 287 static_assert(test()); 288 289 return 0; 290 } 291