1/*
2 Copyright (c) Marshall Clow 2013.
3
4 Distributed under the Boost Software License, Version 1.0. (See accompanying
5 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6
7 For more information, see http://www.boost.org
8*/
9
10#include <boost/config.hpp>
11#include <boost/algorithm/cxx14/mismatch.hpp>
12
13#include "iterator_test.hpp"
14
15#define BOOST_TEST_MAIN
16#include <boost/test/unit_test.hpp>
17
18template <typename T>
19bool eq ( const T& a, const T& b ) { return a == b; }
20
21template <typename T>
22bool never_eq ( const T&, const T& ) { return false; }
23
24namespace ba = boost::algorithm;
25
26template <typename Iter1, typename Iter2>
27bool iter_eq ( std::pair<Iter1, Iter2> pr, Iter1 first, Iter2 second ) {
28 return pr.first == first && pr.second == second;
29 }
30
31void test_mismatch ()
32{
33// Note: The literal values here are tested against directly, careful if you change them:
34 int num[] = { 1, 1, 2, 3, 5 };
35 const int sz = sizeof (num)/sizeof(num[0]);
36
37
38// No mismatch for empty sequences
39 BOOST_CHECK ( iter_eq (
40 ba::mismatch ( input_iterator<int *>(num), input_iterator<int *>(num),
41 input_iterator<int *>(num), input_iterator<int *>(num)),
42 input_iterator<int *>(num), input_iterator<int *>(num)));
43 BOOST_CHECK ( iter_eq (
44 ba::mismatch ( input_iterator<int *>(num), input_iterator<int *>(num),
45 input_iterator<int *>(num), input_iterator<int *>(num),
46 never_eq<int> ),
47 input_iterator<int *>(num), input_iterator<int *>(num)));
48
49 BOOST_CHECK ( iter_eq (
50 ba::mismatch ( random_access_iterator<int *>(num), random_access_iterator<int *>(num),
51 random_access_iterator<int *>(num), random_access_iterator<int *>(num),
52 never_eq<int> ),
53 random_access_iterator<int *>(num), random_access_iterator<int *>(num)));
54
55// Empty vs. non-empty mismatch immediately
56 BOOST_CHECK ( iter_eq (
57 ba::mismatch ( input_iterator<int *>(num), input_iterator<int *>(num),
58 input_iterator<int *>(num), input_iterator<int *>(num + 1)),
59 input_iterator<int *>(num), input_iterator<int *>(num)));
60
61 BOOST_CHECK ( iter_eq (
62 ba::mismatch ( input_iterator<int *>(num + 1), input_iterator<int *>(num + 2),
63 input_iterator<int *>(num), input_iterator<int *>(num)),
64 input_iterator<int *>(num + 1), input_iterator<int *>(num)));
65
66 BOOST_CHECK ( iter_eq (
67 ba::mismatch ( random_access_iterator<int *>(num + 1), random_access_iterator<int *>(num + 2),
68 random_access_iterator<int *>(num), random_access_iterator<int *>(num)),
69 random_access_iterator<int *>(num + 1), random_access_iterator<int *>(num)));
70
71// Single element sequences are equal if they contain the same value
72 BOOST_CHECK ( iter_eq (
73 ba::mismatch ( input_iterator<int *>(num), input_iterator<int *>(num + 1),
74 input_iterator<int *>(num), input_iterator<int *>(num + 1)),
75 input_iterator<int *>(num + 1), input_iterator<int *>(num + 1)));
76
77 BOOST_CHECK ( iter_eq (
78 ba::mismatch ( input_iterator<int *>(num), input_iterator<int *>(num + 1),
79 input_iterator<int *>(num), input_iterator<int *>(num + 1),
80 eq<int> ),
81 input_iterator<int *>(num + 1), input_iterator<int *>(num + 1)));
82
83 BOOST_CHECK ( iter_eq (
84 ba::mismatch ( random_access_iterator<int *>(num), random_access_iterator<int *>(num + 1),
85 random_access_iterator<int *>(num), random_access_iterator<int *>(num + 1),
86 eq<int> ),
87 random_access_iterator<int *>(num + 1), random_access_iterator<int *>(num + 1)));
88
89
90 BOOST_CHECK ( iter_eq (
91 ba::mismatch ( input_iterator<int *>(num), input_iterator<int *>(num + 1),
92 input_iterator<int *>(num), input_iterator<int *>(num + 1),
93 never_eq<int> ),
94 input_iterator<int *>(num), input_iterator<int *>(num)));
95
96 BOOST_CHECK ( iter_eq (
97 ba::mismatch ( random_access_iterator<int *>(num), random_access_iterator<int *>(num + 1),
98 random_access_iterator<int *>(num), random_access_iterator<int *>(num + 1),
99 never_eq<int> ),
100 random_access_iterator<int *>(num), random_access_iterator<int *>(num)));
101
102 BOOST_CHECK ( iter_eq (
103 ba::mismatch ( input_iterator<int *>(num), input_iterator<int *>(num + 1),
104 input_iterator<int *>(num + 1), input_iterator<int *>(num + 2)),
105 input_iterator<int *>(num + 1), input_iterator<int *>(num + 2)));
106
107 BOOST_CHECK ( iter_eq (
108 ba::mismatch ( input_iterator<int *>(num), input_iterator<int *>(num + 1),
109 input_iterator<int *>(num + 1), input_iterator<int *>(num + 2),
110 eq<int> ),
111 input_iterator<int *>(num + 1), input_iterator<int *>(num + 2)));
112
113 BOOST_CHECK ( iter_eq (
114 ba::mismatch ( input_iterator<int *>(num + 2), input_iterator<int *>(num + 3),
115 input_iterator<int *>(num), input_iterator<int *>(num + 1)),
116 input_iterator<int *>(num + 2), input_iterator<int *>(num)));
117
118 BOOST_CHECK ( iter_eq (
119 ba::mismatch ( input_iterator<int *>(num + 2), input_iterator<int *>(num + 3),
120 input_iterator<int *>(num), input_iterator<int *>(num + 1),
121 eq<int> ),
122 input_iterator<int *>(num + 2), input_iterator<int *>(num)));
123
124
125
126// Identical long sequences are equal.
127 BOOST_CHECK ( iter_eq (
128 ba::mismatch ( input_iterator<int *>(num), input_iterator<int *>(num + sz),
129 input_iterator<int *>(num), input_iterator<int *>(num + sz)),
130 input_iterator<int *>(num + sz), input_iterator<int *>(num + sz)));
131
132 BOOST_CHECK ( iter_eq (
133 ba::mismatch ( input_iterator<int *>(num), input_iterator<int *>(num + sz),
134 input_iterator<int *>(num), input_iterator<int *>(num + sz),
135 eq<int> ),
136 input_iterator<int *>(num + sz), input_iterator<int *>(num + sz)));
137
138 BOOST_CHECK ( iter_eq (
139 ba::mismatch ( input_iterator<int *>(num), input_iterator<int *>(num + sz),
140 input_iterator<int *>(num), input_iterator<int *>(num + sz),
141 never_eq<int> ),
142 input_iterator<int *>(num), input_iterator<int *>(num)));
143
144 BOOST_CHECK ( iter_eq (
145 ba::mismatch ( input_iterator<int *>(num), input_iterator<int *>(num + sz),
146 random_access_iterator<int *>(num), random_access_iterator<int *>(num + sz),
147 never_eq<int> ),
148 input_iterator<int *>(num), random_access_iterator<int *>(num)));
149
150// different sequences are different
151 BOOST_CHECK ( iter_eq (
152 ba::mismatch ( input_iterator<int *>(num + 1), input_iterator<int *>(num + sz),
153 input_iterator<int *>(num), input_iterator<int *>(num + sz)),
154 input_iterator<int *>(num + 2), input_iterator<int *>(num + 1)));
155
156 BOOST_CHECK ( iter_eq (
157 ba::mismatch ( input_iterator<int *>(num + 1), input_iterator<int *>(num + sz),
158 input_iterator<int *>(num), input_iterator<int *>(num + sz),
159 eq<int> ),
160 input_iterator<int *>(num + 2), input_iterator<int *>(num + 1)));
161
162}
163
164
165BOOST_AUTO_TEST_CASE( test_main )
166{
167 test_mismatch ();
168}
169