1d835e592SMarshall Clow //===----------------------------------------------------------------------===//
2d835e592SMarshall 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
6d835e592SMarshall Clow //
7d835e592SMarshall Clow //===----------------------------------------------------------------------===//
8d835e592SMarshall Clow
9d835e592SMarshall Clow // <functional>
10d835e592SMarshall Clow
1131cbe0f2SLouis Dionne // UNSUPPORTED: c++03, c++11, c++14
12d835e592SMarshall Clow
13d835e592SMarshall Clow // default searcher
14d835e592SMarshall Clow // template<class _ForwardIterator, class _BinaryPredicate = equal_to<>>
15d835e592SMarshall Clow // class default_searcher {
16d835e592SMarshall Clow // public:
17d835e592SMarshall Clow // default_searcher(_ForwardIterator __f, _ForwardIterator __l,
18d835e592SMarshall Clow // _BinaryPredicate __p = _BinaryPredicate())
19d835e592SMarshall Clow // : __first_(__f), __last_(__l), __pred_(__p) {}
20d835e592SMarshall Clow //
21d835e592SMarshall Clow // template <typename _ForwardIterator2>
22d835e592SMarshall Clow // pair<_ForwardIterator2, _ForwardIterator2>
23d835e592SMarshall Clow // operator () (_ForwardIterator2 __f, _ForwardIterator2 __l) const {
24d835e592SMarshall Clow // return std::search(__f, __l, __first_, __last_, __pred_);
25d835e592SMarshall Clow // }
26d835e592SMarshall Clow //
27d835e592SMarshall Clow // private:
28d835e592SMarshall Clow // _ForwardIterator __first_;
29d835e592SMarshall Clow // _ForwardIterator __last_;
30d835e592SMarshall Clow // _BinaryPredicate __pred_;
31d835e592SMarshall Clow // };
32d835e592SMarshall Clow
33d835e592SMarshall Clow
34d835e592SMarshall Clow #include <algorithm>
35d835e592SMarshall Clow #include <functional>
36d835e592SMarshall Clow #include <cassert>
37d835e592SMarshall Clow
387fc6a556SMarshall Clow #include "test_macros.h"
39d835e592SMarshall Clow #include "test_iterators.h"
40d835e592SMarshall Clow
41d835e592SMarshall Clow struct count_equal
42d835e592SMarshall Clow {
43c0a2d3b9SArthur O'Dwyer int *count;
447b00e9faSArthur O'Dwyer
45d835e592SMarshall Clow template <class T>
operator ()count_equal467b00e9faSArthur O'Dwyer TEST_CONSTEXPR_CXX14 bool operator()(const T& x, const T& y) const
477b00e9faSArthur O'Dwyer {++*count; return x == y;}
48d835e592SMarshall Clow };
49d835e592SMarshall Clow
50d835e592SMarshall Clow template <typename Iter1, typename Iter2>
517b00e9faSArthur O'Dwyer TEST_CONSTEXPR_CXX20
do_search(Iter1 b1,Iter1 e1,Iter2 b2,Iter2 e2,Iter1 result)527b00e9faSArthur O'Dwyer void do_search(Iter1 b1, Iter1 e1, Iter2 b2, Iter2 e2, Iter1 result) {
53c0a2d3b9SArthur O'Dwyer int count = 0;
547b00e9faSArthur O'Dwyer std::default_searcher<Iter2, count_equal> s{b2, e2, count_equal{&count}};
55d835e592SMarshall Clow assert(result == std::search(b1, e1, s));
56*60575179SCasey Carter auto d1 = std::distance(b1, e1);
57*60575179SCasey Carter auto d2 = std::distance(b2, e2);
587b00e9faSArthur O'Dwyer assert((count >= 1) || (d2 == 0) || (d1 < d2));
597b00e9faSArthur O'Dwyer assert((d1 < d2) || count <= d1 * (d1 - d2 + 1));
60d835e592SMarshall Clow }
61d835e592SMarshall Clow
62d835e592SMarshall Clow template <class Iter1, class Iter2>
637b00e9faSArthur O'Dwyer TEST_CONSTEXPR_CXX20
test()647b00e9faSArthur O'Dwyer bool test()
65d835e592SMarshall Clow {
66d835e592SMarshall Clow int ia[] = {0, 1, 2, 3, 4, 5};
67d835e592SMarshall Clow const unsigned sa = sizeof(ia)/sizeof(ia[0]);
687b00e9faSArthur O'Dwyer do_search(Iter1(ia), Iter1(ia+sa), Iter2(ia), Iter2(ia), Iter1(ia));
697b00e9faSArthur O'Dwyer do_search(Iter1(ia), Iter1(ia+sa), Iter2(ia), Iter2(ia+1), Iter1(ia));
707b00e9faSArthur O'Dwyer do_search(Iter1(ia), Iter1(ia+sa), Iter2(ia+1), Iter2(ia+2), Iter1(ia+1));
717b00e9faSArthur O'Dwyer do_search(Iter1(ia), Iter1(ia+sa), Iter2(ia+2), Iter2(ia+2), Iter1(ia));
727b00e9faSArthur O'Dwyer do_search(Iter1(ia), Iter1(ia+sa), Iter2(ia+2), Iter2(ia+3), Iter1(ia+2));
737b00e9faSArthur O'Dwyer do_search(Iter1(ia), Iter1(ia+sa), Iter2(ia+2), Iter2(ia+3), Iter1(ia+2));
747b00e9faSArthur O'Dwyer do_search(Iter1(ia), Iter1(ia), Iter2(ia+2), Iter2(ia+3), Iter1(ia));
757b00e9faSArthur O'Dwyer do_search(Iter1(ia), Iter1(ia+sa), Iter2(ia+sa-1), Iter2(ia+sa), Iter1(ia+sa-1));
767b00e9faSArthur O'Dwyer do_search(Iter1(ia), Iter1(ia+sa), Iter2(ia+sa-3), Iter2(ia+sa), Iter1(ia+sa-3));
777b00e9faSArthur O'Dwyer do_search(Iter1(ia), Iter1(ia+sa), Iter2(ia), Iter2(ia+sa), Iter1(ia));
787b00e9faSArthur O'Dwyer do_search(Iter1(ia), Iter1(ia+sa-1), Iter2(ia), Iter2(ia+sa), Iter1(ia+sa-1));
797b00e9faSArthur O'Dwyer do_search(Iter1(ia), Iter1(ia+1), Iter2(ia), Iter2(ia+sa), Iter1(ia+1));
80d835e592SMarshall Clow int ib[] = {0, 1, 2, 0, 1, 2, 3, 0, 1, 2, 3, 4};
81d835e592SMarshall Clow const unsigned sb = sizeof(ib)/sizeof(ib[0]);
82d835e592SMarshall Clow int ic[] = {1};
837b00e9faSArthur O'Dwyer do_search(Iter1(ib), Iter1(ib+sb), Iter2(ic), Iter2(ic+1), Iter1(ib+1));
84d835e592SMarshall Clow int id[] = {1, 2};
857b00e9faSArthur O'Dwyer do_search(Iter1(ib), Iter1(ib+sb), Iter2(id), Iter2(id+2), Iter1(ib+1));
86d835e592SMarshall Clow int ie[] = {1, 2, 3};
877b00e9faSArthur O'Dwyer do_search(Iter1(ib), Iter1(ib+sb), Iter2(ie), Iter2(ie+3), Iter1(ib+4));
88d835e592SMarshall Clow int ig[] = {1, 2, 3, 4};
897b00e9faSArthur O'Dwyer do_search(Iter1(ib), Iter1(ib+sb), Iter2(ig), Iter2(ig+4), Iter1(ib+8));
90d835e592SMarshall Clow int ih[] = {0, 1, 1, 1, 1, 2, 3, 0, 1, 2, 3, 4};
91d835e592SMarshall Clow const unsigned sh = sizeof(ih)/sizeof(ih[0]);
92d835e592SMarshall Clow int ii[] = {1, 1, 2};
937b00e9faSArthur O'Dwyer do_search(Iter1(ih), Iter1(ih+sh), Iter2(ii), Iter2(ii+3), Iter1(ih+3));
947b00e9faSArthur O'Dwyer
957b00e9faSArthur O'Dwyer return true;
96d835e592SMarshall Clow }
97d835e592SMarshall Clow
main(int,char **)982df59c50SJF Bastien int main(int, char**) {
99d835e592SMarshall Clow test<forward_iterator<const int*>, forward_iterator<const int*> >();
100d835e592SMarshall Clow test<forward_iterator<const int*>, bidirectional_iterator<const int*> >();
101d835e592SMarshall Clow test<forward_iterator<const int*>, random_access_iterator<const int*> >();
102d835e592SMarshall Clow test<bidirectional_iterator<const int*>, forward_iterator<const int*> >();
103d835e592SMarshall Clow test<bidirectional_iterator<const int*>, bidirectional_iterator<const int*> >();
104d835e592SMarshall Clow test<bidirectional_iterator<const int*>, random_access_iterator<const int*> >();
105d835e592SMarshall Clow test<random_access_iterator<const int*>, forward_iterator<const int*> >();
106d835e592SMarshall Clow test<random_access_iterator<const int*>, bidirectional_iterator<const int*> >();
107d835e592SMarshall Clow test<random_access_iterator<const int*>, random_access_iterator<const int*> >();
1082df59c50SJF Bastien
1097b00e9faSArthur O'Dwyer #if TEST_STD_VER >= 20
1107b00e9faSArthur O'Dwyer static_assert(test<forward_iterator<const int*>, forward_iterator<const int*>>());
1117b00e9faSArthur O'Dwyer static_assert(test<forward_iterator<const int*>, bidirectional_iterator<const int*>>());
1127b00e9faSArthur O'Dwyer static_assert(test<forward_iterator<const int*>, random_access_iterator<const int*>>());
1137b00e9faSArthur O'Dwyer static_assert(test<bidirectional_iterator<const int*>, forward_iterator<const int*>>());
1147b00e9faSArthur O'Dwyer static_assert(test<bidirectional_iterator<const int*>, bidirectional_iterator<const int*>>());
1157b00e9faSArthur O'Dwyer static_assert(test<bidirectional_iterator<const int*>, random_access_iterator<const int*>>());
1167b00e9faSArthur O'Dwyer static_assert(test<random_access_iterator<const int*>, forward_iterator<const int*>>());
1177b00e9faSArthur O'Dwyer static_assert(test<random_access_iterator<const int*>, bidirectional_iterator<const int*>>());
1187b00e9faSArthur O'Dwyer static_assert(test<random_access_iterator<const int*>, random_access_iterator<const int*>>());
1197b00e9faSArthur O'Dwyer #endif
1207b00e9faSArthur O'Dwyer
1212df59c50SJF Bastien return 0;
122d835e592SMarshall Clow }
123