1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 // UNSUPPORTED: c++03, c++11, c++14, c++17
10 // UNSUPPORTED: libcpp-has-no-incomplete-ranges
11
12 // template <input_iterator I1, sentinel_for<_I1> S1, input_iterator I2, sentinel_for<_I2> S2,
13 // class Pred = ranges::equal_to, class Proj1 = identity, class Proj2 = identity>
14 // requires indirectly_comparable<I1, I2, Pred, Proj1, Proj2>
15 // constexpr mismatch_result<_I1, _I2>
16 // ranges::mismatch()(I1 first1, S1 last1, I2 first2, S2 last2, Pred pred = {}, Proj1 proj1 = {}, Proj2 proj2 = {})
17
18 // template <input_range R1, input_range R2,
19 // class Pred = ranges::equal_to, class Proj1 = identity, class Proj2 = identity>
20 // requires indirectly_comparable<iterator_t<R1>, iterator_t<R2>, Pred, Proj1, Proj2>
21 // constexpr mismatch_result<borrowed_iterator_t<R1>, borrowed_iterator_t<R2>>
22 // ranges::mismatch(R1&& r1, R2&& r2, Pred pred = {}, Proj1 proj1 = {}, Proj2 proj2 = {})
23
24 #include <algorithm>
25 #include <array>
26 #include <cassert>
27 #include <functional>
28 #include <ranges>
29
30 #include "test_iterators.h"
31
32 template <class Iter1, class Iter2>
test_iterators(Iter1 begin1,Iter1 end1,Iter2 begin2,Iter2 end2,int * expected1,int * expected2)33 constexpr void test_iterators(Iter1 begin1, Iter1 end1, Iter2 begin2, Iter2 end2, int* expected1, int* expected2) {
34 using Expected = std::ranges::mismatch_result<Iter1, Iter2>;
35 std::same_as<Expected> auto ret = std::ranges::mismatch(std::move(begin1), sentinel_wrapper<Iter1>(std::move(end1)),
36 std::move(begin2), sentinel_wrapper<Iter2>(std::move(end2)));
37 assert(base(ret.in1) == expected1);
38 assert(base(ret.in2) == expected2);
39 }
40
41 template <class Iter1, class Iter2>
test_iters()42 constexpr void test_iters() {
43 int a[] = {1, 2, 3, 4, 5};
44 int b[] = {1, 2, 3, 5, 4};
45
46 test_iterators(Iter1(a), Iter1(a + 5), Iter2(b), Iter2(b + 5), a + 3, b + 3);
47 }
48
test()49 constexpr bool test() {
50 test_iters<cpp17_input_iterator<int*>, cpp17_input_iterator<int*>>();
51 test_iters<cpp17_input_iterator<int*>, cpp20_input_iterator<int*>>();
52 test_iters<cpp17_input_iterator<int*>, forward_iterator<int*>>();
53 test_iters<cpp17_input_iterator<int*>, bidirectional_iterator<int*>>();
54 test_iters<cpp17_input_iterator<int*>, random_access_iterator<int*>>();
55 test_iters<cpp17_input_iterator<int*>, contiguous_iterator<int*>>();
56 test_iters<cpp17_input_iterator<int*>, int*>();
57
58 test_iters<cpp20_input_iterator<int*>, cpp17_input_iterator<int*>>();
59 test_iters<cpp20_input_iterator<int*>, cpp20_input_iterator<int*>>();
60 test_iters<cpp20_input_iterator<int*>, forward_iterator<int*>>();
61 test_iters<cpp20_input_iterator<int*>, bidirectional_iterator<int*>>();
62 test_iters<cpp20_input_iterator<int*>, random_access_iterator<int*>>();
63 test_iters<cpp20_input_iterator<int*>, contiguous_iterator<int*>>();
64 test_iters<cpp20_input_iterator<int*>, int*>();
65
66 test_iters<forward_iterator<int*>, cpp17_input_iterator<int*>>();
67 test_iters<forward_iterator<int*>, cpp20_input_iterator<int*>>();
68 test_iters<forward_iterator<int*>, forward_iterator<int*>>();
69 test_iters<forward_iterator<int*>, bidirectional_iterator<int*>>();
70 test_iters<forward_iterator<int*>, random_access_iterator<int*>>();
71 test_iters<forward_iterator<int*>, contiguous_iterator<int*>>();
72 test_iters<forward_iterator<int*>, int*>();
73
74 test_iters<bidirectional_iterator<int*>, cpp17_input_iterator<int*>>();
75 test_iters<bidirectional_iterator<int*>, cpp20_input_iterator<int*>>();
76 test_iters<bidirectional_iterator<int*>, forward_iterator<int*>>();
77 test_iters<bidirectional_iterator<int*>, bidirectional_iterator<int*>>();
78 test_iters<bidirectional_iterator<int*>, random_access_iterator<int*>>();
79 test_iters<bidirectional_iterator<int*>, contiguous_iterator<int*>>();
80 test_iters<bidirectional_iterator<int*>, int*>();
81
82 test_iters<random_access_iterator<int*>, cpp17_input_iterator<int*>>();
83 test_iters<random_access_iterator<int*>, cpp20_input_iterator<int*>>();
84 test_iters<random_access_iterator<int*>, forward_iterator<int*>>();
85 test_iters<random_access_iterator<int*>, bidirectional_iterator<int*>>();
86 test_iters<random_access_iterator<int*>, random_access_iterator<int*>>();
87 test_iters<random_access_iterator<int*>, contiguous_iterator<int*>>();
88 test_iters<random_access_iterator<int*>, int*>();
89
90 test_iters<contiguous_iterator<int*>, cpp17_input_iterator<int*>>();
91 test_iters<contiguous_iterator<int*>, cpp20_input_iterator<int*>>();
92 test_iters<contiguous_iterator<int*>, forward_iterator<int*>>();
93 test_iters<contiguous_iterator<int*>, bidirectional_iterator<int*>>();
94 test_iters<contiguous_iterator<int*>, random_access_iterator<int*>>();
95 test_iters<contiguous_iterator<int*>, contiguous_iterator<int*>>();
96 test_iters<contiguous_iterator<int*>, int*>();
97
98 test_iters<int*, cpp17_input_iterator<int*>>();
99 test_iters<int*, cpp20_input_iterator<int*>>();
100 test_iters<int*, forward_iterator<int*>>();
101 test_iters<int*, bidirectional_iterator<int*>>();
102 test_iters<int*, random_access_iterator<int*>>();
103 test_iters<int*, contiguous_iterator<int*>>();
104 test_iters<int*, int*>();
105
106 { // test with a range
107 std::array<int, 5> a = {1, 2, 3, 4, 5};
108 std::array<int, 5> b = {1, 2, 3, 5, 4};
109 using Expected = std::ranges::mismatch_result<int*, int*>;
110 std::same_as<Expected> auto ret = std::ranges::mismatch(a, b);
111 assert(ret.in1 == a.begin() + 3);
112 assert(ret.in2 == b.begin() + 3);
113 }
114
115 { // test with non-iterator sentinel
116 int a[] = {1, 2, 3, 4, 5};
117 int b[] = {1, 2, 3, 5, 4};
118
119 using Iter = int*;
120 using Sentinel = sentinel_wrapper<Iter>;
121 using Expected = std::ranges::mismatch_result<Iter, Iter>;
122
123 std::same_as<Expected> auto r = std::ranges::mismatch(Iter(a), Sentinel(a + 5), Iter(b), Sentinel(b + 5));
124 assert(r.in1 == a + 3);
125 assert(r.in2 == b + 3);
126 }
127
128 { // test with different array sizes
129 {
130 int a[] = {1, 2, 3};
131 int b[] = {1, 2};
132 test_iterators(a, a + 3, b, b + 2, a + 2, b + 2);
133 using Expected = std::ranges::mismatch_result<int*, int*>;
134 std::same_as<Expected> auto ret = std::ranges::mismatch(a, b);
135 assert(ret.in1 == a + 2);
136 assert(ret.in2 == b + 2);
137 }
138 {
139 int a[] = {1, 2};
140 int b[] = {1, 2, 3};
141 test_iterators(a, a + 2, b, b + 3, a + 2, b + 2);
142 using Expected = std::ranges::mismatch_result<int*, int*>;
143 std::same_as<Expected> auto ret = std::ranges::mismatch(a, b);
144 assert(ret.in1 == a + 2);
145 assert(ret.in2 == b + 2);
146 }
147 }
148
149 { // test with borrowed ranges
150 int r1[] = {1, 2, 3, 4, 5};
151 int r2[] = {1, 2, 3, 5, 4};
152
153 using Expected = std::ranges::mismatch_result<int*, int*>;
154 {
155 std::same_as<Expected> auto ret = std::ranges::mismatch(r1, std::views::all(r2));
156 assert(ret.in1 == r1 + 3);
157 assert(ret.in2 == r2 + 3);
158 }
159 {
160 std::same_as<Expected> auto ret = std::ranges::mismatch(std::views::all(r1), r2);
161 assert(ret.in1 == r1 + 3);
162 assert(ret.in2 == r2 + 3);
163 }
164 {
165 std::same_as<Expected> auto ret = std::ranges::mismatch(std::views::all(r1), std::views::all(r2));
166 assert(ret.in1 == r1 + 3);
167 assert(ret.in2 == r2 + 3);
168 }
169 }
170
171 { // test structured bindings
172 int a[] = {1, 2, 3, 4};
173 int b[] = {1, 2, 4, 8, 16};
174 auto [ai, bi] = std::ranges::mismatch(a, b);
175 assert(ai == a + 2);
176 assert(bi == b + 2);
177 auto [aj, bj] = std::ranges::mismatch(a, a+4, b, b+5);
178 assert(aj == a + 2);
179 assert(bj == b + 2);
180 }
181
182 { // test predicate
183 {
184 int a[] = {7, 6, 9, 3, 5, 1, 2, 4};
185 int b[] = {6, 5, 8, 2, 5, 1, 2, 4};
186 auto ret = std::ranges::mismatch(a, a + 8, b, b + 8, std::ranges::greater{});
187 assert(ret.in1 == a + 4);
188 assert(ret.in2 == b + 4);
189 assert(*ret.in1 == 5);
190 assert(*ret.in2 == 5);
191 }
192
193 {
194 int a[] = {7, 6, 9, 3, 5, 1, 2, 4};
195 int b[] = {6, 5, 8, 2, 5, 1, 2, 4};
196 auto ret = std::ranges::mismatch(a, b, std::ranges::greater{});
197 assert(ret.in1 == a + 4);
198 assert(ret.in2 == b + 4);
199 assert(*ret.in1 == 5);
200 assert(*ret.in2 == 5);
201 }
202 }
203
204 { // test projection
205 {
206 int a[] = {7, 6, 9, 3, 5, 1, 2, 4};
207 int b[] = {6, 5, 8, 2, 5, 1, 2, 4};
208 auto ret = std::ranges::mismatch(a, b,
209 std::ranges::greater{},
210 [](int i) { return i == 5 ? +100 : i; },
211 [](int i) { return i == 5 ? -100 : i; });
212 assert(ret.in1 == a + 5);
213 assert(ret.in2 == b + 5);
214 assert(*ret.in1 == 1);
215 assert(*ret.in2 == 1);
216 }
217 {
218 int a[] = {7, 6, 9, 3, 5, 1, 2, 4};
219 auto ret = std::ranges::mismatch(a, a,
220 std::less<double>{},
221 [](int i) { return i * 1.01; },
222 [c = 0](int i) mutable { return c++ < 5 ? i * 1.02 : i; });
223 assert(ret.in1 == a + 5);
224 assert(ret.in2 == a + 5);
225 assert(*ret.in1 == 1);
226 assert(*ret.in2 == 1);
227 }
228 {
229 int a[] = {7, 6, 9, 3, 5, 1, 2, 4};
230 int b[] = {6, 5, 8, 2, 5, 1, 2, 4};
231 auto ret = std::ranges::mismatch(a, a + 8, b, b + 8,
232 std::ranges::greater{},
233 [](int i) { return i == 5 ? +100 : i; },
234 [](int i) { return i == 5 ? -100 : i; });
235 assert(ret.in1 == a + 5);
236 assert(ret.in2 == b + 5);
237 assert(*ret.in1 == 1);
238 assert(*ret.in2 == 1);
239 }
240 {
241 int a[] = {7, 6, 9, 3, 5, 1, 2, 4};
242 auto ret = std::ranges::mismatch(a, a + 8, a, a + 8,
243 std::less<double>{},
244 [](int i) { return i * 1.01; },
245 [c = 0](int i) mutable { return c++ < 5 ? i * 1.02 : i; });
246 assert(ret.in1 == a + 5);
247 assert(ret.in2 == a + 5);
248 assert(*ret.in1 == 1);
249 assert(*ret.in2 == 1);
250 }
251 }
252
253 { // test predicate and projection call count
254 {
255 int pred_count = 0;
256 int proj1_count = 0;
257 int proj2_count = 0;
258 int a[] = {7, 6, 9, 3, 5, 1, 2, 4};
259 auto ret = std::ranges::mismatch(a, a,
260 [&](int lhs, int rhs) { ++pred_count; return lhs == rhs; },
261 [&](int i) { ++proj1_count; return i; },
262 [&](int i) { ++proj2_count; return i; });
263 assert(ret.in1 == a + 8);
264 assert(ret.in2 == a + 8);
265 assert(pred_count == 8);
266 assert(proj1_count == 8);
267 assert(proj2_count == 8);
268 }
269 {
270 int pred_count = 0;
271 int proj1_count = 0;
272 int proj2_count = 0;
273 int a[] = {7, 6, 9, 3, 5, 1, 2, 4};
274 auto ret = std::ranges::mismatch(a, a + 8, a, a + 8,
275 [&](int lhs, int rhs) { ++pred_count; return lhs == rhs; },
276 [&](int i) { ++proj1_count; return i; },
277 [&](int i) { ++proj2_count; return i; });
278 assert(ret.in1 == a + 8);
279 assert(ret.in2 == a + 8);
280 assert(pred_count == 8);
281 assert(proj1_count == 8);
282 assert(proj2_count == 8);
283 }
284 }
285
286 return true;
287 }
288
main(int,char **)289 int main(int, char**) {
290 test();
291 static_assert(test());
292
293 return 0;
294 }
295