xref: /llvm-project/libcxx/test/std/algorithms/alg.modifying.operations/alg.rotate/ranges_rotate.pass.cpp (revision fb855eb941b6d740cc6560297d0b4d3201dcaf9f)
136c746caSKonstantin Varlamov //===----------------------------------------------------------------------===//
236c746caSKonstantin Varlamov //
336c746caSKonstantin Varlamov // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
436c746caSKonstantin Varlamov // See https://llvm.org/LICENSE.txt for license information.
536c746caSKonstantin Varlamov // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
636c746caSKonstantin Varlamov //
736c746caSKonstantin Varlamov //===----------------------------------------------------------------------===//
836c746caSKonstantin Varlamov 
936c746caSKonstantin Varlamov // UNSUPPORTED: c++03, c++11, c++14, c++17
1036c746caSKonstantin Varlamov 
1136c746caSKonstantin Varlamov // <algorithm>
1236c746caSKonstantin Varlamov 
1336c746caSKonstantin Varlamov // template<permutable I, sentinel_for<I> S>
1436c746caSKonstantin Varlamov //   constexpr subrange<I> rotate(I first, I middle, S last);                                        // since C++20
1536c746caSKonstantin Varlamov //
1636c746caSKonstantin Varlamov // template<forward_range R>
1736c746caSKonstantin Varlamov //   requires permutable<iterator_t<R>>
1836c746caSKonstantin Varlamov //   constexpr borrowed_subrange_t<R> rotate(R&& r, iterator_t<R> middle);                           // Since C++20
1936c746caSKonstantin Varlamov 
2036c746caSKonstantin Varlamov #include <algorithm>
2136c746caSKonstantin Varlamov #include <array>
2236c746caSKonstantin Varlamov #include <cassert>
2336c746caSKonstantin Varlamov #include <ranges>
2436c746caSKonstantin Varlamov 
2536c746caSKonstantin Varlamov #include "almost_satisfies_types.h"
2636c746caSKonstantin Varlamov #include "test_iterators.h"
2736c746caSKonstantin Varlamov 
2836c746caSKonstantin Varlamov // Test constraints of the (iterator, sentinel) overload.
2936c746caSKonstantin Varlamov // ======================================================
3036c746caSKonstantin Varlamov 
3136c746caSKonstantin Varlamov template <class Iter = int*, class Sent = int*>
3236c746caSKonstantin Varlamov concept HasRotateIter =
3336c746caSKonstantin Varlamov     requires(Iter&& iter, Sent&& sent) {
3436c746caSKonstantin Varlamov       std::ranges::rotate(std::forward<Iter>(iter), std::forward<Iter>(iter), std::forward<Sent>(sent));
3536c746caSKonstantin Varlamov     };
3636c746caSKonstantin Varlamov 
3736c746caSKonstantin Varlamov static_assert(HasRotateIter<int*, int*>);
3836c746caSKonstantin Varlamov 
3936c746caSKonstantin Varlamov // !permutable<I>
4036c746caSKonstantin Varlamov static_assert(!HasRotateIter<PermutableNotForwardIterator>);
4136c746caSKonstantin Varlamov static_assert(!HasRotateIter<PermutableNotSwappable>);
4236c746caSKonstantin Varlamov 
4336c746caSKonstantin Varlamov // !sentinel_for<S, I>
4436c746caSKonstantin Varlamov static_assert(!HasRotateIter<int*, SentinelForNotSemiregular>);
4536c746caSKonstantin Varlamov static_assert(!HasRotateIter<int*, SentinelForNotWeaklyEqualityComparableWith>);
4636c746caSKonstantin Varlamov 
4736c746caSKonstantin Varlamov // Test constraints of the (range) overload.
4836c746caSKonstantin Varlamov // =========================================
4936c746caSKonstantin Varlamov 
5036c746caSKonstantin Varlamov template <class Range>
5136c746caSKonstantin Varlamov concept HasRotateRange =
5236c746caSKonstantin Varlamov     requires(Range&& range, std::ranges::iterator_t<Range> iter) {
5336c746caSKonstantin Varlamov       std::ranges::rotate(std::forward<Range>(range), iter);
5436c746caSKonstantin Varlamov     };
5536c746caSKonstantin Varlamov 
5636c746caSKonstantin Varlamov template <class T>
5736c746caSKonstantin Varlamov using R = UncheckedRange<T>;
5836c746caSKonstantin Varlamov 
5936c746caSKonstantin Varlamov static_assert(HasRotateRange<R<int*>>);
6036c746caSKonstantin Varlamov 
6136c746caSKonstantin Varlamov // !forward_range<R>
6236c746caSKonstantin Varlamov static_assert(!HasRotateRange<ForwardRangeNotDerivedFrom>);
6336c746caSKonstantin Varlamov static_assert(!HasRotateRange<ForwardRangeNotIncrementable>);
6436c746caSKonstantin Varlamov static_assert(!HasRotateRange<ForwardRangeNotSentinelSemiregular>);
6536c746caSKonstantin Varlamov static_assert(!HasRotateRange<ForwardRangeNotSentinelEqualityComparableWith>);
6636c746caSKonstantin Varlamov 
6736c746caSKonstantin Varlamov // !permutable<iterator_t<R>>
6836c746caSKonstantin Varlamov static_assert(!HasRotateRange<PermutableRangeNotForwardIterator>);
6936c746caSKonstantin Varlamov static_assert(!HasRotateRange<PermutableRangeNotSwappable>);
7036c746caSKonstantin Varlamov 
71*fb855eb9SMark de Wever template <class Iter, class Sent, std::size_t N>
test_one(const std::array<int,N> input,std::size_t mid_index,std::array<int,N> expected)72*fb855eb9SMark de Wever constexpr void test_one(const std::array<int, N> input, std::size_t mid_index, std::array<int, N> expected) {
7336c746caSKonstantin Varlamov   assert(mid_index <= N);
7436c746caSKonstantin Varlamov 
7536c746caSKonstantin Varlamov   { // (iterator, sentinel) overload.
7636c746caSKonstantin Varlamov     auto in = input;
7736c746caSKonstantin Varlamov     auto begin = Iter(in.data());
7836c746caSKonstantin Varlamov     auto mid = Iter(in.data() + mid_index);
7936c746caSKonstantin Varlamov     auto end = Sent(Iter(in.data() + in.size()));
8036c746caSKonstantin Varlamov 
8136c746caSKonstantin Varlamov     std::same_as<std::ranges::subrange<Iter>> decltype(auto) result = std::ranges::rotate(begin, mid, end);
8236c746caSKonstantin Varlamov     assert(base(result.begin()) == in.data() + in.size() - mid_index);
8336c746caSKonstantin Varlamov     assert(base(result.end()) == in.data() + in.size());
8436c746caSKonstantin Varlamov     assert(in == expected);
8536c746caSKonstantin Varlamov   }
8636c746caSKonstantin Varlamov 
8736c746caSKonstantin Varlamov   { // (range) overload.
8836c746caSKonstantin Varlamov     auto in = input;
8936c746caSKonstantin Varlamov     auto begin = Iter(in.data());
9036c746caSKonstantin Varlamov     auto mid = Iter(in.data() + mid_index);
9136c746caSKonstantin Varlamov     auto end = Sent(Iter(in.data() + in.size()));
9236c746caSKonstantin Varlamov     auto range = std::ranges::subrange(std::move(begin), std::move(end));
9336c746caSKonstantin Varlamov 
9436c746caSKonstantin Varlamov     std::same_as<std::ranges::subrange<Iter>> decltype(auto) result = std::ranges::rotate(range, mid);
9536c746caSKonstantin Varlamov     assert(base(result.begin()) == in.data() + in.size() - mid_index);
9636c746caSKonstantin Varlamov     assert(base(result.end()) == in.data() + in.size());
9736c746caSKonstantin Varlamov     assert(in == expected);
9836c746caSKonstantin Varlamov   }
9936c746caSKonstantin Varlamov }
10036c746caSKonstantin Varlamov 
10136c746caSKonstantin Varlamov template <class Iter, class Sent>
test_iter_sent()10236c746caSKonstantin Varlamov constexpr void test_iter_sent() {
10336c746caSKonstantin Varlamov   // Empty sequence.
10436c746caSKonstantin Varlamov   test_one<Iter, Sent, 0>({}, 0, {});
10536c746caSKonstantin Varlamov 
10636c746caSKonstantin Varlamov   // 1-element sequence.
10736c746caSKonstantin Varlamov   test_one<Iter, Sent, 1>({1}, 0, {1});
10836c746caSKonstantin Varlamov 
10936c746caSKonstantin Varlamov   // 2-element sequence.
11036c746caSKonstantin Varlamov   test_one<Iter, Sent, 2>({1, 2}, 1, {2, 1});
11136c746caSKonstantin Varlamov 
11236c746caSKonstantin Varlamov   // 3-element sequence.
11336c746caSKonstantin Varlamov   test_one<Iter, Sent, 3>({1, 2, 3}, 1, {2, 3, 1});
11436c746caSKonstantin Varlamov   test_one<Iter, Sent, 3>({1, 2, 3}, 2, {3, 1, 2});
11536c746caSKonstantin Varlamov 
11636c746caSKonstantin Varlamov   // Longer sequence.
11736c746caSKonstantin Varlamov   test_one<Iter, Sent, 7>({1, 2, 3, 4, 5, 6, 7}, 2, {3, 4, 5, 6, 7, 1, 2});
11836c746caSKonstantin Varlamov 
11936c746caSKonstantin Varlamov   // Rotate around the middle.
12036c746caSKonstantin Varlamov   test_one<Iter, Sent, 7>({1, 2, 3, 4, 5, 6, 7}, 3, {4, 5, 6, 7, 1, 2, 3});
12136c746caSKonstantin Varlamov 
12236c746caSKonstantin Varlamov   // Rotate around the 1st element (no-op).
12336c746caSKonstantin Varlamov   test_one<Iter, Sent, 7>({1, 2, 3, 4, 5, 6, 7}, 0, {1, 2, 3, 4, 5, 6, 7});
12436c746caSKonstantin Varlamov 
12536c746caSKonstantin Varlamov   // Rotate around the 2nd element.
12636c746caSKonstantin Varlamov   test_one<Iter, Sent, 7>({1, 2, 3, 4, 5, 6, 7}, 1, {2, 3, 4, 5, 6, 7, 1});
12736c746caSKonstantin Varlamov 
12836c746caSKonstantin Varlamov   // Rotate around the last element.
12936c746caSKonstantin Varlamov   test_one<Iter, Sent, 7>({1, 2, 3, 4, 5, 6, 7}, 6, {7, 1, 2, 3, 4, 5, 6});
13036c746caSKonstantin Varlamov 
13136c746caSKonstantin Varlamov   // Pass `end()` as `mid` (no-op).
13236c746caSKonstantin Varlamov   test_one<Iter, Sent, 7>({1, 2, 3, 4, 5, 6, 7}, 7, {1, 2, 3, 4, 5, 6, 7});
13336c746caSKonstantin Varlamov }
13436c746caSKonstantin Varlamov 
13536c746caSKonstantin Varlamov template <class Iter>
test_iter()13636c746caSKonstantin Varlamov constexpr void test_iter() {
13736c746caSKonstantin Varlamov   test_iter_sent<Iter, Iter>();
13836c746caSKonstantin Varlamov   test_iter_sent<Iter, sentinel_wrapper<Iter>>();
13936c746caSKonstantin Varlamov }
14036c746caSKonstantin Varlamov 
test_iterators()14136c746caSKonstantin Varlamov constexpr void test_iterators() {
14236c746caSKonstantin Varlamov   test_iter<forward_iterator<int*>>();
14336c746caSKonstantin Varlamov   test_iter<bidirectional_iterator<int*>>();
14436c746caSKonstantin Varlamov   test_iter<random_access_iterator<int*>>();
14536c746caSKonstantin Varlamov   test_iter<contiguous_iterator<int*>>();
14636c746caSKonstantin Varlamov   test_iter<int*>();
14736c746caSKonstantin Varlamov }
14836c746caSKonstantin Varlamov 
test()14936c746caSKonstantin Varlamov constexpr bool test() {
15036c746caSKonstantin Varlamov   test_iterators();
15136c746caSKonstantin Varlamov 
15236c746caSKonstantin Varlamov   { // Complexity: at most `last - first` swaps.
15336c746caSKonstantin Varlamov     const std::array input = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
15436c746caSKonstantin Varlamov     auto expected = static_cast<int>(input.size());
15536c746caSKonstantin Varlamov 
15636c746caSKonstantin Varlamov     {
15736c746caSKonstantin Varlamov       auto in = input;
15836c746caSKonstantin Varlamov       int swaps = 0;
15936c746caSKonstantin Varlamov       auto begin = adl::Iterator::TrackSwaps(in.data(), swaps);
16036c746caSKonstantin Varlamov       auto end = adl::Iterator::TrackSwaps(in.data() + in.size(), swaps);
16136c746caSKonstantin Varlamov 
162*fb855eb9SMark de Wever       for (std::size_t mid = 0; mid != input.size(); ++mid) {
16336c746caSKonstantin Varlamov         std::ranges::rotate(begin, begin + mid, end);
16436c746caSKonstantin Varlamov         assert(swaps <= expected);
16536c746caSKonstantin Varlamov       }
16636c746caSKonstantin Varlamov     }
16736c746caSKonstantin Varlamov 
16836c746caSKonstantin Varlamov     {
16936c746caSKonstantin Varlamov       auto in = input;
17036c746caSKonstantin Varlamov       int swaps = 0;
17136c746caSKonstantin Varlamov       auto begin = adl::Iterator::TrackSwaps(in.data(), swaps);
17236c746caSKonstantin Varlamov       auto end = adl::Iterator::TrackSwaps(in.data() + in.size(), swaps);
17336c746caSKonstantin Varlamov       auto range = std::ranges::subrange(begin, end);
17436c746caSKonstantin Varlamov 
175*fb855eb9SMark de Wever       for (std::size_t mid = 0; mid != input.size(); ++mid) {
17636c746caSKonstantin Varlamov         std::ranges::rotate(range, begin + mid);
17736c746caSKonstantin Varlamov         assert(swaps <= expected);
17836c746caSKonstantin Varlamov       }
17936c746caSKonstantin Varlamov     }
18036c746caSKonstantin Varlamov   }
18136c746caSKonstantin Varlamov 
18236c746caSKonstantin Varlamov   return true;
18336c746caSKonstantin Varlamov }
18436c746caSKonstantin Varlamov 
main(int,char **)18536c746caSKonstantin Varlamov int main(int, char**) {
18636c746caSKonstantin Varlamov   test();
18736c746caSKonstantin Varlamov   static_assert(test());
18836c746caSKonstantin Varlamov 
18936c746caSKonstantin Varlamov   return 0;
19036c746caSKonstantin Varlamov }
191