Home | History | Annotate | Download | only in alg.equal
      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<InputIterator Iter1, InputIterator Iter2,
     13 //          Predicate<auto, Iter1::value_type, Iter2::value_type> Pred>
     14 //   requires CopyConstructible<Pred>
     15 //   bool
     16 //   equal(Iter1 first1, Iter1 last1, Iter2 first2, Pred pred);
     17 
     18 #include <algorithm>
     19 #include <functional>
     20 #include <cassert>
     21 
     22 #include "test_iterators.h"
     23 
     24 #if _LIBCPP_STD_VER > 11
     25 #define HAS_FOUR_ITERATOR_VERSION
     26 #endif
     27 
     28 int comparison_count = 0;
     29 template <typename T>
     30 bool counting_equals ( const T &a, const T &b ) {
     31     ++comparison_count;
     32     return a == b;
     33 }
     34 
     35 int main()
     36 {
     37     int ia[] = {0, 1, 2, 3, 4, 5};
     38     const unsigned s = sizeof(ia)/sizeof(ia[0]);
     39     int ib[s] = {0, 1, 2, 5, 4, 5};
     40     assert(std::equal(input_iterator<const int*>(ia),
     41                       input_iterator<const int*>(ia+s),
     42                       input_iterator<const int*>(ia),
     43                       std::equal_to<int>()));
     44 #ifdef HAS_FOUR_ITERATOR_VERSION
     45     assert(std::equal(input_iterator<const int*>(ia),
     46                       input_iterator<const int*>(ia+s),
     47                       input_iterator<const int*>(ia),
     48                       input_iterator<const int*>(ia+s),
     49                       std::equal_to<int>()));
     50     assert(std::equal(random_access_iterator<const int*>(ia),
     51                       random_access_iterator<const int*>(ia+s),
     52                       random_access_iterator<const int*>(ia),
     53                       random_access_iterator<const int*>(ia+s),
     54                       std::equal_to<int>()));
     55 
     56     comparison_count = 0;
     57     assert(!std::equal(input_iterator<const int*>(ia),
     58                       input_iterator<const int*>(ia+s),
     59                       input_iterator<const int*>(ia),
     60                       input_iterator<const int*>(ia+s-1),
     61                       counting_equals<int>));
     62     assert(comparison_count > 0);
     63     comparison_count = 0;
     64     assert(!std::equal(random_access_iterator<const int*>(ia),
     65                       random_access_iterator<const int*>(ia+s),
     66                       random_access_iterator<const int*>(ia),
     67                       random_access_iterator<const int*>(ia+s-1),
     68                       counting_equals<int>));
     69     assert(comparison_count == 0);
     70 #endif
     71     assert(!std::equal(input_iterator<const int*>(ia),
     72                        input_iterator<const int*>(ia+s),
     73                        input_iterator<const int*>(ib),
     74                        std::equal_to<int>()));
     75 #ifdef HAS_FOUR_ITERATOR_VERSION
     76     assert(!std::equal(input_iterator<const int*>(ia),
     77                        input_iterator<const int*>(ia+s),
     78                        input_iterator<const int*>(ib),
     79                        input_iterator<const int*>(ib+s),
     80                        std::equal_to<int>()));
     81     assert(!std::equal(random_access_iterator<const int*>(ia),
     82                        random_access_iterator<const int*>(ia+s),
     83                        random_access_iterator<const int*>(ib),
     84                        random_access_iterator<const int*>(ib+s),
     85                        std::equal_to<int>()));
     86 #endif
     87 }
     88