Skip to content

Commit 93307d5

Browse files
committed
Refactor tests for rotate and rotate_copy
1 parent 998b28f commit 93307d5

File tree

6 files changed

+319
-342
lines changed

6 files changed

+319
-342
lines changed

libcxx/test/std/algorithms/alg.modifying.operations/alg.rotate/pstl.rotate_copy.pass.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "test_macros.h"
2828
#include "test_execution_policies.h"
2929
#include "test_iterators.h"
30+
#include "type_algorithms.h"
3031

3132
template <class Iter>
3233
struct Test {

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

+17-26
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,11 @@
2626

2727
#include "almost_satisfies_types.h"
2828
#include "test_iterators.h"
29+
#include "type_algorithms.h"
2930

3031
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); };
32+
concept HasRotateCopyIt =
33+
requires(Iter first, Sent last, Out out) { std::ranges::rotate_copy(first, first, last, out); };
3234

3335
template <class Range, class Out = int*>
3436
concept HasRotateCopyR = requires(Range range, Out out) { std::ranges::rotate_copy(range, nullptr, out); };
@@ -54,11 +56,8 @@ template <class Iter, class OutIter, class Sent, int N>
5456
constexpr void test(std::array<int, N> value, std::size_t middle, std::array<int, N> expected) {
5557
{
5658
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()));
59+
std::same_as<std::ranges::in_out_result<Iter, OutIter>> decltype(auto) ret = std::ranges::rotate_copy(
60+
Iter(value.data()), Iter(value.data() + middle), Sent(Iter(value.data() + value.size())), OutIter(out.data()));
6261
assert(base(ret.in) == value.data() + value.size());
6362
assert(base(ret.out) == out.data() + out.size());
6463
assert(out == expected);
@@ -74,7 +73,7 @@ constexpr void test(std::array<int, N> value, std::size_t middle, std::array<int
7473
}
7574
}
7675

77-
template <class Iter, class OutIter, class Sent>
76+
template <class Iter, class OutIter, class Sent = Iter>
7877
constexpr void test_iterators() {
7978
// simple test
8079
test<Iter, OutIter, Sent, 4>({1, 2, 3, 4}, 2, {3, 4, 1, 2});
@@ -101,41 +100,33 @@ constexpr void test_iterators() {
101100
test<Iter, OutIter, Sent, 7>({1, 2, 3, 4, 5, 6, 7}, 7, {1, 2, 3, 4, 5, 6, 7});
102101
}
103102

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>();
112-
}
113-
114103
constexpr 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*>();
104+
types::for_each(types::forward_iterator_list<int*>(), []<class Iter>() {
105+
types::for_each(types::cpp20_output_iterator_list<int*>(), []<class OutIter>() {
106+
test_iterators<Iter, OutIter>();
107+
});
108+
});
121109

