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

Petr Hosek via libc-commits libc-commits at lists.llvm.org
Tue Jul 9 23:10:49 PDT 2024


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

This is a follow up to #98271.

>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] [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)



More information about the libc-commits mailing list