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 "log.h" 21f6603c60Sopenharmony_ci#include "gtest/gtest.h" 22f6603c60Sopenharmony_ci#include "inttypes.h" 23f6603c60Sopenharmony_ci 24f6603c60Sopenharmony_ciusing namespace testing::ext; 25f6603c60Sopenharmony_ci 26f6603c60Sopenharmony_ciclass MathStdApiTest : public testing::Test { 27f6603c60Sopenharmony_ci}; 28f6603c60Sopenharmony_ci 29f6603c60Sopenharmony_ci/** 30f6603c60Sopenharmony_ci* @tc.number SUB_KERNEL_MATH_STD_STDLIB_0100 31f6603c60Sopenharmony_ci* @tc.name test abs api 32f6603c60Sopenharmony_ci* @tc.desc [C- SOFTWARE -0100] 33f6603c60Sopenharmony_ci*/ 34f6603c60Sopenharmony_ciHWTEST_F(MathStdApiTest, testAbs, Function | MediumTest | Level1) { 35f6603c60Sopenharmony_ci 36f6603c60Sopenharmony_ci const int testCount = 3; 37f6603c60Sopenharmony_ci int testValues[] = {-3, 0, 3}; 38f6603c60Sopenharmony_ci int expected[] = {3, 0, 3}; 39f6603c60Sopenharmony_ci int ret; 40f6603c60Sopenharmony_ci for (int i = 0; i < testCount; ++i) { 41f6603c60Sopenharmony_ci ret = abs(testValues[i]); 42f6603c60Sopenharmony_ci EXPECT_EQ(ret, expected[i]) << "abs failed"; 43f6603c60Sopenharmony_ci } 44f6603c60Sopenharmony_ci} 45f6603c60Sopenharmony_ci 46f6603c60Sopenharmony_ci/** 47f6603c60Sopenharmony_ci* @tc.number SUB_KERNEL_MATH_STD_DIV_0100 48f6603c60Sopenharmony_ci* @tc.name test div api 49f6603c60Sopenharmony_ci* @tc.desc [C- SOFTWARE -0100] 50f6603c60Sopenharmony_ci*/ 51f6603c60Sopenharmony_ciHWTEST_F(MathStdApiTest, testDiv, Function | MediumTest | Level1) { 52f6603c60Sopenharmony_ci 53f6603c60Sopenharmony_ci const int testCount = 3; 54f6603c60Sopenharmony_ci int numer[]={11, -11, 0}; 55f6603c60Sopenharmony_ci int denom[]={2, 2, -1}; 56f6603c60Sopenharmony_ci int aquot[] = {5, -5, 0}; 57f6603c60Sopenharmony_ci int arem[] = {1, -1, 0}; 58f6603c60Sopenharmony_ci 59f6603c60Sopenharmony_ci div_t ret; 60f6603c60Sopenharmony_ci for (int i = 0; i < testCount; ++i) { 61f6603c60Sopenharmony_ci ret = div(numer[i], denom[i]); 62f6603c60Sopenharmony_ci ASSERT_TRUE(ret.quot == aquot[i] && ret.rem == arem[i]) << " div failed"; 63f6603c60Sopenharmony_ci } 64f6603c60Sopenharmony_ci} 65f6603c60Sopenharmony_ci 66f6603c60Sopenharmony_ci/** 67f6603c60Sopenharmony_ci* @tc.number SUB_KERNEL_MATH_STD_IMAXABS_0100 68f6603c60Sopenharmony_ci* @tc.name test imaxabs api 69f6603c60Sopenharmony_ci* @tc.desc [C- SOFTWARE -0100] 70f6603c60Sopenharmony_ci**/ 71f6603c60Sopenharmony_ciHWTEST_F(MathStdApiTest, testimaxabs, Function | MediumTest | Level1) { 72f6603c60Sopenharmony_ci 73f6603c60Sopenharmony_ci const int testCount = 3; 74f6603c60Sopenharmony_ci intmax_t testValues[] = {3765, -1234, 0}; 75f6603c60Sopenharmony_ci intmax_t expected[] = {3765, 1234, 0}; 76f6603c60Sopenharmony_ci 77f6603c60Sopenharmony_ci intmax_t ret; 78f6603c60Sopenharmony_ci for (int i = 0; i < testCount; ++i) { 79f6603c60Sopenharmony_ci ret = imaxabs(testValues[i]); 80f6603c60Sopenharmony_ci EXPECT_EQ(ret, expected[i]) << " imaxabs failed"; 81f6603c60Sopenharmony_ci } 82f6603c60Sopenharmony_ci} 83f6603c60Sopenharmony_ci 84f6603c60Sopenharmony_ci/** 85f6603c60Sopenharmony_ci* @tc.number SUB_KERNEL_MATH_STD_IMAXDIV_0100 86f6603c60Sopenharmony_ci* @tc.name test imaxdiv api 87f6603c60Sopenharmony_ci* @tc.desc [C- SOFTWARE -0100] 88f6603c60Sopenharmony_ci**/ 89f6603c60Sopenharmony_ciHWTEST_F(MathStdApiTest, testimaxdiv, Function | MediumTest | Level1) { 90f6603c60Sopenharmony_ci 91f6603c60Sopenharmony_ci const int testCount = 4; 92f6603c60Sopenharmony_ci intmax_t numerator[] = {2000000000, 2000000000, -2000000001, -2000000001}; 93f6603c60Sopenharmony_ci intmax_t denominator[] = {2, -2, 2, -2}; 94f6603c60Sopenharmony_ci intmax_t expected[] = {1000000000, -1000000000, -1000000000, 1000000000}; 95f6603c60Sopenharmony_ci 96f6603c60Sopenharmony_ci imaxdiv_t ret; 97f6603c60Sopenharmony_ci for (int i = 0; i < testCount; ++i) { 98f6603c60Sopenharmony_ci ret = imaxdiv(numerator[i], denominator[i]); 99f6603c60Sopenharmony_ci ASSERT_TRUE(ret.quot == expected[i]) << " imaxdiv failed"; 100f6603c60Sopenharmony_ci } 101f6603c60Sopenharmony_ci} 102f6603c60Sopenharmony_ci 103f6603c60Sopenharmony_ci/** 104f6603c60Sopenharmony_ci* @tc.number SUB_KERNEL_MATH_STD_LABS_0100 105f6603c60Sopenharmony_ci* @tc.name test labs api 106f6603c60Sopenharmony_ci* @tc.desc [C- SOFTWARE -0100] 107f6603c60Sopenharmony_ci**/ 108f6603c60Sopenharmony_ciHWTEST_F(MathStdApiTest, testlabs, Function | MediumTest | Level1) { 109f6603c60Sopenharmony_ci 110f6603c60Sopenharmony_ci const int testCount = 3; 111f6603c60Sopenharmony_ci long testValues[] = {214748364, -214748364, 0}; 112f6603c60Sopenharmony_ci long expected[] = {214748364, 214748364, 0}; 113f6603c60Sopenharmony_ci 114f6603c60Sopenharmony_ci long ret; 115f6603c60Sopenharmony_ci for (int i = 0; i < testCount; ++i) { 116f6603c60Sopenharmony_ci ret = labs(testValues[i]); 117f6603c60Sopenharmony_ci EXPECT_EQ(ret, expected[i]) << " labs failed"; 118f6603c60Sopenharmony_ci } 119f6603c60Sopenharmony_ci} 120f6603c60Sopenharmony_ci 121f6603c60Sopenharmony_ci/** 122f6603c60Sopenharmony_ci* @tc.number SUB_KERNEL_MATH_STD_LDIV_0100 123f6603c60Sopenharmony_ci* @tc.name test ldiv api 124f6603c60Sopenharmony_ci* @tc.desc [C- SOFTWARE -0100] 125f6603c60Sopenharmony_ci**/ 126f6603c60Sopenharmony_ciHWTEST_F(MathStdApiTest, testldiv, Function | MediumTest | Level1) { 127f6603c60Sopenharmony_ci 128f6603c60Sopenharmony_ci const int testCount = 4; 129f6603c60Sopenharmony_ci long numer[] = {20000000, 20000000, -20000001, -20000001}; 130f6603c60Sopenharmony_ci long denom[] = {2, -2, 2, -2}; 131f6603c60Sopenharmony_ci long aquot[] = {10000000, -10000000, -10000000, 10000000}; 132f6603c60Sopenharmony_ci long arem[] = {0, 0, -1, -1}; 133f6603c60Sopenharmony_ci 134f6603c60Sopenharmony_ci ldiv_t ret; 135f6603c60Sopenharmony_ci for (int i = 0; i < testCount; ++i) { 136f6603c60Sopenharmony_ci ret = ldiv(numer[i], denom[i]); 137f6603c60Sopenharmony_ci ASSERT_TRUE(ret.quot == aquot[i] && ret.rem == arem[i]) << " ldiv failed"; 138f6603c60Sopenharmony_ci } 139f6603c60Sopenharmony_ci} 140f6603c60Sopenharmony_ci 141f6603c60Sopenharmony_ci/** 142f6603c60Sopenharmony_ci* @tc.number SUB_KERNEL_MATH_STD_LLABS_0100 143f6603c60Sopenharmony_ci* @tc.name test llabs api 144f6603c60Sopenharmony_ci* @tc.desc [C- SOFTWARE -0100] 145f6603c60Sopenharmony_ci**/ 146f6603c60Sopenharmony_ciHWTEST_F(MathStdApiTest, testllabs, Function | MediumTest | Level1) { 147f6603c60Sopenharmony_ci 148f6603c60Sopenharmony_ci const int testCount = 3; 149f6603c60Sopenharmony_ci intmax_t testValues[] = {2147483649, -2147483649, 0}; 150f6603c60Sopenharmony_ci intmax_t expected[] = {2147483649, 2147483649, 0}; 151f6603c60Sopenharmony_ci 152f6603c60Sopenharmony_ci float ret; 153f6603c60Sopenharmony_ci for (int i = 0; i < testCount; ++i) { 154f6603c60Sopenharmony_ci ret = llabs(testValues[i]); 155f6603c60Sopenharmony_ci EXPECT_EQ(ret, expected[i]) << " llabs failed"; 156f6603c60Sopenharmony_ci } 157f6603c60Sopenharmony_ci} 158f6603c60Sopenharmony_ci 159f6603c60Sopenharmony_ci/** 160f6603c60Sopenharmony_ci* @tc.number SUB_KERNEL_MATH_STD_LLDIV_0100 161f6603c60Sopenharmony_ci* @tc.name test lldiv api 162f6603c60Sopenharmony_ci* @tc.desc [C- SOFTWARE -0100] 163f6603c60Sopenharmony_ci**/ 164f6603c60Sopenharmony_ciHWTEST_F(MathStdApiTest, testlldiv, Function | MediumTest | Level1) { 165f6603c60Sopenharmony_ci 166f6603c60Sopenharmony_ci const int testCount = 4; 167f6603c60Sopenharmony_ci long long numer[] = {20000000000, 20000000000, -20000000001, -20000000001}; 168f6603c60Sopenharmony_ci long long denom[] = {2, -2, 2, -2}; 169f6603c60Sopenharmony_ci long long aquot[] = {10000000000, -10000000000, -10000000000, 10000000000}; 170f6603c60Sopenharmony_ci long long arem[] = {0, 0, -1, -1}; 171f6603c60Sopenharmony_ci 172f6603c60Sopenharmony_ci lldiv_t ret; 173f6603c60Sopenharmony_ci for (int i = 0; i < testCount; ++i) { 174f6603c60Sopenharmony_ci ret = lldiv(numer[i], denom[i]); 175f6603c60Sopenharmony_ci ASSERT_TRUE(ret.quot == aquot[i] && ret.rem == arem[i]) << " lldiv failed"; 176f6603c60Sopenharmony_ci } 177f6603c60Sopenharmony_ci} 178