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