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 <stdlib.h> 17 18 #include <gtest/gtest.h> 19 20 #include "log.h" 21 #include "IoTest.h" 22 23 using namespace testing::ext; 24 25 /** 26 * @tc.number SUB_KERNEL_IO_STDLIB_0100 27 * @tc.name gcvt basic function test 28 * @tc.desc [C- SOFTWARE -0200] 29 */ 30 HWTEST_F(IoTest, testGcvt, Function | MediumTest | Level1) 31 { 32 char str[50] = {0}; 33 char *ret = gcvt(999, 3, str); 34 EXPECT_STREQ(ret, str); 35 EXPECT_STREQ(str, "999"); 36 37 ret = gcvt(-99.456, 5, str); 38 EXPECT_STREQ(ret, str); 39 EXPECT_STREQ(str, "-99.456"); 40 41 ret = gcvt(0.968e8, 8, str); 42 EXPECT_STREQ(ret, str); 43 EXPECT_STREQ(str, "96800000"); 44 } 45 46 /** 47 * @tc.number SUB_KERNEL_IO_STDLIB_0200 48 * @tc.name fcvt basic function test 49 * @tc.desc [C- SOFTWARE -0200] 50 */ 51 HWTEST_F(IoTest, testFcvt, Function | MediumTest | Level1) 52 { 53 int decpt, sign; 54 char *ret = fcvt(123.456, 5, &decpt, &sign); 55 EXPECT_STREQ(ret, "12345600"); 56 EXPECT_EQ(decpt, 3); 57 EXPECT_EQ(sign, 0); 58 59 ret = fcvt(-7.89e13, 12, &decpt, &sign); 60 EXPECT_STREQ(ret, "789000000000000"); 61 EXPECT_EQ(decpt, 14); 62 EXPECT_EQ(sign, 1); 63 } 64 65 /** 66 * @tc.number SUB_KERNEL_IO_STDLIB_0300 67 * @tc.name swab basic function test 68 * @tc.desc [C- SOFTWARE -0200] 69 */ 70 HWTEST_F(IoTest, testSwab, Function | MediumTest | Level1) 71 { 72 char src[20] = "hello world"; 73 char dest[20] = {0}; 74 swab(src, dest, sizeof(src)); 75 EXPECT_STREQ(dest, "ehll oowlr"); 76 77 char destS[20] = {0}; 78 swab(src, destS, -2); 79 EXPECT_STREQ(destS, ""); 80 81 char srcT[20] = "1111111111"; 82 char destT[20] = {0}; 83 swab(srcT, destT, sizeof(srcT)); 84 EXPECT_STREQ(destT, "1111111111"); 85 } 86 87 /** 88 * @tc.number SUB_KERNEL_IO_STDLIB_0400 89 * @tc.name strtof basic function test 90 * @tc.desc [C- SOFTWARE -0200] 91 */ 92 HWTEST_F(IoTest, testStrtof, Function | MediumTest | Level1) 93 { 94 char *endPtr = nullptr; 95 float ret = strtof(" -3.40E+38 hello", &endPtr); 96 EXPECT_FLOAT_EQ(ret, -3.40E+38); 97 EXPECT_STREQ(endPtr, " hello"); 98 99 ret = strtof(" 3.40E+38 ===", &endPtr); 100 EXPECT_FLOAT_EQ(ret, 3.40E+38); 101 EXPECT_STREQ(endPtr, " ==="); 102 103 ret = strtof("-9.6e17 this 123", &endPtr); 104 EXPECT_FLOAT_EQ(ret, -9.6e17); 105 EXPECT_STREQ(endPtr, " this 123"); 106 107 ret = strtof("this is string", &endPtr); 108 EXPECT_FLOAT_EQ(ret, 0); 109 EXPECT_STREQ(endPtr, "this is string"); 110 } 111 112 /** 113 * @tc.number SUB_KERNEL_IO_STDLIB_0500 114 * @tc.name strtod basic function test 115 * @tc.desc [C- SOFTWARE -0200] 116 */ 117 HWTEST_F(IoTest, testStrtod, Function | MediumTest | Level1) 118 { 119 char *endPtr = nullptr; 120 double ret = strtod(" -1.79E+308 hello", &endPtr); 121 EXPECT_DOUBLE_EQ(ret, -1.79E+308) << "strtod fail, errno = " << errno; 122 EXPECT_STREQ(endPtr, " hello") << "strtod fail, errno = " << errno; 123 124 ret = strtod("1.79E+308 ===", &endPtr); 125 EXPECT_DOUBLE_EQ(ret, 1.79E+308) << "strtod fail, errno = " << errno; 126 EXPECT_STREQ(endPtr, " ===") << "strtod fail, errno = " << errno; 127 128 ret = strtod("-9.6e17 this 123", &endPtr); 129 EXPECT_DOUBLE_EQ(ret, -9.6e17); 130 EXPECT_STREQ(endPtr, " this 123"); 131 132 ret = strtod("this is string", &endPtr); 133 EXPECT_DOUBLE_EQ(ret, 0) << "strtod fail, errno = " << errno; 134 EXPECT_STREQ(endPtr, "this is string") << "strtod fail, errno = " << errno; 135 } 136 137 /** 138 * @tc.number SUB_KERNEL_IO_STDLIB_0600 139 * @tc.name strtold basic function test 140 * @tc.desc [C- SOFTWARE -0200] 141 */ 142 HWTEST_F(IoTest, testStrtold, Function | MediumTest | Level1) 143 { 144 char *endPtr = nullptr; 145 long double ret = strtold(" 2.22507e-308 hello", &endPtr); 146 EXPECT_NEAR(ret, 2.22507e-308, 0.0001) << "strtold fail, errno = " << errno; 147 EXPECT_STREQ(endPtr, " hello") << "strtold fail, errno = " << errno; 148 149 ret = strtold(" 1.79769e+308 ===", &endPtr); 150 EXPECT_NEAR(ret, 1.79769e+308, 0.0001) << "strtold fail, errno = " << errno; 151 EXPECT_STREQ(endPtr, " ===") << "strtold fail, errno = " << errno; 152 153 ret = strtold("-9.6e17 this 123", &endPtr); 154 EXPECT_DOUBLE_EQ(ret, -9.6e17) << "strtold fail, errno = " << errno; 155 EXPECT_STREQ(endPtr, " this 123") << "strtold fail, errno = " << errno; 156 157 ret = strtold("this is string", &endPtr); 158 EXPECT_DOUBLE_EQ(ret, 0) << "strtold fail, errno = " << errno; 159 EXPECT_STREQ(endPtr, "this is string") << "strtold fail, errno = " << errno; 160 } 161 162 163 /** 164 * @tc.number SUB_KERNEL_IO_STDLIB_0700 165 * @tc.name strtol basic function test 166 * @tc.desc [C- SOFTWARE -0200] 167 */ 168 HWTEST_F(IoTest, testStrtol, Function | MediumTest | Level1) 169 { 170 char nPtr[] = "12 0110 0XDEFE 0666 -1.6"; 171 char *endPtr = nullptr; 172 long ret = strtol(nPtr, &endPtr, 10); // decimal 173 EXPECT_EQ(ret, 12) << "> strtol fail, errno = " << errno; 174 EXPECT_STREQ(endPtr, " 0110 0XDEFE 0666 -1.6"); 175 176 ret = strtol(endPtr, &endPtr, 2); // binary 177 EXPECT_EQ(ret, 6) << "> strtol fail, errno = " << errno; 178 EXPECT_STREQ(endPtr, " 0XDEFE 0666 -1.6"); 179 180 ret = strtol(endPtr, &endPtr, 16); // hexadecimal 181 EXPECT_EQ(ret, 0XDEFE) << "> strtol fail, errno = " << errno; 182 EXPECT_STREQ(endPtr, " 0666 -1.6"); 183 184 185 ret = strtol(endPtr, &endPtr, 8); // octal 186 EXPECT_EQ(ret, 0666) << "> strtol fail, errno = " << errno; 187 EXPECT_STREQ(endPtr, " -1.6"); 188 189 ret = strtol(endPtr, &endPtr, 65); // Invalid number format 190 EXPECT_EQ(ret, 0) << "> strtol fail, errno = " << errno; 191 EXPECT_STREQ(endPtr, " -1.6"); 192 193 ret = strtol(endPtr, &endPtr, 0); // decimal 194 EXPECT_EQ(ret, -1) << "> strtol fail, errno = " << errno; 195 EXPECT_STREQ(endPtr, ".6"); 196 197 ret = strtol(endPtr, &endPtr, 10); // Invalid transform 198 EXPECT_EQ(ret, 0) << "> strtol fail, errno = " << errno; 199 EXPECT_STREQ(endPtr, ".6"); 200 201 char nPtr1[] = "2147483647 2147483648 -2147483648 -2147483649"; // boundary value:2^31-1 202 ret = strtol(nPtr1, &endPtr, 10); 203 EXPECT_EQ(ret, 2147483647) << "> strtol fail, errno = " << errno; 204 EXPECT_STREQ(endPtr, " 2147483648 -2147483648 -2147483649"); 205 206 ret = strtol(endPtr, &endPtr, 10); 207 EXPECT_EQ(ret, 2147483647) << "> strtol fail, errno = " << errno; 208 EXPECT_STREQ(endPtr, " -2147483648 -2147483649"); 209 210 ret = strtol(endPtr, &endPtr, 10); 211 EXPECT_EQ(ret, -2147483648) << "> strtol fail, errno = " << errno; 212 EXPECT_STREQ(endPtr, " -2147483649"); 213 214 ret = strtol(endPtr, &endPtr, 10); 215 EXPECT_EQ(ret, -2147483648) << "> strtol fail, errno = " << errno; 216 EXPECT_STREQ(endPtr, ""); 217 } 218 219 /** 220 * @tc.number SUB_KERNEL_IO_STDLIB_0800 221 * @tc.name strtoul basic function test 222 * @tc.desc [C- SOFTWARE -0200] 223 */ 224 HWTEST_F(IoTest, testStrtoul, Function | MediumTest | Level1) 225 { 226 char nPtr[] = "12 0110 0XDEFE 0666 4294967295 4294967296 12.34"; // 2^32-1 227 char *endPtr = nullptr; 228 unsigned long ret = strtoul(nPtr, &endPtr, 10); 229 EXPECT_EQ(ret, 12UL) << "> strtol fail, errno = " << errno; 230 EXPECT_STREQ(endPtr, " 0110 0XDEFE 0666 4294967295 4294967296 12.34"); 231 232 ret = strtoul(endPtr, &endPtr, 2); 233 EXPECT_EQ(ret, 6UL) << "> strtol fail, errno = " << errno; 234 EXPECT_STREQ(endPtr, " 0XDEFE 0666 4294967295 4294967296 12.34"); 235 236 ret = strtoul(endPtr, &endPtr, 16); 237 EXPECT_EQ(ret, 0XDEFEUL) << "> strtol fail, errno = " << errno; 238 EXPECT_STREQ(endPtr, " 0666 4294967295 4294967296 12.34"); 239 240 241 ret = strtoul(endPtr, &endPtr, 8); 242 EXPECT_EQ(ret, 0666UL) << "> strtol fail, errno = " << errno; 243 EXPECT_STREQ(endPtr, " 4294967295 4294967296 12.34"); 244 245 ret = strtoul(endPtr, &endPtr, 0); 246 EXPECT_EQ(ret, 4294967295UL) << "> strtol fail, errno = " << errno; 247 EXPECT_STREQ(endPtr, " 4294967296 12.34"); 248 249 ret = strtoul(endPtr, &endPtr, 0); 250 EXPECT_EQ(ret, 4294967295UL) << "> strtol fail, errno = " << errno; 251 EXPECT_STREQ(endPtr, " 12.34"); 252 253 ret = strtoul(endPtr, &endPtr, 65); 254 EXPECT_EQ(ret, 0UL) << "> strtol fail, errno = " << errno; 255 EXPECT_STREQ(endPtr, " 12.34"); 256 257 ret = strtoul(endPtr, &endPtr, 0); 258 EXPECT_EQ(ret, 12UL) << "> strtol fail, errno = " << errno; 259 EXPECT_STREQ(endPtr, ".34"); 260 261 ret = strtoul(endPtr, &endPtr, 0); 262 EXPECT_EQ(ret, 0UL) << "> strtol fail, errno = " << errno; 263 EXPECT_STREQ(endPtr, ".34"); 264 } 265 266 /** 267 * @tc.number SUB_KERNEL_IO_STDLIB_0900 268 * @tc.name strtoll basic function test 269 * @tc.desc [C- SOFTWARE -0200] 270 */ 271 HWTEST_F(IoTest, testStrtoll, Function | MediumTest | Level1) 272 { 273 char nPtr[] = "12 0110 0XDEFE 0666 -1.6"; 274 char *endPtr = nullptr; 275 long long ret = strtoll(nPtr, &endPtr, 10); 276 EXPECT_EQ(ret, 12) << "> strtoll fail, errno = " << errno; 277 EXPECT_STREQ(endPtr, " 0110 0XDEFE 0666 -1.6"); 278 279 ret = strtoll(endPtr, &endPtr, 2); 280 EXPECT_EQ(ret, 6) << "> strtoll fail, errno = " << errno; 281 EXPECT_STREQ(endPtr, " 0XDEFE 0666 -1.6"); 282 283 ret = strtoll(endPtr, &endPtr, 16); 284 EXPECT_EQ(ret, 0XDEFE) << "> strtoll fail, errno = " << errno; 285 EXPECT_STREQ(endPtr, " 0666 -1.6"); 286 287 288 ret = strtoll(endPtr, &endPtr, 8); 289 EXPECT_EQ(ret, 0666) << "> strtoll fail, errno = " << errno; 290 EXPECT_STREQ(endPtr, " -1.6"); 291 292 ret = strtoll(endPtr, &endPtr, 65); 293 EXPECT_EQ(ret, 0) << "> strtoll fail, errno = " << errno; 294 EXPECT_STREQ(endPtr, " -1.6"); 295 296 ret = strtoll(endPtr, &endPtr, 0); 297 EXPECT_EQ(ret, -1) << "> strtoll fail, errno = " << errno; 298 EXPECT_STREQ(endPtr, ".6"); 299 300 ret = strtoll(endPtr, &endPtr, 0); 301 EXPECT_EQ(ret, 0) << "> strtoll fail, errno = " << errno; 302 EXPECT_STREQ(endPtr, ".6"); 303 304 char nPtr1[] = "9223372036854775807 9223372036854775808 -9223372036854775807"; // 2^63-1 305 ret = strtoll(nPtr1, &endPtr, 10); 306 EXPECT_EQ(ret, 9223372036854775807) << "> strtoll fail, errno = " << errno; 307 EXPECT_STREQ(endPtr, " 9223372036854775808 -9223372036854775807"); 308 309 ret = strtoll(endPtr, &endPtr, 10); 310 EXPECT_EQ(ret, 9223372036854775807) << "> strtoll fail, errno = " << errno; 311 EXPECT_STREQ(endPtr, " -9223372036854775807"); 312 313 ret = strtoll(endPtr, &endPtr, 10); 314 EXPECT_EQ(ret, -9223372036854775807) << "> strtoll fail, errno = " << errno; 315 EXPECT_STREQ(endPtr, ""); 316 } 317 318 /** 319 * @tc.number SUB_KERNEL_IO_STDLIB_1000 320 * @tc.name strtoull basic function test 321 * @tc.desc [C- SOFTWARE -0200] 322 */ 323 HWTEST_F(IoTest, testStrtoull, Function | MediumTest | Level1) 324 { 325 char nPtr[] = "12 0110 0XDEFE 0666 1.6"; 326 char *endPtr = nullptr; 327 unsigned long long ret = strtoull(nPtr, &endPtr, 10); 328 EXPECT_EQ(ret, 12ULL) << "> strtoull fail, errno = " << errno; 329 EXPECT_STREQ(endPtr, " 0110 0XDEFE 0666 1.6"); 330 331 ret = strtoull(endPtr, &endPtr, 2); 332 EXPECT_EQ(ret, 6ULL) << "> strtoull fail, errno = " << errno; 333 EXPECT_STREQ(endPtr, " 0XDEFE 0666 1.6"); 334 335 ret = strtoull(endPtr, &endPtr, 16); 336 EXPECT_EQ(ret, 0XDEFEULL) << "> strtoull fail, errno = " << errno; 337 EXPECT_STREQ(endPtr, " 0666 1.6"); 338 339 340 ret = strtoull(endPtr, &endPtr, 8); 341 EXPECT_EQ(ret, 0666ULL) << "> strtoull fail, errno = " << errno; 342 EXPECT_STREQ(endPtr, " 1.6"); 343 344 ret = strtoull(endPtr, &endPtr, 65); 345 EXPECT_EQ(ret, 0ULL) << "> strtoull fail, errno = " << errno; 346 EXPECT_STREQ(endPtr, " 1.6"); 347 348 ret = strtoull(endPtr, &endPtr, 0); 349 EXPECT_EQ(ret, 1ULL) << "> strtoull fail, errno = " << errno; 350 EXPECT_STREQ(endPtr, ".6"); 351 352 ret = strtoull(endPtr, &endPtr, 0); 353 EXPECT_EQ(ret, 0ULL) << "> strtoull fail, errno = " << errno; 354 EXPECT_STREQ(endPtr, ".6"); 355 356 char nPtr1[] = "18446744073709551615 18446744073709551616"; // 2^64-1 357 ret = strtoull(nPtr1, &endPtr, 10); 358 EXPECT_EQ(ret, 18446744073709551615ULL) << "> strtoull fail, errno = " << errno; 359 EXPECT_STREQ(endPtr, " 18446744073709551616"); 360 361 ret = strtoull(endPtr, &endPtr, 10); 362 EXPECT_EQ(ret, 18446744073709551615ULL) << "> strtoull fail, errno = " << errno; 363 EXPECT_STREQ(endPtr, ""); 364 } 365 366 /** 367 * @tc.number SUB_KERNEL_IO_STDLIB_1100 368 * @tc.name a64l basic function test 369 * @tc.desc [C- SOFTWARE -0200] 370 */ 371 HWTEST_F(IoTest, testA64l, Function | MediumTest | Level1) 372 { 373 long ret = a64l("./0123"); 374 EXPECT_EQ(ret, 1141645376); 375 376 ret = a64l("./01234"); 377 EXPECT_EQ(ret, 1141645376); 378 379 ret = a64l("abcdef"); 380 EXPECT_EQ(ret, -358184474); 381 382 ret = a64l("Az0\0opq"); 383 EXPECT_EQ(ret, 12236); 384 385 ret = a64l("v/"); 386 EXPECT_EQ(ret, 123); 387 } 388 389