122110
{
123111
struct AssignmentCounter {
124112
int* counter;
125113

126114
constexpr AssignmentCounter(int* counter_) : counter(counter_) {}
127-
constexpr AssignmentCounter& operator=(const AssignmentCounter&) { ++*counter; return *this; }
115+
constexpr AssignmentCounter& operator=(const AssignmentCounter&) {
116+
++*counter;
117+
return *this;
118+
}
128119
};
129120

130121
{
131-
int c = 0;
122+
int c = 0;
132123
AssignmentCounter a[] = {&c, &c, &c, &c};
133124
AssignmentCounter b[] = {&c, &c, &c, &c};
134125
std::ranges::rotate_copy(a, a + 2, a + 4, b);
135126
assert(c == 4);
136127
}
137128
{
138-
int c = 0;
129+
int c = 0;
139130
AssignmentCounter a[] = {&c, &c, &c, &c};
140131
AssignmentCounter b[] = {&c, &c, &c, &c};
141132
std::ranges::rotate_copy(a, a + 2, b);

libcxx/test/std/algorithms/alg.modifying.operations/alg.rotate/ranges_rotate.pass.cpp

+24-36
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@
2424

2525
#include "almost_satisfies_types.h"
2626
#include "test_iterators.h"
27+
#include "type_algorithms.h"
2728

2829
// Test constraints of the (iterator, sentinel) overload.
2930
// ======================================================
3031

3132
template <class Iter = int*, class Sent = int*>
32-
concept HasRotateIter =
33-
requires(Iter&& iter, Sent&& sent) {
34-
std::ranges::rotate(std::forward<Iter>(iter), std::forward<Iter>(iter), std::forward<Sent>(sent));
35-
};
33+
concept HasRotateIter = requires(Iter&& iter, Sent&& sent) {
34+
std::ranges::rotate(std::forward<Iter>(iter), std::forward<Iter>(iter), std::forward<Sent>(sent));
35+
};
3636

3737
static_assert(HasRotateIter<int*, int*>);
3838

@@ -48,10 +48,9 @@ static_assert(!HasRotateIter<int*, SentinelForNotWeaklyEqualityComparableWith>);
4848
// =========================================
4949

5050
template <class Range>
51-
concept HasRotateRange =
52-
requires(Range&& range, std::ranges::iterator_t<Range> iter) {
53-
std::ranges::rotate(std::forward<Range>(range), iter);
54-
};
51+
concept HasRotateRange = requires(Range&& range, std::ranges::iterator_t<Range> iter) {
52+
std::ranges::rotate(std::forward<Range>(range), iter);
53+
};
5554

5655
template <class T>
5756
using R = UncheckedRange<T>;
@@ -73,10 +72,10 @@ constexpr void test_one(const std::array<int, N> input, std::size_t mid_index, s
7372
assert(mid_index <= N);
7473

7574
{ // (iterator, sentinel) overload.
76-
auto in = input;
75+
auto in = input;
7776
auto begin = Iter(in.data());
78-
auto mid = Iter(in.data() + mid_index);
79-
auto end = Sent(Iter(in.data() + in.size()));
77+
auto mid = Iter(in.data() + mid_index);
78+
auto end = Sent(Iter(in.data() + in.size()));
8079

8180
std::same_as<std::ranges::subrange<Iter>> decltype(auto) result = std::ranges::rotate(begin, mid, end);
8281
assert(base(result.begin()) == in.data() + in.size() - mid_index);
@@ -85,10 +84,10 @@ constexpr void test_one(const std::array<int, N> input, std::size_t mid_index, s
8584
}
8685

8786
{ // (range) overload.
88-
auto in = input;
87+
auto in = input;
8988
auto begin = Iter(in.data());
90-
auto mid = Iter(in.data() + mid_index);
91-
auto end = Sent(Iter(in.data() + in.size()));
89+
auto mid = Iter(in.data() + mid_index);
90+
auto end = Sent(Iter(in.data() + in.size()));
9291
auto range = std::ranges::subrange(std::move(begin), std::move(end));
9392

9493
std::same_as<std::ranges::subrange<Iter>> decltype(auto) result = std::ranges::rotate(range, mid);
@@ -132,32 +131,21 @@ constexpr void test_iter_sent() {
132131
test_one<Iter, Sent, 7>({1, 2, 3, 4, 5, 6, 7}, 7, {1, 2, 3, 4, 5, 6, 7});
133132
}
134133

135-
template <class Iter>
136-
constexpr void test_iter() {
137-
test_iter_sent<Iter, Iter>();
138-
test_iter_sent<Iter, sentinel_wrapper<Iter>>();
139-
}
140-
141-
constexpr void test_iterators() {
142-
test_iter<forward_iterator<int*>>();
143-
test_iter<bidirectional_iterator<int*>>();
144-
test_iter<random_access_iterator<int*>>();
145-
test_iter<contiguous_iterator<int*>>();
146-
test_iter<int*>();
147-
}
148-
149134
constexpr bool test() {
150-
test_iterators();
135+
types::for_each(types::forward_iterator_list<int*>(), []<class Iter>() {
136+
test_iter_sent<Iter, Iter>();
137+
test_iter_sent<Iter, sentinel_wrapper<Iter>>();
138+
});
151139

152140
{ // Complexity: at most `last - first` swaps.
153141
const std::array input = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
154-
auto expected = static_cast<int>(input.size());
142+
auto expected = static_cast<int>(input.size());
155143

156144
{
157-
auto in = input;
158-
int swaps = 0;
145+
auto in = input;
146+
int swaps = 0;
159147
auto begin = adl::Iterator::TrackSwaps(in.data(), swaps);
160-
auto end = adl::Iterator::TrackSwaps(in.data() + in.size(), swaps);
148+
auto end = adl::Iterator::TrackSwaps(in.data() + in.size(), swaps);
161149

162150
for (std::size_t mid = 0; mid != input.size(); ++mid) {
163151
std::ranges::rotate(begin, begin + mid, end);
@@ -166,10 +154,10 @@ constexpr bool test() {
166154
}
167155

168156
{
169-
auto in = input;
170-
int swaps = 0;
157+
auto in = input;
158+
int swaps = 0;
171159
auto begin = adl::Iterator::TrackSwaps(in.data(), swaps);
172-
auto end = adl::Iterator::TrackSwaps(in.data() + in.size(), swaps);
160+
auto end = adl::Iterator::TrackSwaps(in.data() + in.size(), swaps);
173161
auto range = std::ranges::subrange(begin, end);
174162

175163
for (std::size_t mid = 0; mid != input.size(); ++mid) {

0 commit comments

Comments
 (0)