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/cutils.h"
33 static void test_parse_uint_null(void)
35 unsigned long long i
= 999;
40 r
= parse_uint(NULL
, &i
, &endptr
, 0);
42 g_assert_cmpint(r
, ==, -EINVAL
);
43 g_assert_cmpint(i
, ==, 0);
44 g_assert(endptr
== NULL
);
47 static void test_parse_uint_empty(void)
49 unsigned long long i
= 999;
55 r
= parse_uint(str
, &i
, &endptr
, 0);
57 g_assert_cmpint(r
, ==, -EINVAL
);
58 g_assert_cmpint(i
, ==, 0);
59 g_assert(endptr
== str
);
62 static void test_parse_uint_whitespace(void)
64 unsigned long long i
= 999;
67 const char *str
= " \t ";
70 r
= parse_uint(str
, &i
, &endptr
, 0);
72 g_assert_cmpint(r
, ==, -EINVAL
);
73 g_assert_cmpint(i
, ==, 0);
74 g_assert(endptr
== str
);
78 static void test_parse_uint_invalid(void)
80 unsigned long long i
= 999;
83 const char *str
= " \t xxx";
86 r
= parse_uint(str
, &i
, &endptr
, 0);
88 g_assert_cmpint(r
, ==, -EINVAL
);
89 g_assert_cmpint(i
, ==, 0);
90 g_assert(endptr
== str
);
94 static void test_parse_uint_trailing(void)
96 unsigned long long i
= 999;
99 const char *str
= "123xxx";
102 r
= parse_uint(str
, &i
, &endptr
, 0);
104 g_assert_cmpint(r
, ==, 0);
105 g_assert_cmpint(i
, ==, 123);
106 g_assert(endptr
== str
+ 3);
109 static void test_parse_uint_correct(void)
111 unsigned long long i
= 999;
114 const char *str
= "123";
117 r
= parse_uint(str
, &i
, &endptr
, 0);
119 g_assert_cmpint(r
, ==, 0);
120 g_assert_cmpint(i
, ==, 123);
121 g_assert(endptr
== str
+ strlen(str
));
124 static void test_parse_uint_octal(void)
126 unsigned long long i
= 999;
129 const char *str
= "0123";
132 r
= parse_uint(str
, &i
, &endptr
, 0);
134 g_assert_cmpint(r
, ==, 0);
135 g_assert_cmpint(i
, ==, 0123);
136 g_assert(endptr
== str
+ strlen(str
));
139 static void test_parse_uint_decimal(void)
141 unsigned long long i
= 999;
144 const char *str
= "0123";
147 r
= parse_uint(str
, &i
, &endptr
, 10);
149 g_assert_cmpint(r
, ==, 0);
150 g_assert_cmpint(i
, ==, 123);
151 g_assert(endptr
== str
+ strlen(str
));
155 static void test_parse_uint_llong_max(void)
157 unsigned long long i
= 999;
160 char *str
= g_strdup_printf("%llu", (unsigned long long)LLONG_MAX
+ 1);
163 r
= parse_uint(str
, &i
, &endptr
, 0);
165 g_assert_cmpint(r
, ==, 0);
166 g_assert_cmpint(i
, ==, (unsigned long long)LLONG_MAX
+ 1);
167 g_assert(endptr
== str
+ strlen(str
));
172 static void test_parse_uint_overflow(void)
174 unsigned long long i
= 999;
177 const char *str
= "99999999999999999999999999999999999999";
180 r
= parse_uint(str
, &i
, &endptr
, 0);
182 g_assert_cmpint(r
, ==, -ERANGE
);
183 g_assert_cmpint(i
, ==, ULLONG_MAX
);
184 g_assert(endptr
== str
+ strlen(str
));
187 static void test_parse_uint_negative(void)
189 unsigned long long i
= 999;
192 const char *str
= " \t -321";
195 r
= parse_uint(str
, &i
, &endptr
, 0);
197 g_assert_cmpint(r
, ==, -ERANGE
);
198 g_assert_cmpint(i
, ==, 0);
199 g_assert(endptr
== str
+ strlen(str
));
203 static void test_parse_uint_full_trailing(void)
205 unsigned long long i
= 999;
206 const char *str
= "123xxx";
209 r
= parse_uint_full(str
, &i
, 0);
211 g_assert_cmpint(r
, ==, -EINVAL
);
212 g_assert_cmpint(i
, ==, 0);
215 static void test_parse_uint_full_correct(void)
217 unsigned long long i
= 999;
218 const char *str
= "123";
221 r
= parse_uint_full(str
, &i
, 0);
223 g_assert_cmpint(r
, ==, 0);
224 g_assert_cmpint(i
, ==, 123);
227 static void test_qemu_strtol_correct(void)
229 const char *str
= "12345 foo";
231 const char *endptr
= &f
;
235 err
= qemu_strtol(str
, &endptr
, 0, &res
);
237 g_assert_cmpint(err
, ==, 0);
238 g_assert_cmpint(res
, ==, 12345);
239 g_assert(endptr
== str
+ 5);
242 static void test_qemu_strtol_null(void)
245 const char *endptr
= &f
;
249 err
= qemu_strtol(NULL
, &endptr
, 0, &res
);
251 g_assert_cmpint(err
, ==, -EINVAL
);
252 g_assert(endptr
== NULL
);
255 static void test_qemu_strtol_empty(void)
257 const char *str
= "";
259 const char *endptr
= &f
;
263 err
= qemu_strtol(str
, &endptr
, 0, &res
);
265 g_assert_cmpint(err
, ==, -EINVAL
);
268 static void test_qemu_strtol_whitespace(void)
270 const char *str
= " \t ";
272 const char *endptr
= &f
;
276 err
= qemu_strtol(str
, &endptr
, 0, &res
);
278 g_assert_cmpint(err
, ==, -EINVAL
);
281 static void test_qemu_strtol_invalid(void)
283 const char *str
= " xxxx \t abc";
285 const char *endptr
= &f
;
289 err
= qemu_strtol(str
, &endptr
, 0, &res
);
291 g_assert_cmpint(err
, ==, -EINVAL
);
294 static void test_qemu_strtol_trailing(void)
296 const char *str
= "123xxx";
298 const char *endptr
= &f
;
302 err
= qemu_strtol(str
, &endptr
, 0, &res
);
304 g_assert_cmpint(err
, ==, 0);
305 g_assert_cmpint(res
, ==, 123);
306 g_assert(endptr
== str
+ 3);
309 static void test_qemu_strtol_octal(void)
311 const char *str
= "0123";
313 const char *endptr
= &f
;
317 err
= qemu_strtol(str
, &endptr
, 8, &res
);
319 g_assert_cmpint(err
, ==, 0);
320 g_assert_cmpint(res
, ==, 0123);
321 g_assert(endptr
== str
+ strlen(str
));
325 err
= qemu_strtol(str
, &endptr
, 0, &res
);
327 g_assert_cmpint(err
, ==, 0);
328 g_assert_cmpint(res
, ==, 0123);
329 g_assert(endptr
== str
+ strlen(str
));
332 static void test_qemu_strtol_decimal(void)
334 const char *str
= "0123";
336 const char *endptr
= &f
;
340 err
= qemu_strtol(str
, &endptr
, 10, &res
);
342 g_assert_cmpint(err
, ==, 0);
343 g_assert_cmpint(res
, ==, 123);
344 g_assert(endptr
== str
+ strlen(str
));
349 err
= qemu_strtol(str
, &endptr
, 0, &res
);
351 g_assert_cmpint(err
, ==, 0);
352 g_assert_cmpint(res
, ==, 123);
353 g_assert(endptr
== str
+ strlen(str
));
356 static void test_qemu_strtol_hex(void)
358 const char *str
= "0123";
360 const char *endptr
= &f
;
364 err
= qemu_strtol(str
, &endptr
, 16, &res
);
366 g_assert_cmpint(err
, ==, 0);
367 g_assert_cmpint(res
, ==, 0x123);
368 g_assert(endptr
== str
+ strlen(str
));
373 err
= qemu_strtol(str
, &endptr
, 0, &res
);
375 g_assert_cmpint(err
, ==, 0);
376 g_assert_cmpint(res
, ==, 0x123);
377 g_assert(endptr
== str
+ strlen(str
));
380 static void test_qemu_strtol_max(void)
382 const char *str
= g_strdup_printf("%ld", LONG_MAX
);
384 const char *endptr
= &f
;
388 err
= qemu_strtol(str
, &endptr
, 0, &res
);
390 g_assert_cmpint(err
, ==, 0);
391 g_assert_cmpint(res
, ==, LONG_MAX
);
392 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 const 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
);
511 static void test_qemu_strtoul_correct(void)
513 const char *str
= "12345 foo";
515 const char *endptr
= &f
;
516 unsigned long res
= 999;
519 err
= qemu_strtoul(str
, &endptr
, 0, &res
);
521 g_assert_cmpint(err
, ==, 0);
522 g_assert_cmpint(res
, ==, 12345);
523 g_assert(endptr
== str
+ 5);
526 static void test_qemu_strtoul_null(void)
529 const char *endptr
= &f
;
530 unsigned long res
= 999;
533 err
= qemu_strtoul(NULL
, &endptr
, 0, &res
);
535 g_assert_cmpint(err
, ==, -EINVAL
);
536 g_assert(endptr
== NULL
);
539 static void test_qemu_strtoul_empty(void)
541 const char *str
= "";
543 const char *endptr
= &f
;
544 unsigned long res
= 999;
547 err
= qemu_strtoul(str
, &endptr
, 0, &res
);
549 g_assert_cmpint(err
, ==, -EINVAL
);
552 static void test_qemu_strtoul_whitespace(void)
554 const char *str
= " \t ";
556 const char *endptr
= &f
;
557 unsigned long res
= 999;
560 err
= qemu_strtoul(str
, &endptr
, 0, &res
);
562 g_assert_cmpint(err
, ==, -EINVAL
);
565 static void test_qemu_strtoul_invalid(void)
567 const char *str
= " xxxx \t abc";
569 const char *endptr
= &f
;
570 unsigned long res
= 999;
573 err
= qemu_strtoul(str
, &endptr
, 0, &res
);
575 g_assert_cmpint(err
, ==, -EINVAL
);
578 static void test_qemu_strtoul_trailing(void)
580 const char *str
= "123xxx";
582 const char *endptr
= &f
;
583 unsigned long res
= 999;
586 err
= qemu_strtoul(str
, &endptr
, 0, &res
);
588 g_assert_cmpint(err
, ==, 0);
589 g_assert_cmpint(res
, ==, 123);
590 g_assert(endptr
== str
+ 3);
593 static void test_qemu_strtoul_octal(void)
595 const char *str
= "0123";
597 const char *endptr
= &f
;
598 unsigned long res
= 999;
601 err
= qemu_strtoul(str
, &endptr
, 8, &res
);
603 g_assert_cmpint(err
, ==, 0);
604 g_assert_cmpint(res
, ==, 0123);
605 g_assert(endptr
== str
+ strlen(str
));
609 err
= qemu_strtoul(str
, &endptr
, 0, &res
);
611 g_assert_cmpint(err
, ==, 0);
612 g_assert_cmpint(res
, ==, 0123);
613 g_assert(endptr
== str
+ strlen(str
));
616 static void test_qemu_strtoul_decimal(void)
618 const char *str
= "0123";
620 const char *endptr
= &f
;
621 unsigned long res
= 999;
624 err
= qemu_strtoul(str
, &endptr
, 10, &res
);
626 g_assert_cmpint(err
, ==, 0);
627 g_assert_cmpint(res
, ==, 123);
628 g_assert(endptr
== str
+ strlen(str
));
633 err
= qemu_strtoul(str
, &endptr
, 0, &res
);
635 g_assert_cmpint(err
, ==, 0);
636 g_assert_cmpint(res
, ==, 123);
637 g_assert(endptr
== str
+ strlen(str
));
640 static void test_qemu_strtoul_hex(void)
642 const char *str
= "0123";
644 const char *endptr
= &f
;
645 unsigned long res
= 999;
648 err
= qemu_strtoul(str
, &endptr
, 16, &res
);
650 g_assert_cmpint(err
, ==, 0);
651 g_assert_cmpint(res
, ==, 0x123);
652 g_assert(endptr
== str
+ strlen(str
));
657 err
= qemu_strtoul(str
, &endptr
, 0, &res
);
659 g_assert_cmpint(err
, ==, 0);
660 g_assert_cmpint(res
, ==, 0x123);
661 g_assert(endptr
== str
+ strlen(str
));
664 static void test_qemu_strtoul_max(void)
666 const char *str
= g_strdup_printf("%lu", ULONG_MAX
);
668 const char *endptr
= &f
;
669 unsigned long res
= 999;
672 err
= qemu_strtoul(str
, &endptr
, 0, &res
);
674 g_assert_cmpint(err
, ==, 0);
675 g_assert_cmpint(res
, ==, ULONG_MAX
);
676 g_assert(endptr
== str
+ strlen(str
));
679 static void test_qemu_strtoul_overflow(void)
681 const char *str
= "99999999999999999999999999999999999999999999";
683 const char *endptr
= &f
;
684 unsigned long res
= 999;
687 err
= qemu_strtoul(str
, &endptr
, 0, &res
);
689 g_assert_cmpint(err
, ==, -ERANGE
);
690 g_assert_cmpint(res
, ==, ULONG_MAX
);
691 g_assert(endptr
== str
+ strlen(str
));
694 static void test_qemu_strtoul_underflow(void)
696 const char *str
= "-99999999999999999999999999999999999999999999";
698 const char *endptr
= &f
;
699 unsigned long res
= 999;
702 err
= qemu_strtoul(str
, &endptr
, 0, &res
);
704 g_assert_cmpint(err
, ==, -ERANGE
);
705 g_assert_cmpint(res
, ==, -1ul);
706 g_assert(endptr
== str
+ strlen(str
));
709 static void test_qemu_strtoul_negative(void)
711 const char *str
= " \t -321";
713 const char *endptr
= &f
;
714 unsigned long res
= 999;
717 err
= qemu_strtoul(str
, &endptr
, 0, &res
);
719 g_assert_cmpint(err
, ==, 0);
720 g_assert_cmpint(res
, ==, -321ul);
721 g_assert(endptr
== str
+ strlen(str
));
724 static void test_qemu_strtoul_full_correct(void)
726 const char *str
= "123";
727 unsigned long res
= 999;
730 err
= qemu_strtoul(str
, NULL
, 0, &res
);
732 g_assert_cmpint(err
, ==, 0);
733 g_assert_cmpint(res
, ==, 123);
736 static void test_qemu_strtoul_full_null(void)
738 unsigned long res
= 999;
741 err
= qemu_strtoul(NULL
, NULL
, 0, &res
);
743 g_assert_cmpint(err
, ==, -EINVAL
);
746 static void test_qemu_strtoul_full_empty(void)
748 const char *str
= "";
749 unsigned long res
= 999;
752 err
= qemu_strtoul(str
, NULL
, 0, &res
);
754 g_assert_cmpint(err
, ==, -EINVAL
);
756 static void test_qemu_strtoul_full_negative(void)
758 const char *str
= " \t -321";
759 unsigned long res
= 999;
762 err
= qemu_strtoul(str
, NULL
, 0, &res
);
763 g_assert_cmpint(err
, ==, 0);
764 g_assert_cmpint(res
, ==, -321ul);
767 static void test_qemu_strtoul_full_trailing(void)
769 const char *str
= "123xxx";
773 err
= qemu_strtoul(str
, NULL
, 0, &res
);
775 g_assert_cmpint(err
, ==, -EINVAL
);
778 static void test_qemu_strtoul_full_max(void)
780 const char *str
= g_strdup_printf("%lu", ULONG_MAX
);
781 unsigned long res
= 999;
784 err
= qemu_strtoul(str
, NULL
, 0, &res
);
786 g_assert_cmpint(err
, ==, 0);
787 g_assert_cmpint(res
, ==, ULONG_MAX
);
790 static void test_qemu_strtoll_correct(void)
792 const char *str
= "12345 foo";
794 const char *endptr
= &f
;
798 err
= qemu_strtoll(str
, &endptr
, 0, &res
);
800 g_assert_cmpint(err
, ==, 0);
801 g_assert_cmpint(res
, ==, 12345);
802 g_assert(endptr
== str
+ 5);
805 static void test_qemu_strtoll_null(void)
808 const char *endptr
= &f
;
812 err
= qemu_strtoll(NULL
, &endptr
, 0, &res
);
814 g_assert_cmpint(err
, ==, -EINVAL
);
815 g_assert(endptr
== NULL
);
818 static void test_qemu_strtoll_empty(void)
820 const char *str
= "";
822 const char *endptr
= &f
;
826 err
= qemu_strtoll(str
, &endptr
, 0, &res
);
828 g_assert_cmpint(err
, ==, -EINVAL
);
831 static void test_qemu_strtoll_whitespace(void)
833 const char *str
= " \t ";
835 const char *endptr
= &f
;
839 err
= qemu_strtoll(str
, &endptr
, 0, &res
);
841 g_assert_cmpint(err
, ==, -EINVAL
);
844 static void test_qemu_strtoll_invalid(void)
846 const char *str
= " xxxx \t abc";
848 const char *endptr
= &f
;
852 err
= qemu_strtoll(str
, &endptr
, 0, &res
);
854 g_assert_cmpint(err
, ==, -EINVAL
);
857 static void test_qemu_strtoll_trailing(void)
859 const char *str
= "123xxx";
861 const char *endptr
= &f
;
865 err
= qemu_strtoll(str
, &endptr
, 0, &res
);
867 g_assert_cmpint(err
, ==, 0);
868 g_assert_cmpint(res
, ==, 123);
869 g_assert(endptr
== str
+ 3);
872 static void test_qemu_strtoll_octal(void)
874 const char *str
= "0123";
876 const char *endptr
= &f
;
880 err
= qemu_strtoll(str
, &endptr
, 8, &res
);
882 g_assert_cmpint(err
, ==, 0);
883 g_assert_cmpint(res
, ==, 0123);
884 g_assert(endptr
== str
+ strlen(str
));
888 err
= qemu_strtoll(str
, &endptr
, 0, &res
);
890 g_assert_cmpint(err
, ==, 0);
891 g_assert_cmpint(res
, ==, 0123);
892 g_assert(endptr
== str
+ strlen(str
));
895 static void test_qemu_strtoll_decimal(void)
897 const char *str
= "0123";
899 const char *endptr
= &f
;
903 err
= qemu_strtoll(str
, &endptr
, 10, &res
);
905 g_assert_cmpint(err
, ==, 0);
906 g_assert_cmpint(res
, ==, 123);
907 g_assert(endptr
== str
+ strlen(str
));
912 err
= qemu_strtoll(str
, &endptr
, 0, &res
);
914 g_assert_cmpint(err
, ==, 0);
915 g_assert_cmpint(res
, ==, 123);
916 g_assert(endptr
== str
+ strlen(str
));
919 static void test_qemu_strtoll_hex(void)
921 const char *str
= "0123";
923 const char *endptr
= &f
;
927 err
= qemu_strtoll(str
, &endptr
, 16, &res
);
929 g_assert_cmpint(err
, ==, 0);
930 g_assert_cmpint(res
, ==, 0x123);
931 g_assert(endptr
== str
+ strlen(str
));
936 err
= qemu_strtoll(str
, &endptr
, 0, &res
);
938 g_assert_cmpint(err
, ==, 0);
939 g_assert_cmpint(res
, ==, 0x123);
940 g_assert(endptr
== str
+ strlen(str
));
943 static void test_qemu_strtoll_max(void)
945 const char *str
= g_strdup_printf("%lld", LLONG_MAX
);
947 const char *endptr
= &f
;
951 err
= qemu_strtoll(str
, &endptr
, 0, &res
);
953 g_assert_cmpint(err
, ==, 0);
954 g_assert_cmpint(res
, ==, LLONG_MAX
);
955 g_assert(endptr
== str
+ strlen(str
));
958 static void test_qemu_strtoll_overflow(void)
960 const char *str
= "99999999999999999999999999999999999999999999";
962 const char *endptr
= &f
;
966 err
= qemu_strtoll(str
, &endptr
, 0, &res
);
968 g_assert_cmpint(err
, ==, -ERANGE
);
969 g_assert_cmpint(res
, ==, LLONG_MAX
);
970 g_assert(endptr
== str
+ strlen(str
));
973 static void test_qemu_strtoll_underflow(void)
975 const char *str
= "-99999999999999999999999999999999999999999999";
977 const char *endptr
= &f
;
981 err
= qemu_strtoll(str
, &endptr
, 0, &res
);
983 g_assert_cmpint(err
, ==, -ERANGE
);
984 g_assert_cmpint(res
, ==, LLONG_MIN
);
985 g_assert(endptr
== str
+ strlen(str
));
988 static void test_qemu_strtoll_negative(void)
990 const char *str
= " \t -321";
992 const char *endptr
= &f
;
996 err
= qemu_strtoll(str
, &endptr
, 0, &res
);
998 g_assert_cmpint(err
, ==, 0);
999 g_assert_cmpint(res
, ==, -321);
1000 g_assert(endptr
== str
+ strlen(str
));
1003 static void test_qemu_strtoll_full_correct(void)
1005 const char *str
= "123";
1009 err
= qemu_strtoll(str
, NULL
, 0, &res
);
1011 g_assert_cmpint(err
, ==, 0);
1012 g_assert_cmpint(res
, ==, 123);
1015 static void test_qemu_strtoll_full_null(void)
1020 err
= qemu_strtoll(NULL
, NULL
, 0, &res
);
1022 g_assert_cmpint(err
, ==, -EINVAL
);
1025 static void test_qemu_strtoll_full_empty(void)
1027 const char *str
= "";
1031 err
= qemu_strtoll(str
, NULL
, 0, &res
);
1033 g_assert_cmpint(err
, ==, -EINVAL
);
1036 static void test_qemu_strtoll_full_negative(void)
1038 const char *str
= " \t -321";
1042 err
= qemu_strtoll(str
, NULL
, 0, &res
);
1044 g_assert_cmpint(err
, ==, 0);
1045 g_assert_cmpint(res
, ==, -321);
1048 static void test_qemu_strtoll_full_trailing(void)
1050 const char *str
= "123xxx";
1054 err
= qemu_strtoll(str
, NULL
, 0, &res
);
1056 g_assert_cmpint(err
, ==, -EINVAL
);
1059 static void test_qemu_strtoll_full_max(void)
1062 const char *str
= g_strdup_printf("%lld", LLONG_MAX
);
1066 err
= qemu_strtoll(str
, NULL
, 0, &res
);
1068 g_assert_cmpint(err
, ==, 0);
1069 g_assert_cmpint(res
, ==, LLONG_MAX
);
1072 static void test_qemu_strtoull_correct(void)
1074 const char *str
= "12345 foo";
1076 const char *endptr
= &f
;
1080 err
= qemu_strtoull(str
, &endptr
, 0, &res
);
1082 g_assert_cmpint(err
, ==, 0);
1083 g_assert_cmpint(res
, ==, 12345);
1084 g_assert(endptr
== str
+ 5);
1087 static void test_qemu_strtoull_null(void)
1090 const char *endptr
= &f
;
1094 err
= qemu_strtoull(NULL
, &endptr
, 0, &res
);
1096 g_assert_cmpint(err
, ==, -EINVAL
);
1097 g_assert(endptr
== NULL
);
1100 static void test_qemu_strtoull_empty(void)
1102 const char *str
= "";
1104 const char *endptr
= &f
;
1108 err
= qemu_strtoull(str
, &endptr
, 0, &res
);
1110 g_assert_cmpint(err
, ==, -EINVAL
);
1113 static void test_qemu_strtoull_whitespace(void)
1115 const char *str
= " \t ";
1117 const char *endptr
= &f
;
1121 err
= qemu_strtoull(str
, &endptr
, 0, &res
);
1123 g_assert_cmpint(err
, ==, -EINVAL
);
1126 static void test_qemu_strtoull_invalid(void)
1128 const char *str
= " xxxx \t abc";
1130 const char *endptr
= &f
;
1134 err
= qemu_strtoull(str
, &endptr
, 0, &res
);
1136 g_assert_cmpint(err
, ==, -EINVAL
);
1139 static void test_qemu_strtoull_trailing(void)
1141 const char *str
= "123xxx";
1143 const char *endptr
= &f
;
1147 err
= qemu_strtoull(str
, &endptr
, 0, &res
);
1149 g_assert_cmpint(err
, ==, 0);
1150 g_assert_cmpint(res
, ==, 123);
1151 g_assert(endptr
== str
+ 3);
1154 static void test_qemu_strtoull_octal(void)
1156 const char *str
= "0123";
1158 const char *endptr
= &f
;
1162 err
= qemu_strtoull(str
, &endptr
, 8, &res
);
1164 g_assert_cmpint(err
, ==, 0);
1165 g_assert_cmpint(res
, ==, 0123);
1166 g_assert(endptr
== str
+ strlen(str
));
1170 err
= qemu_strtoull(str
, &endptr
, 0, &res
);
1172 g_assert_cmpint(err
, ==, 0);
1173 g_assert_cmpint(res
, ==, 0123);
1174 g_assert(endptr
== str
+ strlen(str
));
1177 static void test_qemu_strtoull_decimal(void)
1179 const char *str
= "0123";
1181 const char *endptr
= &f
;
1185 err
= qemu_strtoull(str
, &endptr
, 10, &res
);
1187 g_assert_cmpint(err
, ==, 0);
1188 g_assert_cmpint(res
, ==, 123);
1189 g_assert(endptr
== str
+ strlen(str
));
1194 err
= qemu_strtoull(str
, &endptr
, 0, &res
);
1196 g_assert_cmpint(err
, ==, 0);
1197 g_assert_cmpint(res
, ==, 123);
1198 g_assert(endptr
== str
+ strlen(str
));
1201 static void test_qemu_strtoull_hex(void)
1203 const char *str
= "0123";
1205 const char *endptr
= &f
;
1209 err
= qemu_strtoull(str
, &endptr
, 16, &res
);
1211 g_assert_cmpint(err
, ==, 0);
1212 g_assert_cmpint(res
, ==, 0x123);
1213 g_assert(endptr
== str
+ strlen(str
));
1218 err
= qemu_strtoull(str
, &endptr
, 0, &res
);
1220 g_assert_cmpint(err
, ==, 0);
1221 g_assert_cmpint(res
, ==, 0x123);
1222 g_assert(endptr
== str
+ strlen(str
));
1225 static void test_qemu_strtoull_max(void)
1227 const char *str
= g_strdup_printf("%llu", ULLONG_MAX
);
1229 const char *endptr
= &f
;
1233 err
= qemu_strtoull(str
, &endptr
, 0, &res
);
1235 g_assert_cmpint(err
, ==, 0);
1236 g_assert_cmpint(res
, ==, ULLONG_MAX
);
1237 g_assert(endptr
== str
+ strlen(str
));
1240 static void test_qemu_strtoull_overflow(void)
1242 const char *str
= "99999999999999999999999999999999999999999999";
1244 const char *endptr
= &f
;
1248 err
= qemu_strtoull(str
, &endptr
, 0, &res
);
1250 g_assert_cmpint(err
, ==, -ERANGE
);
1251 g_assert_cmpint(res
, ==, ULLONG_MAX
);
1252 g_assert(endptr
== str
+ strlen(str
));
1255 static void test_qemu_strtoull_underflow(void)
1257 const char *str
= "-99999999999999999999999999999999999999999999";
1259 const char *endptr
= &f
;
1263 err
= qemu_strtoull(str
, &endptr
, 0, &res
);
1265 g_assert_cmpint(err
, ==, -ERANGE
);
1266 g_assert_cmpint(res
, ==, -1);
1267 g_assert(endptr
== str
+ strlen(str
));
1270 static void test_qemu_strtoull_negative(void)
1272 const char *str
= " \t -321";
1274 const char *endptr
= &f
;
1278 err
= qemu_strtoull(str
, &endptr
, 0, &res
);
1280 g_assert_cmpint(err
, ==, 0);
1281 g_assert_cmpint(res
, ==, -321);
1282 g_assert(endptr
== str
+ strlen(str
));
1285 static void test_qemu_strtoull_full_correct(void)
1287 const char *str
= "18446744073709551614";
1291 err
= qemu_strtoull(str
, NULL
, 0, &res
);
1293 g_assert_cmpint(err
, ==, 0);
1294 g_assert_cmpint(res
, ==, 18446744073709551614LLU);
1297 static void test_qemu_strtoull_full_null(void)
1302 err
= qemu_strtoull(NULL
, NULL
, 0, &res
);
1304 g_assert_cmpint(err
, ==, -EINVAL
);
1307 static void test_qemu_strtoull_full_empty(void)
1309 const char *str
= "";
1313 err
= qemu_strtoull(str
, NULL
, 0, &res
);
1315 g_assert_cmpint(err
, ==, -EINVAL
);
1318 static void test_qemu_strtoull_full_negative(void)
1320 const char *str
= " \t -321";
1324 err
= qemu_strtoull(str
, NULL
, 0, &res
);
1326 g_assert_cmpint(err
, ==, 0);
1327 g_assert_cmpint(res
, ==, 18446744073709551295LLU);
1330 static void test_qemu_strtoull_full_trailing(void)
1332 const char *str
= "18446744073709551614xxxxxx";
1336 err
= qemu_strtoull(str
, NULL
, 0, &res
);
1338 g_assert_cmpint(err
, ==, -EINVAL
);
1341 static void test_qemu_strtoull_full_max(void)
1343 const char *str
= g_strdup_printf("%lld", ULLONG_MAX
);
1347 err
= qemu_strtoull(str
, NULL
, 0, &res
);
1349 g_assert_cmpint(err
, ==, 0);
1350 g_assert_cmpint(res
, ==, ULLONG_MAX
);
1353 static void test_qemu_strtosz_simple(void)
1355 const char *str
= "12345M";
1356 char *endptr
= NULL
;
1359 res
= qemu_strtosz(str
, &endptr
);
1360 g_assert_cmpint(res
, ==, 12345 * M_BYTE
);
1361 g_assert(endptr
== str
+ 6);
1363 res
= qemu_strtosz(str
, NULL
);
1364 g_assert_cmpint(res
, ==, 12345 * M_BYTE
);
1367 static void test_qemu_strtosz_units(void)
1369 const char *none
= "1";
1370 const char *b
= "1B";
1371 const char *k
= "1K";
1372 const char *m
= "1M";
1373 const char *g
= "1G";
1374 const char *t
= "1T";
1375 const char *p
= "1P";
1376 const char *e
= "1E";
1380 res
= qemu_strtosz(none
, NULL
);
1381 g_assert_cmpint(res
, ==, M_BYTE
);
1383 res
= qemu_strtosz(b
, NULL
);
1384 g_assert_cmpint(res
, ==, 1);
1386 res
= qemu_strtosz(k
, NULL
);
1387 g_assert_cmpint(res
, ==, K_BYTE
);
1389 res
= qemu_strtosz(m
, NULL
);
1390 g_assert_cmpint(res
, ==, M_BYTE
);
1392 res
= qemu_strtosz(g
, NULL
);
1393 g_assert_cmpint(res
, ==, G_BYTE
);
1395 res
= qemu_strtosz(t
, NULL
);
1396 g_assert_cmpint(res
, ==, T_BYTE
);
1398 res
= qemu_strtosz(p
, NULL
);
1399 g_assert_cmpint(res
, ==, P_BYTE
);
1401 res
= qemu_strtosz(e
, NULL
);
1402 g_assert_cmpint(res
, ==, E_BYTE
);
1405 static void test_qemu_strtosz_float(void)
1407 const char *str
= "12.345M";
1410 res
= qemu_strtosz(str
, NULL
);
1411 g_assert_cmpint(res
, ==, 12.345 * M_BYTE
);
1414 static void test_qemu_strtosz_erange(void)
1416 const char *str
= "10E";
1419 res
= qemu_strtosz(str
, NULL
);
1420 g_assert_cmpint(res
, ==, -ERANGE
);
1423 static void test_qemu_strtosz_suffix_unit(void)
1425 const char *str
= "12345";
1428 res
= qemu_strtosz_suffix_unit(str
, NULL
,
1429 QEMU_STRTOSZ_DEFSUFFIX_KB
, 1000);
1430 g_assert_cmpint(res
, ==, 12345000);
1433 int main(int argc
, char **argv
)
1435 g_test_init(&argc
, &argv
, NULL
);
1437 g_test_add_func("/cutils/parse_uint/null", test_parse_uint_null
);
1438 g_test_add_func("/cutils/parse_uint/empty", test_parse_uint_empty
);
1439 g_test_add_func("/cutils/parse_uint/whitespace",
1440 test_parse_uint_whitespace
);
1441 g_test_add_func("/cutils/parse_uint/invalid", test_parse_uint_invalid
);
1442 g_test_add_func("/cutils/parse_uint/trailing", test_parse_uint_trailing
);
1443 g_test_add_func("/cutils/parse_uint/correct", test_parse_uint_correct
);
1444 g_test_add_func("/cutils/parse_uint/octal", test_parse_uint_octal
);
1445 g_test_add_func("/cutils/parse_uint/decimal", test_parse_uint_decimal
);
1446 g_test_add_func("/cutils/parse_uint/llong_max", test_parse_uint_llong_max
);
1447 g_test_add_func("/cutils/parse_uint/overflow", test_parse_uint_overflow
);
1448 g_test_add_func("/cutils/parse_uint/negative", test_parse_uint_negative
);
1449 g_test_add_func("/cutils/parse_uint_full/trailing",
1450 test_parse_uint_full_trailing
);
1451 g_test_add_func("/cutils/parse_uint_full/correct",
1452 test_parse_uint_full_correct
);
1454 /* qemu_strtol() tests */
1455 g_test_add_func("/cutils/qemu_strtol/correct", test_qemu_strtol_correct
);
1456 g_test_add_func("/cutils/qemu_strtol/null", test_qemu_strtol_null
);
1457 g_test_add_func("/cutils/qemu_strtol/empty", test_qemu_strtol_empty
);
1458 g_test_add_func("/cutils/qemu_strtol/whitespace",
1459 test_qemu_strtol_whitespace
);
1460 g_test_add_func("/cutils/qemu_strtol/invalid", test_qemu_strtol_invalid
);
1461 g_test_add_func("/cutils/qemu_strtol/trailing", test_qemu_strtol_trailing
);
1462 g_test_add_func("/cutils/qemu_strtol/octal", test_qemu_strtol_octal
);
1463 g_test_add_func("/cutils/qemu_strtol/decimal", test_qemu_strtol_decimal
);
1464 g_test_add_func("/cutils/qemu_strtol/hex", test_qemu_strtol_hex
);
1465 g_test_add_func("/cutils/qemu_strtol/max", test_qemu_strtol_max
);
1466 g_test_add_func("/cutils/qemu_strtol/overflow", test_qemu_strtol_overflow
);
1467 g_test_add_func("/cutils/qemu_strtol/underflow",
1468 test_qemu_strtol_underflow
);
1469 g_test_add_func("/cutils/qemu_strtol/negative", test_qemu_strtol_negative
);
1470 g_test_add_func("/cutils/qemu_strtol_full/correct",
1471 test_qemu_strtol_full_correct
);
1472 g_test_add_func("/cutils/qemu_strtol_full/null",
1473 test_qemu_strtol_full_null
);
1474 g_test_add_func("/cutils/qemu_strtol_full/empty",
1475 test_qemu_strtol_full_empty
);
1476 g_test_add_func("/cutils/qemu_strtol_full/negative",
1477 test_qemu_strtol_full_negative
);
1478 g_test_add_func("/cutils/qemu_strtol_full/trailing",
1479 test_qemu_strtol_full_trailing
);
1480 g_test_add_func("/cutils/qemu_strtol_full/max",
1481 test_qemu_strtol_full_max
);
1483 /* qemu_strtoul() tests */
1484 g_test_add_func("/cutils/qemu_strtoul/correct", test_qemu_strtoul_correct
);
1485 g_test_add_func("/cutils/qemu_strtoul/null", test_qemu_strtoul_null
);
1486 g_test_add_func("/cutils/qemu_strtoul/empty", test_qemu_strtoul_empty
);
1487 g_test_add_func("/cutils/qemu_strtoul/whitespace",
1488 test_qemu_strtoul_whitespace
);
1489 g_test_add_func("/cutils/qemu_strtoul/invalid", test_qemu_strtoul_invalid
);
1490 g_test_add_func("/cutils/qemu_strtoul/trailing",
1491 test_qemu_strtoul_trailing
);
1492 g_test_add_func("/cutils/qemu_strtoul/octal", test_qemu_strtoul_octal
);
1493 g_test_add_func("/cutils/qemu_strtoul/decimal", test_qemu_strtoul_decimal
);
1494 g_test_add_func("/cutils/qemu_strtoul/hex", test_qemu_strtoul_hex
);
1495 g_test_add_func("/cutils/qemu_strtoul/max", test_qemu_strtoul_max
);
1496 g_test_add_func("/cutils/qemu_strtoul/overflow",
1497 test_qemu_strtoul_overflow
);
1498 g_test_add_func("/cutils/qemu_strtoul/underflow",
1499 test_qemu_strtoul_underflow
);
1500 g_test_add_func("/cutils/qemu_strtoul/negative",
1501 test_qemu_strtoul_negative
);
1502 g_test_add_func("/cutils/qemu_strtoul_full/correct",
1503 test_qemu_strtoul_full_correct
);
1504 g_test_add_func("/cutils/qemu_strtoul_full/null",
1505 test_qemu_strtoul_full_null
);
1506 g_test_add_func("/cutils/qemu_strtoul_full/empty",
1507 test_qemu_strtoul_full_empty
);
1508 g_test_add_func("/cutils/qemu_strtoul_full/negative",
1509 test_qemu_strtoul_full_negative
);
1510 g_test_add_func("/cutils/qemu_strtoul_full/trailing",
1511 test_qemu_strtoul_full_trailing
);
1512 g_test_add_func("/cutils/qemu_strtoul_full/max",
1513 test_qemu_strtoul_full_max
);
1515 /* qemu_strtoll() tests */
1516 g_test_add_func("/cutils/qemu_strtoll/correct", test_qemu_strtoll_correct
);
1517 g_test_add_func("/cutils/qemu_strtoll/null", test_qemu_strtoll_null
);
1518 g_test_add_func("/cutils/qemu_strtoll/empty", test_qemu_strtoll_empty
);
1519 g_test_add_func("/cutils/qemu_strtoll/whitespace",
1520 test_qemu_strtoll_whitespace
);
1521 g_test_add_func("/cutils/qemu_strtoll/invalid", test_qemu_strtoll_invalid
);
1522 g_test_add_func("/cutils/qemu_strtoll/trailing",
1523 test_qemu_strtoll_trailing
);
1524 g_test_add_func("/cutils/qemu_strtoll/octal", test_qemu_strtoll_octal
);
1525 g_test_add_func("/cutils/qemu_strtoll/decimal", test_qemu_strtoll_decimal
);
1526 g_test_add_func("/cutils/qemu_strtoll/hex", test_qemu_strtoll_hex
);
1527 g_test_add_func("/cutils/qemu_strtoll/max", test_qemu_strtoll_max
);
1528 g_test_add_func("/cutils/qemu_strtoll/overflow",
1529 test_qemu_strtoll_overflow
);
1530 g_test_add_func("/cutils/qemu_strtoll/underflow",
1531 test_qemu_strtoll_underflow
);
1532 g_test_add_func("/cutils/qemu_strtoll/negative",
1533 test_qemu_strtoll_negative
);
1534 g_test_add_func("/cutils/qemu_strtoll_full/correct",
1535 test_qemu_strtoll_full_correct
);
1536 g_test_add_func("/cutils/qemu_strtoll_full/null",
1537 test_qemu_strtoll_full_null
);
1538 g_test_add_func("/cutils/qemu_strtoll_full/empty",
1539 test_qemu_strtoll_full_empty
);
1540 g_test_add_func("/cutils/qemu_strtoll_full/negative",
1541 test_qemu_strtoll_full_negative
);
1542 g_test_add_func("/cutils/qemu_strtoll_full/trailing",
1543 test_qemu_strtoll_full_trailing
);
1544 g_test_add_func("/cutils/qemu_strtoll_full/max",
1545 test_qemu_strtoll_full_max
);
1547 /* qemu_strtoull() tests */
1548 g_test_add_func("/cutils/qemu_strtoull/correct",
1549 test_qemu_strtoull_correct
);
1550 g_test_add_func("/cutils/qemu_strtoull/null",
1551 test_qemu_strtoull_null
);
1552 g_test_add_func("/cutils/qemu_strtoull/empty", test_qemu_strtoull_empty
);
1553 g_test_add_func("/cutils/qemu_strtoull/whitespace",
1554 test_qemu_strtoull_whitespace
);
1555 g_test_add_func("/cutils/qemu_strtoull/invalid",
1556 test_qemu_strtoull_invalid
);
1557 g_test_add_func("/cutils/qemu_strtoull/trailing",
1558 test_qemu_strtoull_trailing
);
1559 g_test_add_func("/cutils/qemu_strtoull/octal", test_qemu_strtoull_octal
);
1560 g_test_add_func("/cutils/qemu_strtoull/decimal",
1561 test_qemu_strtoull_decimal
);
1562 g_test_add_func("/cutils/qemu_strtoull/hex", test_qemu_strtoull_hex
);
1563 g_test_add_func("/cutils/qemu_strtoull/max", test_qemu_strtoull_max
);
1564 g_test_add_func("/cutils/qemu_strtoull/overflow",
1565 test_qemu_strtoull_overflow
);
1566 g_test_add_func("/cutils/qemu_strtoull/underflow",
1567 test_qemu_strtoull_underflow
);
1568 g_test_add_func("/cutils/qemu_strtoull/negative",
1569 test_qemu_strtoull_negative
);
1570 g_test_add_func("/cutils/qemu_strtoull_full/correct",
1571 test_qemu_strtoull_full_correct
);
1572 g_test_add_func("/cutils/qemu_strtoull_full/null",
1573 test_qemu_strtoull_full_null
);
1574 g_test_add_func("/cutils/qemu_strtoull_full/empty",
1575 test_qemu_strtoull_full_empty
);
1576 g_test_add_func("/cutils/qemu_strtoull_full/negative",
1577 test_qemu_strtoull_full_negative
);
1578 g_test_add_func("/cutils/qemu_strtoull_full/trailing",
1579 test_qemu_strtoull_full_trailing
);
1580 g_test_add_func("/cutils/qemu_strtoull_full/max",
1581 test_qemu_strtoull_full_max
);
1583 g_test_add_func("/cutils/strtosz/simple",
1584 test_qemu_strtosz_simple
);
1585 g_test_add_func("/cutils/strtosz/units",
1586 test_qemu_strtosz_units
);
1587 g_test_add_func("/cutils/strtosz/float",
1588 test_qemu_strtosz_float
);
1589 g_test_add_func("/cutils/strtosz/erange",
1590 test_qemu_strtosz_erange
);
1591 g_test_add_func("/cutils/strtosz/suffix-unit",
1592 test_qemu_strtosz_suffix_unit
);
1594 return g_test_run();