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 // <functional>
10
11 // UNSUPPORTED: c++03, c++11
12
13 // default searcher
14 // template<class _ForwardIterator, class _BinaryPredicate = equal_to<>>
15 // class default_searcher {
16 // public:
17 // default_searcher(_ForwardIterator __f, _ForwardIterator __l,
18 // _BinaryPredicate __p = _BinaryPredicate())
19 // : __first_(__f), __last_(__l), __pred_(__p) {}
20 //
21 // template <typename _ForwardIterator2>
22 // pair<_ForwardIterator2, _ForwardIterator2>
23 // operator () (_ForwardIterator2 __f, _ForwardIterator2 __l) const {
24 // return std::search(__f, __l, __first_, __last_, __pred_);
25 // }
26 //
27 // private:
28 // _ForwardIterator __first_;
29 // _ForwardIterator __last_;
30 // _BinaryPredicate __pred_;
31 // };
32
33
34 #include <experimental/algorithm>
35 #include <experimental/functional>
36 #include <cassert>
37
38 #include "test_macros.h"
39 #include "test_iterators.h"
40
41 struct count_equal
42 {
43 static unsigned count;
44 template <class T>
operator ()count_equal45 bool operator()(const T& x, const T& y) const
46 {++count; return x == y;}
47 };
48
49 unsigned count_equal::count = 0;
50
51 template <typename Iter1, typename Iter2>
do_search(Iter1 b1,Iter1 e1,Iter2 b2,Iter2 e2,Iter1 result,unsigned max_count)52 void do_search(Iter1 b1, Iter1 e1, Iter2 b2, Iter2 e2, Iter1 result, unsigned max_count) {
53 std::experimental::default_searcher<Iter2, count_equal> s{b2, e2};
54 count_equal::count = 0;
55 assert(result == std::experimental::search(b1, e1, s));
56 assert(count_equal::count <= max_count);
57 }
58
59 template <class Iter1, class Iter2>
60 void
test()61 test()
62 {
63 int ia[] = {0, 1, 2, 3, 4, 5};
64 const unsigned sa = sizeof(ia)/sizeof(ia[0]);
65 do_search(Iter1(ia), Iter1(ia+sa), Iter2(ia), Iter2(ia), Iter1(ia), 0);
66 do_search(Iter1(ia), Iter1(ia+sa), Iter2(ia), Iter2(ia+1), Iter1(ia), sa);
67 do_search(Iter1(ia), Iter1(ia+sa), Iter2(ia+1), Iter2(ia+2), Iter1(ia+1), sa);
68 do_search(Iter1(ia), Iter1(ia+sa), Iter2(ia+2), Iter2(ia+2), Iter1(ia), 0);
69 do_search(Iter1(ia), Iter1(ia+sa), Iter2(ia+2), Iter2(ia+3), Iter1(ia+2), sa);
70 do_search(Iter1(ia), Iter1(ia+sa), Iter2(ia+2), Iter2(ia+3), Iter1(ia+2), sa);
71 do_search(Iter1(ia), Iter1(ia), Iter2(ia+2), Iter2(ia+3), Iter1(ia), 0);
72 do_search(Iter1(ia), Iter1(ia+sa), Iter2(ia+sa-1), Iter2(ia+sa), Iter1(ia+sa-1), sa);
73 do_search(Iter1(ia), Iter1(ia+sa), Iter2(ia+sa-3), Iter2(ia+sa), Iter1(ia+sa-3), 3*sa);
74 do_search(Iter1(ia), Iter1(ia+sa), Iter2(ia), Iter2(ia+sa), Iter1(ia), sa*sa);
75 do_search(Iter1(ia), Iter1(ia+sa-1), Iter2(ia), Iter2(ia+sa), Iter1(ia+sa-1), (sa-1)*sa);
76 do_search(Iter1(ia), Iter1(ia+1), Iter2(ia), Iter2(ia+sa), Iter1(ia+1), sa);
77 int ib[] = {0, 1, 2, 0, 1, 2, 3, 0, 1, 2, 3, 4};
78 const unsigned sb = sizeof(ib)/sizeof(ib[0]);
79 int ic[] = {1};
80 do_search(Iter1(ib), Iter1(ib+sb), Iter2(ic), Iter2(ic+1), Iter1(ib+1), sb);
81 int id[] = {1, 2};
82 do_search(Iter1(ib), Iter1(ib+sb), Iter2(id), Iter2(id+2), Iter1(ib+1), sb*2);
83 int ie[] = {1, 2, 3};
84 do_search(Iter1(ib), Iter1(ib+sb), Iter2(ie), Iter2(ie+3), Iter1(ib+4), sb*3);
85 int ig[] = {1, 2, 3, 4};
86 do_search(Iter1(ib), Iter1(ib+sb), Iter2(ig), Iter2(ig+4), Iter1(ib+8), sb*4);
87 int ih[] = {0, 1, 1, 1, 1, 2, 3, 0, 1, 2, 3, 4};
88 const unsigned sh = sizeof(ih)/sizeof(ih[0]);
89 int ii[] = {1, 1, 2};
90 do_search(Iter1(ih), Iter1(ih+sh), Iter2(ii), Iter2(ii+3), Iter1(ih+3), sh*3);
91 }
92
main(int,char **)93 int main(int, char**) {
94 test<forward_iterator<const int*>, forward_iterator<const int*> >();
95 test<forward_iterator<const int*>, bidirectional_iterator<const int*> >();
96 test<forward_iterator<const int*>, random_access_iterator<const int*> >();
97 test<bidirectional_iterator<const int*>, forward_iterator<const int*> >();
98 test<bidirectional_iterator<const int*>, bidirectional_iterator<const int*> >();
99 test<bidirectional_iterator<const int*>, random_access_iterator<const int*> >();
100 test<random_access_iterator<const int*>, forward_iterator<const int*> >();
101 test<random_access_iterator<const int*>, bidirectional_iterator<const int*> >();
102 test<random_access_iterator<const int*>, random_access_iterator<const int*> >();
103
104 return 0;
105 }
106