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 const 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
));
394 static void test_qemu_strtol_overflow(void)
396 const char *str
= "99999999999999999999999999999999999999999999";
398 const char *endptr
= &f
;
402 err
= qemu_strtol(str
, &endptr
, 0, &res
);
404 g_assert_cmpint(err
, ==, -ERANGE
);
405 g_assert_cmpint(res
, ==, LONG_MAX
);
406 g_assert(endptr
== str
+ strlen(str
));
409 static void test_qemu_strtol_underflow(void)
411 const char *str
= "-99999999999999999999999999999999999999999999";
413 const char *endptr
= &f
;
417 err
= qemu_strtol(str
, &endptr
, 0, &res
);
419 g_assert_cmpint(err
, ==, -ERANGE
);
420 g_assert_cmpint(res
, ==, LONG_MIN
);
421 g_assert(endptr
== str
+ strlen(str
));
424 static void test_qemu_strtol_negative(void)
426 const char *str
= " \t -321";
428 const char *endptr
= &f
;
432 err
= qemu_strtol(str
, &endptr
, 0, &res
);
434 g_assert_cmpint(err
, ==, 0);
435 g_assert_cmpint(res
, ==, -321);
436 g_assert(endptr
== str
+ strlen(str
));
439 static void test_qemu_strtol_full_correct(void)
441 const char *str
= "123";
445 err
= qemu_strtol(str
, NULL
, 0, &res
);
447 g_assert_cmpint(err
, ==, 0);
448 g_assert_cmpint(res
, ==, 123);
451 static void test_qemu_strtol_full_null(void)
454 const char *endptr
= &f
;
458 err
= qemu_strtol(NULL
, &endptr
, 0, &res
);
460 g_assert_cmpint(err
, ==, -EINVAL
);
461 g_assert(endptr
== NULL
);
464 static void test_qemu_strtol_full_empty(void)
466 const char *str
= "";
470 err
= qemu_strtol(str
, NULL
, 0, &res
);
472 g_assert_cmpint(err
, ==, -EINVAL
);
475 static void test_qemu_strtol_full_negative(void)
477 const char *str
= " \t -321";
481 err
= qemu_strtol(str
, NULL
, 0, &res
);
483 g_assert_cmpint(err
, ==, 0);
484 g_assert_cmpint(res
, ==, -321);
487 static void test_qemu_strtol_full_trailing(void)
489 const char *str
= "123xxx";
493 err
= qemu_strtol(str
, NULL
, 0, &res
);
495 g_assert_cmpint(err
, ==, -EINVAL
);
498 static void test_qemu_strtol_full_max(void)
500 const char *str
= g_strdup_printf("%ld", LONG_MAX
);
504 err
= qemu_strtol(str
, NULL
, 0, &res
);
506 g_assert_cmpint(err
, ==, 0);
507 g_assert_cmpint(res
, ==, LONG_MAX
);
510 static void test_qemu_strtoul_correct(void)
512 const char *str
= "12345 foo";
514 const char *endptr
= &f
;
515 unsigned long res
= 999;
518 err
= qemu_strtoul(str
, &endptr
, 0, &res
);
520 g_assert_cmpint(err
, ==, 0);
521 g_assert_cmpint(res
, ==, 12345);
522 g_assert(endptr
== str
+ 5);
525 static void test_qemu_strtoul_null(void)
528 const char *endptr
= &f
;
529 unsigned long res
= 999;
532 err
= qemu_strtoul(NULL
, &endptr
, 0, &res
);
534 g_assert_cmpint(err
, ==, -EINVAL
);
535 g_assert(endptr
== NULL
);
538 static void test_qemu_strtoul_empty(void)
540 const char *str
= "";
542 const char *endptr
= &f
;
543 unsigned long res
= 999;
546 err
= qemu_strtoul(str
, &endptr
, 0, &res
);
548 g_assert_cmpint(err
, ==, -EINVAL
);
551 static void test_qemu_strtoul_whitespace(void)
553 const char *str
= " \t ";
555 const char *endptr
= &f
;
556 unsigned long res
= 999;
559 err
= qemu_strtoul(str
, &endptr
, 0, &res
);
561 g_assert_cmpint(err
, ==, -EINVAL
);
564 static void test_qemu_strtoul_invalid(void)
566 const char *str
= " xxxx \t abc";
568 const char *endptr
= &f
;
569 unsigned long res
= 999;
572 err
= qemu_strtoul(str
, &endptr
, 0, &res
);
574 g_assert_cmpint(err
, ==, -EINVAL
);
577 static void test_qemu_strtoul_trailing(void)
579 const char *str
= "123xxx";
581 const char *endptr
= &f
;
582 unsigned long res
= 999;
585 err
= qemu_strtoul(str
, &endptr
, 0, &res
);
587 g_assert_cmpint(err
, ==, 0);
588 g_assert_cmpint(res
, ==, 123);
589 g_assert(endptr
== str
+ 3);
592 static void test_qemu_strtoul_octal(void)
594 const char *str
= "0123";
596 const char *endptr
= &f
;
597 unsigned long res
= 999;
600 err
= qemu_strtoul(str
, &endptr
, 8, &res
);
602 g_assert_cmpint(err
, ==, 0);
603 g_assert_cmpint(res
, ==, 0123);
604 g_assert(endptr
== str
+ strlen(str
));
608 err
= qemu_strtoul(str
, &endptr
, 0, &res
);
610 g_assert_cmpint(err
, ==, 0);
611 g_assert_cmpint(res
, ==, 0123);
612 g_assert(endptr
== str
+ strlen(str
));
615 static void test_qemu_strtoul_decimal(void)
617 const char *str
= "0123";
619 const char *endptr
= &f
;
620 unsigned long res
= 999;
623 err
= qemu_strtoul(str
, &endptr
, 10, &res
);
625 g_assert_cmpint(err
, ==, 0);
626 g_assert_cmpint(res
, ==, 123);
627 g_assert(endptr
== str
+ strlen(str
));
632 err
= qemu_strtoul(str
, &endptr
, 0, &res
);
634 g_assert_cmpint(err
, ==, 0);
635 g_assert_cmpint(res
, ==, 123);
636 g_assert(endptr
== str
+ strlen(str
));
639 static void test_qemu_strtoul_hex(void)
641 const char *str
= "0123";
643 const char *endptr
= &f
;
644 unsigned long res
= 999;
647 err
= qemu_strtoul(str
, &endptr
, 16, &res
);
649 g_assert_cmpint(err
, ==, 0);
650 g_assert_cmpint(res
, ==, 0x123);
651 g_assert(endptr
== str
+ strlen(str
));
656 err
= qemu_strtoul(str
, &endptr
, 0, &res
);
658 g_assert_cmpint(err
, ==, 0);
659 g_assert_cmpint(res
, ==, 0x123);
660 g_assert(endptr
== str
+ strlen(str
));
663 static void test_qemu_strtoul_max(void)
665 const char *str
= g_strdup_printf("%lu", ULONG_MAX
);
667 const char *endptr
= &f
;
668 unsigned long res
= 999;
671 err
= qemu_strtoul(str
, &endptr
, 0, &res
);
673 g_assert_cmpint(err
, ==, 0);
674 g_assert_cmpint(res
, ==, ULONG_MAX
);
675 g_assert(endptr
== str
+ strlen(str
));
678 static void test_qemu_strtoul_overflow(void)
680 const char *str
= "99999999999999999999999999999999999999999999";
682 const char *endptr
= &f
;
683 unsigned long res
= 999;
686 err
= qemu_strtoul(str
, &endptr
, 0, &res
);
688 g_assert_cmpint(err
, ==, -ERANGE
);
689 g_assert_cmpint(res
, ==, ULONG_MAX
);
690 g_assert(endptr
== str
+ strlen(str
));
693 static void test_qemu_strtoul_underflow(void)
695 const char *str
= "-99999999999999999999999999999999999999999999";
697 const char *endptr
= &f
;
698 unsigned long res
= 999;
701 err
= qemu_strtoul(str
, &endptr
, 0, &res
);
703 g_assert_cmpint(err
, ==, -ERANGE
);
704 g_assert_cmpint(res
, ==, -1ul);
705 g_assert(endptr
== str
+ strlen(str
));
708 static void test_qemu_strtoul_negative(void)
710 const char *str
= " \t -321";
712 const char *endptr
= &f
;
713 unsigned long res
= 999;
716 err
= qemu_strtoul(str
, &endptr
, 0, &res
);
718 g_assert_cmpint(err
, ==, 0);
719 g_assert_cmpint(res
, ==, -321ul);
720 g_assert(endptr
== str
+ strlen(str
));
723 static void test_qemu_strtoul_full_correct(void)
725 const char *str
= "123";
726 unsigned long res
= 999;
729 err
= qemu_strtoul(str
, NULL
, 0, &res
);
731 g_assert_cmpint(err
, ==, 0);
732 g_assert_cmpint(res
, ==, 123);
735 static void test_qemu_strtoul_full_null(void)
737 unsigned long res
= 999;
740 err
= qemu_strtoul(NULL
, NULL
, 0, &res
);
742 g_assert_cmpint(err
, ==, -EINVAL
);
745 static void test_qemu_strtoul_full_empty(void)
747 const char *str
= "";
748 unsigned long res
= 999;
751 err
= qemu_strtoul(str
, NULL
, 0, &res
);
753 g_assert_cmpint(err
, ==, -EINVAL
);
755 static void test_qemu_strtoul_full_negative(void)
757 const char *str
= " \t -321";
758 unsigned long res
= 999;
761 err
= qemu_strtoul(str
, NULL
, 0, &res
);
762 g_assert_cmpint(err
, ==, 0);
763 g_assert_cmpint(res
, ==, -321ul);
766 static void test_qemu_strtoul_full_trailing(void)
768 const char *str
= "123xxx";
772 err
= qemu_strtoul(str
, NULL
, 0, &res
);
774 g_assert_cmpint(err
, ==, -EINVAL
);
777 static void test_qemu_strtoul_full_max(void)
779 const char *str
= g_strdup_printf("%lu", ULONG_MAX
);
780 unsigned long res
= 999;
783 err
= qemu_strtoul(str
, NULL
, 0, &res
);
785 g_assert_cmpint(err
, ==, 0);
786 g_assert_cmpint(res
, ==, ULONG_MAX
);
789 static void test_qemu_strtoll_correct(void)
791 const char *str
= "12345 foo";
793 const char *endptr
= &f
;
797 err
= qemu_strtoll(str
, &endptr
, 0, &res
);
799 g_assert_cmpint(err
, ==, 0);
800 g_assert_cmpint(res
, ==, 12345);
801 g_assert(endptr
== str
+ 5);
804 static void test_qemu_strtoll_null(void)
807 const char *endptr
= &f
;
811 err
= qemu_strtoll(NULL
, &endptr
, 0, &res
);
813 g_assert_cmpint(err
, ==, -EINVAL
);
814 g_assert(endptr
== NULL
);
817 static void test_qemu_strtoll_empty(void)
819 const char *str
= "";
821 const char *endptr
= &f
;
825 err
= qemu_strtoll(str
, &endptr
, 0, &res
);
827 g_assert_cmpint(err
, ==, -EINVAL
);
830 static void test_qemu_strtoll_whitespace(void)
832 const char *str
= " \t ";
834 const char *endptr
= &f
;
838 err
= qemu_strtoll(str
, &endptr
, 0, &res
);
840 g_assert_cmpint(err
, ==, -EINVAL
);
843 static void test_qemu_strtoll_invalid(void)
845 const char *str
= " xxxx \t abc";
847 const char *endptr
= &f
;
851 err
= qemu_strtoll(str
, &endptr
, 0, &res
);
853 g_assert_cmpint(err
, ==, -EINVAL
);
856 static void test_qemu_strtoll_trailing(void)
858 const char *str
= "123xxx";
860 const char *endptr
= &f
;
864 err
= qemu_strtoll(str
, &endptr
, 0, &res
);
866 g_assert_cmpint(err
, ==, 0);
867 g_assert_cmpint(res
, ==, 123);
868 g_assert(endptr
== str
+ 3);
871 static void test_qemu_strtoll_octal(void)
873 const char *str
= "0123";
875 const char *endptr
= &f
;
879 err
= qemu_strtoll(str
, &endptr
, 8, &res
);
881 g_assert_cmpint(err
, ==, 0);
882 g_assert_cmpint(res
, ==, 0123);
883 g_assert(endptr
== str
+ strlen(str
));
887 err
= qemu_strtoll(str
, &endptr
, 0, &res
);
889 g_assert_cmpint(err
, ==, 0);
890 g_assert_cmpint(res
, ==, 0123);
891 g_assert(endptr
== str
+ strlen(str
));
894 static void test_qemu_strtoll_decimal(void)
896 const char *str
= "0123";
898 const char *endptr
= &f
;
902 err
= qemu_strtoll(str
, &endptr
, 10, &res
);
904 g_assert_cmpint(err
, ==, 0);
905 g_assert_cmpint(res
, ==, 123);
906 g_assert(endptr
== str
+ strlen(str
));
911 err
= qemu_strtoll(str
, &endptr
, 0, &res
);
913 g_assert_cmpint(err
, ==, 0);
914 g_assert_cmpint(res
, ==, 123);
915 g_assert(endptr
== str
+ strlen(str
));
918 static void test_qemu_strtoll_hex(void)
920 const char *str
= "0123";
922 const char *endptr
= &f
;
926 err
= qemu_strtoll(str
, &endptr
, 16, &res
);
928 g_assert_cmpint(err
, ==, 0);
929 g_assert_cmpint(res
, ==, 0x123);
930 g_assert(endptr
== str
+ strlen(str
));
935 err
= qemu_strtoll(str
, &endptr
, 0, &res
);
937 g_assert_cmpint(err
, ==, 0);
938 g_assert_cmpint(res
, ==, 0x123);
939 g_assert(endptr
== str
+ strlen(str
));
942 static void test_qemu_strtoll_max(void)
944 const char *str
= g_strdup_printf("%lld", LLONG_MAX
);
946 const char *endptr
= &f
;
950 err
= qemu_strtoll(str
, &endptr
, 0, &res
);
952 g_assert_cmpint(err
, ==, 0);
953 g_assert_cmpint(res
, ==, LLONG_MAX
);
954 g_assert(endptr
== str
+ strlen(str
));
957 static void test_qemu_strtoll_overflow(void)
959 const char *str
= "99999999999999999999999999999999999999999999";
961 const char *endptr
= &f
;
965 err
= qemu_strtoll(str
, &endptr
, 0, &res
);
967 g_assert_cmpint(err
, ==, -ERANGE
);
968 g_assert_cmpint(res
, ==, LLONG_MAX
);
969 g_assert(endptr
== str
+ strlen(str
));
972 static void test_qemu_strtoll_underflow(void)
974 const char *str
= "-99999999999999999999999999999999999999999999";
976 const char *endptr
= &f
;
980 err
= qemu_strtoll(str
, &endptr
, 0, &res
);
982 g_assert_cmpint(err
, ==, -ERANGE
);
983 g_assert_cmpint(res
, ==, LLONG_MIN
);
984 g_assert(endptr
== str
+ strlen(str
));
987 static void test_qemu_strtoll_negative(void)
989 const char *str
= " \t -321";
991 const char *endptr
= &f
;
995 err
= qemu_strtoll(str
, &endptr
, 0, &res
);
997 g_assert_cmpint(err
, ==, 0);
998 g_assert_cmpint(res
, ==, -321);
999 g_assert(endptr
== str
+ strlen(str
));
1002 static void test_qemu_strtoll_full_correct(void)
1004 const char *str
= "123";
1008 err
= qemu_strtoll(str
, NULL
, 0, &res
);
1010 g_assert_cmpint(err
, ==, 0);
1011 g_assert_cmpint(res
, ==, 123);
1014 static void test_qemu_strtoll_full_null(void)
1019 err
= qemu_strtoll(NULL
, NULL
, 0, &res
);
1021 g_assert_cmpint(err
, ==, -EINVAL
);
1024 static void test_qemu_strtoll_full_empty(void)
1026 const char *str
= "";
1030 err
= qemu_strtoll(str
, NULL
, 0, &res
);
1032 g_assert_cmpint(err
, ==, -EINVAL
);
1035 static void test_qemu_strtoll_full_negative(void)
1037 const char *str
= " \t -321";
1041 err
= qemu_strtoll(str
, NULL
, 0, &res
);
1043 g_assert_cmpint(err
, ==, 0);
1044 g_assert_cmpint(res
, ==, -321);
1047 static void test_qemu_strtoll_full_trailing(void)
1049 const char *str
= "123xxx";
1053 err
= qemu_strtoll(str
, NULL
, 0, &res
);
1055 g_assert_cmpint(err
, ==, -EINVAL
);
1058 static void test_qemu_strtoll_full_max(void)
1061 const char *str
= g_strdup_printf("%lld", LLONG_MAX
);
1065 err
= qemu_strtoll(str
, NULL
, 0, &res
);
1067 g_assert_cmpint(err
, ==, 0);
1068 g_assert_cmpint(res
, ==, LLONG_MAX
);
1071 static void test_qemu_strtoull_correct(void)
1073 const char *str
= "12345 foo";
1075 const char *endptr
= &f
;
1079 err
= qemu_strtoull(str
, &endptr
, 0, &res
);
1081 g_assert_cmpint(err
, ==, 0);
1082 g_assert_cmpint(res
, ==, 12345);
1083 g_assert(endptr
== str
+ 5);
1086 static void test_qemu_strtoull_null(void)
1089 const char *endptr
= &f
;
1093 err
= qemu_strtoull(NULL
, &endptr
, 0, &res
);
1095 g_assert_cmpint(err
, ==, -EINVAL
);
1096 g_assert(endptr
== NULL
);
1099 static void test_qemu_strtoull_empty(void)
1101 const char *str
= "";
1103 const char *endptr
= &f
;
1107 err
= qemu_strtoull(str
, &endptr
, 0, &res
);
1109 g_assert_cmpint(err
, ==, -EINVAL
);
1112 static void test_qemu_strtoull_whitespace(void)
1114 const char *str
= " \t ";
1116 const char *endptr
= &f
;
1120 err
= qemu_strtoull(str
, &endptr
, 0, &res
);
1122 g_assert_cmpint(err
, ==, -EINVAL
);
1125 static void test_qemu_strtoull_invalid(void)
1127 const char *str
= " xxxx \t abc";
1129 const char *endptr
= &f
;
1133 err
= qemu_strtoull(str
, &endptr
, 0, &res
);
1135 g_assert_cmpint(err
, ==, -EINVAL
);
1138 static void test_qemu_strtoull_trailing(void)
1140 const char *str
= "123xxx";
1142 const char *endptr
= &f
;
1146 err
= qemu_strtoull(str
, &endptr
, 0, &res
);
1148 g_assert_cmpint(err
, ==, 0);
1149 g_assert_cmpint(res
, ==, 123);
1150 g_assert(endptr
== str
+ 3);
1153 static void test_qemu_strtoull_octal(void)
1155 const char *str
= "0123";
1157 const char *endptr
= &f
;
1161 err
= qemu_strtoull(str
, &endptr
, 8, &res
);
1163 g_assert_cmpint(err
, ==, 0);
1164 g_assert_cmpint(res
, ==, 0123);
1165 g_assert(endptr
== str
+ strlen(str
));
1169 err
= qemu_strtoull(str
, &endptr
, 0, &res
);
1171 g_assert_cmpint(err
, ==, 0);
1172 g_assert_cmpint(res
, ==, 0123);
1173 g_assert(endptr
== str
+ strlen(str
));
1176 static void test_qemu_strtoull_decimal(void)
1178 const char *str
= "0123";
1180 const char *endptr
= &f
;
1184 err
= qemu_strtoull(str
, &endptr
, 10, &res
);
1186 g_assert_cmpint(err
, ==, 0);
1187 g_assert_cmpint(res
, ==, 123);
1188 g_assert(endptr
== str
+ strlen(str
));
1193 err
= qemu_strtoull(str
, &endptr
, 0, &res
);
1195 g_assert_cmpint(err
, ==, 0);
1196 g_assert_cmpint(res
, ==, 123);
1197 g_assert(endptr
== str
+ strlen(str
));
1200 static void test_qemu_strtoull_hex(void)
1202 const char *str
= "0123";
1204 const char *endptr
= &f
;
1208 err
= qemu_strtoull(str
, &endptr
, 16, &res
);
1210 g_assert_cmpint(err
, ==, 0);
1211 g_assert_cmpint(res
, ==, 0x123);
1212 g_assert(endptr
== str
+ strlen(str
));
1217 err
= qemu_strtoull(str
, &endptr
, 0, &res
);
1219 g_assert_cmpint(err
, ==, 0);
1220 g_assert_cmpint(res
, ==, 0x123);
1221 g_assert(endptr
== str
+ strlen(str
));
1224 static void test_qemu_strtoull_max(void)
1226 const char *str
= g_strdup_printf("%llu", ULLONG_MAX
);
1228 const char *endptr
= &f
;
1232 err
= qemu_strtoull(str
, &endptr
, 0, &res
);
1234 g_assert_cmpint(err
, ==, 0);
1235 g_assert_cmpint(res
, ==, ULLONG_MAX
);
1236 g_assert(endptr
== str
+ strlen(str
));
1239 static void test_qemu_strtoull_overflow(void)
1241 const char *str
= "99999999999999999999999999999999999999999999";
1243 const char *endptr
= &f
;
1247 err
= qemu_strtoull(str
, &endptr
, 0, &res
);
1249 g_assert_cmpint(err
, ==, -ERANGE
);
1250 g_assert_cmpint(res
, ==, ULLONG_MAX
);
1251 g_assert(endptr
== str
+ strlen(str
));
1254 static void test_qemu_strtoull_underflow(void)
1256 const char *str
= "-99999999999999999999999999999999999999999999";
1258 const char *endptr
= &f
;
1262 err
= qemu_strtoull(str
, &endptr
, 0, &res
);
1264 g_assert_cmpint(err
, ==, -ERANGE
);
1265 g_assert_cmpint(res
, ==, -1);
1266 g_assert(endptr
== str
+ strlen(str
));
1269 static void test_qemu_strtoull_negative(void)
1271 const char *str
= " \t -321";
1273 const char *endptr
= &f
;
1277 err
= qemu_strtoull(str
, &endptr
, 0, &res
);
1279 g_assert_cmpint(err
, ==, 0);
1280 g_assert_cmpint(res
, ==, -321);
1281 g_assert(endptr
== str
+ strlen(str
));
1284 static void test_qemu_strtoull_full_correct(void)
1286 const char *str
= "18446744073709551614";
1290 err
= qemu_strtoull(str
, NULL
, 0, &res
);
1292 g_assert_cmpint(err
, ==, 0);
1293 g_assert_cmpint(res
, ==, 18446744073709551614LLU);
1296 static void test_qemu_strtoull_full_null(void)
1301 err
= qemu_strtoull(NULL
, NULL
, 0, &res
);
1303 g_assert_cmpint(err
, ==, -EINVAL
);
1306 static void test_qemu_strtoull_full_empty(void)
1308 const char *str
= "";
1312 err
= qemu_strtoull(str
, NULL
, 0, &res
);
1314 g_assert_cmpint(err
, ==, -EINVAL
);
1317 static void test_qemu_strtoull_full_negative(void)
1319 const char *str
= " \t -321";
1323 err
= qemu_strtoull(str
, NULL
, 0, &res
);
1325 g_assert_cmpint(err
, ==, 0);
1326 g_assert_cmpint(res
, ==, 18446744073709551295LLU);
1329 static void test_qemu_strtoull_full_trailing(void)
1331 const char *str
= "18446744073709551614xxxxxx";
1335 err
= qemu_strtoull(str
, NULL
, 0, &res
);
1337 g_assert_cmpint(err
, ==, -EINVAL
);
1340 static void test_qemu_strtoull_full_max(void)
1342 const char *str
= g_strdup_printf("%lld", ULLONG_MAX
);
1346 err
= qemu_strtoull(str
, NULL
, 0, &res
);
1348 g_assert_cmpint(err
, ==, 0);
1349 g_assert_cmpint(res
, ==, ULLONG_MAX
);
1352 static void test_qemu_strtosz_simple(void)
1354 const char *str
= "12345M";
1355 char *endptr
= NULL
;
1358 res
= qemu_strtosz(str
, &endptr
);
1359 g_assert_cmpint(res
, ==, 12345 * M_BYTE
);
1360 g_assert(endptr
== str
+ 6);
1362 res
= qemu_strtosz(str
, NULL
);
1363 g_assert_cmpint(res
, ==, 12345 * M_BYTE
);
1366 static void test_qemu_strtosz_units(void)
1368 const char *none
= "1";
1369 const char *b
= "1B";
1370 const char *k
= "1K";
1371 const char *m
= "1M";
1372 const char *g
= "1G";
1373 const char *t
= "1T";
1374 const char *p
= "1P";
1375 const char *e
= "1E";
1379 res
= qemu_strtosz(none
, NULL
);
1380 g_assert_cmpint(res
, ==, M_BYTE
);
1382 res
= qemu_strtosz(b
, NULL
);
1383 g_assert_cmpint(res
, ==, 1);
1385 res
= qemu_strtosz(k
, NULL
);
1386 g_assert_cmpint(res
, ==, K_BYTE
);
1388 res
= qemu_strtosz(m
, NULL
);
1389 g_assert_cmpint(res
, ==, M_BYTE
);
1391 res
= qemu_strtosz(g
, NULL
);
1392 g_assert_cmpint(res
, ==, G_BYTE
);
1394 res
= qemu_strtosz(t
, NULL
);
1395 g_assert_cmpint(res
, ==, T_BYTE
);
1397 res
= qemu_strtosz(p
, NULL
);
1398 g_assert_cmpint(res
, ==, P_BYTE
);
1400 res
= qemu_strtosz(e
, NULL
);
1401 g_assert_cmpint(res
, ==, E_BYTE
);
1404 static void test_qemu_strtosz_float(void)
1406 const char *str
= "12.345M";
1409 res
= qemu_strtosz(str
, NULL
);
1410 g_assert_cmpint(res
, ==, 12.345 * M_BYTE
);
1413 static void test_qemu_strtosz_erange(void)
1415 const char *str
= "10E";
1418 res
= qemu_strtosz(str
, NULL
);
1419 g_assert_cmpint(res
, ==, -ERANGE
);
1422 static void test_qemu_strtosz_suffix_unit(void)
1424 const char *str
= "12345";
1427 res
= qemu_strtosz_suffix_unit(str
, NULL
,
1428 QEMU_STRTOSZ_DEFSUFFIX_KB
, 1000);
1429 g_assert_cmpint(res
, ==, 12345000);
1432 int main(int argc
, char **argv
)
1434 g_test_init(&argc
, &argv
, NULL
);
1436 g_test_add_func("/cutils/parse_uint/null", test_parse_uint_null
);
1437 g_test_add_func("/cutils/parse_uint/empty", test_parse_uint_empty
);
1438 g_test_add_func("/cutils/parse_uint/whitespace",
1439 test_parse_uint_whitespace
);
1440 g_test_add_func("/cutils/parse_uint/invalid", test_parse_uint_invalid
);
1441 g_test_add_func("/cutils/parse_uint/trailing", test_parse_uint_trailing
);
1442 g_test_add_func("/cutils/parse_uint/correct", test_parse_uint_correct
);
1443 g_test_add_func("/cutils/parse_uint/octal", test_parse_uint_octal
);
1444 g_test_add_func("/cutils/parse_uint/decimal", test_parse_uint_decimal
);
1445 g_test_add_func("/cutils/parse_uint/llong_max", test_parse_uint_llong_max
);
1446 g_test_add_func("/cutils/parse_uint/overflow", test_parse_uint_overflow
);
1447 g_test_add_func("/cutils/parse_uint/negative", test_parse_uint_negative
);
1448 g_test_add_func("/cutils/parse_uint_full/trailing",
1449 test_parse_uint_full_trailing
);
1450 g_test_add_func("/cutils/parse_uint_full/correct",
1451 test_parse_uint_full_correct
);
1453 /* qemu_strtol() tests */
1454 g_test_add_func("/cutils/qemu_strtol/correct", test_qemu_strtol_correct
);
1455 g_test_add_func("/cutils/qemu_strtol/null", test_qemu_strtol_null
);
1456 g_test_add_func("/cutils/qemu_strtol/empty", test_qemu_strtol_empty
);
1457 g_test_add_func("/cutils/qemu_strtol/whitespace",
1458 test_qemu_strtol_whitespace
);
1459 g_test_add_func("/cutils/qemu_strtol/invalid", test_qemu_strtol_invalid
);
1460 g_test_add_func("/cutils/qemu_strtol/trailing", test_qemu_strtol_trailing
);
1461 g_test_add_func("/cutils/qemu_strtol/octal", test_qemu_strtol_octal
);
1462 g_test_add_func("/cutils/qemu_strtol/decimal", test_qemu_strtol_decimal
);
1463 g_test_add_func("/cutils/qemu_strtol/hex", test_qemu_strtol_hex
);
1464 g_test_add_func("/cutils/qemu_strtol/max", test_qemu_strtol_max
);
1465 g_test_add_func("/cutils/qemu_strtol/overflow", test_qemu_strtol_overflow
);
1466 g_test_add_func("/cutils/qemu_strtol/underflow",
1467 test_qemu_strtol_underflow
);
1468 g_test_add_func("/cutils/qemu_strtol/negative", test_qemu_strtol_negative
);
1469 g_test_add_func("/cutils/qemu_strtol_full/correct",
1470 test_qemu_strtol_full_correct
);
1471 g_test_add_func("/cutils/qemu_strtol_full/null",
1472 test_qemu_strtol_full_null
);
1473 g_test_add_func("/cutils/qemu_strtol_full/empty",
1474 test_qemu_strtol_full_empty
);
1475 g_test_add_func("/cutils/qemu_strtol_full/negative",
1476 test_qemu_strtol_full_negative
);
1477 g_test_add_func("/cutils/qemu_strtol_full/trailing",
1478 test_qemu_strtol_full_trailing
);
1479 g_test_add_func("/cutils/qemu_strtol_full/max",
1480 test_qemu_strtol_full_max
);
1482 /* qemu_strtoul() tests */
1483 g_test_add_func("/cutils/qemu_strtoul/correct", test_qemu_strtoul_correct
);
1484 g_test_add_func("/cutils/qemu_strtoul/null", test_qemu_strtoul_null
);
1485 g_test_add_func("/cutils/qemu_strtoul/empty", test_qemu_strtoul_empty
);
1486 g_test_add_func("/cutils/qemu_strtoul/whitespace",
1487 test_qemu_strtoul_whitespace
);
1488 g_test_add_func("/cutils/qemu_strtoul/invalid", test_qemu_strtoul_invalid
);
1489 g_test_add_func("/cutils/qemu_strtoul/trailing",
1490 test_qemu_strtoul_trailing
);
1491 g_test_add_func("/cutils/qemu_strtoul/octal", test_qemu_strtoul_octal
);
1492 g_test_add_func("/cutils/qemu_strtoul/decimal", test_qemu_strtoul_decimal
);
1493 g_test_add_func("/cutils/qemu_strtoul/hex", test_qemu_strtoul_hex
);
1494 g_test_add_func("/cutils/qemu_strtoul/max", test_qemu_strtoul_max
);
1495 g_test_add_func("/cutils/qemu_strtoul/overflow",
1496 test_qemu_strtoul_overflow
);
1497 g_test_add_func("/cutils/qemu_strtoul/underflow",
1498 test_qemu_strtoul_underflow
);
1499 g_test_add_func("/cutils/qemu_strtoul/negative",
1500 test_qemu_strtoul_negative
);
1501 g_test_add_func("/cutils/qemu_strtoul_full/correct",
1502 test_qemu_strtoul_full_correct
);
1503 g_test_add_func("/cutils/qemu_strtoul_full/null",
1504 test_qemu_strtoul_full_null
);
1505 g_test_add_func("/cutils/qemu_strtoul_full/empty",
1506 test_qemu_strtoul_full_empty
);
1507 g_test_add_func("/cutils/qemu_strtoul_full/negative",
1508 test_qemu_strtoul_full_negative
);
1509 g_test_add_func("/cutils/qemu_strtoul_full/trailing",
1510 test_qemu_strtoul_full_trailing
);
1511 g_test_add_func("/cutils/qemu_strtoul_full/max",
1512 test_qemu_strtoul_full_max
);
1514 /* qemu_strtoll() tests */
1515 g_test_add_func("/cutils/qemu_strtoll/correct", test_qemu_strtoll_correct
);
1516 g_test_add_func("/cutils/qemu_strtoll/null", test_qemu_strtoll_null
);
1517 g_test_add_func("/cutils/qemu_strtoll/empty", test_qemu_strtoll_empty
);
1518 g_test_add_func("/cutils/qemu_strtoll/whitespace",
1519 test_qemu_strtoll_whitespace
);
1520 g_test_add_func("/cutils/qemu_strtoll/invalid", test_qemu_strtoll_invalid
);
1521 g_test_add_func("/cutils/qemu_strtoll/trailing",
1522 test_qemu_strtoll_trailing
);
1523 g_test_add_func("/cutils/qemu_strtoll/octal", test_qemu_strtoll_octal
);
1524 g_test_add_func("/cutils/qemu_strtoll/decimal", test_qemu_strtoll_decimal
);
1525 g_test_add_func("/cutils/qemu_strtoll/hex", test_qemu_strtoll_hex
);
1526 g_test_add_func("/cutils/qemu_strtoll/max", test_qemu_strtoll_max
);
1527 g_test_add_func("/cutils/qemu_strtoll/overflow",
1528 test_qemu_strtoll_overflow
);
1529 g_test_add_func("/cutils/qemu_strtoll/underflow",
1530 test_qemu_strtoll_underflow
);
1531 g_test_add_func("/cutils/qemu_strtoll/negative",
1532 test_qemu_strtoll_negative
);
1533 g_test_add_func("/cutils/qemu_strtoll_full/correct",
1534 test_qemu_strtoll_full_correct
);
1535 g_test_add_func("/cutils/qemu_strtoll_full/null",
1536 test_qemu_strtoll_full_null
);
1537 g_test_add_func("/cutils/qemu_strtoll_full/empty",
1538 test_qemu_strtoll_full_empty
);
1539 g_test_add_func("/cutils/qemu_strtoll_full/negative",
1540 test_qemu_strtoll_full_negative
);
1541 g_test_add_func("/cutils/qemu_strtoll_full/trailing",
1542 test_qemu_strtoll_full_trailing
);
1543 g_test_add_func("/cutils/qemu_strtoll_full/max",
1544 test_qemu_strtoll_full_max
);
1546 /* qemu_strtoull() tests */
1547 g_test_add_func("/cutils/qemu_strtoull/correct",
1548 test_qemu_strtoull_correct
);
1549 g_test_add_func("/cutils/qemu_strtoull/null",
1550 test_qemu_strtoull_null
);
1551 g_test_add_func("/cutils/qemu_strtoull/empty", test_qemu_strtoull_empty
);
1552 g_test_add_func("/cutils/qemu_strtoull/whitespace",
1553 test_qemu_strtoull_whitespace
);
1554 g_test_add_func("/cutils/qemu_strtoull/invalid",
1555 test_qemu_strtoull_invalid
);
1556 g_test_add_func("/cutils/qemu_strtoull/trailing",
1557 test_qemu_strtoull_trailing
);
1558 g_test_add_func("/cutils/qemu_strtoull/octal", test_qemu_strtoull_octal
);
1559 g_test_add_func("/cutils/qemu_strtoull/decimal",
1560 test_qemu_strtoull_decimal
);
1561 g_test_add_func("/cutils/qemu_strtoull/hex", test_qemu_strtoull_hex
);
1562 g_test_add_func("/cutils/qemu_strtoull/max", test_qemu_strtoull_max
);
1563 g_test_add_func("/cutils/qemu_strtoull/overflow",
1564 test_qemu_strtoull_overflow
);
1565 g_test_add_func("/cutils/qemu_strtoull/underflow",
1566 test_qemu_strtoull_underflow
);
1567 g_test_add_func("/cutils/qemu_strtoull/negative",
1568 test_qemu_strtoull_negative
);
1569 g_test_add_func("/cutils/qemu_strtoull_full/correct",
1570 test_qemu_strtoull_full_correct
);
1571 g_test_add_func("/cutils/qemu_strtoull_full/null",
1572 test_qemu_strtoull_full_null
);
1573 g_test_add_func("/cutils/qemu_strtoull_full/empty",
1574 test_qemu_strtoull_full_empty
);
1575 g_test_add_func("/cutils/qemu_strtoull_full/negative",
1576 test_qemu_strtoull_full_negative
);
1577 g_test_add_func("/cutils/qemu_strtoull_full/trailing",
1578 test_qemu_strtoull_full_trailing
);
1579 g_test_add_func("/cutils/qemu_strtoull_full/max",
1580 test_qemu_strtoull_full_max
);
1582 g_test_add_func("/cutils/strtosz/simple",
1583 test_qemu_strtosz_simple
);
1584 g_test_add_func("/cutils/strtosz/units",
1585 test_qemu_strtosz_units
);
1586 g_test_add_func("/cutils/strtosz/float",
1587 test_qemu_strtosz_float
);
1588 g_test_add_func("/cutils/strtosz/erange",
1589 test_qemu_strtosz_erange
);
1590 g_test_add_func("/cutils/strtosz/suffix-unit",
1591 test_qemu_strtosz_suffix_unit
);
1593 return g_test_run();