xref: /llvm-project/libcxx/test/std/algorithms/alg.nonmodifying/alg.find.end/find_end.pass.cpp (revision 2df59c50688c122bbcae7467d3eaf862c3ea3088)
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 //   requires HasEqualTo<Iter1::value_type, Iter2::value_type>
13 //   constexpr Iter1  // constexpr after C++17
14 //   find_end(Iter1 first1, Iter1 last1, Iter2 first2, Iter2 last2);
15 
16 #include <algorithm>
17 #include <cassert>
18 
19 #include "test_macros.h"
20 #include "test_iterators.h"
21 
22 #if TEST_STD_VER > 17
test_constexpr()23 TEST_CONSTEXPR bool test_constexpr() {
24     int ia[] = {0, 1, 2};
25     int ib[] = {4, 5, 6};
26     int ic[] = {0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 0, 1, 2, 3, 0, 1, 2, 0, 1, 0};
27     typedef forward_iterator<int*>       FI;
28     typedef bidirectional_iterator<int*> BI;
29     typedef random_access_iterator<int*> RI;
30 
31     return    (std::find_end(FI(std::begin(ic)), FI(std::end(ic)), FI(std::begin(ia)), FI(std::end(ia))) == FI(ic+15))
32            && (std::find_end(FI(std::begin(ic)), FI(std::end(ic)), FI(std::begin(ib)), FI(std::end(ib))) == FI(std::end(ic)))
33            && (std::find_end(BI(std::begin(ic)), BI(std::end(ic)), BI(std::begin(ia)), BI(std::end(ia))) == BI(ic+15))
34            && (std::find_end(BI(std::begin(ic)), BI(std::end(ic)), BI(std::begin(ib)), BI(std::end(ib))) == BI(std::end(ic)))
35            && (std::find_end(RI(std::begin(ic)), RI(std::end(ic)), RI(std::begin(ia)), RI(std::end(ia))) == RI(ic+15))
36            && (std::find_end(RI(std::begin(ic)), RI(std::end(ic)), RI(std::begin(ib)), RI(std::end(ib))) == RI(std::end(ic)))
37            ;
38     }
39 #endif
40 
41 template <class Iter1, class Iter2>
42 void
test()43 test()
44 {
45     int ia[] = {0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 0, 1, 2, 3, 0, 1, 2, 0, 1, 0};
46     const unsigned sa = sizeof(ia)/sizeof(ia[0]);
47     int b[] = {0};
48     assert(std::find_end(Iter1(ia), Iter1(ia+sa), Iter2(b), Iter2(b+1)) == Iter1(ia+sa-1));
49     int c[] = {0, 1};
50     assert(std::find_end(Iter1(ia), Iter1(ia+sa), Iter2(c), Iter2(c+2)) == Iter1(ia+18));
51     int d[] = {0, 1, 2};
52     assert(std::find_end(Iter1(ia), Iter1(ia+sa), Iter2(d), Iter2(d+3)) == Iter1(ia+15));
53     int e[] = {0, 1, 2, 3};
54     assert(std::find_end(Iter1(ia), Iter1(ia+sa), Iter2(e), Iter2(e+4)) == Iter1(ia+11));
55     int f[] = {0, 1, 2, 3, 4};
56     assert(std::find_end(Iter1(ia), Iter1(ia+sa), Iter2(f), Iter2(f+5)) == Iter1(ia+6));
57     int g[] = {0, 1, 2, 3, 4, 5};
58     assert(std::find_end(Iter1(ia), Iter1(ia+sa), Iter2(g), Iter2(g+6)) == Iter1(ia));
59     int h[] = {0, 1, 2, 3, 4, 5, 6};
60     assert(std::find_end(Iter1(ia), Iter1(ia+sa), Iter2(h), Iter2(h+7)) == Iter1(ia+sa));
61     assert(std::find_end(Iter1(ia), Iter1(ia+sa), Iter2(b), Iter2(b)) == Iter1(ia+sa));
62     assert(std::find_end(Iter1(ia), Iter1(ia), Iter2(b), Iter2(b+1)) == Iter1(ia));
63 }
64 
main(int,char **)65 int main(int, char**)
66 {
67     test<forward_iterator<const int*>, forward_iterator<const int*> >();
68     test<forward_iterator<const int*>, bidirectional_iterator<const int*> >();
69     test<forward_iterator<const int*>, random_access_iterator<const int*> >();
70     test<bidirectional_iterator<const int*>, forward_iterator<const int*> >();
71     test<bidirectional_iterator<const int*>, bidirectional_iterator<const int*> >();
72     test<bidirectional_iterator<const int*>, random_access_iterator<const int*> >();
73     test<random_access_iterator<const int*>, forward_iterator<const int*> >();
74     test<random_access_iterator<const int*>, bidirectional_iterator<const int*> >();
75     test<random_access_iterator<const int*>, random_access_iterator<const int*> >();
76 
77 #if TEST_STD_VER > 17
78     static_assert(test_constexpr());
79 #endif
80 
81   return 0;
82 }
83