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

source code of libc/test/src/math/smoke/FMinimumMagTest.h