[libc-commits] [libc] [libc] Unit test for isnan[f,l] (PR #98274)

Petr Hosek via libc-commits libc-commits at lists.llvm.org
Sat Jul 13 21:23:06 PDT 2024


https://github.com/petrhosek updated https://github.com/llvm/llvm-project/pull/98274

>From c6cc61d3cbf253e7d21d794dcdefa16405a8156b Mon Sep 17 00:00:00 2001
From: Petr Hosek <phosek at google.com>
Date: Tue, 9 Jul 2024 23:09:26 -0700
Subject: [PATCH 1/2] [libc] Unit test for isnan[f,l]

This is a follow up to #98271.
---
 libc/test/src/math/CMakeLists.txt  | 39 ++++++++++++++++++++++
 libc/test/src/math/IsNanTest.h     | 53 ++++++++++++++++++++++++++++++
 libc/test/src/math/isnan_test.cpp  | 16 +++++++++
 libc/test/src/math/isnanf_test.cpp | 13 ++++++++
 libc/test/src/math/isnanl_test.cpp | 13 ++++++++
 5 files changed, 134 insertions(+)
 create mode 100644 libc/test/src/math/IsNanTest.h
 create mode 100644 libc/test/src/math/isnan_test.cpp
 create mode 100644 libc/test/src/math/isnanf_test.cpp
 create mode 100644 libc/test/src/math/isnanl_test.cpp

diff --git a/libc/test/src/math/CMakeLists.txt b/libc/test/src/math/CMakeLists.txt
index 0dc7ae6aae2df..2e97d46b870d0 100644
--- a/libc/test/src/math/CMakeLists.txt
+++ b/libc/test/src/math/CMakeLists.txt
@@ -2225,6 +2225,45 @@ add_fp_unittest(
     libc.src.__support.FPUtil.fp_bits
 )
 
+add_fp_unittest(
+  isnan_test
+  SUITE
+    libc-math-unittests
+  SRCS
+    isnan_test.cpp
+  HDRS
+    IsNanTest.h
+  DEPENDS
+    libc.src.math.isnan
+    libc.src.__support.FPUtil.fp_bits
+)
+
+add_fp_unittest(
+  isnanf_test
+  SUITE
+    libc-math-unittests
+  SRCS
+    isnanf_test.cpp
+  HDRS
+    IsNanTest.h
+  DEPENDS
+    libc.src.math.isnanf
+    libc.src.__support.FPUtil.fp_bits
+)
+
+add_fp_unittest(
+  isnanl_test
+  SUITE
+    libc-math-unittests
+  SRCS
+    isnanl_test.cpp
+  HDRS
+    IsNanTest.h
+  DEPENDS
+    libc.src.math.isnanl
+    libc.src.__support.FPUtil.fp_bits
+)
+
 add_subdirectory(generic)
 add_subdirectory(smoke)
 
diff --git a/libc/test/src/math/IsNanTest.h b/libc/test/src/math/IsNanTest.h
new file mode 100644
index 0000000000000..64513cef6e7d0
--- /dev/null
+++ b/libc/test/src/math/IsNanTest.h
@@ -0,0 +1,53 @@
+//===-- Utility class to test isnan[f|l] ------------------------*- C++ -*-===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "test/UnitTest/FEnvSafeTest.h"
+#include "test/UnitTest/FPMatcher.h"
+#include "test/UnitTest/Test.h"
+
+template <typename T>
+class IsNanTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
+
+  DECLARE_SPECIAL_CONSTANTS(T)
+
+  static constexpr T one = static_cast<T>(1.0);
+  static constexpr T neg_one = static_cast<T>(-1.0);
+
+public:
+  typedef int (*IsNanFunc)(T);
+
+  void testSpecialNumbers(IsNanFunc func) {
+    EXPECT_EQ(func(zero), 0);
+    EXPECT_EQ(func(one), 0);
+    EXPECT_EQ(func(inf), 0);
+    EXPECT_EQ(func(aNaN), 1);
+
+    EXPECT_EQ(func(neg_zero), 0);
+    EXPECT_EQ(func(neg_one), 0);
+    EXPECT_EQ(func(neg_inf), 0);
+    EXPECT_EQ(func(neg_aNaN), 1);
+  }
+
+  void testSpecialCases(IsNanFunc func) {
+    EXPECT_EQ(func(one / zero), 0);
+    EXPECT_EQ(func(one / inf), 0);
+    EXPECT_EQ(func(one / neg_inf), 0);
+    EXPECT_EQ(func(inf / neg_inf), 1);
+
+    EXPECT_EQ(func(inf * neg_inf), 0);
+    EXPECT_EQ(func(inf * zero), 1);
+    EXPECT_EQ(func(neg_inf * zero), 1);
+
+    EXPECT_EQ(func(inf + neg_inf), 1);
+  }
+};
+
+#define LIST_ISNAN_TESTS(T, func)                                              \
+  using LlvmLibcIsNanTest = IsNanTest<T>;                                      \
+  TEST_F(LlvmLibcIsNanTest, SpecialNumbers) { testSpecialNumbers(&func); }     \
+  TEST_F(LlvmLibcIsNanTest, SpecialCases) { testSpecialCases(&func); }
diff --git a/libc/test/src/math/isnan_test.cpp b/libc/test/src/math/isnan_test.cpp
new file mode 100644
index 0000000000000..ecee01b019554
--- /dev/null
+++ b/libc/test/src/math/isnan_test.cpp
@@ -0,0 +1,16 @@
+//===-- Unittests for isnan -----------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDSList-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "IsNanTest.h"
+
+// We need to avoid expanding isnan to __builtin_isnan.
+#undef isnan
+
+#include "src/math/isnan.h"
+
+LIST_ISNAN_TESTS(double, LIBC_NAMESPACE::isnan)
diff --git a/libc/test/src/math/isnanf_test.cpp b/libc/test/src/math/isnanf_test.cpp
new file mode 100644
index 0000000000000..633573a9e83b2
--- /dev/null
+++ b/libc/test/src/math/isnanf_test.cpp
@@ -0,0 +1,13 @@
+//===-- Unittests for isnanf ----------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDSList-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "IsNanTest.h"
+
+#include "src/math/isnanf.h"
+
+LIST_ISNAN_TESTS(float, LIBC_NAMESPACE::isnanf)
diff --git a/libc/test/src/math/isnanl_test.cpp b/libc/test/src/math/isnanl_test.cpp
new file mode 100644
index 0000000000000..1e5f4f4057f19
--- /dev/null
+++ b/libc/test/src/math/isnanl_test.cpp
@@ -0,0 +1,13 @@
+//===-- Unittests for isnanl ----------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDSList-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "IsNanTest.h"
+
+#include "src/math/isnanl.h"
+
+LIST_ISNAN_TESTS(long double, LIBC_NAMESPACE::isnanl)

