xref: /llvm-project/libcxx/test/std/algorithms/alg.nonmodifying/alg.find.end/find_end_pred.pass.cpp (revision 2df59c50688c122bbcae7467d3eaf862c3ea3088)
15a83710eSEric Fiselier //===----------------------------------------------------------------------===//
25a83710eSEric Fiselier //
357b08b09SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
457b08b09SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
557b08b09SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
65a83710eSEric Fiselier //
75a83710eSEric Fiselier //===----------------------------------------------------------------------===//
85a83710eSEric Fiselier 
95a83710eSEric Fiselier // <algorithm>
105a83710eSEric Fiselier 
115a83710eSEric Fiselier // template<ForwardIterator Iter1, ForwardIterator Iter2,
125a83710eSEric Fiselier //          Predicate<auto, Iter1::value_type, Iter2::value_type> Pred>
135a83710eSEric Fiselier //   requires CopyConstructible<Pred>
148694428eSMarshall Clow //   constexpr Iter1  // constexpr after C++17
155a83710eSEric Fiselier //   find_end(Iter1 first1, Iter1 last1, Iter2 first2, Iter2 last2, Pred pred);
165a83710eSEric Fiselier 
175a83710eSEric Fiselier #include <algorithm>
188d221b40SMarshall Clow #include <functional>
195a83710eSEric Fiselier #include <cassert>
205a83710eSEric Fiselier 
218694428eSMarshall Clow #include "test_macros.h"
225a83710eSEric Fiselier #include "test_iterators.h"
235a83710eSEric Fiselier 
245a83710eSEric Fiselier struct count_equal
255a83710eSEric Fiselier {
265a83710eSEric Fiselier     static unsigned count;
275a83710eSEric Fiselier     template <class T>
operator ()count_equal28da97ec6cSMarshall Clow     TEST_CONSTEXPR_CXX14 bool operator()(const T& x, const T& y)
295a83710eSEric Fiselier         {++count; return x == y;}
305a83710eSEric Fiselier };
315a83710eSEric Fiselier 
328694428eSMarshall Clow #if TEST_STD_VER > 17
test_constexpr()33decf22e5SEric Fiselier constexpr bool test_constexpr() {
348694428eSMarshall Clow     int ia[] = {0, 1, 2};
358694428eSMarshall Clow     int ib[] = {4, 5, 6};
368694428eSMarshall Clow     int ic[] = {0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 0, 1, 2, 3, 0, 1, 2, 0, 1, 0};
378694428eSMarshall Clow     typedef forward_iterator<int*>       FI;
388694428eSMarshall Clow     typedef bidirectional_iterator<int*> BI;
398694428eSMarshall Clow     typedef random_access_iterator<int*> RI;
40decf22e5SEric Fiselier     std::equal_to<int> eq{};
418694428eSMarshall Clow     return    (std::find_end(FI(std::begin(ic)), FI(std::end(ic)), FI(std::begin(ia)), FI(std::end(ia)), eq) == FI(ic+15))
428694428eSMarshall Clow            && (std::find_end(FI(std::begin(ic)), FI(std::end(ic)), FI(std::begin(ib)), FI(std::end(ib)), eq) == FI(std::end(ic)))
438694428eSMarshall Clow            && (std::find_end(BI(std::begin(ic)), BI(std::end(ic)), BI(std::begin(ia)), BI(std::end(ia)), eq) == BI(ic+15))
448694428eSMarshall Clow            && (std::find_end(BI(std::begin(ic)), BI(std::end(ic)), BI(std::begin(ib)), BI(std::end(ib)), eq) == BI(std::end(ic)))
458694428eSMarshall Clow            && (std::find_end(RI(std::begin(ic)), RI(std::end(ic)), RI(std::begin(ia)), RI(std::end(ia)), eq) == RI(ic+15))
468694428eSMarshall Clow            && (std::find_end(RI(std::begin(ic)), RI(std::end(ic)), RI(std::begin(ib)), RI(std::end(ib)), eq) == RI(std::end(ic)))
478694428eSMarshall Clow            ;
488694428eSMarshall Clow     }
498694428eSMarshall Clow #endif
508694428eSMarshall Clow 
515a83710eSEric Fiselier unsigned count_equal::count = 0;
525a83710eSEric Fiselier 
535a83710eSEric Fiselier template <class Iter1, class Iter2>
545a83710eSEric Fiselier void
test()555a83710eSEric Fiselier test()
565a83710eSEric Fiselier {
575a83710eSEric Fiselier     int ia[] = {0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 0, 1, 2, 3, 0, 1, 2, 0, 1, 0};
585a83710eSEric Fiselier     const unsigned sa = sizeof(ia)/sizeof(ia[0]);
595a83710eSEric Fiselier     int b[] = {0};
605a83710eSEric Fiselier     count_equal::count = 0;
615a83710eSEric Fiselier     assert(std::find_end(Iter1(ia), Iter1(ia+sa), Iter2(b), Iter2(b+1), count_equal()) == Iter1(ia+sa-1));
625a83710eSEric Fiselier     assert(count_equal::count <= 1*(sa-1+1));
635a83710eSEric Fiselier     int c[] = {0, 1};
645a83710eSEric Fiselier     count_equal::count = 0;
655a83710eSEric Fiselier     assert(std::find_end(Iter1(ia), Iter1(ia+sa), Iter2(c), Iter2(c+2), count_equal()) == Iter1(ia+18));
665a83710eSEric Fiselier     assert(count_equal::count <= 2*(sa-2+1));
675a83710eSEric Fiselier     int d[] = {0, 1, 2};
685a83710eSEric Fiselier     count_equal::count = 0;
695a83710eSEric Fiselier     assert(std::find_end(Iter1(ia), Iter1(ia+sa), Iter2(d), Iter2(d+3), count_equal()) == Iter1(ia+15));
705a83710eSEric Fiselier     assert(count_equal::count <= 3*(sa-3+1));
715a83710eSEric Fiselier     int e[] = {0, 1, 2, 3};
725a83710eSEric Fiselier     count_equal::count = 0;
735a83710eSEric Fiselier     assert(std::find_end(Iter1(ia), Iter1(ia+sa), Iter2(e), Iter2(e+4), count_equal()) == Iter1(ia+11));
745a83710eSEric Fiselier     assert(count_equal::count <= 4*(sa-4+1));
755a83710eSEric Fiselier     int f[] = {0, 1, 2, 3, 4};
765a83710eSEric Fiselier     count_equal::count = 0;
775a83710eSEric Fiselier     assert(std::find_end(Iter1(ia), Iter1(ia+sa), Iter2(f), Iter2(f+5), count_equal()) == Iter1(ia+6));
785a83710eSEric Fiselier     assert(count_equal::count <= 5*(sa-5+1));
795a83710eSEric Fiselier     int g[] = {0, 1, 2, 3, 4, 5};
805a83710eSEric Fiselier     count_equal::count = 0;
815a83710eSEric Fiselier     assert(std::find_end(Iter1(ia), Iter1(ia+sa), Iter2(g), Iter2(g+6), count_equal()) == Iter1(ia));
825a83710eSEric Fiselier     assert(count_equal::count <= 6*(sa-6+1));
835a83710eSEric Fiselier     int h[] = {0, 1, 2, 3, 4, 5, 6};
845a83710eSEric Fiselier     count_equal::count = 0;
855a83710eSEric Fiselier     assert(std::find_end(Iter1(ia), Iter1(ia+sa), Iter2(h), Iter2(h+7), count_equal()) == Iter1(ia+sa));
865a83710eSEric Fiselier     assert(count_equal::count <= 7*(sa-7+1));
875a83710eSEric Fiselier     count_equal::count = 0;
885a83710eSEric Fiselier     assert(std::find_end(Iter1(ia), Iter1(ia+sa), Iter2(b), Iter2(b), count_equal()) == Iter1(ia+sa));
895a83710eSEric Fiselier     assert(count_equal::count <= 0);
905a83710eSEric Fiselier     count_equal::count = 0;
915a83710eSEric Fiselier     assert(std::find_end(Iter1(ia), Iter1(ia), Iter2(b), Iter2(b+1), count_equal()) == Iter1(ia));
925a83710eSEric Fiselier     assert(count_equal::count <= 0);
935a83710eSEric Fiselier }
945a83710eSEric Fiselier 
main(int,char **)95*2df59c50SJF Bastien int main(int, char**)
965a83710eSEric Fiselier {
975a83710eSEric Fiselier     test<forward_iterator<const int*>, forward_iterator<const int*> >();
985a83710eSEric Fiselier     test<forward_iterator<const int*>, bidirectional_iterator<const int*> >();
995a83710eSEric Fiselier     test<forward_iterator<const int*>, random_access_iterator<const int*> >();
1005a83710eSEric Fiselier     test<bidirectional_iterator<const int*>, forward_iterator<const int*> >();
1015a83710eSEric Fiselier     test<bidirectional_iterator<const int*>, bidirectional_iterator<const int*> >();
1025a83710eSEric Fiselier     test<bidirectional_iterator<const int*>, random_access_iterator<const int*> >();
1035a83710eSEric Fiselier     test<random_access_iterator<const int*>, forward_iterator<const int*> >();
1045a83710eSEric Fiselier     test<random_access_iterator<const int*>, bidirectional_iterator<const int*> >();
1055a83710eSEric Fiselier     test<random_access_iterator<const int*>, random_access_iterator<const int*> >();
1068694428eSMarshall Clow 
1078694428eSMarshall Clow #if TEST_STD_VER > 17
1088694428eSMarshall Clow     static_assert(test_constexpr());
1098694428eSMarshall Clow #endif
110*2df59c50SJF Bastien 
111*2df59c50SJF Bastien   return 0;
1125a83710eSEric Fiselier }
113