| 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 | |
| 18 | template <typename T> |
| 19 | BOOST_CXX14_CONSTEXPR bool eq ( const T& a, const T& b ) { return a == b; } |
| 20 | |
| 21 | template <typename T> |
| 22 | BOOST_CXX14_CONSTEXPR bool never_eq ( const T&, const T& ) { return false; } |
| 23 | |
| 24 | namespace ba = boost::algorithm; |
| 25 | |
| 26 | template <typename Iter1, typename Iter2> |
| 27 | BOOST_CXX14_CONSTEXPR bool iter_eq ( std::pair<Iter1, Iter2> pr, Iter1 first, Iter2 second ) { |
| 28 | return pr.first == first && pr.second == second; |
| 29 | } |
| 30 | |
| 31 | void test_mismatch () |
| 32 | { |
| 33 | // Note: The literal values here are tested against directly, careful if you change them: |
| 34 | BOOST_CXX14_CONSTEXPR 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<const int *>(num), input_iterator<const int *>(num), |
| 41 | input_iterator<const int *>(num), input_iterator<const int *>(num)), |
| 42 | input_iterator<const int *>(num), input_iterator<const int *>(num))); |
| 43 | BOOST_CHECK ( iter_eq ( |
| 44 | ba::mismatch ( input_iterator<const int *>(num), input_iterator<const int *>(num), |
| 45 | input_iterator<const int *>(num), input_iterator<const int *>(num), |
| 46 | never_eq<int> ), |
| 47 | input_iterator<const int *>(num), input_iterator<const int *>(num))); |
| 48 | |
| 49 | BOOST_CHECK ( iter_eq ( |
| 50 | ba::mismatch ( random_access_iterator<const int *>(num), random_access_iterator<const int *>(num), |
| 51 | random_access_iterator<const int *>(num), random_access_iterator<const int *>(num), |
| 52 | never_eq<int> ), |
| 53 | random_access_iterator<const int *>(num), random_access_iterator<const int *>(num))); |
| 54 | |
| 55 | // Empty vs. non-empty mismatch immediately |
| 56 | BOOST_CHECK ( iter_eq ( |
| 57 | ba::mismatch ( input_iterator<const int *>(num), input_iterator<const int *>(num), |
| 58 | input_iterator<const int *>(num), input_iterator<const int *>(num + 1)), |
| 59 | input_iterator<const int *>(num), input_iterator<const int *>(num))); |
| 60 | |
| 61 | BOOST_CHECK ( iter_eq ( |
| 62 | ba::mismatch ( input_iterator<const int *>(num + 1), input_iterator<const int *>(num + 2), |
| 63 | input_iterator<const int *>(num), input_iterator<const int *>(num)), |
| 64 | input_iterator<const int *>(num + 1), input_iterator<const int *>(num))); |
| 65 | |
| 66 | BOOST_CHECK ( iter_eq ( |
| 67 | ba::mismatch ( random_access_iterator<const int *>(num + 1), random_access_iterator<const int *>(num + 2), |
| 68 | random_access_iterator<const int *>(num), random_access_iterator<const int *>(num)), |
| 69 | random_access_iterator<const int *>(num + 1), random_access_iterator<const 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<const int *>(num), input_iterator<const int *>(num + 1), |
| 74 | input_iterator<const int *>(num), input_iterator<const int *>(num + 1)), |
| 75 | input_iterator<const int *>(num + 1), input_iterator<const int *>(num + 1))); |
| 76 | |
| 77 | BOOST_CHECK ( iter_eq ( |
| 78 | ba::mismatch ( input_iterator<const int *>(num), input_iterator<const int *>(num + 1), |
| 79 | input_iterator<const int *>(num), input_iterator<const int *>(num + 1), |
| 80 | eq<int> ), |
| 81 | input_iterator<const int *>(num + 1), input_iterator<const int *>(num + 1))); |
| 82 | |
| 83 | BOOST_CHECK ( iter_eq ( |
| 84 | ba::mismatch ( random_access_iterator<const int *>(num), random_access_iterator<const int *>(num + 1), |
| 85 | random_access_iterator<const int *>(num), random_access_iterator<const int *>(num + 1), |
| 86 | eq<int> ), |
| 87 | random_access_iterator<const int *>(num + 1), random_access_iterator<const int *>(num + 1))); |
| 88 | |
| 89 | |
| 90 | BOOST_CHECK ( iter_eq ( |
| 91 | ba::mismatch ( input_iterator<const int *>(num), input_iterator<const int *>(num + 1), |
| 92 | input_iterator<const int *>(num), input_iterator<const int *>(num + 1), |
| 93 | never_eq<int> ), |
| 94 | input_iterator<const int *>(num), input_iterator<const int *>(num))); |
| 95 | |
| 96 | BOOST_CHECK ( iter_eq ( |
| 97 | ba::mismatch ( random_access_iterator<const int *>(num), random_access_iterator<const int *>(num + 1), |
| 98 | random_access_iterator<const int *>(num), random_access_iterator<const int *>(num + 1), |
| 99 | never_eq<int> ), |
| 100 | random_access_iterator<const int *>(num), random_access_iterator<const int *>(num))); |
| 101 | |
| 102 | BOOST_CHECK ( iter_eq ( |
| 103 | ba::mismatch ( input_iterator<const int *>(num), input_iterator<const int *>(num + 1), |
| 104 | input_iterator<const int *>(num + 1), input_iterator<const int *>(num + 2)), |
| 105 | input_iterator<const int *>(num + 1), input_iterator<const int *>(num + 2))); |
| 106 | |
| 107 | BOOST_CHECK ( iter_eq ( |
| 108 | ba::mismatch ( input_iterator<const int *>(num), input_iterator<const int *>(num + 1), |
| 109 | input_iterator<const int *>(num + 1), input_iterator<const int *>(num + 2), |
| 110 | eq<int> ), |
| 111 | input_iterator<const int *>(num + 1), input_iterator<const int *>(num + 2))); |
| 112 | |
| 113 | BOOST_CHECK ( iter_eq ( |
| 114 | ba::mismatch ( input_iterator<const int *>(num + 2), input_iterator<const int *>(num + 3), |
| 115 | input_iterator<const int *>(num), input_iterator<const int *>(num + 1)), |
| 116 | input_iterator<const int *>(num + 2), input_iterator<const int *>(num))); |
| 117 | |
| 118 | BOOST_CHECK ( iter_eq ( |
| 119 | ba::mismatch ( input_iterator<const int *>(num + 2), input_iterator<const int *>(num + 3), |
| 120 | input_iterator<const int *>(num), input_iterator<const int *>(num + 1), |
| 121 | eq<int> ), |
| 122 | input_iterator<const int *>(num + 2), input_iterator<const int *>(num))); |
| 123 | |
| 124 | |
| 125 | |
| 126 | // Identical long sequences are equal. |
| 127 | BOOST_CHECK ( iter_eq ( |
| 128 | ba::mismatch ( input_iterator<const int *>(num), input_iterator<const int *>(num + sz), |
| 129 | input_iterator<const int *>(num), input_iterator<const int *>(num + sz)), |
| 130 | input_iterator<const int *>(num + sz), input_iterator<const int *>(num + sz))); |
| 131 | |
| 132 | BOOST_CHECK ( iter_eq ( |
| 133 | ba::mismatch ( input_iterator<const int *>(num), input_iterator<const int *>(num + sz), |
| 134 | input_iterator<const int *>(num), input_iterator<const int *>(num + sz), |
| 135 | eq<int> ), |
| 136 | input_iterator<const int *>(num + sz), input_iterator<const int *>(num + sz))); |
| 137 | |
| 138 | BOOST_CHECK ( iter_eq ( |
| 139 | ba::mismatch ( input_iterator<const int *>(num), input_iterator<const int *>(num + sz), |
| 140 | input_iterator<const int *>(num), input_iterator<const int *>(num + sz), |
| 141 | never_eq<int> ), |
| 142 | input_iterator<const int *>(num), input_iterator<const int *>(num))); |
| 143 | |
| 144 | BOOST_CHECK ( iter_eq ( |
| 145 | ba::mismatch ( input_iterator<const int *>(num), input_iterator<const int *>(num + sz), |
| 146 | random_access_iterator<const int *>(num), random_access_iterator<const int *>(num + sz), |
| 147 | never_eq<int> ), |
| 148 | input_iterator<const int *>(num), random_access_iterator<const int *>(num))); |
| 149 | |
| 150 | // Different sequences are different |
| 151 | BOOST_CHECK ( iter_eq ( |
| 152 | ba::mismatch ( input_iterator<const int *>(num + 1), input_iterator<const int *>(num + sz), |
| 153 | input_iterator<const int *>(num), input_iterator<const int *>(num + sz)), |
| 154 | input_iterator<const int *>(num + 2), input_iterator<const int *>(num + 1))); |
| 155 | |
| 156 | BOOST_CHECK ( iter_eq ( |
| 157 | ba::mismatch ( input_iterator<const int *>(num + 1), input_iterator<const int *>(num + sz), |
| 158 | input_iterator<const int *>(num), input_iterator<const int *>(num + sz), |
| 159 | eq<int> ), |
| 160 | input_iterator<const int *>(num + 2), input_iterator<const int *>(num + 1))); |
| 161 | |
| 162 | // Checks constexpr |
| 163 | BOOST_CXX14_CONSTEXPR bool res = ( |
| 164 | // No mismatch for empty |
| 165 | iter_eq ( |
| 166 | pr: ba::mismatch ( first1: input_iterator<const int *>(num), last1: input_iterator<const int *>(num), |
| 167 | first2: input_iterator<const int *>(num), last2: input_iterator<const int *>(num)), |
| 168 | first: input_iterator<const int *>(num), second: input_iterator<const int *>(num)) |
| 169 | // Empty vs. non-empty mismatch immediately |
| 170 | && iter_eq ( |
| 171 | pr: ba::mismatch ( first1: input_iterator<const int *>(num), last1: input_iterator<const int *>(num), |
| 172 | first2: input_iterator<const int *>(num), last2: input_iterator<const int *>(num + 1)), |
| 173 | first: input_iterator<const int *>(num), second: input_iterator<const int *>(num)) |
| 174 | // Single element sequences are equal if they contain the same value |
| 175 | && iter_eq ( |
| 176 | pr: ba::mismatch ( first1: input_iterator<const int *>(num), last1: input_iterator<const int *>(num + 1), |
| 177 | first2: input_iterator<const int *>(num), last2: input_iterator<const int *>(num + 1), |
| 178 | pred: eq<int>), |
| 179 | first: input_iterator<const int *>(num + 1), second: input_iterator<const int *>(num + 1)) |
| 180 | // Identical long sequences are equal. |
| 181 | && iter_eq ( |
| 182 | pr: ba::mismatch ( first1: input_iterator<const int *>(num), last1: input_iterator<const int *>(num + sz), |
| 183 | first2: input_iterator<const int *>(num), last2: input_iterator<const int *>(num + sz), |
| 184 | pred: eq<int> ), |
| 185 | first: input_iterator<const int *>(num + sz), second: input_iterator<const int *>(num + sz)) |
| 186 | ); |
| 187 | |
| 188 | BOOST_CHECK ( res ); |
| 189 | } |
| 190 | |
| 191 | |
| 192 | BOOST_AUTO_TEST_CASE( test_main ) |
| 193 | { |
| 194 | test_mismatch (); |
| 195 | } |
| 196 | |