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"
30 #include "qemu/cutils.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_cmpint(i
, ==, 0);
43 g_assert(endptr
== NULL
);
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_cmpint(i
, ==, 0);
58 g_assert(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_cmpint(i
, ==, 0);
73 g_assert(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_cmpint(i
, ==, 0);
89 g_assert(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_cmpint(i
, ==, 123);
105 g_assert(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_cmpint(i
, ==, 123);
120 g_assert(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_cmpint(i
, ==, 0123);
135 g_assert(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_cmpint(i
, ==, 123);
150 g_assert(endptr
== str
+ strlen(str
));
154 static void test_parse_uint_llong_max(void)
156 unsigned long long i
= 999;
159 char *str
= g_strdup_printf("%llu", (unsigned long long)LLONG_MAX
+ 1);
162 r
= parse_uint(str
, &i
, &endptr
, 0);
164 g_assert_cmpint(r
, ==, 0);
165 g_assert_cmpint(i
, ==, (unsigned long long)LLONG_MAX
+ 1);
166 g_assert(endptr
== str
+ strlen(str
));
171 static void test_parse_uint_overflow(void)
173 unsigned long long i
= 999;
176 const char *str
= "99999999999999999999999999999999999999";
179 r
= parse_uint(str
, &i
, &endptr
, 0);
181 g_assert_cmpint(r
, ==, -ERANGE
);
182 g_assert_cmpint(i
, ==, ULLONG_MAX
);
183 g_assert(endptr
== str
+ strlen(str
));
186 static void test_parse_uint_negative(void)
188 unsigned long long i
= 999;
191 const char *str
= " \t -321";
194 r
= parse_uint(str
, &i
, &endptr
, 0);
196 g_assert_cmpint(r
, ==, -ERANGE
);
197 g_assert_cmpint(i
, ==, 0);
198 g_assert(endptr
== str
+ strlen(str
));
202 static void test_parse_uint_full_trailing(void)
204 unsigned long long i
= 999;
205 const char *str
= "123xxx";
208 r
= parse_uint_full(str
, &i
, 0);
210 g_assert_cmpint(r
, ==, -EINVAL
);
211 g_assert_cmpint(i
, ==, 0);
214 static void test_parse_uint_full_correct(void)
216 unsigned long long i
= 999;
217 const char *str
= "123";
220 r
= parse_uint_full(str
, &i
, 0);
222 g_assert_cmpint(r
, ==, 0);
223 g_assert_cmpint(i
, ==, 123);
226 static void test_qemu_strtol_correct(void)
228 const char *str
= "12345 foo";
230 const char *endptr
= &f
;
234 err
= qemu_strtol(str
, &endptr
, 0, &res
);
236 g_assert_cmpint(err
, ==, 0);
237 g_assert_cmpint(res
, ==, 12345);
238 g_assert(endptr
== str
+ 5);
241 static void test_qemu_strtol_null(void)
244 const char *endptr
= &f
;
248 err
= qemu_strtol(NULL
, &endptr
, 0, &res
);
250 g_assert_cmpint(err
, ==, -EINVAL
);
251 g_assert(endptr
== NULL
);
254 static void test_qemu_strtol_empty(void)
256 const char *str
= "";
258 const char *endptr
= &f
;
262 err
= qemu_strtol(str
, &endptr
, 0, &res
);
264 g_assert_cmpint(err
, ==, -EINVAL
);
267 static void test_qemu_strtol_whitespace(void)
269 const char *str
= " \t ";
271 const char *endptr
= &f
;
275 err
= qemu_strtol(str
, &endptr
, 0, &res
);
277 g_assert_cmpint(err
, ==, -EINVAL
);
280 static void test_qemu_strtol_invalid(void)
282 const char *str
= " xxxx \t abc";
284 const char *endptr
= &f
;
288 err
= qemu_strtol(str
, &endptr
, 0, &res
);
290 g_assert_cmpint(err
, ==, -EINVAL
);
293 static void test_qemu_strtol_trailing(void)
295 const char *str
= "123xxx";
297 const char *endptr
= &f
;
301 err
= qemu_strtol(str
, &endptr
, 0, &res
);
303 g_assert_cmpint(err
, ==, 0);
304 g_assert_cmpint(res
, ==, 123);
305 g_assert(endptr
== str
+ 3);
308 static void test_qemu_strtol_octal(void)
310 const char *str
= "0123";
312 const char *endptr
= &f
;
316 err
= qemu_strtol(str
, &endptr
, 8, &res
);
318 g_assert_cmpint(err
, ==, 0);
319 g_assert_cmpint(res
, ==, 0123);
320 g_assert(endptr
== str
+ strlen(str
));
324 err
= qemu_strtol(str
, &endptr
, 0, &res
);
326 g_assert_cmpint(err
, ==, 0);
327 g_assert_cmpint(res
, ==, 0123);
328 g_assert(endptr
== str
+ strlen(str
));
331 static void test_qemu_strtol_decimal(void)
333 const char *str
= "0123";
335 const char *endptr
= &f
;
339 err
= qemu_strtol(str
, &endptr
, 10, &res
);
341 g_assert_cmpint(err
, ==, 0);
342 g_assert_cmpint(res
, ==, 123);
343 g_assert(endptr
== str
+ strlen(str
));
348 err
= qemu_strtol(str
, &endptr
, 0, &res
);
350 g_assert_cmpint(err
, ==, 0);
351 g_assert_cmpint(res
, ==, 123);
352 g_assert(endptr
== str
+ strlen(str
));
355 static void test_qemu_strtol_hex(void)
357 const char *str
= "0123";
359 const char *endptr
= &f
;
363 err
= qemu_strtol(str
, &endptr
, 16, &res
);
365 g_assert_cmpint(err
, ==, 0);
366 g_assert_cmpint(res
, ==, 0x123);
367 g_assert(endptr
== str
+ strlen(str
));
372 err
= qemu_strtol(str
, &endptr
, 0, &res
);
374 g_assert_cmpint(err
, ==, 0);
375 g_assert_cmpint(res
, ==, 0x123);
376 g_assert(endptr
== str
+ strlen(str
));
379 static void test_qemu_strtol_max(void)
381 char *str
= g_strdup_printf("%ld", LONG_MAX
);
383 const char *endptr
= &f
;
387 err
= qemu_strtol(str
, &endptr
, 0, &res
);
389 g_assert_cmpint(err
, ==, 0);
390 g_assert_cmpint(res
, ==, LONG_MAX
);
391 g_assert(endptr
== str
+ strlen(str
));
395 static void test_qemu_strtol_overflow(void)
397 const char *str
= "99999999999999999999999999999999999999999999";
399 const char *endptr
= &f
;
403 err
= qemu_strtol(str
, &endptr
, 0, &res
);
405 g_assert_cmpint(err
, ==, -ERANGE
);
406 g_assert_cmpint(res
, ==, LONG_MAX
);
407 g_assert(endptr
== str
+ strlen(str
));
410 static void test_qemu_strtol_underflow(void)
412 const char *str
= "-99999999999999999999999999999999999999999999";
414 const char *endptr
= &f
;
418 err
= qemu_strtol(str
, &endptr
, 0, &res
);
420 g_assert_cmpint(err
, ==, -ERANGE
);
421 g_assert_cmpint(res
, ==, LONG_MIN
);
422 g_assert(endptr
== str
+ strlen(str
));
425 static void test_qemu_strtol_negative(void)
427 const char *str
= " \t -321";
429 const char *endptr
= &f
;
433 err
= qemu_strtol(str
, &endptr
, 0, &res
);
435 g_assert_cmpint(err
, ==, 0);
436 g_assert_cmpint(res
, ==, -321);
437 g_assert(endptr
== str
+ strlen(str
));
440 static void test_qemu_strtol_full_correct(void)
442 const char *str
= "123";
446 err
= qemu_strtol(str
, NULL
, 0, &res
);
448 g_assert_cmpint(err
, ==, 0);
449 g_assert_cmpint(res
, ==, 123);
452 static void test_qemu_strtol_full_null(void)
455 const char *endptr
= &f
;
459 err
= qemu_strtol(NULL
, &endptr
, 0, &res
);
461 g_assert_cmpint(err
, ==, -EINVAL
);
462 g_assert(endptr
== NULL
);
465 static void test_qemu_strtol_full_empty(void)
467 const char *str
= "";
471 err
= qemu_strtol(str
, NULL
, 0, &res
);
473 g_assert_cmpint(err
, ==, -EINVAL
);
476 static void test_qemu_strtol_full_negative(void)
478 const char *str
= " \t -321";
482 err
= qemu_strtol(str
, NULL
, 0, &res
);
484 g_assert_cmpint(err
, ==, 0);
485 g_assert_cmpint(res
, ==, -321);
488 static void test_qemu_strtol_full_trailing(void)
490 const char *str
= "123xxx";
494 err
= qemu_strtol(str
, NULL
, 0, &res
);
496 g_assert_cmpint(err
, ==, -EINVAL
);
499 static void test_qemu_strtol_full_max(void)
501 char *str
= g_strdup_printf("%ld", LONG_MAX
);
505 err
= qemu_strtol(str
, NULL
, 0, &res
);
507 g_assert_cmpint(err
, ==, 0);
508 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 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
));
681 static void test_qemu_strtoul_overflow(void)
683 const char *str
= "99999999999999999999999999999999999999999999";
685 const char *endptr
= &f
;
686 unsigned long res
= 999;
689 err
= qemu_strtoul(str
, &endptr
, 0, &res
);
691 g_assert_cmpint(err
, ==, -ERANGE
);
692 g_assert_cmpint(res
, ==, ULONG_MAX
);
693 g_assert(endptr
== str
+ strlen(str
));
696 static void test_qemu_strtoul_underflow(void)
698 const char *str
= "-99999999999999999999999999999999999999999999";
700 const char *endptr
= &f
;
701 unsigned long res
= 999;
704 err
= qemu_strtoul(str
, &endptr
, 0, &res
);
706 g_assert_cmpint(err
, ==, -ERANGE
);
707 g_assert_cmpint(res
, ==, -1ul);
708 g_assert(endptr
== str
+ strlen(str
));
711 static void test_qemu_strtoul_negative(void)
713 const char *str
= " \t -321";
715 const char *endptr
= &f
;
716 unsigned long res
= 999;
719 err
= qemu_strtoul(str
, &endptr
, 0, &res
);
721 g_assert_cmpint(err
, ==, 0);
722 g_assert_cmpint(res
, ==, -321ul);
723 g_assert(endptr
== str
+ strlen(str
));
726 static void test_qemu_strtoul_full_correct(void)
728 const char *str
= "123";
729 unsigned long res
= 999;
732 err
= qemu_strtoul(str
, NULL
, 0, &res
);
734 g_assert_cmpint(err
, ==, 0);
735 g_assert_cmpint(res
, ==, 123);
738 static void test_qemu_strtoul_full_null(void)
740 unsigned long res
= 999;
743 err
= qemu_strtoul(NULL
, NULL
, 0, &res
);
745 g_assert_cmpint(err
, ==, -EINVAL
);
748 static void test_qemu_strtoul_full_empty(void)
750 const char *str
= "";
751 unsigned long res
= 999;
754 err
= qemu_strtoul(str
, NULL
, 0, &res
);
756 g_assert_cmpint(err
, ==, -EINVAL
);
758 static void test_qemu_strtoul_full_negative(void)
760 const char *str
= " \t -321";
761 unsigned long res
= 999;
764 err
= qemu_strtoul(str
, NULL
, 0, &res
);
765 g_assert_cmpint(err
, ==, 0);
766 g_assert_cmpint(res
, ==, -321ul);
769 static void test_qemu_strtoul_full_trailing(void)
771 const char *str
= "123xxx";
775 err
= qemu_strtoul(str
, NULL
, 0, &res
);
777 g_assert_cmpint(err
, ==, -EINVAL
);
780 static void test_qemu_strtoul_full_max(void)
782 char *str
= g_strdup_printf("%lu", ULONG_MAX
);
783 unsigned long res
= 999;
786 err
= qemu_strtoul(str
, NULL
, 0, &res
);
788 g_assert_cmpint(err
, ==, 0);
789 g_assert_cmpint(res
, ==, ULONG_MAX
);
793 static void test_qemu_strtoll_correct(void)
795 const char *str
= "12345 foo";
797 const char *endptr
= &f
;
801 err
= qemu_strtoll(str
, &endptr
, 0, &res
);
803 g_assert_cmpint(err
, ==, 0);
804 g_assert_cmpint(res
, ==, 12345);
805 g_assert(endptr
== str
+ 5);
808 static void test_qemu_strtoll_null(void)
811 const char *endptr
= &f
;
815 err
= qemu_strtoll(NULL
, &endptr
, 0, &res
);
817 g_assert_cmpint(err
, ==, -EINVAL
);
818 g_assert(endptr
== NULL
);
821 static void test_qemu_strtoll_empty(void)
823 const char *str
= "";
825 const char *endptr
= &f
;
829 err
= qemu_strtoll(str
, &endptr
, 0, &res
);
831 g_assert_cmpint(err
, ==, -EINVAL
);
834 static void test_qemu_strtoll_whitespace(void)
836 const char *str
= " \t ";
838 const char *endptr
= &f
;
842 err
= qemu_strtoll(str
, &endptr
, 0, &res
);
844 g_assert_cmpint(err
, ==, -EINVAL
);
847 static void test_qemu_strtoll_invalid(void)
849 const char *str
= " xxxx \t abc";
851 const char *endptr
= &f
;
855 err
= qemu_strtoll(str
, &endptr
, 0, &res
);
857 g_assert_cmpint(err
, ==, -EINVAL
);
860 static void test_qemu_strtoll_trailing(void)
862 const char *str
= "123xxx";
864 const char *endptr
= &f
;
868 err
= qemu_strtoll(str
, &endptr
, 0, &res
);
870 g_assert_cmpint(err
, ==, 0);
871 g_assert_cmpint(res
, ==, 123);
872 g_assert(endptr
== str
+ 3);
875 static void test_qemu_strtoll_octal(void)
877 const char *str
= "0123";
879 const char *endptr
= &f
;
883 err
= qemu_strtoll(str
, &endptr
, 8, &res
);
885 g_assert_cmpint(err
, ==, 0);
886 g_assert_cmpint(res
, ==, 0123);
887 g_assert(endptr
== str
+ strlen(str
));
891 err
= qemu_strtoll(str
, &endptr
, 0, &res
);
893 g_assert_cmpint(err
, ==, 0);
894 g_assert_cmpint(res
, ==, 0123);
895 g_assert(endptr
== str
+ strlen(str
));
898 static void test_qemu_strtoll_decimal(void)
900 const char *str
= "0123";
902 const char *endptr
= &f
;
906 err
= qemu_strtoll(str
, &endptr
, 10, &res
);
908 g_assert_cmpint(err
, ==, 0);
909 g_assert_cmpint(res
, ==, 123);
910 g_assert(endptr
== str
+ strlen(str
));
915 err
= qemu_strtoll(str
, &endptr
, 0, &res
);
917 g_assert_cmpint(err
, ==, 0);
918 g_assert_cmpint(res
, ==, 123);
919 g_assert(endptr
== str
+ strlen(str
));
922 static void test_qemu_strtoll_hex(void)
924 const char *str
= "0123";
926 const char *endptr
= &f
;
930 err
= qemu_strtoll(str
, &endptr
, 16, &res
);
932 g_assert_cmpint(err
, ==, 0);
933 g_assert_cmpint(res
, ==, 0x123);
934 g_assert(endptr
== str
+ strlen(str
));
939 err
= qemu_strtoll(str
, &endptr
, 0, &res
);
941 g_assert_cmpint(err
, ==, 0);
942 g_assert_cmpint(res
, ==, 0x123);
943 g_assert(endptr
== str
+ strlen(str
));
946 static void test_qemu_strtoll_max(void)
948 char *str
= g_strdup_printf("%lld", LLONG_MAX
);
950 const char *endptr
= &f
;
954 err
= qemu_strtoll(str
, &endptr
, 0, &res
);
956 g_assert_cmpint(err
, ==, 0);
957 g_assert_cmpint(res
, ==, LLONG_MAX
);
958 g_assert(endptr
== str
+ strlen(str
));
962 static void test_qemu_strtoll_overflow(void)
964 const char *str
= "99999999999999999999999999999999999999999999";
966 const char *endptr
= &f
;
970 err
= qemu_strtoll(str
, &endptr
, 0, &res
);
972 g_assert_cmpint(err
, ==, -ERANGE
);
973 g_assert_cmpint(res
, ==, LLONG_MAX
);
974 g_assert(endptr
== str
+ strlen(str
));
977 static void test_qemu_strtoll_underflow(void)
979 const char *str
= "-99999999999999999999999999999999999999999999";
981 const char *endptr
= &f
;
985 err
= qemu_strtoll(str
, &endptr
, 0, &res
);
987 g_assert_cmpint(err
, ==, -ERANGE
);
988 g_assert_cmpint(res
, ==, LLONG_MIN
);
989 g_assert(endptr
== str
+ strlen(str
));
992 static void test_qemu_strtoll_negative(void)
994 const char *str
= " \t -321";
996 const char *endptr
= &f
;
1000 err
= qemu_strtoll(str
, &endptr
, 0, &res
);
1002 g_assert_cmpint(err
, ==, 0);
1003 g_assert_cmpint(res
, ==, -321);
1004 g_assert(endptr
== str
+ strlen(str
));
1007 static void test_qemu_strtoll_full_correct(void)
1009 const char *str
= "123";
1013 err
= qemu_strtoll(str
, NULL
, 0, &res
);
1015 g_assert_cmpint(err
, ==, 0);
1016 g_assert_cmpint(res
, ==, 123);
1019 static void test_qemu_strtoll_full_null(void)
1024 err
= qemu_strtoll(NULL
, NULL
, 0, &res
);
1026 g_assert_cmpint(err
, ==, -EINVAL
);
1029 static void test_qemu_strtoll_full_empty(void)
1031 const char *str
= "";
1035 err
= qemu_strtoll(str
, NULL
, 0, &res
);
1037 g_assert_cmpint(err
, ==, -EINVAL
);
1040 static void test_qemu_strtoll_full_negative(void)
1042 const char *str
= " \t -321";
1046 err
= qemu_strtoll(str
, NULL
, 0, &res
);
1048 g_assert_cmpint(err
, ==, 0);
1049 g_assert_cmpint(res
, ==, -321);
1052 static void test_qemu_strtoll_full_trailing(void)
1054 const char *str
= "123xxx";
1058 err
= qemu_strtoll(str
, NULL
, 0, &res
);
1060 g_assert_cmpint(err
, ==, -EINVAL
);
1063 static void test_qemu_strtoll_full_max(void)
1066 char *str
= g_strdup_printf("%lld", LLONG_MAX
);
1070 err
= qemu_strtoll(str
, NULL
, 0, &res
);
1072 g_assert_cmpint(err
, ==, 0);
1073 g_assert_cmpint(res
, ==, LLONG_MAX
);
1077 static void test_qemu_strtoull_correct(void)
1079 const char *str
= "12345 foo";
1081 const char *endptr
= &f
;
1085 err
= qemu_strtoull(str
, &endptr
, 0, &res
);
1087 g_assert_cmpint(err
, ==, 0);
1088 g_assert_cmpint(res
, ==, 12345);
1089 g_assert(endptr
== str
+ 5);
1092 static void test_qemu_strtoull_null(void)
1095 const char *endptr
= &f
;
1099 err
= qemu_strtoull(NULL
, &endptr
, 0, &res
);
1101 g_assert_cmpint(err
, ==, -EINVAL
);
1102 g_assert(endptr
== NULL
);
1105 static void test_qemu_strtoull_empty(void)
1107 const char *str
= "";
1109 const char *endptr
= &f
;
1113 err
= qemu_strtoull(str
, &endptr
, 0, &res
);
1115 g_assert_cmpint(err
, ==, -EINVAL
);
1118 static void test_qemu_strtoull_whitespace(void)
1120 const char *str
= " \t ";
1122 const char *endptr
= &f
;
1126 err
= qemu_strtoull(str
, &endptr
, 0, &res
);
1128 g_assert_cmpint(err
, ==, -EINVAL
);
1131 static void test_qemu_strtoull_invalid(void)
1133 const char *str
= " xxxx \t abc";
1135 const char *endptr
= &f
;
1139 err
= qemu_strtoull(str
, &endptr
, 0, &res
);
1141 g_assert_cmpint(err
, ==, -EINVAL
);
1144 static void test_qemu_strtoull_trailing(void)
1146 const char *str
= "123xxx";
1148 const char *endptr
= &f
;
1152 err
= qemu_strtoull(str
, &endptr
, 0, &res
);
1154 g_assert_cmpint(err
, ==, 0);
1155 g_assert_cmpint(res
, ==, 123);
1156 g_assert(endptr
== str
+ 3);
1159 static void test_qemu_strtoull_octal(void)
1161 const char *str
= "0123";
1163 const char *endptr
= &f
;
1167 err
= qemu_strtoull(str
, &endptr
, 8, &res
);
1169 g_assert_cmpint(err
, ==, 0);
1170 g_assert_cmpint(res
, ==, 0123);
1171 g_assert(endptr
== str
+ strlen(str
));
1175 err
= qemu_strtoull(str
, &endptr
, 0, &res
);
1177 g_assert_cmpint(err
, ==, 0);
1178 g_assert_cmpint(res
, ==, 0123);
1179 g_assert(endptr
== str
+ strlen(str
));
1182 static void test_qemu_strtoull_decimal(void)
1184 const char *str
= "0123";
1186 const char *endptr
= &f
;
1190 err
= qemu_strtoull(str
, &endptr
, 10, &res
);
1192 g_assert_cmpint(err
, ==, 0);
1193 g_assert_cmpint(res
, ==, 123);
1194 g_assert(endptr
== str
+ strlen(str
));
1199 err
= qemu_strtoull(str
, &endptr
, 0, &res
);
1201 g_assert_cmpint(err
, ==, 0);
1202 g_assert_cmpint(res
, ==, 123);
1203 g_assert(endptr
== str
+ strlen(str
));
1206 static void test_qemu_strtoull_hex(void)
1208 const char *str
= "0123";
1210 const char *endptr
= &f
;
1214 err
= qemu_strtoull(str
, &endptr
, 16, &res
);
1216 g_assert_cmpint(err
, ==, 0);
1217 g_assert_cmpint(res
, ==, 0x123);
1218 g_assert(endptr
== str
+ strlen(str
));
1223 err
= qemu_strtoull(str
, &endptr
, 0, &res
);
1225 g_assert_cmpint(err
, ==, 0);
1226 g_assert_cmpint(res
, ==, 0x123);
1227 g_assert(endptr
== str
+ strlen(str
));
1230 static void test_qemu_strtoull_max(void)
1232 char *str
= g_strdup_printf("%llu", ULLONG_MAX
);
1234 const char *endptr
= &f
;
1238 err
= qemu_strtoull(str
, &endptr
, 0, &res
);
1240 g_assert_cmpint(err
, ==, 0);
1241 g_assert_cmpint(res
, ==, ULLONG_MAX
);
1242 g_assert(endptr
== str
+ strlen(str
));
1246 static void test_qemu_strtoull_overflow(void)
1248 const char *str
= "99999999999999999999999999999999999999999999";
1250 const char *endptr
= &f
;
1254 err
= qemu_strtoull(str
, &endptr
, 0, &res
);
1256 g_assert_cmpint(err
, ==, -ERANGE
);
1257 g_assert_cmpint(res
, ==, ULLONG_MAX
);
1258 g_assert(endptr
== str
+ strlen(str
));
1261 static void test_qemu_strtoull_underflow(void)
1263 const char *str
= "-99999999999999999999999999999999999999999999";
1265 const char *endptr
= &f
;
1269 err
= qemu_strtoull(str
, &endptr
, 0, &res
);
1271 g_assert_cmpint(err
, ==, -ERANGE
);
1272 g_assert_cmpint(res
, ==, -1);
1273 g_assert(endptr
== str
+ strlen(str
));
1276 static void test_qemu_strtoull_negative(void)
1278 const char *str
= " \t -321";
1280 const char *endptr
= &f
;
1284 err
= qemu_strtoull(str
, &endptr
, 0, &res
);
1286 g_assert_cmpint(err
, ==, 0);
1287 g_assert_cmpint(res
, ==, -321);
1288 g_assert(endptr
== str
+ strlen(str
));
1291 static void test_qemu_strtoull_full_correct(void)
1293 const char *str
= "18446744073709551614";
1297 err
= qemu_strtoull(str
, NULL
, 0, &res
);
1299 g_assert_cmpint(err
, ==, 0);
1300 g_assert_cmpint(res
, ==, 18446744073709551614LLU);
1303 static void test_qemu_strtoull_full_null(void)
1308 err
= qemu_strtoull(NULL
, NULL
, 0, &res
);
1310 g_assert_cmpint(err
, ==, -EINVAL
);
1313 static void test_qemu_strtoull_full_empty(void)
1315 const char *str
= "";
1319 err
= qemu_strtoull(str
, NULL
, 0, &res
);
1321 g_assert_cmpint(err
, ==, -EINVAL
);
1324 static void test_qemu_strtoull_full_negative(void)
1326 const char *str
= " \t -321";
1330 err
= qemu_strtoull(str
, NULL
, 0, &res
);
1332 g_assert_cmpint(err
, ==, 0);
1333 g_assert_cmpint(res
, ==, 18446744073709551295LLU);
1336 static void test_qemu_strtoull_full_trailing(void)
1338 const char *str
= "18446744073709551614xxxxxx";
1342 err
= qemu_strtoull(str
, NULL
, 0, &res
);
1344 g_assert_cmpint(err
, ==, -EINVAL
);
1347 static void test_qemu_strtoull_full_max(void)
1349 char *str
= g_strdup_printf("%lld", ULLONG_MAX
);
1353 err
= qemu_strtoull(str
, NULL
, 0, &res
);
1355 g_assert_cmpint(err
, ==, 0);
1356 g_assert_cmpint(res
, ==, ULLONG_MAX
);
1360 static void test_qemu_strtosz_simple(void)
1362 const char *str
= "12345M";
1363 char *endptr
= NULL
;
1366 res
= qemu_strtosz(str
, &endptr
);
1367 g_assert_cmpint(res
, ==, 12345 * M_BYTE
);
1368 g_assert(endptr
== str
+ 6);
1370 res
= qemu_strtosz(str
, NULL
);
1371 g_assert_cmpint(res
, ==, 12345 * M_BYTE
);
1374 static void test_qemu_strtosz_units(void)
1376 const char *none
= "1";
1377 const char *b
= "1B";
1378 const char *k
= "1K";
1379 const char *m
= "1M";
1380 const char *g
= "1G";
1381 const char *t
= "1T";
1382 const char *p
= "1P";
1383 const char *e
= "1E";
1387 res
= qemu_strtosz(none
, NULL
);
1388 g_assert_cmpint(res
, ==, M_BYTE
);
1390 res
= qemu_strtosz(b
, NULL
);
1391 g_assert_cmpint(res
, ==, 1);
1393 res
= qemu_strtosz(k
, NULL
);
1394 g_assert_cmpint(res
, ==, K_BYTE
);
1396 res
= qemu_strtosz(m
, NULL
);
1397 g_assert_cmpint(res
, ==, M_BYTE
);
1399 res
= qemu_strtosz(g
, NULL
);
1400 g_assert_cmpint(res
, ==, G_BYTE
);
1402 res
= qemu_strtosz(t
, NULL
);
1403 g_assert_cmpint(res
, ==, T_BYTE
);
1405 res
= qemu_strtosz(p
, NULL
);
1406 g_assert_cmpint(res
, ==, P_BYTE
);
1408 res
= qemu_strtosz(e
, NULL
);
1409 g_assert_cmpint(res
, ==, E_BYTE
);
1412 static void test_qemu_strtosz_float(void)
1414 const char *str
= "12.345M";
1417 res
= qemu_strtosz(str
, NULL
);
1418 g_assert_cmpint(res
, ==, 12.345 * M_BYTE
);
1421 static void test_qemu_strtosz_erange(void)
1423 const char *str
= "10E";
1426 res
= qemu_strtosz(str
, NULL
);
1427 g_assert_cmpint(res
, ==, -ERANGE
);
1430 static void test_qemu_strtosz_suffix_unit(void)
1432 const char *str
= "12345";
1435 res
= qemu_strtosz_suffix_unit(str
, NULL
,
1436 QEMU_STRTOSZ_DEFSUFFIX_KB
, 1000);
1437 g_assert_cmpint(res
, ==, 12345000);
1440 int main(int argc
, char **argv
)
1442 g_test_init(&argc
, &argv
, NULL
);
1444 g_test_add_func("/cutils/parse_uint/null", test_parse_uint_null
);
1445 g_test_add_func("/cutils/parse_uint/empty", test_parse_uint_empty
);
1446 g_test_add_func("/cutils/parse_uint/whitespace",
1447 test_parse_uint_whitespace
);
1448 g_test_add_func("/cutils/parse_uint/invalid", test_parse_uint_invalid
);
1449 g_test_add_func("/cutils/parse_uint/trailing", test_parse_uint_trailing
);
1450 g_test_add_func("/cutils/parse_uint/correct", test_parse_uint_correct
);
1451 g_test_add_func("/cutils/parse_uint/octal", test_parse_uint_octal
);
1452 g_test_add_func("/cutils/parse_uint/decimal", test_parse_uint_decimal
);
1453 g_test_add_func("/cutils/parse_uint/llong_max", test_parse_uint_llong_max
);
1454 g_test_add_func("/cutils/parse_uint/overflow", test_parse_uint_overflow
);
1455 g_test_add_func("/cutils/parse_uint/negative", test_parse_uint_negative
);
1456 g_test_add_func("/cutils/parse_uint_full/trailing",
1457 test_parse_uint_full_trailing
);
1458 g_test_add_func("/cutils/parse_uint_full/correct",
1459 test_parse_uint_full_correct
);
1461 /* qemu_strtol() tests */
1462 g_test_add_func("/cutils/qemu_strtol/correct", test_qemu_strtol_correct
);
1463 g_test_add_func("/cutils/qemu_strtol/null", test_qemu_strtol_null
);
1464 g_test_add_func("/cutils/qemu_strtol/empty", test_qemu_strtol_empty
);
1465 g_test_add_func("/cutils/qemu_strtol/whitespace",
1466 test_qemu_strtol_whitespace
);
1467 g_test_add_func("/cutils/qemu_strtol/invalid", test_qemu_strtol_invalid
);
1468 g_test_add_func("/cutils/qemu_strtol/trailing", test_qemu_strtol_trailing
);
1469 g_test_add_func("/cutils/qemu_strtol/octal", test_qemu_strtol_octal
);
1470 g_test_add_func("/cutils/qemu_strtol/decimal", test_qemu_strtol_decimal
);
1471 g_test_add_func("/cutils/qemu_strtol/hex", test_qemu_strtol_hex
);
1472 g_test_add_func("/cutils/qemu_strtol/max", test_qemu_strtol_max
);
1473 g_test_add_func("/cutils/qemu_strtol/overflow", test_qemu_strtol_overflow
);
1474 g_test_add_func("/cutils/qemu_strtol/underflow",
1475 test_qemu_strtol_underflow
);
1476 g_test_add_func("/cutils/qemu_strtol/negative", test_qemu_strtol_negative
);
1477 g_test_add_func("/cutils/qemu_strtol_full/correct",
1478 test_qemu_strtol_full_correct
);
1479 g_test_add_func("/cutils/qemu_strtol_full/null",
1480 test_qemu_strtol_full_null
);
1481 g_test_add_func("/cutils/qemu_strtol_full/empty",
1482 test_qemu_strtol_full_empty
);
1483 g_test_add_func("/cutils/qemu_strtol_full/negative",
1484 test_qemu_strtol_full_negative
);
1485 g_test_add_func("/cutils/qemu_strtol_full/trailing",
1486 test_qemu_strtol_full_trailing
);
1487 g_test_add_func("/cutils/qemu_strtol_full/max",
1488 test_qemu_strtol_full_max
);
1490 /* qemu_strtoul() tests */
1491 g_test_add_func("/cutils/qemu_strtoul/correct", test_qemu_strtoul_correct
);
1492 g_test_add_func("/cutils/qemu_strtoul/null", test_qemu_strtoul_null
);
1493 g_test_add_func("/cutils/qemu_strtoul/empty", test_qemu_strtoul_empty
);
1494 g_test_add_func("/cutils/qemu_strtoul/whitespace",
1495 test_qemu_strtoul_whitespace
);
1496 g_test_add_func("/cutils/qemu_strtoul/invalid", test_qemu_strtoul_invalid
);
1497 g_test_add_func("/cutils/qemu_strtoul/trailing",
1498 test_qemu_strtoul_trailing
);
1499 g_test_add_func("/cutils/qemu_strtoul/octal", test_qemu_strtoul_octal
);
1500 g_test_add_func("/cutils/qemu_strtoul/decimal", test_qemu_strtoul_decimal
);
1501 g_test_add_func("/cutils/qemu_strtoul/hex", test_qemu_strtoul_hex
);
1502 g_test_add_func("/cutils/qemu_strtoul/max", test_qemu_strtoul_max
);
1503 g_test_add_func("/cutils/qemu_strtoul/overflow",
1504 test_qemu_strtoul_overflow
);
1505 g_test_add_func("/cutils/qemu_strtoul/underflow",
1506 test_qemu_strtoul_underflow
);
1507 g_test_add_func("/cutils/qemu_strtoul/negative",
1508 test_qemu_strtoul_negative
);
1509 g_test_add_func("/cutils/qemu_strtoul_full/correct",
1510 test_qemu_strtoul_full_correct
);
1511 g_test_add_func("/cutils/qemu_strtoul_full/null",
1512 test_qemu_strtoul_full_null
);
1513 g_test_add_func("/cutils/qemu_strtoul_full/empty",
1514 test_qemu_strtoul_full_empty
);
1515 g_test_add_func("/cutils/qemu_strtoul_full/negative",
1516 test_qemu_strtoul_full_negative
);
1517 g_test_add_func("/cutils/qemu_strtoul_full/trailing",
1518 test_qemu_strtoul_full_trailing
);
1519 g_test_add_func("/cutils/qemu_strtoul_full/max",
1520 test_qemu_strtoul_full_max
);
1522 /* qemu_strtoll() tests */
1523 g_test_add_func("/cutils/qemu_strtoll/correct", test_qemu_strtoll_correct
);
1524 g_test_add_func("/cutils/qemu_strtoll/null", test_qemu_strtoll_null
);
1525 g_test_add_func("/cutils/qemu_strtoll/empty", test_qemu_strtoll_empty
);
1526 g_test_add_func("/cutils/qemu_strtoll/whitespace",
1527 test_qemu_strtoll_whitespace
);
1528 g_test_add_func("/cutils/qemu_strtoll/invalid", test_qemu_strtoll_invalid
);
1529 g_test_add_func("/cutils/qemu_strtoll/trailing",
1530 test_qemu_strtoll_trailing
);
1531 g_test_add_func("/cutils/qemu_strtoll/octal", test_qemu_strtoll_octal
);
1532 g_test_add_func("/cutils/qemu_strtoll/decimal", test_qemu_strtoll_decimal
);
1533 g_test_add_func("/cutils/qemu_strtoll/hex", test_qemu_strtoll_hex
);
1534 g_test_add_func("/cutils/qemu_strtoll/max", test_qemu_strtoll_max
);
1535 g_test_add_func("/cutils/qemu_strtoll/overflow",
1536 test_qemu_strtoll_overflow
);
1537 g_test_add_func("/cutils/qemu_strtoll/underflow",
1538 test_qemu_strtoll_underflow
);
1539 g_test_add_func("/cutils/qemu_strtoll/negative",
1540 test_qemu_strtoll_negative
);
1541 g_test_add_func("/cutils/qemu_strtoll_full/correct",
1542 test_qemu_strtoll_full_correct
);
1543 g_test_add_func("/cutils/qemu_strtoll_full/null",
1544 test_qemu_strtoll_full_null
);
1545 g_test_add_func("/cutils/qemu_strtoll_full/empty",
1546 test_qemu_strtoll_full_empty
);
1547 g_test_add_func("/cutils/qemu_strtoll_full/negative",
1548 test_qemu_strtoll_full_negative
);
1549 g_test_add_func("/cutils/qemu_strtoll_full/trailing",
1550 test_qemu_strtoll_full_trailing
);
1551 g_test_add_func("/cutils/qemu_strtoll_full/max",
1552 test_qemu_strtoll_full_max
);
1554 /* qemu_strtoull() tests */
1555 g_test_add_func("/cutils/qemu_strtoull/correct",
1556 test_qemu_strtoull_correct
);
1557 g_test_add_func("/cutils/qemu_strtoull/null",
1558 test_qemu_strtoull_null
);
1559 g_test_add_func("/cutils/qemu_strtoull/empty", test_qemu_strtoull_empty
);
1560 g_test_add_func("/cutils/qemu_strtoull/whitespace",
1561 test_qemu_strtoull_whitespace
);
1562 g_test_add_func("/cutils/qemu_strtoull/invalid",
1563 test_qemu_strtoull_invalid
);
1564 g_test_add_func("/cutils/qemu_strtoull/trailing",
1565 test_qemu_strtoull_trailing
);
1566 g_test_add_func("/cutils/qemu_strtoull/octal", test_qemu_strtoull_octal
);
1567 g_test_add_func("/cutils/qemu_strtoull/decimal",
1568 test_qemu_strtoull_decimal
);
1569 g_test_add_func("/cutils/qemu_strtoull/hex", test_qemu_strtoull_hex
);
1570 g_test_add_func("/cutils/qemu_strtoull/max", test_qemu_strtoull_max
);
1571 g_test_add_func("/cutils/qemu_strtoull/overflow",
1572 test_qemu_strtoull_overflow
);
1573 g_test_add_func("/cutils/qemu_strtoull/underflow",
1574 test_qemu_strtoull_underflow
);
1575 g_test_add_func("/cutils/qemu_strtoull/negative",
1576 test_qemu_strtoull_negative
);
1577 g_test_add_func("/cutils/qemu_strtoull_full/correct",
1578 test_qemu_strtoull_full_correct
);
1579 g_test_add_func("/cutils/qemu_strtoull_full/null",
1580 test_qemu_strtoull_full_null
);
1581 g_test_add_func("/cutils/qemu_strtoull_full/empty",
1582 test_qemu_strtoull_full_empty
);
1583 g_test_add_func("/cutils/qemu_strtoull_full/negative",
1584 test_qemu_strtoull_full_negative
);
1585 g_test_add_func("/cutils/qemu_strtoull_full/trailing",
1586 test_qemu_strtoull_full_trailing
);
1587 g_test_add_func("/cutils/qemu_strtoull_full/max",
1588 test_qemu_strtoull_full_max
);
1590 g_test_add_func("/cutils/strtosz/simple",
1591 test_qemu_strtosz_simple
);
1592 g_test_add_func("/cutils/strtosz/units",
1593 test_qemu_strtosz_units
);
1594 g_test_add_func("/cutils/strtosz/float",
1595 test_qemu_strtosz_float
);
1596 g_test_add_func("/cutils/strtosz/erange",
1597 test_qemu_strtosz_erange
);
1598 g_test_add_func("/cutils/strtosz/suffix-unit",
1599 test_qemu_strtosz_suffix_unit
);
1601 return g_test_run();