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 // template<ForwardIterator Iter1, ForwardIterator Iter2,
12 //          Predicate<auto, Iter1::value_type, Iter2::value_type> Pred>
13 //   requires CopyConstructible<Pred>
14 //   constexpr Iter1  // constexpr after C++17
15 //   find_end(Iter1 first1, Iter1 last1, Iter2 first2, Iter2 last2, Pred pred);
16 
17 #include <algorithm>
18 #include <functional>
19 #include <cassert>
20 
21 #include "test_macros.h"
22 #include "test_iterators.h"
23 
24 struct count_equal
25 {
26     static unsigned count;
27     template <class T>
operator ()count_equal28     TEST_CONSTEXPR_CXX14 bool operator()(const T& x, const T& y)
29         {++count; return x == y;}
30 };
31 
32 #if TEST_STD_VER > 17
test_constexpr()33 constexpr bool test_constexpr() {
34     int ia[] = {0, 1, 2};
35     int ib[] = {4, 5, 6};
36     int ic[] = {0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 0, 1, 2, 3, 0, 1, 2, 0, 1, 0};
37     typedef forward_iterator<int*>       FI;
38     typedef bidirectional_iterator<int*> BI;
39     typedef random_access_iterator<int*> RI;
40     std::equal_to<int> eq{};
41     return    (std::find_end(FI(std::begin(ic)), FI(std::end(ic)), FI(std::begin(ia)), FI(std::end(ia)), eq) == FI(ic+15))
42            && (std::find_end(FI(std::begin(ic)), FI(std::end(ic)), FI(std::begin(ib)), FI(std::end(ib)), eq) == FI(std::end(ic)))
43            && (std::find_end(BI(std::begin(ic)), BI(std::end(ic)), BI(std::begin(ia)), BI(std::end(ia)), eq) == BI(ic+15))
44            && (std::find_end(BI(std::begin(ic)), BI(std::end(ic)), BI(std::begin(ib)), BI(std::end(ib)), eq) == BI(std::end(ic)))
45            && (std::find_end(RI(std::begin(ic)), RI(std::end(ic)), RI(std::begin(ia)), RI(std::end(ia)), eq) == RI(ic+15))
46            && (std::find_end(RI(std::begin(ic)), RI(std::end(ic)), RI(std::begin(ib)), RI(std::end(ib)), eq) == RI(std::end(ic)))
47            ;
48     }
49 #endif
50 
51 unsigned count_equal::count = 0;
52 
53 template <class Iter1, class Iter2>
54 void
test()55 test()
56 {
57     int ia[] = {0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 0, 1, 2, 3, 0, 1, 2, 0, 1, 0};
58     const unsigned sa = sizeof(ia)/sizeof(ia[0]);
59     int b[] = {0};
60     count_equal::count = 0;
61     assert(std::find_end(Iter1(ia), Iter1(ia+sa), Iter2(b), Iter2(b+1), count_equal()) == Iter1(ia+sa-1));
62     assert(count_equal::count <= 1*(sa-1+1));
63     int c[] = {0, 1};
64     count_equal::count = 0;
65     assert(std::find_end(Iter1(ia), Iter1(ia+sa), Iter2(c), Iter2(c+2), count_equal()) == Iter1(ia+18));
66     assert(count_equal::count <= 2*(sa-2+1));
67     int d[] = {0, 1, 2};
68     count_equal::count = 0;
69     assert(std::find_end(Iter1(ia), Iter1(ia+sa), Iter2(d), Iter2(d+3), count_equal()) == Iter1(ia+15));
70     assert(count_equal::count <= 3*(sa-3+1));
71     int e[] = {0, 1, 2, 3};
72     count_equal::count = 0;
73     assert(std::find_end(Iter1(ia), Iter1(ia+sa), Iter2(e), Iter2(e+4), count_equal()) == Iter1(ia+11));
74     assert(count_equal::count <= 4*(sa-4+1));
75     int f[] = {0, 1, 2, 3, 4};
76     count_equal::count = 0;
77     assert(std::find_end(Iter1(ia), Iter1(ia+sa), Iter2(f), Iter2(f+5), count_equal()) == Iter1(ia+6));
78     assert(count_equal::count <= 5*(sa-5+1));
79     int g[] = {0, 1, 2, 3, 4, 5};
80     count_equal::count = 0;
81     assert(std::find_end(Iter1(ia), Iter1(ia+sa), Iter2(g), Iter2(g+6), count_equal()) == Iter1(ia));
82     assert(count_equal::count <= 6*(sa-6+1));
83     int h[] = {0, 1, 2, 3, 4, 5, 6};
84     count_equal::count = 0;
85     assert(std::find_end(Iter1(ia), Iter1(ia+sa), Iter2(h), Iter2(h+7), count_equal()) == Iter1(ia+sa));
86     assert(count_equal::count <= 7*(sa-7+1));
87     count_equal::count = 0;
88     assert(std::find_end(Iter1(ia), Iter1(ia+sa), Iter2(b), Iter2(b), count_equal()) == Iter1(ia+sa));
89     assert(count_equal::count <= 0);
90     count_equal::count = 0;
91     assert(std::find_end(Iter1(ia), Iter1(ia), Iter2(b), Iter2(b+1), count_equal()) == Iter1(ia));
92     assert(count_equal::count <= 0);
93 }
94 
main(int,char **)95 int main(int, char**)
96 {
97     test<forward_iterator<const int*>, forward_iterator<const int*> >();
98     test<forward_iterator<const int*>, bidirectional_iterator<const int*> >();
99     test<forward_iterator<const int*>, random_access_iterator<const int*> >();
100     test<bidirectional_iterator<const int*>, forward_iterator<const int*> >();
101     test<bidirectional_iterator<const int*>, bidirectional_iterator<const int*> >();
102     test<bidirectional_iterator<const int*>, random_access_iterator<const int*> >();
103     test<random_access_iterator<const int*>, forward_iterator<const int*> >();
104     test<random_access_iterator<const int*>, bidirectional_iterator<const int*> >();
105     test<random_access_iterator<const int*>, random_access_iterator<const int*> >();
106 
107 #if TEST_STD_VER > 17
108     static_assert(test_constexpr());
109 #endif
110 
111   return 0;
112 }
113