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 constexpr bool test() {
111   test_in_iterators<bidirectional_iterator<int*>>();
112   test_in_iterators<random_access_iterator<int*>>();
113   test_in_iterators<contiguous_iterator<int*>>();
114 
115   { // check that ranges::dangling is returned
116     std::array<int, 4> out;
117     std::same_as<std::ranges::in_out_result<std::ranges::dangling, int*>> auto ret =
118       std::ranges::copy_backward(std::array {1, 2, 3, 4}, out.data() + out.size());
119     assert(ret.out == out.data());
120     assert((out == std::array{1, 2, 3, 4}));
121   }
122 
123   { // check that an iterator is returned with a borrowing range
124     std::array in {1, 2, 3, 4};
125     std::array<int, 4> out;
126     std::same_as<std::ranges::in_out_result<int*, int*>> auto ret =
127         std::ranges::copy_backward(std::views::all(in), out.data() + out.size());
128     assert(ret.in == in.data());
129     assert(ret.out == out.data());
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_backward(in.begin(), in.end(), out.end());
148       assert(ret.in == in.begin());
149       assert(ret.out == out.begin());
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_backward(in, out.end());
156       assert(ret.in == in.begin());
157       assert(ret.out == out.begin());
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 backwards
163     struct OnlyBackwardsCopyable {
164       OnlyBackwardsCopyable* next = nullptr;
165       bool canCopy = false;
166       OnlyBackwardsCopyable() = default;
167       constexpr OnlyBackwardsCopyable& operator=(const OnlyBackwardsCopyable&) {
168         assert(canCopy);
169         if (next != nullptr)
170           next->canCopy = true;
171         return *this;
172       }
173     };
174     {
175       std::array<OnlyBackwardsCopyable, 3> in {};
176       std::array<OnlyBackwardsCopyable, 3> out {};
177       out[1].next = &out[0];
178       out[2].next = &out[1];
179       out[2].canCopy = true;
180       auto ret = std::ranges::copy_backward(in, out.end());
181       assert(ret.in == in.begin());
182       assert(ret.out == out.begin());
183       assert(out[0].canCopy);
184       assert(out[1].canCopy);
185       assert(out[2].canCopy);
186     }
187     {
188       std::array<OnlyBackwardsCopyable, 3> in {};
189       std::array<OnlyBackwardsCopyable, 3> out {};
190       out[1].next = &out[0];
191       out[2].next = &out[1];
192       out[2].canCopy = true;
193       auto ret = std::ranges::copy_backward(in.begin(), in.end(), out.end());
194       assert(ret.in == in.begin());
195       assert(ret.out == out.begin());
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