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