1/* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16#include <stdio.h> 17#include <stdlib.h> 18#include <unistd.h> 19#include <math.h> 20#include <limits.h> 21#include "log.h" 22#include "gtest/gtest.h" 23 24using namespace testing::ext; 25 26class MathApiTest : public testing::Test { 27}; 28 29/** 30* @tc.number SUB_KERNEL_MATH_MATH_CEIL_0100 31* @tc.name ceil basic function test 32* @tc.desc [C- SOFTWARE -0100] 33*/ 34HWTEST_F(MathApiTest, testCeil, Function | MediumTest | Level1) { 35 const int testCount = 3; 36 double testValues[] = {-5.9, 0, 123.45}; 37 double expected[] = {-5, 0, 124}; 38 double ret; 39 for (int i = 0; i < testCount; ++i) { 40 ret = ceil(testValues[i]); 41 EXPECT_EQ(ret, expected[i]) << " ceil failed"; 42 } 43} 44 45/** 46* @tc.number SUB_KERNEL_MATH_MATH_CEILF_0100 47* @tc.name ceilf basic function test 48* @tc.desc [C- SOFTWARE -0100] 49*/ 50HWTEST_F(MathApiTest, testCeilf, Function | MediumTest | Level1) { 51 const int testCount = 3; 52 float testValues[] = {-5.9, 0, 123.45}; 53 float expected[] = {-5, 0, 124}; 54 float ret; 55 for (int i = 0; i < testCount; ++i) { 56 ret = ceilf(testValues[i]); 57 EXPECT_EQ(ret, expected[i]) << " ceilf failed"; 58 } 59} 60 61/** 62* @tc.number SUB_KERNEL_MATH_MATH_FINITE_0100 63* @tc.name test finite api 64* @tc.desc [C- SOFTWARE -0100] 65**/ 66HWTEST_F(MathApiTest, testfinite, Function | MediumTest | Level1) { 67 const int testCount = 7; 68 double testValues[7] = {1.0000001, 0.0/0.0, 1.0000001/0.0, 1.79769e+308, NAN}; 69 testValues[5] = sqrt(-1.0000001); 70 testValues[6] = log(0); 71 double expected[] = {1, 0, 0, 1, 0, 0, 0}; 72 double ret; 73 for (int i = 0; i < testCount; ++i) { 74 ret = finite(testValues[i]); 75 EXPECT_EQ(ret, expected[i]) << " finite failed"; 76 } 77} 78 79/** 80* @tc.number SUB_KERNEL_MATH_MATH_FINITEF_0100 81* @tc.name test finitef api 82* @tc.desc [C- SOFTWARE -0100] 83**/ 84HWTEST_F(MathApiTest, testfinitef, Function | MediumTest | Level1) { 85 const int testCount = 6; 86 float testValues[6] = {1, -1, 3.40282e+038, NAN}; 87 testValues[4] = sqrt(-1.0); 88 testValues[5] = log(0); 89 float expected[] = {1, 1, 1, 0, 0, 0}; 90 float ret; 91 for (int i = 0; i < testCount; ++i) { 92 ret = finitef(testValues[i]); 93 EXPECT_EQ(ret, expected[i]) << " finitef failed"; 94 } 95} 96 97/** 98* @tc.number SUB_KERNEL_MATH_MATH_NAN_0100 99* @tc.name test nan api 100* @tc.desc [C- SOFTWARE -0100] 101**/ 102HWTEST_F(MathApiTest, testnan, Function | MediumTest | Level1) { 103 const int testCount = 3; 104 const char *testValues[] = {"1", "99", "abc"}; 105 double ret; 106 for (int i = 0; i < testCount; ++i) { 107 ret = nan(testValues[i]); 108 ASSERT_TRUE(ret != 0) << " nan failed"; 109 } 110} 111 112/** 113* @tc.number SUB_KERNEL_MATH_MATH_NANF_0100 114* @tc.name test nanf api 115* @tc.desc [C- SOFTWARE -0100] 116**/ 117HWTEST_F(MathApiTest, testnanf, Function | MediumTest | Level1) { 118 const int testCount = 3; 119 const char *testValues[] = {"abc", "99", " "}; 120 float ret; 121 for (int i = 0; i < testCount; ++i) { 122 ret = nanf(testValues[i]); 123 ASSERT_TRUE(ret != 0) << " nanf failed"; 124 } 125} 126 127/** 128* @tc.number SUB_KERNEL_MATH_MATH_NANL_0100 129* @tc.name test nanl api 130* @tc.desc [C- SOFTWARE -0100] 131**/ 132HWTEST_F(MathApiTest, testnanl, Function | MediumTest | Level1) { 133 const int testCount = 3; 134 const char *testValues[] = {"abc", "99", " "}; 135 long double ret; 136 for (int i = 0; i < testCount; ++i) { 137 ret = nanl(testValues[i]); 138 ASSERT_TRUE(ret != 0) << " nanl failed"; 139 } 140} 141 142/** 143* @tc.number SUB_KERNEL_MATH_MATH_POW10F_0100 144* @tc.name test pow10f api 145* @tc.desc [C- SOFTWARE -0100] 146**/ 147HWTEST_F(MathApiTest, testpowf10, Function | MediumTest | Level1) { 148 const int testCount = 3; 149 float testValues[] = {0.0, 1.111111, -1.111111, 3.40282e+038}; 150 float expected[] = {1, 12.915494918823242, 0.077426381409168243}; 151 float ret; 152 for (int i = 0; i < testCount; ++i) { 153 ret = pow10f(testValues[i]); 154 EXPECT_EQ(ret, expected[i]) << " pow10f failed"; 155 } 156} 157 158/** 159* @tc.number SUB_KERNEL_MATH_MATH_POW10L_0100 160* @tc.name test pow10l api 161* @tc.desc [C- SOFTWARE -0100] 162**/ 163HWTEST_F(MathApiTest, testpowl10, Function | MediumTest | Level1) { 164 const int testCount = 3; 165 long double testValues[] = {0.0000000000000000, 1.1111111111111111111, -1.1111111111111111111}; 166 long double expected[] = {1, 12.91549665014884, 0.077426368268112708}; 167 long double ret; 168 for (int i = 0; i < testCount; ++i) { 169 ret = pow10l(testValues[i]); 170 EXPECT_EQ(ret, expected[i]) << " pow10l failed"; 171 } 172} 173 174/** 175* @tc.number SUB_KERNEL_MATH_MATH_SIGNBIT_0100 176* @tc.name test signbit api 177* @tc.desc [C- SOFTWARE -0100] 178**/ 179HWTEST_F(MathApiTest, testsignbit, Function | MediumTest | Level1) { 180 const int testCount = 3; 181 float testValues[] = {3.000001, -3.000001, 0.0}; 182 float expected[] = {0, 1, 0}; 183 float ret; 184 for (int i = 0; i < testCount; ++i) { 185 ret = signbit(testValues[i]); 186 EXPECT_EQ(ret, expected[i]) << " signbit failed"; 187 } 188} 189 190/** 191* @tc.number SUB_KERNEL_MATH_MATH_SIGNIFICAND_0100 192* @tc.name test significand api 193* @tc.desc [C- SOFTWARE -0100] 194**/ 195HWTEST_F(MathApiTest, testsignificand, Function | MediumTest | Level1) { 196 const int testCount = 4; 197 double testValues[] = {3.0000001, -3.0000001, 0.0000000, 3.40282e+038}; 198 double expected[] = {1.5000000499999999, -1.5000000499999999, 0.0000000, 1.9999978434325483}; 199 double ret; 200 for (int i = 0; i < testCount; ++i) { 201 ret = significand(testValues[i]); 202 EXPECT_EQ(ret, expected[i]) << " significand failed"; 203 } 204} 205 206/** 207* @tc.number SUB_KERNEL_MATH_MATH_SIGNIFICANDF_0100 208* @tc.name test significandf api 209* @tc.desc [C- SOFTWARE -0100] 210**/ 211HWTEST_F(MathApiTest, testsignificandf, Function | MediumTest | Level1) { 212 const int testCount = 4; 213 float testValues[] = {3.000001, -3.000001, 0.000000, 3.40282e+038}; 214 float expected[] = {1.5000004768371582, -1.5000004768371582, 0.000000, 1.9999978542327881}; 215 float ret; 216 for (int i = 0; i < testCount; ++i) { 217 ret = significandf(testValues[i]); 218 EXPECT_EQ(ret, expected[i]) << " significandf failed"; 219 } 220}