1//===-- Utility class to test fmax[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_FMAXTEST_H
10#define LLVM_LIBC_TEST_SRC_MATH_SMOKE_FMAXTEST_H
11
12#include "test/UnitTest/FEnvSafeTest.h"
13#include "test/UnitTest/FPMatcher.h"
14#include "test/UnitTest/Test.h"
15
16template <typename T>
17class FMaxTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
18
19 DECLARE_SPECIAL_CONSTANTS(T)
20
21public:
22 typedef T (*FMaxFunc)(T, T);
23
24 void testNaN(FMaxFunc func) {
25 EXPECT_FP_EQ(inf, func(aNaN, inf));
26 EXPECT_FP_EQ(neg_inf, func(neg_inf, aNaN));
27 EXPECT_FP_EQ(0.0, func(aNaN, 0.0));
28 EXPECT_FP_EQ(-0.0, func(-0.0, aNaN));
29 EXPECT_FP_EQ(T(-1.2345), func(aNaN, T(-1.2345)));
30 EXPECT_FP_EQ(T(1.2345), func(T(1.2345), aNaN));
31 EXPECT_FP_EQ(aNaN, func(aNaN, aNaN));
32 }
33
34 void testInfArg(FMaxFunc func) {
35 EXPECT_FP_EQ(inf, func(neg_inf, inf));
36 EXPECT_FP_EQ(inf, func(inf, 0.0));
37 EXPECT_FP_EQ(inf, func(-0.0, inf));
38 EXPECT_FP_EQ(inf, func(inf, T(1.2345)));
39 EXPECT_FP_EQ(inf, func(T(-1.2345), inf));
40 }
41
42 void testNegInfArg(FMaxFunc func) {
43 EXPECT_FP_EQ(inf, func(inf, neg_inf));
44 EXPECT_FP_EQ(0.0, func(neg_inf, 0.0));
45 EXPECT_FP_EQ(-0.0, func(-0.0, neg_inf));
46 EXPECT_FP_EQ(T(-1.2345), func(neg_inf, T(-1.2345)));
47 EXPECT_FP_EQ(T(1.2345), func(T(1.2345), neg_inf));
48 }
49
50 void testBothZero(FMaxFunc 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(FMaxFunc 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(x, func(x, y));
74 } else {
75 EXPECT_FP_EQ(y, func(x, y));
76 }
77 }
78 }
79};
80
81#define LIST_FMAX_TESTS(T, func) \
82 using LlvmLibcFMaxTest = FMaxTest<T>; \
83 TEST_F(LlvmLibcFMaxTest, NaN) { testNaN(&func); } \
84 TEST_F(LlvmLibcFMaxTest, InfArg) { testInfArg(&func); } \
85 TEST_F(LlvmLibcFMaxTest, NegInfArg) { testNegInfArg(&func); } \
86 TEST_F(LlvmLibcFMaxTest, BothZero) { testBothZero(&func); } \
87 TEST_F(LlvmLibcFMaxTest, Range) { testRange(&func); }
88
89#endif // LLVM_LIBC_TEST_SRC_MATH_SMOKE_FMAXTEST_H
90

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