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
32 #include "qemu-common.h"
35 static void test_parse_uint_null(void)
37 unsigned long long i
= 999;
42 r
= parse_uint(NULL
, &i
, &endptr
, 0);
44 g_assert_cmpint(r
, ==, -EINVAL
);
45 g_assert_cmpint(i
, ==, 0);
46 g_assert(endptr
== NULL
);
49 static void test_parse_uint_empty(void)
51 unsigned long long i
= 999;
57 r
= parse_uint(str
, &i
, &endptr
, 0);
59 g_assert_cmpint(r
, ==, -EINVAL
);
60 g_assert_cmpint(i
, ==, 0);
61 g_assert(endptr
== str
);
64 static void test_parse_uint_whitespace(void)
66 unsigned long long i
= 999;
69 const char *str
= " \t ";
72 r
= parse_uint(str
, &i
, &endptr
, 0);
74 g_assert_cmpint(r
, ==, -EINVAL
);
75 g_assert_cmpint(i
, ==, 0);
76 g_assert(endptr
== str
);
80 static void test_parse_uint_invalid(void)
82 unsigned long long i
= 999;
85 const char *str
= " \t xxx";
88 r
= parse_uint(str
, &i
, &endptr
, 0);
90 g_assert_cmpint(r
, ==, -EINVAL
);
91 g_assert_cmpint(i
, ==, 0);
92 g_assert(endptr
== str
);
96 static void test_parse_uint_trailing(void)
98 unsigned long long i
= 999;
101 const char *str
= "123xxx";
104 r
= parse_uint(str
, &i
, &endptr
, 0);
106 g_assert_cmpint(r
, ==, 0);
107 g_assert_cmpint(i
, ==, 123);
108 g_assert(endptr
== str
+ 3);
111 static void test_parse_uint_correct(void)
113 unsigned long long i
= 999;
116 const char *str
= "123";
119 r
= parse_uint(str
, &i
, &endptr
, 0);
121 g_assert_cmpint(r
, ==, 0);
122 g_assert_cmpint(i
, ==, 123);
123 g_assert(endptr
== str
+ strlen(str
));
126 static void test_parse_uint_octal(void)
128 unsigned long long i
= 999;
131 const char *str
= "0123";
134 r
= parse_uint(str
, &i
, &endptr
, 0);
136 g_assert_cmpint(r
, ==, 0);
137 g_assert_cmpint(i
, ==, 0123);
138 g_assert(endptr
== str
+ strlen(str
));
141 static void test_parse_uint_decimal(void)
143 unsigned long long i
= 999;
146 const char *str
= "0123";
149 r
= parse_uint(str
, &i
, &endptr
, 10);
151 g_assert_cmpint(r
, ==, 0);
152 g_assert_cmpint(i
, ==, 123);
153 g_assert(endptr
== str
+ strlen(str
));
157 static void test_parse_uint_llong_max(void)
159 unsigned long long i
= 999;
162 char *str
= g_strdup_printf("%llu", (unsigned long long)LLONG_MAX
+ 1);
165 r
= parse_uint(str
, &i
, &endptr
, 0);
167 g_assert_cmpint(r
, ==, 0);
168 g_assert_cmpint(i
, ==, (unsigned long long)LLONG_MAX
+ 1);
169 g_assert(endptr
== str
+ strlen(str
));
174 static void test_parse_uint_overflow(void)
176 unsigned long long i
= 999;
179 const char *str
= "99999999999999999999999999999999999999";
182 r
= parse_uint(str
, &i
, &endptr
, 0);
184 g_assert_cmpint(r
, ==, -ERANGE
);
185 g_assert_cmpint(i
, ==, ULLONG_MAX
);
186 g_assert(endptr
== str
+ strlen(str
));
189 static void test_parse_uint_negative(void)
191 unsigned long long i
= 999;
194 const char *str
= " \t -321";
197 r
= parse_uint(str
, &i
, &endptr
, 0);
199 g_assert_cmpint(r
, ==, -ERANGE
);
200 g_assert_cmpint(i
, ==, 0);
201 g_assert(endptr
== str
+ strlen(str
));
205 static void test_parse_uint_full_trailing(void)
207 unsigned long long i
= 999;
208 const char *str
= "123xxx";
211 r
= parse_uint_full(str
, &i
, 0);
213 g_assert_cmpint(r
, ==, -EINVAL
);
214 g_assert_cmpint(i
, ==, 0);
217 static void test_parse_uint_full_correct(void)
219 unsigned long long i
= 999;
220 const char *str
= "123";
223 r
= parse_uint_full(str
, &i
, 0);
225 g_assert_cmpint(r
, ==, 0);
226 g_assert_cmpint(i
, ==, 123);
229 static void test_qemu_strtol_correct(void)
231 const char *str
= "12345 foo";
233 const char *endptr
= &f
;
237 err
= qemu_strtol(str
, &endptr
, 0, &res
);
239 g_assert_cmpint(err
, ==, 0);
240 g_assert_cmpint(res
, ==, 12345);
241 g_assert(endptr
== str
+ 5);
244 static void test_qemu_strtol_null(void)
247 const char *endptr
= &f
;
251 err
= qemu_strtol(NULL
, &endptr
, 0, &res
);
253 g_assert_cmpint(err
, ==, -EINVAL
);
254 g_assert(endptr
== NULL
);
257 static void test_qemu_strtol_empty(void)
259 const char *str
= "";
261 const char *endptr
= &f
;
265 err
= qemu_strtol(str
, &endptr
, 0, &res
);
267 g_assert_cmpint(err
, ==, -EINVAL
);
270 static void test_qemu_strtol_whitespace(void)
272 const char *str
= " \t ";
274 const char *endptr
= &f
;
278 err
= qemu_strtol(str
, &endptr
, 0, &res
);
280 g_assert_cmpint(err
, ==, -EINVAL
);
283 static void test_qemu_strtol_invalid(void)
285 const char *str
= " xxxx \t abc";
287 const char *endptr
= &f
;
291 err
= qemu_strtol(str
, &endptr
, 0, &res
);
293 g_assert_cmpint(err
, ==, -EINVAL
);
296 static void test_qemu_strtol_trailing(void)
298 const char *str
= "123xxx";
300 const char *endptr
= &f
;
304 err
= qemu_strtol(str
, &endptr
, 0, &res
);
306 g_assert_cmpint(err
, ==, 0);
307 g_assert_cmpint(res
, ==, 123);
308 g_assert(endptr
== str
+ 3);
311 static void test_qemu_strtol_octal(void)
313 const char *str
= "0123";
315 const char *endptr
= &f
;
319 err
= qemu_strtol(str
, &endptr
, 8, &res
);
321 g_assert_cmpint(err
, ==, 0);
322 g_assert_cmpint(res
, ==, 0123);
323 g_assert(endptr
== str
+ strlen(str
));
327 err
= qemu_strtol(str
, &endptr
, 0, &res
);
329 g_assert_cmpint(err
, ==, 0);
330 g_assert_cmpint(res
, ==, 0123);
331 g_assert(endptr
== str
+ strlen(str
));
334 static void test_qemu_strtol_decimal(void)
336 const char *str
= "0123";
338 const char *endptr
= &f
;
342 err
= qemu_strtol(str
, &endptr
, 10, &res
);
344 g_assert_cmpint(err
, ==, 0);
345 g_assert_cmpint(res
, ==, 123);
346 g_assert(endptr
== str
+ strlen(str
));
351 err
= qemu_strtol(str
, &endptr
, 0, &res
);
353 g_assert_cmpint(err
, ==, 0);
354 g_assert_cmpint(res
, ==, 123);
355 g_assert(endptr
== str
+ strlen(str
));
358 static void test_qemu_strtol_hex(void)
360 const char *str
= "0123";
362 const char *endptr
= &f
;
366 err
= qemu_strtol(str
, &endptr
, 16, &res
);
368 g_assert_cmpint(err
, ==, 0);
369 g_assert_cmpint(res
, ==, 0x123);
370 g_assert(endptr
== str
+ strlen(str
));
375 err
= qemu_strtol(str
, &endptr
, 0, &res
);
377 g_assert_cmpint(err
, ==, 0);
378 g_assert_cmpint(res
, ==, 0x123);
379 g_assert(endptr
== str
+ strlen(str
));
382 static void test_qemu_strtol_max(void)
384 const char *str
= g_strdup_printf("%ld", LONG_MAX
);
386 const char *endptr
= &f
;
390 err
= qemu_strtol(str
, &endptr
, 0, &res
);
392 g_assert_cmpint(err
, ==, 0);
393 g_assert_cmpint(res
, ==, LONG_MAX
);
394 g_assert(endptr
== str
+ strlen(str
));
397 static void test_qemu_strtol_overflow(void)
399 const char *str
= "99999999999999999999999999999999999999999999";
401 const char *endptr
= &f
;
405 err
= qemu_strtol(str
, &endptr
, 0, &res
);
407 g_assert_cmpint(err
, ==, -ERANGE
);
408 g_assert_cmpint(res
, ==, LONG_MAX
);
409 g_assert(endptr
== str
+ strlen(str
));
412 static void test_qemu_strtol_underflow(void)
414 const char *str
= "-99999999999999999999999999999999999999999999";
416 const char *endptr
= &f
;
420 err
= qemu_strtol(str
, &endptr
, 0, &res
);
422 g_assert_cmpint(err
, ==, -ERANGE
);
423 g_assert_cmpint(res
, ==, LONG_MIN
);
424 g_assert(endptr
== str
+ strlen(str
));
427 static void test_qemu_strtol_negative(void)
429 const char *str
= " \t -321";
431 const char *endptr
= &f
;
435 err
= qemu_strtol(str
, &endptr
, 0, &res
);
437 g_assert_cmpint(err
, ==, 0);
438 g_assert_cmpint(res
, ==, -321);
439 g_assert(endptr
== str
+ strlen(str
));
442 static void test_qemu_strtol_full_correct(void)
444 const char *str
= "123";
448 err
= qemu_strtol(str
, NULL
, 0, &res
);
450 g_assert_cmpint(err
, ==, 0);
451 g_assert_cmpint(res
, ==, 123);
454 static void test_qemu_strtol_full_null(void)
457 const char *endptr
= &f
;
461 err
= qemu_strtol(NULL
, &endptr
, 0, &res
);
463 g_assert_cmpint(err
, ==, -EINVAL
);
464 g_assert(endptr
== NULL
);
467 static void test_qemu_strtol_full_empty(void)
469 const char *str
= "";
473 err
= qemu_strtol(str
, NULL
, 0, &res
);
475 g_assert_cmpint(err
, ==, -EINVAL
);
478 static void test_qemu_strtol_full_negative(void)
480 const char *str
= " \t -321";
484 err
= qemu_strtol(str
, NULL
, 0, &res
);
486 g_assert_cmpint(err
, ==, 0);
487 g_assert_cmpint(res
, ==, -321);
490 static void test_qemu_strtol_full_trailing(void)
492 const char *str
= "123xxx";
496 err
= qemu_strtol(str
, NULL
, 0, &res
);
498 g_assert_cmpint(err
, ==, -EINVAL
);
501 static void test_qemu_strtol_full_max(void)
503 const char *str
= g_strdup_printf("%ld", LONG_MAX
);
507 err
= qemu_strtol(str
, NULL
, 0, &res
);
509 g_assert_cmpint(err
, ==, 0);
510 g_assert_cmpint(res
, ==, LONG_MAX
);
513 static void test_qemu_strtoul_correct(void)
515 const char *str
= "12345 foo";
517 const char *endptr
= &f
;
518 unsigned long res
= 999;
521 err
= qemu_strtoul(str
, &endptr
, 0, &res
);
523 g_assert_cmpint(err
, ==, 0);
524 g_assert_cmpint(res
, ==, 12345);
525 g_assert(endptr
== str
+ 5);
528 static void test_qemu_strtoul_null(void)
531 const char *endptr
= &f
;
532 unsigned long res
= 999;
535 err
= qemu_strtoul(NULL
, &endptr
, 0, &res
);
537 g_assert_cmpint(err
, ==, -EINVAL
);
538 g_assert(endptr
== NULL
);
541 static void test_qemu_strtoul_empty(void)
543 const char *str
= "";
545 const char *endptr
= &f
;
546 unsigned long res
= 999;
549 err
= qemu_strtoul(str
, &endptr
, 0, &res
);
551 g_assert_cmpint(err
, ==, -EINVAL
);
554 static void test_qemu_strtoul_whitespace(void)
556 const char *str
= " \t ";
558 const char *endptr
= &f
;
559 unsigned long res
= 999;
562 err
= qemu_strtoul(str
, &endptr
, 0, &res
);
564 g_assert_cmpint(err
, ==, -EINVAL
);
567 static void test_qemu_strtoul_invalid(void)
569 const char *str
= " xxxx \t abc";
571 const char *endptr
= &f
;
572 unsigned long res
= 999;
575 err
= qemu_strtoul(str
, &endptr
, 0, &res
);
577 g_assert_cmpint(err
, ==, -EINVAL
);
580 static void test_qemu_strtoul_trailing(void)
582 const char *str
= "123xxx";
584 const char *endptr
= &f
;
585 unsigned long res
= 999;
588 err
= qemu_strtoul(str
, &endptr
, 0, &res
);
590 g_assert_cmpint(err
, ==, 0);
591 g_assert_cmpint(res
, ==, 123);
592 g_assert(endptr
== str
+ 3);
595 static void test_qemu_strtoul_octal(void)
597 const char *str
= "0123";
599 const char *endptr
= &f
;
600 unsigned long res
= 999;
603 err
= qemu_strtoul(str
, &endptr
, 8, &res
);
605 g_assert_cmpint(err
, ==, 0);
606 g_assert_cmpint(res
, ==, 0123);
607 g_assert(endptr
== str
+ strlen(str
));
611 err
= qemu_strtoul(str
, &endptr
, 0, &res
);
613 g_assert_cmpint(err
, ==, 0);
614 g_assert_cmpint(res
, ==, 0123);
615 g_assert(endptr
== str
+ strlen(str
));
618 static void test_qemu_strtoul_decimal(void)
620 const char *str
= "0123";
622 const char *endptr
= &f
;
623 unsigned long res
= 999;
626 err
= qemu_strtoul(str
, &endptr
, 10, &res
);
628 g_assert_cmpint(err
, ==, 0);
629 g_assert_cmpint(res
, ==, 123);
630 g_assert(endptr
== str
+ strlen(str
));
635 err
= qemu_strtoul(str
, &endptr
, 0, &res
);
637 g_assert_cmpint(err
, ==, 0);
638 g_assert_cmpint(res
, ==, 123);
639 g_assert(endptr
== str
+ strlen(str
));
642 static void test_qemu_strtoul_hex(void)
644 const char *str
= "0123";
646 const char *endptr
= &f
;
647 unsigned long res
= 999;
650 err
= qemu_strtoul(str
, &endptr
, 16, &res
);
652 g_assert_cmpint(err
, ==, 0);
653 g_assert_cmpint(res
, ==, 0x123);
654 g_assert(endptr
== str
+ strlen(str
));
659 err
= qemu_strtoul(str
, &endptr
, 0, &res
);
661 g_assert_cmpint(err
, ==, 0);
662 g_assert_cmpint(res
, ==, 0x123);
663 g_assert(endptr
== str
+ strlen(str
));
666 static void test_qemu_strtoul_max(void)
668 const char *str
= g_strdup_printf("%lu", ULONG_MAX
);
670 const char *endptr
= &f
;
671 unsigned long res
= 999;
674 err
= qemu_strtoul(str
, &endptr
, 0, &res
);
676 g_assert_cmpint(err
, ==, 0);
677 g_assert_cmpint(res
, ==, ULONG_MAX
);
678 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 const 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
);
792 static void test_qemu_strtoll_correct(void)
794 const char *str
= "12345 foo";
796 const char *endptr
= &f
;
800 err
= qemu_strtoll(str
, &endptr
, 0, &res
);
802 g_assert_cmpint(err
, ==, 0);
803 g_assert_cmpint(res
, ==, 12345);
804 g_assert(endptr
== str
+ 5);
807 static void test_qemu_strtoll_null(void)
810 const char *endptr
= &f
;
814 err
= qemu_strtoll(NULL
, &endptr
, 0, &res
);
816 g_assert_cmpint(err
, ==, -EINVAL
);
817 g_assert(endptr
== NULL
);
820 static void test_qemu_strtoll_empty(void)
822 const char *str
= "";
824 const char *endptr
= &f
;
828 err
= qemu_strtoll(str
, &endptr
, 0, &res
);
830 g_assert_cmpint(err
, ==, -EINVAL
);
833 static void test_qemu_strtoll_whitespace(void)
835 const char *str
= " \t ";
837 const char *endptr
= &f
;
841 err
= qemu_strtoll(str
, &endptr
, 0, &res
);
843 g_assert_cmpint(err
, ==, -EINVAL
);
846 static void test_qemu_strtoll_invalid(void)
848 const char *str
= " xxxx \t abc";
850 const char *endptr
= &f
;
854 err
= qemu_strtoll(str
, &endptr
, 0, &res
);
856 g_assert_cmpint(err
, ==, -EINVAL
);
859 static void test_qemu_strtoll_trailing(void)
861 const char *str
= "123xxx";
863 const char *endptr
= &f
;
867 err
= qemu_strtoll(str
, &endptr
, 0, &res
);
869 g_assert_cmpint(err
, ==, 0);
870 g_assert_cmpint(res
, ==, 123);
871 g_assert(endptr
== str
+ 3);
874 static void test_qemu_strtoll_octal(void)
876 const char *str
= "0123";
878 const char *endptr
= &f
;
882 err
= qemu_strtoll(str
, &endptr
, 8, &res
);
884 g_assert_cmpint(err
, ==, 0);
885 g_assert_cmpint(res
, ==, 0123);
886 g_assert(endptr
== str
+ strlen(str
));
890 err
= qemu_strtoll(str
, &endptr
, 0, &res
);
892 g_assert_cmpint(err
, ==, 0);
893 g_assert_cmpint(res
, ==, 0123);
894 g_assert(endptr
== str
+ strlen(str
));
897 static void test_qemu_strtoll_decimal(void)
899 const char *str
= "0123";
901 const char *endptr
= &f
;
905 err
= qemu_strtoll(str
, &endptr
, 10, &res
);
907 g_assert_cmpint(err
, ==, 0);
908 g_assert_cmpint(res
, ==, 123);
909 g_assert(endptr
== str
+ strlen(str
));
914 err
= qemu_strtoll(str
, &endptr
, 0, &res
);
916 g_assert_cmpint(err
, ==, 0);
917 g_assert_cmpint(res
, ==, 123);
918 g_assert(endptr
== str
+ strlen(str
));
921 static void test_qemu_strtoll_hex(void)
923 const char *str
= "0123";
925 const char *endptr
= &f
;
929 err
= qemu_strtoll(str
, &endptr
, 16, &res
);
931 g_assert_cmpint(err
, ==, 0);
932 g_assert_cmpint(res
, ==, 0x123);
933 g_assert(endptr
== str
+ strlen(str
));
938 err
= qemu_strtoll(str
, &endptr
, 0, &res
);
940 g_assert_cmpint(err
, ==, 0);
941 g_assert_cmpint(res
, ==, 0x123);
942 g_assert(endptr
== str
+ strlen(str
));
945 static void test_qemu_strtoll_max(void)
947 const char *str
= g_strdup_printf("%lld", LLONG_MAX
);
949 const char *endptr
= &f
;
953 err
= qemu_strtoll(str
, &endptr
, 0, &res
);
955 g_assert_cmpint(err
, ==, 0);
956 g_assert_cmpint(res
, ==, LLONG_MAX
);
957 g_assert(endptr
== str
+ strlen(str
));
960 static void test_qemu_strtoll_overflow(void)
962 const char *str
= "99999999999999999999999999999999999999999999";
964 const char *endptr
= &f
;
968 err
= qemu_strtoll(str
, &endptr
, 0, &res
);
970 g_assert_cmpint(err
, ==, -ERANGE
);
971 g_assert_cmpint(res
, ==, LLONG_MAX
);
972 g_assert(endptr
== str
+ strlen(str
));
975 static void test_qemu_strtoll_underflow(void)
977 const char *str
= "-99999999999999999999999999999999999999999999";
979 const char *endptr
= &f
;
983 err
= qemu_strtoll(str
, &endptr
, 0, &res
);
985 g_assert_cmpint(err
, ==, -ERANGE
);
986 g_assert_cmpint(res
, ==, LLONG_MIN
);
987 g_assert(endptr
== str
+ strlen(str
));
990 static void test_qemu_strtoll_negative(void)
992 const char *str
= " \t -321";
994 const char *endptr
= &f
;
998 err
= qemu_strtoll(str
, &endptr
, 0, &res
);
1000 g_assert_cmpint(err
, ==, 0);
1001 g_assert_cmpint(res
, ==, -321);
1002 g_assert(endptr
== str
+ strlen(str
));
1005 static void test_qemu_strtoll_full_correct(void)
1007 const char *str
= "123";
1011 err
= qemu_strtoll(str
, NULL
, 0, &res
);
1013 g_assert_cmpint(err
, ==, 0);
1014 g_assert_cmpint(res
, ==, 123);
1017 static void test_qemu_strtoll_full_null(void)
1022 err
= qemu_strtoll(NULL
, NULL
, 0, &res
);
1024 g_assert_cmpint(err
, ==, -EINVAL
);
1027 static void test_qemu_strtoll_full_empty(void)
1029 const char *str
= "";
1033 err
= qemu_strtoll(str
, NULL
, 0, &res
);
1035 g_assert_cmpint(err
, ==, -EINVAL
);
1038 static void test_qemu_strtoll_full_negative(void)
1040 const char *str
= " \t -321";
1044 err
= qemu_strtoll(str
, NULL
, 0, &res
);
1046 g_assert_cmpint(err
, ==, 0);
1047 g_assert_cmpint(res
, ==, -321);
1050 static void test_qemu_strtoll_full_trailing(void)
1052 const char *str
= "123xxx";
1056 err
= qemu_strtoll(str
, NULL
, 0, &res
);
1058 g_assert_cmpint(err
, ==, -EINVAL
);
1061 static void test_qemu_strtoll_full_max(void)
1064 const char *str
= g_strdup_printf("%lld", LLONG_MAX
);
1068 err
= qemu_strtoll(str
, NULL
, 0, &res
);
1070 g_assert_cmpint(err
, ==, 0);
1071 g_assert_cmpint(res
, ==, LLONG_MAX
);
1074 static void test_qemu_strtoull_correct(void)
1076 const char *str
= "12345 foo";
1078 const char *endptr
= &f
;
1082 err
= qemu_strtoull(str
, &endptr
, 0, &res
);
1084 g_assert_cmpint(err
, ==, 0);
1085 g_assert_cmpint(res
, ==, 12345);
1086 g_assert(endptr
== str
+ 5);
1089 static void test_qemu_strtoull_null(void)
1092 const char *endptr
= &f
;
1096 err
= qemu_strtoull(NULL
, &endptr
, 0, &res
);
1098 g_assert_cmpint(err
, ==, -EINVAL
);
1099 g_assert(endptr
== NULL
);
1102 static void test_qemu_strtoull_empty(void)
1104 const char *str
= "";
1106 const char *endptr
= &f
;
1110 err
= qemu_strtoull(str
, &endptr
, 0, &res
);
1112 g_assert_cmpint(err
, ==, -EINVAL
);
1115 static void test_qemu_strtoull_whitespace(void)
1117 const char *str
= " \t ";
1119 const char *endptr
= &f
;
1123 err
= qemu_strtoull(str
, &endptr
, 0, &res
);
1125 g_assert_cmpint(err
, ==, -EINVAL
);
1128 static void test_qemu_strtoull_invalid(void)
1130 const char *str
= " xxxx \t abc";
1132 const char *endptr
= &f
;
1136 err
= qemu_strtoull(str
, &endptr
, 0, &res
);
1138 g_assert_cmpint(err
, ==, -EINVAL
);
1141 static void test_qemu_strtoull_trailing(void)
1143 const char *str
= "123xxx";
1145 const char *endptr
= &f
;
1149 err
= qemu_strtoull(str
, &endptr
, 0, &res
);
1151 g_assert_cmpint(err
, ==, 0);
1152 g_assert_cmpint(res
, ==, 123);
1153 g_assert(endptr
== str
+ 3);
1156 static void test_qemu_strtoull_octal(void)
1158 const char *str
= "0123";
1160 const char *endptr
= &f
;
1164 err
= qemu_strtoull(str
, &endptr
, 8, &res
);
1166 g_assert_cmpint(err
, ==, 0);
1167 g_assert_cmpint(res
, ==, 0123);
1168 g_assert(endptr
== str
+ strlen(str
));
1172 err
= qemu_strtoull(str
, &endptr
, 0, &res
);
1174 g_assert_cmpint(err
, ==, 0);
1175 g_assert_cmpint(res
, ==, 0123);
1176 g_assert(endptr
== str
+ strlen(str
));
1179 static void test_qemu_strtoull_decimal(void)
1181 const char *str
= "0123";
1183 const char *endptr
= &f
;
1187 err
= qemu_strtoull(str
, &endptr
, 10, &res
);
1189 g_assert_cmpint(err
, ==, 0);
1190 g_assert_cmpint(res
, ==, 123);
1191 g_assert(endptr
== str
+ strlen(str
));
1196 err
= qemu_strtoull(str
, &endptr
, 0, &res
);
1198 g_assert_cmpint(err
, ==, 0);
1199 g_assert_cmpint(res
, ==, 123);
1200 g_assert(endptr
== str
+ strlen(str
));
1203 static void test_qemu_strtoull_hex(void)
1205 const char *str
= "0123";
1207 const char *endptr
= &f
;
1211 err
= qemu_strtoull(str
, &endptr
, 16, &res
);
1213 g_assert_cmpint(err
, ==, 0);
1214 g_assert_cmpint(res
, ==, 0x123);
1215 g_assert(endptr
== str
+ strlen(str
));
1220 err
= qemu_strtoull(str
, &endptr
, 0, &res
);
1222 g_assert_cmpint(err
, ==, 0);
1223 g_assert_cmpint(res
, ==, 0x123);
1224 g_assert(endptr
== str
+ strlen(str
));
1227 static void test_qemu_strtoull_max(void)
1229 const char *str
= g_strdup_printf("%llu", ULLONG_MAX
);
1231 const char *endptr
= &f
;
1235 err
= qemu_strtoull(str
, &endptr
, 0, &res
);
1237 g_assert_cmpint(err
, ==, 0);
1238 g_assert_cmpint(res
, ==, ULLONG_MAX
);
1239 g_assert(endptr
== str
+ strlen(str
));
1242 static void test_qemu_strtoull_overflow(void)
1244 const char *str
= "99999999999999999999999999999999999999999999";
1246 const char *endptr
= &f
;
1250 err
= qemu_strtoull(str
, &endptr
, 0, &res
);
1252 g_assert_cmpint(err
, ==, -ERANGE
);
1253 g_assert_cmpint(res
, ==, ULLONG_MAX
);
1254 g_assert(endptr
== str
+ strlen(str
));
1257 static void test_qemu_strtoull_underflow(void)
1259 const char *str
= "-99999999999999999999999999999999999999999999";
1261 const char *endptr
= &f
;
1265 err
= qemu_strtoull(str
, &endptr
, 0, &res
);
1267 g_assert_cmpint(err
, ==, -ERANGE
);
1268 g_assert_cmpint(res
, ==, -1);
1269 g_assert(endptr
== str
+ strlen(str
));
1272 static void test_qemu_strtoull_negative(void)
1274 const char *str
= " \t -321";
1276 const char *endptr
= &f
;
1280 err
= qemu_strtoull(str
, &endptr
, 0, &res
);
1282 g_assert_cmpint(err
, ==, 0);
1283 g_assert_cmpint(res
, ==, -321);
1284 g_assert(endptr
== str
+ strlen(str
));
1287 static void test_qemu_strtoull_full_correct(void)
1289 const char *str
= "18446744073709551614";
1293 err
= qemu_strtoull(str
, NULL
, 0, &res
);
1295 g_assert_cmpint(err
, ==, 0);
1296 g_assert_cmpint(res
, ==, 18446744073709551614LLU);
1299 static void test_qemu_strtoull_full_null(void)
1304 err
= qemu_strtoull(NULL
, NULL
, 0, &res
);
1306 g_assert_cmpint(err
, ==, -EINVAL
);
1309 static void test_qemu_strtoull_full_empty(void)
1311 const char *str
= "";
1315 err
= qemu_strtoull(str
, NULL
, 0, &res
);
1317 g_assert_cmpint(err
, ==, -EINVAL
);
1320 static void test_qemu_strtoull_full_negative(void)
1322 const char *str
= " \t -321";
1326 err
= qemu_strtoull(str
, NULL
, 0, &res
);
1328 g_assert_cmpint(err
, ==, 0);
1329 g_assert_cmpint(res
, ==, 18446744073709551295LLU);
1332 static void test_qemu_strtoull_full_trailing(void)
1334 const char *str
= "18446744073709551614xxxxxx";
1338 err
= qemu_strtoull(str
, NULL
, 0, &res
);
1340 g_assert_cmpint(err
, ==, -EINVAL
);
1343 static void test_qemu_strtoull_full_max(void)
1345 const char *str
= g_strdup_printf("%lld", ULLONG_MAX
);
1349 err
= qemu_strtoull(str
, NULL
, 0, &res
);
1351 g_assert_cmpint(err
, ==, 0);
1352 g_assert_cmpint(res
, ==, ULLONG_MAX
);
1355 static void test_qemu_strtosz_simple(void)
1357 const char *str
= "12345M";
1358 char *endptr
= NULL
;
1361 res
= qemu_strtosz(str
, &endptr
);
1362 g_assert_cmpint(res
, ==, 12345 * M_BYTE
);
1363 g_assert(endptr
== str
+ 6);
1365 res
= qemu_strtosz(str
, NULL
);
1366 g_assert_cmpint(res
, ==, 12345 * M_BYTE
);
1369 static void test_qemu_strtosz_units(void)
1371 const char *none
= "1";
1372 const char *b
= "1B";
1373 const char *k
= "1K";
1374 const char *m
= "1M";
1375 const char *g
= "1G";
1376 const char *t
= "1T";
1377 const char *p
= "1P";
1378 const char *e
= "1E";
1382 res
= qemu_strtosz(none
, NULL
);
1383 g_assert_cmpint(res
, ==, M_BYTE
);
1385 res
= qemu_strtosz(b
, NULL
);
1386 g_assert_cmpint(res
, ==, 1);
1388 res
= qemu_strtosz(k
, NULL
);
1389 g_assert_cmpint(res
, ==, K_BYTE
);
1391 res
= qemu_strtosz(m
, NULL
);
1392 g_assert_cmpint(res
, ==, M_BYTE
);
1394 res
= qemu_strtosz(g
, NULL
);
1395 g_assert_cmpint(res
, ==, G_BYTE
);
1397 res
= qemu_strtosz(t
, NULL
);
1398 g_assert_cmpint(res
, ==, T_BYTE
);
1400 res
= qemu_strtosz(p
, NULL
);
1401 g_assert_cmpint(res
, ==, P_BYTE
);
1403 res
= qemu_strtosz(e
, NULL
);
1404 g_assert_cmpint(res
, ==, E_BYTE
);
1407 static void test_qemu_strtosz_float(void)
1409 const char *str
= "12.345M";
1412 res
= qemu_strtosz(str
, NULL
);
1413 g_assert_cmpint(res
, ==, 12.345 * M_BYTE
);
1416 static void test_qemu_strtosz_erange(void)
1418 const char *str
= "10E";
1421 res
= qemu_strtosz(str
, NULL
);
1422 g_assert_cmpint(res
, ==, -ERANGE
);
1425 static void test_qemu_strtosz_suffix_unit(void)
1427 const char *str
= "12345";
1430 res
= qemu_strtosz_suffix_unit(str
, NULL
,
1431 QEMU_STRTOSZ_DEFSUFFIX_KB
, 1000);
1432 g_assert_cmpint(res
, ==, 12345000);
1435 int main(int argc
, char **argv
)
1437 g_test_init(&argc
, &argv
, NULL
);
1439 g_test_add_func("/cutils/parse_uint/null", test_parse_uint_null
);
1440 g_test_add_func("/cutils/parse_uint/empty", test_parse_uint_empty
);
1441 g_test_add_func("/cutils/parse_uint/whitespace",
1442 test_parse_uint_whitespace
);
1443 g_test_add_func("/cutils/parse_uint/invalid", test_parse_uint_invalid
);
1444 g_test_add_func("/cutils/parse_uint/trailing", test_parse_uint_trailing
);
1445 g_test_add_func("/cutils/parse_uint/correct", test_parse_uint_correct
);
1446 g_test_add_func("/cutils/parse_uint/octal", test_parse_uint_octal
);
1447 g_test_add_func("/cutils/parse_uint/decimal", test_parse_uint_decimal
);
1448 g_test_add_func("/cutils/parse_uint/llong_max", test_parse_uint_llong_max
);
1449 g_test_add_func("/cutils/parse_uint/overflow", test_parse_uint_overflow
);
1450 g_test_add_func("/cutils/parse_uint/negative", test_parse_uint_negative
);
1451 g_test_add_func("/cutils/parse_uint_full/trailing",
1452 test_parse_uint_full_trailing
);
1453 g_test_add_func("/cutils/parse_uint_full/correct",
1454 test_parse_uint_full_correct
);
1456 /* qemu_strtol() tests */
1457 g_test_add_func("/cutils/qemu_strtol/correct", test_qemu_strtol_correct
);
1458 g_test_add_func("/cutils/qemu_strtol/null", test_qemu_strtol_null
);
1459 g_test_add_func("/cutils/qemu_strtol/empty", test_qemu_strtol_empty
);
1460 g_test_add_func("/cutils/qemu_strtol/whitespace",
1461 test_qemu_strtol_whitespace
);
1462 g_test_add_func("/cutils/qemu_strtol/invalid", test_qemu_strtol_invalid
);
1463 g_test_add_func("/cutils/qemu_strtol/trailing", test_qemu_strtol_trailing
);
1464 g_test_add_func("/cutils/qemu_strtol/octal", test_qemu_strtol_octal
);
1465 g_test_add_func("/cutils/qemu_strtol/decimal", test_qemu_strtol_decimal
);
1466 g_test_add_func("/cutils/qemu_strtol/hex", test_qemu_strtol_hex
);
1467 g_test_add_func("/cutils/qemu_strtol/max", test_qemu_strtol_max
);
1468 g_test_add_func("/cutils/qemu_strtol/overflow", test_qemu_strtol_overflow
);
1469 g_test_add_func("/cutils/qemu_strtol/underflow",
1470 test_qemu_strtol_underflow
);
1471 g_test_add_func("/cutils/qemu_strtol/negative", test_qemu_strtol_negative
);
1472 g_test_add_func("/cutils/qemu_strtol_full/correct",
1473 test_qemu_strtol_full_correct
);
1474 g_test_add_func("/cutils/qemu_strtol_full/null",
1475 test_qemu_strtol_full_null
);
1476 g_test_add_func("/cutils/qemu_strtol_full/empty",
1477 test_qemu_strtol_full_empty
);
1478 g_test_add_func("/cutils/qemu_strtol_full/negative",
1479 test_qemu_strtol_full_negative
);
1480 g_test_add_func("/cutils/qemu_strtol_full/trailing",
1481 test_qemu_strtol_full_trailing
);
1482 g_test_add_func("/cutils/qemu_strtol_full/max",
1483 test_qemu_strtol_full_max
);
1485 /* qemu_strtoul() tests */
1486 g_test_add_func("/cutils/qemu_strtoul/correct", test_qemu_strtoul_correct
);
1487 g_test_add_func("/cutils/qemu_strtoul/null", test_qemu_strtoul_null
);
1488 g_test_add_func("/cutils/qemu_strtoul/empty", test_qemu_strtoul_empty
);
1489 g_test_add_func("/cutils/qemu_strtoul/whitespace",
1490 test_qemu_strtoul_whitespace
);
1491 g_test_add_func("/cutils/qemu_strtoul/invalid", test_qemu_strtoul_invalid
);
1492 g_test_add_func("/cutils/qemu_strtoul/trailing",
1493 test_qemu_strtoul_trailing
);
1494 g_test_add_func("/cutils/qemu_strtoul/octal", test_qemu_strtoul_octal
);
1495 g_test_add_func("/cutils/qemu_strtoul/decimal", test_qemu_strtoul_decimal
);
1496 g_test_add_func("/cutils/qemu_strtoul/hex", test_qemu_strtoul_hex
);
1497 g_test_add_func("/cutils/qemu_strtoul/max", test_qemu_strtoul_max
);
1498 g_test_add_func("/cutils/qemu_strtoul/overflow",
1499 test_qemu_strtoul_overflow
);
1500 g_test_add_func("/cutils/qemu_strtoul/underflow",
1501 test_qemu_strtoul_underflow
);
1502 g_test_add_func("/cutils/qemu_strtoul/negative",
1503 test_qemu_strtoul_negative
);
1504 g_test_add_func("/cutils/qemu_strtoul_full/correct",
1505 test_qemu_strtoul_full_correct
);
1506 g_test_add_func("/cutils/qemu_strtoul_full/null",
1507 test_qemu_strtoul_full_null
);
1508 g_test_add_func("/cutils/qemu_strtoul_full/empty",
1509 test_qemu_strtoul_full_empty
);
1510 g_test_add_func("/cutils/qemu_strtoul_full/negative",
1511 test_qemu_strtoul_full_negative
);
1512 g_test_add_func("/cutils/qemu_strtoul_full/trailing",
1513 test_qemu_strtoul_full_trailing
);
1514 g_test_add_func("/cutils/qemu_strtoul_full/max",
1515 test_qemu_strtoul_full_max
);
1517 /* qemu_strtoll() tests */
1518 g_test_add_func("/cutils/qemu_strtoll/correct", test_qemu_strtoll_correct
);
1519 g_test_add_func("/cutils/qemu_strtoll/null", test_qemu_strtoll_null
);
1520 g_test_add_func("/cutils/qemu_strtoll/empty", test_qemu_strtoll_empty
);
1521 g_test_add_func("/cutils/qemu_strtoll/whitespace",
1522 test_qemu_strtoll_whitespace
);
1523 g_test_add_func("/cutils/qemu_strtoll/invalid", test_qemu_strtoll_invalid
);
1524 g_test_add_func("/cutils/qemu_strtoll/trailing",
1525 test_qemu_strtoll_trailing
);
1526 g_test_add_func("/cutils/qemu_strtoll/octal", test_qemu_strtoll_octal
);
1527 g_test_add_func("/cutils/qemu_strtoll/decimal", test_qemu_strtoll_decimal
);
1528 g_test_add_func("/cutils/qemu_strtoll/hex", test_qemu_strtoll_hex
);
1529 g_test_add_func("/cutils/qemu_strtoll/max", test_qemu_strtoll_max
);
1530 g_test_add_func("/cutils/qemu_strtoll/overflow",
1531 test_qemu_strtoll_overflow
);
1532 g_test_add_func("/cutils/qemu_strtoll/underflow",
1533 test_qemu_strtoll_underflow
);
1534 g_test_add_func("/cutils/qemu_strtoll/negative",
1535 test_qemu_strtoll_negative
);
1536 g_test_add_func("/cutils/qemu_strtoll_full/correct",
1537 test_qemu_strtoll_full_correct
);
1538 g_test_add_func("/cutils/qemu_strtoll_full/null",
1539 test_qemu_strtoll_full_null
);
1540 g_test_add_func("/cutils/qemu_strtoll_full/empty",
1541 test_qemu_strtoll_full_empty
);
1542 g_test_add_func("/cutils/qemu_strtoll_full/negative",
1543 test_qemu_strtoll_full_negative
);
1544 g_test_add_func("/cutils/qemu_strtoll_full/trailing",
1545 test_qemu_strtoll_full_trailing
);
1546 g_test_add_func("/cutils/qemu_strtoll_full/max",
1547 test_qemu_strtoll_full_max
);
1549 /* qemu_strtoull() tests */
1550 g_test_add_func("/cutils/qemu_strtoull/correct",
1551 test_qemu_strtoull_correct
);
1552 g_test_add_func("/cutils/qemu_strtoull/null",
1553 test_qemu_strtoull_null
);
1554 g_test_add_func("/cutils/qemu_strtoull/empty", test_qemu_strtoull_empty
);
1555 g_test_add_func("/cutils/qemu_strtoull/whitespace",
1556 test_qemu_strtoull_whitespace
);
1557 g_test_add_func("/cutils/qemu_strtoull/invalid",
1558 test_qemu_strtoull_invalid
);
1559 g_test_add_func("/cutils/qemu_strtoull/trailing",
1560 test_qemu_strtoull_trailing
);
1561 g_test_add_func("/cutils/qemu_strtoull/octal", test_qemu_strtoull_octal
);
1562 g_test_add_func("/cutils/qemu_strtoull/decimal",
1563 test_qemu_strtoull_decimal
);
1564 g_test_add_func("/cutils/qemu_strtoull/hex", test_qemu_strtoull_hex
);
1565 g_test_add_func("/cutils/qemu_strtoull/max", test_qemu_strtoull_max
);
1566 g_test_add_func("/cutils/qemu_strtoull/overflow",
1567 test_qemu_strtoull_overflow
);
1568 g_test_add_func("/cutils/qemu_strtoull/underflow",
1569 test_qemu_strtoull_underflow
);
1570 g_test_add_func("/cutils/qemu_strtoull/negative",
1571 test_qemu_strtoull_negative
);
1572 g_test_add_func("/cutils/qemu_strtoull_full/correct",
1573 test_qemu_strtoull_full_correct
);
1574 g_test_add_func("/cutils/qemu_strtoull_full/null",
1575 test_qemu_strtoull_full_null
);
1576 g_test_add_func("/cutils/qemu_strtoull_full/empty",
1577 test_qemu_strtoull_full_empty
);
1578 g_test_add_func("/cutils/qemu_strtoull_full/negative",
1579 test_qemu_strtoull_full_negative
);
1580 g_test_add_func("/cutils/qemu_strtoull_full/trailing",
1581 test_qemu_strtoull_full_trailing
);
1582 g_test_add_func("/cutils/qemu_strtoull_full/max",
1583 test_qemu_strtoull_full_max
);
1585 g_test_add_func("/cutils/strtosz/simple",
1586 test_qemu_strtosz_simple
);
1587 g_test_add_func("/cutils/strtosz/units",
1588 test_qemu_strtosz_units
);
1589 g_test_add_func("/cutils/strtosz/float",
1590 test_qemu_strtosz_float
);
1591 g_test_add_func("/cutils/strtosz/erange",
1592 test_qemu_strtosz_erange
);
1593 g_test_add_func("/cutils/strtosz/suffix-unit",
1594 test_qemu_strtosz_suffix_unit
);
1596 return g_test_run();