[libcxx-commits] [libcxx] c559964 - [libc++][ranges] implement `std::ranges::includes`

Hui Xie via libcxx-commits libcxx-commits at lists.llvm.org
Fri Jul 22 02:31:07 PDT 2022


Author: Hui Xie
Date: 2022-07-22T10:27:48+01:00
New Revision: c559964d85e8efc1f6949df940df9265a0ac0663

URL: https://github.com/llvm/llvm-project/commit/c559964d85e8efc1f6949df940df9265a0ac0663
DIFF: https://github.com/llvm/llvm-project/commit/c559964d85e8efc1f6949df940df9265a0ac0663.diff

LOG: [libc++][ranges] implement `std::ranges::includes`

implement `std::ranges::includes` and delegate to `std::includes`

Differential Revision: https://reviews.llvm.org/D130116

Added: 
    

Modified: 
    libcxx/docs/Status/RangesAlgorithms.csv
    libcxx/include/__algorithm/includes.h
    libcxx/include/__algorithm/ranges_includes.h
    libcxx/include/algorithm
    libcxx/test/libcxx/algorithms/ranges_robust_against_copying_comparators.pass.cpp
    libcxx/test/libcxx/algorithms/ranges_robust_against_copying_projections.pass.cpp
    libcxx/test/std/algorithms/alg.sorting/alg.set.operations/includes/ranges_includes.pass.cpp
    libcxx/test/std/algorithms/ranges_robust_against_nonbool_predicates.pass.cpp
    libcxx/test/std/algorithms/ranges_robust_against_omitting_invoke.pass.cpp
    libcxx/test/std/algorithms/ranges_robust_against_proxy_iterators.pass.cpp
    libcxx/test/std/library/description/conventions/customization.point.object/niebloid.compile.pass.cpp

Removed: 
    


################################################################################
diff  --git a/libcxx/docs/Status/RangesAlgorithms.csv b/libcxx/docs/Status/RangesAlgorithms.csv
index e433266729cef..787ec9b08d4f9 100644
--- a/libcxx/docs/Status/RangesAlgorithms.csv
+++ b/libcxx/docs/Status/RangesAlgorithms.csv
@@ -29,7 +29,7 @@ Search,find_end,Nikolas Klauser,`D124079 <https://llvm.org/D124079>`_,✅
 Read-only,is_partitioned,Nikolas Klauser,`D124440 <https://llvm.org/D124440>`_,✅
 Read-only,is_sorted,Nikolas Klauser,`D125608 <https://llvm.org/D125608>`_,✅
 Read-only,is_sorted_until,Nikolas Klauser,`D125608 <https://llvm.org/D125608>`_,✅
-Read-only,includes,Nikolas Klauser,n/a,Not started
+Read-only,includes,Hui Xie,`D130116 <https://llvm.org/D130116>`,✅
 Read-only,is_heap,Nikolas Klauser,n/a,Not started
 Read-only,is_heap_until,Nikolas Klauser,n/a,Not started
 Read-only,clamp,Nikolas Klauser,`D126193 <https://llvm.org/D126193>`_,Under review

diff  --git a/libcxx/include/__algorithm/includes.h b/libcxx/include/__algorithm/includes.h
index 4c87e8d221168..102d3db39a2d8 100644
--- a/libcxx/include/__algorithm/includes.h
+++ b/libcxx/include/__algorithm/includes.h
@@ -13,6 +13,7 @@
 #include <__algorithm/comp_ref_type.h>
 #include <__config>
 #include <__iterator/iterator_traits.h>
+#include <__utility/move.h>
 
 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
 #  pragma GCC system_header
@@ -20,41 +21,40 @@
 
 _LIBCPP_BEGIN_NAMESPACE_STD
 
