1 /* 2 * cutils.c unit-tests 3 * 4 * Copyright (C) 2013 Red Hat Inc. 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 "qemu/osdep.h" 29 #include "qemu/units.h" 30 #include "qemu/cutils.h" 31 #include "qemu/units.h" 32 33 static void test_parse_uint_null(void) 34 { 35 unsigned long long i = 999; 36 char f = 'X'; 37 char *endptr = &f; 38 int r; 39 40 r = parse_uint(NULL, &i, &endptr, 0); 41 42 g_assert_cmpint(r, ==, -EINVAL); 43 g_assert_cmpint(i, ==, 0); 44 g_assert(endptr == NULL); 45 } 46 47 static void test_parse_uint_empty(void) 48 { 49 unsigned long long i = 999; 50 char f = 'X'; 51 char *endptr = &f; 52 const char *str = ""; 53 int r; 54 55 r = parse_uint(str, &i, &endptr, 0); 56 57 g_assert_cmpint(r, ==, -EINVAL); 58 g_assert_cmpint(i, ==, 0); 59 g_assert(endptr == str); 60 } 61 62 static void test_parse_uint_whitespace(void) 63 { 64 unsigned long long i = 999; 65 char f = 'X'; 66 char *endptr = &f; 67 const char *str = " \t "; 68 int r; 69 70 r = parse_uint(str, &i, &endptr, 0); 71 72 g_assert_cmpint(r, ==, -EINVAL); 73 g_assert_cmpint(i, ==, 0); 74 g_assert(endptr == str); 75 } 76 77 78 static void test_parse_uint_invalid(void) 79 { 80 unsigned long long i = 999; 81 char f = 'X'; 82 char *endptr = &f; 83 const char *str = " \t xxx"; 84 int r; 85 86 r = parse_uint(str, &i, &endptr, 0); 87 88 g_assert_cmpint(r, ==, -EINVAL); 89 g_assert_cmpint(i, ==, 0); 90 g_assert(endptr == str); 91 } 92 93 94 static void test_parse_uint_trailing(void) 95 { 96 unsigned long long i = 999; 97 char f = 'X'; 98 char *endptr = &f; 99 const char *str = "123xxx"; 100 int r; 101 102 r = parse_uint(str, &i, &endptr, 0); 103 104 g_assert_cmpint(r, ==, 0); 105 g_assert_cmpint(i, ==, 123); 106 g_assert(endptr == str + 3); 107 } 108 109 static void test_parse_uint_correct(void) 110 { 111 unsigned long long i = 999; 112 char f = 'X'; 113 char *endptr = &f; 114 const char *str = "123"; 115 int r; 116 117 r = parse_uint(str, &i, &endptr, 0); 118 119 g_assert_cmpint(r, ==, 0); 120 g_assert_cmpint(i, ==, 123); 121 g_assert(endptr == str + strlen(str)); 122 } 123 124 static void test_parse_uint_octal(void) 125 { 126 unsigned long long i = 999; 127 char f = 'X'; 128 char *endptr = &f; 129 const char *str = "0123"; 130 int r; 131 132 r = parse_uint(str, &i, &endptr, 0); 133 134 g_assert_cmpint(r, ==, 0); 135 g_assert_cmpint(i, ==, 0123); 136 g_assert(endptr == str + strlen(str)); 137 } 138 139 static void test_parse_uint_decimal(void) 140 { 141 unsigned long long i = 999; 142 char f = 'X'; 143 char *endptr = &f; 144 const char *str = "0123"; 145 int r; 146 147 r = parse_uint(str, &i, &endptr, 10); 148 149 g_assert_cmpint(r, ==, 0); 150 g_assert_cmpint(i, ==, 123); 151 g_assert(endptr == str + strlen(str)); 152 } 153 154 155 static void test_parse_uint_llong_max(void) 156 { 157 unsigned long long i = 999; 158 char f = 'X'; 159 char *endptr = &f; 160 char *str = g_strdup_printf("%llu", (unsigned long long)LLONG_MAX + 1); 161 int r; 162 163 r = parse_uint(str, &i, &endptr, 0); 164 165 g_assert_cmpint(r, ==, 0); 166 g_assert_cmpint(i, ==, (unsigned long long)LLONG_MAX + 1); 167 g_assert(endptr == str + strlen(str)); 168 169 g_free(str); 170 } 171 172 static void test_parse_uint_overflow(void) 173 { 174 unsigned long long i = 999; 175 char f = 'X'; 176 char *endptr = &f; 177 const char *str = "99999999999999999999999999999999999999"; 178 int r; 179 180 r = parse_uint(str, &i, &endptr, 0); 181 182 g_assert_cmpint(r, ==, -ERANGE); 183 g_assert_cmpint(i, ==, ULLONG_MAX); 184 g_assert(endptr == str + strlen(str)); 185 } 186 187 static void test_parse_uint_negative(void) 188 { 189 unsigned long long i = 999; 190 char f = 'X'; 191 char *endptr = &f; 192 const char *str = " \t -321"; 193 int r; 194 195 r = parse_uint(str, &i, &endptr, 0); 196 197 g_assert_cmpint(r, ==, -ERANGE); 198 g_assert_cmpint(i, ==, 0); 199 g_assert(endptr == str + strlen(str)); 200 } 201 202 203 static void test_parse_uint_full_trailing(void) 204 { 205 unsigned long long i = 999; 206 const char *str = "123xxx"; 207 int r; 208 209 r = parse_uint_full(str, &i, 0); 210 211 g_assert_cmpint(r, ==, -EINVAL); 212 g_assert_cmpint(i, ==, 0); 213 } 214 215 static void test_parse_uint_full_correct(void) 216 { 217 unsigned long long i = 999; 218 const char *str = "123"; 219 int r; 220 221 r = parse_uint_full(str, &i, 0); 222 223 g_assert_cmpint(r, ==, 0); 224 g_assert_cmpint(i, ==, 123); 225 } 226 227 static void test_qemu_strtoi_correct(void) 228 { 229 const char *str = "12345 foo"; 230 char f = 'X'; 231 const char *endptr = &f; 232 int res = 999; 233 int err; 234 235 err = qemu_strtoi(str, &endptr, 0, &res); 236 237 g_assert_cmpint(err, ==, 0); 238 g_assert_cmpint(res, ==, 12345); 239 g_assert(endptr == str + 5); 240 } 241 242 static void test_qemu_strtoi_null(void) 243 { 244 char f = 'X'; 245 const char *endptr = &f; 246 int res = 999; 247 int err; 248 249 err = qemu_strtoi(NULL, &endptr, 0, &res); 250 251 g_assert_cmpint(err, ==, -EINVAL); 252 g_assert(endptr == NULL); 253 } 254 255 static void test_qemu_strtoi_empty(void) 256 { 257 const char *str = ""; 258 char f = 'X'; 259 const char *endptr = &f; 260 int res = 999; 261 int err; 262 263 err = qemu_strtoi(str, &endptr, 0, &res); 264 265 g_assert_cmpint(err, ==, -EINVAL); 266 g_assert(endptr == str); 267 } 268 269 static void test_qemu_strtoi_whitespace(void) 270 { 271 const char *str = " \t "; 272 char f = 'X'; 273 const char *endptr = &f; 274 int res = 999; 275 int err; 276 277 err = qemu_strtoi(str, &endptr, 0, &res); 278 279 g_assert_cmpint(err, ==, -EINVAL); 280 g_assert(endptr == str); 281 } 282 283 static void test_qemu_strtoi_invalid(void) 284 { 285 const char *str = " xxxx \t abc"; 286 char f = 'X'; 287 const char *endptr = &f; 288 int res = 999; 289 int err; 290 291 err = qemu_strtoi(str, &endptr, 0, &res); 292 293 g_assert_cmpint(err, ==, -EINVAL); 294 g_assert(endptr == str); 295 } 296 297 static void test_qemu_strtoi_trailing(void) 298 { 299 const char *str = "123xxx"; 300 char f = 'X'; 301 const char *endptr = &f; 302 int res = 999; 303 int err; 304 305 err = qemu_strtoi(str, &endptr, 0, &res); 306 307 g_assert_cmpint(err, ==, 0); 308 g_assert_cmpint(res, ==, 123); 309 g_assert(endptr == str + 3); 310 } 311 312 static void test_qemu_strtoi_octal(void) 313 { 314 const char *str = "0123"; 315 char f = 'X'; 316 const char *endptr = &f; 317 int res = 999; 318 int err; 319 320 err = qemu_strtoi(str, &endptr, 8, &res); 321 322 g_assert_cmpint(err, ==, 0); 323 g_assert_cmpint(res, ==, 0123); 324 g_assert(endptr == str + strlen(str)); 325 326 res = 999; 327 endptr = &f; 328 err = qemu_strtoi(str, &endptr, 0, &res); 329 330 g_assert_cmpint(err, ==, 0); 331 g_assert_cmpint(res, ==, 0123); 332 g_assert(endptr == str + strlen(str)); 333 } 334 335 static void test_qemu_strtoi_decimal(void) 336 { 337 const char *str = "0123"; 338 char f = 'X'; 339 const char *endptr = &f; 340 int res = 999; 341 int err; 342 343 err = qemu_strtoi(str, &endptr, 10, &res); 344 345 g_assert_cmpint(err, ==, 0); 346 g_assert_cmpint(res, ==, 123); 347 g_assert(endptr == str + strlen(str)); 348 349 str = "123"; 350 res = 999; 351 endptr = &f; 352 err = qemu_strtoi(str, &endptr, 0, &res); 353 354 g_assert_cmpint(err, ==, 0); 355 g_assert_cmpint(res, ==, 123); 356 g_assert(endptr == str + strlen(str)); 357 } 358 359 static void test_qemu_strtoi_hex(void) 360 { 361 const char *str = "0123"; 362 char f = 'X'; 363 const char *endptr = &f; 364 int res = 999; 365 int err; 366 367 err = qemu_strtoi(str, &endptr, 16, &res); 368 369 g_assert_cmpint(err, ==, 0); 370 g_assert_cmpint(res, ==, 0x123); 371 g_assert(endptr == str + strlen(str)); 372 373 str = "0x123"; 374 res = 999; 375 endptr = &f; 376 err = qemu_strtoi(str, &endptr, 0, &res); 377 378 g_assert_cmpint(err, ==, 0); 379 g_assert_cmpint(res, ==, 0x123); 380 g_assert(endptr == str + strlen(str)); 381 } 382 383 static void test_qemu_strtoi_max(void) 384 { 385 char *str = g_strdup_printf("%d", INT_MAX); 386 char f = 'X'; 387 const char *endptr = &f; 388 int res = 999; 389 int err; 390 391 err = qemu_strtoi(str, &endptr, 0, &res); 392 393 g_assert_cmpint(err, ==, 0); 394 g_assert_cmpint(res, ==, INT_MAX); 395 g_assert(endptr == str + strlen(str)); 396 g_free(str); 397 } 398 399 static void test_qemu_strtoi_overflow(void) 400 { 401 char *str = g_strdup_printf("%lld", (long long)INT_MAX + 1ll); 402 char f = 'X'; 403 const char *endptr = &f; 404 int res = 999; 405 int err; 406 407 err = qemu_strtoi(str, &endptr, 0, &res); 408 409 g_assert_cmpint(err, ==, -ERANGE); 410 g_assert_cmpint(res, ==, INT_MAX); 411 g_assert(endptr == str + strlen(str)); 412 g_free(str); 413 } 414 415 static void test_qemu_strtoi_underflow(void) 416 { 417 char *str = g_strdup_printf("%lld", (long long)INT_MIN - 1ll); 418 char f = 'X'; 419 const char *endptr = &f; 420 int res = 999; 421 int err; 422 423 err = qemu_strtoi(str, &endptr, 0, &res); 424 425 g_assert_cmpint(err, ==, -ERANGE); 426 g_assert_cmpint(res, ==, INT_MIN); 427 g_assert(endptr == str + strlen(str)); 428 g_free(str); 429 } 430 431 static void test_qemu_strtoi_negative(void) 432 { 433 const char *str = " \t -321"; 434 char f = 'X'; 435 const char *endptr = &f; 436 int res = 999; 437 int err; 438 439 err = qemu_strtoi(str, &endptr, 0, &res); 440 441 g_assert_cmpint(err, ==, 0); 442 g_assert_cmpint(res, ==, -321); 443 g_assert(endptr == str + strlen(str)); 444 } 445 446 static void test_qemu_strtoi_full_correct(void) 447 { 448 const char *str = "123"; 449 int res = 999; 450 int err; 451 452 err = qemu_strtoi(str, NULL, 0, &res); 453 454 g_assert_cmpint(err, ==, 0); 455 g_assert_cmpint(res, ==, 123); 456 } 457 458 static void test_qemu_strtoi_full_null(void) 459 { 460 char f = 'X'; 461 const char *endptr = &f; 462 int res = 999; 463 int err; 464 465 err = qemu_strtoi(NULL, &endptr, 0, &res); 466 467 g_assert_cmpint(err, ==, -EINVAL); 468 g_assert(endptr == NULL); 469 } 470 471 static void test_qemu_strtoi_full_empty(void) 472 { 473 const char *str = ""; 474 int res = 999L; 475 int err; 476 477 err = qemu_strtoi(str, NULL, 0, &res); 478 479 g_assert_cmpint(err, ==, -EINVAL); 480 } 481 482 static void test_qemu_strtoi_full_negative(void) 483 { 484 const char *str = " \t -321"; 485 int res = 999; 486 int err; 487 488 err = qemu_strtoi(str, NULL, 0, &res); 489 490 g_assert_cmpint(err, ==, 0); 491 g_assert_cmpint(res, ==, -321); 492 } 493 494 static void test_qemu_strtoi_full_trailing(void) 495 { 496 const char *str = "123xxx"; 497 int res; 498 int err; 499 500 err = qemu_strtoi(str, NULL, 0, &res); 501 502 g_assert_cmpint(err, ==, -EINVAL); 503 } 504 505 static void test_qemu_strtoi_full_max(void) 506 { 507 char *str = g_strdup_printf("%d", INT_MAX); 508 int res; 509 int err; 510 511 err = qemu_strtoi(str, NULL, 0, &res); 512 513 g_assert_cmpint(err, ==, 0); 514 g_assert_cmpint(res, ==, INT_MAX); 515 g_free(str); 516 } 517 518 static void test_qemu_strtoui_correct(void) 519 { 520 const char *str = "12345 foo"; 521 char f = 'X'; 522 const char *endptr = &f; 523 unsigned int res = 999; 524 int err; 525 526 err = qemu_strtoui(str, &endptr, 0, &res); 527 528 g_assert_cmpint(err, ==, 0); 529 g_assert_cmpuint(res, ==, 12345); 530 g_assert(endptr == str + 5); 531 } 532 533 static void test_qemu_strtoui_null(void) 534 { 535 char f = 'X'; 536 const char *endptr = &f; 537 unsigned int res = 999; 538 int err; 539 540 err = qemu_strtoui(NULL, &endptr, 0, &res); 541 542 g_assert_cmpint(err, ==, -EINVAL); 543 g_assert(endptr == NULL); 544 } 545 546 static void test_qemu_strtoui_empty(void) 547 { 548 const char *str = ""; 549 char f = 'X'; 550 const char *endptr = &f; 551 unsigned int res = 999; 552 int err; 553 554 err = qemu_strtoui(str, &endptr, 0, &res); 555 556 g_assert_cmpint(err, ==, -EINVAL); 557 g_assert(endptr == str); 558 } 559 560 static void test_qemu_strtoui_whitespace(void) 561 { 562 const char *str = " \t "; 563 char f = 'X'; 564 const char *endptr = &f; 565 unsigned int res = 999; 566 int err; 567 568 err = qemu_strtoui(str, &endptr, 0, &res); 569 570 g_assert_cmpint(err, ==, -EINVAL); 571 g_assert(endptr == str); 572 } 573 574 static void test_qemu_strtoui_invalid(void) 575 { 576 const char *str = " xxxx \t abc"; 577 char f = 'X'; 578 const char *endptr = &f; 579 unsigned int res = 999; 580 int err; 581 582 err = qemu_strtoui(str, &endptr, 0, &res); 583 584 g_assert_cmpint(err, ==, -EINVAL); 585 g_assert(endptr == str); 586 } 587 588 static void test_qemu_strtoui_trailing(void) 589 { 590 const char *str = "123xxx"; 591 char f = 'X'; 592 const char *endptr = &f; 593 unsigned int res = 999; 594 int err; 595 596 err = qemu_strtoui(str, &endptr, 0, &res); 597 598 g_assert_cmpint(err, ==, 0); 599 g_assert_cmpuint(res, ==, 123); 600 g_assert(endptr == str + 3); 601 } 602 603 static void test_qemu_strtoui_octal(void) 604 { 605 const char *str = "0123"; 606 char f = 'X'; 607 const char *endptr = &f; 608 unsigned int res = 999; 609 int err; 610 611 err = qemu_strtoui(str, &endptr, 8, &res); 612 613 g_assert_cmpint(err, ==, 0); 614 g_assert_cmpuint(res, ==, 0123); 615 g_assert(endptr == str + strlen(str)); 616 617 res = 999; 618 endptr = &f; 619 err = qemu_strtoui(str, &endptr, 0, &res); 620 621 g_assert_cmpint(err, ==, 0); 622 g_assert_cmpuint(res, ==, 0123); 623 g_assert(endptr == str + strlen(str)); 624 } 625 626 static void test_qemu_strtoui_decimal(void) 627 { 628 const char *str = "0123"; 629 char f = 'X'; 630 const char *endptr = &f; 631 unsigned int res = 999; 632 int err; 633 634 err = qemu_strtoui(str, &endptr, 10, &res); 635 636 g_assert_cmpint(err, ==, 0); 637 g_assert_cmpuint(res, ==, 123); 638 g_assert(endptr == str + strlen(str)); 639 640 str = "123"; 641 res = 999; 642 endptr = &f; 643 err = qemu_strtoui(str, &endptr, 0, &res); 644 645 g_assert_cmpint(err, ==, 0); 646 g_assert_cmpuint(res, ==, 123); 647 g_assert(endptr == str + strlen(str)); 648 } 649 650 static void test_qemu_strtoui_hex(void) 651 { 652 const char *str = "0123"; 653 char f = 'X'; 654 const char *endptr = &f; 655 unsigned int res = 999; 656 int err; 657 658 err = qemu_strtoui(str, &endptr, 16, &res); 659 660 g_assert_cmpint(err, ==, 0); 661 g_assert_cmphex(res, ==, 0x123); 662 g_assert(endptr == str + strlen(str)); 663 664 str = "0x123"; 665 res = 999; 666 endptr = &f; 667 err = qemu_strtoui(str, &endptr, 0, &res); 668 669 g_assert_cmpint(err, ==, 0); 670 g_assert_cmphex(res, ==, 0x123); 671 g_assert(endptr == str + strlen(str)); 672 } 673 674 static void test_qemu_strtoui_max(void) 675 { 676 char *str = g_strdup_printf("%u", UINT_MAX); 677 char f = 'X'; 678 const char *endptr = &f; 679 unsigned int res = 999; 680 int err; 681 682 err = qemu_strtoui(str, &endptr, 0, &res); 683 684 g_assert_cmpint(err, ==, 0); 685 g_assert_cmphex(res, ==, UINT_MAX); 686 g_assert(endptr == str + strlen(str)); 687 g_free(str); 688 } 689 690 static void test_qemu_strtoui_overflow(void) 691 { 692 char *str = g_strdup_printf("%lld", (long long)UINT_MAX + 1ll); 693 char f = 'X'; 694 const char *endptr = &f; 695 unsigned int res = 999; 696 int err; 697 698 err = qemu_strtoui(str, &endptr, 0, &res); 699 700 g_assert_cmpint(err, ==, -ERANGE); 701 g_assert_cmphex(res, ==, UINT_MAX); 702 g_assert(endptr == str + strlen(str)); 703 g_free(str); 704 } 705 706 static void test_qemu_strtoui_underflow(void) 707 { 708 char *str = g_strdup_printf("%lld", (long long)INT_MIN - 1ll); 709 char f = 'X'; 710 const char *endptr = &f; 711 unsigned int res = 999; 712 int err; 713 714 err = qemu_strtoui(str, &endptr, 0, &res); 715 716 g_assert_cmpint(err, ==, -ERANGE); 717 g_assert_cmpuint(res, ==, (unsigned int)-1); 718 g_assert(endptr == str + strlen(str)); 719 g_free(str); 720 } 721 722 static void test_qemu_strtoui_negative(void) 723 { 724 const char *str = " \t -321"; 725 char f = 'X'; 726 const char *endptr = &f; 727 unsigned int res = 999; 728 int err; 729 730 err = qemu_strtoui(str, &endptr, 0, &res); 731 732 g_assert_cmpint(err, ==, 0); 733 g_assert_cmpuint(res, ==, (unsigned int)-321); 734 g_assert(endptr == str + strlen(str)); 735 } 736 737 static void test_qemu_strtoui_full_correct(void) 738 { 739 const char *str = "123"; 740 unsigned int res = 999; 741 int err; 742 743 err = qemu_strtoui(str, NULL, 0, &res); 744 745 g_assert_cmpint(err, ==, 0); 746 g_assert_cmpuint(res, ==, 123); 747 } 748 749 static void test_qemu_strtoui_full_null(void) 750 { 751 unsigned int res = 999; 752 int err; 753 754 err = qemu_strtoui(NULL, NULL, 0, &res); 755 756 g_assert_cmpint(err, ==, -EINVAL); 757 } 758 759 static void test_qemu_strtoui_full_empty(void) 760 { 761 const char *str = ""; 762 unsigned int res = 999; 763 int err; 764 765 err = qemu_strtoui(str, NULL, 0, &res); 766 767 g_assert_cmpint(err, ==, -EINVAL); 768 } 769 static void test_qemu_strtoui_full_negative(void) 770 { 771 const char *str = " \t -321"; 772 unsigned int res = 999; 773 int err; 774 775 err = qemu_strtoui(str, NULL, 0, &res); 776 g_assert_cmpint(err, ==, 0); 777 g_assert_cmpuint(res, ==, (unsigned int)-321); 778 } 779 780 static void test_qemu_strtoui_full_trailing(void) 781 { 782 const char *str = "123xxx"; 783 unsigned int res; 784 int err; 785 786 err = qemu_strtoui(str, NULL, 0, &res); 787 788 g_assert_cmpint(err, ==, -EINVAL); 789 } 790 791 static void test_qemu_strtoui_full_max(void) 792 { 793 char *str = g_strdup_printf("%u", UINT_MAX); 794 unsigned int res = 999; 795 int err; 796 797 err = qemu_strtoui(str, NULL, 0, &res); 798 799 g_assert_cmpint(err, ==, 0); 800 g_assert_cmphex(res, ==, UINT_MAX); 801 g_free(str); 802 } 803 804 static void test_qemu_strtol_correct(void) 805 { 806 const char *str = "12345 foo"; 807 char f = 'X'; 808 const char *endptr = &f; 809 long res = 999; 810 int err; 811 812 err = qemu_strtol(str, &endptr, 0, &res); 813 814 g_assert_cmpint(err, ==, 0); 815 g_assert_cmpint(res, ==, 12345); 816 g_assert(endptr == str + 5); 817 } 818 819 static void test_qemu_strtol_null(void) 820 { 821 char f = 'X'; 822 const char *endptr = &f; 823 long res = 999; 824 int err; 825 826 err = qemu_strtol(NULL, &endptr, 0, &res); 827 828 g_assert_cmpint(err, ==, -EINVAL); 829 g_assert(endptr == NULL); 830 } 831 832 static void test_qemu_strtol_empty(void) 833 { 834 const char *str = ""; 835 char f = 'X'; 836 const char *endptr = &f; 837 long res = 999; 838 int err; 839 840 err = qemu_strtol(str, &endptr, 0, &res); 841 842 g_assert_cmpint(err, ==, -EINVAL); 843 g_assert(endptr == str); 844 } 845 846 static void test_qemu_strtol_whitespace(void) 847 { 848 const char *str = " \t "; 849 char f = 'X'; 850 const char *endptr = &f; 851 long res = 999; 852 int err; 853 854 err = qemu_strtol(str, &endptr, 0, &res); 855 856 g_assert_cmpint(err, ==, -EINVAL); 857 g_assert(endptr == str); 858 } 859 860 static void test_qemu_strtol_invalid(void) 861 { 862 const char *str = " xxxx \t abc"; 863 char f = 'X'; 864 const char *endptr = &f; 865 long res = 999; 866 int err; 867 868 err = qemu_strtol(str, &endptr, 0, &res); 869 870 g_assert_cmpint(err, ==, -EINVAL); 871 g_assert(endptr == str); 872 } 873 874 static void test_qemu_strtol_trailing(void) 875 { 876 const char *str = "123xxx"; 877 char f = 'X'; 878 const char *endptr = &f; 879 long res = 999; 880 int err; 881 882 err = qemu_strtol(str, &endptr, 0, &res); 883 884 g_assert_cmpint(err, ==, 0); 885 g_assert_cmpint(res, ==, 123); 886 g_assert(endptr == str + 3); 887 } 888 889 static void test_qemu_strtol_octal(void) 890 { 891 const char *str = "0123"; 892 char f = 'X'; 893 const char *endptr = &f; 894 long res = 999; 895 int err; 896 897 err = qemu_strtol(str, &endptr, 8, &res); 898 899 g_assert_cmpint(err, ==, 0); 900 g_assert_cmpint(res, ==, 0123); 901 g_assert(endptr == str + strlen(str)); 902 903 res = 999; 904 endptr = &f; 905 err = qemu_strtol(str, &endptr, 0, &res); 906 907 g_assert_cmpint(err, ==, 0); 908 g_assert_cmpint(res, ==, 0123); 909 g_assert(endptr == str + strlen(str)); 910 } 911 912 static void test_qemu_strtol_decimal(void) 913 { 914 const char *str = "0123"; 915 char f = 'X'; 916 const char *endptr = &f; 917 long res = 999; 918 int err; 919 920 err = qemu_strtol(str, &endptr, 10, &res); 921 922 g_assert_cmpint(err, ==, 0); 923 g_assert_cmpint(res, ==, 123); 924 g_assert(endptr == str + strlen(str)); 925 926 str = "123"; 927 res = 999; 928 endptr = &f; 929 err = qemu_strtol(str, &endptr, 0, &res); 930 931 g_assert_cmpint(err, ==, 0); 932 g_assert_cmpint(res, ==, 123); 933 g_assert(endptr == str + strlen(str)); 934 } 935 936 static void test_qemu_strtol_hex(void) 937 { 938 const char *str = "0123"; 939 char f = 'X'; 940 const char *endptr = &f; 941 long res = 999; 942 int err; 943 944 err = qemu_strtol(str, &endptr, 16, &res); 945 946 g_assert_cmpint(err, ==, 0); 947 g_assert_cmpint(res, ==, 0x123); 948 g_assert(endptr == str + strlen(str)); 949 950 str = "0x123"; 951 res = 999; 952 endptr = &f; 953 err = qemu_strtol(str, &endptr, 0, &res); 954 955 g_assert_cmpint(err, ==, 0); 956 g_assert_cmpint(res, ==, 0x123); 957 g_assert(endptr == str + strlen(str)); 958 } 959 960 static void test_qemu_strtol_max(void) 961 { 962 char *str = g_strdup_printf("%ld", LONG_MAX); 963 char f = 'X'; 964 const char *endptr = &f; 965 long res = 999; 966 int err; 967 968 err = qemu_strtol(str, &endptr, 0, &res); 969 970 g_assert_cmpint(err, ==, 0); 971 g_assert_cmpint(res, ==, LONG_MAX); 972 g_assert(endptr == str + strlen(str)); 973 g_free(str); 974 } 975 976 static void test_qemu_strtol_overflow(void) 977 { 978 const char *str = "99999999999999999999999999999999999999999999"; 979 char f = 'X'; 980 const char *endptr = &f; 981 long res = 999; 982 int err; 983 984 err = qemu_strtol(str, &endptr, 0, &res); 985 986 g_assert_cmpint(err, ==, -ERANGE); 987 g_assert_cmpint(res, ==, LONG_MAX); 988 g_assert(endptr == str + strlen(str)); 989 } 990 991 static void test_qemu_strtol_underflow(void) 992 { 993 const char *str = "-99999999999999999999999999999999999999999999"; 994 char f = 'X'; 995 const char *endptr = &f; 996 long res = 999; 997 int err; 998 999 err = qemu_strtol(str, &endptr, 0, &res); 1000 1001 g_assert_cmpint(err, ==, -ERANGE); 1002 g_assert_cmpint(res, ==, LONG_MIN); 1003 g_assert(endptr == str + strlen(str)); 1004 } 1005 1006 static void test_qemu_strtol_negative(void) 1007 { 1008 const char *str = " \t -321"; 1009 char f = 'X'; 1010 const char *endptr = &f; 1011 long res = 999; 1012 int err; 1013 1014 err = qemu_strtol(str, &endptr, 0, &res); 1015 1016 g_assert_cmpint(err, ==, 0); 1017 g_assert_cmpint(res, ==, -321); 1018 g_assert(endptr == str + strlen(str)); 1019 } 1020 1021 static void test_qemu_strtol_full_correct(void) 1022 { 1023 const char *str = "123"; 1024 long res = 999; 1025 int err; 1026 1027 err = qemu_strtol(str, NULL, 0, &res); 1028 1029 g_assert_cmpint(err, ==, 0); 1030 g_assert_cmpint(res, ==, 123); 1031 } 1032 1033 static void test_qemu_strtol_full_null(void) 1034 { 1035 char f = 'X'; 1036 const char *endptr = &f; 1037 long res = 999; 1038 int err; 1039 1040 err = qemu_strtol(NULL, &endptr, 0, &res); 1041 1042 g_assert_cmpint(err, ==, -EINVAL); 1043 g_assert(endptr == NULL); 1044 } 1045 1046 static void test_qemu_strtol_full_empty(void) 1047 { 1048 const char *str = ""; 1049 long res = 999L; 1050 int err; 1051 1052 err = qemu_strtol(str, NULL, 0, &res); 1053 1054 g_assert_cmpint(err, ==, -EINVAL); 1055 } 1056 1057 static void test_qemu_strtol_full_negative(void) 1058 { 1059 const char *str = " \t -321"; 1060 long res = 999; 1061 int err; 1062 1063 err = qemu_strtol(str, NULL, 0, &res); 1064 1065 g_assert_cmpint(err, ==, 0); 1066 g_assert_cmpint(res, ==, -321); 1067 } 1068 1069 static void test_qemu_strtol_full_trailing(void) 1070 { 1071 const char *str = "123xxx"; 1072 long res; 1073 int err; 1074 1075 err = qemu_strtol(str, NULL, 0, &res); 1076 1077 g_assert_cmpint(err, ==, -EINVAL); 1078 } 1079 1080 static void test_qemu_strtol_full_max(void) 1081 { 1082 char *str = g_strdup_printf("%ld", LONG_MAX); 1083 long res; 1084 int err; 1085 1086 err = qemu_strtol(str, NULL, 0, &res); 1087 1088 g_assert_cmpint(err, ==, 0); 1089 g_assert_cmpint(res, ==, LONG_MAX); 1090 g_free(str); 1091 } 1092 1093 static void test_qemu_strtoul_correct(void) 1094 { 1095 const char *str = "12345 foo"; 1096 char f = 'X'; 1097 const char *endptr = &f; 1098 unsigned long res = 999; 1099 int err; 1100 1101 err = qemu_strtoul(str, &endptr, 0, &res); 1102 1103 g_assert_cmpint(err, ==, 0); 1104 g_assert_cmpuint(res, ==, 12345); 1105 g_assert(endptr == str + 5); 1106 } 1107 1108 static void test_qemu_strtoul_null(void) 1109 { 1110 char f = 'X'; 1111 const char *endptr = &f; 1112 unsigned long res = 999; 1113 int err; 1114 1115 err = qemu_strtoul(NULL, &endptr, 0, &res); 1116 1117 g_assert_cmpint(err, ==, -EINVAL); 1118 g_assert(endptr == NULL); 1119 } 1120 1121 static void test_qemu_strtoul_empty(void) 1122 { 1123 const char *str = ""; 1124 char f = 'X'; 1125 const char *endptr = &f; 1126 unsigned long res = 999; 1127 int err; 1128 1129 err = qemu_strtoul(str, &endptr, 0, &res); 1130 1131 g_assert_cmpint(err, ==, -EINVAL); 1132 g_assert(endptr == str); 1133 } 1134 1135 static void test_qemu_strtoul_whitespace(void) 1136 { 1137 const char *str = " \t "; 1138 char f = 'X'; 1139 const char *endptr = &f; 1140 unsigned long res = 999; 1141 int err; 1142 1143 err = qemu_strtoul(str, &endptr, 0, &res); 1144 1145 g_assert_cmpint(err, ==, -EINVAL); 1146 g_assert(endptr == str); 1147 } 1148 1149 static void test_qemu_strtoul_invalid(void) 1150 { 1151 const char *str = " xxxx \t abc"; 1152 char f = 'X'; 1153 const char *endptr = &f; 1154 unsigned long res = 999; 1155 int err; 1156 1157 err = qemu_strtoul(str, &endptr, 0, &res); 1158 1159 g_assert_cmpint(err, ==, -EINVAL); 1160 g_assert(endptr == str); 1161 } 1162 1163 static void test_qemu_strtoul_trailing(void) 1164 { 1165 const char *str = "123xxx"; 1166 char f = 'X'; 1167 const char *endptr = &f; 1168 unsigned long res = 999; 1169 int err; 1170 1171 err = qemu_strtoul(str, &endptr, 0, &res); 1172 1173 g_assert_cmpint(err, ==, 0); 1174 g_assert_cmpuint(res, ==, 123); 1175 g_assert(endptr == str + 3); 1176 } 1177 1178 static void test_qemu_strtoul_octal(void) 1179 { 1180 const char *str = "0123"; 1181 char f = 'X'; 1182 const char *endptr = &f; 1183 unsigned long res = 999; 1184 int err; 1185 1186 err = qemu_strtoul(str, &endptr, 8, &res); 1187 1188 g_assert_cmpint(err, ==, 0); 1189 g_assert_cmpuint(res, ==, 0123); 1190 g_assert(endptr == str + strlen(str)); 1191 1192 res = 999; 1193 endptr = &f; 1194 err = qemu_strtoul(str, &endptr, 0, &res); 1195 1196 g_assert_cmpint(err, ==, 0); 1197 g_assert_cmpuint(res, ==, 0123); 1198 g_assert(endptr == str + strlen(str)); 1199 } 1200 1201 static void test_qemu_strtoul_decimal(void) 1202 { 1203 const char *str = "0123"; 1204 char f = 'X'; 1205 const char *endptr = &f; 1206 unsigned long res = 999; 1207 int err; 1208 1209 err = qemu_strtoul(str, &endptr, 10, &res); 1210 1211 g_assert_cmpint(err, ==, 0); 1212 g_assert_cmpuint(res, ==, 123); 1213 g_assert(endptr == str + strlen(str)); 1214 1215 str = "123"; 1216 res = 999; 1217 endptr = &f; 1218 err = qemu_strtoul(str, &endptr, 0, &res); 1219 1220 g_assert_cmpint(err, ==, 0); 1221 g_assert_cmpuint(res, ==, 123); 1222 g_assert(endptr == str + strlen(str)); 1223 } 1224 1225 static void test_qemu_strtoul_hex(void) 1226 { 1227 const char *str = "0123"; 1228 char f = 'X'; 1229 const char *endptr = &f; 1230 unsigned long res = 999; 1231 int err; 1232 1233 err = qemu_strtoul(str, &endptr, 16, &res); 1234 1235 g_assert_cmpint(err, ==, 0); 1236 g_assert_cmphex(res, ==, 0x123); 1237 g_assert(endptr == str + strlen(str)); 1238 1239 str = "0x123"; 1240 res = 999; 1241 endptr = &f; 1242 err = qemu_strtoul(str, &endptr, 0, &res); 1243 1244 g_assert_cmpint(err, ==, 0); 1245 g_assert_cmphex(res, ==, 0x123); 1246 g_assert(endptr == str + strlen(str)); 1247 } 1248 1249 static void test_qemu_strtoul_max(void) 1250 { 1251 char *str = g_strdup_printf("%lu", ULONG_MAX); 1252 char f = 'X'; 1253 const char *endptr = &f; 1254 unsigned long res = 999; 1255 int err; 1256 1257 err = qemu_strtoul(str, &endptr, 0, &res); 1258 1259 g_assert_cmpint(err, ==, 0); 1260 g_assert_cmphex(res, ==, ULONG_MAX); 1261 g_assert(endptr == str + strlen(str)); 1262 g_free(str); 1263 } 1264 1265 static void test_qemu_strtoul_overflow(void) 1266 { 1267 const char *str = "99999999999999999999999999999999999999999999"; 1268 char f = 'X'; 1269 const char *endptr = &f; 1270 unsigned long res = 999; 1271 int err; 1272 1273 err = qemu_strtoul(str, &endptr, 0, &res); 1274 1275 g_assert_cmpint(err, ==, -ERANGE); 1276 g_assert_cmphex(res, ==, ULONG_MAX); 1277 g_assert(endptr == str + strlen(str)); 1278 } 1279 1280 static void test_qemu_strtoul_underflow(void) 1281 { 1282 const char *str = "-99999999999999999999999999999999999999999999"; 1283 char f = 'X'; 1284 const char *endptr = &f; 1285 unsigned long res = 999; 1286 int err; 1287 1288 err = qemu_strtoul(str, &endptr, 0, &res); 1289 1290 g_assert_cmpint(err, ==, -ERANGE); 1291 g_assert_cmpuint(res, ==, -1ul); 1292 g_assert(endptr == str + strlen(str)); 1293 } 1294 1295 static void test_qemu_strtoul_negative(void) 1296 { 1297 const char *str = " \t -321"; 1298 char f = 'X'; 1299 const char *endptr = &f; 1300 unsigned long res = 999; 1301 int err; 1302 1303 err = qemu_strtoul(str, &endptr, 0, &res); 1304 1305 g_assert_cmpint(err, ==, 0); 1306 g_assert_cmpuint(res, ==, -321ul); 1307 g_assert(endptr == str + strlen(str)); 1308 } 1309 1310 static void test_qemu_strtoul_full_correct(void) 1311 { 1312 const char *str = "123"; 1313 unsigned long res = 999; 1314 int err; 1315 1316 err = qemu_strtoul(str, NULL, 0, &res); 1317 1318 g_assert_cmpint(err, ==, 0); 1319 g_assert_cmpuint(res, ==, 123); 1320 } 1321 1322 static void test_qemu_strtoul_full_null(void) 1323 { 1324 unsigned long res = 999; 1325 int err; 1326 1327 err = qemu_strtoul(NULL, NULL, 0, &res); 1328 1329 g_assert_cmpint(err, ==, -EINVAL); 1330 } 1331 1332 static void test_qemu_strtoul_full_empty(void) 1333 { 1334 const char *str = ""; 1335 unsigned long res = 999; 1336 int err; 1337 1338 err = qemu_strtoul(str, NULL, 0, &res); 1339 1340 g_assert_cmpint(err, ==, -EINVAL); 1341 } 1342 static void test_qemu_strtoul_full_negative(void) 1343 { 1344 const char *str = " \t -321"; 1345 unsigned long res = 999; 1346 int err; 1347 1348 err = qemu_strtoul(str, NULL, 0, &res); 1349 g_assert_cmpint(err, ==, 0); 1350 g_assert_cmpuint(res, ==, -321ul); 1351 } 1352 1353 static void test_qemu_strtoul_full_trailing(void) 1354 { 1355 const char *str = "123xxx"; 1356 unsigned long res; 1357 int err; 1358 1359 err = qemu_strtoul(str, NULL, 0, &res); 1360 1361 g_assert_cmpint(err, ==, -EINVAL); 1362 } 1363 1364 static void test_qemu_strtoul_full_max(void) 1365 { 1366 char *str = g_strdup_printf("%lu", ULONG_MAX); 1367 unsigned long res = 999; 1368 int err; 1369 1370 err = qemu_strtoul(str, NULL, 0, &res); 1371 1372 g_assert_cmpint(err, ==, 0); 1373 g_assert_cmphex(res, ==, ULONG_MAX); 1374 g_free(str); 1375 } 1376 1377 static void test_qemu_strtoi64_correct(void) 1378 { 1379 const char *str = "12345 foo"; 1380 char f = 'X'; 1381 const char *endptr = &f; 1382 int64_t res = 999; 1383 int err; 1384 1385 err = qemu_strtoi64(str, &endptr, 0, &res); 1386 1387 g_assert_cmpint(err, ==, 0); 1388 g_assert_cmpint(res, ==, 12345); 1389 g_assert(endptr == str + 5); 1390 } 1391 1392 static void test_qemu_strtoi64_null(void) 1393 { 1394 char f = 'X'; 1395 const char *endptr = &f; 1396 int64_t res = 999; 1397 int err; 1398 1399 err = qemu_strtoi64(NULL, &endptr, 0, &res); 1400 1401 g_assert_cmpint(err, ==, -EINVAL); 1402 g_assert(endptr == NULL); 1403 } 1404 1405 static void test_qemu_strtoi64_empty(void) 1406 { 1407 const char *str = ""; 1408 char f = 'X'; 1409 const char *endptr = &f; 1410 int64_t res = 999; 1411 int err; 1412 1413 err = qemu_strtoi64(str, &endptr, 0, &res); 1414 1415 g_assert_cmpint(err, ==, -EINVAL); 1416 g_assert(endptr == str); 1417 } 1418 1419 static void test_qemu_strtoi64_whitespace(void) 1420 { 1421 const char *str = " \t "; 1422 char f = 'X'; 1423 const char *endptr = &f; 1424 int64_t res = 999; 1425 int err; 1426 1427 err = qemu_strtoi64(str, &endptr, 0, &res); 1428 1429 g_assert_cmpint(err, ==, -EINVAL); 1430 g_assert(endptr == str); 1431 } 1432 1433 static void test_qemu_strtoi64_invalid(void) 1434 { 1435 const char *str = " xxxx \t abc"; 1436 char f = 'X'; 1437 const char *endptr = &f; 1438 int64_t res = 999; 1439 int err; 1440 1441 err = qemu_strtoi64(str, &endptr, 0, &res); 1442 1443 g_assert_cmpint(err, ==, -EINVAL); 1444 g_assert(endptr == str); 1445 } 1446 1447 static void test_qemu_strtoi64_trailing(void) 1448 { 1449 const char *str = "123xxx"; 1450 char f = 'X'; 1451 const char *endptr = &f; 1452 int64_t res = 999; 1453 int err; 1454 1455 err = qemu_strtoi64(str, &endptr, 0, &res); 1456 1457 g_assert_cmpint(err, ==, 0); 1458 g_assert_cmpint(res, ==, 123); 1459 g_assert(endptr == str + 3); 1460 } 1461 1462 static void test_qemu_strtoi64_octal(void) 1463 { 1464 const char *str = "0123"; 1465 char f = 'X'; 1466 const char *endptr = &f; 1467 int64_t res = 999; 1468 int err; 1469 1470 err = qemu_strtoi64(str, &endptr, 8, &res); 1471 1472 g_assert_cmpint(err, ==, 0); 1473 g_assert_cmpint(res, ==, 0123); 1474 g_assert(endptr == str + strlen(str)); 1475 1476 endptr = &f; 1477 res = 999; 1478 err = qemu_strtoi64(str, &endptr, 0, &res); 1479 1480 g_assert_cmpint(err, ==, 0); 1481 g_assert_cmpint(res, ==, 0123); 1482 g_assert(endptr == str + strlen(str)); 1483 } 1484 1485 static void test_qemu_strtoi64_decimal(void) 1486 { 1487 const char *str = "0123"; 1488 char f = 'X'; 1489 const char *endptr = &f; 1490 int64_t res = 999; 1491 int err; 1492 1493 err = qemu_strtoi64(str, &endptr, 10, &res); 1494 1495 g_assert_cmpint(err, ==, 0); 1496 g_assert_cmpint(res, ==, 123); 1497 g_assert(endptr == str + strlen(str)); 1498 1499 str = "123"; 1500 endptr = &f; 1501 res = 999; 1502 err = qemu_strtoi64(str, &endptr, 0, &res); 1503 1504 g_assert_cmpint(err, ==, 0); 1505 g_assert_cmpint(res, ==, 123); 1506 g_assert(endptr == str + strlen(str)); 1507 } 1508 1509 static void test_qemu_strtoi64_hex(void) 1510 { 1511 const char *str = "0123"; 1512 char f = 'X'; 1513 const char *endptr = &f; 1514 int64_t res = 999; 1515 int err; 1516 1517 err = qemu_strtoi64(str, &endptr, 16, &res); 1518 1519 g_assert_cmpint(err, ==, 0); 1520 g_assert_cmpint(res, ==, 0x123); 1521 g_assert(endptr == str + strlen(str)); 1522 1523 str = "0x123"; 1524 endptr = &f; 1525 res = 999; 1526 err = qemu_strtoi64(str, &endptr, 0, &res); 1527 1528 g_assert_cmpint(err, ==, 0); 1529 g_assert_cmpint(res, ==, 0x123); 1530 g_assert(endptr == str + strlen(str)); 1531 } 1532 1533 static void test_qemu_strtoi64_max(void) 1534 { 1535 char *str = g_strdup_printf("%lld", LLONG_MAX); 1536 char f = 'X'; 1537 const char *endptr = &f; 1538 int64_t res = 999; 1539 int err; 1540 1541 err = qemu_strtoi64(str, &endptr, 0, &res); 1542 1543 g_assert_cmpint(err, ==, 0); 1544 g_assert_cmpint(res, ==, LLONG_MAX); 1545 g_assert(endptr == str + strlen(str)); 1546 g_free(str); 1547 } 1548 1549 static void test_qemu_strtoi64_overflow(void) 1550 { 1551 const char *str = "99999999999999999999999999999999999999999999"; 1552 char f = 'X'; 1553 const char *endptr = &f; 1554 int64_t res = 999; 1555 int err; 1556 1557 err = qemu_strtoi64(str, &endptr, 0, &res); 1558 1559 g_assert_cmpint(err, ==, -ERANGE); 1560 g_assert_cmpint(res, ==, LLONG_MAX); 1561 g_assert(endptr == str + strlen(str)); 1562 } 1563 1564 static void test_qemu_strtoi64_underflow(void) 1565 { 1566 const char *str = "-99999999999999999999999999999999999999999999"; 1567 char f = 'X'; 1568 const char *endptr = &f; 1569 int64_t res = 999; 1570 int err; 1571 1572 err = qemu_strtoi64(str, &endptr, 0, &res); 1573 1574 g_assert_cmpint(err, ==, -ERANGE); 1575 g_assert_cmpint(res, ==, LLONG_MIN); 1576 g_assert(endptr == str + strlen(str)); 1577 } 1578 1579 static void test_qemu_strtoi64_negative(void) 1580 { 1581 const char *str = " \t -321"; 1582 char f = 'X'; 1583 const char *endptr = &f; 1584 int64_t res = 999; 1585 int err; 1586 1587 err = qemu_strtoi64(str, &endptr, 0, &res); 1588 1589 g_assert_cmpint(err, ==, 0); 1590 g_assert_cmpint(res, ==, -321); 1591 g_assert(endptr == str + strlen(str)); 1592 } 1593 1594 static void test_qemu_strtoi64_full_correct(void) 1595 { 1596 const char *str = "123"; 1597 int64_t res = 999; 1598 int err; 1599 1600 err = qemu_strtoi64(str, NULL, 0, &res); 1601 1602 g_assert_cmpint(err, ==, 0); 1603 g_assert_cmpint(res, ==, 123); 1604 } 1605 1606 static void test_qemu_strtoi64_full_null(void) 1607 { 1608 int64_t res = 999; 1609 int err; 1610 1611 err = qemu_strtoi64(NULL, NULL, 0, &res); 1612 1613 g_assert_cmpint(err, ==, -EINVAL); 1614 } 1615 1616 static void test_qemu_strtoi64_full_empty(void) 1617 { 1618 const char *str = ""; 1619 int64_t res = 999; 1620 int err; 1621 1622 err = qemu_strtoi64(str, NULL, 0, &res); 1623 1624 g_assert_cmpint(err, ==, -EINVAL); 1625 } 1626 1627 static void test_qemu_strtoi64_full_negative(void) 1628 { 1629 const char *str = " \t -321"; 1630 int64_t res = 999; 1631 int err; 1632 1633 err = qemu_strtoi64(str, NULL, 0, &res); 1634 1635 g_assert_cmpint(err, ==, 0); 1636 g_assert_cmpint(res, ==, -321); 1637 } 1638 1639 static void test_qemu_strtoi64_full_trailing(void) 1640 { 1641 const char *str = "123xxx"; 1642 int64_t res = 999; 1643 int err; 1644 1645 err = qemu_strtoi64(str, NULL, 0, &res); 1646 1647 g_assert_cmpint(err, ==, -EINVAL); 1648 } 1649 1650 static void test_qemu_strtoi64_full_max(void) 1651 { 1652 1653 char *str = g_strdup_printf("%lld", LLONG_MAX); 1654 int64_t res; 1655 int err; 1656 1657 err = qemu_strtoi64(str, NULL, 0, &res); 1658 1659 g_assert_cmpint(err, ==, 0); 1660 g_assert_cmpint(res, ==, LLONG_MAX); 1661 g_free(str); 1662 } 1663 1664 static void test_qemu_strtou64_correct(void) 1665 { 1666 const char *str = "12345 foo"; 1667 char f = 'X'; 1668 const char *endptr = &f; 1669 uint64_t res = 999; 1670 int err; 1671 1672 err = qemu_strtou64(str, &endptr, 0, &res); 1673 1674 g_assert_cmpint(err, ==, 0); 1675 g_assert_cmpuint(res, ==, 12345); 1676 g_assert(endptr == str + 5); 1677 } 1678 1679 static void test_qemu_strtou64_null(void) 1680 { 1681 char f = 'X'; 1682 const char *endptr = &f; 1683 uint64_t res = 999; 1684 int err; 1685 1686 err = qemu_strtou64(NULL, &endptr, 0, &res); 1687 1688 g_assert_cmpint(err, ==, -EINVAL); 1689 g_assert(endptr == NULL); 1690 } 1691 1692 static void test_qemu_strtou64_empty(void) 1693 { 1694 const char *str = ""; 1695 char f = 'X'; 1696 const char *endptr = &f; 1697 uint64_t res = 999; 1698 int err; 1699 1700 err = qemu_strtou64(str, &endptr, 0, &res); 1701 1702 g_assert_cmpint(err, ==, -EINVAL); 1703 g_assert(endptr == str); 1704 } 1705 1706 static void test_qemu_strtou64_whitespace(void) 1707 { 1708 const char *str = " \t "; 1709 char f = 'X'; 1710 const char *endptr = &f; 1711 uint64_t res = 999; 1712 int err; 1713 1714 err = qemu_strtou64(str, &endptr, 0, &res); 1715 1716 g_assert_cmpint(err, ==, -EINVAL); 1717 g_assert(endptr == str); 1718 } 1719 1720 static void test_qemu_strtou64_invalid(void) 1721 { 1722 const char *str = " xxxx \t abc"; 1723 char f = 'X'; 1724 const char *endptr = &f; 1725 uint64_t res = 999; 1726 int err; 1727 1728 err = qemu_strtou64(str, &endptr, 0, &res); 1729 1730 g_assert_cmpint(err, ==, -EINVAL); 1731 g_assert(endptr == str); 1732 } 1733 1734 static void test_qemu_strtou64_trailing(void) 1735 { 1736 const char *str = "123xxx"; 1737 char f = 'X'; 1738 const char *endptr = &f; 1739 uint64_t res = 999; 1740 int err; 1741 1742 err = qemu_strtou64(str, &endptr, 0, &res); 1743 1744 g_assert_cmpint(err, ==, 0); 1745 g_assert_cmpuint(res, ==, 123); 1746 g_assert(endptr == str + 3); 1747 } 1748 1749 static void test_qemu_strtou64_octal(void) 1750 { 1751 const char *str = "0123"; 1752 char f = 'X'; 1753 const char *endptr = &f; 1754 uint64_t res = 999; 1755 int err; 1756 1757 err = qemu_strtou64(str, &endptr, 8, &res); 1758 1759 g_assert_cmpint(err, ==, 0); 1760 g_assert_cmpuint(res, ==, 0123); 1761 g_assert(endptr == str + strlen(str)); 1762 1763 endptr = &f; 1764 res = 999; 1765 err = qemu_strtou64(str, &endptr, 0, &res); 1766 1767 g_assert_cmpint(err, ==, 0); 1768 g_assert_cmpuint(res, ==, 0123); 1769 g_assert(endptr == str + strlen(str)); 1770 } 1771 1772 static void test_qemu_strtou64_decimal(void) 1773 { 1774 const char *str = "0123"; 1775 char f = 'X'; 1776 const char *endptr = &f; 1777 uint64_t res = 999; 1778 int err; 1779 1780 err = qemu_strtou64(str, &endptr, 10, &res); 1781 1782 g_assert_cmpint(err, ==, 0); 1783 g_assert_cmpuint(res, ==, 123); 1784 g_assert(endptr == str + strlen(str)); 1785 1786 str = "123"; 1787 endptr = &f; 1788 res = 999; 1789 err = qemu_strtou64(str, &endptr, 0, &res); 1790 1791 g_assert_cmpint(err, ==, 0); 1792 g_assert_cmpuint(res, ==, 123); 1793 g_assert(endptr == str + strlen(str)); 1794 } 1795 1796 static void test_qemu_strtou64_hex(void) 1797 { 1798 const char *str = "0123"; 1799 char f = 'X'; 1800 const char *endptr = &f; 1801 uint64_t res = 999; 1802 int err; 1803 1804 err = qemu_strtou64(str, &endptr, 16, &res); 1805 1806 g_assert_cmpint(err, ==, 0); 1807 g_assert_cmphex(res, ==, 0x123); 1808 g_assert(endptr == str + strlen(str)); 1809 1810 str = "0x123"; 1811 endptr = &f; 1812 res = 999; 1813 err = qemu_strtou64(str, &endptr, 0, &res); 1814 1815 g_assert_cmpint(err, ==, 0); 1816 g_assert_cmphex(res, ==, 0x123); 1817 g_assert(endptr == str + strlen(str)); 1818 } 1819 1820 static void test_qemu_strtou64_max(void) 1821 { 1822 char *str = g_strdup_printf("%llu", ULLONG_MAX); 1823 char f = 'X'; 1824 const char *endptr = &f; 1825 uint64_t res = 999; 1826 int err; 1827 1828 err = qemu_strtou64(str, &endptr, 0, &res); 1829 1830 g_assert_cmpint(err, ==, 0); 1831 g_assert_cmphex(res, ==, ULLONG_MAX); 1832 g_assert(endptr == str + strlen(str)); 1833 g_free(str); 1834 } 1835 1836 static void test_qemu_strtou64_overflow(void) 1837 { 1838 const char *str = "99999999999999999999999999999999999999999999"; 1839 char f = 'X'; 1840 const char *endptr = &f; 1841 uint64_t res = 999; 1842 int err; 1843 1844 err = qemu_strtou64(str, &endptr, 0, &res); 1845 1846 g_assert_cmpint(err, ==, -ERANGE); 1847 g_assert_cmphex(res, ==, ULLONG_MAX); 1848 g_assert(endptr == str + strlen(str)); 1849 } 1850 1851 static void test_qemu_strtou64_underflow(void) 1852 { 1853 const char *str = "-99999999999999999999999999999999999999999999"; 1854 char f = 'X'; 1855 const char *endptr = &f; 1856 uint64_t res = 999; 1857 int err; 1858 1859 err = qemu_strtou64(str, &endptr, 0, &res); 1860 1861 g_assert_cmpint(err, ==, -ERANGE); 1862 g_assert_cmphex(res, ==, -1ull); 1863 g_assert(endptr == str + strlen(str)); 1864 } 1865 1866 static void test_qemu_strtou64_negative(void) 1867 { 1868 const char *str = " \t -321"; 1869 char f = 'X'; 1870 const char *endptr = &f; 1871 uint64_t res = 999; 1872 int err; 1873 1874 err = qemu_strtou64(str, &endptr, 0, &res); 1875 1876 g_assert_cmpint(err, ==, 0); 1877 g_assert_cmpuint(res, ==, -321ull); 1878 g_assert(endptr == str + strlen(str)); 1879 } 1880 1881 static void test_qemu_strtou64_full_correct(void) 1882 { 1883 const char *str = "18446744073709551614"; 1884 uint64_t res = 999; 1885 int err; 1886 1887 err = qemu_strtou64(str, NULL, 0, &res); 1888 1889 g_assert_cmpint(err, ==, 0); 1890 g_assert_cmpuint(res, ==, 18446744073709551614ull); 1891 } 1892 1893 static void test_qemu_strtou64_full_null(void) 1894 { 1895 uint64_t res = 999; 1896 int err; 1897 1898 err = qemu_strtou64(NULL, NULL, 0, &res); 1899 1900 g_assert_cmpint(err, ==, -EINVAL); 1901 } 1902 1903 static void test_qemu_strtou64_full_empty(void) 1904 { 1905 const char *str = ""; 1906 uint64_t res = 999; 1907 int err; 1908 1909 err = qemu_strtou64(str, NULL, 0, &res); 1910 1911 g_assert_cmpint(err, ==, -EINVAL); 1912 } 1913 1914 static void test_qemu_strtou64_full_negative(void) 1915 { 1916 const char *str = " \t -321"; 1917 uint64_t res = 999; 1918 int err; 1919 1920 err = qemu_strtou64(str, NULL, 0, &res); 1921 1922 g_assert_cmpint(err, ==, 0); 1923 g_assert_cmpuint(res, ==, -321ull); 1924 } 1925 1926 static void test_qemu_strtou64_full_trailing(void) 1927 { 1928 const char *str = "18446744073709551614xxxxxx"; 1929 uint64_t res = 999; 1930 int err; 1931 1932 err = qemu_strtou64(str, NULL, 0, &res); 1933 1934 g_assert_cmpint(err, ==, -EINVAL); 1935 } 1936 1937 static void test_qemu_strtou64_full_max(void) 1938 { 1939 char *str = g_strdup_printf("%lld", ULLONG_MAX); 1940 uint64_t res = 999; 1941 int err; 1942 1943 err = qemu_strtou64(str, NULL, 0, &res); 1944 1945 g_assert_cmpint(err, ==, 0); 1946 g_assert_cmphex(res, ==, ULLONG_MAX); 1947 g_free(str); 1948 } 1949 1950 static void test_qemu_strtosz_simple(void) 1951 { 1952 const char *str; 1953 const char *endptr; 1954 int err; 1955 uint64_t res = 0xbaadf00d; 1956 1957 str = "0"; 1958 err = qemu_strtosz(str, &endptr, &res); 1959 g_assert_cmpint(err, ==, 0); 1960 g_assert_cmpint(res, ==, 0); 1961 g_assert(endptr == str + 1); 1962 1963 /* Leading 0 gives decimal results, not octal */ 1964 str = "08"; 1965 err = qemu_strtosz(str, &endptr, &res); 1966 g_assert_cmpint(err, ==, 0); 1967 g_assert_cmpint(res, ==, 8); 1968 g_assert(endptr == str + 2); 1969 1970 /* Leading space is ignored */ 1971 str = " 12345"; 1972 err = qemu_strtosz(str, &endptr, &res); 1973 g_assert_cmpint(err, ==, 0); 1974 g_assert_cmpint(res, ==, 12345); 1975 g_assert(endptr == str + 6); 1976 1977 err = qemu_strtosz(str, NULL, &res); 1978 g_assert_cmpint(err, ==, 0); 1979 g_assert_cmpint(res, ==, 12345); 1980 1981 str = "9007199254740991"; /* 2^53-1 */ 1982 err = qemu_strtosz(str, &endptr, &res); 1983 g_assert_cmpint(err, ==, 0); 1984 g_assert_cmpint(res, ==, 0x1fffffffffffff); 1985 g_assert(endptr == str + 16); 1986 1987 str = "9007199254740992"; /* 2^53 */ 1988 err = qemu_strtosz(str, &endptr, &res); 1989 g_assert_cmpint(err, ==, 0); 1990 g_assert_cmpint(res, ==, 0x20000000000000); 1991 g_assert(endptr == str + 16); 1992 1993 str = "9007199254740993"; /* 2^53+1 */ 1994 err = qemu_strtosz(str, &endptr, &res); 1995 g_assert_cmpint(err, ==, 0); 1996 g_assert_cmpint(res, ==, 0x20000000000001); 1997 g_assert(endptr == str + 16); 1998 1999 str = "18446744073709549568"; /* 0xfffffffffffff800 (53 msbs set) */ 2000 err = qemu_strtosz(str, &endptr, &res); 2001 g_assert_cmpint(err, ==, 0); 2002 g_assert_cmpint(res, ==, 0xfffffffffffff800); 2003 g_assert(endptr == str + 20); 2004 2005 str = "18446744073709550591"; /* 0xfffffffffffffbff */ 2006 err = qemu_strtosz(str, &endptr, &res); 2007 g_assert_cmpint(err, ==, 0); 2008 g_assert_cmpint(res, ==, 0xfffffffffffffbff); 2009 g_assert(endptr == str + 20); 2010 2011 str = "18446744073709551615"; /* 0xffffffffffffffff */ 2012 err = qemu_strtosz(str, &endptr, &res); 2013 g_assert_cmpint(err, ==, 0); 2014 g_assert_cmpint(res, ==, 0xffffffffffffffff); 2015 g_assert(endptr == str + 20); 2016 } 2017 2018 static void test_qemu_strtosz_hex(void) 2019 { 2020 const char *str; 2021 const char *endptr; 2022 int err; 2023 uint64_t res = 0xbaadf00d; 2024 2025 str = "0x0"; 2026 err = qemu_strtosz(str, &endptr, &res); 2027 g_assert_cmpint(err, ==, 0); 2028 g_assert_cmpint(res, ==, 0); 2029 g_assert(endptr == str + 3); 2030 2031 str = "0xab"; 2032 err = qemu_strtosz(str, &endptr, &res); 2033 g_assert_cmpint(err, ==, 0); 2034 g_assert_cmpint(res, ==, 171); 2035 g_assert(endptr == str + 4); 2036 2037 str = "0xae"; 2038 err = qemu_strtosz(str, &endptr, &res); 2039 g_assert_cmpint(err, ==, 0); 2040 g_assert_cmpint(res, ==, 174); 2041 g_assert(endptr == str + 4); 2042 } 2043 2044 static void test_qemu_strtosz_units(void) 2045 { 2046 const char *none = "1"; 2047 const char *b = "1B"; 2048 const char *k = "1K"; 2049 const char *m = "1M"; 2050 const char *g = "1G"; 2051 const char *t = "1T"; 2052 const char *p = "1P"; 2053 const char *e = "1E"; 2054 int err; 2055 const char *endptr; 2056 uint64_t res = 0xbaadf00d; 2057 2058 /* default is M */ 2059 err = qemu_strtosz_MiB(none, &endptr, &res); 2060 g_assert_cmpint(err, ==, 0); 2061 g_assert_cmpint(res, ==, MiB); 2062 g_assert(endptr == none + 1); 2063 2064 err = qemu_strtosz(b, &endptr, &res); 2065 g_assert_cmpint(err, ==, 0); 2066 g_assert_cmpint(res, ==, 1); 2067 g_assert(endptr == b + 2); 2068 2069 err = qemu_strtosz(k, &endptr, &res); 2070 g_assert_cmpint(err, ==, 0); 2071 g_assert_cmpint(res, ==, KiB); 2072 g_assert(endptr == k + 2); 2073 2074 err = qemu_strtosz(m, &endptr, &res); 2075 g_assert_cmpint(err, ==, 0); 2076 g_assert_cmpint(res, ==, MiB); 2077 g_assert(endptr == m + 2); 2078 2079 err = qemu_strtosz(g, &endptr, &res); 2080 g_assert_cmpint(err, ==, 0); 2081 g_assert_cmpint(res, ==, GiB); 2082 g_assert(endptr == g + 2); 2083 2084 err = qemu_strtosz(t, &endptr, &res); 2085 g_assert_cmpint(err, ==, 0); 2086 g_assert_cmpint(res, ==, TiB); 2087 g_assert(endptr == t + 2); 2088 2089 err = qemu_strtosz(p, &endptr, &res); 2090 g_assert_cmpint(err, ==, 0); 2091 g_assert_cmpint(res, ==, PiB); 2092 g_assert(endptr == p + 2); 2093 2094 err = qemu_strtosz(e, &endptr, &res); 2095 g_assert_cmpint(err, ==, 0); 2096 g_assert_cmpint(res, ==, EiB); 2097 g_assert(endptr == e + 2); 2098 } 2099 2100 static void test_qemu_strtosz_float(void) 2101 { 2102 const char *str; 2103 int err; 2104 const char *endptr; 2105 uint64_t res = 0xbaadf00d; 2106 2107 str = "0.5E"; 2108 err = qemu_strtosz(str, &endptr, &res); 2109 g_assert_cmpint(err, ==, 0); 2110 g_assert_cmpint(res, ==, EiB / 2); 2111 g_assert(endptr == str + 4); 2112 2113 /* For convenience, a fraction of 0 is tolerated even on bytes */ 2114 str = "1.0B"; 2115 err = qemu_strtosz(str, &endptr, &res); 2116 g_assert_cmpint(err, ==, 0); 2117 g_assert_cmpint(res, ==, 1); 2118 g_assert(endptr == str + 4); 2119 2120 /* An empty fraction is tolerated */ 2121 str = "1.k"; 2122 err = qemu_strtosz(str, &endptr, &res); 2123 g_assert_cmpint(err, ==, 0); 2124 g_assert_cmpint(res, ==, 1024); 2125 g_assert(endptr == str + 3); 2126 2127 /* For convenience, we permit values that are not byte-exact */ 2128 str = "12.345M"; 2129 err = qemu_strtosz(str, &endptr, &res); 2130 g_assert_cmpint(err, ==, 0); 2131 g_assert_cmpint(res, ==, (uint64_t) (12.345 * MiB + 0.5)); 2132 g_assert(endptr == str + 7); 2133 } 2134 2135 static void test_qemu_strtosz_invalid(void) 2136 { 2137 const char *str; 2138 const char *endptr; 2139 int err; 2140 uint64_t res = 0xbaadf00d; 2141 2142 str = ""; 2143 err = qemu_strtosz(str, &endptr, &res); 2144 g_assert_cmpint(err, ==, -EINVAL); 2145 g_assert(endptr == str); 2146 2147 str = " \t "; 2148 err = qemu_strtosz(str, &endptr, &res); 2149 g_assert_cmpint(err, ==, -EINVAL); 2150 g_assert(endptr == str); 2151 2152 str = "crap"; 2153 err = qemu_strtosz(str, &endptr, &res); 2154 g_assert_cmpint(err, ==, -EINVAL); 2155 g_assert(endptr == str); 2156 2157 str = "inf"; 2158 err = qemu_strtosz(str, &endptr, &res); 2159 g_assert_cmpint(err, ==, -EINVAL); 2160 g_assert(endptr == str); 2161 2162 str = "NaN"; 2163 err = qemu_strtosz(str, &endptr, &res); 2164 g_assert_cmpint(err, ==, -EINVAL); 2165 g_assert(endptr == str); 2166 2167 /* Fractional values require scale larger than bytes */ 2168 str = "1.1B"; 2169 err = qemu_strtosz(str, &endptr, &res); 2170 g_assert_cmpint(err, ==, -EINVAL); 2171 g_assert(endptr == str); 2172 2173 str = "1.1"; 2174 err = qemu_strtosz(str, &endptr, &res); 2175 g_assert_cmpint(err, ==, -EINVAL); 2176 g_assert(endptr == str); 2177 2178 /* No floating point exponents */ 2179 str = "1.5e1k"; 2180 err = qemu_strtosz(str, &endptr, &res); 2181 g_assert_cmpint(err, ==, -EINVAL); 2182 g_assert(endptr == str); 2183 2184 str = "1.5E+0k"; 2185 err = qemu_strtosz(str, &endptr, &res); 2186 g_assert_cmpint(err, ==, -EINVAL); 2187 g_assert(endptr == str); 2188 2189 /* No hex fractions */ 2190 str = "0x1.8k"; 2191 err = qemu_strtosz(str, &endptr, &res); 2192 g_assert_cmpint(err, ==, -EINVAL); 2193 g_assert(endptr == str); 2194 2195 /* No negative values */ 2196 str = "-0"; 2197 err = qemu_strtosz(str, &endptr, &res); 2198 g_assert_cmpint(err, ==, -EINVAL); 2199 g_assert(endptr == str); 2200 2201 str = "-1"; 2202 err = qemu_strtosz(str, &endptr, &res); 2203 g_assert_cmpint(err, ==, -EINVAL); 2204 g_assert(endptr == str); 2205 } 2206 2207 static void test_qemu_strtosz_trailing(void) 2208 { 2209 const char *str; 2210 const char *endptr; 2211 int err; 2212 uint64_t res = 0xbaadf00d; 2213 2214 str = "123xxx"; 2215 err = qemu_strtosz_MiB(str, &endptr, &res); 2216 g_assert_cmpint(res, ==, 123 * MiB); 2217 g_assert(endptr == str + 3); 2218 2219 err = qemu_strtosz(str, NULL, &res); 2220 g_assert_cmpint(err, ==, -EINVAL); 2221 2222 str = "1kiB"; 2223 err = qemu_strtosz(str, &endptr, &res); 2224 g_assert_cmpint(err, ==, 0); 2225 g_assert_cmpint(res, ==, 1024); 2226 g_assert(endptr == str + 2); 2227 2228 err = qemu_strtosz(str, NULL, &res); 2229 g_assert_cmpint(err, ==, -EINVAL); 2230 2231 str = "0x"; 2232 err = qemu_strtosz(str, &endptr, &res); 2233 g_assert_cmpint(res, ==, 0); 2234 g_assert(endptr == str + 1); 2235 2236 err = qemu_strtosz(str, NULL, &res); 2237 g_assert_cmpint(err, ==, -EINVAL); 2238 2239 str = "0.NaN"; 2240 err = qemu_strtosz(str, &endptr, &res); 2241 g_assert_cmpint(err, ==, 0); 2242 g_assert(endptr == str + 2); 2243 2244 err = qemu_strtosz(str, NULL, &res); 2245 g_assert_cmpint(err, ==, -EINVAL); 2246 2247 str = "123-45"; 2248 err = qemu_strtosz(str, &endptr, &res); 2249 g_assert_cmpint(res, ==, 123); 2250 g_assert(endptr == str + 3); 2251 2252 err = qemu_strtosz(str, NULL, &res); 2253 g_assert_cmpint(err, ==, -EINVAL); 2254 } 2255 2256 static void test_qemu_strtosz_erange(void) 2257 { 2258 const char *str; 2259 const char *endptr; 2260 int err; 2261 uint64_t res = 0xbaadf00d; 2262 2263 str = "18446744073709551616"; /* 2^64; see strtosz_simple for 2^64-1 */ 2264 err = qemu_strtosz(str, &endptr, &res); 2265 g_assert_cmpint(err, ==, -ERANGE); 2266 g_assert(endptr == str + 20); 2267 2268 str = "20E"; 2269 err = qemu_strtosz(str, &endptr, &res); 2270 g_assert_cmpint(err, ==, -ERANGE); 2271 g_assert(endptr == str + 3); 2272 } 2273 2274 static void test_qemu_strtosz_metric(void) 2275 { 2276 const char *str; 2277 int err; 2278 const char *endptr; 2279 uint64_t res = 0xbaadf00d; 2280 2281 str = "12345k"; 2282 err = qemu_strtosz_metric(str, &endptr, &res); 2283 g_assert_cmpint(err, ==, 0); 2284 g_assert_cmpint(res, ==, 12345000); 2285 g_assert(endptr == str + 6); 2286 2287 str = "12.345M"; 2288 err = qemu_strtosz_metric(str, &endptr, &res); 2289 g_assert_cmpint(err, ==, 0); 2290 g_assert_cmpint(res, ==, 12345000); 2291 g_assert(endptr == str + 7); 2292 } 2293 2294 int main(int argc, char **argv) 2295 { 2296 g_test_init(&argc, &argv, NULL); 2297 2298 g_test_add_func("/cutils/parse_uint/null", test_parse_uint_null); 2299 g_test_add_func("/cutils/parse_uint/empty", test_parse_uint_empty); 2300 g_test_add_func("/cutils/parse_uint/whitespace", 2301 test_parse_uint_whitespace); 2302 g_test_add_func("/cutils/parse_uint/invalid", test_parse_uint_invalid); 2303 g_test_add_func("/cutils/parse_uint/trailing", test_parse_uint_trailing); 2304 g_test_add_func("/cutils/parse_uint/correct", test_parse_uint_correct); 2305 g_test_add_func("/cutils/parse_uint/octal", test_parse_uint_octal); 2306 g_test_add_func("/cutils/parse_uint/decimal", test_parse_uint_decimal); 2307 g_test_add_func("/cutils/parse_uint/llong_max", test_parse_uint_llong_max); 2308 g_test_add_func("/cutils/parse_uint/overflow", test_parse_uint_overflow); 2309 g_test_add_func("/cutils/parse_uint/negative", test_parse_uint_negative); 2310 g_test_add_func("/cutils/parse_uint_full/trailing", 2311 test_parse_uint_full_trailing); 2312 g_test_add_func("/cutils/parse_uint_full/correct", 2313 test_parse_uint_full_correct); 2314 2315 /* qemu_strtoi() tests */ 2316 g_test_add_func("/cutils/qemu_strtoi/correct", 2317 test_qemu_strtoi_correct); 2318 g_test_add_func("/cutils/qemu_strtoi/null", 2319 test_qemu_strtoi_null); 2320 g_test_add_func("/cutils/qemu_strtoi/empty", 2321 test_qemu_strtoi_empty); 2322 g_test_add_func("/cutils/qemu_strtoi/whitespace", 2323 test_qemu_strtoi_whitespace); 2324 g_test_add_func("/cutils/qemu_strtoi/invalid", 2325 test_qemu_strtoi_invalid); 2326 g_test_add_func("/cutils/qemu_strtoi/trailing", 2327 test_qemu_strtoi_trailing); 2328 g_test_add_func("/cutils/qemu_strtoi/octal", 2329 test_qemu_strtoi_octal); 2330 g_test_add_func("/cutils/qemu_strtoi/decimal", 2331 test_qemu_strtoi_decimal); 2332 g_test_add_func("/cutils/qemu_strtoi/hex", 2333 test_qemu_strtoi_hex); 2334 g_test_add_func("/cutils/qemu_strtoi/max", 2335 test_qemu_strtoi_max); 2336 g_test_add_func("/cutils/qemu_strtoi/overflow", 2337 test_qemu_strtoi_overflow); 2338 g_test_add_func("/cutils/qemu_strtoi/underflow", 2339 test_qemu_strtoi_underflow); 2340 g_test_add_func("/cutils/qemu_strtoi/negative", 2341 test_qemu_strtoi_negative); 2342 g_test_add_func("/cutils/qemu_strtoi_full/correct", 2343 test_qemu_strtoi_full_correct); 2344 g_test_add_func("/cutils/qemu_strtoi_full/null", 2345 test_qemu_strtoi_full_null); 2346 g_test_add_func("/cutils/qemu_strtoi_full/empty", 2347 test_qemu_strtoi_full_empty); 2348 g_test_add_func("/cutils/qemu_strtoi_full/negative", 2349 test_qemu_strtoi_full_negative); 2350 g_test_add_func("/cutils/qemu_strtoi_full/trailing", 2351 test_qemu_strtoi_full_trailing); 2352 g_test_add_func("/cutils/qemu_strtoi_full/max", 2353 test_qemu_strtoi_full_max); 2354 2355 /* qemu_strtoui() tests */ 2356 g_test_add_func("/cutils/qemu_strtoui/correct", 2357 test_qemu_strtoui_correct); 2358 g_test_add_func("/cutils/qemu_strtoui/null", 2359 test_qemu_strtoui_null); 2360 g_test_add_func("/cutils/qemu_strtoui/empty", 2361 test_qemu_strtoui_empty); 2362 g_test_add_func("/cutils/qemu_strtoui/whitespace", 2363 test_qemu_strtoui_whitespace); 2364 g_test_add_func("/cutils/qemu_strtoui/invalid", 2365 test_qemu_strtoui_invalid); 2366 g_test_add_func("/cutils/qemu_strtoui/trailing", 2367 test_qemu_strtoui_trailing); 2368 g_test_add_func("/cutils/qemu_strtoui/octal", 2369 test_qemu_strtoui_octal); 2370 g_test_add_func("/cutils/qemu_strtoui/decimal", 2371 test_qemu_strtoui_decimal); 2372 g_test_add_func("/cutils/qemu_strtoui/hex", 2373 test_qemu_strtoui_hex); 2374 g_test_add_func("/cutils/qemu_strtoui/max", 2375 test_qemu_strtoui_max); 2376 g_test_add_func("/cutils/qemu_strtoui/overflow", 2377 test_qemu_strtoui_overflow); 2378 g_test_add_func("/cutils/qemu_strtoui/underflow", 2379 test_qemu_strtoui_underflow); 2380 g_test_add_func("/cutils/qemu_strtoui/negative", 2381 test_qemu_strtoui_negative); 2382 g_test_add_func("/cutils/qemu_strtoui_full/correct", 2383 test_qemu_strtoui_full_correct); 2384 g_test_add_func("/cutils/qemu_strtoui_full/null", 2385 test_qemu_strtoui_full_null); 2386 g_test_add_func("/cutils/qemu_strtoui_full/empty", 2387 test_qemu_strtoui_full_empty); 2388 g_test_add_func("/cutils/qemu_strtoui_full/negative", 2389 test_qemu_strtoui_full_negative); 2390 g_test_add_func("/cutils/qemu_strtoui_full/trailing", 2391 test_qemu_strtoui_full_trailing); 2392 g_test_add_func("/cutils/qemu_strtoui_full/max", 2393 test_qemu_strtoui_full_max); 2394 2395 /* qemu_strtol() tests */ 2396 g_test_add_func("/cutils/qemu_strtol/correct", 2397 test_qemu_strtol_correct); 2398 g_test_add_func("/cutils/qemu_strtol/null", 2399 test_qemu_strtol_null); 2400 g_test_add_func("/cutils/qemu_strtol/empty", 2401 test_qemu_strtol_empty); 2402 g_test_add_func("/cutils/qemu_strtol/whitespace", 2403 test_qemu_strtol_whitespace); 2404 g_test_add_func("/cutils/qemu_strtol/invalid", 2405 test_qemu_strtol_invalid); 2406 g_test_add_func("/cutils/qemu_strtol/trailing", 2407 test_qemu_strtol_trailing); 2408 g_test_add_func("/cutils/qemu_strtol/octal", 2409 test_qemu_strtol_octal); 2410 g_test_add_func("/cutils/qemu_strtol/decimal", 2411 test_qemu_strtol_decimal); 2412 g_test_add_func("/cutils/qemu_strtol/hex", 2413 test_qemu_strtol_hex); 2414 g_test_add_func("/cutils/qemu_strtol/max", 2415 test_qemu_strtol_max); 2416 g_test_add_func("/cutils/qemu_strtol/overflow", 2417 test_qemu_strtol_overflow); 2418 g_test_add_func("/cutils/qemu_strtol/underflow", 2419 test_qemu_strtol_underflow); 2420 g_test_add_func("/cutils/qemu_strtol/negative", 2421 test_qemu_strtol_negative); 2422 g_test_add_func("/cutils/qemu_strtol_full/correct", 2423 test_qemu_strtol_full_correct); 2424 g_test_add_func("/cutils/qemu_strtol_full/null", 2425 test_qemu_strtol_full_null); 2426 g_test_add_func("/cutils/qemu_strtol_full/empty", 2427 test_qemu_strtol_full_empty); 2428 g_test_add_func("/cutils/qemu_strtol_full/negative", 2429 test_qemu_strtol_full_negative); 2430 g_test_add_func("/cutils/qemu_strtol_full/trailing", 2431 test_qemu_strtol_full_trailing); 2432 g_test_add_func("/cutils/qemu_strtol_full/max", 2433 test_qemu_strtol_full_max); 2434 2435 /* qemu_strtoul() tests */ 2436 g_test_add_func("/cutils/qemu_strtoul/correct", 2437 test_qemu_strtoul_correct); 2438 g_test_add_func("/cutils/qemu_strtoul/null", 2439 test_qemu_strtoul_null); 2440 g_test_add_func("/cutils/qemu_strtoul/empty", 2441 test_qemu_strtoul_empty); 2442 g_test_add_func("/cutils/qemu_strtoul/whitespace", 2443 test_qemu_strtoul_whitespace); 2444 g_test_add_func("/cutils/qemu_strtoul/invalid", 2445 test_qemu_strtoul_invalid); 2446 g_test_add_func("/cutils/qemu_strtoul/trailing", 2447 test_qemu_strtoul_trailing); 2448 g_test_add_func("/cutils/qemu_strtoul/octal", 2449 test_qemu_strtoul_octal); 2450 g_test_add_func("/cutils/qemu_strtoul/decimal", 2451 test_qemu_strtoul_decimal); 2452 g_test_add_func("/cutils/qemu_strtoul/hex", 2453 test_qemu_strtoul_hex); 2454 g_test_add_func("/cutils/qemu_strtoul/max", 2455 test_qemu_strtoul_max); 2456 g_test_add_func("/cutils/qemu_strtoul/overflow", 2457 test_qemu_strtoul_overflow); 2458 g_test_add_func("/cutils/qemu_strtoul/underflow", 2459 test_qemu_strtoul_underflow); 2460 g_test_add_func("/cutils/qemu_strtoul/negative", 2461 test_qemu_strtoul_negative); 2462 g_test_add_func("/cutils/qemu_strtoul_full/correct", 2463 test_qemu_strtoul_full_correct); 2464 g_test_add_func("/cutils/qemu_strtoul_full/null", 2465 test_qemu_strtoul_full_null); 2466 g_test_add_func("/cutils/qemu_strtoul_full/empty", 2467 test_qemu_strtoul_full_empty); 2468 g_test_add_func("/cutils/qemu_strtoul_full/negative", 2469 test_qemu_strtoul_full_negative); 2470 g_test_add_func("/cutils/qemu_strtoul_full/trailing", 2471 test_qemu_strtoul_full_trailing); 2472 g_test_add_func("/cutils/qemu_strtoul_full/max", 2473 test_qemu_strtoul_full_max); 2474 2475 /* qemu_strtoi64() tests */ 2476 g_test_add_func("/cutils/qemu_strtoi64/correct", 2477 test_qemu_strtoi64_correct); 2478 g_test_add_func("/cutils/qemu_strtoi64/null", 2479 test_qemu_strtoi64_null); 2480 g_test_add_func("/cutils/qemu_strtoi64/empty", 2481 test_qemu_strtoi64_empty); 2482 g_test_add_func("/cutils/qemu_strtoi64/whitespace", 2483 test_qemu_strtoi64_whitespace); 2484 g_test_add_func("/cutils/qemu_strtoi64/invalid" 2485 , 2486 test_qemu_strtoi64_invalid); 2487 g_test_add_func("/cutils/qemu_strtoi64/trailing", 2488 test_qemu_strtoi64_trailing); 2489 g_test_add_func("/cutils/qemu_strtoi64/octal", 2490 test_qemu_strtoi64_octal); 2491 g_test_add_func("/cutils/qemu_strtoi64/decimal", 2492 test_qemu_strtoi64_decimal); 2493 g_test_add_func("/cutils/qemu_strtoi64/hex", 2494 test_qemu_strtoi64_hex); 2495 g_test_add_func("/cutils/qemu_strtoi64/max", 2496 test_qemu_strtoi64_max); 2497 g_test_add_func("/cutils/qemu_strtoi64/overflow", 2498 test_qemu_strtoi64_overflow); 2499 g_test_add_func("/cutils/qemu_strtoi64/underflow", 2500 test_qemu_strtoi64_underflow); 2501 g_test_add_func("/cutils/qemu_strtoi64/negative", 2502 test_qemu_strtoi64_negative); 2503 g_test_add_func("/cutils/qemu_strtoi64_full/correct", 2504 test_qemu_strtoi64_full_correct); 2505 g_test_add_func("/cutils/qemu_strtoi64_full/null", 2506 test_qemu_strtoi64_full_null); 2507 g_test_add_func("/cutils/qemu_strtoi64_full/empty", 2508 test_qemu_strtoi64_full_empty); 2509 g_test_add_func("/cutils/qemu_strtoi64_full/negative", 2510 test_qemu_strtoi64_full_negative); 2511 g_test_add_func("/cutils/qemu_strtoi64_full/trailing", 2512 test_qemu_strtoi64_full_trailing); 2513 g_test_add_func("/cutils/qemu_strtoi64_full/max", 2514 test_qemu_strtoi64_full_max); 2515 2516 /* qemu_strtou64() tests */ 2517 g_test_add_func("/cutils/qemu_strtou64/correct", 2518 test_qemu_strtou64_correct); 2519 g_test_add_func("/cutils/qemu_strtou64/null", 2520 test_qemu_strtou64_null); 2521 g_test_add_func("/cutils/qemu_strtou64/empty", 2522 test_qemu_strtou64_empty); 2523 g_test_add_func("/cutils/qemu_strtou64/whitespace", 2524 test_qemu_strtou64_whitespace); 2525 g_test_add_func("/cutils/qemu_strtou64/invalid", 2526 test_qemu_strtou64_invalid); 2527 g_test_add_func("/cutils/qemu_strtou64/trailing", 2528 test_qemu_strtou64_trailing); 2529 g_test_add_func("/cutils/qemu_strtou64/octal", 2530 test_qemu_strtou64_octal); 2531 g_test_add_func("/cutils/qemu_strtou64/decimal", 2532 test_qemu_strtou64_decimal); 2533 g_test_add_func("/cutils/qemu_strtou64/hex", 2534 test_qemu_strtou64_hex); 2535 g_test_add_func("/cutils/qemu_strtou64/max", 2536 test_qemu_strtou64_max); 2537 g_test_add_func("/cutils/qemu_strtou64/overflow", 2538 test_qemu_strtou64_overflow); 2539 g_test_add_func("/cutils/qemu_strtou64/underflow", 2540 test_qemu_strtou64_underflow); 2541 g_test_add_func("/cutils/qemu_strtou64/negative", 2542 test_qemu_strtou64_negative); 2543 g_test_add_func("/cutils/qemu_strtou64_full/correct", 2544 test_qemu_strtou64_full_correct); 2545 g_test_add_func("/cutils/qemu_strtou64_full/null", 2546 test_qemu_strtou64_full_null); 2547 g_test_add_func("/cutils/qemu_strtou64_full/empty", 2548 test_qemu_strtou64_full_empty); 2549 g_test_add_func("/cutils/qemu_strtou64_full/negative", 2550 test_qemu_strtou64_full_negative); 2551 g_test_add_func("/cutils/qemu_strtou64_full/trailing", 2552 test_qemu_strtou64_full_trailing); 2553 g_test_add_func("/cutils/qemu_strtou64_full/max", 2554 test_qemu_strtou64_full_max); 2555 2556 g_test_add_func("/cutils/strtosz/simple", 2557 test_qemu_strtosz_simple); 2558 g_test_add_func("/cutils/strtosz/hex", 2559 test_qemu_strtosz_hex); 2560 g_test_add_func("/cutils/strtosz/units", 2561 test_qemu_strtosz_units); 2562 g_test_add_func("/cutils/strtosz/float", 2563 test_qemu_strtosz_float); 2564 g_test_add_func("/cutils/strtosz/invalid", 2565 test_qemu_strtosz_invalid); 2566 g_test_add_func("/cutils/strtosz/trailing", 2567 test_qemu_strtosz_trailing); 2568 g_test_add_func("/cutils/strtosz/erange", 2569 test_qemu_strtosz_erange); 2570 g_test_add_func("/cutils/strtosz/metric", 2571 test_qemu_strtosz_metric); 2572 2573 return g_test_run(); 2574 } 2575