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