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 = {});
31 #include "almost_satisfies_types.h"
32 #include "test_iterators.h"
34 template <class Iter1
, class Sent1
= sentinel_wrapper
<Iter1
>,
35 class Iter2
= Iter1
, class Sent2
= sentinel_wrapper
<Iter2
>>
36 concept HasEqualIt
= requires (Iter1 first1
, Sent1 last1
, Iter2 first2
, Sent2 last2
) {
37 std::ranges::equal(first1
, last1
, first2
, last2
);
40 static_assert(HasEqualIt
<int*>);
41 static_assert(!HasEqualIt
<InputIteratorNotDerivedFrom
>);
42 static_assert(!HasEqualIt
<InputIteratorNotIndirectlyReadable
>);
43 static_assert(!HasEqualIt
<InputIteratorNotInputOrOutputIterator
>);
44 static_assert(!HasEqualIt
<int*, int*, InputIteratorNotDerivedFrom
>);
45 static_assert(!HasEqualIt
<int*, int*, InputIteratorNotIndirectlyReadable
>);
46 static_assert(!HasEqualIt
<int*, int*, InputIteratorNotInputOrOutputIterator
>);
47 static_assert(!HasEqualIt
<int*, SentinelForNotSemiregular
>);
48 static_assert(!HasEqualIt
<int*, SentinelForNotWeaklyEqualityComparableWith
>);
49 static_assert(!HasEqualIt
<int*, int*, int*, SentinelForNotSemiregular
>);
50 static_assert(!HasEqualIt
<int*, int*, int*, SentinelForNotWeaklyEqualityComparableWith
>);
51 static_assert(!HasEqualIt
<int*, int*, int**>);
53 template <class Range1
, class Range2
>
54 concept HasEqualR
= requires (Range1 range1
, Range2 range2
) {
55 std::ranges::equal(range1
, range2
);
58 static_assert(HasEqualR
<UncheckedRange
<int*>, UncheckedRange
<int*>>);
59 static_assert(!HasEqualR
<InputRangeNotDerivedFrom
, UncheckedRange
<int*>>);
60 static_assert(!HasEqualR
<InputRangeNotIndirectlyReadable
, UncheckedRange
<int*>>);
61 static_assert(!HasEqualR
<InputRangeNotInputOrOutputIterator
, UncheckedRange
<int*>>);
62 static_assert(!HasEqualR
<InputRangeNotSentinelSemiregular
, UncheckedRange
<int*>>);
63 static_assert(!HasEqualR
<InputRangeNotSentinelEqualityComparableWith
, UncheckedRange
<int*>>);
64 static_assert(!HasEqualR
<UncheckedRange
<int*>, InputRangeNotDerivedFrom
>);
65 static_assert(!HasEqualR
<UncheckedRange
<int*>, InputRangeNotIndirectlyReadable
>);
66 static_assert(!HasEqualR
<UncheckedRange
<int*>, InputRangeNotInputOrOutputIterator
>);
67 static_assert(!HasEqualR
<UncheckedRange
<int*>, InputRangeNotSentinelSemiregular
>);
68 static_assert(!HasEqualR
<UncheckedRange
<int*>, InputRangeNotSentinelEqualityComparableWith
>);
69 static_assert(!HasEqualR
<UncheckedRange
<int*>, UncheckedRange
<int**>>);
71 template <class Iter1
, class Sent1
, class Iter2
, class Sent2
= Iter2
>
72 constexpr void test_iterators() {
75 int a
[] = {1, 2, 3, 4};
76 int b
[] = {1, 2, 3, 4};
77 std::same_as
<bool> decltype(auto) ret
= std::ranges::equal(Iter1(a
), Sent1(Iter1(a
+ 4)),
78 Iter2(b
), Sent2(Iter2(b
+ 4)));
82 int a
[] = {1, 2, 3, 4};
83 int b
[] = {1, 2, 3, 4};
84 auto range1
= std::ranges::subrange(Iter1(a
), Sent1(Iter1(a
+ 4)));
85 auto range2
= std::ranges::subrange(Iter2(b
), Sent2(Iter2(b
+ 4)));
86 std::same_as
<bool> decltype(auto) ret
= std::ranges::equal(range1
, range2
);
91 { // check that false is returned for non-equal ranges
93 int a
[] = {1, 2, 3, 4};
94 int b
[] = {1, 2, 4, 4};
95 assert(!std::ranges::equal(Iter1(a
), Sent1(Iter1(a
+ 4)), Iter2(b
), Sent2(Iter2(b
+ 4))));
98 int a
[] = {1, 2, 3, 4};
99 int b
[] = {1, 2, 4, 4};
100 auto range1
= std::ranges::subrange(Iter1(a
), Sent1(Iter1(a
+ 4)));
101 auto range2
= std::ranges::subrange(Iter2(b
), Sent2(Iter2(b
+ 4)));
102 assert(!std::ranges::equal(range1
, range2
));
106 { // check that the predicate is used (return true)
108 int a
[] = {1, 2, 3, 4};
109 int b
[] = {2, 3, 4, 5};
110 auto ret
= std::ranges::equal(Iter1(a
), Sent1(Iter1(a
+ 4)), Iter2(b
), Sent2(Iter2(b
+ 4)),
111 [](int l
, int r
) { return l
!= r
; });
115 int a
[] = {1, 2, 3, 4};
116 int b
[] = {2, 3, 4, 5};
117 auto range1
= std::ranges::subrange(Iter1(a
), Sent1(Iter1(a
+ 4)));
118 auto range2
= std::ranges::subrange(Iter2(b
), Sent2(Iter2(b
+ 4)));
119 auto ret
= std::ranges::equal(range1
, range2
, [](int l
, int r
) { return l
!= r
; });
124 { // check that the predicate is used (return false)
126 int a
[] = {1, 2, 3, 4};
127 int b
[] = {2, 3, 3, 5};
128 auto ret
= std::ranges::equal(Iter1(a
), Sent1(Iter1(a
+ 4)), Iter2(b
), Sent2(Iter2(b
+ 4)),
129 [](int l
, int r
) { return l
!= r
; });
133 int a
[] = {1, 2, 3, 4};
134 int b
[] = {2, 3, 3, 5};
135 auto range1
= std::ranges::subrange(Iter1(a
), Sent1(Iter1(a
+ 4)));
136 auto range2
= std::ranges::subrange(Iter2(b
), Sent2(Iter2(b
+ 4)));
137 auto ret
= std::ranges::equal(range1
, range2
, [](int l
, int r
) { return l
!= r
; });
142 { // check that the projections are used
144 int a
[] = {1, 2, 3, 4, 5};
145 int b
[] = {6, 10, 14, 18, 22};
146 auto ret
= std::ranges::equal(Iter1(a
), Sent1(Iter1(a
+ 5)),
147 Iter2(b
), Sent2(Iter2(b
+ 5)),
149 [](int i
) { return i
* 4; },
150 [](int i
) { return i
- 2; });
154 int a
[] = {1, 2, 3, 4, 5};
155 int b
[] = {6, 10, 14, 18, 22};
156 auto range1
= std::ranges::subrange(Iter1(a
), Sent1(Iter1(a
+ 5)));
157 auto range2
= std::ranges::subrange(Iter2(b
), Sent2(Iter2(b
+ 5)));
158 auto ret
= std::ranges::equal(range1
,
161 [](int i
) { return i
* 4; },
162 [](int i
) { return i
- 2; });
167 { // check that different sized ranges work
169 int a
[] = {4, 3, 2, 1};
170 int b
[] = {4, 3, 2, 1, 5, 6, 7};
171 auto ret
= std::ranges::equal(Iter1(a
), Sent1(Iter1(a
+ 4)), Iter2(b
), Sent2(Iter2(b
+ 7)));
175 int a
[] = {4, 3, 2, 1};
176 int b
[] = {4, 3, 2, 1, 5, 6, 7};
177 auto range1
= std::ranges::subrange(Iter1(a
), Sent1(Iter1(a
+ 4)));
178 auto range2
= std::ranges::subrange(Iter2(b
), Sent2(Iter2(b
+ 7)));
179 auto ret
= std::ranges::equal(range1
, range2
);
184 { // check that two ranges with the same size but different values are different
186 int a
[] = {4, 6, 34, 76, 5};
187 int b
[] = {4, 6, 34, 67, 5};
188 auto ret
= std::ranges::equal(Iter1(a
), Sent1(Iter1(a
+ 5)), Iter2(b
), Sent2(Iter2(b
+ 5)));
192 int a
[] = {4, 6, 34, 76, 5};
193 int b
[] = {4, 6, 34, 67, 5};
194 auto range1
= std::ranges::subrange(Iter1(a
), Sent1(Iter1(a
+ 5)));
195 auto range2
= std::ranges::subrange(Iter2(b
), Sent2(Iter2(b
+ 5)));
196 auto ret
= std::ranges::equal(range1
, range2
);
201 { // check that two empty ranges work
205 auto ret
= std::ranges::equal(Iter1(a
), Sent1(Iter1(a
)), Iter2(b
), Sent2(Iter2(b
)));
211 auto range1
= std::ranges::subrange(Iter1(a
), Sent1(Iter1(a
)));
212 auto range2
= std::ranges::subrange(Iter2(b
), Sent2(Iter2(b
)));
213 auto ret
= std::ranges::equal(range1
, range2
);
218 { // check that it works with the first range empty
222 auto ret
= std::ranges::equal(Iter1(a
), Sent1(Iter1(a
)), Iter2(b
), Sent2(Iter2(b
+ 2)));
228 auto range1
= std::ranges::subrange(Iter1(a
), Sent1(Iter1(a
)));
229 auto range2
= std::ranges::subrange(Iter2(b
), Sent2(Iter2(b
+ 2)));
230 auto ret
= std::ranges::equal(range1
, range2
);
235 { // check that it works with the second range empty
239 auto ret
= std::ranges::equal(Iter1(a
), Sent1(Iter1(a
+ 2)), Iter2(b
), Sent2(Iter2(b
)));
245 auto range1
= std::ranges::subrange(Iter1(a
), Sent1(Iter1(a
+ 2)));
246 auto range2
= std::ranges::subrange(Iter2(b
), Sent2(Iter2(b
)));
247 auto ret
= std::ranges::equal(range1
, range2
);
253 template<class Iter1
, class Sent1
= Iter1
>
254 constexpr void test_iterators2() {
255 test_iterators
<Iter1
, Sent1
, cpp17_input_iterator
<int*>, sentinel_wrapper
<cpp17_input_iterator
<int*>>>();
256 test_iterators
<Iter1
, Sent1
, cpp20_input_iterator
<int*>, sentinel_wrapper
<cpp20_input_iterator
<int*>>>();
257 test_iterators
<Iter1
, Sent1
, forward_iterator
<int*>>();
258 test_iterators
<Iter1
, Sent1
, bidirectional_iterator
<int*>>();
259 test_iterators
<Iter1
, Sent1
, random_access_iterator
<int*>>();
260 test_iterators
<Iter1
, Sent1
, contiguous_iterator
<int*>>();
261 test_iterators
<Iter1
, Sent1
, int*>();
262 test_iterators
<Iter1
, Sent1
, const int*>();
265 constexpr bool test() {
266 test_iterators2
<cpp17_input_iterator
<int*>, sentinel_wrapper
<cpp17_input_iterator
<int*>>>();
267 test_iterators2
<cpp20_input_iterator
<int*>, sentinel_wrapper
<cpp20_input_iterator
<int*>>>();
268 test_iterators2
<forward_iterator
<int*>>();
269 test_iterators2
<bidirectional_iterator
<int*>>();
270 test_iterators2
<random_access_iterator
<int*>>();
271 test_iterators2
<contiguous_iterator
<int*>>();
272 test_iterators2
<int*>();
273 test_iterators2
<const int*>();
275 { // check that std::invoke is used
277 constexpr S(int i_
) : i(i_
) {}
278 constexpr bool equal(int o
) { return i
== o
; }
279 constexpr S
& identity() { return *this; }
285 auto ret
= std::ranges::equal(a
, a
+ 3, b
, b
+ 3, &S::equal
, &S::identity
, &S::i
);
291 auto ret
= std::ranges::equal(a
, b
, &S::equal
, &S::identity
, &S::i
);
296 { // check that the complexity requirements are met
300 int b
[] = {1, 2, 3, 4};
303 auto pred
= [&](int l
, int r
) { ++predCount
; return l
== r
; };
304 auto proj
= [&](int i
) { ++projCount
; return i
; };
305 auto ret
= std::ranges::equal(a
, a
+ 3, b
, b
+ 4, pred
, proj
, proj
);
307 assert(predCount
== 0);
308 assert(projCount
== 0);
312 int b
[] = {1, 2, 3, 4};
315 auto pred
= [&](int l
, int r
) { ++predCount
; return l
== r
; };
316 auto proj
= [&](int i
) { ++projCount
; return i
; };
317 auto ret
= std::ranges::equal(a
, b
, pred
, proj
, proj
);
319 assert(predCount
== 0);
320 assert(projCount
== 0);
324 { // not a sized sentinel
327 int b
[] = {1, 2, 3, 4};
330 auto pred
= [&](int l
, int r
) { ++predCount
; return l
== r
; };
331 auto proj
= [&](int i
) { ++projCount
; return i
; };
332 auto ret
= std::ranges::equal(a
, sentinel_wrapper(a
+ 3), b
, sentinel_wrapper(b
+ 4), pred
, proj
, proj
);
334 assert(predCount
<= 4);
335 assert(projCount
<= 7);
339 int b
[] = {1, 2, 3, 4};
342 auto pred
= [&](int l
, int r
) { ++predCount
; return l
== r
; };
343 auto proj
= [&](int i
) { ++projCount
; return i
; };
344 auto range1
= std::ranges::subrange(a
, sentinel_wrapper(a
+ 3));
345 auto range2
= std::ranges::subrange(b
, sentinel_wrapper(b
+ 4));
346 auto ret
= std::ranges::equal(range1
, range2
, pred
, proj
, proj
);
348 assert(predCount
<= 4);
349 assert(projCount
<= 7);
359 auto pred
= [&](int l
, int r
) { ++predCount
; return l
== r
; };
360 auto proj
= [&](int i
) { ++projCount
; return i
; };
361 auto ret
= std::ranges::equal(a
, a
+ 3, b
, b
+ 3, pred
, proj
, proj
);
363 assert(predCount
== 3);
364 assert(projCount
== 6);
371 auto pred
= [&](int l
, int r
) { ++predCount
; return l
== r
; };
372 auto proj
= [&](int i
) { ++projCount
; return i
; };
373 auto ret
= std::ranges::equal(a
, b
, pred
, proj
, proj
);
375 assert(predCount
== 3);
376 assert(projCount
== 6);
384 int main(int, char**) {
386 static_assert(test());