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_not(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_not(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 "boolean_testable.h"
29 #include "test_iterators.h"
30 
31 struct Predicate {
32   bool operator()(int);
33 };
34 
35 template <class It, class Sent = It>
36 concept HasFindIfNotIt = requires(It it, Sent sent) { std::ranges::find_if_not(it, sent, Predicate{}); };
37 static_assert(HasFindIfNotIt<int*>);
38 static_assert(!HasFindIfNotIt<InputIteratorNotDerivedFrom>);
39 static_assert(!HasFindIfNotIt<InputIteratorNotIndirectlyReadable>);
40 static_assert(!HasFindIfNotIt<InputIteratorNotInputOrOutputIterator>);
41 static_assert(!HasFindIfNotIt<cpp20_input_iterator<int*>, SentinelForNotSemiregular>);
42 static_assert(!HasFindIfNotIt<cpp20_input_iterator<int*>, InputRangeNotSentinelEqualityComparableWith>);
43 
44 static_assert(!HasFindIfNotIt<int*, int>);
45 static_assert(!HasFindIfNotIt<int, int*>);
46 
47 template <class Pred>
48 concept HasFindIfNotPred = requires(int* it, Pred pred) {std::ranges::find_if_not(it, it, pred); };
49 
50 static_assert(!HasFindIfNotPred<IndirectUnaryPredicateNotCopyConstructible>);
51 static_assert(!HasFindIfNotPred<IndirectUnaryPredicateNotPredicate>);
52 
53 template <class R>
54 concept HasFindIfNotR = requires(R r) { std::ranges::find_if_not(r, Predicate{}); };
55 static_assert(HasFindIfNotR<std::array<int, 0>>);
56 static_assert(!HasFindIfNotR<int>);
57 static_assert(!HasFindIfNotR<InputRangeNotDerivedFrom>);
58 static_assert(!HasFindIfNotR<InputRangeNotIndirectlyReadable>);
59 static_assert(!HasFindIfNotR<InputRangeNotInputOrOutputIterator>);
60 static_assert(!HasFindIfNotR<InputRangeNotSentinelSemiregular>);
61 static_assert(!HasFindIfNotR<InputRangeNotSentinelEqualityComparableWith>);
62 
63 template <class It, class Sent = It>
64 constexpr void test_iterators() {
65   {
66     int a[] = {1, 2, 3, 4};
67     std::same_as<It> auto ret = std::ranges::find_if_not(It(a), Sent(It(a + 4)), [c = 0](int) mutable { return c++ <= 2; });
68     assert(base(ret) == a + 3);
69     assert(*ret == 4);
70   }
71   {
72     int a[] = {1, 2, 3, 4};
73     auto range = std::ranges::subrange(It(a), Sent(It(a + 4)));
74     std::same_as<It> auto ret = std::ranges::find_if_not(range, [c = 0](int) mutable { return c++ <= 2; });
75     assert(base(ret) == a + 3);
76     assert(*ret == 4);
77   }
78 }
79 
80 struct NonConstComparableLValue {
81   friend constexpr bool operator==(const NonConstComparableLValue&, const NonConstComparableLValue&) { return false; }
82   friend constexpr bool operator==(NonConstComparableLValue&, NonConstComparableLValue&) { return false; }
83   friend constexpr bool operator==(const NonConstComparableLValue&, NonConstComparableLValue&) { return false; }
84   friend constexpr bool operator==(NonConstComparableLValue&, const NonConstComparableLValue&) { return true; }
85 };
86 
87 constexpr bool test() {
88   test_iterators<int*>();
89   test_iterators<const int*>();
90   test_iterators<cpp20_input_iterator<int*>, sentinel_wrapper<cpp20_input_iterator<int*>>>();
91   test_iterators<bidirectional_iterator<int*>>();
92   test_iterators<forward_iterator<int*>>();
93   test_iterators<random_access_iterator<int*>>();
94   test_iterators<contiguous_iterator<int*>>();
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_not(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_not(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_not(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_not(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   { // check that ranges::dangling is returned
150     [[maybe_unused]] std::same_as<std::ranges::dangling> auto ret =
151       std::ranges::find_if_not(std::array{1, 2}, [](int){ return true; });
152   }
153 
154   { // check that an iterator is returned with a borrowing range
155     int a[] = {1, 2, 3, 4};
156     std::same_as<int*> auto ret = std::ranges::find_if_not(std::views::all(a), [](int){ return false; });
157     assert(ret == a);
158     assert(*ret == 1);
159   }
160 
161   { // check that std::invoke is used
162     struct S { int i; };
163     S a[] = { S{1}, S{3}, S{2} };
164     std::same_as<S*> auto ret = std::ranges::find_if_not(a, [](int) { return true; }, &S::i);
165     assert(ret == a + 3);
166   }
167 
168   { // count projection and predicate invocation count
169     {
170       int a[] = {1, 2, 3, 4};
171       int predicate_count = 0;
172       int projection_count = 0;
173       auto ret = std::ranges::find_if_not(a, a + 4,
174                                       [&](int i) { ++predicate_count; return i != 2; },
175                                       [&](int i) { ++projection_count; return i; });
176       assert(ret == a + 1);
177       assert(*ret == 2);
178       assert(predicate_count == 2);
179       assert(projection_count == 2);
180     }
181     {
182       int a[] = {1, 2, 3, 4};
183       int predicate_count = 0;
184       int projection_count = 0;
185       auto ret = std::ranges::find_if_not(a,
186                                       [&](int i) { ++predicate_count; return i != 2; },
187                                       [&](int i) { ++projection_count; return i; });
188       assert(ret == a + 1);
189       assert(*ret == 2);
190       assert(predicate_count == 2);
191       assert(projection_count == 2);
192     }
193   }
194 
195   { // check that the return type of `iter::operator*` doesn't change
196     {
197       NonConstComparableLValue a[] = { NonConstComparableLValue{} };
198       auto ret = std::ranges::find_if_not(a, a + 1, [](auto&& e) { return e != NonConstComparableLValue{}; });
199       assert(ret == a);
200     }
201     {
202       NonConstComparableLValue a[] = { NonConstComparableLValue{} };
203       auto ret = std::ranges::find_if_not(a, [](auto&& e) { return e != NonConstComparableLValue{}; });
204       assert(ret == a);
205     }
206   }
207 
208   {
209     // check that an empty range works
210     {
211       std::array<int ,0> a = {};
212       auto ret = std::ranges::find_if_not(a.begin(), a.end(), [](int) { return true; });
213       assert(ret == a.begin());
214     }
215     {
216       std::array<int, 0> a = {};
217       auto ret = std::ranges::find_if_not(a, [](int) { return true; });
218       assert(ret == a.begin());
219     }
220   }
221 
222   {
223     // check that the implicit conversion to bool works
224     {
225       int a[] = {1, 2, 3, 4};
226       auto ret = std::ranges::find_if_not(a, a + 4, [](const int& i) { return BooleanTestable{i != 3}; });
227       assert(ret == a + 2);
228     }
229     {
230       int a[] = {1, 2, 3, 4};
231       auto ret = std::ranges::find_if_not(a, [](const int& b) { return BooleanTestable{b != 3}; });
232       assert(ret == a + 2);
233     }
234   }
235 
236   return true;
237 }
238 
239 int main(int, char**) {
240   test();
241   static_assert(test());
242 
243   return 0;
244 }
245