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 14 // template<bidirectional_iterator I1, sentinel_for<I1> S1, bidirectional_iterator I2> 15 // requires indirectly_copyable<I1, I2> 16 // constexpr ranges::copy_backward_result<I1, I2> 17 // ranges::copy_backward(I1 first, S1 last, I2 result); 18 // template<bidirectional_range R, bidirectional_iterator I> 19 // requires indirectly_copyable<iterator_t<R>, I> 20 // constexpr ranges::copy_backward_result<borrowed_iterator_t<R>, I> 21 // ranges::copy_backward(R&& r, I result); 22 23 #include <algorithm> 24 #include <array> 25 #include <cassert> 26 #include <ranges> 27 28 #include "almost_satisfies_types.h" 29 #include "test_iterators.h" 30 31 template <class In, class Out = In, class Sent = sentinel_wrapper<In>> 32 concept HasCopyBackwardIt = requires(In in, Sent sent, Out out) { std::ranges::copy_backward(in, sent, out); }; 33 34 static_assert(HasCopyBackwardIt<int*>); 35 static_assert(!HasCopyBackwardIt<InputIteratorNotDerivedFrom>); 36 static_assert(!HasCopyBackwardIt<InputIteratorNotIndirectlyReadable>); 37 static_assert(!HasCopyBackwardIt<InputIteratorNotInputOrOutputIterator>); 38 static_assert(!HasCopyBackwardIt<int*, WeaklyIncrementableNotMovable>); 39 struct NotIndirectlyCopyable {}; 40 static_assert(!HasCopyBackwardIt<int*, NotIndirectlyCopyable*>); 41 static_assert(!HasCopyBackwardIt<int*, int*, SentinelForNotSemiregular>); 42 static_assert(!HasCopyBackwardIt<int*, int*, SentinelForNotWeaklyEqualityComparableWith>); 43 44 template <class Range, class Out> 45 concept HasCopyBackwardR = requires(Range range, Out out) { std::ranges::copy_backward(range, out); }; 46 47 static_assert(HasCopyBackwardR<std::array<int, 10>, int*>); 48 static_assert(!HasCopyBackwardR<InputRangeNotDerivedFrom, int*>); 49 static_assert(!HasCopyBackwardR<InputRangeNotIndirectlyReadable, int*>); 50 static_assert(!HasCopyBackwardR<InputRangeNotInputOrOutputIterator, int*>); 51 static_assert(!HasCopyBackwardR<WeaklyIncrementableNotMovable, int*>); 52 static_assert(!HasCopyBackwardR<UncheckedRange<NotIndirectlyCopyable*>, int*>); 53 static_assert(!HasCopyBackwardR<InputRangeNotSentinelSemiregular, int*>); 54 static_assert(!HasCopyBackwardR<InputRangeNotSentinelEqualityComparableWith, int*>); 55 56 static_assert(std::is_same_v<std::ranges::copy_result<int, long>, std::ranges::in_out_result<int, long>>); 57 58 template <class In, class Out, class Sent> 59 constexpr void test_iterators() { 60 { // simple test 61 { 62 std::array in {1, 2, 3, 4}; 63 std::array<int, 4> out; 64 std::same_as<std::ranges::in_out_result<In, Out>> auto ret = 65 std::ranges::copy_backward(In(in.data()), Sent(In(in.data() + in.size())), Out(out.data() + out.size())); 66 assert(in == out); 67 assert(base(ret.in) == in.data() + in.size()); 68 assert(base(ret.out) == out.data()); 69 } 70 { 71 std::array in {1, 2, 3, 4}; 72 std::array<int, 4> out; 73 auto range = std::ranges::subrange(In(in.data()), Sent(In(in.data() + in.size()))); 74 std::same_as<std::ranges::in_out_result<In, Out>> auto ret = 75 std::ranges::copy_backward(range, Out(out.data() + out.size())); 76 assert(in == out); 77 assert(base(ret.in) == in.data() + in.size()); 78 assert(base(ret.out) == out.data()); 79 } 80 } 81 82 { // check that an empty range works 83 { 84 std::array<int, 0> in; 85 std::array<int, 0> out; 86 auto ret = 87 std::ranges::copy_backward(In(in.data()), Sent(In(in.data() + in.size())), Out(out.data() + out.size())); 88 assert(base(ret.in) == in.data() + in.size()); 89 assert(base(ret.out) == out.data()); 90 } 91 { 92 std::array<int, 0> in; 93 std::array<int, 0> out; 94 auto range = std::ranges::subrange(In(in.data()), Sent(In(in.data() + in.size()))); 95 auto ret = std::ranges::copy_backward(range, Out(out.data())); 96 assert(base(ret.in) == in.data() + in.size()); 97 assert(base(ret.out) == out.data()); 98 } 99 } 100 } 101 102 template <class InIter, class OutIter> 103 constexpr void test_sentinels() { 104 test_iterators<InIter, OutIter, InIter>(); 105 test_iterators<InIter, OutIter, sentinel_wrapper<InIter>>(); 106 test_iterators<InIter, OutIter, sized_sentinel<InIter>>(); 107 } 108 109 template <class Out> 110 constexpr void test_in_iterators() { 111 test_sentinels<bidirectional_iterator<int*>, Out>(); 112 test_sentinels<random_access_iterator<int*>, Out>(); 113 test_sentinels<contiguous_iterator<int*>, Out>(); 114 } 115 116 template <class Out> 117 constexpr void test_proxy_in_iterators() { 118 test_sentinels<ProxyIterator<bidirectional_iterator<int*>>, Out>(); 119 test_sentinels<ProxyIterator<random_access_iterator<int*>>, Out>(); 120 test_sentinels<ProxyIterator<contiguous_iterator<int*>>, Out>(); 121 } 122 123 constexpr bool test() { 124 test_in_iterators<bidirectional_iterator<int*>>(); 125 test_in_iterators<random_access_iterator<int*>>(); 126 test_in_iterators<contiguous_iterator<int*>>(); 127 128 test_proxy_in_iterators<ProxyIterator<bidirectional_iterator<int*>>>(); 129 test_proxy_in_iterators<ProxyIterator<random_access_iterator<int*>>>(); 130 test_proxy_in_iterators<ProxyIterator<contiguous_iterator<int*>>>(); 131 132 { // check that ranges::dangling is returned 133 std::array<int, 4> out; 134 std::same_as<std::ranges::in_out_result<std::ranges::dangling, int*>> auto ret = 135 std::ranges::copy_backward(std::array {1, 2, 3, 4}, out.data() + out.size()); 136 assert(ret.out == out.data()); 137 assert((out == std::array{1, 2, 3, 4})); 138 } 139 140 { // check that an iterator is returned with a borrowing range 141 std::array in {1, 2, 3, 4}; 142 std::array<int, 4> out; 143 std::same_as<std::ranges::in_out_result<int*, int*>> auto ret = 144 std::ranges::copy_backward(std::views::all(in), out.data() + out.size()); 145 assert(ret.in == in.data() + in.size()); 146 assert(ret.out == out.data()); 147 assert(in == out); 148 } 149 150 { // check that every element is copied exactly once 151 struct CopyOnce { 152 bool copied = false; 153 constexpr CopyOnce() = default; 154 constexpr CopyOnce(const CopyOnce& other) = delete; 155 constexpr CopyOnce& operator=(const CopyOnce& other) { 156 assert(!other.copied); 157 copied = true; 158 return *this; 159 } 160 }; 161 { 162 std::array<CopyOnce, 4> in {}; 163 std::array<CopyOnce, 4> out {}; 164 auto ret = std::ranges::copy_backward(in.begin(), in.end(), out.end()); 165 assert(ret.in == in.end()); 166 assert(ret.out == out.begin()); 167 assert(std::all_of(out.begin(), out.end(), [](const auto& e) { return e.copied; })); 168 } 169 { 170 std::array<CopyOnce, 4> in {}; 171 std::array<CopyOnce, 4> out {}; 172 auto ret = std::ranges::copy_backward(in, out.end()); 173 assert(ret.in == in.end()); 174 assert(ret.out == out.begin()); 175 assert(std::all_of(out.begin(), out.end(), [](const auto& e) { return e.copied; })); 176 } 177 } 178 179 { // check that the range is copied backwards 180 struct OnlyBackwardsCopyable { 181 OnlyBackwardsCopyable* next = nullptr; 182 bool canCopy = false; 183 OnlyBackwardsCopyable() = default; 184 constexpr OnlyBackwardsCopyable& operator=(const OnlyBackwardsCopyable&) { 185 assert(canCopy); 186 if (next != nullptr) 187 next->canCopy = true; 188 return *this; 189 } 190 }; 191 { 192 std::array<OnlyBackwardsCopyable, 3> in {}; 193 std::array<OnlyBackwardsCopyable, 3> out {}; 194 out[1].next = &out[0]; 195 out[2].next = &out[1]; 196 out[2].canCopy = true; 197 auto ret = std::ranges::copy_backward(in, out.end()); 198 assert(ret.in == in.end()); 199 assert(ret.out == out.begin()); 200 assert(out[0].canCopy); 201 assert(out[1].canCopy); 202 assert(out[2].canCopy); 203 } 204 { 205 std::array<OnlyBackwardsCopyable, 3> in {}; 206 std::array<OnlyBackwardsCopyable, 3> out {}; 207 out[1].next = &out[0]; 208 out[2].next = &out[1]; 209 out[2].canCopy = true; 210 auto ret = std::ranges::copy_backward(in.begin(), in.end(), out.end()); 211 assert(ret.in == in.end()); 212 assert(ret.out == out.begin()); 213 assert(out[0].canCopy); 214 assert(out[1].canCopy); 215 assert(out[2].canCopy); 216 } 217 } 218 219 return true; 220 } 221 222 int main(int, char**) { 223 test(); 224 static_assert(test()); 225 226 return 0; 227 } 228