Skip to content

[libc++][test] Refactor tests for rotate and rotate_copy #126458

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "test_macros.h"
#include "test_execution_policies.h"
#include "test_iterators.h"
#include "type_algorithms.h"

template <class Iter>
struct Test {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@

#include "almost_satisfies_types.h"
#include "test_iterators.h"
#include "type_algorithms.h"

template <class Iter, class Out = int*, class Sent = sentinel_wrapper<Iter>>
concept HasRotateCopyIt = requires(Iter first, Sent last, Out out) { std::ranges::rotate_copy(first, first, last, out); };
concept HasRotateCopyIt =
requires(Iter first, Sent last, Out out) { std::ranges::rotate_copy(first, first, last, out); };

template <class Range, class Out = int*>
concept HasRotateCopyR = requires(Range range, Out out) { std::ranges::rotate_copy(range, nullptr, out); };
Expand All @@ -54,11 +56,8 @@ template <class Iter, class OutIter, class Sent, int N>
constexpr void test(std::array<int, N> value, std::size_t middle, std::array<int, N> expected) {
{
std::array<int, N> out;
std::same_as<std::ranges::in_out_result<Iter, OutIter>> decltype(auto) ret =
std::ranges::rotate_copy(Iter(value.data()),
Iter(value.data() + middle),
Sent(Iter(value.data() + value.size())),
OutIter(out.data()));
std::same_as<std::ranges::in_out_result<Iter, OutIter>> decltype(auto) ret = std::ranges::rotate_copy(
Iter(value.data()), Iter(value.data() + middle), Sent(Iter(value.data() + value.size())), OutIter(out.data()));
assert(base(ret.in) == value.data() + value.size());
assert(base(ret.out) == out.data() + out.size());
assert(out == expected);
Expand Down Expand Up @@ -101,41 +100,33 @@ constexpr void test_iterators() {
test<Iter, OutIter, Sent, 7>({1, 2, 3, 4, 5, 6, 7}, 7, {1, 2, 3, 4, 5, 6, 7});
}

template <class Iter, class Sent = Iter>
constexpr void test_out_iterators() {
test_iterators<Iter, cpp20_output_iterator<int*>, Sent>();
test_iterators<Iter, forward_iterator<int*>, Sent>();
test_iterators<Iter, bidirectional_iterator<int*>, Sent>();
test_iterators<Iter, random_access_iterator<int*>, Sent>();
test_iterators<Iter, contiguous_iterator<int*>, Sent>();
test_iterators<Iter, int*, Sent>();
}

constexpr bool test() {
test_out_iterators<forward_iterator<int*>>();
test_out_iterators<bidirectional_iterator<int*>>();
test_out_iterators<random_access_iterator<int*>>();
test_out_iterators<contiguous_iterator<int*>>();
test_out_iterators<int*>();
test_out_iterators<const int*>();
types::for_each(types::forward_iterator_list<const int*>(), []<class Iter>() {
types::for_each(
types::concatenate_t<types::forward_iterator_list<int*>, types::type_list<cpp20_output_iterator<int*> > >(),
[]<class OutIter>() { test_iterators<Iter, OutIter, Iter>(); });
});

{
struct AssignmentCounter {
int* counter;

constexpr AssignmentCounter(int* counter_) : counter(counter_) {}
constexpr AssignmentCounter& operator=(const AssignmentCounter&) { ++*counter; return *this; }
constexpr AssignmentCounter& operator=(const AssignmentCounter&) {
++*counter;
return *this;
}
};

{
int c = 0;
int c = 0;
AssignmentCounter a[] = {&c, &c, &c, &c};
AssignmentCounter b[] = {&c, &c, &c, &c};
std::ranges::rotate_copy(a, a + 2, a + 4, b);
assert(c == 4);
}
{
int c = 0;
int c = 0;
AssignmentCounter a[] = {&c, &c, &c, &c};
AssignmentCounter b[] = {&c, &c, &c, &c};
std::ranges::rotate_copy(a, a + 2, b);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@

#include "almost_satisfies_types.h"
#include "test_iterators.h"
#include "type_algorithms.h"

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

template <class Iter = int*, class Sent = int*>
concept HasRotateIter =
requires(Iter&& iter, Sent&& sent) {
std::ranges::rotate(std::forward<Iter>(iter), std::forward<Iter>(iter), std::forward<Sent>(sent));
};
concept HasRotateIter = requires(Iter&& iter, Sent&& sent) {
std::ranges::rotate(std::forward<Iter>(iter), std::forward<Iter>(iter), std::forward<Sent>(sent));
};

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

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

template <class Range>
concept HasRotateRange =
requires(Range&& range, std::ranges::iterator_t<Range> iter) {
std::ranges::rotate(std::forward<Range>(range), iter);
};
concept HasRotateRange = requires(Range&& range, std::ranges::iterator_t<Range> iter) {
std::ranges::rotate(std::forward<Range>(range), iter);
};

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

{ // (iterator, sentinel) overload.
auto in = input;
auto in = input;
auto begin = Iter(in.data());
auto mid = Iter(in.data() + mid_index);
auto end = Sent(Iter(in.data() + in.size()));
auto mid = Iter(in.data() + mid_index);
auto end = Sent(Iter(in.data() + in.size()));

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

{ // (range) overload.
auto in = input;
auto in = input;
auto begin = Iter(in.data());
auto mid = Iter(in.data() + mid_index);
auto end = Sent(Iter(in.data() + in.size()));
auto mid = Iter(in.data() + mid_index);
auto end = Sent(Iter(in.data() + in.size()));
auto range = std::ranges::subrange(std::move(begin), std::move(end));

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

template <class Iter>
constexpr void test_iter() {
test_iter_sent<Iter, Iter>();
test_iter_sent<Iter, sentinel_wrapper<Iter>>();
}

constexpr void test_iterators() {
test_iter<forward_iterator<int*>>();
test_iter<bidirectional_iterator<int*>>();
test_iter<random_access_iterator<int*>>();
test_iter<contiguous_iterator<int*>>();
test_iter<int*>();
}

constexpr bool test() {
test_iterators();
types::for_each(types::forward_iterator_list<int*>(), []<class Iter>() {
test_iter_sent<Iter, Iter>();
test_iter_sent<Iter, sentinel_wrapper<Iter>>();
});

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

{
auto in = input;
int swaps = 0;
auto in = input;
int swaps = 0;
auto begin = adl::Iterator::TrackSwaps(in.data(), swaps);
auto end = adl::Iterator::TrackSwaps(in.data() + in.size(), swaps);
auto end = adl::Iterator::TrackSwaps(in.data() + in.size(), swaps);

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

{
auto in = input;
int swaps = 0;
auto in = input;
int swaps = 0;
auto begin = adl::Iterator::TrackSwaps(in.data(), swaps);
auto end = adl::Iterator::TrackSwaps(in.data() + in.size(), swaps);
auto end = adl::Iterator::TrackSwaps(in.data() + in.size(), swaps);
auto range = std::ranges::subrange(begin, end);

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