1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: c++03, c++11, c++14, c++17
11 // template<forward_iterator I, sentinel_for<I> S, weakly_incrementable O>
12 // requires indirectly_copyable<I, O>
13 // constexpr ranges::rotate_copy_result<I, O>
14 // ranges::rotate_copy(I first, I middle, S last, O result);
15 // template<forward_range R, weakly_incrementable O>
16 // requires indirectly_copyable<iterator_t<R>, O>
17 // constexpr ranges::rotate_copy_result<borrowed_iterator_t<R>, O>
18 // ranges::rotate_copy(R&& r, iterator_t<R> middle, O result);
27 #include "almost_satisfies_types.h"
28 #include "test_iterators.h"
30 template <class Iter
, class Out
= int*, class Sent
= sentinel_wrapper
<Iter
>>
31 concept HasRotateCopyIt
= requires(Iter first
, Sent last
, Out out
) { std::ranges::rotate_copy(first
, first
, last
, out
); };
33 template <class Range
, class Out
= int*>
34 concept HasRotateCopyR
= requires(Range range
, Out out
) { std::ranges::rotate_copy(range
, nullptr, out
); };
36 static_assert(HasRotateCopyIt
<int*>);
37 static_assert(!HasRotateCopyIt
<BidirectionalIteratorNotDerivedFrom
>);
38 static_assert(!HasRotateCopyIt
<BidirectionalIteratorNotDecrementable
>);
39 static_assert(!HasRotateCopyIt
<int*, SentinelForNotSemiregular
>);
40 static_assert(!HasRotateCopyIt
<int*, SentinelForNotWeaklyEqualityComparableWith
>);
41 static_assert(!HasRotateCopyIt
<int*, OutputIteratorNotIndirectlyWritable
>);
42 static_assert(!HasRotateCopyIt
<int*, OutputIteratorNotInputOrOutputIterator
>);
44 static_assert(HasRotateCopyR
<UncheckedRange
<int*>>);
45 static_assert(!HasRotateCopyR
<BidirectionalRangeNotDerivedFrom
>);
46 static_assert(!HasRotateCopyR
<BidirectionalRangeNotDecrementable
>);
47 static_assert(!HasRotateCopyR
<UncheckedRange
<int*, SentinelForNotSemiregular
>>);
48 static_assert(!HasRotateCopyR
<UncheckedRange
<int*>, OutputIteratorNotIndirectlyWritable
>);
49 static_assert(!HasRotateCopyR
<UncheckedRange
<int*>, OutputIteratorNotInputOrOutputIterator
>);
51 static_assert(std::is_same_v
<std::ranges::rotate_copy_result
<int, int>, std::ranges::in_out_result
<int, int>>);
53 template <class Iter
, class OutIter
, class Sent
, int N
>
54 constexpr void test(std::array
<int, N
> value
, std::size_t middle
, std::array
<int, N
> expected
) {
56 std::array
<int, N
> out
;
57 std::same_as
<std::ranges::in_out_result
<Iter
, OutIter
>> decltype(auto) ret
=
58 std::ranges::rotate_copy(Iter(value
.data()),
59 Iter(value
.data() + middle
),
60 Sent(Iter(value
.data() + value
.size())),
62 assert(base(ret
.in
) == value
.data() + value
.size());
63 assert(base(ret
.out
) == out
.data() + out
.size());
64 assert(out
== expected
);
67 std::array
<int, N
> out
;
68 auto range
= std::ranges::subrange(Iter(value
.data()), Sent(Iter(value
.data() + value
.size())));
69 std::same_as
<std::ranges::in_out_result
<Iter
, OutIter
>> decltype(auto) ret
=
70 std::ranges::rotate_copy(range
, Iter(value
.data() + middle
), OutIter(out
.data()));
71 assert(base(ret
.in
) == value
.data() + value
.size());
72 assert(base(ret
.out
) == out
.data() + out
.size());
73 assert(out
== expected
);
77 template <class Iter
, class OutIter
, class Sent
>
78 constexpr void test_iterators() {
80 test
<Iter
, OutIter
, Sent
, 4>({1, 2, 3, 4}, 2, {3, 4, 1, 2});
82 // check that an empty range works
83 test
<Iter
, OutIter
, Sent
, 0>({}, 0, {});
85 // check that a single element range works
86 test
<Iter
, OutIter
, Sent
, 1>({1}, 0, {1});
88 // check that a two element range works
89 test
<Iter
, OutIter
, Sent
, 2>({1, 2}, 1, {2, 1});
91 // rotate on the first element
92 test
<Iter
, OutIter
, Sent
, 7>({1, 2, 3, 4, 5, 6, 7}, 0, {1, 2, 3, 4, 5, 6, 7});
94 // rotate on the second element
95 test
<Iter
, OutIter
, Sent
, 7>({1, 2, 3, 4, 5, 6, 7}, 1, {2, 3, 4, 5, 6, 7, 1});
97 // rotate on the last element
98 test
<Iter
, OutIter
, Sent
, 7>({1, 2, 3, 4, 5, 6, 7}, 6, {7, 1, 2, 3, 4, 5, 6});
100 // rotate on the one-past-the-end pointer
101 test
<Iter
, OutIter
, Sent
, 7>({1, 2, 3, 4, 5, 6, 7}, 7, {1, 2, 3, 4, 5, 6, 7});
104 template <class Iter
, class Sent
= Iter
>
105 constexpr void test_out_iterators() {
106 test_iterators
<Iter
, cpp20_output_iterator
<int*>, Sent
>();
107 test_iterators
<Iter
, forward_iterator
<int*>, Sent
>();
108 test_iterators
<Iter
, bidirectional_iterator
<int*>, Sent
>();
109 test_iterators
<Iter
, random_access_iterator
<int*>, Sent
>();
110 test_iterators
<Iter
, contiguous_iterator
<int*>, Sent
>();
111 test_iterators
<Iter
, int*, Sent
>();
114 constexpr bool test() {
115 test_out_iterators
<bidirectional_iterator
<int*>>();
116 test_out_iterators
<random_access_iterator
<int*>>();
117 test_out_iterators
<contiguous_iterator
<int*>>();
118 test_out_iterators
<int*>();
119 test_out_iterators
<const int*>();
122 struct AssignmentCounter
{
125 constexpr AssignmentCounter(int* counter_
) : counter(counter_
) {}
126 constexpr AssignmentCounter
& operator=(const AssignmentCounter
&) { ++*counter
; return *this; }
131 AssignmentCounter a
[] = {&c
, &c
, &c
, &c
};
132 AssignmentCounter b
[] = {&c
, &c
, &c
, &c
};
133 std::ranges::rotate_copy(a
, a
+ 2, a
+ 4, b
);
138 AssignmentCounter a
[] = {&c
, &c
, &c
, &c
};
139 AssignmentCounter b
[] = {&c
, &c
, &c
, &c
};
140 std::ranges::rotate_copy(a
, a
+ 2, b
);
148 int main(int, char**) {
150 static_assert(test());