aboutsummaryrefslogtreecommitdiff
path: root/libcxx/test/std/algorithms
diff options
context:
space:
mode:
authorZijunZhaoCCK <88353225+ZijunZhaoCCK@users.noreply.github.com>2023-12-19 16:34:19 -0800
committerGitHub <noreply@github.com>2023-12-19 16:34:19 -0800
commitfdd089b500631b123bc70d04dd016b41f9323f4c (patch)
treeaffdc65a77818080502478cb7494d02fde860ab9 /libcxx/test/std/algorithms
parent22764918b5c1496970ab6bb1547535c554a06347 (diff)
downloadllvm-fdd089b500631b123bc70d04dd016b41f9323f4c.zip
llvm-fdd089b500631b123bc70d04dd016b41f9323f4c.tar.gz
llvm-fdd089b500631b123bc70d04dd016b41f9323f4c.tar.bz2
[libc++] Implement ranges::contains (#65148)
Differential Revision: https://reviews.llvm.org/D159232 ``` Running ./ranges_contains.libcxx.out Run on (10 X 24.121 MHz CPU s) CPU Caches: L1 Data 64 KiB (x10) L1 Instruction 128 KiB (x10) L2 Unified 4096 KiB (x5) Load Average: 3.37, 6.77, 5.27 -------------------------------------------------------------------- Benchmark Time CPU Iterations -------------------------------------------------------------------- bm_contains_char/16 1.88 ns 1.87 ns 371607095 bm_contains_char/256 7.48 ns 7.47 ns 93292285 bm_contains_char/4096 99.7 ns 99.6 ns 7013185 bm_contains_char/65536 1296 ns 1294 ns 540436 bm_contains_char/1048576 23887 ns 23860 ns 29302 bm_contains_char/16777216 389420 ns 389095 ns 1796 bm_contains_int/16 7.14 ns 7.14 ns 97776288 bm_contains_int/256 90.4 ns 90.3 ns 7558089 bm_contains_int/4096 1294 ns 1290 ns 543052 bm_contains_int/65536 20482 ns 20443 ns 34334 bm_contains_int/1048576 328817 ns 327965 ns 2147 bm_contains_int/16777216 5246279 ns 5239361 ns 133 bm_contains_bool/16 2.19 ns 2.19 ns 322565780 bm_contains_bool/256 3.42 ns 3.41 ns 205025467 bm_contains_bool/4096 22.1 ns 22.1 ns 31780479 bm_contains_bool/65536 333 ns 332 ns 2106606 bm_contains_bool/1048576 5126 ns 5119 ns 135901 bm_contains_bool/16777216 81656 ns 81574 ns 8569 ``` --------- Co-authored-by: Nathan Gauër <brioche@google.com>
Diffstat (limited to 'libcxx/test/std/algorithms')
-rw-r--r--libcxx/test/std/algorithms/alg.nonmodifying/alg.contains/ranges.contains.pass.cpp298
-rw-r--r--libcxx/test/std/algorithms/ranges_robust_against_omitting_invoke.pass.cpp1
-rw-r--r--libcxx/test/std/algorithms/ranges_robust_against_proxy_iterators.pass.cpp1
3 files changed, 300 insertions, 0 deletions
diff --git a/libcxx/test/std/algorithms/alg.nonmodifying/alg.contains/ranges.contains.pass.cpp b/libcxx/test/std/algorithms/alg.nonmodifying/alg.contains/ranges.contains.pass.cpp
new file mode 100644
index 0000000..c928698
--- /dev/null
+++ b/libcxx/test/std/algorithms/alg.nonmodifying/alg.contains/ranges.contains.pass.cpp
@@ -0,0 +1,298 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// <algorithm>
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
+
+// template<input_iterator I, sentinel_for<I> S, class T, class Proj = identity>
+// requires indirect_binary_predicate<ranges::equal_to, projected<I, Proj>, const T*>
+// constexpr bool ranges::contains(I first, S last, const T& value, Proj proj = {}); // since C++23
+
+// template<input_range R, class T, class Proj = identity>
+// requires indirect_binary_predicate<ranges::equal_to, projected<iterator_t<R>, Proj>, const T*>
+// constexpr bool ranges::contains(R&& r, const T& value, Proj proj = {}); // since C++23
+
+#include <algorithm>
+#include <cassert>
+#include <list>
+#include <ranges>
+#include <string>
+#include <vector>
+
+#include "almost_satisfies_types.h"
+#include "boolean_testable.h"
+#include "test_iterators.h"
+
+struct NotEqualityComparable {};
+
+template <class Iter, class Sent = Iter>
+concept HasContainsIt = requires(Iter iter, Sent sent) { std::ranges::contains(iter, sent, *iter); };
+
+static_assert(HasContainsIt<int*>);
+static_assert(HasContainsIt<int*, int*>);
+static_assert(!HasContainsIt<NotEqualityComparable*>);
+static_assert(!HasContainsIt<InputIteratorNotDerivedFrom>);
+static_assert(!HasContainsIt<InputIteratorNotIndirectlyReadable>);
+static_assert(!HasContainsIt<InputIteratorNotInputOrOutputIterator>);
+static_assert(!HasContainsIt<cpp20_input_iterator<int*>, SentinelForNotSemiregular>);
+static_assert(!HasContainsIt<cpp20_input_iterator<int*>, InputRangeNotSentinelEqualityComparableWith>);
+static_assert(!HasContainsIt<cpp20_input_iterator<int*>, sentinel_wrapper<cpp20_input_iterator<int*>>>);
+
+static_assert(!HasContainsIt<int*, int>);
+static_assert(!HasContainsIt<int, int*>);
+
+template <class Range, class ValT>
+concept HasContainsR = requires(Range&& range) { std::ranges::contains(std::forward<Range>(range), ValT{}); };
+
+static_assert(!HasContainsR<int, int>);
+static_assert(HasContainsR<int[1], int>);
+static_assert(!HasContainsR<NotEqualityComparable[1], NotEqualityComparable>);
+static_assert(!HasContainsR<InputRangeNotDerivedFrom, int>);
+static_assert(!HasContainsR<InputRangeNotIndirectlyReadable, int>);
+static_assert(!HasContainsR<InputRangeNotInputOrOutputIterator, int>);
+static_assert(!HasContainsR<InputRangeNotSentinelSemiregular, int>);
+static_assert(!HasContainsR<InputRangeNotSentinelEqualityComparableWith, int>);
+
+template <class Iter, class Sent = Iter>
+constexpr void test_iterators() {
+ using ValueT = std::iter_value_t<Iter>;
+ { // simple tests
+ ValueT a[] = {1, 2, 3, 4, 5, 6};
+ auto whole = std::ranges::subrange(Iter(a), Sent(Iter(a + 6)));
+ {
+ std::same_as<bool> decltype(auto) ret = std::ranges::contains(whole.begin(), whole.end(), 3);
+ assert(ret);
+ }
+ {
+ std::same_as<bool> decltype(auto) ret = std::ranges::contains(whole, 3);
+ assert(ret);
+ }
+ }
+
+ { // check that a range with a single element works
+ ValueT a[] = {32};
+ auto whole = std::ranges::subrange(Iter(a), Sent(Iter(a + 1)));
+ {
+ bool ret = std::ranges::contains(whole.begin(), whole.end(), 32);
+ assert(ret);
+ }
+ {
+ bool ret = std::ranges::contains(whole, 32);
+ assert(ret);
+ }
+ }
+
+ { // check that an empty range works
+ ValueT a[] = {};
+ auto whole = std::ranges::subrange(Iter(a), Sent(Iter(a)));
+ {
+ bool ret = std::ranges::contains(whole.begin(), whole.end(), 1);
+ assert(!ret);
+ }
+ {
+ bool ret = std::ranges::contains(whole, 1);
+ assert(!ret);
+ }
+ }
+
+ { // check that the first element matches
+ ValueT a[] = {32, 3, 2, 1, 0, 23, 21, 9, 40, 100};
+ auto whole = std::ranges::subrange(Iter(a), Sent(Iter(a + 10)));
+ {
+ bool ret = std::ranges::contains(whole.begin(), whole.end(), 32);
+ assert(ret);
+ }
+ {
+ bool ret = std::ranges::contains(whole, 32);
+ assert(ret);
+ }
+ }
+
+ { // check that the last element matches
+ ValueT a[] = {3, 22, 1, 43, 99, 0, 56, 100, 32};
+ auto whole = std::ranges::subrange(Iter(a), Sent(Iter(a + 9)));
+ {
+ bool ret = std::ranges::contains(whole.begin(), whole.end(), 32);
+ assert(ret);
+ }
+ {
+ bool ret = std::ranges::contains(whole, 32);
+ assert(ret);
+ }
+ }
+
+ { // no match
+ ValueT a[] = {13, 1, 21, 4, 5};
+ auto whole = std::ranges::subrange(Iter(a), Sent(Iter(a + 5)));
+ {
+ bool ret = std::ranges::contains(whole.begin(), whole.end(), 10);
+ assert(!ret);
+ }
+ {
+ bool ret = std::ranges::contains(whole, 10);
+ assert(!ret);
+ }
+ }
+
+ { // check that the projections are used
+ int a[] = {1, 9, 0, 13, 25};
+ {
+ bool ret = std::ranges::contains(a, a + 5, -13, [&](int i) { return i * -1; });
+ assert(ret);
+ }
+ {
+ auto range = std::ranges::subrange(a, a + 5);
+ bool ret = std::ranges::contains(range, -13, [&](int i) { return i * -1; });
+ assert(ret);
+ }
+ }
+}
+
+constexpr bool test() {
+ types::for_each(types::type_list<char, long long>{}, []<class T> {
+ types::for_each(types::cpp20_input_iterator_list<T*>{}, []<class Iter> {
+ if constexpr (std::forward_iterator<Iter>)
+ test_iterators<Iter>();
+ test_iterators<Iter, sentinel_wrapper<Iter>>();
+ test_iterators<Iter, sized_sentinel<Iter>>();
+ });
+ });
+
+ { // count invocations of the projection for continuous iterators
+ int a[] = {1, 9, 0, 13, 25};
+ int projection_count = 0;
+ {
+ bool ret = std::ranges::contains(a, a + 5, 0, [&](int i) {
+ ++projection_count;
+ return i;
+ });
+ assert(ret);
+ assert(projection_count == 3);
+ projection_count = 0;
+ }
+ {
+ bool ret = std::ranges::contains(a, 0, [&](int i) {
+ ++projection_count;
+ return i;
+ });
+ assert(ret);
+ assert(projection_count == 3);
+ }
+ }
+
+ { // check invocations of the projection for std::string
+ const std::string str{"hello world"};
+ const std::string str1{"hi world"};
+ int projection_count = 0;
+ {
+ std::string a[] = {str1, str1, str, str1, str1};
+ auto whole =
+ std::ranges::subrange(forward_iterator(std::move_iterator(a)), forward_iterator(std::move_iterator(a + 5)));
+ bool ret = std::ranges::contains(whole.begin(), whole.end(), "hello world", [&](const std::string i) {
+ ++projection_count;
+ return i;
+ });
+ assert(ret);
+ assert(projection_count == 3);
+ projection_count = 0;
+ }
+ {
+ std::string a[] = {str1, str1, str, str1, str1};
+ auto whole =
+ std::ranges::subrange(forward_iterator(std::move_iterator(a)), forward_iterator(std::move_iterator(a + 5)));
+ bool ret = std::ranges::contains(whole, "hello world", [&](const std::string i) {
+ ++projection_count;
+ return i;
+ });
+ assert(ret);
+ assert(projection_count == 3);
+ }
+ }
+
+ { // check invocations of the projection for non-continuous iterators
+ std::vector<bool> whole{false, false, true, false};
+ int projection_count = 0;
+ {
+ bool ret = std::ranges::contains(whole.begin(), whole.end(), true, [&](int i) {
+ ++projection_count;
+ return i;
+ });
+ assert(ret);
+ assert(projection_count == 3);
+ projection_count = 0;
+ }
+ {
+ bool ret = std::ranges::contains(whole, true, [&](int i) {
+ ++projection_count;
+ return i;
+ });
+ assert(ret);
+ assert(projection_count == 3);
+ }
+ }
+
+ { // check invocations of the projection for views::transform
+ int a[] = {1, 2, 3, 4, 5};
+ int projection_count = 0;
+ auto square_number = a | std::views::transform([](int x) { return x * x; });
+ {
+ bool ret = std::ranges::contains(square_number.begin(), square_number.end(), 16, [&](int i) {
+ ++projection_count;
+ return i;
+ });
+ assert(ret);
+ assert(projection_count == 4);
+ projection_count = 0;
+ }
+ {
+ bool ret = std::ranges::contains(square_number, 16, [&](int i) {
+ ++projection_count;
+ return i;
+ });
+ assert(ret);
+ assert(projection_count == 4);
+ }
+ }
+
+ return true;
+}
+
+// test for non-contiguous containers
+bool test_nonconstexpr() {
+ std::list<int> a = {7, 5, 0, 16, 8};
+ int projection_count = 0;
+ {
+ bool ret = std::ranges::contains(a.begin(), a.end(), 0, [&](int i) {
+ ++projection_count;
+ return i;
+ });
+ assert(ret);
+ assert(projection_count == 3);
+ projection_count = 0;
+ }
+ {
+ bool ret = std::ranges::contains(a, 0, [&](int i) {
+ ++projection_count;
+ return i;
+ });
+ assert(ret);
+ assert(projection_count == 3);
+ }
+
+ return true;
+}
+
+int main(int, char**) {
+ test();
+ static_assert(test());
+
+ assert(test_nonconstexpr());
+
+ return 0;
+}
diff --git a/libcxx/test/std/algorithms/ranges_robust_against_omitting_invoke.pass.cpp b/libcxx/test/std/algorithms/ranges_robust_against_omitting_invoke.pass.cpp
index d17dce00..85fe6fb 100644
--- a/libcxx/test/std/algorithms/ranges_robust_against_omitting_invoke.pass.cpp
+++ b/libcxx/test/std/algorithms/ranges_robust_against_omitting_invoke.pass.cpp
@@ -75,6 +75,7 @@ constexpr bool test_all() {
test(std::ranges::any_of, in, &Foo::unary_pred, &Bar::val);
test(std::ranges::all_of, in, &Foo::unary_pred, &Bar::val);
#if TEST_STD_VER >= 23
+ test(std::ranges::contains, in, x, &Bar::val);
test(std::ranges::ends_with, in, in2, &Foo::binary_pred, &Bar::val, &Bar::val);
#endif
test(std::ranges::none_of, in, &Foo::unary_pred, &Bar::val);
diff --git a/libcxx/test/std/algorithms/ranges_robust_against_proxy_iterators.pass.cpp b/libcxx/test/std/algorithms/ranges_robust_against_proxy_iterators.pass.cpp
index 5c8aa01..139f199 100644
--- a/libcxx/test/std/algorithms/ranges_robust_against_proxy_iterators.pass.cpp
+++ b/libcxx/test/std/algorithms/ranges_robust_against_proxy_iterators.pass.cpp
@@ -75,6 +75,7 @@ constexpr void run_tests() {
test(std::ranges::any_of, in, unary_pred);
test(std::ranges::all_of, in, unary_pred);
#if TEST_STD_VER >= 23
+ test(std::ranges::contains, in, x);
test(std::ranges::ends_with, in, in2);
#endif
test(std::ranges::none_of, in, unary_pred);