Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / test / std / algorithms / alg.nonmodifying / alg.find / ranges.find_if.pass.cpp
blob3f7ec622f145ef04c997d4ccb16a6242e717ca73
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 //===----------------------------------------------------------------------===//
9 // <algorithm>
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 = {});
21 #include <algorithm>
22 #include <array>
23 #include <cassert>
24 #include <ranges>
26 #include "almost_satisfies_types.h"
27 #include "boolean_testable.h"
28 #include "test_iterators.h"
30 struct Predicate {
31 bool operator()(int);
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>);
43 static_assert(!HasFindIfIt<int*, int>);
44 static_assert(!HasFindIfIt<int, int*>);
46 template <class Pred>
47 concept HasFindIfPred = requires(int* it, Pred pred) {std::ranges::find_if(it, it, pred); };
49 static_assert(!HasFindIfPred<IndirectUnaryPredicateNotCopyConstructible>);
50 static_assert(!HasFindIfPred<IndirectUnaryPredicateNotPredicate>);
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>);
62 template <class It, class Sent = It>
63 constexpr void test_iterators() {
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);
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);
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; }
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*>>();
96 // check that projections are used properly and that they are called with the iterator directly
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);
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);
110 // check that the first element is returned
112 struct S {
113 int comp;
114 int other;
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);
123 struct S {
124 int comp;
125 int other;
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);
136 // check that end + 1 iterator is returned with no match
138 int a[] = {1, 1, 1};
139 auto ret = std::ranges::find_if(a, a + 3, [](int) { return false; });
140 assert(ret == a + 3);
143 int a[] = {1, 1, 1};
144 auto ret = std::ranges::find_if(a, [](int){ return false; });
145 assert(ret == a + 3);
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; });
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);
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);
172 // count projection and predicate invocation count
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);
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);
200 // check that the return type of `iter::operator*` doesn't change
202 NonConstComparable a[] = { NonConstComparable{} };
203 auto ret = std::ranges::find_if(a, a + 1, [](auto&& e) { return e == NonConstComparable{}; });
204 assert(ret == a);
207 NonConstComparable a[] = { NonConstComparable{} };
208 auto ret = std::ranges::find_if(a, [](auto&& e) { return e == NonConstComparable{}; });
209 assert(ret == a);
214 // check that an empty range works
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());
221 std::array<int, 0> a = {};
222 auto ret = std::ranges::find_if(a, [](int) { return true; });
223 assert(ret == a.begin());
228 // check that the implicit conversion to bool works
230 int a[] = {1, 2, 3, 4};
231 auto ret = std::ranges::find_if(a, a + 4, [](const int& i) { return BooleanTestable{i == 3}; });
232 assert(ret == a + 2);
235 int a[] = {1, 2, 3, 4};
236 auto ret = std::ranges::find_if(a, [](const int& b) { return BooleanTestable{b == 3}; });
237 assert(ret == a + 2);
241 return true;
244 int main(int, char**) {
245 test();
246 static_assert(test());
248 return 0;