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, c++20
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::ends_with(I1 first1, S1 last1, I2 first2, S2 last2, Pred pred = {},
17 // Proj1 proj1 = {}, Proj2 proj2 = {});
18 // template<input_range R1, input_range R2, class Pred = ranges::equal_to, class Proj1 = identity,
19 // class Proj2 = identity>
20 // requires indirectly_comparable<iterator_t<R1>, iterator_t<R2>, Pred, Proj1, Proj2>
21 // constexpr bool ranges::ends_with(R1&& r1, R2&& r2, Pred pred = {},
22 // Proj1 proj1 = {}, Proj2 proj2 = {});
28 #include "almost_satisfies_types.h"
29 #include "test_iterators.h"
31 using namespace std::chrono
;
33 template <class Iter1
, class Sent1
= Iter1
, class Iter2
= int*, class Sent2
= Iter2
>
34 concept HasEndsWithIt
= requires(Iter1 first1
, Sent1 last1
, Iter2 first2
, Sent2 last2
) {
35 std::ranges::ends_with(first1
, last1
, first2
, last2
);
38 static_assert(HasEndsWithIt
<int*>);
39 static_assert(!HasEndsWithIt
<ForwardIteratorNotDerivedFrom
>);
40 static_assert(!HasEndsWithIt
<ForwardIteratorNotIncrementable
>);
41 static_assert(HasEndsWithIt
<int*, int*>);
42 static_assert(!HasEndsWithIt
<int*, SentinelForNotSemiregular
>);
43 static_assert(!HasEndsWithIt
<int*, int*, int**>); // not indirectly comparable
44 static_assert(!HasEndsWithIt
<int*, SentinelForNotWeaklyEqualityComparableWith
>);
45 static_assert(!HasEndsWithIt
<int*, int*, ForwardIteratorNotDerivedFrom
>);
46 static_assert(!HasEndsWithIt
<int*, int*, ForwardIteratorNotIncrementable
>);
47 static_assert(!HasEndsWithIt
<int*, int*, int*, SentinelForNotSemiregular
>);
48 static_assert(!HasEndsWithIt
<int*, int*, int*, SentinelForNotWeaklyEqualityComparableWith
>);
50 template <class Range1
, class Range2
= UncheckedRange
<int*>>
51 concept HasEndsWithR
= requires(Range1
&& range1
, Range2
&& range2
) {
52 std::ranges::ends_with(std::forward
<Range1
>(range1
), std::forward
<Range2
>(range2
)); };
54 static_assert(HasEndsWithR
<UncheckedRange
<int*>>);
55 static_assert(!HasEndsWithR
<ForwardRangeNotDerivedFrom
>);
56 static_assert(!HasEndsWithR
<ForwardIteratorNotIncrementable
>);
57 static_assert(!HasEndsWithR
<ForwardRangeNotSentinelSemiregular
>);
58 static_assert(!HasEndsWithR
<ForwardRangeNotSentinelEqualityComparableWith
>);
59 static_assert(HasEndsWithR
<UncheckedRange
<int*>, UncheckedRange
<int*>>);
60 static_assert(!HasEndsWithR
<UncheckedRange
<int*>, UncheckedRange
<int**>>); // not indirectly comparable
61 static_assert(!HasEndsWithR
<UncheckedRange
<int*>, ForwardRangeNotDerivedFrom
>);
62 static_assert(!HasEndsWithR
<UncheckedRange
<int*>, ForwardRangeNotSentinelSemiregular
>);
65 template <class Iter1
, class Sent1
= Iter1
, class Iter2
, class Sent2
= Iter2
>
66 constexpr void test_iterators() {
68 int a
[] = {1, 2, 3, 4, 5, 6};
70 auto whole
= std::ranges::subrange(Iter1(a
), Sent1(Iter1(a
+ 6)));
71 auto suffix
= std::ranges::subrange(Iter2(p
), Sent2(Iter2(p
+ 3)));
73 [[maybe_unused
]] std::same_as
<bool> decltype(auto) ret
= std::ranges::ends_with(whole
.begin(), whole
.end(), suffix
.begin(), suffix
.end());
77 [[maybe_unused
]] std::same_as
<bool> decltype(auto) ret
= std::ranges::ends_with(whole
, suffix
);
82 { // suffix doesn't match
83 int a
[] = {1, 2, 3, 4, 5, 6};
85 auto whole
= std::ranges::subrange(Iter1(a
), Sent1(Iter1(a
+ 6)));
86 auto suffix
= std::ranges::subrange(Iter2(p
), Sent2(Iter2(p
+ 3)));
88 bool ret
= std::ranges::ends_with(whole
.begin(), whole
.end(), suffix
.begin(), suffix
.end());
92 bool ret
= std::ranges::ends_with(whole
, suffix
);
97 { // range consists of just one element
100 auto whole
= std::ranges::subrange(Iter1(a
), Sent1(Iter1(a
+ 1)));
101 auto suffix
= std::ranges::subrange(Iter2(p
), Sent2(Iter2(p
+ 1)));
103 bool ret
= std::ranges::ends_with(whole
.begin(), whole
.end(), suffix
.begin(), suffix
.end());
107 bool ret
= std::ranges::ends_with(whole
, suffix
);
112 { // suffix consists of just one element
113 int a
[] = {5, 1, 2, 4, 3};
115 auto whole
= std::ranges::subrange(Iter1(a
), Sent1(Iter1(a
+ 5)));
116 auto suffix
= std::ranges::subrange(Iter2(p
), Sent2(Iter2(p
+ 1)));
118 bool ret
= std::ranges::ends_with(whole
.begin(), whole
.end(), suffix
.begin(), suffix
.end());
122 bool ret
= std::ranges::ends_with(whole
, suffix
);
127 { // range and suffix are identical
128 int a
[] = {1, 2, 3, 4, 5, 6};
129 int p
[] = {1, 2, 3, 4, 5, 6};
130 auto whole
= std::ranges::subrange(Iter1(a
), Sent1(Iter1(a
+ 6)));
131 auto suffix
= std::ranges::subrange(Iter2(p
), Sent2(Iter2(p
+ 6)));
133 bool ret
= std::ranges::ends_with(whole
.begin(), whole
.end(), suffix
.begin(), suffix
.end());
137 bool ret
= std::ranges::ends_with(whole
, suffix
);
142 { // suffix is longer than range
143 int a
[] = {3, 4, 5, 6, 7, 8};
144 int p
[] = {1, 2, 3, 4, 5, 6, 7, 8};
145 auto whole
= std::ranges::subrange(Iter1(a
), Sent1(Iter1(a
+ 6)));
146 auto suffix
= std::ranges::subrange(Iter2(p
), Sent2(Iter2(p
+ 8)));
148 bool ret
= std::ranges::ends_with(whole
.begin(), whole
.end(), suffix
.begin(), suffix
.end());
152 bool ret
= std::ranges::ends_with(whole
, suffix
);
157 { // suffix has zero length
158 int a
[] = {1, 2, 3, 4, 5, 6};
160 auto whole
= std::ranges::subrange(Iter1(a
), Sent1(Iter1(a
+ 6)));
161 auto suffix
= std::ranges::subrange(Iter2(p
), Sent2(Iter2(p
)));
163 bool ret
= std::ranges::ends_with(whole
.begin(), whole
.end(), suffix
.begin(), suffix
.end());
167 bool ret
= std::ranges::ends_with(whole
, suffix
);
172 { // range has zero length
174 int p
[] = {1, 2, 3, 4, 5, 6, 7, 8};
175 auto whole
= std::ranges::subrange(Iter1(a
), Sent1(Iter1(a
)));
176 auto suffix
= std::ranges::subrange(Iter2(p
), Sent2(Iter2(p
+ 8)));
178 bool ret
= std::ranges::ends_with(whole
.begin(), whole
.end(), suffix
.begin(), suffix
.end());
182 bool ret
= std::ranges::ends_with(whole
, suffix
);
188 int a
[] = {0, 3, 5, 10, 7, 3, 5, 89, 3, 5, 2, 1, 8, 6};
190 auto whole
= std::ranges::subrange(Iter1(a
), Sent1(Iter1(a
+ 13)));
191 auto suffix
= std::ranges::subrange(Iter2(p
), Sent2(Iter2(p
+ 2)));
193 bool ret
= std::ranges::ends_with(whole
.begin(), whole
.end(), suffix
.begin(), suffix
.end());
197 bool ret
= std::ranges::ends_with(whole
, suffix
);
203 int a
[] = {8, 6, 3, 5, 1, 2};
204 int p
[] = {1, 2, 1, 2};
205 auto whole
= std::ranges::subrange(Iter1(a
), Sent1(Iter1(a
+ 6)));
206 auto suffix
= std::ranges::subrange(Iter2(p
), Sent2(Iter2(p
+ 4)));
208 bool ret
= std::ranges::ends_with(whole
.begin(), whole
.end(), suffix
.begin(), suffix
.end());
212 bool ret
= std::ranges::ends_with(whole
, suffix
);
217 { // check that the predicate is used
218 int a
[] = {5, 1, 3, 2, 7};
220 auto pred
= [](int l
, int r
) { return l
* -1 == r
; };
221 auto whole
= std::ranges::subrange(Iter1(a
), Sent1(Iter1(a
+ 5)));
222 auto suffix
= std::ranges::subrange(Iter2(p
), Sent2(Iter2(p
+ 2)));
224 bool ret
= std::ranges::ends_with(whole
.begin(), whole
.end(), suffix
.begin(), suffix
.end(), pred
);
228 bool ret
= std::ranges::ends_with(whole
, suffix
, pred
);
233 { // check that the projections are used
234 int a
[] = {1, 3, 15, 1, 2, 1};
236 auto whole
= std::ranges::subrange(Iter1(a
), Sent1(Iter1(a
+ 6)));
237 auto suffix
= std::ranges::subrange(Iter2(p
), Sent2(Iter2(p
+ 3)));
239 bool ret
= std::ranges::ends_with(whole
.begin(), whole
.end(), suffix
.begin(), suffix
.end(), {},
240 [](int i
) { return i
- 3; },
241 [](int i
) { return i
* -1; });
245 bool ret
= std::ranges::ends_with(whole
, suffix
, {},
246 [](int i
) { return i
- 3; },
247 [](int i
) { return i
* -1; });
253 constexpr bool test() {
254 // This is to test (forward_iterator<_Iter1> || sized_sentinel_for<_Sent1, _Iter1>) condition.
255 types::for_each(types::cpp20_input_iterator_list
<int*>{}, []<class Iter2
>() {
256 types::for_each(types::cpp20_input_iterator_list
<int*>{}, []<class Iter1
>() {
257 if constexpr (std::forward_iterator
<Iter1
> && std::forward_iterator
<Iter2
>)
258 test_iterators
<Iter1
, Iter1
, Iter2
, Iter2
>();
259 if constexpr (std::forward_iterator
<Iter2
>)
260 test_iterators
<Iter1
, sized_sentinel
<Iter1
>, Iter2
, Iter2
>();
261 if constexpr (std::forward_iterator
<Iter1
>)
262 test_iterators
<Iter1
, Iter1
, Iter2
, sized_sentinel
<Iter2
>>();
263 test_iterators
<Iter1
, sized_sentinel
<Iter1
>, Iter2
, sized_sentinel
<Iter2
>>();
270 int main(int, char**) {
272 static_assert(test());