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