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