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 // UNSUPPORTED: libcpp-has-no-incomplete-ranges
11
12 // template<forward_iterator I, sentinel_for<I> S, weakly_incrementable O>
13 // requires indirectly_copyable<I, O>
14 // constexpr ranges::rotate_copy_result<I, O>
15 // ranges::rotate_copy(I first, I middle, S last, O result);
16 // template<forward_range R, weakly_incrementable O>
17 // requires indirectly_copyable<iterator_t<R>, O>
18 // constexpr ranges::rotate_copy_result<borrowed_iterator_t<R>, O>
19 // ranges::rotate_copy(R&& r, iterator_t<R> middle, O result);
20
21 // <algorithm>
22
23 #include <algorithm>
24 #include <array>
25 #include <cassert>
26 #include <ranges>
27
28 #include "almost_satisfies_types.h"
29 #include "test_iterators.h"
30
31 template <class Iter, class Out = int*, class Sent = sentinel_wrapper<Iter>>
32 concept HasRotateCopyIt = requires(Iter first, Sent last, Out out) { std::ranges::rotate_copy(first, first, last, out); };
33
34 template <class Range, class Out = int*>
35 concept HasRotateCopyR = requires(Range range, Out out) { std::ranges::rotate_copy(range, nullptr, out); };
36
37 static_assert(HasRotateCopyIt<int*>);
38 static_assert(!HasRotateCopyIt<BidirectionalIteratorNotDerivedFrom>);
39 static_assert(!HasRotateCopyIt<BidirectionalIteratorNotDecrementable>);
40 static_assert(!HasRotateCopyIt<int*, SentinelForNotSemiregular>);
41 static_assert(!HasRotateCopyIt<int*, SentinelForNotWeaklyEqualityComparableWith>);
42 static_assert(!HasRotateCopyIt<int*, OutputIteratorNotIndirectlyWritable>);
43 static_assert(!HasRotateCopyIt<int*, OutputIteratorNotInputOrOutputIterator>);
44
45 static_assert(HasRotateCopyR<UncheckedRange<int*>>);
46 static_assert(!HasRotateCopyR<BidirectionalRangeNotDerivedFrom>);
47 static_assert(!HasRotateCopyR<BidirectionalRangeNotDecrementable>);
48 static_assert(!HasRotateCopyR<UncheckedRange<int*, SentinelForNotSemiregular>>);
49 static_assert(!HasRotateCopyR<UncheckedRange<int*>, OutputIteratorNotIndirectlyWritable>);
50 static_assert(!HasRotateCopyR<UncheckedRange<int*>, OutputIteratorNotInputOrOutputIterator>);
51
52 static_assert(std::is_same_v<std::ranges::rotate_copy_result<int, int>, std::ranges::in_out_result<int, int>>);
53
54 template <class Iter, class OutIter, class Sent, int N>
test(std::array<int,N> value,size_t middle,std::array<int,N> expected)55 constexpr void test(std::array<int, N> value, size_t middle, std::array<int, N> expected) {
56 {
57 std::array<int, N> out;
58 std::same_as<std::ranges::in_out_result<Iter, OutIter>> decltype(auto) ret =
59 std::ranges::rotate_copy(Iter(value.data()),
60 Iter(value.data() + middle),
61 Sent(Iter(value.data() + value.size())),
62 OutIter(out.data()));
63 assert(base(ret.in) == value.data() + value.size());
64 assert(base(ret.out) == out.data() + out.size());
65 assert(out == expected);
66 }
67 {
68 std::array<int, N> out;
69 auto range = std::ranges::subrange(Iter(value.data()), Sent(Iter(value.data() + value.size())));
70 std::same_as<std::ranges::in_out_result<Iter, OutIter>> decltype(auto) ret =
71 std::ranges::rotate_copy(range, Iter(value.data() + middle), OutIter(out.data()));
72 assert(base(ret.in) == value.data() + value.size());
73 assert(base(ret.out) == out.data() + out.size());
74 assert(out == expected);
75 }
76 }
77
78 template <class Iter, class OutIter, class Sent>
test_iterators()79 constexpr void test_iterators() {
80 // simple test
81 test<Iter, OutIter, Sent, 4>({1, 2, 3, 4}, 2, {3, 4, 1, 2});
82
83 // check that an empty range works
84 test<Iter, OutIter, Sent, 0>({}, 0, {});
85
86 // check that a single element range works
87 test<Iter, OutIter, Sent, 1>({1}, 0, {1});
88
89 // check that a two element range works
90 test<Iter, OutIter, Sent, 2>({1, 2}, 1, {2, 1});
91
92 // rotate on the first element
93 test<Iter, OutIter, Sent, 7>({1, 2, 3, 4, 5, 6, 7}, 0, {1, 2, 3, 4, 5, 6, 7});
94
95 // rotate on the second element
96 test<Iter, OutIter, Sent, 7>({1, 2, 3, 4, 5, 6, 7}, 1, {2, 3, 4, 5, 6, 7, 1});
97
98 // rotate on the last element
99 test<Iter, OutIter, Sent, 7>({1, 2, 3, 4, 5, 6, 7}, 6, {7, 1, 2, 3, 4, 5, 6});
100
101 // rotate on the one-past-the-end pointer
102 test<Iter, OutIter, Sent, 7>({1, 2, 3, 4, 5, 6, 7}, 7, {1, 2, 3, 4, 5, 6, 7});
103 }
104
105 template <class Iter, class Sent = Iter>
test_out_iterators()106 constexpr void test_out_iterators() {
107 test_iterators<Iter, cpp20_output_iterator<int*>, Sent>();
108 test_iterators<Iter, forward_iterator<int*>, Sent>();
109 test_iterators<Iter, bidirectional_iterator<int*>, Sent>();
110 test_iterators<Iter, random_access_iterator<int*>, Sent>();
111 test_iterators<Iter, contiguous_iterator<int*>, Sent>();
112 test_iterators<Iter, int*, Sent>();
113 }
114
test()115 constexpr bool test() {
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
main(int,char **)149 int main(int, char**) {
150 test();
151 static_assert(test());
152
153 return 0;
154 }
155