1 /* 2 * cutils.c unit-tests 3 * 4 * Copyright Red Hat 5 * 6 * Authors: 7 * Eduardo Habkost <ehabkost@redhat.com> 8 * 9 * Permission is hereby granted, free of charge, to any person obtaining a copy 10 * of this software and associated documentation files (the "Software"), to deal 11 * in the Software without restriction, including without limitation the rights 12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 * copies of the Software, and to permit persons to whom the Software is 14 * furnished to do so, subject to the following conditions: 15 * 16 * The above copyright notice and this permission notice shall be included in 17 * all copies or substantial portions of the Software. 18 * 19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 22 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 25 * THE SOFTWARE. 26 */ 27 28 #include <math.h> 29 30 #include "qemu/osdep.h" 31 #include "qemu/cutils.h" 32 #include "qemu/units.h" 33 34 static void test_parse_uint_null(void) 35 { 36 uint64_t i = 999; 37 const char *endptr = "somewhere"; 38 int r; 39 40 r = parse_uint(NULL, &endptr, 0, &i); 41 42 g_assert_cmpint(r, ==, -EINVAL); 43 g_assert_cmpuint(i, ==, 0); 44 g_assert_null(endptr); 45 } 46 47 static void test_parse_uint_empty(void) 48 { 49 uint64_t i = 999; 50 const char *endptr = "somewhere"; 51 const char *str = ""; 52 int r; 53 54 r = parse_uint(str, &endptr, 0, &i); 55 56 g_assert_cmpint(r, ==, -EINVAL); 57 g_assert_cmpuint(i, ==, 0); 58 g_assert_true(endptr == str); 59 } 60 61 static void test_parse_uint_whitespace(void) 62 { 63 uint64_t i = 999; 64 const char *endptr = "somewhere"; 65 const char *str = " \t "; 66 int r; 67 68 r = parse_uint(str, &endptr, 0, &i); 69 70 g_assert_cmpint(r, ==, -EINVAL); 71 g_assert_cmpuint(i, ==, 0); 72 g_assert_true(endptr == str); 73 } 74 75 76 static void test_parse_uint_invalid(void) 77 { 78 uint64_t i = 999; 79 const char *endptr = "somewhere"; 80 const char *str = " \t xxx"; 81 int r; 82 83 r = parse_uint(str, &endptr, 0, &i); 84 85 g_assert_cmpint(r, ==, -EINVAL); 86 g_assert_cmpuint(i, ==, 0); 87 g_assert_true(endptr == str); 88 } 89 90 91 static void test_parse_uint_trailing(void) 92 { 93 uint64_t i = 999; 94 const char *endptr = "somewhere"; 95 const char *str = "123xxx"; 96 int r; 97 98 r = parse_uint(str, &endptr, 0, &i); 99 100 g_assert_cmpint(r, ==, 0); 101 g_assert_cmpuint(i, ==, 123); 102 g_assert_true(endptr == str + 3); 103 } 104 105 static void test_parse_uint_correct(void) 106 { 107 uint64_t i = 999; 108 const char *endptr = "somewhere"; 109 const char *str = "123"; 110 int r; 111 112 r = parse_uint(str, &endptr, 0, &i); 113 114 g_assert_cmpint(r, ==, 0); 115 g_assert_cmpuint(i, ==, 123); 116 g_assert_true(endptr == str + strlen(str)); 117 } 118 119 static void test_parse_uint_octal(void) 120 { 121 uint64_t i = 999; 122 const char *endptr = "somewhere"; 123 const char *str = "0123"; 124 int r; 125 126 r = parse_uint(str, &endptr, 0, &i); 127 128 g_assert_cmpint(r, ==, 0); 129 g_assert_cmpuint(i, ==, 0123); 130 g_assert_true(endptr == str + strlen(str)); 131 } 132 133 static void test_parse_uint_decimal(void) 134 { 135 uint64_t i = 999; 136 const char *endptr = "somewhere"; 137 const char *str = "0123"; 138 int r; 139 140 r = parse_uint(str, &endptr, 10, &i); 141 142 g_assert_cmpint(r, ==, 0); 143 g_assert_cmpuint(i, ==, 123); 144 g_assert_true(endptr == str + strlen(str)); 145 } 146 147 static void test_parse_uint_llong_max(void) 148 { 149 uint64_t i = 999; 150 const char *endptr = "somewhere"; 151 char *str = g_strdup_printf("%llu", (unsigned long long)LLONG_MAX + 1); 152 int r; 153 154 r = parse_uint(str, &endptr, 0, &i); 155 156 g_assert_cmpint(r, ==, 0); 157 g_assert_cmpuint(i, ==, (unsigned long long)LLONG_MAX + 1); 158 g_assert_true(endptr == str + strlen(str)); 159 160 g_free(str); 161 } 162 163 static void test_parse_uint_max(void) 164 { 165 uint64_t i = 999; 166 const char *endptr = "somewhere"; 167 char *str = g_strdup_printf("%llu", ULLONG_MAX); 168 int r; 169 170 r = parse_uint(str, &endptr, 0, &i); 171 172 g_assert_cmpint(r, ==, 0); 173 g_assert_cmpuint(i, ==, ULLONG_MAX); 174 g_assert_true(endptr == str + strlen(str)); 175 176 g_free(str); 177 } 178 179 static void test_parse_uint_overflow(void) 180 { 181 uint64_t i; 182 const char *endptr; 183 const char *str; 184 int r; 185 186 i = 999; 187 endptr = "somewhere"; 188 str = "99999999999999999999999999999999999999"; 189 r = parse_uint(str, &endptr, 0, &i); 190 g_assert_cmpint(r, ==, -ERANGE); 191 g_assert_cmpuint(i, ==, ULLONG_MAX); 192 g_assert_true(endptr == str + strlen(str)); 193 194 i = 999; 195 endptr = "somewhere"; 196 str = "0x10000000000000000"; /* 65 bits, 64-bit sign bit clear */ 197 r = parse_uint(str, &endptr, 0, &i); 198 g_assert_cmpint(r, ==, -ERANGE); 199 g_assert_cmpuint(i, ==, ULLONG_MAX); 200 g_assert_true(endptr == str + strlen(str)); 201 202 i = 999; 203 endptr = "somewhere"; 204 str = "0x18000000080000000"; /* 65 bits, 64-bit sign bit set */ 205 r = parse_uint(str, &endptr, 0, &i); 206 g_assert_cmpint(r, ==, -ERANGE); 207 g_assert_cmpuint(i, ==, ULLONG_MAX); 208 g_assert_true(endptr == str + strlen(str)); 209 } 210 211 static void test_parse_uint_negative(void) 212 { 213 uint64_t i; 214 const char *endptr; 215 const char *str; 216 int r; 217 218 i = 999; 219 endptr = "somewhere"; 220 str = " \t -321"; 221 r = parse_uint(str, &endptr, 0, &i); 222 g_assert_cmpint(r, ==, -ERANGE); 223 g_assert_cmpuint(i, ==, 0); 224 g_assert_true(endptr == str + strlen(str)); 225 226 i = 999; 227 endptr = "somewhere"; 228 str = "-0xffffffff00000001"; 229 r = parse_uint(str, &endptr, 0, &i); 230 g_assert_cmpint(r, ==, -ERANGE); 231 g_assert_cmpuint(i, ==, 0); 232 g_assert_true(endptr == str + strlen(str)); 233 } 234 235 static void test_parse_uint_negzero(void) 236 { 237 uint64_t i = 999; 238 const char *endptr = "somewhere"; 239 const char *str = " -0"; 240 int r; 241 242 r = parse_uint(str, &endptr, 0, &i); 243 244 g_assert_cmpint(r, ==, -ERANGE); 245 g_assert_cmpuint(i, ==, 0); 246 g_assert_true(endptr == str + strlen(str)); 247 } 248 249 static void test_parse_uint_full_trailing(void) 250 { 251 uint64_t i = 999; 252 const char *str = "123xxx"; 253 int r; 254 255 r = parse_uint_full(str, 0, &i); 256 257 g_assert_cmpint(r, ==, -EINVAL); 258 g_assert_cmpuint(i, ==, 0); 259 } 260 261 static void test_parse_uint_full_correct(void) 262 { 263 uint64_t i = 999; 264 const char *str = "123"; 265 int r; 266 267 r = parse_uint_full(str, 0, &i); 268 269 g_assert_cmpint(r, ==, 0); 270 g_assert_cmpuint(i, ==, 123); 271 } 272 273 static void test_parse_uint_full_erange_junk(void) 274 { 275 /* EINVAL has priority over ERANGE */ 276 uint64_t i = 999; 277 const char *str = "-2junk"; 278 int r; 279 280 r = parse_uint_full(str, 0, &i); 281 282 g_assert_cmpint(r, ==, -EINVAL); 283 g_assert_cmpuint(i, ==, 0); 284 } 285 286 static void test_parse_uint_full_null(void) 287 { 288 uint64_t i = 999; 289 const char *str = NULL; 290 int r; 291 292 r = parse_uint_full(str, 0, &i); 293 294 g_assert_cmpint(r, ==, -EINVAL); 295 g_assert_cmpuint(i, ==, 0); 296 } 297 298 static void test_qemu_strtoi_correct(void) 299 { 300 const char *str = "12345 foo"; 301 char f = 'X'; 302 const char *endptr = &f; 303 int res = 999; 304 int err; 305 306 err = qemu_strtoi(str, &endptr, 0, &res); 307 308 g_assert_cmpint(err, ==, 0); 309 g_assert_cmpint(res, ==, 12345); 310 g_assert_true(endptr == str + 5); 311 } 312 313 static void test_qemu_strtoi_null(void) 314 { 315 char f = 'X'; 316 const char *endptr = &f; 317 int res = 999; 318 int err; 319 320 err = qemu_strtoi(NULL, &endptr, 0, &res); 321 322 g_assert_cmpint(err, ==, -EINVAL); 323 g_assert_cmpint(res, ==, 999); 324 g_assert_null(endptr); 325 } 326 327 static void test_qemu_strtoi_empty(void) 328 { 329 const char *str = ""; 330 char f = 'X'; 331 const char *endptr = &f; 332 int res = 999; 333 int err; 334 335 err = qemu_strtoi(str, &endptr, 0, &res); 336 337 g_assert_cmpint(err, ==, -EINVAL); 338 g_assert_cmpint(res, ==, 0); 339 g_assert_true(endptr == str); 340 } 341 342 static void test_qemu_strtoi_whitespace(void) 343 { 344 const char *str = " \t "; 345 char f = 'X'; 346 const char *endptr = &f; 347 int res = 999; 348 int err; 349 350 err = qemu_strtoi(str, &endptr, 0, &res); 351 352 g_assert_cmpint(err, ==, -EINVAL); 353 g_assert_cmpint(res, ==, 0); 354 g_assert_true(endptr == str); 355 } 356 357 static void test_qemu_strtoi_invalid(void) 358 { 359 const char *str = " xxxx \t abc"; 360 char f = 'X'; 361 const char *endptr = &f; 362 int res = 999; 363 int err; 364 365 err = qemu_strtoi(str, &endptr, 0, &res); 366 367 g_assert_cmpint(err, ==, -EINVAL); 368 g_assert_cmpint(res, ==, 0); 369 g_assert_true(endptr == str); 370 } 371 372 static void test_qemu_strtoi_trailing(void) 373 { 374 const char *str = "123xxx"; 375 char f = 'X'; 376 const char *endptr = &f; 377 int res = 999; 378 int err; 379 380 err = qemu_strtoi(str, &endptr, 0, &res); 381 382 g_assert_cmpint(err, ==, 0); 383 g_assert_cmpint(res, ==, 123); 384 g_assert_true(endptr == str + 3); 385 } 386 387 static void test_qemu_strtoi_octal(void) 388 { 389 const char *str = "0123"; 390 char f = 'X'; 391 const char *endptr = &f; 392 int res = 999; 393 int err; 394 395 err = qemu_strtoi(str, &endptr, 8, &res); 396 397 g_assert_cmpint(err, ==, 0); 398 g_assert_cmpint(res, ==, 0123); 399 g_assert_true(endptr == str + strlen(str)); 400 401 res = 999; 402 endptr = &f; 403 err = qemu_strtoi(str, &endptr, 0, &res); 404 405 g_assert_cmpint(err, ==, 0); 406 g_assert_cmpint(res, ==, 0123); 407 g_assert_true(endptr == str + strlen(str)); 408 } 409 410 static void test_qemu_strtoi_decimal(void) 411 { 412 const char *str = "0123"; 413 char f = 'X'; 414 const char *endptr = &f; 415 int res = 999; 416 int err; 417 418 err = qemu_strtoi(str, &endptr, 10, &res); 419 420 g_assert_cmpint(err, ==, 0); 421 g_assert_cmpint(res, ==, 123); 422 g_assert_true(endptr == str + strlen(str)); 423 424 str = "123"; 425 res = 999; 426 endptr = &f; 427 err = qemu_strtoi(str, &endptr, 0, &res); 428 429 g_assert_cmpint(err, ==, 0); 430 g_assert_cmpint(res, ==, 123); 431 g_assert_true(endptr == str + strlen(str)); 432 } 433 434 static void test_qemu_strtoi_hex(void) 435 { 436 const char *str = "0123"; 437 char f = 'X'; 438 const char *endptr = &f; 439 int res = 999; 440 int err; 441 442 err = qemu_strtoi(str, &endptr, 16, &res); 443 444 g_assert_cmpint(err, ==, 0); 445 g_assert_cmpint(res, ==, 0x123); 446 g_assert_true(endptr == str + strlen(str)); 447 448 str = "0x123"; 449 res = 999; 450 endptr = &f; 451 err = qemu_strtoi(str, &endptr, 0, &res); 452 453 g_assert_cmpint(err, ==, 0); 454 g_assert_cmpint(res, ==, 0x123); 455 g_assert_true(endptr == str + strlen(str)); 456 457 str = "0x"; 458 res = 999; 459 endptr = &f; 460 err = qemu_strtoi(str, &endptr, 16, &res); 461 462 g_assert_cmpint(err, ==, 0); 463 g_assert_cmpint(res, ==, 0); 464 g_assert_true(endptr == str + 1); 465 } 466 467 static void test_qemu_strtoi_max(void) 468 { 469 char *str = g_strdup_printf("%d", INT_MAX); 470 char f = 'X'; 471 const char *endptr = &f; 472 int res = 999; 473 int err; 474 475 err = qemu_strtoi(str, &endptr, 0, &res); 476 477 g_assert_cmpint(err, ==, 0); 478 g_assert_cmpint(res, ==, INT_MAX); 479 g_assert_true(endptr == str + strlen(str)); 480 g_free(str); 481 } 482 483 static void test_qemu_strtoi_overflow(void) 484 { 485 const char *str; 486 const char *endptr; 487 int res; 488 int err; 489 490 str = "2147483648"; /* INT_MAX + 1ll */ 491 endptr = "somewhere"; 492 res = 999; 493 err = qemu_strtoi(str, &endptr, 0, &res); 494 g_assert_cmpint(err, ==, -ERANGE); 495 g_assert_cmpint(res, ==, INT_MAX); 496 g_assert_true(endptr == str + strlen(str)); 497 498 str = "0x7fffffffffffffff"; /* LLONG_MAX */ 499 endptr = "somewhere"; 500 res = 999; 501 err = qemu_strtoi(str, &endptr, 0, &res); 502 g_assert_cmpint(err, ==, -ERANGE); 503 g_assert_cmpint(res, ==, INT_MAX); 504 g_assert_true(endptr == str + strlen(str)); 505 506 str = "0x8000000000000000"; /* (uint64_t)LLONG_MIN */ 507 endptr = "somewhere"; 508 res = 999; 509 err = qemu_strtoi(str, &endptr, 0, &res); 510 g_assert_cmpint(err, ==, -ERANGE); 511 g_assert_cmpint(res, ==, INT_MAX); 512 g_assert_true(endptr == str + strlen(str)); 513 514 str = "0x10000000000000000"; /* 65 bits, 32-bit sign bit clear */ 515 endptr = "somewhere"; 516 res = 999; 517 err = qemu_strtoi(str, &endptr, 0, &res); 518 g_assert_cmpint(err, ==, -ERANGE); 519 g_assert_cmpint(res, ==, INT_MAX); 520 g_assert_true(endptr == str + strlen(str)); 521 522 str = "0x18000000080000000"; /* 65 bits, 32-bit sign bit set */ 523 endptr = "somewhere"; 524 res = 999; 525 err = qemu_strtoi(str, &endptr, 0, &res); 526 g_assert_cmpint(err, ==, -ERANGE); 527 g_assert_cmpint(res, ==, INT_MAX); 528 g_assert_true(endptr == str + strlen(str)); 529 } 530 531 static void test_qemu_strtoi_min(void) 532 { 533 char *str = g_strdup_printf("%d", INT_MIN); 534 char f = 'X'; 535 const char *endptr = &f; 536 int res = 999; 537 int err; 538 539 err = qemu_strtoi(str, &endptr, 0, &res); 540 541 g_assert_cmpint(err, ==, 0); 542 g_assert_cmpint(res, ==, INT_MIN); 543 g_assert_true(endptr == str + strlen(str)); 544 g_free(str); 545 } 546 547 static void test_qemu_strtoi_underflow(void) 548 { 549 const char *str; 550 const char *endptr; 551 int res; 552 int err; 553 554 str = "-2147483649"; /* INT_MIN - 1ll */ 555 endptr = "somewhere"; 556 res = 999; 557 err = qemu_strtoi(str, &endptr, 0, &res); 558 g_assert_cmpint(err, ==, -ERANGE); 559 g_assert_cmpint(res, ==, INT_MIN); 560 g_assert_true(endptr == str + strlen(str)); 561 562 str = "-0x7fffffffffffffff"; /* -LLONG_MAX */ 563 endptr = "somewhere"; 564 res = 999; 565 err = qemu_strtoi(str, &endptr, 0, &res); 566 g_assert_cmpint(err, ==, -ERANGE); 567 g_assert_cmpint(res, ==, INT_MIN); 568 g_assert_true(endptr == str + strlen(str)); 569 570 str = "-0x8000000000000000"; /* (uint64_t)LLONG_MIN */ 571 endptr = "somewhere"; 572 res = 999; 573 err = qemu_strtoi(str, &endptr, 0, &res); 574 g_assert_cmpint(err, ==, -ERANGE); 575 g_assert_cmpint(res, ==, INT_MIN); 576 g_assert_true(endptr == str + strlen(str)); 577 578 str = "-18446744073709551615"; /* -UINT64_MAX (not 1) */ 579 endptr = "somewhere"; 580 res = 999; 581 err = qemu_strtoi(str, &endptr, 0, &res); 582 g_assert_cmpint(err, ==, -ERANGE); 583 g_assert_cmpint(res, ==, INT_MIN); 584 g_assert_true(endptr == str + strlen(str)); 585 586 str = "-0x10000000000000000"; /* 65 bits, 32-bit sign bit clear */ 587 endptr = "somewhere"; 588 res = 999; 589 err = qemu_strtoi(str, &endptr, 0, &res); 590 g_assert_cmpint(err, ==, -ERANGE); 591 g_assert_cmpint(res, ==, INT_MIN); 592 g_assert_true(endptr == str + strlen(str)); 593 594 str = "-0x18000000080000000"; /* 65 bits, 32-bit sign bit set */ 595 endptr = "somewhere"; 596 res = 999; 597 err = qemu_strtoi(str, &endptr, 0, &res); 598 g_assert_cmpint(err, ==, -ERANGE); 599 g_assert_cmpint(res, ==, INT_MIN); 600 g_assert_true(endptr == str + strlen(str)); 601 } 602 603 static void test_qemu_strtoi_negative(void) 604 { 605 const char *str; 606 const char *endptr; 607 int res; 608 int err; 609 610 str = " \t -321"; 611 endptr = "somewhere"; 612 res = 999; 613 err = qemu_strtoi(str, &endptr, 0, &res); 614 g_assert_cmpint(err, ==, 0); 615 g_assert_cmpint(res, ==, -321); 616 g_assert_true(endptr == str + strlen(str)); 617 618 str = "-2147483648"; /* INT_MIN */ 619 endptr = "somewhere"; 620 res = 999; 621 err = qemu_strtoi(str, &endptr, 0, &res); 622 g_assert_cmpint(err, ==, 0); 623 g_assert_cmpint(res, ==, INT_MIN); 624 g_assert_true(endptr == str + strlen(str)); 625 } 626 627 static void test_qemu_strtoi_negzero(void) 628 { 629 const char *str = " -0"; 630 char f = 'X'; 631 const char *endptr = &f; 632 int res = 999; 633 int err; 634 635 err = qemu_strtoi(str, &endptr, 0, &res); 636 637 g_assert_cmpint(err, ==, 0); 638 g_assert_cmpint(res, ==, 0); 639 g_assert_true(endptr == str + strlen(str)); 640 } 641 642 static void test_qemu_strtoi_full_correct(void) 643 { 644 const char *str = "123"; 645 int res = 999; 646 int err; 647 648 err = qemu_strtoi(str, NULL, 0, &res); 649 650 g_assert_cmpint(err, ==, 0); 651 g_assert_cmpint(res, ==, 123); 652 } 653 654 static void test_qemu_strtoi_full_null(void) 655 { 656 char f = 'X'; 657 const char *endptr = &f; 658 int res = 999; 659 int err; 660 661 err = qemu_strtoi(NULL, &endptr, 0, &res); 662 663 g_assert_cmpint(err, ==, -EINVAL); 664 g_assert_cmpint(res, ==, 999); 665 g_assert_null(endptr); 666 } 667 668 static void test_qemu_strtoi_full_empty(void) 669 { 670 const char *str = ""; 671 int res = 999; 672 int err; 673 674 err = qemu_strtoi(str, NULL, 0, &res); 675 676 g_assert_cmpint(err, ==, -EINVAL); 677 g_assert_cmpint(res, ==, 0); 678 } 679 680 static void test_qemu_strtoi_full_negative(void) 681 { 682 const char *str = " \t -321"; 683 int res = 999; 684 int err; 685 686 err = qemu_strtoi(str, NULL, 0, &res); 687 688 g_assert_cmpint(err, ==, 0); 689 g_assert_cmpint(res, ==, -321); 690 } 691 692 static void test_qemu_strtoi_full_negzero(void) 693 { 694 const char *str = " -0"; 695 int res = 999; 696 int err; 697 698 err = qemu_strtoi(str, NULL, 0, &res); 699 700 g_assert_cmpint(err, ==, 0); 701 g_assert_cmpint(res, ==, 0); 702 } 703 704 static void test_qemu_strtoi_full_trailing(void) 705 { 706 const char *str = "123xxx"; 707 int res = 999; 708 int err; 709 710 err = qemu_strtoi(str, NULL, 0, &res); 711 712 g_assert_cmpint(err, ==, -EINVAL); 713 g_assert_cmpint(res, ==, 123); 714 } 715 716 static void test_qemu_strtoi_full_max(void) 717 { 718 char *str = g_strdup_printf("%d", INT_MAX); 719 int res = 999; 720 int err; 721 722 err = qemu_strtoi(str, NULL, 0, &res); 723 724 g_assert_cmpint(err, ==, 0); 725 g_assert_cmpint(res, ==, INT_MAX); 726 g_free(str); 727 } 728 729 static void test_qemu_strtoi_full_erange_junk(void) 730 { 731 /* EINVAL has priority over ERANGE */ 732 const char *str = "-9999999999junk"; 733 int res = 999; 734 int err; 735 736 err = qemu_strtoi(str, NULL, 0, &res); 737 738 g_assert_cmpint(err, ==, -EINVAL); 739 g_assert_cmpint(res, ==, INT_MIN); 740 } 741 742 static void test_qemu_strtoui_correct(void) 743 { 744 const char *str = "12345 foo"; 745 char f = 'X'; 746 const char *endptr = &f; 747 unsigned int res = 999; 748 int err; 749 750 err = qemu_strtoui(str, &endptr, 0, &res); 751 752 g_assert_cmpint(err, ==, 0); 753 g_assert_cmpuint(res, ==, 12345); 754 g_assert_true(endptr == str + 5); 755 } 756 757 static void test_qemu_strtoui_null(void) 758 { 759 char f = 'X'; 760 const char *endptr = &f; 761 unsigned int res = 999; 762 int err; 763 764 err = qemu_strtoui(NULL, &endptr, 0, &res); 765 766 g_assert_cmpint(err, ==, -EINVAL); 767 g_assert_cmpuint(res, ==, 999); 768 g_assert_null(endptr); 769 } 770 771 static void test_qemu_strtoui_empty(void) 772 { 773 const char *str = ""; 774 char f = 'X'; 775 const char *endptr = &f; 776 unsigned int res = 999; 777 int err; 778 779 err = qemu_strtoui(str, &endptr, 0, &res); 780 781 g_assert_cmpint(err, ==, -EINVAL); 782 g_assert_cmpuint(res, ==, 0); 783 g_assert_true(endptr == str); 784 } 785 786 static void test_qemu_strtoui_whitespace(void) 787 { 788 const char *str = " \t "; 789 char f = 'X'; 790 const char *endptr = &f; 791 unsigned int res = 999; 792 int err; 793 794 err = qemu_strtoui(str, &endptr, 0, &res); 795 796 g_assert_cmpint(err, ==, -EINVAL); 797 g_assert_cmpuint(res, ==, 0); 798 g_assert_true(endptr == str); 799 } 800 801 static void test_qemu_strtoui_invalid(void) 802 { 803 const char *str = " xxxx \t abc"; 804 char f = 'X'; 805 const char *endptr = &f; 806 unsigned int res = 999; 807 int err; 808 809 err = qemu_strtoui(str, &endptr, 0, &res); 810 811 g_assert_cmpint(err, ==, -EINVAL); 812 g_assert_cmpuint(res, ==, 0); 813 g_assert_true(endptr == str); 814 } 815 816 static void test_qemu_strtoui_trailing(void) 817 { 818 const char *str = "123xxx"; 819 char f = 'X'; 820 const char *endptr = &f; 821 unsigned int res = 999; 822 int err; 823 824 err = qemu_strtoui(str, &endptr, 0, &res); 825 826 g_assert_cmpint(err, ==, 0); 827 g_assert_cmpuint(res, ==, 123); 828 g_assert_true(endptr == str + 3); 829 } 830 831 static void test_qemu_strtoui_octal(void) 832 { 833 const char *str = "0123"; 834 char f = 'X'; 835 const char *endptr = &f; 836 unsigned int res = 999; 837 int err; 838 839 err = qemu_strtoui(str, &endptr, 8, &res); 840 841 g_assert_cmpint(err, ==, 0); 842 g_assert_cmpuint(res, ==, 0123); 843 g_assert_true(endptr == str + strlen(str)); 844 845 res = 999; 846 endptr = &f; 847 err = qemu_strtoui(str, &endptr, 0, &res); 848 849 g_assert_cmpint(err, ==, 0); 850 g_assert_cmpuint(res, ==, 0123); 851 g_assert_true(endptr == str + strlen(str)); 852 } 853 854 static void test_qemu_strtoui_decimal(void) 855 { 856 const char *str = "0123"; 857 char f = 'X'; 858 const char *endptr = &f; 859 unsigned int res = 999; 860 int err; 861 862 err = qemu_strtoui(str, &endptr, 10, &res); 863 864 g_assert_cmpint(err, ==, 0); 865 g_assert_cmpuint(res, ==, 123); 866 g_assert_true(endptr == str + strlen(str)); 867 868 str = "123"; 869 res = 999; 870 endptr = &f; 871 err = qemu_strtoui(str, &endptr, 0, &res); 872 873 g_assert_cmpint(err, ==, 0); 874 g_assert_cmpuint(res, ==, 123); 875 g_assert_true(endptr == str + strlen(str)); 876 } 877 878 static void test_qemu_strtoui_hex(void) 879 { 880 const char *str = "0123"; 881 char f = 'X'; 882 const char *endptr = &f; 883 unsigned int res = 999; 884 int err; 885 886 err = qemu_strtoui(str, &endptr, 16, &res); 887 888 g_assert_cmpint(err, ==, 0); 889 g_assert_cmphex(res, ==, 0x123); 890 g_assert_true(endptr == str + strlen(str)); 891 892 str = "0x123"; 893 res = 999; 894 endptr = &f; 895 err = qemu_strtoui(str, &endptr, 0, &res); 896 897 g_assert_cmpint(err, ==, 0); 898 g_assert_cmphex(res, ==, 0x123); 899 g_assert_true(endptr == str + strlen(str)); 900 901 str = "0x"; 902 res = 999; 903 endptr = &f; 904 err = qemu_strtoui(str, &endptr, 16, &res); 905 906 g_assert_cmpint(err, ==, 0); 907 g_assert_cmphex(res, ==, 0); 908 g_assert_true(endptr == str + 1); 909 } 910 911 static void test_qemu_strtoui_wrap(void) 912 { 913 /* wraparound is consistent with 32-bit strtoul */ 914 const char *str = "-4294967295"; /* 1 mod 2^32 */ 915 char f = 'X'; 916 const char *endptr = &f; 917 unsigned int res = 999; 918 int err; 919 920 err = qemu_strtoui(str, &endptr, 0, &res); 921 922 g_assert_cmpint(err, ==, 0); 923 g_assert_cmphex(res, ==, 1); 924 g_assert_true(endptr == str + strlen(str)); 925 } 926 927 static void test_qemu_strtoui_max(void) 928 { 929 char *str = g_strdup_printf("%u", UINT_MAX); 930 char f = 'X'; 931 const char *endptr = &f; 932 unsigned int res = 999; 933 int err; 934 935 err = qemu_strtoui(str, &endptr, 0, &res); 936 937 g_assert_cmpint(err, ==, 0); 938 g_assert_cmphex(res, ==, UINT_MAX); 939 g_assert_true(endptr == str + strlen(str)); 940 g_free(str); 941 } 942 943 static void test_qemu_strtoui_overflow(void) 944 { 945 const char *str; 946 const char *endptr; 947 unsigned int res; 948 int err; 949 950 str = "4294967296"; /* UINT_MAX + 1ll */ 951 endptr = "somewhere"; 952 res = 999; 953 err = qemu_strtoui(str, &endptr, 0, &res); 954 g_assert_cmpint(err, ==, -ERANGE); 955 g_assert_cmpuint(res, ==, UINT_MAX); 956 g_assert_true(endptr == str + strlen(str)); 957 958 str = "0x7fffffffffffffff"; /* LLONG_MAX */ 959 endptr = "somewhere"; 960 res = 999; 961 err = qemu_strtoui(str, &endptr, 0, &res); 962 g_assert_cmpint(err, ==, -ERANGE); 963 g_assert_cmpuint(res, ==, UINT_MAX); 964 g_assert_true(endptr == str + strlen(str)); 965 966 str = "0x8000000000000000"; /* (uint64_t)LLONG_MIN */ 967 endptr = "somewhere"; 968 res = 999; 969 err = qemu_strtoui(str, &endptr, 0, &res); 970 g_assert_cmpint(err, ==, -ERANGE); 971 g_assert_cmpuint(res, ==, UINT_MAX); 972 g_assert_true(endptr == str + strlen(str)); 973 974 str = "0xffffffff00000001"; /* ULLONG_MAX - UINT_MAX + 1 (not 1) */ 975 endptr = "somewhere"; 976 res = 999; 977 err = qemu_strtoui(str, &endptr, 0, &res); 978 g_assert_cmpint(err, ==, -ERANGE); 979 g_assert_cmpuint(res, ==, UINT_MAX); 980 g_assert_true(endptr == str + strlen(str)); 981 982 str = "0xfffffffffffffffe"; /* ULLONG_MAX - 1 (not UINT_MAX - 1) */ 983 endptr = "somewhere"; 984 res = 999; 985 err = qemu_strtoui(str, &endptr, 0, &res); 986 g_assert_cmpint(err, ==, -ERANGE); 987 g_assert_cmpuint(res, ==, UINT_MAX); 988 g_assert_true(endptr == str + strlen(str)); 989 990 str = "0x10000000000000000"; /* 65 bits, 32-bit sign bit clear */ 991 endptr = "somewhere"; 992 res = 999; 993 err = qemu_strtoui(str, &endptr, 0, &res); 994 g_assert_cmpint(err, ==, -ERANGE); 995 g_assert_cmpuint(res, ==, UINT_MAX); 996 g_assert_true(endptr == str + strlen(str)); 997 998 str = "0x18000000080000000"; /* 65 bits, 32-bit sign bit set */ 999 endptr = "somewhere"; 1000 res = 999; 1001 err = qemu_strtoui(str, &endptr, 0, &res); 1002 g_assert_cmpint(err, ==, -ERANGE); 1003 g_assert_cmpuint(res, ==, UINT_MAX); 1004 g_assert_true(endptr == str + strlen(str)); 1005 } 1006 1007 static void test_qemu_strtoui_underflow(void) 1008 { 1009 const char *str; 1010 const char *endptr; 1011 unsigned int res; 1012 int err; 1013 1014 str = "-4294967296"; /* -(long long)UINT_MAX - 1ll */ 1015 endptr = "somewhere"; 1016 res = 999; 1017 err = qemu_strtoui(str, &endptr, 0, &res); 1018 g_assert_cmpint(err, ==, -ERANGE); 1019 g_assert_cmpuint(res, ==, UINT_MAX); 1020 g_assert_true(endptr == str + strlen(str)); 1021 1022 str = "-18446744073709551615"; /* -UINT64_MAX (not -(-1)) */ 1023 endptr = "somewhere"; 1024 res = 999; 1025 err = qemu_strtoui(str, &endptr, 0, &res); 1026 g_assert_cmpint(err, ==, -ERANGE); 1027 g_assert_cmpuint(res, ==, UINT_MAX); 1028 g_assert_true(endptr == str + strlen(str)); 1029 1030 str = "-0xffffffff00000002"; 1031 endptr = "somewhere"; 1032 res = 999; 1033 err = qemu_strtoui(str, &endptr, 0, &res); 1034 g_assert_cmpint(err, ==, -ERANGE); 1035 g_assert_cmpuint(res, ==, UINT_MAX); 1036 g_assert_true(endptr == str + strlen(str)); 1037 1038 str = "-0x10000000000000000"; /* 65 bits, 32-bit sign bit clear */ 1039 endptr = "somewhere"; 1040 res = 999; 1041 err = qemu_strtoui(str, &endptr, 0, &res); 1042 g_assert_cmpint(err, ==, -ERANGE); 1043 g_assert_cmpuint(res, ==, UINT_MAX); 1044 g_assert_true(endptr == str + strlen(str)); 1045 1046 str = "-0x18000000080000000"; /* 65 bits, 32-bit sign bit set */ 1047 endptr = "somewhere"; 1048 res = 999; 1049 err = qemu_strtoui(str, &endptr, 0, &res); 1050 g_assert_cmpint(err, ==, -ERANGE); 1051 g_assert_cmpuint(res, ==, UINT_MAX); 1052 g_assert_true(endptr == str + strlen(str)); 1053 } 1054 1055 static void test_qemu_strtoui_negative(void) 1056 { 1057 const char *str = " \t -321"; 1058 char f = 'X'; 1059 const char *endptr = &f; 1060 unsigned int res = 999; 1061 int err; 1062 1063 err = qemu_strtoui(str, &endptr, 0, &res); 1064 1065 g_assert_cmpint(err, ==, 0); 1066 g_assert_cmpuint(res, ==, (unsigned int)-321); 1067 g_assert_true(endptr == str + strlen(str)); 1068 } 1069 1070 static void test_qemu_strtoui_negzero(void) 1071 { 1072 const char *str = " -0"; 1073 char f = 'X'; 1074 const char *endptr = &f; 1075 unsigned int res = 999; 1076 int err; 1077 1078 err = qemu_strtoui(str, &endptr, 0, &res); 1079 1080 g_assert_cmpint(err, ==, 0); 1081 g_assert_cmpuint(res, ==, 0); 1082 g_assert_true(endptr == str + strlen(str)); 1083 } 1084 1085 static void test_qemu_strtoui_full_correct(void) 1086 { 1087 const char *str = "123"; 1088 unsigned int res = 999; 1089 int err; 1090 1091 err = qemu_strtoui(str, NULL, 0, &res); 1092 1093 g_assert_cmpint(err, ==, 0); 1094 g_assert_cmpuint(res, ==, 123); 1095 } 1096 1097 static void test_qemu_strtoui_full_null(void) 1098 { 1099 unsigned int res = 999; 1100 int err; 1101 1102 err = qemu_strtoui(NULL, NULL, 0, &res); 1103 1104 g_assert_cmpint(err, ==, -EINVAL); 1105 g_assert_cmpuint(res, ==, 999); 1106 } 1107 1108 static void test_qemu_strtoui_full_empty(void) 1109 { 1110 const char *str = ""; 1111 unsigned int res = 999; 1112 int err; 1113 1114 err = qemu_strtoui(str, NULL, 0, &res); 1115 1116 g_assert_cmpint(err, ==, -EINVAL); 1117 g_assert_cmpuint(res, ==, 0); 1118 } 1119 1120 static void test_qemu_strtoui_full_negative(void) 1121 { 1122 const char *str = " \t -321"; 1123 unsigned int res = 999; 1124 int err; 1125 1126 err = qemu_strtoui(str, NULL, 0, &res); 1127 g_assert_cmpint(err, ==, 0); 1128 g_assert_cmpuint(res, ==, (unsigned int)-321); 1129 } 1130 1131 static void test_qemu_strtoui_full_negzero(void) 1132 { 1133 const char *str = " -0"; 1134 unsigned int res = 999; 1135 int err; 1136 1137 err = qemu_strtoui(str, NULL, 0, &res); 1138 g_assert_cmpint(err, ==, 0); 1139 g_assert_cmpuint(res, ==, 0); 1140 } 1141 1142 static void test_qemu_strtoui_full_trailing(void) 1143 { 1144 const char *str = "123xxx"; 1145 unsigned int res = 999; 1146 int err; 1147 1148 err = qemu_strtoui(str, NULL, 0, &res); 1149 1150 g_assert_cmpint(err, ==, -EINVAL); 1151 g_assert_cmpuint(res, ==, 123); 1152 } 1153 1154 static void test_qemu_strtoui_full_max(void) 1155 { 1156 char *str = g_strdup_printf("%u", UINT_MAX); 1157 unsigned int res = 999; 1158 int err; 1159 1160 err = qemu_strtoui(str, NULL, 0, &res); 1161 1162 g_assert_cmpint(err, ==, 0); 1163 g_assert_cmphex(res, ==, UINT_MAX); 1164 g_free(str); 1165 } 1166 1167 static void test_qemu_strtoui_full_erange_junk(void) 1168 { 1169 /* EINVAL has priority over ERANGE */ 1170 const char *str = "-9999999999junk"; 1171 unsigned int res = 999; 1172 int err; 1173 1174 err = qemu_strtoui(str, NULL, 0, &res); 1175 1176 g_assert_cmpint(err, ==, -EINVAL); 1177 g_assert_cmpuint(res, ==, UINT_MAX); 1178 } 1179 1180 static void test_qemu_strtol_correct(void) 1181 { 1182 const char *str = "12345 foo"; 1183 char f = 'X'; 1184 const char *endptr = &f; 1185 long res = 999; 1186 int err; 1187 1188 err = qemu_strtol(str, &endptr, 0, &res); 1189 1190 g_assert_cmpint(err, ==, 0); 1191 g_assert_cmpint(res, ==, 12345); 1192 g_assert_true(endptr == str + 5); 1193 } 1194 1195 static void test_qemu_strtol_null(void) 1196 { 1197 char f = 'X'; 1198 const char *endptr = &f; 1199 long res = 999; 1200 int err; 1201 1202 err = qemu_strtol(NULL, &endptr, 0, &res); 1203 1204 g_assert_cmpint(err, ==, -EINVAL); 1205 g_assert_cmpint(res, ==, 999); 1206 g_assert_null(endptr); 1207 } 1208 1209 static void test_qemu_strtol_empty(void) 1210 { 1211 const char *str = ""; 1212 char f = 'X'; 1213 const char *endptr = &f; 1214 long res = 999; 1215 int err; 1216 1217 err = qemu_strtol(str, &endptr, 0, &res); 1218 1219 g_assert_cmpint(err, ==, -EINVAL); 1220 g_assert_cmpint(res, ==, 0); 1221 g_assert_true(endptr == str); 1222 } 1223 1224 static void test_qemu_strtol_whitespace(void) 1225 { 1226 const char *str = " \t "; 1227 char f = 'X'; 1228 const char *endptr = &f; 1229 long res = 999; 1230 int err; 1231 1232 err = qemu_strtol(str, &endptr, 0, &res); 1233 1234 g_assert_cmpint(err, ==, -EINVAL); 1235 g_assert_cmpint(res, ==, 0); 1236 g_assert_true(endptr == str); 1237 } 1238 1239 static void test_qemu_strtol_invalid(void) 1240 { 1241 const char *str = " xxxx \t abc"; 1242 char f = 'X'; 1243 const char *endptr = &f; 1244 long res = 999; 1245 int err; 1246 1247 err = qemu_strtol(str, &endptr, 0, &res); 1248 1249 g_assert_cmpint(err, ==, -EINVAL); 1250 g_assert_cmpint(res, ==, 0); 1251 g_assert_true(endptr == str); 1252 } 1253 1254 static void test_qemu_strtol_trailing(void) 1255 { 1256 const char *str = "123xxx"; 1257 char f = 'X'; 1258 const char *endptr = &f; 1259 long res = 999; 1260 int err; 1261 1262 err = qemu_strtol(str, &endptr, 0, &res); 1263 1264 g_assert_cmpint(err, ==, 0); 1265 g_assert_cmpint(res, ==, 123); 1266 g_assert_true(endptr == str + 3); 1267 } 1268 1269 static void test_qemu_strtol_octal(void) 1270 { 1271 const char *str = "0123"; 1272 char f = 'X'; 1273 const char *endptr = &f; 1274 long res = 999; 1275 int err; 1276 1277 err = qemu_strtol(str, &endptr, 8, &res); 1278 1279 g_assert_cmpint(err, ==, 0); 1280 g_assert_cmpint(res, ==, 0123); 1281 g_assert_true(endptr == str + strlen(str)); 1282 1283 res = 999; 1284 endptr = &f; 1285 err = qemu_strtol(str, &endptr, 0, &res); 1286 1287 g_assert_cmpint(err, ==, 0); 1288 g_assert_cmpint(res, ==, 0123); 1289 g_assert_true(endptr == str + strlen(str)); 1290 } 1291 1292 static void test_qemu_strtol_decimal(void) 1293 { 1294 const char *str = "0123"; 1295 char f = 'X'; 1296 const char *endptr = &f; 1297 long res = 999; 1298 int err; 1299 1300 err = qemu_strtol(str, &endptr, 10, &res); 1301 1302 g_assert_cmpint(err, ==, 0); 1303 g_assert_cmpint(res, ==, 123); 1304 g_assert_true(endptr == str + strlen(str)); 1305 1306 str = "123"; 1307 res = 999; 1308 endptr = &f; 1309 err = qemu_strtol(str, &endptr, 0, &res); 1310 1311 g_assert_cmpint(err, ==, 0); 1312 g_assert_cmpint(res, ==, 123); 1313 g_assert_true(endptr == str + strlen(str)); 1314 } 1315 1316 static void test_qemu_strtol_hex(void) 1317 { 1318 const char *str = "0123"; 1319 char f = 'X'; 1320 const char *endptr = &f; 1321 long res = 999; 1322 int err; 1323 1324 err = qemu_strtol(str, &endptr, 16, &res); 1325 1326 g_assert_cmpint(err, ==, 0); 1327 g_assert_cmpint(res, ==, 0x123); 1328 g_assert_true(endptr == str + strlen(str)); 1329 1330 str = "0x123"; 1331 res = 999; 1332 endptr = &f; 1333 err = qemu_strtol(str, &endptr, 0, &res); 1334 1335 g_assert_cmpint(err, ==, 0); 1336 g_assert_cmpint(res, ==, 0x123); 1337 g_assert_true(endptr == str + strlen(str)); 1338 1339 str = "0x"; 1340 res = 999; 1341 endptr = &f; 1342 err = qemu_strtol(str, &endptr, 16, &res); 1343 1344 g_assert_cmpint(err, ==, 0); 1345 g_assert_cmpint(res, ==, 0); 1346 g_assert_true(endptr == str + 1); 1347 } 1348 1349 static void test_qemu_strtol_max(void) 1350 { 1351 char *str = g_strdup_printf("%ld", LONG_MAX); 1352 char f = 'X'; 1353 const char *endptr = &f; 1354 long res = 999; 1355 int err; 1356 1357 err = qemu_strtol(str, &endptr, 0, &res); 1358 1359 g_assert_cmpint(err, ==, 0); 1360 g_assert_cmpint(res, ==, LONG_MAX); 1361 g_assert_true(endptr == str + strlen(str)); 1362 g_free(str); 1363 } 1364 1365 static void test_qemu_strtol_overflow(void) 1366 { 1367 const char *str; 1368 const char *endptr; 1369 long res; 1370 int err; 1371 1372 /* 1 more than LONG_MAX */ 1373 str = LONG_MAX == INT_MAX ? "2147483648" : "9223372036854775808"; 1374 endptr = "somewhere"; 1375 res = 999; 1376 err = qemu_strtol(str, &endptr, 0, &res); 1377 g_assert_cmpint(err, ==, -ERANGE); 1378 g_assert_cmpint(res, ==, LONG_MAX); 1379 g_assert_true(endptr == str + strlen(str)); 1380 1381 if (LONG_MAX == INT_MAX) { 1382 str = "0xffffffff00000001"; /* ULLONG_MAX - UINT_MAX + 1 (not 1) */ 1383 endptr = "somewhere"; 1384 res = 999; 1385 err = qemu_strtol(str, &endptr, 0, &res); 1386 g_assert_cmpint(err, ==, -ERANGE); 1387 g_assert_cmpint(res, ==, LONG_MAX); 1388 g_assert_true(endptr == str + strlen(str)); 1389 } 1390 1391 str = "0x10000000000000000"; /* 65 bits, either sign bit position clear */ 1392 endptr = "somewhere"; 1393 res = 999; 1394 err = qemu_strtol(str, &endptr, 0, &res); 1395 g_assert_cmpint(err, ==, -ERANGE); 1396 g_assert_cmpint(res, ==, LONG_MAX); 1397 g_assert_true(endptr == str + strlen(str)); 1398 1399 str = "0x18000000080000000"; /* 65 bits, either sign bit position set */ 1400 endptr = "somewhere"; 1401 res = 999; 1402 err = qemu_strtol(str, &endptr, 0, &res); 1403 g_assert_cmpint(err, ==, -ERANGE); 1404 g_assert_cmpint(res, ==, LONG_MAX); 1405 g_assert_true(endptr == str + strlen(str)); 1406 } 1407 1408 static void test_qemu_strtol_min(void) 1409 { 1410 char *str = g_strdup_printf("%ld", LONG_MIN); 1411 char f = 'X'; 1412 const char *endptr = &f; 1413 long res = 999; 1414 int err; 1415 1416 err = qemu_strtol(str, &endptr, 0, &res); 1417 1418 g_assert_cmpint(err, ==, 0); 1419 g_assert_cmpint(res, ==, LONG_MIN); 1420 g_assert_true(endptr == str + strlen(str)); 1421 g_free(str); 1422 } 1423 1424 static void test_qemu_strtol_underflow(void) 1425 { 1426 const char *str; 1427 const char *endptr; 1428 long res; 1429 int err; 1430 1431 /* 1 less than LONG_MIN */ 1432 str = LONG_MIN == INT_MIN ? "-2147483649" : "-9223372036854775809"; 1433 endptr = "somewhere"; 1434 res = 999; 1435 err = qemu_strtol(str, &endptr, 0, &res); 1436 g_assert_cmpint(err, ==, -ERANGE); 1437 g_assert_cmpint(res, ==, LONG_MIN); 1438 g_assert_true(endptr == str + strlen(str)); 1439 1440 if (LONG_MAX == INT_MAX) { 1441 str = "-18446744073709551615"; /* -UINT64_MAX (not 1) */ 1442 endptr = "somewhere"; 1443 res = 999; 1444 err = qemu_strtol(str, &endptr, 0, &res); 1445 g_assert_cmpint(err, ==, -ERANGE); 1446 g_assert_cmpint(res, ==, LONG_MIN); 1447 g_assert_true(endptr == str + strlen(str)); 1448 } 1449 1450 str = "-0x10000000000000000"; /* 65 bits, either sign bit position clear */ 1451 endptr = "somewhere"; 1452 res = 999; 1453 err = qemu_strtol(str, &endptr, 0, &res); 1454 g_assert_cmpint(err, ==, -ERANGE); 1455 g_assert_cmpint(res, ==, LONG_MIN); 1456 g_assert_true(endptr == str + strlen(str)); 1457 1458 str = "-0x18000000080000000"; /* 65 bits, either sign bit position set */ 1459 endptr = "somewhere"; 1460 res = 999; 1461 err = qemu_strtol(str, &endptr, 0, &res); 1462 g_assert_cmpint(err, ==, -ERANGE); 1463 g_assert_cmpint(res, ==, LONG_MIN); 1464 g_assert_true(endptr == str + strlen(str)); 1465 } 1466 1467 static void test_qemu_strtol_negative(void) 1468 { 1469 const char *str = " \t -321"; 1470 char f = 'X'; 1471 const char *endptr = &f; 1472 long res = 999; 1473 int err; 1474 1475 err = qemu_strtol(str, &endptr, 0, &res); 1476 1477 g_assert_cmpint(err, ==, 0); 1478 g_assert_cmpint(res, ==, -321); 1479 g_assert_true(endptr == str + strlen(str)); 1480 } 1481 1482 static void test_qemu_strtol_negzero(void) 1483 { 1484 const char *str = " -0"; 1485 char f = 'X'; 1486 const char *endptr = &f; 1487 long res = 999; 1488 int err; 1489 1490 err = qemu_strtol(str, &endptr, 0, &res); 1491 1492 g_assert_cmpint(err, ==, 0); 1493 g_assert_cmpint(res, ==, 0); 1494 g_assert_true(endptr == str + strlen(str)); 1495 } 1496 1497 static void test_qemu_strtol_full_correct(void) 1498 { 1499 const char *str = "123"; 1500 long res = 999; 1501 int err; 1502 1503 err = qemu_strtol(str, NULL, 0, &res); 1504 1505 g_assert_cmpint(err, ==, 0); 1506 g_assert_cmpint(res, ==, 123); 1507 } 1508 1509 static void test_qemu_strtol_full_null(void) 1510 { 1511 char f = 'X'; 1512 const char *endptr = &f; 1513 long res = 999; 1514 int err; 1515 1516 err = qemu_strtol(NULL, &endptr, 0, &res); 1517 1518 g_assert_cmpint(err, ==, -EINVAL); 1519 g_assert_cmpint(res, ==, 999); 1520 g_assert_null(endptr); 1521 } 1522 1523 static void test_qemu_strtol_full_empty(void) 1524 { 1525 const char *str = ""; 1526 long res = 999L; 1527 int err; 1528 1529 err = qemu_strtol(str, NULL, 0, &res); 1530 1531 g_assert_cmpint(err, ==, -EINVAL); 1532 g_assert_cmpint(res, ==, 0); 1533 } 1534 1535 static void test_qemu_strtol_full_negative(void) 1536 { 1537 const char *str = " \t -321"; 1538 long res = 999; 1539 int err; 1540 1541 err = qemu_strtol(str, NULL, 0, &res); 1542 1543 g_assert_cmpint(err, ==, 0); 1544 g_assert_cmpint(res, ==, -321); 1545 } 1546 1547 static void test_qemu_strtol_full_negzero(void) 1548 { 1549 const char *str = " -0"; 1550 long res = 999; 1551 int err; 1552 1553 err = qemu_strtol(str, NULL, 0, &res); 1554 1555 g_assert_cmpint(err, ==, 0); 1556 g_assert_cmpint(res, ==, 0); 1557 } 1558 1559 static void test_qemu_strtol_full_trailing(void) 1560 { 1561 const char *str = "123xxx"; 1562 long res = 999; 1563 int err; 1564 1565 err = qemu_strtol(str, NULL, 0, &res); 1566 1567 g_assert_cmpint(err, ==, -EINVAL); 1568 g_assert_cmpint(res, ==, 123); 1569 } 1570 1571 static void test_qemu_strtol_full_max(void) 1572 { 1573 char *str = g_strdup_printf("%ld", LONG_MAX); 1574 long res = 999; 1575 int err; 1576 1577 err = qemu_strtol(str, NULL, 0, &res); 1578 1579 g_assert_cmpint(err, ==, 0); 1580 g_assert_cmpint(res, ==, LONG_MAX); 1581 g_free(str); 1582 } 1583 1584 static void test_qemu_strtol_full_erange_junk(void) 1585 { 1586 /* EINVAL has priority over ERANGE */ 1587 const char *str = "-99999999999999999999junk"; 1588 long res = 999; 1589 int err; 1590 1591 err = qemu_strtol(str, NULL, 0, &res); 1592 1593 g_assert_cmpint(err, ==, -EINVAL); 1594 g_assert_cmpint(res, ==, LONG_MIN); 1595 } 1596 1597 static void test_qemu_strtoul_correct(void) 1598 { 1599 const char *str = "12345 foo"; 1600 char f = 'X'; 1601 const char *endptr = &f; 1602 unsigned long res = 999; 1603 int err; 1604 1605 err = qemu_strtoul(str, &endptr, 0, &res); 1606 1607 g_assert_cmpint(err, ==, 0); 1608 g_assert_cmpuint(res, ==, 12345); 1609 g_assert_true(endptr == str + 5); 1610 } 1611 1612 static void test_qemu_strtoul_null(void) 1613 { 1614 char f = 'X'; 1615 const char *endptr = &f; 1616 unsigned long res = 999; 1617 int err; 1618 1619 err = qemu_strtoul(NULL, &endptr, 0, &res); 1620 1621 g_assert_cmpint(err, ==, -EINVAL); 1622 g_assert_cmpuint(res, ==, 999); 1623 g_assert_null(endptr); 1624 } 1625 1626 static void test_qemu_strtoul_empty(void) 1627 { 1628 const char *str = ""; 1629 char f = 'X'; 1630 const char *endptr = &f; 1631 unsigned long res = 999; 1632 int err; 1633 1634 err = qemu_strtoul(str, &endptr, 0, &res); 1635 1636 g_assert_cmpint(err, ==, -EINVAL); 1637 g_assert_cmpuint(res, ==, 0); 1638 g_assert_true(endptr == str); 1639 } 1640 1641 static void test_qemu_strtoul_whitespace(void) 1642 { 1643 const char *str = " \t "; 1644 char f = 'X'; 1645 const char *endptr = &f; 1646 unsigned long res = 999; 1647 int err; 1648 1649 err = qemu_strtoul(str, &endptr, 0, &res); 1650 1651 g_assert_cmpint(err, ==, -EINVAL); 1652 g_assert_cmpuint(res, ==, 0); 1653 g_assert_true(endptr == str); 1654 } 1655 1656 static void test_qemu_strtoul_invalid(void) 1657 { 1658 const char *str = " xxxx \t abc"; 1659 char f = 'X'; 1660 const char *endptr = &f; 1661 unsigned long res = 999; 1662 int err; 1663 1664 err = qemu_strtoul(str, &endptr, 0, &res); 1665 1666 g_assert_cmpint(err, ==, -EINVAL); 1667 g_assert_cmpuint(res, ==, 0); 1668 g_assert_true(endptr == str); 1669 } 1670 1671 static void test_qemu_strtoul_trailing(void) 1672 { 1673 const char *str = "123xxx"; 1674 char f = 'X'; 1675 const char *endptr = &f; 1676 unsigned long res = 999; 1677 int err; 1678 1679 err = qemu_strtoul(str, &endptr, 0, &res); 1680 1681 g_assert_cmpint(err, ==, 0); 1682 g_assert_cmpuint(res, ==, 123); 1683 g_assert_true(endptr == str + 3); 1684 } 1685 1686 static void test_qemu_strtoul_octal(void) 1687 { 1688 const char *str = "0123"; 1689 char f = 'X'; 1690 const char *endptr = &f; 1691 unsigned long res = 999; 1692 int err; 1693 1694 err = qemu_strtoul(str, &endptr, 8, &res); 1695 1696 g_assert_cmpint(err, ==, 0); 1697 g_assert_cmpuint(res, ==, 0123); 1698 g_assert_true(endptr == str + strlen(str)); 1699 1700 res = 999; 1701 endptr = &f; 1702 err = qemu_strtoul(str, &endptr, 0, &res); 1703 1704 g_assert_cmpint(err, ==, 0); 1705 g_assert_cmpuint(res, ==, 0123); 1706 g_assert_true(endptr == str + strlen(str)); 1707 } 1708 1709 static void test_qemu_strtoul_decimal(void) 1710 { 1711 const char *str = "0123"; 1712 char f = 'X'; 1713 const char *endptr = &f; 1714 unsigned long res = 999; 1715 int err; 1716 1717 err = qemu_strtoul(str, &endptr, 10, &res); 1718 1719 g_assert_cmpint(err, ==, 0); 1720 g_assert_cmpuint(res, ==, 123); 1721 g_assert_true(endptr == str + strlen(str)); 1722 1723 str = "123"; 1724 res = 999; 1725 endptr = &f; 1726 err = qemu_strtoul(str, &endptr, 0, &res); 1727 1728 g_assert_cmpint(err, ==, 0); 1729 g_assert_cmpuint(res, ==, 123); 1730 g_assert_true(endptr == str + strlen(str)); 1731 } 1732 1733 static void test_qemu_strtoul_hex(void) 1734 { 1735 const char *str = "0123"; 1736 char f = 'X'; 1737 const char *endptr = &f; 1738 unsigned long res = 999; 1739 int err; 1740 1741 err = qemu_strtoul(str, &endptr, 16, &res); 1742 1743 g_assert_cmpint(err, ==, 0); 1744 g_assert_cmphex(res, ==, 0x123); 1745 g_assert_true(endptr == str + strlen(str)); 1746 1747 str = "0x123"; 1748 res = 999; 1749 endptr = &f; 1750 err = qemu_strtoul(str, &endptr, 0, &res); 1751 1752 g_assert_cmpint(err, ==, 0); 1753 g_assert_cmphex(res, ==, 0x123); 1754 g_assert_true(endptr == str + strlen(str)); 1755 1756 str = "0x"; 1757 res = 999; 1758 endptr = &f; 1759 err = qemu_strtoul(str, &endptr, 16, &res); 1760 1761 g_assert_cmpint(err, ==, 0); 1762 g_assert_cmphex(res, ==, 0); 1763 g_assert_true(endptr == str + 1); 1764 } 1765 1766 static void test_qemu_strtoul_wrap(void) 1767 { 1768 const char *str; 1769 char f = 'X'; 1770 const char *endptr = &f; 1771 unsigned long res = 999; 1772 int err; 1773 1774 /* 1 mod 2^(sizeof(long)*8) */ 1775 str = LONG_MAX == INT_MAX ? "-4294967295" : "-18446744073709551615"; 1776 err = qemu_strtoul(str, &endptr, 0, &res); 1777 1778 g_assert_cmpint(err, ==, 0); 1779 g_assert_cmphex(res, ==, 1); 1780 g_assert_true(endptr == str + strlen(str)); 1781 } 1782 1783 static void test_qemu_strtoul_max(void) 1784 { 1785 char *str = g_strdup_printf("%lu", ULONG_MAX); 1786 char f = 'X'; 1787 const char *endptr = &f; 1788 unsigned long res = 999; 1789 int err; 1790 1791 err = qemu_strtoul(str, &endptr, 0, &res); 1792 1793 g_assert_cmpint(err, ==, 0); 1794 g_assert_cmphex(res, ==, ULONG_MAX); 1795 g_assert_true(endptr == str + strlen(str)); 1796 g_free(str); 1797 } 1798 1799 static void test_qemu_strtoul_overflow(void) 1800 { 1801 const char *str; 1802 const char *endptr; 1803 unsigned long res; 1804 int err; 1805 1806 /* 1 more than ULONG_MAX */ 1807 str = ULONG_MAX == UINT_MAX ? "4294967296" : "18446744073709551616"; 1808 endptr = "somewhere"; 1809 res = 999; 1810 err = qemu_strtoul(str, &endptr, 0, &res); 1811 g_assert_cmpint(err, ==, -ERANGE); 1812 g_assert_cmpuint(res, ==, ULONG_MAX); 1813 g_assert_true(endptr == str + strlen(str)); 1814 1815 if (LONG_MAX == INT_MAX) { 1816 str = "0xffffffff00000001"; /* UINT64_MAX - UINT_MAX + 1 (not 1) */ 1817 endptr = "somewhere"; 1818 res = 999; 1819 err = qemu_strtoul(str, &endptr, 0, &res); 1820 g_assert_cmpint(err, ==, -ERANGE); 1821 g_assert_cmpuint(res, ==, ULONG_MAX); 1822 g_assert_true(endptr == str + strlen(str)); 1823 } 1824 1825 str = "0x10000000000000000"; /* 65 bits, either sign bit position clear */ 1826 endptr = "somewhere"; 1827 res = 999; 1828 err = qemu_strtoul(str, &endptr, 0, &res); 1829 g_assert_cmpint(err, ==, -ERANGE); 1830 g_assert_cmpuint(res, ==, ULONG_MAX); 1831 g_assert_true(endptr == str + strlen(str)); 1832 1833 str = "0x18000000080000000"; /* 65 bits, either sign bit position set */ 1834 endptr = "somewhere"; 1835 res = 999; 1836 err = qemu_strtoul(str, &endptr, 0, &res); 1837 g_assert_cmpint(err, ==, -ERANGE); 1838 g_assert_cmpuint(res, ==, ULONG_MAX); 1839 g_assert_true(endptr == str + strlen(str)); 1840 } 1841 1842 static void test_qemu_strtoul_underflow(void) 1843 { 1844 const char *str; 1845 const char *endptr; 1846 unsigned long res; 1847 int err; 1848 1849 /* 1 less than -ULONG_MAX */ 1850 str = ULONG_MAX == UINT_MAX ? "-4294967296" : "-18446744073709551616"; 1851 endptr = "somewhere"; 1852 res = 999; 1853 err = qemu_strtoul(str, &endptr, 0, &res); 1854 g_assert_cmpint(err, ==, -ERANGE); 1855 g_assert_cmpuint(res, ==, ULONG_MAX); 1856 g_assert_true(endptr == str + strlen(str)); 1857 1858 if (LONG_MAX == INT_MAX) { 1859 str = "-0xffffffff00000002"; 1860 endptr = "somewhere"; 1861 res = 999; 1862 err = qemu_strtoul(str, &endptr, 0, &res); 1863 g_assert_cmpint(err, ==, -ERANGE); 1864 g_assert_cmpuint(res, ==, ULONG_MAX); 1865 g_assert_true(endptr == str + strlen(str)); 1866 } 1867 1868 str = "-0x10000000000000000"; /* 65 bits, either sign bit position clear */ 1869 endptr = "somewhere"; 1870 res = 999; 1871 err = qemu_strtoul(str, &endptr, 0, &res); 1872 g_assert_cmpint(err, ==, -ERANGE); 1873 g_assert_cmpuint(res, ==, ULONG_MAX); 1874 g_assert_true(endptr == str + strlen(str)); 1875 1876 str = "-0x18000000080000000"; /* 65 bits, either sign bit position set */ 1877 endptr = "somewhere"; 1878 res = 999; 1879 err = qemu_strtoul(str, &endptr, 0, &res); 1880 g_assert_cmpint(err, ==, -ERANGE); 1881 g_assert_cmpuint(res, ==, ULONG_MAX); 1882 g_assert_true(endptr == str + strlen(str)); 1883 } 1884 1885 static void test_qemu_strtoul_negative(void) 1886 { 1887 const char *str = " \t -321"; 1888 char f = 'X'; 1889 const char *endptr = &f; 1890 unsigned long res = 999; 1891 int err; 1892 1893 err = qemu_strtoul(str, &endptr, 0, &res); 1894 1895 g_assert_cmpint(err, ==, 0); 1896 g_assert_cmpuint(res, ==, -321ul); 1897 g_assert_true(endptr == str + strlen(str)); 1898 } 1899 1900 static void test_qemu_strtoul_negzero(void) 1901 { 1902 const char *str = " -0"; 1903 char f = 'X'; 1904 const char *endptr = &f; 1905 unsigned long res = 999; 1906 int err; 1907 1908 err = qemu_strtoul(str, &endptr, 0, &res); 1909 1910 g_assert_cmpint(err, ==, 0); 1911 g_assert_cmpuint(res, ==, 0); 1912 g_assert_true(endptr == str + strlen(str)); 1913 } 1914 1915 static void test_qemu_strtoul_full_correct(void) 1916 { 1917 const char *str = "123"; 1918 unsigned long res = 999; 1919 int err; 1920 1921 err = qemu_strtoul(str, NULL, 0, &res); 1922 1923 g_assert_cmpint(err, ==, 0); 1924 g_assert_cmpuint(res, ==, 123); 1925 } 1926 1927 static void test_qemu_strtoul_full_null(void) 1928 { 1929 unsigned long res = 999; 1930 int err; 1931 1932 err = qemu_strtoul(NULL, NULL, 0, &res); 1933 1934 g_assert_cmpint(err, ==, -EINVAL); 1935 g_assert_cmpuint(res, ==, 999); 1936 } 1937 1938 static void test_qemu_strtoul_full_empty(void) 1939 { 1940 const char *str = ""; 1941 unsigned long res = 999; 1942 int err; 1943 1944 err = qemu_strtoul(str, NULL, 0, &res); 1945 1946 g_assert_cmpint(err, ==, -EINVAL); 1947 g_assert_cmpuint(res, ==, 0); 1948 } 1949 1950 static void test_qemu_strtoul_full_negative(void) 1951 { 1952 const char *str = " \t -321"; 1953 unsigned long res = 999; 1954 int err; 1955 1956 err = qemu_strtoul(str, NULL, 0, &res); 1957 g_assert_cmpint(err, ==, 0); 1958 g_assert_cmpuint(res, ==, -321ul); 1959 } 1960 1961 static void test_qemu_strtoul_full_negzero(void) 1962 { 1963 const char *str = " -0"; 1964 unsigned long res = 999; 1965 int err; 1966 1967 err = qemu_strtoul(str, NULL, 0, &res); 1968 g_assert_cmpint(err, ==, 0); 1969 g_assert_cmpuint(res, ==, 0); 1970 } 1971 1972 static void test_qemu_strtoul_full_trailing(void) 1973 { 1974 const char *str = "123xxx"; 1975 unsigned long res = 999; 1976 int err; 1977 1978 err = qemu_strtoul(str, NULL, 0, &res); 1979 1980 g_assert_cmpint(err, ==, -EINVAL); 1981 g_assert_cmpuint(res, ==, 123); 1982 } 1983 1984 static void test_qemu_strtoul_full_max(void) 1985 { 1986 char *str = g_strdup_printf("%lu", ULONG_MAX); 1987 unsigned long res = 999; 1988 int err; 1989 1990 err = qemu_strtoul(str, NULL, 0, &res); 1991 1992 g_assert_cmpint(err, ==, 0); 1993 g_assert_cmphex(res, ==, ULONG_MAX); 1994 g_free(str); 1995 } 1996 1997 static void test_qemu_strtoul_full_erange_junk(void) 1998 { 1999 /* EINVAL has priority over ERANGE */ 2000 const char *str = "-99999999999999999999junk"; 2001 unsigned long res = 999; 2002 int err; 2003 2004 err = qemu_strtoul(str, NULL, 0, &res); 2005 2006 g_assert_cmpint(err, ==, -EINVAL); 2007 g_assert_cmpuint(res, ==, ULONG_MAX); 2008 } 2009 2010 static void test_qemu_strtoi64_correct(void) 2011 { 2012 const char *str = "12345 foo"; 2013 char f = 'X'; 2014 const char *endptr = &f; 2015 int64_t res = 999; 2016 int err; 2017 2018 err = qemu_strtoi64(str, &endptr, 0, &res); 2019 2020 g_assert_cmpint(err, ==, 0); 2021 g_assert_cmpint(res, ==, 12345); 2022 g_assert_true(endptr == str + 5); 2023 } 2024 2025 static void test_qemu_strtoi64_null(void) 2026 { 2027 char f = 'X'; 2028 const char *endptr = &f; 2029 int64_t res = 999; 2030 int err; 2031 2032 err = qemu_strtoi64(NULL, &endptr, 0, &res); 2033 2034 g_assert_cmpint(err, ==, -EINVAL); 2035 g_assert_cmpint(res, ==, 999); 2036 g_assert_null(endptr); 2037 } 2038 2039 static void test_qemu_strtoi64_empty(void) 2040 { 2041 const char *str = ""; 2042 char f = 'X'; 2043 const char *endptr = &f; 2044 int64_t res = 999; 2045 int err; 2046 2047 err = qemu_strtoi64(str, &endptr, 0, &res); 2048 2049 g_assert_cmpint(err, ==, -EINVAL); 2050 g_assert_cmpint(res, ==, 0); 2051 g_assert_true(endptr == str); 2052 } 2053 2054 static void test_qemu_strtoi64_whitespace(void) 2055 { 2056 const char *str = " \t "; 2057 char f = 'X'; 2058 const char *endptr = &f; 2059 int64_t res = 999; 2060 int err; 2061 2062 err = qemu_strtoi64(str, &endptr, 0, &res); 2063 2064 g_assert_cmpint(err, ==, -EINVAL); 2065 g_assert_cmpint(res, ==, 0); 2066 g_assert_true(endptr == str); 2067 } 2068 2069 static void test_qemu_strtoi64_invalid(void) 2070 { 2071 const char *str = " xxxx \t abc"; 2072 char f = 'X'; 2073 const char *endptr = &f; 2074 int64_t res = 999; 2075 int err; 2076 2077 err = qemu_strtoi64(str, &endptr, 0, &res); 2078 2079 g_assert_cmpint(err, ==, -EINVAL); 2080 g_assert_cmpint(res, ==, 0); 2081 g_assert_true(endptr == str); 2082 } 2083 2084 static void test_qemu_strtoi64_trailing(void) 2085 { 2086 const char *str = "123xxx"; 2087 char f = 'X'; 2088 const char *endptr = &f; 2089 int64_t res = 999; 2090 int err; 2091 2092 err = qemu_strtoi64(str, &endptr, 0, &res); 2093 2094 g_assert_cmpint(err, ==, 0); 2095 g_assert_cmpint(res, ==, 123); 2096 g_assert_true(endptr == str + 3); 2097 } 2098 2099 static void test_qemu_strtoi64_octal(void) 2100 { 2101 const char *str = "0123"; 2102 char f = 'X'; 2103 const char *endptr = &f; 2104 int64_t res = 999; 2105 int err; 2106 2107 err = qemu_strtoi64(str, &endptr, 8, &res); 2108 2109 g_assert_cmpint(err, ==, 0); 2110 g_assert_cmpint(res, ==, 0123); 2111 g_assert_true(endptr == str + strlen(str)); 2112 2113 endptr = &f; 2114 res = 999; 2115 err = qemu_strtoi64(str, &endptr, 0, &res); 2116 2117 g_assert_cmpint(err, ==, 0); 2118 g_assert_cmpint(res, ==, 0123); 2119 g_assert_true(endptr == str + strlen(str)); 2120 } 2121 2122 static void test_qemu_strtoi64_decimal(void) 2123 { 2124 const char *str = "0123"; 2125 char f = 'X'; 2126 const char *endptr = &f; 2127 int64_t res = 999; 2128 int err; 2129 2130 err = qemu_strtoi64(str, &endptr, 10, &res); 2131 2132 g_assert_cmpint(err, ==, 0); 2133 g_assert_cmpint(res, ==, 123); 2134 g_assert_true(endptr == str + strlen(str)); 2135 2136 str = "123"; 2137 endptr = &f; 2138 res = 999; 2139 err = qemu_strtoi64(str, &endptr, 0, &res); 2140 2141 g_assert_cmpint(err, ==, 0); 2142 g_assert_cmpint(res, ==, 123); 2143 g_assert_true(endptr == str + strlen(str)); 2144 } 2145 2146 static void test_qemu_strtoi64_hex(void) 2147 { 2148 const char *str = "0123"; 2149 char f = 'X'; 2150 const char *endptr = &f; 2151 int64_t res = 999; 2152 int err; 2153 2154 err = qemu_strtoi64(str, &endptr, 16, &res); 2155 2156 g_assert_cmpint(err, ==, 0); 2157 g_assert_cmpint(res, ==, 0x123); 2158 g_assert_true(endptr == str + strlen(str)); 2159 2160 str = "0x123"; 2161 endptr = &f; 2162 res = 999; 2163 err = qemu_strtoi64(str, &endptr, 0, &res); 2164 2165 g_assert_cmpint(err, ==, 0); 2166 g_assert_cmpint(res, ==, 0x123); 2167 g_assert_true(endptr == str + strlen(str)); 2168 2169 str = "0x"; 2170 endptr = &f; 2171 res = 999; 2172 err = qemu_strtoi64(str, &endptr, 16, &res); 2173 2174 g_assert_cmpint(err, ==, 0); 2175 g_assert_cmpint(res, ==, 0); 2176 g_assert_true(endptr == str + 1); 2177 } 2178 2179 static void test_qemu_strtoi64_max(void) 2180 { 2181 char *str = g_strdup_printf("%lld", LLONG_MAX); 2182 char f = 'X'; 2183 const char *endptr = &f; 2184 int64_t res = 999; 2185 int err; 2186 2187 err = qemu_strtoi64(str, &endptr, 0, &res); 2188 2189 g_assert_cmpint(err, ==, 0); 2190 g_assert_cmpint(res, ==, LLONG_MAX); 2191 g_assert_true(endptr == str + strlen(str)); 2192 g_free(str); 2193 } 2194 2195 static void test_qemu_strtoi64_overflow(void) 2196 { 2197 const char *str; 2198 const char *endptr; 2199 int64_t res; 2200 int err; 2201 2202 str = "9223372036854775808"; /* 1 more than INT64_MAX */ 2203 endptr = "somewhere"; 2204 res = 999; 2205 err = qemu_strtoi64(str, &endptr, 0, &res); 2206 g_assert_cmpint(err, ==, -ERANGE); 2207 g_assert_cmpint(res, ==, INT64_MAX); 2208 g_assert_true(endptr == str + strlen(str)); 2209 2210 str = "0x10000000000000000"; /* 65 bits, 64-bit sign bit clear */ 2211 endptr = "somewhere"; 2212 res = 999; 2213 err = qemu_strtoi64(str, &endptr, 0, &res); 2214 g_assert_cmpint(err, ==, -ERANGE); 2215 g_assert_cmpint(res, ==, INT64_MAX); 2216 g_assert_true(endptr == str + strlen(str)); 2217 2218 str = "0x18000000080000000"; /* 65 bits, 64-bit sign bit set */ 2219 endptr = "somewhere"; 2220 res = 999; 2221 err = qemu_strtoi64(str, &endptr, 0, &res); 2222 g_assert_cmpint(err, ==, -ERANGE); 2223 g_assert_cmpint(res, ==, INT64_MAX); 2224 g_assert_true(endptr == str + strlen(str)); 2225 } 2226 2227 static void test_qemu_strtoi64_min(void) 2228 { 2229 char *str = g_strdup_printf("%lld", LLONG_MIN); 2230 char f = 'X'; 2231 const char *endptr = &f; 2232 int64_t res = 999; 2233 int err; 2234 2235 err = qemu_strtoi64(str, &endptr, 0, &res); 2236 2237 g_assert_cmpint(err, ==, 0); 2238 g_assert_cmpint(res, ==, LLONG_MIN); 2239 g_assert_true(endptr == str + strlen(str)); 2240 g_free(str); 2241 } 2242 2243 static void test_qemu_strtoi64_underflow(void) 2244 { 2245 const char *str; 2246 const char *endptr; 2247 int64_t res; 2248 int err; 2249 2250 str = "-9223372036854775809"; /* 1 less than INT64_MIN */ 2251 endptr = "somewhere"; 2252 res = 999; 2253 err = qemu_strtoi64(str, &endptr, 0, &res); 2254 g_assert_cmpint(err, ==, -ERANGE); 2255 g_assert_cmpint(res, ==, INT64_MIN); 2256 g_assert_true(endptr == str + strlen(str)); 2257 2258 str = "-0x10000000000000000"; /* 65 bits, 64-bit sign bit clear */ 2259 endptr = "somewhere"; 2260 res = 999; 2261 err = qemu_strtoi64(str, &endptr, 0, &res); 2262 g_assert_cmpint(err, ==, -ERANGE); 2263 g_assert_cmpint(res, ==, INT64_MIN); 2264 g_assert_true(endptr == str + strlen(str)); 2265 2266 str = "-0x18000000080000000"; /* 65 bits, 64-bit sign bit set */ 2267 endptr = "somewhere"; 2268 res = 999; 2269 err = qemu_strtoi64(str, &endptr, 0, &res); 2270 g_assert_cmpint(err, ==, -ERANGE); 2271 g_assert_cmpint(res, ==, INT64_MIN); 2272 g_assert_true(endptr == str + strlen(str)); 2273 } 2274 2275 static void test_qemu_strtoi64_negative(void) 2276 { 2277 const char *str = " \t -321"; 2278 char f = 'X'; 2279 const char *endptr = &f; 2280 int64_t res = 999; 2281 int err; 2282 2283 err = qemu_strtoi64(str, &endptr, 0, &res); 2284 2285 g_assert_cmpint(err, ==, 0); 2286 g_assert_cmpint(res, ==, -321); 2287 g_assert_true(endptr == str + strlen(str)); 2288 } 2289 2290 static void test_qemu_strtoi64_negzero(void) 2291 { 2292 const char *str = " -0"; 2293 char f = 'X'; 2294 const char *endptr = &f; 2295 int64_t res = 999; 2296 int err; 2297 2298 err = qemu_strtoi64(str, &endptr, 0, &res); 2299 2300 g_assert_cmpint(err, ==, 0); 2301 g_assert_cmpint(res, ==, 0); 2302 g_assert_true(endptr == str + strlen(str)); 2303 } 2304 2305 static void test_qemu_strtoi64_full_correct(void) 2306 { 2307 const char *str = "123"; 2308 int64_t res = 999; 2309 int err; 2310 2311 err = qemu_strtoi64(str, NULL, 0, &res); 2312 2313 g_assert_cmpint(err, ==, 0); 2314 g_assert_cmpint(res, ==, 123); 2315 } 2316 2317 static void test_qemu_strtoi64_full_null(void) 2318 { 2319 int64_t res = 999; 2320 int err; 2321 2322 err = qemu_strtoi64(NULL, NULL, 0, &res); 2323 2324 g_assert_cmpint(err, ==, -EINVAL); 2325 g_assert_cmpint(res, ==, 999); 2326 } 2327 2328 static void test_qemu_strtoi64_full_empty(void) 2329 { 2330 const char *str = ""; 2331 int64_t res = 999; 2332 int err; 2333 2334 err = qemu_strtoi64(str, NULL, 0, &res); 2335 2336 g_assert_cmpint(err, ==, -EINVAL); 2337 g_assert_cmpint(res, ==, 0); 2338 } 2339 2340 static void test_qemu_strtoi64_full_negative(void) 2341 { 2342 const char *str = " \t -321"; 2343 int64_t res = 999; 2344 int err; 2345 2346 err = qemu_strtoi64(str, NULL, 0, &res); 2347 2348 g_assert_cmpint(err, ==, 0); 2349 g_assert_cmpint(res, ==, -321); 2350 } 2351 2352 static void test_qemu_strtoi64_full_negzero(void) 2353 { 2354 const char *str = " -0"; 2355 int64_t res = 999; 2356 int err; 2357 2358 err = qemu_strtoi64(str, NULL, 0, &res); 2359 2360 g_assert_cmpint(err, ==, 0); 2361 g_assert_cmpint(res, ==, 0); 2362 } 2363 2364 static void test_qemu_strtoi64_full_trailing(void) 2365 { 2366 const char *str = "123xxx"; 2367 int64_t res = 999; 2368 int err; 2369 2370 err = qemu_strtoi64(str, NULL, 0, &res); 2371 2372 g_assert_cmpint(err, ==, -EINVAL); 2373 g_assert_cmpint(res, ==, 123); 2374 } 2375 2376 static void test_qemu_strtoi64_full_max(void) 2377 { 2378 2379 char *str = g_strdup_printf("%lld", LLONG_MAX); 2380 int64_t res = 999; 2381 int err; 2382 2383 err = qemu_strtoi64(str, NULL, 0, &res); 2384 2385 g_assert_cmpint(err, ==, 0); 2386 g_assert_cmpint(res, ==, LLONG_MAX); 2387 g_free(str); 2388 } 2389 2390 static void test_qemu_strtoi64_full_erange_junk(void) 2391 { 2392 /* EINVAL has priority over ERANGE */ 2393 const char *str = "-99999999999999999999junk"; 2394 int64_t res = 999; 2395 int err; 2396 2397 err = qemu_strtoi64(str, NULL, 0, &res); 2398 2399 g_assert_cmpint(err, ==, -EINVAL); 2400 g_assert_cmpint(res, ==, INT64_MIN); 2401 } 2402 2403 static void test_qemu_strtou64_correct(void) 2404 { 2405 const char *str = "12345 foo"; 2406 char f = 'X'; 2407 const char *endptr = &f; 2408 uint64_t res = 999; 2409 int err; 2410 2411 err = qemu_strtou64(str, &endptr, 0, &res); 2412 2413 g_assert_cmpint(err, ==, 0); 2414 g_assert_cmpuint(res, ==, 12345); 2415 g_assert_true(endptr == str + 5); 2416 } 2417 2418 static void test_qemu_strtou64_null(void) 2419 { 2420 char f = 'X'; 2421 const char *endptr = &f; 2422 uint64_t res = 999; 2423 int err; 2424 2425 err = qemu_strtou64(NULL, &endptr, 0, &res); 2426 2427 g_assert_cmpint(err, ==, -EINVAL); 2428 g_assert_cmpuint(res, ==, 999); 2429 g_assert_null(endptr); 2430 } 2431 2432 static void test_qemu_strtou64_empty(void) 2433 { 2434 const char *str = ""; 2435 char f = 'X'; 2436 const char *endptr = &f; 2437 uint64_t res = 999; 2438 int err; 2439 2440 err = qemu_strtou64(str, &endptr, 0, &res); 2441 2442 g_assert_cmpint(err, ==, -EINVAL); 2443 g_assert_cmpuint(res, ==, 0); 2444 g_assert_true(endptr == str); 2445 } 2446 2447 static void test_qemu_strtou64_whitespace(void) 2448 { 2449 const char *str = " \t "; 2450 char f = 'X'; 2451 const char *endptr = &f; 2452 uint64_t res = 999; 2453 int err; 2454 2455 err = qemu_strtou64(str, &endptr, 0, &res); 2456 2457 g_assert_cmpint(err, ==, -EINVAL); 2458 g_assert_cmpuint(res, ==, 0); 2459 g_assert_true(endptr == str); 2460 } 2461 2462 static void test_qemu_strtou64_invalid(void) 2463 { 2464 const char *str = " xxxx \t abc"; 2465 char f = 'X'; 2466 const char *endptr = &f; 2467 uint64_t res = 999; 2468 int err; 2469 2470 err = qemu_strtou64(str, &endptr, 0, &res); 2471 2472 g_assert_cmpint(err, ==, -EINVAL); 2473 g_assert_cmpuint(res, ==, 0); 2474 g_assert_true(endptr == str); 2475 } 2476 2477 static void test_qemu_strtou64_trailing(void) 2478 { 2479 const char *str = "123xxx"; 2480 char f = 'X'; 2481 const char *endptr = &f; 2482 uint64_t res = 999; 2483 int err; 2484 2485 err = qemu_strtou64(str, &endptr, 0, &res); 2486 2487 g_assert_cmpint(err, ==, 0); 2488 g_assert_cmpuint(res, ==, 123); 2489 g_assert_true(endptr == str + 3); 2490 } 2491 2492 static void test_qemu_strtou64_octal(void) 2493 { 2494 const char *str = "0123"; 2495 char f = 'X'; 2496 const char *endptr = &f; 2497 uint64_t res = 999; 2498 int err; 2499 2500 err = qemu_strtou64(str, &endptr, 8, &res); 2501 2502 g_assert_cmpint(err, ==, 0); 2503 g_assert_cmpuint(res, ==, 0123); 2504 g_assert_true(endptr == str + strlen(str)); 2505 2506 endptr = &f; 2507 res = 999; 2508 err = qemu_strtou64(str, &endptr, 0, &res); 2509 2510 g_assert_cmpint(err, ==, 0); 2511 g_assert_cmpuint(res, ==, 0123); 2512 g_assert_true(endptr == str + strlen(str)); 2513 } 2514 2515 static void test_qemu_strtou64_decimal(void) 2516 { 2517 const char *str = "0123"; 2518 char f = 'X'; 2519 const char *endptr = &f; 2520 uint64_t res = 999; 2521 int err; 2522 2523 err = qemu_strtou64(str, &endptr, 10, &res); 2524 2525 g_assert_cmpint(err, ==, 0); 2526 g_assert_cmpuint(res, ==, 123); 2527 g_assert_true(endptr == str + strlen(str)); 2528 2529 str = "123"; 2530 endptr = &f; 2531 res = 999; 2532 err = qemu_strtou64(str, &endptr, 0, &res); 2533 2534 g_assert_cmpint(err, ==, 0); 2535 g_assert_cmpuint(res, ==, 123); 2536 g_assert_true(endptr == str + strlen(str)); 2537 } 2538 2539 static void test_qemu_strtou64_hex(void) 2540 { 2541 const char *str = "0123"; 2542 char f = 'X'; 2543 const char *endptr = &f; 2544 uint64_t res = 999; 2545 int err; 2546 2547 err = qemu_strtou64(str, &endptr, 16, &res); 2548 2549 g_assert_cmpint(err, ==, 0); 2550 g_assert_cmphex(res, ==, 0x123); 2551 g_assert_true(endptr == str + strlen(str)); 2552 2553 str = "0x123"; 2554 endptr = &f; 2555 res = 999; 2556 err = qemu_strtou64(str, &endptr, 0, &res); 2557 2558 g_assert_cmpint(err, ==, 0); 2559 g_assert_cmphex(res, ==, 0x123); 2560 g_assert_true(endptr == str + strlen(str)); 2561 2562 str = "0x"; 2563 endptr = &f; 2564 res = 999; 2565 err = qemu_strtou64(str, &endptr, 16, &res); 2566 2567 g_assert_cmpint(err, ==, 0); 2568 g_assert_cmphex(res, ==, 0); 2569 g_assert_true(endptr == str + 1); 2570 } 2571 2572 static void test_qemu_strtou64_wrap(void) 2573 { 2574 const char *str = "-18446744073709551615"; /* 1 mod 2^64 */ 2575 char f = 'X'; 2576 const char *endptr = &f; 2577 uint64_t res = 999; 2578 int err; 2579 2580 err = qemu_strtou64(str, &endptr, 0, &res); 2581 2582 g_assert_cmpint(err, ==, 0); 2583 g_assert_cmpuint(res, ==, 1); 2584 g_assert_true(endptr == str + strlen(str)); 2585 } 2586 2587 static void test_qemu_strtou64_max(void) 2588 { 2589 char *str = g_strdup_printf("%llu", ULLONG_MAX); 2590 char f = 'X'; 2591 const char *endptr = &f; 2592 uint64_t res = 999; 2593 int err; 2594 2595 err = qemu_strtou64(str, &endptr, 0, &res); 2596 2597 g_assert_cmpint(err, ==, 0); 2598 g_assert_cmphex(res, ==, ULLONG_MAX); 2599 g_assert_true(endptr == str + strlen(str)); 2600 g_free(str); 2601 } 2602 2603 static void test_qemu_strtou64_overflow(void) 2604 { 2605 const char *str; 2606 const char *endptr; 2607 uint64_t res; 2608 int err; 2609 2610 str = "18446744073709551616"; /* 1 more than UINT64_MAX */ 2611 endptr = "somewhere"; 2612 res = 999; 2613 err = qemu_strtou64(str, &endptr, 0, &res); 2614 g_assert_cmpint(err, ==, -ERANGE); 2615 g_assert_cmpuint(res, ==, UINT64_MAX); 2616 g_assert_true(endptr == str + strlen(str)); 2617 2618 str = "0x10000000000000000"; /* 65 bits, 64-bit sign bit clear */ 2619 endptr = "somewhere"; 2620 res = 999; 2621 err = qemu_strtou64(str, &endptr, 0, &res); 2622 g_assert_cmpint(err, ==, -ERANGE); 2623 g_assert_cmpuint(res, ==, UINT64_MAX); 2624 g_assert_true(endptr == str + strlen(str)); 2625 2626 str = "0x18000000080000000"; /* 65 bits, 64-bit sign bit set */ 2627 endptr = "somewhere"; 2628 res = 999; 2629 err = qemu_strtou64(str, &endptr, 0, &res); 2630 g_assert_cmpint(err, ==, -ERANGE); 2631 g_assert_cmpuint(res, ==, UINT64_MAX); 2632 g_assert_true(endptr == str + strlen(str)); 2633 } 2634 2635 static void test_qemu_strtou64_underflow(void) 2636 { 2637 const char *str; 2638 const char *endptr; 2639 uint64_t res; 2640 int err; 2641 2642 str = "-99999999999999999999999999999999999999999999"; 2643 endptr = "somewhere"; 2644 res = 999; 2645 err = qemu_strtou64(str, &endptr, 0, &res); 2646 g_assert_cmpint(err, ==, -ERANGE); 2647 g_assert_cmpuint(res, ==, UINT64_MAX); 2648 g_assert_true(endptr == str + strlen(str)); 2649 2650 str = "-0x10000000000000000"; /* 65 bits, 64-bit sign bit clear */ 2651 endptr = "somewhere"; 2652 res = 999; 2653 err = qemu_strtou64(str, &endptr, 0, &res); 2654 g_assert_cmpint(err, ==, -ERANGE); 2655 g_assert_cmpuint(res, ==, UINT64_MAX); 2656 g_assert_true(endptr == str + strlen(str)); 2657 2658 str = "-0x18000000080000000"; /* 65 bits, 64-bit sign bit set */ 2659 endptr = "somewhere"; 2660 res = 999; 2661 err = qemu_strtou64(str, &endptr, 0, &res); 2662 g_assert_cmpint(err, ==, -ERANGE); 2663 g_assert_cmpuint(res, ==, UINT64_MAX); 2664 g_assert_true(endptr == str + strlen(str)); 2665 } 2666 2667 static void test_qemu_strtou64_negative(void) 2668 { 2669 const char *str = " \t -321"; 2670 char f = 'X'; 2671 const char *endptr = &f; 2672 uint64_t res = 999; 2673 int err; 2674 2675 err = qemu_strtou64(str, &endptr, 0, &res); 2676 2677 g_assert_cmpint(err, ==, 0); 2678 g_assert_cmpuint(res, ==, -321ull); 2679 g_assert_true(endptr == str + strlen(str)); 2680 } 2681 2682 static void test_qemu_strtou64_negzero(void) 2683 { 2684 const char *str = " -0"; 2685 char f = 'X'; 2686 const char *endptr = &f; 2687 uint64_t res = 999; 2688 int err; 2689 2690 err = qemu_strtou64(str, &endptr, 0, &res); 2691 2692 g_assert_cmpint(err, ==, 0); 2693 g_assert_cmpuint(res, ==, 0); 2694 g_assert_true(endptr == str + strlen(str)); 2695 } 2696 2697 static void test_qemu_strtou64_full_correct(void) 2698 { 2699 const char *str = "18446744073709551614"; 2700 uint64_t res = 999; 2701 int err; 2702 2703 err = qemu_strtou64(str, NULL, 0, &res); 2704 2705 g_assert_cmpint(err, ==, 0); 2706 g_assert_cmpuint(res, ==, 18446744073709551614ull); 2707 } 2708 2709 static void test_qemu_strtou64_full_null(void) 2710 { 2711 uint64_t res = 999; 2712 int err; 2713 2714 err = qemu_strtou64(NULL, NULL, 0, &res); 2715 2716 g_assert_cmpint(err, ==, -EINVAL); 2717 g_assert_cmpuint(res, ==, 999); 2718 } 2719 2720 static void test_qemu_strtou64_full_empty(void) 2721 { 2722 const char *str = ""; 2723 uint64_t res = 999; 2724 int err; 2725 2726 err = qemu_strtou64(str, NULL, 0, &res); 2727 2728 g_assert_cmpint(err, ==, -EINVAL); 2729 g_assert_cmpuint(res, ==, 0); 2730 } 2731 2732 static void test_qemu_strtou64_full_negative(void) 2733 { 2734 const char *str = " \t -321"; 2735 uint64_t res = 999; 2736 int err; 2737 2738 err = qemu_strtou64(str, NULL, 0, &res); 2739 2740 g_assert_cmpint(err, ==, 0); 2741 g_assert_cmpuint(res, ==, -321ull); 2742 } 2743 2744 static void test_qemu_strtou64_full_negzero(void) 2745 { 2746 const char *str = " -0"; 2747 uint64_t res = 999; 2748 int err; 2749 2750 err = qemu_strtou64(str, NULL, 0, &res); 2751 2752 g_assert_cmpint(err, ==, 0); 2753 g_assert_cmpuint(res, ==, 0); 2754 } 2755 2756 static void test_qemu_strtou64_full_trailing(void) 2757 { 2758 const char *str = "18446744073709551614xxxxxx"; 2759 uint64_t res = 999; 2760 int err; 2761 2762 err = qemu_strtou64(str, NULL, 0, &res); 2763 2764 g_assert_cmpint(err, ==, -EINVAL); 2765 g_assert_cmpuint(res, ==, 18446744073709551614ULL); 2766 } 2767 2768 static void test_qemu_strtou64_full_max(void) 2769 { 2770 char *str = g_strdup_printf("%lld", ULLONG_MAX); 2771 uint64_t res = 999; 2772 int err; 2773 2774 err = qemu_strtou64(str, NULL, 0, &res); 2775 2776 g_assert_cmpint(err, ==, 0); 2777 g_assert_cmphex(res, ==, ULLONG_MAX); 2778 g_free(str); 2779 } 2780 2781 static void test_qemu_strtou64_full_erange_junk(void) 2782 { 2783 /* EINVAL has priority over ERANGE */ 2784 const char *str = "-99999999999999999999junk"; 2785 uint64_t res = 999; 2786 int err; 2787 2788 err = qemu_strtou64(str, NULL, 0, &res); 2789 2790 g_assert_cmpint(err, ==, -EINVAL); 2791 g_assert_cmpuint(res, ==, UINT64_MAX); 2792 } 2793 2794 static void test_qemu_strtod_simple(void) 2795 { 2796 const char *str; 2797 const char *endptr; 2798 int err; 2799 double res; 2800 2801 /* no radix or exponent */ 2802 str = "1"; 2803 endptr = "somewhere"; 2804 res = 999; 2805 err = qemu_strtod(str, &endptr, &res); 2806 g_assert_cmpint(err, ==, 0); 2807 g_assert_cmpfloat(res, ==, 1.0); 2808 g_assert_true(endptr == str + 1); 2809 2810 /* leading space and sign */ 2811 str = " -0.0"; 2812 endptr = "somewhere"; 2813 res = 999; 2814 err = qemu_strtod(str, &endptr, &res); 2815 g_assert_cmpint(err, ==, 0); 2816 g_assert_cmpfloat(res, ==, -0.0); 2817 g_assert_true(signbit(res)); 2818 g_assert_true(endptr == str + 5); 2819 2820 /* fraction only */ 2821 str = "+.5"; 2822 endptr = "somewhere"; 2823 res = 999; 2824 err = qemu_strtod(str, &endptr, &res); 2825 g_assert_cmpint(err, ==, 0); 2826 g_assert_cmpfloat(res, ==, 0.5); 2827 g_assert_true(endptr == str + 3); 2828 2829 /* exponent */ 2830 str = "1.e+1"; 2831 endptr = "somewhere"; 2832 res = 999; 2833 err = qemu_strtod(str, &endptr, &res); 2834 g_assert_cmpint(err, ==, 0); 2835 g_assert_cmpfloat(res, ==, 10.0); 2836 g_assert_true(endptr == str + 5); 2837 2838 /* hex without radix */ 2839 str = "0x10"; 2840 endptr = "somewhere"; 2841 res = 999; 2842 err = qemu_strtod(str, &endptr, &res); 2843 g_assert_cmpint(err, ==, 0); 2844 g_assert_cmpfloat(res, ==, 16.0); 2845 g_assert_true(endptr == str + 4); 2846 } 2847 2848 static void test_qemu_strtod_einval(void) 2849 { 2850 const char *str; 2851 const char *endptr; 2852 int err; 2853 double res; 2854 2855 /* empty */ 2856 str = ""; 2857 endptr = "somewhere"; 2858 res = 999; 2859 err = qemu_strtod(str, &endptr, &res); 2860 g_assert_cmpint(err, ==, -EINVAL); 2861 g_assert_cmpfloat(res, ==, 0.0); 2862 g_assert_false(signbit(res)); 2863 g_assert_true(endptr == str); 2864 2865 /* NULL */ 2866 str = NULL; 2867 endptr = "random"; 2868 res = 999; 2869 err = qemu_strtod(str, &endptr, &res); 2870 g_assert_cmpint(err, ==, -EINVAL); 2871 g_assert_cmpfloat(res, ==, 999.0); 2872 g_assert_null(endptr); 2873 2874 /* not recognizable */ 2875 str = " junk"; 2876 endptr = "somewhere"; 2877 res = 999; 2878 err = qemu_strtod(str, &endptr, &res); 2879 g_assert_cmpint(err, ==, -EINVAL); 2880 g_assert_cmpfloat(res, ==, 0.0); 2881 g_assert_false(signbit(res)); 2882 g_assert_true(endptr == str); 2883 } 2884 2885 static void test_qemu_strtod_erange(void) 2886 { 2887 const char *str; 2888 const char *endptr; 2889 int err; 2890 double res; 2891 2892 /* overflow */ 2893 str = "9e999"; 2894 endptr = "somewhere"; 2895 res = 999; 2896 err = qemu_strtod(str, &endptr, &res); 2897 g_assert_cmpint(err, ==, -ERANGE); 2898 g_assert_cmpfloat(res, ==, HUGE_VAL); 2899 g_assert_true(endptr == str + 5); 2900 2901 str = "-9e+999"; 2902 endptr = "somewhere"; 2903 res = 999; 2904 err = qemu_strtod(str, &endptr, &res); 2905 g_assert_cmpint(err, ==, -ERANGE); 2906 g_assert_cmpfloat(res, ==, -HUGE_VAL); 2907 g_assert_true(endptr == str + 7); 2908 2909 /* underflow */ 2910 str = "-9e-999"; 2911 endptr = "somewhere"; 2912 res = 999; 2913 err = qemu_strtod(str, &endptr, &res); 2914 g_assert_cmpint(err, ==, -ERANGE); 2915 g_assert_cmpfloat(res, >=, -DBL_MIN); 2916 g_assert_cmpfloat(res, <=, -0.0); 2917 g_assert_true(signbit(res)); 2918 g_assert_true(endptr == str + 7); 2919 } 2920 2921 static void test_qemu_strtod_nonfinite(void) 2922 { 2923 const char *str; 2924 const char *endptr; 2925 int err; 2926 double res; 2927 2928 /* infinity */ 2929 str = "inf"; 2930 endptr = "somewhere"; 2931 res = 999; 2932 err = qemu_strtod(str, &endptr, &res); 2933 g_assert_cmpint(err, ==, 0); 2934 g_assert_true(isinf(res)); 2935 g_assert_false(signbit(res)); 2936 g_assert_true(endptr == str + 3); 2937 2938 str = "-infinity"; 2939 endptr = "somewhere"; 2940 res = 999; 2941 err = qemu_strtod(str, &endptr, &res); 2942 g_assert_cmpint(err, ==, 0); 2943 g_assert_true(isinf(res)); 2944 g_assert_true(signbit(res)); 2945 g_assert_true(endptr == str + 9); 2946 2947 /* not a number */ 2948 str = " NaN"; 2949 endptr = "somewhere"; 2950 res = 999; 2951 err = qemu_strtod(str, &endptr, &res); 2952 g_assert_cmpint(err, ==, 0); 2953 g_assert_true(isnan(res)); 2954 g_assert_true(endptr == str + 4); 2955 } 2956 2957 static void test_qemu_strtod_trailing(void) 2958 { 2959 const char *str; 2960 const char *endptr; 2961 int err; 2962 double res; 2963 2964 /* trailing whitespace */ 2965 str = "1. "; 2966 endptr = "somewhere"; 2967 res = 999; 2968 err = qemu_strtod(str, &endptr, &res); 2969 g_assert_cmpint(err, ==, 0); 2970 g_assert_cmpfloat(res, ==, 1.0); 2971 g_assert_true(endptr == str + 2); 2972 2973 endptr = "somewhere"; 2974 res = 999; 2975 err = qemu_strtod(str, NULL, &res); 2976 g_assert_cmpint(err, ==, -EINVAL); 2977 g_assert_cmpfloat(res, ==, 1.0); 2978 2979 /* trailing e is not an exponent */ 2980 str = ".5e"; 2981 endptr = "somewhere"; 2982 res = 999; 2983 err = qemu_strtod(str, &endptr, &res); 2984 g_assert_cmpint(err, ==, 0); 2985 g_assert_cmpfloat(res, ==, 0.5); 2986 g_assert_true(endptr == str + 2); 2987 2988 endptr = "somewhere"; 2989 res = 999; 2990 err = qemu_strtod(str, NULL, &res); 2991 g_assert_cmpint(err, ==, -EINVAL); 2992 g_assert_cmpfloat(res, ==, 0.5); 2993 2994 /* trailing ( not part of long NaN */ 2995 str = "nan("; 2996 endptr = "somewhere"; 2997 res = 999; 2998 err = qemu_strtod(str, &endptr, &res); 2999 g_assert_cmpint(err, ==, 0); 3000 g_assert_true(isnan(res)); 3001 g_assert_true(endptr == str + 3); 3002 3003 endptr = "somewhere"; 3004 res = 999; 3005 err = qemu_strtod(str, NULL, &res); 3006 g_assert_cmpint(err, ==, -EINVAL); 3007 g_assert_true(isnan(res)); 3008 } 3009 3010 static void test_qemu_strtod_erange_junk(void) 3011 { 3012 const char *str; 3013 const char *endptr; 3014 int err; 3015 double res; 3016 3017 /* ERANGE with trailing junk... */ 3018 str = "1e-999junk"; 3019 endptr = "somewhere"; 3020 res = 999; 3021 err = qemu_strtod(str, &endptr, &res); 3022 g_assert_cmpint(err, ==, -ERANGE); 3023 g_assert_cmpfloat(res, <=, DBL_MIN); 3024 g_assert_cmpfloat(res, >=, 0.0); 3025 g_assert_false(signbit(res)); 3026 g_assert_true(endptr == str + 6); 3027 3028 /* ...has less priority than EINVAL when full parse not possible */ 3029 endptr = "somewhere"; 3030 res = 999; 3031 err = qemu_strtod(str, NULL, &res); 3032 g_assert_cmpint(err, ==, -EINVAL); 3033 g_assert_cmpfloat(res, ==, 0.0); 3034 g_assert_false(signbit(res)); 3035 } 3036 3037 static void test_qemu_strtod_finite_simple(void) 3038 { 3039 const char *str; 3040 const char *endptr; 3041 int err; 3042 double res; 3043 3044 /* no radix or exponent */ 3045 str = "1"; 3046 endptr = "somewhere"; 3047 res = 999; 3048 err = qemu_strtod_finite(str, &endptr, &res); 3049 g_assert_cmpint(err, ==, 0); 3050 g_assert_cmpfloat(res, ==, 1.0); 3051 g_assert_true(endptr == str + 1); 3052 3053 /* leading space and sign */ 3054 str = " -0.0"; 3055 endptr = "somewhere"; 3056 res = 999; 3057 err = qemu_strtod_finite(str, &endptr, &res); 3058 g_assert_cmpint(err, ==, 0); 3059 g_assert_cmpfloat(res, ==, -0.0); 3060 g_assert_true(signbit(res)); 3061 g_assert_true(endptr == str + 5); 3062 3063 /* fraction only */ 3064 str = "+.5"; 3065 endptr = "somewhere"; 3066 res = 999; 3067 err = qemu_strtod_finite(str, &endptr, &res); 3068 g_assert_cmpint(err, ==, 0); 3069 g_assert_cmpfloat(res, ==, 0.5); 3070 g_assert_true(endptr == str + 3); 3071 3072 /* exponent */ 3073 str = "1.e+1"; 3074 endptr = "somewhere"; 3075 res = 999; 3076 err = qemu_strtod_finite(str, &endptr, &res); 3077 g_assert_cmpint(err, ==, 0); 3078 g_assert_cmpfloat(res, ==, 10.0); 3079 g_assert_true(endptr == str + 5); 3080 3081 /* hex without radix */ 3082 str = "0x10"; 3083 endptr = "somewhere"; 3084 res = 999; 3085 err = qemu_strtod(str, &endptr, &res); 3086 g_assert_cmpint(err, ==, 0); 3087 g_assert_cmpfloat(res, ==, 16.0); 3088 g_assert_true(endptr == str + 4); 3089 } 3090 3091 static void test_qemu_strtod_finite_einval(void) 3092 { 3093 const char *str; 3094 const char *endptr; 3095 int err; 3096 double res; 3097 3098 /* empty */ 3099 str = ""; 3100 endptr = "somewhere"; 3101 res = 999; 3102 err = qemu_strtod_finite(str, &endptr, &res); 3103 g_assert_cmpint(err, ==, -EINVAL); 3104 g_assert_cmpfloat(res, ==, 999.0); 3105 g_assert_true(endptr == str); 3106 3107 /* NULL */ 3108 str = NULL; 3109 endptr = "random"; 3110 res = 999; 3111 err = qemu_strtod_finite(str, &endptr, &res); 3112 g_assert_cmpint(err, ==, -EINVAL); 3113 g_assert_cmpfloat(res, ==, 999.0); 3114 g_assert_null(endptr); 3115 3116 /* not recognizable */ 3117 str = " junk"; 3118 endptr = "somewhere"; 3119 res = 999; 3120 err = qemu_strtod_finite(str, &endptr, &res); 3121 g_assert_cmpint(err, ==, -EINVAL); 3122 g_assert_cmpfloat(res, ==, 999.0); 3123 g_assert_true(endptr == str); 3124 } 3125 3126 static void test_qemu_strtod_finite_erange(void) 3127 { 3128 const char *str; 3129 const char *endptr; 3130 int err; 3131 double res; 3132 3133 /* overflow */ 3134 str = "9e999"; 3135 endptr = "somewhere"; 3136 res = 999; 3137 err = qemu_strtod_finite(str, &endptr, &res); 3138 g_assert_cmpint(err, ==, -ERANGE); 3139 g_assert_cmpfloat(res, ==, HUGE_VAL); 3140 g_assert_true(endptr == str + 5); 3141 3142 str = "-9e+999"; 3143 endptr = "somewhere"; 3144 res = 999; 3145 err = qemu_strtod_finite(str, &endptr, &res); 3146 g_assert_cmpint(err, ==, -ERANGE); 3147 g_assert_cmpfloat(res, ==, -HUGE_VAL); 3148 g_assert_true(endptr == str + 7); 3149 3150 /* underflow */ 3151 str = "-9e-999"; 3152 endptr = "somewhere"; 3153 res = 999; 3154 err = qemu_strtod_finite(str, &endptr, &res); 3155 g_assert_cmpint(err, ==, -ERANGE); 3156 g_assert_cmpfloat(res, >=, -DBL_MIN); 3157 g_assert_cmpfloat(res, <=, -0.0); 3158 g_assert_true(signbit(res)); 3159 g_assert_true(endptr == str + 7); 3160 } 3161 3162 static void test_qemu_strtod_finite_nonfinite(void) 3163 { 3164 const char *str; 3165 const char *endptr; 3166 int err; 3167 double res; 3168 3169 /* infinity */ 3170 str = "inf"; 3171 endptr = "somewhere"; 3172 res = 999; 3173 err = qemu_strtod_finite(str, &endptr, &res); 3174 g_assert_cmpint(err, ==, -EINVAL); 3175 g_assert_cmpfloat(res, ==, 999.0); 3176 g_assert_true(endptr == str); 3177 3178 str = "-infinity"; 3179 endptr = "somewhere"; 3180 res = 999; 3181 err = qemu_strtod_finite(str, &endptr, &res); 3182 g_assert_cmpint(err, ==, -EINVAL); 3183 g_assert_cmpfloat(res, ==, 999.0); 3184 g_assert_true(endptr == str); 3185 3186 /* not a number */ 3187 str = " NaN"; 3188 endptr = "somewhere"; 3189 res = 999; 3190 err = qemu_strtod_finite(str, &endptr, &res); 3191 g_assert_cmpint(err, ==, -EINVAL); 3192 g_assert_cmpfloat(res, ==, 999.0); 3193 g_assert_true(endptr == str); 3194 } 3195 3196 static void test_qemu_strtod_finite_trailing(void) 3197 { 3198 const char *str; 3199 const char *endptr; 3200 int err; 3201 double res; 3202 3203 /* trailing whitespace */ 3204 str = "1. "; 3205 endptr = "somewhere"; 3206 res = 999; 3207 err = qemu_strtod_finite(str, &endptr, &res); 3208 g_assert_cmpint(err, ==, 0); 3209 g_assert_cmpfloat(res, ==, 1.0); 3210 g_assert_true(endptr == str + 2); 3211 3212 endptr = "somewhere"; 3213 res = 999; 3214 err = qemu_strtod_finite(str, NULL, &res); 3215 g_assert_cmpint(err, ==, -EINVAL); 3216 g_assert_cmpfloat(res, ==, 999.0); 3217 3218 /* trailing e is not an exponent */ 3219 str = ".5e"; 3220 endptr = "somewhere"; 3221 res = 999; 3222 err = qemu_strtod_finite(str, &endptr, &res); 3223 g_assert_cmpint(err, ==, 0); 3224 g_assert_cmpfloat(res, ==, 0.5); 3225 g_assert_true(endptr == str + 2); 3226 3227 endptr = "somewhere"; 3228 res = 999; 3229 err = qemu_strtod_finite(str, NULL, &res); 3230 g_assert_cmpint(err, ==, -EINVAL); 3231 g_assert_cmpfloat(res, ==, 999.0); 3232 3233 /* trailing ( not part of long NaN */ 3234 str = "nan("; 3235 endptr = "somewhere"; 3236 res = 999; 3237 err = qemu_strtod_finite(str, &endptr, &res); 3238 g_assert_cmpint(err, ==, -EINVAL); 3239 g_assert_cmpfloat(res, ==, 999.0); 3240 g_assert_true(endptr == str); 3241 3242 endptr = "somewhere"; 3243 res = 999; 3244 err = qemu_strtod_finite(str, NULL, &res); 3245 g_assert_cmpint(err, ==, -EINVAL); 3246 g_assert_cmpfloat(res, ==, 999.0); 3247 } 3248 3249 static void test_qemu_strtod_finite_erange_junk(void) 3250 { 3251 const char *str; 3252 const char *endptr; 3253 int err; 3254 double res; 3255 3256 /* ERANGE with trailing junk... */ 3257 str = "1e-999junk"; 3258 endptr = "somewhere"; 3259 res = 999; 3260 err = qemu_strtod_finite(str, &endptr, &res); 3261 g_assert_cmpint(err, ==, -ERANGE); 3262 g_assert_cmpfloat(res, <=, DBL_MIN); 3263 g_assert_cmpfloat(res, >=, 0.0); 3264 g_assert_false(signbit(res)); 3265 g_assert_true(endptr == str + 6); 3266 3267 /* ...has less priority than EINVAL when full parse not possible */ 3268 endptr = "somewhere"; 3269 res = 999; 3270 err = qemu_strtod_finite(str, NULL, &res); 3271 g_assert_cmpint(err, ==, -EINVAL); 3272 g_assert_cmpfloat(res, ==, 999.0); 3273 } 3274 3275 static void test_qemu_strtosz_simple(void) 3276 { 3277 const char *str; 3278 const char *endptr; 3279 int err; 3280 uint64_t res; 3281 3282 str = "0"; 3283 endptr = str; 3284 res = 0xbaadf00d; 3285 err = qemu_strtosz(str, &endptr, &res); 3286 g_assert_cmpint(err, ==, 0); 3287 g_assert_cmpuint(res, ==, 0); 3288 g_assert_true(endptr == str + 1); 3289 3290 /* Leading 0 gives decimal results, not octal */ 3291 str = "08"; 3292 endptr = str; 3293 res = 0xbaadf00d; 3294 err = qemu_strtosz(str, &endptr, &res); 3295 g_assert_cmpint(err, ==, 0); 3296 g_assert_cmpuint(res, ==, 8); 3297 g_assert_true(endptr == str + 2); 3298 3299 /* Leading space is ignored */ 3300 str = " 12345"; 3301 endptr = str; 3302 res = 0xbaadf00d; 3303 err = qemu_strtosz(str, &endptr, &res); 3304 g_assert_cmpint(err, ==, 0); 3305 g_assert_cmpuint(res, ==, 12345); 3306 g_assert_true(endptr == str + 6); 3307 3308 res = 0xbaadf00d; 3309 err = qemu_strtosz(str, NULL, &res); 3310 g_assert_cmpint(err, ==, 0); 3311 g_assert_cmpuint(res, ==, 12345); 3312 3313 str = "9007199254740991"; /* 2^53-1 */ 3314 endptr = str; 3315 res = 0xbaadf00d; 3316 err = qemu_strtosz(str, &endptr, &res); 3317 g_assert_cmpint(err, ==, 0); 3318 g_assert_cmphex(res, ==, 0x1fffffffffffffULL); 3319 g_assert_true(endptr == str + 16); 3320 3321 str = "9007199254740992"; /* 2^53 */ 3322 endptr = str; 3323 res = 0xbaadf00d; 3324 err = qemu_strtosz(str, &endptr, &res); 3325 g_assert_cmpint(err, ==, 0); 3326 g_assert_cmphex(res, ==, 0x20000000000000ULL); 3327 g_assert_true(endptr == str + 16); 3328 3329 str = "9007199254740993"; /* 2^53+1 */ 3330 endptr = str; 3331 res = 0xbaadf00d; 3332 err = qemu_strtosz(str, &endptr, &res); 3333 g_assert_cmpint(err, ==, 0); 3334 g_assert_cmphex(res, ==, 0x20000000000001ULL); 3335 g_assert_true(endptr == str + 16); 3336 3337 str = "18446744073709549568"; /* 0xfffffffffffff800 (53 msbs set) */ 3338 endptr = str; 3339 res = 0xbaadf00d; 3340 err = qemu_strtosz(str, &endptr, &res); 3341 g_assert_cmpint(err, ==, 0); 3342 g_assert_cmphex(res, ==, 0xfffffffffffff800ULL); 3343 g_assert_true(endptr == str + 20); 3344 3345 str = "18446744073709550591"; /* 0xfffffffffffffbff */ 3346 endptr = str; 3347 res = 0xbaadf00d; 3348 err = qemu_strtosz(str, &endptr, &res); 3349 g_assert_cmpint(err, ==, 0); 3350 g_assert_cmphex(res, ==, 0xfffffffffffffbffULL); 3351 g_assert_true(endptr == str + 20); 3352 3353 str = "18446744073709551615"; /* 0xffffffffffffffff */ 3354 endptr = str; 3355 res = 0xbaadf00d; 3356 err = qemu_strtosz(str, &endptr, &res); 3357 g_assert_cmpint(err, ==, 0); 3358 g_assert_cmphex(res, ==, 0xffffffffffffffffULL); 3359 g_assert_true(endptr == str + 20); 3360 } 3361 3362 static void test_qemu_strtosz_hex(void) 3363 { 3364 const char *str; 3365 const char *endptr; 3366 int err; 3367 uint64_t res; 3368 3369 str = "0x0"; 3370 endptr = str; 3371 res = 0xbaadf00d; 3372 err = qemu_strtosz(str, &endptr, &res); 3373 g_assert_cmpint(err, ==, 0); 3374 g_assert_cmpuint(res, ==, 0); 3375 g_assert_true(endptr == str + 3); 3376 3377 str = "0xab"; 3378 endptr = str; 3379 res = 0xbaadf00d; 3380 err = qemu_strtosz(str, &endptr, &res); 3381 g_assert_cmpint(err, ==, 0); 3382 g_assert_cmpuint(res, ==, 171); 3383 g_assert_true(endptr == str + 4); 3384 3385 str = "0xae"; 3386 endptr = str; 3387 res = 0xbaadf00d; 3388 err = qemu_strtosz(str, &endptr, &res); 3389 g_assert_cmpint(err, ==, 0); 3390 g_assert_cmpuint(res, ==, 174); 3391 g_assert_true(endptr == str + 4); 3392 } 3393 3394 static void test_qemu_strtosz_units(void) 3395 { 3396 const char *none = "1"; 3397 const char *b = "1B"; 3398 const char *k = "1K"; 3399 const char *m = "1M"; 3400 const char *g = "1G"; 3401 const char *t = "1T"; 3402 const char *p = "1P"; 3403 const char *e = "1E"; 3404 int err; 3405 const char *endptr; 3406 uint64_t res; 3407 3408 /* default is M */ 3409 endptr = NULL; 3410 res = 0xbaadf00d; 3411 err = qemu_strtosz_MiB(none, &endptr, &res); 3412 g_assert_cmpint(err, ==, 0); 3413 g_assert_cmpuint(res, ==, MiB); 3414 g_assert_true(endptr == none + 1); 3415 3416 endptr = NULL; 3417 res = 0xbaadf00d; 3418 err = qemu_strtosz(b, &endptr, &res); 3419 g_assert_cmpint(err, ==, 0); 3420 g_assert_cmpuint(res, ==, 1); 3421 g_assert_true(endptr == b + 2); 3422 3423 endptr = NULL; 3424 res = 0xbaadf00d; 3425 err = qemu_strtosz(k, &endptr, &res); 3426 g_assert_cmpint(err, ==, 0); 3427 g_assert_cmpuint(res, ==, KiB); 3428 g_assert_true(endptr == k + 2); 3429 3430 endptr = NULL; 3431 res = 0xbaadf00d; 3432 err = qemu_strtosz(m, &endptr, &res); 3433 g_assert_cmpint(err, ==, 0); 3434 g_assert_cmpuint(res, ==, MiB); 3435 g_assert_true(endptr == m + 2); 3436 3437 endptr = NULL; 3438 res = 0xbaadf00d; 3439 err = qemu_strtosz(g, &endptr, &res); 3440 g_assert_cmpint(err, ==, 0); 3441 g_assert_cmpuint(res, ==, GiB); 3442 g_assert_true(endptr == g + 2); 3443 3444 endptr = NULL; 3445 res = 0xbaadf00d; 3446 err = qemu_strtosz(t, &endptr, &res); 3447 g_assert_cmpint(err, ==, 0); 3448 g_assert_cmpuint(res, ==, TiB); 3449 g_assert_true(endptr == t + 2); 3450 3451 endptr = NULL; 3452 res = 0xbaadf00d; 3453 err = qemu_strtosz(p, &endptr, &res); 3454 g_assert_cmpint(err, ==, 0); 3455 g_assert_cmpuint(res, ==, PiB); 3456 g_assert_true(endptr == p + 2); 3457 3458 endptr = NULL; 3459 res = 0xbaadf00d; 3460 err = qemu_strtosz(e, &endptr, &res); 3461 g_assert_cmpint(err, ==, 0); 3462 g_assert_cmpuint(res, ==, EiB); 3463 g_assert_true(endptr == e + 2); 3464 } 3465 3466 static void test_qemu_strtosz_float(void) 3467 { 3468 const char *str; 3469 int err; 3470 const char *endptr; 3471 uint64_t res; 3472 3473 str = "0.5E"; 3474 endptr = str; 3475 res = 0xbaadf00d; 3476 err = qemu_strtosz(str, &endptr, &res); 3477 g_assert_cmpint(err, ==, 0); 3478 g_assert_cmpuint(res, ==, EiB / 2); 3479 g_assert_true(endptr == str + 4); 3480 3481 /* For convenience, a fraction of 0 is tolerated even on bytes */ 3482 str = "1.0B"; 3483 endptr = str; 3484 res = 0xbaadf00d; 3485 err = qemu_strtosz(str, &endptr, &res); 3486 g_assert_cmpint(err, ==, 0); 3487 g_assert_cmpuint(res, ==, 1); 3488 g_assert_true(endptr == str + 4); 3489 3490 /* An empty fraction is tolerated */ 3491 str = "1.k"; 3492 endptr = str; 3493 res = 0xbaadf00d; 3494 err = qemu_strtosz(str, &endptr, &res); 3495 g_assert_cmpint(err, ==, 0); 3496 g_assert_cmpuint(res, ==, 1024); 3497 g_assert_true(endptr == str + 3); 3498 3499 /* For convenience, we permit values that are not byte-exact */ 3500 str = "12.345M"; 3501 endptr = str; 3502 res = 0xbaadf00d; 3503 err = qemu_strtosz(str, &endptr, &res); 3504 g_assert_cmpint(err, ==, 0); 3505 g_assert_cmpuint(res, ==, (uint64_t) (12.345 * MiB + 0.5)); 3506 g_assert_true(endptr == str + 7); 3507 } 3508 3509 static void test_qemu_strtosz_invalid(void) 3510 { 3511 const char *str; 3512 const char *endptr; 3513 int err; 3514 uint64_t res = 0xbaadf00d; 3515 3516 str = ""; 3517 endptr = NULL; 3518 err = qemu_strtosz(str, &endptr, &res); 3519 g_assert_cmpint(err, ==, -EINVAL); 3520 g_assert_cmphex(res, ==, 0xbaadf00d); 3521 g_assert_true(endptr == str); 3522 3523 str = " \t "; 3524 endptr = NULL; 3525 err = qemu_strtosz(str, &endptr, &res); 3526 g_assert_cmpint(err, ==, -EINVAL); 3527 g_assert_cmphex(res, ==, 0xbaadf00d); 3528 g_assert_true(endptr == str); 3529 3530 str = "crap"; 3531 endptr = NULL; 3532 err = qemu_strtosz(str, &endptr, &res); 3533 g_assert_cmpint(err, ==, -EINVAL); 3534 g_assert_cmphex(res, ==, 0xbaadf00d); 3535 g_assert_true(endptr == str); 3536 3537 str = "inf"; 3538 endptr = NULL; 3539 err = qemu_strtosz(str, &endptr, &res); 3540 g_assert_cmpint(err, ==, -EINVAL); 3541 g_assert_cmphex(res, ==, 0xbaadf00d); 3542 g_assert_true(endptr == str); 3543 3544 str = "NaN"; 3545 endptr = NULL; 3546 err = qemu_strtosz(str, &endptr, &res); 3547 g_assert_cmpint(err, ==, -EINVAL); 3548 g_assert_cmphex(res, ==, 0xbaadf00d); 3549 g_assert_true(endptr == str); 3550 3551 /* Fractional values require scale larger than bytes */ 3552 str = "1.1B"; 3553 endptr = NULL; 3554 err = qemu_strtosz(str, &endptr, &res); 3555 g_assert_cmpint(err, ==, -EINVAL); 3556 g_assert_cmphex(res, ==, 0xbaadf00d); 3557 g_assert_true(endptr == str); 3558 3559 str = "1.1"; 3560 endptr = NULL; 3561 err = qemu_strtosz(str, &endptr, &res); 3562 g_assert_cmpint(err, ==, -EINVAL); 3563 g_assert_cmphex(res, ==, 0xbaadf00d); 3564 g_assert_true(endptr == str); 3565 3566 /* No hex fractions */ 3567 str = "0x1.8k"; 3568 endptr = NULL; 3569 err = qemu_strtosz(str, &endptr, &res); 3570 g_assert_cmpint(err, ==, -EINVAL); 3571 g_assert_cmphex(res, ==, 0xbaadf00d); 3572 g_assert_true(endptr == str); 3573 3574 /* No suffixes */ 3575 str = "0x18M"; 3576 endptr = NULL; 3577 err = qemu_strtosz(str, &endptr, &res); 3578 g_assert_cmpint(err, ==, -EINVAL); 3579 g_assert_cmphex(res, ==, 0xbaadf00d); 3580 g_assert_true(endptr == str); 3581 3582 /* No negative values */ 3583 str = "-0"; 3584 endptr = NULL; 3585 err = qemu_strtosz(str, &endptr, &res); 3586 g_assert_cmpint(err, ==, -EINVAL); 3587 g_assert_cmphex(res, ==, 0xbaadf00d); 3588 g_assert_true(endptr == str); 3589 3590 str = "-1"; 3591 endptr = NULL; 3592 err = qemu_strtosz(str, &endptr, &res); 3593 g_assert_cmpint(err, ==, -EINVAL); 3594 g_assert_cmphex(res, ==, 0xbaadf00d); 3595 g_assert_true(endptr == str); 3596 } 3597 3598 static void test_qemu_strtosz_trailing(void) 3599 { 3600 const char *str; 3601 const char *endptr; 3602 int err; 3603 uint64_t res; 3604 3605 str = "123xxx"; 3606 endptr = NULL; 3607 res = 0xbaadf00d; 3608 err = qemu_strtosz_MiB(str, &endptr, &res); 3609 g_assert_cmpint(err, ==, 0); 3610 g_assert_cmpuint(res, ==, 123 * MiB); 3611 g_assert_true(endptr == str + 3); 3612 3613 res = 0xbaadf00d; 3614 err = qemu_strtosz(str, NULL, &res); 3615 g_assert_cmpint(err, ==, -EINVAL); 3616 g_assert_cmphex(res, ==, 0xbaadf00d); 3617 3618 str = "1kiB"; 3619 endptr = NULL; 3620 res = 0xbaadf00d; 3621 err = qemu_strtosz(str, &endptr, &res); 3622 g_assert_cmpint(err, ==, 0); 3623 g_assert_cmpuint(res, ==, 1024); 3624 g_assert_true(endptr == str + 2); 3625 3626 res = 0xbaadf00d; 3627 err = qemu_strtosz(str, NULL, &res); 3628 g_assert_cmpint(err, ==, -EINVAL); 3629 g_assert_cmphex(res, ==, 0xbaadf00d); 3630 3631 str = "0x"; 3632 endptr = NULL; 3633 res = 0xbaadf00d; 3634 err = qemu_strtosz(str, &endptr, &res); 3635 g_assert_cmpint(err, ==, 0); 3636 g_assert_cmpuint(res, ==, 0); 3637 g_assert_true(endptr == str + 1); 3638 3639 res = 0xbaadf00d; 3640 err = qemu_strtosz(str, NULL, &res); 3641 g_assert_cmpint(err, ==, -EINVAL); 3642 g_assert_cmphex(res, ==, 0xbaadf00d); 3643 3644 str = "0.NaN"; 3645 endptr = NULL; 3646 res = 0xbaadf00d; 3647 err = qemu_strtosz(str, &endptr, &res); 3648 g_assert_cmpint(err, ==, 0); 3649 g_assert_cmpuint(res, ==, 0); 3650 g_assert_true(endptr == str + 2); 3651 3652 res = 0xbaadf00d; 3653 err = qemu_strtosz(str, NULL, &res); 3654 g_assert_cmpint(err, ==, -EINVAL); 3655 g_assert_cmphex(res, ==, 0xbaadf00d); 3656 3657 str = "123-45"; 3658 endptr = NULL; 3659 res = 0xbaadf00d; 3660 err = qemu_strtosz(str, &endptr, &res); 3661 g_assert_cmpint(err, ==, 0); 3662 g_assert_cmpuint(res, ==, 123); 3663 g_assert_true(endptr == str + 3); 3664 3665 res = 0xbaadf00d; 3666 err = qemu_strtosz(str, NULL, &res); 3667 g_assert_cmpint(err, ==, -EINVAL); 3668 g_assert_cmphex(res, ==, 0xbaadf00d); 3669 3670 /* FIXME should stop parse after 'e'. No floating point exponents */ 3671 str = "1.5e1k"; 3672 endptr = NULL; 3673 res = 0xbaadf00d; 3674 err = qemu_strtosz(str, &endptr, &res); 3675 g_assert_cmpint(err, ==, -EINVAL /* FIXME 0 */); 3676 g_assert_cmphex(res, ==, 0xbaadf00d /* FIXME EiB * 1.5 */); 3677 g_assert_true(endptr == str /* FIXME + 4 */); 3678 3679 res = 0xbaadf00d; 3680 err = qemu_strtosz(str, NULL, &res); 3681 g_assert_cmpint(err, ==, -EINVAL); 3682 g_assert_cmpint(res, ==, 0xbaadf00d); 3683 3684 str = "1.5E+0k"; 3685 endptr = NULL; 3686 res = 0xbaadf00d; 3687 err = qemu_strtosz(str, &endptr, &res); 3688 g_assert_cmpint(err, ==, -EINVAL /* FIXME 0 */); 3689 g_assert_cmphex(res, ==, 0xbaadf00d /* FIXME EiB * 1.5 */); 3690 g_assert_true(endptr == str /* FIXME + 4 */); 3691 3692 res = 0xbaadf00d; 3693 err = qemu_strtosz(str, NULL, &res); 3694 g_assert_cmpint(err, ==, -EINVAL); 3695 g_assert_cmphex(res, ==, 0xbaadf00d); 3696 } 3697 3698 static void test_qemu_strtosz_erange(void) 3699 { 3700 const char *str; 3701 const char *endptr; 3702 int err; 3703 uint64_t res = 0xbaadf00d; 3704 3705 str = "18446744073709551616"; /* 2^64; see strtosz_simple for 2^64-1 */ 3706 endptr = NULL; 3707 err = qemu_strtosz(str, &endptr, &res); 3708 g_assert_cmpint(err, ==, -ERANGE); 3709 g_assert_cmphex(res, ==, 0xbaadf00d); 3710 g_assert_true(endptr == str + 20); 3711 3712 str = "20E"; 3713 endptr = NULL; 3714 err = qemu_strtosz(str, &endptr, &res); 3715 g_assert_cmpint(err, ==, -ERANGE); 3716 g_assert_cmphex(res, ==, 0xbaadf00d); 3717 g_assert_true(endptr == str + 3); 3718 } 3719 3720 static void test_qemu_strtosz_metric(void) 3721 { 3722 const char *str; 3723 int err; 3724 const char *endptr; 3725 uint64_t res; 3726 3727 str = "12345k"; 3728 endptr = str; 3729 res = 0xbaadf00d; 3730 err = qemu_strtosz_metric(str, &endptr, &res); 3731 g_assert_cmpint(err, ==, 0); 3732 g_assert_cmpuint(res, ==, 12345000); 3733 g_assert_true(endptr == str + 6); 3734 3735 str = "12.345M"; 3736 endptr = str; 3737 res = 0xbaadf00d; 3738 err = qemu_strtosz_metric(str, &endptr, &res); 3739 g_assert_cmpint(err, ==, 0); 3740 g_assert_cmpuint(res, ==, 12345000); 3741 g_assert_true(endptr == str + 7); 3742 } 3743 3744 static void test_freq_to_str(void) 3745 { 3746 char *str; 3747 3748 str = freq_to_str(999); 3749 g_assert_cmpstr(str, ==, "999 Hz"); 3750 g_free(str); 3751 3752 str = freq_to_str(1000); 3753 g_assert_cmpstr(str, ==, "1 KHz"); 3754 g_free(str); 3755 3756 str = freq_to_str(1010); 3757 g_assert_cmpstr(str, ==, "1.01 KHz"); 3758 g_free(str); 3759 } 3760 3761 static void test_size_to_str(void) 3762 { 3763 char *str; 3764 3765 str = size_to_str(0); 3766 g_assert_cmpstr(str, ==, "0 B"); 3767 g_free(str); 3768 3769 str = size_to_str(1); 3770 g_assert_cmpstr(str, ==, "1 B"); 3771 g_free(str); 3772 3773 str = size_to_str(1016); 3774 g_assert_cmpstr(str, ==, "0.992 KiB"); 3775 g_free(str); 3776 3777 str = size_to_str(1024); 3778 g_assert_cmpstr(str, ==, "1 KiB"); 3779 g_free(str); 3780 3781 str = size_to_str(512ull << 20); 3782 g_assert_cmpstr(str, ==, "512 MiB"); 3783 g_free(str); 3784 } 3785 3786 static void test_iec_binary_prefix(void) 3787 { 3788 g_assert_cmpstr(iec_binary_prefix(0), ==, ""); 3789 g_assert_cmpstr(iec_binary_prefix(10), ==, "Ki"); 3790 g_assert_cmpstr(iec_binary_prefix(20), ==, "Mi"); 3791 g_assert_cmpstr(iec_binary_prefix(30), ==, "Gi"); 3792 g_assert_cmpstr(iec_binary_prefix(40), ==, "Ti"); 3793 g_assert_cmpstr(iec_binary_prefix(50), ==, "Pi"); 3794 g_assert_cmpstr(iec_binary_prefix(60), ==, "Ei"); 3795 } 3796 3797 static void test_si_prefix(void) 3798 { 3799 g_assert_cmpstr(si_prefix(-18), ==, "a"); 3800 g_assert_cmpstr(si_prefix(-15), ==, "f"); 3801 g_assert_cmpstr(si_prefix(-12), ==, "p"); 3802 g_assert_cmpstr(si_prefix(-9), ==, "n"); 3803 g_assert_cmpstr(si_prefix(-6), ==, "u"); 3804 g_assert_cmpstr(si_prefix(-3), ==, "m"); 3805 g_assert_cmpstr(si_prefix(0), ==, ""); 3806 g_assert_cmpstr(si_prefix(3), ==, "K"); 3807 g_assert_cmpstr(si_prefix(6), ==, "M"); 3808 g_assert_cmpstr(si_prefix(9), ==, "G"); 3809 g_assert_cmpstr(si_prefix(12), ==, "T"); 3810 g_assert_cmpstr(si_prefix(15), ==, "P"); 3811 g_assert_cmpstr(si_prefix(18), ==, "E"); 3812 } 3813 3814 int main(int argc, char **argv) 3815 { 3816 g_test_init(&argc, &argv, NULL); 3817 3818 g_test_add_func("/cutils/parse_uint/null", test_parse_uint_null); 3819 g_test_add_func("/cutils/parse_uint/empty", test_parse_uint_empty); 3820 g_test_add_func("/cutils/parse_uint/whitespace", 3821 test_parse_uint_whitespace); 3822 g_test_add_func("/cutils/parse_uint/invalid", test_parse_uint_invalid); 3823 g_test_add_func("/cutils/parse_uint/trailing", test_parse_uint_trailing); 3824 g_test_add_func("/cutils/parse_uint/correct", test_parse_uint_correct); 3825 g_test_add_func("/cutils/parse_uint/octal", test_parse_uint_octal); 3826 g_test_add_func("/cutils/parse_uint/decimal", test_parse_uint_decimal); 3827 g_test_add_func("/cutils/parse_uint/llong_max", test_parse_uint_llong_max); 3828 g_test_add_func("/cutils/parse_uint/max", test_parse_uint_max); 3829 g_test_add_func("/cutils/parse_uint/overflow", test_parse_uint_overflow); 3830 g_test_add_func("/cutils/parse_uint/negative", test_parse_uint_negative); 3831 g_test_add_func("/cutils/parse_uint/negzero", test_parse_uint_negzero); 3832 g_test_add_func("/cutils/parse_uint_full/trailing", 3833 test_parse_uint_full_trailing); 3834 g_test_add_func("/cutils/parse_uint_full/correct", 3835 test_parse_uint_full_correct); 3836 g_test_add_func("/cutils/parse_uint_full/erange_junk", 3837 test_parse_uint_full_erange_junk); 3838 g_test_add_func("/cutils/parse_uint_full/null", 3839 test_parse_uint_full_null); 3840 3841 /* qemu_strtoi() tests */ 3842 g_test_add_func("/cutils/qemu_strtoi/correct", 3843 test_qemu_strtoi_correct); 3844 g_test_add_func("/cutils/qemu_strtoi/null", 3845 test_qemu_strtoi_null); 3846 g_test_add_func("/cutils/qemu_strtoi/empty", 3847 test_qemu_strtoi_empty); 3848 g_test_add_func("/cutils/qemu_strtoi/whitespace", 3849 test_qemu_strtoi_whitespace); 3850 g_test_add_func("/cutils/qemu_strtoi/invalid", 3851 test_qemu_strtoi_invalid); 3852 g_test_add_func("/cutils/qemu_strtoi/trailing", 3853 test_qemu_strtoi_trailing); 3854 g_test_add_func("/cutils/qemu_strtoi/octal", 3855 test_qemu_strtoi_octal); 3856 g_test_add_func("/cutils/qemu_strtoi/decimal", 3857 test_qemu_strtoi_decimal); 3858 g_test_add_func("/cutils/qemu_strtoi/hex", 3859 test_qemu_strtoi_hex); 3860 g_test_add_func("/cutils/qemu_strtoi/max", 3861 test_qemu_strtoi_max); 3862 g_test_add_func("/cutils/qemu_strtoi/overflow", 3863 test_qemu_strtoi_overflow); 3864 g_test_add_func("/cutils/qemu_strtoi/min", 3865 test_qemu_strtoi_min); 3866 g_test_add_func("/cutils/qemu_strtoi/underflow", 3867 test_qemu_strtoi_underflow); 3868 g_test_add_func("/cutils/qemu_strtoi/negative", 3869 test_qemu_strtoi_negative); 3870 g_test_add_func("/cutils/qemu_strtoi/negzero", 3871 test_qemu_strtoi_negzero); 3872 g_test_add_func("/cutils/qemu_strtoi_full/correct", 3873 test_qemu_strtoi_full_correct); 3874 g_test_add_func("/cutils/qemu_strtoi_full/null", 3875 test_qemu_strtoi_full_null); 3876 g_test_add_func("/cutils/qemu_strtoi_full/empty", 3877 test_qemu_strtoi_full_empty); 3878 g_test_add_func("/cutils/qemu_strtoi_full/negative", 3879 test_qemu_strtoi_full_negative); 3880 g_test_add_func("/cutils/qemu_strtoi_full/negzero", 3881 test_qemu_strtoi_full_negzero); 3882 g_test_add_func("/cutils/qemu_strtoi_full/trailing", 3883 test_qemu_strtoi_full_trailing); 3884 g_test_add_func("/cutils/qemu_strtoi_full/max", 3885 test_qemu_strtoi_full_max); 3886 g_test_add_func("/cutils/qemu_strtoi_full/erange_junk", 3887 test_qemu_strtoi_full_erange_junk); 3888 3889 /* qemu_strtoui() tests */ 3890 g_test_add_func("/cutils/qemu_strtoui/correct", 3891 test_qemu_strtoui_correct); 3892 g_test_add_func("/cutils/qemu_strtoui/null", 3893 test_qemu_strtoui_null); 3894 g_test_add_func("/cutils/qemu_strtoui/empty", 3895 test_qemu_strtoui_empty); 3896 g_test_add_func("/cutils/qemu_strtoui/whitespace", 3897 test_qemu_strtoui_whitespace); 3898 g_test_add_func("/cutils/qemu_strtoui/invalid", 3899 test_qemu_strtoui_invalid); 3900 g_test_add_func("/cutils/qemu_strtoui/trailing", 3901 test_qemu_strtoui_trailing); 3902 g_test_add_func("/cutils/qemu_strtoui/octal", 3903 test_qemu_strtoui_octal); 3904 g_test_add_func("/cutils/qemu_strtoui/decimal", 3905 test_qemu_strtoui_decimal); 3906 g_test_add_func("/cutils/qemu_strtoui/hex", 3907 test_qemu_strtoui_hex); 3908 g_test_add_func("/cutils/qemu_strtoui/wrap", 3909 test_qemu_strtoui_wrap); 3910 g_test_add_func("/cutils/qemu_strtoui/max", 3911 test_qemu_strtoui_max); 3912 g_test_add_func("/cutils/qemu_strtoui/overflow", 3913 test_qemu_strtoui_overflow); 3914 g_test_add_func("/cutils/qemu_strtoui/underflow", 3915 test_qemu_strtoui_underflow); 3916 g_test_add_func("/cutils/qemu_strtoui/negative", 3917 test_qemu_strtoui_negative); 3918 g_test_add_func("/cutils/qemu_strtoui/negzero", 3919 test_qemu_strtoui_negzero); 3920 g_test_add_func("/cutils/qemu_strtoui_full/correct", 3921 test_qemu_strtoui_full_correct); 3922 g_test_add_func("/cutils/qemu_strtoui_full/null", 3923 test_qemu_strtoui_full_null); 3924 g_test_add_func("/cutils/qemu_strtoui_full/empty", 3925 test_qemu_strtoui_full_empty); 3926 g_test_add_func("/cutils/qemu_strtoui_full/negative", 3927 test_qemu_strtoui_full_negative); 3928 g_test_add_func("/cutils/qemu_strtoui_full/negzero", 3929 test_qemu_strtoui_full_negzero); 3930 g_test_add_func("/cutils/qemu_strtoui_full/trailing", 3931 test_qemu_strtoui_full_trailing); 3932 g_test_add_func("/cutils/qemu_strtoui_full/max", 3933 test_qemu_strtoui_full_max); 3934 g_test_add_func("/cutils/qemu_strtoui_full/erange_junk", 3935 test_qemu_strtoui_full_erange_junk); 3936 3937 /* qemu_strtol() tests */ 3938 g_test_add_func("/cutils/qemu_strtol/correct", 3939 test_qemu_strtol_correct); 3940 g_test_add_func("/cutils/qemu_strtol/null", 3941 test_qemu_strtol_null); 3942 g_test_add_func("/cutils/qemu_strtol/empty", 3943 test_qemu_strtol_empty); 3944 g_test_add_func("/cutils/qemu_strtol/whitespace", 3945 test_qemu_strtol_whitespace); 3946 g_test_add_func("/cutils/qemu_strtol/invalid", 3947 test_qemu_strtol_invalid); 3948 g_test_add_func("/cutils/qemu_strtol/trailing", 3949 test_qemu_strtol_trailing); 3950 g_test_add_func("/cutils/qemu_strtol/octal", 3951 test_qemu_strtol_octal); 3952 g_test_add_func("/cutils/qemu_strtol/decimal", 3953 test_qemu_strtol_decimal); 3954 g_test_add_func("/cutils/qemu_strtol/hex", 3955 test_qemu_strtol_hex); 3956 g_test_add_func("/cutils/qemu_strtol/max", 3957 test_qemu_strtol_max); 3958 g_test_add_func("/cutils/qemu_strtol/overflow", 3959 test_qemu_strtol_overflow); 3960 g_test_add_func("/cutils/qemu_strtol/min", 3961 test_qemu_strtol_min); 3962 g_test_add_func("/cutils/qemu_strtol/underflow", 3963 test_qemu_strtol_underflow); 3964 g_test_add_func("/cutils/qemu_strtol/negative", 3965 test_qemu_strtol_negative); 3966 g_test_add_func("/cutils/qemu_strtol/negzero", 3967 test_qemu_strtol_negzero); 3968 g_test_add_func("/cutils/qemu_strtol_full/correct", 3969 test_qemu_strtol_full_correct); 3970 g_test_add_func("/cutils/qemu_strtol_full/null", 3971 test_qemu_strtol_full_null); 3972 g_test_add_func("/cutils/qemu_strtol_full/empty", 3973 test_qemu_strtol_full_empty); 3974 g_test_add_func("/cutils/qemu_strtol_full/negative", 3975 test_qemu_strtol_full_negative); 3976 g_test_add_func("/cutils/qemu_strtol_full/negzero", 3977 test_qemu_strtol_full_negzero); 3978 g_test_add_func("/cutils/qemu_strtol_full/trailing", 3979 test_qemu_strtol_full_trailing); 3980 g_test_add_func("/cutils/qemu_strtol_full/max", 3981 test_qemu_strtol_full_max); 3982 g_test_add_func("/cutils/qemu_strtol_full/erange_junk", 3983 test_qemu_strtol_full_erange_junk); 3984 3985 /* qemu_strtoul() tests */ 3986 g_test_add_func("/cutils/qemu_strtoul/correct", 3987 test_qemu_strtoul_correct); 3988 g_test_add_func("/cutils/qemu_strtoul/null", 3989 test_qemu_strtoul_null); 3990 g_test_add_func("/cutils/qemu_strtoul/empty", 3991 test_qemu_strtoul_empty); 3992 g_test_add_func("/cutils/qemu_strtoul/whitespace", 3993 test_qemu_strtoul_whitespace); 3994 g_test_add_func("/cutils/qemu_strtoul/invalid", 3995 test_qemu_strtoul_invalid); 3996 g_test_add_func("/cutils/qemu_strtoul/trailing", 3997 test_qemu_strtoul_trailing); 3998 g_test_add_func("/cutils/qemu_strtoul/octal", 3999 test_qemu_strtoul_octal); 4000 g_test_add_func("/cutils/qemu_strtoul/decimal", 4001 test_qemu_strtoul_decimal); 4002 g_test_add_func("/cutils/qemu_strtoul/hex", 4003 test_qemu_strtoul_hex); 4004 g_test_add_func("/cutils/qemu_strtoul/wrap", 4005 test_qemu_strtoul_wrap); 4006 g_test_add_func("/cutils/qemu_strtoul/max", 4007 test_qemu_strtoul_max); 4008 g_test_add_func("/cutils/qemu_strtoul/overflow", 4009 test_qemu_strtoul_overflow); 4010 g_test_add_func("/cutils/qemu_strtoul/underflow", 4011 test_qemu_strtoul_underflow); 4012 g_test_add_func("/cutils/qemu_strtoul/negative", 4013 test_qemu_strtoul_negative); 4014 g_test_add_func("/cutils/qemu_strtoul/negzero", 4015 test_qemu_strtoul_negzero); 4016 g_test_add_func("/cutils/qemu_strtoul_full/correct", 4017 test_qemu_strtoul_full_correct); 4018 g_test_add_func("/cutils/qemu_strtoul_full/null", 4019 test_qemu_strtoul_full_null); 4020 g_test_add_func("/cutils/qemu_strtoul_full/empty", 4021 test_qemu_strtoul_full_empty); 4022 g_test_add_func("/cutils/qemu_strtoul_full/negative", 4023 test_qemu_strtoul_full_negative); 4024 g_test_add_func("/cutils/qemu_strtoul_full/negzero", 4025 test_qemu_strtoul_full_negzero); 4026 g_test_add_func("/cutils/qemu_strtoul_full/trailing", 4027 test_qemu_strtoul_full_trailing); 4028 g_test_add_func("/cutils/qemu_strtoul_full/max", 4029 test_qemu_strtoul_full_max); 4030 g_test_add_func("/cutils/qemu_strtoul_full/erange_junk", 4031 test_qemu_strtoul_full_erange_junk); 4032 4033 /* qemu_strtoi64() tests */ 4034 g_test_add_func("/cutils/qemu_strtoi64/correct", 4035 test_qemu_strtoi64_correct); 4036 g_test_add_func("/cutils/qemu_strtoi64/null", 4037 test_qemu_strtoi64_null); 4038 g_test_add_func("/cutils/qemu_strtoi64/empty", 4039 test_qemu_strtoi64_empty); 4040 g_test_add_func("/cutils/qemu_strtoi64/whitespace", 4041 test_qemu_strtoi64_whitespace); 4042 g_test_add_func("/cutils/qemu_strtoi64/invalid", 4043 test_qemu_strtoi64_invalid); 4044 g_test_add_func("/cutils/qemu_strtoi64/trailing", 4045 test_qemu_strtoi64_trailing); 4046 g_test_add_func("/cutils/qemu_strtoi64/octal", 4047 test_qemu_strtoi64_octal); 4048 g_test_add_func("/cutils/qemu_strtoi64/decimal", 4049 test_qemu_strtoi64_decimal); 4050 g_test_add_func("/cutils/qemu_strtoi64/hex", 4051 test_qemu_strtoi64_hex); 4052 g_test_add_func("/cutils/qemu_strtoi64/max", 4053 test_qemu_strtoi64_max); 4054 g_test_add_func("/cutils/qemu_strtoi64/overflow", 4055 test_qemu_strtoi64_overflow); 4056 g_test_add_func("/cutils/qemu_strtoi64/min", 4057 test_qemu_strtoi64_min); 4058 g_test_add_func("/cutils/qemu_strtoi64/underflow", 4059 test_qemu_strtoi64_underflow); 4060 g_test_add_func("/cutils/qemu_strtoi64/negative", 4061 test_qemu_strtoi64_negative); 4062 g_test_add_func("/cutils/qemu_strtoi64/negzero", 4063 test_qemu_strtoi64_negzero); 4064 g_test_add_func("/cutils/qemu_strtoi64_full/correct", 4065 test_qemu_strtoi64_full_correct); 4066 g_test_add_func("/cutils/qemu_strtoi64_full/null", 4067 test_qemu_strtoi64_full_null); 4068 g_test_add_func("/cutils/qemu_strtoi64_full/empty", 4069 test_qemu_strtoi64_full_empty); 4070 g_test_add_func("/cutils/qemu_strtoi64_full/negative", 4071 test_qemu_strtoi64_full_negative); 4072 g_test_add_func("/cutils/qemu_strtoi64_full/negzero", 4073 test_qemu_strtoi64_full_negzero); 4074 g_test_add_func("/cutils/qemu_strtoi64_full/trailing", 4075 test_qemu_strtoi64_full_trailing); 4076 g_test_add_func("/cutils/qemu_strtoi64_full/max", 4077 test_qemu_strtoi64_full_max); 4078 g_test_add_func("/cutils/qemu_strtoi64_full/erange_junk", 4079 test_qemu_strtoi64_full_erange_junk); 4080 4081 /* qemu_strtou64() tests */ 4082 g_test_add_func("/cutils/qemu_strtou64/correct", 4083 test_qemu_strtou64_correct); 4084 g_test_add_func("/cutils/qemu_strtou64/null", 4085 test_qemu_strtou64_null); 4086 g_test_add_func("/cutils/qemu_strtou64/empty", 4087 test_qemu_strtou64_empty); 4088 g_test_add_func("/cutils/qemu_strtou64/whitespace", 4089 test_qemu_strtou64_whitespace); 4090 g_test_add_func("/cutils/qemu_strtou64/invalid", 4091 test_qemu_strtou64_invalid); 4092 g_test_add_func("/cutils/qemu_strtou64/trailing", 4093 test_qemu_strtou64_trailing); 4094 g_test_add_func("/cutils/qemu_strtou64/octal", 4095 test_qemu_strtou64_octal); 4096 g_test_add_func("/cutils/qemu_strtou64/decimal", 4097 test_qemu_strtou64_decimal); 4098 g_test_add_func("/cutils/qemu_strtou64/hex", 4099 test_qemu_strtou64_hex); 4100 g_test_add_func("/cutils/qemu_strtou64/wrap", 4101 test_qemu_strtou64_wrap); 4102 g_test_add_func("/cutils/qemu_strtou64/max", 4103 test_qemu_strtou64_max); 4104 g_test_add_func("/cutils/qemu_strtou64/overflow", 4105 test_qemu_strtou64_overflow); 4106 g_test_add_func("/cutils/qemu_strtou64/underflow", 4107 test_qemu_strtou64_underflow); 4108 g_test_add_func("/cutils/qemu_strtou64/negative", 4109 test_qemu_strtou64_negative); 4110 g_test_add_func("/cutils/qemu_strtou64/negzero", 4111 test_qemu_strtou64_negzero); 4112 g_test_add_func("/cutils/qemu_strtou64_full/correct", 4113 test_qemu_strtou64_full_correct); 4114 g_test_add_func("/cutils/qemu_strtou64_full/null", 4115 test_qemu_strtou64_full_null); 4116 g_test_add_func("/cutils/qemu_strtou64_full/empty", 4117 test_qemu_strtou64_full_empty); 4118 g_test_add_func("/cutils/qemu_strtou64_full/negative", 4119 test_qemu_strtou64_full_negative); 4120 g_test_add_func("/cutils/qemu_strtou64_full/negzero", 4121 test_qemu_strtou64_full_negzero); 4122 g_test_add_func("/cutils/qemu_strtou64_full/trailing", 4123 test_qemu_strtou64_full_trailing); 4124 g_test_add_func("/cutils/qemu_strtou64_full/max", 4125 test_qemu_strtou64_full_max); 4126 g_test_add_func("/cutils/qemu_strtou64_full/erange_junk", 4127 test_qemu_strtou64_full_erange_junk); 4128 4129 /* qemu_strtod() tests */ 4130 g_test_add_func("/cutils/qemu_strtod/simple", 4131 test_qemu_strtod_simple); 4132 g_test_add_func("/cutils/qemu_strtod/einval", 4133 test_qemu_strtod_einval); 4134 g_test_add_func("/cutils/qemu_strtod/erange", 4135 test_qemu_strtod_erange); 4136 g_test_add_func("/cutils/qemu_strtod/nonfinite", 4137 test_qemu_strtod_nonfinite); 4138 g_test_add_func("/cutils/qemu_strtod/trailing", 4139 test_qemu_strtod_trailing); 4140 g_test_add_func("/cutils/qemu_strtod/erange_junk", 4141 test_qemu_strtod_erange_junk); 4142 4143 /* qemu_strtod_finite() tests */ 4144 g_test_add_func("/cutils/qemu_strtod_finite/simple", 4145 test_qemu_strtod_finite_simple); 4146 g_test_add_func("/cutils/qemu_strtod_finite/einval", 4147 test_qemu_strtod_finite_einval); 4148 g_test_add_func("/cutils/qemu_strtod_finite/erange", 4149 test_qemu_strtod_finite_erange); 4150 g_test_add_func("/cutils/qemu_strtod_finite/nonfinite", 4151 test_qemu_strtod_finite_nonfinite); 4152 g_test_add_func("/cutils/qemu_strtod_finite/trailing", 4153 test_qemu_strtod_finite_trailing); 4154 g_test_add_func("/cutils/qemu_strtod_finite/erange_junk", 4155 test_qemu_strtod_finite_erange_junk); 4156 4157 /* qemu_strtosz() tests */ 4158 g_test_add_func("/cutils/strtosz/simple", 4159 test_qemu_strtosz_simple); 4160 g_test_add_func("/cutils/strtosz/hex", 4161 test_qemu_strtosz_hex); 4162 g_test_add_func("/cutils/strtosz/units", 4163 test_qemu_strtosz_units); 4164 g_test_add_func("/cutils/strtosz/float", 4165 test_qemu_strtosz_float); 4166 g_test_add_func("/cutils/strtosz/invalid", 4167 test_qemu_strtosz_invalid); 4168 g_test_add_func("/cutils/strtosz/trailing", 4169 test_qemu_strtosz_trailing); 4170 g_test_add_func("/cutils/strtosz/erange", 4171 test_qemu_strtosz_erange); 4172 g_test_add_func("/cutils/strtosz/metric", 4173 test_qemu_strtosz_metric); 4174 4175 g_test_add_func("/cutils/size_to_str", 4176 test_size_to_str); 4177 g_test_add_func("/cutils/freq_to_str", 4178 test_freq_to_str); 4179 g_test_add_func("/cutils/iec_binary_prefix", 4180 test_iec_binary_prefix); 4181 g_test_add_func("/cutils/si_prefix", 4182 test_si_prefix); 4183 return g_test_run(); 4184 } 4185