-template <class _Compare, class _InputIterator1, class _InputIterator2>
-_LIBCPP_CONSTEXPR_AFTER_CXX17 bool
-__includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2,
-           _Compare __comp)
-{
-    for (; __first2 != __last2; ++__first1)
-    {
-        if (__first1 == __last1 || __comp(*__first2, *__first1))
-            return false;
-        if (!__comp(*__first1, *__first2))
-            ++__first2;
-    }
-    return true;
+template <class _Iter1, class _Sent1, class _Iter2, class _Sent2, class _Comp>
+_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 bool
+__includes(_Iter1 __first1, _Sent1 __last1, _Iter2 __first2, _Sent2 __last2, _Comp&& __comp) {
+  for (; __first2 != __last2; ++__first1) {
+    if (__first1 == __last1 || __comp(*__first2, *__first1))
+      return false;
+    if (!__comp(*__first1, *__first2))
+      ++__first2;
+  }
+  return true;
 }
 
 template <class _InputIterator1, class _InputIterator2, class _Compare>
-_LIBCPP_NODISCARD_EXT inline
-_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
-bool
-includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2,
-         _Compare __comp)
-{
-    typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
-    return _VSTD::__includes<_Comp_ref>(__first1, __last1, __first2, __last2, __comp);
+_LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 bool includes(
+    _InputIterator1 __first1,
+    _InputIterator1 __last1,
+    _InputIterator2 __first2,
+    _InputIterator2 __last2,
+    _Compare __comp) {
+  typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
+  return std::__includes(
+      std::move(__first1), std::move(__last1), std::move(__first2), std::move(__last2), static_cast<_Comp_ref>(__comp));
 }
 
 template <class _InputIterator1, class _InputIterator2>
-_LIBCPP_NODISCARD_EXT inline
-_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
-bool
-includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2)
-{
-    return _VSTD::includes(__first1, __last1, __first2, __last2,
-                          __less<typename iterator_traits<_InputIterator1>::value_type,
-                                 typename iterator_traits<_InputIterator2>::value_type>());
+_LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 bool
+includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2) {
+  return std::includes(
+      std::move(__first1),
+      std::move(__last1),
+      std::move(__first2),
+      std::move(__last2),
+      __less<typename iterator_traits<_InputIterator1>::value_type,
+             typename iterator_traits<_InputIterator2>::value_type>());
 }
 
 _LIBCPP_END_NAMESPACE_STD

diff  --git a/libcxx/include/__algorithm/ranges_includes.h b/libcxx/include/__algorithm/ranges_includes.h
index db550de5674f5..ba054e6fd89da 100644
--- a/libcxx/include/__algorithm/ranges_includes.h
+++ b/libcxx/include/__algorithm/ranges_includes.h
@@ -9,8 +9,8 @@
 #ifndef _LIBCPP___ALGORITHM_RANGES_INCLUDES_H
 #define _LIBCPP___ALGORITHM_RANGES_INCLUDES_H
 
-#include <__algorithm/make_projected.h>
 #include <__algorithm/includes.h>
+#include <__algorithm/make_projected.h>
 #include <__config>
 #include <__functional/identity.h>
 #include <__functional/invoke.h>
