1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: c++03, c++11, c++14, c++17
13 // Range algorithms should use `std::invoke` to call the given projection(s) (and predicates, where applicable).
19 #include <initializer_list>
22 #include "test_macros.h"
26 constexpr bool unary_pred() const { return val
> 0; }
27 constexpr bool binary_pred(const Foo
& rhs
) const { return val
< rhs
.val
; }
28 constexpr auto operator<=>(const Foo
&) const = default;
33 Bar
create() const { return Bar(); }
36 // Invokes both the (iterator, sentinel, ...) and the (range, ...) overloads of the given niebloid.
39 template <class Func
, std::ranges::range Input
, class... Args
>
40 constexpr void test(Func
&& func
, Input
& in
, Args
&&... args
) {
41 (void)func(in
.begin(), in
.end(), std::forward
<Args
>(args
)...);
42 (void)func(in
, std::forward
<Args
>(args
)...);
46 template <class Func
, std::ranges::range Input
, class... Args
>
47 constexpr void test(Func
&& func
, Input
& in1
, Input
& in2
, Args
&&... args
) {
48 (void)func(in1
.begin(), in1
.end(), in2
.begin(), in2
.end(), std::forward
<Args
>(args
)...);
49 (void)func(in1
, in2
, std::forward
<Args
>(args
)...);
53 template <class Func
, std::ranges::range Input
, class... Args
>
54 constexpr void test_mid(Func
&& func
, Input
& in
, std::ranges::iterator_t
<Input
> mid
, Args
&&... args
) {
55 (void)func(in
.begin(), mid
, in
.end(), std::forward
<Args
>(args
)...);
56 (void)func(in
, mid
, std::forward
<Args
>(args
)...);
59 constexpr bool test_all() {
60 std::array in
= {Bar
{Foo
{1}}, Bar
{Foo
{2}}, Bar
{Foo
{3}}};
61 std::array in2
= {Bar
{Foo
{4}}, Bar
{Foo
{5}}, Bar
{Foo
{6}}};
62 auto mid
= in
.begin() + 1;
64 std::array output
= {Bar
{Foo
{7}}, Bar
{Foo
{8}}, Bar
{Foo
{9}}, Bar
{Foo
{10}}, Bar
{Foo
{11}}, Bar
{Foo
{12}}};
65 auto out
= output
.begin();
66 auto out2
= output
.begin() + 1;
73 std::size_t count
= 1;
75 test(std::ranges::any_of
, in
, &Foo::unary_pred
, &Bar::val
);
76 test(std::ranges::all_of
, in
, &Foo::unary_pred
, &Bar::val
);
77 #if TEST_STD_VER >= 23
78 test(std::ranges::contains
, in
, x
, &Bar::val
);
79 test(std::ranges::ends_with
, in
, in2
, &Foo::binary_pred
, &Bar::val
, &Bar::val
);
81 test(std::ranges::none_of
, in
, &Foo::unary_pred
, &Bar::val
);
82 test(std::ranges::find
, in
, x
, &Bar::val
);
83 test(std::ranges::find_if
, in
, &Foo::unary_pred
, &Bar::val
);
84 test(std::ranges::find_if_not
, in
, &Foo::unary_pred
, &Bar::val
);
85 test(std::ranges::find_first_of
, in
, in2
, &Foo::binary_pred
, &Bar::val
, &Bar::val
);
86 test(std::ranges::adjacent_find
, in
, &Foo::binary_pred
, &Bar::val
);
87 test(std::ranges::mismatch
, in
, in2
, &Foo::binary_pred
, &Bar::val
, &Bar::val
);
88 test(std::ranges::equal
, in
, in2
, &Foo::binary_pred
, &Bar::val
, &Bar::val
);
89 test(std::ranges::lexicographical_compare
, in
, in2
, &Foo::binary_pred
, &Bar::val
, &Bar::val
);
90 test(std::ranges::partition_point
, in
, &Foo::unary_pred
, &Bar::val
);
91 test(std::ranges::lower_bound
, in
, x
, &Foo::binary_pred
, &Bar::val
);
92 test(std::ranges::upper_bound
, in
, x
, &Foo::binary_pred
, &Bar::val
);
93 test(std::ranges::equal_range
, in
, x
, &Foo::binary_pred
, &Bar::val
);
94 test(std::ranges::binary_search
, in
, x
, &Foo::binary_pred
, &Bar::val
);
97 (void)std::ranges::min(a
, b
, &Foo::binary_pred
, &Bar::val
);
98 (void)std::ranges::min(std::initializer_list
<Bar
>{a
, b
}, &Foo::binary_pred
, &Bar::val
);
99 (void)std::ranges::min(in
, &Foo::binary_pred
, &Bar::val
);
101 (void)std::ranges::max(a
, b
, &Foo::binary_pred
, &Bar::val
);
102 (void)std::ranges::max(std::initializer_list
<Bar
>{a
, b
}, &Foo::binary_pred
, &Bar::val
);
103 (void)std::ranges::max(in
, &Foo::binary_pred
, &Bar::val
);
105 (void)std::ranges::minmax(a
, b
, &Foo::binary_pred
, &Bar::val
);
106 (void)std::ranges::minmax(std::initializer_list
<Bar
>{a
, b
}, &Foo::binary_pred
, &Bar::val
);
107 (void)std::ranges::minmax(in
, &Foo::binary_pred
, &Bar::val
);
109 test(std::ranges::min_element
, in
, &Foo::binary_pred
, &Bar::val
);
110 test(std::ranges::max_element
, in
, &Foo::binary_pred
, &Bar::val
);
111 test(std::ranges::minmax_element
, in
, &Foo::binary_pred
, &Bar::val
);
112 test(std::ranges::count
, in
, x
, &Bar::val
);
113 test(std::ranges::count_if
, in
, &Foo::unary_pred
, &Bar::val
);
114 test(std::ranges::search
, in
, in2
, &Foo::binary_pred
, &Bar::val
, &Bar::val
);
115 test(std::ranges::search_n
, in
, count
, x
, &Foo::binary_pred
, &Bar::val
);
116 test(std::ranges::find_end
, in
, in2
, &Foo::binary_pred
, &Bar::val
, &Bar::val
);
117 test(std::ranges::is_partitioned
, in
, &Foo::unary_pred
, &Bar::val
);
118 test(std::ranges::is_sorted
, in
, &Foo::binary_pred
, &Bar::val
);
119 test(std::ranges::is_sorted_until
, in
, &Foo::binary_pred
, &Bar::val
);
120 test(std::ranges::includes
, in
, in2
, &Foo::binary_pred
, &Bar::val
, &Bar::val
);
121 test(std::ranges::is_heap
, in
, &Foo::binary_pred
, &Bar::val
);
122 test(std::ranges::is_heap_until
, in
, &Foo::binary_pred
, &Bar::val
);
123 (void)std::ranges::clamp(b
, a
, c
, &Foo::binary_pred
, &Bar::val
);
124 test(std::ranges::is_permutation
, in
, in2
, &Foo::binary_pred
, &Bar::val
, &Bar::val
);
125 test(std::ranges::for_each
, in
, &Foo::unary_pred
, &Bar::val
);
126 std::ranges::for_each_n(in
.begin(), count
, &Foo::unary_pred
, &Bar::val
);
127 // `copy`, `copy_n` and `copy_backward` have neither a projection nor a predicate.
128 test(std::ranges::copy_if
, in
, out
, &Foo::unary_pred
, &Bar::val
);
129 // `move` and `move_backward` have neither a projection nor a predicate.
130 // `fill` and `fill_n` have neither a projection nor a predicate.
132 std::array out_transform
= {false, true, true};
133 test(std::ranges::transform
, in
, out_transform
.begin(), &Foo::unary_pred
, &Bar::val
);
135 // Whether `ranges::generate{,_n}` invokes `gen` via `std::invoke` is not observable.
136 test(std::ranges::remove_copy
, in
, out
, x
, &Bar::val
);
137 test(std::ranges::remove_copy_if
, in
, out
, &Foo::unary_pred
, &Bar::val
);
138 // `replace*` algorithms only use the projection to compare the elements, not to write them.
139 test(std::ranges::replace
, in
, x
, a
, &Bar::val
);
140 test(std::ranges::replace_if
, in
, &Foo::unary_pred
, a
, &Bar::val
);
141 test(std::ranges::replace_copy
, in
, out
, x
, a
, &Bar::val
);
142 test(std::ranges::replace_copy_if
, in
, out
, &Foo::unary_pred
, a
, &Bar::val
);
143 // `swap_ranges` has neither a projection nor a predicate.
144 // `reverse_copy` has neither a projection nor a predicate.
145 // `rotate_copy` has neither a projection nor a predicate.
146 // For `sample`, whether the given generator is invoked via `std::invoke` is not observable.
147 test(std::ranges::unique_copy
, in
, out
, &Foo::binary_pred
, &Bar::val
);
148 test(std::ranges::partition_copy
, in
, out
, out2
, &Foo::unary_pred
, &Bar::val
);
149 test(std::ranges::partial_sort_copy
, in
, in2
, &Foo::binary_pred
, &Bar::val
, &Bar::val
);
150 test(std::ranges::merge
, in
, in2
, out
, &Foo::binary_pred
, &Bar::val
, &Bar::val
);
151 #if TEST_STD_VER > 20
152 test(std::ranges::starts_with
, in
, in2
, &Foo::binary_pred
, &Bar::val
, &Bar::val
);
154 test(std::ranges::set_difference
, in
, in2
, out
, &Foo::binary_pred
, &Bar::val
, &Bar::val
);
155 test(std::ranges::set_intersection
, in
, in2
, out
, &Foo::binary_pred
, &Bar::val
, &Bar::val
);
156 test(std::ranges::set_symmetric_difference
, in
, in2
, out
, &Foo::binary_pred
, &Bar::val
, &Bar::val
);
157 test(std::ranges::set_union
, in
, in2
, out
, &Foo::binary_pred
, &Bar::val
, &Bar::val
);
158 test(std::ranges::remove
, in
, x
, &Bar::val
);
159 test(std::ranges::remove_if
, in
, &Foo::unary_pred
, &Bar::val
);
160 // `reverse` has neither a projection nor a predicate.
161 // `rotate` has neither a projection nor a predicate.
162 // For `shuffle`, whether the given generator is invoked via `std::invoke` is not observable.
163 test(std::ranges::unique
, in
, &Foo::binary_pred
, &Bar::val
);
164 test(std::ranges::partition
, in
, &Foo::unary_pred
, &Bar::val
);
165 if (!std::is_constant_evaluated())
166 test(std::ranges::stable_partition
, in
, &Foo::unary_pred
, &Bar::val
);
167 test(std::ranges::sort
, in
, &Foo::binary_pred
, &Bar::val
);
168 if (!std::is_constant_evaluated())
169 test(std::ranges::stable_sort
, in
, &Foo::binary_pred
, &Bar::val
);
170 test_mid(std::ranges::partial_sort
, in
, mid
, &Foo::binary_pred
, &Bar::val
);
171 test_mid(std::ranges::nth_element
, in
, mid
, &Foo::binary_pred
, &Bar::val
);
172 if (!std::is_constant_evaluated())
173 test_mid(std::ranges::inplace_merge
, in
, mid
, &Foo::binary_pred
, &Bar::val
);
174 test(std::ranges::make_heap
, in
, &Foo::binary_pred
, &Bar::val
);
175 test(std::ranges::push_heap
, in
, &Foo::binary_pred
, &Bar::val
);
176 test(std::ranges::pop_heap
, in
, &Foo::binary_pred
, &Bar::val
);
177 test(std::ranges::sort_heap
, in
, &Foo::binary_pred
, &Bar::val
);
178 test(std::ranges::prev_permutation
, in
, &Foo::binary_pred
, &Bar::val
);
179 test(std::ranges::next_permutation
, in
, &Foo::binary_pred
, &Bar::val
);
184 int main(int, char**) {
186 static_assert(test_all());