xref: /llvm-project/libcxx/test/std/algorithms/alg.nonmodifying/alg.find/ranges.find_if.pass.cpp (revision ee0f8c4010309a25c95115a9f727a02741e2de48)
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 // <algorithm>
10 
11 // UNSUPPORTED: c++03, c++11, c++14, c++17
12 // UNSUPPORTED: libcpp-has-no-incomplete-ranges
13 
14 // template<input_iterator I, sentinel_for<I> S, class Proj = identity,
15 //          indirect_unary_predicate<projected<I, Proj>> Pred>
16 //   constexpr I ranges::find_if(I first, S last, Pred pred, Proj proj = {});
17 // template<input_range R, class Proj = identity,
18 //          indirect_unary_predicate<projected<iterator_t<R>, Proj>> Pred>
19 //   constexpr borrowed_iterator_t<R>
20 //     ranges::find_if(R&& r, Pred pred, Proj proj = {});
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 
30 struct Predicate {
31   bool operator()(int);
32 };
33 
34 template <class It, class Sent = It>
35 concept HasFindIfIt = requires(It it, Sent sent) { std::ranges::find_if(it, sent, Predicate{}); };
36 static_assert(HasFindIfIt<int*>);
37 static_assert(!HasFindIfIt<InputIteratorNotDerivedFrom>);
38 static_assert(!HasFindIfIt<InputIteratorNotIndirectlyReadable>);
39 static_assert(!HasFindIfIt<InputIteratorNotInputOrOutputIterator>);
40 static_assert(!HasFindIfIt<cpp20_input_iterator<int*>, SentinelForNotSemiregular>);
41 static_assert(!HasFindIfIt<cpp20_input_iterator<int*>, InputRangeNotSentinelEqualityComparableWith>);
42 
43 static_assert(!HasFindIfIt<int*, int>);
44 static_assert(!HasFindIfIt<int, int*>);
45 
46 template <class Pred>
47 concept HasFindIfPred = requires(int* it, Pred pred) {std::ranges::find_if(it, it, pred); };
48 
49 static_assert(!HasFindIfPred<IndirectUnaryPredicateNotCopyConstructible>);
50 static_assert(!HasFindIfPred<IndirectUnaryPredicateNotPredicate>);
51 
52 template <class R>
53 concept HasFindIfR = requires(R r) { std::ranges::find_if(r, Predicate{}); };
54 static_assert(HasFindIfR<std::array<int, 0>>);
55 static_assert(!HasFindIfR<int>);
56 static_assert(!HasFindIfR<InputRangeNotDerivedFrom>);
57 static_assert(!HasFindIfR<InputRangeNotIndirectlyReadable>);
58 static_assert(!HasFindIfR<InputRangeNotInputOrOutputIterator>);
59 static_assert(!HasFindIfR<InputRangeNotSentinelSemiregular>);
60 static_assert(!HasFindIfR<InputRangeNotSentinelEqualityComparableWith>);
61 
62 template <class It, class Sent = It>
63 constexpr void test_iterators() {
64   {
65     int a[] = {1, 2, 3, 4};
66     std::same_as<It> auto ret = std::ranges::find_if(It(a), Sent(It(a + 4)), [](int x) mutable { return x == 4; });
67     assert(base(ret) == a + 3);
68     assert(*ret == 4);
69   }
70   {
71     int a[] = {1, 2, 3, 4};
72     auto range = std::ranges::subrange(It(a), Sent(It(a + 4)));
73     std::same_as<It> auto ret = std::ranges::find_if(range, [](int x) mutable { return x == 4; });
74     assert(base(ret) == a + 3);
75     assert(*ret == 4);
76   }
77 }
78 
79 struct NonConstComparable {
80   friend constexpr bool operator==(const NonConstComparable&, const NonConstComparable&) { return false; }
81   friend constexpr bool operator==(NonConstComparable&, NonConstComparable&) { return false; }
82   friend constexpr bool operator==(const NonConstComparable&, NonConstComparable&) { return false; }
83   friend constexpr bool operator==(NonConstComparable&, const NonConstComparable&) { return true; }
84 };
85 
86 constexpr bool test() {
87   test_iterators<int*>();
88   test_iterators<const int*>();
89   test_iterators<cpp20_input_iterator<int*>, sentinel_wrapper<cpp20_input_iterator<int*>>>();
90   test_iterators<bidirectional_iterator<int*>>();
91   test_iterators<forward_iterator<int*>>();
92   test_iterators<random_access_iterator<int*>>();
93   test_iterators<contiguous_iterator<int*>>();
94 
95   {
96     // check that projections are used properly and that they are called with the iterator directly
97     {
98       int a[] = {1, 2, 3, 4};
99       auto ret = std::ranges::find_if(a, a + 4, [&](int* i) { return i == a + 3; }, [](int& i) { return &i; });
100       assert(ret == a + 3);
101     }
102     {
103       int a[] = {1, 2, 3, 4};
104       auto ret = std::ranges::find_if(a, [&](int* i) { return i == a + 3; }, [](int& i) { return &i; });
105       assert(ret == a + 3);
106     }
107   }
108 
109   {
110     // check that the first element is returned
111     {
112       struct S {
113         int comp;
114         int other;
115       };
116       S a[] = { {0, 0}, {0, 2}, {0, 1} };
117       auto ret = std::ranges::find_if(a, [](int i){ return i == 0; }, &S::comp);
118       assert(ret == a);
119       assert(ret->comp == 0);
120       assert(ret->other == 0);
121     }
122     {
123       struct S {
124         int comp;
125         int other;
126       };
127       S a[] = { {0, 0}, {0, 2}, {0, 1} };
128       auto ret = std::ranges::find_if(a, a + 3, [](int i) { return i == 0; }, &S::comp);
129       assert(ret == a);
130       assert(ret->comp == 0);
131       assert(ret->other == 0);
132     }
133   }
134 
135   {
136     // check that end + 1 iterator is returned with no match
137     {
138       int a[] = {1, 1, 1};
139       auto ret = std::ranges::find_if(a, a + 3, [](int) { return false; });
140       assert(ret == a + 3);
141     }
142     {
143       int a[] = {1, 1, 1};
144       auto ret = std::ranges::find_if(a, [](int){ return false; });
145       assert(ret == a + 3);
146     }
147   }
148 
149   {
150     // check that ranges::dangling is returned
151     [[maybe_unused]] std::same_as<std::ranges::dangling> auto ret =
152       std::ranges::find_if(std::array{1, 2}, [](int){ return false; });
153   }
154 
155   {
156     // check that an iterator is returned with a borrowing range
157     int a[] = {1, 2, 3, 4};
158     std::same_as<int*> auto ret = std::ranges::find_if(std::views::all(a), [](int){ return true; });
159     assert(ret == a);
160     assert(*ret == 1);
161   }
162 
163   {
164     // check that std::invoke is used
165     struct S { int i; };
166     S a[] = { S{1}, S{3}, S{2} };
167     std::same_as<S*> auto ret = std::ranges::find_if(a, [](int) { return false; }, &S::i);
168     assert(ret == a + 3);
169   }
170 
171   {
172     // count projection and predicate invocation count
173     {
174       int a[] = {1, 2, 3, 4};
175       int predicate_count = 0;
176       int projection_count = 0;
177       auto ret = std::ranges::find_if(a, a + 4,
178                                       [&](int i) { ++predicate_count; return i == 2; },
179                                       [&](int i) { ++projection_count; return i; });
180       assert(ret == a + 1);
181       assert(*ret == 2);
182       assert(predicate_count == 2);
183       assert(projection_count == 2);
184     }
185     {
186       int a[] = {1, 2, 3, 4};
187       int predicate_count = 0;
188       int projection_count = 0;
189       auto ret = std::ranges::find_if(a,
190                                       [&](int i) { ++predicate_count; return i == 2; },
191                                       [&](int i) { ++projection_count; return i; });
192       assert(ret == a + 1);
193       assert(*ret == 2);
194       assert(predicate_count == 2);
195       assert(projection_count == 2);
196     }
197   }
198 
199   {
200     // check that the return type of `iter::operator*` doesn't change
201     {
202       NonConstComparable a[] = { NonConstComparable{} };
203       auto ret = std::ranges::find_if(a, a + 1, [](auto&& e) { return e == NonConstComparable{}; });
204       assert(ret == a);
205     }
206     {
207       NonConstComparable a[] = { NonConstComparable{} };
208       auto ret = std::ranges::find_if(a, [](auto&& e) { return e == NonConstComparable{}; });
209       assert(ret == a);
210     }
211   }
212 
213   {
214     // check that an empty range works
215     {
216       std::array<int ,0> a = {};
217       auto ret = std::ranges::find_if(a.begin(), a.end(), [](int) { return true; });
218       assert(ret == a.begin());
219     }
220     {
221       std::array<int, 0> a = {};
222       auto ret = std::ranges::find_if(a, [](int) { return true; });
223       assert(ret == a.begin());
224     }
225   }
226 
227   return true;
228 }
229 
230 int main(int, char**) {
231   test();
232   static_assert(test());
233 
234   return 0;
235 }
236