>From e4bb13d7c700a8d63ca32d3788d4c3527b9a8928 Mon Sep 17 00:00:00 2001
From: Petr Hosek <phosek at google.com>
Date: Sat, 13 Jul 2024 21:06:11 -0700
Subject: [PATCH 2/2] Use `testing::FPTest<T>`

---
 libc/test/include/CMakeLists.txt  | 32 +------------------------
 libc/test/include/IsNanTest.h     | 39 -------------------------------
 libc/test/include/isnan_test.cpp  | 12 ----------
 libc/test/include/isnanf_test.cpp | 12 ----------
 libc/test/include/isnanl_test.cpp | 12 ----------
 libc/test/src/math/IsNanTest.h    | 31 ++++--------------------
 6 files changed, 6 insertions(+), 132 deletions(-)
 delete mode 100644 libc/test/include/IsNanTest.h
 delete mode 100644 libc/test/include/isnan_test.cpp
 delete mode 100644 libc/test/include/isnanf_test.cpp
 delete mode 100644 libc/test/include/isnanl_test.cpp

diff --git a/libc/test/include/CMakeLists.txt b/libc/test/include/CMakeLists.txt
index 32513f234b9b9..287de7b0dff99 100644
--- a/libc/test/include/CMakeLists.txt
+++ b/libc/test/include/CMakeLists.txt
@@ -111,36 +111,6 @@ add_libc_test(
     libc.include.llvm-libc-macros.math_function_macros
 )
 
