1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
11 // UNSUPPORTED: c++03, c++11, c++14, c++17
13 // template<input_iterator I1, sentinel_for<I1> S1, input_iterator I2, sentinel_for<I2> S2,
14 // class Pred = ranges::equal_to, class Proj1 = identity, class Proj2 = identity>
15 // requires indirectly_comparable<I1, I2, Pred, Proj1, Proj2>
16 // constexpr bool ranges::equal(I1 first1, S1 last1, I2 first2, S2 last2,
18 // Proj1 proj1 = {}, Proj2 proj2 = {});
19 // template<input_range R1, input_range R2, class Pred = ranges::equal_to,
20 // class Proj1 = identity, class Proj2 = identity>
21 // requires indirectly_comparable<iterator_t<R1>, iterator_t<R2>, Pred, Proj1, Proj2>
22 // constexpr bool ranges::equal(R1&& r1, R2&& r2, Pred pred = {},
23 // Proj1 proj1 = {}, Proj2 proj2 = {});
32 #include "almost_satisfies_types.h"
33 #include "test_iterators.h"
35 template <class Iter1
, class Sent1
= sentinel_wrapper
<Iter1
>,
36 class Iter2
= Iter1
, class Sent2
= sentinel_wrapper
<Iter2
>>
37 concept HasEqualIt
= requires (Iter1 first1
, Sent1 last1
, Iter2 first2
, Sent2 last2
) {
38 std::ranges::equal(first1
, last1
, first2
, last2
);
41 static_assert(HasEqualIt
<int*>);
42 static_assert(!HasEqualIt
<InputIteratorNotDerivedFrom
>);
43 static_assert(!HasEqualIt
<InputIteratorNotIndirectlyReadable
>);
44 static_assert(!HasEqualIt
<InputIteratorNotInputOrOutputIterator
>);
45 static_assert(!HasEqualIt
<int*, int*, InputIteratorNotDerivedFrom
>);
46 static_assert(!HasEqualIt
<int*, int*, InputIteratorNotIndirectlyReadable
>);
47 static_assert(!HasEqualIt
<int*, int*, InputIteratorNotInputOrOutputIterator
>);
48 static_assert(!HasEqualIt
<int*, SentinelForNotSemiregular
>);
49 static_assert(!HasEqualIt
<int*, SentinelForNotWeaklyEqualityComparableWith
>);
50 static_assert(!HasEqualIt
<int*, int*, int*, SentinelForNotSemiregular
>);
51 static_assert(!HasEqualIt
<int*, int*, int*, SentinelForNotWeaklyEqualityComparableWith
>);
52 static_assert(!HasEqualIt
<int*, int*, int**>);
54 template <class Range1
, class Range2
>
55 concept HasEqualR
= requires (Range1 range1
, Range2 range2
) {
56 std::ranges::equal(range1
, range2
);
59 static_assert(HasEqualR
<UncheckedRange
<int*>, UncheckedRange
<int*>>);
60 static_assert(!HasEqualR
<InputRangeNotDerivedFrom
, UncheckedRange
<int*>>);
61 static_assert(!HasEqualR
<InputRangeNotIndirectlyReadable
, UncheckedRange
<int*>>);
62 static_assert(!HasEqualR
<InputRangeNotInputOrOutputIterator
, UncheckedRange
<int*>>);
63 static_assert(!HasEqualR
<InputRangeNotSentinelSemiregular
, UncheckedRange
<int*>>);
64 static_assert(!HasEqualR
<InputRangeNotSentinelEqualityComparableWith
, UncheckedRange
<int*>>);
65 static_assert(!HasEqualR
<UncheckedRange
<int*>, InputRangeNotDerivedFrom
>);
66 static_assert(!HasEqualR
<UncheckedRange
<int*>, InputRangeNotIndirectlyReadable
>);
67 static_assert(!HasEqualR
<UncheckedRange
<int*>, InputRangeNotInputOrOutputIterator
>);
68 static_assert(!HasEqualR
<UncheckedRange
<int*>, InputRangeNotSentinelSemiregular
>);
69 static_assert(!HasEqualR
<UncheckedRange
<int*>, InputRangeNotSentinelEqualityComparableWith
>);
70 static_assert(!HasEqualR
<UncheckedRange
<int*>, UncheckedRange
<int**>>);
72 template <class Iter1
, class Sent1
, class Iter2
, class Sent2
= Iter2
>
73 constexpr void test_iterators() {
76 int a
[] = {1, 2, 3, 4};
77 int b
[] = {1, 2, 3, 4};
78 std::same_as
<bool> decltype(auto) ret
= std::ranges::equal(Iter1(a
), Sent1(Iter1(a
+ 4)),
79 Iter2(b
), Sent2(Iter2(b
+ 4)));
83 int a
[] = {1, 2, 3, 4};
84 int b
[] = {1, 2, 3, 4};
85 auto range1
= std::ranges::subrange(Iter1(a
), Sent1(Iter1(a
+ 4)));
86 auto range2
= std::ranges::subrange(Iter2(b
), Sent2(Iter2(b
+ 4)));
87 std::same_as
<bool> decltype(auto) ret
= std::ranges::equal(range1
, range2
);
92 { // check that false is returned for non-equal ranges
94 int a
[] = {1, 2, 3, 4};
95 int b
[] = {1, 2, 4, 4};
96 assert(!std::ranges::equal(Iter1(a
), Sent1(Iter1(a
+ 4)), Iter2(b
), Sent2(Iter2(b
+ 4))));
99 int a
[] = {1, 2, 3, 4};
100 int b
[] = {1, 2, 4, 4};
101 auto range1
= std::ranges::subrange(Iter1(a
), Sent1(Iter1(a
+ 4)));
102 auto range2
= std::ranges::subrange(Iter2(b
), Sent2(Iter2(b
+ 4)));
103 assert(!std::ranges::equal(range1
, range2
));
107 { // check that the predicate is used (return true)
109 int a
[] = {1, 2, 3, 4};
110 int b
[] = {2, 3, 4, 5};
111 auto ret
= std::ranges::equal(Iter1(a
), Sent1(Iter1(a
+ 4)), Iter2(b
), Sent2(Iter2(b
+ 4)),
112 [](int l
, int r
) { return l
!= r
; });
116 int a
[] = {1, 2, 3, 4};
117 int b
[] = {2, 3, 4, 5};
118 auto range1
= std::ranges::subrange(Iter1(a
), Sent1(Iter1(a
+ 4)));
119 auto range2
= std::ranges::subrange(Iter2(b
), Sent2(Iter2(b
+ 4)));
120 auto ret
= std::ranges::equal(range1
, range2
, [](int l
, int r
) { return l
!= r
; });
125 { // check that the predicate is used (return false)
127 int a
[] = {1, 2, 3, 4};
128 int b
[] = {2, 3, 3, 5};
129 auto ret
= std::ranges::equal(Iter1(a
), Sent1(Iter1(a
+ 4)), Iter2(b
), Sent2(Iter2(b
+ 4)),
130 [](int l
, int r
) { return l
!= r
; });
134 int a
[] = {1, 2, 3, 4};
135 int b
[] = {2, 3, 3, 5};
136 auto range1
= std::ranges::subrange(Iter1(a
), Sent1(Iter1(a
+ 4)));
137 auto range2
= std::ranges::subrange(Iter2(b
), Sent2(Iter2(b
+ 4)));
138 auto ret
= std::ranges::equal(range1
, range2
, [](int l
, int r
) { return l
!= r
; });
143 { // check that the projections are used
145 int a
[] = {1, 2, 3, 4, 5};
146 int b
[] = {6, 10, 14, 18, 22};
147 auto ret
= std::ranges::equal(Iter1(a
), Sent1(Iter1(a
+ 5)),
148 Iter2(b
), Sent2(Iter2(b
+ 5)),
150 [](int i
) { return i
* 4; },
151 [](int i
) { return i
- 2; });
155 int a
[] = {1, 2, 3, 4, 5};
156 int b
[] = {6, 10, 14, 18, 22};
157 auto range1
= std::ranges::subrange(Iter1(a
), Sent1(Iter1(a
+ 5)));
158 auto range2
= std::ranges::subrange(Iter2(b
), Sent2(Iter2(b
+ 5)));
159 auto ret
= std::ranges::equal(range1
,
162 [](int i
) { return i
* 4; },
163 [](int i
) { return i
- 2; });
168 { // check that different sized ranges work
170 int a
[] = {4, 3, 2, 1};
171 int b
[] = {4, 3, 2, 1, 5, 6, 7};
172 auto ret
= std::ranges::equal(Iter1(a
), Sent1(Iter1(a
+ 4)), Iter2(b
), Sent2(Iter2(b
+ 7)));
176 int a
[] = {4, 3, 2, 1};
177 int b
[] = {4, 3, 2, 1, 5, 6, 7};
178 auto range1
= std::ranges::subrange(Iter1(a
), Sent1(Iter1(a
+ 4)));
179 auto range2
= std::ranges::subrange(Iter2(b
), Sent2(Iter2(b
+ 7)));
180 auto ret
= std::ranges::equal(range1
, range2
);
185 { // check that two ranges with the same size but different values are different
187 int a
[] = {4, 6, 34, 76, 5};
188 int b
[] = {4, 6, 34, 67, 5};
189 auto ret
= std::ranges::equal(Iter1(a
), Sent1(Iter1(a
+ 5)), Iter2(b
), Sent2(Iter2(b
+ 5)));
193 int a
[] = {4, 6, 34, 76, 5};
194 int b
[] = {4, 6, 34, 67, 5};
195 auto range1
= std::ranges::subrange(Iter1(a
), Sent1(Iter1(a
+ 5)));
196 auto range2
= std::ranges::subrange(Iter2(b
), Sent2(Iter2(b
+ 5)));
197 auto ret
= std::ranges::equal(range1
, range2
);
202 { // check that two empty ranges work
204 std::array
<int, 0> a
= {};
205 std::array
<int, 0> b
= {};
206 auto ret
= std::ranges::equal(Iter1(a
.data()), Sent1(Iter1(a
.data())), Iter2(b
.data()), Sent2(Iter2(b
.data())));
210 std::array
<int, 0> a
= {};
211 std::array
<int, 0> b
= {};
212 auto range1
= std::ranges::subrange(Iter1(a
.data()), Sent1(Iter1(a
.data())));
213 auto range2
= std::ranges::subrange(Iter2(b
.data()), Sent2(Iter2(b
.data())));
214 auto ret
= std::ranges::equal(range1
, range2
);
219 { // check that it works with the first range empty
221 std::array
<int, 0> a
= {};
223 auto ret
= std::ranges::equal(Iter1(a
.data()), Sent1(Iter1(a
.data())), Iter2(b
), Sent2(Iter2(b
+ 2)));
227 std::array
<int, 0> a
= {};
229 auto range1
= std::ranges::subrange(Iter1(a
.data()), Sent1(Iter1(a
.data())));
230 auto range2
= std::ranges::subrange(Iter2(b
), Sent2(Iter2(b
+ 2)));
231 auto ret
= std::ranges::equal(range1
, range2
);
236 { // check that it works with the second range empty
239 std::array
<int, 0> b
= {};
240 auto ret
= std::ranges::equal(Iter1(a
), Sent1(Iter1(a
+ 2)), Iter2(b
.data()), Sent2(Iter2(b
.data())));
245 std::array
<int, 0> b
= {};
246 auto range1
= std::ranges::subrange(Iter1(a
), Sent1(Iter1(a
+ 2)));
247 auto range2
= std::ranges::subrange(Iter2(b
.data()), Sent2(Iter2(b
.data())));
248 auto ret
= std::ranges::equal(range1
, range2
);
254 template<class Iter1
, class Sent1
= Iter1
>
255 constexpr void test_iterators2() {
256 test_iterators
<Iter1
, Sent1
, cpp17_input_iterator
<int*>, sentinel_wrapper
<cpp17_input_iterator
<int*>>>();
257 test_iterators
<Iter1
, Sent1
, cpp20_input_iterator
<int*>, sentinel_wrapper
<cpp20_input_iterator
<int*>>>();
258 test_iterators
<Iter1
, Sent1
, forward_iterator
<int*>>();
259 test_iterators
<Iter1
, Sent1
, bidirectional_iterator
<int*>>();
260 test_iterators
<Iter1
, Sent1
, random_access_iterator
<int*>>();
261 test_iterators
<Iter1
, Sent1
, contiguous_iterator
<int*>>();
262 test_iterators
<Iter1
, Sent1
, int*>();
263 test_iterators
<Iter1
, Sent1
, const int*>();
266 constexpr bool test() {
267 test_iterators2
<cpp17_input_iterator
<int*>, sentinel_wrapper
<cpp17_input_iterator
<int*>>>();
268 test_iterators2
<cpp20_input_iterator
<int*>, sentinel_wrapper
<cpp20_input_iterator
<int*>>>();
269 test_iterators2
<forward_iterator
<int*>>();
270 test_iterators2
<bidirectional_iterator
<int*>>();
271 test_iterators2
<random_access_iterator
<int*>>();
272 test_iterators2
<contiguous_iterator
<int*>>();
273 test_iterators2
<int*>();
274 test_iterators2
<const int*>();
276 { // check that std::invoke is used
278 constexpr S(int i_
) : i(i_
) {}
279 constexpr bool equal(int o
) { return i
== o
; }
280 constexpr S
& identity() { return *this; }
286 auto ret
= std::ranges::equal(a
, a
+ 3, b
, b
+ 3, &S::equal
, &S::identity
, &S::i
);
292 auto ret
= std::ranges::equal(a
, b
, &S::equal
, &S::identity
, &S::i
);
297 { // check that the complexity requirements are met
301 int b
[] = {1, 2, 3, 4};
304 auto pred
= [&](int l
, int r
) { ++predCount
; return l
== r
; };
305 auto proj
= [&](int i
) { ++projCount
; return i
; };
306 auto ret
= std::ranges::equal(a
, a
+ 3, b
, b
+ 4, pred
, proj
, proj
);
308 assert(predCount
== 0);
309 assert(projCount
== 0);
313 int b
[] = {1, 2, 3, 4};
316 auto pred
= [&](int l
, int r
) { ++predCount
; return l
== r
; };
317 auto proj
= [&](int i
) { ++projCount
; return i
; };
318 auto ret
= std::ranges::equal(a
, b
, pred
, proj
, proj
);
320 assert(predCount
== 0);
321 assert(projCount
== 0);
325 { // not a sized sentinel
328 int b
[] = {1, 2, 3, 4};
331 auto pred
= [&](int l
, int r
) { ++predCount
; return l
== r
; };
332 auto proj
= [&](int i
) { ++projCount
; return i
; };
333 auto ret
= std::ranges::equal(a
, sentinel_wrapper(a
+ 3), b
, sentinel_wrapper(b
+ 4), pred
, proj
, proj
);
335 assert(predCount
<= 4);
336 assert(projCount
<= 7);
340 int b
[] = {1, 2, 3, 4};
343 auto pred
= [&](int l
, int r
) { ++predCount
; return l
== r
; };
344 auto proj
= [&](int i
) { ++projCount
; return i
; };
345 auto range1
= std::ranges::subrange(a
, sentinel_wrapper(a
+ 3));
346 auto range2
= std::ranges::subrange(b
, sentinel_wrapper(b
+ 4));
347 auto ret
= std::ranges::equal(range1
, range2
, pred
, proj
, proj
);
349 assert(predCount
<= 4);
350 assert(projCount
<= 7);
360 auto pred
= [&](int l
, int r
) { ++predCount
; return l
== r
; };
361 auto proj
= [&](int i
) { ++projCount
; return i
; };
362 auto ret
= std::ranges::equal(a
, a
+ 3, b
, b
+ 3, pred
, proj
, proj
);
364 assert(predCount
== 3);
365 assert(projCount
== 6);
372 auto pred
= [&](int l
, int r
) { ++predCount
; return l
== r
; };
373 auto proj
= [&](int i
) { ++projCount
; return i
; };
374 auto ret
= std::ranges::equal(a
, b
, pred
, proj
, proj
);
376 assert(predCount
== 3);
377 assert(projCount
== 6);
385 int main(int, char**) {
387 static_assert(test());