1205c333cSMarshall Clow //===----------------------------------------------------------------------===//
2205c333cSMarshall Clow //
357b08b09SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
457b08b09SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
557b08b09SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6205c333cSMarshall Clow //
7205c333cSMarshall Clow //===----------------------------------------------------------------------===//
8205c333cSMarshall Clow
9205c333cSMarshall Clow // <functional>
10205c333cSMarshall Clow
1131cbe0f2SLouis Dionne // UNSUPPORTED: c++03, c++11
12*971e9c80SNikolas Klauser // ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_NO_EXPERIMENTAL_DEPRECATION_WARNING_SEARCHERS
131aa45677SMarshall Clow
14205c333cSMarshall Clow // default searcher
15205c333cSMarshall Clow // template<class _ForwardIterator, class _BinaryPredicate = equal_to<>>
16205c333cSMarshall Clow // class default_searcher {
17205c333cSMarshall Clow // public:
18205c333cSMarshall Clow // default_searcher(_ForwardIterator __f, _ForwardIterator __l,
19205c333cSMarshall Clow // _BinaryPredicate __p = _BinaryPredicate())
20205c333cSMarshall Clow // : __first_(__f), __last_(__l), __pred_(__p) {}
21205c333cSMarshall Clow //
22205c333cSMarshall Clow // template <typename _ForwardIterator2>
2328cc4ddeSMarshall Clow // pair<_ForwardIterator2, _ForwardIterator2>
2428cc4ddeSMarshall Clow // operator () (_ForwardIterator2 __f, _ForwardIterator2 __l) const {
25205c333cSMarshall Clow // return std::search(__f, __l, __first_, __last_, __pred_);
26205c333cSMarshall Clow // }
27205c333cSMarshall Clow //
28205c333cSMarshall Clow // private:
29205c333cSMarshall Clow // _ForwardIterator __first_;
30205c333cSMarshall Clow // _ForwardIterator __last_;
31205c333cSMarshall Clow // _BinaryPredicate __pred_;
32205c333cSMarshall Clow // };
33205c333cSMarshall Clow
34205c333cSMarshall Clow
35205c333cSMarshall Clow #include <experimental/algorithm>
36205c333cSMarshall Clow #include <experimental/functional>
37205c333cSMarshall Clow #include <cassert>
38205c333cSMarshall Clow
397fc6a556SMarshall Clow #include "test_macros.h"
40205c333cSMarshall Clow #include "test_iterators.h"
41205c333cSMarshall Clow
42205c333cSMarshall Clow struct count_equal
43205c333cSMarshall Clow {
44205c333cSMarshall Clow static unsigned count;
45205c333cSMarshall Clow template <class T>
operator ()count_equal461aa45677SMarshall Clow bool operator()(const T& x, const T& y) const
47205c333cSMarshall Clow {++count; return x == y;}
48205c333cSMarshall Clow };
49205c333cSMarshall Clow
50205c333cSMarshall Clow unsigned count_equal::count = 0;
51205c333cSMarshall Clow
52205c333cSMarshall Clow template <typename Iter1, typename Iter2>
do_search(Iter1 b1,Iter1 e1,Iter2 b2,Iter2 e2,Iter1 result,unsigned max_count)53205c333cSMarshall Clow void do_search(Iter1 b1, Iter1 e1, Iter2 b2, Iter2 e2, Iter1 result, unsigned max_count) {
54205c333cSMarshall Clow std::experimental::default_searcher<Iter2, count_equal> s{b2, e2};
55205c333cSMarshall Clow count_equal::count = 0;
56205c333cSMarshall Clow assert(result == std::experimental::search(b1, e1, s));
57205c333cSMarshall Clow assert(count_equal::count <= max_count);
58205c333cSMarshall Clow }
59205c333cSMarshall Clow
60205c333cSMarshall Clow template <class Iter1, class Iter2>
61205c333cSMarshall Clow void
test()62205c333cSMarshall Clow test()
63205c333cSMarshall Clow {
64205c333cSMarshall Clow int ia[] = {0, 1, 2, 3, 4, 5};
65205c333cSMarshall Clow const unsigned sa = sizeof(ia)/sizeof(ia[0]);
66205c333cSMarshall Clow do_search(Iter1(ia), Iter1(ia+sa), Iter2(ia), Iter2(ia), Iter1(ia), 0);
67205c333cSMarshall Clow do_search(Iter1(ia), Iter1(ia+sa), Iter2(ia), Iter2(ia+1), Iter1(ia), sa);
68205c333cSMarshall Clow do_search(Iter1(ia), Iter1(ia+sa), Iter2(ia+1), Iter2(ia+2), Iter1(ia+1), sa);
69205c333cSMarshall Clow do_search(Iter1(ia), Iter1(ia+sa), Iter2(ia+2), Iter2(ia+2), Iter1(ia), 0);
70205c333cSMarshall Clow do_search(Iter1(ia), Iter1(ia+sa), Iter2(ia+2), Iter2(ia+3), Iter1(ia+2), sa);
71205c333cSMarshall Clow do_search(Iter1(ia), Iter1(ia+sa), Iter2(ia+2), Iter2(ia+3), Iter1(ia+2), sa);
72205c333cSMarshall Clow do_search(Iter1(ia), Iter1(ia), Iter2(ia+2), Iter2(ia+3), Iter1(ia), 0);
73205c333cSMarshall Clow do_search(Iter1(ia), Iter1(ia+sa), Iter2(ia+sa-1), Iter2(ia+sa), Iter1(ia+sa-1), sa);
74205c333cSMarshall Clow do_search(Iter1(ia), Iter1(ia+sa), Iter2(ia+sa-3), Iter2(ia+sa), Iter1(ia+sa-3), 3*sa);
75205c333cSMarshall Clow do_search(Iter1(ia), Iter1(ia+sa), Iter2(ia), Iter2(ia+sa), Iter1(ia), sa*sa);
76205c333cSMarshall Clow do_search(Iter1(ia), Iter1(ia+sa-1), Iter2(ia), Iter2(ia+sa), Iter1(ia+sa-1), (sa-1)*sa);
77205c333cSMarshall Clow do_search(Iter1(ia), Iter1(ia+1), Iter2(ia), Iter2(ia+sa), Iter1(ia+1), sa);
78205c333cSMarshall Clow int ib[] = {0, 1, 2, 0, 1, 2, 3, 0, 1, 2, 3, 4};
79205c333cSMarshall Clow const unsigned sb = sizeof(ib)/sizeof(ib[0]);
80205c333cSMarshall Clow int ic[] = {1};
81205c333cSMarshall Clow do_search(Iter1(ib), Iter1(ib+sb), Iter2(ic), Iter2(ic+1), Iter1(ib+1), sb);
82205c333cSMarshall Clow int id[] = {1, 2};
83205c333cSMarshall Clow do_search(Iter1(ib), Iter1(ib+sb), Iter2(id), Iter2(id+2), Iter1(ib+1), sb*2);
84205c333cSMarshall Clow int ie[] = {1, 2, 3};
85205c333cSMarshall Clow do_search(Iter1(ib), Iter1(ib+sb), Iter2(ie), Iter2(ie+3), Iter1(ib+4), sb*3);
86205c333cSMarshall Clow int ig[] = {1, 2, 3, 4};
87205c333cSMarshall Clow do_search(Iter1(ib), Iter1(ib+sb), Iter2(ig), Iter2(ig+4), Iter1(ib+8), sb*4);
88205c333cSMarshall Clow int ih[] = {0, 1, 1, 1, 1, 2, 3, 0, 1, 2, 3, 4};
89205c333cSMarshall Clow const unsigned sh = sizeof(ih)/sizeof(ih[0]);
90205c333cSMarshall Clow int ii[] = {1, 1, 2};
91205c333cSMarshall Clow do_search(Iter1(ih), Iter1(ih+sh), Iter2(ii), Iter2(ii+3), Iter1(ih+3), sh*3);
92205c333cSMarshall Clow }
93205c333cSMarshall Clow
main(int,char **)942df59c50SJF Bastien int main(int, char**) {
95205c333cSMarshall Clow test<forward_iterator<const int*>, forward_iterator<const int*> >();
96205c333cSMarshall Clow test<forward_iterator<const int*>, bidirectional_iterator<const int*> >();
97205c333cSMarshall Clow test<forward_iterator<const int*>, random_access_iterator<const int*> >();
98205c333cSMarshall Clow test<bidirectional_iterator<const int*>, forward_iterator<const int*> >();
99205c333cSMarshall Clow test<bidirectional_iterator<const int*>, bidirectional_iterator<const int*> >();
100205c333cSMarshall Clow test<bidirectional_iterator<const int*>, random_access_iterator<const int*> >();
101205c333cSMarshall Clow test<random_access_iterator<const int*>, forward_iterator<const int*> >();
102205c333cSMarshall Clow test<random_access_iterator<const int*>, bidirectional_iterator<const int*> >();
103205c333cSMarshall Clow test<random_access_iterator<const int*>, random_access_iterator<const int*> >();
1042df59c50SJF Bastien
1052df59c50SJF Bastien return 0;
106205c333cSMarshall Clow }
107