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// <algorithm>
12//
13// Range algorithms that take predicates should support predicates that return a non-boolean value as long as the
14// returned type is implicitly convertible to bool.
15
16#include <algorithm>
17
18#include <initializer_list>
19#include <ranges>
20
21#include "boolean_testable.h"
22#include "test_macros.h"
23
24using Value = StrictComparable<int>;
25using Iterator = StrictBooleanIterator<Value*>;
26using Range = std::ranges::subrange<Iterator>;
27auto pred1 = StrictUnaryPredicate;
28auto pred2 = StrictBinaryPredicate;
29auto projection = [](Value const& val) -> Value { return val; };
30
31void f(Iterator it, Range in, Iterator out, std::size_t n, Value const& val, std::initializer_list<Value> ilist) {
32 // Functions of the form (in, pred)
33 auto in_pred = [&](auto func, auto pred) {
34 (void)func(it, it, pred);
35 (void)func(in, pred);
36 (void)func(it, it, pred, projection);
37 (void)func(in, pred, projection);
38 };
39
40 // Functions of the form (in, in, pred)
41 auto in_in_pred = [&](auto func, auto pred) {
42 (void)func(it, it, it, it, pred);
43 (void)func(in, in, pred);
44 (void)func(it, it, it, it, pred, projection);
45 (void)func(in, in, pred, projection);
46 };
47
48 in_pred(std::ranges::any_of, pred1);
49 in_pred(std::ranges::all_of, pred1);
50#if TEST_STD_VER >= 23
51 in_in_pred(std::ranges::ends_with, pred2);
52#endif
53 in_pred(std::ranges::none_of, pred1);
54 in_pred(std::ranges::find_if, pred1);
55 in_pred(std::ranges::find_if_not, pred1);
56 in_in_pred(std::ranges::find_first_of, pred2);
57 in_pred(std::ranges::adjacent_find, pred2);
58 in_in_pred(std::ranges::mismatch, pred2);
59 in_in_pred(std::ranges::equal, pred2);
60 in_in_pred(std::ranges::lexicographical_compare, pred2);
61 in_pred(std::ranges::partition_point, pred1);
62 // lower_bound
63 {
64 (void)std::ranges::lower_bound(it, it, val, pred2);
65 (void)std::ranges::lower_bound(in, val, pred2);
66 (void)std::ranges::lower_bound(it, it, val, pred2, projection);
67 (void)std::ranges::lower_bound(in, val, pred2, projection);
68 }
69 // upper_bound
70 {
71 (void)std::ranges::upper_bound(it, it, val, pred2);
72 (void)std::ranges::upper_bound(in, val, pred2);
73 (void)std::ranges::upper_bound(it, it, val, pred2, projection);
74 (void)std::ranges::upper_bound(in, val, pred2, projection);
75 }
76 // equal_range
77 {
78 (void)std::ranges::equal_range(it, it, val, pred2);
79 (void)std::ranges::equal_range(in, val, pred2);
80 (void)std::ranges::equal_range(it, it, val, pred2, projection);
81 (void)std::ranges::equal_range(in, val, pred2, projection);
82 }
83 // binary_search
84 {
85 (void)std::ranges::binary_search(it, it, val, pred2);
86 (void)std::ranges::binary_search(in, val, pred2);
87 (void)std::ranges::binary_search(it, it, val, pred2, projection);
88 (void)std::ranges::binary_search(in, val, pred2, projection);
89 }
90 // min
91 {
92 (void)std::ranges::min(val, val, pred2);
93 (void)std::ranges::min(val, val, pred2, projection);
94 (void)std::ranges::min(ilist, pred2);
95 (void)std::ranges::min(ilist, pred2, projection);
96 (void)std::ranges::min(in, pred2);
97 (void)std::ranges::min(in, pred2, projection);
98 }
99 // max
100 {
101 (void)std::ranges::max(val, val, pred2);
102 (void)std::ranges::max(val, val, pred2, projection);
103 (void)std::ranges::max(ilist, pred2);
104 (void)std::ranges::max(ilist, pred2, projection);
105 (void)std::ranges::max(in, pred2);
106 (void)std::ranges::max(in, pred2, projection);
107 }
108 // minmax
109 {
110 (void)std::ranges::minmax(val, val, pred2);
111 (void)std::ranges::minmax(val, val, pred2, projection);
112 (void)std::ranges::minmax(ilist, pred2);
113 (void)std::ranges::minmax(ilist, pred2, projection);
114 (void)std::ranges::minmax(in, pred2);
115 (void)std::ranges::minmax(in, pred2, projection);
116 }
117
118 in_pred(std::ranges::min_element, pred2);
119 in_pred(std::ranges::max_element, pred2);
120 in_pred(std::ranges::minmax_element, pred2);
121 in_pred(std::ranges::count_if, pred1);
122 in_in_pred(std::ranges::search, pred2);
123 // search_n
124 {
125 (void)std::ranges::search_n(it, it, n, val, pred2);
126 (void)std::ranges::search_n(in, n, val, pred2);
127 (void)std::ranges::search_n(it, it, n, val, pred2, projection);
128 (void)std::ranges::search_n(in, n, val, pred2, projection);
129 }
130 in_in_pred(std::ranges::find_end, pred2);
131 in_pred(std::ranges::is_partitioned, pred1);
132 in_pred(std::ranges::is_sorted, pred2);
133 in_pred(std::ranges::is_sorted_until, pred2);
134 in_in_pred(std::ranges::includes, pred2);
135 in_pred(std::ranges::is_heap, pred2);
136 in_pred(std::ranges::is_heap_until, pred2);
137 // clamp
138 {
139 (void)std::ranges::clamp(val, val, val);
140 (void)std::ranges::clamp(val, val, val, pred2);
141 (void)std::ranges::clamp(val, val, val, pred2, projection);
142 }
143 in_in_pred(std::ranges::is_permutation, pred2);
144 // copy_if
145 {
146 (void)std::ranges::copy_if(it, it, out, pred1);
147 (void)std::ranges::copy_if(in, out, pred1);
148 (void)std::ranges::copy_if(it, it, out, pred1, projection);
149 (void)std::ranges::copy_if(in, out, pred1, projection);
150 }
151 {
152 (void)std::ranges::remove_copy_if(it, it, out, pred1);
153 (void)std::ranges::remove_copy_if(in, out, pred1);
154 (void)std::ranges::remove_copy_if(it, it, out, pred1, projection);
155 (void)std::ranges::remove_copy_if(in, out, pred1, projection);
156 }
157 // remove_copy
158 {
159 (void)std::ranges::remove_copy(it, it, out, val);
160 (void)std::ranges::remove_copy(in, out, val);
161 (void)std::ranges::remove_copy(it, it, out, val, projection);
162 (void)std::ranges::remove_copy(in, out, val, projection);
163 }
164 // replace
165 {
166 (void)std::ranges::replace(it, it, val, val);
167 (void)std::ranges::replace(in, val, val);
168 (void)std::ranges::replace(it, it, val, val, projection);
169 (void)std::ranges::replace(in, val, val, projection);
170 }
171 // replace_if
172 {
173 (void)std::ranges::replace_if(it, it, pred1, val);
174 (void)std::ranges::replace_if(in, pred1, val);
175 (void)std::ranges::replace_if(it, it, pred1, val, projection);
176 (void)std::ranges::replace_if(in, pred1, val, projection);
177 }
178 // replace_copy_if
179 {
180 (void)std::ranges::replace_copy_if(it, it, out, pred1, val);
181 (void)std::ranges::replace_copy_if(in, out, pred1, val);
182 (void)std::ranges::replace_copy_if(it, it, out, pred1, val, projection);
183 (void)std::ranges::replace_copy_if(in, out, pred1, val, projection);
184 }
185 // replace_copy
186 {
187 (void)std::ranges::replace_copy(it, it, out, val, val);
188 (void)std::ranges::replace_copy(in, out, val, val);
189 (void)std::ranges::replace_copy(it, it, out, val, val, projection);
190 (void)std::ranges::replace_copy(in, out, val, val, projection);
191 }
192 // unique_copy
193 {
194 (void)std::ranges::unique_copy(it, it, out, pred2);
195 (void)std::ranges::unique_copy(in, out, pred2);
196 (void)std::ranges::unique_copy(it, it, out, pred2, projection);
197 (void)std::ranges::unique_copy(in, out, pred2, projection);
198 }
199 // partition_copy
200 {
201 (void)std::ranges::partition_copy(it, it, out, out, pred1);
202 (void)std::ranges::partition_copy(in, out, out, pred1);
203 (void)std::ranges::partition_copy(it, it, out, out, pred1, projection);
204 (void)std::ranges::partition_copy(in, out, out, pred1, projection);
205 }
206 in_in_pred(std::ranges::partial_sort_copy, pred2);
207#if TEST_STD_VER > 20
208 in_in_pred(std::ranges::starts_with, pred2);
209#endif
210 // merge
211 {
212 (void)std::ranges::merge(it, it, it, it, out, pred2);
213 (void)std::ranges::merge(in, in, out, pred2);
214 (void)std::ranges::merge(it, it, it, it, out, pred2, projection, projection);
215 (void)std::ranges::merge(in, in, out, pred2, projection, projection);
216 }
217 // set_difference
218 {
219 (void)std::ranges::set_difference(it, it, it, it, out, pred2);
220 (void)std::ranges::set_difference(in, in, out, pred2);
221 (void)std::ranges::set_difference(it, it, it, it, out, pred2, projection, projection);
222 (void)std::ranges::set_difference(in, in, out, pred2, projection, projection);
223 }
224 // set_intersection
225 {
226 (void)std::ranges::set_intersection(it, it, it, it, out, pred2);
227 (void)std::ranges::set_intersection(in, in, out, pred2);
228 (void)std::ranges::set_intersection(it, it, it, it, out, pred2, projection, projection);
229 (void)std::ranges::set_intersection(in, in, out, pred2, projection, projection);
230 }
231 // set_symmetric_difference
232 {
233 (void)std::ranges::set_symmetric_difference(it, it, it, it, out, pred2);
234 (void)std::ranges::set_symmetric_difference(in, in, out, pred2);
235 (void)std::ranges::set_symmetric_difference(it, it, it, it, out, pred2, projection, projection);
236 (void)std::ranges::set_symmetric_difference(in, in, out, pred2, projection, projection);
237 }
238 // set_union
239 {
240 (void)std::ranges::set_union(it, it, it, it, out, pred2);
241 (void)std::ranges::set_union(in, in, out, pred2);
242 (void)std::ranges::set_union(it, it, it, it, out, pred2, projection, projection);
243 (void)std::ranges::set_union(in, in, out, pred2, projection, projection);
244 }
245 in_pred(std::ranges::remove_if, pred1);
246 // remove
247 {
248 (void)std::ranges::remove(it, it, val);
249 (void)std::ranges::remove(it, it, val, projection);
250 (void)std::ranges::remove(in, val);
251 (void)std::ranges::remove(in, val, projection);
252 }
253 in_pred(std::ranges::unique, pred2);
254 in_pred(std::ranges::partition, pred1);
255 in_pred(std::ranges::stable_partition, pred1);
256 in_pred(std::ranges::sort, pred2);
257 in_pred(std::ranges::stable_sort, pred2);
258 // partial_sort
259 {
260 (void)std::ranges::partial_sort(it, it, it, pred2);
261 (void)std::ranges::partial_sort(in, it, pred2);
262 (void)std::ranges::partial_sort(it, it, it, pred2, projection);
263 (void)std::ranges::partial_sort(in, it, pred2, projection);
264 }
265 // nth_element
266 {
267 (void)std::ranges::nth_element(it, it, it, pred2);
268 (void)std::ranges::nth_element(in, it, pred2);
269 (void)std::ranges::nth_element(it, it, it, pred2, projection);
270 (void)std::ranges::nth_element(in, it, pred2, projection);
271 }
272 // inplace_merge
273 {
274 (void)std::ranges::inplace_merge(it, it, it, pred2);
275 (void)std::ranges::inplace_merge(in, it, pred2);
276 (void)std::ranges::inplace_merge(it, it, it, pred2, projection);
277 (void)std::ranges::inplace_merge(in, it, pred2, projection);
278 }
279 in_pred(std::ranges::make_heap, pred2);
280 in_pred(std::ranges::push_heap, pred2);
281 in_pred(std::ranges::pop_heap, pred2);
282 in_pred(std::ranges::sort_heap, pred2);
283 in_pred(std::ranges::prev_permutation, pred2);
284 in_pred(std::ranges::next_permutation, pred2);
285}
286

source code of libcxx/test/std/algorithms/ranges_robust_against_nonbool.compile.pass.cpp