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