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 
14 // template<bidirectional_iterator I1, sentinel_for<I1> S1, bidirectional_iterator I2>
15 //   requires indirectly_copyable<I1, I2>
16 //   constexpr ranges::copy_backward_result<I1, I2>
17 //     ranges::copy_backward(I1 first, S1 last, I2 result);
18 // template<bidirectional_range R, bidirectional_iterator I>
19 //   requires indirectly_copyable<iterator_t<R>, I>
20 //   constexpr ranges::copy_backward_result<borrowed_iterator_t<R>, I>
21 //     ranges::copy_backward(R&& r, I result);
22 
23 #include <algorithm>
24 #include <array>
25 #include <cassert>
26 #include <ranges>
27 
28 #include "almost_satisfies_types.h"
29 #include "test_iterators.h"
30 
31 template <class In, class Out = In, class Sent = sentinel_wrapper<In>>
32 concept HasCopyBackwardIt = requires(In in, Sent sent, Out out) { std::ranges::copy_backward(in, sent, out); };
33 
34 static_assert(HasCopyBackwardIt<int*>);
35 static_assert(!HasCopyBackwardIt<InputIteratorNotDerivedFrom>);
36 static_assert(!HasCopyBackwardIt<InputIteratorNotIndirectlyReadable>);
37 static_assert(!HasCopyBackwardIt<InputIteratorNotInputOrOutputIterator>);
38 static_assert(!HasCopyBackwardIt<int*, WeaklyIncrementableNotMovable>);
39 struct NotIndirectlyCopyable {};
40 static_assert(!HasCopyBackwardIt<int*, NotIndirectlyCopyable*>);
41 static_assert(!HasCopyBackwardIt<int*, int*, SentinelForNotSemiregular>);
42 static_assert(!HasCopyBackwardIt<int*, int*, SentinelForNotWeaklyEqualityComparableWith>);
43 
44 template <class Range, class Out>
45 concept HasCopyBackwardR = requires(Range range, Out out) { std::ranges::copy_backward(range, out); };
46 
47 static_assert(HasCopyBackwardR<std::array<int, 10>, int*>);
48 static_assert(!HasCopyBackwardR<InputRangeNotDerivedFrom, int*>);
49 static_assert(!HasCopyBackwardR<InputRangeNotIndirectlyReadable, int*>);
50 static_assert(!HasCopyBackwardR<InputRangeNotInputOrOutputIterator, int*>);
51 static_assert(!HasCopyBackwardR<WeaklyIncrementableNotMovable, int*>);
52 static_assert(!HasCopyBackwardR<UncheckedRange<NotIndirectlyCopyable*>, int*>);
53 static_assert(!HasCopyBackwardR<InputRangeNotSentinelSemiregular, int*>);
54 static_assert(!HasCopyBackwardR<InputRangeNotSentinelEqualityComparableWith, int*>);
55 
56 static_assert(std::is_same_v<std::ranges::copy_result<int, long>, std::ranges::in_out_result<int, long>>);
57 
58 template <class In, class Out, class Sent>
59 constexpr void test_iterators() {
60   { // simple test
61     {
62       std::array in {1, 2, 3, 4};
63       std::array<int, 4> out;
64       std::same_as<std::ranges::in_out_result<In, Out>> auto ret =
65         std::ranges::copy_backward(In(in.data()), Sent(In(in.data() + in.size())), Out(out.data() + out.size()));
66       assert(in == out);
67       assert(base(ret.in) == in.data() + in.size());
68       assert(base(ret.out) == out.data());
69     }
70     {
71       std::array in {1, 2, 3, 4};
72       std::array<int, 4> out;
73       auto range = std::ranges::subrange(In(in.data()), Sent(In(in.data() + in.size())));
74       std::same_as<std::ranges::in_out_result<In, Out>> auto ret =
75           std::ranges::copy_backward(range, Out(out.data() + out.size()));
76       assert(in == out);
77       assert(base(ret.in) == in.data() + in.size());
78       assert(base(ret.out) == out.data());
79     }
80   }
81 
82   { // check that an empty range works
83     {
84       std::array<int, 0> in;
85       std::array<int, 0> out;
86       auto ret =
87           std::ranges::copy_backward(In(in.data()), Sent(In(in.data() + in.size())), Out(out.data() + out.size()));
88       assert(base(ret.in) == in.data() + in.size());
89       assert(base(ret.out) == out.data());
90     }
91     {
92       std::array<int, 0> in;
93       std::array<int, 0> out;
94       auto range = std::ranges::subrange(In(in.data()), Sent(In(in.data() + in.size())));
95       auto ret = std::ranges::copy_backward(range, Out(out.data()));
96       assert(base(ret.in) == in.data() + in.size());
97       assert(base(ret.out) == out.data());
98     }
99   }
100 }
101 
102 template <class InIter, class OutIter>
103 constexpr void test_sentinels() {
104   test_iterators<InIter, OutIter, InIter>();
105   test_iterators<InIter, OutIter, sentinel_wrapper<InIter>>();
106   test_iterators<InIter, OutIter, sized_sentinel<InIter>>();
107 }
108 
109 template <class Out>
110 constexpr void test_in_iterators() {
111   test_sentinels<bidirectional_iterator<int*>, Out>();
112   test_sentinels<random_access_iterator<int*>, Out>();
113   test_sentinels<contiguous_iterator<int*>, Out>();
114   test_sentinels<int*, Out>();
115 }
116 
117 template <class Out>
118 constexpr void test_proxy_in_iterators() {
119   test_sentinels<ProxyIterator<bidirectional_iterator<int*>>, Out>();
120   test_sentinels<ProxyIterator<random_access_iterator<int*>>, Out>();
121   test_sentinels<ProxyIterator<contiguous_iterator<int*>>, Out>();
122 }
123 
124 constexpr bool test() {
125   test_in_iterators<bidirectional_iterator<int*>>();
126   test_in_iterators<random_access_iterator<int*>>();
127   test_in_iterators<contiguous_iterator<int*>>();
128   test_in_iterators<int*>();
129 
130   test_proxy_in_iterators<ProxyIterator<bidirectional_iterator<int*>>>();
131   test_proxy_in_iterators<ProxyIterator<random_access_iterator<int*>>>();
132   test_proxy_in_iterators<ProxyIterator<contiguous_iterator<int*>>>();
133 
134   { // check that ranges::dangling is returned
135     std::array<int, 4> out;
136     std::same_as<std::ranges::in_out_result<std::ranges::dangling, int*>> auto ret =
137       std::ranges::copy_backward(std::array {1, 2, 3, 4}, out.data() + out.size());
138     assert(ret.out == out.data());
139     assert((out == std::array{1, 2, 3, 4}));
140   }
141 
142   { // check that an iterator is returned with a borrowing range
143     std::array in {1, 2, 3, 4};
144     std::array<int, 4> out;
145     std::same_as<std::ranges::in_out_result<int*, int*>> auto ret =
146         std::ranges::copy_backward(std::views::all(in), out.data() + out.size());
147     assert(ret.in == in.data() + in.size());
148     assert(ret.out == out.data());
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_backward(in.begin(), in.end(), out.end());
167       assert(ret.in == in.end());
168       assert(ret.out == out.begin());
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_backward(in, out.end());
175       assert(ret.in == in.end());
176       assert(ret.out == out.begin());
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 backwards
182     struct OnlyBackwardsCopyable {
183       OnlyBackwardsCopyable* next = nullptr;
184       bool canCopy = false;
185       OnlyBackwardsCopyable() = default;
186       constexpr OnlyBackwardsCopyable& operator=(const OnlyBackwardsCopyable&) {
187         assert(canCopy);
188         if (next != nullptr)
189           next->canCopy = true;
190         return *this;
191       }
192     };
193     {
194       std::array<OnlyBackwardsCopyable, 3> in {};
195       std::array<OnlyBackwardsCopyable, 3> out {};
196       out[1].next = &out[0];
197       out[2].next = &out[1];
198       out[2].canCopy = true;
199       auto ret = std::ranges::copy_backward(in, out.end());
200       assert(ret.in == in.end());
201       assert(ret.out == out.begin());
202       assert(out[0].canCopy);
203       assert(out[1].canCopy);
204       assert(out[2].canCopy);
205     }
206     {
207       std::array<OnlyBackwardsCopyable, 3> in {};
208       std::array<OnlyBackwardsCopyable, 3> out {};
209       out[1].next = &out[0];
210       out[2].next = &out[1];
211       out[2].canCopy = true;
212       auto ret = std::ranges::copy_backward(in.begin(), in.end(), out.end());
213       assert(ret.in == in.end());
214       assert(ret.out == out.begin());
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