1 //===-- Utility class to test different flavors of cimag --------*- 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_CIMAGTEST_H
10 #define LLVM_LIBC_TEST_SRC_COMPLEX_CIMAGTEST_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 CImagTest
: public LIBC_NAMESPACE::testing::FEnvSafeTest
{
21 DECLARE_SPECIAL_CONSTANTS(FPT
)
24 typedef FPT (*CImagFunc
)(CFPT
);
26 void testSpecialNumbers(CImagFunc func
) {
27 EXPECT_FP_EQ(func(CFPT(67.123 + aNaN
* 1.0i
)), aNaN
);
28 EXPECT_FP_EQ(func(CFPT(78.319 + neg_aNaN
* 1.0i
)), neg_aNaN
);
29 EXPECT_FP_EQ(func(CFPT(7813.131 + sNaN
* 1.0i
)), sNaN
);
30 EXPECT_FP_EQ(func(CFPT(7824.152 + neg_sNaN
* 1.0i
)), neg_sNaN
);
31 EXPECT_FP_EQ(func(CFPT(9024.2442 + inf
* 1.0i
)), inf
);
32 EXPECT_FP_EQ(func(CFPT(8923.124 + neg_inf
* 1.0i
)), neg_inf
);
33 EXPECT_FP_EQ(func(CFPT(782.124 + min_normal
* 1.0i
)), min_normal
);
34 EXPECT_FP_EQ(func(CFPT(2141.2352 + max_normal
* 1.0i
)), max_normal
);
35 EXPECT_FP_EQ(func(CFPT(341.134 + neg_max_normal
* 1.0i
)), neg_max_normal
);
36 EXPECT_FP_EQ(func(CFPT(781.142 + min_denormal
* 1.0i
)), min_denormal
);
37 EXPECT_FP_EQ(func(CFPT(781.134 + neg_min_denormal
* 1.0i
)),
39 EXPECT_FP_EQ(func(CFPT(1241.112 + max_denormal
* 1.0i
)), max_denormal
);
40 EXPECT_FP_EQ(func(CFPT(121.121 + zero
* 1.0i
)), zero
);
41 EXPECT_FP_EQ(func(CFPT(neg_zero
+ zero
* 1.0i
)), zero
);
42 EXPECT_FP_EQ(func(CFPT(neg_zero
+ neg_zero
* 1.0i
)), neg_zero
);
43 EXPECT_FP_EQ(func(CFPT(zero
+ neg_zero
* 1.0i
)), neg_zero
);
46 void testRoundedNumbers(CImagFunc func
) {
47 EXPECT_FP_EQ(func((CFPT
)(4523.1413 + 12413.1414i
)), (FPT
)(12413.1414));
48 EXPECT_FP_EQ(func((CFPT
)(-4523.1413 + 12413.1414i
)), (FPT
)(12413.1414));
49 EXPECT_FP_EQ(func((CFPT
)(4523.1413 - 12413.1414i
)), (FPT
)(-12413.1414));
50 EXPECT_FP_EQ(func((CFPT
)(-4523.1413 - 12413.1414i
)), (FPT
)(-12413.1414));
52 EXPECT_FP_EQ(func((CFPT
)(3210.5678 + 9876.5432i
)), (FPT
)(9876.5432));
53 EXPECT_FP_EQ(func((CFPT
)(-3210.5678 + 9876.5432i
)), (FPT
)(9876.5432));
54 EXPECT_FP_EQ(func((CFPT
)(3210.5678 - 9876.5432i
)), (FPT
)(-9876.5432));
55 EXPECT_FP_EQ(func((CFPT
)(-3210.5678 - 9876.5432i
)), (FPT
)(-9876.5432));
57 EXPECT_FP_EQ(func((CFPT
)(1234.4321 + 4321.1234i
)), (FPT
)(4321.1234));
58 EXPECT_FP_EQ(func((CFPT
)(-1234.4321 + 4321.1234i
)), (FPT
)(4321.1234));
59 EXPECT_FP_EQ(func((CFPT
)(1234.4321 - 4321.1234i
)), (FPT
)(-4321.1234));
60 EXPECT_FP_EQ(func((CFPT
)(-1234.4321 - 4321.1234i
)), (FPT
)(-4321.1234));
62 EXPECT_FP_EQ(func((CFPT
)(6789.1234 + 8765.6789i
)), (FPT
)(8765.6789));
63 EXPECT_FP_EQ(func((CFPT
)(-6789.1234 + 8765.6789i
)), (FPT
)(8765.6789));
64 EXPECT_FP_EQ(func((CFPT
)(6789.1234 - 8765.6789i
)), (FPT
)(-8765.6789));
65 EXPECT_FP_EQ(func((CFPT
)(-6789.1234 - 8765.6789i
)), (FPT
)(-8765.6789));
69 #define LIST_CIMAG_TESTS(U, T, func) \
70 using LlvmLibcCImagTest = CImagTest<U, T>; \
71 TEST_F(LlvmLibcCImagTest, SpecialNumbers) { testSpecialNumbers(&func); } \
72 TEST_F(LlvmLibcCImagTest, RoundedNumbers) { testRoundedNumbers(&func); }
74 #endif // LLVM_LIBC_TEST_SRC_COMPLEX_CIMAGTEST_H