• 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 //   search(Iter1 first1, Iter1 last1, Iter2 first2, Iter2 last2);
16 //
17 //   template<class ForwardIterator, class Searcher>
18 //   ForwardIterator search(ForwardIterator first, ForwardIterator last,
19 //                          const Searcher& searcher); // C++17
20 
21 #include <algorithm>
22 #include <cassert>
23 
24 #include "test_macros.h"
25 #include "test_iterators.h"
26 
27 struct MySearcherC {
28     template <typename Iterator>
29     std::pair<Iterator, Iterator>
operator ()MySearcherC30     TEST_CONSTEXPR operator() (Iterator b, Iterator e) const
31     {
32         return std::make_pair(b, e);
33     }
34 };
35 
36 #if TEST_STD_VER > 17
test_constexpr()37 TEST_CONSTEXPR bool test_constexpr() {
38     int ia[] = {0, 1, 2, 3};
39     int ib[] = {0, 1, 5, 3};
40     int ic[] = {0, 1, 2, 0, 1, 2, 3, 0, 1, 2, 3, 4};
41     return    (std::search(std::begin(ic), std::end(ic), std::begin(ia), std::end(ia)) == ic+3)
42            && (std::search(std::begin(ic), std::end(ic), std::begin(ib), std::end(ib)) == std::end(ic))
43            && (std::search(std::begin(ic), std::end(ic), MySearcherC()) == std::begin(ic))
44            ;
45     }
46 #endif
47 
48 int searcher_called = 0;
49 
50 struct MySearcher {
51     template <typename Iterator>
52     std::pair<Iterator, Iterator>
operator ()MySearcher53     operator() (Iterator b, Iterator e) const
54     {
55         ++searcher_called;
56         return std::make_pair(b, e);
57     }
58 };
59 
60 template <class Iter1, class Iter2>
61 void
test()62 test()
63 {
64     int ia[] = {0, 1, 2, 3, 4, 5};
65     const unsigned sa = sizeof(ia)/sizeof(ia[0]);
66     assert(std::search(Iter1(ia), Iter1(ia+sa), Iter2(ia), Iter2(ia)) == Iter1(ia));
67     assert(std::search(Iter1(ia), Iter1(ia+sa), Iter2(ia), Iter2(ia+1)) == Iter1(ia));
68     assert(std::search(Iter1(ia), Iter1(ia+sa), Iter2(ia+1), Iter2(ia+2)) == Iter1(ia+1));
69     assert(std::search(Iter1(ia), Iter1(ia+sa), Iter2(ia+2), Iter2(ia+2)) == Iter1(ia));
70     assert(std::search(Iter1(ia), Iter1(ia+sa), Iter2(ia+2), Iter2(ia+3)) == Iter1(ia+2));
71     assert(std::search(Iter1(ia), Iter1(ia+sa), Iter2(ia+2), Iter2(ia+3)) == Iter1(ia+2));
72     assert(std::search(Iter1(ia), Iter1(ia), Iter2(ia+2), Iter2(ia+3)) == Iter1(ia));
73     assert(std::search(Iter1(ia), Iter1(ia+sa), Iter2(ia+sa-1), Iter2(ia+sa)) == Iter1(ia+sa-1));
74     assert(std::search(Iter1(ia), Iter1(ia+sa), Iter2(ia+sa-3), Iter2(ia+sa)) == Iter1(ia+sa-3));
75     assert(std::search(Iter1(ia), Iter1(ia+sa), Iter2(ia), Iter2(ia+sa)) == Iter1(ia));
76     assert(std::search(Iter1(ia), Iter1(ia+sa-1), Iter2(ia), Iter2(ia+sa)) == Iter1(ia+sa-1));
77     assert(std::search(Iter1(ia), Iter1(ia+1), Iter2(ia), Iter2(ia+sa)) == Iter1(ia+1));
78     int ib[] = {0, 1, 2, 0, 1, 2, 3, 0, 1, 2, 3, 4};
79     const unsigned sb = sizeof(ib)/sizeof(ib[0]);
80     int ic[] = {1};
81     assert(std::search(Iter1(ib), Iter1(ib+sb), Iter2(ic), Iter2(ic+1)) == Iter1(ib+1));
82     int id[] = {1, 2};
83     assert(std::search(Iter1(ib), Iter1(ib+sb), Iter2(id), Iter2(id+2)) == Iter1(ib+1));
84     int ie[] = {1, 2, 3};
85     assert(std::search(Iter1(ib), Iter1(ib+sb), Iter2(ie), Iter2(ie+3)) == Iter1(ib+4));
86     int ig[] = {1, 2, 3, 4};
87     assert(std::search(Iter1(ib), Iter1(ib+sb), Iter2(ig), Iter2(ig+4)) == Iter1(ib+8));
88     int ih[] = {0, 1, 1, 1, 1, 2, 3, 0, 1, 2, 3, 4};
89     const unsigned sh = sizeof(ih)/sizeof(ih[0]);
90     int ii[] = {1, 1, 2};
91     assert(std::search(Iter1(ih), Iter1(ih+sh), Iter2(ii), Iter2(ii+3)) == Iter1(ih+3));
92     int ij[] = {0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0};
93     const unsigned sj = sizeof(ij)/sizeof(ij[0]);
94     int ik[] = {0, 0, 0, 0, 1, 1, 1, 1, 0, 0};
95     const unsigned sk = sizeof(ik)/sizeof(ik[0]);
96     assert(std::search(Iter1(ij), Iter1(ij+sj), Iter2(ik), Iter2(ik+sk)) == Iter1(ij+6));
97 }
98 
main()99 int main()
100 {
101     test<forward_iterator<const int*>, forward_iterator<const int*> >();
102     test<forward_iterator<const int*>, bidirectional_iterator<const int*> >();
103     test<forward_iterator<const int*>, random_access_iterator<const int*> >();
104     test<bidirectional_iterator<const int*>, forward_iterator<const int*> >();
105     test<bidirectional_iterator<const int*>, bidirectional_iterator<const int*> >();
106     test<bidirectional_iterator<const int*>, random_access_iterator<const int*> >();
107     test<random_access_iterator<const int*>, forward_iterator<const int*> >();
108     test<random_access_iterator<const int*>, bidirectional_iterator<const int*> >();
109     test<random_access_iterator<const int*>, random_access_iterator<const int*> >();
110 
111 #if TEST_STD_VER > 14
112 {
113     typedef int * RI;
114     static_assert((std::is_same<RI, decltype(std::search(RI(), RI(), MySearcher()))>::value), "" );
115 
116     RI it(nullptr);
117     assert(it == std::search(it, it, MySearcher()));
118     assert(searcher_called == 1);
119 }
120 #endif
121 
122 #if TEST_STD_VER > 17
123     static_assert(test_constexpr());
124 #endif
125 }
126