test-cutils.c 43.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/*
 * cutils.c unit-tests
 *
 * Copyright (C) 2013 Red Hat Inc.
 *
 * Authors:
 *  Eduardo Habkost <ehabkost@redhat.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

P
Peter Maydell 已提交
28
#include "qemu/osdep.h"
29

30
#include "qemu/cutils.h"
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225

static void test_parse_uint_null(void)
{
    unsigned long long i = 999;
    char f = 'X';
    char *endptr = &f;
    int r;

    r = parse_uint(NULL, &i, &endptr, 0);

    g_assert_cmpint(r, ==, -EINVAL);
    g_assert_cmpint(i, ==, 0);
    g_assert(endptr == NULL);
}

static void test_parse_uint_empty(void)
{
    unsigned long long i = 999;
    char f = 'X';
    char *endptr = &f;
    const char *str = "";
    int r;

    r = parse_uint(str, &i, &endptr, 0);

    g_assert_cmpint(r, ==, -EINVAL);
    g_assert_cmpint(i, ==, 0);
    g_assert(endptr == str);
}

static void test_parse_uint_whitespace(void)
{
    unsigned long long i = 999;
    char f = 'X';
    char *endptr = &f;
    const char *str = "   \t   ";
    int r;

    r = parse_uint(str, &i, &endptr, 0);

    g_assert_cmpint(r, ==, -EINVAL);
    g_assert_cmpint(i, ==, 0);
    g_assert(endptr == str);
}


static void test_parse_uint_invalid(void)
{
    unsigned long long i = 999;
    char f = 'X';
    char *endptr = &f;
    const char *str = " \t xxx";
    int r;

    r = parse_uint(str, &i, &endptr, 0);

    g_assert_cmpint(r, ==, -EINVAL);
    g_assert_cmpint(i, ==, 0);
    g_assert(endptr == str);
}


static void test_parse_uint_trailing(void)
{
    unsigned long long i = 999;
    char f = 'X';
    char *endptr = &f;
    const char *str = "123xxx";
    int r;

    r = parse_uint(str, &i, &endptr, 0);

    g_assert_cmpint(r, ==, 0);
    g_assert_cmpint(i, ==, 123);
    g_assert(endptr == str + 3);
}

static void test_parse_uint_correct(void)
{
    unsigned long long i = 999;
    char f = 'X';
    char *endptr = &f;
    const char *str = "123";
    int r;

    r = parse_uint(str, &i, &endptr, 0);

    g_assert_cmpint(r, ==, 0);
    g_assert_cmpint(i, ==, 123);
    g_assert(endptr == str + strlen(str));
}

static void test_parse_uint_octal(void)
{
    unsigned long long i = 999;
    char f = 'X';
    char *endptr = &f;
    const char *str = "0123";
    int r;

    r = parse_uint(str, &i, &endptr, 0);

    g_assert_cmpint(r, ==, 0);
    g_assert_cmpint(i, ==, 0123);
    g_assert(endptr == str + strlen(str));
}

static void test_parse_uint_decimal(void)
{
    unsigned long long i = 999;
    char f = 'X';
    char *endptr = &f;
    const char *str = "0123";
    int r;

    r = parse_uint(str, &i, &endptr, 10);

    g_assert_cmpint(r, ==, 0);
    g_assert_cmpint(i, ==, 123);
    g_assert(endptr == str + strlen(str));
}


static void test_parse_uint_llong_max(void)
{
    unsigned long long i = 999;
    char f = 'X';
    char *endptr = &f;
    char *str = g_strdup_printf("%llu", (unsigned long long)LLONG_MAX + 1);
    int r;

    r = parse_uint(str, &i, &endptr, 0);

    g_assert_cmpint(r, ==, 0);
    g_assert_cmpint(i, ==, (unsigned long long)LLONG_MAX + 1);
    g_assert(endptr == str + strlen(str));

    g_free(str);
}

static void test_parse_uint_overflow(void)
{
    unsigned long long i = 999;
    char f = 'X';
    char *endptr = &f;
    const char *str = "99999999999999999999999999999999999999";
    int r;

    r = parse_uint(str, &i, &endptr, 0);

    g_assert_cmpint(r, ==, -ERANGE);
    g_assert_cmpint(i, ==, ULLONG_MAX);
    g_assert(endptr == str + strlen(str));
}

static void test_parse_uint_negative(void)
{
    unsigned long long i = 999;
    char f = 'X';
    char *endptr = &f;
    const char *str = " \t -321";
    int r;

    r = parse_uint(str, &i, &endptr, 0);

    g_assert_cmpint(r, ==, -ERANGE);
    g_assert_cmpint(i, ==, 0);
    g_assert(endptr == str + strlen(str));
}


static void test_parse_uint_full_trailing(void)
{
    unsigned long long i = 999;
    const char *str = "123xxx";
    int r;

    r = parse_uint_full(str, &i, 0);

    g_assert_cmpint(r, ==, -EINVAL);
    g_assert_cmpint(i, ==, 0);
}

static void test_parse_uint_full_correct(void)
{
    unsigned long long i = 999;
    const char *str = "123";
    int r;

    r = parse_uint_full(str, &i, 0);

    g_assert_cmpint(r, ==, 0);
    g_assert_cmpint(i, ==, 123);
}

226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
static void test_qemu_strtol_correct(void)
{
    const char *str = "12345 foo";
    char f = 'X';
    const char *endptr = &f;
    long res = 999;
    int err;

    err = qemu_strtol(str, &endptr, 0, &res);

    g_assert_cmpint(err, ==, 0);
    g_assert_cmpint(res, ==, 12345);
    g_assert(endptr == str + 5);
}

static void test_qemu_strtol_null(void)
{
    char f = 'X';
    const char *endptr = &f;
    long res = 999;
    int err;

    err = qemu_strtol(NULL, &endptr, 0, &res);

    g_assert_cmpint(err, ==, -EINVAL);
    g_assert(endptr == NULL);
}

static void test_qemu_strtol_empty(void)
{
    const char *str = "";
    char f = 'X';
    const char *endptr = &f;
    long res = 999;
    int err;

    err = qemu_strtol(str, &endptr, 0, &res);

264
    g_assert_cmpint(err, ==, -EINVAL);
265
    g_assert(endptr == str);
266 267 268 269 270 271 272 273 274 275 276 277
}

static void test_qemu_strtol_whitespace(void)
{
    const char *str = "  \t  ";
    char f = 'X';
    const char *endptr = &f;
    long res = 999;
    int err;

    err = qemu_strtol(str, &endptr, 0, &res);

278
    g_assert_cmpint(err, ==, -EINVAL);
279
    g_assert(endptr == str);
280 281 282 283 284 285 286 287 288 289 290 291
}

static void test_qemu_strtol_invalid(void)
{
    const char *str = "   xxxx  \t abc";
    char f = 'X';
    const char *endptr = &f;
    long res = 999;
    int err;

    err = qemu_strtol(str, &endptr, 0, &res);

292
    g_assert_cmpint(err, ==, -EINVAL);
293
    g_assert(endptr == str);
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
}

static void test_qemu_strtol_trailing(void)
{
    const char *str = "123xxx";
    char f = 'X';
    const char *endptr = &f;
    long res = 999;
    int err;

    err = qemu_strtol(str, &endptr, 0, &res);

    g_assert_cmpint(err, ==, 0);
    g_assert_cmpint(res, ==, 123);
    g_assert(endptr == str + 3);
}

static void test_qemu_strtol_octal(void)
{
    const char *str = "0123";
    char f = 'X';
    const char *endptr = &f;
    long res = 999;
    int err;

    err = qemu_strtol(str, &endptr, 8, &res);

    g_assert_cmpint(err, ==, 0);
    g_assert_cmpint(res, ==, 0123);
    g_assert(endptr == str + strlen(str));

    res = 999;
    endptr = &f;
    err = qemu_strtol(str, &endptr, 0, &res);

    g_assert_cmpint(err, ==, 0);
    g_assert_cmpint(res, ==, 0123);
    g_assert(endptr == str + strlen(str));
}

static void test_qemu_strtol_decimal(void)
{
    const char *str = "0123";
    char f = 'X';
    const char *endptr = &f;
    long res = 999;
    int err;

    err = qemu_strtol(str, &endptr, 10, &res);

    g_assert_cmpint(err, ==, 0);
    g_assert_cmpint(res, ==, 123);
    g_assert(endptr == str + strlen(str));

    str = "123";
    res = 999;
    endptr = &f;
    err = qemu_strtol(str, &endptr, 0, &res);

    g_assert_cmpint(err, ==, 0);
    g_assert_cmpint(res, ==, 123);
    g_assert(endptr == str + strlen(str));
}

static void test_qemu_strtol_hex(void)
{
    const char *str = "0123";
    char f = 'X';
    const char *endptr = &f;
    long res = 999;
    int err;

    err = qemu_strtol(str, &endptr, 16, &res);

    g_assert_cmpint(err, ==, 0);
    g_assert_cmpint(res, ==, 0x123);
    g_assert(endptr == str + strlen(str));

    str = "0x123";
    res = 999;
    endptr = &f;
    err = qemu_strtol(str, &endptr, 0, &res);

    g_assert_cmpint(err, ==, 0);
    g_assert_cmpint(res, ==, 0x123);
    g_assert(endptr == str + strlen(str));
}

static void test_qemu_strtol_max(void)
{
384
    char *str = g_strdup_printf("%ld", LONG_MAX);
385 386 387 388 389 390 391 392 393 394
    char f = 'X';
    const char *endptr = &f;
    long res = 999;
    int err;

    err = qemu_strtol(str, &endptr, 0, &res);

    g_assert_cmpint(err, ==, 0);
    g_assert_cmpint(res, ==, LONG_MAX);
    g_assert(endptr == str + strlen(str));
395
    g_free(str);
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
}

static void test_qemu_strtol_overflow(void)
{
    const char *str = "99999999999999999999999999999999999999999999";
    char f = 'X';
    const char *endptr = &f;
    long res = 999;
    int err;

    err = qemu_strtol(str, &endptr, 0, &res);

    g_assert_cmpint(err, ==, -ERANGE);
    g_assert_cmpint(res, ==, LONG_MAX);
    g_assert(endptr == str + strlen(str));
}

static void test_qemu_strtol_underflow(void)
{
    const char *str = "-99999999999999999999999999999999999999999999";
    char f = 'X';
    const char *endptr = &f;
    long res = 999;
    int err;

    err  = qemu_strtol(str, &endptr, 0, &res);

    g_assert_cmpint(err, ==, -ERANGE);
    g_assert_cmpint(res, ==, LONG_MIN);
    g_assert(endptr == str + strlen(str));
}

static void test_qemu_strtol_negative(void)
{
    const char *str = "  \t -321";
    char f = 'X';
    const char *endptr = &f;
    long res = 999;
    int err;

    err = qemu_strtol(str, &endptr, 0, &res);

    g_assert_cmpint(err, ==, 0);
    g_assert_cmpint(res, ==, -321);
    g_assert(endptr == str + strlen(str));
}

static void test_qemu_strtol_full_correct(void)
{
    const char *str = "123";
    long res = 999;
    int err;

    err = qemu_strtol(str, NULL, 0, &res);

    g_assert_cmpint(err, ==, 0);
    g_assert_cmpint(res, ==, 123);
}

static void test_qemu_strtol_full_null(void)
{
    char f = 'X';
    const char *endptr = &f;
    long res = 999;
    int err;

    err = qemu_strtol(NULL, &endptr, 0, &res);

    g_assert_cmpint(err, ==, -EINVAL);
    g_assert(endptr == NULL);
}

static void test_qemu_strtol_full_empty(void)
{
    const char *str = "";
    long res = 999L;
    int err;

    err =  qemu_strtol(str, NULL, 0, &res);

476
    g_assert_cmpint(err, ==, -EINVAL);
477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
}

static void test_qemu_strtol_full_negative(void)
{
    const char *str = " \t -321";
    long res = 999;
    int err;

    err = qemu_strtol(str, NULL, 0, &res);

    g_assert_cmpint(err, ==, 0);
    g_assert_cmpint(res, ==, -321);
}

static void test_qemu_strtol_full_trailing(void)
{
    const char *str = "123xxx";
    long res;
    int err;

    err = qemu_strtol(str, NULL, 0, &res);

    g_assert_cmpint(err, ==, -EINVAL);
}

static void test_qemu_strtol_full_max(void)
{
504
    char *str = g_strdup_printf("%ld", LONG_MAX);
505 506 507 508 509 510 511
    long res;
    int err;

    err = qemu_strtol(str, NULL, 0, &res);

    g_assert_cmpint(err, ==, 0);
    g_assert_cmpint(res, ==, LONG_MAX);
512
    g_free(str);
513
}
514 515 516 517 518 519 520 521 522 523 524 525

static void test_qemu_strtoul_correct(void)
{
    const char *str = "12345 foo";
    char f = 'X';
    const char *endptr = &f;
    unsigned long res = 999;
    int err;

    err = qemu_strtoul(str, &endptr, 0, &res);

    g_assert_cmpint(err, ==, 0);
526
    g_assert_cmpuint(res, ==, 12345);
527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552
    g_assert(endptr == str + 5);
}

static void test_qemu_strtoul_null(void)
{
    char f = 'X';
    const char *endptr = &f;
    unsigned long res = 999;
    int err;

    err = qemu_strtoul(NULL, &endptr, 0, &res);

    g_assert_cmpint(err, ==, -EINVAL);
    g_assert(endptr == NULL);
}

static void test_qemu_strtoul_empty(void)
{
    const char *str = "";
    char f = 'X';
    const char *endptr = &f;
    unsigned long res = 999;
    int err;

    err = qemu_strtoul(str, &endptr, 0, &res);

553
    g_assert_cmpint(err, ==, -EINVAL);
554
    g_assert(endptr == str);
555 556 557 558 559 560 561 562 563 564 565 566
}

static void test_qemu_strtoul_whitespace(void)
{
    const char *str = "  \t  ";
    char f = 'X';
    const char *endptr = &f;
    unsigned long res = 999;
    int err;

    err = qemu_strtoul(str, &endptr, 0, &res);

567
    g_assert_cmpint(err, ==, -EINVAL);
568
    g_assert(endptr == str);
569 570 571 572 573 574 575 576 577 578 579 580
}

static void test_qemu_strtoul_invalid(void)
{
    const char *str = "   xxxx  \t abc";
    char f = 'X';
    const char *endptr = &f;
    unsigned long res = 999;
    int err;

    err = qemu_strtoul(str, &endptr, 0, &res);

581
    g_assert_cmpint(err, ==, -EINVAL);
582
    g_assert(endptr == str);
583 584 585 586 587 588 589 590 591 592 593 594 595
}

static void test_qemu_strtoul_trailing(void)
{
    const char *str = "123xxx";
    char f = 'X';
    const char *endptr = &f;
    unsigned long res = 999;
    int err;

    err = qemu_strtoul(str, &endptr, 0, &res);

    g_assert_cmpint(err, ==, 0);
596
    g_assert_cmpuint(res, ==, 123);
597 598 599 600 601 602 603 604 605 606 607 608 609 610
    g_assert(endptr == str + 3);
}

static void test_qemu_strtoul_octal(void)
{
    const char *str = "0123";
    char f = 'X';
    const char *endptr = &f;
    unsigned long res = 999;
    int err;

    err = qemu_strtoul(str, &endptr, 8, &res);

    g_assert_cmpint(err, ==, 0);
611
    g_assert_cmpuint(res, ==, 0123);
612 613 614 615 616 617 618
    g_assert(endptr == str + strlen(str));

    res = 999;
    endptr = &f;
    err = qemu_strtoul(str, &endptr, 0, &res);

    g_assert_cmpint(err, ==, 0);
619
    g_assert_cmpuint(res, ==, 0123);
620 621 622 623 624 625 626 627 628 629 630 631 632 633
    g_assert(endptr == str + strlen(str));
}

static void test_qemu_strtoul_decimal(void)
{
    const char *str = "0123";
    char f = 'X';
    const char *endptr = &f;
    unsigned long res = 999;
    int err;

    err = qemu_strtoul(str, &endptr, 10, &res);

    g_assert_cmpint(err, ==, 0);
634
    g_assert_cmpuint(res, ==, 123);
635 636 637 638 639 640 641 642
    g_assert(endptr == str + strlen(str));

    str = "123";
    res = 999;
    endptr = &f;
    err = qemu_strtoul(str, &endptr, 0, &res);

    g_assert_cmpint(err, ==, 0);
643
    g_assert_cmpuint(res, ==, 123);
644 645 646 647 648 649 650 651 652 653 654 655 656 657
    g_assert(endptr == str + strlen(str));
}

static void test_qemu_strtoul_hex(void)
{
    const char *str = "0123";
    char f = 'X';
    const char *endptr = &f;
    unsigned long res = 999;
    int err;

    err = qemu_strtoul(str, &endptr, 16, &res);

    g_assert_cmpint(err, ==, 0);
658
    g_assert_cmphex(res, ==, 0x123);
659 660 661 662 663 664 665 666
    g_assert(endptr == str + strlen(str));

    str = "0x123";
    res = 999;
    endptr = &f;
    err = qemu_strtoul(str, &endptr, 0, &res);

    g_assert_cmpint(err, ==, 0);
667
    g_assert_cmphex(res, ==, 0x123);
668 669 670 671 672
    g_assert(endptr == str + strlen(str));
}

static void test_qemu_strtoul_max(void)
{
673
    char *str = g_strdup_printf("%lu", ULONG_MAX);
674 675 676 677 678 679 680 681
    char f = 'X';
    const char *endptr = &f;
    unsigned long res = 999;
    int err;

    err = qemu_strtoul(str, &endptr, 0, &res);

    g_assert_cmpint(err, ==, 0);
682
    g_assert_cmphex(res, ==, ULONG_MAX);
683
    g_assert(endptr == str + strlen(str));
684
    g_free(str);
685 686 687 688 689 690 691 692 693 694 695 696 697
}

static void test_qemu_strtoul_overflow(void)
{
    const char *str = "99999999999999999999999999999999999999999999";
    char f = 'X';
    const char *endptr = &f;
    unsigned long res = 999;
    int err;

    err = qemu_strtoul(str, &endptr, 0, &res);

    g_assert_cmpint(err, ==, -ERANGE);
698
    g_assert_cmphex(res, ==, ULONG_MAX);
699 700 701 702 703 704 705 706 707 708 709 710 711 712
    g_assert(endptr == str + strlen(str));
}

static void test_qemu_strtoul_underflow(void)
{
    const char *str = "-99999999999999999999999999999999999999999999";
    char f = 'X';
    const char *endptr = &f;
    unsigned long res = 999;
    int err;

    err  = qemu_strtoul(str, &endptr, 0, &res);

    g_assert_cmpint(err, ==, -ERANGE);
713
    g_assert_cmpuint(res, ==, -1ul);
714 715 716 717 718 719 720 721 722 723 724 725 726 727
    g_assert(endptr == str + strlen(str));
}

static void test_qemu_strtoul_negative(void)
{
    const char *str = "  \t -321";
    char f = 'X';
    const char *endptr = &f;
    unsigned long res = 999;
    int err;

    err = qemu_strtoul(str, &endptr, 0, &res);

    g_assert_cmpint(err, ==, 0);
728
    g_assert_cmpuint(res, ==, -321ul);
729 730 731 732 733 734 735 736 737 738 739 740
    g_assert(endptr == str + strlen(str));
}

static void test_qemu_strtoul_full_correct(void)
{
    const char *str = "123";
    unsigned long res = 999;
    int err;

    err = qemu_strtoul(str, NULL, 0, &res);

    g_assert_cmpint(err, ==, 0);
741
    g_assert_cmpuint(res, ==, 123);
742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761
}

static void test_qemu_strtoul_full_null(void)
{
    unsigned long res = 999;
    int err;

    err = qemu_strtoul(NULL, NULL, 0, &res);

    g_assert_cmpint(err, ==, -EINVAL);
}

static void test_qemu_strtoul_full_empty(void)
{
    const char *str = "";
    unsigned long res = 999;
    int err;

    err = qemu_strtoul(str, NULL, 0, &res);

762
    g_assert_cmpint(err, ==, -EINVAL);
763 764 765 766 767 768 769 770 771
}
static void test_qemu_strtoul_full_negative(void)
{
    const char *str = " \t -321";
    unsigned long res = 999;
    int err;

    err = qemu_strtoul(str, NULL, 0, &res);
    g_assert_cmpint(err, ==, 0);
772
    g_assert_cmpuint(res, ==, -321ul);
773 774 775 776 777 778 779 780 781 782 783 784 785 786 787
}

static void test_qemu_strtoul_full_trailing(void)
{
    const char *str = "123xxx";
    unsigned long res;
    int err;

    err = qemu_strtoul(str, NULL, 0, &res);

    g_assert_cmpint(err, ==, -EINVAL);
}

static void test_qemu_strtoul_full_max(void)
{
788
    char *str = g_strdup_printf("%lu", ULONG_MAX);
789 790 791 792 793 794
    unsigned long res = 999;
    int err;

    err = qemu_strtoul(str, NULL, 0, &res);

    g_assert_cmpint(err, ==, 0);
795
    g_assert_cmphex(res, ==, ULONG_MAX);
796
    g_free(str);
797 798
}

799
static void test_qemu_strtoi64_correct(void)
800 801 802 803 804 805 806
{
    const char *str = "12345 foo";
    char f = 'X';
    const char *endptr = &f;
    int64_t res = 999;
    int err;

807
    err = qemu_strtoi64(str, &endptr, 0, &res);
808 809 810 811 812 813

    g_assert_cmpint(err, ==, 0);
    g_assert_cmpint(res, ==, 12345);
    g_assert(endptr == str + 5);
}

814
static void test_qemu_strtoi64_null(void)
815 816 817 818 819 820
{
    char f = 'X';
    const char *endptr = &f;
    int64_t res = 999;
    int err;

821
    err = qemu_strtoi64(NULL, &endptr, 0, &res);
822 823 824 825 826

    g_assert_cmpint(err, ==, -EINVAL);
    g_assert(endptr == NULL);
}

827
static void test_qemu_strtoi64_empty(void)
828 829 830 831 832 833 834
{
    const char *str = "";
    char f = 'X';
    const char *endptr = &f;
    int64_t res = 999;
    int err;

835
    err = qemu_strtoi64(str, &endptr, 0, &res);
836

837
    g_assert_cmpint(err, ==, -EINVAL);
838
    g_assert(endptr == str);
839 840
}

841
static void test_qemu_strtoi64_whitespace(void)
842 843 844 845 846 847 848
{
    const char *str = "  \t  ";
    char f = 'X';
    const char *endptr = &f;
    int64_t res = 999;
    int err;

849
    err = qemu_strtoi64(str, &endptr, 0, &res);
850

851
    g_assert_cmpint(err, ==, -EINVAL);
852
    g_assert(endptr == str);
853 854
}

855
static void test_qemu_strtoi64_invalid(void)
856 857 858 859 860 861 862
{
    const char *str = "   xxxx  \t abc";
    char f = 'X';
    const char *endptr = &f;
    int64_t res = 999;
    int err;

863
    err = qemu_strtoi64(str, &endptr, 0, &res);
864

865
    g_assert_cmpint(err, ==, -EINVAL);
866
    g_assert(endptr == str);
867 868
}

869
static void test_qemu_strtoi64_trailing(void)
870 871 872 873 874 875 876
{
    const char *str = "123xxx";
    char f = 'X';
    const char *endptr = &f;
    int64_t res = 999;
    int err;

877
    err = qemu_strtoi64(str, &endptr, 0, &res);
878 879 880 881 882 883

    g_assert_cmpint(err, ==, 0);
    g_assert_cmpint(res, ==, 123);
    g_assert(endptr == str + 3);
}

884
static void test_qemu_strtoi64_octal(void)
885 886 887 888 889 890 891
{
    const char *str = "0123";
    char f = 'X';
    const char *endptr = &f;
    int64_t res = 999;
    int err;

892
    err = qemu_strtoi64(str, &endptr, 8, &res);
893 894 895 896 897 898 899

    g_assert_cmpint(err, ==, 0);
    g_assert_cmpint(res, ==, 0123);
    g_assert(endptr == str + strlen(str));

    endptr = &f;
    res = 999;
900
    err = qemu_strtoi64(str, &endptr, 0, &res);
901 902 903 904 905 906

    g_assert_cmpint(err, ==, 0);
    g_assert_cmpint(res, ==, 0123);
    g_assert(endptr == str + strlen(str));
}

907
static void test_qemu_strtoi64_decimal(void)
908 909 910 911 912 913 914
{
    const char *str = "0123";
    char f = 'X';
    const char *endptr = &f;
    int64_t res = 999;
    int err;

915
    err = qemu_strtoi64(str, &endptr, 10, &res);
916 917 918 919 920 921 922 923

    g_assert_cmpint(err, ==, 0);
    g_assert_cmpint(res, ==, 123);
    g_assert(endptr == str + strlen(str));

    str = "123";
    endptr = &f;
    res = 999;
924
    err = qemu_strtoi64(str, &endptr, 0, &res);
925 926 927 928 929 930

    g_assert_cmpint(err, ==, 0);
    g_assert_cmpint(res, ==, 123);
    g_assert(endptr == str + strlen(str));
}

931
static void test_qemu_strtoi64_hex(void)
932 933 934 935 936 937 938
{
    const char *str = "0123";
    char f = 'X';
    const char *endptr = &f;
    int64_t res = 999;
    int err;

939
    err = qemu_strtoi64(str, &endptr, 16, &res);
940 941 942 943 944 945 946 947

    g_assert_cmpint(err, ==, 0);
    g_assert_cmpint(res, ==, 0x123);
    g_assert(endptr == str + strlen(str));

    str = "0x123";
    endptr = &f;
    res = 999;
948
    err = qemu_strtoi64(str, &endptr, 0, &res);
949 950 951 952 953 954

    g_assert_cmpint(err, ==, 0);
    g_assert_cmpint(res, ==, 0x123);
    g_assert(endptr == str + strlen(str));
}

955
static void test_qemu_strtoi64_max(void)
956
{
957
    char *str = g_strdup_printf("%lld", LLONG_MAX);
958 959 960 961 962
    char f = 'X';
    const char *endptr = &f;
    int64_t res = 999;
    int err;

963
    err = qemu_strtoi64(str, &endptr, 0, &res);
964 965 966 967

    g_assert_cmpint(err, ==, 0);
    g_assert_cmpint(res, ==, LLONG_MAX);
    g_assert(endptr == str + strlen(str));
968
    g_free(str);
969 970
}

971
static void test_qemu_strtoi64_overflow(void)
972 973 974 975 976 977 978
{
    const char *str = "99999999999999999999999999999999999999999999";
    char f = 'X';
    const char *endptr = &f;
    int64_t res = 999;
    int err;

979
    err = qemu_strtoi64(str, &endptr, 0, &res);
980 981 982 983 984 985

    g_assert_cmpint(err, ==, -ERANGE);
    g_assert_cmpint(res, ==, LLONG_MAX);
    g_assert(endptr == str + strlen(str));
}

986
static void test_qemu_strtoi64_underflow(void)
987 988 989 990 991 992 993
{
    const char *str = "-99999999999999999999999999999999999999999999";
    char f = 'X';
    const char *endptr = &f;
    int64_t res = 999;
    int err;

994
    err  = qemu_strtoi64(str, &endptr, 0, &res);
995 996 997 998 999 1000

    g_assert_cmpint(err, ==, -ERANGE);
    g_assert_cmpint(res, ==, LLONG_MIN);
    g_assert(endptr == str + strlen(str));
}

1001
static void test_qemu_strtoi64_negative(void)
1002 1003 1004 1005 1006 1007 1008
{
    const char *str = "  \t -321";
    char f = 'X';
    const char *endptr = &f;
    int64_t res = 999;
    int err;

1009
    err = qemu_strtoi64(str, &endptr, 0, &res);
1010 1011 1012 1013 1014 1015

    g_assert_cmpint(err, ==, 0);
    g_assert_cmpint(res, ==, -321);
    g_assert(endptr == str + strlen(str));
}

1016
static void test_qemu_strtoi64_full_correct(void)
1017 1018 1019 1020 1021
{
    const char *str = "123";
    int64_t res = 999;
    int err;

1022
    err = qemu_strtoi64(str, NULL, 0, &res);
1023 1024 1025 1026 1027

    g_assert_cmpint(err, ==, 0);
    g_assert_cmpint(res, ==, 123);
}

1028
static void test_qemu_strtoi64_full_null(void)
1029 1030 1031 1032
{
    int64_t res = 999;
    int err;

1033
    err = qemu_strtoi64(NULL, NULL, 0, &res);
1034 1035 1036 1037

    g_assert_cmpint(err, ==, -EINVAL);
}

1038
static void test_qemu_strtoi64_full_empty(void)
1039 1040 1041 1042 1043
{
    const char *str = "";
    int64_t res = 999;
    int err;

1044
    err = qemu_strtoi64(str, NULL, 0, &res);
1045

1046
    g_assert_cmpint(err, ==, -EINVAL);
1047 1048
}

1049
static void test_qemu_strtoi64_full_negative(void)
1050 1051 1052 1053 1054
{
    const char *str = " \t -321";
    int64_t res = 999;
    int err;

1055
    err = qemu_strtoi64(str, NULL, 0, &res);
1056 1057 1058 1059 1060

    g_assert_cmpint(err, ==, 0);
    g_assert_cmpint(res, ==, -321);
}

1061
static void test_qemu_strtoi64_full_trailing(void)
1062 1063 1064 1065 1066
{
    const char *str = "123xxx";
    int64_t res = 999;
    int err;

1067
    err = qemu_strtoi64(str, NULL, 0, &res);
1068 1069 1070 1071

    g_assert_cmpint(err, ==, -EINVAL);
}

1072
static void test_qemu_strtoi64_full_max(void)
1073 1074
{

1075
    char *str = g_strdup_printf("%lld", LLONG_MAX);
1076 1077 1078
    int64_t res;
    int err;

1079
    err = qemu_strtoi64(str, NULL, 0, &res);
1080 1081 1082

    g_assert_cmpint(err, ==, 0);
    g_assert_cmpint(res, ==, LLONG_MAX);
1083
    g_free(str);
1084 1085
}

1086
static void test_qemu_strtou64_correct(void)
1087 1088 1089 1090 1091 1092 1093
{
    const char *str = "12345 foo";
    char f = 'X';
    const char *endptr = &f;
    uint64_t res = 999;
    int err;

1094
    err = qemu_strtou64(str, &endptr, 0, &res);
1095 1096

    g_assert_cmpint(err, ==, 0);
1097
    g_assert_cmpuint(res, ==, 12345);
1098 1099 1100
    g_assert(endptr == str + 5);
}

1101
static void test_qemu_strtou64_null(void)
1102 1103 1104 1105 1106 1107
{
    char f = 'X';
    const char *endptr = &f;
    uint64_t res = 999;
    int err;

1108
    err = qemu_strtou64(NULL, &endptr, 0, &res);
1109 1110 1111 1112 1113

    g_assert_cmpint(err, ==, -EINVAL);
    g_assert(endptr == NULL);
}

1114
static void test_qemu_strtou64_empty(void)
1115 1116 1117 1118 1119 1120 1121
{
    const char *str = "";
    char f = 'X';
    const char *endptr = &f;
    uint64_t res = 999;
    int err;

1122
    err = qemu_strtou64(str, &endptr, 0, &res);
1123

1124
    g_assert_cmpint(err, ==, -EINVAL);
1125
    g_assert(endptr == str);
1126 1127
}

1128
static void test_qemu_strtou64_whitespace(void)
1129 1130 1131 1132 1133 1134 1135
{
    const char *str = "  \t  ";
    char f = 'X';
    const char *endptr = &f;
    uint64_t res = 999;
    int err;

1136
    err = qemu_strtou64(str, &endptr, 0, &res);
1137

1138
    g_assert_cmpint(err, ==, -EINVAL);
1139
    g_assert(endptr == str);
1140 1141
}

1142
static void test_qemu_strtou64_invalid(void)
1143 1144 1145 1146 1147 1148 1149
{
    const char *str = "   xxxx  \t abc";
    char f = 'X';
    const char *endptr = &f;
    uint64_t res = 999;
    int err;

1150
    err = qemu_strtou64(str, &endptr, 0, &res);
1151

1152
    g_assert_cmpint(err, ==, -EINVAL);
1153
    g_assert(endptr == str);
1154 1155
}

1156
static void test_qemu_strtou64_trailing(void)
1157 1158 1159 1160 1161 1162 1163
{
    const char *str = "123xxx";
    char f = 'X';
    const char *endptr = &f;
    uint64_t res = 999;
    int err;

1164
    err = qemu_strtou64(str, &endptr, 0, &res);
1165 1166

    g_assert_cmpint(err, ==, 0);
1167
    g_assert_cmpuint(res, ==, 123);
1168 1169 1170
    g_assert(endptr == str + 3);
}

1171
static void test_qemu_strtou64_octal(void)
1172 1173 1174 1175 1176 1177 1178
{
    const char *str = "0123";
    char f = 'X';
    const char *endptr = &f;
    uint64_t res = 999;
    int err;

1179
    err = qemu_strtou64(str, &endptr, 8, &res);
1180 1181

    g_assert_cmpint(err, ==, 0);
1182
    g_assert_cmpuint(res, ==, 0123);
1183 1184 1185 1186
    g_assert(endptr == str + strlen(str));

    endptr = &f;
    res = 999;
1187
    err = qemu_strtou64(str, &endptr, 0, &res);
1188 1189

    g_assert_cmpint(err, ==, 0);
1190
    g_assert_cmpuint(res, ==, 0123);
1191 1192 1193
    g_assert(endptr == str + strlen(str));
}

1194
static void test_qemu_strtou64_decimal(void)
1195 1196 1197 1198 1199 1200 1201
{
    const char *str = "0123";
    char f = 'X';
    const char *endptr = &f;
    uint64_t res = 999;
    int err;

1202
    err = qemu_strtou64(str, &endptr, 10, &res);
1203 1204

    g_assert_cmpint(err, ==, 0);
1205
    g_assert_cmpuint(res, ==, 123);
1206 1207 1208 1209 1210
    g_assert(endptr == str + strlen(str));

    str = "123";
    endptr = &f;
    res = 999;
1211
    err = qemu_strtou64(str, &endptr, 0, &res);
1212 1213

    g_assert_cmpint(err, ==, 0);
1214
    g_assert_cmpuint(res, ==, 123);
1215 1216 1217
    g_assert(endptr == str + strlen(str));
}

1218
static void test_qemu_strtou64_hex(void)
1219 1220 1221 1222 1223 1224 1225
{
    const char *str = "0123";
    char f = 'X';
    const char *endptr = &f;
    uint64_t res = 999;
    int err;

1226
    err = qemu_strtou64(str, &endptr, 16, &res);
1227 1228

    g_assert_cmpint(err, ==, 0);
1229
    g_assert_cmphex(res, ==, 0x123);
1230 1231 1232 1233 1234
    g_assert(endptr == str + strlen(str));

    str = "0x123";
    endptr = &f;
    res = 999;
1235
    err = qemu_strtou64(str, &endptr, 0, &res);
1236 1237

    g_assert_cmpint(err, ==, 0);
1238
    g_assert_cmphex(res, ==, 0x123);
1239 1240 1241
    g_assert(endptr == str + strlen(str));
}

1242
static void test_qemu_strtou64_max(void)
1243
{
1244
    char *str = g_strdup_printf("%llu", ULLONG_MAX);
1245 1246 1247 1248 1249
    char f = 'X';
    const char *endptr = &f;
    uint64_t res = 999;
    int err;

1250
    err = qemu_strtou64(str, &endptr, 0, &res);
1251 1252

    g_assert_cmpint(err, ==, 0);
1253
    g_assert_cmphex(res, ==, ULLONG_MAX);
1254
    g_assert(endptr == str + strlen(str));
1255
    g_free(str);
1256 1257
}

1258
static void test_qemu_strtou64_overflow(void)
1259 1260 1261 1262 1263 1264 1265
{
    const char *str = "99999999999999999999999999999999999999999999";
    char f = 'X';
    const char *endptr = &f;
    uint64_t res = 999;
    int err;

1266
    err = qemu_strtou64(str, &endptr, 0, &res);
1267 1268

    g_assert_cmpint(err, ==, -ERANGE);
1269
    g_assert_cmphex(res, ==, ULLONG_MAX);
1270 1271 1272
    g_assert(endptr == str + strlen(str));
}

1273
static void test_qemu_strtou64_underflow(void)
1274 1275 1276 1277 1278 1279 1280
{
    const char *str = "-99999999999999999999999999999999999999999999";
    char f = 'X';
    const char *endptr = &f;
    uint64_t res = 999;
    int err;

1281
    err  = qemu_strtou64(str, &endptr, 0, &res);
1282 1283

    g_assert_cmpint(err, ==, -ERANGE);
1284
    g_assert_cmphex(res, ==, -1ull);
1285 1286 1287
    g_assert(endptr == str + strlen(str));
}

1288
static void test_qemu_strtou64_negative(void)
1289 1290 1291 1292 1293 1294 1295
{
    const char *str = "  \t -321";
    char f = 'X';
    const char *endptr = &f;
    uint64_t res = 999;
    int err;

1296
    err = qemu_strtou64(str, &endptr, 0, &res);
1297 1298

    g_assert_cmpint(err, ==, 0);
1299
    g_assert_cmpuint(res, ==, -321ull);
1300 1301 1302
    g_assert(endptr == str + strlen(str));
}

1303
static void test_qemu_strtou64_full_correct(void)
1304 1305 1306 1307 1308
{
    const char *str = "18446744073709551614";
    uint64_t res = 999;
    int err;

1309
    err = qemu_strtou64(str, NULL, 0, &res);
1310 1311

    g_assert_cmpint(err, ==, 0);
1312
    g_assert_cmpuint(res, ==, 18446744073709551614ull);
1313 1314
}

1315
static void test_qemu_strtou64_full_null(void)
1316 1317 1318 1319
{
    uint64_t res = 999;
    int err;

1320
    err = qemu_strtou64(NULL, NULL, 0, &res);
1321 1322 1323 1324

    g_assert_cmpint(err, ==, -EINVAL);
}

1325
static void test_qemu_strtou64_full_empty(void)
1326 1327 1328 1329 1330
{
    const char *str = "";
    uint64_t res = 999;
    int err;

1331
    err = qemu_strtou64(str, NULL, 0, &res);
1332

1333
    g_assert_cmpint(err, ==, -EINVAL);
1334 1335
}

1336
static void test_qemu_strtou64_full_negative(void)
1337 1338 1339 1340 1341
{
    const char *str = " \t -321";
    uint64_t res = 999;
    int err;

1342
    err = qemu_strtou64(str, NULL, 0, &res);
1343 1344

    g_assert_cmpint(err, ==, 0);
1345
    g_assert_cmpuint(res, ==, -321ull);
1346 1347
}

1348
static void test_qemu_strtou64_full_trailing(void)
1349 1350 1351 1352 1353
{
    const char *str = "18446744073709551614xxxxxx";
    uint64_t res = 999;
    int err;

1354
    err = qemu_strtou64(str, NULL, 0, &res);
1355 1356 1357 1358

    g_assert_cmpint(err, ==, -EINVAL);
}

1359
static void test_qemu_strtou64_full_max(void)
1360
{
1361
    char *str = g_strdup_printf("%lld", ULLONG_MAX);
1362 1363 1364
    uint64_t res = 999;
    int err;

1365
    err = qemu_strtou64(str, NULL, 0, &res);
1366 1367

    g_assert_cmpint(err, ==, 0);
1368
    g_assert_cmphex(res, ==, ULLONG_MAX);
1369
    g_free(str);
1370 1371
}

1372 1373
static void test_qemu_strtosz_simple(void)
{
1374
    const char *str;
1375 1376 1377
    char *endptr = NULL;
    int64_t res;

1378 1379 1380 1381 1382 1383
    str = "0";
    res = qemu_strtosz(str, &endptr);
    g_assert_cmpint(res, ==, 0);
    g_assert(endptr == str + 1);

    str = "12345M";
1384 1385 1386 1387 1388 1389
    res = qemu_strtosz(str, &endptr);
    g_assert_cmpint(res, ==, 12345 * M_BYTE);
    g_assert(endptr == str + 6);

    res = qemu_strtosz(str, NULL);
    g_assert_cmpint(res, ==, 12345 * M_BYTE);
1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419

    /* Note: precision is 53 bits since we're parsing with strtod() */

    str = "9007199254740991"; /* 2^53-1 */
    res = qemu_strtosz_suffix(str, &endptr, QEMU_STRTOSZ_DEFSUFFIX_B);
    g_assert_cmpint(res, ==, 0x1fffffffffffff);
    g_assert(endptr == str + 16);

    str = "9007199254740992"; /* 2^53 */
    res = qemu_strtosz_suffix(str, &endptr, QEMU_STRTOSZ_DEFSUFFIX_B);
    g_assert_cmpint(res, ==, 0x20000000000000);
    g_assert(endptr == str + 16);

    str = "9007199254740993"; /* 2^53+1 */
    res = qemu_strtosz_suffix(str, &endptr, QEMU_STRTOSZ_DEFSUFFIX_B);
    g_assert_cmpint(res, ==, 0x20000000000000); /* rounded to 53 bits */
    g_assert(endptr == str + 16);

    str = "9223372036854774784"; /* 0x7ffffffffffffc00 (53 msbs set) */
    res = qemu_strtosz_suffix(str, &endptr, QEMU_STRTOSZ_DEFSUFFIX_B);
    g_assert_cmpint(res, ==, 0x7ffffffffffffc00);
    g_assert(endptr == str + 19);

    str = "9223372036854775295"; /* 0x7ffffffffffffdff */
    res = qemu_strtosz_suffix(str, &endptr, QEMU_STRTOSZ_DEFSUFFIX_B);
    g_assert_cmpint(res, ==, 0x7ffffffffffffc00); /* rounded to 53 bits */
    g_assert(endptr == str + 19);

    /* 0x7ffffffffffffe00..0x7fffffffffffffff get rounded to
     * 0x8000000000000000, thus -ERANGE; see test_qemu_strtosz_erange() */
