1 /*
2 * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3 * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this list of
9 * conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 * of conditions and the following disclaimer in the documentation and/or other materials
13 * provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16 * to endorse or promote products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <float.h>
33 #include <math.h>
34 #include "ohos_types.h"
35 #include "posix_test.h"
36 #include "los_config.h"
37 #include "kernel_test.h"
38 #include "log.h"
39
40 #define RET_OK 0
41 #define TEST_VALUE_X 0
42 #define TEST_VALUE_Y 1
43 #define TEST_EXPECTED 2
44
DoubleEquals(double a, double b)45 int DoubleEquals(double a, double b)
46 {
47 if (a == INFINITY && b == INFINITY) {
48 return 1;
49 }
50 if (a == -INFINITY && b == -INFINITY) {
51 return 1;
52 }
53 if (a == INFINITY && b != INFINITY) {
54 return 0;
55 }
56 if (a == -INFINITY && b != -INFINITY) {
57 return 0;
58 }
59 if (isnan(a) && isnan(b)) {
60 return 1;
61 }
62 if (isnan(a) || isnan(b)) {
63 return 0;
64 }
65
66 return fabs(a - b) < DBL_EPSILON;
67 }
68
69 /* *
70 * @tc.desc : register a test suite, this suite is used to test basic flow and interface dependency
71 * @param : subsystem name is utils
72 * @param : module name is utilsFile
73 * @param : test suit name is PosixMathFuncTestSuite
74 */
75 LITE_TEST_SUIT(Posix, Posixmath, PosixMathFuncTestSuite);
76
77 /* *
78 * @tc.setup : setup for all testcases
79 * @return : setup result, TRUE is success, FALSE is fail
80 */
PosixMathFuncTestSuiteSetUp(void)81 static BOOL PosixMathFuncTestSuiteSetUp(void)
82 {
83 return TRUE;
84 }
85
86 /* *
87 * @tc.teardown : teardown for all testcases
88 * @return : teardown result, TRUE is success, FALSE is fail
89 */
PosixMathFuncTestSuiteTearDown(void)90 static BOOL PosixMathFuncTestSuiteTearDown(void)
91 {
92 LOG("+-------------------------------------------+\n");
93 return TRUE;
94 }
95
96 /* *
97 * @tc.number SUB_KERNEL_MATH_ABS_001
98 * @tc.name test abs api
99 * @tc.desc [C- SOFTWARE -0100]
100 */
101 LITE_TEST_CASE(PosixMathFuncTestSuite, testMathAbs001, Function | MediumTest | Level1)
102 {
103 const int testCount = 3;
104 int testValues[] = {-3, 0, 3};
105 int expected[] = {3, 0, 3};
106 int ret;
107 for (int i = 0; i < testCount; ++i) {
108 ret = abs(testValues[i]);
109 LOG("\n [POSIXTEST][abs]abs(%d) = %d, expected is %d", testValues[i], ret, expected[i]);
110 ICUNIT_ASSERT_EQUAL(ret, expected[i], ret);
111 }
112 return 0;
113 };
114
115 /* *
116 * @tc.number SUB_KERNEL_MATH_ABS_002
117 * @tc.name test abs api for boundary value
118 * @tc.desc [C- SOFTWARE -0100]
119 */
120 LITE_TEST_CASE(PosixMathFuncTestSuite, testMathAbs002, Function | MediumTest | Level1)
121 {
122 const int testCount = 3;
123 int testValues[] = {-2147483648, -2147483647, 2147483647};
124 int expected[] = {-2147483648, 2147483647, 2147483647};
125 int ret;
126 for (int i = 0; i < testCount; ++i) {
127 ret = abs(testValues[i]);
128 LOG("\n [POSIXTEST][abs]abs(%d) = %d, expected is %d", testValues[i], ret, expected[i]);
129 ICUNIT_ASSERT_EQUAL(ret, expected[i], ret);
130 }
131 return 0;
132 };
133
134 /* *
135 * @tc.number SUB_KERNEL_MATH_LOG_001
136 * @tc.name log basic function test
137 * @tc.desc [C- SOFTWARE -0100]
138 */
139 LITE_TEST_CASE(PosixMathFuncTestSuite, testMathLog001, Function | MediumTest | Level1)
140 {
141 const int testCount = 3;
142 double testValues[] = { 0.5, 5.5, 1};
143 double expected[] = { -0.69314718055994528623, 1.70474809223842527217, 0.00000000000000000000};
144 double ret;
145 PRINT_EMG("GZHTESTLOG PRINT_EMG: %lf, %lf, %lf", testValues[0], testValues[1], testValues[2]);
146 LOG("GZHTESTLOG LOG: %lf, %lf, %lf", testValues[0], testValues[1], testValues[2]);
147 for (int i = 0; i < testCount; ++i) {
148 ret = log(testValues[i]);
149 LOG("\n [POSIXTEST][log]log(%lf) = %lf, expected is %lf", testValues[i], ret, expected[i]);
150 TEST_ASSERT_EQUAL_FLOAT(expected[i], ret);
151 ICUNIT_ASSERT_EQUAL(DoubleEquals(expected[i], ret), TRUE, 0);
152 }
153 return 0;
154 };
155
156 /* *
157 * @tc.number SUB_KERNEL_MATH_LOG_002
158 * @tc.name log function test for invalid input
159 * @tc.desc [C- SOFTWARE -0100]
160 */
161 LITE_TEST_CASE(PosixMathFuncTestSuite, testMathLog002, Function | MediumTest | Level1)
162 {
163 const int testCount = 4;
164 double testValues[] = { 0, -INFINITY, -2.0, NAN};
165 double expected[] = { -INFINITY, NAN, NAN, NAN};
166 double ret;
167
168 LOG("\n (math_errhandling & MATH_ERRNO) = %d", (math_errhandling & MATH_ERRNO));
169 LOG("\n (math_errhandling & MATH_ERREXCEPT) = %d", (math_errhandling & MATH_ERREXCEPT));
170
171 for (int i = 0; i < testCount; ++i) {
172 ret = log(testValues[i]);
173 LOG("\n [POSIXTEST][log]log(%lf) = %lf, expected is %lf", testValues[i], ret, expected[i]);
174 TEST_ASSERT_EQUAL_FLOAT(expected[i], ret);
175 }
176 return 0;
177 };
178
179 /* *
180 * @tc.number SUB_KERNEL_MATH_SQRT_001
181 * @tc.name sqrt basic function test
182 * @tc.desc [C- SOFTWARE -0100]
183 */
184 LITE_TEST_CASE(PosixMathFuncTestSuite, testMathSqrt001, Function | MediumTest | Level1)
185 {
186 const int testCount = 3;
187 double testValues[] = { 4, 3, 10 };
188 double expected[] = { 2.00000000000000000000, 1.73205080756887719318, 3.16227766016837952279 };
189 double ret;
190 for (int i = 0; i < testCount; ++i) {
191 ret = sqrt(testValues[i]);
192 LOG("\n [POSIXTEST][sqrt]sqrt(%lf) = %lf, expected is %lf", testValues[i], ret, expected[i]);
193 TEST_ASSERT_EQUAL_FLOAT(expected[i], ret);
194 ICUNIT_ASSERT_EQUAL(DoubleEquals(expected[i], ret), TRUE, 0);
195 }
196 return 0;
197 };
198
199 /* *
200 * @tc.number SUB_KERNEL_MATH_SQRT_002
201 * @tc.name sqrt function test for invalid input
202 * @tc.desc [C- SOFTWARE -0100]
203 */
204 LITE_TEST_CASE(PosixMathFuncTestSuite, testMathSqrt002, Function | MediumTest | Level1)
205 {
206 const int testCount = 5;
207 double testValues[] = { 0, INFINITY, -2.0, -INFINITY, NAN };
208 double expected[] = { 0.00000000000000000000, INFINITY, NAN, NAN, NAN};
209 double ret;
210 for (int i = 0; i < testCount; ++i) {
211 ret = sqrt(testValues[i]);
212 LOG("\n [POSIXTEST][sqrt]sqrt(%lf) = %lf, expected is %lf", testValues[i], ret, expected[i]);
213 TEST_ASSERT_EQUAL_FLOAT(expected[i], ret);
214 }
215 return 0;
216 };
217
218 /* *
219 * @tc.number SUB_KERNEL_MATH_POW_001
220 * @tc.name pow basic function test
221 * @tc.desc [C- SOFTWARE -0100]
222 */
223 LITE_TEST_CASE(PosixMathFuncTestSuite, testMathPow001, Function | MediumTest | Level1)
224 {
225 double testValues[][TEST_EXPECTED + 1] = {
226 {1, 1.12, 1.00000000000000000000},
227 {2.8, 2.14, 9.0556199394902243682},
228 {3.6, 3.16, 57.26851244563656706532},
229 {678.88, 0, 1.00000000000000000000}
230 };
231
232 const int testCount = sizeof(testValues) / (sizeof(double) * 3);
233 double ret;
234 for (int i = 0; i < testCount; ++i) {
235 ret = pow(testValues[i][TEST_VALUE_X], testValues[i][TEST_VALUE_Y]);
236 LOG("\n [POSIXTEST][pow]pow1(%lf,%lf) = %lf, expected is %lf", testValues[i][TEST_VALUE_X],
237 testValues[i][TEST_VALUE_Y], ret, testValues[i][TEST_EXPECTED]);
238 TEST_ASSERT_EQUAL_FLOAT(testValues[i][TEST_EXPECTED], ret);
239 ICUNIT_ASSERT_EQUAL(DoubleEquals(testValues[i][TEST_EXPECTED], ret), TRUE, 0);
240 }
241 return 0;
242 };
243
244
245 /* *
246 * @tc.number SUB_KERNEL_MATH_POW_002
247 * @tc.name pow basic function test for range input
248 * @tc.desc [C- SOFTWARE -0100]
249 */
250 LITE_TEST_CASE(PosixMathFuncTestSuite, testMathPow002, Function | MediumTest | Level1)
251 {
252 double testValues[][TEST_EXPECTED + 1] = {
253 {1, NAN, 1.00000000000000000000},
254 {-1, -INFINITY, 1.00000000000000000000},
255 {-0.5, -INFINITY, INFINITY},
256 {3.5, -INFINITY, 0},
257 {0.5, INFINITY, 0},
258 {-3.5, INFINITY, INFINITY},
259 {-INFINITY, -5, -0.00000000000000000000},
260 {-INFINITY, -4.37, 0.00000000000000000000},
261 {-INFINITY, 7, -INFINITY},
262 {-INFINITY, 4, INFINITY},
263 {INFINITY, -4, 0.00000000000000000000},
264 {INFINITY, 4, INFINITY}
265 };
266
267 const int testCount = sizeof(testValues) / (sizeof(double) * 3);
268 double ret;
269 for (int i = 0; i < testCount; ++i) {
270 ret = pow(testValues[i][TEST_VALUE_X], testValues[i][TEST_VALUE_Y]);
271 TEST_ASSERT_EQUAL_FLOAT(testValues[i][TEST_EXPECTED], ret);
272 ICUNIT_ASSERT_EQUAL(DoubleEquals(testValues[i][TEST_EXPECTED], ret), TRUE, 0);
273 LOG("\n [POSIXTEST][pow]pow1(%lf,%lf) = %lf, expected is %lf", testValues[i][TEST_VALUE_X],
274 testValues[i][TEST_VALUE_Y], ret, testValues[i][TEST_EXPECTED]);
275 }
276 return 0;
277 };
278
279 /* *
280 * @tc.number SUB_KERNEL_MATH_POW_003
281 * @tc.name pow basic function test for invalid input
282 * @tc.desc [C- SOFTWARE -0100]
283 */
284 LITE_TEST_CASE(PosixMathFuncTestSuite, testMathPow003, Function | MediumTest | Level1)
285 {
286 double testValues[][TEST_EXPECTED + 1] = {
287 {0.0, -7, HUGE_VAL},
288 {-0.0, -6.22, HUGE_VAL},
289 {-7.23, 3.57, NAN},
290 {121223, 5674343, HUGE_VAL}
291 };
292
293 const int testCount = sizeof(testValues) / (sizeof(double) * 3);
294 double ret;
295 for (int i = 0; i < testCount; ++i) {
296 ret = pow(testValues[i][TEST_VALUE_X], testValues[i][TEST_VALUE_Y]);
297 TEST_ASSERT_EQUAL_FLOAT(testValues[i][TEST_EXPECTED], ret);
298 LOG("\n [POSIXTEST][pow]pow1(%lf,%lf) = %lf, expected is %lf", testValues[i][TEST_VALUE_X],
299 testValues[i][TEST_VALUE_Y], ret, testValues[i][TEST_EXPECTED]);
300 ICUNIT_ASSERT_EQUAL(DoubleEquals(testValues[i][TEST_EXPECTED], ret), TRUE, 0);
301 }
302 return 0;
303 };
304
305 /* *
306 * @tc.number SUB_KERNEL_MATH_ROUND_001
307 * @tc.name round basic function test
308 * @tc.desc [C- SOFTWARE -0100]
309 */
310 LITE_TEST_CASE(PosixMathFuncTestSuite, testMathRound001, Function | MediumTest | Level1)
311 {
312 double testValues[] = { -0.5, 2.5, -3.812345679812345, -0.125, 0.125};
313 double expected[] = { -1.00000000000000000000, 3.00000000000000000000, -4.00000000000000000000,
314 0.00000000000000000000, 0.00000000000000000000};
315 const int testCount = sizeof(testValues) / sizeof(double);
316 double ret;
317 for (int i = 0; i < testCount; ++i) {
318 ret = round(testValues[i]);
319 LOG("\n [POSIXTEST][round]round1(%lf) = %lf, expected is %lf", testValues[i], ret, expected[i]);
320 TEST_ASSERT_EQUAL_FLOAT(expected[i], ret);
321 ICUNIT_ASSERT_EQUAL(DoubleEquals(expected[i], ret), TRUE, 0);
322 }
323 return 0;
324 };
325
326 /* *
327 * @tc.number SUB_KERNEL_MATH_ROUND_002
328 * @tc.name round basic function test
329 * @tc.desc [C- SOFTWARE -0100]
330 */
331 LITE_TEST_CASE(PosixMathFuncTestSuite, testMathRound002, Function | MediumTest | Level1)
332 {
333 double testValues[] = { NAN, -INFINITY, INFINITY, 0};
334 double expected[] = { NAN, -INFINITY, INFINITY, 0.00000000000000000000};
335 const int testCount = sizeof(testValues) / sizeof(double);
336 double ret;
337 for (int i = 0; i < testCount; ++i) {
338 ret = round(testValues[i]);
339 LOG("\n [POSIXTEST][round]round1(%lf) = %lf, expected is %lf", testValues[i], ret, expected[i]);
340 TEST_ASSERT_EQUAL_FLOAT(expected[i], ret);
341 ICUNIT_ASSERT_EQUAL(DoubleEquals(expected[i], ret), TRUE, 0);
342 }
343 return 0;
344 };
345
346 RUN_TEST_SUITE(PosixMathFuncTestSuite);
347
PosixMathFuncTestnull348 void PosixMathFuncTest()
349 {
350 LOG("begin PosixMathFuncTest....");
351 RUN_ONE_TESTCASE(testMathAbs001);
352 RUN_ONE_TESTCASE(testMathAbs002);
353 RUN_ONE_TESTCASE(testMathLog001);
354 RUN_ONE_TESTCASE(testMathLog002);
355 RUN_ONE_TESTCASE(testMathSqrt001);
356 RUN_ONE_TESTCASE(testMathSqrt002);
357 RUN_ONE_TESTCASE(testMathPow001);
358 RUN_ONE_TESTCASE(testMathPow002);
359 RUN_ONE_TESTCASE(testMathPow003);
360 RUN_ONE_TESTCASE(testMathRound001);
361 RUN_ONE_TESTCASE(testMathRound002);
362
363 return;
364 }