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 "hctest.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 TEST_ASSERT_EQUAL_UINT32(12UL, ret);
85 TEST_ASSERT_EQUAL_STRING(endPtr, " 0110 0XDEFE 0666 4294967295 4294967296 12.34");
86 }
87
88 /* *
89 * @tc.number : TEST_STDLIB_STRTOUL_002
90 * @tc.name : convert string by strtoul
91 * @tc.desc : [C- SOFTWARE -0200]
92 */
93 LITE_TEST_CASE(PosixStdlibStrtoulTest, testStdlibStrtoul002, Function | MediumTest | Level1)
94 {
95 char nPtr[] = " 0110 0XDEFE 0666 4294967295 4294967296 12.34";
96 char *endPtr = NULL;
97 unsigned long ret = strtoul(nPtr, &endPtr, 2);
98 if (ret == 6UL) {
99 LOG("[DEMO] posix stdlib test case 2:strtoul(base=2) ret:%lu,%s, endPtr:%s ok.\n", ret, nPtr, endPtr);
100 } else {
101 LOG("[DEMO] posix stdlib test case 2:strtoul(base=2) ret:%lu,%s fail.\n", ret, endPtr);
102 }
103 TEST_ASSERT_EQUAL_UINT32(6UL, ret);
104 TEST_ASSERT_EQUAL_STRING(endPtr, " 0XDEFE 0666 4294967295 4294967296 12.34");
105 }
106
107 /* *
108 * @tc.number : TEST_STDLIB_STRTOUL_003
109 * @tc.name : convert string by strtoul
110 * @tc.desc : [C- SOFTWARE -0200]
111 */
112 LITE_TEST_CASE(PosixStdlibStrtoulTest, testStdlibStrtoul003, Function | MediumTest | Level1)
113 {
114 char nPtr[] = " 0XDEFE 0666 4294967295 4294967296 12.34";
115 char *endPtr = NULL;
116 unsigned long ret = strtoul(nPtr, &endPtr, 16);
117 if (ret == 0XDEFEUL) {
118 LOG("[DEMO] posix stdlib test case 3:strtoul(base=16) ret:%lu,%s, endPtr:%s ok.\n", ret, nPtr, endPtr);
119 } else {
120 LOG("[DEMO] posix stdlib test case 3:strtoul(base=16) ret:%lu,%s fail.\n", ret, endPtr);
121 }
122 TEST_ASSERT_EQUAL_UINT32(0XDEFEUL, ret);
123 TEST_ASSERT_EQUAL_STRING(endPtr, " 0666 4294967295 4294967296 12.34");
124 }
125
126 /* *
127 * @tc.number : TEST_STDLIB_STRTOUL_004
128 * @tc.name : convert string by strtoul
129 * @tc.desc : [C- SOFTWARE -0200]
130 */
131 LITE_TEST_CASE(PosixStdlibStrtoulTest, testStdlibStrtoul004, Function | MediumTest | Level1)
132 {
133 char nPtr[] = " 0666 4294967295 4294967296 12.34";
134 char *endPtr = NULL;
135 unsigned long ret = strtoul(nPtr, &endPtr, 8);
136 if (ret == 0666UL) {
137 LOG("[DEMO] posix stdlib test case 4:strtoul(base=8) ret:%lu,%s, endPtr:%s ok.\n", ret, nPtr, endPtr);
138 } else {
139 LOG("[DEMO] posix stdlib test case 4:strtoul(base=8) ret:%lu,%s fail.\n", ret, endPtr);
140 }
141 TEST_ASSERT_EQUAL_UINT32(0666UL, ret);
142 TEST_ASSERT_EQUAL_STRING(endPtr, " 4294967295 4294967296 12.34");
143 }
144
145 /* *
146 * @tc.number : TEST_STDLIB_STRTOUL_005
147 * @tc.name : convert string by strtoul
148 * @tc.desc : [C- SOFTWARE -0200]
149 */
150 LITE_TEST_CASE(PosixStdlibStrtoulTest, testStdlibStrtoul005, Function | MediumTest | Level1)
151 {
152 char nPtr[] = " 4294967295 4294967296 12.34";
153 char *endPtr = NULL;
154 unsigned long ret = strtoul(nPtr, &endPtr, 0);
155 if (ret == 4294967295UL) {
156 LOG("[DEMO] posix stdlib test case 5:strtoul(base=0) ret:%lu,%s, endPtr:%s ok.\n", ret, nPtr, endPtr);
157 } else {
158 LOG("[DEMO] posix stdlib test case 5:strtoul(base=0) ret:%lu,%s fail.\n", ret, endPtr);
159 }
160 TEST_ASSERT_EQUAL_UINT32(4294967295UL, ret);
161 TEST_ASSERT_EQUAL_STRING(endPtr, " 4294967296 12.34");
162 }
163
164 /* *
165 * @tc.number : TEST_STDLIB_STRTOUL_006
166 * @tc.name : convert string by strtoul
167 * @tc.desc : [C- SOFTWARE -0200]
168 */
169 LITE_TEST_CASE(PosixStdlibStrtoulTest, testStdlibStrtoul006, Function | MediumTest | Level1)
170 {
171 char nPtr[] = " 4294967296 12.34";
172 char *endPtr = NULL;
173 unsigned long ret = strtoul(nPtr, &endPtr, 0);
174 if (ret == 0UL) {
175 LOG("[DEMO] posix stdlib test case 6:strtoul(base=0) ret:%lu,%s, endPtr:%s ok.\n", ret, nPtr, endPtr);
176 } else {
177 LOG("[DEMO] posix stdlib test case 6:strtoul(base=0) ret:%lu,%s fail.\n", ret, endPtr);
178 }
179
180 TEST_ASSERT_EQUAL_UINT32(ULONG_MAX, ret);
181 TEST_ASSERT_EQUAL_STRING(endPtr, " 12.34");
182 }
183
184 /* *
185 * @tc.number : TEST_STDLIB_STRTOUL_007
186 * @tc.name : convert string by strtoul
187 * @tc.desc : [C- SOFTWARE -0200]
188 */
189 LITE_TEST_CASE(PosixStdlibStrtoulTest, testStdlibStrtoul007, Function | MediumTest | Level1)
190 {
191 char nPtr[] = " 12.34";
192 char *endPtr = NULL;
193 unsigned long ret = strtoul(nPtr, &endPtr, 65);
194 if (ret == 0UL) {
195 LOG("[DEMO] posix stdlib test case 7:strtoul(base=65) ret:%lu,%s, endPtr:%s ok.\n", ret, nPtr, endPtr);
196 } else {
197 LOG("[DEMO] posix stdlib test case 7:strtoul(base=65) ret:%lu,%s fail.\n", ret, endPtr);
198 }
199 TEST_ASSERT_EQUAL_UINT32(0UL, ret);
200 TEST_ASSERT_EQUAL_STRING(endPtr, " 12.34");
201 }
202
203 /* *
204 * @tc.number : TEST_STDLIB_STRTOUL_008
205 * @tc.name : convert string by strtoul
206 * @tc.desc : [C- SOFTWARE -0200]
207 */
208 LITE_TEST_CASE(PosixStdlibStrtoulTest, testStdlibStrtoul008, Function | MediumTest | Level1)
209 {
210 char nPtr[] = ".34";
211 char *endPtr = NULL;
212 unsigned long ret = strtoul(nPtr, &endPtr, 0);
213 if (ret == 0UL) {
214 LOG("[DEMO] posix stdlib test case 8:strtoul(base=0) ret:%lu,%s, endPtr:%s ok.\n", ret, nPtr, endPtr);
215 } else {
216 LOG("[DEMO] posix stdlib test case 8:strtoul(base=0) ret:%lu,%s fail.\n", ret, endPtr);
217 }
218 TEST_ASSERT_EQUAL_UINT32(0UL, ret);
219 TEST_ASSERT_EQUAL_STRING(endPtr, ".34");
220 }
221
222 /**
223 * @tc.number : TEST_STDLIB_STRTOUL_009
224 * @tc.name : convert string by strtoul
225 * @tc.desc : [C- SOFTWARE -0200]
226 */
227 LITE_TEST_CASE(PosixStdlibStrtoulTest, testStdlibStrtoul009, Function | MediumTest | Level1)
228 {
229 char nPtr[] = "0XDEFE 0666";
230 char *endPtr = NULL;
231 unsigned long ret = strtoul(nPtr, &endPtr, 0);
232 if (ret == 0XDEFE) {
233 LOG("[DEMO] posix stdlib test case 9:strtoul(base=0) ret:%lu,%s, endPtr:%s ok.\n", ret, nPtr, endPtr);
234 } else {
235 LOG("[DEMO] posix stdlib test case 9:strtoul(base=0) ret:%lu,%s fail.\n", ret, endPtr);
236 }
237 TEST_ASSERT_EQUAL_UINT32(0XDEFE, ret);
238 TEST_ASSERT_EQUAL_STRING(endPtr, " 0666");
239 }
240
241 /* *
242 * @tc.number : TEST_STDLIB_STRTOUL_010
243 * @tc.name : convert string by strtoul
244 * @tc.desc : [C- SOFTWARE -0200]
245 */
246 LITE_TEST_CASE(PosixStdlibStrtoulTest, testStdlibStrtoul010, Function | MediumTest | Level1)
247 {
248 char nPtr[] = " 0666";
249 char *endPtr = NULL;
250 unsigned long ret = strtoul(nPtr, &endPtr, 0);
251 if (ret == 0666) {
252 LOG("[DEMO] posix stdlib test case 9:strtoul(base=0) ret:%lu,%s, endPtr:%s ok.\n", ret, nPtr, endPtr);
253 } else {
254 LOG("[DEMO] posix stdlib test case 9:strtoul(base=0) ret:%lu,%s fail.\n", ret, endPtr);
255 }
256 TEST_ASSERT_EQUAL_UINT32(0666, ret);
257 TEST_ASSERT_EQUAL_STRING(endPtr, "");
258 }
259
260 RUN_TEST_SUITE(PosixStdlibStrtoulTest);
261
PosixStdlibStrtoulFuncTest()262 void PosixStdlibStrtoulFuncTest()
263 {
264 LOG("begin PosixStdlibStrtoulFuncTest....");
265 RUN_ONE_TESTCASE(testStdlibStrtoul001);
266 RUN_ONE_TESTCASE(testStdlibStrtoul002);
267 RUN_ONE_TESTCASE(testStdlibStrtoul003);
268 RUN_ONE_TESTCASE(testStdlibStrtoul004);
269 RUN_ONE_TESTCASE(testStdlibStrtoul005);
270 RUN_ONE_TESTCASE(testStdlibStrtoul006);
271 RUN_ONE_TESTCASE(testStdlibStrtoul007);
272 RUN_ONE_TESTCASE(testStdlibStrtoul008);
273 RUN_ONE_TESTCASE(testStdlibStrtoul009);
274 RUN_ONE_TESTCASE(testStdlibStrtoul010);
275
276 return;
277 }