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 18 template <typename CFPT, typename FPT> 19 class CImagTest : public LIBC_NAMESPACE::testing::FEnvSafeTest { 20 21 DECLARE_SPECIAL_CONSTANTS(FPT) 22 23 public: 24 typedef FPT (*CImagFunc)(CFPT); 25 testSpecialNumbers(CImagFunc func)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(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); 44 } 45 testRoundedNumbers(CImagFunc func)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)); 51 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)); 56 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)); 61 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)); 66 } 67 }; 68 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); } 73 74 #endif // LLVM_LIBC_TEST_SRC_COMPLEX_CIMAGTEST_H 75