1 //===-- Utility class to test different flavors of creal --------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_LIBC_TEST_SRC_COMPLEX_CREALTEST_H
10 #define LLVM_LIBC_TEST_SRC_COMPLEX_CREALTEST_H
12 #include "test/UnitTest/FEnvSafeTest.h"
13 #include "test/UnitTest/FPMatcher.h"
14 #include "test/UnitTest/Test.h"
16 #include "hdr/math_macros.h"
18 template <typename CFPT
, typename FPT
>
19 class CRealTest
: public LIBC_NAMESPACE::testing::FEnvSafeTest
{
21 DECLARE_SPECIAL_CONSTANTS(FPT
)
24 typedef FPT (*CRealFunc
)(CFPT
);
26 void testSpecialNumbers(CRealFunc func
) {
27 EXPECT_FP_EQ(func(CFPT(aNaN
+ 67.123i
)), aNaN
);
28 EXPECT_FP_EQ(func(CFPT(neg_aNaN
+ 78.319i
)), neg_aNaN
);
29 EXPECT_FP_EQ(func(CFPT(sNaN
+ 7813.131i
)), sNaN
);
30 EXPECT_FP_EQ(func(CFPT(neg_sNaN
+ 7824.152i
)), neg_sNaN
);
31 EXPECT_FP_EQ(func(CFPT(inf
+ 9024.2442i
)), inf
);
32 EXPECT_FP_EQ(func(CFPT(neg_inf
+ 8923.124i
)), neg_inf
);
33 EXPECT_FP_EQ(func(CFPT(min_normal
+ 782.124i
)), min_normal
);
34 EXPECT_FP_EQ(func(CFPT(max_normal
+ 2141.2352i
)), max_normal
);
35 EXPECT_FP_EQ(func(CFPT(neg_max_normal
+ 341.134i
)), neg_max_normal
);
36 EXPECT_FP_EQ(func(CFPT(min_denormal
+ 781.142i
)), min_denormal
);
37 EXPECT_FP_EQ(func(CFPT(neg_min_denormal
+ 781.134i
)), neg_min_denormal
);
38 EXPECT_FP_EQ(func(CFPT(max_denormal
+ 1241.112i
)), max_denormal
);
39 EXPECT_FP_EQ(func(CFPT(zero
+ 121.121i
)), zero
);
40 EXPECT_FP_EQ(func(CFPT(neg_zero
+ neg_zero
* 1.0i
)), neg_zero
);
41 EXPECT_FP_EQ(func(CFPT(neg_zero
+ zero
* 1.0i
)), zero
);
44 void testRoundedNumbers(CRealFunc func
) {
45 EXPECT_FP_EQ(func((CFPT
)(4523.1413 + 12413.1414i
)), (FPT
)(4523.1413));
46 EXPECT_FP_EQ(func((CFPT
)(-4523.1413 + 12413.1414i
)), (FPT
)(-4523.1413));
47 EXPECT_FP_EQ(func((CFPT
)(4523.1413 - 12413.1414i
)), (FPT
)(4523.1413));
48 EXPECT_FP_EQ(func((CFPT
)(-4523.1413 - 12413.1414i
)), (FPT
)(-4523.1413));
50 EXPECT_FP_EQ(func((CFPT
)(3210.5678 + 9876.5432i
)), (FPT
)(3210.5678));
51 EXPECT_FP_EQ(func((CFPT
)(-3210.5678 + 9876.5432i
)), (FPT
)(-3210.5678));
52 EXPECT_FP_EQ(func((CFPT
)(3210.5678 - 9876.5432i
)), (FPT
)(3210.5678));
53 EXPECT_FP_EQ(func((CFPT
)(-3210.5678 - 9876.5432i
)), (FPT
)(-3210.5678));
55 EXPECT_FP_EQ(func((CFPT
)(1234.4321 + 4321.1234i
)), (FPT
)(1234.4321));
56 EXPECT_FP_EQ(func((CFPT
)(-1234.4321 + 4321.1234i
)), (FPT
)(-1234.4321));
57 EXPECT_FP_EQ(func((CFPT
)(1234.4321 - 4321.1234i
)), (FPT
)(1234.4321));
58 EXPECT_FP_EQ(func((CFPT
)(-1234.4321 - 4321.1234i
)), (FPT
)(-1234.4321));
60 EXPECT_FP_EQ(func((CFPT
)(6789.1234 + 8765.6789i
)), (FPT
)(6789.1234));
61 EXPECT_FP_EQ(func((CFPT
)(-6789.1234 + 8765.6789i
)), (FPT
)(-6789.1234));
62 EXPECT_FP_EQ(func((CFPT
)(6789.1234 - 8765.6789i
)), (FPT
)(6789.1234));
63 EXPECT_FP_EQ(func((CFPT
)(-6789.1234 - 8765.6789i
)), (FPT
)(-6789.1234));
67 #define LIST_CREAL_TESTS(U, T, func) \
68 using LlvmLibcCRealTest = CRealTest<U, T>; \
69 TEST_F(LlvmLibcCRealTest, SpecialNumbers) { testSpecialNumbers(&func); } \
70 TEST_F(LlvmLibcCRealTest, RoundedNumbers) { testRoundedNumbers(&func); }
72 #endif // LLVM_LIBC_TEST_SRC_COMPLEX_CREALTEST_H