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