xref: /llvm-project/libcxx/test/std/algorithms/alg.modifying.operations/alg.copy/ranges.copy.pass.cpp (revision 1323461fe7cf92f6b7bdb1f088fe4c748660a046)
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 #include "type_algorithms.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 constexpr bool test() {
99   meta::for_each(meta::forward_iterator_list<int*>{}, []<class Out>() {
100     test_iterators<cpp20_input_iterator<int*>, Out, sentinel_wrapper<cpp20_input_iterator<int*>>>();
101     test_iterators<ProxyIterator<cpp20_input_iterator<int*>>,
102                    ProxyIterator<Out>,
103                    sentinel_wrapper<ProxyIterator<cpp20_input_iterator<int*>>>>();
104 
105     meta::for_each(meta::forward_iterator_list<int*>{}, []<class In>() {
106       test_iterators<In, Out>();
107       test_iterators<In, Out, sized_sentinel<In>>();
108       test_iterators<In, Out, sentinel_wrapper<In>>();
109 
110       test_iterators<ProxyIterator<In>, ProxyIterator<Out>>();
111       test_iterators<ProxyIterator<In>, ProxyIterator<Out>, sized_sentinel<ProxyIterator<In>>>();
112       test_iterators<ProxyIterator<In>, ProxyIterator<Out>, sentinel_wrapper<ProxyIterator<In>>>();
113     });
114   });
115 
116   { // check that ranges::dangling is returned
117     std::array<int, 4> out;
118     std::same_as<std::ranges::in_out_result<std::ranges::dangling, int*>> auto ret =
119         std::ranges::copy(std::array{1, 2, 3, 4}, out.data());
120     assert(ret.out == out.data() + 4);
121     assert((out == std::array{1, 2, 3, 4}));
122   }
123 
124   { // check that an iterator is returned with a borrowing range
125     std::array in {1, 2, 3, 4};
126     std::array<int, 4> out;
127     std::same_as<std::ranges::in_out_result<int*, int*>> auto ret = std::ranges::copy(std::views::all(in), out.data());
128     assert(ret.in == in.data() + 4);
129     assert(ret.out == out.data() + 4);
130     assert(in == out);
131   }
132 
133   { // check that every element is copied exactly once
134     struct CopyOnce {
135       bool copied = false;
136       constexpr CopyOnce() = default;
137       constexpr CopyOnce(const CopyOnce& other) = delete;
138       constexpr CopyOnce& operator=(const CopyOnce& other) {
139         assert(!other.copied);
140         copied = true;
141         return *this;
142       }
143     };
144     {
145       std::array<CopyOnce, 4> in {};
146       std::array<CopyOnce, 4> out {};
147       auto ret = std::ranges::copy(in.begin(), in.end(), out.begin());
148       assert(ret.in == in.end());
149       assert(ret.out == out.end());
150       assert(std::all_of(out.begin(), out.end(), [](const auto& e) { return e.copied; }));
151     }
152     {
153       std::array<CopyOnce, 4> in {};
154       std::array<CopyOnce, 4> out {};
155       auto ret = std::ranges::copy(in, out.begin());
156       assert(ret.in == in.end());
157       assert(ret.out == out.end());
158       assert(std::all_of(out.begin(), out.end(), [](const auto& e) { return e.copied; }));
159     }
160   }
161 
162   { // check that the range is copied forwards
163     struct OnlyForwardsCopyable {
164       OnlyForwardsCopyable* next = nullptr;
165       bool canCopy = false;
166       OnlyForwardsCopyable() = default;
167       constexpr OnlyForwardsCopyable& operator=(const OnlyForwardsCopyable&) {
168         assert(canCopy);
169         if (next != nullptr)
170           next->canCopy = true;
171         return *this;
172       }
173     };
174     {
175       std::array<OnlyForwardsCopyable, 3> in {};
176       std::array<OnlyForwardsCopyable, 3> out {};
177       out[0].next = &out[1];
178       out[1].next = &out[2];
179       out[0].canCopy = true;
180       auto ret = std::ranges::copy(in.begin(), in.end(), out.begin());
181       assert(ret.in == in.end());
182       assert(ret.out == out.end());
183       assert(out[0].canCopy);
184       assert(out[1].canCopy);
185       assert(out[2].canCopy);
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, 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 
202   return true;
203 }
204 
205 int main(int, char**) {
206   test();
207   static_assert(test());
208 
209   return 0;
210 }
211