@@ -35,29 +35,46 @@ namespace ranges {
 namespace __includes {
 
 struct __fn {
-
-  template <input_iterator _Iter1, sentinel_for<_Iter1> _Sent1, input_iterator _Iter2, sentinel_for<_Iter2> _Sent2,
-            class _Proj1 = identity, class _Proj2 = identity,
-            indirect_strict_weak_order<projected<_Iter1, _Proj1>, projected<_Iter2, _Proj2>> _Comp = ranges::less>
-  _LIBCPP_HIDE_FROM_ABI constexpr
-  bool operator()(_Iter1 __first1, _Sent1 __last1, _Iter2 __first2, _Sent2 __last2, _Comp __comp = {},
-                  _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const {
-    // TODO: implement
-    (void)__first1; (void)__last1; (void)__first2; (void)__last2; (void)__comp; (void)__proj1; (void)__proj2;
-    return {};
+  template <
+      input_iterator _Iter1,
+      sentinel_for<_Iter1> _Sent1,
+      input_iterator _Iter2,
+      sentinel_for<_Iter2> _Sent2,
+      class _Proj1                                                                           = identity,
+      class _Proj2                                                                           = identity,
+      indirect_strict_weak_order<projected<_Iter1, _Proj1>, projected<_Iter2, _Proj2>> _Comp = ranges::less>
+  _LIBCPP_HIDE_FROM_ABI constexpr bool operator()(
+      _Iter1 __first1,
+      _Sent1 __last1,
+      _Iter2 __first2,
+      _Sent2 __last2,
+      _Comp __comp   = {},
+      _Proj1 __proj1 = {},
+      _Proj2 __proj2 = {}) const {
+    return std::__includes(
+        std::move(__first1),
+        std::move(__last1),
+        std::move(__first2),
+        std::move(__last2),
+        ranges::__make_projected_comp(__comp, __proj1, __proj2));
   }
 
-  template <input_range _Range1, input_range _Range2, class _Proj1 = identity, class _Proj2 = identity,
-            indirect_strict_weak_order<projected<iterator_t<_Range1>, _Proj1>,
-                                       projected<iterator_t<_Range2>, _Proj2>> _Comp = ranges::less>
-  _LIBCPP_HIDE_FROM_ABI constexpr
-  bool operator()(_Range1&& __range1, _Range2&& __range2, _Comp __comp = {},
-                  _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const {
-    // TODO: implement
-    (void)__range1; (void)__range2; (void)__comp; (void)__proj1; (void)__proj2;
-    return {};
+  template <
+      input_range _Range1,
+      input_range _Range2,
+      class _Proj1 = identity,
+      class _Proj2 = identity,
+      indirect_strict_weak_order<projected<iterator_t<_Range1>, _Proj1>, projected<iterator_t<_Range2>, _Proj2>>
+          _Comp = ranges::less>
+  _LIBCPP_HIDE_FROM_ABI constexpr bool operator()(
+      _Range1&& __range1, _Range2&& __range2, _Comp __comp = {}, _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const {
+    return std::__includes(
+        ranges::begin(__range1),
+        ranges::end(__range1),
+        ranges::begin(__range2),
+        ranges::end(__range2),
+        ranges::__make_projected_comp(__comp, __proj1, __proj2));
   }
-
 };
 
 } // namespace __includes

diff  --git a/libcxx/include/algorithm b/libcxx/include/algorithm
index 19cf3b8fc4aae..aea6ad86eb59c 100644
--- a/libcxx/include/algorithm
+++ b/libcxx/include/algorithm
@@ -801,6 +801,20 @@ namespace ranges {
     constexpr set_union_result<borrowed_iterator_t<R1>, borrowed_iterator_t<R2>, O>
       set_union(R1&& r1, R2&& r2, O result, Comp comp = {},
                 Proj1 proj1 = {}, Proj2 proj2 = {});                                                // since C++20
+
+  template<input_iterator I1, sentinel_for<I1> S1, input_iterator I2, sentinel_for<I2> S2,
+           class Proj1 = identity, class Proj2 = identity,
+           indirect_strict_weak_order<projected<I1, Proj1>, projected<I2, Proj2>> Comp =
+             ranges::less>
+    constexpr bool includes(I1 first1, S1 last1, I2 first2, S2 last2, Comp comp = {},
+                            Proj1 proj1 = {}, Proj2 proj2 = {});                                   // Since C++20
+  
+  template<input_range R1, input_range R2, class Proj1 = identity,
+           class Proj2 = identity,
+           indirect_strict_weak_order<projected<iterator_t<R1>, Proj1>,
+                                      projected<iterator_t<R2>, Proj2>> Comp = ranges::less>
+    constexpr bool includes(R1&& r1, R2&& r2, Comp comp = {},
+                            Proj1 proj1 = {}, Proj2 proj2 = {});                                   // Since C++20
 }
 
     constexpr bool     // constexpr in C++20
@@ -1551,6 +1565,7 @@ template <class BidirectionalIterator, class Compare>
 #include <__algorithm/ranges_find_if_not.h>
 #include <__algorithm/ranges_for_each.h>
 #include <__algorithm/ranges_for_each_n.h>
+#include <__algorithm/ranges_includes.h>
 #include <__algorithm/ranges_is_partitioned.h>
 #include <__algorithm/ranges_is_sorted.h>
 #include <__algorithm/ranges_is_sorted_until.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
index 4ac4c4f3909c5..6fe0cfc177ef6 100644
--- a/libcxx/test/libcxx/algorithms/ranges_robust_against_copying_comparators.pass.cpp
+++ b/libcxx/test/libcxx/algorithms/ranges_robust_against_copying_comparators.pass.cpp
@@ -124,8 +124,8 @@ constexpr bool all_the_algorithms()
     //(void)std::ranges::generate(first, last, NullaryValue(&copies)); assert(copies == 0);
     //(void)std::ranges::generate(a, NullaryValue(&copies)); assert(copies == 0);
     //(void)std::ranges::generate_n(first, count, NullaryValue(&copies)); assert(copies == 0);
-    //(void)std::ranges::includes(first, last, first2, last2, Less(&copies)); assert(copies == 0);
-    //(void)std::ranges::includes(a, b, Less(&copies)); assert(copies == 0);
+    (void)std::ranges::includes(first, last, first2, last2, Less(&copies)); assert(copies == 0);
+    (void)std::ranges::includes(a, b, Less(&copies)); assert(copies == 0);
     //(void)std::ranges::is_heap(first, last, Less(&copies)); assert(copies == 0);
     //(void)std::ranges::is_heap(a, Less(&copies)); assert(copies == 0);
     //(void)std::ranges::is_heap_until(first, 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
index 62950e0230389..56e7471c73f98 100644
--- a/libcxx/test/libcxx/algorithms/ranges_robust_against_copying_projections.pass.cpp
+++ b/libcxx/test/libcxx/algorithms/ranges_robust_against_copying_projections.pass.cpp
@@ -107,8 +107,8 @@ constexpr bool all_the_algorithms()
     (void)std::ranges::for_each(first, last, UnaryVoid(), Proj(&copies)); assert(copies == 0);
     (void)std::ranges::for_each(a, UnaryVoid(), Proj(&copies)); assert(copies == 0);
     (void)std::ranges::for_each_n(first, count, UnaryVoid(), Proj(&copies)); assert(copies == 0);
-    //(void)std::ranges::includes(first, last, first2, last2, Less(), Proj(&copies), Proj(&copies)); assert(copies == 0);
-    //(void)std::ranges::includes(a, b, Less(), Proj(&copies), Proj(&copies)); assert(copies == 0);
+    (void)std::ranges::includes(first, last, first2, last2, Less(), Proj(&copies), Proj(&copies)); assert(copies == 0);
+    (void)std::ranges::includes(a, b, Less(), Proj(&copies), Proj(&copies)); assert(copies == 0);
     //(void)std::ranges::is_heap(first, last, Less(), Proj(&copies)); assert(copies == 0);
     //(void)std::ranges::is_heap(a, Less(), Proj(&copies)); assert(copies == 0);
     //(void)std::ranges::is_heap_until(first, last, Less(), Proj(&copies)); assert(copies == 0);

diff  --git a/libcxx/test/std/algorithms/alg.sorting/alg.set.operations/includes/ranges_includes.pass.cpp b/libcxx/test/std/algorithms/alg.sorting/alg.set.operations/includes/ranges_includes.pass.cpp
index 2415a19841cc8..e7b23e2dc26c0 100644
--- a/libcxx/test/std/algorithms/alg.sorting/alg.set.operations/includes/ranges_includes.pass.cpp
+++ b/libcxx/test/std/algorithms/alg.sorting/alg.set.operations/includes/ranges_includes.pass.cpp
@@ -30,16 +30,318 @@
 #include <concepts>
 #include <functional>
 #include <ranges>
+#include <utility>
 
 #include "almost_satisfies_types.h"
 #include "test_iterators.h"
 
-// TODO: SFINAE tests.
+template <
+    class Iter1 = int*,
+    class Sent1 = int*,
+    class Iter2 = int*,
+    class Sent2 = int*,
+    class Comp  = std::ranges::less,
+    class Proj1 = std::identity,
+    class Proj2 = std::identity>
+concept HasIncludesIter =
+    requires(Iter1&& iter1, Sent1&& sent1, Iter2&& iter2, Sent2&& sent2, Comp&& comp, Proj1&& proj1, Proj2&& proj2) {
+      std::ranges::includes(
+          std::forward<Iter1>(iter1),
+          std::forward<Sent1>(sent1),
+          std::forward<Iter2>(iter2),
+          std::forward<Sent2>(sent2),
+          std::forward<Comp>(comp),
+          std::forward<Proj1>(proj1),
+          std::forward<Proj2>(proj2));
+    };
+
+static_assert(HasIncludesIter<int*, int*, int*, int*>);
+
+// !std::input_iterator<I1>
+static_assert(!HasIncludesIter<InputIteratorNotDerivedFrom>);
+
+// !std::sentinel_for<S1, I1>
+static_assert(!HasIncludesIter<int*, SentinelForNotSemiregular>);
+
+// !std::input_iterator<I2>
+static_assert(!HasIncludesIter<int*, int*, InputIteratorNotDerivedFrom>);
+
+// !std::sentinel_for<S2, I2>
+static_assert(!HasIncludesIter<int*, int*, int*, SentinelForNotSemiregular>);
+
+// !indirect_strict_weak_order<Comp, projected<I1, Proj1>, projected<I2, Proj2>>
+struct NotAComparator {};
+static_assert(!HasIncludesIter<int*, int*, int*, int*, NotAComparator>);
+
+template <
+    class Range1,
+    class Range2,
+    class Comp  = std::ranges::less,
+    class Proj1 = std::identity,
+    class Proj2 = std::identity>
+concept HasIncludesRange =
+    requires(Range1&& range1, Range2&& range2, Comp&& comp, Proj1&& proj1, Proj2&& proj2) {
+      std::ranges::includes(
+          std::forward<Range1>(range1),
+          std::forward<Range2>(range2),
+          std::forward<Comp>(comp),
+          std::forward<Proj1>(proj1),
+          std::forward<Proj2>(proj2));
+    };
+
+template <class T>
+using R = UncheckedRange<T>;
+
+static_assert(HasIncludesRange<R<int*>, R<int*>>);
+
+// !std::input_range<R2>
+static_assert(!HasIncludesRange<R<InputIteratorNotDerivedFrom>, R<int*>>);
+
+// !std::input_range<R2>
+static_assert(!HasIncludesRange<R<int*>, R<InputIteratorNotDerivedFrom>>);
+
+// !indirect_strict_weak_order<Comp, projected<iterator_t<R1>, Proj1>,
+//                                   projected<iterator_t<R2>, Proj2>>
+static_assert(!HasIncludesRange<R<int*>, R<int*>, NotAComparator>);
+
+template <class In1, class In2, template <class> class SentWrapper, std::size_t N1, std::size_t N2>
+constexpr void testIncludesImpl(std::array<int, N1> in1, std::array<int, N2> in2, bool expected) {
+  using Sent1 = SentWrapper<In1>;
+  using Sent2 = SentWrapper<In2>;
+
+  // iterator overload
+  {
+    std::same_as<bool> decltype(auto) result = std::ranges::includes(
+        In1{in1.data()}, Sent1{In1{in1.data() + in1.size()}}, In2{in2.data()}, Sent2{In2{in2.data() + in2.size()}});
+    assert(result == expected);
+  }
+
+  // range overload
+  {
+    std::ranges::subrange r1{In1{in1.data()}, Sent1{In1{in1.data() + in1.size()}}};
+    std::ranges::subrange r2{In2{in2.data()}, Sent2{In2{in2.data() + in2.size()}}};
+    std::same_as<bool> decltype(auto) result = std::ranges::includes(r1, r2);
+    assert(result == expected);
+  }
+}
+
+template <class In1, class In2, template <class> class SentWrapper>
+constexpr void testImpl() {
+  // range 1 shorter than range2
+  {
+    std::array in1{0, 1, 5, 6, 9, 10};
+    std::array in2{3, 6, 7, 9, 13, 15, 100};
+    bool expected = false;
+    testIncludesImpl<In1, In2, SentWrapper>(in1, in2, expected);
+  }
+  // range 2 shorter than range 1 but not subsequence
+  {
+    std::array in1{2, 6, 8, 12, 15, 16};
+    std::array in2{0, 2, 8};
+    bool expected = false;
+    testIncludesImpl<In1, In2, SentWrapper>(in1, in2, expected);
+  }
+
+  // range 1 and range 2 has the same length but 
diff erent elements
+  {
+    std::array in1{2, 6, 8, 12, 15, 16};
+    std::array in2{0, 2, 8, 15, 17, 19};
+    bool expected = false;
+    testIncludesImpl<In1, In2, SentWrapper>(in1, in2, expected);
+  }
+
+  // range 1 == range 2
+  {
+    std::array in1{0, 1, 2};
+    std::array in2{0, 1, 2};
+    bool expected = true;
+    testIncludesImpl<In1, In2, SentWrapper>(in1, in2, expected);
+  }
+
+  // range 2 is subsequence of range 1
+  {
+    std::array in1{8, 9, 10, 12, 13};
+    std::array in2{8, 10};
+    bool expected = true;
+    testIncludesImpl<In1, In2, SentWrapper>(in1, in2, expected);
+  }
+
+  // range 1 is subsequence of range 2
+  {
+    std::array in1{0, 1, 1};
+    std::array in2{0, 1, 1, 2, 5};
+    bool expected = false;
+    testIncludesImpl<In1, In2, SentWrapper>(in1, in2, expected);
+  }
+
+  // range 2 is subsequence of range 1 with duplicated elements
+  {
+    std::array in1{8, 9, 10, 12, 12, 12};
+    std::array in2{8, 12, 12};
+    bool expected = true;
+    testIncludesImpl<In1, In2, SentWrapper>(in1, in2, expected);
+  }
+
+  // range 2 is not a subsequence of range 1 because of duplicated elements
+  {
+    std::array in1{8, 9, 10, 12, 13};
+    std::array in2{8, 10, 10};
+    bool expected = false;
+    testIncludesImpl<In1, In2, SentWrapper>(in1, in2, expected);
+  }
+
+  // range 1 is empty
+  {
+    std::array<int, 0> in1{};
+    std::array in2{3, 4, 5};
+    bool expected = false;
+    testIncludesImpl<In1, In2, SentWrapper>(in1, in2, expected);
+  }
+
+  // range 2 is empty
+  {
+    std::array in1{3, 4, 5};
+    std::array<int, 0> in2{};
+    bool expected = true;
+    testIncludesImpl<In1, In2, SentWrapper>(in1, in2, expected);
+  }
+
+  // both ranges are empty
+  {
+    std::array<int, 0> in1{};
+    std::array<int, 0> in2{};
+    bool expected = true;
+    testIncludesImpl<In1, In2, SentWrapper>(in1, in2, expected);
+  }
+}
+
+template <class Iter2, template <class> class SentWrapper>
+constexpr void withAllPermutationsOfIter1() {
+  // C++17 InputIterator may or may not satisfy std::input_iterator
+  testImpl<cpp20_input_iterator<int*>, Iter2, sentinel_wrapper>();
+  testImpl<forward_iterator<int*>, Iter2, SentWrapper>();
+  testImpl<bidirectional_iterator<int*>, Iter2, SentWrapper>();
+  testImpl<random_access_iterator<int*>, Iter2, SentWrapper>();
+  testImpl<contiguous_iterator<int*>, Iter2, SentWrapper>();
+  testImpl<int*, Iter2, SentWrapper>();
+}
+
+template <template <class> class SentWrapper>
+constexpr void withAllPermutationsOfIter1AndIter2() {
+  withAllPermutationsOfIter1<cpp20_input_iterator<int*>, sentinel_wrapper>();
+  withAllPermutationsOfIter1<forward_iterator<int*>, SentWrapper>();
+  withAllPermutationsOfIter1<bidirectional_iterator<int*>, SentWrapper>();
+  withAllPermutationsOfIter1<random_access_iterator<int*>, SentWrapper>();
+  withAllPermutationsOfIter1<contiguous_iterator<int*>, SentWrapper>();
+  withAllPermutationsOfIter1<int*, SentWrapper>();
+}
 
 constexpr bool test() {
-  // TODO: main tests.
-  // TODO: A custom comparator works.
-  // TODO: A custom projection works.
+  withAllPermutationsOfIter1AndIter2<std::type_identity_t>();
+  withAllPermutationsOfIter1AndIter2<sentinel_wrapper>();
+
+  struct Data {
+    int data;
+  };
+
+  // Test custom comparator
+  {
+    std::array r1{Data{4}, Data{8}, Data{12}};
+    std::array r2{Data{4}, Data{12}};
+
+    const auto comp = [](const Data& x, const Data& y) { return x.data < y.data; };
+    bool expected   = true;
+
+    // iterator overload
+    {
+      auto result = std::ranges::includes(r1.begin(), r1.end(), r2.begin(), r2.end(), comp);
+      assert(result == expected);
+    }
+
+    // range overload
+    {
+      auto result = std::ranges::includes(r1, r2, comp);
+      assert(result == expected);
+    }
+  }
+
+  // Test custom projection
+  {
+    std::array r1{Data{4}, Data{8}, Data{12}};
+    std::array r2{Data{4}, Data{9}};
+
+    const auto proj = &Data::data;
+    bool expected   = false;
+
+    // iterator overload
+    {
+      auto result = std::ranges::includes(r1.begin(), r1.end(), r2.begin(), r2.end(), {}, proj, proj);
+      assert(result == expected);
+    }
+
+    // range overload
+    {
+      auto result = std::ranges::includes(r1, r2, {}, proj, proj);
+      assert(result == expected);
+    }
+  }
+
+  // Complexity: At most 2 * ((last1 - first1) + (last2 - first2)) - 1
+  // comparisons and applications of each projection.
+  {
+    struct CompProjs {
+      std::size_t numberOfComp  = 0;
+      std::size_t numberOfProj1 = 0;
+      std::size_t numberOfProj2 = 0;
+
+      constexpr auto comp() {
+        return [this](int x, int y) {
+          ++numberOfComp;
+          return x < y;
+        };
+      }
+
+      constexpr auto proj1() {
+        return [this](const Data& d) {
+          ++numberOfProj1;
+          return d.data;
+        };
+      }
+
+      constexpr auto proj2() {
+        return [this](const Data& d) {
+          ++numberOfProj2;
+          return d.data;
+        };
+      }
+    };
+    std::array<Data, 4> r1{{{0}, {1}, {2}, {3}}};
+    std::array<Data, 2> r2{{{4}, {5}}};
+    const std::size_t maxOperation = 2 * (r1.size() + r2.size()) - 1;
+
+    // iterator overload
+    {
+      CompProjs compProjs{};
+
+      auto result = std::ranges::includes(
+          r1.begin(), r1.end(), r2.begin(), r2.end(), compProjs.comp(), compProjs.proj1(), compProjs.proj2());
+      assert(!result);
+      assert(compProjs.numberOfComp < maxOperation);
+      assert(compProjs.numberOfProj1 < maxOperation);
+      assert(compProjs.numberOfProj2 < maxOperation);
+    }
+
+    // range overload
+    {
+      CompProjs compProjs{};
+
+      auto result = std::ranges::includes(r1, r2, compProjs.comp(), compProjs.proj1(), compProjs.proj2());
+      assert(!result);
+      assert(compProjs.numberOfComp < maxOperation);
+      assert(compProjs.numberOfProj1 < maxOperation);
+      assert(compProjs.numberOfProj2 < maxOperation);
+    }
+  }
 
   return true;
 }

diff  --git a/libcxx/test/std/algorithms/ranges_robust_against_nonbool_predicates.pass.cpp b/libcxx/test/std/algorithms/ranges_robust_against_nonbool_predicates.pass.cpp
index 18c3a02979474..7448f552364f2 100644
--- a/libcxx/test/std/algorithms/ranges_robust_against_nonbool_predicates.pass.cpp
+++ b/libcxx/test/std/algorithms/ranges_robust_against_nonbool_predicates.pass.cpp
@@ -106,7 +106,7 @@ constexpr bool test_all() {
   test(std::ranges::is_partitioned, in, unary_pred);
   test(std::ranges::is_sorted, in, binary_pred);
   test(std::ranges::is_sorted_until, in, binary_pred);
-  //test(std::ranges::includes, in, in2, binary_pred);
+  test(std::ranges::includes, in, in2, binary_pred);
   //test(std::ranges::is_heap, in, binary_pred);
   //test(std::ranges::is_heap_until, in, binary_pred);
   //std::ranges::clamp(2, 1, 3, binary_pred);

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 39cefeced4ab6..44a15208dd185 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
@@ -113,7 +113,7 @@ constexpr bool test_all() {
   test(std::ranges::is_partitioned, in, &Foo::unary_pred, &Bar::val);
   test(std::ranges::is_sorted, in, &Foo::binary_pred, &Bar::val);
   test(std::ranges::is_sorted_until, in, &Foo::binary_pred, &Bar::val);
-  //test(std::ranges::includes, in, in2, &Foo::binary_pred, &Bar::val, &Bar::val);
+  test(std::ranges::includes, in, in2, &Foo::binary_pred, &Bar::val, &Bar::val);
   //test(std::ranges::is_heap, in, &Foo::binary_pred, &Bar::val);
   //test(std::ranges::is_heap_until, in, &Foo::binary_pred, &Bar::val);
   //std::ranges::clamp(b, a, c, &Foo::binary_pred);

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 cfa54db5bc808..d13ff1fcc7283 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
@@ -99,7 +99,7 @@ constexpr void run_tests() {
   test(std::ranges::is_partitioned, in, unary_pred);
   test(std::ranges::is_sorted, in);
   test(std::ranges::is_sorted_until, in);
-  //test(std::ranges::includes, in, in2);
+  test(std::ranges::includes, in, in2);
   //test(std::ranges::is_heap, in);
   //test(std::ranges::is_heap_until, in);
   //test(std::ranges::is_permutation, in, in2);

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
index f71732db12ab9..8327090f1e793 100644
--- 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
@@ -86,7 +86,7 @@ static_assert(test(std::ranges::for_each, a, odd));
 static_assert(test(std::ranges::for_each_n, a, 10, odd));
 //static_assert(test(std::ranges::generate, a, gen));
 //static_assert(test(std::ranges::generate_n, a, 10, gen));
-//static_assert(test(std::ranges::includes, a, a));
+static_assert(test(std::ranges::includes, a, a));
 //static_assert(test(std::ranges::inplace_merge, a, a+5));
 //static_assert(test(std::ranges::is_heap, a));
 //static_assert(test(std::ranges::is_heap_until, a));


        


More information about the libcxx-commits mailing list