1//===-- Utility class to test different flavors of cimag --------*- 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_CIMAGTEST_H
10#define LLVM_LIBC_TEST_SRC_COMPLEX_CIMAGTEST_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
18template <typename CFPT, typename FPT>
19class CImagTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
20
21 DECLARE_SPECIAL_CONSTANTS(FPT)
22
23public:
24 typedef FPT (*CImagFunc)(CFPT);
25
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)),
38 neg_min_denormal);
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(0.0 + 0.0i)), zero);
42 EXPECT_FP_EQ(func(CFPT(-0.0 + 0.0i)), zero);
43 EXPECT_FP_EQ(func(CFPT(0.0 - 0.0i)), neg_zero);
44 EXPECT_FP_EQ(func(CFPT(-0.0 - 0.0i)), neg_zero);
45 EXPECT_FP_EQ(func(CFPT(0.0)), zero);
46 EXPECT_FP_EQ(func(CFPT(-0.0)), zero);
47 EXPECT_FP_EQ(func(CFPT(0.0i)), zero);
48 EXPECT_FP_EQ(func(CFPT(-0.0i)), neg_zero);
49 }
50
51 void testRoundedNumbers(CImagFunc func) {
52 EXPECT_FP_EQ(func((CFPT)(4523.1413 + 12413.1414i)), (FPT)(12413.1414));
53 EXPECT_FP_EQ(func((CFPT)(-4523.1413 + 12413.1414i)), (FPT)(12413.1414));
54 EXPECT_FP_EQ(func((CFPT)(4523.1413 - 12413.1414i)), (FPT)(-12413.1414));
55 EXPECT_FP_EQ(func((CFPT)(-4523.1413 - 12413.1414i)), (FPT)(-12413.1414));
56
57 EXPECT_FP_EQ(func((CFPT)(3210.5678 + 9876.5432i)), (FPT)(9876.5432));
58 EXPECT_FP_EQ(func((CFPT)(-3210.5678 + 9876.5432i)), (FPT)(9876.5432));
59 EXPECT_FP_EQ(func((CFPT)(3210.5678 - 9876.5432i)), (FPT)(-9876.5432));
60 EXPECT_FP_EQ(func((CFPT)(-3210.5678 - 9876.5432i)), (FPT)(-9876.5432));
61
62 EXPECT_FP_EQ(func((CFPT)(1234.4321 + 4321.1234i)), (FPT)(4321.1234));
63 EXPECT_FP_EQ(func((CFPT)(-1234.4321 + 4321.1234i)), (FPT)(4321.1234));
64 EXPECT_FP_EQ(func((CFPT)(1234.4321 - 4321.1234i)), (FPT)(-4321.1234));
65 EXPECT_FP_EQ(func((CFPT)(-1234.4321 - 4321.1234i)), (FPT)(-4321.1234));
66
67 EXPECT_FP_EQ(func((CFPT)(6789.1234 + 8765.6789i)), (FPT)(8765.6789));
68 EXPECT_FP_EQ(func((CFPT)(-6789.1234 + 8765.6789i)), (FPT)(8765.6789));
69 EXPECT_FP_EQ(func((CFPT)(6789.1234 - 8765.6789i)), (FPT)(-8765.6789));
70 EXPECT_FP_EQ(func((CFPT)(-6789.1234 - 8765.6789i)), (FPT)(-8765.6789));
71 }
72};
73
74#define LIST_CIMAG_TESTS(U, T, func) \
75 using LlvmLibcCImagTest = CImagTest<U, T>; \
76 TEST_F(LlvmLibcCImagTest, SpecialNumbers) { testSpecialNumbers(&func); } \
77 TEST_F(LlvmLibcCImagTest, RoundedNumbers) { testRoundedNumbers(&func); }
78
79#endif // LLVM_LIBC_TEST_SRC_COMPLEX_CIMAGTEST_H
80

source code of libc/test/src/complex/CImagTest.h