4 * Copyright (C) 2013 Red Hat Inc.
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"
31 #include "qemu-common.h"
34 static void test_parse_uint_null(void)
36 unsigned long long i
= 999;
41 r
= parse_uint(NULL
, &i
, &endptr
, 0);
43 g_assert_cmpint(r
, ==, -EINVAL
);
44 g_assert_cmpint(i
, ==, 0);
45 g_assert(endptr
== NULL
);
48 static void test_parse_uint_empty(void)
50 unsigned long long i
= 999;
56 r
= parse_uint(str
, &i
, &endptr
, 0);
58 g_assert_cmpint(r
, ==, -EINVAL
);
59 g_assert_cmpint(i
, ==, 0);
60 g_assert(endptr
== str
);
63 static void test_parse_uint_whitespace(void)
65 unsigned long long i
= 999;
68 const char *str
= " \t ";
71 r
= parse_uint(str
, &i
, &endptr
, 0);
73 g_assert_cmpint(r
, ==, -EINVAL
);
74 g_assert_cmpint(i
, ==, 0);
75 g_assert(endptr
== str
);
79 static void test_parse_uint_invalid(void)
81 unsigned long long i
= 999;
84 const char *str
= " \t xxx";
87 r
= parse_uint(str
, &i
, &endptr
, 0);
89 g_assert_cmpint(r
, ==, -EINVAL
);
90 g_assert_cmpint(i
, ==, 0);
91 g_assert(endptr
== str
);
95 static void test_parse_uint_trailing(void)
97 unsigned long long i
= 999;
100 const char *str
= "123xxx";
103 r
= parse_uint(str
, &i
, &endptr
, 0);
105 g_assert_cmpint(r
, ==, 0);
106 g_assert_cmpint(i
, ==, 123);
107 g_assert(endptr
== str
+ 3);
110 static void test_parse_uint_correct(void)
112 unsigned long long i
= 999;
115 const char *str
= "123";
118 r
= parse_uint(str
, &i
, &endptr
, 0);
120 g_assert_cmpint(r
, ==, 0);
121 g_assert_cmpint(i
, ==, 123);
122 g_assert(endptr
== str
+ strlen(str
));
125 static void test_parse_uint_octal(void)
127 unsigned long long i
= 999;
130 const char *str
= "0123";
133 r
= parse_uint(str
, &i
, &endptr
, 0);
135 g_assert_cmpint(r
, ==, 0);
136 g_assert_cmpint(i
, ==, 0123);
137 g_assert(endptr
== str
+ strlen(str
));
140 static void test_parse_uint_decimal(void)
142 unsigned long long i
= 999;
145 const char *str
= "0123";
148 r
= parse_uint(str
, &i
, &endptr
, 10);
150 g_assert_cmpint(r
, ==, 0);
151 g_assert_cmpint(i
, ==, 123);
152 g_assert(endptr
== str
+ strlen(str
));
156 static void test_parse_uint_llong_max(void)
158 unsigned long long i
= 999;
161 char *str
= g_strdup_printf("%llu", (unsigned long long)LLONG_MAX
+ 1);
164 r
= parse_uint(str
, &i
, &endptr
, 0);
166 g_assert_cmpint(r
, ==, 0);
167 g_assert_cmpint(i
, ==, (unsigned long long)LLONG_MAX
+ 1);
168 g_assert(endptr
== str
+ strlen(str
));
173 static void test_parse_uint_overflow(void)
175 unsigned long long i
= 999;
178 const char *str
= "99999999999999999999999999999999999999";
181 r
= parse_uint(str
, &i
, &endptr
, 0);
183 g_assert_cmpint(r
, ==, -ERANGE
);
184 g_assert_cmpint(i
, ==, ULLONG_MAX
);
185 g_assert(endptr
== str
+ strlen(str
));
188 static void test_parse_uint_negative(void)
190 unsigned long long i
= 999;
193 const char *str
= " \t -321";
196 r
= parse_uint(str
, &i
, &endptr
, 0);
198 g_assert_cmpint(r
, ==, -ERANGE
);
199 g_assert_cmpint(i
, ==, 0);
200 g_assert(endptr
== str
+ strlen(str
));
204 static void test_parse_uint_full_trailing(void)
206 unsigned long long i
= 999;
207 const char *str
= "123xxx";
210 r
= parse_uint_full(str
, &i
, 0);
212 g_assert_cmpint(r
, ==, -EINVAL
);
213 g_assert_cmpint(i
, ==, 0);
216 static void test_parse_uint_full_correct(void)
218 unsigned long long i
= 999;
219 const char *str
= "123";
222 r
= parse_uint_full(str
, &i
, 0);
224 g_assert_cmpint(r
, ==, 0);
225 g_assert_cmpint(i
, ==, 123);
228 static void test_qemu_strtol_correct(void)
230 const char *str
= "12345 foo";
232 const char *endptr
= &f
;
236 err
= qemu_strtol(str
, &endptr
, 0, &res
);
238 g_assert_cmpint(err
, ==, 0);
239 g_assert_cmpint(res
, ==, 12345);
240 g_assert(endptr
== str
+ 5);
243 static void test_qemu_strtol_null(void)
246 const char *endptr
= &f
;
250 err
= qemu_strtol(NULL
, &endptr
, 0, &res
);
252 g_assert_cmpint(err
, ==, -EINVAL
);
253 g_assert(endptr
== NULL
);
256 static void test_qemu_strtol_empty(void)
258 const char *str
= "";
260 const char *endptr
= &f
;
264 err
= qemu_strtol(str
, &endptr
, 0, &res
);
266 g_assert_cmpint(err
, ==, -EINVAL
);
269 static void test_qemu_strtol_whitespace(void)
271 const char *str
= " \t ";
273 const char *endptr
= &f
;
277 err
= qemu_strtol(str
, &endptr
, 0, &res
);
279 g_assert_cmpint(err
, ==, -EINVAL
);
282 static void test_qemu_strtol_invalid(void)
284 const char *str
= " xxxx \t abc";
286 const char *endptr
= &f
;
290 err
= qemu_strtol(str
, &endptr
, 0, &res
);
292 g_assert_cmpint(err
, ==, -EINVAL
);
295 static void test_qemu_strtol_trailing(void)
297 const char *str
= "123xxx";
299 const char *endptr
= &f
;
303 err
= qemu_strtol(str
, &endptr
, 0, &res
);
305 g_assert_cmpint(err
, ==, 0);
306 g_assert_cmpint(res
, ==, 123);
307 g_assert(endptr
== str
+ 3);
310 static void test_qemu_strtol_octal(void)
312 const char *str
= "0123";
314 const char *endptr
= &f
;
318 err
= qemu_strtol(str
, &endptr
, 8, &res
);
320 g_assert_cmpint(err
, ==, 0);
321 g_assert_cmpint(res
, ==, 0123);
322 g_assert(endptr
== str
+ strlen(str
));
326 err
= qemu_strtol(str
, &endptr
, 0, &res
);
328 g_assert_cmpint(err
, ==, 0);
329 g_assert_cmpint(res
, ==, 0123);
330 g_assert(endptr
== str
+ strlen(str
));
333 static void test_qemu_strtol_decimal(void)
335 const char *str
= "0123";
337 const char *endptr
= &f
;
341 err
= qemu_strtol(str
, &endptr
, 10, &res
);
343 g_assert_cmpint(err
, ==, 0);
344 g_assert_cmpint(res
, ==, 123);
345 g_assert(endptr
== str
+ strlen(str
));
350 err
= qemu_strtol(str
, &endptr
, 0, &res
);
352 g_assert_cmpint(err
, ==, 0);
353 g_assert_cmpint(res
, ==, 123);
354 g_assert(endptr
== str
+ strlen(str
));
357 static void test_qemu_strtol_hex(void)
359 const char *str
= "0123";
361 const char *endptr
= &f
;
365 err
= qemu_strtol(str
, &endptr
, 16, &res
);
367 g_assert_cmpint(err
, ==, 0);
368 g_assert_cmpint(res
, ==, 0x123);
369 g_assert(endptr
== str
+ strlen(str
));
374 err
= qemu_strtol(str
, &endptr
, 0, &res
);
376 g_assert_cmpint(err
, ==, 0);
377 g_assert_cmpint(res
, ==, 0x123);
378 g_assert(endptr
== str
+ strlen(str
));
381 static void test_qemu_strtol_max(void)
383 const char *str
= g_strdup_printf("%ld", LONG_MAX
);
385 const char *endptr
= &f
;
389 err
= qemu_strtol(str
, &endptr
, 0, &res
);
391 g_assert_cmpint(err
, ==, 0);
392 g_assert_cmpint(res
, ==, LONG_MAX
);
393 g_assert(endptr
== str
+ strlen(str
));
396 static void test_qemu_strtol_overflow(void)
398 const char *str
= "99999999999999999999999999999999999999999999";
400 const char *endptr
= &f
;
404 err
= qemu_strtol(str
, &endptr
, 0, &res
);
406 g_assert_cmpint(err
, ==, -ERANGE
);
407 g_assert_cmpint(res
, ==, LONG_MAX
);
408 g_assert(endptr
== str
+ strlen(str
));
411 static void test_qemu_strtol_underflow(void)
413 const char *str
= "-99999999999999999999999999999999999999999999";
415 const char *endptr
= &f
;
419 err
= qemu_strtol(str
, &endptr
, 0, &res
);
421 g_assert_cmpint(err
, ==, -ERANGE
);
422 g_assert_cmpint(res
, ==, LONG_MIN
);
423 g_assert(endptr
== str
+ strlen(str
));
426 static void test_qemu_strtol_negative(void)
428 const char *str
= " \t -321";
430 const char *endptr
= &f
;
434 err
= qemu_strtol(str
, &endptr
, 0, &res
);
436 g_assert_cmpint(err
, ==, 0);
437 g_assert_cmpint(res
, ==, -321);
438 g_assert(endptr
== str
+ strlen(str
));
441 static void test_qemu_strtol_full_correct(void)
443 const char *str
= "123";
447 err
= qemu_strtol(str
, NULL
, 0, &res
);
449 g_assert_cmpint(err
, ==, 0);
450 g_assert_cmpint(res
, ==, 123);
453 static void test_qemu_strtol_full_null(void)
456 const char *endptr
= &f
;
460 err
= qemu_strtol(NULL
, &endptr
, 0, &res
);
462 g_assert_cmpint(err
, ==, -EINVAL
);
463 g_assert(endptr
== NULL
);
466 static void test_qemu_strtol_full_empty(void)
468 const char *str
= "";
472 err
= qemu_strtol(str
, NULL
, 0, &res
);
474 g_assert_cmpint(err
, ==, -EINVAL
);
477 static void test_qemu_strtol_full_negative(void)
479 const char *str
= " \t -321";
483 err
= qemu_strtol(str
, NULL
, 0, &res
);
485 g_assert_cmpint(err
, ==, 0);
486 g_assert_cmpint(res
, ==, -321);
489 static void test_qemu_strtol_full_trailing(void)
491 const char *str
= "123xxx";
495 err
= qemu_strtol(str
, NULL
, 0, &res
);
497 g_assert_cmpint(err
, ==, -EINVAL
);
500 static void test_qemu_strtol_full_max(void)
502 const char *str
= g_strdup_printf("%ld", LONG_MAX
);
506 err
= qemu_strtol(str
, NULL
, 0, &res
);
508 g_assert_cmpint(err
, ==, 0);
509 g_assert_cmpint(res
, ==, LONG_MAX
);
512 static void test_qemu_strtoul_correct(void)
514 const char *str
= "12345 foo";
516 const char *endptr
= &f
;
517 unsigned long res
= 999;
520 err
= qemu_strtoul(str
, &endptr
, 0, &res
);
522 g_assert_cmpint(err
, ==, 0);
523 g_assert_cmpint(res
, ==, 12345);
524 g_assert(endptr
== str
+ 5);
527 static void test_qemu_strtoul_null(void)
530 const char *endptr
= &f
;
531 unsigned long res
= 999;
534 err
= qemu_strtoul(NULL
, &endptr
, 0, &res
);
536 g_assert_cmpint(err
, ==, -EINVAL
);
537 g_assert(endptr
== NULL
);
540 static void test_qemu_strtoul_empty(void)
542 const char *str
= "";
544 const char *endptr
= &f
;
545 unsigned long res
= 999;
548 err
= qemu_strtoul(str
, &endptr
, 0, &res
);
550 g_assert_cmpint(err
, ==, -EINVAL
);
553 static void test_qemu_strtoul_whitespace(void)
555 const char *str
= " \t ";
557 const char *endptr
= &f
;
558 unsigned long res
= 999;
561 err
= qemu_strtoul(str
, &endptr
, 0, &res
);
563 g_assert_cmpint(err
, ==, -EINVAL
);
566 static void test_qemu_strtoul_invalid(void)
568 const char *str
= " xxxx \t abc";
570 const char *endptr
= &f
;
571 unsigned long res
= 999;
574 err
= qemu_strtoul(str
, &endptr
, 0, &res
);
576 g_assert_cmpint(err
, ==, -EINVAL
);
579 static void test_qemu_strtoul_trailing(void)
581 const char *str
= "123xxx";
583 const char *endptr
= &f
;
584 unsigned long res
= 999;
587 err
= qemu_strtoul(str
, &endptr
, 0, &res
);
589 g_assert_cmpint(err
, ==, 0);
590 g_assert_cmpint(res
, ==, 123);
591 g_assert(endptr
== str
+ 3);
594 static void test_qemu_strtoul_octal(void)
596 const char *str
= "0123";
598 const char *endptr
= &f
;
599 unsigned long res
= 999;
602 err
= qemu_strtoul(str
, &endptr
, 8, &res
);
604 g_assert_cmpint(err
, ==, 0);
605 g_assert_cmpint(res
, ==, 0123);
606 g_assert(endptr
== str
+ strlen(str
));
610 err
= qemu_strtoul(str
, &endptr
, 0, &res
);
612 g_assert_cmpint(err
, ==, 0);
613 g_assert_cmpint(res
, ==, 0123);
614 g_assert(endptr
== str
+ strlen(str
));
617 static void test_qemu_strtoul_decimal(void)
619 const char *str
= "0123";
621 const char *endptr
= &f
;
622 unsigned long res
= 999;
625 err
= qemu_strtoul(str
, &endptr
, 10, &res
);
627 g_assert_cmpint(err
, ==, 0);
628 g_assert_cmpint(res
, ==, 123);
629 g_assert(endptr
== str
+ strlen(str
));
634 err
= qemu_strtoul(str
, &endptr
, 0, &res
);
636 g_assert_cmpint(err
, ==, 0);
637 g_assert_cmpint(res
, ==, 123);
638 g_assert(endptr
== str
+ strlen(str
));
641 static void test_qemu_strtoul_hex(void)
643 const char *str
= "0123";
645 const char *endptr
= &f
;
646 unsigned long res
= 999;
649 err
= qemu_strtoul(str
, &endptr
, 16, &res
);
651 g_assert_cmpint(err
, ==, 0);
652 g_assert_cmpint(res
, ==, 0x123);
653 g_assert(endptr
== str
+ strlen(str
));
658 err
= qemu_strtoul(str
, &endptr
, 0, &res
);
660 g_assert_cmpint(err
, ==, 0);
661 g_assert_cmpint(res
, ==, 0x123);
662 g_assert(endptr
== str
+ strlen(str
));
665 static void test_qemu_strtoul_max(void)
667 const char *str
= g_strdup_printf("%lu", ULONG_MAX
);
669 const char *endptr
= &f
;
670 unsigned long res
= 999;
673 err
= qemu_strtoul(str
, &endptr
, 0, &res
);
675 g_assert_cmpint(err
, ==, 0);
676 g_assert_cmpint(res
, ==, ULONG_MAX
);
677 g_assert(endptr
== str
+ strlen(str
));
680 static void test_qemu_strtoul_overflow(void)
682 const char *str
= "99999999999999999999999999999999999999999999";
684 const char *endptr
= &f
;
685 unsigned long res
= 999;
688 err
= qemu_strtoul(str
, &endptr
, 0, &res
);
690 g_assert_cmpint(err
, ==, -ERANGE
);
691 g_assert_cmpint(res
, ==, ULONG_MAX
);
692 g_assert(endptr
== str
+ strlen(str
));
695 static void test_qemu_strtoul_underflow(void)
697 const char *str
= "-99999999999999999999999999999999999999999999";
699 const char *endptr
= &f
;
700 unsigned long res
= 999;
703 err
= qemu_strtoul(str
, &endptr
, 0, &res
);
705 g_assert_cmpint(err
, ==, -ERANGE
);
706 g_assert_cmpint(res
, ==, -1ul);
707 g_assert(endptr
== str
+ strlen(str
));
710 static void test_qemu_strtoul_negative(void)
712 const char *str
= " \t -321";
714 const char *endptr
= &f
;
715 unsigned long res
= 999;
718 err
= qemu_strtoul(str
, &endptr
, 0, &res
);
720 g_assert_cmpint(err
, ==, 0);
721 g_assert_cmpint(res
, ==, -321ul);
722 g_assert(endptr
== str
+ strlen(str
));
725 static void test_qemu_strtoul_full_correct(void)
727 const char *str
= "123";
728 unsigned long res
= 999;
731 err
= qemu_strtoul(str
, NULL
, 0, &res
);
733 g_assert_cmpint(err
, ==, 0);
734 g_assert_cmpint(res
, ==, 123);
737 static void test_qemu_strtoul_full_null(void)
739 unsigned long res
= 999;
742 err
= qemu_strtoul(NULL
, NULL
, 0, &res
);
744 g_assert_cmpint(err
, ==, -EINVAL
);
747 static void test_qemu_strtoul_full_empty(void)
749 const char *str
= "";
750 unsigned long res
= 999;
753 err
= qemu_strtoul(str
, NULL
, 0, &res
);
755 g_assert_cmpint(err
, ==, -EINVAL
);
757 static void test_qemu_strtoul_full_negative(void)
759 const char *str
= " \t -321";
760 unsigned long res
= 999;
763 err
= qemu_strtoul(str
, NULL
, 0, &res
);
764 g_assert_cmpint(err
, ==, 0);
765 g_assert_cmpint(res
, ==, -321ul);
768 static void test_qemu_strtoul_full_trailing(void)
770 const char *str
= "123xxx";
774 err
= qemu_strtoul(str
, NULL
, 0, &res
);
776 g_assert_cmpint(err
, ==, -EINVAL
);
779 static void test_qemu_strtoul_full_max(void)
781 const char *str
= g_strdup_printf("%lu", ULONG_MAX
);
782 unsigned long res
= 999;
785 err
= qemu_strtoul(str
, NULL
, 0, &res
);
787 g_assert_cmpint(err
, ==, 0);
788 g_assert_cmpint(res
, ==, ULONG_MAX
);
791 static void test_qemu_strtoll_correct(void)
793 const char *str
= "12345 foo";
795 const char *endptr
= &f
;
799 err
= qemu_strtoll(str
, &endptr
, 0, &res
);
801 g_assert_cmpint(err
, ==, 0);
802 g_assert_cmpint(res
, ==, 12345);
803 g_assert(endptr
== str
+ 5);
806 static void test_qemu_strtoll_null(void)
809 const char *endptr
= &f
;
813 err
= qemu_strtoll(NULL
, &endptr
, 0, &res
);
815 g_assert_cmpint(err
, ==, -EINVAL
);
816 g_assert(endptr
== NULL
);
819 static void test_qemu_strtoll_empty(void)
821 const char *str
= "";
823 const char *endptr
= &f
;
827 err
= qemu_strtoll(str
, &endptr
, 0, &res
);
829 g_assert_cmpint(err
, ==, -EINVAL
);
832 static void test_qemu_strtoll_whitespace(void)
834 const char *str
= " \t ";
836 const char *endptr
= &f
;
840 err
= qemu_strtoll(str
, &endptr
, 0, &res
);
842 g_assert_cmpint(err
, ==, -EINVAL
);
845 static void test_qemu_strtoll_invalid(void)
847 const char *str
= " xxxx \t abc";
849 const char *endptr
= &f
;
853 err
= qemu_strtoll(str
, &endptr
, 0, &res
);
855 g_assert_cmpint(err
, ==, -EINVAL
);
858 static void test_qemu_strtoll_trailing(void)
860 const char *str
= "123xxx";
862 const char *endptr
= &f
;
866 err
= qemu_strtoll(str
, &endptr
, 0, &res
);
868 g_assert_cmpint(err
, ==, 0);
869 g_assert_cmpint(res
, ==, 123);
870 g_assert(endptr
== str
+ 3);
873 static void test_qemu_strtoll_octal(void)
875 const char *str
= "0123";
877 const char *endptr
= &f
;
881 err
= qemu_strtoll(str
, &endptr
, 8, &res
);
883 g_assert_cmpint(err
, ==, 0);
884 g_assert_cmpint(res
, ==, 0123);
885 g_assert(endptr
== str
+ strlen(str
));
889 err
= qemu_strtoll(str
, &endptr
, 0, &res
);
891 g_assert_cmpint(err
, ==, 0);
892 g_assert_cmpint(res
, ==, 0123);
893 g_assert(endptr
== str
+ strlen(str
));
896 static void test_qemu_strtoll_decimal(void)
898 const char *str
= "0123";
900 const char *endptr
= &f
;
904 err
= qemu_strtoll(str
, &endptr
, 10, &res
);
906 g_assert_cmpint(err
, ==, 0);
907 g_assert_cmpint(res
, ==, 123);
908 g_assert(endptr
== str
+ strlen(str
));
913 err
= qemu_strtoll(str
, &endptr
, 0, &res
);
915 g_assert_cmpint(err
, ==, 0);
916 g_assert_cmpint(res
, ==, 123);
917 g_assert(endptr
== str
+ strlen(str
));
920 static void test_qemu_strtoll_hex(void)
922 const char *str
= "0123";
924 const char *endptr
= &f
;
928 err
= qemu_strtoll(str
, &endptr
, 16, &res
);
930 g_assert_cmpint(err
, ==, 0);
931 g_assert_cmpint(res
, ==, 0x123);
932 g_assert(endptr
== str
+ strlen(str
));
937 err
= qemu_strtoll(str
, &endptr
, 0, &res
);
939 g_assert_cmpint(err
, ==, 0);
940 g_assert_cmpint(res
, ==, 0x123);
941 g_assert(endptr
== str
+ strlen(str
));
944 static void test_qemu_strtoll_max(void)
946 const char *str
= g_strdup_printf("%lld", LLONG_MAX
);
948 const char *endptr
= &f
;
952 err
= qemu_strtoll(str
, &endptr
, 0, &res
);
954 g_assert_cmpint(err
, ==, 0);
955 g_assert_cmpint(res
, ==, LLONG_MAX
);
956 g_assert(endptr
== str
+ strlen(str
));
959 static void test_qemu_strtoll_overflow(void)
961 const char *str
= "99999999999999999999999999999999999999999999";
963 const char *endptr
= &f
;
967 err
= qemu_strtoll(str
, &endptr
, 0, &res
);
969 g_assert_cmpint(err
, ==, -ERANGE
);
970 g_assert_cmpint(res
, ==, LLONG_MAX
);
971 g_assert(endptr
== str
+ strlen(str
));
974 static void test_qemu_strtoll_underflow(void)
976 const char *str
= "-99999999999999999999999999999999999999999999";
978 const char *endptr
= &f
;
982 err
= qemu_strtoll(str
, &endptr
, 0, &res
);
984 g_assert_cmpint(err
, ==, -ERANGE
);
985 g_assert_cmpint(res
, ==, LLONG_MIN
);
986 g_assert(endptr
== str
+ strlen(str
));
989 static void test_qemu_strtoll_negative(void)
991 const char *str
= " \t -321";
993 const char *endptr
= &f
;
997 err
= qemu_strtoll(str
, &endptr
, 0, &res
);
999 g_assert_cmpint(err
, ==, 0);
1000 g_assert_cmpint(res
, ==, -321);
1001 g_assert(endptr
== str
+ strlen(str
));
1004 static void test_qemu_strtoll_full_correct(void)
1006 const char *str
= "123";
1010 err
= qemu_strtoll(str
, NULL
, 0, &res
);
1012 g_assert_cmpint(err
, ==, 0);
1013 g_assert_cmpint(res
, ==, 123);
1016 static void test_qemu_strtoll_full_null(void)
1021 err
= qemu_strtoll(NULL
, NULL
, 0, &res
);
1023 g_assert_cmpint(err
, ==, -EINVAL
);
1026 static void test_qemu_strtoll_full_empty(void)
1028 const char *str
= "";
1032 err
= qemu_strtoll(str
, NULL
, 0, &res
);
1034 g_assert_cmpint(err
, ==, -EINVAL
);
1037 static void test_qemu_strtoll_full_negative(void)
1039 const char *str
= " \t -321";
1043 err
= qemu_strtoll(str
, NULL
, 0, &res
);
1045 g_assert_cmpint(err
, ==, 0);
1046 g_assert_cmpint(res
, ==, -321);
1049 static void test_qemu_strtoll_full_trailing(void)
1051 const char *str
= "123xxx";
1055 err
= qemu_strtoll(str
, NULL
, 0, &res
);
1057 g_assert_cmpint(err
, ==, -EINVAL
);
1060 static void test_qemu_strtoll_full_max(void)
1063 const char *str
= g_strdup_printf("%lld", LLONG_MAX
);
1067 err
= qemu_strtoll(str
, NULL
, 0, &res
);
1069 g_assert_cmpint(err
, ==, 0);
1070 g_assert_cmpint(res
, ==, LLONG_MAX
);
1073 static void test_qemu_strtoull_correct(void)
1075 const char *str
= "12345 foo";
1077 const char *endptr
= &f
;
1081 err
= qemu_strtoull(str
, &endptr
, 0, &res
);
1083 g_assert_cmpint(err
, ==, 0);
1084 g_assert_cmpint(res
, ==, 12345);
1085 g_assert(endptr
== str
+ 5);
1088 static void test_qemu_strtoull_null(void)
1091 const char *endptr
= &f
;
1095 err
= qemu_strtoull(NULL
, &endptr
, 0, &res
);
1097 g_assert_cmpint(err
, ==, -EINVAL
);
1098 g_assert(endptr
== NULL
);
1101 static void test_qemu_strtoull_empty(void)
1103 const char *str
= "";
1105 const char *endptr
= &f
;
1109 err
= qemu_strtoull(str
, &endptr
, 0, &res
);
1111 g_assert_cmpint(err
, ==, -EINVAL
);
1114 static void test_qemu_strtoull_whitespace(void)
1116 const char *str
= " \t ";
1118 const char *endptr
= &f
;
1122 err
= qemu_strtoull(str
, &endptr
, 0, &res
);
1124 g_assert_cmpint(err
, ==, -EINVAL
);
1127 static void test_qemu_strtoull_invalid(void)
1129 const char *str
= " xxxx \t abc";
1131 const char *endptr
= &f
;
1135 err
= qemu_strtoull(str
, &endptr
, 0, &res
);
1137 g_assert_cmpint(err
, ==, -EINVAL
);
1140 static void test_qemu_strtoull_trailing(void)
1142 const char *str
= "123xxx";
1144 const char *endptr
= &f
;
1148 err
= qemu_strtoull(str
, &endptr
, 0, &res
);
1150 g_assert_cmpint(err
, ==, 0);
1151 g_assert_cmpint(res
, ==, 123);
1152 g_assert(endptr
== str
+ 3);
1155 static void test_qemu_strtoull_octal(void)
1157 const char *str
= "0123";
1159 const char *endptr
= &f
;
1163 err
= qemu_strtoull(str
, &endptr
, 8, &res
);
1165 g_assert_cmpint(err
, ==, 0);
1166 g_assert_cmpint(res
, ==, 0123);
1167 g_assert(endptr
== str
+ strlen(str
));
1171 err
= qemu_strtoull(str
, &endptr
, 0, &res
);
1173 g_assert_cmpint(err
, ==, 0);
1174 g_assert_cmpint(res
, ==, 0123);
1175 g_assert(endptr
== str
+ strlen(str
));
1178 static void test_qemu_strtoull_decimal(void)
1180 const char *str
= "0123";
1182 const char *endptr
= &f
;
1186 err
= qemu_strtoull(str
, &endptr
, 10, &res
);
1188 g_assert_cmpint(err
, ==, 0);
1189 g_assert_cmpint(res
, ==, 123);
1190 g_assert(endptr
== str
+ strlen(str
));
1195 err
= qemu_strtoull(str
, &endptr
, 0, &res
);
1197 g_assert_cmpint(err
, ==, 0);
1198 g_assert_cmpint(res
, ==, 123);
1199 g_assert(endptr
== str
+ strlen(str
));
1202 static void test_qemu_strtoull_hex(void)
1204 const char *str
= "0123";
1206 const char *endptr
= &f
;
1210 err
= qemu_strtoull(str
, &endptr
, 16, &res
);
1212 g_assert_cmpint(err
, ==, 0);
1213 g_assert_cmpint(res
, ==, 0x123);
1214 g_assert(endptr
== str
+ strlen(str
));
1219 err
= qemu_strtoull(str
, &endptr
, 0, &res
);
1221 g_assert_cmpint(err
, ==, 0);
1222 g_assert_cmpint(res
, ==, 0x123);
1223 g_assert(endptr
== str
+ strlen(str
));
1226 static void test_qemu_strtoull_max(void)
1228 const char *str
= g_strdup_printf("%llu", ULLONG_MAX
);
1230 const char *endptr
= &f
;
1234 err
= qemu_strtoull(str
, &endptr
, 0, &res
);
1236 g_assert_cmpint(err
, ==, 0);
1237 g_assert_cmpint(res
, ==, ULLONG_MAX
);
1238 g_assert(endptr
== str
+ strlen(str
));
1241 static void test_qemu_strtoull_overflow(void)
1243 const char *str
= "99999999999999999999999999999999999999999999";
1245 const char *endptr
= &f
;
1249 err
= qemu_strtoull(str
, &endptr
, 0, &res
);
1251 g_assert_cmpint(err
, ==, -ERANGE
);
1252 g_assert_cmpint(res
, ==, ULLONG_MAX
);
1253 g_assert(endptr
== str
+ strlen(str
));
1256 static void test_qemu_strtoull_underflow(void)
1258 const char *str
= "-99999999999999999999999999999999999999999999";
1260 const char *endptr
= &f
;
1264 err
= qemu_strtoull(str
, &endptr
, 0, &res
);
1266 g_assert_cmpint(err
, ==, -ERANGE
);
1267 g_assert_cmpint(res
, ==, -1);
1268 g_assert(endptr
== str
+ strlen(str
));
1271 static void test_qemu_strtoull_negative(void)
1273 const char *str
= " \t -321";
1275 const char *endptr
= &f
;
1279 err
= qemu_strtoull(str
, &endptr
, 0, &res
);
1281 g_assert_cmpint(err
, ==, 0);
1282 g_assert_cmpint(res
, ==, -321);
1283 g_assert(endptr
== str
+ strlen(str
));
1286 static void test_qemu_strtoull_full_correct(void)
1288 const char *str
= "18446744073709551614";
1292 err
= qemu_strtoull(str
, NULL
, 0, &res
);
1294 g_assert_cmpint(err
, ==, 0);
1295 g_assert_cmpint(res
, ==, 18446744073709551614LLU);
1298 static void test_qemu_strtoull_full_null(void)
1303 err
= qemu_strtoull(NULL
, NULL
, 0, &res
);
1305 g_assert_cmpint(err
, ==, -EINVAL
);
1308 static void test_qemu_strtoull_full_empty(void)
1310 const char *str
= "";
1314 err
= qemu_strtoull(str
, NULL
, 0, &res
);
1316 g_assert_cmpint(err
, ==, -EINVAL
);
1319 static void test_qemu_strtoull_full_negative(void)
1321 const char *str
= " \t -321";
1325 err
= qemu_strtoull(str
, NULL
, 0, &res
);
1327 g_assert_cmpint(err
, ==, 0);
1328 g_assert_cmpint(res
, ==, 18446744073709551295LLU);
1331 static void test_qemu_strtoull_full_trailing(void)
1333 const char *str
= "18446744073709551614xxxxxx";
1337 err
= qemu_strtoull(str
, NULL
, 0, &res
);
1339 g_assert_cmpint(err
, ==, -EINVAL
);
1342 static void test_qemu_strtoull_full_max(void)
1344 const char *str
= g_strdup_printf("%lld", ULLONG_MAX
);
1348 err
= qemu_strtoull(str
, NULL
, 0, &res
);
1350 g_assert_cmpint(err
, ==, 0);
1351 g_assert_cmpint(res
, ==, ULLONG_MAX
);
1354 static void test_qemu_strtosz_simple(void)
1356 const char *str
= "12345M";
1357 char *endptr
= NULL
;
1360 res
= qemu_strtosz(str
, &endptr
);
1361 g_assert_cmpint(res
, ==, 12345 * M_BYTE
);
1362 g_assert(endptr
== str
+ 6);
1364 res
= qemu_strtosz(str
, NULL
);
1365 g_assert_cmpint(res
, ==, 12345 * M_BYTE
);
1368 static void test_qemu_strtosz_units(void)
1370 const char *none
= "1";
1371 const char *b
= "1B";
1372 const char *k
= "1K";
1373 const char *m
= "1M";
1374 const char *g
= "1G";
1375 const char *t
= "1T";
1376 const char *p
= "1P";
1377 const char *e
= "1E";
1381 res
= qemu_strtosz(none
, NULL
);
1382 g_assert_cmpint(res
, ==, M_BYTE
);
1384 res
= qemu_strtosz(b
, NULL
);
1385 g_assert_cmpint(res
, ==, 1);
1387 res
= qemu_strtosz(k
, NULL
);
1388 g_assert_cmpint(res
, ==, K_BYTE
);
1390 res
= qemu_strtosz(m
, NULL
);
1391 g_assert_cmpint(res
, ==, M_BYTE
);
1393 res
= qemu_strtosz(g
, NULL
);
1394 g_assert_cmpint(res
, ==, G_BYTE
);
1396 res
= qemu_strtosz(t
, NULL
);
1397 g_assert_cmpint(res
, ==, T_BYTE
);
1399 res
= qemu_strtosz(p
, NULL
);
1400 g_assert_cmpint(res
, ==, P_BYTE
);
1402 res
= qemu_strtosz(e
, NULL
);
1403 g_assert_cmpint(res
, ==, E_BYTE
);
1406 static void test_qemu_strtosz_float(void)
1408 const char *str
= "12.345M";
1411 res
= qemu_strtosz(str
, NULL
);
1412 g_assert_cmpint(res
, ==, 12.345 * M_BYTE
);
1415 static void test_qemu_strtosz_erange(void)
1417 const char *str
= "10E";
1420 res
= qemu_strtosz(str
, NULL
);
1421 g_assert_cmpint(res
, ==, -ERANGE
);
1424 static void test_qemu_strtosz_suffix_unit(void)
1426 const char *str
= "12345";
1429 res
= qemu_strtosz_suffix_unit(str
, NULL
,
1430 QEMU_STRTOSZ_DEFSUFFIX_KB
, 1000);
1431 g_assert_cmpint(res
, ==, 12345000);
1434 int main(int argc
, char **argv
)
1436 g_test_init(&argc
, &argv
, NULL
);
1438 g_test_add_func("/cutils/parse_uint/null", test_parse_uint_null
);
1439 g_test_add_func("/cutils/parse_uint/empty", test_parse_uint_empty
);
1440 g_test_add_func("/cutils/parse_uint/whitespace",
1441 test_parse_uint_whitespace
);
1442 g_test_add_func("/cutils/parse_uint/invalid", test_parse_uint_invalid
);
1443 g_test_add_func("/cutils/parse_uint/trailing", test_parse_uint_trailing
);
1444 g_test_add_func("/cutils/parse_uint/correct", test_parse_uint_correct
);
1445 g_test_add_func("/cutils/parse_uint/octal", test_parse_uint_octal
);
1446 g_test_add_func("/cutils/parse_uint/decimal", test_parse_uint_decimal
);
1447 g_test_add_func("/cutils/parse_uint/llong_max", test_parse_uint_llong_max
);
1448 g_test_add_func("/cutils/parse_uint/overflow", test_parse_uint_overflow
);
1449 g_test_add_func("/cutils/parse_uint/negative", test_parse_uint_negative
);
1450 g_test_add_func("/cutils/parse_uint_full/trailing",
1451 test_parse_uint_full_trailing
);
1452 g_test_add_func("/cutils/parse_uint_full/correct",
1453 test_parse_uint_full_correct
);
1455 /* qemu_strtol() tests */
1456 g_test_add_func("/cutils/qemu_strtol/correct", test_qemu_strtol_correct
);
1457 g_test_add_func("/cutils/qemu_strtol/null", test_qemu_strtol_null
);
1458 g_test_add_func("/cutils/qemu_strtol/empty", test_qemu_strtol_empty
);
1459 g_test_add_func("/cutils/qemu_strtol/whitespace",
1460 test_qemu_strtol_whitespace
);
1461 g_test_add_func("/cutils/qemu_strtol/invalid", test_qemu_strtol_invalid
);
1462 g_test_add_func("/cutils/qemu_strtol/trailing", test_qemu_strtol_trailing
);
1463 g_test_add_func("/cutils/qemu_strtol/octal", test_qemu_strtol_octal
);
1464 g_test_add_func("/cutils/qemu_strtol/decimal", test_qemu_strtol_decimal
);
1465 g_test_add_func("/cutils/qemu_strtol/hex", test_qemu_strtol_hex
);
1466 g_test_add_func("/cutils/qemu_strtol/max", test_qemu_strtol_max
);
1467 g_test_add_func("/cutils/qemu_strtol/overflow", test_qemu_strtol_overflow
);
1468 g_test_add_func("/cutils/qemu_strtol/underflow",
1469 test_qemu_strtol_underflow
);
1470 g_test_add_func("/cutils/qemu_strtol/negative", test_qemu_strtol_negative
);
1471 g_test_add_func("/cutils/qemu_strtol_full/correct",
1472 test_qemu_strtol_full_correct
);
1473 g_test_add_func("/cutils/qemu_strtol_full/null",
1474 test_qemu_strtol_full_null
);
1475 g_test_add_func("/cutils/qemu_strtol_full/empty",
1476 test_qemu_strtol_full_empty
);
1477 g_test_add_func("/cutils/qemu_strtol_full/negative",
1478 test_qemu_strtol_full_negative
);
1479 g_test_add_func("/cutils/qemu_strtol_full/trailing",
1480 test_qemu_strtol_full_trailing
);
1481 g_test_add_func("/cutils/qemu_strtol_full/max",
1482 test_qemu_strtol_full_max
);
1484 /* qemu_strtoul() tests */
1485 g_test_add_func("/cutils/qemu_strtoul/correct", test_qemu_strtoul_correct
);
1486 g_test_add_func("/cutils/qemu_strtoul/null", test_qemu_strtoul_null
);
1487 g_test_add_func("/cutils/qemu_strtoul/empty", test_qemu_strtoul_empty
);
1488 g_test_add_func("/cutils/qemu_strtoul/whitespace",
1489 test_qemu_strtoul_whitespace
);
1490 g_test_add_func("/cutils/qemu_strtoul/invalid", test_qemu_strtoul_invalid
);
1491 g_test_add_func("/cutils/qemu_strtoul/trailing",
1492 test_qemu_strtoul_trailing
);
1493 g_test_add_func("/cutils/qemu_strtoul/octal", test_qemu_strtoul_octal
);
1494 g_test_add_func("/cutils/qemu_strtoul/decimal", test_qemu_strtoul_decimal
);
1495 g_test_add_func("/cutils/qemu_strtoul/hex", test_qemu_strtoul_hex
);
1496 g_test_add_func("/cutils/qemu_strtoul/max", test_qemu_strtoul_max
);
1497 g_test_add_func("/cutils/qemu_strtoul/overflow",
1498 test_qemu_strtoul_overflow
);
1499 g_test_add_func("/cutils/qemu_strtoul/underflow",
1500 test_qemu_strtoul_underflow
);
1501 g_test_add_func("/cutils/qemu_strtoul/negative",
1502 test_qemu_strtoul_negative
);
1503 g_test_add_func("/cutils/qemu_strtoul_full/correct",
1504 test_qemu_strtoul_full_correct
);
1505 g_test_add_func("/cutils/qemu_strtoul_full/null",
1506 test_qemu_strtoul_full_null
);
1507 g_test_add_func("/cutils/qemu_strtoul_full/empty",
1508 test_qemu_strtoul_full_empty
);
1509 g_test_add_func("/cutils/qemu_strtoul_full/negative",
1510 test_qemu_strtoul_full_negative
);
1511 g_test_add_func("/cutils/qemu_strtoul_full/trailing",
1512 test_qemu_strtoul_full_trailing
);
1513 g_test_add_func("/cutils/qemu_strtoul_full/max",
1514 test_qemu_strtoul_full_max
);
1516 /* qemu_strtoll() tests */
1517 g_test_add_func("/cutils/qemu_strtoll/correct", test_qemu_strtoll_correct
);
1518 g_test_add_func("/cutils/qemu_strtoll/null", test_qemu_strtoll_null
);
1519 g_test_add_func("/cutils/qemu_strtoll/empty", test_qemu_strtoll_empty
);
1520 g_test_add_func("/cutils/qemu_strtoll/whitespace",
1521 test_qemu_strtoll_whitespace
);
1522 g_test_add_func("/cutils/qemu_strtoll/invalid", test_qemu_strtoll_invalid
);
1523 g_test_add_func("/cutils/qemu_strtoll/trailing",
1524 test_qemu_strtoll_trailing
);
1525 g_test_add_func("/cutils/qemu_strtoll/octal", test_qemu_strtoll_octal
);
1526 g_test_add_func("/cutils/qemu_strtoll/decimal", test_qemu_strtoll_decimal
);
1527 g_test_add_func("/cutils/qemu_strtoll/hex", test_qemu_strtoll_hex
);
1528 g_test_add_func("/cutils/qemu_strtoll/max", test_qemu_strtoll_max
);
1529 g_test_add_func("/cutils/qemu_strtoll/overflow",
1530 test_qemu_strtoll_overflow
);
1531 g_test_add_func("/cutils/qemu_strtoll/underflow",
1532 test_qemu_strtoll_underflow
);
1533 g_test_add_func("/cutils/qemu_strtoll/negative",
1534 test_qemu_strtoll_negative
);
1535 g_test_add_func("/cutils/qemu_strtoll_full/correct",
1536 test_qemu_strtoll_full_correct
);
1537 g_test_add_func("/cutils/qemu_strtoll_full/null",
1538 test_qemu_strtoll_full_null
);
1539 g_test_add_func("/cutils/qemu_strtoll_full/empty",
1540 test_qemu_strtoll_full_empty
);
1541 g_test_add_func("/cutils/qemu_strtoll_full/negative",
1542 test_qemu_strtoll_full_negative
);
1543 g_test_add_func("/cutils/qemu_strtoll_full/trailing",
1544 test_qemu_strtoll_full_trailing
);
1545 g_test_add_func("/cutils/qemu_strtoll_full/max",
1546 test_qemu_strtoll_full_max
);
1548 /* qemu_strtoull() tests */
1549 g_test_add_func("/cutils/qemu_strtoull/correct",
1550 test_qemu_strtoull_correct
);
1551 g_test_add_func("/cutils/qemu_strtoull/null",
1552 test_qemu_strtoull_null
);
1553 g_test_add_func("/cutils/qemu_strtoull/empty", test_qemu_strtoull_empty
);
1554 g_test_add_func("/cutils/qemu_strtoull/whitespace",
1555 test_qemu_strtoull_whitespace
);
1556 g_test_add_func("/cutils/qemu_strtoull/invalid",
1557 test_qemu_strtoull_invalid
);
1558 g_test_add_func("/cutils/qemu_strtoull/trailing",
1559 test_qemu_strtoull_trailing
);
1560 g_test_add_func("/cutils/qemu_strtoull/octal", test_qemu_strtoull_octal
);
1561 g_test_add_func("/cutils/qemu_strtoull/decimal",
1562 test_qemu_strtoull_decimal
);
1563 g_test_add_func("/cutils/qemu_strtoull/hex", test_qemu_strtoull_hex
);
1564 g_test_add_func("/cutils/qemu_strtoull/max", test_qemu_strtoull_max
);
1565 g_test_add_func("/cutils/qemu_strtoull/overflow",
1566 test_qemu_strtoull_overflow
);
1567 g_test_add_func("/cutils/qemu_strtoull/underflow",
1568 test_qemu_strtoull_underflow
);
1569 g_test_add_func("/cutils/qemu_strtoull/negative",
1570 test_qemu_strtoull_negative
);
1571 g_test_add_func("/cutils/qemu_strtoull_full/correct",
1572 test_qemu_strtoull_full_correct
);
1573 g_test_add_func("/cutils/qemu_strtoull_full/null",
1574 test_qemu_strtoull_full_null
);
1575 g_test_add_func("/cutils/qemu_strtoull_full/empty",
1576 test_qemu_strtoull_full_empty
);
1577 g_test_add_func("/cutils/qemu_strtoull_full/negative",
1578 test_qemu_strtoull_full_negative
);
1579 g_test_add_func("/cutils/qemu_strtoull_full/trailing",
1580 test_qemu_strtoull_full_trailing
);
1581 g_test_add_func("/cutils/qemu_strtoull_full/max",
1582 test_qemu_strtoull_full_max
);
1584 g_test_add_func("/cutils/strtosz/simple",
1585 test_qemu_strtosz_simple
);
1586 g_test_add_func("/cutils/strtosz/units",
1587 test_qemu_strtosz_units
);
1588 g_test_add_func("/cutils/strtosz/float",
1589 test_qemu_strtosz_float
);
1590 g_test_add_func("/cutils/strtosz/erange",
1591 test_qemu_strtosz_erange
);
1592 g_test_add_func("/cutils/strtosz/suffix-unit",
1593 test_qemu_strtosz_suffix_unit
);
1595 return g_test_run();