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