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// UNSUPPORTED: c++03, c++11, c++14, c++17
10
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);
19
20// <algorithm>
21
22#include <algorithm>
23#include <array>
24#include <cassert>
25#include <ranges>
26
27#include "almost_satisfies_types.h"
28#include "test_iterators.h"
29
30template <class Iter, class Out = int*, class Sent = sentinel_wrapper<Iter>>
31concept HasRotateCopyIt = requires(Iter first, Sent last, Out out) { std::ranges::rotate_copy(first, first, last, out); };
32
33template <class Range, class Out = int*>
34concept HasRotateCopyR = requires(Range range, Out out) { std::ranges::rotate_copy(range, nullptr, out); };
35
36static_assert(HasRotateCopyIt<int*>);
37static_assert(!HasRotateCopyIt<ForwardIteratorNotDerivedFrom>);
38static_assert(!HasRotateCopyIt<ForwardIteratorNotIncrementable>);
39static_assert(!HasRotateCopyIt<int*, SentinelForNotSemiregular>);
40static_assert(!HasRotateCopyIt<int*, SentinelForNotWeaklyEqualityComparableWith>);
41static_assert(!HasRotateCopyIt<int*, OutputIteratorNotIndirectlyWritable>);
42static_assert(!HasRotateCopyIt<int*, OutputIteratorNotInputOrOutputIterator>);
43
44static_assert(HasRotateCopyR<UncheckedRange<int*>>);
45static_assert(!HasRotateCopyR<ForwardRangeNotDerivedFrom>);
46static_assert(!HasRotateCopyR<ForwardRangeNotIncrementable>);
47static_assert(!HasRotateCopyR<UncheckedRange<int*, SentinelForNotSemiregular>>);
48static_assert(!HasRotateCopyR<UncheckedRange<int*>, OutputIteratorNotIndirectlyWritable>);
49static_assert(!HasRotateCopyR<UncheckedRange<int*>, OutputIteratorNotInputOrOutputIterator>);
50
51static_assert(std::is_same_v<std::ranges::rotate_copy_result<int, int>, std::ranges::in_out_result<int, int>>);
52
53template <class Iter, class OutIter, class Sent, int N>
54constexpr void test(std::array<int, N> value, std::size_t middle, std::array<int, N> expected) {
55 {
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())),
61 OutIter(out.data()));
62 assert(base(ret.in) == value.data() + value.size());
63 assert(base(ret.out) == out.data() + out.size());
64 assert(out == expected);
65 }
66 {
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);
74 }
75}
76
77template <class Iter, class OutIter, class Sent>
78constexpr void test_iterators() {
79 // simple test
80 test<Iter, OutIter, Sent, 4>({1, 2, 3, 4}, 2, {3, 4, 1, 2});
81
82 // check that an empty range works
83 test<Iter, OutIter, Sent, 0>({}, 0, {});
84
85 // check that a single element range works
86 test<Iter, OutIter, Sent, 1>({1}, 0, {1});
87
88 // check that a two element range works
89 test<Iter, OutIter, Sent, 2>({1, 2}, 1, {2, 1});
90
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});
93
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});
96
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});
99
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});
102}
103
104template <class Iter, class Sent = Iter>
105constexpr 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>();
112}
113
114constexpr bool test() {
115 test_out_iterators<forward_iterator<int*>>();
116 test_out_iterators<bidirectional_iterator<int*>>();
117 test_out_iterators<random_access_iterator<int*>>();
118 test_out_iterators<contiguous_iterator<int*>>();
119 test_out_iterators<int*>();
120 test_out_iterators<const int*>();
121
122 {
123 struct AssignmentCounter {
124 int* counter;
125
126 constexpr AssignmentCounter(int* counter_) : counter(counter_) {}
127 constexpr AssignmentCounter& operator=(const AssignmentCounter&) { ++*counter; return *this; }
128 };
129
130 {
131 int c = 0;
132 AssignmentCounter a[] = {&c, &c, &c, &c};
133 AssignmentCounter b[] = {&c, &c, &c, &c};
134 std::ranges::rotate_copy(a, a + 2, a + 4, b);
135 assert(c == 4);
136 }
137 {
138 int c = 0;
139 AssignmentCounter a[] = {&c, &c, &c, &c};
140 AssignmentCounter b[] = {&c, &c, &c, &c};
141 std::ranges::rotate_copy(a, a + 2, b);
142 assert(c == 4);
143 }
144 }
145
146 return true;
147}
148
149int main(int, char**) {
150 test();
151 static_assert(test());
152
153 return 0;
154}
155

source code of libcxx/test/std/algorithms/alg.modifying.operations/alg.rotate/ranges.rotate_copy.pass.cpp