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