[libcxx-commits] [libcxx] [libc++][math] Add `constexpr` to comparison functions (PR #210075)
A. Jiang via libcxx-commits
libcxx-commits at lists.llvm.org
Wed Jul 22 04:33:02 PDT 2026
================
@@ -0,0 +1,92 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// bool isgreater(floating-point-type x, floating-point-type y); // constexpr since C++23
+
+#include <cassert>
+#include <cmath>
+#include <limits>
+#include <type_traits>
+
+#include "test_macros.h"
+#include "type_algorithms.h"
+
+struct TestFloat {
+ template <class T>
+ static TEST_CONSTEXPR_CXX23 bool test() {
+ assert(std::isgreater(std::numeric_limits<T>::max(), T(0)));
+ assert(!std::isgreater(T(0), std::numeric_limits<T>::max()));
+ assert(!std::isgreater(std::numeric_limits<T>::max(), std::numeric_limits<T>::max()));
+
+ assert(std::isgreater(std::numeric_limits<T>::infinity(), std::numeric_limits<T>::max()));
+ assert(!std::isgreater(-std::numeric_limits<T>::infinity(), std::numeric_limits<T>::lowest()));
+ assert(!std::isgreater(std::numeric_limits<T>::infinity(), std::numeric_limits<T>::infinity()));
+
+ assert(!std::isgreater(std::numeric_limits<T>::quiet_NaN(), T(0)));
+ assert(!std::isgreater(T(0), std::numeric_limits<T>::quiet_NaN()));
+ assert(!std::isgreater(std::numeric_limits<T>::quiet_NaN(), std::numeric_limits<T>::quiet_NaN()));
+ assert(!std::isgreater(std::numeric_limits<T>::signaling_NaN(), T(0)));
+
+ return true;
+ }
+
+ template <class T>
+ TEST_CONSTEXPR_CXX23 void operator()() {
+ test<T>();
+#if TEST_STD_VER >= 23
+ static_assert(test<T>());
+#endif
+ }
+};
+
+struct TestInt {
+ template <class T>
+ static TEST_CONSTEXPR_CXX23 bool test() {
+ assert(std::isgreater(std::numeric_limits<T>::max(), T(0)));
+ assert(!std::isgreater(T(0), std::numeric_limits<T>::max()));
+ assert(!std::isgreater(std::numeric_limits<T>::max(), std::numeric_limits<T>::max()));
+
+ assert(!std::isgreater(T(1), T(1)));
+ assert(!std::isgreater(std::numeric_limits<T>::lowest(), T(0)));
+
+ if (std::is_signed<T>::value) {
+ assert(std::isgreater(T(-1), std::numeric_limits<T>::lowest()));
+ assert(!std::isgreater(std::numeric_limits<T>::lowest(), T(-1)));
+ }
+
+ return true;
+ }
+
+ template <class T>
+ TEST_CONSTEXPR_CXX23 void operator()() {
+ test<T>();
+#if TEST_STD_VER >= 23
+ static_assert(test<T>());
+#endif
+ }
----------------
frederick-vs-ja wrote:
> I copied the exact same assertion behaviour from `signbit.pass.cpp` which is part of the PR of `constexpr` for `std::signbit`.
I don't think it's a good idea to be consistent with an old, inconsistent test. Let's refactor these into:
```c++
TEST_CONSTEXPR_CXX23 bool test() {
// ...
return true;
}
int main(int, char**) {
test();
#if TEST_STD_VER >= 23
static_assert(test());
#endif
}
```
I think it would be better to open another PR to refactor the test for `signbit`.
https://github.com/llvm/llvm-project/pull/210075
More information about the libcxx-commits
mailing list