[libcxx-commits] [libcxx] f83d833 - [libc++][ranges] Implement ranges::min

Nikolas Klauser via libcxx-commits libcxx-commits at lists.llvm.org
Fri Mar 18 04:52:32 PDT 2022


Author: Nikolas Klauser
Date: 2022-03-18T12:52:21+01:00
New Revision: f83d833e41d70322a4174c5c37fb3726c60888f6

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

LOG: [libc++][ranges] Implement ranges::min

Reviewed By: var-const, Mordante, #libc

Spies: jwakely, ldionne, libcxx-commits, mgorny

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

Added: 
    libcxx/include/__algorithm/ranges_min.h
    libcxx/test/libcxx/diagnostics/detail.headers/algorithm/ranges_min.module.verify.cpp
    libcxx/test/std/algorithms/alg.sorting/alg.min.max/ranges.min.pass.cpp

Modified: 
    libcxx/docs/Status/RangesAlgorithms.csv
    libcxx/include/CMakeLists.txt
    libcxx/include/__algorithm/ranges_min_element.h
    libcxx/include/__ranges/take_view.h
    libcxx/include/algorithm
    libcxx/include/module.modulemap
    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 3eb07b96bcdd7..a380fc5169a93 100644
--- a/libcxx/docs/Status/RangesAlgorithms.csv
+++ b/libcxx/docs/Status/RangesAlgorithms.csv
@@ -15,11 +15,11 @@ Search,lower_bound,Christopher Di Bella,`D105795 <https://llvm.org/D105795>`_,Un
 Search,upper_bound,Christopher Di Bella,`D105795 <https://llvm.org/D105795>`_,Under review
 Search,equal_range,Christopher Di Bella,n/a,Not started
 Search,binary_search,Christopher Di Bella,n/a,Not started
-Search,min,Not assigned,n/a,Not started
+Search,min,Nikolas Klauser,`D119589 <https://llvm.org/D119589>`_,✅
 Search,max,Not assigned,n/a,Not started
 Search,minmax,Not assigned,n/a,Not started
-Search,min_element,Nikolas Klauser,n/a,✅
-Search,max_element,Not assigned,n/a,Not started
+Search,min_element,Nikolas Klauser,`D117025 <https://llvm.org/D117025>`_,✅
+Search,max_element,Nikolas Klauser,`D117523 <https://llvm.org/D117523>`_,✅
 Search,minmax_element,Not assigned,n/a,Not started
 Search,count,Not assigned,n/a,Not started
 Search,count_if,Not assigned,n/a,Not started

