Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / test / std / algorithms / alg.nonmodifying / alg.find.first.of / ranges.find_first_of.pass.cpp
blobcea30420428cd558b5618b1a85139ce1a2559b04
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 I1, sentinel_for<I1> S1, forward_iterator I2, sentinel_for<I2> S2,
14 // class Pred = ranges::equal_to, class Proj1 = identity, class Proj2 = identity>
15 // requires indirectly_comparable<I1, I2, Pred, Proj1, Proj2>
16 // constexpr I1 ranges::find_first_of(I1 first1, S1 last1, I2 first2, S2 last2,
17 // Pred pred = {},
18 // Proj1 proj1 = {}, Proj2 proj2 = {});
19 // template<input_range R1, forward_range R2,
20 // class Pred = ranges::equal_to, class Proj1 = identity, class Proj2 = identity>
21 // requires indirectly_comparable<iterator_t<R1>, iterator_t<R2>, Pred, Proj1, Proj2>
22 // constexpr borrowed_iterator_t<R1>
23 // ranges::find_first_of(R1&& r1, R2&& r2,
24 // Pred pred = {},
25 // Proj1 proj1 = {}, Proj2 proj2 = {});
27 #include <algorithm>
28 #include <array>
29 #include <functional>
30 #include <ranges>
32 #include "almost_satisfies_types.h"
33 #include "boolean_testable.h"
34 #include "test_iterators.h"
36 template <class Iter1, class Iter2 = int*, class Sent1 = Iter1, class Sent2 = Iter2>
37 concept HasFindFirstOfIt = requires(Iter1 iter1, Sent1 sent1, Iter2 iter2, Sent2 sent2) {
38 std::ranges::find_first_of(iter1, sent1, iter2, sent2);
41 static_assert(HasFindFirstOfIt<int*>);
42 static_assert(!HasFindFirstOfIt<InputIteratorNotDerivedFrom>);
43 static_assert(!HasFindFirstOfIt<InputIteratorNotIndirectlyReadable>);
44 static_assert(!HasFindFirstOfIt<InputIteratorNotInputOrOutputIterator>);
45 static_assert(!HasFindFirstOfIt<int*, ForwardIteratorNotDerivedFrom>);
46 static_assert(!HasFindFirstOfIt<int*, ForwardIteratorNotIncrementable>);
47 static_assert(!HasFindFirstOfIt<int*, int*, SentinelForNotSemiregular>);
48 static_assert(!HasFindFirstOfIt<int*, int*, SentinelForNotWeaklyEqualityComparableWith>);
49 static_assert(!HasFindFirstOfIt<int*, int*, int*, SentinelForNotSemiregular>);
50 static_assert(!HasFindFirstOfIt<int*, int*, int*, SentinelForNotWeaklyEqualityComparableWith>);
51 static_assert(!HasFindFirstOfIt<int*, int**>); // not indirectly_comparable
53 template <class Range1, class Range2 = UncheckedRange<int*>>
54 concept HasFindFirstOfR = requires(Range1 range1, Range2 range2) {
55 std::ranges::find_first_of(range1, range2);
58 static_assert(HasFindFirstOfR<UncheckedRange<int*>>);
59 static_assert(!HasFindFirstOfR<InputRangeNotDerivedFrom>);
60 static_assert(!HasFindFirstOfR<InputRangeNotIndirectlyReadable>);
61 static_assert(!HasFindFirstOfR<InputRangeNotInputOrOutputIterator>);
62 static_assert(!HasFindFirstOfR<UncheckedRange<int*>, ForwardRangeNotDerivedFrom>);
63 static_assert(!HasFindFirstOfR<UncheckedRange<int*>, ForwardRangeNotIncrementable>);
64 static_assert(!HasFindFirstOfR<UncheckedRange<int*>, InputRangeNotSentinelSemiregular>);
65 static_assert(!HasFindFirstOfR<UncheckedRange<int*>, InputRangeNotSentinelEqualityComparableWith>);
66 static_assert(!HasFindFirstOfR<UncheckedRange<int*>, ForwardRangeNotSentinelSemiregular>);
67 static_assert(!HasFindFirstOfR<UncheckedRange<int*>, ForwardRangeNotSentinelEqualityComparableWith>);
68 static_assert(!HasFindFirstOfR<UncheckedRange<int*>, UncheckedRange<int**>>); // not indirectly_comparable
70 template <int N1, int N2>
71 struct Data {
72 std::array<int, N1> input1;
73 std::array<int, N2> input2;
74 std::ptrdiff_t expected;
77 template <class Iter1, class Sent1, class Iter2, class Sent2, int N1, int N2>
78 constexpr void test(Data<N1, N2> d) {
80 std::same_as<Iter1> decltype(auto) ret =
81 std::ranges::find_first_of(Iter1(d.input1.data()), Sent1(Iter1(d.input1.data() + d.input1.size())),
82 Iter2(d.input2.data()), Sent2(Iter2(d.input2.data() + d.input2.size())));
83 assert(base(ret) == d.input1.data() + d.expected);
86 auto range1 = std::ranges::subrange(Iter1(d.input1.data()), Sent1(Iter1(d.input1.data() + d.input1.size())));
87 auto range2 = std::ranges::subrange(Iter2(d.input2.data()), Sent2(Iter2(d.input2.data() + d.input2.size())));
88 std::same_as<Iter1> decltype(auto) ret = std::ranges::find_first_of(range1, range2);
89 assert(base(ret) == d.input1.data() + d.expected);
93 template <class Iter1, class Sent1, class Iter2, class Sent2 = Iter2>
94 constexpr void test_iterators() {
95 // simple test
96 test<Iter1, Sent1, Iter2, Sent2, 4, 2>({.input1 = {1, 2, 3, 4}, .input2 = {2, 3}, .expected = 1});
97 // other elements from input2 are checked
98 test<Iter1, Sent1, Iter2, Sent2, 4, 2>({.input1 = {1, 2, 3, 4}, .input2 = {3, 2}, .expected = 1});
99 // an empty second range returns last
100 test<Iter1, Sent1, Iter2, Sent2, 4, 0>({.input1 = {1, 2, 3, 4}, .input2 = {}, .expected = 4});
101 // check that an empty first range works
102 test<Iter1, Sent1, Iter2, Sent2, 0, 1>({.input1 = {}, .input2 = {1}, .expected = 0});
103 // check both ranges empty works
104 test<Iter1, Sent1, Iter2, Sent2, 0, 0>({.input1 = {}, .input2 = {}, .expected = 0});
105 // the first element is checked properly
106 test<Iter1, Sent1, Iter2, Sent2, 5, 2>({.input1 = {5, 4, 3, 2, 1}, .input2 = {1, 5}, .expected = 0});
107 // the last element is checked properly
108 test<Iter1, Sent1, Iter2, Sent2, 5, 2>({.input1 = {5, 4, 3, 2, 1}, .input2 = {1, 6}, .expected = 4});
109 // no match, one-past-the-end iterator should be returned
110 test<Iter1, Sent1, Iter2, Sent2, 4, 4>({.input1 = {1, 3, 5, 7}, .input2 = {0, 2, 4, 6}, .expected = 4});
111 // input2 contains a single element
112 test<Iter1, Sent1, Iter2, Sent2, 4, 1>({.input1 = {1, 3, 5, 7}, .input2 = {1}, .expected = 0});
115 template <class Iter1, class Sent1 = Iter1>
116 constexpr void test_iterators1() {
117 test_iterators<Iter1, Sent1, forward_iterator<int*>, sentinel_wrapper<forward_iterator<int*>>>();
118 test_iterators<Iter1, Sent1, forward_iterator<int*>>();
119 test_iterators<Iter1, Sent1, bidirectional_iterator<int*>>();
120 test_iterators<Iter1, Sent1, random_access_iterator<int*>>();
121 test_iterators<Iter1, Sent1, contiguous_iterator<int*>>();
122 test_iterators<Iter1, Sent1, int*>();
125 constexpr bool test() {
126 test_iterators1<cpp20_input_iterator<int*>, sentinel_wrapper<cpp20_input_iterator<int*>>>();
127 test_iterators1<cpp17_input_iterator<int*>, sentinel_wrapper<cpp17_input_iterator<int*>>>();
128 test_iterators1<forward_iterator<int*>>();
129 test_iterators1<bidirectional_iterator<int*>>();
130 test_iterators1<random_access_iterator<int*>>();
131 test_iterators1<contiguous_iterator<int*>>();
132 test_iterators1<int*>();
134 { // check that std::ranges::dangling is returned
135 [[maybe_unused]] std::same_as<std::ranges::dangling> decltype(auto) ret =
136 std::ranges::find_first_of(std::array {1}, std::array {1});
139 { // check that the predicate is used
140 int a[] = {1, 2, 3, 4};
141 int b[] = {2};
143 auto ret = std::ranges::find_first_of(std::begin(a), std::end(a),
144 std::begin(b), std::end(b),
145 std::ranges::greater{});
146 assert(ret == a + 2);
149 auto ret = std::ranges::find_first_of(a, b, std::ranges::greater{});
150 assert(ret == a + 2);
154 { // check that the projections are used
155 int a[] = {1, 2, 3, 4};
156 int b[] = {4};
158 auto ret = std::ranges::find_first_of(std::begin(a), std::end(a),
159 std::begin(b), std::end(b), {},
160 [](int i) { return i / 2; },
161 [](int i) { return i - 3; });
162 assert(ret == a + 1);
165 auto ret = std::ranges::find_first_of(a, b, {}, [](int i) { return i / 2; }, [](int i) { return i - 3; });
166 assert(ret == a + 1);
170 { // check that std::invoke is used
171 struct S1 {
172 constexpr S1(int i_) : i(i_) {}
173 constexpr bool compare(int j) const { return j == i; }
174 constexpr const S1& identity() const { return *this; }
175 int i;
177 struct S2 {
178 constexpr S2(int i_) : i(i_) {}
179 int i;
183 S1 a[] = {1, 2, 3, 4};
184 S2 b[] = {2, 3};
185 auto ret = std::ranges::find_first_of(std::begin(a), std::end(a),
186 std::begin(b), std::end(b), &S1::compare, &S1::identity, &S2::i);
187 assert(ret == a + 1);
190 S1 a[] = {1, 2, 3, 4};
191 S2 b[] = {2, 3};
192 auto ret = std::ranges::find_first_of(a, b, &S1::compare, &S1::identity, &S2::i);
193 assert(ret == a + 1);
197 { // check that the implicit conversion to bool works
198 StrictComparable<int> a[] = {1, 2, 3, 4};
199 StrictComparable<int> b[] = {2, 3};
201 auto ret = std::ranges::find_first_of(a, std::end(a), b, std::end(b));
202 assert(ret == a + 1);
205 auto ret = std::ranges::find_first_of(a, b);
206 assert(ret == a + 1);
210 { // check that the complexity requirements are met
211 int a[] = {1, 2, 3, 4};
212 int b[] = {2, 3};
214 int predCount = 0;
215 auto predCounter = [&](int, int) { ++predCount; return false; };
216 int proj1Count = 0;
217 auto proj1Counter = [&](int i) { ++proj1Count; return i; };
218 int proj2Count = 0;
219 auto proj2Counter = [&](int i) { ++proj2Count; return i; };
220 auto ret = std::ranges::find_first_of(std::begin(a), std::end(a),
221 std::begin(b), std::end(b), predCounter, proj1Counter, proj2Counter);
222 assert(ret == a + 4);
223 assert(predCount <= 8);
224 assert(proj1Count <= 8);
225 assert(proj2Count <= 8);
228 int predCount = 0;
229 auto predCounter = [&](int, int) { ++predCount; return false; };
230 int proj1Count = 0;
231 auto proj1Counter = [&](int i) { ++proj1Count; return i; };
232 int proj2Count = 0;
233 auto proj2Counter = [&](int i) { ++proj2Count; return i; };
234 auto ret = std::ranges::find_first_of(a, b, predCounter, proj1Counter, proj2Counter);
235 assert(ret == a + 4);
236 assert(predCount == 8);
237 assert(proj1Count == 8);
238 assert(proj2Count == 8);
242 return true;
245 int main(int, char**) {
246 test();
247 static_assert(test());
249 return 0;