1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431
}

static void test_qemu_strtosz_units(void)
{
    const char *none = "1";
    const char *b = "1B";
    const char *k = "1K";
    const char *m = "1M";
    const char *g = "1G";
    const char *t = "1T";
    const char *p = "1P";
    const char *e = "1E";
1432
    char *endptr = NULL;
1433 1434 1435
    int64_t res;

    /* default is M */
1436
    res = qemu_strtosz(none, &endptr);
1437
    g_assert_cmpint(res, ==, M_BYTE);
1438
    g_assert(endptr == none + 1);
1439

1440
    res = qemu_strtosz(b, &endptr);
1441
    g_assert_cmpint(res, ==, 1);
1442
    g_assert(endptr == b + 2);
1443

1444
    res = qemu_strtosz(k, &endptr);
1445
    g_assert_cmpint(res, ==, K_BYTE);
1446
    g_assert(endptr == k + 2);
1447

1448
    res = qemu_strtosz(m, &endptr);
1449
    g_assert_cmpint(res, ==, M_BYTE);
1450
    g_assert(endptr == m + 2);
1451

1452
    res = qemu_strtosz(g, &endptr);
1453
    g_assert_cmpint(res, ==, G_BYTE);
1454
    g_assert(endptr == g + 2);
1455

1456
    res = qemu_strtosz(t, &endptr);
1457
    g_assert_cmpint(res, ==, T_BYTE);
1458
    g_assert(endptr == t + 2);
1459

1460
    res = qemu_strtosz(p, &endptr);
1461
    g_assert_cmpint(res, ==, P_BYTE);
1462
    g_assert(endptr == p + 2);
1463

1464
    res = qemu_strtosz(e, &endptr);
1465
    g_assert_cmpint(res, ==, E_BYTE);
1466
    g_assert(endptr == e + 2);
1467 1468 1469 1470 1471
}

