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