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