[libcxx-commits] [libcxx] [libc++] Refactor midpoint test and clean unused functions (PR #175388)

Hristo Hristov via libcxx-commits libcxx-commits at lists.llvm.org
Sat Jan 10 20:46:24 PST 2026


================
@@ -16,83 +16,76 @@
 
 // template <class _Tp>
 // _Tp* midpoint(_Tp* __a, _Tp* __b) noexcept
-//
+// Constraints:
+//  - T is a complete object type.
 
-#include <numeric>
 #include <cassert>
+#include <cstddef>
+#include <numeric>
+#include <type_traits>
+#include <utility>
 
 #include "test_macros.h"
 
-
+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;
+}
 
 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 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 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);
+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>());
 }
 
+using FuncPtr = void (*)();
+struct Incomplete;
+
 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
+concept has_midpoint = requires(T a, T b) { std::midpoint(a, b); };
+
+static void test_constraints() {
----------------
Zingam wrote:

I think we prefer to put tests on constraints on top and you don't need to call that function or even wrap the static asserts in a function if possible.

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


More information about the libcxx-commits mailing list