7 * Eduardo Habkost <ehabkost@redhat.com>
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:
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
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
28 #include "qemu/osdep.h"
29 #include "qemu/cutils.h"
30 #include "qemu/units.h"
32 static void test_parse_uint_null(void)
34 unsigned long long i
= 999;
39 r
= parse_uint(NULL
, &i
, &endptr
, 0);
41 g_assert_cmpint(r
, ==, -EINVAL
);
42 g_assert_cmpuint(i
, ==, 0);
43 g_assert_null(endptr
);
46 static void test_parse_uint_empty(void)
48 unsigned long long i
= 999;
54 r
= parse_uint(str
, &i
, &endptr
, 0);
56 g_assert_cmpint(r
, ==, -EINVAL
);
57 g_assert_cmpuint(i
, ==, 0);
58 g_assert_true(endptr
== str
);
61 static void test_parse_uint_whitespace(void)
63 unsigned long long i
= 999;
66 const char *str
= " \t ";
69 r
= parse_uint(str
, &i
, &endptr
, 0);
71 g_assert_cmpint(r
, ==, -EINVAL
);
72 g_assert_cmpuint(i
, ==, 0);
73 g_assert_true(endptr
== str
);
77 static void test_parse_uint_invalid(void)
79 unsigned long long i
= 999;
82 const char *str
= " \t xxx";
85 r
= parse_uint(str
, &i
, &endptr
, 0);
87 g_assert_cmpint(r
, ==, -EINVAL
);
88 g_assert_cmpuint(i
, ==, 0);
89 g_assert_true(endptr
== str
);
93 static void test_parse_uint_trailing(void)
95 unsigned long long i
= 999;
98 const char *str
= "123xxx";
101 r
= parse_uint(str
, &i
, &endptr
, 0);
103 g_assert_cmpint(r
, ==, 0);
104 g_assert_cmpuint(i
, ==, 123);
105 g_assert_true(endptr
== str
+ 3);
108 static void test_parse_uint_correct(void)
110 unsigned long long i
= 999;
113 const char *str
= "123";
116 r
= parse_uint(str
, &i
, &endptr
, 0);
118 g_assert_cmpint(r
, ==, 0);
119 g_assert_cmpuint(i
, ==, 123);
120 g_assert_true(endptr
== str
+ strlen(str
));
123 static void test_parse_uint_octal(void)
125 unsigned long long i
= 999;
128 const char *str
= "0123";
131 r
= parse_uint(str
, &i
, &endptr
, 0);
133 g_assert_cmpint(r
, ==, 0);
134 g_assert_cmpuint(i
, ==, 0123);
135 g_assert_true(endptr
== str
+ strlen(str
));
138 static void test_parse_uint_decimal(void)
140 unsigned long long i
= 999;
143 const char *str
= "0123";
146 r
= parse_uint(str
, &i
, &endptr
, 10);
148 g_assert_cmpint(r
, ==, 0);
149 g_assert_cmpuint(i
, ==, 123);
150 g_assert_true(endptr
== str
+ strlen(str
));
153 static void test_parse_uint_llong_max(void)
155 unsigned long long i
= 999;
158 char *str
= g_strdup_printf("%llu", (unsigned long long)LLONG_MAX
+ 1);
161 r
= parse_uint(str
, &i
, &endptr
, 0);
163 g_assert_cmpint(r
, ==, 0);
164 g_assert_cmpuint(i
, ==, (unsigned long long)LLONG_MAX
+ 1);
165 g_assert_true(endptr
== str
+ strlen(str
));
170 static void test_parse_uint_max(void)
172 unsigned long long i
= 999;
175 char *str
= g_strdup_printf("%llu", ULLONG_MAX
);
178 r
= parse_uint(str
, &i
, &endptr
, 0);
180 g_assert_cmpint(r
, ==, 0);
181 g_assert_cmpuint(i
, ==, ULLONG_MAX
);
182 g_assert_true(endptr
== str
+ strlen(str
));
187 static void test_parse_uint_overflow(void)
189 unsigned long long i
;
197 str
= "99999999999999999999999999999999999999";
198 r
= parse_uint(str
, &i
, &endptr
, 0);
199 g_assert_cmpint(r
, ==, -ERANGE
);
200 g_assert_cmpuint(i
, ==, ULLONG_MAX
);
201 g_assert_true(endptr
== str
+ strlen(str
));
205 str
= "0x10000000000000000"; /* 65 bits, 64-bit sign bit clear */
206 r
= parse_uint(str
, &i
, &endptr
, 0);
207 g_assert_cmpint(r
, ==, -ERANGE
);
208 g_assert_cmpuint(i
, ==, ULLONG_MAX
);
209 g_assert_true(endptr
== str
+ strlen(str
));
213 str
= "0x18000000080000000"; /* 65 bits, 64-bit sign bit set */
214 r
= parse_uint(str
, &i
, &endptr
, 0);
215 g_assert_cmpint(r
, ==, -ERANGE
);
216 g_assert_cmpuint(i
, ==, ULLONG_MAX
);
217 g_assert_true(endptr
== str
+ strlen(str
));
220 static void test_parse_uint_negative(void)
222 unsigned long long i
;
231 r
= parse_uint(str
, &i
, &endptr
, 0);
232 g_assert_cmpint(r
, ==, -ERANGE
);
233 g_assert_cmpuint(i
, ==, 0);
234 g_assert_true(endptr
== str
+ strlen(str
));
238 str
= "-0xffffffff00000001";
239 r
= parse_uint(str
, &i
, &endptr
, 0);
240 g_assert_cmpint(r
, ==, -ERANGE
);
241 g_assert_cmpuint(i
, ==, 0);
242 g_assert_true(endptr
== str
+ strlen(str
));
245 static void test_parse_uint_negzero(void)
247 unsigned long long i
= 999;
250 const char *str
= " -0";
253 r
= parse_uint(str
, &i
, &endptr
, 0);
255 g_assert_cmpint(r
, ==, -ERANGE
);
256 g_assert_cmpuint(i
, ==, 0);
257 g_assert_true(endptr
== str
+ strlen(str
));
260 static void test_parse_uint_full_trailing(void)
262 unsigned long long i
= 999;
263 const char *str
= "123xxx";
266 r
= parse_uint_full(str
, &i
, 0);
268 g_assert_cmpint(r
, ==, -EINVAL
);
269 g_assert_cmpuint(i
, ==, 0);
272 static void test_parse_uint_full_correct(void)
274 unsigned long long i
= 999;
275 const char *str
= "123";
278 r
= parse_uint_full(str
, &i
, 0);
280 g_assert_cmpint(r
, ==, 0);
281 g_assert_cmpuint(i
, ==, 123);
284 static void test_parse_uint_full_erange_junk(void)
286 /* FIXME - inconsistent with qemu_strto* which favors EINVAL */
287 unsigned long long i
= 999;
288 const char *str
= "-2junk";
291 r
= parse_uint_full(str
, &i
, 0);
293 g_assert_cmpint(r
, ==, -ERANGE
/* FIXME -EINVAL */);
294 g_assert_cmpuint(i
, ==, 0);
297 static void test_qemu_strtoi_correct(void)
299 const char *str
= "12345 foo";
301 const char *endptr
= &f
;
305 err
= qemu_strtoi(str
, &endptr
, 0, &res
);
307 g_assert_cmpint(err
, ==, 0);
308 g_assert_cmpint(res
, ==, 12345);
309 g_assert_true(endptr
== str
+ 5);
312 static void test_qemu_strtoi_null(void)
315 const char *endptr
= &f
;
319 err
= qemu_strtoi(NULL
, &endptr
, 0, &res
);
321 g_assert_cmpint(err
, ==, -EINVAL
);
322 g_assert_cmpint(res
, ==, 999);
323 g_assert_null(endptr
);
326 static void test_qemu_strtoi_empty(void)
328 const char *str
= "";
330 const char *endptr
= &f
;
334 err
= qemu_strtoi(str
, &endptr
, 0, &res
);
336 g_assert_cmpint(err
, ==, -EINVAL
);
337 g_assert_cmpint(res
, ==, 0);
338 g_assert_true(endptr
== str
);
341 static void test_qemu_strtoi_whitespace(void)
343 const char *str
= " \t ";
345 const char *endptr
= &f
;
349 err
= qemu_strtoi(str
, &endptr
, 0, &res
);
351 g_assert_cmpint(err
, ==, -EINVAL
);
352 g_assert_cmpint(res
, ==, 0);
353 g_assert_true(endptr
== str
);
356 static void test_qemu_strtoi_invalid(void)
358 const char *str
= " xxxx \t abc";
360 const char *endptr
= &f
;
364 err
= qemu_strtoi(str
, &endptr
, 0, &res
);
366 g_assert_cmpint(err
, ==, -EINVAL
);
367 g_assert_cmpint(res
, ==, 0);
368 g_assert_true(endptr
== str
);
371 static void test_qemu_strtoi_trailing(void)
373 const char *str
= "123xxx";
375 const char *endptr
= &f
;
379 err
= qemu_strtoi(str
, &endptr
, 0, &res
);
381 g_assert_cmpint(err
, ==, 0);
382 g_assert_cmpint(res
, ==, 123);
383 g_assert_true(endptr
== str
+ 3);
386 static void test_qemu_strtoi_octal(void)
388 const char *str
= "0123";
390 const char *endptr
= &f
;
394 err
= qemu_strtoi(str
, &endptr
, 8, &res
);
396 g_assert_cmpint(err
, ==, 0);
397 g_assert_cmpint(res
, ==, 0123);
398 g_assert_true(endptr
== str
+ strlen(str
));
402 err
= qemu_strtoi(str
, &endptr
, 0, &res
);
404 g_assert_cmpint(err
, ==, 0);
405 g_assert_cmpint(res
, ==, 0123);
406 g_assert_true(endptr
== str
+ strlen(str
));
409 static void test_qemu_strtoi_decimal(void)
411 const char *str
= "0123";
413 const char *endptr
= &f
;
417 err
= qemu_strtoi(str
, &endptr
, 10, &res
);
419 g_assert_cmpint(err
, ==, 0);
420 g_assert_cmpint(res
, ==, 123);
421 g_assert_true(endptr
== str
+ strlen(str
));
426 err
= qemu_strtoi(str
, &endptr
, 0, &res
);
428 g_assert_cmpint(err
, ==, 0);
429 g_assert_cmpint(res
, ==, 123);
430 g_assert_true(endptr
== str
+ strlen(str
));
433 static void test_qemu_strtoi_hex(void)
435 const char *str
= "0123";
437 const char *endptr
= &f
;
441 err
= qemu_strtoi(str
, &endptr
, 16, &res
);
443 g_assert_cmpint(err
, ==, 0);
444 g_assert_cmpint(res
, ==, 0x123);
445 g_assert_true(endptr
== str
+ strlen(str
));
450 err
= qemu_strtoi(str
, &endptr
, 0, &res
);
452 g_assert_cmpint(err
, ==, 0);
453 g_assert_cmpint(res
, ==, 0x123);
454 g_assert_true(endptr
== str
+ strlen(str
));
459 err
= qemu_strtoi(str
, &endptr
, 16, &res
);
461 g_assert_cmpint(err
, ==, 0);
462 g_assert_cmpint(res
, ==, 0);
463 g_assert_true(endptr
== str
+ 1);
466 static void test_qemu_strtoi_max(void)
468 char *str
= g_strdup_printf("%d", INT_MAX
);
470 const char *endptr
= &f
;
474 err
= qemu_strtoi(str
, &endptr
, 0, &res
);
476 g_assert_cmpint(err
, ==, 0);
477 g_assert_cmpint(res
, ==, INT_MAX
);
478 g_assert_true(endptr
== str
+ strlen(str
));
482 static void test_qemu_strtoi_overflow(void)
489 str
= "2147483648"; /* INT_MAX + 1ll */
490 endptr
= "somewhere";
492 err
= qemu_strtoi(str
, &endptr
, 0, &res
);
493 g_assert_cmpint(err
, ==, -ERANGE
);
494 g_assert_cmpint(res
, ==, INT_MAX
);
495 g_assert_true(endptr
== str
+ strlen(str
));
497 str
= "0x7fffffffffffffff"; /* LLONG_MAX */
498 endptr
= "somewhere";
500 err
= qemu_strtoi(str
, &endptr
, 0, &res
);
501 g_assert_cmpint(err
, ==, -ERANGE
);
502 g_assert_cmpint(res
, ==, INT_MAX
);
503 g_assert_true(endptr
== str
+ strlen(str
));
505 str
= "0x8000000000000000"; /* (uint64_t)LLONG_MIN */
506 endptr
= "somewhere";
508 err
= qemu_strtoi(str
, &endptr
, 0, &res
);
509 g_assert_cmpint(err
, ==, -ERANGE
);
510 g_assert_cmpint(res
, ==, INT_MAX
);
511 g_assert_true(endptr
== str
+ strlen(str
));
513 str
= "0x10000000000000000"; /* 65 bits, 32-bit sign bit clear */
514 endptr
= "somewhere";
516 err
= qemu_strtoi(str
, &endptr
, 0, &res
);
517 g_assert_cmpint(err
, ==, -ERANGE
);
518 g_assert_cmpint(res
, ==, INT_MAX
);
519 g_assert_true(endptr
== str
+ strlen(str
));
521 str
= "0x18000000080000000"; /* 65 bits, 32-bit sign bit set */
522 endptr
= "somewhere";
524 err
= qemu_strtoi(str
, &endptr
, 0, &res
);
525 g_assert_cmpint(err
, ==, -ERANGE
);
526 g_assert_cmpint(res
, ==, INT_MAX
);
527 g_assert_true(endptr
== str
+ strlen(str
));
530 static void test_qemu_strtoi_min(void)
532 char *str
= g_strdup_printf("%d", INT_MIN
);
534 const char *endptr
= &f
;
538 err
= qemu_strtoi(str
, &endptr
, 0, &res
);
540 g_assert_cmpint(err
, ==, 0);
541 g_assert_cmpint(res
, ==, INT_MIN
);
542 g_assert_true(endptr
== str
+ strlen(str
));
546 static void test_qemu_strtoi_underflow(void)
553 str
= "-2147483649"; /* INT_MIN - 1ll */
554 endptr
= "somewhere";
556 err
= qemu_strtoi(str
, &endptr
, 0, &res
);
557 g_assert_cmpint(err
, ==, -ERANGE
);
558 g_assert_cmpint(res
, ==, INT_MIN
);
559 g_assert_true(endptr
== str
+ strlen(str
));
561 str
= "-0x7fffffffffffffff"; /* -LLONG_MAX */
562 endptr
= "somewhere";
564 err
= qemu_strtoi(str
, &endptr
, 0, &res
);
565 g_assert_cmpint(err
, ==, -ERANGE
);
566 g_assert_cmpint(res
, ==, INT_MIN
);
567 g_assert_true(endptr
== str
+ strlen(str
));
569 str
= "-0x8000000000000000"; /* (uint64_t)LLONG_MIN */
570 endptr
= "somewhere";
572 err
= qemu_strtoi(str
, &endptr
, 0, &res
);
573 g_assert_cmpint(err
, ==, -ERANGE
);
574 g_assert_cmpint(res
, ==, INT_MIN
);
575 g_assert_true(endptr
== str
+ strlen(str
));
577 str
= "-18446744073709551615"; /* -UINT64_MAX (not 1) */
578 endptr
= "somewhere";
580 err
= qemu_strtoi(str
, &endptr
, 0, &res
);
581 g_assert_cmpint(err
, ==, -ERANGE
);
582 g_assert_cmpint(res
, ==, INT_MIN
);
583 g_assert_true(endptr
== str
+ strlen(str
));
585 str
= "-0x10000000000000000"; /* 65 bits, 32-bit sign bit clear */
586 endptr
= "somewhere";
588 err
= qemu_strtoi(str
, &endptr
, 0, &res
);
589 g_assert_cmpint(err
, ==, -ERANGE
);
590 g_assert_cmpint(res
, ==, INT_MIN
);
591 g_assert_true(endptr
== str
+ strlen(str
));
593 str
= "-0x18000000080000000"; /* 65 bits, 32-bit sign bit set */
594 endptr
= "somewhere";
596 err
= qemu_strtoi(str
, &endptr
, 0, &res
);
597 g_assert_cmpint(err
, ==, -ERANGE
);
598 g_assert_cmpint(res
, ==, INT_MIN
);
599 g_assert_true(endptr
== str
+ strlen(str
));
602 static void test_qemu_strtoi_negative(void)
610 endptr
= "somewhere";
612 err
= qemu_strtoi(str
, &endptr
, 0, &res
);
613 g_assert_cmpint(err
, ==, 0);
614 g_assert_cmpint(res
, ==, -321);
615 g_assert_true(endptr
== str
+ strlen(str
));
617 str
= "-2147483648"; /* INT_MIN */
618 endptr
= "somewhere";
620 err
= qemu_strtoi(str
, &endptr
, 0, &res
);
621 g_assert_cmpint(err
, ==, 0);
622 g_assert_cmpint(res
, ==, INT_MIN
);
623 g_assert_true(endptr
== str
+ strlen(str
));
626 static void test_qemu_strtoi_negzero(void)
628 const char *str
= " -0";
630 const char *endptr
= &f
;
634 err
= qemu_strtoi(str
, &endptr
, 0, &res
);
636 g_assert_cmpint(err
, ==, 0);
637 g_assert_cmpint(res
, ==, 0);
638 g_assert_true(endptr
== str
+ strlen(str
));
641 static void test_qemu_strtoi_full_correct(void)
643 const char *str
= "123";
647 err
= qemu_strtoi(str
, NULL
, 0, &res
);
649 g_assert_cmpint(err
, ==, 0);
650 g_assert_cmpint(res
, ==, 123);
653 static void test_qemu_strtoi_full_null(void)
656 const char *endptr
= &f
;
660 err
= qemu_strtoi(NULL
, &endptr
, 0, &res
);
662 g_assert_cmpint(err
, ==, -EINVAL
);
663 g_assert_cmpint(res
, ==, 999);
664 g_assert_null(endptr
);
667 static void test_qemu_strtoi_full_empty(void)
669 const char *str
= "";
673 err
= qemu_strtoi(str
, NULL
, 0, &res
);
675 g_assert_cmpint(err
, ==, -EINVAL
);
676 g_assert_cmpint(res
, ==, 0);
679 static void test_qemu_strtoi_full_negative(void)
681 const char *str
= " \t -321";
685 err
= qemu_strtoi(str
, NULL
, 0, &res
);
687 g_assert_cmpint(err
, ==, 0);
688 g_assert_cmpint(res
, ==, -321);
691 static void test_qemu_strtoi_full_negzero(void)
693 const char *str
= " -0";
697 err
= qemu_strtoi(str
, NULL
, 0, &res
);
699 g_assert_cmpint(err
, ==, 0);
700 g_assert_cmpint(res
, ==, 0);
703 static void test_qemu_strtoi_full_trailing(void)
705 const char *str
= "123xxx";
709 err
= qemu_strtoi(str
, NULL
, 0, &res
);
711 g_assert_cmpint(err
, ==, -EINVAL
);
712 g_assert_cmpint(res
, ==, 123);
715 static void test_qemu_strtoi_full_max(void)
717 char *str
= g_strdup_printf("%d", INT_MAX
);
721 err
= qemu_strtoi(str
, NULL
, 0, &res
);
723 g_assert_cmpint(err
, ==, 0);
724 g_assert_cmpint(res
, ==, INT_MAX
);
728 static void test_qemu_strtoi_full_erange_junk(void)
730 /* EINVAL has priority over ERANGE */
731 const char *str
= "-9999999999junk";
735 err
= qemu_strtoi(str
, NULL
, 0, &res
);
737 g_assert_cmpint(err
, ==, -EINVAL
);
738 g_assert_cmpint(res
, ==, INT_MIN
);
741 static void test_qemu_strtoui_correct(void)
743 const char *str
= "12345 foo";
745 const char *endptr
= &f
;
746 unsigned int res
= 999;
749 err
= qemu_strtoui(str
, &endptr
, 0, &res
);
751 g_assert_cmpint(err
, ==, 0);
752 g_assert_cmpuint(res
, ==, 12345);
753 g_assert_true(endptr
== str
+ 5);
756 static void test_qemu_strtoui_null(void)
759 const char *endptr
= &f
;
760 unsigned int res
= 999;
763 err
= qemu_strtoui(NULL
, &endptr
, 0, &res
);
765 g_assert_cmpint(err
, ==, -EINVAL
);
766 g_assert_cmpuint(res
, ==, 999);
767 g_assert_null(endptr
);
770 static void test_qemu_strtoui_empty(void)
772 const char *str
= "";
774 const char *endptr
= &f
;
775 unsigned int res
= 999;
778 err
= qemu_strtoui(str
, &endptr
, 0, &res
);
780 g_assert_cmpint(err
, ==, -EINVAL
);
781 g_assert_cmpuint(res
, ==, 0);
782 g_assert_true(endptr
== str
);
785 static void test_qemu_strtoui_whitespace(void)
787 const char *str
= " \t ";
789 const char *endptr
= &f
;
790 unsigned int res
= 999;
793 err
= qemu_strtoui(str
, &endptr
, 0, &res
);
795 g_assert_cmpint(err
, ==, -EINVAL
);
796 g_assert_cmpuint(res
, ==, 0);
797 g_assert_true(endptr
== str
);
800 static void test_qemu_strtoui_invalid(void)
802 const char *str
= " xxxx \t abc";
804 const char *endptr
= &f
;
805 unsigned int res
= 999;
808 err
= qemu_strtoui(str
, &endptr
, 0, &res
);
810 g_assert_cmpint(err
, ==, -EINVAL
);
811 g_assert_cmpuint(res
, ==, 0);
812 g_assert_true(endptr
== str
);
815 static void test_qemu_strtoui_trailing(void)
817 const char *str
= "123xxx";
819 const char *endptr
= &f
;
820 unsigned int res
= 999;
823 err
= qemu_strtoui(str
, &endptr
, 0, &res
);
825 g_assert_cmpint(err
, ==, 0);
826 g_assert_cmpuint(res
, ==, 123);
827 g_assert_true(endptr
== str
+ 3);
830 static void test_qemu_strtoui_octal(void)
832 const char *str
= "0123";
834 const char *endptr
= &f
;
835 unsigned int res
= 999;
838 err
= qemu_strtoui(str
, &endptr
, 8, &res
);
840 g_assert_cmpint(err
, ==, 0);
841 g_assert_cmpuint(res
, ==, 0123);
842 g_assert_true(endptr
== str
+ strlen(str
));
846 err
= qemu_strtoui(str
, &endptr
, 0, &res
);
848 g_assert_cmpint(err
, ==, 0);
849 g_assert_cmpuint(res
, ==, 0123);
850 g_assert_true(endptr
== str
+ strlen(str
));
853 static void test_qemu_strtoui_decimal(void)
855 const char *str
= "0123";
857 const char *endptr
= &f
;
858 unsigned int res
= 999;
861 err
= qemu_strtoui(str
, &endptr
, 10, &res
);
863 g_assert_cmpint(err
, ==, 0);
864 g_assert_cmpuint(res
, ==, 123);
865 g_assert_true(endptr
== str
+ strlen(str
));
870 err
= qemu_strtoui(str
, &endptr
, 0, &res
);
872 g_assert_cmpint(err
, ==, 0);
873 g_assert_cmpuint(res
, ==, 123);
874 g_assert_true(endptr
== str
+ strlen(str
));
877 static void test_qemu_strtoui_hex(void)
879 const char *str
= "0123";
881 const char *endptr
= &f
;
882 unsigned int res
= 999;
885 err
= qemu_strtoui(str
, &endptr
, 16, &res
);
887 g_assert_cmpint(err
, ==, 0);
888 g_assert_cmphex(res
, ==, 0x123);
889 g_assert_true(endptr
== str
+ strlen(str
));
894 err
= qemu_strtoui(str
, &endptr
, 0, &res
);
896 g_assert_cmpint(err
, ==, 0);
897 g_assert_cmphex(res
, ==, 0x123);
898 g_assert_true(endptr
== str
+ strlen(str
));
903 err
= qemu_strtoui(str
, &endptr
, 16, &res
);
905 g_assert_cmpint(err
, ==, 0);
906 g_assert_cmphex(res
, ==, 0);
907 g_assert_true(endptr
== str
+ 1);
910 static void test_qemu_strtoui_wrap(void)
912 /* wraparound is consistent with 32-bit strtoul */
913 const char *str
= "-4294967295"; /* 1 mod 2^32 */
915 const char *endptr
= &f
;
916 unsigned int res
= 999;
919 err
= qemu_strtoui(str
, &endptr
, 0, &res
);
921 g_assert_cmpint(err
, ==, 0);
922 g_assert_cmphex(res
, ==, 1);
923 g_assert_true(endptr
== str
+ strlen(str
));
926 static void test_qemu_strtoui_max(void)
928 char *str
= g_strdup_printf("%u", UINT_MAX
);
930 const char *endptr
= &f
;
931 unsigned int res
= 999;
934 err
= qemu_strtoui(str
, &endptr
, 0, &res
);
936 g_assert_cmpint(err
, ==, 0);
937 g_assert_cmphex(res
, ==, UINT_MAX
);
938 g_assert_true(endptr
== str
+ strlen(str
));
942 static void test_qemu_strtoui_overflow(void)
949 str
= "4294967296"; /* UINT_MAX + 1ll */
950 endptr
= "somewhere";
952 err
= qemu_strtoui(str
, &endptr
, 0, &res
);
953 g_assert_cmpint(err
, ==, -ERANGE
);
954 g_assert_cmpuint(res
, ==, UINT_MAX
);
955 g_assert_true(endptr
== str
+ strlen(str
));
957 str
= "0x7fffffffffffffff"; /* LLONG_MAX */
958 endptr
= "somewhere";
960 err
= qemu_strtoui(str
, &endptr
, 0, &res
);
961 g_assert_cmpint(err
, ==, -ERANGE
);
962 g_assert_cmpuint(res
, ==, UINT_MAX
);
963 g_assert_true(endptr
== str
+ strlen(str
));
965 str
= "0x8000000000000000"; /* (uint64_t)LLONG_MIN */
966 endptr
= "somewhere";
968 err
= qemu_strtoui(str
, &endptr
, 0, &res
);
969 g_assert_cmpint(err
, ==, -ERANGE
);
970 g_assert_cmpuint(res
, ==, UINT_MAX
);
971 g_assert_true(endptr
== str
+ strlen(str
));
973 str
= "0xffffffff00000001"; /* ULLONG_MAX - UINT_MAX + 1 (not 1) */
974 endptr
= "somewhere";
976 err
= qemu_strtoui(str
, &endptr
, 0, &res
);
977 g_assert_cmpint(err
, ==, -ERANGE
);
978 g_assert_cmpuint(res
, ==, UINT_MAX
);
979 g_assert_true(endptr
== str
+ strlen(str
));
981 str
= "0xfffffffffffffffe"; /* ULLONG_MAX - 1 (not UINT_MAX - 1) */
982 endptr
= "somewhere";
984 err
= qemu_strtoui(str
, &endptr
, 0, &res
);
985 g_assert_cmpint(err
, ==, -ERANGE
);
986 g_assert_cmpuint(res
, ==, UINT_MAX
);
987 g_assert_true(endptr
== str
+ strlen(str
));
989 str
= "0x10000000000000000"; /* 65 bits, 32-bit sign bit clear */
990 endptr
= "somewhere";
992 err
= qemu_strtoui(str
, &endptr
, 0, &res
);
993 g_assert_cmpint(err
, ==, -ERANGE
);
994 g_assert_cmpuint(res
, ==, UINT_MAX
);
995 g_assert_true(endptr
== str
+ strlen(str
));
997 str
= "0x18000000080000000"; /* 65 bits, 32-bit sign bit set */
998 endptr
= "somewhere";
1000 err
= qemu_strtoui(str
, &endptr
, 0, &res
);
1001 g_assert_cmpint(err
, ==, -ERANGE
);
1002 g_assert_cmpuint(res
, ==, UINT_MAX
);
1003 g_assert_true(endptr
== str
+ strlen(str
));
1006 static void test_qemu_strtoui_underflow(void)
1013 str
= "-4294967296"; /* -(long long)UINT_MAX - 1ll */
1014 endptr
= "somewhere";
1016 err
= qemu_strtoui(str
, &endptr
, 0, &res
);
1017 g_assert_cmpint(err
, ==, -ERANGE
);
1018 g_assert_cmpuint(res
, ==, UINT_MAX
);
1019 g_assert_true(endptr
== str
+ strlen(str
));
1021 str
= "-18446744073709551615"; /* -UINT64_MAX (not -(-1)) */
1022 endptr
= "somewhere";
1024 err
= qemu_strtoui(str
, &endptr
, 0, &res
);
1025 g_assert_cmpint(err
, ==, -ERANGE
);
1026 g_assert_cmpuint(res
, ==, UINT_MAX
);
1027 g_assert_true(endptr
== str
+ strlen(str
));
1029 str
= "-0xffffffff00000002";
1030 endptr
= "somewhere";
1032 err
= qemu_strtoui(str
, &endptr
, 0, &res
);
1033 g_assert_cmpint(err
, ==, -ERANGE
);
1034 g_assert_cmpuint(res
, ==, UINT_MAX
);
1035 g_assert_true(endptr
== str
+ strlen(str
));
1037 str
= "-0x10000000000000000"; /* 65 bits, 32-bit sign bit clear */
1038 endptr
= "somewhere";
1040 err
= qemu_strtoui(str
, &endptr
, 0, &res
);
1041 g_assert_cmpint(err
, ==, -ERANGE
);
1042 g_assert_cmpuint(res
, ==, UINT_MAX
);
1043 g_assert_true(endptr
== str
+ strlen(str
));
1045 str
= "-0x18000000080000000"; /* 65 bits, 32-bit sign bit set */
1046 endptr
= "somewhere";
1048 err
= qemu_strtoui(str
, &endptr
, 0, &res
);
1049 g_assert_cmpint(err
, ==, -ERANGE
);
1050 g_assert_cmpuint(res
, ==, UINT_MAX
);
1051 g_assert_true(endptr
== str
+ strlen(str
));
1054 static void test_qemu_strtoui_negative(void)
1056 const char *str
= " \t -321";
1058 const char *endptr
= &f
;
1059 unsigned int res
= 999;
1062 err
= qemu_strtoui(str
, &endptr
, 0, &res
);
1064 g_assert_cmpint(err
, ==, 0);
1065 g_assert_cmpuint(res
, ==, (unsigned int)-321);
1066 g_assert_true(endptr
== str
+ strlen(str
));
1069 static void test_qemu_strtoui_negzero(void)
1071 const char *str
= " -0";
1073 const char *endptr
= &f
;
1074 unsigned int res
= 999;
1077 err
= qemu_strtoui(str
, &endptr
, 0, &res
);
1079 g_assert_cmpint(err
, ==, 0);
1080 g_assert_cmpuint(res
, ==, 0);
1081 g_assert_true(endptr
== str
+ strlen(str
));
1084 static void test_qemu_strtoui_full_correct(void)
1086 const char *str
= "123";
1087 unsigned int res
= 999;
1090 err
= qemu_strtoui(str
, NULL
, 0, &res
);
1092 g_assert_cmpint(err
, ==, 0);
1093 g_assert_cmpuint(res
, ==, 123);
1096 static void test_qemu_strtoui_full_null(void)
1098 unsigned int res
= 999;
1101 err
= qemu_strtoui(NULL
, NULL
, 0, &res
);
1103 g_assert_cmpint(err
, ==, -EINVAL
);
1104 g_assert_cmpuint(res
, ==, 999);
1107 static void test_qemu_strtoui_full_empty(void)
1109 const char *str
= "";
1110 unsigned int res
= 999;
1113 err
= qemu_strtoui(str
, NULL
, 0, &res
);
1115 g_assert_cmpint(err
, ==, -EINVAL
);
1116 g_assert_cmpuint(res
, ==, 0);
1119 static void test_qemu_strtoui_full_negative(void)
1121 const char *str
= " \t -321";
1122 unsigned int res
= 999;
1125 err
= qemu_strtoui(str
, NULL
, 0, &res
);
1126 g_assert_cmpint(err
, ==, 0);
1127 g_assert_cmpuint(res
, ==, (unsigned int)-321);
1130 static void test_qemu_strtoui_full_negzero(void)
1132 const char *str
= " -0";
1133 unsigned int res
= 999;
1136 err
= qemu_strtoui(str
, NULL
, 0, &res
);
1137 g_assert_cmpint(err
, ==, 0);
1138 g_assert_cmpuint(res
, ==, 0);
1141 static void test_qemu_strtoui_full_trailing(void)
1143 const char *str
= "123xxx";
1144 unsigned int res
= 999;
1147 err
= qemu_strtoui(str
, NULL
, 0, &res
);
1149 g_assert_cmpint(err
, ==, -EINVAL
);
1150 g_assert_cmpuint(res
, ==, 123);
1153 static void test_qemu_strtoui_full_max(void)
1155 char *str
= g_strdup_printf("%u", UINT_MAX
);
1156 unsigned int res
= 999;
1159 err
= qemu_strtoui(str
, NULL
, 0, &res
);
1161 g_assert_cmpint(err
, ==, 0);
1162 g_assert_cmphex(res
, ==, UINT_MAX
);
1166 static void test_qemu_strtoui_full_erange_junk(void)
1168 /* EINVAL has priority over ERANGE */
1169 const char *str
= "-9999999999junk";
1170 unsigned int res
= 999;
1173 err
= qemu_strtoui(str
, NULL
, 0, &res
);
1175 g_assert_cmpint(err
, ==, -EINVAL
);
1176 g_assert_cmpuint(res
, ==, UINT_MAX
);
1179 static void test_qemu_strtol_correct(void)
1181 const char *str
= "12345 foo";
1183 const char *endptr
= &f
;
1187 err
= qemu_strtol(str
, &endptr
, 0, &res
);
1189 g_assert_cmpint(err
, ==, 0);
1190 g_assert_cmpint(res
, ==, 12345);
1191 g_assert_true(endptr
== str
+ 5);
1194 static void test_qemu_strtol_null(void)
1197 const char *endptr
= &f
;
1201 err
= qemu_strtol(NULL
, &endptr
, 0, &res
);
1203 g_assert_cmpint(err
, ==, -EINVAL
);
1204 g_assert_cmpint(res
, ==, 999);
1205 g_assert_null(endptr
);
1208 static void test_qemu_strtol_empty(void)
1210 const char *str
= "";
1212 const char *endptr
= &f
;
1216 err
= qemu_strtol(str
, &endptr
, 0, &res
);
1218 g_assert_cmpint(err
, ==, -EINVAL
);
1219 g_assert_cmpint(res
, ==, 0);
1220 g_assert_true(endptr
== str
);
1223 static void test_qemu_strtol_whitespace(void)
1225 const char *str
= " \t ";
1227 const char *endptr
= &f
;
1231 err
= qemu_strtol(str
, &endptr
, 0, &res
);
1233 g_assert_cmpint(err
, ==, -EINVAL
);
1234 g_assert_cmpint(res
, ==, 0);
1235 g_assert_true(endptr
== str
);
1238 static void test_qemu_strtol_invalid(void)
1240 const char *str
= " xxxx \t abc";
1242 const char *endptr
= &f
;
1246 err
= qemu_strtol(str
, &endptr
, 0, &res
);
1248 g_assert_cmpint(err
, ==, -EINVAL
);
1249 g_assert_cmpint(res
, ==, 0);
1250 g_assert_true(endptr
== str
);
1253 static void test_qemu_strtol_trailing(void)
1255 const char *str
= "123xxx";
1257 const char *endptr
= &f
;
1261 err
= qemu_strtol(str
, &endptr
, 0, &res
);
1263 g_assert_cmpint(err
, ==, 0);
1264 g_assert_cmpint(res
, ==, 123);
1265 g_assert_true(endptr
== str
+ 3);
1268 static void test_qemu_strtol_octal(void)
1270 const char *str
= "0123";
1272 const char *endptr
= &f
;
1276 err
= qemu_strtol(str
, &endptr
, 8, &res
);
1278 g_assert_cmpint(err
, ==, 0);
1279 g_assert_cmpint(res
, ==, 0123);
1280 g_assert_true(endptr
== str
+ strlen(str
));
1284 err
= qemu_strtol(str
, &endptr
, 0, &res
);
1286 g_assert_cmpint(err
, ==, 0);
1287 g_assert_cmpint(res
, ==, 0123);
1288 g_assert_true(endptr
== str
+ strlen(str
));
1291 static void test_qemu_strtol_decimal(void)
1293 const char *str
= "0123";
1295 const char *endptr
= &f
;
1299 err
= qemu_strtol(str
, &endptr
, 10, &res
);
1301 g_assert_cmpint(err
, ==, 0);
1302 g_assert_cmpint(res
, ==, 123);
1303 g_assert_true(endptr
== str
+ strlen(str
));
1308 err
= qemu_strtol(str
, &endptr
, 0, &res
);
1310 g_assert_cmpint(err
, ==, 0);
1311 g_assert_cmpint(res
, ==, 123);
1312 g_assert_true(endptr
== str
+ strlen(str
));
1315 static void test_qemu_strtol_hex(void)
1317 const char *str
= "0123";
1319 const char *endptr
= &f
;
1323 err
= qemu_strtol(str
, &endptr
, 16, &res
);
1325 g_assert_cmpint(err
, ==, 0);
1326 g_assert_cmpint(res
, ==, 0x123);
1327 g_assert_true(endptr
== str
+ strlen(str
));
1332 err
= qemu_strtol(str
, &endptr
, 0, &res
);
1334 g_assert_cmpint(err
, ==, 0);
1335 g_assert_cmpint(res
, ==, 0x123);
1336 g_assert_true(endptr
== str
+ strlen(str
));
1341 err
= qemu_strtol(str
, &endptr
, 16, &res
);
1343 g_assert_cmpint(err
, ==, 0);
1344 g_assert_cmpint(res
, ==, 0);
1345 g_assert_true(endptr
== str
+ 1);
1348 static void test_qemu_strtol_max(void)
1350 char *str
= g_strdup_printf("%ld", LONG_MAX
);
1352 const char *endptr
= &f
;
1356 err
= qemu_strtol(str
, &endptr
, 0, &res
);
1358 g_assert_cmpint(err
, ==, 0);
1359 g_assert_cmpint(res
, ==, LONG_MAX
);
1360 g_assert_true(endptr
== str
+ strlen(str
));
1364 static void test_qemu_strtol_overflow(void)
1371 /* 1 more than LONG_MAX */
1372 str
= LONG_MAX
== INT_MAX
? "2147483648" : "9223372036854775808";
1373 endptr
= "somewhere";
1375 err
= qemu_strtol(str
, &endptr
, 0, &res
);
1376 g_assert_cmpint(err
, ==, -ERANGE
);
1377 g_assert_cmpint(res
, ==, LONG_MAX
);
1378 g_assert_true(endptr
== str
+ strlen(str
));
1380 if (LONG_MAX
== INT_MAX
) {
1381 str
= "0xffffffff00000001"; /* ULLONG_MAX - UINT_MAX + 1 (not 1) */
1382 endptr
= "somewhere";
1384 err
= qemu_strtol(str
, &endptr
, 0, &res
);
1385 g_assert_cmpint(err
, ==, -ERANGE
);
1386 g_assert_cmpint(res
, ==, LONG_MAX
);
1387 g_assert_true(endptr
== str
+ strlen(str
));
1390 str
= "0x10000000000000000"; /* 65 bits, either sign bit position clear */
1391 endptr
= "somewhere";
1393 err
= qemu_strtol(str
, &endptr
, 0, &res
);
1394 g_assert_cmpint(err
, ==, -ERANGE
);
1395 g_assert_cmpint(res
, ==, LONG_MAX
);
1396 g_assert_true(endptr
== str
+ strlen(str
));
1398 str
= "0x18000000080000000"; /* 65 bits, either sign bit position set */
1399 endptr
= "somewhere";
1401 err
= qemu_strtol(str
, &endptr
, 0, &res
);
1402 g_assert_cmpint(err
, ==, -ERANGE
);
1403 g_assert_cmpint(res
, ==, LONG_MAX
);
1404 g_assert_true(endptr
== str
+ strlen(str
));
1407 static void test_qemu_strtol_min(void)
1409 char *str
= g_strdup_printf("%ld", LONG_MIN
);
1411 const char *endptr
= &f
;
1415 err
= qemu_strtol(str
, &endptr
, 0, &res
);
1417 g_assert_cmpint(err
, ==, 0);
1418 g_assert_cmpint(res
, ==, LONG_MIN
);
1419 g_assert_true(endptr
== str
+ strlen(str
));
1423 static void test_qemu_strtol_underflow(void)
1430 /* 1 less than LONG_MIN */
1431 str
= LONG_MIN
== INT_MIN
? "-2147483649" : "-9223372036854775809";
1432 endptr
= "somewhere";
1434 err
= qemu_strtol(str
, &endptr
, 0, &res
);
1435 g_assert_cmpint(err
, ==, -ERANGE
);
1436 g_assert_cmpint(res
, ==, LONG_MIN
);
1437 g_assert_true(endptr
== str
+ strlen(str
));
1439 if (LONG_MAX
== INT_MAX
) {
1440 str
= "-18446744073709551615"; /* -UINT64_MAX (not 1) */
1441 endptr
= "somewhere";
1443 err
= qemu_strtol(str
, &endptr
, 0, &res
);
1444 g_assert_cmpint(err
, ==, -ERANGE
);
1445 g_assert_cmpint(res
, ==, LONG_MIN
);
1446 g_assert_true(endptr
== str
+ strlen(str
));
1449 str
= "-0x10000000000000000"; /* 65 bits, either sign bit position clear */
1450 endptr
= "somewhere";
1452 err
= qemu_strtol(str
, &endptr
, 0, &res
);
1453 g_assert_cmpint(err
, ==, -ERANGE
);
1454 g_assert_cmpint(res
, ==, LONG_MIN
);
1455 g_assert_true(endptr
== str
+ strlen(str
));
1457 str
= "-0x18000000080000000"; /* 65 bits, either sign bit position set */
1458 endptr
= "somewhere";
1460 err
= qemu_strtol(str
, &endptr
, 0, &res
);
1461 g_assert_cmpint(err
, ==, -ERANGE
);
1462 g_assert_cmpint(res
, ==, LONG_MIN
);
1463 g_assert_true(endptr
== str
+ strlen(str
));
1466 static void test_qemu_strtol_negative(void)
1468 const char *str
= " \t -321";
1470 const char *endptr
= &f
;
1474 err
= qemu_strtol(str
, &endptr
, 0, &res
);
1476 g_assert_cmpint(err
, ==, 0);
1477 g_assert_cmpint(res
, ==, -321);
1478 g_assert_true(endptr
== str
+ strlen(str
));
1481 static void test_qemu_strtol_negzero(void)
1483 const char *str
= " -0";
1485 const char *endptr
= &f
;
1489 err
= qemu_strtol(str
, &endptr
, 0, &res
);
1491 g_assert_cmpint(err
, ==, 0);
1492 g_assert_cmpint(res
, ==, 0);
1493 g_assert_true(endptr
== str
+ strlen(str
));
1496 static void test_qemu_strtol_full_correct(void)
1498 const char *str
= "123";
1502 err
= qemu_strtol(str
, NULL
, 0, &res
);
1504 g_assert_cmpint(err
, ==, 0);
1505 g_assert_cmpint(res
, ==, 123);
1508 static void test_qemu_strtol_full_null(void)
1511 const char *endptr
= &f
;
1515 err
= qemu_strtol(NULL
, &endptr
, 0, &res
);
1517 g_assert_cmpint(err
, ==, -EINVAL
);
1518 g_assert_cmpint(res
, ==, 999);
1519 g_assert_null(endptr
);
1522 static void test_qemu_strtol_full_empty(void)
1524 const char *str
= "";
1528 err
= qemu_strtol(str
, NULL
, 0, &res
);
1530 g_assert_cmpint(err
, ==, -EINVAL
);
1531 g_assert_cmpint(res
, ==, 0);
1534 static void test_qemu_strtol_full_negative(void)
1536 const char *str
= " \t -321";
1540 err
= qemu_strtol(str
, NULL
, 0, &res
);
1542 g_assert_cmpint(err
, ==, 0);
1543 g_assert_cmpint(res
, ==, -321);
1546 static void test_qemu_strtol_full_negzero(void)
1548 const char *str
= " -0";
1552 err
= qemu_strtol(str
, NULL
, 0, &res
);
1554 g_assert_cmpint(err
, ==, 0);
1555 g_assert_cmpint(res
, ==, 0);
1558 static void test_qemu_strtol_full_trailing(void)
1560 const char *str
= "123xxx";
1564 err
= qemu_strtol(str
, NULL
, 0, &res
);
1566 g_assert_cmpint(err
, ==, -EINVAL
);
1567 g_assert_cmpint(res
, ==, 123);
1570 static void test_qemu_strtol_full_max(void)
1572 char *str
= g_strdup_printf("%ld", LONG_MAX
);
1576 err
= qemu_strtol(str
, NULL
, 0, &res
);
1578 g_assert_cmpint(err
, ==, 0);
1579 g_assert_cmpint(res
, ==, LONG_MAX
);
1583 static void test_qemu_strtol_full_erange_junk(void)
1585 /* EINVAL has priority over ERANGE */
1586 const char *str
= "-99999999999999999999junk";
1590 err
= qemu_strtol(str
, NULL
, 0, &res
);
1592 g_assert_cmpint(err
, ==, -EINVAL
);
1593 g_assert_cmpint(res
, ==, LONG_MIN
);
1596 static void test_qemu_strtoul_correct(void)
1598 const char *str
= "12345 foo";
1600 const char *endptr
= &f
;
1601 unsigned long res
= 999;
1604 err
= qemu_strtoul(str
, &endptr
, 0, &res
);
1606 g_assert_cmpint(err
, ==, 0);
1607 g_assert_cmpuint(res
, ==, 12345);
1608 g_assert_true(endptr
== str
+ 5);
1611 static void test_qemu_strtoul_null(void)
1614 const char *endptr
= &f
;
1615 unsigned long res
= 999;
1618 err
= qemu_strtoul(NULL
, &endptr
, 0, &res
);
1620 g_assert_cmpint(err
, ==, -EINVAL
);
1621 g_assert_cmpuint(res
, ==, 999);
1622 g_assert_null(endptr
);
1625 static void test_qemu_strtoul_empty(void)
1627 const char *str
= "";
1629 const char *endptr
= &f
;
1630 unsigned long res
= 999;
1633 err
= qemu_strtoul(str
, &endptr
, 0, &res
);
1635 g_assert_cmpint(err
, ==, -EINVAL
);
1636 g_assert_cmpuint(res
, ==, 0);
1637 g_assert_true(endptr
== str
);
1640 static void test_qemu_strtoul_whitespace(void)
1642 const char *str
= " \t ";
1644 const char *endptr
= &f
;
1645 unsigned long res
= 999;
1648 err
= qemu_strtoul(str
, &endptr
, 0, &res
);
1650 g_assert_cmpint(err
, ==, -EINVAL
);
1651 g_assert_cmpuint(res
, ==, 0);
1652 g_assert_true(endptr
== str
);
1655 static void test_qemu_strtoul_invalid(void)
1657 const char *str
= " xxxx \t abc";
1659 const char *endptr
= &f
;
1660 unsigned long res
= 999;
1663 err
= qemu_strtoul(str
, &endptr
, 0, &res
);
1665 g_assert_cmpint(err
, ==, -EINVAL
);
1666 g_assert_cmpuint(res
, ==, 0);
1667 g_assert_true(endptr
== str
);
1670 static void test_qemu_strtoul_trailing(void)
1672 const char *str
= "123xxx";
1674 const char *endptr
= &f
;
1675 unsigned long res
= 999;
1678 err
= qemu_strtoul(str
, &endptr
, 0, &res
);
1680 g_assert_cmpint(err
, ==, 0);
1681 g_assert_cmpuint(res
, ==, 123);
1682 g_assert_true(endptr
== str
+ 3);
1685 static void test_qemu_strtoul_octal(void)
1687 const char *str
= "0123";
1689 const char *endptr
= &f
;
1690 unsigned long res
= 999;
1693 err
= qemu_strtoul(str
, &endptr
, 8, &res
);
1695 g_assert_cmpint(err
, ==, 0);
1696 g_assert_cmpuint(res
, ==, 0123);
1697 g_assert_true(endptr
== str
+ strlen(str
));
1701 err
= qemu_strtoul(str
, &endptr
, 0, &res
);
1703 g_assert_cmpint(err
, ==, 0);
1704 g_assert_cmpuint(res
, ==, 0123);
1705 g_assert_true(endptr
== str
+ strlen(str
));
1708 static void test_qemu_strtoul_decimal(void)
1710 const char *str
= "0123";
1712 const char *endptr
= &f
;
1713 unsigned long res
= 999;
1716 err
= qemu_strtoul(str
, &endptr
, 10, &res
);
1718 g_assert_cmpint(err
, ==, 0);
1719 g_assert_cmpuint(res
, ==, 123);
1720 g_assert_true(endptr
== str
+ strlen(str
));
1725 err
= qemu_strtoul(str
, &endptr
, 0, &res
);
1727 g_assert_cmpint(err
, ==, 0);
1728 g_assert_cmpuint(res
, ==, 123);
1729 g_assert_true(endptr
== str
+ strlen(str
));
1732 static void test_qemu_strtoul_hex(void)
1734 const char *str
= "0123";
1736 const char *endptr
= &f
;
1737 unsigned long res
= 999;
1740 err
= qemu_strtoul(str
, &endptr
, 16, &res
);
1742 g_assert_cmpint(err
, ==, 0);
1743 g_assert_cmphex(res
, ==, 0x123);
1744 g_assert_true(endptr
== str
+ strlen(str
));
1749 err
= qemu_strtoul(str
, &endptr
, 0, &res
);
1751 g_assert_cmpint(err
, ==, 0);
1752 g_assert_cmphex(res
, ==, 0x123);
1753 g_assert_true(endptr
== str
+ strlen(str
));
1758 err
= qemu_strtoul(str
, &endptr
, 16, &res
);
1760 g_assert_cmpint(err
, ==, 0);
1761 g_assert_cmphex(res
, ==, 0);
1762 g_assert_true(endptr
== str
+ 1);
1765 static void test_qemu_strtoul_wrap(void)
1769 const char *endptr
= &f
;
1770 unsigned long res
= 999;
1773 /* 1 mod 2^(sizeof(long)*8) */
1774 str
= LONG_MAX
== INT_MAX
? "-4294967295" : "-18446744073709551615";
1775 err
= qemu_strtoul(str
, &endptr
, 0, &res
);
1777 g_assert_cmpint(err
, ==, 0);
1778 g_assert_cmphex(res
, ==, 1);
1779 g_assert_true(endptr
== str
+ strlen(str
));
1782 static void test_qemu_strtoul_max(void)
1784 char *str
= g_strdup_printf("%lu", ULONG_MAX
);
1786 const char *endptr
= &f
;
1787 unsigned long res
= 999;
1790 err
= qemu_strtoul(str
, &endptr
, 0, &res
);
1792 g_assert_cmpint(err
, ==, 0);
1793 g_assert_cmphex(res
, ==, ULONG_MAX
);
1794 g_assert_true(endptr
== str
+ strlen(str
));
1798 static void test_qemu_strtoul_overflow(void)
1805 /* 1 more than ULONG_MAX */
1806 str
= ULONG_MAX
== UINT_MAX
? "4294967296" : "18446744073709551616";
1807 endptr
= "somewhere";
1809 err
= qemu_strtoul(str
, &endptr
, 0, &res
);
1810 g_assert_cmpint(err
, ==, -ERANGE
);
1811 g_assert_cmpuint(res
, ==, ULONG_MAX
);
1812 g_assert_true(endptr
== str
+ strlen(str
));
1814 if (LONG_MAX
== INT_MAX
) {
1815 str
= "0xffffffff00000001"; /* UINT64_MAX - UINT_MAX + 1 (not 1) */
1816 endptr
= "somewhere";
1818 err
= qemu_strtoul(str
, &endptr
, 0, &res
);
1819 g_assert_cmpint(err
, ==, -ERANGE
);
1820 g_assert_cmpuint(res
, ==, ULONG_MAX
);
1821 g_assert_true(endptr
== str
+ strlen(str
));
1824 str
= "0x10000000000000000"; /* 65 bits, either sign bit position clear */
1825 endptr
= "somewhere";
1827 err
= qemu_strtoul(str
, &endptr
, 0, &res
);
1828 g_assert_cmpint(err
, ==, -ERANGE
);
1829 g_assert_cmpuint(res
, ==, ULONG_MAX
);
1830 g_assert_true(endptr
== str
+ strlen(str
));
1832 str
= "0x18000000080000000"; /* 65 bits, either sign bit position set */
1833 endptr
= "somewhere";
1835 err
= qemu_strtoul(str
, &endptr
, 0, &res
);
1836 g_assert_cmpint(err
, ==, -ERANGE
);
1837 g_assert_cmpuint(res
, ==, ULONG_MAX
);
1838 g_assert_true(endptr
== str
+ strlen(str
));
1841 static void test_qemu_strtoul_underflow(void)
1848 /* 1 less than -ULONG_MAX */
1849 str
= ULONG_MAX
== UINT_MAX
? "-4294967296" : "-18446744073709551616";
1850 endptr
= "somewhere";
1852 err
= qemu_strtoul(str
, &endptr
, 0, &res
);
1853 g_assert_cmpint(err
, ==, -ERANGE
);
1854 g_assert_cmpuint(res
, ==, ULONG_MAX
);
1855 g_assert_true(endptr
== str
+ strlen(str
));
1857 if (LONG_MAX
== INT_MAX
) {
1858 str
= "-0xffffffff00000002";
1859 endptr
= "somewhere";
1861 err
= qemu_strtoul(str
, &endptr
, 0, &res
);
1862 g_assert_cmpint(err
, ==, -ERANGE
);
1863 g_assert_cmpuint(res
, ==, ULONG_MAX
);
1864 g_assert_true(endptr
== str
+ strlen(str
));
1867 str
= "-0x10000000000000000"; /* 65 bits, either sign bit position clear */
1868 endptr
= "somewhere";
1870 err
= qemu_strtoul(str
, &endptr
, 0, &res
);
1871 g_assert_cmpint(err
, ==, -ERANGE
);
1872 g_assert_cmpuint(res
, ==, ULONG_MAX
);
1873 g_assert_true(endptr
== str
+ strlen(str
));
1875 str
= "-0x18000000080000000"; /* 65 bits, either sign bit position set */
1876 endptr
= "somewhere";
1878 err
= qemu_strtoul(str
, &endptr
, 0, &res
);
1879 g_assert_cmpint(err
, ==, -ERANGE
);
1880 g_assert_cmpuint(res
, ==, ULONG_MAX
);
1881 g_assert_true(endptr
== str
+ strlen(str
));
1884 static void test_qemu_strtoul_negative(void)
1886 const char *str
= " \t -321";
1888 const char *endptr
= &f
;
1889 unsigned long res
= 999;
1892 err
= qemu_strtoul(str
, &endptr
, 0, &res
);
1894 g_assert_cmpint(err
, ==, 0);
1895 g_assert_cmpuint(res
, ==, -321ul);
1896 g_assert_true(endptr
== str
+ strlen(str
));
1899 static void test_qemu_strtoul_negzero(void)
1901 const char *str
= " -0";
1903 const char *endptr
= &f
;
1904 unsigned long res
= 999;
1907 err
= qemu_strtoul(str
, &endptr
, 0, &res
);
1909 g_assert_cmpint(err
, ==, 0);
1910 g_assert_cmpuint(res
, ==, 0);
1911 g_assert_true(endptr
== str
+ strlen(str
));
1914 static void test_qemu_strtoul_full_correct(void)
1916 const char *str
= "123";
1917 unsigned long res
= 999;
1920 err
= qemu_strtoul(str
, NULL
, 0, &res
);
1922 g_assert_cmpint(err
, ==, 0);
1923 g_assert_cmpuint(res
, ==, 123);
1926 static void test_qemu_strtoul_full_null(void)
1928 unsigned long res
= 999;
1931 err
= qemu_strtoul(NULL
, NULL
, 0, &res
);
1933 g_assert_cmpint(err
, ==, -EINVAL
);
1934 g_assert_cmpuint(res
, ==, 999);
1937 static void test_qemu_strtoul_full_empty(void)
1939 const char *str
= "";
1940 unsigned long res
= 999;
1943 err
= qemu_strtoul(str
, NULL
, 0, &res
);
1945 g_assert_cmpint(err
, ==, -EINVAL
);
1946 g_assert_cmpuint(res
, ==, 0);
1949 static void test_qemu_strtoul_full_negative(void)
1951 const char *str
= " \t -321";
1952 unsigned long res
= 999;
1955 err
= qemu_strtoul(str
, NULL
, 0, &res
);
1956 g_assert_cmpint(err
, ==, 0);
1957 g_assert_cmpuint(res
, ==, -321ul);
1960 static void test_qemu_strtoul_full_negzero(void)
1962 const char *str
= " -0";
1963 unsigned long res
= 999;
1966 err
= qemu_strtoul(str
, NULL
, 0, &res
);
1967 g_assert_cmpint(err
, ==, 0);
1968 g_assert_cmpuint(res
, ==, 0);
1971 static void test_qemu_strtoul_full_trailing(void)
1973 const char *str
= "123xxx";
1974 unsigned long res
= 999;
1977 err
= qemu_strtoul(str
, NULL
, 0, &res
);
1979 g_assert_cmpint(err
, ==, -EINVAL
);
1980 g_assert_cmpuint(res
, ==, 123);
1983 static void test_qemu_strtoul_full_max(void)
1985 char *str
= g_strdup_printf("%lu", ULONG_MAX
);
1986 unsigned long res
= 999;
1989 err
= qemu_strtoul(str
, NULL
, 0, &res
);
1991 g_assert_cmpint(err
, ==, 0);
1992 g_assert_cmphex(res
, ==, ULONG_MAX
);
1996 static void test_qemu_strtoul_full_erange_junk(void)
1998 /* EINVAL has priority over ERANGE */
1999 const char *str
= "-99999999999999999999junk";
2000 unsigned long res
= 999;
2003 err
= qemu_strtoul(str
, NULL
, 0, &res
);
2005 g_assert_cmpint(err
, ==, -EINVAL
);
2006 g_assert_cmpuint(res
, ==, ULONG_MAX
);
2009 static void test_qemu_strtoi64_correct(void)
2011 const char *str
= "12345 foo";
2013 const char *endptr
= &f
;
2017 err
= qemu_strtoi64(str
, &endptr
, 0, &res
);
2019 g_assert_cmpint(err
, ==, 0);
2020 g_assert_cmpint(res
, ==, 12345);
2021 g_assert_true(endptr
== str
+ 5);
2024 static void test_qemu_strtoi64_null(void)
2027 const char *endptr
= &f
;
2031 err
= qemu_strtoi64(NULL
, &endptr
, 0, &res
);
2033 g_assert_cmpint(err
, ==, -EINVAL
);
2034 g_assert_cmpint(res
, ==, 999);
2035 g_assert_null(endptr
);
2038 static void test_qemu_strtoi64_empty(void)
2040 const char *str
= "";
2042 const char *endptr
= &f
;
2046 err
= qemu_strtoi64(str
, &endptr
, 0, &res
);
2048 g_assert_cmpint(err
, ==, -EINVAL
);
2049 g_assert_cmpint(res
, ==, 0);
2050 g_assert_true(endptr
== str
);
2053 static void test_qemu_strtoi64_whitespace(void)
2055 const char *str
= " \t ";
2057 const char *endptr
= &f
;
2061 err
= qemu_strtoi64(str
, &endptr
, 0, &res
);
2063 g_assert_cmpint(err
, ==, -EINVAL
);
2064 g_assert_cmpint(res
, ==, 0);
2065 g_assert_true(endptr
== str
);
2068 static void test_qemu_strtoi64_invalid(void)
2070 const char *str
= " xxxx \t abc";
2072 const char *endptr
= &f
;
2076 err
= qemu_strtoi64(str
, &endptr
, 0, &res
);
2078 g_assert_cmpint(err
, ==, -EINVAL
);
2079 g_assert_cmpint(res
, ==, 0);
2080 g_assert_true(endptr
== str
);
2083 static void test_qemu_strtoi64_trailing(void)
2085 const char *str
= "123xxx";
2087 const char *endptr
= &f
;
2091 err
= qemu_strtoi64(str
, &endptr
, 0, &res
);
2093 g_assert_cmpint(err
, ==, 0);
2094 g_assert_cmpint(res
, ==, 123);
2095 g_assert_true(endptr
== str
+ 3);
2098 static void test_qemu_strtoi64_octal(void)
2100 const char *str
= "0123";
2102 const char *endptr
= &f
;
2106 err
= qemu_strtoi64(str
, &endptr
, 8, &res
);
2108 g_assert_cmpint(err
, ==, 0);
2109 g_assert_cmpint(res
, ==, 0123);
2110 g_assert_true(endptr
== str
+ strlen(str
));
2114 err
= qemu_strtoi64(str
, &endptr
, 0, &res
);
2116 g_assert_cmpint(err
, ==, 0);
2117 g_assert_cmpint(res
, ==, 0123);
2118 g_assert_true(endptr
== str
+ strlen(str
));
2121 static void test_qemu_strtoi64_decimal(void)
2123 const char *str
= "0123";
2125 const char *endptr
= &f
;
2129 err
= qemu_strtoi64(str
, &endptr
, 10, &res
);
2131 g_assert_cmpint(err
, ==, 0);
2132 g_assert_cmpint(res
, ==, 123);
2133 g_assert_true(endptr
== str
+ strlen(str
));
2138 err
= qemu_strtoi64(str
, &endptr
, 0, &res
);
2140 g_assert_cmpint(err
, ==, 0);
2141 g_assert_cmpint(res
, ==, 123);
2142 g_assert_true(endptr
== str
+ strlen(str
));
2145 static void test_qemu_strtoi64_hex(void)
2147 const char *str
= "0123";
2149 const char *endptr
= &f
;
2153 err
= qemu_strtoi64(str
, &endptr
, 16, &res
);
2155 g_assert_cmpint(err
, ==, 0);
2156 g_assert_cmpint(res
, ==, 0x123);
2157 g_assert_true(endptr
== str
+ strlen(str
));
2162 err
= qemu_strtoi64(str
, &endptr
, 0, &res
);
2164 g_assert_cmpint(err
, ==, 0);
2165 g_assert_cmpint(res
, ==, 0x123);
2166 g_assert_true(endptr
== str
+ strlen(str
));
2171 err
= qemu_strtoi64(str
, &endptr
, 16, &res
);
2173 g_assert_cmpint(err
, ==, 0);
2174 g_assert_cmpint(res
, ==, 0);
2175 g_assert_true(endptr
== str
+ 1);
2178 static void test_qemu_strtoi64_max(void)
2180 char *str
= g_strdup_printf("%lld", LLONG_MAX
);
2182 const char *endptr
= &f
;
2186 err
= qemu_strtoi64(str
, &endptr
, 0, &res
);
2188 g_assert_cmpint(err
, ==, 0);
2189 g_assert_cmpint(res
, ==, LLONG_MAX
);
2190 g_assert_true(endptr
== str
+ strlen(str
));
2194 static void test_qemu_strtoi64_overflow(void)
2201 str
= "9223372036854775808"; /* 1 more than INT64_MAX */
2202 endptr
= "somewhere";
2204 err
= qemu_strtoi64(str
, &endptr
, 0, &res
);
2205 g_assert_cmpint(err
, ==, -ERANGE
);
2206 g_assert_cmpint(res
, ==, INT64_MAX
);
2207 g_assert_true(endptr
== str
+ strlen(str
));
2209 str
= "0x10000000000000000"; /* 65 bits, 64-bit sign bit clear */
2210 endptr
= "somewhere";
2212 err
= qemu_strtoi64(str
, &endptr
, 0, &res
);
2213 g_assert_cmpint(err
, ==, -ERANGE
);
2214 g_assert_cmpint(res
, ==, INT64_MAX
);
2215 g_assert_true(endptr
== str
+ strlen(str
));
2217 str
= "0x18000000080000000"; /* 65 bits, 64-bit sign bit set */
2218 endptr
= "somewhere";
2220 err
= qemu_strtoi64(str
, &endptr
, 0, &res
);
2221 g_assert_cmpint(err
, ==, -ERANGE
);
2222 g_assert_cmpint(res
, ==, INT64_MAX
);
2223 g_assert_true(endptr
== str
+ strlen(str
));
2226 static void test_qemu_strtoi64_min(void)
2228 char *str
= g_strdup_printf("%lld", LLONG_MIN
);
2230 const char *endptr
= &f
;
2234 err
= qemu_strtoi64(str
, &endptr
, 0, &res
);
2236 g_assert_cmpint(err
, ==, 0);
2237 g_assert_cmpint(res
, ==, LLONG_MIN
);
2238 g_assert_true(endptr
== str
+ strlen(str
));
2242 static void test_qemu_strtoi64_underflow(void)
2249 str
= "-9223372036854775809"; /* 1 less than INT64_MIN */
2250 endptr
= "somewhere";
2252 err
= qemu_strtoi64(str
, &endptr
, 0, &res
);
2253 g_assert_cmpint(err
, ==, -ERANGE
);
2254 g_assert_cmpint(res
, ==, INT64_MIN
);
2255 g_assert_true(endptr
== str
+ strlen(str
));
2257 str
= "-0x10000000000000000"; /* 65 bits, 64-bit sign bit clear */
2258 endptr
= "somewhere";
2260 err
= qemu_strtoi64(str
, &endptr
, 0, &res
);
2261 g_assert_cmpint(err
, ==, -ERANGE
);
2262 g_assert_cmpint(res
, ==, INT64_MIN
);
2263 g_assert_true(endptr
== str
+ strlen(str
));
2265 str
= "-0x18000000080000000"; /* 65 bits, 64-bit sign bit set */
2266 endptr
= "somewhere";
2268 err
= qemu_strtoi64(str
, &endptr
, 0, &res
);
2269 g_assert_cmpint(err
, ==, -ERANGE
);
2270 g_assert_cmpint(res
, ==, INT64_MIN
);
2271 g_assert_true(endptr
== str
+ strlen(str
));
2274 static void test_qemu_strtoi64_negative(void)
2276 const char *str
= " \t -321";
2278 const char *endptr
= &f
;
2282 err
= qemu_strtoi64(str
, &endptr
, 0, &res
);
2284 g_assert_cmpint(err
, ==, 0);
2285 g_assert_cmpint(res
, ==, -321);
2286 g_assert_true(endptr
== str
+ strlen(str
));
2289 static void test_qemu_strtoi64_negzero(void)
2291 const char *str
= " -0";
2293 const char *endptr
= &f
;
2297 err
= qemu_strtoi64(str
, &endptr
, 0, &res
);
2299 g_assert_cmpint(err
, ==, 0);
2300 g_assert_cmpint(res
, ==, 0);
2301 g_assert_true(endptr
== str
+ strlen(str
));
2304 static void test_qemu_strtoi64_full_correct(void)
2306 const char *str
= "123";
2310 err
= qemu_strtoi64(str
, NULL
, 0, &res
);
2312 g_assert_cmpint(err
, ==, 0);
2313 g_assert_cmpint(res
, ==, 123);
2316 static void test_qemu_strtoi64_full_null(void)
2321 err
= qemu_strtoi64(NULL
, NULL
, 0, &res
);
2323 g_assert_cmpint(err
, ==, -EINVAL
);
2324 g_assert_cmpint(res
, ==, 999);
2327 static void test_qemu_strtoi64_full_empty(void)
2329 const char *str
= "";
2333 err
= qemu_strtoi64(str
, NULL
, 0, &res
);
2335 g_assert_cmpint(err
, ==, -EINVAL
);
2336 g_assert_cmpint(res
, ==, 0);
2339 static void test_qemu_strtoi64_full_negative(void)
2341 const char *str
= " \t -321";
2345 err
= qemu_strtoi64(str
, NULL
, 0, &res
);
2347 g_assert_cmpint(err
, ==, 0);
2348 g_assert_cmpint(res
, ==, -321);
2351 static void test_qemu_strtoi64_full_negzero(void)
2353 const char *str
= " -0";
2357 err
= qemu_strtoi64(str
, NULL
, 0, &res
);
2359 g_assert_cmpint(err
, ==, 0);
2360 g_assert_cmpint(res
, ==, 0);
2363 static void test_qemu_strtoi64_full_trailing(void)
2365 const char *str
= "123xxx";
2369 err
= qemu_strtoi64(str
, NULL
, 0, &res
);
2371 g_assert_cmpint(err
, ==, -EINVAL
);
2372 g_assert_cmpint(res
, ==, 123);
2375 static void test_qemu_strtoi64_full_max(void)
2378 char *str
= g_strdup_printf("%lld", LLONG_MAX
);
2382 err
= qemu_strtoi64(str
, NULL
, 0, &res
);
2384 g_assert_cmpint(err
, ==, 0);
2385 g_assert_cmpint(res
, ==, LLONG_MAX
);
2389 static void test_qemu_strtoi64_full_erange_junk(void)
2391 /* EINVAL has priority over ERANGE */
2392 const char *str
= "-99999999999999999999junk";
2396 err
= qemu_strtoi64(str
, NULL
, 0, &res
);
2398 g_assert_cmpint(err
, ==, -EINVAL
);
2399 g_assert_cmpint(res
, ==, INT64_MIN
);
2402 static void test_qemu_strtou64_correct(void)
2404 const char *str
= "12345 foo";
2406 const char *endptr
= &f
;
2410 err
= qemu_strtou64(str
, &endptr
, 0, &res
);
2412 g_assert_cmpint(err
, ==, 0);
2413 g_assert_cmpuint(res
, ==, 12345);
2414 g_assert_true(endptr
== str
+ 5);
2417 static void test_qemu_strtou64_null(void)
2420 const char *endptr
= &f
;
2424 err
= qemu_strtou64(NULL
, &endptr
, 0, &res
);
2426 g_assert_cmpint(err
, ==, -EINVAL
);
2427 g_assert_cmpuint(res
, ==, 999);
2428 g_assert_null(endptr
);
2431 static void test_qemu_strtou64_empty(void)
2433 const char *str
= "";
2435 const char *endptr
= &f
;
2439 err
= qemu_strtou64(str
, &endptr
, 0, &res
);
2441 g_assert_cmpint(err
, ==, -EINVAL
);
2442 g_assert_cmpuint(res
, ==, 0);
2443 g_assert_true(endptr
== str
);
2446 static void test_qemu_strtou64_whitespace(void)
2448 const char *str
= " \t ";
2450 const char *endptr
= &f
;
2454 err
= qemu_strtou64(str
, &endptr
, 0, &res
);
2456 g_assert_cmpint(err
, ==, -EINVAL
);
2457 g_assert_cmpuint(res
, ==, 0);
2458 g_assert_true(endptr
== str
);
2461 static void test_qemu_strtou64_invalid(void)
2463 const char *str
= " xxxx \t abc";
2465 const char *endptr
= &f
;
2469 err
= qemu_strtou64(str
, &endptr
, 0, &res
);
2471 g_assert_cmpint(err
, ==, -EINVAL
);
2472 g_assert_cmpuint(res
, ==, 0);
2473 g_assert_true(endptr
== str
);
2476 static void test_qemu_strtou64_trailing(void)
2478 const char *str
= "123xxx";
2480 const char *endptr
= &f
;
2484 err
= qemu_strtou64(str
, &endptr
, 0, &res
);
2486 g_assert_cmpint(err
, ==, 0);
2487 g_assert_cmpuint(res
, ==, 123);
2488 g_assert_true(endptr
== str
+ 3);
2491 static void test_qemu_strtou64_octal(void)
2493 const char *str
= "0123";
2495 const char *endptr
= &f
;
2499 err
= qemu_strtou64(str
, &endptr
, 8, &res
);
2501 g_assert_cmpint(err
, ==, 0);
2502 g_assert_cmpuint(res
, ==, 0123);
2503 g_assert_true(endptr
== str
+ strlen(str
));
2507 err
= qemu_strtou64(str
, &endptr
, 0, &res
);
2509 g_assert_cmpint(err
, ==, 0);
2510 g_assert_cmpuint(res
, ==, 0123);
2511 g_assert_true(endptr
== str
+ strlen(str
));
2514 static void test_qemu_strtou64_decimal(void)
2516 const char *str
= "0123";
2518 const char *endptr
= &f
;
2522 err
= qemu_strtou64(str
, &endptr
, 10, &res
);
2524 g_assert_cmpint(err
, ==, 0);
2525 g_assert_cmpuint(res
, ==, 123);
2526 g_assert_true(endptr
== str
+ strlen(str
));
2531 err
= qemu_strtou64(str
, &endptr
, 0, &res
);
2533 g_assert_cmpint(err
, ==, 0);
2534 g_assert_cmpuint(res
, ==, 123);
2535 g_assert_true(endptr
== str
+ strlen(str
));
2538 static void test_qemu_strtou64_hex(void)
2540 const char *str
= "0123";
2542 const char *endptr
= &f
;
2546 err
= qemu_strtou64(str
, &endptr
, 16, &res
);
2548 g_assert_cmpint(err
, ==, 0);
2549 g_assert_cmphex(res
, ==, 0x123);
2550 g_assert_true(endptr
== str
+ strlen(str
));
2555 err
= qemu_strtou64(str
, &endptr
, 0, &res
);
2557 g_assert_cmpint(err
, ==, 0);
2558 g_assert_cmphex(res
, ==, 0x123);
2559 g_assert_true(endptr
== str
+ strlen(str
));
2564 err
= qemu_strtou64(str
, &endptr
, 16, &res
);
2566 g_assert_cmpint(err
, ==, 0);
2567 g_assert_cmphex(res
, ==, 0);
2568 g_assert_true(endptr
== str
+ 1);
2571 static void test_qemu_strtou64_wrap(void)
2573 const char *str
= "-18446744073709551615"; /* 1 mod 2^64 */
2575 const char *endptr
= &f
;
2579 err
= qemu_strtou64(str
, &endptr
, 0, &res
);
2581 g_assert_cmpint(err
, ==, 0);
2582 g_assert_cmpuint(res
, ==, 1);
2583 g_assert_true(endptr
== str
+ strlen(str
));
2586 static void test_qemu_strtou64_max(void)
2588 char *str
= g_strdup_printf("%llu", ULLONG_MAX
);
2590 const char *endptr
= &f
;
2594 err
= qemu_strtou64(str
, &endptr
, 0, &res
);
2596 g_assert_cmpint(err
, ==, 0);
2597 g_assert_cmphex(res
, ==, ULLONG_MAX
);
2598 g_assert_true(endptr
== str
+ strlen(str
));
2602 static void test_qemu_strtou64_overflow(void)
2609 str
= "18446744073709551616"; /* 1 more than UINT64_MAX */
2610 endptr
= "somewhere";
2612 err
= qemu_strtou64(str
, &endptr
, 0, &res
);
2613 g_assert_cmpint(err
, ==, -ERANGE
);
2614 g_assert_cmpuint(res
, ==, UINT64_MAX
);
2615 g_assert_true(endptr
== str
+ strlen(str
));
2617 str
= "0x10000000000000000"; /* 65 bits, 64-bit sign bit clear */
2618 endptr
= "somewhere";
2620 err
= qemu_strtou64(str
, &endptr
, 0, &res
);
2621 g_assert_cmpint(err
, ==, -ERANGE
);
2622 g_assert_cmpuint(res
, ==, UINT64_MAX
);
2623 g_assert_true(endptr
== str
+ strlen(str
));
2625 str
= "0x18000000080000000"; /* 65 bits, 64-bit sign bit set */
2626 endptr
= "somewhere";
2628 err
= qemu_strtou64(str
, &endptr
, 0, &res
);
2629 g_assert_cmpint(err
, ==, -ERANGE
);
2630 g_assert_cmpuint(res
, ==, UINT64_MAX
);
2631 g_assert_true(endptr
== str
+ strlen(str
));
2634 static void test_qemu_strtou64_underflow(void)
2641 str
= "-99999999999999999999999999999999999999999999";
2642 endptr
= "somewhere";
2644 err
= qemu_strtou64(str
, &endptr
, 0, &res
);
2645 g_assert_cmpint(err
, ==, -ERANGE
);
2646 g_assert_cmpuint(res
, ==, UINT64_MAX
);
2647 g_assert_true(endptr
== str
+ strlen(str
));
2649 str
= "-0x10000000000000000"; /* 65 bits, 64-bit sign bit clear */
2650 endptr
= "somewhere";
2652 err
= qemu_strtou64(str
, &endptr
, 0, &res
);
2653 g_assert_cmpint(err
, ==, -ERANGE
);
2654 g_assert_cmpuint(res
, ==, UINT64_MAX
);
2655 g_assert_true(endptr
== str
+ strlen(str
));
2657 str
= "-0x18000000080000000"; /* 65 bits, 64-bit sign bit set */
2658 endptr
= "somewhere";
2660 err
= qemu_strtou64(str
, &endptr
, 0, &res
);
2661 g_assert_cmpint(err
, ==, -ERANGE
);
2662 g_assert_cmpuint(res
, ==, UINT64_MAX
);
2663 g_assert_true(endptr
== str
+ strlen(str
));
2666 static void test_qemu_strtou64_negative(void)
2668 const char *str
= " \t -321";
2670 const char *endptr
= &f
;
2674 err
= qemu_strtou64(str
, &endptr
, 0, &res
);
2676 g_assert_cmpint(err
, ==, 0);
2677 g_assert_cmpuint(res
, ==, -321ull);
2678 g_assert_true(endptr
== str
+ strlen(str
));
2681 static void test_qemu_strtou64_negzero(void)
2683 const char *str
= " -0";
2685 const char *endptr
= &f
;
2689 err
= qemu_strtou64(str
, &endptr
, 0, &res
);
2691 g_assert_cmpint(err
, ==, 0);
2692 g_assert_cmpuint(res
, ==, 0);
2693 g_assert_true(endptr
== str
+ strlen(str
));
2696 static void test_qemu_strtou64_full_correct(void)
2698 const char *str
= "18446744073709551614";
2702 err
= qemu_strtou64(str
, NULL
, 0, &res
);
2704 g_assert_cmpint(err
, ==, 0);
2705 g_assert_cmpuint(res
, ==, 18446744073709551614ull);
2708 static void test_qemu_strtou64_full_null(void)
2713 err
= qemu_strtou64(NULL
, NULL
, 0, &res
);
2715 g_assert_cmpint(err
, ==, -EINVAL
);
2716 g_assert_cmpuint(res
, ==, 999);
2719 static void test_qemu_strtou64_full_empty(void)
2721 const char *str
= "";
2725 err
= qemu_strtou64(str
, NULL
, 0, &res
);
2727 g_assert_cmpint(err
, ==, -EINVAL
);
2728 g_assert_cmpuint(res
, ==, 0);
2731 static void test_qemu_strtou64_full_negative(void)
2733 const char *str
= " \t -321";
2737 err
= qemu_strtou64(str
, NULL
, 0, &res
);
2739 g_assert_cmpint(err
, ==, 0);
2740 g_assert_cmpuint(res
, ==, -321ull);
2743 static void test_qemu_strtou64_full_negzero(void)
2745 const char *str
= " -0";
2749 err
= qemu_strtou64(str
, NULL
, 0, &res
);
2751 g_assert_cmpint(err
, ==, 0);
2752 g_assert_cmpuint(res
, ==, 0);
2755 static void test_qemu_strtou64_full_trailing(void)
2757 const char *str
= "18446744073709551614xxxxxx";
2761 err
= qemu_strtou64(str
, NULL
, 0, &res
);
2763 g_assert_cmpint(err
, ==, -EINVAL
);
2764 g_assert_cmpuint(res
, ==, 18446744073709551614ULL);
2767 static void test_qemu_strtou64_full_max(void)
2769 char *str
= g_strdup_printf("%lld", ULLONG_MAX
);
2773 err
= qemu_strtou64(str
, NULL
, 0, &res
);
2775 g_assert_cmpint(err
, ==, 0);
2776 g_assert_cmphex(res
, ==, ULLONG_MAX
);
2780 static void test_qemu_strtou64_full_erange_junk(void)
2782 /* EINVAL has priority over ERANGE */
2783 const char *str
= "-99999999999999999999junk";
2787 err
= qemu_strtou64(str
, NULL
, 0, &res
);
2789 g_assert_cmpint(err
, ==, -EINVAL
);
2790 g_assert_cmpuint(res
, ==, UINT64_MAX
);
2793 static void test_qemu_strtosz_simple(void)
2803 err
= qemu_strtosz(str
, &endptr
, &res
);
2804 g_assert_cmpint(err
, ==, 0);
2805 g_assert_cmpuint(res
, ==, 0);
2806 g_assert_true(endptr
== str
+ 1);
2808 /* Leading 0 gives decimal results, not octal */
2812 err
= qemu_strtosz(str
, &endptr
, &res
);
2813 g_assert_cmpint(err
, ==, 0);
2814 g_assert_cmpuint(res
, ==, 8);
2815 g_assert_true(endptr
== str
+ 2);
2817 /* Leading space is ignored */
2821 err
= qemu_strtosz(str
, &endptr
, &res
);
2822 g_assert_cmpint(err
, ==, 0);
2823 g_assert_cmpuint(res
, ==, 12345);
2824 g_assert_true(endptr
== str
+ 6);
2827 err
= qemu_strtosz(str
, NULL
, &res
);
2828 g_assert_cmpint(err
, ==, 0);
2829 g_assert_cmpuint(res
, ==, 12345);
2831 str
= "9007199254740991"; /* 2^53-1 */
2834 err
= qemu_strtosz(str
, &endptr
, &res
);
2835 g_assert_cmpint(err
, ==, 0);
2836 g_assert_cmphex(res
, ==, 0x1fffffffffffffULL
);
2837 g_assert_true(endptr
== str
+ 16);
2839 str
= "9007199254740992"; /* 2^53 */
2842 err
= qemu_strtosz(str
, &endptr
, &res
);
2843 g_assert_cmpint(err
, ==, 0);
2844 g_assert_cmphex(res
, ==, 0x20000000000000ULL
);
2845 g_assert_true(endptr
== str
+ 16);
2847 str
= "9007199254740993"; /* 2^53+1 */
2850 err
= qemu_strtosz(str
, &endptr
, &res
);
2851 g_assert_cmpint(err
, ==, 0);
2852 g_assert_cmphex(res
, ==, 0x20000000000001ULL
);
2853 g_assert_true(endptr
== str
+ 16);
2855 str
= "18446744073709549568"; /* 0xfffffffffffff800 (53 msbs set) */
2858 err
= qemu_strtosz(str
, &endptr
, &res
);
2859 g_assert_cmpint(err
, ==, 0);
2860 g_assert_cmphex(res
, ==, 0xfffffffffffff800ULL
);
2861 g_assert_true(endptr
== str
+ 20);
2863 str
= "18446744073709550591"; /* 0xfffffffffffffbff */
2866 err
= qemu_strtosz(str
, &endptr
, &res
);
2867 g_assert_cmpint(err
, ==, 0);
2868 g_assert_cmphex(res
, ==, 0xfffffffffffffbffULL
);
2869 g_assert_true(endptr
== str
+ 20);
2871 str
= "18446744073709551615"; /* 0xffffffffffffffff */
2874 err
= qemu_strtosz(str
, &endptr
, &res
);
2875 g_assert_cmpint(err
, ==, 0);
2876 g_assert_cmphex(res
, ==, 0xffffffffffffffffULL
);
2877 g_assert_true(endptr
== str
+ 20);
2880 static void test_qemu_strtosz_hex(void)
2890 err
= qemu_strtosz(str
, &endptr
, &res
);
2891 g_assert_cmpint(err
, ==, 0);
2892 g_assert_cmpuint(res
, ==, 0);
2893 g_assert_true(endptr
== str
+ 3);
2898 err
= qemu_strtosz(str
, &endptr
, &res
);
2899 g_assert_cmpint(err
, ==, 0);
2900 g_assert_cmpuint(res
, ==, 171);
2901 g_assert_true(endptr
== str
+ 4);
2906 err
= qemu_strtosz(str
, &endptr
, &res
);
2907 g_assert_cmpint(err
, ==, 0);
2908 g_assert_cmpuint(res
, ==, 174);
2909 g_assert_true(endptr
== str
+ 4);
2912 static void test_qemu_strtosz_units(void)
2914 const char *none
= "1";
2915 const char *b
= "1B";
2916 const char *k
= "1K";
2917 const char *m
= "1M";
2918 const char *g
= "1G";
2919 const char *t
= "1T";
2920 const char *p
= "1P";
2921 const char *e
= "1E";
2929 err
= qemu_strtosz_MiB(none
, &endptr
, &res
);
2930 g_assert_cmpint(err
, ==, 0);
2931 g_assert_cmpuint(res
, ==, MiB
);
2932 g_assert_true(endptr
== none
+ 1);
2936 err
= qemu_strtosz(b
, &endptr
, &res
);
2937 g_assert_cmpint(err
, ==, 0);
2938 g_assert_cmpuint(res
, ==, 1);
2939 g_assert_true(endptr
== b
+ 2);
2943 err
= qemu_strtosz(k
, &endptr
, &res
);
2944 g_assert_cmpint(err
, ==, 0);
2945 g_assert_cmpuint(res
, ==, KiB
);
2946 g_assert_true(endptr
== k
+ 2);
2950 err
= qemu_strtosz(m
, &endptr
, &res
);
2951 g_assert_cmpint(err
, ==, 0);
2952 g_assert_cmpuint(res
, ==, MiB
);
2953 g_assert_true(endptr
== m
+ 2);
2957 err
= qemu_strtosz(g
, &endptr
, &res
);
2958 g_assert_cmpint(err
, ==, 0);
2959 g_assert_cmpuint(res
, ==, GiB
);
2960 g_assert_true(endptr
== g
+ 2);
2964 err
= qemu_strtosz(t
, &endptr
, &res
);
2965 g_assert_cmpint(err
, ==, 0);
2966 g_assert_cmpuint(res
, ==, TiB
);
2967 g_assert_true(endptr
== t
+ 2);
2971 err
= qemu_strtosz(p
, &endptr
, &res
);
2972 g_assert_cmpint(err
, ==, 0);
2973 g_assert_cmpuint(res
, ==, PiB
);
2974 g_assert_true(endptr
== p
+ 2);
2978 err
= qemu_strtosz(e
, &endptr
, &res
);
2979 g_assert_cmpint(err
, ==, 0);
2980 g_assert_cmpuint(res
, ==, EiB
);
2981 g_assert_true(endptr
== e
+ 2);
2984 static void test_qemu_strtosz_float(void)
2994 err
= qemu_strtosz(str
, &endptr
, &res
);
2995 g_assert_cmpint(err
, ==, 0);
2996 g_assert_cmpuint(res
, ==, EiB
/ 2);
2997 g_assert_true(endptr
== str
+ 4);
2999 /* For convenience, a fraction of 0 is tolerated even on bytes */
3003 err
= qemu_strtosz(str
, &endptr
, &res
);
3004 g_assert_cmpint(err
, ==, 0);
3005 g_assert_cmpuint(res
, ==, 1);
3006 g_assert_true(endptr
== str
+ 4);
3008 /* An empty fraction is tolerated */
3012 err
= qemu_strtosz(str
, &endptr
, &res
);
3013 g_assert_cmpint(err
, ==, 0);
3014 g_assert_cmpuint(res
, ==, 1024);
3015 g_assert_true(endptr
== str
+ 3);
3017 /* For convenience, we permit values that are not byte-exact */
3021 err
= qemu_strtosz(str
, &endptr
, &res
);
3022 g_assert_cmpint(err
, ==, 0);
3023 g_assert_cmpuint(res
, ==, (uint64_t) (12.345 * MiB
+ 0.5));
3024 g_assert_true(endptr
== str
+ 7);
3027 static void test_qemu_strtosz_invalid(void)
3032 uint64_t res
= 0xbaadf00d;
3036 err
= qemu_strtosz(str
, &endptr
, &res
);
3037 g_assert_cmpint(err
, ==, -EINVAL
);
3038 g_assert_cmphex(res
, ==, 0xbaadf00d);
3039 g_assert_true(endptr
== str
);
3043 err
= qemu_strtosz(str
, &endptr
, &res
);
3044 g_assert_cmpint(err
, ==, -EINVAL
);
3045 g_assert_cmphex(res
, ==, 0xbaadf00d);
3046 g_assert_true(endptr
== str
);
3050 err
= qemu_strtosz(str
, &endptr
, &res
);
3051 g_assert_cmpint(err
, ==, -EINVAL
);
3052 g_assert_cmphex(res
, ==, 0xbaadf00d);
3053 g_assert_true(endptr
== str
);
3057 err
= qemu_strtosz(str
, &endptr
, &res
);
3058 g_assert_cmpint(err
, ==, -EINVAL
);
3059 g_assert_cmphex(res
, ==, 0xbaadf00d);
3060 g_assert_true(endptr
== str
);
3064 err
= qemu_strtosz(str
, &endptr
, &res
);
3065 g_assert_cmpint(err
, ==, -EINVAL
);
3066 g_assert_cmphex(res
, ==, 0xbaadf00d);
3067 g_assert_true(endptr
== str
);
3069 /* Fractional values require scale larger than bytes */
3072 err
= qemu_strtosz(str
, &endptr
, &res
);
3073 g_assert_cmpint(err
, ==, -EINVAL
);
3074 g_assert_cmphex(res
, ==, 0xbaadf00d);
3075 g_assert_true(endptr
== str
);
3079 err
= qemu_strtosz(str
, &endptr
, &res
);
3080 g_assert_cmpint(err
, ==, -EINVAL
);
3081 g_assert_cmphex(res
, ==, 0xbaadf00d);
3082 g_assert_true(endptr
== str
);
3084 /* No floating point exponents */
3087 err
= qemu_strtosz(str
, &endptr
, &res
);
3088 g_assert_cmpint(err
, ==, -EINVAL
);
3089 g_assert_cmphex(res
, ==, 0xbaadf00d);
3090 g_assert_true(endptr
== str
);
3094 err
= qemu_strtosz(str
, &endptr
, &res
);
3095 g_assert_cmpint(err
, ==, -EINVAL
);
3096 g_assert_cmphex(res
, ==, 0xbaadf00d);
3097 g_assert_true(endptr
== str
);
3099 /* No hex fractions */
3102 err
= qemu_strtosz(str
, &endptr
, &res
);
3103 g_assert_cmpint(err
, ==, -EINVAL
);
3104 g_assert_cmphex(res
, ==, 0xbaadf00d);
3105 g_assert_true(endptr
== str
);
3110 err
= qemu_strtosz(str
, &endptr
, &res
);
3111 g_assert_cmpint(err
, ==, -EINVAL
);
3112 g_assert_cmphex(res
, ==, 0xbaadf00d);
3113 g_assert_true(endptr
== str
);
3115 /* No negative values */
3118 err
= qemu_strtosz(str
, &endptr
, &res
);
3119 g_assert_cmpint(err
, ==, -EINVAL
);
3120 g_assert_cmphex(res
, ==, 0xbaadf00d);
3121 g_assert_true(endptr
== str
);
3125 err
= qemu_strtosz(str
, &endptr
, &res
);
3126 g_assert_cmpint(err
, ==, -EINVAL
);
3127 g_assert_cmphex(res
, ==, 0xbaadf00d);
3128 g_assert_true(endptr
== str
);
3131 static void test_qemu_strtosz_trailing(void)
3141 err
= qemu_strtosz_MiB(str
, &endptr
, &res
);
3142 g_assert_cmpint(err
, ==, 0);
3143 g_assert_cmpuint(res
, ==, 123 * MiB
);
3144 g_assert_true(endptr
== str
+ 3);
3147 err
= qemu_strtosz(str
, NULL
, &res
);
3148 g_assert_cmpint(err
, ==, -EINVAL
);
3149 g_assert_cmphex(res
, ==, 0xbaadf00d);
3154 err
= qemu_strtosz(str
, &endptr
, &res
);
3155 g_assert_cmpint(err
, ==, 0);
3156 g_assert_cmpuint(res
, ==, 1024);
3157 g_assert_true(endptr
== str
+ 2);
3160 err
= qemu_strtosz(str
, NULL
, &res
);
3161 g_assert_cmpint(err
, ==, -EINVAL
);
3162 g_assert_cmphex(res
, ==, 0xbaadf00d);
3167 err
= qemu_strtosz(str
, &endptr
, &res
);
3168 g_assert_cmpint(err
, ==, 0);
3169 g_assert_cmpuint(res
, ==, 0);
3170 g_assert_true(endptr
== str
+ 1);
3173 err
= qemu_strtosz(str
, NULL
, &res
);
3174 g_assert_cmpint(err
, ==, -EINVAL
);
3175 g_assert_cmphex(res
, ==, 0xbaadf00d);
3180 err
= qemu_strtosz(str
, &endptr
, &res
);
3181 g_assert_cmpint(err
, ==, 0);
3182 g_assert_cmpuint(res
, ==, 0);
3183 g_assert_true(endptr
== str
+ 2);
3186 err
= qemu_strtosz(str
, NULL
, &res
);
3187 g_assert_cmpint(err
, ==, -EINVAL
);
3188 g_assert_cmphex(res
, ==, 0xbaadf00d);
3193 err
= qemu_strtosz(str
, &endptr
, &res
);
3194 g_assert_cmpint(err
, ==, 0);
3195 g_assert_cmpuint(res
, ==, 123);
3196 g_assert_true(endptr
== str
+ 3);
3199 err
= qemu_strtosz(str
, NULL
, &res
);
3200 g_assert_cmpint(err
, ==, -EINVAL
);
3201 g_assert_cmphex(res
, ==, 0xbaadf00d);
3204 static void test_qemu_strtosz_erange(void)
3209 uint64_t res
= 0xbaadf00d;
3211 str
= "18446744073709551616"; /* 2^64; see strtosz_simple for 2^64-1 */
3213 err
= qemu_strtosz(str
, &endptr
, &res
);
3214 g_assert_cmpint(err
, ==, -ERANGE
);
3215 g_assert_cmphex(res
, ==, 0xbaadf00d);
3216 g_assert_true(endptr
== str
+ 20);
3220 err
= qemu_strtosz(str
, &endptr
, &res
);
3221 g_assert_cmpint(err
, ==, -ERANGE
);
3222 g_assert_cmphex(res
, ==, 0xbaadf00d);
3223 g_assert_true(endptr
== str
+ 3);
3226 static void test_qemu_strtosz_metric(void)
3236 err
= qemu_strtosz_metric(str
, &endptr
, &res
);
3237 g_assert_cmpint(err
, ==, 0);
3238 g_assert_cmpuint(res
, ==, 12345000);
3239 g_assert_true(endptr
== str
+ 6);
3244 err
= qemu_strtosz_metric(str
, &endptr
, &res
);
3245 g_assert_cmpint(err
, ==, 0);
3246 g_assert_cmpuint(res
, ==, 12345000);
3247 g_assert_true(endptr
== str
+ 7);
3250 static void test_freq_to_str(void)
3254 str
= freq_to_str(999);
3255 g_assert_cmpstr(str
, ==, "999 Hz");
3258 str
= freq_to_str(1000);
3259 g_assert_cmpstr(str
, ==, "1 KHz");
3262 str
= freq_to_str(1010);
3263 g_assert_cmpstr(str
, ==, "1.01 KHz");
3267 static void test_size_to_str(void)
3271 str
= size_to_str(0);
3272 g_assert_cmpstr(str
, ==, "0 B");
3275 str
= size_to_str(1);
3276 g_assert_cmpstr(str
, ==, "1 B");
3279 str
= size_to_str(1016);
3280 g_assert_cmpstr(str
, ==, "0.992 KiB");
3283 str
= size_to_str(1024);
3284 g_assert_cmpstr(str
, ==, "1 KiB");
3287 str
= size_to_str(512ull << 20);
3288 g_assert_cmpstr(str
, ==, "512 MiB");
3292 static void test_iec_binary_prefix(void)
3294 g_assert_cmpstr(iec_binary_prefix(0), ==, "");
3295 g_assert_cmpstr(iec_binary_prefix(10), ==, "Ki");
3296 g_assert_cmpstr(iec_binary_prefix(20), ==, "Mi");
3297 g_assert_cmpstr(iec_binary_prefix(30), ==, "Gi");
3298 g_assert_cmpstr(iec_binary_prefix(40), ==, "Ti");
3299 g_assert_cmpstr(iec_binary_prefix(50), ==, "Pi");
3300 g_assert_cmpstr(iec_binary_prefix(60), ==, "Ei");
3303 static void test_si_prefix(void)
3305 g_assert_cmpstr(si_prefix(-18), ==, "a");
3306 g_assert_cmpstr(si_prefix(-15), ==, "f");
3307 g_assert_cmpstr(si_prefix(-12), ==, "p");
3308 g_assert_cmpstr(si_prefix(-9), ==, "n");
3309 g_assert_cmpstr(si_prefix(-6), ==, "u");
3310 g_assert_cmpstr(si_prefix(-3), ==, "m");
3311 g_assert_cmpstr(si_prefix(0), ==, "");
3312 g_assert_cmpstr(si_prefix(3), ==, "K");
3313 g_assert_cmpstr(si_prefix(6), ==, "M");
3314 g_assert_cmpstr(si_prefix(9), ==, "G");
3315 g_assert_cmpstr(si_prefix(12), ==, "T");
3316 g_assert_cmpstr(si_prefix(15), ==, "P");
3317 g_assert_cmpstr(si_prefix(18), ==, "E");
3320 int main(int argc
, char **argv
)
3322 g_test_init(&argc
, &argv
, NULL
);
3324 g_test_add_func("/cutils/parse_uint/null", test_parse_uint_null
);
3325 g_test_add_func("/cutils/parse_uint/empty", test_parse_uint_empty
);
3326 g_test_add_func("/cutils/parse_uint/whitespace",
3327 test_parse_uint_whitespace
);
3328 g_test_add_func("/cutils/parse_uint/invalid", test_parse_uint_invalid
);
3329 g_test_add_func("/cutils/parse_uint/trailing", test_parse_uint_trailing
);
3330 g_test_add_func("/cutils/parse_uint/correct", test_parse_uint_correct
);
3331 g_test_add_func("/cutils/parse_uint/octal", test_parse_uint_octal
);
3332 g_test_add_func("/cutils/parse_uint/decimal", test_parse_uint_decimal
);
3333 g_test_add_func("/cutils/parse_uint/llong_max", test_parse_uint_llong_max
);
3334 g_test_add_func("/cutils/parse_uint/max", test_parse_uint_max
);
3335 g_test_add_func("/cutils/parse_uint/overflow", test_parse_uint_overflow
);
3336 g_test_add_func("/cutils/parse_uint/negative", test_parse_uint_negative
);
3337 g_test_add_func("/cutils/parse_uint/negzero", test_parse_uint_negzero
);
3338 g_test_add_func("/cutils/parse_uint_full/trailing",
3339 test_parse_uint_full_trailing
);
3340 g_test_add_func("/cutils/parse_uint_full/correct",
3341 test_parse_uint_full_correct
);
3342 g_test_add_func("/cutils/parse_uint_full/erange_junk",
3343 test_parse_uint_full_erange_junk
);
3345 /* qemu_strtoi() tests */
3346 g_test_add_func("/cutils/qemu_strtoi/correct",
3347 test_qemu_strtoi_correct
);
3348 g_test_add_func("/cutils/qemu_strtoi/null",
3349 test_qemu_strtoi_null
);
3350 g_test_add_func("/cutils/qemu_strtoi/empty",
3351 test_qemu_strtoi_empty
);
3352 g_test_add_func("/cutils/qemu_strtoi/whitespace",
3353 test_qemu_strtoi_whitespace
);
3354 g_test_add_func("/cutils/qemu_strtoi/invalid",
3355 test_qemu_strtoi_invalid
);
3356 g_test_add_func("/cutils/qemu_strtoi/trailing",
3357 test_qemu_strtoi_trailing
);
3358 g_test_add_func("/cutils/qemu_strtoi/octal",
3359 test_qemu_strtoi_octal
);
3360 g_test_add_func("/cutils/qemu_strtoi/decimal",
3361 test_qemu_strtoi_decimal
);
3362 g_test_add_func("/cutils/qemu_strtoi/hex",
3363 test_qemu_strtoi_hex
);
3364 g_test_add_func("/cutils/qemu_strtoi/max",
3365 test_qemu_strtoi_max
);
3366 g_test_add_func("/cutils/qemu_strtoi/overflow",
3367 test_qemu_strtoi_overflow
);
3368 g_test_add_func("/cutils/qemu_strtoi/min",
3369 test_qemu_strtoi_min
);
3370 g_test_add_func("/cutils/qemu_strtoi/underflow",
3371 test_qemu_strtoi_underflow
);
3372 g_test_add_func("/cutils/qemu_strtoi/negative",
3373 test_qemu_strtoi_negative
);
3374 g_test_add_func("/cutils/qemu_strtoi/negzero",
3375 test_qemu_strtoi_negzero
);
3376 g_test_add_func("/cutils/qemu_strtoi_full/correct",
3377 test_qemu_strtoi_full_correct
);
3378 g_test_add_func("/cutils/qemu_strtoi_full/null",
3379 test_qemu_strtoi_full_null
);
3380 g_test_add_func("/cutils/qemu_strtoi_full/empty",
3381 test_qemu_strtoi_full_empty
);
3382 g_test_add_func("/cutils/qemu_strtoi_full/negative",
3383 test_qemu_strtoi_full_negative
);
3384 g_test_add_func("/cutils/qemu_strtoi_full/negzero",
3385 test_qemu_strtoi_full_negzero
);
3386 g_test_add_func("/cutils/qemu_strtoi_full/trailing",
3387 test_qemu_strtoi_full_trailing
);
3388 g_test_add_func("/cutils/qemu_strtoi_full/max",
3389 test_qemu_strtoi_full_max
);
3390 g_test_add_func("/cutils/qemu_strtoi_full/erange_junk",
3391 test_qemu_strtoi_full_erange_junk
);
3393 /* qemu_strtoui() tests */
3394 g_test_add_func("/cutils/qemu_strtoui/correct",
3395 test_qemu_strtoui_correct
);
3396 g_test_add_func("/cutils/qemu_strtoui/null",
3397 test_qemu_strtoui_null
);
3398 g_test_add_func("/cutils/qemu_strtoui/empty",
3399 test_qemu_strtoui_empty
);
3400 g_test_add_func("/cutils/qemu_strtoui/whitespace",
3401 test_qemu_strtoui_whitespace
);
3402 g_test_add_func("/cutils/qemu_strtoui/invalid",
3403 test_qemu_strtoui_invalid
);
3404 g_test_add_func("/cutils/qemu_strtoui/trailing",
3405 test_qemu_strtoui_trailing
);
3406 g_test_add_func("/cutils/qemu_strtoui/octal",
3407 test_qemu_strtoui_octal
);
3408 g_test_add_func("/cutils/qemu_strtoui/decimal",
3409 test_qemu_strtoui_decimal
);
3410 g_test_add_func("/cutils/qemu_strtoui/hex",
3411 test_qemu_strtoui_hex
);
3412 g_test_add_func("/cutils/qemu_strtoui/wrap",
3413 test_qemu_strtoui_wrap
);
3414 g_test_add_func("/cutils/qemu_strtoui/max",
3415 test_qemu_strtoui_max
);
3416 g_test_add_func("/cutils/qemu_strtoui/overflow",
3417 test_qemu_strtoui_overflow
);
3418 g_test_add_func("/cutils/qemu_strtoui/underflow",
3419 test_qemu_strtoui_underflow
);
3420 g_test_add_func("/cutils/qemu_strtoui/negative",
3421 test_qemu_strtoui_negative
);
3422 g_test_add_func("/cutils/qemu_strtoui/negzero",
3423 test_qemu_strtoui_negzero
);
3424 g_test_add_func("/cutils/qemu_strtoui_full/correct",
3425 test_qemu_strtoui_full_correct
);
3426 g_test_add_func("/cutils/qemu_strtoui_full/null",
3427 test_qemu_strtoui_full_null
);
3428 g_test_add_func("/cutils/qemu_strtoui_full/empty",
3429 test_qemu_strtoui_full_empty
);
3430 g_test_add_func("/cutils/qemu_strtoui_full/negative",
3431 test_qemu_strtoui_full_negative
);
3432 g_test_add_func("/cutils/qemu_strtoui_full/negzero",
3433 test_qemu_strtoui_full_negzero
);
3434 g_test_add_func("/cutils/qemu_strtoui_full/trailing",
3435 test_qemu_strtoui_full_trailing
);
3436 g_test_add_func("/cutils/qemu_strtoui_full/max",
3437 test_qemu_strtoui_full_max
);
3438 g_test_add_func("/cutils/qemu_strtoui_full/erange_junk",
3439 test_qemu_strtoui_full_erange_junk
);
3441 /* qemu_strtol() tests */
3442 g_test_add_func("/cutils/qemu_strtol/correct",
3443 test_qemu_strtol_correct
);
3444 g_test_add_func("/cutils/qemu_strtol/null",
3445 test_qemu_strtol_null
);
3446 g_test_add_func("/cutils/qemu_strtol/empty",
3447 test_qemu_strtol_empty
);
3448 g_test_add_func("/cutils/qemu_strtol/whitespace",
3449 test_qemu_strtol_whitespace
);
3450 g_test_add_func("/cutils/qemu_strtol/invalid",
3451 test_qemu_strtol_invalid
);
3452 g_test_add_func("/cutils/qemu_strtol/trailing",
3453 test_qemu_strtol_trailing
);
3454 g_test_add_func("/cutils/qemu_strtol/octal",
3455 test_qemu_strtol_octal
);
3456 g_test_add_func("/cutils/qemu_strtol/decimal",
3457 test_qemu_strtol_decimal
);
3458 g_test_add_func("/cutils/qemu_strtol/hex",
3459 test_qemu_strtol_hex
);
3460 g_test_add_func("/cutils/qemu_strtol/max",
3461 test_qemu_strtol_max
);
3462 g_test_add_func("/cutils/qemu_strtol/overflow",
3463 test_qemu_strtol_overflow
);
3464 g_test_add_func("/cutils/qemu_strtol/min",
3465 test_qemu_strtol_min
);
3466 g_test_add_func("/cutils/qemu_strtol/underflow",
3467 test_qemu_strtol_underflow
);
3468 g_test_add_func("/cutils/qemu_strtol/negative",
3469 test_qemu_strtol_negative
);
3470 g_test_add_func("/cutils/qemu_strtol/negzero",
3471 test_qemu_strtol_negzero
);
3472 g_test_add_func("/cutils/qemu_strtol_full/correct",
3473 test_qemu_strtol_full_correct
);
3474 g_test_add_func("/cutils/qemu_strtol_full/null",
3475 test_qemu_strtol_full_null
);
3476 g_test_add_func("/cutils/qemu_strtol_full/empty",
3477 test_qemu_strtol_full_empty
);
3478 g_test_add_func("/cutils/qemu_strtol_full/negative",
3479 test_qemu_strtol_full_negative
);
3480 g_test_add_func("/cutils/qemu_strtol_full/negzero",
3481 test_qemu_strtol_full_negzero
);
3482 g_test_add_func("/cutils/qemu_strtol_full/trailing",
3483 test_qemu_strtol_full_trailing
);
3484 g_test_add_func("/cutils/qemu_strtol_full/max",
3485 test_qemu_strtol_full_max
);
3486 g_test_add_func("/cutils/qemu_strtol_full/erange_junk",
3487 test_qemu_strtol_full_erange_junk
);
3489 /* qemu_strtoul() tests */
3490 g_test_add_func("/cutils/qemu_strtoul/correct",
3491 test_qemu_strtoul_correct
);
3492 g_test_add_func("/cutils/qemu_strtoul/null",
3493 test_qemu_strtoul_null
);
3494 g_test_add_func("/cutils/qemu_strtoul/empty",
3495 test_qemu_strtoul_empty
);
3496 g_test_add_func("/cutils/qemu_strtoul/whitespace",
3497 test_qemu_strtoul_whitespace
);
3498 g_test_add_func("/cutils/qemu_strtoul/invalid",
3499 test_qemu_strtoul_invalid
);
3500 g_test_add_func("/cutils/qemu_strtoul/trailing",
3501 test_qemu_strtoul_trailing
);
3502 g_test_add_func("/cutils/qemu_strtoul/octal",
3503 test_qemu_strtoul_octal
);
3504 g_test_add_func("/cutils/qemu_strtoul/decimal",
3505 test_qemu_strtoul_decimal
);
3506 g_test_add_func("/cutils/qemu_strtoul/hex",
3507 test_qemu_strtoul_hex
);
3508 g_test_add_func("/cutils/qemu_strtoul/wrap",
3509 test_qemu_strtoul_wrap
);
3510 g_test_add_func("/cutils/qemu_strtoul/max",
3511 test_qemu_strtoul_max
);
3512 g_test_add_func("/cutils/qemu_strtoul/overflow",
3513 test_qemu_strtoul_overflow
);
3514 g_test_add_func("/cutils/qemu_strtoul/underflow",
3515 test_qemu_strtoul_underflow
);
3516 g_test_add_func("/cutils/qemu_strtoul/negative",
3517 test_qemu_strtoul_negative
);
3518 g_test_add_func("/cutils/qemu_strtoul/negzero",
3519 test_qemu_strtoul_negzero
);
3520 g_test_add_func("/cutils/qemu_strtoul_full/correct",
3521 test_qemu_strtoul_full_correct
);
3522 g_test_add_func("/cutils/qemu_strtoul_full/null",
3523 test_qemu_strtoul_full_null
);
3524 g_test_add_func("/cutils/qemu_strtoul_full/empty",
3525 test_qemu_strtoul_full_empty
);
3526 g_test_add_func("/cutils/qemu_strtoul_full/negative",
3527 test_qemu_strtoul_full_negative
);
3528 g_test_add_func("/cutils/qemu_strtoul_full/negzero",
3529 test_qemu_strtoul_full_negzero
);
3530 g_test_add_func("/cutils/qemu_strtoul_full/trailing",
3531 test_qemu_strtoul_full_trailing
);
3532 g_test_add_func("/cutils/qemu_strtoul_full/max",
3533 test_qemu_strtoul_full_max
);
3534 g_test_add_func("/cutils/qemu_strtoul_full/erange_junk",
3535 test_qemu_strtoul_full_erange_junk
);
3537 /* qemu_strtoi64() tests */
3538 g_test_add_func("/cutils/qemu_strtoi64/correct",
3539 test_qemu_strtoi64_correct
);
3540 g_test_add_func("/cutils/qemu_strtoi64/null",
3541 test_qemu_strtoi64_null
);
3542 g_test_add_func("/cutils/qemu_strtoi64/empty",
3543 test_qemu_strtoi64_empty
);
3544 g_test_add_func("/cutils/qemu_strtoi64/whitespace",
3545 test_qemu_strtoi64_whitespace
);
3546 g_test_add_func("/cutils/qemu_strtoi64/invalid",
3547 test_qemu_strtoi64_invalid
);
3548 g_test_add_func("/cutils/qemu_strtoi64/trailing",
3549 test_qemu_strtoi64_trailing
);
3550 g_test_add_func("/cutils/qemu_strtoi64/octal",
3551 test_qemu_strtoi64_octal
);
3552 g_test_add_func("/cutils/qemu_strtoi64/decimal",
3553 test_qemu_strtoi64_decimal
);
3554 g_test_add_func("/cutils/qemu_strtoi64/hex",
3555 test_qemu_strtoi64_hex
);
3556 g_test_add_func("/cutils/qemu_strtoi64/max",
3557 test_qemu_strtoi64_max
);
3558 g_test_add_func("/cutils/qemu_strtoi64/overflow",
3559 test_qemu_strtoi64_overflow
);
3560 g_test_add_func("/cutils/qemu_strtoi64/min",
3561 test_qemu_strtoi64_min
);
3562 g_test_add_func("/cutils/qemu_strtoi64/underflow",
3563 test_qemu_strtoi64_underflow
);
3564 g_test_add_func("/cutils/qemu_strtoi64/negative",
3565 test_qemu_strtoi64_negative
);
3566 g_test_add_func("/cutils/qemu_strtoi64/negzero",
3567 test_qemu_strtoi64_negzero
);
3568 g_test_add_func("/cutils/qemu_strtoi64_full/correct",
3569 test_qemu_strtoi64_full_correct
);
3570 g_test_add_func("/cutils/qemu_strtoi64_full/null",
3571 test_qemu_strtoi64_full_null
);
3572 g_test_add_func("/cutils/qemu_strtoi64_full/empty",
3573 test_qemu_strtoi64_full_empty
);
3574 g_test_add_func("/cutils/qemu_strtoi64_full/negative",
3575 test_qemu_strtoi64_full_negative
);
3576 g_test_add_func("/cutils/qemu_strtoi64_full/negzero",
3577 test_qemu_strtoi64_full_negzero
);
3578 g_test_add_func("/cutils/qemu_strtoi64_full/trailing",
3579 test_qemu_strtoi64_full_trailing
);
3580 g_test_add_func("/cutils/qemu_strtoi64_full/max",
3581 test_qemu_strtoi64_full_max
);
3582 g_test_add_func("/cutils/qemu_strtoi64_full/erange_junk",
3583 test_qemu_strtoi64_full_erange_junk
);
3585 /* qemu_strtou64() tests */
3586 g_test_add_func("/cutils/qemu_strtou64/correct",
3587 test_qemu_strtou64_correct
);
3588 g_test_add_func("/cutils/qemu_strtou64/null",
3589 test_qemu_strtou64_null
);
3590 g_test_add_func("/cutils/qemu_strtou64/empty",
3591 test_qemu_strtou64_empty
);
3592 g_test_add_func("/cutils/qemu_strtou64/whitespace",
3593 test_qemu_strtou64_whitespace
);
3594 g_test_add_func("/cutils/qemu_strtou64/invalid",
3595 test_qemu_strtou64_invalid
);
3596 g_test_add_func("/cutils/qemu_strtou64/trailing",
3597 test_qemu_strtou64_trailing
);
3598 g_test_add_func("/cutils/qemu_strtou64/octal",
3599 test_qemu_strtou64_octal
);
3600 g_test_add_func("/cutils/qemu_strtou64/decimal",
3601 test_qemu_strtou64_decimal
);
3602 g_test_add_func("/cutils/qemu_strtou64/hex",
3603 test_qemu_strtou64_hex
);
3604 g_test_add_func("/cutils/qemu_strtou64/wrap",
3605 test_qemu_strtou64_wrap
);
3606 g_test_add_func("/cutils/qemu_strtou64/max",
3607 test_qemu_strtou64_max
);
3608 g_test_add_func("/cutils/qemu_strtou64/overflow",
3609 test_qemu_strtou64_overflow
);
3610 g_test_add_func("/cutils/qemu_strtou64/underflow",
3611 test_qemu_strtou64_underflow
);
3612 g_test_add_func("/cutils/qemu_strtou64/negative",
3613 test_qemu_strtou64_negative
);
3614 g_test_add_func("/cutils/qemu_strtou64/negzero",
3615 test_qemu_strtou64_negzero
);
3616 g_test_add_func("/cutils/qemu_strtou64_full/correct",
3617 test_qemu_strtou64_full_correct
);
3618 g_test_add_func("/cutils/qemu_strtou64_full/null",
3619 test_qemu_strtou64_full_null
);
3620 g_test_add_func("/cutils/qemu_strtou64_full/empty",
3621 test_qemu_strtou64_full_empty
);
3622 g_test_add_func("/cutils/qemu_strtou64_full/negative",
3623 test_qemu_strtou64_full_negative
);
3624 g_test_add_func("/cutils/qemu_strtou64_full/negzero",
3625 test_qemu_strtou64_full_negzero
);
3626 g_test_add_func("/cutils/qemu_strtou64_full/trailing",
3627 test_qemu_strtou64_full_trailing
);
3628 g_test_add_func("/cutils/qemu_strtou64_full/max",
3629 test_qemu_strtou64_full_max
);
3630 g_test_add_func("/cutils/qemu_strtou64_full/erange_junk",
3631 test_qemu_strtou64_full_erange_junk
);
3633 g_test_add_func("/cutils/strtosz/simple",
3634 test_qemu_strtosz_simple
);
3635 g_test_add_func("/cutils/strtosz/hex",
3636 test_qemu_strtosz_hex
);
3637 g_test_add_func("/cutils/strtosz/units",
3638 test_qemu_strtosz_units
);
3639 g_test_add_func("/cutils/strtosz/float",
3640 test_qemu_strtosz_float
);
3641 g_test_add_func("/cutils/strtosz/invalid",
3642 test_qemu_strtosz_invalid
);
3643 g_test_add_func("/cutils/strtosz/trailing",
3644 test_qemu_strtosz_trailing
);
3645 g_test_add_func("/cutils/strtosz/erange",
3646 test_qemu_strtosz_erange
);
3647 g_test_add_func("/cutils/strtosz/metric",
3648 test_qemu_strtosz_metric
);
3650 g_test_add_func("/cutils/size_to_str",
3652 g_test_add_func("/cutils/freq_to_str",
3654 g_test_add_func("/cutils/iec_binary_prefix",
3655 test_iec_binary_prefix
);
3656 g_test_add_func("/cutils/si_prefix",
3658 return g_test_run();