static void test_qemu_strtosz_float(void)
{
    const char *str = "12.345M";
1472
    char *endptr = NULL;
1473 1474
    int64_t res;

1475
    res = qemu_strtosz(str, &endptr);
1476
    g_assert_cmpint(res, ==, 12.345 * M_BYTE);
1477
    g_assert(endptr == str + 7);
1478 1479
}

1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501
static void test_qemu_strtosz_invalid(void)
{
    const char *str;
    char *endptr = NULL;
    int64_t res;

    str = "";
    res = qemu_strtosz(str, &endptr);
    g_assert_cmpint(res, ==, -EINVAL);
    g_assert(endptr == str);

    str = " \t ";
    res = qemu_strtosz(str, &endptr);
    g_assert_cmpint(res, ==, -EINVAL);
    g_assert(endptr == str);

    str = "crap";
    res = qemu_strtosz(str, &endptr);
    g_assert_cmpint(res, ==, -EINVAL);
    g_assert(endptr == str);
}

1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518
static void test_qemu_strtosz_trailing(void)
{
    const char *str;
    char *endptr = NULL;
    int64_t res;

    str = "123xxx";
    res = qemu_strtosz(str, &endptr);
    g_assert_cmpint(res, ==, 123 * M_BYTE);
    g_assert(endptr == str + 3);

    str = "1kiB";
    res = qemu_strtosz(str, &endptr);
    g_assert_cmpint(res, ==, 1024);
    g_assert(endptr == str + 2);
}

