xref: /aosp_15_r20/external/llvm-libc/test/src/complex/CRealTest.h (revision 71db0c75aadcf003ffe3238005f61d7618a3fead)
1 //===-- Utility class to test different flavors of creal --------*- 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_COMPLEX_CREALTEST_H
10 #define LLVM_LIBC_TEST_SRC_COMPLEX_CREALTEST_H
11 
12 #include "test/UnitTest/FEnvSafeTest.h"
13 #include "test/UnitTest/FPMatcher.h"
14 #include "test/UnitTest/Test.h"
15 
16 #include "hdr/math_macros.h"
17 
18 template <typename CFPT, typename FPT>
19 class CRealTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
20 
21   DECLARE_SPECIAL_CONSTANTS(FPT)
22 
23 public:
24   typedef FPT (*CRealFunc)(CFPT);
25 
testSpecialNumbers(CRealFunc func)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);
42   }
43 
testRoundedNumbers(CRealFunc func)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));
49 
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));
54 
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));
59 
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));
64   }
65 };
66 
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); }
71 
72 #endif // LLVM_LIBC_TEST_SRC_COMPLEX_CREALTEST_H
73