1 // Copyright (C) 2013-2021 Free Software Foundation, Inc.
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)
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.
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/>.
20 // { dg-do run { target c++14 } }
23 #include <testsuite_hooks.h>
24 #include <testsuite_iterators.h>
26 using __gnu_test::test_container
;
27 using __gnu_test::input_iterator_wrapper
;
29 typedef test_container
<int, input_iterator_wrapper
> Container
;
31 int array1
[] = {0, 1};
32 int array2
[] = {1, 0};
33 int array3
[] = {1, 0, 1};
39 bool operator()(int l
, int r
)
46 int equal_to::count
= 0;
48 bool __attribute__((unused
)) test
= false;
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 );
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
);
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 );
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
);
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 );
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
);
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 );
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 );
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 );
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 );
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 );
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 );
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 );