xref: /llvm-project/libcxx/test/std/algorithms/alg.nonmodifying/alg.find.end/find_end.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 //   requires HasEqualTo<Iter1::value_type, Iter2::value_type>
138694428eSMarshall Clow //   constexpr Iter1  // constexpr after C++17
145a83710eSEric Fiselier //   find_end(Iter1 first1, Iter1 last1, Iter2 first2, Iter2 last2);
155a83710eSEric Fiselier 
165a83710eSEric Fiselier #include <algorithm>
175a83710eSEric Fiselier #include <cassert>
185a83710eSEric Fiselier 
198694428eSMarshall Clow #include "test_macros.h"
205a83710eSEric Fiselier #include "test_iterators.h"
215a83710eSEric Fiselier 
228694428eSMarshall Clow #if TEST_STD_VER > 17
test_constexpr()238694428eSMarshall Clow TEST_CONSTEXPR bool test_constexpr() {
248694428eSMarshall Clow     int ia[] = {0, 1, 2};
258694428eSMarshall Clow     int ib[] = {4, 5, 6};
268694428eSMarshall Clow     int ic[] = {0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 0, 1, 2, 3, 0, 1, 2, 0, 1, 0};
278694428eSMarshall Clow     typedef forward_iterator<int*>       FI;
288694428eSMarshall Clow     typedef bidirectional_iterator<int*> BI;
298694428eSMarshall Clow     typedef random_access_iterator<int*> RI;
308694428eSMarshall Clow 
318694428eSMarshall Clow     return    (std::find_end(FI(std::begin(ic)), FI(std::end(ic)), FI(std::begin(ia)), FI(std::end(ia))) == FI(ic+15))
328694428eSMarshall Clow            && (std::find_end(FI(std::begin(ic)), FI(std::end(ic)), FI(std::begin(ib)), FI(std::end(ib))) == FI(std::end(ic)))
338694428eSMarshall Clow            && (std::find_end(BI(std::begin(ic)), BI(std::end(ic)), BI(std::begin(ia)), BI(std::end(ia))) == BI(ic+15))
348694428eSMarshall Clow            && (std::find_end(BI(std::begin(ic)), BI(std::end(ic)), BI(std::begin(ib)), BI(std::end(ib))) == BI(std::end(ic)))
358694428eSMarshall Clow            && (std::find_end(RI(std::begin(ic)), RI(std::end(ic)), RI(std::begin(ia)), RI(std::end(ia))) == RI(ic+15))
368694428eSMarshall Clow            && (std::find_end(RI(std::begin(ic)), RI(std::end(ic)), RI(std::begin(ib)), RI(std::end(ib))) == RI(std::end(ic)))
378694428eSMarshall Clow            ;
388694428eSMarshall Clow     }
398694428eSMarshall Clow #endif
408694428eSMarshall Clow 
415a83710eSEric Fiselier template <class Iter1, class Iter2>
425a83710eSEric Fiselier void
test()435a83710eSEric Fiselier test()
445a83710eSEric Fiselier {
455a83710eSEric Fiselier     int ia[] = {0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 0, 1, 2, 3, 0, 1, 2, 0, 1, 0};
465a83710eSEric Fiselier     const unsigned sa = sizeof(ia)/sizeof(ia[0]);
475a83710eSEric Fiselier     int b[] = {0};
485a83710eSEric Fiselier     assert(std::find_end(Iter1(ia), Iter1(ia+sa), Iter2(b), Iter2(b+1)) == Iter1(ia+sa-1));
495a83710eSEric Fiselier     int c[] = {0, 1};
505a83710eSEric Fiselier     assert(std::find_end(Iter1(ia), Iter1(ia+sa), Iter2(c), Iter2(c+2)) == Iter1(ia+18));
515a83710eSEric Fiselier     int d[] = {0, 1, 2};
525a83710eSEric Fiselier     assert(std::find_end(Iter1(ia), Iter1(ia+sa), Iter2(d), Iter2(d+3)) == Iter1(ia+15));
535a83710eSEric Fiselier     int e[] = {0, 1, 2, 3};
545a83710eSEric Fiselier     assert(std::find_end(Iter1(ia), Iter1(ia+sa), Iter2(e), Iter2(e+4)) == Iter1(ia+11));
555a83710eSEric Fiselier     int f[] = {0, 1, 2, 3, 4};
565a83710eSEric Fiselier     assert(std::find_end(Iter1(ia), Iter1(ia+sa), Iter2(f), Iter2(f+5)) == Iter1(ia+6));
575a83710eSEric Fiselier     int g[] = {0, 1, 2, 3, 4, 5};
585a83710eSEric Fiselier     assert(std::find_end(Iter1(ia), Iter1(ia+sa), Iter2(g), Iter2(g+6)) == Iter1(ia));
595a83710eSEric Fiselier     int h[] = {0, 1, 2, 3, 4, 5, 6};
605a83710eSEric Fiselier     assert(std::find_end(Iter1(ia), Iter1(ia+sa), Iter2(h), Iter2(h+7)) == Iter1(ia+sa));
615a83710eSEric Fiselier     assert(std::find_end(Iter1(ia), Iter1(ia+sa), Iter2(b), Iter2(b)) == Iter1(ia+sa));
625a83710eSEric Fiselier     assert(std::find_end(Iter1(ia), Iter1(ia), Iter2(b), Iter2(b+1)) == Iter1(ia));
635a83710eSEric Fiselier }
645a83710eSEric Fiselier 
main(int,char **)65*2df59c50SJF Bastien int main(int, char**)
665a83710eSEric Fiselier {
675a83710eSEric Fiselier     test<forward_iterator<const int*>, forward_iterator<const int*> >();
685a83710eSEric Fiselier     test<forward_iterator<const int*>, bidirectional_iterator<const int*> >();
695a83710eSEric Fiselier     test<forward_iterator<const int*>, random_access_iterator<const int*> >();
705a83710eSEric Fiselier     test<bidirectional_iterator<const int*>, forward_iterator<const int*> >();
715a83710eSEric Fiselier     test<bidirectional_iterator<const int*>, bidirectional_iterator<const int*> >();
725a83710eSEric Fiselier     test<bidirectional_iterator<const int*>, random_access_iterator<const int*> >();
735a83710eSEric Fiselier     test<random_access_iterator<const int*>, forward_iterator<const int*> >();
745a83710eSEric Fiselier     test<random_access_iterator<const int*>, bidirectional_iterator<const int*> >();
755a83710eSEric Fiselier     test<random_access_iterator<const int*>, random_access_iterator<const int*> >();
768694428eSMarshall Clow 
778694428eSMarshall Clow #if TEST_STD_VER > 17
788694428eSMarshall Clow     static_assert(test_constexpr());
798694428eSMarshall Clow #endif
80*2df59c50SJF Bastien 
81*2df59c50SJF Bastien   return 0;
825a83710eSEric Fiselier }
83