1 /* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #include <stdio.h> 17 18 #include "IoTest.h" 19 20 using namespace testing::ext; 21 22 /** 23 * @tc.number SUB_KERNEL_IO_SSCANF_0100 24 * @tc.name sscanf basic function test of float 25 * @tc.desc [C- SOFTWARE -0200] 26 */ 27 HWTEST_F(IoTest, testSscanfFloat, Function | MediumTest | Level1) 28 { 29 float f; 30 int ret = sscanf("1.196532544", "%f", &f); 31 EXPECT_EQ(ret, 1) << "> sscanf fail, errno = " << errno; 32 EXPECT_FLOAT_EQ(f, 1.1965325); 33 34 long double f1; 35 ret = sscanf("1.196532544", "%Lf", &f1); 36 EXPECT_EQ(ret, 1) << "> sscanf fail, errno = " << errno; 37 EXPECT_FLOAT_EQ(f1, 1.1965325); 38 39 ret = sscanf("1.66666", "%6f", &f); 40 EXPECT_EQ(ret, 1) << "> sscanf fail, errno = " << errno; 41 EXPECT_FLOAT_EQ(f, 1.6666); 42 43 ret = sscanf("1.66666", "%8f", &f); 44 EXPECT_EQ(ret, 1) << "> sscanf fail, errno = " << errno; 45 EXPECT_FLOAT_EQ(f, 1.66666); 46 } 47 48 /** 49 * @tc.number SUB_KERNEL_IO_SSCANF_0200 50 * @tc.name sscanf basic function test of float with format E 51 * @tc.desc [C- SOFTWARE -0200] 52 */ 53 HWTEST_F(IoTest, testSscanfFloatE, Function | MediumTest | Level1) 54 { 55 float f; 56 long double f1; 57 int ret = sscanf("1.100000e+00", "%e", &f); 58 EXPECT_EQ(ret, 1) << "> sscanf fail, errno = " << errno; 59 EXPECT_FLOAT_EQ(f, 1.100000); 60 61 ret = sscanf("1.100000e+00", "%Le", &f1); 62 EXPECT_EQ(ret, 1) << "> sscanf fail, errno = " << errno; 63 EXPECT_FLOAT_EQ(f1, 1.100000); 64 65 ret = sscanf("1.100000e+00", "%5e", &f); 66 EXPECT_EQ(ret, 1) << "> sscanf fail, errno = " << errno; 67 EXPECT_FLOAT_EQ(f, 1.100000); 68 69 ret = sscanf("1.100000E+00", "%E", &f); 70 EXPECT_EQ(ret, 1) << "> sscanf fail, errno = " << errno; 71 EXPECT_FLOAT_EQ(f, 1.100000); 72 73 ret = sscanf("1.100000E+00", "%LE", &f1); 74 EXPECT_EQ(ret, 1) << "> sscanf fail, errno = " << errno; 75 EXPECT_FLOAT_EQ(f1, 1.100000); 76 77 ret = sscanf("1.100000E+00", "%5E", &f); 78 EXPECT_EQ(ret, 1) << "> sscanf fail, errno = " << errno; 79 EXPECT_FLOAT_EQ(f, 1.100000); 80 } 81 82 /** 83 * @tc.number SUB_KERNEL_IO_SSCANF_0300 84 * @tc.name sscanf basic function test of float with format G 85 * @tc.desc [C- SOFTWARE -0200] 86 */ 87 HWTEST_F(IoTest, testSscanfFloatG, Function | MediumTest | Level1) 88 { 89 float f; 90 long double f1; 91 int ret = sscanf("1.23e-07", "%g", &f); 92 EXPECT_EQ(ret, 1) << "> sscanf fail, errno = " << errno; 93 EXPECT_FLOAT_EQ(f, 0.000000123); 94 95 ret = sscanf("1.23e-07", "%Lg", &f1); 96 EXPECT_EQ(ret, 1) << "> sscanf fail, errno = " << errno; 97 EXPECT_FLOAT_EQ(f1, 0.000000123); 98 99 ret = sscanf("1.23E-07", "%G", &f); 100 EXPECT_EQ(ret, 1) << "> sscanf fail, errno = " << errno; 101 EXPECT_FLOAT_EQ(f, 1.2300001e-07); 102 103 ret = sscanf("1.23E-07", "%LG", &f1); 104 EXPECT_EQ(ret, 1) << "> sscanf fail, errno = " << errno; 105 EXPECT_FLOAT_EQ(f1, 1.2300001e-07); 106 } 107 108 /** 109 * @tc.number SUB_KERNEL_IO_SSCANF_0400 110 * @tc.name sscanf basic function test of float with format A 111 * @tc.desc [C- SOFTWARE -0200] 112 */ 113 HWTEST_F(IoTest, testSscanfFloatA, Function | MediumTest | Level1) 114 { 115 float f; 116 long double f1; 117 int ret = sscanf("0x1.199999999999ap+0", "%a", &f); 118 EXPECT_EQ(ret, 1) << "> sscanf fail, errno = " << errno; 119 EXPECT_FLOAT_EQ(f, 1.1); 120 121 ret = sscanf("0x1.199999999999ap+0", "%La", &f1); 122 EXPECT_EQ(ret, 1) << "> sscanf fail, errno = " << errno; 123 EXPECT_FLOAT_EQ(f1, 1.1); 124 125 ret = sscanf("0x1.199999999999ap+0", "%5a", &f); 126 EXPECT_EQ(ret, 1) << "> sscanf fail, errno = " << errno; 127 EXPECT_FLOAT_EQ(f, 1.0625); 128 129 ret = sscanf("0X1.199999999999AP+0", "%A", &f); 130 EXPECT_EQ(ret, 1) << "> sscanf fail, errno = " << errno; 131 EXPECT_FLOAT_EQ(f, 1.1); 132 133 ret = sscanf("0X1.199999999999AP+0", "%LA", &f1); 134 EXPECT_EQ(ret, 1) << "> sscanf fail, errno = " << errno; 135 EXPECT_FLOAT_EQ(f1, 1.1); 136 137 ret = sscanf("0X1.199999999999AP+0", "%5A", &f); 138 EXPECT_EQ(ret, 1) << "> sscanf fail, errno = " << errno; 139 EXPECT_FLOAT_EQ(f, 1.0625); 140 } 141 142 /** 143 * @tc.number SUB_KERNEL_IO_SSCANF_0500 144 * @tc.name sscanf basic function test of char 145 * @tc.desc [C- SOFTWARE -0200] 146 */ 147 HWTEST_F(IoTest, testSscanfChar, Function | MediumTest | Level1) 148 { 149 char c, c1; 150 int ret = sscanf("a", "%c", &c); 151 EXPECT_EQ(ret, 1) << "> sscanf fail, errno = " << errno; 152 EXPECT_EQ(c, 'a'); 153 154 char c2[10] = {0}; 155 ret = sscanf("helloworld", "%5c", c2); 156 EXPECT_EQ(ret, 1) << "> sscanf fail, errno = " << errno; 157 EXPECT_EQ(c2[0], 'h'); 158 EXPECT_EQ(c2[1], 'e'); 159 EXPECT_EQ(c2[2], 'l'); 160 EXPECT_EQ(c2[3], 'l'); 161 EXPECT_EQ(c2[4], 'o'); 162 163 ret = sscanf("(a) and |b|", "(%c) and |%c%*c|", &c, &c1); 164 EXPECT_EQ(ret, 2) << "> sscanf fail, errno = " << errno; 165 EXPECT_EQ(c, 'a'); 166 EXPECT_EQ(c1, 'b'); 167 } 168 169 /** 170 * @tc.number SUB_KERNEL_IO_SSCANF_0600 171 * @tc.name sscanf basic function test of string 172 * @tc.desc [C- SOFTWARE -0200] 173 */ 174 HWTEST_F(IoTest, testSscanfString, Function | MediumTest | Level1) 175 { 176 char str[50] = {0}; 177 int ret = sscanf("hello", "%s", str); 178 EXPECT_EQ(ret, 1) << "> sscanf fail, errno = " << errno; 179 EXPECT_STREQ(str, "hello"); 180 181 ret = sscanf("hello", "%3s", str); 182 EXPECT_EQ(ret, 1) << "> sscanf fail, errno = " << errno; 183 EXPECT_STREQ(str, "hel"); 184 185 ret = sscanf("hellowo", "%6s", str); 186 EXPECT_EQ(ret, 1) << "> sscanf fail, errno = " << errno; 187 EXPECT_STREQ(str, "hellow"); 188 189 ret = sscanf("hello world", "%*s%s", str); 190 EXPECT_EQ(ret, 1) << "> sscanf fail, errno = " << errno; 191 EXPECT_STREQ(str, "world"); 192 } 193 194 /** 195 * @tc.number SUB_KERNEL_IO_SSCANF_0700 196 * @tc.name sscanf basic function test of int 197 * @tc.desc [C- SOFTWARE -0200] 198 */ 199 HWTEST_F(IoTest, testSscanfIntD, Function | MediumTest | Level1) 200 { 201 int i; 202 int ret = sscanf("-2147483648", "%d", &i); 203 EXPECT_EQ(ret, 1) << "> sscanf fail, errno = " << errno; 204 EXPECT_EQ(i, -2147483648); 205 206 ret = sscanf("2147483647", "%d", &i); 207 EXPECT_EQ(ret, 1) << "> sscanf fail, errno = " << errno; 208 EXPECT_EQ(i, 2147483647); 209 210 ret = sscanf("2147483647", "%15d", &i); 211 EXPECT_EQ(ret, 1) << "> sscanf fail, errno = " << errno; 212 EXPECT_EQ(i, 2147483647); 213 214 ret = sscanf("2147483647", "%8d", &i); 215 EXPECT_EQ(ret, 1) << "> sscanf fail, errno = " << errno; 216 EXPECT_EQ(i, 21474836); 217 218 unsigned intU; 219 ret = sscanf("0", "%u", &intU); 220 EXPECT_EQ(ret, 1) << "> sscanf fail, errno = " << errno; 221 EXPECT_EQ(intU, 0U); 222 223 ret = sscanf("4294967295", "%u", &intU); 224 EXPECT_EQ(ret, 1) << "> sscanf fail, errno = " << errno; 225 EXPECT_EQ(intU, 4294967295U); 226 227 ret = sscanf("4294967295", "%15u", &intU); 228 EXPECT_EQ(ret, 1) << "> sscanf fail, errno = " << errno; 229 EXPECT_EQ(intU, 4294967295U); 230 231 ret = sscanf("4294967295", "%5u", &intU); 232 EXPECT_EQ(ret, 1) << "> sscanf fail, errno = " << errno; 233 EXPECT_EQ(intU, 42949U); 234 } 235 236 /** 237 * @tc.number SUB_KERNEL_IO_SSCANF_0800 238 * @tc.name sscanf basic function test of signed char 239 * @tc.desc [C- SOFTWARE -0200] 240 */ 241 HWTEST_F(IoTest, testSscanfIntHhd, Function | MediumTest | Level1) 242 { 243 signed char intHhd; 244 int ret = sscanf("-128", "%hhd", &intHhd); 245 EXPECT_EQ(ret, 1) << "> sscanf fail, errno = " << errno; 246 EXPECT_EQ(intHhd, -128); 247 248 ret = sscanf("127", "%hhd", &intHhd); 249 EXPECT_EQ(ret, 1) << "> sscanf fail, errno = " << errno; 250 EXPECT_EQ(intHhd, 127); 251 252 ret = sscanf("127", "%5hhd", &intHhd); 253 EXPECT_EQ(ret, 1) << "> sscanf fail, errno = " << errno; 254 EXPECT_EQ(intHhd, 127); 255 256 ret = sscanf("127", "%2hhd", &intHhd); 257 EXPECT_EQ(ret, 1) << "> sscanf fail, errno = " << errno; 258 EXPECT_EQ(intHhd, 12); 259 260 unsigned char intHhu; 261 ret = sscanf("0", "%hhu", &intHhu); 262 EXPECT_EQ(ret, 1) << "> sscanf fail, errno = " << errno; 263 EXPECT_EQ(intHhu, 0); 264 265 ret = sscanf("255", "%hhu", &intHhu); 266 EXPECT_EQ(ret, 1) << "> sscanf fail, errno = " << errno; 267 EXPECT_EQ(intHhu, 255); 268 269 ret = sscanf("255", "%5hhu", &intHhu); 270 EXPECT_EQ(ret, 1) << "> sscanf fail, errno = " << errno; 271 EXPECT_EQ(intHhu, 255); 272 273 ret = sscanf("255", "%2hhu", &intHhu); 274 EXPECT_EQ(ret, 1) << "> sscanf fail, errno = " << errno; 275 EXPECT_EQ(intHhu, 25); 276 } 277 278 /** 279 * @tc.number SUB_KERNEL_IO_SSCANF_0900 280 * @tc.name sscanf basic function test of short int 281 * @tc.desc [C- SOFTWARE -0200] 282 */ 283 HWTEST_F(IoTest, testSscanfIntHd, Function | MediumTest | Level1) 284 { 285 short intHd; 286 int ret = sscanf("32767", "%hd", &intHd); 287 EXPECT_EQ(ret, 1) << "> sscanf fail, errno = " << errno; 288 EXPECT_EQ(intHd, 32767); 289 290 ret = sscanf("-32768", "%hd", &intHd); 291 EXPECT_EQ(ret, 1) << "> sscanf fail, errno = " << errno; 292 EXPECT_EQ(intHd, -32768); 293 294 ret = sscanf("-32768", "%10hd", &intHd); 295 EXPECT_EQ(ret, 1) << "> sscanf fail, errno = " << errno; 296 EXPECT_EQ(intHd, -32768); 297 298 ret = sscanf("-32768", "%5hd", &intHd); 299 EXPECT_EQ(ret, 1) << "> sscanf fail, errno = " << errno; 300 EXPECT_EQ(intHd, -3276); 301 302 unsigned short intHu; 303 ret = sscanf("0", "%hu", &intHu); 304 EXPECT_EQ(ret, 1) << "> sscanf fail, errno = " << errno; 305 EXPECT_EQ(intHu, 0); 306 307 ret = sscanf("65535", "%hu", &intHu); 308 EXPECT_EQ(ret, 1) << "> sscanf fail, errno = " << errno; 309 EXPECT_EQ(intHu, 65535); 310 311 ret = sscanf("65535", "%8hu", &intHu); 312 EXPECT_EQ(ret, 1) << "> sscanf fail, errno = " << errno; 313 EXPECT_EQ(intHu, 65535); 314 315 ret = sscanf("65535", "%3hu", &intHu); 316 EXPECT_EQ(ret, 1) << "> sscanf fail, errno = " << errno; 317 EXPECT_EQ(intHu, 655); 318 } 319 320 /** 321 * @tc.number SUB_KERNEL_IO_SSCANF_1000 322 * @tc.name sscanf basic function test of long int 323 * @tc.desc [C- SOFTWARE -0200] 324 */ 325 HWTEST_F(IoTest, testSscanfIntLd, Function | MediumTest | Level1) 326 { 327 long intL; 328 int ret = sscanf("-2147483648", "%ld", &intL); 329 EXPECT_EQ(ret, 1) << "> sscanf fail, errno = " << errno; 330 EXPECT_EQ(intL, -2147483648); 331 332 ret = sscanf("2147483647", "%ld", &intL); 333 EXPECT_EQ(ret, 1) << "> sscanf fail, errno = " << errno; 334 EXPECT_EQ(intL, 2147483647); 335 336 ret = sscanf("2147483647", "%15ld", &intL); 337 EXPECT_EQ(ret, 1) << "> sscanf fail, errno = " << errno; 338 EXPECT_EQ(intL, 2147483647); 339 340 ret = sscanf("2147483647", "%5ld", &intL); 341 EXPECT_EQ(ret, 1) << "> sscanf fail, errno = " << errno; 342 EXPECT_EQ(intL, 21474); 343 344 unsigned long intUl; 345 ret = sscanf("0", "%lu", &intUl); 346 EXPECT_EQ(ret, 1) << "> sscanf fail, errno = " << errno; 347 EXPECT_EQ(intUl, 0UL); 348 349 ret = sscanf("4294967295 ", "%lu", &intUl); 350 EXPECT_EQ(ret, 1) << "> sscanf fail, errno = " << errno; 351 EXPECT_EQ(intUl, 4294967295 ); 352 353 ret = sscanf("4294967295 ", "%15lu", &intUl); 354 EXPECT_EQ(ret, 1) << "> sscanf fail, errno = " << errno; 355 EXPECT_EQ(intUl, 4294967295 ); 356 357 ret = sscanf("4294967295 ", "%5lu", &intUl); 358 EXPECT_EQ(ret, 1) << "> sscanf fail, errno = " << errno; 359 EXPECT_EQ(intUl, 42949UL); 360 } 361 362 /** 363 * @tc.number SUB_KERNEL_IO_SSCANF_1100 364 * @tc.name sscanf basic function test of long long int 365 * @tc.desc [C- SOFTWARE -0200] 366 */ 367 HWTEST_F(IoTest, testSscanfIntLld, Function | MediumTest | Level1) 368 { 369 long long intLl; 370 int ret = sscanf("-9223372036854775807", "%lld", &intLl); 371 EXPECT_EQ(ret, 1) << "> sscanf fail, errno = " << errno; 372 EXPECT_EQ(intLl, -9223372036854775807); 373 374 ret = sscanf("9223372036854775807", "%lld", &intLl); 375 EXPECT_EQ(ret, 1) << "> sscanf fail, errno = " << errno; 376 EXPECT_EQ(intLl, 9223372036854775807); 377 378 ret = sscanf("9223372036854775807", "%25lld", &intLl); 379 EXPECT_EQ(ret, 1) << "> sscanf fail, errno = " << errno; 380 EXPECT_EQ(intLl, 9223372036854775807); 381 382 ret = sscanf("9223372036854775807", "%5lld", &intLl); 383 EXPECT_EQ(ret, 1) << "> sscanf fail, errno = " << errno; 384 EXPECT_EQ(intLl, 92233); 385 386 unsigned long long intUll; 387 ret = sscanf("0", "%llu", &intUll); 388 EXPECT_EQ(ret, 1) << "> sscanf fail, errno = " << errno; 389 EXPECT_EQ(intUll, 0ULL); 390 391 ret = sscanf("18446744073709551615", "%llu", &intUll); 392 EXPECT_EQ(ret, 1) << "> sscanf fail, errno = " << errno; 393 EXPECT_EQ(intUll, 18446744073709551615ULL); 394 395 ret = sscanf("18446744073709551615", "%25llu", &intUll); 396 EXPECT_EQ(ret, 1) << "> sscanf fail, errno = " << errno; 397 EXPECT_EQ(intUll, 18446744073709551615ULL); 398 399 ret = sscanf("18446744073709551615", "%5llu", &intUll); 400 EXPECT_EQ(ret, 1) << "> sscanf fail, errno = " << errno; 401 EXPECT_EQ(intUll, 18446ULL); 402 } 403 404 /** 405 * @tc.number SUB_KERNEL_IO_SSCANF_1200 406 * @tc.name sscanf basic function test of intmax_t and uintmax_t 407 * @tc.desc [C- SOFTWARE -0200] 408 */ 409 HWTEST_F(IoTest, testSscanfIntMax, Function | MediumTest | Level1) 410 { 411 intmax_t intMax; 412 int ret = sscanf("-9223372036854775807", "%jd", &intMax); 413 EXPECT_EQ(ret, 1) << "> sscanf fail, errno = " << errno; 414 EXPECT_EQ(intMax, -9223372036854775807); 415 416 ret = sscanf("9223372036854775807", "%jd", &intMax); 417 EXPECT_EQ(ret, 1) << "> sscanf fail, errno = " << errno; 418 EXPECT_EQ(intMax, 9223372036854775807); 419 420 ret = sscanf("9223372036854775807", "%25jd", &intMax); 421 EXPECT_EQ(ret, 1) << "> sscanf fail, errno = " << errno; 422 EXPECT_EQ(intMax, 9223372036854775807); 423 424 ret = sscanf("9223372036854775807", "%5jd", &intMax); 425 EXPECT_EQ(ret, 1) << "> sscanf fail, errno = " << errno; 426 EXPECT_EQ(intMax, 92233); 427 428 uintmax_t intUmax; 429 ret = sscanf("0", "%jd", &intUmax); 430 EXPECT_EQ(ret, 1) << "> sscanf fail, errno = " << errno; 431 EXPECT_EQ(intUmax, 0U); 432 433 ret = sscanf("18446744073709551615", "%jd", &intUmax); 434 EXPECT_EQ(ret, 1) << "> sscanf fail, errno = " << errno; 435 EXPECT_EQ(intUmax, 18446744073709551615ULL); 436 437 ret = sscanf("18446744073709551615", "%25jd", &intUmax); 438 EXPECT_EQ(ret, 1) << "> sscanf fail, errno = " << errno; 439 EXPECT_EQ(intUmax, 18446744073709551615ULL); 440 441 ret = sscanf("18446744073709551615", "%5jd", &intUmax); 442 EXPECT_EQ(ret, 1) << "> sscanf fail, errno = " << errno; 443 EXPECT_EQ(intUmax, 18446U); 444 } 445 446 /** 447 * @tc.number SUB_KERNEL_IO_SSCANF_1300 448 * @tc.name sscanf basic function test of size_t 449 * @tc.desc [C- SOFTWARE -0200] 450 */ 451 HWTEST_F(IoTest, testSscanfSizeT, Function | MediumTest | Level1) 452 { 453 size_t intS; 454 int ret = sscanf("0", "%zd", &intS); 455 EXPECT_EQ(ret, 1) << "> sscanf fail, errno = " << errno; 456 EXPECT_EQ(intS, 0U); 457 458 ret = sscanf("4294967295", "%zd", &intS); 459 EXPECT_EQ(ret, 1) << "> sscanf fail, errno = " << errno; 460 EXPECT_EQ(intS, 4294967295U); 461 462 ret = sscanf("4294967295", "%15zd", &intS); 463 EXPECT_EQ(ret, 1) << "> sscanf fail, errno = " << errno; 464 EXPECT_EQ(intS, 4294967295U); 465 466 ret = sscanf("4294967295", "%5zd", &intS); 467 EXPECT_EQ(ret, 1) << "> sscanf fail, errno = " << errno; 468 EXPECT_EQ(intS, 42949U); 469 } 470 471 /** 472 * @tc.number SUB_KERNEL_IO_SSCANF_1400 473 * @tc.name sscanf basic function test of ptrdiff_t 474 * @tc.desc [C- SOFTWARE -0200] 475 */ 476 HWTEST_F(IoTest, testSscanfPtrdiffT, Function | MediumTest | Level1) 477 { 478 ptrdiff_t intP; 479 int ret = sscanf("-2147483648", "%td", &intP); 480 EXPECT_EQ(ret, 1) << "> sscanf fail, errno = " << errno; 481 EXPECT_EQ(intP, -2147483648); 482 483 ret = sscanf("2147483647", "%td", &intP); 484 EXPECT_EQ(ret, 1) << "> sscanf fail, errno = " << errno; 485 EXPECT_EQ(intP, 2147483647); 486 487 ret = sscanf("2147483647", "%15td", &intP); 488 EXPECT_EQ(ret, 1) << "> sscanf fail, errno = " << errno; 489 EXPECT_EQ(intP, 2147483647); 490 491 ret = sscanf("2147483647", "%5td", &intP); 492 EXPECT_EQ(ret, 1) << "> sscanf fail, errno = " << errno; 493 EXPECT_EQ(intP, 21474); 494 } 495 496 /** 497 * @tc.number SUB_KERNEL_IO_SSCANF_1500 498 * @tc.name sscanf basic function test of pointer 499 * @tc.desc [C- SOFTWARE -0200] 500 */ 501 HWTEST_F(IoTest, testSscanfPointer, Function | MediumTest | Level1) 502 { 503 int i; 504 int ret = sscanf("0x61", "%p", &i); 505 EXPECT_EQ(ret, 1) << "> sscanf fail, errno = " << errno; 506 EXPECT_EQ(i, 97); 507 508 ret = sscanf("0xffffff", "%15p", &i); 509 EXPECT_EQ(ret, 1) << "> sscanf fail, errno = " << errno; 510 EXPECT_EQ(i, 16777215); 511 512 ret = sscanf("0xffffff", "%6p", &i); 513 EXPECT_EQ(ret, 1) << "> sscanf fail, errno = " << errno; 514 EXPECT_EQ(i, 65535); 515 } 516 517 /** 518 * @tc.number SUB_KERNEL_IO_SSCANF_1600 519 * @tc.name sscanf basic function test of hex and oct 520 * @tc.desc [C- SOFTWARE -0200] 521 */ 522 HWTEST_F(IoTest, testSscanfIntHexOct, Function | MediumTest | Level1) 523 { 524 int i; 525 int ret = sscanf("a", "%x", &i); 526 EXPECT_EQ(ret, 1) << "> sscanf fail, errno = " << errno; 527 EXPECT_EQ(i, 10); 528 529 ret = sscanf("abaaaaa", "%6x", &i); 530 EXPECT_EQ(ret, 1) << "> sscanf fail, errno = " << errno; 531 EXPECT_EQ(i, 11250346); 532 533 ret = sscanf("aba", "%6x", &i); 534 EXPECT_EQ(ret, 1) << "> sscanf fail, errno = " << errno; 535 EXPECT_EQ(i, 2746); 536 537 ret = sscanf("A", "%X", &i); 538 EXPECT_EQ(ret, 1) << "> sscanf fail, errno = " << errno; 539 EXPECT_EQ(i, 10); 540 541 ret = sscanf("ABAAAAA", "%6X", &i); 542 EXPECT_EQ(ret, 1) << "> sscanf fail, errno = " << errno; 543 EXPECT_EQ(i, 11250346); 544 545 ret = sscanf("ABA", "%6X", &i); 546 EXPECT_EQ(ret, 1) << "> sscanf fail, errno = " << errno; 547 EXPECT_EQ(i, 2746); 548 549 ret = sscanf("12", "%o", &i); 550 EXPECT_EQ(ret, 1) << "> sscanf fail, errno = " << errno; 551 EXPECT_EQ(i, 10); 552 553 ret = sscanf("123", "%4o", &i); 554 EXPECT_EQ(ret, 1) << "> sscanf fail, errno = " << errno; 555 EXPECT_EQ(i, 83); 556 557 ret = sscanf("12345", "%4o", &i); 558 EXPECT_EQ(ret, 1) << "> sscanf fail, errno = " << errno; 559 EXPECT_EQ(i, 668); 560 } 561 562 /** 563 * @tc.number SUB_KERNEL_IO_SSCANF_1700 564 * @tc.name sscanf basic function test of regexp 565 * @tc.desc [C- SOFTWARE -0200] 566 */ 567 HWTEST_F(IoTest, testSscanfRegexp, Function | MediumTest | Level1) 568 { 569 char str[50] = {0}; 570 int ret = sscanf("hello!world", "%[^!]", str); 571 EXPECT_EQ(ret, 1) << "> sscanf fail, errno = " << errno; 572 EXPECT_STREQ(str, "hello"); 573 574 ret = sscanf("1234956abolcd", "%[1-6a-d]", str); 575 EXPECT_EQ(ret, 1) << "> sscanf fail, errno = " << errno; 576 EXPECT_STREQ(str, "1234"); 577 578 ret = sscanf("123456abolcd", "%[1-6a-d]", str); 579 EXPECT_EQ(ret, 1) << "> sscanf fail, errno = " << errno; 580 EXPECT_STREQ(str, "123456ab"); 581 582 ret = sscanf("123ABCD", "%[^A-Z]", str); 583 EXPECT_EQ(ret, 1) << "> sscanf fail, errno = " << errno; 584 EXPECT_STREQ(str, "123"); 585 586 ret = sscanf("ABC-DEF@123", "%*[^-]-%[^@]", str); 587 EXPECT_EQ(ret, 1) << "> sscanf fail, errno = " << errno; 588 EXPECT_STREQ(str, "DEF"); 589 } 590 591 /** 592 * @tc.number SUB_KERNEL_IO_SSCANF_1800 593 * @tc.name sscanf basic function test of return value 594 * @tc.desc [C- SOFTWARE -0200] 595 */ 596 HWTEST_F(IoTest, testSscanfReturnValue, Function | MediumTest | Level1) 597 { 598 char str[50] = {0}; 599 int ret = sscanf("helloworld", "%*s%s", str); 600 EXPECT_EQ(ret, -1) << "> sscanf fail, errno = " << errno; 601 602 ret = sscanf("hello", "ab%5s123%*3s|>>>|", str); 603 EXPECT_EQ(ret, 0) << "> sscanf fail, errno = " << errno; 604 605 ret = sscanf("", "%*s%s", str); 606 EXPECT_EQ(ret, -1) << "> sscanf fail, errno = " << errno; 607 608 ret = sscanf("ab/c", "%s/%c/%d/%f", str); 609 EXPECT_EQ(ret, 1) << "> sscanf fail, errno = " << errno; 610 EXPECT_STREQ(str, "ab/c"); 611 612 char c; 613 ret = sscanf("ab c", "%s %c %d/%f", str, &c); 614 EXPECT_EQ(ret, 2) << "> sscanf fail, errno = " << errno; 615 EXPECT_STREQ(str, "ab"); 616 EXPECT_EQ(c, 'c'); 617 618 int ip[4] = {0}; 619 ret = sscanf("11.11.11/11", "%d.%d.%d.%d", &ip[0], &ip[1], &ip[2], &ip[3]); 620 EXPECT_EQ(ret, 3) << "> sscanf fail, errno = " << errno; 621 EXPECT_EQ(ip[0], 11); 622 EXPECT_EQ(ip[1], 11); 623 EXPECT_EQ(ip[2], 11); 624 EXPECT_EQ(ip[3], 0); 625 } 626