1519 1520
static void test_qemu_strtosz_erange(void)
{
1521
    const char *str;
1522
    char *endptr = NULL;
1523 1524
    int64_t res;

1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545
    str = "-1";
    res = qemu_strtosz(str, &endptr);
    g_assert_cmpint(res, ==, -ERANGE);
    g_assert(endptr == str + 2);

    str = "9223372036854775296"; /* 0x7ffffffffffffe00 */
    res = qemu_strtosz_suffix(str, &endptr, QEMU_STRTOSZ_DEFSUFFIX_B);
    g_assert_cmpint(res, ==, -ERANGE);
    g_assert(endptr == str + 19);

    str = "9223372036854775807"; /* 2^63-1 */
    res = qemu_strtosz_suffix(str, &endptr, QEMU_STRTOSZ_DEFSUFFIX_B);
    g_assert_cmpint(res, ==, -ERANGE);
    g_assert(endptr == str + 19);

    str = "9223372036854775808"; /* 2^63 */
    res = qemu_strtosz_suffix(str, &endptr, QEMU_STRTOSZ_DEFSUFFIX_B);
    g_assert_cmpint(res, ==, -ERANGE);
    g_assert(endptr == str + 19);

    str = "10E";
1546
    res = qemu_strtosz(str, &endptr);
1547
    g_assert_cmpint(res, ==, -ERANGE);
1548
    g_assert(endptr == str + 3);
1549 1550 1551 1552 1553
}

