qemu/tests/test-cutils.c
<<
>>
Prefs
   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
  30#include "qemu/cutils.h"
  31
  32static void test_parse_uint_null(void)
  33{
  34    unsigned long long i = 999;
  35    char f = 'X';
  36    char *endptr = &f;
  37    int r;
  38
  39    r = parse_uint(NULL, &i, &endptr, 0);
  40
  41    g_assert_cmpint(r, ==, -EINVAL);
  42    g_assert_cmpint(i, ==, 0);
  43    g_assert(endptr == NULL);
  44}
  45
  46static void test_parse_uint_empty(void)
  47{
  48    unsigned long long i = 999;
  49    char f = 'X';
  50    char *endptr = &f;
  51    const char *str = "";
  52    int r;
  53
  54    r = parse_uint(str, &i, &endptr, 0);
  55
  56    g_assert_cmpint(r, ==, -EINVAL);
  57    g_assert_cmpint(i, ==, 0);
  58    g_assert(endptr == str);
  59}
  60
  61static void test_parse_uint_whitespace(void)
  62{
  63    unsigned long long i = 999;
  64    char f = 'X';
  65    char *endptr = &f;
  66    const char *str = "   \t   ";
  67    int r;
  68
  69    r = parse_uint(str, &i, &endptr, 0);
  70
  71    g_assert_cmpint(r, ==, -EINVAL);
  72    g_assert_cmpint(i, ==, 0);
  73    g_assert(endptr == str);
  74}
  75
  76
  77static void test_parse_uint_invalid(void)
  78{
  79    unsigned long long i = 999;
  80    char f = 'X';
  81    char *endptr = &f;
  82    const char *str = " \t xxx";
  83    int r;
  84
  85    r = parse_uint(str, &i, &endptr, 0);
  86
  87    g_assert_cmpint(r, ==, -EINVAL);
  88    g_assert_cmpint(i, ==, 0);
  89    g_assert(endptr == str);
  90}
  91
  92
  93static void test_parse_uint_trailing(void)
  94{
  95    unsigned long long i = 999;
  96    char f = 'X';
  97    char *endptr = &f;
  98    const char *str = "123xxx";
  99    int r;
 100
 101    r = parse_uint(str, &i, &endptr, 0);
 102
 103    g_assert_cmpint(r, ==, 0);
 104    g_assert_cmpint(i, ==, 123);
 105    g_assert(endptr == str + 3);
 106}
 107
 108static void test_parse_uint_correct(void)
 109{
 110    unsigned long long i = 999;
 111    char f = 'X';
 112    char *endptr = &f;
 113    const char *str = "123";
 114    int r;
 115
 116    r = parse_uint(str, &i, &endptr, 0);
 117
 118    g_assert_cmpint(r, ==, 0);
 119    g_assert_cmpint(i, ==, 123);
 120    g_assert(endptr == str + strlen(str));
 121}
 122
 123static void test_parse_uint_octal(void)
 124{
 125    unsigned long long i = 999;
 126    char f = 'X';
 127    char *endptr = &f;
 128    const char *str = "0123";
 129    int r;
 130
 131    r = parse_uint(str, &i, &endptr, 0);
 132
 133    g_assert_cmpint(r, ==, 0);
 134    g_assert_cmpint(i, ==, 0123);
 135    g_assert(endptr == str + strlen(str));
 136}
 137
 138static void test_parse_uint_decimal(void)
 139{
 140    unsigned long long i = 999;
 141    char f = 'X';
 142    char *endptr = &f;
 143    const char *str = "0123";
 144    int r;
 145
 146    r = parse_uint(str, &i, &endptr, 10);
 147
 148    g_assert_cmpint(r, ==, 0);
 149    g_assert_cmpint(i, ==, 123);
 150    g_assert(endptr == str + strlen(str));
 151}
 152
 153
 154static void test_parse_uint_llong_max(void)
 155{
 156    unsigned long long i = 999;
 157    char f = 'X';
 158    char *endptr = &f;
 159    char *str = g_strdup_printf("%llu", (unsigned long long)LLONG_MAX + 1);
 160    int r;
 161
 162    r = parse_uint(str, &i, &endptr, 0);
 163
 164    g_assert_cmpint(r, ==, 0);
 165    g_assert_cmpint(i, ==, (unsigned long long)LLONG_MAX + 1);
 166    g_assert(endptr == str + strlen(str));
 167
 168    g_free(str);
 169}
 170
 171static void test_parse_uint_overflow(void)
 172{
 173    unsigned long long i = 999;
 174    char f = 'X';
 175    char *endptr = &f;
 176    const char *str = "99999999999999999999999999999999999999";
 177    int r;
 178
 179    r = parse_uint(str, &i, &endptr, 0);
 180
 181    g_assert_cmpint(r, ==, -ERANGE);
 182    g_assert_cmpint(i, ==, ULLONG_MAX);
 183    g_assert(endptr == str + strlen(str));
 184}
 185
 186static void test_parse_uint_negative(void)
 187{
 188    unsigned long long i = 999;
 189    char f = 'X';
 190    char *endptr = &f;
 191    const char *str = " \t -321";
 192    int r;
 193
 194    r = parse_uint(str, &i, &endptr, 0);
 195
 196    g_assert_cmpint(r, ==, -ERANGE);
 197    g_assert_cmpint(i, ==, 0);
 198    g_assert(endptr == str + strlen(str));
 199}
 200
 201
 202static void test_parse_uint_full_trailing(void)
 203{
 204    unsigned long long i = 999;
 205    const char *str = "123xxx";
 206    int r;
 207
 208    r = parse_uint_full(str, &i, 0);
 209
 210    g_assert_cmpint(r, ==, -EINVAL);
 211    g_assert_cmpint(i, ==, 0);
 212}
 213
 214static void test_parse_uint_full_correct(void)
 215{
 216    unsigned long long i = 999;
 217    const char *str = "123";
 218    int r;
 219
 220    r = parse_uint_full(str, &i, 0);
 221
 222    g_assert_cmpint(r, ==, 0);
 223    g_assert_cmpint(i, ==, 123);
 224}
 225
 226static void test_qemu_strtol_correct(void)
 227{
 228    const char *str = "12345 foo";
 229    char f = 'X';
 230    const char *endptr = &f;
 231    long res = 999;
 232    int err;
 233
 234    err = qemu_strtol(str, &endptr, 0, &res);
 235
 236    g_assert_cmpint(err, ==, 0);
 237    g_assert_cmpint(res, ==, 12345);
 238    g_assert(endptr == str + 5);
 239}
 240
 241static void test_qemu_strtol_null(void)
 242{
 243    char f = 'X';
 244    const char *endptr = &f;
 245    long res = 999;
 246    int err;
 247
 248    err = qemu_strtol(NULL, &endptr, 0, &res);
 249
 250    g_assert_cmpint(err, ==, -EINVAL);
 251    g_assert(endptr == NULL);
 252}
 253
 254static void test_qemu_strtol_empty(void)
 255{
 256    const char *str = "";
 257    char f = 'X';
 258    const char *endptr = &f;
 259    long res = 999;
 260    int err;
 261
 262    err = qemu_strtol(str, &endptr, 0, &res);
 263
 264    g_assert_cmpint(err, ==, -EINVAL);
 265    g_assert(endptr == str);
 266}
 267
 268static void test_qemu_strtol_whitespace(void)
 269{
 270    const char *str = "  \t  ";
 271    char f = 'X';
 272    const char *endptr = &f;
 273    long res = 999;
 274    int err;
 275
 276    err = qemu_strtol(str, &endptr, 0, &res);
 277
 278    g_assert_cmpint(err, ==, -EINVAL);
 279    g_assert(endptr == str);
 280}
 281
 282static void test_qemu_strtol_invalid(void)
 283{
 284    const char *str = "   xxxx  \t abc";
 285    char f = 'X';
 286    const char *endptr = &f;
 287    long res = 999;
 288    int err;
 289
 290    err = qemu_strtol(str, &endptr, 0, &res);
 291
 292    g_assert_cmpint(err, ==, -EINVAL);
 293    g_assert(endptr == str);
 294}
 295
 296static void test_qemu_strtol_trailing(void)
 297{
 298    const char *str = "123xxx";
 299    char f = 'X';
 300    const char *endptr = &f;
 301    long res = 999;
 302    int err;
 303
 304    err = qemu_strtol(str, &endptr, 0, &res);
 305
 306    g_assert_cmpint(err, ==, 0);
 307    g_assert_cmpint(res, ==, 123);
 308    g_assert(endptr == str + 3);
 309}
 310
 311static void test_qemu_strtol_octal(void)
 312{
 313    const char *str = "0123";
 314    char f = 'X';
 315    const char *endptr = &f;
 316    long res = 999;
 317    int err;
 318
 319    err = qemu_strtol(str, &endptr, 8, &res);
 320
 321    g_assert_cmpint(err, ==, 0);
 322    g_assert_cmpint(res, ==, 0123);
 323    g_assert(endptr == str + strlen(str));
 324
 325    res = 999;
 326    endptr = &f;
 327    err = qemu_strtol(str, &endptr, 0, &res);
 328
 329    g_assert_cmpint(err, ==, 0);
 330    g_assert_cmpint(res, ==, 0123);
 331    g_assert(endptr == str + strlen(str));
 332}
 333
 334static void test_qemu_strtol_decimal(void)
 335{
 336    const char *str = "0123";
 337    char f = 'X';
 338    const char *endptr = &f;
 339    long res = 999;
 340    int err;
 341
 342    err = qemu_strtol(str, &endptr, 10, &res);
 343
 344    g_assert_cmpint(err, ==, 0);
 345    g_assert_cmpint(res, ==, 123);
 346    g_assert(endptr == str + strlen(str));
 347
 348    str = "123";
 349    res = 999;
 350    endptr = &f;
 351    err = qemu_strtol(str, &endptr, 0, &res);
 352
 353    g_assert_cmpint(err, ==, 0);
 354    g_assert_cmpint(res, ==, 123);
 355    g_assert(endptr == str + strlen(str));
 356}
 357
 358static void test_qemu_strtol_hex(void)
 359{
 360    const char *str = "0123";
 361    char f = 'X';
 362    const char *endptr = &f;
 363    long res = 999;
 364    int err;
 365
 366    err = qemu_strtol(str, &endptr, 16, &res);
 367
 368    g_assert_cmpint(err, ==, 0);
 369    g_assert_cmpint(res, ==, 0x123);
 370    g_assert(endptr == str + strlen(str));
 371
 372    str = "0x123";
 373    res = 999;
 374    endptr = &f;
 375    err = qemu_strtol(str, &endptr, 0, &res);
 376
 377    g_assert_cmpint(err, ==, 0);
 378    g_assert_cmpint(res, ==, 0x123);
 379    g_assert(endptr == str + strlen(str));
 380}
 381
 382static void test_qemu_strtol_max(void)
 383{
 384    char *str = g_strdup_printf("%ld", LONG_MAX);
 385    char f = 'X';
 386    const char *endptr = &f;
 387    long res = 999;
 388    int err;
 389
 390    err = qemu_strtol(str, &endptr, 0, &res);
 391
 392    g_assert_cmpint(err, ==, 0);
 393    g_assert_cmpint(res, ==, LONG_MAX);
 394    g_assert(endptr == str + strlen(str));
 395    g_free(str);
 396}
 397
 398static void test_qemu_strtol_overflow(void)
 399{
 400    const char *str = "99999999999999999999999999999999999999999999";
 401    char f = 'X';
 402    const char *endptr = &f;
 403    long res = 999;
 404    int err;
 405
 406    err = qemu_strtol(str, &endptr, 0, &res);
 407
 408    g_assert_cmpint(err, ==, -ERANGE);
 409    g_assert_cmpint(res, ==, LONG_MAX);
 410    g_assert(endptr == str + strlen(str));
 411}
 412
 413static void test_qemu_strtol_underflow(void)
 414{
 415    const char *str = "-99999999999999999999999999999999999999999999";
 416    char f = 'X';
 417    const char *endptr = &f;
 418    long res = 999;
 419    int err;
 420
 421    err  = qemu_strtol(str, &endptr, 0, &res);
 422
 423    g_assert_cmpint(err, ==, -ERANGE);
 424    g_assert_cmpint(res, ==, LONG_MIN);
 425    g_assert(endptr == str + strlen(str));
 426}
 427
 428static void test_qemu_strtol_negative(void)
 429{
 430    const char *str = "  \t -321";
 431    char f = 'X';
 432    const char *endptr = &f;
 433    long res = 999;
 434    int err;
 435
 436    err = qemu_strtol(str, &endptr, 0, &res);
 437
 438    g_assert_cmpint(err, ==, 0);
 439    g_assert_cmpint(res, ==, -321);
 440    g_assert(endptr == str + strlen(str));
 441}
 442
 443static void test_qemu_strtol_full_correct(void)
 444{
 445    const char *str = "123";
 446    long res = 999;
 447    int err;
 448
 449    err = qemu_strtol(str, NULL, 0, &res);
 450
 451    g_assert_cmpint(err, ==, 0);
 452    g_assert_cmpint(res, ==, 123);
 453}
 454
 455static void test_qemu_strtol_full_null(void)
 456{
 457    char f = 'X';
 458    const char *endptr = &f;
 459    long res = 999;
 460    int err;
 461
 462    err = qemu_strtol(NULL, &endptr, 0, &res);
 463
 464    g_assert_cmpint(err, ==, -EINVAL);
 465    g_assert(endptr == NULL);
 466}
 467
 468static void test_qemu_strtol_full_empty(void)
 469{
 470    const char *str = "";
 471    long res = 999L;
 472    int err;
 473
 474    err =  qemu_strtol(str, NULL, 0, &res);
 475
 476    g_assert_cmpint(err, ==, -EINVAL);
 477}
 478
 479static void test_qemu_strtol_full_negative(void)
 480{
 481    const char *str = " \t -321";
 482    long res = 999;
 483    int err;
 484
 485    err = qemu_strtol(str, NULL, 0, &res);
 486
 487    g_assert_cmpint(err, ==, 0);
 488    g_assert_cmpint(res, ==, -321);
 489}
 490
 491static void test_qemu_strtol_full_trailing(void)
 492{
 493    const char *str = "123xxx";
 494    long res;
 495    int err;
 496
 497    err = qemu_strtol(str, NULL, 0, &res);
 498
 499    g_assert_cmpint(err, ==, -EINVAL);
 500}
 501
 502static void test_qemu_strtol_full_max(void)
 503{
 504    char *str = g_strdup_printf("%ld", LONG_MAX);
 505    long res;
 506    int err;
 507
 508    err = qemu_strtol(str, NULL, 0, &res);
 509
 510    g_assert_cmpint(err, ==, 0);
 511    g_assert_cmpint(res, ==, LONG_MAX);
 512    g_free(str);
 513}
 514
 515static void test_qemu_strtoul_correct(void)
 516{
 517    const char *str = "12345 foo";
 518    char f = 'X';
 519    const char *endptr = &f;
 520    unsigned long res = 999;
 521    int err;
 522
 523    err = qemu_strtoul(str, &endptr, 0, &res);
 524
 525    g_assert_cmpint(err, ==, 0);
 526    g_assert_cmpuint(res, ==, 12345);
 527    g_assert(endptr == str + 5);
 528}
 529
 530static void test_qemu_strtoul_null(void)
 531{
 532    char f = 'X';
 533    const char *endptr = &f;
 534    unsigned long res = 999;
 535    int err;
 536
 537    err = qemu_strtoul(NULL, &endptr, 0, &res);
 538
 539    g_assert_cmpint(err, ==, -EINVAL);
 540    g_assert(endptr == NULL);
 541}
 542
 543static void test_qemu_strtoul_empty(void)
 544{
 545    const char *str = "";
 546    char f = 'X';
 547    const char *endptr = &f;
 548    unsigned long res = 999;
 549    int err;
 550
 551    err = qemu_strtoul(str, &endptr, 0, &res);
 552
 553    g_assert_cmpint(err, ==, -EINVAL);
 554    g_assert(endptr == str);
 555}
 556
 557static void test_qemu_strtoul_whitespace(void)
 558{
 559    const char *str = "  \t  ";
 560    char f = 'X';
 561    const char *endptr = &f;
 562    unsigned long res = 999;
 563    int err;
 564
 565    err = qemu_strtoul(str, &endptr, 0, &res);
 566
 567    g_assert_cmpint(err, ==, -EINVAL);
 568    g_assert(endptr == str);
 569}
 570
 571static void test_qemu_strtoul_invalid(void)
 572{
 573    const char *str = "   xxxx  \t abc";
 574    char f = 'X';
 575    const char *endptr = &f;
 576    unsigned long res = 999;
 577    int err;
 578
 579    err = qemu_strtoul(str, &endptr, 0, &res);
 580
 581    g_assert_cmpint(err, ==, -EINVAL);
 582    g_assert(endptr == str);
 583}
 584
 585static void test_qemu_strtoul_trailing(void)
 586{
 587    const char *str = "123xxx";
 588    char f = 'X';
 589    const char *endptr = &f;
 590    unsigned long res = 999;
 591    int err;
 592
 593    err = qemu_strtoul(str, &endptr, 0, &res);
 594
 595    g_assert_cmpint(err, ==, 0);
 596    g_assert_cmpuint(res, ==, 123);
 597    g_assert(endptr == str + 3);
 598}
 599
 600static void test_qemu_strtoul_octal(void)
 601{
 602    const char *str = "0123";
 603    char f = 'X';
 604    const char *endptr = &f;
 605    unsigned long res = 999;
 606    int err;
 607
 608    err = qemu_strtoul(str, &endptr, 8, &res);
 609
 610    g_assert_cmpint(err, ==, 0);
 611    g_assert_cmpuint(res, ==, 0123);
 612    g_assert(endptr == str + strlen(str));
 613
 614    res = 999;
 615    endptr = &f;
 616    err = qemu_strtoul(str, &endptr, 0, &res);
 617
 618    g_assert_cmpint(err, ==, 0);
 619    g_assert_cmpuint(res, ==, 0123);
 620    g_assert(endptr == str + strlen(str));
 621}
 622
 623static void test_qemu_strtoul_decimal(void)
 624{
 625    const char *str = "0123";
 626    char f = 'X';
 627    const char *endptr = &f;
 628    unsigned long res = 999;
 629    int err;
 630
 631    err = qemu_strtoul(str, &endptr, 10, &res);
 632
 633    g_assert_cmpint(err, ==, 0);
 634    g_assert_cmpuint(res, ==, 123);
 635    g_assert(endptr == str + strlen(str));
 636
 637    str = "123";
 638    res = 999;
 639    endptr = &f;
 640    err = qemu_strtoul(str, &endptr, 0, &res);
 641
 642    g_assert_cmpint(err, ==, 0);
 643    g_assert_cmpuint(res, ==, 123);
 644    g_assert(endptr == str + strlen(str));
 645}
 646
 647static void test_qemu_strtoul_hex(void)
 648{
 649    const char *str = "0123";
 650    char f = 'X';
 651    const char *endptr = &f;
 652    unsigned long res = 999;
 653    int err;
 654
 655    err = qemu_strtoul(str, &endptr, 16, &res);
 656
 657    g_assert_cmpint(err, ==, 0);
 658    g_assert_cmphex(res, ==, 0x123);
 659    g_assert(endptr == str + strlen(str));
 660
 661    str = "0x123";
 662    res = 999;
 663    endptr = &f;
 664    err = qemu_strtoul(str, &endptr, 0, &res);
 665
 666    g_assert_cmpint(err, ==, 0);
 667    g_assert_cmphex(res, ==, 0x123);
 668    g_assert(endptr == str + strlen(str));
 669}
 670
 671static void test_qemu_strtoul_max(void)
 672{
 673    char *str = g_strdup_printf("%lu", ULONG_MAX);
 674    char f = 'X';
 675    const char *endptr = &f;
 676    unsigned long res = 999;
 677    int err;
 678
 679    err = qemu_strtoul(str, &endptr, 0, &res);
 680
 681    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
 687static void test_qemu_strtoul_overflow(void)
 688{
 689    const char *str = "99999999999999999999999999999999999999999999";
 690    char f = 'X';
 691    const char *endptr = &f;
 692    unsigned long res = 999;
 693    int err;
 694
 695    err = qemu_strtoul(str, &endptr, 0, &res);
 696
 697    g_assert_cmpint(err, ==, -ERANGE);
 698    g_assert_cmphex(res, ==, ULONG_MAX);
 699    g_assert(endptr == str + strlen(str));
 700}
 701
 702static void test_qemu_strtoul_underflow(void)
 703{
 704    const char *str = "-99999999999999999999999999999999999999999999";
 705    char f = 'X';
 706    const char *endptr = &f;
 707    unsigned long res = 999;
 708    int err;
 709
 710    err  = qemu_strtoul(str, &endptr, 0, &res);
 711
 712    g_assert_cmpint(err, ==, -ERANGE);
 713    g_assert_cmpuint(res, ==, -1ul);
 714    g_assert(endptr == str + strlen(str));
 715}
 716
 717static void test_qemu_strtoul_negative(void)
 718{
 719    const char *str = "  \t -321";
 720    char f = 'X';
 721    const char *endptr = &f;
 722    unsigned long res = 999;
 723    int err;
 724
 725    err = qemu_strtoul(str, &endptr, 0, &res);
 726
 727    g_assert_cmpint(err, ==, 0);
 728    g_assert_cmpuint(res, ==, -321ul);
 729    g_assert(endptr == str + strlen(str));
 730}
 731
 732static void test_qemu_strtoul_full_correct(void)
 733{
 734    const char *str = "123";
 735    unsigned long res = 999;
 736    int err;
 737
 738    err = qemu_strtoul(str, NULL, 0, &res);
 739
 740    g_assert_cmpint(err, ==, 0);
 741    g_assert_cmpuint(res, ==, 123);
 742}
 743
 744static void test_qemu_strtoul_full_null(void)
 745{
 746    unsigned long res = 999;
 747    int err;
 748
 749    err = qemu_strtoul(NULL, NULL, 0, &res);
 750
 751    g_assert_cmpint(err, ==, -EINVAL);
 752}
 753
 754static void test_qemu_strtoul_full_empty(void)
 755{
 756    const char *str = "";
 757    unsigned long res = 999;
 758    int err;
 759
 760    err = qemu_strtoul(str, NULL, 0, &res);
 761
 762    g_assert_cmpint(err, ==, -EINVAL);
 763}
 764static void test_qemu_strtoul_full_negative(void)
 765{
 766    const char *str = " \t -321";
 767    unsigned long res = 999;
 768    int err;
 769
 770    err = qemu_strtoul(str, NULL, 0, &res);
 771    g_assert_cmpint(err, ==, 0);
 772    g_assert_cmpuint(res, ==, -321ul);
 773}
 774
 775static void test_qemu_strtoul_full_trailing(void)
 776{
 777    const char *str = "123xxx";
 778    unsigned long res;
 779    int err;
 780
 781    err = qemu_strtoul(str, NULL, 0, &res);
 782
 783    g_assert_cmpint(err, ==, -EINVAL);
 784}
 785
 786static void test_qemu_strtoul_full_max(void)
 787{
 788    char *str = g_strdup_printf("%lu", ULONG_MAX);
 789    unsigned long res = 999;
 790    int err;
 791
 792    err = qemu_strtoul(str, NULL, 0, &res);
 793
 794    g_assert_cmpint(err, ==, 0);
 795    g_assert_cmphex(res, ==, ULONG_MAX);
 796    g_free(str);
 797}
 798
 799static void test_qemu_strtoi64_correct(void)
 800{
 801    const char *str = "12345 foo";
 802    char f = 'X';
 803    const char *endptr = &f;
 804    int64_t res = 999;
 805    int err;
 806
 807    err = qemu_strtoi64(str, &endptr, 0, &res);
 808
 809    g_assert_cmpint(err, ==, 0);
 810    g_assert_cmpint(res, ==, 12345);
 811    g_assert(endptr == str + 5);
 812}
 813
 814static void test_qemu_strtoi64_null(void)
 815{
 816    char f = 'X';
 817    const char *endptr = &f;
 818    int64_t res = 999;
 819    int err;
 820
 821    err = qemu_strtoi64(NULL, &endptr, 0, &res);
 822
 823    g_assert_cmpint(err, ==, -EINVAL);
 824    g_assert(endptr == NULL);
 825}
 826
 827static void test_qemu_strtoi64_empty(void)
 828{
 829    const char *str = "";
 830    char f = 'X';
 831    const char *endptr = &f;
 832    int64_t res = 999;
 833    int err;
 834
 835    err = qemu_strtoi64(str, &endptr, 0, &res);
 836
 837    g_assert_cmpint(err, ==, -EINVAL);
 838    g_assert(endptr == str);
 839}
 840
 841static void test_qemu_strtoi64_whitespace(void)
 842{
 843    const char *str = "  \t  ";
 844    char f = 'X';
 845    const char *endptr = &f;
 846    int64_t res = 999;
 847    int err;
 848
 849    err = qemu_strtoi64(str, &endptr, 0, &res);
 850
 851    g_assert_cmpint(err, ==, -EINVAL);
 852    g_assert(endptr == str);
 853}
 854
 855static void test_qemu_strtoi64_invalid(void)
 856{
 857    const char *str = "   xxxx  \t abc";
 858    char f = 'X';
 859    const char *endptr = &f;
 860    int64_t res = 999;
 861    int err;
 862
 863    err = qemu_strtoi64(str, &endptr, 0, &res);
 864
 865    g_assert_cmpint(err, ==, -EINVAL);
 866    g_assert(endptr == str);
 867}
 868
 869static void test_qemu_strtoi64_trailing(void)
 870{
 871    const char *str = "123xxx";
 872    char f = 'X';
 873    const char *endptr = &f;
 874    int64_t res = 999;
 875    int err;
 876
 877    err = qemu_strtoi64(str, &endptr, 0, &res);
 878
 879    g_assert_cmpint(err, ==, 0);
 880    g_assert_cmpint(res, ==, 123);
 881    g_assert(endptr == str + 3);
 882}
 883
 884static void test_qemu_strtoi64_octal(void)
 885{
 886    const char *str = "0123";
 887    char f = 'X';
 888    const char *endptr = &f;
 889    int64_t res = 999;
 890    int err;
 891
 892    err = qemu_strtoi64(str, &endptr, 8, &res);
 893
 894    g_assert_cmpint(err, ==, 0);
 895    g_assert_cmpint(res, ==, 0123);
 896    g_assert(endptr == str + strlen(str));
 897
 898    endptr = &f;
 899    res = 999;
 900    err = qemu_strtoi64(str, &endptr, 0, &res);
 901
 902    g_assert_cmpint(err, ==, 0);
 903    g_assert_cmpint(res, ==, 0123);
 904    g_assert(endptr == str + strlen(str));
 905}
 906
 907static void test_qemu_strtoi64_decimal(void)
 908{
 909    const char *str = "0123";
 910    char f = 'X';
 911    const char *endptr = &f;
 912    int64_t res = 999;
 913    int err;
 914
 915    err = qemu_strtoi64(str, &endptr, 10, &res);
 916
 917    g_assert_cmpint(err, ==, 0);
 918    g_assert_cmpint(res, ==, 123);
 919    g_assert(endptr == str + strlen(str));
 920
 921    str = "123";
 922    endptr = &f;
 923    res = 999;
 924    err = qemu_strtoi64(str, &endptr, 0, &res);
 925
 926    g_assert_cmpint(err, ==, 0);
 927    g_assert_cmpint(res, ==, 123);
 928    g_assert(endptr == str + strlen(str));
 929}
 930
 931static void test_qemu_strtoi64_hex(void)
 932{
 933    const char *str = "0123";
 934    char f = 'X';
 935    const char *endptr = &f;
 936    int64_t res = 999;
 937    int err;
 938
 939    err = qemu_strtoi64(str, &endptr, 16, &res);
 940
 941    g_assert_cmpint(err, ==, 0);
 942    g_assert_cmpint(res, ==, 0x123);
 943    g_assert(endptr == str + strlen(str));
 944
 945    str = "0x123";
 946    endptr = &f;
 947    res = 999;
 948    err = qemu_strtoi64(str, &endptr, 0, &res);
 949
 950    g_assert_cmpint(err, ==, 0);
 951    g_assert_cmpint(res, ==, 0x123);
 952    g_assert(endptr == str + strlen(str));
 953}
 954
 955static void test_qemu_strtoi64_max(void)
 956{
 957    char *str = g_strdup_printf("%lld", LLONG_MAX);
 958    char f = 'X';
 959    const char *endptr = &f;
 960    int64_t res = 999;
 961    int err;
 962
 963    err = qemu_strtoi64(str, &endptr, 0, &res);
 964
 965    g_assert_cmpint(err, ==, 0);
 966    g_assert_cmpint(res, ==, LLONG_MAX);
 967    g_assert(endptr == str + strlen(str));
 968    g_free(str);
 969}
 970
 971static void test_qemu_strtoi64_overflow(void)
 972{
 973    const char *str = "99999999999999999999999999999999999999999999";
 974    char f = 'X';
 975    const char *endptr = &f;
 976    int64_t res = 999;
 977    int err;
 978
 979    err = qemu_strtoi64(str, &endptr, 0, &res);
 980
 981    g_assert_cmpint(err, ==, -ERANGE);
 982    g_assert_cmpint(res, ==, LLONG_MAX);
 983    g_assert(endptr == str + strlen(str));
 984}
 985
 986static void test_qemu_strtoi64_underflow(void)
 987{
 988    const char *str = "-99999999999999999999999999999999999999999999";
 989    char f = 'X';
 990    const char *endptr = &f;
 991    int64_t res = 999;
 992    int err;
 993
 994    err  = qemu_strtoi64(str, &endptr, 0, &res);
 995
 996    g_assert_cmpint(err, ==, -ERANGE);
 997    g_assert_cmpint(res, ==, LLONG_MIN);
 998    g_assert(endptr == str + strlen(str));
 999}
1000
1001static void test_qemu_strtoi64_negative(void)
1002{
1003    const char *str = "  \t -321";
1004    char f = 'X';
1005    const char *endptr = &f;
1006    int64_t res = 999;
1007    int err;
1008
1009    err = qemu_strtoi64(str, &endptr, 0, &res);
1010
1011    g_assert_cmpint(err, ==, 0);
1012    g_assert_cmpint(res, ==, -321);
1013    g_assert(endptr == str + strlen(str));
1014}
1015
1016static void test_qemu_strtoi64_full_correct(void)
1017{
1018    const char *str = "123";
1019    int64_t res = 999;
1020    int err;
1021
1022    err = qemu_strtoi64(str, NULL, 0, &res);
1023
1024    g_assert_cmpint(err, ==, 0);
1025    g_assert_cmpint(res, ==, 123);
1026}
1027
1028static void test_qemu_strtoi64_full_null(void)
1029{
1030    int64_t res = 999;
1031    int err;
1032
1033    err = qemu_strtoi64(NULL, NULL, 0, &res);
1034
1035    g_assert_cmpint(err, ==, -EINVAL);
1036}
1037
1038static void test_qemu_strtoi64_full_empty(void)
1039{
1040    const char *str = "";
1041    int64_t res = 999;
1042    int err;
1043
1044    err = qemu_strtoi64(str, NULL, 0, &res);
1045
1046    g_assert_cmpint(err, ==, -EINVAL);
1047}
1048
1049static void test_qemu_strtoi64_full_negative(void)
1050{
1051    const char *str = " \t -321";
1052    int64_t res = 999;
1053    int err;
1054
1055    err = qemu_strtoi64(str, NULL, 0, &res);
1056
1057    g_assert_cmpint(err, ==, 0);
1058    g_assert_cmpint(res, ==, -321);
1059}
1060
1061static void test_qemu_strtoi64_full_trailing(void)
1062{
1063    const char *str = "123xxx";
1064    int64_t res = 999;
1065    int err;
1066
1067    err = qemu_strtoi64(str, NULL, 0, &res);
1068
1069    g_assert_cmpint(err, ==, -EINVAL);
1070}
1071
1072static void test_qemu_strtoi64_full_max(void)
1073{
1074
1075    char *str = g_strdup_printf("%lld", LLONG_MAX);
1076    int64_t res;
1077    int err;
1078
1079    err = qemu_strtoi64(str, NULL, 0, &res);
1080
1081    g_assert_cmpint(err, ==, 0);
1082    g_assert_cmpint(res, ==, LLONG_MAX);
1083    g_free(str);
1084}
1085
1086static void test_qemu_strtou64_correct(void)
1087{
1088    const char *str = "12345 foo";
1089    char f = 'X';
1090    const char *endptr = &f;
1091    uint64_t res = 999;
1092    int err;
1093
1094    err = qemu_strtou64(str, &endptr, 0, &res);
1095
1096    g_assert_cmpint(err, ==, 0);
1097    g_assert_cmpuint(res, ==, 12345);
1098    g_assert(endptr == str + 5);
1099}
1100
1101static void test_qemu_strtou64_null(void)
1102{
1103    char f = 'X';
1104    const char *endptr = &f;
1105    uint64_t res = 999;
1106    int err;
1107
1108    err = qemu_strtou64(NULL, &endptr, 0, &res);
1109
1110    g_assert_cmpint(err, ==, -EINVAL);
1111    g_assert(endptr == NULL);
1112}
1113
1114static void test_qemu_strtou64_empty(void)
1115{
1116    const char *str = "";
1117    char f = 'X';
1118    const char *endptr = &f;
1119    uint64_t res = 999;
1120    int err;
1121
1122    err = qemu_strtou64(str, &endptr, 0, &res);
1123
1124    g_assert_cmpint(err, ==, -EINVAL);
1125    g_assert(endptr == str);
1126}
1127
1128static void test_qemu_strtou64_whitespace(void)
1129{
1130    const char *str = "  \t  ";
1131    char f = 'X';
1132    const char *endptr = &f;
1133    uint64_t res = 999;
1134    int err;
1135
1136    err = qemu_strtou64(str, &endptr, 0, &res);
1137
1138    g_assert_cmpint(err, ==, -EINVAL);
1139    g_assert(endptr == str);
1140}
1141
1142static void test_qemu_strtou64_invalid(void)
1143{
1144    const char *str = "   xxxx  \t abc";
1145    char f = 'X';
1146    const char *endptr = &f;
1147    uint64_t res = 999;
1148    int err;
1149
1150    err = qemu_strtou64(str, &endptr, 0, &res);
1151
1152    g_assert_cmpint(err, ==, -EINVAL);
1153    g_assert(endptr == str);
1154}
1155
1156static void test_qemu_strtou64_trailing(void)
1157{
1158    const char *str = "123xxx";
1159    char f = 'X';
1160    const char *endptr = &f;
1161    uint64_t res = 999;
1162    int err;
1163
1164    err = qemu_strtou64(str, &endptr, 0, &res);
1165
1166    g_assert_cmpint(err, ==, 0);
1167    g_assert_cmpuint(res, ==, 123);
1168    g_assert(endptr == str + 3);
1169}
1170
1171static void test_qemu_strtou64_octal(void)
1172{
1173    const char *str = "0123";
1174    char f = 'X';
1175    const char *endptr = &f;
1176    uint64_t res = 999;
1177    int err;
1178
1179    err = qemu_strtou64(str, &endptr, 8, &res);
1180
1181    g_assert_cmpint(err, ==, 0);
1182    g_assert_cmpuint(res, ==, 0123);
1183    g_assert(endptr == str + strlen(str));
1184
1185    endptr = &f;
1186    res = 999;
1187    err = qemu_strtou64(str, &endptr, 0, &res);
1188
1189    g_assert_cmpint(err, ==, 0);
1190    g_assert_cmpuint(res, ==, 0123);
1191    g_assert(endptr == str + strlen(str));
1192}
1193
1194static void test_qemu_strtou64_decimal(void)
1195{
1196    const char *str = "0123";
1197    char f = 'X';
1198    const char *endptr = &f;
1199    uint64_t res = 999;
1200    int err;
1201
1202    err = qemu_strtou64(str, &endptr, 10, &res);
1203
1204    g_assert_cmpint(err, ==, 0);
1205    g_assert_cmpuint(res, ==, 123);
1206    g_assert(endptr == str + strlen(str));
1207
1208    str = "123";
1209    endptr = &f;
1210    res = 999;
1211    err = qemu_strtou64(str, &endptr, 0, &res);
1212
1213    g_assert_cmpint(err, ==, 0);
1214    g_assert_cmpuint(res, ==, 123);
1215    g_assert(endptr == str + strlen(str));
1216}
1217
1218static void test_qemu_strtou64_hex(void)
1219{
1220    const char *str = "0123";
1221    char f = 'X';
1222    const char *endptr = &f;
1223    uint64_t res = 999;
1224    int err;
1225
1226    err = qemu_strtou64(str, &endptr, 16, &res);
1227
1228    g_assert_cmpint(err, ==, 0);
1229    g_assert_cmphex(res, ==, 0x123);
1230    g_assert(endptr == str + strlen(str));
1231
1232    str = "0x123";
1233    endptr = &f;
1234    res = 999;
1235    err = qemu_strtou64(str, &endptr, 0, &res);
1236
1237    g_assert_cmpint(err, ==, 0);
1238    g_assert_cmphex(res, ==, 0x123);
1239    g_assert(endptr == str + strlen(str));
1240}
1241
1242static void test_qemu_strtou64_max(void)
1243{
1244    char *str = g_strdup_printf("%llu", ULLONG_MAX);
1245    char f = 'X';
1246    const char *endptr = &f;
1247    uint64_t res = 999;
1248    int err;
1249
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
1258static void test_qemu_strtou64_overflow(void)
1259{
1260    const char *str = "99999999999999999999999999999999999999999999";
1261    char f = 'X';
1262    const char *endptr = &f;
1263    uint64_t res = 999;
1264    int err;
1265
1266    err = qemu_strtou64(str, &endptr, 0, &res);
1267
1268    g_assert_cmpint(err, ==, -ERANGE);
1269    g_assert_cmphex(res, ==, ULLONG_MAX);
1270    g_assert(endptr == str + strlen(str));
1271}
1272
1273static void test_qemu_strtou64_underflow(void)
1274{
1275    const char *str = "-99999999999999999999999999999999999999999999";
1276    char f = 'X';
1277    const char *endptr = &f;
1278    uint64_t res = 999;
1279    int err;
1280
1281    err  = qemu_strtou64(str, &endptr, 0, &res);
1282
1283    g_assert_cmpint(err, ==, -ERANGE);
1284    g_assert_cmphex(res, ==, -1ull);
1285    g_assert(endptr == str + strlen(str));
1286}
1287
1288static void test_qemu_strtou64_negative(void)
1289{
1290    const char *str = "  \t -321";
1291    char f = 'X';
1292    const char *endptr = &f;
1293    uint64_t res = 999;
1294    int err;
1295
1296    err = qemu_strtou64(str, &endptr, 0, &res);
1297
1298    g_assert_cmpint(err, ==, 0);
1299    g_assert_cmpuint(res, ==, -321ull);
1300    g_assert(endptr == str + strlen(str));
1301}
1302
1303static void test_qemu_strtou64_full_correct(void)
1304{
1305    const char *str = "18446744073709551614";
1306    uint64_t res = 999;
1307    int err;
1308
1309    err = qemu_strtou64(str, NULL, 0, &res);
1310
1311    g_assert_cmpint(err, ==, 0);
1312    g_assert_cmpuint(res, ==, 18446744073709551614ull);
1313}
1314
1315static void test_qemu_strtou64_full_null(void)
1316{
1317    uint64_t res = 999;
1318    int err;
1319
1320    err = qemu_strtou64(NULL, NULL, 0, &res);
1321
1322    g_assert_cmpint(err, ==, -EINVAL);
1323}
1324
1325static void test_qemu_strtou64_full_empty(void)
1326{
1327    const char *str = "";
1328    uint64_t res = 999;
1329    int err;
1330
1331    err = qemu_strtou64(str, NULL, 0, &res);
1332
1333    g_assert_cmpint(err, ==, -EINVAL);
1334}
1335
1336static void test_qemu_strtou64_full_negative(void)
1337{
1338    const char *str = " \t -321";
1339    uint64_t res = 999;
1340    int err;
1341
1342    err = qemu_strtou64(str, NULL, 0, &res);
1343
1344    g_assert_cmpint(err, ==, 0);
1345    g_assert_cmpuint(res, ==, -321ull);
1346}
1347
1348static void test_qemu_strtou64_full_trailing(void)
1349{
1350    const char *str = "18446744073709551614xxxxxx";
1351    uint64_t res = 999;
1352    int err;
1353
1354    err = qemu_strtou64(str, NULL, 0, &res);
1355
1356    g_assert_cmpint(err, ==, -EINVAL);
1357}
1358
1359static void test_qemu_strtou64_full_max(void)
1360{
1361    char *str = g_strdup_printf("%lld", ULLONG_MAX);
1362    uint64_t res = 999;
1363    int err;
1364
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
1372static void test_qemu_strtosz_simple(void)
1373{
1374    const char *str;
1375    char *endptr = NULL;
1376    int err;
1377    uint64_t res = 0xbaadf00d;
1378
1379    str = "0";
1380    err = qemu_strtosz(str, &endptr, &res);
1381    g_assert_cmpint(err, ==, 0);
1382    g_assert_cmpint(res, ==, 0);
1383    g_assert(endptr == str + 1);
1384
1385    str = "12345";
1386    err = qemu_strtosz(str, &endptr, &res);
1387    g_assert_cmpint(err, ==, 0);
1388    g_assert_cmpint(res, ==, 12345);
1389    g_assert(endptr == str + 5);
1390
1391    err = qemu_strtosz(str, NULL, &res);
1392    g_assert_cmpint(err, ==, 0);
1393    g_assert_cmpint(res, ==, 12345);
1394
1395    /* Note: precision is 53 bits since we're parsing with strtod() */
1396
1397    str = "9007199254740991"; /* 2^53-1 */
1398    err = qemu_strtosz(str, &endptr, &res);
1399    g_assert_cmpint(err, ==, 0);
1400    g_assert_cmpint(res, ==, 0x1fffffffffffff);
1401    g_assert(endptr == str + 16);
1402
1403    str = "9007199254740992"; /* 2^53 */
1404    err = qemu_strtosz(str, &endptr, &res);
1405    g_assert_cmpint(err, ==, 0);
1406    g_assert_cmpint(res, ==, 0x20000000000000);
1407    g_assert(endptr == str + 16);
1408
1409    str = "9007199254740993"; /* 2^53+1 */
1410    err = qemu_strtosz(str, &endptr, &res);
1411    g_assert_cmpint(err, ==, 0);
1412    g_assert_cmpint(res, ==, 0x20000000000000); /* rounded to 53 bits */
1413    g_assert(endptr == str + 16);
1414
1415    str = "18446744073709549568"; /* 0xfffffffffffff800 (53 msbs set) */
1416    err = qemu_strtosz(str, &endptr, &res);
1417    g_assert_cmpint(err, ==, 0);
1418    g_assert_cmpint(res, ==, 0xfffffffffffff800);
1419    g_assert(endptr == str + 20);
1420
1421    str = "18446744073709550591"; /* 0xfffffffffffffbff */
1422    err = qemu_strtosz(str, &endptr, &res);
1423    g_assert_cmpint(err, ==, 0);
1424    g_assert_cmpint(res, ==, 0xfffffffffffff800); /* rounded to 53 bits */
1425    g_assert(endptr == str + 20);
1426
1427    /* 0x7ffffffffffffe00..0x7fffffffffffffff get rounded to
1428     * 0x8000000000000000, thus -ERANGE; see test_qemu_strtosz_erange() */
1429}
1430
1431static void test_qemu_strtosz_units(void)
1432{
1433    const char *none = "1";
1434    const char *b = "1B";
1435    const char *k = "1K";
1436    const char *m = "1M";
1437    const char *g = "1G";
1438    const char *t = "1T";
1439    const char *p = "1P";
1440    const char *e = "1E";
1441    int err;
1442    char *endptr = NULL;
1443    uint64_t res = 0xbaadf00d;
1444
1445    /* default is M */
1446    err = qemu_strtosz_MiB(none, &endptr, &res);
1447    g_assert_cmpint(err, ==, 0);
1448    g_assert_cmpint(res, ==, M_BYTE);
1449    g_assert(endptr == none + 1);
1450
1451    err = qemu_strtosz(b, &endptr, &res);
1452    g_assert_cmpint(err, ==, 0);
1453    g_assert_cmpint(res, ==, 1);
1454    g_assert(endptr == b + 2);
1455
1456    err = qemu_strtosz(k, &endptr, &res);
1457    g_assert_cmpint(err, ==, 0);
1458    g_assert_cmpint(res, ==, K_BYTE);
1459    g_assert(endptr == k + 2);
1460
1461    err = qemu_strtosz(m, &endptr, &res);
1462    g_assert_cmpint(err, ==, 0);
1463    g_assert_cmpint(res, ==, M_BYTE);
1464    g_assert(endptr == m + 2);
1465
1466    err = qemu_strtosz(g, &endptr, &res);
1467    g_assert_cmpint(err, ==, 0);
1468    g_assert_cmpint(res, ==, G_BYTE);
1469    g_assert(endptr == g + 2);
1470
1471    err = qemu_strtosz(t, &endptr, &res);
1472    g_assert_cmpint(err, ==, 0);
1473    g_assert_cmpint(res, ==, T_BYTE);
1474    g_assert(endptr == t + 2);
1475
1476    err = qemu_strtosz(p, &endptr, &res);
1477    g_assert_cmpint(err, ==, 0);
1478    g_assert_cmpint(res, ==, P_BYTE);
1479    g_assert(endptr == p + 2);
1480
1481    err = qemu_strtosz(e, &endptr, &res);
1482    g_assert_cmpint(err, ==, 0);
1483    g_assert_cmpint(res, ==, E_BYTE);
1484    g_assert(endptr == e + 2);
1485}
1486
1487static void test_qemu_strtosz_float(void)
1488{
1489    const char *str = "12.345M";
1490    int err;
1491    char *endptr = NULL;
1492    uint64_t res = 0xbaadf00d;
1493
1494    err = qemu_strtosz(str, &endptr, &res);
1495    g_assert_cmpint(err, ==, 0);
1496    g_assert_cmpint(res, ==, 12.345 * M_BYTE);
1497    g_assert(endptr == str + 7);
1498}
1499
1500static void test_qemu_strtosz_invalid(void)
1501{
1502    const char *str;
1503    char *endptr = NULL;
1504    int err;
1505    uint64_t res = 0xbaadf00d;
1506
1507    str = "";
1508    err = qemu_strtosz(str, &endptr, &res);
1509    g_assert_cmpint(err, ==, -EINVAL);
1510    g_assert(endptr == str);
1511
1512    str = " \t ";
1513    err = qemu_strtosz(str, &endptr, &res);
1514    g_assert_cmpint(err, ==, -EINVAL);
1515    g_assert(endptr == str);
1516
1517    str = "crap";
1518    err = qemu_strtosz(str, &endptr, &res);
1519    g_assert_cmpint(err, ==, -EINVAL);
1520    g_assert(endptr == str);
1521}
1522
1523static void test_qemu_strtosz_trailing(void)
1524{
1525    const char *str;
1526    char *endptr = NULL;
1527    int err;
1528    uint64_t res = 0xbaadf00d;
1529
1530    str = "123xxx";
1531    err = qemu_strtosz_MiB(str, &endptr, &res);
1532    g_assert_cmpint(res, ==, 123 * M_BYTE);
1533    g_assert(endptr == str + 3);
1534
1535    err = qemu_strtosz(str, NULL, &res);
1536    g_assert_cmpint(err, ==, -EINVAL);
1537
1538    str = "1kiB";
1539    err = qemu_strtosz(str, &endptr, &res);
1540    g_assert_cmpint(err, ==, 0);
1541    g_assert_cmpint(res, ==, 1024);
1542    g_assert(endptr == str + 2);
1543
1544    err = qemu_strtosz(str, NULL, &res);
1545    g_assert_cmpint(err, ==, -EINVAL);
1546}
1547
1548static void test_qemu_strtosz_erange(void)
1549{
1550    const char *str;
1551    char *endptr = NULL;
1552    int err;
1553    uint64_t res = 0xbaadf00d;
1554
1555    str = "-1";
1556    err = qemu_strtosz(str, &endptr, &res);
1557    g_assert_cmpint(err, ==, -ERANGE);
1558    g_assert(endptr == str + 2);
1559
1560    str = "18446744073709550592"; /* 0xfffffffffffffc00 */
1561    err = qemu_strtosz(str, &endptr, &res);
1562    g_assert_cmpint(err, ==, -ERANGE);
1563    g_assert(endptr == str + 20);
1564
1565    str = "18446744073709551615"; /* 2^64-1 */
1566    err = qemu_strtosz(str, &endptr, &res);
1567    g_assert_cmpint(err, ==, -ERANGE);
1568    g_assert(endptr == str + 20);
1569
1570    str = "18446744073709551616"; /* 2^64 */
1571    err = qemu_strtosz(str, &endptr, &res);
1572    g_assert_cmpint(err, ==, -ERANGE);
1573    g_assert(endptr == str + 20);
1574
1575    str = "20E";
1576    err = qemu_strtosz(str, &endptr, &res);
1577    g_assert_cmpint(err, ==, -ERANGE);
1578    g_assert(endptr == str + 3);
1579}
1580
1581static void test_qemu_strtosz_metric(void)
1582{
1583    const char *str = "12345k";
1584    int err;
1585    char *endptr = NULL;
1586    uint64_t res = 0xbaadf00d;
1587
1588    err = qemu_strtosz_metric(str, &endptr, &res);
1589    g_assert_cmpint(err, ==, 0);
1590    g_assert_cmpint(res, ==, 12345000);
1591    g_assert(endptr == str + 6);
1592}
1593
1594int main(int argc, char **argv)
1595{
1596    g_test_init(&argc, &argv, NULL);
1597
1598    g_test_add_func("/cutils/parse_uint/null", test_parse_uint_null);
1599    g_test_add_func("/cutils/parse_uint/empty", test_parse_uint_empty);
1600    g_test_add_func("/cutils/parse_uint/whitespace",
1601                    test_parse_uint_whitespace);
1602    g_test_add_func("/cutils/parse_uint/invalid", test_parse_uint_invalid);
1603    g_test_add_func("/cutils/parse_uint/trailing", test_parse_uint_trailing);
1604    g_test_add_func("/cutils/parse_uint/correct", test_parse_uint_correct);
1605    g_test_add_func("/cutils/parse_uint/octal", test_parse_uint_octal);
1606    g_test_add_func("/cutils/parse_uint/decimal", test_parse_uint_decimal);
1607    g_test_add_func("/cutils/parse_uint/llong_max", test_parse_uint_llong_max);
1608    g_test_add_func("/cutils/parse_uint/overflow", test_parse_uint_overflow);
1609    g_test_add_func("/cutils/parse_uint/negative", test_parse_uint_negative);
1610    g_test_add_func("/cutils/parse_uint_full/trailing",
1611                    test_parse_uint_full_trailing);
1612    g_test_add_func("/cutils/parse_uint_full/correct",
1613                    test_parse_uint_full_correct);
1614
1615    /* qemu_strtol() tests */
1616    g_test_add_func("/cutils/qemu_strtol/correct",
1617                    test_qemu_strtol_correct);
1618    g_test_add_func("/cutils/qemu_strtol/null",
1619                    test_qemu_strtol_null);
1620    g_test_add_func("/cutils/qemu_strtol/empty",
1621                    test_qemu_strtol_empty);
1622    g_test_add_func("/cutils/qemu_strtol/whitespace",
1623                    test_qemu_strtol_whitespace);
1624    g_test_add_func("/cutils/qemu_strtol/invalid",
1625                    test_qemu_strtol_invalid);
1626    g_test_add_func("/cutils/qemu_strtol/trailing",
1627                    test_qemu_strtol_trailing);
1628    g_test_add_func("/cutils/qemu_strtol/octal",
1629                    test_qemu_strtol_octal);
1630    g_test_add_func("/cutils/qemu_strtol/decimal",
1631                    test_qemu_strtol_decimal);
1632    g_test_add_func("/cutils/qemu_strtol/hex",
1633                    test_qemu_strtol_hex);
1634    g_test_add_func("/cutils/qemu_strtol/max",
1635                    test_qemu_strtol_max);
1636    g_test_add_func("/cutils/qemu_strtol/overflow",
1637                    test_qemu_strtol_overflow);
1638    g_test_add_func("/cutils/qemu_strtol/underflow",
1639                    test_qemu_strtol_underflow);
1640    g_test_add_func("/cutils/qemu_strtol/negative",
1641                    test_qemu_strtol_negative);
1642    g_test_add_func("/cutils/qemu_strtol_full/correct",
1643                    test_qemu_strtol_full_correct);
1644    g_test_add_func("/cutils/qemu_strtol_full/null",
1645                    test_qemu_strtol_full_null);
1646    g_test_add_func("/cutils/qemu_strtol_full/empty",
1647                    test_qemu_strtol_full_empty);
1648    g_test_add_func("/cutils/qemu_strtol_full/negative",
1649                    test_qemu_strtol_full_negative);
1650    g_test_add_func("/cutils/qemu_strtol_full/trailing",
1651                    test_qemu_strtol_full_trailing);
1652    g_test_add_func("/cutils/qemu_strtol_full/max",
1653                    test_qemu_strtol_full_max);
1654
1655    /* qemu_strtoul() tests */
1656    g_test_add_func("/cutils/qemu_strtoul/correct",
1657                    test_qemu_strtoul_correct);
1658    g_test_add_func("/cutils/qemu_strtoul/null",
1659                    test_qemu_strtoul_null);
1660    g_test_add_func("/cutils/qemu_strtoul/empty",
1661                    test_qemu_strtoul_empty);
1662    g_test_add_func("/cutils/qemu_strtoul/whitespace",
1663                    test_qemu_strtoul_whitespace);
1664    g_test_add_func("/cutils/qemu_strtoul/invalid",
1665                    test_qemu_strtoul_invalid);
1666    g_test_add_func("/cutils/qemu_strtoul/trailing",
1667                    test_qemu_strtoul_trailing);
1668    g_test_add_func("/cutils/qemu_strtoul/octal",
1669                    test_qemu_strtoul_octal);
1670    g_test_add_func("/cutils/qemu_strtoul/decimal",
1671                    test_qemu_strtoul_decimal);
1672    g_test_add_func("/cutils/qemu_strtoul/hex",
1673                    test_qemu_strtoul_hex);
1674    g_test_add_func("/cutils/qemu_strtoul/max",
1675                    test_qemu_strtoul_max);
1676    g_test_add_func("/cutils/qemu_strtoul/overflow",
1677                    test_qemu_strtoul_overflow);
1678    g_test_add_func("/cutils/qemu_strtoul/underflow",
1679                    test_qemu_strtoul_underflow);
1680    g_test_add_func("/cutils/qemu_strtoul/negative",
1681                    test_qemu_strtoul_negative);
1682    g_test_add_func("/cutils/qemu_strtoul_full/correct",
1683                    test_qemu_strtoul_full_correct);
1684    g_test_add_func("/cutils/qemu_strtoul_full/null",
1685                    test_qemu_strtoul_full_null);
1686    g_test_add_func("/cutils/qemu_strtoul_full/empty",
1687                    test_qemu_strtoul_full_empty);
1688    g_test_add_func("/cutils/qemu_strtoul_full/negative",
1689                    test_qemu_strtoul_full_negative);
1690    g_test_add_func("/cutils/qemu_strtoul_full/trailing",
1691                    test_qemu_strtoul_full_trailing);
1692    g_test_add_func("/cutils/qemu_strtoul_full/max",
1693                    test_qemu_strtoul_full_max);
1694
1695    /* qemu_strtoi64() tests */
1696    g_test_add_func("/cutils/qemu_strtoi64/correct",
1697                    test_qemu_strtoi64_correct);
1698    g_test_add_func("/cutils/qemu_strtoi64/null",
1699                    test_qemu_strtoi64_null);
1700    g_test_add_func("/cutils/qemu_strtoi64/empty",
1701                    test_qemu_strtoi64_empty);
1702    g_test_add_func("/cutils/qemu_strtoi64/whitespace",
1703                    test_qemu_strtoi64_whitespace);
1704    g_test_add_func("/cutils/qemu_strtoi64/invalid"
1705                    ,
1706                    test_qemu_strtoi64_invalid);
1707    g_test_add_func("/cutils/qemu_strtoi64/trailing",
1708                    test_qemu_strtoi64_trailing);
1709    g_test_add_func("/cutils/qemu_strtoi64/octal",
1710                    test_qemu_strtoi64_octal);
1711    g_test_add_func("/cutils/qemu_strtoi64/decimal",
1712                    test_qemu_strtoi64_decimal);
1713    g_test_add_func("/cutils/qemu_strtoi64/hex",
1714                    test_qemu_strtoi64_hex);
1715    g_test_add_func("/cutils/qemu_strtoi64/max",
1716                    test_qemu_strtoi64_max);
1717    g_test_add_func("/cutils/qemu_strtoi64/overflow",
1718                    test_qemu_strtoi64_overflow);
1719    g_test_add_func("/cutils/qemu_strtoi64/underflow",
1720                    test_qemu_strtoi64_underflow);
1721    g_test_add_func("/cutils/qemu_strtoi64/negative",
1722                    test_qemu_strtoi64_negative);
1723    g_test_add_func("/cutils/qemu_strtoi64_full/correct",
1724                    test_qemu_strtoi64_full_correct);
1725    g_test_add_func("/cutils/qemu_strtoi64_full/null",
1726                    test_qemu_strtoi64_full_null);
1727    g_test_add_func("/cutils/qemu_strtoi64_full/empty",
1728                    test_qemu_strtoi64_full_empty);
1729    g_test_add_func("/cutils/qemu_strtoi64_full/negative",
1730                    test_qemu_strtoi64_full_negative);
1731    g_test_add_func("/cutils/qemu_strtoi64_full/trailing",
1732                    test_qemu_strtoi64_full_trailing);
1733    g_test_add_func("/cutils/qemu_strtoi64_full/max",
1734                    test_qemu_strtoi64_full_max);
1735
1736    /* qemu_strtou64() tests */
1737    g_test_add_func("/cutils/qemu_strtou64/correct",
1738                    test_qemu_strtou64_correct);
1739    g_test_add_func("/cutils/qemu_strtou64/null",
1740                    test_qemu_strtou64_null);
1741    g_test_add_func("/cutils/qemu_strtou64/empty",
1742                    test_qemu_strtou64_empty);
1743    g_test_add_func("/cutils/qemu_strtou64/whitespace",
1744                    test_qemu_strtou64_whitespace);
1745    g_test_add_func("/cutils/qemu_strtou64/invalid",
1746                    test_qemu_strtou64_invalid);
1747    g_test_add_func("/cutils/qemu_strtou64/trailing",
1748                    test_qemu_strtou64_trailing);
1749    g_test_add_func("/cutils/qemu_strtou64/octal",
1750                    test_qemu_strtou64_octal);
1751    g_test_add_func("/cutils/qemu_strtou64/decimal",
1752                    test_qemu_strtou64_decimal);
1753    g_test_add_func("/cutils/qemu_strtou64/hex",
1754                    test_qemu_strtou64_hex);
1755    g_test_add_func("/cutils/qemu_strtou64/max",
1756                    test_qemu_strtou64_max);
1757    g_test_add_func("/cutils/qemu_strtou64/overflow",
1758                    test_qemu_strtou64_overflow);
1759    g_test_add_func("/cutils/qemu_strtou64/underflow",
1760                    test_qemu_strtou64_underflow);
1761    g_test_add_func("/cutils/qemu_strtou64/negative",
1762                    test_qemu_strtou64_negative);
1763    g_test_add_func("/cutils/qemu_strtou64_full/correct",
1764                    test_qemu_strtou64_full_correct);
1765    g_test_add_func("/cutils/qemu_strtou64_full/null",
1766                    test_qemu_strtou64_full_null);
1767    g_test_add_func("/cutils/qemu_strtou64_full/empty",
1768                    test_qemu_strtou64_full_empty);
1769    g_test_add_func("/cutils/qemu_strtou64_full/negative",
1770                    test_qemu_strtou64_full_negative);
1771    g_test_add_func("/cutils/qemu_strtou64_full/trailing",
1772                    test_qemu_strtou64_full_trailing);
1773    g_test_add_func("/cutils/qemu_strtou64_full/max",
1774                    test_qemu_strtou64_full_max);
1775
1776    g_test_add_func("/cutils/strtosz/simple",
1777                    test_qemu_strtosz_simple);
1778    g_test_add_func("/cutils/strtosz/units",
1779                    test_qemu_strtosz_units);
1780    g_test_add_func("/cutils/strtosz/float",
1781                    test_qemu_strtosz_float);
1782    g_test_add_func("/cutils/strtosz/invalid",
1783                    test_qemu_strtosz_invalid);
1784    g_test_add_func("/cutils/strtosz/trailing",
1785                    test_qemu_strtosz_trailing);
1786    g_test_add_func("/cutils/strtosz/erange",
1787                    test_qemu_strtosz_erange);
1788    g_test_add_func("/cutils/strtosz/metric",
1789                    test_qemu_strtosz_metric);
1790
1791    return g_test_run();
1792}
1793