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