• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "hctest.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         TEST_ASSERT_EQUAL_INT(expected[i], ret);
111     }
112 };
113 
114 /* *
115  * @tc.number     SUB_KERNEL_MATH_ABS_002
116  * @tc.name       test abs api for boundary value
117  * @tc.desc       [C- SOFTWARE -0100]
118  */
119 LITE_TEST_CASE(PosixMathFuncTestSuite, testMathAbs002, Function | MediumTest | Level1)
120 {
121     const int testCount = 3;
122     int testValues[] = {-2147483648, -2147483647, 2147483647};
123     int expected[] = {-2147483648, 2147483647, 2147483647};
124     int ret;
125     for (int i = 0; i < testCount; ++i) {
126         ret = abs(testValues[i]);
127         LOG("\n [POSIXTEST][abs]abs(%d) = %d, expected is %d", testValues[i], ret, expected[i]);
128         TEST_ASSERT_EQUAL_INT(expected[i], ret);
129     }
130 };
131 
132 /* *
133  * @tc.number     SUB_KERNEL_MATH_LOG_001
134  * @tc.name       log basic function test
135  * @tc.desc       [C- SOFTWARE -0100]
136  */
137 LITE_TEST_CASE(PosixMathFuncTestSuite, testMathLog001, Function | MediumTest | Level1)
138 {
139     const int testCount = 3;
140     double testValues[] = { 0.5, 5.5, 1};
141     double expected[] = { -0.69314718055994528623, 1.70474809223842527217, 0.00000000000000000000};
142     double ret;
143     PRINT_EMG("GZHTESTLOG PRINT_EMG: %f, %f, %f", testValues[0], testValues[1], testValues[2]);
144     LOG("GZHTESTLOG LOG: %f, %f, %f", testValues[0], testValues[1], testValues[2]);
145     for (int i = 0; i < testCount; ++i) {
146         ret = log(testValues[i]);
147         LOG("\n [POSIXTEST][log]log(%f) = %f, expected is %f", testValues[i], ret, expected[i]);
148         TEST_ASSERT_EQUAL_FLOAT(expected[i], ret);
149         TEST_ASSERT_TRUE(DoubleEquals(expected[i], ret));
150     }
151 };
152 
153 /* *
154  * @tc.number     SUB_KERNEL_MATH_LOG_002
155  * @tc.name       log function test for invalid input
156  * @tc.desc       [C- SOFTWARE -0100]
157  */
158 LITE_TEST_CASE(PosixMathFuncTestSuite, testMathLog002, Function | MediumTest | Level1)
159 {
160     const int testCount = 4;
161     double testValues[] = { 0, -INFINITY, -2.0, NAN};
162     double expected[] = { -INFINITY, NAN, NAN, NAN};
163     double ret;
164 
165     LOG("\n (math_errhandling & MATH_ERRNO) = %d", (math_errhandling & MATH_ERRNO));
166     LOG("\n (math_errhandling & MATH_ERREXCEPT) = %d", (math_errhandling & MATH_ERREXCEPT));
167 
168     for (int i = 0; i < testCount; ++i) {
169         ret = log(testValues[i]);
170         LOG("\n [POSIXTEST][log]log(%f) = %f, expected is %f", testValues[i], ret, expected[i]);
171         TEST_ASSERT_EQUAL_FLOAT(expected[i], ret);
172     }
173 };
174 
175 /* *
176  * @tc.number     SUB_KERNEL_MATH_SQRT_001
177  * @tc.name       sqrt basic function test
178  * @tc.desc       [C- SOFTWARE -0100]
179  */
180 LITE_TEST_CASE(PosixMathFuncTestSuite, testMathSqrt001, Function | MediumTest | Level1)
181 {
182     const int testCount = 3;
183     double testValues[] = { 4, 3, 10 };
184     double expected[] = { 2.00000000000000000000, 1.73205080756887719318, 3.16227766016837952279 };
185     double ret;
186     for (int i = 0; i < testCount; ++i) {
187         ret = sqrt(testValues[i]);
188         LOG("\n [POSIXTEST][sqrt]sqrt(%f) = %f, expected is %f", testValues[i], ret, expected[i]);
189         TEST_ASSERT_EQUAL_FLOAT(expected[i], ret);
190         TEST_ASSERT_TRUE(DoubleEquals(expected[i], ret));
191     }
192 };
193 
194 /* *
195  * @tc.number     SUB_KERNEL_MATH_SQRT_002
196  * @tc.name       sqrt function test for invalid input
197  * @tc.desc       [C- SOFTWARE -0100]
198  */
199 LITE_TEST_CASE(PosixMathFuncTestSuite, testMathSqrt002, Function | MediumTest | Level1)
200 {
201     const int testCount = 5;
202     double testValues[] = { 0, INFINITY, -2.0, -INFINITY, NAN };
203     double expected[] = { 0.00000000000000000000, INFINITY, NAN, NAN, NAN};
204     double ret;
205     for (int i = 0; i < testCount; ++i) {
206         ret = sqrt(testValues[i]);
207         LOG("\n [POSIXTEST][sqrt]sqrt(%f) = %f, expected is %f", testValues[i], ret, expected[i]);
208         TEST_ASSERT_EQUAL_FLOAT(expected[i], ret);
209     }
210 };
211 
212 /* *
213  * @tc.number     SUB_KERNEL_MATH_POW_001
214  * @tc.name       pow basic function test
215  * @tc.desc       [C- SOFTWARE -0100]
216  */
217 LITE_TEST_CASE(PosixMathFuncTestSuite, testMathPow001, Function | MediumTest | Level1)
218 {
219     double testValues[][TEST_EXPECTED + 1] = {
220         {1, 1.12, 1.00000000000000000000},
221         {2.8, 2.14, 9.0556199394902243682},
222         {3.6, 3.16, 57.26851244563656706532},
223         {678.88, 0, 1.00000000000000000000}
224     };
225 
226     const int testCount = sizeof(testValues) / (sizeof(double) * 3);
227     double ret;
228     for (int i = 0; i < testCount; ++i) {
229         ret = pow(testValues[i][TEST_VALUE_X], testValues[i][TEST_VALUE_Y]);
230         LOG("\n [POSIXTEST][pow]pow1(%f,%f) = %f, expected is %f", testValues[i][TEST_VALUE_X],
231             testValues[i][TEST_VALUE_Y], ret, testValues[i][TEST_EXPECTED]);
232         TEST_ASSERT_EQUAL_FLOAT(testValues[i][TEST_EXPECTED], ret);
233         TEST_ASSERT_TRUE(DoubleEquals(testValues[i][TEST_EXPECTED], ret));
234     }
235 };
236 
237 
238 /* *
239  * @tc.number     SUB_KERNEL_MATH_POW_002
240  * @tc.name       pow basic function test for range input
241  * @tc.desc       [C- SOFTWARE -0100]
242  */
243 LITE_TEST_CASE(PosixMathFuncTestSuite, testMathPow002, Function | MediumTest | Level1)
244 {
245     double testValues[][TEST_EXPECTED + 1] = {
246         {1, NAN, 1.00000000000000000000},
247         {-1, -INFINITY, 1.00000000000000000000},
248         {-0.5, -INFINITY, INFINITY},
249         {3.5, -INFINITY, 0},
250         {0.5, INFINITY, 0},
251         {-3.5, INFINITY, INFINITY},
252         {-INFINITY, -5, -0.00000000000000000000},
253         {-INFINITY, -4.37, 0.00000000000000000000},
254         {-INFINITY, 7, -INFINITY},
255         {-INFINITY, 4, INFINITY},
256         {INFINITY, -4, 0.00000000000000000000},
257         {INFINITY, 4, INFINITY}
258     };
259 
260     const int testCount = sizeof(testValues) / (sizeof(double) * 3);
261     double ret;
262     for (int i = 0; i < testCount; ++i) {
263         ret = pow(testValues[i][TEST_VALUE_X], testValues[i][TEST_VALUE_Y]);
264         TEST_ASSERT_EQUAL_FLOAT(testValues[i][TEST_EXPECTED], ret);
265         TEST_ASSERT_TRUE(DoubleEquals(testValues[i][TEST_EXPECTED], ret));
266         LOG("\n [POSIXTEST][pow]pow1(%f,%f) = %f, expected is %f", testValues[i][TEST_VALUE_X],
267             testValues[i][TEST_VALUE_Y], ret, testValues[i][TEST_EXPECTED]);
268     }
269 };
270 
271 /* *
272  * @tc.number     SUB_KERNEL_MATH_POW_003
273  * @tc.name       pow basic function test for invalid input
274  * @tc.desc       [C- SOFTWARE -0100]
275  */
276 LITE_TEST_CASE(PosixMathFuncTestSuite, testMathPow003, Function | MediumTest | Level1)
277 {
278     double testValues[][TEST_EXPECTED + 1] = {
279         {0.0, -7, HUGE_VAL},
280         {-0.0, -6.22, HUGE_VAL},
281         {-7.23, 3.57, NAN},
282         {121223, 5674343, HUGE_VAL}
283     };
284 
285     const int testCount = sizeof(testValues) / (sizeof(double) * 3);
286     double ret;
287     for (int i = 0; i < testCount; ++i) {
288         ret = pow(testValues[i][TEST_VALUE_X], testValues[i][TEST_VALUE_Y]);
289         TEST_ASSERT_EQUAL_FLOAT(testValues[i][TEST_EXPECTED], ret);
290         LOG("\n [POSIXTEST][pow]pow1(%f,%f) = %f, expected is %f", testValues[i][TEST_VALUE_X],
291             testValues[i][TEST_VALUE_Y], ret, testValues[i][TEST_EXPECTED]);
292         TEST_ASSERT_TRUE(DoubleEquals(testValues[i][TEST_EXPECTED], ret));
293     }
294 };
295 
296 /* *
297  * @tc.number     SUB_KERNEL_MATH_ROUND_001
298  * @tc.name       round basic function test
299  * @tc.desc       [C- SOFTWARE -0100]
300  */
301 LITE_TEST_CASE(PosixMathFuncTestSuite, testMathRound001, Function | MediumTest | Level1)
302 {
303     double testValues[] = { -0.5, 2.5, -3.812345679812345, -0.125, 0.125};
304     double expected[] = { -1.00000000000000000000, 3.00000000000000000000, -4.00000000000000000000,
305         0.00000000000000000000, 0.00000000000000000000};
306     const int testCount = sizeof(testValues) / sizeof(double);
307     double ret;
308     for (int i = 0; i < testCount; ++i) {
309         ret = round(testValues[i]);
310         LOG("\n [POSIXTEST][round]round1(%f) = %f, expected is %f", testValues[i], ret, expected[i]);
311         TEST_ASSERT_EQUAL_FLOAT(expected[i], ret);
312         TEST_ASSERT_TRUE(DoubleEquals(expected[i], ret));
313     }
314 };
315 
316 /* *
317  * @tc.number     SUB_KERNEL_MATH_ROUND_002
318  * @tc.name       round basic function test
319  * @tc.desc       [C- SOFTWARE -0100]
320  */
321 LITE_TEST_CASE(PosixMathFuncTestSuite, testMathRound002, Function | MediumTest | Level1)
322 {
323     double testValues[] = { NAN, -INFINITY, INFINITY, 0};
324     double expected[] = { NAN, -INFINITY, INFINITY, 0.00000000000000000000};
325     const int testCount = sizeof(testValues) / sizeof(double);
326     double ret;
327     for (int i = 0; i < testCount; ++i) {
328         ret = round(testValues[i]);
329         LOG("\n [POSIXTEST][round]round1(%f) = %f, expected is %f", testValues[i], ret, expected[i]);
330         TEST_ASSERT_EQUAL_FLOAT(expected[i], ret);
331         TEST_ASSERT_TRUE(DoubleEquals(expected[i], ret));
332     }
333 };
334 
335 RUN_TEST_SUITE(PosixMathFuncTestSuite);
336 
PosixMathFuncTest()337 void PosixMathFuncTest()
338 {
339     LOG("begin PosixMathFuncTest....");
340     RUN_ONE_TESTCASE(testMathAbs001);
341     RUN_ONE_TESTCASE(testMathAbs002);
342     RUN_ONE_TESTCASE(testMathLog001);
343     RUN_ONE_TESTCASE(testMathLog002);
344     RUN_ONE_TESTCASE(testMathSqrt001);
345     RUN_ONE_TESTCASE(testMathSqrt002);
346     RUN_ONE_TESTCASE(testMathPow001);
347     RUN_ONE_TESTCASE(testMathPow002);
348     RUN_ONE_TESTCASE(testMathPow003);
349     RUN_ONE_TESTCASE(testMathRound001);
350     RUN_ONE_TESTCASE(testMathRound002);
351 
352     return;
353 }