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