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