| 1 |
// Copyright (C) 2013-2025 Free Software Foundation, Inc. |
| 2 |
// |
| 3 |
// This file is part of the GNU ISO C++ Library. This library is free |
| 4 |
// software; you can redistribute it and/or modify it under the |
| 5 |
// terms of the GNU General Public License as published by the |
| 6 |
// Free Software Foundation; either version 3, or (at your option) |
| 7 |
// any later version. |
| 8 |
|
| 9 |
// This library is distributed in the hope that it will be useful, |
| 10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
// GNU General Public License for more details. |
| 13 |
|
| 14 |
// You should have received a copy of the GNU General Public License along |
| 15 |
// with this library; see the file COPYING3. If not see |
| 16 |
// <http://www.gnu.org/licenses/>. |
| 17 |
|
| 18 |
// 25.2.10 [mismatch] |
| 19 |
|
| 20 |
// { dg-do run { target c++14 } } |
| 21 |
|
| 22 |
#include <algorithm> |
| 23 |
#include <testsuite_hooks.h> |
| 24 |
#include <testsuite_iterators.h> |
| 25 |
|
| 26 |
using __gnu_test::test_container; |
| 27 |
using __gnu_test::input_iterator_wrapper; |
| 28 |
|
| 29 |
typedef test_container<int, input_iterator_wrapper> Container; |
| 30 |
|
| 31 |
int array1[] = {0, 1}; |
| 32 |
int array2[] = {1, 0}; |
| 33 |
int array3[] = {1, 0, 1}; |
| 34 |
|
| 35 |
struct equal_to |
| 36 |
{ |
| 37 |
static int count; |
| 38 |
|
| 39 |
bool operator()(int l, int r) |
| 40 |
{ |
| 41 |
++count; |
| 42 |
return l == r; |
| 43 |
} |
| 44 |
} eq; |
| 45 |
|
| 46 |
int equal_to::count = 0; |
| 47 |
|
| 48 |
bool __attribute__((unused)) test = false; |
| 49 |
|
| 50 |
void test1() |
| 51 |
{ |
| 52 |
// empty ranges |
| 53 |
Container con1(array1, array1); |
| 54 |
Container con2(array2, array2); |
| 55 |
auto res = std::mismatch(con1.begin(), con1.end(), con2.begin(), con2.end()); |
| 56 |
VERIFY( res.first.ptr == array1 ); |
| 57 |
VERIFY( res.second.ptr == array2 ); |
| 58 |
res = std::mismatch(con1.begin(), con1.end(), con2.begin(), con2.end(), eq); |
| 59 |
VERIFY( res.first.ptr == array1 ); |
| 60 |
VERIFY( res.second.ptr == array2 ); |
| 61 |
VERIFY( equal_to::count == 0 ); |
| 62 |
} |
| 63 |
|
| 64 |
void test2() |
| 65 |
{ |
| 66 |
// first range empty, second non-empty |
| 67 |
Container con1(array1, array1); |
| 68 |
Container con2(array2, array2 + 2); |
| 69 |
auto res = std::mismatch(con1.begin(), con1.end(), con2.begin(), con2.end()); |
| 70 |
VERIFY( res.first.ptr == array1 ); |
| 71 |
VERIFY( res.second.ptr == array2 ); |
| 72 |
|
| 73 |
res = std::mismatch(con1.begin(), con1.end(), con2.begin(), con2.end(), eq); |
| 74 |
VERIFY( res.first.ptr == array1 ); |
| 75 |
VERIFY( res.second.ptr == array2 ); |
| 76 |
VERIFY( equal_to::count == 0 ); |
| 77 |
} |
| 78 |
|
| 79 |
void test3() |
| 80 |
{ |
| 81 |
// first range non-empty, second empty |
| 82 |
Container con1(array1, array1 + 2); |
| 83 |
Container con2(array2, array2); |
| 84 |
auto res = std::mismatch(con1.begin(), con1.end(), con2.begin(), con2.end()); |
| 85 |
VERIFY( res.first.ptr == array1 ); |
| 86 |
VERIFY( res.second.ptr == array2 ); |
| 87 |
|
| 88 |
res = std::mismatch(con1.begin(), con1.end(), con2.begin(), con2.end(), eq); |
| 89 |
VERIFY( res.first.ptr == array1 ); |
| 90 |
VERIFY( res.second.ptr == array2 ); |
| 91 |
VERIFY( equal_to::count == 0 ); |
| 92 |
} |
| 93 |
|
| 94 |
void test4() |
| 95 |
{ |
| 96 |
// non-empty, mismatching ranges |
| 97 |
Container con1(array1, array1 + 2); |
| 98 |
Container con2(array2, array2 + 2); |
| 99 |
auto res = std::mismatch(con1.begin(), con1.end(), con2.begin(), con2.end()); |
| 100 |
VERIFY( res.first.ptr == array1 ); |
| 101 |
VERIFY( res.second.ptr == array2 ); |
| 102 |
|
| 103 |
con1.bounds.first = array1; |
| 104 |
con2.bounds.first = array2; |
| 105 |
res = std::mismatch(con1.begin(), con1.end(), con2.begin(), con2.end(), eq); |
| 106 |
VERIFY( res.first.ptr == array1 ); |
| 107 |
VERIFY( res.second.ptr == array2 ); |
| 108 |
VERIFY( equal_to::count == 1 ); |
| 109 |
equal_to::count = 0; |
| 110 |
} |
| 111 |
|
| 112 |
void test5() |
| 113 |
{ |
| 114 |
// non-empty, matching ranges |
| 115 |
Container con3(array3, array3 + 2); |
| 116 |
Container con2(array2, array2 + 2); |
| 117 |
auto res = std::mismatch(con3.begin(), con3.end(), con2.begin(), con2.end()); |
| 118 |
VERIFY( res.first.ptr == array3 + 2 ); |
| 119 |
VERIFY( res.second.ptr == array2 + 2 ); |
| 120 |
|
| 121 |
con3.bounds.first = array3; |
| 122 |
con2.bounds.first = array2; |
| 123 |
res = std::mismatch(con3.begin(), con3.end(), con2.begin(), con2.end(), eq); |
| 124 |
VERIFY( res.first.ptr == array3 + 2 ); |
| 125 |
VERIFY( res.second.ptr == array2 + 2 ); |
| 126 |
VERIFY( equal_to::count == 2 ); |
| 127 |
equal_to::count = 0; |
| 128 |
} |
| 129 |
|
| 130 |
void test6() |
| 131 |
{ |
| 132 |
// non-empty, matching sub-ranges, first range longer |
| 133 |
Container con3(array3, array3 + 3); |
| 134 |
Container con2(array2, array2 + 2); |
| 135 |
auto res = std::mismatch(con3.begin(), con3.end(), con2.begin(), con2.end()); |
| 136 |
VERIFY( res.first.ptr == array3 + 2 ); |
| 137 |
VERIFY( res.second.ptr == array2 + 2 ); |
| 138 |
|
| 139 |
con3.bounds.first = array3; |
| 140 |
con2.bounds.first = array2; |
| 141 |
res = std::mismatch(con3.begin(), con3.end(), con2.begin(), con2.end(), eq); |
| 142 |
VERIFY( res.first.ptr == array3 + 2 ); |
| 143 |
VERIFY( res.second.ptr == array2 + 2 ); |
| 144 |
VERIFY( equal_to::count == 2 ); |
| 145 |
equal_to::count = 0; |
| 146 |
} |
| 147 |
|
| 148 |
void test7() |
| 149 |
{ |
| 150 |
// non-empty, matching sub-ranges, second range longer |
| 151 |
Container con3(array3, array3 + 3); |
| 152 |
Container con2(array2, array2 + 2); |
| 153 |
auto res = std::mismatch(con2.begin(), con2.end(), con3.begin(), con3.end()); |
| 154 |
VERIFY( res.first.ptr == array2 + 2 ); |
| 155 |
VERIFY( res.second.ptr == array3 + 2 ); |
| 156 |
|
| 157 |
con3.bounds.first = array3; |
| 158 |
con2.bounds.first = array2; |
| 159 |
res = std::mismatch(con2.begin(), con2.end(), con3.begin(), con3.end(), eq); |
| 160 |
VERIFY( res.first.ptr == array2 + 2 ); |
| 161 |
VERIFY( res.second.ptr == array3 + 2 ); |
| 162 |
VERIFY( equal_to::count == 2 ); |
| 163 |
equal_to::count = 0; |
| 164 |
} |
| 165 |
|
| 166 |
int main() |
| 167 |
{ |
| 168 |
test1(); |
| 169 |
test2(); |
| 170 |
test3(); |
| 171 |
test4(); |
| 172 |
test5(); |
| 173 |
test6(); |
| 174 |
test7(); |
| 175 |
} |