Revert "[libc] Use best-fit binary trie to make malloc logarithmic" (#117065)
[llvm-project.git] / libcxx / test / std / algorithms / alg.nonmodifying / alg.contains / ranges.contains.pass.cpp
blob08d8e119a4d24b2381b7b6684dbb8c062c4a6dc1
1 //===----------------------------------------------------------------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
9 // <algorithm>
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
21 #include <algorithm>
22 #include <array>
23 #include <cassert>
24 #include <list>
25 #include <ranges>
26 #include <string>
27 #include <vector>
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>;
66 { // simple tests
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);
70 assert(ret);
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);
75 assert(ret);
79 { // check that a range with a single element works
80 ValueT a[] = {32};
82 bool ret = std::ranges::contains(Iter(a), Sent(Iter(a + 1)), 32);
83 assert(ret);
86 auto whole = std::ranges::subrange(Iter(a), Sent(Iter(a + 1)));
87 bool ret = std::ranges::contains(whole, 32);
88 assert(ret);
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);
96 assert(!ret);
99 auto whole = std::ranges::subrange(Iter(a.data()), Sent(Iter(a.data())));
100 bool ret = std::ranges::contains(whole, 1);
101 assert(!ret);
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);
109 assert(ret);
112 auto whole = std::ranges::subrange(Iter(a), Sent(Iter(a + 10)));
113 bool ret = std::ranges::contains(whole, 32);
114 assert(ret);
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);
122 assert(ret);
125 auto whole = std::ranges::subrange(Iter(a), Sent(Iter(a + 9)));
126 bool ret = std::ranges::contains(whole, 32);
127 assert(ret);
131 { // no match
132 ValueT a[] = {13, 1, 21, 4, 5};
134 bool ret = std::ranges::contains(Iter(a), Sent(Iter(a + 5)), 10);
135 assert(!ret);
138 auto whole = std::ranges::subrange(Iter(a), Sent(Iter(a + 5)));
139 bool ret = std::ranges::contains(whole, 10);
140 assert(!ret);
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; });
148 assert(ret);
151 auto range = std::ranges::subrange(a, a + 5);
152 bool ret = std::ranges::contains(range, -13, [&](int i) { return i * -1; });
153 assert(ret);
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) {
173 ++projection_count;
174 return i;
176 assert(ret);
177 assert(projection_count == 3);
178 projection_count = 0;
181 bool ret = std::ranges::contains(a, 0, [&](int i) {
182 ++projection_count;
183 return i;
185 assert(ret);
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};
196 auto whole =
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) {
199 ++projection_count;
200 return i;
202 assert(ret);
203 assert(projection_count == 3);
204 projection_count = 0;
207 std::string a[] = {str1, str1, str, str1, str1};
208 auto whole =
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) {
211 ++projection_count;
212 return i;
214 assert(ret);
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) {
224 ++projection_count;
225 return b;
227 assert(ret);
228 assert(projection_count == 3);
229 projection_count = 0;
232 bool ret = std::ranges::contains(whole, true, [&](bool b) {
233 ++projection_count;
234 return b;
236 assert(ret);
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) {
247 ++projection_count;
248 return i;
250 assert(ret);
251 assert(projection_count == 4);
252 projection_count = 0;
255 bool ret = std::ranges::contains(square_number, 16, [&](int i) {
256 ++projection_count;
257 return i;
259 assert(ret);
260 assert(projection_count == 4);
264 return true;
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) {
273 ++projection_count;
274 return i;
276 assert(ret);
277 assert(projection_count == 3);
278 projection_count = 0;
281 bool ret = std::ranges::contains(a, 0, [&](int i) {
282 ++projection_count;
283 return i;
285 assert(ret);
286 assert(projection_count == 3);
289 return true;
292 int main(int, char**) {
293 test();
294 static_assert(test());
296 assert(test_nonconstexpr());
298 return 0;