Home | History | Annotate | Download | only in mismatch
      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 //   constexpr pair<Iter1, Iter2>   // constexpr after c++17
     16 //   mismatch(Iter1 first1, Iter1 last1, Iter2 first2, Pred pred);
     17 //
     18 // template<InputIterator Iter1, InputIterator Iter2, Predicate Pred>
     19 //   constexpr pair<Iter1, Iter2>   // constexpr after c++17
     20 //   mismatch(Iter1 first1, Iter1 last1, Iter2 first2, Iter2 last2, Pred pred); // C++14
     21 
     22 #include <algorithm>
     23 #include <functional>
     24 #include <cassert>
     25 
     26 #include "test_macros.h"
     27 #include "test_iterators.h"
     28 #include "counting_predicates.hpp"
     29 
     30 #if TEST_STD_VER > 17
     31 TEST_CONSTEXPR bool eq(int a, int b) { return a == b; }
     32 
     33 TEST_CONSTEXPR bool test_constexpr() {
     34     int ia[] = {1, 3, 6, 7};
     35     int ib[] = {1, 3};
     36     int ic[] = {1, 3, 5, 7};
     37     typedef input_iterator<int*>         II;
     38     typedef bidirectional_iterator<int*> BI;
     39 
     40     auto p1 = std::mismatch(std::begin(ia), std::end(ia), std::begin(ic), eq);
     41     if (p1.first != ia+2 || p1.second != ic+2)
     42         return false;
     43 
     44     auto p2 = std::mismatch(std::begin(ia), std::end(ia), std::begin(ic), std::end(ic), eq);
     45     if (p2.first != ia+2 || p2.second != ic+2)
     46         return false;
     47 
     48     auto p3 = std::mismatch(std::begin(ib), std::end(ib), std::begin(ic), eq);
     49     if (p3.first != ib+2 || p3.second != ic+2)
     50         return false;
     51 
     52     auto p4 = std::mismatch(std::begin(ib), std::end(ib), std::begin(ic), std::end(ic), eq);
     53     if (p4.first != ib+2 || p4.second != ic+2)
     54         return false;
     55 
     56     auto p5 = std::mismatch(II(std::begin(ib)), II(std::end(ib)), II(std::begin(ic)), eq);
     57     if (p5.first != II(ib+2) || p5.second != II(ic+2))
     58         return false;
     59     auto p6 = std::mismatch(BI(std::begin(ib)), BI(std::end(ib)), BI(std::begin(ic)), BI(std::end(ic)), eq);
     60     if (p6.first != BI(ib+2) || p6.second != BI(ic+2))
     61         return false;
     62 
     63     return true;
     64     }
     65 #endif
     66 
     67 
     68 #if TEST_STD_VER > 11
     69 #define HAS_FOUR_ITERATOR_VERSION
     70 #endif
     71 
     72 int main()
     73 {
     74     int ia[] = {0, 1, 2, 2, 0, 1, 2, 3};
     75     const unsigned sa = sizeof(ia)/sizeof(ia[0]);
     76     int ib[] = {0, 1, 2, 3, 0, 1, 2, 3};
     77     const unsigned sb = sizeof(ib)/sizeof(ib[0]); ((void)sb); // unused in C++11
     78 
     79     typedef input_iterator<const int*> II;
     80     typedef random_access_iterator<const int*>  RAI;
     81     typedef std::equal_to<int> EQ;
     82 
     83     assert(std::mismatch(II(ia), II(ia + sa), II(ib), EQ())
     84             == (std::pair<II, II>(II(ia+3), II(ib+3))));
     85     assert(std::mismatch(RAI(ia), RAI(ia + sa), RAI(ib), EQ())
     86             == (std::pair<RAI, RAI>(RAI(ia+3), RAI(ib+3))));
     87 
     88     binary_counting_predicate<EQ, int> bcp((EQ()));
     89     assert(std::mismatch(RAI(ia), RAI(ia + sa), RAI(ib), std::ref(bcp))
     90             == (std::pair<RAI, RAI>(RAI(ia+3), RAI(ib+3))));
     91     assert(bcp.count() > 0 && bcp.count() < sa);
     92     bcp.reset();
     93 
     94 #if TEST_STD_VER >= 14
     95     assert(std::mismatch(II(ia), II(ia + sa), II(ib), II(ib + sb), EQ())
     96             == (std::pair<II, II>(II(ia+3), II(ib+3))));
     97     assert(std::mismatch(RAI(ia), RAI(ia + sa), RAI(ib), RAI(ib + sb), EQ())
     98             == (std::pair<RAI, RAI>(RAI(ia+3), RAI(ib+3))));
     99 
    100     assert(std::mismatch(II(ia), II(ia + sa), II(ib), II(ib + sb), std::ref(bcp))
    101             == (std::pair<II, II>(II(ia+3), II(ib+3))));
    102     assert(bcp.count() > 0 && bcp.count() < std::min(sa, sb));
    103 #endif
    104 
    105     assert(std::mismatch(ia, ia + sa, ib, EQ()) ==
    106            (std::pair<int*,int*>(ia+3,ib+3)));
    107 
    108 #if TEST_STD_VER >= 14
    109     assert(std::mismatch(ia, ia + sa, ib, ib + sb, EQ()) ==
    110            (std::pair<int*,int*>(ia+3,ib+3)));
    111     assert(std::mismatch(ia, ia + sa, ib, ib + 2, EQ()) ==
    112            (std::pair<int*,int*>(ia+2,ib+2)));
    113 #endif
    114 
    115 #if TEST_STD_VER > 17
    116     static_assert(test_constexpr());
    117 #endif
    118 }
    119