[libcxx-commits] [libcxx] [libc++] Refactor std::midpoint tests and add constraint checks (PR #175388)

via libcxx-commits libcxx-commits at lists.llvm.org
Tue Feb 24 04:25:47 PST 2026


https://github.com/eiytoq updated https://github.com/llvm/llvm-project/pull/175388

>From 16872e58cea6b69f4eae92141776a475fa709939 Mon Sep 17 00:00:00 2001
From: eiytoq <eiytoq at outlook.com>
Date: Mon, 12 Jan 2026 18:22:13 +0800
Subject: [PATCH 1/2] Refactor midpoint tests

---
 .../midpoint.float.pass.cpp                   | 221 ++++++++++--------
 .../midpoint.integer.pass.cpp                 | 175 ++++++--------
 .../midpoint.pointer.pass.cpp                 |  86 +++----
 3 files changed, 232 insertions(+), 250 deletions(-)

diff --git a/libcxx/test/std/numerics/numeric.ops/numeric.ops.midpoint/midpoint.float.pass.cpp b/libcxx/test/std/numerics/numeric.ops/numeric.ops.midpoint/midpoint.float.pass.cpp
index c9fa38694a32e..1332d714eb8b6 100644
--- a/libcxx/test/std/numerics/numeric.ops/numeric.ops.midpoint/midpoint.float.pass.cpp
+++ b/libcxx/test/std/numerics/numeric.ops/numeric.ops.midpoint/midpoint.float.pass.cpp
@@ -14,125 +14,152 @@
 // _Fp midpoint(_Fp __a, _Fp __b) noexcept
 
 #include <cassert>
+#include <cmath>
+#include <concepts>
 #include <limits>
 #include <numeric>
 
 #include "test_macros.h"
 #include "fp_compare.h"
 
-//  Totally arbitrary picks for precision
 template <typename T>
-constexpr T fp_error_pct();
-
-template <>
-constexpr float fp_error_pct<float>() {
-  return 1.0e-4f;
+constexpr bool is_nan(T x) {
+  return x != x;
 }
 
-template <>
-constexpr double fp_error_pct<double>() {
-  return 1.0e-12;
+template <typename T>
+constexpr T get_error_pct() {
+  if constexpr (std::same_as<T, float>)
+    return 1.0e-4f;
+  else if constexpr (std::same_as<T, double>)
+    return 1.0e-12;
+  else
+    return 1.0e-13l;
 }
 
-template <>
-constexpr long double fp_error_pct<long double>() {
-  return 1.0e-13l;
+template <typename T>
+constexpr bool check_near(T a, T b, T expect) {
+  if (std::is_constant_evaluated())
+    return true;
+  return fptest_close_pct(std::midpoint(a, b), expect, get_error_pct<T>());
 }
 
 template <typename T>
-void fp_test() {
-  ASSERT_SAME_TYPE(T, decltype(std::midpoint(T(), T())));
-  ASSERT_NOEXCEPT(std::midpoint(T(), T()));
-
-  constexpr T maxV = std::numeric_limits<T>::max();
-  constexpr T minV = std::numeric_limits<T>::min();
-
-  //  Things that can be compared exactly
-  static_assert((std::midpoint(T(0), T(0)) == T(0)), "");
-  static_assert((std::midpoint(T(2), T(4)) == T(3)), "");
-  static_assert((std::midpoint(T(4), T(2)) == T(3)), "");
-  static_assert((std::midpoint(T(3), T(4)) == T(3.5)), "");
-  static_assert((std::midpoint(T(0), T(0.4)) == T(0.2)), "");
-
-  //  Things that can't be compared exactly
-  constexpr T pct = fp_error_pct<T>();
-  assert((fptest_close_pct(std::midpoint(T(1.3), T(11.4)), T(6.35), pct)));
-  assert((fptest_close_pct(std::midpoint(T(11.33), T(31.45)), T(21.39), pct)));
-  assert((fptest_close_pct(std::midpoint(T(-1.3), T(11.4)), T(5.05), pct)));
-  assert((fptest_close_pct(std::midpoint(T(11.4), T(-1.3)), T(5.05), pct)));
-  assert((fptest_close_pct(std::midpoint(T(0.1), T(0.4)), T(0.25), pct)));
-
-  assert((fptest_close_pct(std::midpoint(T(11.2345), T(14.5432)), T(12.88885), pct)));
-
-  //  From e to pi
-  assert((fptest_close_pct(
-      std::midpoint(T(2.71828182845904523536028747135266249775724709369995),
-                    T(3.14159265358979323846264338327950288419716939937510)),
-      T(2.92993724102441923691146542731608269097720824653752),
-      pct)));
-
-  assert((fptest_close_pct(std::midpoint(maxV, T(0)), maxV / 2, pct)));
-  assert((fptest_close_pct(std::midpoint(T(0), maxV), maxV / 2, pct)));
-  assert((fptest_close_pct(std::midpoint(minV, T(0)), minV / 2, pct)));
-  assert((fptest_close_pct(std::midpoint(T(0), minV), minV / 2, pct)));
-  assert((fptest_close_pct(std::midpoint(maxV, maxV), maxV, pct)));
-  assert((fptest_close_pct(std::midpoint(minV, minV), minV, pct)));
-  assert((fptest_close_pct(std::midpoint(maxV, minV), maxV / 2, pct)));
-  assert((fptest_close_pct(std::midpoint(minV, maxV), maxV / 2, pct)));
-
-  //  Near the min and the max
-  assert((fptest_close_pct(std::midpoint(maxV * T(0.75), maxV * T(0.50)), maxV * T(0.625), pct)));
-  assert((fptest_close_pct(std::midpoint(maxV * T(0.50), maxV * T(0.75)), maxV * T(0.625), pct)));
-  assert((fptest_close_pct(std::midpoint(minV * T(2), minV * T(8)), minV * T(5), pct)));
-
-  //  Big numbers of different signs
-  assert((fptest_close_pct(std::midpoint(maxV * T(0.75), maxV * T(-0.5)), maxV * T(0.125), pct)));
-  assert((fptest_close_pct(std::midpoint(maxV * T(-0.75), maxV * T(0.5)), maxV * T(-0.125), pct)));
-
-  //  Denormalized values
-  //  TODO
-
-  //  Check two values "close to each other"
-  T d1 = T(3.14);
-  T d0 = std::nextafter(d1, T(2));
-  T d2 = std::nextafter(d1, T(5));
-  assert(d0 < d1); // sanity checking
-  assert(d1 < d2); // sanity checking
+constexpr bool check_exact(T a, T b, T expect) {
+  T res = std::midpoint(a, b);
+  if (is_nan(expect)) {
+    return is_nan(res);
+  } else {
+    return res == expect;
+  }
+}
 
+template <std::floating_point T>
+constexpr bool test_ppc_edge_cases() {
+  if (std::is_constant_evaluated())
+    return true;
+
+// For 128 bit long double implemented as 2 doubles on PowerPC,
+// nextafterl() of libm gives imprecise results which fails the
+// midpoint() tests below. So skip the test for this case.
 #if defined(__PPC__) && (defined(__LONG_DOUBLE_128__) && __LONG_DOUBLE_128__) &&                                       \
     !(defined(__LONG_DOUBLE_IEEE128__) && __LONG_DOUBLE_IEEE128__)
-  //  For 128 bit long double implemented as 2 doubles on PowerPC,
-  //  nextafterl() of libm gives imprecise results which fails the
-  //  midpoint() tests below. So skip the test for this case.
-  if constexpr (sizeof(T) != 16)
+  if constexpr (sizeof(T) == 16)
+    return true;
 #endif
-  {
-    //  Since there's nothing in between, the midpoint has to be one or the other
-    T res;
-    res = std::midpoint(d0, d1);
-    assert(res == d0 || res == d1);
-    assert(d0 <= res);
-    assert(res <= d1);
-    res = std::midpoint(d1, d0);
-    assert(res == d0 || res == d1);
-    assert(d0 <= res);
-    assert(res <= d1);
-
-    res = std::midpoint(d1, d2);
-    assert(res == d1 || res == d2);
-    assert(d1 <= res);
-    assert(res <= d2);
-    res = std::midpoint(d2, d1);
-    assert(res == d1 || res == d2);
-    assert(d1 <= res);
-    assert(res <= d2);
+
+  T d1 = 3.14;
+  T d0 = std::nextafter(d1, T{2});
+  T d2 = std::nextafter(d1, T{5});
+
+  auto verify = [](T res, T low, T high) { return (res == low || res == high) && (low <= res && res <= high); };
+
+  return verify(std::midpoint(d0, d1), d0, d1) && verify(std::midpoint(d1, d2), d1, d2);
+}
+
+template <typename T>
+constexpr bool test_floating_points() {
+  ASSERT_SAME_TYPE(T, decltype(std::midpoint(T{}, T{})));
+  ASSERT_NOEXCEPT(std::midpoint(T{}, T{}));
+
+  constexpr T max_v      = std::numeric_limits<T>::max();
+  constexpr T min_v      = std::numeric_limits<T>::min();
+  constexpr T denorm_min = std::numeric_limits<T>::denorm_min();
+  constexpr T inf        = std::numeric_limits<T>::infinity();
+  constexpr T qnan       = std::numeric_limits<T>::quiet_NaN();
+
+  // Things that can be compared exactly
+  assert(check_exact<T>(0, 0, 0));
+  assert(check_exact<T>(2, 4, 3));
+  assert(check_exact<T>(4, 2, 3));
+  assert(check_exact<T>(3, 4, 3.5));
+  assert(check_exact<T>(0, 0.4, 0.2));
+  assert(check_exact<T>(-2, -4, -3));
+  assert(check_exact<T>(-2, 2, 0));
+  assert(check_exact<T>(2, -2, 0));
+
+  // Infinity
+  assert(check_exact<T>(inf, inf, inf));
+  assert(check_exact<T>(-inf, -inf, -inf));
+  if (!std::is_constant_evaluated()) {
+    assert(check_exact<T>(inf, -inf, qnan));
+  }
+  assert(check_exact<T>(inf, 0, inf));
+
+  // NaN
+  if (!std::is_constant_evaluated()) {
+    assert(check_exact<T>(qnan, 0, qnan));
+    assert(check_exact<T>(qnan, qnan, qnan));
   }
+
+  // Subnormal
+  assert(check_exact<T>(denorm_min, 0, 0));
+  assert(check_exact<T>(denorm_min, denorm_min, denorm_min));
+
+  // Things that can't be compared exactly
+  assert(check_near<T>(1.3, 11.4, 6.35));
+  assert(check_near<T>(11.33, 31.45, 21.39));
+  assert(check_near<T>(-1.3, 11.4, 5.05));
+  assert(check_near<T>(11.4, -1.3, 5.05));
+  assert(check_near<T>(11.2345, 14.5432, 12.88885));
+  assert(check_near<T>(2.71828182845904523536028747135266249775724709369995,
+                       3.14159265358979323846264338327950288419716939937510,
+                       2.92993724102441923691146542731608269097720824653752));
+
+  assert(check_near<T>(max_v, 0, max_v / 2));
+  assert(check_near<T>(0, max_v, max_v / 2));
+  assert(check_near<T>(min_v, 0, min_v / 2));
+  assert(check_near<T>(0, min_v, min_v / 2));
+  assert(check_near<T>(max_v, max_v, max_v));
+  assert(check_near<T>(min_v, min_v, min_v));
+  assert(check_near<T>(max_v, min_v, max_v / 2));
+  assert(check_near<T>(min_v, max_v, max_v / 2));
+
+  // Near the min and the max
+  assert(check_near<T>(max_v * 0.75, max_v * 0.50, max_v * 0.625));
+  assert(check_near<T>(max_v * 0.50, max_v * 0.75, max_v * 0.625));
+  assert(check_near<T>(min_v * 2, min_v * 8, min_v * 5));
+
+  // Big numbers of different signs
+  assert(check_near<T>(max_v * 0.75, max_v * -0.50, max_v * 0.125));
+  assert(check_near<T>(max_v * -0.75, max_v * 0.50, max_v * -0.125));
+
+  assert(test_ppc_edge_cases<T>());
+
+  return true;
+}
+
+constexpr bool test() {
+  test_floating_points<float>();
+  test_floating_points<double>();
+  test_floating_points<long double>();
+
+  return true;
 }
 
 int main(int, char**) {
-  fp_test<float>();
-  fp_test<double>();
-  fp_test<long double>();
+  test();
+  static_assert(test());
 
   return 0;
 }
diff --git a/libcxx/test/std/numerics/numeric.ops/numeric.ops.midpoint/midpoint.integer.pass.cpp b/libcxx/test/std/numerics/numeric.ops/numeric.ops.midpoint/midpoint.integer.pass.cpp
index d58397bdfd67a..c5ad2792fb2e3 100644
--- a/libcxx/test/std/numerics/numeric.ops/numeric.ops.midpoint/midpoint.integer.pass.cpp
+++ b/libcxx/test/std/numerics/numeric.ops/numeric.ops.midpoint/midpoint.integer.pass.cpp
@@ -16,128 +16,103 @@
 //   - T is an arithmetic type other than bool.
 
 #include <cassert>
+#include <concepts>
 #include <cstddef>
-#include <cstdint>
 #include <limits>
 #include <numeric>
-#include <stdint.h>
 
 #include "test_macros.h"
+#include "type_algorithms.h"
 
 template <typename T>
-void signed_test() {
-  constexpr T zero{0};
-  constexpr T one{1};
-  constexpr T two{2};
-  constexpr T three{3};
-  constexpr T four{4};
-
-  ASSERT_SAME_TYPE(decltype(std::midpoint(T(), T())), T);
-  ASSERT_NOEXCEPT(std::midpoint(T(), T()));
+constexpr void test_signed() {
+  ASSERT_SAME_TYPE(decltype(std::midpoint(T{}, T{})), T);
+  ASSERT_NOEXCEPT(std::midpoint(T{}, T{}));
   using limits = std::numeric_limits<T>;
 
-  static_assert(std::midpoint(one, three) == two, "");
-  static_assert(std::midpoint(three, one) == two, "");
-
-  assert(std::midpoint(zero, zero) == zero);
-  assert(std::midpoint(zero, two) == one);
-  assert(std::midpoint(two, zero) == one);
-  assert(std::midpoint(two, two) == two);
-
-  assert(std::midpoint(one, four) == two);
-  assert(std::midpoint(four, one) == three);
-  assert(std::midpoint(three, four) == three);
-  assert(std::midpoint(four, three) == four);
-
-  assert(std::midpoint(T(3), T(4)) == T(3));
-  assert(std::midpoint(T(4), T(3)) == T(4));
-  assert(std::midpoint(T(-3), T(4)) == T(0));
-  assert(std::midpoint(T(-4), T(3)) == T(-1));
-  assert(std::midpoint(T(3), T(-4)) == T(0));
-  assert(std::midpoint(T(4), T(-3)) == T(1));
-  assert(std::midpoint(T(-3), T(-4)) == T(-3));
-  assert(std::midpoint(T(-4), T(-3)) == T(-4));
-
-  static_assert(std::midpoint(limits::min(), limits::max()) == T(-1), "");
-  static_assert(std::midpoint(limits::max(), limits::min()) == T(0), "");
-
-  static_assert(std::midpoint(limits::min(), T(6)) == limits::min() / 2 + 3, "");
-  assert(std::midpoint(T(6), limits::min()) == limits::min() / 2 + 3);
-  assert(std::midpoint(limits::max(), T(6)) == limits::max() / 2 + 4);
-  static_assert(std::midpoint(T(6), limits::max()) == limits::max() / 2 + 3, "");
-
-  assert(std::midpoint(limits::min(), T(-6)) == limits::min() / 2 - 3);
-  static_assert(std::midpoint(T(-6), limits::min()) == limits::min() / 2 - 3, "");
-  static_assert(std::midpoint(limits::max(), T(-6)) == limits::max() / 2 - 2, "");
-  assert(std::midpoint(T(-6), limits::max()) == limits::max() / 2 - 3);
+  assert(std::midpoint(T{1}, T{3}) == T{2});
+  assert(std::midpoint(T{3}, T{1}) == T{2});
+
+  assert(std::midpoint(T{0}, T{0}) == T{0});
+  assert(std::midpoint(T{0}, T{2}) == T{1});
+  assert(std::midpoint(T{2}, T{0}) == T{1});
+  assert(std::midpoint(T{2}, T{2}) == T{2});
+
+  assert(std::midpoint(T{1}, T{4}) == T{2});
+  assert(std::midpoint(T{4}, T{1}) == T{3});
+  assert(std::midpoint(T{3}, T{4}) == T{3});
+  assert(std::midpoint(T{4}, T{3}) == T{4});
+
+  assert(std::midpoint(T{-3}, T{4}) == T{0});
+  assert(std::midpoint(T{-4}, T{3}) == T{-1});
+  assert(std::midpoint(T{3}, T{-4}) == T{0});
+  assert(std::midpoint(T{4}, T{-3}) == T{1});
+  assert(std::midpoint(T{-3}, T{-4}) == T{-3});
+  assert(std::midpoint(T{-4}, T{-3}) == T{-4});
+
+  assert(std::midpoint(limits::min(), limits::max()) == T{-1});
+  assert(std::midpoint(limits::max(), limits::min()) == T{0});
+
+  assert(std::midpoint(limits::min(), T{6}) == T{limits::min() / 2 + 3});
+  assert(std::midpoint(T{6}, limits::min()) == T{limits::min() / 2 + 3});
+
+  assert(std::midpoint(limits::max(), T{6}) == T{limits::max() / 2 + 4});
+  assert(std::midpoint(T{6}, limits::max()) == T{limits::max() / 2 + 3});
+
+  assert(std::midpoint(limits::min(), T{-6}) == T{limits::min() / 2 - 3});
+  assert(std::midpoint(T{-6}, limits::min()) == T{limits::min() / 2 - 3});
+
+  assert(std::midpoint(limits::max(), T{-6}) == T{limits::max() / 2 - 2});
+  assert(std::midpoint(T{-6}, limits::max()) == T{limits::max() / 2 - 3});
 }
 
 template <typename T>
-void unsigned_test() {
-  constexpr T zero{0};
-  constexpr T one{1};
-  constexpr T two{2};
-  constexpr T three{3};
-  constexpr T four{4};
-
-  ASSERT_SAME_TYPE(decltype(std::midpoint(T(), T())), T);
-  ASSERT_NOEXCEPT(std::midpoint(T(), T()));
+constexpr void test_unsigned() {
+  ASSERT_SAME_TYPE(decltype(std::midpoint(T{}, T{})), T);
+  ASSERT_NOEXCEPT(std::midpoint(T{}, T{}));
+
   using limits     = std::numeric_limits<T>;
   const T half_way = (limits::max() - limits::min()) / 2;
 
-  static_assert(std::midpoint(one, three) == two, "");
-  static_assert(std::midpoint(three, one) == two, "");
+  assert(std::midpoint(T{1}, T{3}) == T{2});
+  assert(std::midpoint(T{3}, T{1}) == T{2});
 
-  assert(std::midpoint(zero, zero) == zero);
-  assert(std::midpoint(zero, two) == one);
-  assert(std::midpoint(two, zero) == one);
-  assert(std::midpoint(two, two) == two);
+  assert(std::midpoint(T{0}, T{0}) == T{0});
+  assert(std::midpoint(T{0}, T{2}) == T{1});
+  assert(std::midpoint(T{2}, T{0}) == T{1});
+  assert(std::midpoint(T{2}, T{2}) == T{2});
 
-  assert(std::midpoint(one, four) == two);
-  assert(std::midpoint(four, one) == three);
-  assert(std::midpoint(three, four) == three);
-  assert(std::midpoint(four, three) == four);
+  assert(std::midpoint(T{1}, T{4}) == T{2});
+  assert(std::midpoint(T{4}, T{1}) == T{3});
+  assert(std::midpoint(T{3}, T{4}) == T{3});
+  assert(std::midpoint(T{4}, T{3}) == T{4});
 
-  assert(std::midpoint(limits::min(), limits::max()) == T(half_way));
-  assert(std::midpoint(limits::max(), limits::min()) == T(half_way + 1));
+  assert(std::midpoint(limits::min(), limits::max()) == half_way);
+  assert(std::midpoint(limits::max(), limits::min()) == T{half_way + 1});
+
+  assert(std::midpoint(limits::min(), T{6}) == T{3});
+  assert(std::midpoint(T{6}, limits::min()) == T{3});
+
+  assert(std::midpoint(limits::max(), T{6}) == T{half_way + 4});
+  assert(std::midpoint(T{6}, limits::max()) == T{half_way + 3});
+}
 
-  static_assert(std::midpoint(limits::min(), T(6)) == limits::min() / 2 + 3, "");
-  assert(std::midpoint(T(6), limits::min()) == limits::min() / 2 + 3);
-  assert(std::midpoint(limits::max(), T(6)) == half_way + 4);
-  static_assert(std::midpoint(T(6), limits::max()) == half_way + 3, "");
+constexpr bool test() {
+  types::for_each(types::integer_types(), []<class T>() {
+    if constexpr (!std::same_as<T, bool>) {
+      if constexpr (std::signed_integral<T>) {
+        test_signed<T>();
+      } else {
+        test_unsigned<T>();
+      }
+    }
+  });
+  return true;
 }
 
 int main(int, char**) {
-  signed_test<signed char>();
-  signed_test<short>();
-  signed_test<int>();
-  signed_test<long>();
-  signed_test<long long>();
-
-  signed_test<std::int8_t>();
-  signed_test<std::int16_t>();
-  signed_test<std::int32_t>();
-  signed_test<std::int64_t>();
-
-  unsigned_test<unsigned char>();
-  unsigned_test<unsigned short>();
-  unsigned_test<unsigned int>();
-  unsigned_test<unsigned long>();
-  unsigned_test<unsigned long long>();
-
-  unsigned_test<std::uint8_t>();
-  unsigned_test<std::uint16_t>();
-  unsigned_test<std::uint32_t>();
-  unsigned_test<std::uint64_t>();
-
-#ifndef TEST_HAS_NO_INT128
-  unsigned_test<__uint128_t>();
-  signed_test<__int128_t>();
-#endif
-
-  //     int_test<char>();
-  signed_test<std::ptrdiff_t>();
-  unsigned_test<std::size_t>();
+  test();
+  static_assert(test());
 
   return 0;
 }
diff --git a/libcxx/test/std/numerics/numeric.ops/numeric.ops.midpoint/midpoint.pointer.pass.cpp b/libcxx/test/std/numerics/numeric.ops/numeric.ops.midpoint/midpoint.pointer.pass.cpp
index 36092d4af7aa8..a6639bd8529de 100644
--- a/libcxx/test/std/numerics/numeric.ops/numeric.ops.midpoint/midpoint.pointer.pass.cpp
+++ b/libcxx/test/std/numerics/numeric.ops/numeric.ops.midpoint/midpoint.pointer.pass.cpp
@@ -20,73 +20,53 @@
 //  - T is a complete object type.
 
 #include <cassert>
+#include <cstddef>
 #include <numeric>
+#include <type_traits>
+#include <utility>
 
 #include "test_macros.h"
 
 template <typename T>
-constexpr void constexpr_test() {
-  constexpr T array[1000] = {};
-  ASSERT_SAME_TYPE(decltype(std::midpoint(array, array)), const T*);
-  ASSERT_NOEXCEPT(std::midpoint(array, array));
-
-  static_assert(std::midpoint(array, array) == array, "");
-  static_assert(std::midpoint(array, array + 1000) == array + 500, "");
-
-  static_assert(std::midpoint(array, array + 9) == array + 4, "");
-  static_assert(std::midpoint(array, array + 10) == array + 5, "");
-  static_assert(std::midpoint(array, array + 11) == array + 5, "");
-  static_assert(std::midpoint(array + 9, array) == array + 5, "");
-  static_assert(std::midpoint(array + 10, array) == array + 5, "");
-  static_assert(std::midpoint(array + 11, array) == array + 6, "");
+constexpr bool check(T* base, std::ptrdiff_t i, std::ptrdiff_t j, std::ptrdiff_t expect) {
+  return std::midpoint(base + i, base + j) == base + expect;
 }
 
 template <typename T>
-void runtime_test() {
-  T array[1000] = {}; // we need an array to make valid pointers
-  ASSERT_SAME_TYPE(decltype(std::midpoint(array, array)), T*);
-  ASSERT_NOEXCEPT(std::midpoint(array, array));
-
-  assert(std::midpoint(array, array) == array);
-  assert(std::midpoint(array, array + 1000) == array + 500);
-
-  assert(std::midpoint(array, array + 9) == array + 4);
-  assert(std::midpoint(array, array + 10) == array + 5);
-  assert(std::midpoint(array, array + 11) == array + 5);
-  assert(std::midpoint(array + 9, array) == array + 5);
-  assert(std::midpoint(array + 10, array) == array + 5);
-  assert(std::midpoint(array + 11, array) == array + 6);
-
-  // explicit instantiation
-  ASSERT_SAME_TYPE(decltype(std::midpoint<T>(array, array)), T*);
-  ASSERT_NOEXCEPT(std::midpoint<T>(array, array));
-  assert(std::midpoint<T>(array, array) == array);
-  assert(std::midpoint<T>(array, array + 1000) == array + 500);
+constexpr bool test_pointer() {
+  ASSERT_SAME_TYPE(decltype(std::midpoint(std::declval<T*>(), std::declval<T*>())), T*);
+  ASSERT_NOEXCEPT(std::midpoint(std::declval<T*>(), std::declval<T*>()));
+
+  std::remove_cv_t<T> array[20] = {};
+  assert(check(array, 0, 0, 0));
+  assert(check(array, 1, 1, 1));
+  assert(check(array, 0, 9, 4));
+  assert(check(array, 0, 10, 5));
+  assert(check(array, 0, 11, 5));
+  assert(check(array, 9, 0, 5));
+  assert(check(array, 10, 0, 5));
+  assert(check(array, 11, 0, 6));
+  assert(check(array, 0, 18, 9));
+  assert(check(array, 2, 12, 7));
+
+  return true;
 }
 
 template <typename T>
-void pointer_test() {
-  runtime_test< T>();
-  runtime_test<const T>();
-  runtime_test< volatile T>();
-  runtime_test<const volatile T>();
-
-  //  The constexpr tests are always const, but we can test them anyway.
-  constexpr_test< T>();
-  constexpr_test<const T>();
-
-//  GCC 9.0.1 (unreleased as of 2019-03) barfs on this, but we have a bot for it.
-//  Uncomment when gcc 9.1 is released
-#ifndef TEST_COMPILER_GCC
-  constexpr_test< volatile T>();
-  constexpr_test<const volatile T>();
-#endif
+void test() {
+  assert(test_pointer<T>());
+  assert(test_pointer<const T>());
+  assert(test_pointer<volatile T>());
+  assert(test_pointer<const volatile T>());
+
+  static_assert(test_pointer<T>());
+  static_assert(test_pointer<const T>());
 }
 
 int main(int, char**) {
-  pointer_test<char>();
-  pointer_test<int>();
-  pointer_test<double>();
+  test<char>();
+  test<int>();
+  test<double>();
 
   return 0;
 }

>From 93171ebe829ac35fa2176262714708c914e8569a Mon Sep 17 00:00:00 2001
From: eiytoq <eiytoq at outlook.com>
Date: Mon, 12 Jan 2026 18:28:31 +0800
Subject: [PATCH 2/2] Add SFINAE-friendly tests

---
 .../midpoint.integer.pass.cpp                 |  8 ++++
 .../midpoint.pointer.pass.cpp                 | 15 +++++++
 .../numeric.ops.midpoint/midpoint.verify.cpp  | 44 -------------------
 3 files changed, 23 insertions(+), 44 deletions(-)
 delete mode 100644 libcxx/test/std/numerics/numeric.ops/numeric.ops.midpoint/midpoint.verify.cpp

diff --git a/libcxx/test/std/numerics/numeric.ops/numeric.ops.midpoint/midpoint.integer.pass.cpp b/libcxx/test/std/numerics/numeric.ops/numeric.ops.midpoint/midpoint.integer.pass.cpp
index c5ad2792fb2e3..9baadb1e47dbd 100644
--- a/libcxx/test/std/numerics/numeric.ops/numeric.ops.midpoint/midpoint.integer.pass.cpp
+++ b/libcxx/test/std/numerics/numeric.ops/numeric.ops.midpoint/midpoint.integer.pass.cpp
@@ -24,6 +24,14 @@
 #include "test_macros.h"
 #include "type_algorithms.h"
 
+template <typename T>
+concept has_midpoint = requires(T a, T b) { std::midpoint(a, b); };
+
+static_assert(!has_midpoint<bool>);
+static_assert(!has_midpoint<const bool>);
+static_assert(!has_midpoint<volatile bool>);
+static_assert(!has_midpoint<const volatile bool>);
+
 template <typename T>
 constexpr void test_signed() {
   ASSERT_SAME_TYPE(decltype(std::midpoint(T{}, T{})), T);
diff --git a/libcxx/test/std/numerics/numeric.ops/numeric.ops.midpoint/midpoint.pointer.pass.cpp b/libcxx/test/std/numerics/numeric.ops/numeric.ops.midpoint/midpoint.pointer.pass.cpp
index a6639bd8529de..ba7314390c9e2 100644
--- a/libcxx/test/std/numerics/numeric.ops/numeric.ops.midpoint/midpoint.pointer.pass.cpp
+++ b/libcxx/test/std/numerics/numeric.ops/numeric.ops.midpoint/midpoint.pointer.pass.cpp
@@ -27,6 +27,21 @@
 
 #include "test_macros.h"
 
+using FuncPtr = void (*)();
+struct Incomplete;
+
+template <typename T>
+concept has_midpoint = requires(T a, T b) { std::midpoint(a, b); };
+
+static_assert(!has_midpoint<std::nullptr_t>);
+static_assert(!has_midpoint<FuncPtr>);
+LIBCPP_STATIC_ASSERT(!has_midpoint<Incomplete*>);
+
+static_assert(!has_midpoint<void*>);
+static_assert(!has_midpoint<const void*>);
+static_assert(!has_midpoint<volatile void*>);
+static_assert(!has_midpoint<const volatile void*>);
+
 template <typename T>
 constexpr bool check(T* base, std::ptrdiff_t i, std::ptrdiff_t j, std::ptrdiff_t expect) {
   return std::midpoint(base + i, base + j) == base + expect;
diff --git a/libcxx/test/std/numerics/numeric.ops/numeric.ops.midpoint/midpoint.verify.cpp b/libcxx/test/std/numerics/numeric.ops/numeric.ops.midpoint/midpoint.verify.cpp
deleted file mode 100644
index 62a2a2db0231c..0000000000000
--- a/libcxx/test/std/numerics/numeric.ops/numeric.ops.midpoint/midpoint.verify.cpp
+++ /dev/null
@@ -1,44 +0,0 @@
-//===----------------------------------------------------------------------===//
-//
-// 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: std-at-least-c++20
-
-// <numeric>
-
-// template <class _Tp>
-// _Tp midpoint(_Tp __a, _Tp __b) noexcept
-
-// An overload exists for each of char and all arithmetic types except bool.
-
-#include <numeric>
-
-#include "test_macros.h"
-
-int func1() { return 1; }
-int func2() { return 2; }
-
-struct Incomplete;
-Incomplete* ip = nullptr;
-void* vp       = nullptr;
-
-void test() {
-  // expected-error at +1 {{no matching function for call to 'midpoint'}}
-  (void)std::midpoint(false, true);
-  // expected-error at +1 {{no matching function for call to 'midpoint'}}
-  (void)std::midpoint<const bool>(false, true);
-  // expected-error at +1 {{no matching function for call to 'midpoint'}}
-  (void)std::midpoint<const volatile bool>(false, true);
-  // expected-error at +1 {{no matching function for call to 'midpoint'}}
-  (void)std::midpoint<volatile bool>(false, true);
-
-  //  A couple of odd pointer types that should fail
-  (void)std::midpoint(nullptr, nullptr); // expected-error {{no matching function for call to 'midpoint'}}
-  (void)std::midpoint(func1, func2);     // expected-error {{no matching function for call to 'midpoint'}}
-  (void)std::midpoint(ip, ip);           // expected-error {{no matching function for call to 'midpoint'}}
-  (void)std::midpoint(vp, vp);           // expected-error {{no matching function for call to 'midpoint'}}
-}



More information about the libcxx-commits mailing list