| 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 | #include "type_algorithms.h" |
| 30 | |
| 31 | template <class Iter, class Out = int*, class Sent = sentinel_wrapper<Iter>> |
| 32 | concept HasRotateCopyIt = |
| 33 | requires(Iter first, Sent last, Out out) { std::ranges::rotate_copy(first, first, last, out); }; |
| 34 | |
| 35 | template <class Range, class Out = int*> |
| 36 | concept HasRotateCopyR = requires(Range range, Out out) { std::ranges::rotate_copy(range, nullptr, out); }; |
| 37 | |
| 38 | static_assert(HasRotateCopyIt<int*>); |
| 39 | static_assert(!HasRotateCopyIt<ForwardIteratorNotDerivedFrom>); |
| 40 | static_assert(!HasRotateCopyIt<ForwardIteratorNotIncrementable>); |
| 41 | static_assert(!HasRotateCopyIt<int*, SentinelForNotSemiregular>); |
| 42 | static_assert(!HasRotateCopyIt<int*, SentinelForNotWeaklyEqualityComparableWith>); |
| 43 | static_assert(!HasRotateCopyIt<int*, OutputIteratorNotIndirectlyWritable>); |
| 44 | static_assert(!HasRotateCopyIt<int*, OutputIteratorNotInputOrOutputIterator>); |
| 45 | |
| 46 | static_assert(HasRotateCopyR<UncheckedRange<int*>>); |
| 47 | static_assert(!HasRotateCopyR<ForwardRangeNotDerivedFrom>); |
| 48 | static_assert(!HasRotateCopyR<ForwardRangeNotIncrementable>); |
| 49 | static_assert(!HasRotateCopyR<UncheckedRange<int*, SentinelForNotSemiregular>>); |
| 50 | static_assert(!HasRotateCopyR<UncheckedRange<int*>, OutputIteratorNotIndirectlyWritable>); |
| 51 | static_assert(!HasRotateCopyR<UncheckedRange<int*>, OutputIteratorNotInputOrOutputIterator>); |
| 52 | |
| 53 | static_assert(std::is_same_v<std::ranges::rotate_copy_result<int, int>, std::ranges::in_out_result<int, int>>); |
| 54 | |
| 55 | template <class Iter, class OutIter, class Sent, int N> |
| 56 | constexpr void test(std::array<int, N> value, std::size_t middle, std::array<int, N> expected) { |
| 57 | { |
| 58 | std::array<int, N> out; |
| 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())); |
| 61 | assert(base(ret.in) == value.data() + value.size()); |
| 62 | assert(base(ret.out) == out.data() + out.size()); |
| 63 | assert(out == expected); |
| 64 | } |
| 65 | { |
| 66 | std::array<int, N> out; |
| 67 | auto range = std::ranges::subrange(Iter(value.data()), Sent(Iter(value.data() + value.size()))); |
| 68 | std::same_as<std::ranges::in_out_result<Iter, OutIter>> decltype(auto) ret = |
| 69 | std::ranges::rotate_copy(range, Iter(value.data() + middle), OutIter(out.data())); |
| 70 | assert(base(ret.in) == value.data() + value.size()); |
| 71 | assert(base(ret.out) == out.data() + out.size()); |
| 72 | assert(out == expected); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | template <class Iter, class OutIter, class Sent> |
| 77 | constexpr void test_iterators() { |
| 78 | // simple test |
| 79 | test<Iter, OutIter, Sent, 4>({1, 2, 3, 4}, 2, {3, 4, 1, 2}); |
| 80 | |
| 81 | // check that an empty range works |
| 82 | test<Iter, OutIter, Sent, 0>({}, 0, {}); |
| 83 | |
| 84 | // check that a single element range works |
| 85 | test<Iter, OutIter, Sent, 1>({1}, 0, {1}); |
| 86 | |
| 87 | // check that a two element range works |
| 88 | test<Iter, OutIter, Sent, 2>({1, 2}, 1, {2, 1}); |
| 89 | |
| 90 | // rotate on the first element |
| 91 | test<Iter, OutIter, Sent, 7>({1, 2, 3, 4, 5, 6, 7}, 0, {1, 2, 3, 4, 5, 6, 7}); |
| 92 | |
| 93 | // rotate on the second element |
| 94 | test<Iter, OutIter, Sent, 7>({1, 2, 3, 4, 5, 6, 7}, 1, {2, 3, 4, 5, 6, 7, 1}); |
| 95 | |
| 96 | // rotate on the last element |
| 97 | test<Iter, OutIter, Sent, 7>({1, 2, 3, 4, 5, 6, 7}, 6, {7, 1, 2, 3, 4, 5, 6}); |
| 98 | |
| 99 | // rotate on the one-past-the-end pointer |
| 100 | test<Iter, OutIter, Sent, 7>({1, 2, 3, 4, 5, 6, 7}, 7, {1, 2, 3, 4, 5, 6, 7}); |
| 101 | } |
| 102 | |
| 103 | constexpr bool test() { |
| 104 | types::for_each(types::forward_iterator_list<const int*>(), []<class Iter>() { |
| 105 | types::for_each( |
| 106 | types::concatenate_t<types::forward_iterator_list<int*>, types::type_list<cpp20_output_iterator<int*> > >(), |
| 107 | []<class OutIter>() { test_iterators<Iter, OutIter, Iter>(); }); |
| 108 | }); |
| 109 | |
| 110 | { |
| 111 | struct AssignmentCounter { |
| 112 | int* counter; |
| 113 | |
| 114 | constexpr AssignmentCounter(int* counter_) : counter(counter_) {} |
| 115 | constexpr AssignmentCounter& operator=(const AssignmentCounter&) { |
| 116 | ++*counter; |
| 117 | return *this; |
| 118 | } |
| 119 | }; |
| 120 | |
| 121 | { |
| 122 | int c = 0; |
| 123 | AssignmentCounter a[] = {&c, &c, &c, &c}; |
| 124 | AssignmentCounter b[] = {&c, &c, &c, &c}; |
| 125 | std::ranges::rotate_copy(a, a + 2, a + 4, b); |
| 126 | assert(c == 4); |
| 127 | } |
| 128 | { |
| 129 | int c = 0; |
| 130 | AssignmentCounter a[] = {&c, &c, &c, &c}; |
| 131 | AssignmentCounter b[] = {&c, &c, &c, &c}; |
| 132 | std::ranges::rotate_copy(a, a + 2, b); |
| 133 | assert(c == 4); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | return true; |
| 138 | } |
| 139 | |
| 140 | int main(int, char**) { |
| 141 | test(); |
| 142 | static_assert(test()); |
| 143 | |
| 144 | return 0; |
| 145 | } |
| 146 | |