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 //   pair<Iter1, Iter2>
16 //   mismatch(Iter1 first1, Iter1 last1, Iter2 first2, Pred pred);
17 
18 #include <algorithm>
19 #include <functional>
20 #include <cassert>
21 
22 #include "test_macros.h"
23 #include "test_iterators.h"
24 #include "counting_predicates.hpp"
25 
26 #if TEST_STD_VER > 11
27 #define HAS_FOUR_ITERATOR_VERSION
28 #endif
29 
main()30 int main()
31 {
32     int ia[] = {0, 1, 2, 2, 0, 1, 2, 3};
33     const unsigned sa = sizeof(ia)/sizeof(ia[0]);
34     int ib[] = {0, 1, 2, 3, 0, 1, 2, 3};
35     const unsigned sb = sizeof(ib)/sizeof(ib[0]); ((void)sb); // unused in c++11
36 
37     typedef input_iterator<const int*> II;
38     typedef random_access_iterator<const int*>  RAI;
39     typedef std::equal_to<int> EQ;
40 
41     assert(std::mismatch(II(ia), II(ia + sa), II(ib), EQ())
42             == (std::pair<II, II>(II(ia+3), II(ib+3))));
43     assert(std::mismatch(RAI(ia), RAI(ia + sa), RAI(ib), EQ())
44             == (std::pair<RAI, RAI>(RAI(ia+3), RAI(ib+3))));
45 
46     binary_counting_predicate<EQ, int> bcp((EQ()));
47     assert(std::mismatch(RAI(ia), RAI(ia + sa), RAI(ib), std::ref(bcp))
48             == (std::pair<RAI, RAI>(RAI(ia+3), RAI(ib+3))));
49     assert(bcp.count() > 0 && bcp.count() < sa);
50     bcp.reset();
51 
52 #ifdef HAS_FOUR_ITERATOR_VERSION
53     assert(std::mismatch(II(ia), II(ia + sa), II(ib), II(ib + sb), EQ())
54             == (std::pair<II, II>(II(ia+3), II(ib+3))));
55     assert(std::mismatch(RAI(ia), RAI(ia + sa), RAI(ib), RAI(ib + sb), EQ())
56             == (std::pair<RAI, RAI>(RAI(ia+3), RAI(ib+3))));
57 
58     assert(std::mismatch(II(ia), II(ia + sa), II(ib), II(ib + sb), std::ref(bcp))
59             == (std::pair<II, II>(II(ia+3), II(ib+3))));
60     assert(bcp.count() > 0 && bcp.count() < std::min(sa, sb));
61 #endif
62 
63     assert(std::mismatch(ia, ia + sa, ib, EQ()) ==
64            (std::pair<int*,int*>(ia+3,ib+3)));
65 
66 #ifdef HAS_FOUR_ITERATOR_VERSION
67     assert(std::mismatch(ia, ia + sa, ib, ib + sb, EQ()) ==
68            (std::pair<int*,int*>(ia+3,ib+3)));
69     assert(std::mismatch(ia, ia + sa, ib, ib + 2, EQ()) ==
70            (std::pair<int*,int*>(ia+2,ib+2)));
71 #endif
72 }
73