1 | //===-- Utility class to test fminimum_mag_num[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 | #ifndef LLVM_LIBC_TEST_SRC_MATH_SMOKE_FMINIMUMMAG_NUMTEST_H |
10 | #define LLVM_LIBC_TEST_SRC_MATH_SMOKE_FMINIMUMMAG_NUMTEST_H |
11 | |
12 | #include "src/__support/FPUtil/BasicOperations.h" |
13 | #include "src/__support/FPUtil/FPBits.h" |
14 | #include "test/UnitTest/FEnvSafeTest.h" |
15 | #include "test/UnitTest/FPMatcher.h" |
16 | #include "test/UnitTest/Test.h" |
17 | |
18 | template <typename T> |
19 | class FMinimumMagNumTest : public LIBC_NAMESPACE::testing::FEnvSafeTest { |
20 | |
21 | DECLARE_SPECIAL_CONSTANTS(T) |
22 | |
23 | public: |
24 | typedef T (*FMinimumMagNumFunc)(T, T); |
25 | |
26 | void testNaN(FMinimumMagNumFunc func) { |
27 | EXPECT_FP_EQ(inf, func(aNaN, inf)); |
28 | EXPECT_FP_EQ_WITH_EXCEPTION(inf, func(sNaN, inf), FE_INVALID); |
29 | EXPECT_FP_EQ(neg_inf, func(neg_inf, aNaN)); |
30 | EXPECT_FP_EQ_WITH_EXCEPTION(neg_inf, func(neg_inf, sNaN), FE_INVALID); |
31 | EXPECT_EQ(FPBits(aNaN).uintval(), FPBits(func(aNaN, aNaN)).uintval()); |
32 | EXPECT_FP_EQ(0.0, func(aNaN, 0.0)); |
33 | EXPECT_FP_EQ(-0.0, func(-0.0, aNaN)); |
34 | EXPECT_FP_EQ_WITH_EXCEPTION(0.0, func(sNaN, 0.0), FE_INVALID); |
35 | EXPECT_FP_EQ_WITH_EXCEPTION(-0.0, func(-0.0, sNaN), FE_INVALID); |
36 | EXPECT_FP_EQ(T(-1.2345), func(aNaN, T(-1.2345))); |
37 | EXPECT_FP_EQ(T(1.2345), func(T(1.2345), aNaN)); |
38 | EXPECT_FP_EQ_WITH_EXCEPTION(T(-1.2345), func(sNaN, T(-1.2345)), FE_INVALID); |
39 | EXPECT_FP_EQ_WITH_EXCEPTION(T(1.2345), func(T(1.2345), sNaN), FE_INVALID); |
40 | EXPECT_FP_IS_NAN_WITH_EXCEPTION(func(aNaN, sNaN), FE_INVALID); |
41 | EXPECT_FP_IS_NAN_WITH_EXCEPTION(func(sNaN, aNaN), FE_INVALID); |
42 | EXPECT_EQ(FPBits(aNaN).uintval(), FPBits(func(aNaN, sNaN)).uintval()); |
43 | EXPECT_EQ(FPBits(aNaN).uintval(), FPBits(func(sNaN, aNaN)).uintval()); |
44 | EXPECT_EQ(FPBits(aNaN).uintval(), FPBits(func(sNaN, sNaN)).uintval()); |
45 | } |
46 | |
47 | void testInfArg(FMinimumMagNumFunc func) { |
48 | EXPECT_FP_EQ(neg_inf, func(neg_inf, inf)); |
49 | EXPECT_FP_EQ(0.0, func(inf, 0.0)); |
50 | EXPECT_FP_EQ(-0.0, func(-0.0, inf)); |
51 | EXPECT_FP_EQ(T(1.2345), func(inf, T(1.2345))); |
52 | EXPECT_FP_EQ(T(-1.2345), func(T(-1.2345), inf)); |
53 | } |
54 | |
55 | void testNegInfArg(FMinimumMagNumFunc func) { |
56 | EXPECT_FP_EQ(neg_inf, func(inf, neg_inf)); |
57 | EXPECT_FP_EQ(0.0, func(neg_inf, 0.0)); |
58 | EXPECT_FP_EQ(-0.0, func(-0.0, neg_inf)); |
59 | EXPECT_FP_EQ(T(-1.2345), func(neg_inf, T(-1.2345))); |
60 | EXPECT_FP_EQ(T(1.2345), func(T(1.2345), neg_inf)); |
61 | } |
62 | |
63 | void testBothZero(FMinimumMagNumFunc func) { |
64 | EXPECT_FP_EQ(0.0, func(0.0, 0.0)); |
65 | EXPECT_FP_EQ(-0.0, func(-0.0, 0.0)); |
66 | EXPECT_FP_EQ(-0.0, func(0.0, -0.0)); |
67 | EXPECT_FP_EQ(-0.0, func(-0.0, -0.0)); |
68 | } |
69 | |
70 | void testRange(FMinimumMagNumFunc func) { |
71 | constexpr StorageType COUNT = 100'001; |
72 | constexpr StorageType STEP = STORAGE_MAX / COUNT; |
73 | for (StorageType i = 0, v = 0, w = STORAGE_MAX; i <= COUNT; |
74 | ++i, v += STEP, w -= STEP) { |
75 | FPBits xbits(v), ybits(w); |
76 | if (xbits.is_inf_or_nan()) |
77 | continue; |
78 | if (ybits.is_inf_or_nan()) |
79 | continue; |
80 | T x = xbits.get_val(); |
81 | T y = ybits.get_val(); |
82 | if ((x == 0) && (y == 0)) |
83 | continue; |
84 | |
85 | if (LIBC_NAMESPACE::fputil::abs(x) > LIBC_NAMESPACE::fputil::abs(y)) { |
86 | EXPECT_FP_EQ(y, func(x, y)); |
87 | } else { |
88 | EXPECT_FP_EQ(x, func(x, y)); |
89 | } |
90 | } |
91 | } |
92 | }; |
93 | |
94 | #define LIST_FMINIMUM_MAG_NUM_TESTS(T, func) \ |
95 | using LlvmLibcFMinimumMagNumTest = FMinimumMagNumTest<T>; \ |
96 | TEST_F(LlvmLibcFMinimumMagNumTest, NaN) { testNaN(&func); } \ |
97 | TEST_F(LlvmLibcFMinimumMagNumTest, InfArg) { testInfArg(&func); } \ |
98 | TEST_F(LlvmLibcFMinimumMagNumTest, NegInfArg) { testNegInfArg(&func); } \ |
99 | TEST_F(LlvmLibcFMinimumMagNumTest, BothZero) { testBothZero(&func); } \ |
100 | TEST_F(LlvmLibcFMinimumMagNumTest, Range) { testRange(&func); } |
101 | |
102 | #endif // LLVM_LIBC_TEST_SRC_MATH_SMOKE_FMINIMUMMAG_NUMTEST_H |
103 | |