-add_libc_test(
-  isnan_test
-  SUITE
-    libc_include_tests
-  SRCS
-    isnan_test.cpp
-  DEPENDS
-    libc.include.llvm-libc-macros.math_function_macros
-)
-
-add_libc_test(
-  isnanf_test
-  SUITE
-    libc_include_tests
-  SRCS
-    isnanf_test.cpp
-  DEPENDS
-    libc.include.llvm-libc-macros.math_function_macros
-)
-
-add_libc_test(
-  isnanl_test
-  SUITE
-    libc_include_tests
-  SRCS
-    isnanl_test.cpp
-  DEPENDS
-    libc.include.llvm-libc-macros.math_function_macros
-)
-
 add_libc_test(
   isinf_test
   SUITE
@@ -217,7 +187,7 @@ add_libc_test(
 )
 
 add_libc_test(
-  isnan_c_test
+  isnan_test
   C_TEST
   UNIT_TEST_ONLY
   SUITE
diff --git a/libc/test/include/IsNanTest.h b/libc/test/include/IsNanTest.h
deleted file mode 100644
index 362699a18dfc1..0000000000000
--- a/libc/test/include/IsNanTest.h
+++ /dev/null
@@ -1,39 +0,0 @@
-//===-- Utility class to test the isnan macro [f|l] -------------*- C++ -*-===//
-//
-// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
-// See https://llvm.org/LICENSE.txt for license nanormation.
-// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-//
-//===----------------------------------------------------------------------===//
-
-#ifndef LLVM_LIBC_TEST_INCLUDE_MATH_ISNAN_H
-#define LLVM_LIBC_TEST_INCLUDE_MATH_ISNAN_H
-
-#include "test/UnitTest/FPMatcher.h"
-#include "test/UnitTest/Test.h"
-
-#include "include/llvm-libc-macros/math-function-macros.h"
-
-template <typename T> class IsNanTest : public LIBC_NAMESPACE::testing::Test {
-
-  DECLARE_SPECIAL_CONSTANTS(T)
-
-public:
-  typedef int (*IsNanFunc)(T);
-
-  void testSpecialNumbers(IsNanFunc func) {
-    EXPECT_EQ(func(zero), 0);
-    EXPECT_EQ(func(neg_zero), 0);
-    EXPECT_EQ(func(aNaN), 1);
-    EXPECT_EQ(func(sNaN), 1);
-  }
-};
-
-#define LIST_ISNAN_TESTS(T, func)                                              \
-  using LlvmLibcIsNanTest = IsNanTest<T>;                                      \
-  TEST_F(LlvmLibcIsNanTest, SpecialNumbers) {                                  \
-    auto isnan_func = [](T x) { return func(x); };                             \
-    testSpecialNumbers(isnan_func);                                            \
-  }
-
-#endif // LLVM_LIBC_TEST_INCLUDE_MATH_ISNAN_H
diff --git a/libc/test/include/isnan_test.cpp b/libc/test/include/isnan_test.cpp
deleted file mode 100644
index 07dfab740724b..0000000000000
--- a/libc/test/include/isnan_test.cpp
+++ /dev/null
@@ -1,12 +0,0 @@
-//===-- Unittest for isnan[l] macro ---------------------------------------===//
-//
-// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
-// See https://llvm.org/LICENSE.txt for license information.
-// SPDSList-License-Identifier: Apache-2.0 WITH LLVM-exception
-//
-//===----------------------------------------------------------------------===//
-
-#include "IsNanTest.h"
-#include "include/llvm-libc-macros/math-function-macros.h"
-
-LIST_ISNAN_TESTS(double, isnan)
diff --git a/libc/test/include/isnanf_test.cpp b/libc/test/include/isnanf_test.cpp
deleted file mode 100644
index e78a8e45e0233..0000000000000
--- a/libc/test/include/isnanf_test.cpp
+++ /dev/null
@@ -1,12 +0,0 @@
-//===-- Unittest for isnan[f] macro ---------------------------------------===//
-//
-// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
-// See https://llvm.org/LICENSE.txt for license information.
-// SPDSList-License-Identifier: Apache-2.0 WITH LLVM-exception
-//
-//===----------------------------------------------------------------------===//
-
-#include "IsNanTest.h"
-#include "include/llvm-libc-macros/math-function-macros.h"
-
-LIST_ISNAN_TESTS(float, isnan)
diff --git a/libc/test/include/isnanl_test.cpp b/libc/test/include/isnanl_test.cpp
deleted file mode 100644
index 84759a3ab28bc..0000000000000
--- a/libc/test/include/isnanl_test.cpp
+++ /dev/null
@@ -1,12 +0,0 @@
-//===-- Unittest for isnan[l] macro ---------------------------------------===//
-//
-// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
-// See https://llvm.org/LICENSE.txt for license information.
-// SPDSList-License-Identifier: Apache-2.0 WITH LLVM-exception
-//
-//===----------------------------------------------------------------------===//
-
-#include "IsNanTest.h"
-#include "include/llvm-libc-macros/math-function-macros.h"
-
-LIST_ISNAN_TESTS(long double, isnan)
diff --git a/libc/test/src/math/IsNanTest.h b/libc/test/src/math/IsNanTest.h
index 64513cef6e7d0..e281395f2cf4b 100644
--- a/libc/test/src/math/IsNanTest.h
+++ b/libc/test/src/math/IsNanTest.h
@@ -6,48 +6,27 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "test/UnitTest/FEnvSafeTest.h"
 #include "test/UnitTest/FPMatcher.h"
 #include "test/UnitTest/Test.h"
 
 template <typename T>
-class IsNanTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
+class IsNanTest : public LIBC_NAMESPACE::testing::FPTest<T> {
 
   DECLARE_SPECIAL_CONSTANTS(T)
 
-  static constexpr T one = static_cast<T>(1.0);
-  static constexpr T neg_one = static_cast<T>(-1.0);
-
 public:
   typedef int (*IsNanFunc)(T);
 
   void testSpecialNumbers(IsNanFunc func) {
     EXPECT_EQ(func(zero), 0);
-    EXPECT_EQ(func(one), 0);
-    EXPECT_EQ(func(inf), 0);
-    EXPECT_EQ(func(aNaN), 1);
-
     EXPECT_EQ(func(neg_zero), 0);
-    EXPECT_EQ(func(neg_one), 0);
+    EXPECT_EQ(func(inf), 0);
     EXPECT_EQ(func(neg_inf), 0);
-    EXPECT_EQ(func(neg_aNaN), 1);
-  }
-
-  void testSpecialCases(IsNanFunc func) {
-    EXPECT_EQ(func(one / zero), 0);
-    EXPECT_EQ(func(one / inf), 0);
-    EXPECT_EQ(func(one / neg_inf), 0);
-    EXPECT_EQ(func(inf / neg_inf), 1);
-
-    EXPECT_EQ(func(inf * neg_inf), 0);
-    EXPECT_EQ(func(inf * zero), 1);
-    EXPECT_EQ(func(neg_inf * zero), 1);
-
-    EXPECT_EQ(func(inf + neg_inf), 1);
+    EXPECT_EQ(func(aNaN), 1);
+    EXPECT_EQ(func(sNaN), 1);
   }
 };
 
 #define LIST_ISNAN_TESTS(T, func)                                              \
   using LlvmLibcIsNanTest = IsNanTest<T>;                                      \
-  TEST_F(LlvmLibcIsNanTest, SpecialNumbers) { testSpecialNumbers(&func); }     \
-  TEST_F(LlvmLibcIsNanTest, SpecialCases) { testSpecialCases(&func); }
+  TEST_F(LlvmLibcIsNanTest, SpecialNumbers) { testSpecialNumbers(&func); }



More information about the libc-commits mailing list