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