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