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