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 // Predicate<auto, Iter1::value_type, Iter2::value_type> Pred> 14 // requires CopyConstructible<Pred> 15 // Iter1 16 // find_end(Iter1 first1, Iter1 last1, Iter2 first2, Iter2 last2, Pred pred); 17 18 #include <algorithm> 19 #include <cassert> 20 21 #include "test_iterators.h" 22 23 struct count_equal 24 { 25 static unsigned count; 26 template <class T> 27 bool operator()(const T& x, const T& y) 28 {++count; return x == y;} 29 }; 30 31 unsigned count_equal::count = 0; 32 33 template <class Iter1, class Iter2> 34 void 35 test() 36 { 37 int ia[] = {0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 0, 1, 2, 3, 0, 1, 2, 0, 1, 0}; 38 const unsigned sa = sizeof(ia)/sizeof(ia[0]); 39 int b[] = {0}; 40 count_equal::count = 0; 41 assert(std::find_end(Iter1(ia), Iter1(ia+sa), Iter2(b), Iter2(b+1), count_equal()) == Iter1(ia+sa-1)); 42 assert(count_equal::count <= 1*(sa-1+1)); 43 int c[] = {0, 1}; 44 count_equal::count = 0; 45 assert(std::find_end(Iter1(ia), Iter1(ia+sa), Iter2(c), Iter2(c+2), count_equal()) == Iter1(ia+18)); 46 assert(count_equal::count <= 2*(sa-2+1)); 47 int d[] = {0, 1, 2}; 48 count_equal::count = 0; 49 assert(std::find_end(Iter1(ia), Iter1(ia+sa), Iter2(d), Iter2(d+3), count_equal()) == Iter1(ia+15)); 50 assert(count_equal::count <= 3*(sa-3+1)); 51 int e[] = {0, 1, 2, 3}; 52 count_equal::count = 0; 53 assert(std::find_end(Iter1(ia), Iter1(ia+sa), Iter2(e), Iter2(e+4), count_equal()) == Iter1(ia+11)); 54 assert(count_equal::count <= 4*(sa-4+1)); 55 int f[] = {0, 1, 2, 3, 4}; 56 count_equal::count = 0; 57 assert(std::find_end(Iter1(ia), Iter1(ia+sa), Iter2(f), Iter2(f+5), count_equal()) == Iter1(ia+6)); 58 assert(count_equal::count <= 5*(sa-5+1)); 59 int g[] = {0, 1, 2, 3, 4, 5}; 60 count_equal::count = 0; 61 assert(std::find_end(Iter1(ia), Iter1(ia+sa), Iter2(g), Iter2(g+6), count_equal()) == Iter1(ia)); 62 assert(count_equal::count <= 6*(sa-6+1)); 63 int h[] = {0, 1, 2, 3, 4, 5, 6}; 64 count_equal::count = 0; 65 assert(std::find_end(Iter1(ia), Iter1(ia+sa), Iter2(h), Iter2(h+7), count_equal()) == Iter1(ia+sa)); 66 assert(count_equal::count <= 7*(sa-7+1)); 67 count_equal::count = 0; 68 assert(std::find_end(Iter1(ia), Iter1(ia+sa), Iter2(b), Iter2(b), count_equal()) == Iter1(ia+sa)); 69 assert(count_equal::count <= 0); 70 count_equal::count = 0; 71 assert(std::find_end(Iter1(ia), Iter1(ia), Iter2(b), Iter2(b+1), count_equal()) == Iter1(ia)); 72 assert(count_equal::count <= 0); 73 } 74 75 int main() 76 { 77 test<forward_iterator<const int*>, forward_iterator<const int*> >(); 78 test<forward_iterator<const int*>, bidirectional_iterator<const int*> >(); 79 test<forward_iterator<const int*>, random_access_iterator<const int*> >(); 80 test<bidirectional_iterator<const int*>, forward_iterator<const int*> >(); 81 test<bidirectional_iterator<const int*>, bidirectional_iterator<const int*> >(); 82 test<bidirectional_iterator<const int*>, random_access_iterator<const int*> >(); 83 test<random_access_iterator<const int*>, forward_iterator<const int*> >(); 84 test<random_access_iterator<const int*>, bidirectional_iterator<const int*> >(); 85 test<random_access_iterator<const int*>, random_access_iterator<const int*> >(); 86 } 87