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 I, sentinel_for<I> S, class T, class Proj = identity>
14 // requires indirect_binary_predicate<ranges::equal_to, projected<I, Proj>, const T*>
15 // constexpr bool ranges::contains(I first, S last, const T& value, Proj proj = {}); // since C++23
17 // template<input_range R, class T, class Proj = identity>
18 // requires indirect_binary_predicate<ranges::equal_to, projected<iterator_t<R>, Proj>, const T*>
19 // constexpr bool ranges::contains(R&& r, const T& value, Proj proj = {}); // since C++23
29 #include "almost_satisfies_types.h"
30 #include "boolean_testable.h"
31 #include "test_iterators.h"
33 struct NotEqualityComparable
{};
35 template <class Iter
, class Sent
= Iter
>
36 concept HasContainsIt
= requires(Iter iter
, Sent sent
) { std::ranges::contains(iter
, sent
, *iter
); };
38 static_assert(HasContainsIt
<int*>);
39 static_assert(HasContainsIt
<int*, int*>);
40 static_assert(!HasContainsIt
<NotEqualityComparable
*>);
41 static_assert(!HasContainsIt
<InputIteratorNotDerivedFrom
>);
42 static_assert(!HasContainsIt
<InputIteratorNotIndirectlyReadable
>);
43 static_assert(!HasContainsIt
<InputIteratorNotInputOrOutputIterator
>);
44 static_assert(!HasContainsIt
<cpp20_input_iterator
<int*>, SentinelForNotSemiregular
>);
45 static_assert(!HasContainsIt
<cpp20_input_iterator
<int*>, InputRangeNotSentinelEqualityComparableWith
>);
46 static_assert(!HasContainsIt
<cpp20_input_iterator
<int*>, sentinel_wrapper
<cpp20_input_iterator
<int*>>>);
48 static_assert(!HasContainsIt
<int*, int>);
49 static_assert(!HasContainsIt
<int, int*>);
51 template <class Range
, class ValT
>
52 concept HasContainsR
= requires(Range
&& range
) { std::ranges::contains(std::forward
<Range
>(range
), ValT
{}); };
54 static_assert(!HasContainsR
<int, int>);
55 static_assert(HasContainsR
<int[1], int>);
56 static_assert(!HasContainsR
<NotEqualityComparable
[1], NotEqualityComparable
>);
57 static_assert(!HasContainsR
<InputRangeNotDerivedFrom
, int>);
58 static_assert(!HasContainsR
<InputRangeNotIndirectlyReadable
, int>);
59 static_assert(!HasContainsR
<InputRangeNotInputOrOutputIterator
, int>);
60 static_assert(!HasContainsR
<InputRangeNotSentinelSemiregular
, int>);
61 static_assert(!HasContainsR
<InputRangeNotSentinelEqualityComparableWith
, int>);
63 template <class Iter
, class Sent
= Iter
>
64 constexpr void test_iterators() {
65 using ValueT
= std::iter_value_t
<Iter
>;
67 ValueT a
[] = {1, 2, 3, 4, 5, 6};
69 std::same_as
<bool> decltype(auto) ret
= std::ranges::contains(Iter(a
), Sent(Iter(a
+ 6)), 3);
73 auto whole
= std::ranges::subrange(Iter(a
), Sent(Iter(a
+ 6)));
74 std::same_as
<bool> decltype(auto) ret
= std::ranges::contains(whole
, 3);
79 { // check that a range with a single element works
82 bool ret
= std::ranges::contains(Iter(a
), Sent(Iter(a
+ 1)), 32);
86 auto whole
= std::ranges::subrange(Iter(a
), Sent(Iter(a
+ 1)));
87 bool ret
= std::ranges::contains(whole
, 32);
92 { // check that an empty range works
93 std::array
<ValueT
, 0> a
= {};
95 bool ret
= std::ranges::contains(Iter(a
.data()), Sent(Iter(a
.data())), 1);
99 auto whole
= std::ranges::subrange(Iter(a
.data()), Sent(Iter(a
.data())));
100 bool ret
= std::ranges::contains(whole
, 1);
105 { // check that the first element matches
106 ValueT a
[] = {32, 3, 2, 1, 0, 23, 21, 9, 40, 100};
108 bool ret
= std::ranges::contains(Iter(a
), Sent(Iter(a
+ 10)), 32);
112 auto whole
= std::ranges::subrange(Iter(a
), Sent(Iter(a
+ 10)));
113 bool ret
= std::ranges::contains(whole
, 32);
118 { // check that the last element matches
119 ValueT a
[] = {3, 22, 1, 43, 99, 0, 56, 100, 32};
121 bool ret
= std::ranges::contains(Iter(a
), Sent(Iter(a
+ 9)), 32);
125 auto whole
= std::ranges::subrange(Iter(a
), Sent(Iter(a
+ 9)));
126 bool ret
= std::ranges::contains(whole
, 32);
132 ValueT a
[] = {13, 1, 21, 4, 5};
134 bool ret
= std::ranges::contains(Iter(a
), Sent(Iter(a
+ 5)), 10);
138 auto whole
= std::ranges::subrange(Iter(a
), Sent(Iter(a
+ 5)));
139 bool ret
= std::ranges::contains(whole
, 10);
144 { // check that the projections are used
145 int a
[] = {1, 9, 0, 13, 25};
147 bool ret
= std::ranges::contains(a
, a
+ 5, -13, [&](int i
) { return i
* -1; });
151 auto range
= std::ranges::subrange(a
, a
+ 5);
152 bool ret
= std::ranges::contains(range
, -13, [&](int i
) { return i
* -1; });
158 constexpr bool test() {
159 types::for_each(types::type_list
<char, long long>{}, []<class T
> {
160 types::for_each(types::cpp20_input_iterator_list
<T
*>{}, []<class Iter
> {
161 if constexpr (std::forward_iterator
<Iter
>)
162 test_iterators
<Iter
>();
163 test_iterators
<Iter
, sentinel_wrapper
<Iter
>>();
164 test_iterators
<Iter
, sized_sentinel
<Iter
>>();
168 { // count invocations of the projection for contiguous iterators
169 int a
[] = {1, 9, 0, 13, 25};
170 int projection_count
= 0;
172 bool ret
= std::ranges::contains(a
, a
+ 5, 0, [&](int i
) {
177 assert(projection_count
== 3);
178 projection_count
= 0;
181 bool ret
= std::ranges::contains(a
, 0, [&](int i
) {
186 assert(projection_count
== 3);
190 { // check invocations of the projection for std::string
191 const std::string str
{"hello world"};
192 const std::string str1
{"hi world"};
193 int projection_count
= 0;
195 std::string a
[] = {str1
, str1
, str
, str1
, str1
};
197 std::ranges::subrange(forward_iterator(std::move_iterator(a
)), forward_iterator(std::move_iterator(a
+ 5)));
198 bool ret
= std::ranges::contains(whole
.begin(), whole
.end(), "hello world", [&](const std::string i
) {
203 assert(projection_count
== 3);
204 projection_count
= 0;
207 std::string a
[] = {str1
, str1
, str
, str1
, str1
};
209 std::ranges::subrange(forward_iterator(std::move_iterator(a
)), forward_iterator(std::move_iterator(a
+ 5)));
210 bool ret
= std::ranges::contains(whole
, "hello world", [&](const std::string i
) {
215 assert(projection_count
== 3);
219 { // check invocations of the projection for non-contiguous iterators
220 std::vector
<bool> whole
{false, false, true, false};
221 int projection_count
= 0;
223 bool ret
= std::ranges::contains(whole
.begin(), whole
.end(), true, [&](bool b
) {
228 assert(projection_count
== 3);
229 projection_count
= 0;
232 bool ret
= std::ranges::contains(whole
, true, [&](bool b
) {
237 assert(projection_count
== 3);
241 { // check invocations of the projection for views::transform
242 int a
[] = {1, 2, 3, 4, 5};
243 int projection_count
= 0;
244 auto square_number
= a
| std::views::transform([](int x
) { return x
* x
; });
246 bool ret
= std::ranges::contains(square_number
.begin(), square_number
.end(), 16, [&](int i
) {
251 assert(projection_count
== 4);
252 projection_count
= 0;
255 bool ret
= std::ranges::contains(square_number
, 16, [&](int i
) {
260 assert(projection_count
== 4);
267 // test for non-contiguous containers
268 bool test_nonconstexpr() {
269 std::list
<int> a
= {7, 5, 0, 16, 8};
270 int projection_count
= 0;
272 bool ret
= std::ranges::contains(a
.begin(), a
.end(), 0, [&](int i
) {
277 assert(projection_count
== 3);
278 projection_count
= 0;
281 bool ret
= std::ranges::contains(a
, 0, [&](int i
) {
286 assert(projection_count
== 3);
292 int main(int, char**) {
294 static_assert(test());
296 assert(test_nonconstexpr());