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