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 #include <algorithm>
18 #include <cassert>
19
20 #include "test_macros.h"
21 #include "test_iterators.h"
22
23 #if TEST_STD_VER > 17
eq(int a,int b)24 TEST_CONSTEXPR bool eq(int a, int b) { return a == b; }
25
test_constexpr()26 TEST_CONSTEXPR bool test_constexpr() {
27 int ia[] = {0, 1, 2, 3};
28 int ib[] = {0, 1, 5, 3};
29 int ic[] = {0, 1, 2, 0, 1, 2, 3, 0, 1, 2, 3, 4};
30 return (std::search(std::begin(ic), std::end(ic), std::begin(ia), std::end(ia), eq) == ic+3)
31 && (std::search(std::begin(ic), std::end(ic), std::begin(ib), std::end(ib), eq) == std::end(ic))
32 ;
33 }
34 #endif
35
36 struct count_equal
37 {
38 static unsigned count;
39 template <class T>
operator ()count_equal40 bool operator()(const T& x, const T& y)
41 {++count; return x == y;}
42 };
43
44 unsigned count_equal::count = 0;
45
46 template <class Iter1, class Iter2>
47 void
test()48 test()
49 {
50 int ia[] = {0, 1, 2, 3, 4, 5};
51 const unsigned sa = sizeof(ia)/sizeof(ia[0]);
52 count_equal::count = 0;
53 assert(std::search(Iter1(ia), Iter1(ia+sa), Iter2(ia), Iter2(ia), count_equal()) == Iter1(ia));
54 assert(count_equal::count <= 0);
55 count_equal::count = 0;
56 assert(std::search(Iter1(ia), Iter1(ia+sa), Iter2(ia), Iter2(ia+1), count_equal()) == Iter1(ia));
57 assert(count_equal::count <= sa);
58 count_equal::count = 0;
59 assert(std::search(Iter1(ia), Iter1(ia+sa), Iter2(ia+1), Iter2(ia+2), count_equal()) == Iter1(ia+1));
60 assert(count_equal::count <= sa);
61 count_equal::count = 0;
62 assert(std::search(Iter1(ia), Iter1(ia+sa), Iter2(ia+2), Iter2(ia+2), count_equal()) == Iter1(ia));
63 assert(count_equal::count <= 0);
64 count_equal::count = 0;
65 assert(std::search(Iter1(ia), Iter1(ia+sa), Iter2(ia+2), Iter2(ia+3), count_equal()) == Iter1(ia+2));
66 assert(count_equal::count <= sa);
67 count_equal::count = 0;
68 assert(std::search(Iter1(ia), Iter1(ia+sa), Iter2(ia+2), Iter2(ia+3), count_equal()) == Iter1(ia+2));
69 assert(count_equal::count <= sa);
70 count_equal::count = 0;
71 assert(std::search(Iter1(ia), Iter1(ia), Iter2(ia+2), Iter2(ia+3), count_equal()) == Iter1(ia));
72 assert(count_equal::count <= 0);
73 count_equal::count = 0;
74 assert(std::search(Iter1(ia), Iter1(ia+sa), Iter2(ia+sa-1), Iter2(ia+sa), count_equal()) == Iter1(ia+sa-1));
75 assert(count_equal::count <= sa);
76 count_equal::count = 0;
77 assert(std::search(Iter1(ia), Iter1(ia+sa), Iter2(ia+sa-3), Iter2(ia+sa), count_equal()) == Iter1(ia+sa-3));
78 assert(count_equal::count <= sa*3);
79 count_equal::count = 0;
80 assert(std::search(Iter1(ia), Iter1(ia+sa), Iter2(ia), Iter2(ia+sa), count_equal()) == Iter1(ia));
81 assert(count_equal::count <= sa*sa);
82 count_equal::count = 0;
83 assert(std::search(Iter1(ia), Iter1(ia+sa-1), Iter2(ia), Iter2(ia+sa), count_equal()) == Iter1(ia+sa-1));
84 assert(count_equal::count <= (sa-1)*sa);
85 count_equal::count = 0;
86 assert(std::search(Iter1(ia), Iter1(ia+1), Iter2(ia), Iter2(ia+sa), count_equal()) == Iter1(ia+1));
87 assert(count_equal::count <= sa);
88 count_equal::count = 0;
89 int ib[] = {0, 1, 2, 0, 1, 2, 3, 0, 1, 2, 3, 4};
90 const unsigned sb = sizeof(ib)/sizeof(ib[0]);
91 int ic[] = {1};
92 assert(std::search(Iter1(ib), Iter1(ib+sb), Iter2(ic), Iter2(ic+1), count_equal()) == Iter1(ib+1));
93 assert(count_equal::count <= sb);
94 count_equal::count = 0;
95 int id[] = {1, 2};
96 assert(std::search(Iter1(ib), Iter1(ib+sb), Iter2(id), Iter2(id+2), count_equal()) == Iter1(ib+1));
97 assert(count_equal::count <= sb*2);
98 count_equal::count = 0;
99 int ie[] = {1, 2, 3};
100 assert(std::search(Iter1(ib), Iter1(ib+sb), Iter2(ie), Iter2(ie+3), count_equal()) == Iter1(ib+4));
101 assert(count_equal::count <= sb*3);
102 count_equal::count = 0;
103 int ig[] = {1, 2, 3, 4};
104 assert(std::search(Iter1(ib), Iter1(ib+sb), Iter2(ig), Iter2(ig+4), count_equal()) == Iter1(ib+8));
105 assert(count_equal::count <= sb*4);
106 count_equal::count = 0;
107 int ih[] = {0, 1, 1, 1, 1, 2, 3, 0, 1, 2, 3, 4};
108 const unsigned sh = sizeof(ih)/sizeof(ih[0]);
109 int ii[] = {1, 1, 2};
110 assert(std::search(Iter1(ih), Iter1(ih+sh), Iter2(ii), Iter2(ii+3), count_equal()) == Iter1(ih+3));
111 assert(count_equal::count <= sh*3);
112 }
113
main()114 int main()
115 {
116 test<forward_iterator<const int*>, forward_iterator<const int*> >();
117 test<forward_iterator<const int*>, bidirectional_iterator<const int*> >();
118 test<forward_iterator<const int*>, random_access_iterator<const int*> >();
119 test<bidirectional_iterator<const int*>, forward_iterator<const int*> >();
120 test<bidirectional_iterator<const int*>, bidirectional_iterator<const int*> >();
121 test<bidirectional_iterator<const int*>, random_access_iterator<const int*> >();
122 test<random_access_iterator<const int*>, forward_iterator<const int*> >();
123 test<random_access_iterator<const int*>, bidirectional_iterator<const int*> >();
124 test<random_access_iterator<const int*>, random_access_iterator<const int*> >();
125
126 #if TEST_STD_VER > 17
127 static_assert(test_constexpr());
128 #endif
129 }
130