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 // UNSUPPORTED: libcpp-has-no-incomplete-ranges 11 12 // template<bidirectional_iterator I, sentinel_for<I> S, weakly_incrementable O> 13 // requires indirectly_copyable<I, O> 14 // constexpr ranges::reverse_copy_result<I, O> 15 // ranges::reverse_copy(I first, S last, O result); 16 // template<bidirectional_range R, weakly_incrementable O> 17 // requires indirectly_copyable<iterator_t<R>, O> 18 // constexpr ranges::reverse_copy_result<borrowed_iterator_t<R>, O> 19 // ranges::reverse_copy(R&& r, O result); 20 21 // <algorithm> 22 23 #include <algorithm> 24 #include <array> 25 #include <cassert> 26 #include <ranges> 27 28 #include "almost_satisfies_types.h" 29 #include "test_iterators.h" 30 31 template <class Iter, class Out = int*, class Sent = sentinel_wrapper<Iter>> 32 concept HasReverseCopyIt = requires(Iter first, Sent last, Out out) { std::ranges::reverse_copy(first, last, out); }; 33 34 template <class Range, class Out = int*> 35 concept HasReverseCopyR = requires(Range range, Out out) { std::ranges::reverse_copy(range, out); }; 36 37 static_assert(HasReverseCopyIt<int*>); 38 static_assert(!HasReverseCopyIt<BidirectionalIteratorNotDerivedFrom>); 39 static_assert(!HasReverseCopyIt<BidirectionalIteratorNotDecrementable>); 40 static_assert(!HasReverseCopyIt<int*, SentinelForNotSemiregular>); 41 static_assert(!HasReverseCopyIt<int*, SentinelForNotWeaklyEqualityComparableWith>); 42 static_assert(!HasReverseCopyIt<int*, OutputIteratorNotIndirectlyWritable>); 43 static_assert(!HasReverseCopyIt<int*, OutputIteratorNotInputOrOutputIterator>); 44 45 static_assert(HasReverseCopyR<UncheckedRange<int*>>); 46 static_assert(!HasReverseCopyR<BidirectionalRangeNotDerivedFrom>); 47 static_assert(!HasReverseCopyR<BidirectionalRangeNotDecrementable>); 48 static_assert(!HasReverseCopyR<UncheckedRange<int*, SentinelForNotSemiregular>>); 49 static_assert(!HasReverseCopyR<UncheckedRange<int*>, OutputIteratorNotIndirectlyWritable>); 50 static_assert(!HasReverseCopyR<UncheckedRange<int*>, OutputIteratorNotInputOrOutputIterator>); 51 52 static_assert(std::is_same_v<std::ranges::reverse_copy_result<int, int>, std::ranges::in_out_result<int, int>>); 53 54 template <class Iter, class OutIter, class Sent, int N> 55 constexpr void test(std::array<int, N> value, std::array<int, N> expected) { 56 { 57 std::array<int, N> out; 58 std::same_as<std::ranges::in_out_result<Iter, OutIter>> decltype(auto) ret = 59 std::ranges::reverse_copy(Iter(value.data()), 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::reverse_copy(range, 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}, {4, 3, 2, 1}); 81 82 // check that an empty range works 83 test<Iter, OutIter, Sent, 0>({}, {}); 84 85 // check that a single element range works 86 test<Iter, OutIter, Sent, 1>({1}, {1}); 87 88 // check that a two element range works 89 test<Iter, OutIter, Sent, 2>({1, 2}, {2, 1}); 90 } 91 92 template <class Iter, class Sent = Iter> 93 constexpr void test_out_iterators() { 94 test_iterators<Iter, cpp20_output_iterator<int*>, Sent>(); 95 test_iterators<Iter, forward_iterator<int*>, Sent>(); 96 test_iterators<Iter, bidirectional_iterator<int*>, Sent>(); 97 test_iterators<Iter, random_access_iterator<int*>, Sent>(); 98 test_iterators<Iter, contiguous_iterator<int*>, Sent>(); 99 test_iterators<Iter, int*, Sent>(); 100 } 101 102 constexpr bool test() { 103 test_out_iterators<bidirectional_iterator<int*>>(); 104 test_out_iterators<random_access_iterator<int*>>(); 105 test_out_iterators<contiguous_iterator<int*>>(); 106 test_out_iterators<int*>(); 107 test_out_iterators<const int*>(); 108 109 { 110 struct AssignmentCounter { 111 int* counter; 112 113 constexpr AssignmentCounter(int* counter_) : counter(counter_) {} 114 constexpr AssignmentCounter& operator=(const AssignmentCounter&) { ++*counter; return *this; } 115 }; 116 117 { 118 int c = 0; 119 AssignmentCounter a[] = {&c, &c, &c, &c}; 120 AssignmentCounter b[] = {&c, &c, &c, &c}; 121 std::ranges::reverse_copy(a, a + 4, b); 122 assert(c == 4); 123 } 124 { 125 int c = 0; 126 AssignmentCounter a[] = {&c, &c, &c, &c}; 127 AssignmentCounter b[] = {&c, &c, &c, &c}; 128 std::ranges::reverse_copy(a, b); 129 assert(c == 4); 130 } 131 } 132 133 return true; 134 } 135 136 int main(int, char**) { 137 test(); 138 static_assert(test()); 139 140 return 0; 141 } 142