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

source code of libcxx/test/std/algorithms/alg.modifying.operations/alg.reverse/ranges.reverse_copy.pass.cpp