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 //===----------------------------------------------------------------------===//
11 // UNSUPPORTED: c++03, c++11, c++14, c++17
13 // template<input_iterator I, sentinel_for<I> S, class Proj = identity,
14 // indirect_unary_predicate<projected<I, Proj>> Pred>
15 // constexpr I ranges::find_if(I first, S last, Pred pred, Proj proj = {});
16 // template<input_range R, class Proj = identity,
17 // indirect_unary_predicate<projected<iterator_t<R>, Proj>> Pred>
18 // constexpr borrowed_iterator_t<R>
19 // ranges::find_if(R&& r, Pred pred, Proj proj = {});
26 #include "almost_satisfies_types.h"
27 #include "test_iterators.h"
33 template <class It
, class Sent
= It
>
34 concept HasFindIfIt
= requires(It it
, Sent sent
) { std::ranges::find_if(it
, sent
, Predicate
{}); };
35 static_assert(HasFindIfIt
<int*>);
36 static_assert(!HasFindIfIt
<InputIteratorNotDerivedFrom
>);
37 static_assert(!HasFindIfIt
<InputIteratorNotIndirectlyReadable
>);
38 static_assert(!HasFindIfIt
<InputIteratorNotInputOrOutputIterator
>);
39 static_assert(!HasFindIfIt
<cpp20_input_iterator
<int*>, SentinelForNotSemiregular
>);
40 static_assert(!HasFindIfIt
<cpp20_input_iterator
<int*>, InputRangeNotSentinelEqualityComparableWith
>);
42 static_assert(!HasFindIfIt
<int*, int>);
43 static_assert(!HasFindIfIt
<int, int*>);
46 concept HasFindIfPred
= requires(int* it
, Pred pred
) {std::ranges::find_if(it
, it
, pred
); };
48 static_assert(!HasFindIfPred
<IndirectUnaryPredicateNotCopyConstructible
>);
49 static_assert(!HasFindIfPred
<IndirectUnaryPredicateNotPredicate
>);
52 concept HasFindIfR
= requires(R r
) { std::ranges::find_if(r
, Predicate
{}); };
53 static_assert(HasFindIfR
<std::array
<int, 0>>);
54 static_assert(!HasFindIfR
<int>);
55 static_assert(!HasFindIfR
<InputRangeNotDerivedFrom
>);
56 static_assert(!HasFindIfR
<InputRangeNotIndirectlyReadable
>);
57 static_assert(!HasFindIfR
<InputRangeNotInputOrOutputIterator
>);
58 static_assert(!HasFindIfR
<InputRangeNotSentinelSemiregular
>);
59 static_assert(!HasFindIfR
<InputRangeNotSentinelEqualityComparableWith
>);
61 template <class It
, class Sent
= It
>
62 constexpr void test_iterators() {
64 int a
[] = {1, 2, 3, 4};
65 std::same_as
<It
> auto ret
= std::ranges::find_if(It(a
), Sent(It(a
+ 4)), [](int x
) mutable { return x
== 4; });
66 assert(base(ret
) == a
+ 3);
70 int a
[] = {1, 2, 3, 4};
71 auto range
= std::ranges::subrange(It(a
), Sent(It(a
+ 4)));
72 std::same_as
<It
> auto ret
= std::ranges::find_if(range
, [](int x
) mutable { return x
== 4; });
73 assert(base(ret
) == a
+ 3);
78 struct NonConstComparable
{
79 friend constexpr bool operator==(const NonConstComparable
&, const NonConstComparable
&) { return false; }
80 friend constexpr bool operator==(NonConstComparable
&, NonConstComparable
&) { return false; }
81 friend constexpr bool operator==(const NonConstComparable
&, NonConstComparable
&) { return false; }
82 friend constexpr bool operator==(NonConstComparable
&, const NonConstComparable
&) { return true; }
85 constexpr bool test() {
86 test_iterators
<int*>();
87 test_iterators
<const int*>();
88 test_iterators
<cpp20_input_iterator
<int*>, sentinel_wrapper
<cpp20_input_iterator
<int*>>>();
89 test_iterators
<bidirectional_iterator
<int*>>();
90 test_iterators
<forward_iterator
<int*>>();
91 test_iterators
<random_access_iterator
<int*>>();
92 test_iterators
<contiguous_iterator
<int*>>();
95 // check that projections are used properly and that they are called with the iterator directly
97 int a
[] = {1, 2, 3, 4};
98 auto ret
= std::ranges::find_if(a
, a
+ 4, [&](int* i
) { return i
== a
+ 3; }, [](int& i
) { return &i
; });
102 int a
[] = {1, 2, 3, 4};
103 auto ret
= std::ranges::find_if(a
, [&](int* i
) { return i
== a
+ 3; }, [](int& i
) { return &i
; });
104 assert(ret
== a
+ 3);
109 // check that the first element is returned
115 S a
[] = { {0, 0}, {0, 2}, {0, 1} };
116 auto ret
= std::ranges::find_if(a
, [](int i
){ return i
== 0; }, &S::comp
);
118 assert(ret
->comp
== 0);
119 assert(ret
->other
== 0);
126 S a
[] = { {0, 0}, {0, 2}, {0, 1} };
127 auto ret
= std::ranges::find_if(a
, a
+ 3, [](int i
) { return i
== 0; }, &S::comp
);
129 assert(ret
->comp
== 0);
130 assert(ret
->other
== 0);
135 // check that end + 1 iterator is returned with no match
138 auto ret
= std::ranges::find_if(a
, a
+ 3, [](int) { return false; });
139 assert(ret
== a
+ 3);
143 auto ret
= std::ranges::find_if(a
, [](int){ return false; });
144 assert(ret
== a
+ 3);
149 // check that ranges::dangling is returned
150 [[maybe_unused
]] std::same_as
<std::ranges::dangling
> auto ret
=
151 std::ranges::find_if(std::array
{1, 2}, [](int){ return false; });
155 // check that an iterator is returned with a borrowing range
156 int a
[] = {1, 2, 3, 4};
157 std::same_as
<int*> auto ret
= std::ranges::find_if(std::views::all(a
), [](int){ return true; });
163 // check that std::invoke is used
165 S a
[] = { S
{1}, S
{3}, S
{2} };
166 std::same_as
<S
*> auto ret
= std::ranges::find_if(a
, [](int) { return false; }, &S::i
);
167 assert(ret
== a
+ 3);
171 // count projection and predicate invocation count
173 int a
[] = {1, 2, 3, 4};
174 int predicate_count
= 0;
175 int projection_count
= 0;
176 auto ret
= std::ranges::find_if(a
, a
+ 4,
177 [&](int i
) { ++predicate_count
; return i
== 2; },
178 [&](int i
) { ++projection_count
; return i
; });
179 assert(ret
== a
+ 1);
181 assert(predicate_count
== 2);
182 assert(projection_count
== 2);
185 int a
[] = {1, 2, 3, 4};
186 int predicate_count
= 0;
187 int projection_count
= 0;
188 auto ret
= std::ranges::find_if(a
,
189 [&](int i
) { ++predicate_count
; return i
== 2; },
190 [&](int i
) { ++projection_count
; return i
; });
191 assert(ret
== a
+ 1);
193 assert(predicate_count
== 2);
194 assert(projection_count
== 2);
199 // check that the return type of `iter::operator*` doesn't change
201 NonConstComparable a
[] = { NonConstComparable
{} };
202 auto ret
= std::ranges::find_if(a
, a
+ 1, [](auto&& e
) { return e
== NonConstComparable
{}; });
206 NonConstComparable a
[] = { NonConstComparable
{} };
207 auto ret
= std::ranges::find_if(a
, [](auto&& e
) { return e
== NonConstComparable
{}; });
213 // check that an empty range works
215 std::array
<int ,0> a
= {};
216 auto ret
= std::ranges::find_if(a
.begin(), a
.end(), [](int) { return true; });
217 assert(ret
== a
.begin());
220 std::array
<int, 0> a
= {};
221 auto ret
= std::ranges::find_if(a
, [](int) { return true; });
222 assert(ret
== a
.begin());
229 int main(int, char**) {
231 static_assert(test());