diff  --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt
index 9267a7fc01f0b..090adf0d47031 100644
--- a/libcxx/include/CMakeLists.txt
+++ b/libcxx/include/CMakeLists.txt
@@ -70,6 +70,7 @@ set(files
   __algorithm/ranges_find_if.h
   __algorithm/ranges_find_if_not.h
   __algorithm/ranges_max_element.h
+  __algorithm/ranges_min.h
   __algorithm/ranges_min_element.h
   __algorithm/ranges_mismatch.h
   __algorithm/ranges_swap_ranges.h

diff  --git a/libcxx/include/__algorithm/ranges_min.h b/libcxx/include/__algorithm/ranges_min.h
new file mode 100644
index 0000000000000..0bb1e72ac5ac1
--- /dev/null
+++ b/libcxx/include/__algorithm/ranges_min.h
@@ -0,0 +1,89 @@
+//===----------------------------------------------------------------------===//
+//
+// 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_MIN_H
+#define _LIBCPP___ALGORITHM_RANGES_MIN_H
+
+#include <__algorithm/ranges_min_element.h>
+#include <__assert>
+#include <__concepts/copyable.h>
+#include <__config>
+#include <__functional/identity.h>
+#include <__functional/invoke.h>
+#include <__functional/ranges_operations.h>
+#include <__iterator/concepts.h>
+#include <__iterator/projected.h>
+#include <__ranges/access.h>
+#include <__ranges/concepts.h>
+#include <initializer_list>
+
+#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_PUSH_MACROS
+#include <__undef_macros>
+
+_LIBCPP_BEGIN_NAMESPACE_STD
+
+namespace ranges {
+namespace __min {
+struct __fn {
+  template <class _Tp, class _Proj = identity,
+            indirect_strict_weak_order<projected<const _Tp*, _Proj>> _Comp = ranges::less>
+  _LIBCPP_HIDE_FROM_ABI constexpr
+  const _Tp& operator()(const _Tp& __a, const _Tp& __b, _Comp __comp = {}, _Proj __proj = {}) const {
+    return std::invoke(__comp, std::invoke(__proj, __b), std::invoke(__proj, __a)) ? __b : __a;
+  }
+
+  template <copyable _Tp, class _Proj = identity,
+            indirect_strict_weak_order<projected<const _Tp*, _Proj>> _Comp = ranges::less>
+  _LIBCPP_HIDE_FROM_ABI constexpr
+  _Tp operator()(initializer_list<_Tp> __il, _Comp __comp = {}, _Proj __proj = {}) const {
+    _LIBCPP_ASSERT(__il.begin() != __il.end(), "initializer_list must contain at least one element");
+    return *ranges::__min_element_impl(__il.begin(), __il.end(), __comp, __proj);
+  }
+
+  template <input_range _Rp, class _Proj = identity,
+            indirect_strict_weak_order<projected<iterator_t<_Rp>, _Proj>> _Comp = ranges::less>
+    requires indirectly_copyable_storable<iterator_t<_Rp>, range_value_t<_Rp>*>
+  _LIBCPP_HIDE_FROM_ABI constexpr
+  range_value_t<_Rp> operator()(_Rp&& __r, _Comp __comp = {}, _Proj __proj = {}) const {
+    auto __first = ranges::begin(__r);
+    auto __last = ranges::end(__r);
+
+    _LIBCPP_ASSERT(__first != __last, "range must contain at least one element");
+
+    if constexpr (forward_range<_Rp>) {
+      return *ranges::__min_element_impl(__first, __last, __comp, __proj);
+    } else {
+      range_value_t<_Rp> __result = *__first;
+      while (++__first != __last) {
+        if (std::invoke(__comp, std::invoke(__proj, *__first), std::invoke(__proj, __result)))
+          __result = *__first;
+      }
+      return __result;
+    }
+  }
+};
+} // namespace __min
+
+inline namespace __cpo {
+  inline constexpr auto min = __min::__fn{};
+} // namespace __cpo
+} // namespace ranges
+
+_LIBCPP_END_NAMESPACE_STD
+
+_LIBCPP_POP_MACROS
+
+#endif // _LIBCPP_STD_VER > 17 && && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES)
+
+#endif // _LIBCPP___ALGORITHM_RANGES_MIN_H

diff  --git a/libcxx/include/__algorithm/ranges_min_element.h b/libcxx/include/__algorithm/ranges_min_element.h
index 70497f484e125..ae82dceb9ad8c 100644
--- a/libcxx/include/__algorithm/ranges_min_element.h
+++ b/libcxx/include/__algorithm/ranges_min_element.h
@@ -29,33 +29,34 @@
 _LIBCPP_BEGIN_NAMESPACE_STD
 
 namespace ranges {
-namespace __min_element {
-struct __fn {
-  template <class _Ip, class _Sp, class _Proj, class _Comp>
-  _LIBCPP_HIDE_FROM_ABI static constexpr
-  _Ip __go(_Ip __first, _Sp __last, _Comp& __comp, _Proj& __proj) {
-    if (__first == __last)
-      return __first;
 
-    _Ip __i = __first;
-    while (++__i != __last)
-      if (std::invoke(__comp, std::invoke(__proj, *__i), std::invoke(__proj, *__first)))
-        __first = __i;
+template <class _Ip, class _Sp, class _Proj, class _Comp>
+_LIBCPP_HIDE_FROM_ABI static constexpr
+_Ip __min_element_impl(_Ip __first, _Sp __last, _Comp& __comp, _Proj& __proj) {
+  if (__first == __last)
     return __first;
-  }
 
+  _Ip __i = __first;
+  while (++__i != __last)
+    if (std::invoke(__comp, std::invoke(__proj, *__i), std::invoke(__proj, *__first)))
+      __first = __i;
+  return __first;
+}
+
+namespace __min_element {
+struct __fn {
   template <forward_iterator _Ip, sentinel_for<_Ip> _Sp, class _Proj = identity,
             indirect_strict_weak_order<projected<_Ip, _Proj>> _Comp = ranges::less>
   _LIBCPP_HIDE_FROM_ABI constexpr
   _Ip operator()(_Ip __first, _Sp __last, _Comp __comp = {}, _Proj __proj = {}) const {
-    return __go(__first, __last, __comp, __proj);
+    return ranges::__min_element_impl(__first, __last, __comp, __proj);
   }
 
   template <forward_range _Rp, class _Proj = identity,
             indirect_strict_weak_order<projected<iterator_t<_Rp>, _Proj>> _Comp = ranges::less>
   _LIBCPP_HIDE_FROM_ABI constexpr
   borrowed_iterator_t<_Rp> operator()(_Rp&& __r, _Comp __comp = {}, _Proj __proj = {}) const {
-    return __go(ranges::begin(__r), ranges::end(__r), __comp, __proj);
+    return ranges::__min_element_impl(ranges::begin(__r), ranges::end(__r), __comp, __proj);
   }
 };
 } // namespace __min_element

diff  --git a/libcxx/include/__ranges/take_view.h b/libcxx/include/__ranges/take_view.h
index 7193206d4cffe..9602ee1b2a0b9 100644
--- a/libcxx/include/__ranges/take_view.h
+++ b/libcxx/include/__ranges/take_view.h
@@ -9,7 +9,7 @@
 #ifndef _LIBCPP___RANGES_TAKE_VIEW_H
 #define _LIBCPP___RANGES_TAKE_VIEW_H
 
-#include <__algorithm/min.h>
+#include <__algorithm/ranges_min.h>
 #include <__config>
 #include <__iterator/concepts.h>
 #include <__iterator/counted_iterator.h>
@@ -118,15 +118,13 @@ namespace ranges {
     _LIBCPP_HIDE_FROM_ABI
     constexpr auto size() requires sized_range<_View> {
       auto __n = ranges::size(__base_);
-      // TODO: use ranges::min here.
-      return std::min(__n, static_cast<decltype(__n)>(__count_));
+      return ranges::min(__n, static_cast<decltype(__n)>(__count_));
     }
 
     _LIBCPP_HIDE_FROM_ABI
     constexpr auto size() const requires sized_range<const _View> {
       auto __n = ranges::size(__base_);
-      // TODO: use ranges::min here.
-      return std::min(__n, static_cast<decltype(__n)>(__count_));
+      return ranges::min(__n, static_cast<decltype(__n)>(__count_));
     }
   };
 

diff  --git a/libcxx/include/algorithm b/libcxx/include/algorithm
index 8383b40f20ef9..51a1781450fe2 100644
--- a/libcxx/include/algorithm
+++ b/libcxx/include/algorithm
@@ -57,7 +57,6 @@ namespace ranges {
   constexpr mismatch_result<borrowed_iterator_t<R1>, borrowed_iterator_t<R2>>
   mismatch(R1&& r1, R2&& r2, Pred pred = {}, Proj1 proj1 = {}, Proj2 proj2 = {})                           // since 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 I find(I first, S last, const T& value, Proj proj = {});              // since C++20
 
@@ -83,9 +82,22 @@ namespace ranges {
            indirect_unary_predicate<projected<iterator_t<R>, Proj>> Pred>
     constexpr borrowed_iterator_t<R>
       find_if_not(R&& r, Pred pred, Proj proj = {});                                // since C++20
+
+ template<class T, class Proj = identity,
+          indirect_strict_weak_order<projected<const T*, Proj>> Comp = ranges::less>
+   constexpr const T& min(const T& a, const T& b, Comp comp = {}, Proj proj = {});  // since C++20
+
+ template<copyable T, class Proj = identity,
+          indirect_strict_weak_order<projected<const T*, Proj>> Comp = ranges::less>
+   constexpr T min(initializer_list<T> r, Comp comp = {}, Proj proj = {});          // since C++20
+
+ template<input_range R, class Proj = identity,
+          indirect_strict_weak_order<projected<iterator_t<R>, Proj>> Comp = ranges::less>
+   requires indirectly_copyable_storable<iterator_t<R>, range_value_t<R>*>
+   constexpr range_value_t<R>
+     min(R&& r, Comp comp = {}, Proj proj = {});                                    // since C++20
 }
 
-template <class InputIterator, class Predicate>
     constexpr bool     // constexpr in C++20
     all_of(InputIterator first, InputIterator last, Predicate pred);
 
@@ -804,6 +816,7 @@ template <class BidirectionalIterator, class Compare>
 #include <__algorithm/ranges_find_if.h>
 #include <__algorithm/ranges_find_if_not.h>
 #include <__algorithm/ranges_max_element.h>
+#include <__algorithm/ranges_min.h>
 #include <__algorithm/ranges_min_element.h>
 #include <__algorithm/ranges_mismatch.h>
 #include <__algorithm/ranges_swap_ranges.h>

diff  --git a/libcxx/include/module.modulemap b/libcxx/include/module.modulemap
index 1ee895dd1567c..faf700de533b3 100644
--- a/libcxx/include/module.modulemap
+++ b/libcxx/include/module.modulemap
@@ -298,6 +298,7 @@ module std [system] {
       module ranges_find_if           { private header "__algorithm/ranges_find_if.h" }
       module ranges_find_if_not       { private header "__algorithm/ranges_find_if_not.h" }
       module ranges_max_element       { private header "__algorithm/ranges_max_element.h" }
+      module ranges_min               { private header "__algorithm/ranges_min.h" }
       module ranges_min_element       { private header "__algorithm/ranges_min_element.h" }
       module ranges_mismatch          { private header "__algorithm/ranges_mismatch.h" }
       module ranges_swap_ranges       { private header "__algorithm/ranges_swap_ranges.h" }

diff  --git a/libcxx/test/libcxx/diagnostics/detail.headers/algorithm/ranges_min.module.verify.cpp b/libcxx/test/libcxx/diagnostics/detail.headers/algorithm/ranges_min.module.verify.cpp
new file mode 100644
index 0000000000000..dedab3a728e1f
--- /dev/null
+++ b/libcxx/test/libcxx/diagnostics/detail.headers/algorithm/ranges_min.module.verify.cpp
@@ -0,0 +1,15 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// REQUIRES: modules-build
+
+// WARNING: This test was generated by 'generate_private_header_tests.py'
+// and should not be edited manually.
+
+// expected-error@*:* {{use of private header from outside its module: '__algorithm/ranges_min.h'}}
+#include <__algorithm/ranges_min.h>

diff  --git a/libcxx/test/std/algorithms/alg.sorting/alg.min.max/ranges.min.pass.cpp b/libcxx/test/std/algorithms/alg.sorting/alg.min.max/ranges.min.pass.cpp
new file mode 100644
index 0000000000000..85feb34a30597
--- /dev/null
+++ b/libcxx/test/std/algorithms/alg.sorting/alg.min.max/ranges.min.pass.cpp
@@ -0,0 +1,259 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+// UNSUPPORTED: libcpp-no-concepts
+// UNSUPPORTED: libcpp-has-no-incomplete-ranges
+
+// template<class T, class Proj = identity,
+//          indirect_strict_weak_order<projected<const T*, Proj>> Comp = ranges::less>
+//   constexpr const T& ranges::min(const T& a, const T& b, Comp comp = {}, Proj proj = {});
+// template<copyable T, class Proj = identity,
+//          indirect_strict_weak_order<projected<const T*, Proj>> Comp = ranges::less>
+//   constexpr T ranges::min(initializer_list<T> r, Comp comp = {}, Proj proj = {});
+// template<input_range R, class Proj = identity,
+//          indirect_strict_weak_order<projected<iterator_t<R>, Proj>> Comp = ranges::less>
+//   requires indirectly_copyable_storable<iterator_t<R>, range_value_t<R>*>
+//   constexpr range_value_t<R>
+//     ranges::min(R&& r, Comp comp = {}, Proj proj = {});
+
+#include <algorithm>
+#include <array>
+#include <cassert>
+#include <ranges>
+
+#include "almost_satisfies_types.h"
+#include "test_iterators.h"
+#include "test_macros.h"
+
+template <class T>
+concept HasMinR = requires { std::ranges::min(std::declval<T>()); };
+
+struct NoLessThanOp {};
+struct NotTotallyOrdered {
+  int i;
+  bool operator<(const NotTotallyOrdered& o) const { return i < o.i; }
+};
+
+struct Movable {
+  Movable& operator=(Movable&&) = default;
+  Movable(Movable&&) = default;
+  Movable(const Movable&) = delete;
+};
+
+static_assert(!HasMinR<int>);
+
+static_assert(HasMinR<int(&)[10]>);
+static_assert(HasMinR<int(&&)[10]>);
+static_assert(!HasMinR<NoLessThanOp(&)[10]>);
+static_assert(!HasMinR<NotTotallyOrdered(&)[10]>);
+static_assert(!HasMinR<Movable(&)[10]>);
+
+static_assert(HasMinR<std::initializer_list<int>>);
+static_assert(!HasMinR<std::initializer_list<NoLessThanOp>>);
+static_assert(!HasMinR<std::initializer_list<NotTotallyOrdered>>);
+static_assert(!HasMinR<std::initializer_list<Movable>>);
+static_assert(!HasMinR<InputRangeNotDerivedFrom>);
+static_assert(!HasMinR<InputRangeNotIndirectlyReadable>);
+static_assert(!HasMinR<InputRangeNotInputOrOutputIterator>);
+static_assert(!HasMinR<InputRangeNotSentinelSemiregular>);
+static_assert(!HasMinR<InputRangeNotSentinelEqualityComparableWith>);
+
+template <class T, class U = T>
+concept HasMin2 = requires { std::ranges::min(std::declval<T>(), std::declval<U>()); };
+
+static_assert(HasMin2<int>);
+static_assert(!HasMin2<int, long>);
+
+static_assert(std::is_same_v<decltype(std::ranges::min(1, 2)), const int&>);
+
+constexpr void test_2_arguments() {
+  assert(std::ranges::min(1, 2) == 1);
+  assert(std::ranges::min(2, 1) == 1);
+  // test comparator
+  assert(std::ranges::min(1, 2, std::ranges::greater{}) == 2);
+  // test projection
+  assert(std::ranges::min(1, 2, std::ranges::less{}, [](int i){ return i == 1 ? 10 : i; }) == 2);
+
+  { // check that std::invoke is used
+    struct S { int i; };
+    S a[3] = { S{2}, S{1}, S{3} };
+    decltype(auto) ret = std::ranges::min(a[0], a[1], {}, &S::i);
+    ASSERT_SAME_TYPE(decltype(ret), const S&);
+    assert(&ret == &a[1]);
+    assert(ret.i == 1);
+  }
+
+  { // check that pointers are compared and not a range
+    int i;
+    int* a[] = {&i, &i + 1};
+    auto ret = std::ranges::min(a[0], a[1]);
+    assert(ret == &i);
+  }
+
+  { // test predicate and projection count
+    int compares = 0;
+    int projections = 0;
+    auto comparator = [&](int x, int y) {
+      ++compares;
+      return x < y;
+    };
+    auto projection = [&](int x) {
+      ++projections;
+      return x;
+    };
+    auto ret = std::ranges::min(1, 2, comparator, projection);
+    assert(ret == 1);
+    assert(compares == 1);
+    assert(projections == 2);
+  }
+
+  { // check that the first argument is returned
+    struct S { int check; int other; };
+    auto ret = std::ranges::min(S {0, 1}, S {0, 2}, {}, &S::check);
+    assert(ret.other == 1);
+  }
+}
+
+constexpr void test_initializer_list() {
+  {
+    // test projection
+    auto proj = [](int i) { return i == 5 ? -100 : i; };
+    int ret = std::ranges::min({7, 6, 9, 3, 5, 1, 2, 4}, std::ranges::less{}, proj);
+    assert(ret == 5);
+  }
+
+  {
+    // test comparator
+    int ret = std::ranges::min({7, 6, 9, 3, 5, 1, 2, 4}, std::ranges::greater{});
+    assert(ret == 9);
+  }
+
+  {
+    int compares = 0;
+    int projections = 0;
+    auto comparator = [&](int a, int b) {
+      ++compares;
+      return a < b;
+    };
+    auto projection = [&](int a) {
+      ++projections;
+      return a;
+    };
+    std::same_as<int> decltype(auto) ret = std::ranges::min({1, 2, 3}, comparator, projection);
+    assert(ret == 1);
+    assert(compares == 2);
+    assert(projections == 4);
+  }
+
+  {
+    struct S { int i; };
+    decltype(auto) ret = std::ranges::min({ S{2}, S{1}, S{3} }, {}, &S::i);
+    ASSERT_SAME_TYPE(decltype(ret), S);
+    assert(ret.i == 1);
+  }
+
+  {
+    int a[] = {7, 6, 9, 3, 5, 1, 2, 4};
+    using It = cpp20_input_iterator<int*>;
+    using Sent = sentinel_wrapper<It>;
+    auto range = std::ranges::subrange(It(a), Sent(It(a + 8)));
+    auto ret = std::ranges::min(range);
+    assert(ret == 1);
+  }
+}
+
+template <class It, class Sent = It>
+constexpr void test_range_types() {
+  int a[] = {7, 6, 9, 3, 5, 1, 2, 4};
+  auto range = std::ranges::subrange(It(a), Sent(It(a + 8)));
+  int ret = std::ranges::min(range);
+  assert(ret == 1);
+}
+
+constexpr void test_range() {
+  { // check that all range types work
+    test_range_types<cpp20_input_iterator<int*>, sentinel_wrapper<cpp20_input_iterator<int*>>>();
+    test_range_types<forward_iterator<int*>>();
+    test_range_types<bidirectional_iterator<int*>>();
+    test_range_types<random_access_iterator<int*>>();
+    test_range_types<contiguous_iterator<int*>>();
+  }
+
+  int a[] = {7, 6, 9, 3, 5, 1, 2, 4};
+  {
+    // test projection
+    auto proj = [](int& i) { return i == 5 ? -100 : i; };
+    int ret = std::ranges::min(a, std::ranges::less{}, proj);
+    assert(ret == 5);
+  }
+
+  {
+    // test comparator
+    int ret = std::ranges::min(a, std::ranges::greater{});
+    assert(ret == 9);
+  }
+
+  { // check that predicate and projection call counts are correct
+    int compares = 0;
+    int projections = 0;
+    auto comparator = [&](int x, int y) {
+      ++compares;
+      return x < y;
+    };
+    auto projection = [&](int x) {
+      ++projections;
+      return x;
+    };
+    std::same_as<int> decltype(auto) ret = std::ranges::min(std::array{1, 2, 3}, comparator, projection);
+    assert(ret == 1);
+    assert(compares == 2);
+    assert(projections == 4);
+  }
+
+  { // check that std::invoke is used
+    struct S { int i; };
+    S b[3] = { S{2}, S{1}, S{3} };
+    std::same_as<S> decltype(auto) ret = std::ranges::min(b, {}, &S::i);
+    assert(ret.i == 1);
+  }
+
+  { // check that the first smallest element is returned
+    { // where the first element is the smallest
+      struct S { int check; int other; };
+      S b[] = { S{0, 1}, S{1, 2}, S{0, 3} };
+      auto ret = std::ranges::min(b, {}, &S::check);
+      assert(ret.check == 0);
+      assert(ret.other == 1);
+    }
+    { // where the first element isn't the smallest
+      struct S { int check; int other; };
+      S b[] = { S{2, 1}, S{0, 2}, S{0, 3} };
+      auto ret = std::ranges::min(b, {}, &S::check);
+      assert(ret.check == 0);
+      assert(ret.other == 2);
+    }
+  }
+}
+
+constexpr bool test() {
+  test_2_arguments();
+  test_initializer_list();
+  test_range();
+
+  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
index c909f4593c313..71b43b272c23c 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
@@ -99,7 +99,7 @@ static_assert(test(std::ranges::find_if_not, a, odd));
 //static_assert(test(std::ranges::max, a));
 static_assert(test(std::ranges::max_element, a));
 //static_assert(test(std::ranges::merge, a, a, a));
-//static_assert(test(std::ranges::min, a));
+static_assert(test(std::ranges::min, a));
 static_assert(test(std::ranges::min_element, a));
 //static_assert(test(std::ranges::minmax, a));
 //static_assert(test(std::ranges::minmax_element, a));


        


More information about the libcxx-commits mailing list