1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
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);
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
25 #include "test_macros.h"
26 #include "test_iterators.h"
27 #include "counting_predicates.h"
30 TEST_CONSTEXPR
bool eq(int a
, int b
) { return a
== b
; }
32 TEST_CONSTEXPR
bool test_constexpr() {
33 int ia
[] = {1, 3, 6, 7};
35 int ic
[] = {1, 3, 5, 7};
36 typedef input_iterator
<int*> II
;
37 typedef bidirectional_iterator
<int*> BI
;
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)
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)
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)
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)
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))
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))
68 #define HAS_FOUR_ITERATOR_VERSION
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
78 typedef input_iterator
<const int*> II
;
79 typedef random_access_iterator
<const int*> RAI
;
80 typedef std::equal_to
<int> EQ
;
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))));
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
);
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))));
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
));
104 assert(std::mismatch(ia
, ia
+ sa
, ib
, EQ()) ==
105 (std::pair
<int*,int*>(ia
+3,ib
+3)));
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)));
114 #if TEST_STD_VER > 17
115 static_assert(test_constexpr());