static void test_qemu_strtosz_suffix_unit(void)
{
    const char *str = "12345";
1554
    char *endptr = NULL;
1555 1556
    int64_t res;

1557
    res = qemu_strtosz_suffix_unit(str, &endptr,
1558 1559
                                   QEMU_STRTOSZ_DEFSUFFIX_KB, 1000);
    g_assert_cmpint(res, ==, 12345000);
1560
    g_assert(endptr == str + 5);
1561 1562
}

1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583
int main(int argc, char **argv)
{
    g_test_init(&argc, &argv, NULL);

    g_test_add_func("/cutils/parse_uint/null", test_parse_uint_null);
    g_test_add_func("/cutils/parse_uint/empty", test_parse_uint_empty);
    g_test_add_func("/cutils/parse_uint/whitespace",
                    test_parse_uint_whitespace);
    g_test_add_func("/cutils/parse_uint/invalid", test_parse_uint_invalid);
    g_test_add_func("/cutils/parse_uint/trailing", test_parse_uint_trailing);
    g_test_add_func("/cutils/parse_uint/correct", test_parse_uint_correct);
    g_test_add_func("/cutils/parse_uint/octal", test_parse_uint_octal);
    g_test_add_func("/cutils/parse_uint/decimal", test_parse_uint_decimal);
    g_test_add_func("/cutils/parse_uint/llong_max", test_parse_uint_llong_max);
    g_test_add_func("/cutils/parse_uint/overflow", test_parse_uint_overflow);
    g_test_add_func("/cutils/parse_uint/negative", test_parse_uint_negative);
    g_test_add_func("/cutils/parse_uint_full/trailing",
                    test_parse_uint_full_trailing);
    g_test_add_func("/cutils/parse_uint_full/correct",
                    test_parse_uint_full_correct);

1584
    /* qemu_strtol() tests */
1585 1586 1587 1588 1589 1590
    g_test_add_func("/cutils/qemu_strtol/correct",
                    test_qemu_strtol_correct);
    g_test_add_func("/cutils/qemu_strtol/null",
                    test_qemu_strtol_null);
    g_test_add_func("/cutils/qemu_strtol/empty",
                    test_qemu_strtol_empty);
1591 1592
    g_test_add_func("/cutils/qemu_strtol/whitespace",
                    test_qemu_strtol_whitespace);
1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606
    g_test_add_func("/cutils/qemu_strtol/invalid",
                    test_qemu_strtol_invalid);
    g_test_add_func("/cutils/qemu_strtol/trailing",
                    test_qemu_strtol_trailing);
    g_test_add_func("/cutils/qemu_strtol/octal",
                    test_qemu_strtol_octal);
    g_test_add_func("/cutils/qemu_strtol/decimal",
                    test_qemu_strtol_decimal);
    g_test_add_func("/cutils/qemu_strtol/hex",
                    test_qemu_strtol_hex);
    g_test_add_func("/cutils/qemu_strtol/max",
                    test_qemu_strtol_max);
    g_test_add_func("/cutils/qemu_strtol/overflow",
                    test_qemu_strtol_overflow);
1607 1608
    g_test_add_func("/cutils/qemu_strtol/underflow",
                    test_qemu_strtol_underflow);
1609 1610
    g_test_add_func("/cutils/qemu_strtol/negative",
                    test_qemu_strtol_negative);
1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623
    g_test_add_func("/cutils/qemu_strtol_full/correct",
                    test_qemu_strtol_full_correct);
    g_test_add_func("/cutils/qemu_strtol_full/null",
                    test_qemu_strtol_full_null);
    g_test_add_func("/cutils/qemu_strtol_full/empty",
                    test_qemu_strtol_full_empty);
    g_test_add_func("/cutils/qemu_strtol_full/negative",
                    test_qemu_strtol_full_negative);
    g_test_add_func("/cutils/qemu_strtol_full/trailing",
                    test_qemu_strtol_full_trailing);
    g_test_add_func("/cutils/qemu_strtol_full/max",
                    test_qemu_strtol_full_max);

1624
    /* qemu_strtoul() tests */
1625 1626 1627 1628 1629 1630
    g_test_add_func("/cutils/qemu_strtoul/correct",
                    test_qemu_strtoul_correct);
    g_test_add_func("/cutils/qemu_strtoul/null",
                    test_qemu_strtoul_null);
    g_test_add_func("/cutils/qemu_strtoul/empty",
                    test_qemu_strtoul_empty);
1631 1632
    g_test_add_func("/cutils/qemu_strtoul/whitespace",
                    test_qemu_strtoul_whitespace);
1633 1634
    g_test_add_func("/cutils/qemu_strtoul/invalid",
                    test_qemu_strtoul_invalid);
1635 1636
    g_test_add_func("/cutils/qemu_strtoul/trailing",
                    test_qemu_strtoul_trailing);
1637 1638 1639 1640 1641 1642 1643 1644
    g_test_add_func("/cutils/qemu_strtoul/octal",
                    test_qemu_strtoul_octal);
    g_test_add_func("/cutils/qemu_strtoul/decimal",
                    test_qemu_strtoul_decimal);
    g_test_add_func("/cutils/qemu_strtoul/hex",
                    test_qemu_strtoul_hex);
    g_test_add_func("/cutils/qemu_strtoul/max",
                    test_qemu_strtoul_max);
1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663
    g_test_add_func("/cutils/qemu_strtoul/overflow",
                    test_qemu_strtoul_overflow);
    g_test_add_func("/cutils/qemu_strtoul/underflow",
                    test_qemu_strtoul_underflow);
    g_test_add_func("/cutils/qemu_strtoul/negative",
                    test_qemu_strtoul_negative);
    g_test_add_func("/cutils/qemu_strtoul_full/correct",
                    test_qemu_strtoul_full_correct);
    g_test_add_func("/cutils/qemu_strtoul_full/null",
                    test_qemu_strtoul_full_null);
    g_test_add_func("/cutils/qemu_strtoul_full/empty",
                    test_qemu_strtoul_full_empty);
    g_test_add_func("/cutils/qemu_strtoul_full/negative",
                    test_qemu_strtoul_full_negative);
    g_test_add_func("/cutils/qemu_strtoul_full/trailing",
                    test_qemu_strtoul_full_trailing);
    g_test_add_func("/cutils/qemu_strtoul_full/max",
                    test_qemu_strtoul_full_max);

1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743
    /* qemu_strtoi64() tests */
    g_test_add_func("/cutils/qemu_strtoi64/correct",
                    test_qemu_strtoi64_correct);
    g_test_add_func("/cutils/qemu_strtoi64/null",
                    test_qemu_strtoi64_null);
    g_test_add_func("/cutils/qemu_strtoi64/empty",
                    test_qemu_strtoi64_empty);
    g_test_add_func("/cutils/qemu_strtoi64/whitespace",
                    test_qemu_strtoi64_whitespace);
    g_test_add_func("/cutils/qemu_strtoi64/invalid"
                    ,
                    test_qemu_strtoi64_invalid);
    g_test_add_func("/cutils/qemu_strtoi64/trailing",
                    test_qemu_strtoi64_trailing);
    g_test_add_func("/cutils/qemu_strtoi64/octal",
                    test_qemu_strtoi64_octal);
    g_test_add_func("/cutils/qemu_strtoi64/decimal",
                    test_qemu_strtoi64_decimal);
    g_test_add_func("/cutils/qemu_strtoi64/hex",
                    test_qemu_strtoi64_hex);
    g_test_add_func("/cutils/qemu_strtoi64/max",
                    test_qemu_strtoi64_max);
    g_test_add_func("/cutils/qemu_strtoi64/overflow",
                    test_qemu_strtoi64_overflow);
    g_test_add_func("/cutils/qemu_strtoi64/underflow",
                    test_qemu_strtoi64_underflow);
    g_test_add_func("/cutils/qemu_strtoi64/negative",
                    test_qemu_strtoi64_negative);
    g_test_add_func("/cutils/qemu_strtoi64_full/correct",
                    test_qemu_strtoi64_full_correct);
    g_test_add_func("/cutils/qemu_strtoi64_full/null",
                    test_qemu_strtoi64_full_null);
    g_test_add_func("/cutils/qemu_strtoi64_full/empty",
                    test_qemu_strtoi64_full_empty);
    g_test_add_func("/cutils/qemu_strtoi64_full/negative",
                    test_qemu_strtoi64_full_negative);
    g_test_add_func("/cutils/qemu_strtoi64_full/trailing",
                    test_qemu_strtoi64_full_trailing);
    g_test_add_func("/cutils/qemu_strtoi64_full/max",
                    test_qemu_strtoi64_full_max);

    /* qemu_strtou64() tests */
    g_test_add_func("/cutils/qemu_strtou64/correct",
                    test_qemu_strtou64_correct);
    g_test_add_func("/cutils/qemu_strtou64/null",
                    test_qemu_strtou64_null);
    g_test_add_func("/cutils/qemu_strtou64/empty",
                    test_qemu_strtou64_empty);
    g_test_add_func("/cutils/qemu_strtou64/whitespace",
                    test_qemu_strtou64_whitespace);
    g_test_add_func("/cutils/qemu_strtou64/invalid",
                    test_qemu_strtou64_invalid);
    g_test_add_func("/cutils/qemu_strtou64/trailing",
                    test_qemu_strtou64_trailing);
    g_test_add_func("/cutils/qemu_strtou64/octal",
                    test_qemu_strtou64_octal);
    g_test_add_func("/cutils/qemu_strtou64/decimal",
                    test_qemu_strtou64_decimal);
    g_test_add_func("/cutils/qemu_strtou64/hex",
                    test_qemu_strtou64_hex);
    g_test_add_func("/cutils/qemu_strtou64/max",
                    test_qemu_strtou64_max);
    g_test_add_func("/cutils/qemu_strtou64/overflow",
                    test_qemu_strtou64_overflow);
    g_test_add_func("/cutils/qemu_strtou64/underflow",
                    test_qemu_strtou64_underflow);
    g_test_add_func("/cutils/qemu_strtou64/negative",
                    test_qemu_strtou64_negative);
    g_test_add_func("/cutils/qemu_strtou64_full/correct",
                    test_qemu_strtou64_full_correct);
    g_test_add_func("/cutils/qemu_strtou64_full/null",
                    test_qemu_strtou64_full_null);
    g_test_add_func("/cutils/qemu_strtou64_full/empty",
                    test_qemu_strtou64_full_empty);
    g_test_add_func("/cutils/qemu_strtou64_full/negative",
                    test_qemu_strtou64_full_negative);
    g_test_add_func("/cutils/qemu_strtou64_full/trailing",
                    test_qemu_strtou64_full_trailing);
    g_test_add_func("/cutils/qemu_strtou64_full/max",
                    test_qemu_strtou64_full_max);
1744

1745 1746 1747 1748 1749 1750
    g_test_add_func("/cutils/strtosz/simple",
                    test_qemu_strtosz_simple);
    g_test_add_func("/cutils/strtosz/units",
                    test_qemu_strtosz_units);
    g_test_add_func("/cutils/strtosz/float",
                    test_qemu_strtosz_float);
1751 1752
    g_test_add_func("/cutils/strtosz/invalid",
                    test_qemu_strtosz_invalid);
1753 1754
    g_test_add_func("/cutils/strtosz/trailing",
                    test_qemu_strtosz_trailing);
1755 1756 1757 1758 1759
    g_test_add_func("/cutils/strtosz/erange",
                    test_qemu_strtosz_erange);
    g_test_add_func("/cutils/strtosz/suffix-unit",
                    test_qemu_strtosz_suffix_unit);

1760 1761
    return g_test_run();
}