1 /*
2 * Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification,
5 * are permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this list of
8 * conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11 * of conditions and the following disclaimer in the documentation and/or other materials
12 * provided with the distribution.
13 *
14 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
15 * to endorse or promote products derived from this software without specific prior written
16 * permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
22 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #include "xts_io.h"
32
33 LITE_TEST_SUIT(IO, IoStdlibApi, IoStdlibApiTestSuite);
34
IoStdlibApiTestSuiteSetUp(void)35 static BOOL IoStdlibApiTestSuiteSetUp(void)
36 {
37 return TRUE;
38 }
39
IoStdlibApiTestSuiteTearDown(void)40 static BOOL IoStdlibApiTestSuiteTearDown(void)
41 {
42 return TRUE;
43 }
44
45 /**
46 * @tc.number SUB_KERNEL_IO_STDLIB_0400
47 * @tc.name strtof basic function test
48 * @tc.desc [C- SOFTWARE -0200]
49 */
50 LITE_TEST_CASE(IoStdlibApiTestSuite, testStrtof, Function | MediumTest | Level1)
51 {
52 char *endPtr = NULL;
53 float ret = strtof(" -3.40E+38 hello", &endPtr);
54 ICUNIT_ASSERT_EQUAL(ret, (float)(-3.40E+38), ret);
55 ICUNIT_ASSERT_STRING_EQUAL(endPtr, " hello", endPtr);
56
57 ret = strtof(" 3.40E+38 ===", &endPtr);
58 ICUNIT_ASSERT_EQUAL(ret, (float)(3.40E+38), ret);
59 ICUNIT_ASSERT_STRING_EQUAL(endPtr, " ===", endPtr);
60
61 ret = strtof("-9.6e17 this 123", &endPtr);
62 ICUNIT_ASSERT_EQUAL(ret, (float)(-9.6e17), ret);
63 ICUNIT_ASSERT_STRING_EQUAL(endPtr, " this 123", endPtr);
64
65 ret = strtof("this is string", &endPtr);
66 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
67 ICUNIT_ASSERT_STRING_EQUAL(endPtr, "this is string", endPtr);
68 return 0;
69 }
70
71 /**
72 * @tc.number SUB_KERNEL_IO_STDLIB_0500
73 * @tc.name strtod basic function test
74 * @tc.desc [C- SOFTWARE -0200]
75 */
76 LITE_TEST_CASE(IoStdlibApiTestSuite, testStrtod, Function | MediumTest | Level1)
77 {
78 char *endPtr = NULL;
79 double ret = strtod(" -1.79E+308 hello", &endPtr);
80 ICUNIT_ASSERT_EQUAL(ret, -1.79E+308, ret);
81 ICUNIT_ASSERT_STRING_EQUAL(endPtr, " hello", endPtr);
82
83 ret = strtod("1.79E+308 ===", &endPtr);
84 ICUNIT_ASSERT_EQUAL(ret, 1.79E+308, ret);
85 ICUNIT_ASSERT_STRING_EQUAL(endPtr, " ===", endPtr);
86
87 ret = strtod("-9.6e17 this 123", &endPtr);
88 ICUNIT_ASSERT_EQUAL(ret, -9.6e17, ret);
89 ICUNIT_ASSERT_STRING_EQUAL(endPtr, " this 123", endPtr);
90
91 ret = strtod("this is string", &endPtr);
92 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
93 ICUNIT_ASSERT_STRING_EQUAL(endPtr, "this is string", endPtr);
94 return 0;
95 }
96
97 /**
98 * @tc.number SUB_KERNEL_IO_STDLIB_0600
99 * @tc.name strtold basic function test
100 * @tc.desc [C- SOFTWARE -0200]
101 */
102 LITE_TEST_CASE(IoStdlibApiTestSuite, testStrtold, Function | MediumTest | Level1)
103 {
104 char *endPtr = NULL;
105 long double ret = strtold(" 2.22507e-308 hello", &endPtr);
106 ICUNIT_ASSERT_STRING_EQUAL(endPtr, " hello", endPtr);
107
108 ret = strtold(" 1.79769e+308 ===", &endPtr);
109 ICUNIT_ASSERT_STRING_EQUAL(endPtr, " ===", endPtr);
110
111 ret = strtold("-9.6e17 this 123", &endPtr);
112 ICUNIT_ASSERT_EQUAL(ret, -9.6e17, ret);
113 ICUNIT_ASSERT_STRING_EQUAL(endPtr, " this 123", endPtr);
114
115 ret = strtold("this is string", &endPtr);
116 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
117 ICUNIT_ASSERT_STRING_EQUAL(endPtr, "this is string", endPtr);
118 return 0;
119 }
120
121 /**
122 * @tc.number SUB_KERNEL_IO_STDLIB_0700
123 * @tc.name strtol basic function test
124 * @tc.desc [C- SOFTWARE -0200]
125 */
126 LITE_TEST_CASE(IoStdlibApiTestSuite, testStrtol, Function | MediumTest | Level1)
127 {
128 char nPtr[] = "12 0110 0XDEFE 0666 -1.6";
129 char *endPtr = NULL;
130 long ret = strtol(nPtr, &endPtr, 10); /* 10 common data for test, no special meaning */
131 ICUNIT_ASSERT_EQUAL(ret, 12, ret); /* 12 common data for test, no special meaning */
132 ICUNIT_ASSERT_STRING_EQUAL(endPtr, " 0110 0XDEFE 0666 -1.6", endPtr);
133
134 ret = strtol(endPtr, &endPtr, 2); /* 2 common data for test, no special meaning */
135 ICUNIT_ASSERT_EQUAL(ret, 6, ret); /* 6 common data for test, no special meaning */
136 ICUNIT_ASSERT_STRING_EQUAL(endPtr, " 0XDEFE 0666 -1.6", endPtr);
137
138 ret = strtol(endPtr, &endPtr, 16); /* 16 common data for test, no special meaning */
139 ICUNIT_ASSERT_EQUAL(ret, 0XDEFE, ret);
140 ICUNIT_ASSERT_STRING_EQUAL(endPtr, " 0666 -1.6", endPtr);
141
142 ret = strtol(endPtr, &endPtr, 8); /* 8 common data for test, no special meaning */
143 ICUNIT_ASSERT_EQUAL(ret, 0666, ret);
144 ICUNIT_ASSERT_STRING_EQUAL(endPtr, " -1.6", endPtr);
145
146 ret = strtol(endPtr, &endPtr, 65); /* 65 common data for test, no special meaning */
147 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
148 ICUNIT_ASSERT_STRING_EQUAL(endPtr, " -1.6", endPtr);
149
150 ret = strtol(endPtr, &endPtr, 0);
151 ICUNIT_ASSERT_EQUAL(ret, -1, ret); /* -1 common data for test, no special meaning */
152 ICUNIT_ASSERT_STRING_EQUAL(endPtr, ".6", endPtr);
153
154 ret = strtol(endPtr, &endPtr, 10); /* 10 common data for test, no special meaning */
155 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
156 ICUNIT_ASSERT_STRING_EQUAL(endPtr, ".6", endPtr);
157
158 char nPtr1[] = "2147483647 2147483648 -2147483648 -2147483649";
159 ret = strtol(nPtr1, &endPtr, 10); /* 10 common data for test, no special meaning */
160 ICUNIT_ASSERT_EQUAL(ret, 2147483647, ret); /* 2147483647 common data for test, no special meaning */
161 ICUNIT_ASSERT_STRING_EQUAL(endPtr, " 2147483648 -2147483648 -2147483649", endPtr);
162
163 ret = strtol(endPtr, &endPtr, 10); /* 10 common data for test, no special meaning */
164 ICUNIT_ASSERT_EQUAL(ret, 2147483647, ret); /* 2147483647 common data for test, no special meaning */
165 ICUNIT_ASSERT_STRING_EQUAL(endPtr, " -2147483648 -2147483649", endPtr);
166
167 ret = strtol(endPtr, &endPtr, 10); /* 10 common data for test, no special meaning */
168 ICUNIT_ASSERT_EQUAL(ret, -2147483648, ret); /* -2147483648 common data for test, no special meaning */
169 ICUNIT_ASSERT_STRING_EQUAL(endPtr, " -2147483649", endPtr);
170
171 ret = strtol(endPtr, &endPtr, 10); /* 10 common data for test, no special meaning */
172 ICUNIT_ASSERT_EQUAL(ret, -2147483648, ret); /* -2147483648 common data for test, no special meaning */
173 ICUNIT_ASSERT_STRING_EQUAL(endPtr, "", endPtr);
174 return 0;
175 }
176
177 /**
178 * @tc.number SUB_KERNEL_IO_STDLIB_0800
179 * @tc.name strtoul basic function test
180 * @tc.desc [C- SOFTWARE -0200]
181 */
182 LITE_TEST_CASE(IoStdlibApiTestSuite, testStrtoul, Function | MediumTest | Level1)
183 {
184 char nPtr[] = "12 0110 0XDEFE 0666 4294967295 4294967296 12.34";
185 char *endPtr = NULL;
186 unsigned long ret = strtoul(nPtr, &endPtr, 10); /* 10 common data for test, no special meaning */
187 ICUNIT_ASSERT_EQUAL(ret, 12UL, ret);
188 ICUNIT_ASSERT_STRING_EQUAL(endPtr, " 0110 0XDEFE 0666 4294967295 4294967296 12.34", endPtr);
189
190 ret = strtoul(endPtr, &endPtr, 2); /* 2 common data for test, no special meaning */
191 ICUNIT_ASSERT_EQUAL(ret, 6UL, ret);
192 ICUNIT_ASSERT_STRING_EQUAL(endPtr, " 0XDEFE 0666 4294967295 4294967296 12.34", endPtr);
193
194 ret = strtoul(endPtr, &endPtr, 16); /* 16 common data for test, no special meaning */
195 ICUNIT_ASSERT_EQUAL(ret, 0XDEFEUL, ret);
196 ICUNIT_ASSERT_STRING_EQUAL(endPtr, " 0666 4294967295 4294967296 12.34", endPtr);
197
198 ret = strtoul(endPtr, &endPtr, 8); /* 8 common data for test, no special meaning */
199 ICUNIT_ASSERT_EQUAL(ret, 0666UL, ret);
200 ICUNIT_ASSERT_STRING_EQUAL(endPtr, " 4294967295 4294967296 12.34", endPtr);
201
202 ret = strtoul(endPtr, &endPtr, 0);
203 ICUNIT_ASSERT_EQUAL(ret, 4294967295UL, ret);
204 ICUNIT_ASSERT_STRING_EQUAL(endPtr, " 4294967296 12.34", endPtr);
205
206 ret = strtoul(endPtr, &endPtr, 0);
207 ICUNIT_ASSERT_EQUAL(ret, 4294967295UL, ret);
208 ICUNIT_ASSERT_STRING_EQUAL(endPtr, " 12.34", endPtr);
209
210 ret = strtoul(endPtr, &endPtr, 65); /* 65 common data for test, no special meaning */
211 ICUNIT_ASSERT_EQUAL(ret, 0UL, ret);
212 ICUNIT_ASSERT_STRING_EQUAL(endPtr, " 12.34", endPtr);
213
214 ret = strtoul(endPtr, &endPtr, 0);
215 ICUNIT_ASSERT_EQUAL(ret, 12UL, ret);
216 ICUNIT_ASSERT_STRING_EQUAL(endPtr, ".34", endPtr);
217
218 ret = strtoul(endPtr, &endPtr, 0);
219 ICUNIT_ASSERT_EQUAL(ret, 0UL, ret);
220 ICUNIT_ASSERT_STRING_EQUAL(endPtr, ".34", endPtr);
221 return 0;
222 }
223
224 /**
225 * @tc.number SUB_KERNEL_IO_STDLIB_1000
226 * @tc.name strtoull basic function test
227 * @tc.desc [C- SOFTWARE -0200]
228 */
229 LITE_TEST_CASE(IoStdlibApiTestSuite, testStrtoull, Function | MediumTest | Level1)
230 {
231 char nPtr[] = "12 0110 0XDEFE 0666 1.6";
232 char *endPtr = NULL;
233 unsigned long long ret = strtoull(nPtr, &endPtr, 10); /* 10 common data for test, no special meaning */
234 ICUNIT_ASSERT_EQUAL(ret, 12ULL, ret);
235 ICUNIT_ASSERT_STRING_EQUAL(endPtr, " 0110 0XDEFE 0666 1.6", endPtr);
236
237 ret = strtoull(endPtr, &endPtr, 2); /* 2 common data for test, no special meaning */
238 ICUNIT_ASSERT_EQUAL(ret, 6ULL, ret);
239 ICUNIT_ASSERT_STRING_EQUAL(endPtr, " 0XDEFE 0666 1.6", endPtr);
240
241 ret = strtoull(endPtr, &endPtr, 16); /* 16 common data for test, no special meaning */
242 ICUNIT_ASSERT_EQUAL(ret, 0XDEFEULL, ret);
243 ICUNIT_ASSERT_STRING_EQUAL(endPtr, " 0666 1.6", endPtr);
244
245 ret = strtoull(endPtr, &endPtr, 8); /* 8 common data for test, no special meaning */
246 ICUNIT_ASSERT_EQUAL(ret, 0666ULL, ret);
247 ICUNIT_ASSERT_STRING_EQUAL(endPtr, " 1.6", endPtr);
248
249 ret = strtoull(endPtr, &endPtr, 65); /* 65 common data for test, no special meaning */
250 ICUNIT_ASSERT_EQUAL(ret, 0ULL, ret);
251 ICUNIT_ASSERT_STRING_EQUAL(endPtr, " 1.6", endPtr);
252
253 ret = strtoull(endPtr, &endPtr, 0);
254 ICUNIT_ASSERT_EQUAL(ret, 1ULL, ret);
255 ICUNIT_ASSERT_STRING_EQUAL(endPtr, ".6", endPtr);
256
257 ret = strtoull(endPtr, &endPtr, 0);
258 ICUNIT_ASSERT_EQUAL(ret, 0ULL, ret);
259 ICUNIT_ASSERT_STRING_EQUAL(endPtr, ".6", endPtr);
260
261 char nPtr1[] = "18446744073709551615 18446744073709551616";
262 ret = strtoull(nPtr1, &endPtr, 10); /* 10 common data for test, no special meaning */
263 ICUNIT_ASSERT_EQUAL(ret, 18446744073709551615ULL, ret);
264 ICUNIT_ASSERT_STRING_EQUAL(endPtr, " 18446744073709551616", endPtr);
265
266 ret = strtoull(endPtr, &endPtr, 10); /* 10 common data for test, no special meaning */
267 ICUNIT_ASSERT_EQUAL(ret, 18446744073709551615ULL, ret);
268 ICUNIT_ASSERT_STRING_EQUAL(endPtr, "", endPtr);
269 return 0;
270 }
271
272 RUN_TEST_SUITE(IoStdlibApiTestSuite);
273
XtsIoStdlibFuncTest(void)274 void XtsIoStdlibFuncTest(void)
275 {
276 RUN_ONE_TESTCASE(testStrtof);
277 RUN_ONE_TESTCASE(testStrtod);
278 RUN_ONE_TESTCASE(testStrtold);
279 RUN_ONE_TESTCASE(testStrtol);
280 RUN_ONE_TESTCASE(testStrtoul);
281 RUN_ONE_TESTCASE(testStrtoull);
282 }
283