xref: /llvm-project/libcxx/test/std/algorithms/alg.modifying.operations/alg.copy/ranges.copy.pass.cpp (revision e01b4fe956dd038fed71cf3c552d3383905d022a)
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 In, class Out>
99 constexpr void test_sentinels() {
100   test_iterators<In, Out>();
101   test_iterators<In, Out, sized_sentinel<In>>();
102   test_iterators<In, Out, sentinel_wrapper<In>>();
103 }
104 
105 template <class Out>
106 constexpr void test_in_iterators() {
107   test_iterators<cpp20_input_iterator<int*>, Out, sentinel_wrapper<cpp20_input_iterator<int*>>>();
108   test_sentinels<forward_iterator<int*>, Out>();
109   test_sentinels<bidirectional_iterator<int*>, Out>();
110   test_sentinels<random_access_iterator<int*>, Out>();
111   test_sentinels<contiguous_iterator<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 
130   test_proxy_in_iterators<ProxyIterator<cpp20_input_iterator<int*>>>();
131   test_proxy_in_iterators<ProxyIterator<forward_iterator<int*>>>();
132   test_proxy_in_iterators<ProxyIterator<bidirectional_iterator<int*>>>();
133   test_proxy_in_iterators<ProxyIterator<random_access_iterator<int*>>>();
134   test_proxy_in_iterators<ProxyIterator<contiguous_iterator<int*>>>();
135 
136   { // check that ranges::dangling is returned
137     std::array<int, 4> out;
138     std::same_as<std::ranges::in_out_result<std::ranges::dangling, int*>> auto ret =
139       std::ranges::copy(std::array {1, 2, 3, 4}, out.data());
140     assert(ret.out == out.data() + 4);
141     assert((out == std::array{1, 2, 3, 4}));
142   }
143 
144   { // check that an iterator is returned with a borrowing range
145     std::array in {1, 2, 3, 4};
146     std::array<int, 4> out;
147     std::same_as<std::ranges::in_out_result<int*, int*>> auto ret = std::ranges::copy(std::views::all(in), out.data());
148     assert(ret.in == in.data() + 4);
149     assert(ret.out == out.data() + 4);
150     assert(in == out);
151   }
152 
153   { // check that every element is copied exactly once
154     struct CopyOnce {
155       bool copied = false;
156       constexpr CopyOnce() = default;
157       constexpr CopyOnce(const CopyOnce& other) = delete;
158       constexpr CopyOnce& operator=(const CopyOnce& other) {
159         assert(!other.copied);
160         copied = true;
161         return *this;
162       }
163     };
164     {
165       std::array<CopyOnce, 4> in {};
166       std::array<CopyOnce, 4> out {};
167       auto ret = std::ranges::copy(in.begin(), in.end(), out.begin());
168       assert(ret.in == in.end());
169       assert(ret.out == out.end());
170       assert(std::all_of(out.begin(), out.end(), [](const auto& e) { return e.copied; }));
171     }
172     {
173       std::array<CopyOnce, 4> in {};
174       std::array<CopyOnce, 4> out {};
175       auto ret = std::ranges::copy(in, out.begin());
176       assert(ret.in == in.end());
177       assert(ret.out == out.end());
178       assert(std::all_of(out.begin(), out.end(), [](const auto& e) { return e.copied; }));
179     }
180   }
181 
182   { // check that the range is copied forwards
183     struct OnlyForwardsCopyable {
184       OnlyForwardsCopyable* next = nullptr;
185       bool canCopy = false;
186       OnlyForwardsCopyable() = default;
187       constexpr OnlyForwardsCopyable& operator=(const OnlyForwardsCopyable&) {
188         assert(canCopy);
189         if (next != nullptr)
190           next->canCopy = true;
191         return *this;
192       }
193     };
194     {
195       std::array<OnlyForwardsCopyable, 3> in {};
196       std::array<OnlyForwardsCopyable, 3> out {};
197       out[0].next = &out[1];
198       out[1].next = &out[2];
199       out[0].canCopy = true;
200       auto ret = std::ranges::copy(in.begin(), in.end(), out.begin());
201       assert(ret.in == in.end());
202       assert(ret.out == out.end());
203       assert(out[0].canCopy);
204       assert(out[1].canCopy);
205       assert(out[2].canCopy);
206     }
207     {
208       std::array<OnlyForwardsCopyable, 3> in {};
209       std::array<OnlyForwardsCopyable, 3> out {};
210       out[0].next = &out[1];
211       out[1].next = &out[2];
212       out[0].canCopy = true;
213       auto ret = std::ranges::copy(in, out.begin());
214       assert(ret.in == in.end());
215       assert(ret.out == out.end());
216       assert(out[0].canCopy);
217       assert(out[1].canCopy);
218       assert(out[2].canCopy);
219     }
220   }
221 
222   return true;
223 }
224 
225 int main(int, char**) {
226   test();
227   static_assert(test());
228 
229   return 0;
230 }
231