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