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::starts_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::starts_with(R1&& r1, R2&& r2, Pred pred = {},
22 // Proj1 proj1 = {}, Proj2 proj2 = {});
28 #include "almost_satisfies_types.h"
29 #include "test_iterators.h"
31 template <class Iter1
, class Sent1
= Iter1
, class Iter2
= int*, class Sent2
= Iter2
>
32 concept HasStartsWithIt
= requires(Iter1 first1
, Sent1 last1
, Iter2 first2
, Sent2 last2
) {
33 std::ranges::starts_with(first1
, last1
, first2
, last2
);
36 static_assert(HasStartsWithIt
<int*>);
37 static_assert(HasStartsWithIt
<ForwardIteratorNotDerivedFrom
>);
38 static_assert(HasStartsWithIt
<ForwardIteratorNotIncrementable
>);
39 static_assert(!HasStartsWithIt
<int*, SentinelForNotSemiregular
>);
40 static_assert(!HasStartsWithIt
<int*, int*, int**>); // not indirectly comparable
41 static_assert(!HasStartsWithIt
<int*, SentinelForNotWeaklyEqualityComparableWith
>);
42 static_assert(HasStartsWithIt
<int*, int*, ForwardIteratorNotDerivedFrom
>);
43 static_assert(HasStartsWithIt
<int*, int*, ForwardIteratorNotIncrementable
>);
44 static_assert(!HasStartsWithIt
<int*, int*, int*, SentinelForNotSemiregular
>);
45 static_assert(!HasStartsWithIt
<int*, int*, int*, SentinelForNotWeaklyEqualityComparableWith
>);
47 template <class Range1
, class Range2
= UncheckedRange
<int*>>
48 concept HasStartsWithR
= requires(Range1 range1
, Range2 range2
) { std::ranges::starts_with(range1
, range2
); };
50 static_assert(HasStartsWithR
<UncheckedRange
<int*>>);
51 static_assert(HasStartsWithR
<ForwardRangeNotDerivedFrom
>);
52 static_assert(!HasStartsWithR
<ForwardIteratorNotIncrementable
>);
53 static_assert(!HasStartsWithR
<ForwardRangeNotSentinelSemiregular
>);
54 static_assert(!HasStartsWithR
<ForwardRangeNotSentinelEqualityComparableWith
>);
55 static_assert(!HasStartsWithR
<UncheckedRange
<int*>, UncheckedRange
<int**>>); // not indirectly comparable
56 static_assert(HasStartsWithR
<UncheckedRange
<int*>, ForwardRangeNotDerivedFrom
>);
57 static_assert(HasStartsWithR
<UncheckedRange
<int*>, ForwardRangeNotIncrementable
>);
58 static_assert(!HasStartsWithR
<UncheckedRange
<int*>, ForwardRangeNotSentinelSemiregular
>);
59 static_assert(!HasStartsWithR
<UncheckedRange
<int*>, ForwardRangeNotSentinelEqualityComparableWith
>);
62 template <class Iter1
, class Sent1
= Iter1
, class Iter2
, class Sent2
= Iter2
>
63 constexpr void test_iterators() {
66 int a
[] = {1, 2, 3, 4, 5, 6};
68 std::same_as
<bool> decltype(auto) ret
=
69 std::ranges::starts_with(Iter1(a
), Sent1(Iter1(a
+ 6)), Iter2(p
), Sent2(Iter2(p
+ 2)));
73 int a
[] = {1, 2, 3, 4, 5, 6};
75 auto whole
= std::ranges::subrange(Iter1(a
), Sent1(Iter1(a
+ 6)));
76 auto prefix
= std::ranges::subrange(Iter2(p
), Sent2(Iter2(p
+ 2)));
77 std::same_as
<bool> decltype(auto) ret
= std::ranges::starts_with(whole
, prefix
);
82 { // prefix doesn't match
84 int a
[] = {1, 2, 3, 4, 5, 6};
86 std::same_as
<bool> decltype(auto) ret
=
87 std::ranges::starts_with(Iter1(a
), Sent1(Iter1(a
+ 6)), Iter2(p
), Sent2(Iter2(p
+ 3)));
91 int a
[] = {1, 2, 3, 4, 5, 6};
93 auto whole
= std::ranges::subrange(Iter1(a
), Sent1(Iter1(a
+ 6)));
94 auto prefix
= std::ranges::subrange(Iter2(p
), Sent2(Iter2(p
+ 3)));
95 std::same_as
<bool> decltype(auto) ret
= std::ranges::starts_with(whole
, prefix
);
100 { // range and prefix are identical
102 int a
[] = {1, 2, 3, 4, 5, 6};
103 int p
[] = {1, 2, 3, 4, 5, 6};
104 std::same_as
<bool> decltype(auto) ret
=
105 std::ranges::starts_with(Iter1(a
), Sent1(Iter1(a
+ 6)), Iter2(p
), Sent2(Iter2(p
+ 6)));
109 int a
[] = {1, 2, 3, 4, 5, 6};
110 int p
[] = {1, 2, 3, 4, 5, 6};
111 auto whole
= std::ranges::subrange(Iter1(a
), Sent1(Iter1(a
+ 6)));
112 auto prefix
= std::ranges::subrange(Iter2(p
), Sent2(Iter2(p
+ 6)));
113 std::same_as
<bool> decltype(auto) ret
= std::ranges::starts_with(whole
, prefix
);
118 { // prefix is longer than range
120 int a
[] = {1, 2, 3, 4, 5, 6};
121 int p
[] = {1, 2, 3, 4, 5, 6, 7, 8};
122 std::same_as
<bool> decltype(auto) ret
=
123 std::ranges::starts_with(Iter1(a
), Sent1(Iter1(a
+ 6)), Iter2(p
), Sent2(Iter2(p
+ 8)));
127 int a
[] = {1, 2, 3, 4, 5, 6};
128 int p
[] = {1, 2, 3, 4, 5, 6, 7, 8};
129 auto whole
= std::ranges::subrange(Iter1(a
), Sent1(Iter1(a
+ 6)));
130 auto prefix
= std::ranges::subrange(Iter2(p
), Sent2(Iter2(p
+ 8)));
131 std::same_as
<bool> decltype(auto) ret
= std::ranges::starts_with(whole
, prefix
);
136 { // prefix has zero length
138 int a
[] = {1, 2, 3, 4, 5, 6};
139 std::array
<int, 0> p
= {};
140 std::same_as
<bool> decltype(auto) ret
=
141 std::ranges::starts_with(Iter1(a
), Sent1(Iter1(a
+ 6)), Iter2(p
.data()), Sent2(Iter2(p
.data())));
145 int a
[] = {1, 2, 3, 4, 5, 6};
146 std::array
<int, 0> p
= {};
147 auto whole
= std::ranges::subrange(Iter1(a
), Sent1(Iter1(a
+ 6)));
148 auto prefix
= std::ranges::subrange(Iter2(p
.data()), Sent2(Iter2(p
.data())));
149 std::same_as
<bool> decltype(auto) ret
= std::ranges::starts_with(whole
, prefix
);
154 { // range has zero length
156 std::array
<int, 0> a
= {};
157 int p
[] = {1, 2, 3, 4, 5, 6, 7, 8};
158 std::same_as
<bool> decltype(auto) ret
=
159 std::ranges::starts_with(Iter1(a
.data()), Sent1(Iter1(a
.data())), Iter2(p
), Sent2(Iter2(p
+ 8)));
163 std::array
<int, 0> a
= {};
164 int p
[] = {1, 2, 3, 4, 5, 6, 7, 8};
165 auto whole
= std::ranges::subrange(Iter1(a
.data()), Sent1(Iter1(a
.data())));
166 auto prefix
= std::ranges::subrange(Iter2(p
), Sent2(Iter2(p
+ 8)));
167 std::same_as
<bool> decltype(auto) ret
= std::ranges::starts_with(whole
, prefix
);
172 { // check that the predicate is used
174 int a
[] = {11, 8, 3, 4, 0, 6};
176 std::same_as
<bool> decltype(auto) ret
= std::ranges::starts_with(
177 Iter1(a
), Sent1(Iter1(a
+ 6)), Iter2(p
), Sent2(Iter2(p
+ 2)), [](int l
, int r
) { return l
> r
; });
181 int a
[] = {11, 8, 3, 4, 0, 6};
183 auto whole
= std::ranges::subrange(Iter1(a
), Sent1(Iter1(a
+ 6)));
184 auto prefix
= std::ranges::subrange(Iter2(p
), Sent2(Iter2(p
+ 2)));
185 std::same_as
<bool> decltype(auto) ret
= std::ranges::starts_with(whole
, prefix
, [](int l
, int r
) { return l
> r
; });
190 { // check that the projections are used
192 int a
[] = {1, 3, 5, 1, 5, 6};
194 std::same_as
<bool> decltype(auto) ret
= std::ranges::starts_with(
200 [](int i
) { return i
+ 3; },
201 [](int i
) { return i
* 2; });
205 int a
[] = {1, 3, 5, 1, 5, 6};
207 auto whole
= std::ranges::subrange(Iter1(a
), Sent1(Iter1(a
+ 6)));
208 auto prefix
= std::ranges::subrange(Iter2(p
), Sent2(Iter2(p
+ 3)));
209 std::same_as
<bool> decltype(auto) ret
= std::ranges::starts_with(
210 whole
, prefix
, {}, [](int i
) { return i
+ 3; }, [](int i
) { return i
* 2; });
216 constexpr bool test() {
217 types::for_each(types::cpp20_input_iterator_list
<int*>{}, []<class Iter2
>() {
218 types::for_each(types::cpp20_input_iterator_list
<int*>{}, []<class Iter1
>() {
219 if constexpr (std::forward_iterator
<Iter1
> && std::forward_iterator
<Iter2
>)
220 test_iterators
<Iter1
, Iter1
, Iter2
, Iter2
>();
221 if constexpr (std::forward_iterator
<Iter2
>)
222 test_iterators
<Iter1
, sized_sentinel
<Iter1
>, Iter2
, Iter2
>();
223 if constexpr (std::forward_iterator
<Iter1
>)
224 test_iterators
<Iter1
, Iter1
, Iter2
, sized_sentinel
<Iter2
>>();
225 test_iterators
<Iter1
, sized_sentinel
<Iter1
>, Iter2
, sized_sentinel
<Iter2
>>();
229 { // check that std::invoke is used
233 constexpr S
identity() { return *this; }
235 constexpr bool compare(const S
& s
) { return i
== s
.i
; }
238 S a
[] = {{1}, {2}, {3}, {4}};
240 auto ret
= std::ranges::starts_with(a
, a
+ 4, p
, p
+ 2, &S::compare
, &S::identity
, &S::identity
);
244 S a
[] = {{1}, {2}, {3}, {4}};
246 auto ret
= std::ranges::starts_with(a
, p
, &S::compare
, &S::identity
, &S::identity
);
254 int main(int, char**) {
256 static_assert(test());