diff --git a/libcxx/docs/Status/RangesAlgorithms.csv b/libcxx/docs/Status/RangesAlgorithms.csv --- a/libcxx/docs/Status/RangesAlgorithms.csv +++ b/libcxx/docs/Status/RangesAlgorithms.csv @@ -1,7 +1,7 @@ Category,Algorithm,Assignee,CL,Complete -Search,any_of,Christopher Di Bella,`D105793 `_ -Search,all_of,Christopher Di Bella,`D105793 `_ -Search,none_of,Christopher Di Bella,`D105793 `_ +Search,any_of,Nikolas Klauser,`D123016 `_,✅ +Search,all_of,Nikolas Klauser,`D123016 `_,✅ +Search,none_of,Nikolas Klauser,`D123016 `_,✅ Search,find,Nikolas Klauser,`D121248 `_,✅ Search,find_if,Nikolas Klauser,`D121248 `_,✅ Search,find_if_not,Nikolas Klauser,`D121248 `_,✅ diff --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt --- a/libcxx/include/CMakeLists.txt +++ b/libcxx/include/CMakeLists.txt @@ -66,6 +66,8 @@ __algorithm/pop_heap.h __algorithm/prev_permutation.h __algorithm/push_heap.h + __algorithm/ranges_all_of.h + __algorithm/ranges_any_of.h __algorithm/ranges_copy.h __algorithm/ranges_copy_backward.h __algorithm/ranges_copy_if.h @@ -88,6 +90,7 @@ __algorithm/ranges_minmax.h __algorithm/ranges_minmax_element.h __algorithm/ranges_mismatch.h + __algorithm/ranges_none_of.h __algorithm/ranges_reverse.h __algorithm/ranges_swap_ranges.h __algorithm/ranges_transform.h diff --git a/libcxx/include/__algorithm/ranges_all_of.h b/libcxx/include/__algorithm/ranges_all_of.h new file mode 100644 --- /dev/null +++ b/libcxx/include/__algorithm/ranges_all_of.h @@ -0,0 +1,68 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#ifndef _LIBCPP___ALGORITHM_RANGES_ALL_OF_H +#define _LIBCPP___ALGORITHM_RANGES_ALL_OF_H + +#include <__config> +#include <__functional/identity.h> +#include <__functional/invoke.h> +#include <__iterator/concepts.h> +#include <__iterator/projected.h> +#include <__ranges/access.h> +#include <__ranges/concepts.h> +#include <__utility/move.h> + +#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) +# pragma GCC system_header +#endif + +#if _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES) + +_LIBCPP_BEGIN_NAMESPACE_STD + +namespace ranges { +namespace __all_of { +struct __fn { + + template + _LIBCPP_HIDE_FROM_ABI constexpr static + bool __all_of_impl(_Iter __first, _Sent __last, _Pred& __pred, _Proj& __proj) { + for (; __first != __last; ++__first) { + if (!std::invoke(__pred, std::invoke(__proj, *__first))) + return false; + } + return true; + } + + template _Sent, class _Proj = identity, + indirect_unary_predicate> _Pred> + _LIBCPP_HIDE_FROM_ABI constexpr + bool operator()(_Iter __first, _Sent __last, _Pred __pred, _Proj __proj = {}) const { + return __all_of_impl(std::move(__first), std::move(__last), __pred, __proj); + } + + template , _Proj>> _Pred> + _LIBCPP_HIDE_FROM_ABI constexpr + bool operator()(_Range&& __range, _Pred __pred, _Proj __proj = {}) const { + return __all_of_impl(ranges::begin(__range), ranges::end(__range), __pred, __proj); + } +}; +} // namespace __all_of + +inline namespace __cpo { + inline constexpr auto all_of = __all_of::__fn{}; +} // namespace __cpo +} // namespace ranges + +_LIBCPP_END_NAMESPACE_STD + +#endif // _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES) + +#endif // _LIBCPP___ALGORITHM_RANGES_ALL_OF_H diff --git a/libcxx/include/__algorithm/ranges_any_of.h b/libcxx/include/__algorithm/ranges_any_of.h new file mode 100644 --- /dev/null +++ b/libcxx/include/__algorithm/ranges_any_of.h @@ -0,0 +1,68 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#ifndef _LIBCPP___ALGORITHM_RANGES_ANY_OF_H +#define _LIBCPP___ALGORITHM_RANGES_ANY_OF_H + +#include <__config> +#include <__functional/identity.h> +#include <__functional/invoke.h> +#include <__iterator/concepts.h> +#include <__iterator/projected.h> +#include <__ranges/access.h> +#include <__ranges/concepts.h> +#include <__utility/move.h> + +#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) +# pragma GCC system_header +#endif + +#if _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES) + +_LIBCPP_BEGIN_NAMESPACE_STD + +namespace ranges { +namespace __any_of { +struct __fn { + + template + _LIBCPP_HIDE_FROM_ABI constexpr static + bool __any_of_impl(_Iter __first, _Sent __last, _Pred& __pred, _Proj& __proj) { + for (; __first != __last; ++__first) { + if (std::invoke(__pred, std::invoke(__proj, *__first))) + return true; + } + return false; + } + + template _Sent, class _Proj = identity, + indirect_unary_predicate> _Pred> + _LIBCPP_HIDE_FROM_ABI constexpr + bool operator()(_Iter __first, _Sent __last, _Pred __pred = {}, _Proj __proj = {}) const { + return __any_of_impl(std::move(__first), std::move(__last), __pred, __proj); + } + + template , _Proj>> _Pred> + _LIBCPP_HIDE_FROM_ABI constexpr + bool operator()(_Range&& __range, _Pred __pred, _Proj __proj = {}) const { + return __any_of_impl(ranges::begin(__range), ranges::end(__range), __pred, __proj); + } +}; +} // namespace __any_of + +inline namespace __cpo { + inline constexpr auto any_of = __any_of::__fn{}; +} // namespace __cpo +} // namespace ranges + +_LIBCPP_END_NAMESPACE_STD + +#endif // _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES) + +#endif // _LIBCPP___ALGORITHM_RANGES_ANY_OF_H diff --git a/libcxx/include/__algorithm/ranges_none_of.h b/libcxx/include/__algorithm/ranges_none_of.h new file mode 100644 --- /dev/null +++ b/libcxx/include/__algorithm/ranges_none_of.h @@ -0,0 +1,68 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#ifndef _LIBCPP___ALGORITHM_RANGES_NONE_OF_H +#define _LIBCPP___ALGORITHM_RANGES_NONE_OF_H + +#include <__config> +#include <__functional/identity.h> +#include <__functional/invoke.h> +#include <__iterator/concepts.h> +#include <__iterator/projected.h> +#include <__ranges/access.h> +#include <__ranges/concepts.h> +#include <__utility/move.h> + +#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) +# pragma GCC system_header +#endif + +#if _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES) + +_LIBCPP_BEGIN_NAMESPACE_STD + +namespace ranges { +namespace __none_of { +struct __fn { + + template + _LIBCPP_HIDE_FROM_ABI constexpr static + bool __none_of_impl(_Iter __first, _Sent __last, _Pred& __pred, _Proj& __proj) { + for (; __first != __last; ++__first) { + if (std::invoke(__pred, std::invoke(__proj, *__first))) + return false; + } + return true; + } + + template _Sent, class _Proj = identity, + indirect_unary_predicate> _Pred> + _LIBCPP_HIDE_FROM_ABI constexpr + bool operator()(_Iter __first, _Sent __last, _Pred __pred = {}, _Proj __proj = {}) const { + return __none_of_impl(std::move(__first), std::move(__last), __pred, __proj); + } + + template , _Proj>> _Pred> + _LIBCPP_HIDE_FROM_ABI constexpr + bool operator()(_Range&& __range, _Pred __pred, _Proj __proj = {}) const { + return __none_of_impl(ranges::begin(__range), ranges::end(__range), __pred, __proj); + } +}; +} // namespace __none_of + +inline namespace __cpo { + inline constexpr auto none_of = __none_of::__fn{}; +} // namespace __cpo +} // namespace ranges + +_LIBCPP_END_NAMESPACE_STD + +#endif // _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES) + +#endif // _LIBCPP___ALGORITHM_RANGES_NONE_OF_H diff --git a/libcxx/include/algorithm b/libcxx/include/algorithm --- a/libcxx/include/algorithm +++ b/libcxx/include/algorithm @@ -305,6 +305,30 @@ constexpr bool ranges::equal(R1&& r1, R2&& r2, Pred pred = {}, Proj1 proj1 = {}, Proj2 proj2 = {}); // since C++20 + template S, class Proj = identity, + indirect_unary_predicate> Pred> + constexpr bool ranges::all_of(I first, S last, Pred pred, Proj proj = {}); // since C++20 + + template, Proj>> Pred> + constexpr bool ranges::all_of(R&& r, Pred pred, Proj proj = {}); // since C++20 + + template S, class Proj = identity, + indirect_unary_predicate> Pred> + constexpr bool ranges::any_of(I first, S last, Pred pred, Proj proj = {}); // since C++20 + + template, Proj>> Pred> + constexpr bool ranges::any_of(R&& r, Pred pred, Proj proj = {}); // since C++20 + + template S, class Proj = identity, + indirect_unary_predicate> Pred> + constexpr bool ranges::none_of(I first, S last, Pred pred, Proj proj = {}); // since C++20 + + template, Proj>> Pred> + constexpr bool ranges::none_of(R&& r, Pred pred, Proj proj = {}); // since C++20 + } constexpr bool // constexpr in C++20 @@ -1021,6 +1045,8 @@ #include <__algorithm/pop_heap.h> #include <__algorithm/prev_permutation.h> #include <__algorithm/push_heap.h> +#include <__algorithm/ranges_all_of.h> +#include <__algorithm/ranges_any_of.h> #include <__algorithm/ranges_copy.h> #include <__algorithm/ranges_copy_backward.h> #include <__algorithm/ranges_copy_if.h> @@ -1043,6 +1069,7 @@ #include <__algorithm/ranges_minmax.h> #include <__algorithm/ranges_minmax_element.h> #include <__algorithm/ranges_mismatch.h> +#include <__algorithm/ranges_none_of.h> #include <__algorithm/ranges_reverse.h> #include <__algorithm/ranges_swap_ranges.h> #include <__algorithm/ranges_transform.h> diff --git a/libcxx/include/module.modulemap b/libcxx/include/module.modulemap --- a/libcxx/include/module.modulemap +++ b/libcxx/include/module.modulemap @@ -298,6 +298,8 @@ module pop_heap { private header "__algorithm/pop_heap.h" } module prev_permutation { private header "__algorithm/prev_permutation.h" } module push_heap { private header "__algorithm/push_heap.h" } + module ranges_all_of { private header "__algorithm/ranges_all_of.h" } + module ranges_any_of { private header "__algorithm/ranges_any_of.h" } module ranges_copy { private header "__algorithm/ranges_copy.h" } module ranges_copy_backward { private header "__algorithm/ranges_copy_backward.h" } module ranges_copy_if { private header "__algorithm/ranges_copy_if.h" } @@ -320,6 +322,7 @@ module ranges_minmax { private header "__algorithm/ranges_minmax.h" } module ranges_minmax_element { private header "__algorithm/ranges_minmax_element.h" } module ranges_mismatch { private header "__algorithm/ranges_mismatch.h" } + module ranges_none_of { private header "__algorithm/ranges_none_of.h" } module ranges_reverse { private header "__algorithm/ranges_reverse.h" } module ranges_swap_ranges { private header "__algorithm/ranges_swap_ranges.h" } module ranges_transform { private header "__algorithm/ranges_transform.h" } diff --git a/libcxx/test/libcxx/algorithms/ranges_robust_against_copying_comparators.pass.cpp b/libcxx/test/libcxx/algorithms/ranges_robust_against_copying_comparators.pass.cpp --- a/libcxx/test/libcxx/algorithms/ranges_robust_against_copying_comparators.pass.cpp +++ b/libcxx/test/libcxx/algorithms/ranges_robust_against_copying_comparators.pass.cpp @@ -92,10 +92,10 @@ int copies = 0; //(void)std::ranges::adjacent_find(first, last, Equal(&copies)); assert(copies == 0); //(void)std::ranges::adjacent_find(a, Equal(&copies)); assert(copies == 0); - //(void)std::ranges::all_of(first, last, UnaryTrue(&copies)); assert(copies == 0); - //(void)std::ranges::all_of(a, UnaryTrue(&copies)); assert(copies == 0); - //(void)std::ranges::any_of(first, last, UnaryTrue(&copies)); assert(copies == 0); - //(void)std::ranges::any_of(a, UnaryTrue(&copies)); assert(copies == 0); + (void)std::ranges::all_of(first, last, UnaryTrue(&copies)); assert(copies == 0); + (void)std::ranges::all_of(a, UnaryTrue(&copies)); assert(copies == 0); + (void)std::ranges::any_of(first, last, UnaryTrue(&copies)); assert(copies == 0); + (void)std::ranges::any_of(a, UnaryTrue(&copies)); assert(copies == 0); //(void)std::ranges::binary_search(first, last, value, Less(&copies)); assert(copies == 0); //(void)std::ranges::binary_search(a, value, Less(&copies)); assert(copies == 0); //(void)std::ranges::clamp(value, value, value, Less(&copies)); assert(copies == 0); @@ -167,8 +167,8 @@ (void)std::ranges::mismatch(a, b, Equal(&copies)); assert(copies == 0); //(void)std::ranges::next_permutation(first, last, Less(&copies)); assert(copies == 0); //(void)std::ranges::next_permutation(a, Less(&copies)); assert(copies == 0); - //(void)std::ranges::none_of(first, last, UnaryTrue(&copies)); assert(copies == 0); - //(void)std::ranges::none_of(a, UnaryTrue(&copies)); assert(copies == 0); + (void)std::ranges::none_of(first, last, UnaryTrue(&copies)); assert(copies == 0); + (void)std::ranges::none_of(a, UnaryTrue(&copies)); assert(copies == 0); //(void)std::ranges::nth_element(first, mid, last, Less(&copies)); assert(copies == 0); //(void)std::ranges::nth_element(a, mid, Less(&copies)); assert(copies == 0); //(void)std::ranges::partial_sort(first, mid, last, Less(&copies)); assert(copies == 0); diff --git a/libcxx/test/libcxx/algorithms/ranges_robust_against_copying_projections.pass.cpp b/libcxx/test/libcxx/algorithms/ranges_robust_against_copying_projections.pass.cpp --- a/libcxx/test/libcxx/algorithms/ranges_robust_against_copying_projections.pass.cpp +++ b/libcxx/test/libcxx/algorithms/ranges_robust_against_copying_projections.pass.cpp @@ -74,10 +74,10 @@ int copies = 0; //(void)std::ranges::adjacent_find(first, last, Equal(), Proj(&copies)); assert(copies == 0); //(void)std::ranges::adjacent_find(a, Equal(), Proj(&copies)); assert(copies == 0); - //(void)std::ranges::all_of(first, last, UnaryTrue(), Proj(&copies)); assert(copies == 0); - //(void)std::ranges::all_of(a, UnaryTrue(), Proj(&copies)); assert(copies == 0); - //(void)std::ranges::any_of(first, last, UnaryTrue(), Proj(&copies)); assert(copies == 0); - //(void)std::ranges::any_of(a, UnaryTrue(), Proj(&copies)); assert(copies == 0); + (void)std::ranges::all_of(first, last, UnaryTrue(), Proj(&copies)); assert(copies == 0); + (void)std::ranges::all_of(a, UnaryTrue(), Proj(&copies)); assert(copies == 0); + (void)std::ranges::any_of(first, last, UnaryTrue(), Proj(&copies)); assert(copies == 0); + (void)std::ranges::any_of(a, UnaryTrue(), Proj(&copies)); assert(copies == 0); //(void)std::ranges::binary_search(first, last, value, Less(), Proj(&copies)); assert(copies == 0); //(void)std::ranges::binary_search(a, value, Less(), Proj(&copies)); assert(copies == 0); //(void)std::ranges::clamp(T(), T(), T(), Less(), Proj(&copies)); assert(copies == 0); @@ -150,8 +150,8 @@ (void)std::ranges::mismatch(a, b, Equal(), Proj(&copies), Proj(&copies)); assert(copies == 0); //(void)std::ranges::next_permutation(first, last, Less(), Proj(&copies)); assert(copies == 0); //(void)std::ranges::next_permutation(a, Less(), Proj(&copies)); assert(copies == 0); - //(void)std::ranges::none_of(first, last, UnaryTrue(), Proj(&copies)); assert(copies == 0); - //(void)std::ranges::none_of(a, UnaryTrue(), Proj(&copies)); assert(copies == 0); + (void)std::ranges::none_of(first, last, UnaryTrue(), Proj(&copies)); assert(copies == 0); + (void)std::ranges::none_of(a, UnaryTrue(), Proj(&copies)); assert(copies == 0); //(void)std::ranges::nth_element(first, mid, last, Less(), Proj(&copies)); assert(copies == 0); //(void)std::ranges::nth_element(a, mid, Less(), Proj(&copies)); assert(copies == 0); //(void)std::ranges::partial_sort(first, mid, last, Less(), Proj(&copies)); assert(copies == 0); diff --git a/libcxx/test/libcxx/private_headers.verify.cpp b/libcxx/test/libcxx/private_headers.verify.cpp --- a/libcxx/test/libcxx/private_headers.verify.cpp +++ b/libcxx/test/libcxx/private_headers.verify.cpp @@ -103,6 +103,8 @@ #include <__algorithm/pop_heap.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/pop_heap.h'}} #include <__algorithm/prev_permutation.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/prev_permutation.h'}} #include <__algorithm/push_heap.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/push_heap.h'}} +#include <__algorithm/ranges_all_of.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_all_of.h'}} +#include <__algorithm/ranges_any_of.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_any_of.h'}} #include <__algorithm/ranges_copy.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_copy.h'}} #include <__algorithm/ranges_copy_backward.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_copy_backward.h'}} #include <__algorithm/ranges_copy_if.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_copy_if.h'}} @@ -125,6 +127,7 @@ #include <__algorithm/ranges_minmax.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_minmax.h'}} #include <__algorithm/ranges_minmax_element.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_minmax_element.h'}} #include <__algorithm/ranges_mismatch.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_mismatch.h'}} +#include <__algorithm/ranges_none_of.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_none_of.h'}} #include <__algorithm/ranges_reverse.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_reverse.h'}} #include <__algorithm/ranges_swap_ranges.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_swap_ranges.h'}} #include <__algorithm/ranges_transform.h> // expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_transform.h'}} diff --git a/libcxx/test/std/algorithms/alg.nonmodifying/alg.all_of/ranges.all_of.pass.cpp b/libcxx/test/std/algorithms/alg.nonmodifying/alg.all_of/ranges.all_of.pass.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/algorithms/alg.nonmodifying/alg.all_of/ranges.all_of.pass.cpp @@ -0,0 +1,159 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// + +// UNSUPPORTED: c++03, c++11, c++14, c++17 +// UNSUPPORTED: libcpp-has-no-incomplete-ranges + +// template S, class Proj = identity, +// indirect_unary_predicate> Pred> +// constexpr bool ranges::all_of(I first, S last, Pred pred, Proj proj = {}); +// template, Proj>> Pred> +// constexpr bool ranges::all_of(R&& r, Pred pred, Proj proj = {}); + +#include +#include +#include +#include + +#include "almost_satisfies_types.h" +#include "test_iterators.h" + +struct UnaryFunctor { + bool operator()(auto&&); +}; + +template > +concept HasAllOfIt = requires(It first, Sent last) { std::ranges::all_of(first, last, UnaryFunctor{}); }; + +static_assert(HasAllOfIt); +static_assert(!HasAllOfIt); +static_assert(!HasAllOfIt); +static_assert(!HasAllOfIt); +static_assert(!HasAllOfIt); +static_assert(!HasAllOfIt); + +template +concept HasAllOfItFunc = requires(int* ptr) { std::ranges::all_of(ptr, ptr, Func{}); }; + +static_assert(HasAllOfItFunc); +static_assert(!HasAllOfItFunc); +static_assert(!HasAllOfItFunc); + +template +concept HasAllOfR = requires(Range range) { std::ranges::all_of(range, UnaryFunctor{}); }; + +static_assert(HasAllOfR>); +static_assert(!HasAllOfR); +static_assert(!HasAllOfR); +static_assert(!HasAllOfR); +static_assert(!HasAllOfR); +static_assert(!HasAllOfR); + +template +concept HasAllOfRFunc = requires(std::array range) { std::ranges::all_of(range, Func{}); }; + +static_assert(HasAllOfRFunc); +static_assert(!HasAllOfRFunc); +static_assert(!HasAllOfRFunc); + +template +constexpr void test_iterators() { + { // simple test + { + int a[] = {1, 2, 3, 4}; + std::same_as decltype(auto) ret = std::ranges::all_of(It(a), Sent(It(a + 4)), [](int) { return true; }); + assert(ret); + } + { + int a[] = {1, 2, 3, 4}; + auto range = std::ranges::subrange(It(a), Sent(It(a + 4))); + std::same_as decltype(auto) ret = std::ranges::all_of(range, [](int) { return true; }); + assert(ret); + } + } + + { // check that an empty range works + std::array a; + assert(std::ranges::all_of(It(a.data()), Sent(It(a.data())), [](int) { return false; })); + auto range = std::ranges::subrange(It(a.data()), Sent(It(a.data()))); + assert(std::ranges::all_of(range, [](int) { return false; })); + } + + { // check that the complexity requirements are met + { + int predicateCount = 0; + int projectionCount = 0; + auto pred = [&](int) { ++predicateCount; return true; }; + auto proj = [&](int i) { ++projectionCount; return i; }; + std::array a = {9, 7, 5, 3}; + assert(std::ranges::all_of(It(a.begin()), Sent(It(a.end())), pred, proj)); + assert(predicateCount == 4); + assert(projectionCount == 4); + } + { + int predicateCount = 0; + int projectionCount = 0; + auto pred = [&](int) { ++predicateCount; return true; }; + auto proj = [&](int i) { ++projectionCount; return i; }; + std::array a = {9, 7, 5, 3}; + auto range = std::ranges::subrange(It(a.data()), Sent(It(a.data() + a.size()))); + assert(std::ranges::all_of(range, pred, proj)); + assert(predicateCount == 4); + assert(projectionCount == 4); + } + } + + { // check that false is returned if no element satisfies the condition + std::array a = {1, 2, 3, 4}; + assert(!std::ranges::all_of(It(a.data()), Sent(It(a.data() + a.size())), [](int i) { return i > 5; })); + auto range = std::ranges::subrange(It(a.data()), Sent(It(a.data() + a.size()))); + assert(!std::ranges::all_of(range, [](int i) { return i > 5; })); + } + + { // check that true is returned if all elements satisfy the condition + std::array a = {1, 2, 3, 4}; + assert(std::ranges::all_of(It(a.data()), Sent(It(a.data() + a.size())), [](int i) { return i < 5; })); + auto range = std::ranges::subrange(It(a.data()), Sent(It(a.data() + a.size()))); + assert(std::ranges::all_of(range, [](int i) { return i < 5; })); + } + + { // check that false is returned if ony one elements satisfies the condition + std::array a = {1, 2, 3, 4, 6}; + assert(!std::ranges::all_of(It(a.data()), Sent(It(a.data() + a.size())), [](int i) { return i > 5; })); + auto range = std::ranges::subrange(It(a.data()), Sent(It(a.data() + a.size()))); + assert(!std::ranges::all_of(range, [](int i) { return i > 5; })); + } +} + +constexpr bool test() { + test_iterators, sentinel_wrapper>>(); + test_iterators>(); + test_iterators>(); + test_iterators>(); + test_iterators>(); + test_iterators(); + + { // check that std::invoke is used + struct S { int check; int other; }; + S a[] = {{1, 2}, {1, 7}, {1, 3}}; + assert(std::ranges::all_of(a, a + 3, [](int i) { return i == 1; }, &S::check)); + assert(std::ranges::all_of(a, [](int i) { return i == 1; }, &S::check)); + } + + return true; +} + +int main(int, char**) { + test(); + static_assert(test()); + + return 0; +} diff --git a/libcxx/test/std/algorithms/alg.nonmodifying/alg.any_of/ranges.any_of.pass.cpp b/libcxx/test/std/algorithms/alg.nonmodifying/alg.any_of/ranges.any_of.pass.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/algorithms/alg.nonmodifying/alg.any_of/ranges.any_of.pass.cpp @@ -0,0 +1,159 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// + +// UNSUPPORTED: c++03, c++11, c++14, c++17 +// UNSUPPORTED: libcpp-has-no-incomplete-ranges + +// template S, class Proj = identity, +// indirect_unary_predicate> Pred> +// constexpr bool ranges::any_of(I first, S last, Pred pred, Proj proj = {}); +// template, Proj>> Pred> +// constexpr bool ranges::any_of(R&& r, Pred pred, Proj proj = {}); + +#include +#include +#include +#include + +#include "almost_satisfies_types.h" +#include "test_iterators.h" + +struct UnaryFunctor { + bool operator()(auto&&); +}; + +template > +concept HasAnyOfIt = requires(It first, Sent last) { std::ranges::any_of(first, last, UnaryFunctor{}); }; + +static_assert(HasAnyOfIt); +static_assert(!HasAnyOfIt); +static_assert(!HasAnyOfIt); +static_assert(!HasAnyOfIt); +static_assert(!HasAnyOfIt); +static_assert(!HasAnyOfIt); + +template +concept HasAnyOfItFunc = requires(int* ptr) { std::ranges::any_of(ptr, ptr, Func{}); }; + +static_assert(HasAnyOfItFunc); +static_assert(!HasAnyOfItFunc); +static_assert(!HasAnyOfItFunc); + +template +concept HasAnyOfR = requires(Range range) { std::ranges::any_of(range, UnaryFunctor{}); }; + +static_assert(HasAnyOfR>); +static_assert(!HasAnyOfR); +static_assert(!HasAnyOfR); +static_assert(!HasAnyOfR); +static_assert(!HasAnyOfR); +static_assert(!HasAnyOfR); + +template +concept HasAnyOfRFunc = requires(std::array range) { std::ranges::any_of(range, Func{}); }; + +static_assert(HasAnyOfRFunc); +static_assert(!HasAnyOfRFunc); +static_assert(!HasAnyOfRFunc); + +template +constexpr void test_iterators() { + { // simple test + { + int a[] = {1, 2, 3, 4}; + std::same_as decltype(auto) ret = std::ranges::any_of(It(a), Sent(It(a + 4)), [](int) { return true; }); + assert(ret); + } + { + int a[] = {1, 2, 3, 4}; + auto range = std::ranges::subrange(It(a), Sent(It(a + 4))); + std::same_as decltype(auto) ret = std::ranges::any_of(range, [](int) { return true; }); + assert(ret); + } + } + + { // check that an empty range works + std::array a; + assert(!std::ranges::any_of(It(a.data()), Sent(It(a.data())), [](int) { return false; })); + auto range = std::ranges::subrange(It(a.data()), Sent(It(a.data()))); + assert(!std::ranges::any_of(range, [](int) { return false; })); + } + + { // check that the complexity requirements are met + { + int predicateCount = 0; + int projectionCount = 0; + auto pred = [&](int) { ++predicateCount; return false; }; + auto proj = [&](int i) { ++projectionCount; return i; }; + std::array a = {9, 7, 5, 3}; + assert(!std::ranges::any_of(It(a.begin()), Sent(It(a.end())), pred, proj)); + assert(predicateCount == 4); + assert(projectionCount == 4); + } + { + int predicateCount = 0; + int projectionCount = 0; + auto pred = [&](int) { ++predicateCount; return false; }; + auto proj = [&](int i) { ++projectionCount; return i; }; + std::array a = {9, 7, 5, 3}; + auto range = std::ranges::subrange(It(a.data()), Sent(It(a.data() + a.size()))); + assert(!std::ranges::any_of(range, pred, proj)); + assert(predicateCount == 4); + assert(projectionCount == 4); + } + } + + { // check that false is returned if no element satisfies the condition + std::array a = {1, 2, 3, 4}; + assert(!std::ranges::any_of(It(a.data()), Sent(It(a.data() + a.size())), [](int i) { return i > 5; })); + auto range = std::ranges::subrange(It(a.data()), Sent(It(a.data() + a.size()))); + assert(!std::ranges::any_of(range, [](int i) { return i > 5; })); + } + + { // check that true is returned if all elements satisfy the condition + std::array a = {1, 2, 3, 4}; + assert(std::ranges::any_of(It(a.data()), Sent(It(a.data() + a.size())), [](int i) { return i < 5; })); + auto range = std::ranges::subrange(It(a.data()), Sent(It(a.data() + a.size()))); + assert(std::ranges::any_of(range, [](int i) { return i < 5; })); + } + + { // check that true is returned if ony one elements satisfies the condition + std::array a = {1, 2, 3, 4, 6}; + assert(std::ranges::any_of(It(a.data()), Sent(It(a.data() + a.size())), [](int i) { return i > 5; })); + auto range = std::ranges::subrange(It(a.data()), Sent(It(a.data() + a.size()))); + assert(std::ranges::any_of(range, [](int i) { return i > 5; })); + } +} + +constexpr bool test() { + test_iterators, sentinel_wrapper>>(); + test_iterators>(); + test_iterators>(); + test_iterators>(); + test_iterators>(); + test_iterators(); + + { // check that std::invoke is used + struct S { int check; int other; }; + S a[] = {{1, 2}, {1, 7}, {1, 3}}; + assert(std::ranges::any_of(a, a + 3, [](int i) { return i == 1; }, &S::check)); + assert(std::ranges::any_of(a, [](int i) { return i == 1; }, &S::check)); + } + + return true; +} + +int main(int, char**) { + test(); + static_assert(test()); + + return 0; +} diff --git a/libcxx/test/std/algorithms/alg.nonmodifying/alg.none_of/ranges.none_of.pass.cpp b/libcxx/test/std/algorithms/alg.nonmodifying/alg.none_of/ranges.none_of.pass.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/algorithms/alg.nonmodifying/alg.none_of/ranges.none_of.pass.cpp @@ -0,0 +1,159 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// + +// UNSUPPORTED: c++03, c++11, c++14, c++17 +// UNSUPPORTED: libcpp-has-no-incomplete-ranges + +// template S, class Proj = identity, +// indirect_unary_predicate> Pred> +// constexpr bool ranges::none_of(I first, S last, Pred pred, Proj proj = {}); +// template, Proj>> Pred> +// constexpr bool ranges::none_of(R&& r, Pred pred, Proj proj = {}); + +#include +#include +#include +#include + +#include "almost_satisfies_types.h" +#include "test_iterators.h" + +struct UnaryFunctor { + bool operator()(auto&&); +}; + +template > +concept HasNoneOfIt = requires(It first, Sent last) { std::ranges::none_of(first, last, UnaryFunctor{}); }; + +static_assert(HasNoneOfIt); +static_assert(!HasNoneOfIt); +static_assert(!HasNoneOfIt); +static_assert(!HasNoneOfIt); +static_assert(!HasNoneOfIt); +static_assert(!HasNoneOfIt); + +template +concept HasNoneOfItFunc = requires(int* ptr) { std::ranges::none_of(ptr, ptr, Func{}); }; + +static_assert(HasNoneOfItFunc); +static_assert(!HasNoneOfItFunc); +static_assert(!HasNoneOfItFunc); + +template +concept HasNoneOfR = requires(Range range) { std::ranges::none_of(range, UnaryFunctor{}); }; + +static_assert(HasNoneOfR>); +static_assert(!HasNoneOfR); +static_assert(!HasNoneOfR); +static_assert(!HasNoneOfR); +static_assert(!HasNoneOfR); +static_assert(!HasNoneOfR); + +template +concept HasNoneOfRFunc = requires(std::array range) { std::ranges::none_of(range, Func{}); }; + +static_assert(HasNoneOfRFunc); +static_assert(!HasNoneOfRFunc); +static_assert(!HasNoneOfRFunc); + +template +constexpr void test_iterators() { + { // simple test + { + int a[] = {1, 2, 3, 4}; + std::same_as decltype(auto) ret = std::ranges::none_of(It(a), Sent(It(a + 4)), [](int) { return true; }); + assert(!ret); + } + { + int a[] = {1, 2, 3, 4}; + auto range = std::ranges::subrange(It(a), Sent(It(a + 4))); + std::same_as decltype(auto) ret = std::ranges::none_of(range, [](int) { return true; }); + assert(!ret); + } + } + + { // check that an empty range works + std::array a; + assert(std::ranges::none_of(It(a.data()), Sent(It(a.data())), [](int) { return false; })); + auto range = std::ranges::subrange(It(a.data()), Sent(It(a.data()))); + assert(std::ranges::none_of(range, [](int) { return false; })); + } + + { // check that the complexity requirements are met + { + int predicateCount = 0; + int projectionCount = 0; + auto pred = [&](int) { ++predicateCount; return false; }; + auto proj = [&](int i) { ++projectionCount; return i; }; + std::array a = {9, 7, 5, 3}; + assert(std::ranges::none_of(It(a.begin()), Sent(It(a.end())), pred, proj)); + assert(predicateCount == 4); + assert(projectionCount == 4); + } + { + int predicateCount = 0; + int projectionCount = 0; + auto pred = [&](int) { ++predicateCount; return false; }; + auto proj = [&](int i) { ++projectionCount; return i; }; + std::array a = {9, 7, 5, 3}; + auto range = std::ranges::subrange(It(a.data()), Sent(It(a.data() + a.size()))); + assert(std::ranges::none_of(range, pred, proj)); + assert(predicateCount == 4); + assert(projectionCount == 4); + } + } + + { // check that true is returned if no element satisfies the condition + std::array a = {1, 2, 3, 4}; + assert(std::ranges::none_of(It(a.data()), Sent(It(a.data() + a.size())), [](int i) { return i > 5; })); + auto range = std::ranges::subrange(It(a.data()), Sent(It(a.data() + a.size()))); + assert(std::ranges::none_of(range, [](int i) { return i > 5; })); + } + + { // check that false is returned if all elements satisfy the condition + std::array a = {1, 2, 3, 4}; + assert(!std::ranges::none_of(It(a.data()), Sent(It(a.data() + a.size())), [](int i) { return i < 5; })); + auto range = std::ranges::subrange(It(a.data()), Sent(It(a.data() + a.size()))); + assert(!std::ranges::none_of(range, [](int i) { return i < 5; })); + } + + { // check that false is returned if ony one elements satisfies the condition + std::array a = {1, 2, 3, 4, 6}; + assert(!std::ranges::none_of(It(a.data()), Sent(It(a.data() + a.size())), [](int i) { return i > 5; })); + auto range = std::ranges::subrange(It(a.data()), Sent(It(a.data() + a.size()))); + assert(!std::ranges::none_of(range, [](int i) { return i > 5; })); + } +} + +constexpr bool test() { + test_iterators, sentinel_wrapper>>(); + test_iterators>(); + test_iterators>(); + test_iterators>(); + test_iterators>(); + test_iterators(); + + { // check that std::invoke is used + struct S { int check; int other; }; + S a[] = {{1, 2}, {1, 7}, {1, 3}}; + assert(std::ranges::none_of(a, a + 3, [](int i) { return i != 1; }, &S::check)); + assert(std::ranges::none_of(a, [](int i) { return i != 1; }, &S::check)); + } + + return true; +} + +int main(int, char**) { + test(); + static_assert(test()); + + return 0; +} diff --git a/libcxx/test/std/library/description/conventions/customization.point.object/niebloid.compile.pass.cpp b/libcxx/test/std/library/description/conventions/customization.point.object/niebloid.compile.pass.cpp --- a/libcxx/test/std/library/description/conventions/customization.point.object/niebloid.compile.pass.cpp +++ b/libcxx/test/std/library/description/conventions/customization.point.object/niebloid.compile.pass.cpp @@ -61,8 +61,8 @@ // [algorithm.syn] //static_assert(test(std::ranges::adjacent_find, a)); -//static_assert(test(std::ranges::all_of, a, odd)); -//static_assert(test(std::ranges::any_of, a, odd)); +static_assert(test(std::ranges::all_of, a, odd)); +static_assert(test(std::ranges::any_of, a, odd)); //static_assert(test(std::ranges::binary_search, a, 42)); //static_assert(test(std::ranges::clamp, 42, 42, 42)); //static_assert(test(std::ranges::copy, a, a)); @@ -107,7 +107,7 @@ //static_assert(test(std::ranges::move, a, a)); //static_assert(test(std::ranges::move_backward, a, a)); //static_assert(test(std::ranges::next_permutation, a)); -//static_assert(test(std::ranges::none_of, a, odd)); +static_assert(test(std::ranges::none_of, a, odd)); //static_assert(test(std::ranges::nth_element, a, a+5)); //static_assert(test(std::ranges::partial_sort, a, a+5)); //static_assert(test(std::ranges::partial_sort_copy, a, a));