Skip to content

Commit c6cc61d

Browse files
committed
[libc] Unit test for isnan[f,l]
This is a follow up to llvm#98271.
1 parent d99efd5 commit c6cc61d

File tree

5 files changed

+134
-0
lines changed

5 files changed

+134
-0
lines changed

libc/test/src/math/CMakeLists.txt

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2225,6 +2225,45 @@ add_fp_unittest(
22252225
libc.src.__support.FPUtil.fp_bits
22262226
)
22272227

2228+
add_fp_unittest(
2229+
isnan_test
2230+
SUITE
2231+
libc-math-unittests
2232+
SRCS
2233+
isnan_test.cpp
2234+
HDRS
2235+
IsNanTest.h
2236+
DEPENDS
2237+
libc.src.math.isnan
2238+
libc.src.__support.FPUtil.fp_bits
2239+
)
2240+
2241+
add_fp_unittest(
2242+
isnanf_test
2243+
SUITE
2244+
libc-math-unittests
2245+
SRCS
2246+
isnanf_test.cpp
2247+
HDRS
2248+
IsNanTest.h
2249+
DEPENDS
2250+
libc.src.math.isnanf
2251+
libc.src.__support.FPUtil.fp_bits
2252+
)
2253+
2254+
add_fp_unittest(
2255+
isnanl_test
2256+
SUITE
2257+
libc-math-unittests
2258+
SRCS
2259+
isnanl_test.cpp
2260+
HDRS
2261+
IsNanTest.h
2262+
DEPENDS
2263+
libc.src.math.isnanl
2264+
libc.src.__support.FPUtil.fp_bits
2265+
)
2266+
22282267
add_subdirectory(generic)
22292268
add_subdirectory(smoke)
22302269

libc/test/src/math/IsNanTest.h

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
//===-- Utility class to test isnan[f|l] ------------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "test/UnitTest/FEnvSafeTest.h"
10+
#include "test/UnitTest/FPMatcher.h"
11+
#include "test/UnitTest/Test.h"
12+
13+
template <typename T>
14+
class IsNanTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
15+
16+
DECLARE_SPECIAL_CONSTANTS(T)
17+
18+
static constexpr T one = static_cast<T>(1.0);
19+
static constexpr T neg_one = static_cast<T>(-1.0);
20+
21+
public:
22+
typedef int (*IsNanFunc)(T);
23+
24+
void testSpecialNumbers(IsNanFunc func) {
25+
EXPECT_EQ(func(zero), 0);
26+
EXPECT_EQ(func(one), 0);
27+
EXPECT_EQ(func(inf), 0);
28+
EXPECT_EQ(func(aNaN), 1);
29+
30+
EXPECT_EQ(func(neg_zero), 0);
31+
EXPECT_EQ(func(neg_one), 0);
32+
EXPECT_EQ(func(neg_inf), 0);
33+
EXPECT_EQ(func(neg_aNaN), 1);
34+
}
35+
36+
void testSpecialCases(IsNanFunc func) {
37+
EXPECT_EQ(func(one / zero), 0);
38+
EXPECT_EQ(func(one / inf), 0);
39+
EXPECT_EQ(func(one / neg_inf), 0);
40+
EXPECT_EQ(func(inf / neg_inf), 1);
41+
42+
EXPECT_EQ(func(inf * neg_inf), 0);
43+
EXPECT_EQ(func(inf * zero), 1);
44+
EXPECT_EQ(func(neg_inf * zero), 1);
45+
46+
EXPECT_EQ(func(inf + neg_inf), 1);
47+
}
48+
};
49+
50+
#define LIST_ISNAN_TESTS(T, func) \
51+
using LlvmLibcIsNanTest = IsNanTest<T>; \
52+
TEST_F(LlvmLibcIsNanTest, SpecialNumbers) { testSpecialNumbers(&func); } \
53+
TEST_F(LlvmLibcIsNanTest, SpecialCases) { testSpecialCases(&func); }

libc/test/src/math/isnan_test.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//===-- Unittests for isnan -----------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDSList-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "IsNanTest.h"
10+
11+
// We need to avoid expanding isnan to __builtin_isnan.
12+
#undef isnan
13+
14+
#include "src/math/isnan.h"
15+
16+
LIST_ISNAN_TESTS(double, LIBC_NAMESPACE::isnan)

libc/test/src/math/isnanf_test.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//===-- Unittests for isnanf ----------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDSList-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "IsNanTest.h"
10+
11+
#include "src/math/isnanf.h"
12+
13+
LIST_ISNAN_TESTS(float, LIBC_NAMESPACE::isnanf)

libc/test/src/math/isnanl_test.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//===-- Unittests for isnanl ----------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDSList-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "IsNanTest.h"
10+
11+
#include "src/math/isnanl.h"
12+
13+
LIST_ISNAN_TESTS(long double, LIBC_NAMESPACE::isnanl)

0 commit comments

Comments
 (0)