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 "ohos_types.h"
33 #include "posix_test.h"
34 #include "los_config.h"
35 #include "kernel_test.h"
36 #include "ctype.h"
37 #include "stdlib.h"
38 #include "string.h"
39 #include "log.h"
40
41 /* *
42 * @tc.desc : register a test suite, this suite is used to test basic flow and interface dependency
43 * @param : subsystem name is utils
44 * @param : module name is utilsFile
45 * @param : test suit name is CmsisTaskFuncTestSuite
46 */
47 LITE_TEST_SUIT(Posix, Posixtimer, PosixStdlibStrtolTest);
48
49 /* *
50 * @tc.setup : setup for all testcases
51 * @return : setup result, TRUE is success, FALSE is fail
52 */
PosixStdlibStrtolTestSetUp(void)53 static BOOL PosixStdlibStrtolTestSetUp(void)
54 {
55 return TRUE;
56 }
57
58 /* *
59 * @tc.teardown : teardown for all testcases
60 * @return : teardown result, TRUE is success, FALSE is fail
61 */
PosixStdlibStrtolTestTearDown(void)62 static BOOL PosixStdlibStrtolTestTearDown(void)
63 {
64 LOG("+-------------------------------------------+\n");
65 return TRUE;
66 }
67
68 /* *
69 * @tc.number : TEST_STDLIB_STRTOL_001
70 * @tc.name : convert string to long integer
71 * @tc.desc : [C- SOFTWARE -0200]
72 */
73 LITE_TEST_CASE(PosixStdlibStrtolTest, testStdlibStrtol001, Function | MediumTest | Level1)
74 {
75 char nPtr16[] = " 10";
76 char *endPtr16 = NULL;
77 long ret = strtol(nPtr16, &endPtr16, 16);
78 if (ret == 16) {
79 LOG("[DEMO] posix stdlib test case 1:strtol(base=16) ret:%ld,%s, endPtr:%s ok.\n", ret, nPtr16, endPtr16);
80 } else {
81 LOG("[DEMO] posix stdlib test case 1:strtol(base=16) ret:%ld,%s fail.\n", ret, nPtr16);
82 }
83 TEST_ASSERT_EQUAL_INT32(16, ret);
84 TEST_ASSERT_EQUAL_STRING(endPtr16, "");
85 return 0;
86 }
87
88 /* *
89 * @tc.number : TEST_STDLIB_STRTOL_002
90 * @tc.name : convert string to long integer
91 * @tc.desc : [C- SOFTWARE -0200]
92 */
93 LITE_TEST_CASE(PosixStdlibStrtolTest, testStdlibStrtol002, Function | MediumTest | Level1)
94 {
95 char nPtr16[] = "0x10";
96 char *endPtr16 = NULL;
97 long ret = strtol(nPtr16, &endPtr16, 0);
98 if (ret == 16) {
99 LOG("[DEMO] posix stdlib test case 2:strtol(base=16) ret:%ld,%s, endPtr:%s ok.\n", ret, nPtr16, endPtr16);
100 } else {
101 LOG("[DEMO] posix stdlib test case 2:strtol(base=16) ret:%ld,%s fail.\n", ret, nPtr16);
102 }
103 TEST_ASSERT_EQUAL_INT32(16, ret);
104 TEST_ASSERT_EQUAL_STRING(endPtr16, "");
105 return 0;
106 }
107
108 /* *
109 * @tc.number : TEST_STDLIB_STRTOL_003
110 * @tc.name : convert string to long integer
111 * @tc.desc : [C- SOFTWARE -0200]
112 */
113 LITE_TEST_CASE(PosixStdlibStrtolTest, testStdlibStrtol003, Function | MediumTest | Level1)
114 {
115 char nPtr10[] = "10";
116 char *endPtr10 = NULL;
117 long ret = strtol(nPtr10, &endPtr10, 10);
118 if (ret == 10) {
119 LOG("[DEMO] posix stdlib test case 3:strtol(base=10) ret:%ld,%s, endPtr:%s ok.\n", ret, nPtr10, endPtr10);
120 } else {
121 LOG("[DEMO] posix stdlib test case 3:strtol(base=10) ret:%ld,%s fail.\n", ret, nPtr10);
122 }
123 TEST_ASSERT_EQUAL_INT32(10, ret);
124 TEST_ASSERT_EQUAL_STRING(endPtr10, "");
125 return 0;
126 }
127
128 /* *
129 * @tc.number : TEST_STDLIB_STRTOL_004
130 * @tc.name : convert string to long integer
131 * @tc.desc : [C- SOFTWARE -0200]
132 */
133 LITE_TEST_CASE(PosixStdlibStrtolTest, testStdlibStrtol004, Function | MediumTest | Level1)
134 {
135 char nPtr10[] = "-10";
136 char *endPtr10 = NULL;
137 long ret = strtol(nPtr10, &endPtr10, 10);
138 if (ret == -10) {
139 LOG("[DEMO] posix stdlib test case 4:strtol(base=10) ret:%ld,%s, endPtr:%s ok.\n", ret, nPtr10, endPtr10);
140 } else {
141 LOG("[DEMO] posix stdlib test case 4:strtol(base=10) ret:%ld,%s fail.\n", ret, nPtr10);
142 }
143 TEST_ASSERT_EQUAL_INT32(-10, ret);
144 TEST_ASSERT_EQUAL_STRING(endPtr10, "");
145 return 0;
146 }
147
148 /* *
149 * @tc.number : TEST_STDLIB_STRTOL_005
150 * @tc.name : convert string to long integer
151 * @tc.desc : [C- SOFTWARE -0200]
152 */
153 LITE_TEST_CASE(PosixStdlibStrtolTest, testStdlibStrtol005, Function | MediumTest | Level1)
154 {
155 char nPtr10_3[] = "10";
156 char *endPtr10_3 = NULL;
157 long ret = strtol(nPtr10_3, &endPtr10_3, 0);
158 if (ret == 10) {
159 LOG("[DEMO] posix stdlib test case 5:strtol(base=0) ret:%ld,%s, endPtr:%s ok.\n", ret, nPtr10_3, endPtr10_3);
160 } else {
161 LOG("[DEMO] posix stdlib test case 5:strtol(base=0) ret:%ld,%s fail.\n", ret, nPtr10_3);
162 }
163 TEST_ASSERT_EQUAL_INT32(10, ret);
164 TEST_ASSERT_EQUAL_STRING(endPtr10_3, "");
165 return 0;
166 }
167
168 /* *
169 * @tc.number : TEST_STDLIB_STRTOL_006
170 * @tc.name : convert string to long integer
171 * @tc.desc : [C- SOFTWARE -0200]
172 */
173 LITE_TEST_CASE(PosixStdlibStrtolTest, testStdlibStrtol006, Function | MediumTest | Level1)
174 {
175 char nPtr8[] = "10";
176 char *endPtr8 = NULL;
177 long ret = strtol(nPtr8, &endPtr8, 8);
178 if (ret == 8) {
179 LOG("[DEMO] posix stdlib test case 6:strtol(base=8) ret:%ld,%s, endPtr:%s ok.\n", ret, nPtr8, endPtr8);
180 } else {
181 LOG("[DEMO] posix stdlib test case 6:strtol(base=8) ret:%ld,%s fail.\n", ret, nPtr8);
182 }
183 TEST_ASSERT_EQUAL_INT32(8, ret);
184 TEST_ASSERT_EQUAL_STRING(endPtr8, "");
185 return 0;
186 }
187
188 /* *
189 * @tc.number : TEST_STDLIB_STRTOL_007
190 * @tc.name : convert string to long integer
191 * @tc.desc : [C- SOFTWARE -0200]
192 */
193 LITE_TEST_CASE(PosixStdlibStrtolTest, testStdlibStrtol007, Function | MediumTest | Level1)
194 {
195 char nPtr8_2[] = "010";
196 char *endPtr8_2 = NULL;
197 long ret = strtol(nPtr8_2, &endPtr8_2, 8);
198 if (ret == 8) {
199 LOG("[DEMO] posix stdlib test case 7:strtol(base=8) ret:%ld,%s, endPtr:%s ok.\n", ret, nPtr8_2, endPtr8_2);
200 } else {
201 LOG("[DEMO] posix stdlib test case 7:strtol(base=8) ret:%ld,%s fail.\n", ret, nPtr8_2);
202 }
203 TEST_ASSERT_EQUAL_INT32(8, ret);
204 TEST_ASSERT_EQUAL_STRING(endPtr8_2, "");
205 return 0;
206 }
207
208 /* *
209 * @tc.number : TEST_STDLIB_STRTOL_008
210 * @tc.name : convert string to long integer
211 * @tc.desc : [C- SOFTWARE -0200]
212 */
213 LITE_TEST_CASE(PosixStdlibStrtolTest, testStdlibStrtol008, Function | MediumTest | Level1)
214 {
215 char nPtr8_3[] = "010";
216 char *endPtr8_3 = NULL;
217 long ret = strtol(nPtr8_3, &endPtr8_3, 0);
218 if (ret == 8) {
219 LOG("[DEMO] posix stdlib test case 8:strtol(base=8) ret:%ld,%s, endPtr:%s ok.\n", ret, nPtr8_3, endPtr8_3);
220 } else {
221 LOG("[DEMO] posix stdlib test case 8:strtol(base=8) ret:%ld,%s fail.\n", ret, nPtr8_3);
222 }
223 TEST_ASSERT_EQUAL_INT32(8, ret);
224 TEST_ASSERT_EQUAL_STRING(endPtr8_3, "");
225 return 0;
226 }
227
228 /* *
229 * @tc.number : TEST_STDLIB_STRTOL_009
230 * @tc.name : convert string to long integer
231 * @tc.desc : [C- SOFTWARE -0200]
232 */
233 LITE_TEST_CASE(PosixStdlibStrtolTest, testStdlibStrtol009, Function | MediumTest | Level1)
234 {
235 char nPtr2[] = "10";
236 char *endPtr2 = NULL;
237 long ret = strtol(nPtr2, &endPtr2, 2);
238 if (ret == 2) {
239 LOG("[DEMO] posix stdlib test case 9:strtol(base=2) ret:%ld,%s, endPtr:%s ok.\n", ret, nPtr2, endPtr2);
240 } else {
241 LOG("[DEMO] posix stdlib test case 9:strtol(base=2) ret:%ld,%s fail.\n", ret, nPtr2);
242 }
243 TEST_ASSERT_EQUAL_INT32(2, ret);
244 TEST_ASSERT_EQUAL_STRING(endPtr2, "");
245 return 0;
246 }
247
248 /* *
249 * @tc.number : TEST_STDLIB_STRTOL_010
250 * @tc.name : convert string to long integer
251 * @tc.desc : [C- SOFTWARE -0200]
252 */
253 LITE_TEST_CASE(PosixStdlibStrtolTest, testStdlibStrtol010, Function | MediumTest | Level1)
254 {
255 char nPtr[] = "12 0110 0XDEFE 0666 -1.6";
256 char *endPtr = NULL;
257 long ret = strtol(nPtr, &endPtr, 10);
258 if (ret == 12) {
259 LOG("[DEMO] posix stdlib test case 10:strtol(base=10) ret:%ld, %s, endPtr:%s ok.\n", ret, endPtr, endPtr);
260 } else {
261 LOG("[DEMO] posix stdlib test case 10:strtol(base=10) ret:%ld, %s fail.\n", ret, endPtr);
262 }
263 TEST_ASSERT_EQUAL_INT32(12, ret);
264 TEST_ASSERT_EQUAL_STRING(endPtr, " 0110 0XDEFE 0666 -1.6");
265 return 0;
266 }
267
268 #if (LOSCFG_LIBC_MUSL == 1)
269 /* *
270 * @tc.number : TEST_STDLIB_STRTOL_011
271 * @tc.name : convert string to long integer
272 * @tc.desc : [C- SOFTWARE -0200]
273 */
274 LITE_TEST_CASE(PosixStdlibStrtolTest, testStdlibStrtol011, Function | MediumTest | Level1)
275 {
276 char nPtr[] = "12 1.5";
277 char *endPtr = NULL;
278 long ret = strtol(nPtr, &endPtr, 65);
279 if (ret == 0) {
280 LOG("[DEMO] posix stdlib test case 11:strtol(base=65) ret:%ld, %s, endPtr:%s ok.\n", ret, endPtr, endPtr);
281 } else {
282 LOG("[DEMO] posix stdlib test case 11:strtol(base=65) ret:%ld, %s fail.\n", ret, endPtr);
283 }
284 TEST_ASSERT_EQUAL_INT32(0, ret);
285 TEST_ASSERT_EQUAL_STRING(endPtr, "12 1.5");
286 return 0;
287 }
288 #endif
289
290 LITE_TEST_CASE(PosixStdlibStrtolTest, testStdlibStrtol012, Function | MediumTest | Level1)
291 {
292 char nPtr[] = "2147483647 -2147483648";
293 char *endPtr = NULL;
294 long ret = strtol(nPtr, &endPtr, 10);
295 if (ret == 2147483647) {
296 LOG("[DEMO] posix stdlib test case 12:strtol(base=10) ret:%ld, %s, endPtr:%s ok.\n", ret, endPtr, endPtr);
297 } else {
298 LOG("[DEMO] posix stdlib test case 12:strtol(base=10) ret:%ld, %s fail.\n", ret, endPtr);
299 }
300 TEST_ASSERT_EQUAL_INT32(2147483647, ret);
301 TEST_ASSERT_EQUAL_STRING(endPtr, " -2147483648");
302 return 0;
303 }
304
305 /* *
306 * @tc.number : TEST_STDLIB_ATOL_002
307 * @tc.name : convert string to long integer
308 * @tc.desc : [C- SOFTWARE -0200]
309 */
310 LITE_TEST_CASE(PosixStdlibStrtolTest, testStdlibStrtol013, Function | MediumTest | Level1)
311 {
312 char nPtr[] = " -2147483648";
313 char *endPtr = NULL;
314 long ret = strtol(nPtr, &endPtr, 10);
315 if (ret == -2147483648) {
316 LOG("[DEMO] posix stdlib test case 13:strtol(base=10) ret:%ld, %s, endPtr:%s ok.\n", ret, endPtr, endPtr);
317 } else {
318 LOG("[DEMO] posix stdlib test case 13:strtol(base=10) ret:%ld, %s fail.\n", ret, endPtr);
319 }
320 TEST_ASSERT_EQUAL_INT32(-2147483648, ret);
321 TEST_ASSERT_EQUAL_STRING(endPtr, "");
322 return 0;
323 }
324
325 RUN_TEST_SUITE(PosixStdlibStrtolTest);
326
PosixStdlibStrtolFuncTest()327 void PosixStdlibStrtolFuncTest()
328 {
329 LOG("begin PosixStdlibStrtolFuncTest....");
330 RUN_ONE_TESTCASE(testStdlibStrtol001);
331 RUN_ONE_TESTCASE(testStdlibStrtol002);
332 RUN_ONE_TESTCASE(testStdlibStrtol003);
333 RUN_ONE_TESTCASE(testStdlibStrtol004);
334 RUN_ONE_TESTCASE(testStdlibStrtol005);
335 RUN_ONE_TESTCASE(testStdlibStrtol006);
336 RUN_ONE_TESTCASE(testStdlibStrtol007);
337 RUN_ONE_TESTCASE(testStdlibStrtol008);
338 RUN_ONE_TESTCASE(testStdlibStrtol009);
339 RUN_ONE_TESTCASE(testStdlibStrtol010);
340 #if (LOSCFG_LIBC_MUSL == 1)
341 RUN_ONE_TESTCASE(testStdlibStrtol011);
342 #endif
343 RUN_ONE_TESTCASE(testStdlibStrtol012);
344 RUN_ONE_TESTCASE(testStdlibStrtol013);
345
346 return;
347 }