• 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 "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 "limits.h"
40 #include "log.h"
41 
42 /* *
43  * @tc.desc      : register a test suite, this suite is used to test basic flow and interface dependency
44  * @param        : subsystem name is utils
45  * @param        : module name is utilsFile
46  * @param        : test suit name is CmsisTaskFuncTestSuite
47  */
48 LITE_TEST_SUIT(Posix, Posixtimer, PosixStdlibStrtoulTest);
49 
50 /* *
51  * @tc.setup     : setup for all testcases
52  * @return       : setup result, TRUE is success, FALSE is fail
53  */
PosixStdlibStrtoulTestSetUp(void)54 static BOOL PosixStdlibStrtoulTestSetUp(void)
55 {
56     return TRUE;
57 }
58 
59 /* *
60  * @tc.teardown  : teardown for all testcases
61  * @return       : teardown result, TRUE is success, FALSE is fail
62  */
PosixStdlibStrtoulTestTearDown(void)63 static BOOL PosixStdlibStrtoulTestTearDown(void)
64 {
65     LOG("+-------------------------------------------+\n");
66     return TRUE;
67 }
68 
69 /* *
70  * @tc.number    : TEST_STDLIB_STRTOUL_001
71  * @tc.name      : convert string by strtoul
72  * @tc.desc      : [C- SOFTWARE -0200]
73  */
74 LITE_TEST_CASE(PosixStdlibStrtoulTest, testStdlibStrtoul001, Function | MediumTest | Level1)
75 {
76     char nPtr[] = "12 0110 0XDEFE 0666 4294967295 4294967296 12.34";
77     char *endPtr = NULL;
78     unsigned long ret = strtoul(nPtr, &endPtr, 10);
79     if (ret == 12UL) {
80         LOG("[DEMO] posix stdlib test case 1:strtoul(base=10) ret:%lu,%s, endPtr:%s ok.\n", ret, nPtr, endPtr);
81     } else {
82         LOG("[DEMO] posix stdlib test case 1:strtoul(base=10) ret:%lu,%s fail.\n", ret, nPtr);
83     }
84     ICUNIT_ASSERT_EQUAL(ret, 12UL, ret);
85     ICUNIT_ASSERT_STRING_EQUAL(endPtr, " 0110 0XDEFE 0666 4294967295 4294967296 12.34", 0);
86     return 0;
87 }
88 
89 /* *
90  * @tc.number    : TEST_STDLIB_STRTOUL_002
91  * @tc.name      : convert string by strtoul
92  * @tc.desc      : [C- SOFTWARE -0200]
93  */
94 LITE_TEST_CASE(PosixStdlibStrtoulTest, testStdlibStrtoul002, Function | MediumTest | Level1)
95 {
96     char nPtr[] = " 0110 0XDEFE 0666 4294967295 4294967296 12.34";
97     char *endPtr = NULL;
98     unsigned long ret = strtoul(nPtr, &endPtr, 2);
99     if (ret == 6UL) {
100         LOG("[DEMO] posix stdlib test case 2:strtoul(base=2) ret:%lu,%s, endPtr:%s ok.\n", ret, nPtr, endPtr);
101     } else {
102         LOG("[DEMO] posix stdlib test case 2:strtoul(base=2) ret:%lu,%s fail.\n", ret, endPtr);
103     }
104     ICUNIT_ASSERT_EQUAL(ret, 6UL, ret);
105     ICUNIT_ASSERT_STRING_EQUAL(endPtr, " 0XDEFE 0666 4294967295 4294967296 12.34", 0);
106     return 0;
107 }
108 
109 /* *
110  * @tc.number    : TEST_STDLIB_STRTOUL_003
111  * @tc.name      : convert string by strtoul
112  * @tc.desc      : [C- SOFTWARE -0200]
113  */
114 LITE_TEST_CASE(PosixStdlibStrtoulTest, testStdlibStrtoul003, Function | MediumTest | Level1)
115 {
116     char nPtr[] = " 0XDEFE 0666 4294967295 4294967296 12.34";
117     char *endPtr = NULL;
118     unsigned long ret = strtoul(nPtr, &endPtr, 16);
119     if (ret == 0XDEFEUL) {
120         LOG("[DEMO] posix stdlib test case 3:strtoul(base=16) ret:%lu,%s, endPtr:%s ok.\n", ret, nPtr, endPtr);
121     } else {
122         LOG("[DEMO] posix stdlib test case 3:strtoul(base=16) ret:%lu,%s fail.\n", ret, endPtr);
123     }
124     ICUNIT_ASSERT_EQUAL(ret, 0XDEFEUL, ret);
125     ICUNIT_ASSERT_STRING_EQUAL(endPtr, " 0666 4294967295 4294967296 12.34", 0);
126     return 0;
127 }
128 
129 /* *
130  * @tc.number    : TEST_STDLIB_STRTOUL_004
131  * @tc.name      : convert string by strtoul
132  * @tc.desc      : [C- SOFTWARE -0200]
133  */
134 LITE_TEST_CASE(PosixStdlibStrtoulTest, testStdlibStrtoul004, Function | MediumTest | Level1)
135 {
136     char nPtr[] = " 0666 4294967295 4294967296 12.34";
137     char *endPtr = NULL;
138     unsigned long ret = strtoul(nPtr, &endPtr, 8);
139     if (ret == 0666UL) {
140         LOG("[DEMO] posix stdlib test case 4:strtoul(base=8) ret:%lu,%s, endPtr:%s ok.\n", ret, nPtr, endPtr);
141     } else {
142         LOG("[DEMO] posix stdlib test case 4:strtoul(base=8) ret:%lu,%s fail.\n", ret, endPtr);
143     }
144     ICUNIT_ASSERT_EQUAL(ret, 0666UL, ret);
145     ICUNIT_ASSERT_STRING_EQUAL(endPtr, " 4294967295 4294967296 12.34", 0);
146     return 0;
147 }
148 
149 /* *
150  * @tc.number    : TEST_STDLIB_STRTOUL_005
151  * @tc.name      : convert string by strtoul
152  * @tc.desc      : [C- SOFTWARE -0200]
153  */
154 LITE_TEST_CASE(PosixStdlibStrtoulTest, testStdlibStrtoul005, Function | MediumTest | Level1)
155 {
156     char nPtr[] = " 4294967295 4294967296 12.34";
157     char *endPtr = NULL;
158     unsigned long ret = strtoul(nPtr, &endPtr, 0);
159     if (ret == 4294967295UL) {
160         LOG("[DEMO] posix stdlib test case 5:strtoul(base=0) ret:%lu,%s, endPtr:%s ok.\n", ret, nPtr, endPtr);
161     } else {
162         LOG("[DEMO] posix stdlib test case 5:strtoul(base=0) ret:%lu,%s fail.\n", ret, endPtr);
163     }
164     ICUNIT_ASSERT_EQUAL(ret, 4294967295UL, ret);
165     ICUNIT_ASSERT_STRING_EQUAL(endPtr, " 4294967296 12.34", 0);
166     return 0;
167 }
168 
169 /* *
170  * @tc.number    : TEST_STDLIB_STRTOUL_006
171  * @tc.name      : convert string by strtoul
172  * @tc.desc      : [C- SOFTWARE -0200]
173  */
174 LITE_TEST_CASE(PosixStdlibStrtoulTest, testStdlibStrtoul006, Function | MediumTest | Level1)
175 {
176     char nPtr[] = " 4294967296 12.34";
177     char *endPtr = NULL;
178     unsigned long ret = strtoul(nPtr, &endPtr, 0);
179     if (ret == 0UL) {
180         LOG("[DEMO] posix stdlib test case 6:strtoul(base=0) ret:%lu,%s, endPtr:%s ok.\n", ret, nPtr, endPtr);
181     } else {
182         LOG("[DEMO] posix stdlib test case 6:strtoul(base=0) ret:%lu,%s fail.\n", ret, endPtr);
183     }
184 
185     ICUNIT_ASSERT_EQUAL(ret, ULONG_MAX, ret);
186     ICUNIT_ASSERT_STRING_EQUAL(endPtr, " 12.34", 0);
187     return 0;
188 }
189 
190 #if (LOSCFG_LIBC_MUSL == 1)
191 /* *
192  * @tc.number    : TEST_STDLIB_STRTOUL_007
193  * @tc.name      : convert string by strtoul
194  * @tc.desc      : [C- SOFTWARE -0200]
195  */
196 LITE_TEST_CASE(PosixStdlibStrtoulTest, testStdlibStrtoul007, Function | MediumTest | Level1)
197 {
198     char nPtr[] = " 12.34";
199     char *endPtr = NULL;
200     unsigned long ret = strtoul(nPtr, &endPtr, 65);
201     if (ret == 0UL) {
202         LOG("[DEMO] posix stdlib test case 7:strtoul(base=65) ret:%lu,%s, endPtr:%s ok.\n", ret, nPtr, endPtr);
203     } else {
204         LOG("[DEMO] posix stdlib test case 7:strtoul(base=65) ret:%lu,%s fail.\n", ret, endPtr);
205     }
206     ICUNIT_ASSERT_EQUAL(ret, 0UL, ret);
207     ICUNIT_ASSERT_STRING_EQUAL(endPtr, " 12.34", 0);
208     return 0;
209 }
210 #endif
211 
212 /* *
213  * @tc.number    : TEST_STDLIB_STRTOUL_008
214  * @tc.name      : convert string by strtoul
215  * @tc.desc      : [C- SOFTWARE -0200]
216  */
217 LITE_TEST_CASE(PosixStdlibStrtoulTest, testStdlibStrtoul008, Function | MediumTest | Level1)
218 {
219     char nPtr[] = ".34";
220     char *endPtr = NULL;
221     unsigned long ret = strtoul(nPtr, &endPtr, 0);
222     if (ret == 0UL) {
223         LOG("[DEMO] posix stdlib test case 8:strtoul(base=0) ret:%lu,%s, endPtr:%s ok.\n", ret, nPtr, endPtr);
224     } else {
225         LOG("[DEMO] posix stdlib test case 8:strtoul(base=0) ret:%lu,%s fail.\n", ret, endPtr);
226     }
227     ICUNIT_ASSERT_EQUAL(ret, 0UL, ret);
228     ICUNIT_ASSERT_STRING_EQUAL(endPtr, ".34", 0);
229     return 0;
230 }
231 
232 /**
233  * @tc.number    : TEST_STDLIB_STRTOUL_009
234  * @tc.name      : convert string by strtoul
235  * @tc.desc      : [C- SOFTWARE -0200]
236  */
237 LITE_TEST_CASE(PosixStdlibStrtoulTest, testStdlibStrtoul009, Function | MediumTest | Level1)
238 {
239     char nPtr[] = "0XDEFE 0666";
240     char *endPtr = NULL;
241     unsigned long ret = strtoul(nPtr, &endPtr, 0);
242     if (ret == 0XDEFE) {
243         LOG("[DEMO] posix stdlib test case 9:strtoul(base=0) ret:%lu,%s, endPtr:%s ok.\n", ret, nPtr, endPtr);
244     } else {
245         LOG("[DEMO] posix stdlib test case 9:strtoul(base=0) ret:%lu,%s fail.\n", ret, endPtr);
246     }
247     ICUNIT_ASSERT_EQUAL(ret, 0XDEFE, ret);
248     ICUNIT_ASSERT_STRING_EQUAL(endPtr, " 0666", 0);
249     return 0;
250 }
251 
252 /* *
253  * @tc.number    : TEST_STDLIB_STRTOUL_010
254  * @tc.name      : convert string by strtoul
255  * @tc.desc      : [C- SOFTWARE -0200]
256  */
257 LITE_TEST_CASE(PosixStdlibStrtoulTest, testStdlibStrtoul010, Function | MediumTest | Level1)
258 {
259     char nPtr[] = " 0666";
260     char *endPtr = NULL;
261     unsigned long ret = strtoul(nPtr, &endPtr, 0);
262     if (ret == 0666) {
263         LOG("[DEMO] posix stdlib test case 9:strtoul(base=0) ret:%lu,%s, endPtr:%s ok.\n", ret, nPtr, endPtr);
264     } else {
265         LOG("[DEMO] posix stdlib test case 9:strtoul(base=0) ret:%lu,%s fail.\n", ret, endPtr);
266     }
267     ICUNIT_ASSERT_EQUAL(ret, 0666, ret);
268     ICUNIT_ASSERT_STRING_EQUAL(endPtr, "", 0);
269     return 0;
270 }
271 
272 RUN_TEST_SUITE(PosixStdlibStrtoulTest);
273 
PosixStdlibStrtoulFuncTest()274 void PosixStdlibStrtoulFuncTest()
275 {
276     LOG("begin PosixStdlibStrtoulFuncTest....");
277     RUN_ONE_TESTCASE(testStdlibStrtoul001);
278     RUN_ONE_TESTCASE(testStdlibStrtoul002);
279     RUN_ONE_TESTCASE(testStdlibStrtoul003);
280     RUN_ONE_TESTCASE(testStdlibStrtoul004);
281     RUN_ONE_TESTCASE(testStdlibStrtoul005);
282     RUN_ONE_TESTCASE(testStdlibStrtoul006);
283 #if (LOSCFG_LIBC_MUSL == 1)
284     RUN_ONE_TESTCASE(testStdlibStrtoul007);
285 #endif
286     RUN_ONE_TESTCASE(testStdlibStrtoul008);
287     RUN_ONE_TESTCASE(testStdlibStrtoul009);
288     RUN_ONE_TESTCASE(testStdlibStrtoul010);
289 
290     return;
291 }