libcxx

libcxx mirror with random patches
git clone https://git.neptards.moe/neptards/libcxx.git
Log | Files | Refs

mismatch.pass.cpp (3189B)


      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 //   requires HasEqualTo<Iter1::value_type, Iter2::value_type>
     14 //   constexpr pair<Iter1, Iter2>   // constexpr after c++17
     15 //   mismatch(Iter1 first1, Iter1 last1, Iter2 first2);
     16 //
     17 // template<InputIterator Iter1, InputIterator Iter2Pred>
     18 //   constexpr pair<Iter1, Iter2>   // constexpr after c++17
     19 //   mismatch(Iter1 first1, Iter1 last1, Iter2 first2, Iter2 last2); // C++14
     20 
     21 #include <algorithm>
     22 #include <cassert>
     23 
     24 #include "test_macros.h"
     25 #include "test_iterators.h"
     26 
     27 #if TEST_STD_VER > 17
     28 TEST_CONSTEXPR bool test_constexpr() {
     29     int ia[] = {1, 3, 6, 7};
     30     int ib[] = {1, 3};
     31     int ic[] = {1, 3, 5, 7};
     32     typedef input_iterator<int*>         II;
     33     typedef bidirectional_iterator<int*> BI;
     34 
     35     auto p1 = std::mismatch(std::begin(ia), std::end(ia), std::begin(ic));
     36     if (p1.first != ia+2 || p1.second != ic+2)
     37         return false;
     38 
     39     auto p2 = std::mismatch(std::begin(ia), std::end(ia), std::begin(ic), std::end(ic));
     40     if (p2.first != ia+2 || p2.second != ic+2)
     41         return false;
     42 
     43     auto p3 = std::mismatch(std::begin(ib), std::end(ib), std::begin(ic));
     44     if (p3.first != ib+2 || p3.second != ic+2)
     45         return false;
     46 
     47     auto p4 = std::mismatch(std::begin(ib), std::end(ib), std::begin(ic), std::end(ic));
     48     if (p4.first != ib+2 || p4.second != ic+2)
     49         return false;
     50 
     51     auto p5 = std::mismatch(II(std::begin(ib)), II(std::end(ib)), II(std::begin(ic)));
     52     if (p5.first != II(ib+2) || p5.second != II(ic+2))
     53         return false;
     54     auto p6 = std::mismatch(BI(std::begin(ib)), BI(std::end(ib)), BI(std::begin(ic)), BI(std::end(ic)));
     55     if (p6.first != BI(ib+2) || p6.second != BI(ic+2))
     56         return false;
     57 
     58     return true;
     59     }
     60 #endif
     61 
     62 int main()
     63 {
     64     int ia[] = {0, 1, 2, 2, 0, 1, 2, 3};
     65     const unsigned sa = sizeof(ia)/sizeof(ia[0]);
     66     int ib[] = {0, 1, 2, 3, 0, 1, 2, 3};
     67     const unsigned sb = sizeof(ib)/sizeof(ib[0]); ((void)sb); // unused in C++11
     68 
     69     typedef input_iterator<const int*> II;
     70     typedef random_access_iterator<const int*>  RAI;
     71 
     72     assert(std::mismatch(II(ia), II(ia + sa), II(ib))
     73             == (std::pair<II, II>(II(ia+3), II(ib+3))));
     74 
     75     assert(std::mismatch(RAI(ia), RAI(ia + sa), RAI(ib))
     76             == (std::pair<RAI, RAI>(RAI(ia+3), RAI(ib+3))));
     77 
     78 #if TEST_STD_VER > 11 // We have the four iteration version
     79     assert(std::mismatch(II(ia), II(ia + sa), II(ib), II(ib+sb))
     80             == (std::pair<II, II>(II(ia+3), II(ib+3))));
     81 
     82     assert(std::mismatch(RAI(ia), RAI(ia + sa), RAI(ib), RAI(ib+sb))
     83             == (std::pair<RAI, RAI>(RAI(ia+3), RAI(ib+3))));
     84 
     85 
     86     assert(std::mismatch(II(ia), II(ia + sa), II(ib), II(ib+2))
     87             == (std::pair<II, II>(II(ia+2), II(ib+2))));
     88 #endif
     89 
     90 #if TEST_STD_VER > 17
     91     static_assert(test_constexpr());
     92 #endif
     93 }