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 <stdlib.h> 17f6603c60Sopenharmony_ci 18f6603c60Sopenharmony_ci#include "gtest/gtest.h" 19f6603c60Sopenharmony_ci#include "log.h" 20f6603c60Sopenharmony_ci#include "utils.h" 21f6603c60Sopenharmony_ci 22f6603c60Sopenharmony_ciusing namespace testing::ext; 23f6603c60Sopenharmony_ci 24f6603c60Sopenharmony_ciclass ActsUtilMathApiTest : public testing::Test { 25f6603c60Sopenharmony_ciprotected: 26f6603c60Sopenharmony_ci // SetUpTestCase: Testsuit setup, run before 1st testcase 27f6603c60Sopenharmony_ci static void SetUpTestCase(void) 28f6603c60Sopenharmony_ci { 29f6603c60Sopenharmony_ci } 30f6603c60Sopenharmony_ci // TearDownTestCase: Testsuit teardown, run after last testcase 31f6603c60Sopenharmony_ci static void TearDownTestCase(void) 32f6603c60Sopenharmony_ci { 33f6603c60Sopenharmony_ci } 34f6603c60Sopenharmony_ci // Testcase setup 35f6603c60Sopenharmony_ci virtual void SetUp() 36f6603c60Sopenharmony_ci { 37f6603c60Sopenharmony_ci } 38f6603c60Sopenharmony_ci // Testcase teardown 39f6603c60Sopenharmony_ci virtual void TearDown() 40f6603c60Sopenharmony_ci { 41f6603c60Sopenharmony_ci } 42f6603c60Sopenharmony_ci}; 43f6603c60Sopenharmony_ci 44f6603c60Sopenharmony_ci/** 45f6603c60Sopenharmony_ci* @tc.number SUB_KERNEL_UTIL_MATH_DRAND48_0100 46f6603c60Sopenharmony_ci* @tc.name test drand48 api 47f6603c60Sopenharmony_ci* @tc.desc [C- SOFTWARE -0200] 48f6603c60Sopenharmony_ci*/ 49f6603c60Sopenharmony_ciHWTEST_F(ActsUtilMathApiTest, testDrand480100, Function | MediumTest | Level1) { 50f6603c60Sopenharmony_ci double returnVal; 51f6603c60Sopenharmony_ci long seedVal; 52f6603c60Sopenharmony_ci 53f6603c60Sopenharmony_ci seedVal = 12345; 54f6603c60Sopenharmony_ci srand48(seedVal); 55f6603c60Sopenharmony_ci returnVal = drand48(); 56f6603c60Sopenharmony_ci LOGD(" drand48 returnVal:='%f'\n", returnVal); 57f6603c60Sopenharmony_ci ASSERT_TRUE(returnVal >= 0.0 && returnVal < 1.0) << "ErrInfo: drand48 returnVal:='" << returnVal << "'"; 58f6603c60Sopenharmony_ci} 59f6603c60Sopenharmony_ci 60f6603c60Sopenharmony_ci/** 61f6603c60Sopenharmony_ci* @tc.number SUB_KERNEL_UTIL_MATH_ERAND48_0200 62f6603c60Sopenharmony_ci* @tc.name test erand48 api 63f6603c60Sopenharmony_ci* @tc.desc [C- SOFTWARE -0200] 64f6603c60Sopenharmony_ci*/ 65f6603c60Sopenharmony_ciHWTEST_F(ActsUtilMathApiTest, testErand480200, Function | MediumTest | Level1) { 66f6603c60Sopenharmony_ci double returnVal; 67f6603c60Sopenharmony_ci unsigned short xsubi[3] = {0, 1, 2}; 68f6603c60Sopenharmony_ci 69f6603c60Sopenharmony_ci returnVal = erand48(xsubi); 70f6603c60Sopenharmony_ci LOGD(" erand48 returnVal:='%f'\n", returnVal); 71f6603c60Sopenharmony_ci ASSERT_TRUE(returnVal >= 0.0 && returnVal < 1.0) << "ErrInfo: erand48 returnVal:='" << returnVal << "'"; 72f6603c60Sopenharmony_ci} 73f6603c60Sopenharmony_ci 74f6603c60Sopenharmony_ci/** 75f6603c60Sopenharmony_ci* @tc.number SUB_KERNEL_UTIL_MATH_JRAND48_0300 76f6603c60Sopenharmony_ci* @tc.name test jrand48 api 77f6603c60Sopenharmony_ci* @tc.desc [C- SOFTWARE -0200] 78f6603c60Sopenharmony_ci*/ 79f6603c60Sopenharmony_ciHWTEST_F(ActsUtilMathApiTest, testJrand480300, Function | MediumTest | Level1) { 80f6603c60Sopenharmony_ci long long returnVal; 81f6603c60Sopenharmony_ci unsigned short xsubi[3] = {0, 1, 2}; 82f6603c60Sopenharmony_ci 83f6603c60Sopenharmony_ci returnVal = jrand48(xsubi); 84f6603c60Sopenharmony_ci LOGD(" jrand48 returnVal:='%ld'\n", returnVal); 85f6603c60Sopenharmony_ci ASSERT_TRUE(returnVal >= -2147483648 && returnVal < 2147483648) 86f6603c60Sopenharmony_ci << "ErrInfo: jrand48 returnVal:='" << returnVal << "'"; 87f6603c60Sopenharmony_ci} 88f6603c60Sopenharmony_ci 89f6603c60Sopenharmony_ci/** 90f6603c60Sopenharmony_ci* @tc.number SUB_KERNEL_UTIL_MATH_MRAND48_0400 91f6603c60Sopenharmony_ci* @tc.name test mrand48 api 92f6603c60Sopenharmony_ci* @tc.desc [C- SOFTWARE -0200] 93f6603c60Sopenharmony_ci*/ 94f6603c60Sopenharmony_ciHWTEST_F(ActsUtilMathApiTest, testMrand480400, Function | MediumTest | Level1) { 95f6603c60Sopenharmony_ci long long returnVal; 96f6603c60Sopenharmony_ci unsigned short paraVal[7] = {0, 1, 2, 3, 4, 5, 6}; 97f6603c60Sopenharmony_ci 98f6603c60Sopenharmony_ci lcong48(paraVal); 99f6603c60Sopenharmony_ci returnVal = mrand48(); 100f6603c60Sopenharmony_ci LOGD(" mrand48 returnVal:='%ld'\n", returnVal); 101f6603c60Sopenharmony_ci ASSERT_TRUE(returnVal >= -2147483648 && returnVal < 2147483648) 102f6603c60Sopenharmony_ci << "ErrInfo: mrand48 returnVal:='" << returnVal << "'"; 103f6603c60Sopenharmony_ci} 104f6603c60Sopenharmony_ci 105f6603c60Sopenharmony_ci/** 106f6603c60Sopenharmony_ci* @tc.number SUB_KERNEL_UTIL_MATH_LRAND48_0500 107f6603c60Sopenharmony_ci* @tc.name test lrand48 api 108f6603c60Sopenharmony_ci* @tc.desc [C- SOFTWARE -0200] 109f6603c60Sopenharmony_ci*/ 110f6603c60Sopenharmony_ciHWTEST_F(ActsUtilMathApiTest, testLrand480500, Function | MediumTest | Level1) { 111f6603c60Sopenharmony_ci long long returnVal; 112f6603c60Sopenharmony_ci unsigned short paraVal[3] = {0, 1, 2}; 113f6603c60Sopenharmony_ci 114f6603c60Sopenharmony_ci seed48(paraVal); 115f6603c60Sopenharmony_ci returnVal = lrand48(); 116f6603c60Sopenharmony_ci LOGD(" lrand48 returnVal:='%ld'\n", returnVal); 117f6603c60Sopenharmony_ci ASSERT_TRUE(returnVal >= 0 && returnVal < 2147483648) << "ErrInfo: lrand48 returnVal:='" << returnVal << "'"; 118f6603c60Sopenharmony_ci} 119f6603c60Sopenharmony_ci 120f6603c60Sopenharmony_ci/** 121f6603c60Sopenharmony_ci* @tc.number SUB_KERNEL_UTIL_MATH_SRANDOM_0600 122f6603c60Sopenharmony_ci* @tc.name test srandom api 123f6603c60Sopenharmony_ci* @tc.desc [C- SOFTWARE -0200] 124f6603c60Sopenharmony_ci*/ 125f6603c60Sopenharmony_ciHWTEST_F(ActsUtilMathApiTest, testSrandom0600, Function | MediumTest | Level1) { 126f6603c60Sopenharmony_ci char *returnVal = nullptr; 127f6603c60Sopenharmony_ci unsigned int seedVal; 128f6603c60Sopenharmony_ci char stateVal[8]; 129f6603c60Sopenharmony_ci size_t stateSize; 130f6603c60Sopenharmony_ci 131f6603c60Sopenharmony_ci seedVal = 1; 132f6603c60Sopenharmony_ci stateSize = 8; 133f6603c60Sopenharmony_ci srandom(seedVal); 134f6603c60Sopenharmony_ci returnVal = initstate(seedVal, stateVal, stateSize); 135f6603c60Sopenharmony_ci LOGD(" initstate returnVal:='%x'\n", returnVal); 136f6603c60Sopenharmony_ci ASSERT_TRUE(returnVal != nullptr) << "ErrInfo: srandom returnVal:='" << returnVal << "'"; 137f6603c60Sopenharmony_ci returnVal = setstate(stateVal); 138f6603c60Sopenharmony_ci LOGD(" setstate returnVal:='%x'\n", returnVal); 139f6603c60Sopenharmony_ci ASSERT_TRUE(returnVal != nullptr) << "ErrInfo: srandom returnVal:='" << returnVal << "'"; 140f6603c60Sopenharmony_ci} 141f6603c60Sopenharmony_ci 142f6603c60Sopenharmony_ci/** 143f6603c60Sopenharmony_ci* @tc.number SUB_KERNEL_UTIL_MATH_NRAND48_0700 144f6603c60Sopenharmony_ci* @tc.name test nrand48 api 145f6603c60Sopenharmony_ci* @tc.desc [C- SOFTWARE -0200] 146f6603c60Sopenharmony_ci*/ 147f6603c60Sopenharmony_ciHWTEST_F(ActsUtilMathApiTest, testNrand480700, Function | MediumTest | Level1) { 148f6603c60Sopenharmony_ci long long returnVal; 149f6603c60Sopenharmony_ci unsigned short paraVal[3] = {0, 1, 2}; 150f6603c60Sopenharmony_ci 151f6603c60Sopenharmony_ci returnVal = nrand48(paraVal); 152f6603c60Sopenharmony_ci LOGD(" nrand48 returnVal:='%ld'\n", returnVal); 153f6603c60Sopenharmony_ci ASSERT_TRUE(returnVal >= 0 && returnVal < 2147483648) << "ErrInfo: nrand48 returnVal:='" << returnVal << "'"; 154f6603c60Sopenharmony_ci} 155f6603c60Sopenharmony_ci 156f6603c60Sopenharmony_ci/** 157f6603c60Sopenharmony_ci* @tc.number SUB_KERNEL_UTIL_MATH_RAND_R_0800 158f6603c60Sopenharmony_ci* @tc.name test rand_r api 159f6603c60Sopenharmony_ci* @tc.desc [C- SOFTWARE -0200] 160f6603c60Sopenharmony_ci*/ 161f6603c60Sopenharmony_ciHWTEST_F(ActsUtilMathApiTest, testNrand480800, Function | MediumTest | Level1) { 162f6603c60Sopenharmony_ci int returnVal; 163f6603c60Sopenharmony_ci unsigned int paraVal; 164f6603c60Sopenharmony_ci 165f6603c60Sopenharmony_ci returnVal = rand_r(¶Val); 166f6603c60Sopenharmony_ci LOGD(" rand_r returnVal:='%ld'\n", returnVal); 167f6603c60Sopenharmony_ci ASSERT_TRUE(returnVal >= 0 && returnVal <= RAND_MAX) << "ErrInfo: rand_r returnVal:='" << returnVal << "'"; 168f6603c60Sopenharmony_ci} 169