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
30 #include "qemu/osdep.h"
31 #include "qemu/cutils.h"
32 #include "qemu/units.h"
34 static void test_parse_uint_null(void)
37 const char *endptr
= "somewhere";
40 r
= parse_uint(NULL
, &endptr
, 0, &i
);
42 g_assert_cmpint(r
, ==, -EINVAL
);
43 g_assert_cmpuint(i
, ==, 0);
44 g_assert_null(endptr
);
47 static void test_parse_uint_empty(void)
50 const char *endptr
= "somewhere";
54 r
= parse_uint(str
, &endptr
, 0, &i
);
56 g_assert_cmpint(r
, ==, -EINVAL
);
57 g_assert_cmpuint(i
, ==, 0);
58 g_assert_true(endptr
== str
);
61 static void test_parse_uint_whitespace(void)
64 const char *endptr
= "somewhere";
65 const char *str
= " \t ";
68 r
= parse_uint(str
, &endptr
, 0, &i
);
70 g_assert_cmpint(r
, ==, -EINVAL
);
71 g_assert_cmpuint(i
, ==, 0);
72 g_assert_true(endptr
== str
);
76 static void test_parse_uint_invalid(void)
79 const char *endptr
= "somewhere";
80 const char *str
= " \t xxx";
83 r
= parse_uint(str
, &endptr
, 0, &i
);
85 g_assert_cmpint(r
, ==, -EINVAL
);
86 g_assert_cmpuint(i
, ==, 0);
87 g_assert_true(endptr
== str
);
91 static void test_parse_uint_trailing(void)
94 const char *endptr
= "somewhere";
95 const char *str
= "123xxx";
98 r
= parse_uint(str
, &endptr
, 0, &i
);
100 g_assert_cmpint(r
, ==, 0);
101 g_assert_cmpuint(i
, ==, 123);
102 g_assert_true(endptr
== str
+ 3);
105 static void test_parse_uint_correct(void)
108 const char *endptr
= "somewhere";
109 const char *str
= "123";
112 r
= parse_uint(str
, &endptr
, 0, &i
);
114 g_assert_cmpint(r
, ==, 0);
115 g_assert_cmpuint(i
, ==, 123);
116 g_assert_true(endptr
== str
+ strlen(str
));
119 static void test_parse_uint_octal(void)
122 const char *endptr
= "somewhere";
123 const char *str
= "0123";
126 r
= parse_uint(str
, &endptr
, 0, &i
);
128 g_assert_cmpint(r
, ==, 0);
129 g_assert_cmpuint(i
, ==, 0123);
130 g_assert_true(endptr
== str
+ strlen(str
));
133 static void test_parse_uint_decimal(void)
136 const char *endptr
= "somewhere";
137 const char *str
= "0123";
140 r
= parse_uint(str
, &endptr
, 10, &i
);
142 g_assert_cmpint(r
, ==, 0);
143 g_assert_cmpuint(i
, ==, 123);
144 g_assert_true(endptr
== str
+ strlen(str
));
147 static void test_parse_uint_llong_max(void)
150 const char *endptr
= "somewhere";
151 char *str
= g_strdup_printf("%llu", (unsigned long long)LLONG_MAX
+ 1);
154 r
= parse_uint(str
, &endptr
, 0, &i
);
156 g_assert_cmpint(r
, ==, 0);
157 g_assert_cmpuint(i
, ==, (unsigned long long)LLONG_MAX
+ 1);
158 g_assert_true(endptr
== str
+ strlen(str
));
163 static void test_parse_uint_max(void)
166 const char *endptr
= "somewhere";
167 char *str
= g_strdup_printf("%llu", ULLONG_MAX
);
170 r
= parse_uint(str
, &endptr
, 0, &i
);
172 g_assert_cmpint(r
, ==, 0);
173 g_assert_cmpuint(i
, ==, ULLONG_MAX
);
174 g_assert_true(endptr
== str
+ strlen(str
));
179 static void test_parse_uint_overflow(void)
187 endptr
= "somewhere";
188 str
= "99999999999999999999999999999999999999";
189 r
= parse_uint(str
, &endptr
, 0, &i
);
190 g_assert_cmpint(r
, ==, -ERANGE
);
191 g_assert_cmpuint(i
, ==, ULLONG_MAX
);
192 g_assert_true(endptr
== str
+ strlen(str
));
195 endptr
= "somewhere";
196 str
= "0x10000000000000000"; /* 65 bits, 64-bit sign bit clear */
197 r
= parse_uint(str
, &endptr
, 0, &i
);
198 g_assert_cmpint(r
, ==, -ERANGE
);
199 g_assert_cmpuint(i
, ==, ULLONG_MAX
);
200 g_assert_true(endptr
== str
+ strlen(str
));
203 endptr
= "somewhere";
204 str
= "0x18000000080000000"; /* 65 bits, 64-bit sign bit set */
205 r
= parse_uint(str
, &endptr
, 0, &i
);
206 g_assert_cmpint(r
, ==, -ERANGE
);
207 g_assert_cmpuint(i
, ==, ULLONG_MAX
);
208 g_assert_true(endptr
== str
+ strlen(str
));
211 static void test_parse_uint_negative(void)
219 endptr
= "somewhere";
221 r
= parse_uint(str
, &endptr
, 0, &i
);
222 g_assert_cmpint(r
, ==, -ERANGE
);
223 g_assert_cmpuint(i
, ==, 0);
224 g_assert_true(endptr
== str
+ strlen(str
));
227 endptr
= "somewhere";
228 str
= "-0xffffffff00000001";
229 r
= parse_uint(str
, &endptr
, 0, &i
);
230 g_assert_cmpint(r
, ==, -ERANGE
);
231 g_assert_cmpuint(i
, ==, 0);
232 g_assert_true(endptr
== str
+ strlen(str
));
235 static void test_parse_uint_negzero(void)
238 const char *endptr
= "somewhere";
239 const char *str
= " -0";
242 r
= parse_uint(str
, &endptr
, 0, &i
);
244 g_assert_cmpint(r
, ==, -ERANGE
);
245 g_assert_cmpuint(i
, ==, 0);
246 g_assert_true(endptr
== str
+ strlen(str
));
249 static void test_parse_uint_full_trailing(void)
252 const char *str
= "123xxx";
255 r
= parse_uint_full(str
, 0, &i
);
257 g_assert_cmpint(r
, ==, -EINVAL
);
258 g_assert_cmpuint(i
, ==, 0);
261 static void test_parse_uint_full_correct(void)
264 const char *str
= "123";
267 r
= parse_uint_full(str
, 0, &i
);
269 g_assert_cmpint(r
, ==, 0);
270 g_assert_cmpuint(i
, ==, 123);
273 static void test_parse_uint_full_erange_junk(void)
275 /* EINVAL has priority over ERANGE */
277 const char *str
= "-2junk";
280 r
= parse_uint_full(str
, 0, &i
);
282 g_assert_cmpint(r
, ==, -EINVAL
);
283 g_assert_cmpuint(i
, ==, 0);
286 static void test_parse_uint_full_null(void)
289 const char *str
= NULL
;
292 r
= parse_uint_full(str
, 0, &i
);
294 g_assert_cmpint(r
, ==, -EINVAL
);
295 g_assert_cmpuint(i
, ==, 0);
298 static void test_qemu_strtoi_correct(void)
300 const char *str
= "12345 foo";
302 const char *endptr
= &f
;
306 err
= qemu_strtoi(str
, &endptr
, 0, &res
);
308 g_assert_cmpint(err
, ==, 0);
309 g_assert_cmpint(res
, ==, 12345);
310 g_assert_true(endptr
== str
+ 5);
313 static void test_qemu_strtoi_null(void)
316 const char *endptr
= &f
;
320 err
= qemu_strtoi(NULL
, &endptr
, 0, &res
);
322 g_assert_cmpint(err
, ==, -EINVAL
);
323 g_assert_cmpint(res
, ==, 999);
324 g_assert_null(endptr
);
327 static void test_qemu_strtoi_empty(void)
329 const char *str
= "";
331 const char *endptr
= &f
;
335 err
= qemu_strtoi(str
, &endptr
, 0, &res
);
337 g_assert_cmpint(err
, ==, -EINVAL
);
338 g_assert_cmpint(res
, ==, 0);
339 g_assert_true(endptr
== str
);
342 static void test_qemu_strtoi_whitespace(void)
344 const char *str
= " \t ";
346 const char *endptr
= &f
;
350 err
= qemu_strtoi(str
, &endptr
, 0, &res
);
352 g_assert_cmpint(err
, ==, -EINVAL
);
353 g_assert_cmpint(res
, ==, 0);
354 g_assert_true(endptr
== str
);
357 static void test_qemu_strtoi_invalid(void)
359 const char *str
= " xxxx \t abc";
361 const char *endptr
= &f
;
365 err
= qemu_strtoi(str
, &endptr
, 0, &res
);
367 g_assert_cmpint(err
, ==, -EINVAL
);
368 g_assert_cmpint(res
, ==, 0);
369 g_assert_true(endptr
== str
);
372 static void test_qemu_strtoi_trailing(void)
374 const char *str
= "123xxx";
376 const char *endptr
= &f
;
380 err
= qemu_strtoi(str
, &endptr
, 0, &res
);
382 g_assert_cmpint(err
, ==, 0);
383 g_assert_cmpint(res
, ==, 123);
384 g_assert_true(endptr
== str
+ 3);
387 static void test_qemu_strtoi_octal(void)
389 const char *str
= "0123";
391 const char *endptr
= &f
;
395 err
= qemu_strtoi(str
, &endptr
, 8, &res
);
397 g_assert_cmpint(err
, ==, 0);
398 g_assert_cmpint(res
, ==, 0123);
399 g_assert_true(endptr
== str
+ strlen(str
));
403 err
= qemu_strtoi(str
, &endptr
, 0, &res
);
405 g_assert_cmpint(err
, ==, 0);
406 g_assert_cmpint(res
, ==, 0123);
407 g_assert_true(endptr
== str
+ strlen(str
));
410 static void test_qemu_strtoi_decimal(void)
412 const char *str
= "0123";
414 const char *endptr
= &f
;
418 err
= qemu_strtoi(str
, &endptr
, 10, &res
);
420 g_assert_cmpint(err
, ==, 0);
421 g_assert_cmpint(res
, ==, 123);
422 g_assert_true(endptr
== str
+ strlen(str
));
427 err
= qemu_strtoi(str
, &endptr
, 0, &res
);
429 g_assert_cmpint(err
, ==, 0);
430 g_assert_cmpint(res
, ==, 123);
431 g_assert_true(endptr
== str
+ strlen(str
));
434 static void test_qemu_strtoi_hex(void)
436 const char *str
= "0123";
438 const char *endptr
= &f
;
442 err
= qemu_strtoi(str
, &endptr
, 16, &res
);
444 g_assert_cmpint(err
, ==, 0);
445 g_assert_cmpint(res
, ==, 0x123);
446 g_assert_true(endptr
== str
+ strlen(str
));
451 err
= qemu_strtoi(str
, &endptr
, 0, &res
);
453 g_assert_cmpint(err
, ==, 0);
454 g_assert_cmpint(res
, ==, 0x123);
455 g_assert_true(endptr
== str
+ strlen(str
));
460 err
= qemu_strtoi(str
, &endptr
, 16, &res
);
462 g_assert_cmpint(err
, ==, 0);
463 g_assert_cmpint(res
, ==, 0);
464 g_assert_true(endptr
== str
+ 1);
467 static void test_qemu_strtoi_max(void)
469 char *str
= g_strdup_printf("%d", INT_MAX
);
471 const char *endptr
= &f
;
475 err
= qemu_strtoi(str
, &endptr
, 0, &res
);
477 g_assert_cmpint(err
, ==, 0);
478 g_assert_cmpint(res
, ==, INT_MAX
);
479 g_assert_true(endptr
== str
+ strlen(str
));
483 static void test_qemu_strtoi_overflow(void)
490 str
= "2147483648"; /* INT_MAX + 1ll */
491 endptr
= "somewhere";
493 err
= qemu_strtoi(str
, &endptr
, 0, &res
);
494 g_assert_cmpint(err
, ==, -ERANGE
);
495 g_assert_cmpint(res
, ==, INT_MAX
);
496 g_assert_true(endptr
== str
+ strlen(str
));
498 str
= "0x7fffffffffffffff"; /* LLONG_MAX */
499 endptr
= "somewhere";
501 err
= qemu_strtoi(str
, &endptr
, 0, &res
);
502 g_assert_cmpint(err
, ==, -ERANGE
);
503 g_assert_cmpint(res
, ==, INT_MAX
);
504 g_assert_true(endptr
== str
+ strlen(str
));
506 str
= "0x8000000000000000"; /* (uint64_t)LLONG_MIN */
507 endptr
= "somewhere";
509 err
= qemu_strtoi(str
, &endptr
, 0, &res
);
510 g_assert_cmpint(err
, ==, -ERANGE
);
511 g_assert_cmpint(res
, ==, INT_MAX
);
512 g_assert_true(endptr
== str
+ strlen(str
));
514 str
= "0x10000000000000000"; /* 65 bits, 32-bit sign bit clear */
515 endptr
= "somewhere";
517 err
= qemu_strtoi(str
, &endptr
, 0, &res
);
518 g_assert_cmpint(err
, ==, -ERANGE
);
519 g_assert_cmpint(res
, ==, INT_MAX
);
520 g_assert_true(endptr
== str
+ strlen(str
));
522 str
= "0x18000000080000000"; /* 65 bits, 32-bit sign bit set */
523 endptr
= "somewhere";
525 err
= qemu_strtoi(str
, &endptr
, 0, &res
);
526 g_assert_cmpint(err
, ==, -ERANGE
);
527 g_assert_cmpint(res
, ==, INT_MAX
);
528 g_assert_true(endptr
== str
+ strlen(str
));
531 static void test_qemu_strtoi_min(void)
533 char *str
= g_strdup_printf("%d", INT_MIN
);
535 const char *endptr
= &f
;
539 err
= qemu_strtoi(str
, &endptr
, 0, &res
);
541 g_assert_cmpint(err
, ==, 0);
542 g_assert_cmpint(res
, ==, INT_MIN
);
543 g_assert_true(endptr
== str
+ strlen(str
));
547 static void test_qemu_strtoi_underflow(void)
554 str
= "-2147483649"; /* INT_MIN - 1ll */
555 endptr
= "somewhere";
557 err
= qemu_strtoi(str
, &endptr
, 0, &res
);
558 g_assert_cmpint(err
, ==, -ERANGE
);
559 g_assert_cmpint(res
, ==, INT_MIN
);
560 g_assert_true(endptr
== str
+ strlen(str
));
562 str
= "-0x7fffffffffffffff"; /* -LLONG_MAX */
563 endptr
= "somewhere";
565 err
= qemu_strtoi(str
, &endptr
, 0, &res
);
566 g_assert_cmpint(err
, ==, -ERANGE
);
567 g_assert_cmpint(res
, ==, INT_MIN
);
568 g_assert_true(endptr
== str
+ strlen(str
));
570 str
= "-0x8000000000000000"; /* (uint64_t)LLONG_MIN */
571 endptr
= "somewhere";
573 err
= qemu_strtoi(str
, &endptr
, 0, &res
);
574 g_assert_cmpint(err
, ==, -ERANGE
);
575 g_assert_cmpint(res
, ==, INT_MIN
);
576 g_assert_true(endptr
== str
+ strlen(str
));
578 str
= "-18446744073709551615"; /* -UINT64_MAX (not 1) */
579 endptr
= "somewhere";
581 err
= qemu_strtoi(str
, &endptr
, 0, &res
);
582 g_assert_cmpint(err
, ==, -ERANGE
);
583 g_assert_cmpint(res
, ==, INT_MIN
);
584 g_assert_true(endptr
== str
+ strlen(str
));
586 str
= "-0x10000000000000000"; /* 65 bits, 32-bit sign bit clear */
587 endptr
= "somewhere";
589 err
= qemu_strtoi(str
, &endptr
, 0, &res
);
590 g_assert_cmpint(err
, ==, -ERANGE
);
591 g_assert_cmpint(res
, ==, INT_MIN
);
592 g_assert_true(endptr
== str
+ strlen(str
));
594 str
= "-0x18000000080000000"; /* 65 bits, 32-bit sign bit set */
595 endptr
= "somewhere";
597 err
= qemu_strtoi(str
, &endptr
, 0, &res
);
598 g_assert_cmpint(err
, ==, -ERANGE
);
599 g_assert_cmpint(res
, ==, INT_MIN
);
600 g_assert_true(endptr
== str
+ strlen(str
));
603 static void test_qemu_strtoi_negative(void)
611 endptr
= "somewhere";
613 err
= qemu_strtoi(str
, &endptr
, 0, &res
);
614 g_assert_cmpint(err
, ==, 0);
615 g_assert_cmpint(res
, ==, -321);
616 g_assert_true(endptr
== str
+ strlen(str
));
618 str
= "-2147483648"; /* INT_MIN */
619 endptr
= "somewhere";
621 err
= qemu_strtoi(str
, &endptr
, 0, &res
);
622 g_assert_cmpint(err
, ==, 0);
623 g_assert_cmpint(res
, ==, INT_MIN
);
624 g_assert_true(endptr
== str
+ strlen(str
));
627 static void test_qemu_strtoi_negzero(void)
629 const char *str
= " -0";
631 const char *endptr
= &f
;
635 err
= qemu_strtoi(str
, &endptr
, 0, &res
);
637 g_assert_cmpint(err
, ==, 0);
638 g_assert_cmpint(res
, ==, 0);
639 g_assert_true(endptr
== str
+ strlen(str
));
642 static void test_qemu_strtoi_full_correct(void)
644 const char *str
= "123";
648 err
= qemu_strtoi(str
, NULL
, 0, &res
);
650 g_assert_cmpint(err
, ==, 0);
651 g_assert_cmpint(res
, ==, 123);
654 static void test_qemu_strtoi_full_null(void)
657 const char *endptr
= &f
;
661 err
= qemu_strtoi(NULL
, &endptr
, 0, &res
);
663 g_assert_cmpint(err
, ==, -EINVAL
);
664 g_assert_cmpint(res
, ==, 999);
665 g_assert_null(endptr
);
668 static void test_qemu_strtoi_full_empty(void)
670 const char *str
= "";
674 err
= qemu_strtoi(str
, NULL
, 0, &res
);
676 g_assert_cmpint(err
, ==, -EINVAL
);
677 g_assert_cmpint(res
, ==, 0);
680 static void test_qemu_strtoi_full_negative(void)
682 const char *str
= " \t -321";
686 err
= qemu_strtoi(str
, NULL
, 0, &res
);
688 g_assert_cmpint(err
, ==, 0);
689 g_assert_cmpint(res
, ==, -321);
692 static void test_qemu_strtoi_full_negzero(void)
694 const char *str
= " -0";
698 err
= qemu_strtoi(str
, NULL
, 0, &res
);
700 g_assert_cmpint(err
, ==, 0);
701 g_assert_cmpint(res
, ==, 0);
704 static void test_qemu_strtoi_full_trailing(void)
706 const char *str
= "123xxx";
710 err
= qemu_strtoi(str
, NULL
, 0, &res
);
712 g_assert_cmpint(err
, ==, -EINVAL
);
713 g_assert_cmpint(res
, ==, 123);
716 static void test_qemu_strtoi_full_max(void)
718 char *str
= g_strdup_printf("%d", INT_MAX
);
722 err
= qemu_strtoi(str
, NULL
, 0, &res
);
724 g_assert_cmpint(err
, ==, 0);
725 g_assert_cmpint(res
, ==, INT_MAX
);
729 static void test_qemu_strtoi_full_erange_junk(void)
731 /* EINVAL has priority over ERANGE */
732 const char *str
= "-9999999999junk";
736 err
= qemu_strtoi(str
, NULL
, 0, &res
);
738 g_assert_cmpint(err
, ==, -EINVAL
);
739 g_assert_cmpint(res
, ==, INT_MIN
);
742 static void test_qemu_strtoui_correct(void)
744 const char *str
= "12345 foo";
746 const char *endptr
= &f
;
747 unsigned int res
= 999;
750 err
= qemu_strtoui(str
, &endptr
, 0, &res
);
752 g_assert_cmpint(err
, ==, 0);
753 g_assert_cmpuint(res
, ==, 12345);
754 g_assert_true(endptr
== str
+ 5);
757 static void test_qemu_strtoui_null(void)
760 const char *endptr
= &f
;
761 unsigned int res
= 999;
764 err
= qemu_strtoui(NULL
, &endptr
, 0, &res
);
766 g_assert_cmpint(err
, ==, -EINVAL
);
767 g_assert_cmpuint(res
, ==, 999);
768 g_assert_null(endptr
);
771 static void test_qemu_strtoui_empty(void)
773 const char *str
= "";
775 const char *endptr
= &f
;
776 unsigned int res
= 999;
779 err
= qemu_strtoui(str
, &endptr
, 0, &res
);
781 g_assert_cmpint(err
, ==, -EINVAL
);
782 g_assert_cmpuint(res
, ==, 0);
783 g_assert_true(endptr
== str
);
786 static void test_qemu_strtoui_whitespace(void)
788 const char *str
= " \t ";
790 const char *endptr
= &f
;
791 unsigned int res
= 999;
794 err
= qemu_strtoui(str
, &endptr
, 0, &res
);
796 g_assert_cmpint(err
, ==, -EINVAL
);
797 g_assert_cmpuint(res
, ==, 0);
798 g_assert_true(endptr
== str
);
801 static void test_qemu_strtoui_invalid(void)
803 const char *str
= " xxxx \t abc";
805 const char *endptr
= &f
;
806 unsigned int res
= 999;
809 err
= qemu_strtoui(str
, &endptr
, 0, &res
);
811 g_assert_cmpint(err
, ==, -EINVAL
);
812 g_assert_cmpuint(res
, ==, 0);
813 g_assert_true(endptr
== str
);
816 static void test_qemu_strtoui_trailing(void)
818 const char *str
= "123xxx";
820 const char *endptr
= &f
;
821 unsigned int res
= 999;
824 err
= qemu_strtoui(str
, &endptr
, 0, &res
);
826 g_assert_cmpint(err
, ==, 0);
827 g_assert_cmpuint(res
, ==, 123);
828 g_assert_true(endptr
== str
+ 3);
831 static void test_qemu_strtoui_octal(void)
833 const char *str
= "0123";
835 const char *endptr
= &f
;
836 unsigned int res
= 999;
839 err
= qemu_strtoui(str
, &endptr
, 8, &res
);
841 g_assert_cmpint(err
, ==, 0);
842 g_assert_cmpuint(res
, ==, 0123);
843 g_assert_true(endptr
== str
+ strlen(str
));
847 err
= qemu_strtoui(str
, &endptr
, 0, &res
);
849 g_assert_cmpint(err
, ==, 0);
850 g_assert_cmpuint(res
, ==, 0123);
851 g_assert_true(endptr
== str
+ strlen(str
));
854 static void test_qemu_strtoui_decimal(void)
856 const char *str
= "0123";
858 const char *endptr
= &f
;
859 unsigned int res
= 999;
862 err
= qemu_strtoui(str
, &endptr
, 10, &res
);
864 g_assert_cmpint(err
, ==, 0);
865 g_assert_cmpuint(res
, ==, 123);
866 g_assert_true(endptr
== str
+ strlen(str
));
871 err
= qemu_strtoui(str
, &endptr
, 0, &res
);
873 g_assert_cmpint(err
, ==, 0);
874 g_assert_cmpuint(res
, ==, 123);
875 g_assert_true(endptr
== str
+ strlen(str
));
878 static void test_qemu_strtoui_hex(void)
880 const char *str
= "0123";
882 const char *endptr
= &f
;
883 unsigned int res
= 999;
886 err
= qemu_strtoui(str
, &endptr
, 16, &res
);
888 g_assert_cmpint(err
, ==, 0);
889 g_assert_cmphex(res
, ==, 0x123);
890 g_assert_true(endptr
== str
+ strlen(str
));
895 err
= qemu_strtoui(str
, &endptr
, 0, &res
);
897 g_assert_cmpint(err
, ==, 0);
898 g_assert_cmphex(res
, ==, 0x123);
899 g_assert_true(endptr
== str
+ strlen(str
));
904 err
= qemu_strtoui(str
, &endptr
, 16, &res
);
906 g_assert_cmpint(err
, ==, 0);
907 g_assert_cmphex(res
, ==, 0);
908 g_assert_true(endptr
== str
+ 1);
911 static void test_qemu_strtoui_wrap(void)
913 /* wraparound is consistent with 32-bit strtoul */
914 const char *str
= "-4294967295"; /* 1 mod 2^32 */
916 const char *endptr
= &f
;
917 unsigned int res
= 999;
920 err
= qemu_strtoui(str
, &endptr
, 0, &res
);
922 g_assert_cmpint(err
, ==, 0);
923 g_assert_cmphex(res
, ==, 1);
924 g_assert_true(endptr
== str
+ strlen(str
));
927 static void test_qemu_strtoui_max(void)
929 char *str
= g_strdup_printf("%u", UINT_MAX
);
931 const char *endptr
= &f
;
932 unsigned int res
= 999;
935 err
= qemu_strtoui(str
, &endptr
, 0, &res
);
937 g_assert_cmpint(err
, ==, 0);
938 g_assert_cmphex(res
, ==, UINT_MAX
);
939 g_assert_true(endptr
== str
+ strlen(str
));
943 static void test_qemu_strtoui_overflow(void)
950 str
= "4294967296"; /* UINT_MAX + 1ll */
951 endptr
= "somewhere";
953 err
= qemu_strtoui(str
, &endptr
, 0, &res
);
954 g_assert_cmpint(err
, ==, -ERANGE
);
955 g_assert_cmpuint(res
, ==, UINT_MAX
);
956 g_assert_true(endptr
== str
+ strlen(str
));
958 str
= "0x7fffffffffffffff"; /* LLONG_MAX */
959 endptr
= "somewhere";
961 err
= qemu_strtoui(str
, &endptr
, 0, &res
);
962 g_assert_cmpint(err
, ==, -ERANGE
);
963 g_assert_cmpuint(res
, ==, UINT_MAX
);
964 g_assert_true(endptr
== str
+ strlen(str
));
966 str
= "0x8000000000000000"; /* (uint64_t)LLONG_MIN */
967 endptr
= "somewhere";
969 err
= qemu_strtoui(str
, &endptr
, 0, &res
);
970 g_assert_cmpint(err
, ==, -ERANGE
);
971 g_assert_cmpuint(res
, ==, UINT_MAX
);
972 g_assert_true(endptr
== str
+ strlen(str
));
974 str
= "0xffffffff00000001"; /* ULLONG_MAX - UINT_MAX + 1 (not 1) */
975 endptr
= "somewhere";
977 err
= qemu_strtoui(str
, &endptr
, 0, &res
);
978 g_assert_cmpint(err
, ==, -ERANGE
);
979 g_assert_cmpuint(res
, ==, UINT_MAX
);
980 g_assert_true(endptr
== str
+ strlen(str
));
982 str
= "0xfffffffffffffffe"; /* ULLONG_MAX - 1 (not UINT_MAX - 1) */
983 endptr
= "somewhere";
985 err
= qemu_strtoui(str
, &endptr
, 0, &res
);
986 g_assert_cmpint(err
, ==, -ERANGE
);
987 g_assert_cmpuint(res
, ==, UINT_MAX
);
988 g_assert_true(endptr
== str
+ strlen(str
));
990 str
= "0x10000000000000000"; /* 65 bits, 32-bit sign bit clear */
991 endptr
= "somewhere";
993 err
= qemu_strtoui(str
, &endptr
, 0, &res
);
994 g_assert_cmpint(err
, ==, -ERANGE
);
995 g_assert_cmpuint(res
, ==, UINT_MAX
);
996 g_assert_true(endptr
== str
+ strlen(str
));
998 str
= "0x18000000080000000"; /* 65 bits, 32-bit sign bit set */
999 endptr
= "somewhere";
1001 err
= qemu_strtoui(str
, &endptr
, 0, &res
);
1002 g_assert_cmpint(err
, ==, -ERANGE
);
1003 g_assert_cmpuint(res
, ==, UINT_MAX
);
1004 g_assert_true(endptr
== str
+ strlen(str
));
1007 static void test_qemu_strtoui_underflow(void)
1014 str
= "-4294967296"; /* -(long long)UINT_MAX - 1ll */
1015 endptr
= "somewhere";
1017 err
= qemu_strtoui(str
, &endptr
, 0, &res
);
1018 g_assert_cmpint(err
, ==, -ERANGE
);
1019 g_assert_cmpuint(res
, ==, UINT_MAX
);
1020 g_assert_true(endptr
== str
+ strlen(str
));
1022 str
= "-18446744073709551615"; /* -UINT64_MAX (not -(-1)) */
1023 endptr
= "somewhere";
1025 err
= qemu_strtoui(str
, &endptr
, 0, &res
);
1026 g_assert_cmpint(err
, ==, -ERANGE
);
1027 g_assert_cmpuint(res
, ==, UINT_MAX
);
1028 g_assert_true(endptr
== str
+ strlen(str
));
1030 str
= "-0xffffffff00000002";
1031 endptr
= "somewhere";
1033 err
= qemu_strtoui(str
, &endptr
, 0, &res
);
1034 g_assert_cmpint(err
, ==, -ERANGE
);
1035 g_assert_cmpuint(res
, ==, UINT_MAX
);
1036 g_assert_true(endptr
== str
+ strlen(str
));
1038 str
= "-0x10000000000000000"; /* 65 bits, 32-bit sign bit clear */
1039 endptr
= "somewhere";
1041 err
= qemu_strtoui(str
, &endptr
, 0, &res
);
1042 g_assert_cmpint(err
, ==, -ERANGE
);
1043 g_assert_cmpuint(res
, ==, UINT_MAX
);
1044 g_assert_true(endptr
== str
+ strlen(str
));
1046 str
= "-0x18000000080000000"; /* 65 bits, 32-bit sign bit set */
1047 endptr
= "somewhere";
1049 err
= qemu_strtoui(str
, &endptr
, 0, &res
);
1050 g_assert_cmpint(err
, ==, -ERANGE
);
1051 g_assert_cmpuint(res
, ==, UINT_MAX
);
1052 g_assert_true(endptr
== str
+ strlen(str
));
1055 static void test_qemu_strtoui_negative(void)
1057 const char *str
= " \t -321";
1059 const char *endptr
= &f
;
1060 unsigned int res
= 999;
1063 err
= qemu_strtoui(str
, &endptr
, 0, &res
);
1065 g_assert_cmpint(err
, ==, 0);
1066 g_assert_cmpuint(res
, ==, (unsigned int)-321);
1067 g_assert_true(endptr
== str
+ strlen(str
));
1070 static void test_qemu_strtoui_negzero(void)
1072 const char *str
= " -0";
1074 const char *endptr
= &f
;
1075 unsigned int res
= 999;
1078 err
= qemu_strtoui(str
, &endptr
, 0, &res
);
1080 g_assert_cmpint(err
, ==, 0);
1081 g_assert_cmpuint(res
, ==, 0);
1082 g_assert_true(endptr
== str
+ strlen(str
));
1085 static void test_qemu_strtoui_full_correct(void)
1087 const char *str
= "123";
1088 unsigned int res
= 999;
1091 err
= qemu_strtoui(str
, NULL
, 0, &res
);
1093 g_assert_cmpint(err
, ==, 0);
1094 g_assert_cmpuint(res
, ==, 123);
1097 static void test_qemu_strtoui_full_null(void)
1099 unsigned int res
= 999;
1102 err
= qemu_strtoui(NULL
, NULL
, 0, &res
);
1104 g_assert_cmpint(err
, ==, -EINVAL
);
1105 g_assert_cmpuint(res
, ==, 999);
1108 static void test_qemu_strtoui_full_empty(void)
1110 const char *str
= "";
1111 unsigned int res
= 999;
1114 err
= qemu_strtoui(str
, NULL
, 0, &res
);
1116 g_assert_cmpint(err
, ==, -EINVAL
);
1117 g_assert_cmpuint(res
, ==, 0);
1120 static void test_qemu_strtoui_full_negative(void)
1122 const char *str
= " \t -321";
1123 unsigned int res
= 999;
1126 err
= qemu_strtoui(str
, NULL
, 0, &res
);
1127 g_assert_cmpint(err
, ==, 0);
1128 g_assert_cmpuint(res
, ==, (unsigned int)-321);
1131 static void test_qemu_strtoui_full_negzero(void)
1133 const char *str
= " -0";
1134 unsigned int res
= 999;
1137 err
= qemu_strtoui(str
, NULL
, 0, &res
);
1138 g_assert_cmpint(err
, ==, 0);
1139 g_assert_cmpuint(res
, ==, 0);
1142 static void test_qemu_strtoui_full_trailing(void)
1144 const char *str
= "123xxx";
1145 unsigned int res
= 999;
1148 err
= qemu_strtoui(str
, NULL
, 0, &res
);
1150 g_assert_cmpint(err
, ==, -EINVAL
);
1151 g_assert_cmpuint(res
, ==, 123);
1154 static void test_qemu_strtoui_full_max(void)
1156 char *str
= g_strdup_printf("%u", UINT_MAX
);
1157 unsigned int res
= 999;
1160 err
= qemu_strtoui(str
, NULL
, 0, &res
);
1162 g_assert_cmpint(err
, ==, 0);
1163 g_assert_cmphex(res
, ==, UINT_MAX
);
1167 static void test_qemu_strtoui_full_erange_junk(void)
1169 /* EINVAL has priority over ERANGE */
1170 const char *str
= "-9999999999junk";
1171 unsigned int res
= 999;
1174 err
= qemu_strtoui(str
, NULL
, 0, &res
);
1176 g_assert_cmpint(err
, ==, -EINVAL
);
1177 g_assert_cmpuint(res
, ==, UINT_MAX
);
1180 static void test_qemu_strtol_correct(void)
1182 const char *str
= "12345 foo";
1184 const char *endptr
= &f
;
1188 err
= qemu_strtol(str
, &endptr
, 0, &res
);
1190 g_assert_cmpint(err
, ==, 0);
1191 g_assert_cmpint(res
, ==, 12345);
1192 g_assert_true(endptr
== str
+ 5);
1195 static void test_qemu_strtol_null(void)
1198 const char *endptr
= &f
;
1202 err
= qemu_strtol(NULL
, &endptr
, 0, &res
);
1204 g_assert_cmpint(err
, ==, -EINVAL
);
1205 g_assert_cmpint(res
, ==, 999);
1206 g_assert_null(endptr
);
1209 static void test_qemu_strtol_empty(void)
1211 const char *str
= "";
1213 const char *endptr
= &f
;
1217 err
= qemu_strtol(str
, &endptr
, 0, &res
);
1219 g_assert_cmpint(err
, ==, -EINVAL
);
1220 g_assert_cmpint(res
, ==, 0);
1221 g_assert_true(endptr
== str
);
1224 static void test_qemu_strtol_whitespace(void)
1226 const char *str
= " \t ";
1228 const char *endptr
= &f
;
1232 err
= qemu_strtol(str
, &endptr
, 0, &res
);
1234 g_assert_cmpint(err
, ==, -EINVAL
);
1235 g_assert_cmpint(res
, ==, 0);
1236 g_assert_true(endptr
== str
);
1239 static void test_qemu_strtol_invalid(void)
1241 const char *str
= " xxxx \t abc";
1243 const char *endptr
= &f
;
1247 err
= qemu_strtol(str
, &endptr
, 0, &res
);
1249 g_assert_cmpint(err
, ==, -EINVAL
);
1250 g_assert_cmpint(res
, ==, 0);
1251 g_assert_true(endptr
== str
);
1254 static void test_qemu_strtol_trailing(void)
1256 const char *str
= "123xxx";
1258 const char *endptr
= &f
;
1262 err
= qemu_strtol(str
, &endptr
, 0, &res
);
1264 g_assert_cmpint(err
, ==, 0);
1265 g_assert_cmpint(res
, ==, 123);
1266 g_assert_true(endptr
== str
+ 3);
1269 static void test_qemu_strtol_octal(void)
1271 const char *str
= "0123";
1273 const char *endptr
= &f
;
1277 err
= qemu_strtol(str
, &endptr
, 8, &res
);
1279 g_assert_cmpint(err
, ==, 0);
1280 g_assert_cmpint(res
, ==, 0123);
1281 g_assert_true(endptr
== str
+ strlen(str
));
1285 err
= qemu_strtol(str
, &endptr
, 0, &res
);
1287 g_assert_cmpint(err
, ==, 0);
1288 g_assert_cmpint(res
, ==, 0123);
1289 g_assert_true(endptr
== str
+ strlen(str
));
1292 static void test_qemu_strtol_decimal(void)
1294 const char *str
= "0123";
1296 const char *endptr
= &f
;
1300 err
= qemu_strtol(str
, &endptr
, 10, &res
);
1302 g_assert_cmpint(err
, ==, 0);
1303 g_assert_cmpint(res
, ==, 123);
1304 g_assert_true(endptr
== str
+ strlen(str
));
1309 err
= qemu_strtol(str
, &endptr
, 0, &res
);
1311 g_assert_cmpint(err
, ==, 0);
1312 g_assert_cmpint(res
, ==, 123);
1313 g_assert_true(endptr
== str
+ strlen(str
));
1316 static void test_qemu_strtol_hex(void)
1318 const char *str
= "0123";
1320 const char *endptr
= &f
;
1324 err
= qemu_strtol(str
, &endptr
, 16, &res
);
1326 g_assert_cmpint(err
, ==, 0);
1327 g_assert_cmpint(res
, ==, 0x123);
1328 g_assert_true(endptr
== str
+ strlen(str
));
1333 err
= qemu_strtol(str
, &endptr
, 0, &res
);
1335 g_assert_cmpint(err
, ==, 0);
1336 g_assert_cmpint(res
, ==, 0x123);
1337 g_assert_true(endptr
== str
+ strlen(str
));
1342 err
= qemu_strtol(str
, &endptr
, 16, &res
);
1344 g_assert_cmpint(err
, ==, 0);
1345 g_assert_cmpint(res
, ==, 0);
1346 g_assert_true(endptr
== str
+ 1);
1349 static void test_qemu_strtol_max(void)
1351 char *str
= g_strdup_printf("%ld", LONG_MAX
);
1353 const char *endptr
= &f
;
1357 err
= qemu_strtol(str
, &endptr
, 0, &res
);
1359 g_assert_cmpint(err
, ==, 0);
1360 g_assert_cmpint(res
, ==, LONG_MAX
);
1361 g_assert_true(endptr
== str
+ strlen(str
));
1365 static void test_qemu_strtol_overflow(void)
1372 /* 1 more than LONG_MAX */
1373 str
= LONG_MAX
== INT_MAX
? "2147483648" : "9223372036854775808";
1374 endptr
= "somewhere";
1376 err
= qemu_strtol(str
, &endptr
, 0, &res
);
1377 g_assert_cmpint(err
, ==, -ERANGE
);
1378 g_assert_cmpint(res
, ==, LONG_MAX
);
1379 g_assert_true(endptr
== str
+ strlen(str
));
1381 if (LONG_MAX
== INT_MAX
) {
1382 str
= "0xffffffff00000001"; /* ULLONG_MAX - UINT_MAX + 1 (not 1) */
1383 endptr
= "somewhere";
1385 err
= qemu_strtol(str
, &endptr
, 0, &res
);
1386 g_assert_cmpint(err
, ==, -ERANGE
);
1387 g_assert_cmpint(res
, ==, LONG_MAX
);
1388 g_assert_true(endptr
== str
+ strlen(str
));
1391 str
= "0x10000000000000000"; /* 65 bits, either sign bit position clear */
1392 endptr
= "somewhere";
1394 err
= qemu_strtol(str
, &endptr
, 0, &res
);
1395 g_assert_cmpint(err
, ==, -ERANGE
);
1396 g_assert_cmpint(res
, ==, LONG_MAX
);
1397 g_assert_true(endptr
== str
+ strlen(str
));
1399 str
= "0x18000000080000000"; /* 65 bits, either sign bit position set */
1400 endptr
= "somewhere";
1402 err
= qemu_strtol(str
, &endptr
, 0, &res
);
1403 g_assert_cmpint(err
, ==, -ERANGE
);
1404 g_assert_cmpint(res
, ==, LONG_MAX
);
1405 g_assert_true(endptr
== str
+ strlen(str
));
1408 static void test_qemu_strtol_min(void)
1410 char *str
= g_strdup_printf("%ld", LONG_MIN
);
1412 const char *endptr
= &f
;
1416 err
= qemu_strtol(str
, &endptr
, 0, &res
);
1418 g_assert_cmpint(err
, ==, 0);
1419 g_assert_cmpint(res
, ==, LONG_MIN
);
1420 g_assert_true(endptr
== str
+ strlen(str
));
1424 static void test_qemu_strtol_underflow(void)
1431 /* 1 less than LONG_MIN */
1432 str
= LONG_MIN
== INT_MIN
? "-2147483649" : "-9223372036854775809";
1433 endptr
= "somewhere";
1435 err
= qemu_strtol(str
, &endptr
, 0, &res
);
1436 g_assert_cmpint(err
, ==, -ERANGE
);
1437 g_assert_cmpint(res
, ==, LONG_MIN
);
1438 g_assert_true(endptr
== str
+ strlen(str
));
1440 if (LONG_MAX
== INT_MAX
) {
1441 str
= "-18446744073709551615"; /* -UINT64_MAX (not 1) */
1442 endptr
= "somewhere";
1444 err
= qemu_strtol(str
, &endptr
, 0, &res
);
1445 g_assert_cmpint(err
, ==, -ERANGE
);
1446 g_assert_cmpint(res
, ==, LONG_MIN
);
1447 g_assert_true(endptr
== str
+ strlen(str
));
1450 str
= "-0x10000000000000000"; /* 65 bits, either sign bit position clear */
1451 endptr
= "somewhere";
1453 err
= qemu_strtol(str
, &endptr
, 0, &res
);
1454 g_assert_cmpint(err
, ==, -ERANGE
);
1455 g_assert_cmpint(res
, ==, LONG_MIN
);
1456 g_assert_true(endptr
== str
+ strlen(str
));
1458 str
= "-0x18000000080000000"; /* 65 bits, either sign bit position set */
1459 endptr
= "somewhere";
1461 err
= qemu_strtol(str
, &endptr
, 0, &res
);
1462 g_assert_cmpint(err
, ==, -ERANGE
);
1463 g_assert_cmpint(res
, ==, LONG_MIN
);
1464 g_assert_true(endptr
== str
+ strlen(str
));
1467 static void test_qemu_strtol_negative(void)
1469 const char *str
= " \t -321";
1471 const char *endptr
= &f
;
1475 err
= qemu_strtol(str
, &endptr
, 0, &res
);
1477 g_assert_cmpint(err
, ==, 0);
1478 g_assert_cmpint(res
, ==, -321);
1479 g_assert_true(endptr
== str
+ strlen(str
));
1482 static void test_qemu_strtol_negzero(void)
1484 const char *str
= " -0";
1486 const char *endptr
= &f
;
1490 err
= qemu_strtol(str
, &endptr
, 0, &res
);
1492 g_assert_cmpint(err
, ==, 0);
1493 g_assert_cmpint(res
, ==, 0);
1494 g_assert_true(endptr
== str
+ strlen(str
));
1497 static void test_qemu_strtol_full_correct(void)
1499 const char *str
= "123";
1503 err
= qemu_strtol(str
, NULL
, 0, &res
);
1505 g_assert_cmpint(err
, ==, 0);
1506 g_assert_cmpint(res
, ==, 123);
1509 static void test_qemu_strtol_full_null(void)
1512 const char *endptr
= &f
;
1516 err
= qemu_strtol(NULL
, &endptr
, 0, &res
);
1518 g_assert_cmpint(err
, ==, -EINVAL
);
1519 g_assert_cmpint(res
, ==, 999);
1520 g_assert_null(endptr
);
1523 static void test_qemu_strtol_full_empty(void)
1525 const char *str
= "";
1529 err
= qemu_strtol(str
, NULL
, 0, &res
);
1531 g_assert_cmpint(err
, ==, -EINVAL
);
1532 g_assert_cmpint(res
, ==, 0);
1535 static void test_qemu_strtol_full_negative(void)
1537 const char *str
= " \t -321";
1541 err
= qemu_strtol(str
, NULL
, 0, &res
);
1543 g_assert_cmpint(err
, ==, 0);
1544 g_assert_cmpint(res
, ==, -321);
1547 static void test_qemu_strtol_full_negzero(void)
1549 const char *str
= " -0";
1553 err
= qemu_strtol(str
, NULL
, 0, &res
);
1555 g_assert_cmpint(err
, ==, 0);
1556 g_assert_cmpint(res
, ==, 0);
1559 static void test_qemu_strtol_full_trailing(void)
1561 const char *str
= "123xxx";
1565 err
= qemu_strtol(str
, NULL
, 0, &res
);
1567 g_assert_cmpint(err
, ==, -EINVAL
);
1568 g_assert_cmpint(res
, ==, 123);
1571 static void test_qemu_strtol_full_max(void)
1573 char *str
= g_strdup_printf("%ld", LONG_MAX
);
1577 err
= qemu_strtol(str
, NULL
, 0, &res
);
1579 g_assert_cmpint(err
, ==, 0);
1580 g_assert_cmpint(res
, ==, LONG_MAX
);
1584 static void test_qemu_strtol_full_erange_junk(void)
1586 /* EINVAL has priority over ERANGE */
1587 const char *str
= "-99999999999999999999junk";
1591 err
= qemu_strtol(str
, NULL
, 0, &res
);
1593 g_assert_cmpint(err
, ==, -EINVAL
);
1594 g_assert_cmpint(res
, ==, LONG_MIN
);
1597 static void test_qemu_strtoul_correct(void)
1599 const char *str
= "12345 foo";
1601 const char *endptr
= &f
;
1602 unsigned long res
= 999;
1605 err
= qemu_strtoul(str
, &endptr
, 0, &res
);
1607 g_assert_cmpint(err
, ==, 0);
1608 g_assert_cmpuint(res
, ==, 12345);
1609 g_assert_true(endptr
== str
+ 5);
1612 static void test_qemu_strtoul_null(void)
1615 const char *endptr
= &f
;
1616 unsigned long res
= 999;
1619 err
= qemu_strtoul(NULL
, &endptr
, 0, &res
);
1621 g_assert_cmpint(err
, ==, -EINVAL
);
1622 g_assert_cmpuint(res
, ==, 999);
1623 g_assert_null(endptr
);
1626 static void test_qemu_strtoul_empty(void)
1628 const char *str
= "";
1630 const char *endptr
= &f
;
1631 unsigned long res
= 999;
1634 err
= qemu_strtoul(str
, &endptr
, 0, &res
);
1636 g_assert_cmpint(err
, ==, -EINVAL
);
1637 g_assert_cmpuint(res
, ==, 0);
1638 g_assert_true(endptr
== str
);
1641 static void test_qemu_strtoul_whitespace(void)
1643 const char *str
= " \t ";
1645 const char *endptr
= &f
;
1646 unsigned long res
= 999;
1649 err
= qemu_strtoul(str
, &endptr
, 0, &res
);
1651 g_assert_cmpint(err
, ==, -EINVAL
);
1652 g_assert_cmpuint(res
, ==, 0);
1653 g_assert_true(endptr
== str
);
1656 static void test_qemu_strtoul_invalid(void)
1658 const char *str
= " xxxx \t abc";
1660 const char *endptr
= &f
;
1661 unsigned long res
= 999;
1664 err
= qemu_strtoul(str
, &endptr
, 0, &res
);
1666 g_assert_cmpint(err
, ==, -EINVAL
);
1667 g_assert_cmpuint(res
, ==, 0);
1668 g_assert_true(endptr
== str
);
1671 static void test_qemu_strtoul_trailing(void)
1673 const char *str
= "123xxx";
1675 const char *endptr
= &f
;
1676 unsigned long res
= 999;
1679 err
= qemu_strtoul(str
, &endptr
, 0, &res
);
1681 g_assert_cmpint(err
, ==, 0);
1682 g_assert_cmpuint(res
, ==, 123);
1683 g_assert_true(endptr
== str
+ 3);
1686 static void test_qemu_strtoul_octal(void)
1688 const char *str
= "0123";
1690 const char *endptr
= &f
;
1691 unsigned long res
= 999;
1694 err
= qemu_strtoul(str
, &endptr
, 8, &res
);
1696 g_assert_cmpint(err
, ==, 0);
1697 g_assert_cmpuint(res
, ==, 0123);
1698 g_assert_true(endptr
== str
+ strlen(str
));
1702 err
= qemu_strtoul(str
, &endptr
, 0, &res
);
1704 g_assert_cmpint(err
, ==, 0);
1705 g_assert_cmpuint(res
, ==, 0123);
1706 g_assert_true(endptr
== str
+ strlen(str
));
1709 static void test_qemu_strtoul_decimal(void)
1711 const char *str
= "0123";
1713 const char *endptr
= &f
;
1714 unsigned long res
= 999;
1717 err
= qemu_strtoul(str
, &endptr
, 10, &res
);
1719 g_assert_cmpint(err
, ==, 0);
1720 g_assert_cmpuint(res
, ==, 123);
1721 g_assert_true(endptr
== str
+ strlen(str
));
1726 err
= qemu_strtoul(str
, &endptr
, 0, &res
);
1728 g_assert_cmpint(err
, ==, 0);
1729 g_assert_cmpuint(res
, ==, 123);
1730 g_assert_true(endptr
== str
+ strlen(str
));
1733 static void test_qemu_strtoul_hex(void)
1735 const char *str
= "0123";
1737 const char *endptr
= &f
;
1738 unsigned long res
= 999;
1741 err
= qemu_strtoul(str
, &endptr
, 16, &res
);
1743 g_assert_cmpint(err
, ==, 0);
1744 g_assert_cmphex(res
, ==, 0x123);
1745 g_assert_true(endptr
== str
+ strlen(str
));
1750 err
= qemu_strtoul(str
, &endptr
, 0, &res
);
1752 g_assert_cmpint(err
, ==, 0);
1753 g_assert_cmphex(res
, ==, 0x123);
1754 g_assert_true(endptr
== str
+ strlen(str
));
1759 err
= qemu_strtoul(str
, &endptr
, 16, &res
);
1761 g_assert_cmpint(err
, ==, 0);
1762 g_assert_cmphex(res
, ==, 0);
1763 g_assert_true(endptr
== str
+ 1);
1766 static void test_qemu_strtoul_wrap(void)
1770 const char *endptr
= &f
;
1771 unsigned long res
= 999;
1774 /* 1 mod 2^(sizeof(long)*8) */
1775 str
= LONG_MAX
== INT_MAX
? "-4294967295" : "-18446744073709551615";
1776 err
= qemu_strtoul(str
, &endptr
, 0, &res
);
1778 g_assert_cmpint(err
, ==, 0);
1779 g_assert_cmphex(res
, ==, 1);
1780 g_assert_true(endptr
== str
+ strlen(str
));
1783 static void test_qemu_strtoul_max(void)
1785 char *str
= g_strdup_printf("%lu", ULONG_MAX
);
1787 const char *endptr
= &f
;
1788 unsigned long res
= 999;
1791 err
= qemu_strtoul(str
, &endptr
, 0, &res
);
1793 g_assert_cmpint(err
, ==, 0);
1794 g_assert_cmphex(res
, ==, ULONG_MAX
);
1795 g_assert_true(endptr
== str
+ strlen(str
));
1799 static void test_qemu_strtoul_overflow(void)
1806 /* 1 more than ULONG_MAX */
1807 str
= ULONG_MAX
== UINT_MAX
? "4294967296" : "18446744073709551616";
1808 endptr
= "somewhere";
1810 err
= qemu_strtoul(str
, &endptr
, 0, &res
);
1811 g_assert_cmpint(err
, ==, -ERANGE
);
1812 g_assert_cmpuint(res
, ==, ULONG_MAX
);
1813 g_assert_true(endptr
== str
+ strlen(str
));
1815 if (LONG_MAX
== INT_MAX
) {
1816 str
= "0xffffffff00000001"; /* UINT64_MAX - UINT_MAX + 1 (not 1) */
1817 endptr
= "somewhere";
1819 err
= qemu_strtoul(str
, &endptr
, 0, &res
);
1820 g_assert_cmpint(err
, ==, -ERANGE
);
1821 g_assert_cmpuint(res
, ==, ULONG_MAX
);
1822 g_assert_true(endptr
== str
+ strlen(str
));
1825 str
= "0x10000000000000000"; /* 65 bits, either sign bit position clear */
1826 endptr
= "somewhere";
1828 err
= qemu_strtoul(str
, &endptr
, 0, &res
);
1829 g_assert_cmpint(err
, ==, -ERANGE
);
1830 g_assert_cmpuint(res
, ==, ULONG_MAX
);
1831 g_assert_true(endptr
== str
+ strlen(str
));
1833 str
= "0x18000000080000000"; /* 65 bits, either sign bit position set */
1834 endptr
= "somewhere";
1836 err
= qemu_strtoul(str
, &endptr
, 0, &res
);
1837 g_assert_cmpint(err
, ==, -ERANGE
);
1838 g_assert_cmpuint(res
, ==, ULONG_MAX
);
1839 g_assert_true(endptr
== str
+ strlen(str
));
1842 static void test_qemu_strtoul_underflow(void)
1849 /* 1 less than -ULONG_MAX */
1850 str
= ULONG_MAX
== UINT_MAX
? "-4294967296" : "-18446744073709551616";
1851 endptr
= "somewhere";
1853 err
= qemu_strtoul(str
, &endptr
, 0, &res
);
1854 g_assert_cmpint(err
, ==, -ERANGE
);
1855 g_assert_cmpuint(res
, ==, ULONG_MAX
);
1856 g_assert_true(endptr
== str
+ strlen(str
));
1858 if (LONG_MAX
== INT_MAX
) {
1859 str
= "-0xffffffff00000002";
1860 endptr
= "somewhere";
1862 err
= qemu_strtoul(str
, &endptr
, 0, &res
);
1863 g_assert_cmpint(err
, ==, -ERANGE
);
1864 g_assert_cmpuint(res
, ==, ULONG_MAX
);
1865 g_assert_true(endptr
== str
+ strlen(str
));
1868 str
= "-0x10000000000000000"; /* 65 bits, either sign bit position clear */
1869 endptr
= "somewhere";
1871 err
= qemu_strtoul(str
, &endptr
, 0, &res
);
1872 g_assert_cmpint(err
, ==, -ERANGE
);
1873 g_assert_cmpuint(res
, ==, ULONG_MAX
);
1874 g_assert_true(endptr
== str
+ strlen(str
));
1876 str
= "-0x18000000080000000"; /* 65 bits, either sign bit position set */
1877 endptr
= "somewhere";
1879 err
= qemu_strtoul(str
, &endptr
, 0, &res
);
1880 g_assert_cmpint(err
, ==, -ERANGE
);
1881 g_assert_cmpuint(res
, ==, ULONG_MAX
);
1882 g_assert_true(endptr
== str
+ strlen(str
));
1885 static void test_qemu_strtoul_negative(void)
1887 const char *str
= " \t -321";
1889 const char *endptr
= &f
;
1890 unsigned long res
= 999;
1893 err
= qemu_strtoul(str
, &endptr
, 0, &res
);
1895 g_assert_cmpint(err
, ==, 0);
1896 g_assert_cmpuint(res
, ==, -321ul);
1897 g_assert_true(endptr
== str
+ strlen(str
));
1900 static void test_qemu_strtoul_negzero(void)
1902 const char *str
= " -0";
1904 const char *endptr
= &f
;
1905 unsigned long res
= 999;
1908 err
= qemu_strtoul(str
, &endptr
, 0, &res
);
1910 g_assert_cmpint(err
, ==, 0);
1911 g_assert_cmpuint(res
, ==, 0);
1912 g_assert_true(endptr
== str
+ strlen(str
));
1915 static void test_qemu_strtoul_full_correct(void)
1917 const char *str
= "123";
1918 unsigned long res
= 999;
1921 err
= qemu_strtoul(str
, NULL
, 0, &res
);
1923 g_assert_cmpint(err
, ==, 0);
1924 g_assert_cmpuint(res
, ==, 123);
1927 static void test_qemu_strtoul_full_null(void)
1929 unsigned long res
= 999;
1932 err
= qemu_strtoul(NULL
, NULL
, 0, &res
);
1934 g_assert_cmpint(err
, ==, -EINVAL
);
1935 g_assert_cmpuint(res
, ==, 999);
1938 static void test_qemu_strtoul_full_empty(void)
1940 const char *str
= "";
1941 unsigned long res
= 999;
1944 err
= qemu_strtoul(str
, NULL
, 0, &res
);
1946 g_assert_cmpint(err
, ==, -EINVAL
);
1947 g_assert_cmpuint(res
, ==, 0);
1950 static void test_qemu_strtoul_full_negative(void)
1952 const char *str
= " \t -321";
1953 unsigned long res
= 999;
1956 err
= qemu_strtoul(str
, NULL
, 0, &res
);
1957 g_assert_cmpint(err
, ==, 0);
1958 g_assert_cmpuint(res
, ==, -321ul);
1961 static void test_qemu_strtoul_full_negzero(void)
1963 const char *str
= " -0";
1964 unsigned long res
= 999;
1967 err
= qemu_strtoul(str
, NULL
, 0, &res
);
1968 g_assert_cmpint(err
, ==, 0);
1969 g_assert_cmpuint(res
, ==, 0);
1972 static void test_qemu_strtoul_full_trailing(void)
1974 const char *str
= "123xxx";
1975 unsigned long res
= 999;
1978 err
= qemu_strtoul(str
, NULL
, 0, &res
);
1980 g_assert_cmpint(err
, ==, -EINVAL
);
1981 g_assert_cmpuint(res
, ==, 123);
1984 static void test_qemu_strtoul_full_max(void)
1986 char *str
= g_strdup_printf("%lu", ULONG_MAX
);
1987 unsigned long res
= 999;
1990 err
= qemu_strtoul(str
, NULL
, 0, &res
);
1992 g_assert_cmpint(err
, ==, 0);
1993 g_assert_cmphex(res
, ==, ULONG_MAX
);
1997 static void test_qemu_strtoul_full_erange_junk(void)
1999 /* EINVAL has priority over ERANGE */
2000 const char *str
= "-99999999999999999999junk";
2001 unsigned long res
= 999;
2004 err
= qemu_strtoul(str
, NULL
, 0, &res
);
2006 g_assert_cmpint(err
, ==, -EINVAL
);
2007 g_assert_cmpuint(res
, ==, ULONG_MAX
);
2010 static void test_qemu_strtoi64_correct(void)
2012 const char *str
= "12345 foo";
2014 const char *endptr
= &f
;
2018 err
= qemu_strtoi64(str
, &endptr
, 0, &res
);
2020 g_assert_cmpint(err
, ==, 0);
2021 g_assert_cmpint(res
, ==, 12345);
2022 g_assert_true(endptr
== str
+ 5);
2025 static void test_qemu_strtoi64_null(void)
2028 const char *endptr
= &f
;
2032 err
= qemu_strtoi64(NULL
, &endptr
, 0, &res
);
2034 g_assert_cmpint(err
, ==, -EINVAL
);
2035 g_assert_cmpint(res
, ==, 999);
2036 g_assert_null(endptr
);
2039 static void test_qemu_strtoi64_empty(void)
2041 const char *str
= "";
2043 const char *endptr
= &f
;
2047 err
= qemu_strtoi64(str
, &endptr
, 0, &res
);
2049 g_assert_cmpint(err
, ==, -EINVAL
);
2050 g_assert_cmpint(res
, ==, 0);
2051 g_assert_true(endptr
== str
);
2054 static void test_qemu_strtoi64_whitespace(void)
2056 const char *str
= " \t ";
2058 const char *endptr
= &f
;
2062 err
= qemu_strtoi64(str
, &endptr
, 0, &res
);
2064 g_assert_cmpint(err
, ==, -EINVAL
);
2065 g_assert_cmpint(res
, ==, 0);
2066 g_assert_true(endptr
== str
);
2069 static void test_qemu_strtoi64_invalid(void)
2071 const char *str
= " xxxx \t abc";
2073 const char *endptr
= &f
;
2077 err
= qemu_strtoi64(str
, &endptr
, 0, &res
);
2079 g_assert_cmpint(err
, ==, -EINVAL
);
2080 g_assert_cmpint(res
, ==, 0);
2081 g_assert_true(endptr
== str
);
2084 static void test_qemu_strtoi64_trailing(void)
2086 const char *str
= "123xxx";
2088 const char *endptr
= &f
;
2092 err
= qemu_strtoi64(str
, &endptr
, 0, &res
);
2094 g_assert_cmpint(err
, ==, 0);
2095 g_assert_cmpint(res
, ==, 123);
2096 g_assert_true(endptr
== str
+ 3);
2099 static void test_qemu_strtoi64_octal(void)
2101 const char *str
= "0123";
2103 const char *endptr
= &f
;
2107 err
= qemu_strtoi64(str
, &endptr
, 8, &res
);
2109 g_assert_cmpint(err
, ==, 0);
2110 g_assert_cmpint(res
, ==, 0123);
2111 g_assert_true(endptr
== str
+ strlen(str
));
2115 err
= qemu_strtoi64(str
, &endptr
, 0, &res
);
2117 g_assert_cmpint(err
, ==, 0);
2118 g_assert_cmpint(res
, ==, 0123);
2119 g_assert_true(endptr
== str
+ strlen(str
));
2122 static void test_qemu_strtoi64_decimal(void)
2124 const char *str
= "0123";
2126 const char *endptr
= &f
;
2130 err
= qemu_strtoi64(str
, &endptr
, 10, &res
);
2132 g_assert_cmpint(err
, ==, 0);
2133 g_assert_cmpint(res
, ==, 123);
2134 g_assert_true(endptr
== str
+ strlen(str
));
2139 err
= qemu_strtoi64(str
, &endptr
, 0, &res
);
2141 g_assert_cmpint(err
, ==, 0);
2142 g_assert_cmpint(res
, ==, 123);
2143 g_assert_true(endptr
== str
+ strlen(str
));
2146 static void test_qemu_strtoi64_hex(void)
2148 const char *str
= "0123";
2150 const char *endptr
= &f
;
2154 err
= qemu_strtoi64(str
, &endptr
, 16, &res
);
2156 g_assert_cmpint(err
, ==, 0);
2157 g_assert_cmpint(res
, ==, 0x123);
2158 g_assert_true(endptr
== str
+ strlen(str
));
2163 err
= qemu_strtoi64(str
, &endptr
, 0, &res
);
2165 g_assert_cmpint(err
, ==, 0);
2166 g_assert_cmpint(res
, ==, 0x123);
2167 g_assert_true(endptr
== str
+ strlen(str
));
2172 err
= qemu_strtoi64(str
, &endptr
, 16, &res
);
2174 g_assert_cmpint(err
, ==, 0);
2175 g_assert_cmpint(res
, ==, 0);
2176 g_assert_true(endptr
== str
+ 1);
2179 static void test_qemu_strtoi64_max(void)
2181 char *str
= g_strdup_printf("%lld", LLONG_MAX
);
2183 const char *endptr
= &f
;
2187 err
= qemu_strtoi64(str
, &endptr
, 0, &res
);
2189 g_assert_cmpint(err
, ==, 0);
2190 g_assert_cmpint(res
, ==, LLONG_MAX
);
2191 g_assert_true(endptr
== str
+ strlen(str
));
2195 static void test_qemu_strtoi64_overflow(void)
2202 str
= "9223372036854775808"; /* 1 more than INT64_MAX */
2203 endptr
= "somewhere";
2205 err
= qemu_strtoi64(str
, &endptr
, 0, &res
);
2206 g_assert_cmpint(err
, ==, -ERANGE
);
2207 g_assert_cmpint(res
, ==, INT64_MAX
);
2208 g_assert_true(endptr
== str
+ strlen(str
));
2210 str
= "0x10000000000000000"; /* 65 bits, 64-bit sign bit clear */
2211 endptr
= "somewhere";
2213 err
= qemu_strtoi64(str
, &endptr
, 0, &res
);
2214 g_assert_cmpint(err
, ==, -ERANGE
);
2215 g_assert_cmpint(res
, ==, INT64_MAX
);
2216 g_assert_true(endptr
== str
+ strlen(str
));
2218 str
= "0x18000000080000000"; /* 65 bits, 64-bit sign bit set */
2219 endptr
= "somewhere";
2221 err
= qemu_strtoi64(str
, &endptr
, 0, &res
);
2222 g_assert_cmpint(err
, ==, -ERANGE
);
2223 g_assert_cmpint(res
, ==, INT64_MAX
);
2224 g_assert_true(endptr
== str
+ strlen(str
));
2227 static void test_qemu_strtoi64_min(void)
2229 char *str
= g_strdup_printf("%lld", LLONG_MIN
);
2231 const char *endptr
= &f
;
2235 err
= qemu_strtoi64(str
, &endptr
, 0, &res
);
2237 g_assert_cmpint(err
, ==, 0);
2238 g_assert_cmpint(res
, ==, LLONG_MIN
);
2239 g_assert_true(endptr
== str
+ strlen(str
));
2243 static void test_qemu_strtoi64_underflow(void)
2250 str
= "-9223372036854775809"; /* 1 less than INT64_MIN */
2251 endptr
= "somewhere";
2253 err
= qemu_strtoi64(str
, &endptr
, 0, &res
);
2254 g_assert_cmpint(err
, ==, -ERANGE
);
2255 g_assert_cmpint(res
, ==, INT64_MIN
);
2256 g_assert_true(endptr
== str
+ strlen(str
));
2258 str
= "-0x10000000000000000"; /* 65 bits, 64-bit sign bit clear */
2259 endptr
= "somewhere";
2261 err
= qemu_strtoi64(str
, &endptr
, 0, &res
);
2262 g_assert_cmpint(err
, ==, -ERANGE
);
2263 g_assert_cmpint(res
, ==, INT64_MIN
);
2264 g_assert_true(endptr
== str
+ strlen(str
));
2266 str
= "-0x18000000080000000"; /* 65 bits, 64-bit sign bit set */
2267 endptr
= "somewhere";
2269 err
= qemu_strtoi64(str
, &endptr
, 0, &res
);
2270 g_assert_cmpint(err
, ==, -ERANGE
);
2271 g_assert_cmpint(res
, ==, INT64_MIN
);
2272 g_assert_true(endptr
== str
+ strlen(str
));
2275 static void test_qemu_strtoi64_negative(void)
2277 const char *str
= " \t -321";
2279 const char *endptr
= &f
;
2283 err
= qemu_strtoi64(str
, &endptr
, 0, &res
);
2285 g_assert_cmpint(err
, ==, 0);
2286 g_assert_cmpint(res
, ==, -321);
2287 g_assert_true(endptr
== str
+ strlen(str
));
2290 static void test_qemu_strtoi64_negzero(void)
2292 const char *str
= " -0";
2294 const char *endptr
= &f
;
2298 err
= qemu_strtoi64(str
, &endptr
, 0, &res
);
2300 g_assert_cmpint(err
, ==, 0);
2301 g_assert_cmpint(res
, ==, 0);
2302 g_assert_true(endptr
== str
+ strlen(str
));
2305 static void test_qemu_strtoi64_full_correct(void)
2307 const char *str
= "123";
2311 err
= qemu_strtoi64(str
, NULL
, 0, &res
);
2313 g_assert_cmpint(err
, ==, 0);
2314 g_assert_cmpint(res
, ==, 123);
2317 static void test_qemu_strtoi64_full_null(void)
2322 err
= qemu_strtoi64(NULL
, NULL
, 0, &res
);
2324 g_assert_cmpint(err
, ==, -EINVAL
);
2325 g_assert_cmpint(res
, ==, 999);
2328 static void test_qemu_strtoi64_full_empty(void)
2330 const char *str
= "";
2334 err
= qemu_strtoi64(str
, NULL
, 0, &res
);
2336 g_assert_cmpint(err
, ==, -EINVAL
);
2337 g_assert_cmpint(res
, ==, 0);
2340 static void test_qemu_strtoi64_full_negative(void)
2342 const char *str
= " \t -321";
2346 err
= qemu_strtoi64(str
, NULL
, 0, &res
);
2348 g_assert_cmpint(err
, ==, 0);
2349 g_assert_cmpint(res
, ==, -321);
2352 static void test_qemu_strtoi64_full_negzero(void)
2354 const char *str
= " -0";
2358 err
= qemu_strtoi64(str
, NULL
, 0, &res
);
2360 g_assert_cmpint(err
, ==, 0);
2361 g_assert_cmpint(res
, ==, 0);
2364 static void test_qemu_strtoi64_full_trailing(void)
2366 const char *str
= "123xxx";
2370 err
= qemu_strtoi64(str
, NULL
, 0, &res
);
2372 g_assert_cmpint(err
, ==, -EINVAL
);
2373 g_assert_cmpint(res
, ==, 123);
2376 static void test_qemu_strtoi64_full_max(void)
2379 char *str
= g_strdup_printf("%lld", LLONG_MAX
);
2383 err
= qemu_strtoi64(str
, NULL
, 0, &res
);
2385 g_assert_cmpint(err
, ==, 0);
2386 g_assert_cmpint(res
, ==, LLONG_MAX
);
2390 static void test_qemu_strtoi64_full_erange_junk(void)
2392 /* EINVAL has priority over ERANGE */
2393 const char *str
= "-99999999999999999999junk";
2397 err
= qemu_strtoi64(str
, NULL
, 0, &res
);
2399 g_assert_cmpint(err
, ==, -EINVAL
);
2400 g_assert_cmpint(res
, ==, INT64_MIN
);
2403 static void test_qemu_strtou64_correct(void)
2405 const char *str
= "12345 foo";
2407 const char *endptr
= &f
;
2411 err
= qemu_strtou64(str
, &endptr
, 0, &res
);
2413 g_assert_cmpint(err
, ==, 0);
2414 g_assert_cmpuint(res
, ==, 12345);
2415 g_assert_true(endptr
== str
+ 5);
2418 static void test_qemu_strtou64_null(void)
2421 const char *endptr
= &f
;
2425 err
= qemu_strtou64(NULL
, &endptr
, 0, &res
);
2427 g_assert_cmpint(err
, ==, -EINVAL
);
2428 g_assert_cmpuint(res
, ==, 999);
2429 g_assert_null(endptr
);
2432 static void test_qemu_strtou64_empty(void)
2434 const char *str
= "";
2436 const char *endptr
= &f
;
2440 err
= qemu_strtou64(str
, &endptr
, 0, &res
);
2442 g_assert_cmpint(err
, ==, -EINVAL
);
2443 g_assert_cmpuint(res
, ==, 0);
2444 g_assert_true(endptr
== str
);
2447 static void test_qemu_strtou64_whitespace(void)
2449 const char *str
= " \t ";
2451 const char *endptr
= &f
;
2455 err
= qemu_strtou64(str
, &endptr
, 0, &res
);
2457 g_assert_cmpint(err
, ==, -EINVAL
);
2458 g_assert_cmpuint(res
, ==, 0);
2459 g_assert_true(endptr
== str
);
2462 static void test_qemu_strtou64_invalid(void)
2464 const char *str
= " xxxx \t abc";
2466 const char *endptr
= &f
;
2470 err
= qemu_strtou64(str
, &endptr
, 0, &res
);
2472 g_assert_cmpint(err
, ==, -EINVAL
);
2473 g_assert_cmpuint(res
, ==, 0);
2474 g_assert_true(endptr
== str
);
2477 static void test_qemu_strtou64_trailing(void)
2479 const char *str
= "123xxx";
2481 const char *endptr
= &f
;
2485 err
= qemu_strtou64(str
, &endptr
, 0, &res
);
2487 g_assert_cmpint(err
, ==, 0);
2488 g_assert_cmpuint(res
, ==, 123);
2489 g_assert_true(endptr
== str
+ 3);
2492 static void test_qemu_strtou64_octal(void)
2494 const char *str
= "0123";
2496 const char *endptr
= &f
;
2500 err
= qemu_strtou64(str
, &endptr
, 8, &res
);
2502 g_assert_cmpint(err
, ==, 0);
2503 g_assert_cmpuint(res
, ==, 0123);
2504 g_assert_true(endptr
== str
+ strlen(str
));
2508 err
= qemu_strtou64(str
, &endptr
, 0, &res
);
2510 g_assert_cmpint(err
, ==, 0);
2511 g_assert_cmpuint(res
, ==, 0123);
2512 g_assert_true(endptr
== str
+ strlen(str
));
2515 static void test_qemu_strtou64_decimal(void)
2517 const char *str
= "0123";
2519 const char *endptr
= &f
;
2523 err
= qemu_strtou64(str
, &endptr
, 10, &res
);
2525 g_assert_cmpint(err
, ==, 0);
2526 g_assert_cmpuint(res
, ==, 123);
2527 g_assert_true(endptr
== str
+ strlen(str
));
2532 err
= qemu_strtou64(str
, &endptr
, 0, &res
);
2534 g_assert_cmpint(err
, ==, 0);
2535 g_assert_cmpuint(res
, ==, 123);
2536 g_assert_true(endptr
== str
+ strlen(str
));
2539 static void test_qemu_strtou64_hex(void)
2541 const char *str
= "0123";
2543 const char *endptr
= &f
;
2547 err
= qemu_strtou64(str
, &endptr
, 16, &res
);
2549 g_assert_cmpint(err
, ==, 0);
2550 g_assert_cmphex(res
, ==, 0x123);
2551 g_assert_true(endptr
== str
+ strlen(str
));
2556 err
= qemu_strtou64(str
, &endptr
, 0, &res
);
2558 g_assert_cmpint(err
, ==, 0);
2559 g_assert_cmphex(res
, ==, 0x123);
2560 g_assert_true(endptr
== str
+ strlen(str
));
2565 err
= qemu_strtou64(str
, &endptr
, 16, &res
);
2567 g_assert_cmpint(err
, ==, 0);
2568 g_assert_cmphex(res
, ==, 0);
2569 g_assert_true(endptr
== str
+ 1);
2572 static void test_qemu_strtou64_wrap(void)
2574 const char *str
= "-18446744073709551615"; /* 1 mod 2^64 */
2576 const char *endptr
= &f
;
2580 err
= qemu_strtou64(str
, &endptr
, 0, &res
);
2582 g_assert_cmpint(err
, ==, 0);
2583 g_assert_cmpuint(res
, ==, 1);
2584 g_assert_true(endptr
== str
+ strlen(str
));
2587 static void test_qemu_strtou64_max(void)
2589 char *str
= g_strdup_printf("%llu", ULLONG_MAX
);
2591 const char *endptr
= &f
;
2595 err
= qemu_strtou64(str
, &endptr
, 0, &res
);
2597 g_assert_cmpint(err
, ==, 0);
2598 g_assert_cmphex(res
, ==, ULLONG_MAX
);
2599 g_assert_true(endptr
== str
+ strlen(str
));
2603 static void test_qemu_strtou64_overflow(void)
2610 str
= "18446744073709551616"; /* 1 more than UINT64_MAX */
2611 endptr
= "somewhere";
2613 err
= qemu_strtou64(str
, &endptr
, 0, &res
);
2614 g_assert_cmpint(err
, ==, -ERANGE
);
2615 g_assert_cmpuint(res
, ==, UINT64_MAX
);
2616 g_assert_true(endptr
== str
+ strlen(str
));
2618 str
= "0x10000000000000000"; /* 65 bits, 64-bit sign bit clear */
2619 endptr
= "somewhere";
2621 err
= qemu_strtou64(str
, &endptr
, 0, &res
);
2622 g_assert_cmpint(err
, ==, -ERANGE
);
2623 g_assert_cmpuint(res
, ==, UINT64_MAX
);
2624 g_assert_true(endptr
== str
+ strlen(str
));
2626 str
= "0x18000000080000000"; /* 65 bits, 64-bit sign bit set */
2627 endptr
= "somewhere";
2629 err
= qemu_strtou64(str
, &endptr
, 0, &res
);
2630 g_assert_cmpint(err
, ==, -ERANGE
);
2631 g_assert_cmpuint(res
, ==, UINT64_MAX
);
2632 g_assert_true(endptr
== str
+ strlen(str
));
2635 static void test_qemu_strtou64_underflow(void)
2642 str
= "-99999999999999999999999999999999999999999999";
2643 endptr
= "somewhere";
2645 err
= qemu_strtou64(str
, &endptr
, 0, &res
);
2646 g_assert_cmpint(err
, ==, -ERANGE
);
2647 g_assert_cmpuint(res
, ==, UINT64_MAX
);
2648 g_assert_true(endptr
== str
+ strlen(str
));
2650 str
= "-0x10000000000000000"; /* 65 bits, 64-bit sign bit clear */
2651 endptr
= "somewhere";
2653 err
= qemu_strtou64(str
, &endptr
, 0, &res
);
2654 g_assert_cmpint(err
, ==, -ERANGE
);
2655 g_assert_cmpuint(res
, ==, UINT64_MAX
);
2656 g_assert_true(endptr
== str
+ strlen(str
));
2658 str
= "-0x18000000080000000"; /* 65 bits, 64-bit sign bit set */
2659 endptr
= "somewhere";
2661 err
= qemu_strtou64(str
, &endptr
, 0, &res
);
2662 g_assert_cmpint(err
, ==, -ERANGE
);
2663 g_assert_cmpuint(res
, ==, UINT64_MAX
);
2664 g_assert_true(endptr
== str
+ strlen(str
));
2667 static void test_qemu_strtou64_negative(void)
2669 const char *str
= " \t -321";
2671 const char *endptr
= &f
;
2675 err
= qemu_strtou64(str
, &endptr
, 0, &res
);
2677 g_assert_cmpint(err
, ==, 0);
2678 g_assert_cmpuint(res
, ==, -321ull);
2679 g_assert_true(endptr
== str
+ strlen(str
));
2682 static void test_qemu_strtou64_negzero(void)
2684 const char *str
= " -0";
2686 const char *endptr
= &f
;
2690 err
= qemu_strtou64(str
, &endptr
, 0, &res
);
2692 g_assert_cmpint(err
, ==, 0);
2693 g_assert_cmpuint(res
, ==, 0);
2694 g_assert_true(endptr
== str
+ strlen(str
));
2697 static void test_qemu_strtou64_full_correct(void)
2699 const char *str
= "18446744073709551614";
2703 err
= qemu_strtou64(str
, NULL
, 0, &res
);
2705 g_assert_cmpint(err
, ==, 0);
2706 g_assert_cmpuint(res
, ==, 18446744073709551614ull);
2709 static void test_qemu_strtou64_full_null(void)
2714 err
= qemu_strtou64(NULL
, NULL
, 0, &res
);
2716 g_assert_cmpint(err
, ==, -EINVAL
);
2717 g_assert_cmpuint(res
, ==, 999);
2720 static void test_qemu_strtou64_full_empty(void)
2722 const char *str
= "";
2726 err
= qemu_strtou64(str
, NULL
, 0, &res
);
2728 g_assert_cmpint(err
, ==, -EINVAL
);
2729 g_assert_cmpuint(res
, ==, 0);
2732 static void test_qemu_strtou64_full_negative(void)
2734 const char *str
= " \t -321";
2738 err
= qemu_strtou64(str
, NULL
, 0, &res
);
2740 g_assert_cmpint(err
, ==, 0);
2741 g_assert_cmpuint(res
, ==, -321ull);
2744 static void test_qemu_strtou64_full_negzero(void)
2746 const char *str
= " -0";
2750 err
= qemu_strtou64(str
, NULL
, 0, &res
);
2752 g_assert_cmpint(err
, ==, 0);
2753 g_assert_cmpuint(res
, ==, 0);
2756 static void test_qemu_strtou64_full_trailing(void)
2758 const char *str
= "18446744073709551614xxxxxx";
2762 err
= qemu_strtou64(str
, NULL
, 0, &res
);
2764 g_assert_cmpint(err
, ==, -EINVAL
);
2765 g_assert_cmpuint(res
, ==, 18446744073709551614ULL);
2768 static void test_qemu_strtou64_full_max(void)
2770 char *str
= g_strdup_printf("%lld", ULLONG_MAX
);
2774 err
= qemu_strtou64(str
, NULL
, 0, &res
);
2776 g_assert_cmpint(err
, ==, 0);
2777 g_assert_cmphex(res
, ==, ULLONG_MAX
);
2781 static void test_qemu_strtou64_full_erange_junk(void)
2783 /* EINVAL has priority over ERANGE */
2784 const char *str
= "-99999999999999999999junk";
2788 err
= qemu_strtou64(str
, NULL
, 0, &res
);
2790 g_assert_cmpint(err
, ==, -EINVAL
);
2791 g_assert_cmpuint(res
, ==, UINT64_MAX
);
2794 static void test_qemu_strtod_simple(void)
2801 /* no radix or exponent */
2803 endptr
= "somewhere";
2805 err
= qemu_strtod(str
, &endptr
, &res
);
2806 g_assert_cmpint(err
, ==, 0);
2807 g_assert_cmpfloat(res
, ==, 1.0);
2808 g_assert_true(endptr
== str
+ 1);
2810 /* leading space and sign */
2812 endptr
= "somewhere";
2814 err
= qemu_strtod(str
, &endptr
, &res
);
2815 g_assert_cmpint(err
, ==, 0);
2816 g_assert_cmpfloat(res
, ==, -0.0);
2817 g_assert_true(signbit(res
));
2818 g_assert_true(endptr
== str
+ 5);
2822 endptr
= "somewhere";
2824 err
= qemu_strtod(str
, &endptr
, &res
);
2825 g_assert_cmpint(err
, ==, 0);
2826 g_assert_cmpfloat(res
, ==, 0.5);
2827 g_assert_true(endptr
== str
+ 3);
2831 endptr
= "somewhere";
2833 err
= qemu_strtod(str
, &endptr
, &res
);
2834 g_assert_cmpint(err
, ==, 0);
2835 g_assert_cmpfloat(res
, ==, 10.0);
2836 g_assert_true(endptr
== str
+ 5);
2838 /* hex without radix */
2840 endptr
= "somewhere";
2842 err
= qemu_strtod(str
, &endptr
, &res
);
2843 g_assert_cmpint(err
, ==, 0);
2844 g_assert_cmpfloat(res
, ==, 16.0);
2845 g_assert_true(endptr
== str
+ 4);
2848 static void test_qemu_strtod_einval(void)
2857 endptr
= "somewhere";
2859 err
= qemu_strtod(str
, &endptr
, &res
);
2860 g_assert_cmpint(err
, ==, -EINVAL
);
2861 g_assert_cmpfloat(res
, ==, 0.0);
2862 g_assert_false(signbit(res
));
2863 g_assert_true(endptr
== str
);
2869 err
= qemu_strtod(str
, &endptr
, &res
);
2870 g_assert_cmpint(err
, ==, -EINVAL
);
2871 g_assert_cmpfloat(res
, ==, 999.0);
2872 g_assert_null(endptr
);
2874 /* not recognizable */
2876 endptr
= "somewhere";
2878 err
= qemu_strtod(str
, &endptr
, &res
);
2879 g_assert_cmpint(err
, ==, -EINVAL
);
2880 g_assert_cmpfloat(res
, ==, 0.0);
2881 g_assert_false(signbit(res
));
2882 g_assert_true(endptr
== str
);
2885 static void test_qemu_strtod_erange(void)
2894 endptr
= "somewhere";
2896 err
= qemu_strtod(str
, &endptr
, &res
);
2897 g_assert_cmpint(err
, ==, -ERANGE
);
2898 g_assert_cmpfloat(res
, ==, HUGE_VAL
);
2899 g_assert_true(endptr
== str
+ 5);
2902 endptr
= "somewhere";
2904 err
= qemu_strtod(str
, &endptr
, &res
);
2905 g_assert_cmpint(err
, ==, -ERANGE
);
2906 g_assert_cmpfloat(res
, ==, -HUGE_VAL
);
2907 g_assert_true(endptr
== str
+ 7);
2911 endptr
= "somewhere";
2913 err
= qemu_strtod(str
, &endptr
, &res
);
2914 g_assert_cmpint(err
, ==, -ERANGE
);
2915 g_assert_cmpfloat(res
, >=, -DBL_MIN
);
2916 g_assert_cmpfloat(res
, <=, -0.0);
2917 g_assert_true(signbit(res
));
2918 g_assert_true(endptr
== str
+ 7);
2921 static void test_qemu_strtod_nonfinite(void)
2930 endptr
= "somewhere";
2932 err
= qemu_strtod(str
, &endptr
, &res
);
2933 g_assert_cmpint(err
, ==, 0);
2934 g_assert_true(isinf(res
));
2935 g_assert_false(signbit(res
));
2936 g_assert_true(endptr
== str
+ 3);
2939 endptr
= "somewhere";
2941 err
= qemu_strtod(str
, &endptr
, &res
);
2942 g_assert_cmpint(err
, ==, 0);
2943 g_assert_true(isinf(res
));
2944 g_assert_true(signbit(res
));
2945 g_assert_true(endptr
== str
+ 9);
2949 endptr
= "somewhere";
2951 err
= qemu_strtod(str
, &endptr
, &res
);
2952 g_assert_cmpint(err
, ==, 0);
2953 g_assert_true(isnan(res
));
2954 g_assert_true(endptr
== str
+ 4);
2957 static void test_qemu_strtod_trailing(void)
2964 /* trailing whitespace */
2966 endptr
= "somewhere";
2968 err
= qemu_strtod(str
, &endptr
, &res
);
2969 g_assert_cmpint(err
, ==, 0);
2970 g_assert_cmpfloat(res
, ==, 1.0);
2971 g_assert_true(endptr
== str
+ 2);
2973 endptr
= "somewhere";
2975 err
= qemu_strtod(str
, NULL
, &res
);
2976 g_assert_cmpint(err
, ==, -EINVAL
);
2977 g_assert_cmpfloat(res
, ==, 1.0);
2979 /* trailing e is not an exponent */
2981 endptr
= "somewhere";
2983 err
= qemu_strtod(str
, &endptr
, &res
);
2984 g_assert_cmpint(err
, ==, 0);
2985 g_assert_cmpfloat(res
, ==, 0.5);
2986 g_assert_true(endptr
== str
+ 2);
2988 endptr
= "somewhere";
2990 err
= qemu_strtod(str
, NULL
, &res
);
2991 g_assert_cmpint(err
, ==, -EINVAL
);
2992 g_assert_cmpfloat(res
, ==, 0.5);
2994 /* trailing ( not part of long NaN */
2996 endptr
= "somewhere";
2998 err
= qemu_strtod(str
, &endptr
, &res
);
2999 g_assert_cmpint(err
, ==, 0);
3000 g_assert_true(isnan(res
));
3001 g_assert_true(endptr
== str
+ 3);
3003 endptr
= "somewhere";
3005 err
= qemu_strtod(str
, NULL
, &res
);
3006 g_assert_cmpint(err
, ==, -EINVAL
);
3007 g_assert_true(isnan(res
));
3010 static void test_qemu_strtod_erange_junk(void)
3017 /* ERANGE with trailing junk... */
3019 endptr
= "somewhere";
3021 err
= qemu_strtod(str
, &endptr
, &res
);
3022 g_assert_cmpint(err
, ==, -ERANGE
);
3023 g_assert_cmpfloat(res
, <=, DBL_MIN
);
3024 g_assert_cmpfloat(res
, >=, 0.0);
3025 g_assert_false(signbit(res
));
3026 g_assert_true(endptr
== str
+ 6);
3028 /* ...has less priority than EINVAL when full parse not possible */
3029 endptr
= "somewhere";
3031 err
= qemu_strtod(str
, NULL
, &res
);
3032 g_assert_cmpint(err
, ==, -EINVAL
);
3033 g_assert_cmpfloat(res
, ==, 0.0);
3034 g_assert_false(signbit(res
));
3037 static void test_qemu_strtod_finite_simple(void)
3044 /* no radix or exponent */
3046 endptr
= "somewhere";
3048 err
= qemu_strtod_finite(str
, &endptr
, &res
);
3049 g_assert_cmpint(err
, ==, 0);
3050 g_assert_cmpfloat(res
, ==, 1.0);
3051 g_assert_true(endptr
== str
+ 1);
3053 /* leading space and sign */
3055 endptr
= "somewhere";
3057 err
= qemu_strtod_finite(str
, &endptr
, &res
);
3058 g_assert_cmpint(err
, ==, 0);
3059 g_assert_cmpfloat(res
, ==, -0.0);
3060 g_assert_true(signbit(res
));
3061 g_assert_true(endptr
== str
+ 5);
3065 endptr
= "somewhere";
3067 err
= qemu_strtod_finite(str
, &endptr
, &res
);
3068 g_assert_cmpint(err
, ==, 0);
3069 g_assert_cmpfloat(res
, ==, 0.5);
3070 g_assert_true(endptr
== str
+ 3);
3074 endptr
= "somewhere";
3076 err
= qemu_strtod_finite(str
, &endptr
, &res
);
3077 g_assert_cmpint(err
, ==, 0);
3078 g_assert_cmpfloat(res
, ==, 10.0);
3079 g_assert_true(endptr
== str
+ 5);
3081 /* hex without radix */
3083 endptr
= "somewhere";
3085 err
= qemu_strtod(str
, &endptr
, &res
);
3086 g_assert_cmpint(err
, ==, 0);
3087 g_assert_cmpfloat(res
, ==, 16.0);
3088 g_assert_true(endptr
== str
+ 4);
3091 static void test_qemu_strtod_finite_einval(void)
3100 endptr
= "somewhere";
3102 err
= qemu_strtod_finite(str
, &endptr
, &res
);
3103 g_assert_cmpint(err
, ==, -EINVAL
);
3104 g_assert_cmpfloat(res
, ==, 999.0);
3105 g_assert_true(endptr
== str
);
3111 err
= qemu_strtod_finite(str
, &endptr
, &res
);
3112 g_assert_cmpint(err
, ==, -EINVAL
);
3113 g_assert_cmpfloat(res
, ==, 999.0);
3114 g_assert_null(endptr
);
3116 /* not recognizable */
3118 endptr
= "somewhere";
3120 err
= qemu_strtod_finite(str
, &endptr
, &res
);
3121 g_assert_cmpint(err
, ==, -EINVAL
);
3122 g_assert_cmpfloat(res
, ==, 999.0);
3123 g_assert_true(endptr
== str
);
3126 static void test_qemu_strtod_finite_erange(void)
3135 endptr
= "somewhere";
3137 err
= qemu_strtod_finite(str
, &endptr
, &res
);
3138 g_assert_cmpint(err
, ==, -ERANGE
);
3139 g_assert_cmpfloat(res
, ==, HUGE_VAL
);
3140 g_assert_true(endptr
== str
+ 5);
3143 endptr
= "somewhere";
3145 err
= qemu_strtod_finite(str
, &endptr
, &res
);
3146 g_assert_cmpint(err
, ==, -ERANGE
);
3147 g_assert_cmpfloat(res
, ==, -HUGE_VAL
);
3148 g_assert_true(endptr
== str
+ 7);
3152 endptr
= "somewhere";
3154 err
= qemu_strtod_finite(str
, &endptr
, &res
);
3155 g_assert_cmpint(err
, ==, -ERANGE
);
3156 g_assert_cmpfloat(res
, >=, -DBL_MIN
);
3157 g_assert_cmpfloat(res
, <=, -0.0);
3158 g_assert_true(signbit(res
));
3159 g_assert_true(endptr
== str
+ 7);
3162 static void test_qemu_strtod_finite_nonfinite(void)
3171 endptr
= "somewhere";
3173 err
= qemu_strtod_finite(str
, &endptr
, &res
);
3174 g_assert_cmpint(err
, ==, -EINVAL
);
3175 g_assert_cmpfloat(res
, ==, 999.0);
3176 g_assert_true(endptr
== str
);
3179 endptr
= "somewhere";
3181 err
= qemu_strtod_finite(str
, &endptr
, &res
);
3182 g_assert_cmpint(err
, ==, -EINVAL
);
3183 g_assert_cmpfloat(res
, ==, 999.0);
3184 g_assert_true(endptr
== str
);
3188 endptr
= "somewhere";
3190 err
= qemu_strtod_finite(str
, &endptr
, &res
);
3191 g_assert_cmpint(err
, ==, -EINVAL
);
3192 g_assert_cmpfloat(res
, ==, 999.0);
3193 g_assert_true(endptr
== str
);
3196 static void test_qemu_strtod_finite_trailing(void)
3203 /* trailing whitespace */
3205 endptr
= "somewhere";
3207 err
= qemu_strtod_finite(str
, &endptr
, &res
);
3208 g_assert_cmpint(err
, ==, 0);
3209 g_assert_cmpfloat(res
, ==, 1.0);
3210 g_assert_true(endptr
== str
+ 2);
3212 endptr
= "somewhere";
3214 err
= qemu_strtod_finite(str
, NULL
, &res
);
3215 g_assert_cmpint(err
, ==, -EINVAL
);
3216 g_assert_cmpfloat(res
, ==, 999.0);
3218 /* trailing e is not an exponent */
3220 endptr
= "somewhere";
3222 err
= qemu_strtod_finite(str
, &endptr
, &res
);
3223 g_assert_cmpint(err
, ==, 0);
3224 g_assert_cmpfloat(res
, ==, 0.5);
3225 g_assert_true(endptr
== str
+ 2);
3227 endptr
= "somewhere";
3229 err
= qemu_strtod_finite(str
, NULL
, &res
);
3230 g_assert_cmpint(err
, ==, -EINVAL
);
3231 g_assert_cmpfloat(res
, ==, 999.0);
3233 /* trailing ( not part of long NaN */
3235 endptr
= "somewhere";
3237 err
= qemu_strtod_finite(str
, &endptr
, &res
);
3238 g_assert_cmpint(err
, ==, -EINVAL
);
3239 g_assert_cmpfloat(res
, ==, 999.0);
3240 g_assert_true(endptr
== str
);
3242 endptr
= "somewhere";
3244 err
= qemu_strtod_finite(str
, NULL
, &res
);
3245 g_assert_cmpint(err
, ==, -EINVAL
);
3246 g_assert_cmpfloat(res
, ==, 999.0);
3249 static void test_qemu_strtod_finite_erange_junk(void)
3256 /* ERANGE with trailing junk... */
3258 endptr
= "somewhere";
3260 err
= qemu_strtod_finite(str
, &endptr
, &res
);
3261 g_assert_cmpint(err
, ==, -ERANGE
);
3262 g_assert_cmpfloat(res
, <=, DBL_MIN
);
3263 g_assert_cmpfloat(res
, >=, 0.0);
3264 g_assert_false(signbit(res
));
3265 g_assert_true(endptr
== str
+ 6);
3267 /* ...has less priority than EINVAL when full parse not possible */
3268 endptr
= "somewhere";
3270 err
= qemu_strtod_finite(str
, NULL
, &res
);
3271 g_assert_cmpint(err
, ==, -EINVAL
);
3272 g_assert_cmpfloat(res
, ==, 999.0);
3275 static void test_qemu_strtosz_simple(void)
3285 err
= qemu_strtosz(str
, &endptr
, &res
);
3286 g_assert_cmpint(err
, ==, 0);
3287 g_assert_cmpuint(res
, ==, 0);
3288 g_assert_true(endptr
== str
+ 1);
3290 /* Leading 0 gives decimal results, not octal */
3294 err
= qemu_strtosz(str
, &endptr
, &res
);
3295 g_assert_cmpint(err
, ==, 0);
3296 g_assert_cmpuint(res
, ==, 8);
3297 g_assert_true(endptr
== str
+ 2);
3299 /* Leading space is ignored */
3303 err
= qemu_strtosz(str
, &endptr
, &res
);
3304 g_assert_cmpint(err
, ==, 0);
3305 g_assert_cmpuint(res
, ==, 12345);
3306 g_assert_true(endptr
== str
+ 6);
3309 err
= qemu_strtosz(str
, NULL
, &res
);
3310 g_assert_cmpint(err
, ==, 0);
3311 g_assert_cmpuint(res
, ==, 12345);
3313 str
= "9007199254740991"; /* 2^53-1 */
3316 err
= qemu_strtosz(str
, &endptr
, &res
);
3317 g_assert_cmpint(err
, ==, 0);
3318 g_assert_cmphex(res
, ==, 0x1fffffffffffffULL
);
3319 g_assert_true(endptr
== str
+ 16);
3321 str
= "9007199254740992"; /* 2^53 */
3324 err
= qemu_strtosz(str
, &endptr
, &res
);
3325 g_assert_cmpint(err
, ==, 0);
3326 g_assert_cmphex(res
, ==, 0x20000000000000ULL
);
3327 g_assert_true(endptr
== str
+ 16);
3329 str
= "9007199254740993"; /* 2^53+1 */
3332 err
= qemu_strtosz(str
, &endptr
, &res
);
3333 g_assert_cmpint(err
, ==, 0);
3334 g_assert_cmphex(res
, ==, 0x20000000000001ULL
);
3335 g_assert_true(endptr
== str
+ 16);
3337 str
= "18446744073709549568"; /* 0xfffffffffffff800 (53 msbs set) */
3340 err
= qemu_strtosz(str
, &endptr
, &res
);
3341 g_assert_cmpint(err
, ==, 0);
3342 g_assert_cmphex(res
, ==, 0xfffffffffffff800ULL
);
3343 g_assert_true(endptr
== str
+ 20);
3345 str
= "18446744073709550591"; /* 0xfffffffffffffbff */
3348 err
= qemu_strtosz(str
, &endptr
, &res
);
3349 g_assert_cmpint(err
, ==, 0);
3350 g_assert_cmphex(res
, ==, 0xfffffffffffffbffULL
);
3351 g_assert_true(endptr
== str
+ 20);
3353 str
= "18446744073709551615"; /* 0xffffffffffffffff */
3356 err
= qemu_strtosz(str
, &endptr
, &res
);
3357 g_assert_cmpint(err
, ==, 0);
3358 g_assert_cmphex(res
, ==, 0xffffffffffffffffULL
);
3359 g_assert_true(endptr
== str
+ 20);
3362 static void test_qemu_strtosz_hex(void)
3372 err
= qemu_strtosz(str
, &endptr
, &res
);
3373 g_assert_cmpint(err
, ==, 0);
3374 g_assert_cmpuint(res
, ==, 0);
3375 g_assert_true(endptr
== str
+ 3);
3380 err
= qemu_strtosz(str
, &endptr
, &res
);
3381 g_assert_cmpint(err
, ==, 0);
3382 g_assert_cmpuint(res
, ==, 171);
3383 g_assert_true(endptr
== str
+ 4);
3388 err
= qemu_strtosz(str
, &endptr
, &res
);
3389 g_assert_cmpint(err
, ==, 0);
3390 g_assert_cmpuint(res
, ==, 174);
3391 g_assert_true(endptr
== str
+ 4);
3394 static void test_qemu_strtosz_units(void)
3396 const char *none
= "1";
3397 const char *b
= "1B";
3398 const char *k
= "1K";
3399 const char *m
= "1M";
3400 const char *g
= "1G";
3401 const char *t
= "1T";
3402 const char *p
= "1P";
3403 const char *e
= "1E";
3411 err
= qemu_strtosz_MiB(none
, &endptr
, &res
);
3412 g_assert_cmpint(err
, ==, 0);
3413 g_assert_cmpuint(res
, ==, MiB
);
3414 g_assert_true(endptr
== none
+ 1);
3418 err
= qemu_strtosz(b
, &endptr
, &res
);
3419 g_assert_cmpint(err
, ==, 0);
3420 g_assert_cmpuint(res
, ==, 1);
3421 g_assert_true(endptr
== b
+ 2);
3425 err
= qemu_strtosz(k
, &endptr
, &res
);
3426 g_assert_cmpint(err
, ==, 0);
3427 g_assert_cmpuint(res
, ==, KiB
);
3428 g_assert_true(endptr
== k
+ 2);
3432 err
= qemu_strtosz(m
, &endptr
, &res
);
3433 g_assert_cmpint(err
, ==, 0);
3434 g_assert_cmpuint(res
, ==, MiB
);
3435 g_assert_true(endptr
== m
+ 2);
3439 err
= qemu_strtosz(g
, &endptr
, &res
);
3440 g_assert_cmpint(err
, ==, 0);
3441 g_assert_cmpuint(res
, ==, GiB
);
3442 g_assert_true(endptr
== g
+ 2);
3446 err
= qemu_strtosz(t
, &endptr
, &res
);
3447 g_assert_cmpint(err
, ==, 0);
3448 g_assert_cmpuint(res
, ==, TiB
);
3449 g_assert_true(endptr
== t
+ 2);
3453 err
= qemu_strtosz(p
, &endptr
, &res
);
3454 g_assert_cmpint(err
, ==, 0);
3455 g_assert_cmpuint(res
, ==, PiB
);
3456 g_assert_true(endptr
== p
+ 2);
3460 err
= qemu_strtosz(e
, &endptr
, &res
);
3461 g_assert_cmpint(err
, ==, 0);
3462 g_assert_cmpuint(res
, ==, EiB
);
3463 g_assert_true(endptr
== e
+ 2);
3466 static void test_qemu_strtosz_float(void)
3476 err
= qemu_strtosz(str
, &endptr
, &res
);
3477 g_assert_cmpint(err
, ==, 0);
3478 g_assert_cmpuint(res
, ==, EiB
/ 2);
3479 g_assert_true(endptr
== str
+ 4);
3481 /* For convenience, a fraction of 0 is tolerated even on bytes */
3485 err
= qemu_strtosz(str
, &endptr
, &res
);
3486 g_assert_cmpint(err
, ==, 0);
3487 g_assert_cmpuint(res
, ==, 1);
3488 g_assert_true(endptr
== str
+ 4);
3490 /* An empty fraction is tolerated */
3494 err
= qemu_strtosz(str
, &endptr
, &res
);
3495 g_assert_cmpint(err
, ==, 0);
3496 g_assert_cmpuint(res
, ==, 1024);
3497 g_assert_true(endptr
== str
+ 3);
3499 /* For convenience, we permit values that are not byte-exact */
3503 err
= qemu_strtosz(str
, &endptr
, &res
);
3504 g_assert_cmpint(err
, ==, 0);
3505 g_assert_cmpuint(res
, ==, (uint64_t) (12.345 * MiB
+ 0.5));
3506 g_assert_true(endptr
== str
+ 7);
3509 static void test_qemu_strtosz_invalid(void)
3514 uint64_t res
= 0xbaadf00d;
3518 err
= qemu_strtosz(str
, &endptr
, &res
);
3519 g_assert_cmpint(err
, ==, -EINVAL
);
3520 g_assert_cmphex(res
, ==, 0xbaadf00d);
3521 g_assert_true(endptr
== str
);
3525 err
= qemu_strtosz(str
, &endptr
, &res
);
3526 g_assert_cmpint(err
, ==, -EINVAL
);
3527 g_assert_cmphex(res
, ==, 0xbaadf00d);
3528 g_assert_true(endptr
== str
);
3532 err
= qemu_strtosz(str
, &endptr
, &res
);
3533 g_assert_cmpint(err
, ==, -EINVAL
);
3534 g_assert_cmphex(res
, ==, 0xbaadf00d);
3535 g_assert_true(endptr
== str
);
3539 err
= qemu_strtosz(str
, &endptr
, &res
);
3540 g_assert_cmpint(err
, ==, -EINVAL
);
3541 g_assert_cmphex(res
, ==, 0xbaadf00d);
3542 g_assert_true(endptr
== str
);
3546 err
= qemu_strtosz(str
, &endptr
, &res
);
3547 g_assert_cmpint(err
, ==, -EINVAL
);
3548 g_assert_cmphex(res
, ==, 0xbaadf00d);
3549 g_assert_true(endptr
== str
);
3551 /* Fractional values require scale larger than bytes */
3554 err
= qemu_strtosz(str
, &endptr
, &res
);
3555 g_assert_cmpint(err
, ==, -EINVAL
);
3556 g_assert_cmphex(res
, ==, 0xbaadf00d);
3557 g_assert_true(endptr
== str
);
3561 err
= qemu_strtosz(str
, &endptr
, &res
);
3562 g_assert_cmpint(err
, ==, -EINVAL
);
3563 g_assert_cmphex(res
, ==, 0xbaadf00d);
3564 g_assert_true(endptr
== str
);
3566 /* No floating point exponents */
3569 err
= qemu_strtosz(str
, &endptr
, &res
);
3570 g_assert_cmpint(err
, ==, -EINVAL
);
3571 g_assert_cmphex(res
, ==, 0xbaadf00d);
3572 g_assert_true(endptr
== str
);
3576 err
= qemu_strtosz(str
, &endptr
, &res
);
3577 g_assert_cmpint(err
, ==, -EINVAL
);
3578 g_assert_cmphex(res
, ==, 0xbaadf00d);
3579 g_assert_true(endptr
== str
);
3581 /* No hex fractions */
3584 err
= qemu_strtosz(str
, &endptr
, &res
);
3585 g_assert_cmpint(err
, ==, -EINVAL
);
3586 g_assert_cmphex(res
, ==, 0xbaadf00d);
3587 g_assert_true(endptr
== str
);
3592 err
= qemu_strtosz(str
, &endptr
, &res
);
3593 g_assert_cmpint(err
, ==, -EINVAL
);
3594 g_assert_cmphex(res
, ==, 0xbaadf00d);
3595 g_assert_true(endptr
== str
);
3597 /* No negative values */
3600 err
= qemu_strtosz(str
, &endptr
, &res
);
3601 g_assert_cmpint(err
, ==, -EINVAL
);
3602 g_assert_cmphex(res
, ==, 0xbaadf00d);
3603 g_assert_true(endptr
== str
);
3607 err
= qemu_strtosz(str
, &endptr
, &res
);
3608 g_assert_cmpint(err
, ==, -EINVAL
);
3609 g_assert_cmphex(res
, ==, 0xbaadf00d);
3610 g_assert_true(endptr
== str
);
3613 static void test_qemu_strtosz_trailing(void)
3623 err
= qemu_strtosz_MiB(str
, &endptr
, &res
);
3624 g_assert_cmpint(err
, ==, 0);
3625 g_assert_cmpuint(res
, ==, 123 * MiB
);
3626 g_assert_true(endptr
== str
+ 3);
3629 err
= qemu_strtosz(str
, NULL
, &res
);
3630 g_assert_cmpint(err
, ==, -EINVAL
);
3631 g_assert_cmphex(res
, ==, 0xbaadf00d);
3636 err
= qemu_strtosz(str
, &endptr
, &res
);
3637 g_assert_cmpint(err
, ==, 0);
3638 g_assert_cmpuint(res
, ==, 1024);
3639 g_assert_true(endptr
== str
+ 2);
3642 err
= qemu_strtosz(str
, NULL
, &res
);
3643 g_assert_cmpint(err
, ==, -EINVAL
);
3644 g_assert_cmphex(res
, ==, 0xbaadf00d);
3649 err
= qemu_strtosz(str
, &endptr
, &res
);
3650 g_assert_cmpint(err
, ==, 0);
3651 g_assert_cmpuint(res
, ==, 0);
3652 g_assert_true(endptr
== str
+ 1);
3655 err
= qemu_strtosz(str
, NULL
, &res
);
3656 g_assert_cmpint(err
, ==, -EINVAL
);
3657 g_assert_cmphex(res
, ==, 0xbaadf00d);
3662 err
= qemu_strtosz(str
, &endptr
, &res
);
3663 g_assert_cmpint(err
, ==, 0);
3664 g_assert_cmpuint(res
, ==, 0);
3665 g_assert_true(endptr
== str
+ 2);
3668 err
= qemu_strtosz(str
, NULL
, &res
);
3669 g_assert_cmpint(err
, ==, -EINVAL
);
3670 g_assert_cmphex(res
, ==, 0xbaadf00d);
3675 err
= qemu_strtosz(str
, &endptr
, &res
);
3676 g_assert_cmpint(err
, ==, 0);
3677 g_assert_cmpuint(res
, ==, 123);
3678 g_assert_true(endptr
== str
+ 3);
3681 err
= qemu_strtosz(str
, NULL
, &res
);
3682 g_assert_cmpint(err
, ==, -EINVAL
);
3683 g_assert_cmphex(res
, ==, 0xbaadf00d);
3686 static void test_qemu_strtosz_erange(void)
3691 uint64_t res
= 0xbaadf00d;
3693 str
= "18446744073709551616"; /* 2^64; see strtosz_simple for 2^64-1 */
3695 err
= qemu_strtosz(str
, &endptr
, &res
);
3696 g_assert_cmpint(err
, ==, -ERANGE
);
3697 g_assert_cmphex(res
, ==, 0xbaadf00d);
3698 g_assert_true(endptr
== str
+ 20);
3702 err
= qemu_strtosz(str
, &endptr
, &res
);
3703 g_assert_cmpint(err
, ==, -ERANGE
);
3704 g_assert_cmphex(res
, ==, 0xbaadf00d);
3705 g_assert_true(endptr
== str
+ 3);
3708 static void test_qemu_strtosz_metric(void)
3718 err
= qemu_strtosz_metric(str
, &endptr
, &res
);
3719 g_assert_cmpint(err
, ==, 0);
3720 g_assert_cmpuint(res
, ==, 12345000);
3721 g_assert_true(endptr
== str
+ 6);
3726 err
= qemu_strtosz_metric(str
, &endptr
, &res
);
3727 g_assert_cmpint(err
, ==, 0);
3728 g_assert_cmpuint(res
, ==, 12345000);
3729 g_assert_true(endptr
== str
+ 7);
3732 static void test_freq_to_str(void)
3736 str
= freq_to_str(999);
3737 g_assert_cmpstr(str
, ==, "999 Hz");
3740 str
= freq_to_str(1000);
3741 g_assert_cmpstr(str
, ==, "1 KHz");
3744 str
= freq_to_str(1010);
3745 g_assert_cmpstr(str
, ==, "1.01 KHz");
3749 static void test_size_to_str(void)
3753 str
= size_to_str(0);
3754 g_assert_cmpstr(str
, ==, "0 B");
3757 str
= size_to_str(1);
3758 g_assert_cmpstr(str
, ==, "1 B");
3761 str
= size_to_str(1016);
3762 g_assert_cmpstr(str
, ==, "0.992 KiB");
3765 str
= size_to_str(1024);
3766 g_assert_cmpstr(str
, ==, "1 KiB");
3769 str
= size_to_str(512ull << 20);
3770 g_assert_cmpstr(str
, ==, "512 MiB");
3774 static void test_iec_binary_prefix(void)
3776 g_assert_cmpstr(iec_binary_prefix(0), ==, "");
3777 g_assert_cmpstr(iec_binary_prefix(10), ==, "Ki");
3778 g_assert_cmpstr(iec_binary_prefix(20), ==, "Mi");
3779 g_assert_cmpstr(iec_binary_prefix(30), ==, "Gi");
3780 g_assert_cmpstr(iec_binary_prefix(40), ==, "Ti");
3781 g_assert_cmpstr(iec_binary_prefix(50), ==, "Pi");
3782 g_assert_cmpstr(iec_binary_prefix(60), ==, "Ei");
3785 static void test_si_prefix(void)
3787 g_assert_cmpstr(si_prefix(-18), ==, "a");
3788 g_assert_cmpstr(si_prefix(-15), ==, "f");
3789 g_assert_cmpstr(si_prefix(-12), ==, "p");
3790 g_assert_cmpstr(si_prefix(-9), ==, "n");
3791 g_assert_cmpstr(si_prefix(-6), ==, "u");
3792 g_assert_cmpstr(si_prefix(-3), ==, "m");
3793 g_assert_cmpstr(si_prefix(0), ==, "");
3794 g_assert_cmpstr(si_prefix(3), ==, "K");
3795 g_assert_cmpstr(si_prefix(6), ==, "M");
3796 g_assert_cmpstr(si_prefix(9), ==, "G");
3797 g_assert_cmpstr(si_prefix(12), ==, "T");
3798 g_assert_cmpstr(si_prefix(15), ==, "P");
3799 g_assert_cmpstr(si_prefix(18), ==, "E");
3802 int main(int argc
, char **argv
)
3804 g_test_init(&argc
, &argv
, NULL
);
3806 g_test_add_func("/cutils/parse_uint/null", test_parse_uint_null
);
3807 g_test_add_func("/cutils/parse_uint/empty", test_parse_uint_empty
);
3808 g_test_add_func("/cutils/parse_uint/whitespace",
3809 test_parse_uint_whitespace
);
3810 g_test_add_func("/cutils/parse_uint/invalid", test_parse_uint_invalid
);
3811 g_test_add_func("/cutils/parse_uint/trailing", test_parse_uint_trailing
);
3812 g_test_add_func("/cutils/parse_uint/correct", test_parse_uint_correct
);
3813 g_test_add_func("/cutils/parse_uint/octal", test_parse_uint_octal
);
3814 g_test_add_func("/cutils/parse_uint/decimal", test_parse_uint_decimal
);
3815 g_test_add_func("/cutils/parse_uint/llong_max", test_parse_uint_llong_max
);
3816 g_test_add_func("/cutils/parse_uint/max", test_parse_uint_max
);
3817 g_test_add_func("/cutils/parse_uint/overflow", test_parse_uint_overflow
);
3818 g_test_add_func("/cutils/parse_uint/negative", test_parse_uint_negative
);
3819 g_test_add_func("/cutils/parse_uint/negzero", test_parse_uint_negzero
);
3820 g_test_add_func("/cutils/parse_uint_full/trailing",
3821 test_parse_uint_full_trailing
);
3822 g_test_add_func("/cutils/parse_uint_full/correct",
3823 test_parse_uint_full_correct
);
3824 g_test_add_func("/cutils/parse_uint_full/erange_junk",
3825 test_parse_uint_full_erange_junk
);
3826 g_test_add_func("/cutils/parse_uint_full/null",
3827 test_parse_uint_full_null
);
3829 /* qemu_strtoi() tests */
3830 g_test_add_func("/cutils/qemu_strtoi/correct",
3831 test_qemu_strtoi_correct
);
3832 g_test_add_func("/cutils/qemu_strtoi/null",
3833 test_qemu_strtoi_null
);
3834 g_test_add_func("/cutils/qemu_strtoi/empty",
3835 test_qemu_strtoi_empty
);
3836 g_test_add_func("/cutils/qemu_strtoi/whitespace",
3837 test_qemu_strtoi_whitespace
);
3838 g_test_add_func("/cutils/qemu_strtoi/invalid",
3839 test_qemu_strtoi_invalid
);
3840 g_test_add_func("/cutils/qemu_strtoi/trailing",
3841 test_qemu_strtoi_trailing
);
3842 g_test_add_func("/cutils/qemu_strtoi/octal",
3843 test_qemu_strtoi_octal
);
3844 g_test_add_func("/cutils/qemu_strtoi/decimal",
3845 test_qemu_strtoi_decimal
);
3846 g_test_add_func("/cutils/qemu_strtoi/hex",
3847 test_qemu_strtoi_hex
);
3848 g_test_add_func("/cutils/qemu_strtoi/max",
3849 test_qemu_strtoi_max
);
3850 g_test_add_func("/cutils/qemu_strtoi/overflow",
3851 test_qemu_strtoi_overflow
);
3852 g_test_add_func("/cutils/qemu_strtoi/min",
3853 test_qemu_strtoi_min
);
3854 g_test_add_func("/cutils/qemu_strtoi/underflow",
3855 test_qemu_strtoi_underflow
);
3856 g_test_add_func("/cutils/qemu_strtoi/negative",
3857 test_qemu_strtoi_negative
);
3858 g_test_add_func("/cutils/qemu_strtoi/negzero",
3859 test_qemu_strtoi_negzero
);
3860 g_test_add_func("/cutils/qemu_strtoi_full/correct",
3861 test_qemu_strtoi_full_correct
);
3862 g_test_add_func("/cutils/qemu_strtoi_full/null",
3863 test_qemu_strtoi_full_null
);
3864 g_test_add_func("/cutils/qemu_strtoi_full/empty",
3865 test_qemu_strtoi_full_empty
);
3866 g_test_add_func("/cutils/qemu_strtoi_full/negative",
3867 test_qemu_strtoi_full_negative
);
3868 g_test_add_func("/cutils/qemu_strtoi_full/negzero",
3869 test_qemu_strtoi_full_negzero
);
3870 g_test_add_func("/cutils/qemu_strtoi_full/trailing",
3871 test_qemu_strtoi_full_trailing
);
3872 g_test_add_func("/cutils/qemu_strtoi_full/max",
3873 test_qemu_strtoi_full_max
);
3874 g_test_add_func("/cutils/qemu_strtoi_full/erange_junk",
3875 test_qemu_strtoi_full_erange_junk
);
3877 /* qemu_strtoui() tests */
3878 g_test_add_func("/cutils/qemu_strtoui/correct",
3879 test_qemu_strtoui_correct
);
3880 g_test_add_func("/cutils/qemu_strtoui/null",
3881 test_qemu_strtoui_null
);
3882 g_test_add_func("/cutils/qemu_strtoui/empty",
3883 test_qemu_strtoui_empty
);
3884 g_test_add_func("/cutils/qemu_strtoui/whitespace",
3885 test_qemu_strtoui_whitespace
);
3886 g_test_add_func("/cutils/qemu_strtoui/invalid",
3887 test_qemu_strtoui_invalid
);
3888 g_test_add_func("/cutils/qemu_strtoui/trailing",
3889 test_qemu_strtoui_trailing
);
3890 g_test_add_func("/cutils/qemu_strtoui/octal",
3891 test_qemu_strtoui_octal
);
3892 g_test_add_func("/cutils/qemu_strtoui/decimal",
3893 test_qemu_strtoui_decimal
);
3894 g_test_add_func("/cutils/qemu_strtoui/hex",
3895 test_qemu_strtoui_hex
);
3896 g_test_add_func("/cutils/qemu_strtoui/wrap",
3897 test_qemu_strtoui_wrap
);
3898 g_test_add_func("/cutils/qemu_strtoui/max",
3899 test_qemu_strtoui_max
);
3900 g_test_add_func("/cutils/qemu_strtoui/overflow",
3901 test_qemu_strtoui_overflow
);
3902 g_test_add_func("/cutils/qemu_strtoui/underflow",
3903 test_qemu_strtoui_underflow
);
3904 g_test_add_func("/cutils/qemu_strtoui/negative",
3905 test_qemu_strtoui_negative
);
3906 g_test_add_func("/cutils/qemu_strtoui/negzero",
3907 test_qemu_strtoui_negzero
);
3908 g_test_add_func("/cutils/qemu_strtoui_full/correct",
3909 test_qemu_strtoui_full_correct
);
3910 g_test_add_func("/cutils/qemu_strtoui_full/null",
3911 test_qemu_strtoui_full_null
);
3912 g_test_add_func("/cutils/qemu_strtoui_full/empty",
3913 test_qemu_strtoui_full_empty
);
3914 g_test_add_func("/cutils/qemu_strtoui_full/negative",
3915 test_qemu_strtoui_full_negative
);
3916 g_test_add_func("/cutils/qemu_strtoui_full/negzero",
3917 test_qemu_strtoui_full_negzero
);
3918 g_test_add_func("/cutils/qemu_strtoui_full/trailing",
3919 test_qemu_strtoui_full_trailing
);
3920 g_test_add_func("/cutils/qemu_strtoui_full/max",
3921 test_qemu_strtoui_full_max
);
3922 g_test_add_func("/cutils/qemu_strtoui_full/erange_junk",
3923 test_qemu_strtoui_full_erange_junk
);
3925 /* qemu_strtol() tests */
3926 g_test_add_func("/cutils/qemu_strtol/correct",
3927 test_qemu_strtol_correct
);
3928 g_test_add_func("/cutils/qemu_strtol/null",
3929 test_qemu_strtol_null
);
3930 g_test_add_func("/cutils/qemu_strtol/empty",
3931 test_qemu_strtol_empty
);
3932 g_test_add_func("/cutils/qemu_strtol/whitespace",
3933 test_qemu_strtol_whitespace
);
3934 g_test_add_func("/cutils/qemu_strtol/invalid",
3935 test_qemu_strtol_invalid
);
3936 g_test_add_func("/cutils/qemu_strtol/trailing",
3937 test_qemu_strtol_trailing
);
3938 g_test_add_func("/cutils/qemu_strtol/octal",
3939 test_qemu_strtol_octal
);
3940 g_test_add_func("/cutils/qemu_strtol/decimal",
3941 test_qemu_strtol_decimal
);
3942 g_test_add_func("/cutils/qemu_strtol/hex",
3943 test_qemu_strtol_hex
);
3944 g_test_add_func("/cutils/qemu_strtol/max",
3945 test_qemu_strtol_max
);
3946 g_test_add_func("/cutils/qemu_strtol/overflow",
3947 test_qemu_strtol_overflow
);
3948 g_test_add_func("/cutils/qemu_strtol/min",
3949 test_qemu_strtol_min
);
3950 g_test_add_func("/cutils/qemu_strtol/underflow",
3951 test_qemu_strtol_underflow
);
3952 g_test_add_func("/cutils/qemu_strtol/negative",
3953 test_qemu_strtol_negative
);
3954 g_test_add_func("/cutils/qemu_strtol/negzero",
3955 test_qemu_strtol_negzero
);
3956 g_test_add_func("/cutils/qemu_strtol_full/correct",
3957 test_qemu_strtol_full_correct
);
3958 g_test_add_func("/cutils/qemu_strtol_full/null",
3959 test_qemu_strtol_full_null
);
3960 g_test_add_func("/cutils/qemu_strtol_full/empty",
3961 test_qemu_strtol_full_empty
);
3962 g_test_add_func("/cutils/qemu_strtol_full/negative",
3963 test_qemu_strtol_full_negative
);
3964 g_test_add_func("/cutils/qemu_strtol_full/negzero",
3965 test_qemu_strtol_full_negzero
);
3966 g_test_add_func("/cutils/qemu_strtol_full/trailing",
3967 test_qemu_strtol_full_trailing
);
3968 g_test_add_func("/cutils/qemu_strtol_full/max",
3969 test_qemu_strtol_full_max
);
3970 g_test_add_func("/cutils/qemu_strtol_full/erange_junk",
3971 test_qemu_strtol_full_erange_junk
);
3973 /* qemu_strtoul() tests */
3974 g_test_add_func("/cutils/qemu_strtoul/correct",
3975 test_qemu_strtoul_correct
);
3976 g_test_add_func("/cutils/qemu_strtoul/null",
3977 test_qemu_strtoul_null
);
3978 g_test_add_func("/cutils/qemu_strtoul/empty",
3979 test_qemu_strtoul_empty
);
3980 g_test_add_func("/cutils/qemu_strtoul/whitespace",
3981 test_qemu_strtoul_whitespace
);
3982 g_test_add_func("/cutils/qemu_strtoul/invalid",
3983 test_qemu_strtoul_invalid
);
3984 g_test_add_func("/cutils/qemu_strtoul/trailing",
3985 test_qemu_strtoul_trailing
);
3986 g_test_add_func("/cutils/qemu_strtoul/octal",
3987 test_qemu_strtoul_octal
);
3988 g_test_add_func("/cutils/qemu_strtoul/decimal",
3989 test_qemu_strtoul_decimal
);
3990 g_test_add_func("/cutils/qemu_strtoul/hex",
3991 test_qemu_strtoul_hex
);
3992 g_test_add_func("/cutils/qemu_strtoul/wrap",
3993 test_qemu_strtoul_wrap
);
3994 g_test_add_func("/cutils/qemu_strtoul/max",
3995 test_qemu_strtoul_max
);
3996 g_test_add_func("/cutils/qemu_strtoul/overflow",
3997 test_qemu_strtoul_overflow
);
3998 g_test_add_func("/cutils/qemu_strtoul/underflow",
3999 test_qemu_strtoul_underflow
);
4000 g_test_add_func("/cutils/qemu_strtoul/negative",
4001 test_qemu_strtoul_negative
);
4002 g_test_add_func("/cutils/qemu_strtoul/negzero",
4003 test_qemu_strtoul_negzero
);
4004 g_test_add_func("/cutils/qemu_strtoul_full/correct",
4005 test_qemu_strtoul_full_correct
);
4006 g_test_add_func("/cutils/qemu_strtoul_full/null",
4007 test_qemu_strtoul_full_null
);
4008 g_test_add_func("/cutils/qemu_strtoul_full/empty",
4009 test_qemu_strtoul_full_empty
);
4010 g_test_add_func("/cutils/qemu_strtoul_full/negative",
4011 test_qemu_strtoul_full_negative
);
4012 g_test_add_func("/cutils/qemu_strtoul_full/negzero",
4013 test_qemu_strtoul_full_negzero
);
4014 g_test_add_func("/cutils/qemu_strtoul_full/trailing",
4015 test_qemu_strtoul_full_trailing
);
4016 g_test_add_func("/cutils/qemu_strtoul_full/max",
4017 test_qemu_strtoul_full_max
);
4018 g_test_add_func("/cutils/qemu_strtoul_full/erange_junk",
4019 test_qemu_strtoul_full_erange_junk
);
4021 /* qemu_strtoi64() tests */
4022 g_test_add_func("/cutils/qemu_strtoi64/correct",
4023 test_qemu_strtoi64_correct
);
4024 g_test_add_func("/cutils/qemu_strtoi64/null",
4025 test_qemu_strtoi64_null
);
4026 g_test_add_func("/cutils/qemu_strtoi64/empty",
4027 test_qemu_strtoi64_empty
);
4028 g_test_add_func("/cutils/qemu_strtoi64/whitespace",
4029 test_qemu_strtoi64_whitespace
);
4030 g_test_add_func("/cutils/qemu_strtoi64/invalid",
4031 test_qemu_strtoi64_invalid
);
4032 g_test_add_func("/cutils/qemu_strtoi64/trailing",
4033 test_qemu_strtoi64_trailing
);
4034 g_test_add_func("/cutils/qemu_strtoi64/octal",
4035 test_qemu_strtoi64_octal
);
4036 g_test_add_func("/cutils/qemu_strtoi64/decimal",
4037 test_qemu_strtoi64_decimal
);
4038 g_test_add_func("/cutils/qemu_strtoi64/hex",
4039 test_qemu_strtoi64_hex
);
4040 g_test_add_func("/cutils/qemu_strtoi64/max",
4041 test_qemu_strtoi64_max
);
4042 g_test_add_func("/cutils/qemu_strtoi64/overflow",
4043 test_qemu_strtoi64_overflow
);
4044 g_test_add_func("/cutils/qemu_strtoi64/min",
4045 test_qemu_strtoi64_min
);
4046 g_test_add_func("/cutils/qemu_strtoi64/underflow",
4047 test_qemu_strtoi64_underflow
);
4048 g_test_add_func("/cutils/qemu_strtoi64/negative",
4049 test_qemu_strtoi64_negative
);
4050 g_test_add_func("/cutils/qemu_strtoi64/negzero",
4051 test_qemu_strtoi64_negzero
);
4052 g_test_add_func("/cutils/qemu_strtoi64_full/correct",
4053 test_qemu_strtoi64_full_correct
);
4054 g_test_add_func("/cutils/qemu_strtoi64_full/null",
4055 test_qemu_strtoi64_full_null
);
4056 g_test_add_func("/cutils/qemu_strtoi64_full/empty",
4057 test_qemu_strtoi64_full_empty
);
4058 g_test_add_func("/cutils/qemu_strtoi64_full/negative",
4059 test_qemu_strtoi64_full_negative
);
4060 g_test_add_func("/cutils/qemu_strtoi64_full/negzero",
4061 test_qemu_strtoi64_full_negzero
);
4062 g_test_add_func("/cutils/qemu_strtoi64_full/trailing",
4063 test_qemu_strtoi64_full_trailing
);
4064 g_test_add_func("/cutils/qemu_strtoi64_full/max",
4065 test_qemu_strtoi64_full_max
);
4066 g_test_add_func("/cutils/qemu_strtoi64_full/erange_junk",
4067 test_qemu_strtoi64_full_erange_junk
);
4069 /* qemu_strtou64() tests */
4070 g_test_add_func("/cutils/qemu_strtou64/correct",
4071 test_qemu_strtou64_correct
);
4072 g_test_add_func("/cutils/qemu_strtou64/null",
4073 test_qemu_strtou64_null
);
4074 g_test_add_func("/cutils/qemu_strtou64/empty",
4075 test_qemu_strtou64_empty
);
4076 g_test_add_func("/cutils/qemu_strtou64/whitespace",
4077 test_qemu_strtou64_whitespace
);
4078 g_test_add_func("/cutils/qemu_strtou64/invalid",
4079 test_qemu_strtou64_invalid
);
4080 g_test_add_func("/cutils/qemu_strtou64/trailing",
4081 test_qemu_strtou64_trailing
);
4082 g_test_add_func("/cutils/qemu_strtou64/octal",
4083 test_qemu_strtou64_octal
);
4084 g_test_add_func("/cutils/qemu_strtou64/decimal",
4085 test_qemu_strtou64_decimal
);
4086 g_test_add_func("/cutils/qemu_strtou64/hex",
4087 test_qemu_strtou64_hex
);
4088 g_test_add_func("/cutils/qemu_strtou64/wrap",
4089 test_qemu_strtou64_wrap
);
4090 g_test_add_func("/cutils/qemu_strtou64/max",
4091 test_qemu_strtou64_max
);
4092 g_test_add_func("/cutils/qemu_strtou64/overflow",
4093 test_qemu_strtou64_overflow
);
4094 g_test_add_func("/cutils/qemu_strtou64/underflow",
4095 test_qemu_strtou64_underflow
);
4096 g_test_add_func("/cutils/qemu_strtou64/negative",
4097 test_qemu_strtou64_negative
);
4098 g_test_add_func("/cutils/qemu_strtou64/negzero",
4099 test_qemu_strtou64_negzero
);
4100 g_test_add_func("/cutils/qemu_strtou64_full/correct",
4101 test_qemu_strtou64_full_correct
);
4102 g_test_add_func("/cutils/qemu_strtou64_full/null",
4103 test_qemu_strtou64_full_null
);
4104 g_test_add_func("/cutils/qemu_strtou64_full/empty",
4105 test_qemu_strtou64_full_empty
);
4106 g_test_add_func("/cutils/qemu_strtou64_full/negative",
4107 test_qemu_strtou64_full_negative
);
4108 g_test_add_func("/cutils/qemu_strtou64_full/negzero",
4109 test_qemu_strtou64_full_negzero
);
4110 g_test_add_func("/cutils/qemu_strtou64_full/trailing",
4111 test_qemu_strtou64_full_trailing
);
4112 g_test_add_func("/cutils/qemu_strtou64_full/max",
4113 test_qemu_strtou64_full_max
);
4114 g_test_add_func("/cutils/qemu_strtou64_full/erange_junk",
4115 test_qemu_strtou64_full_erange_junk
);
4117 /* qemu_strtod() tests */
4118 g_test_add_func("/cutils/qemu_strtod/simple",
4119 test_qemu_strtod_simple
);
4120 g_test_add_func("/cutils/qemu_strtod/einval",
4121 test_qemu_strtod_einval
);
4122 g_test_add_func("/cutils/qemu_strtod/erange",
4123 test_qemu_strtod_erange
);
4124 g_test_add_func("/cutils/qemu_strtod/nonfinite",
4125 test_qemu_strtod_nonfinite
);
4126 g_test_add_func("/cutils/qemu_strtod/trailing",
4127 test_qemu_strtod_trailing
);
4128 g_test_add_func("/cutils/qemu_strtod/erange_junk",
4129 test_qemu_strtod_erange_junk
);
4131 /* qemu_strtod_finite() tests */
4132 g_test_add_func("/cutils/qemu_strtod_finite/simple",
4133 test_qemu_strtod_finite_simple
);
4134 g_test_add_func("/cutils/qemu_strtod_finite/einval",
4135 test_qemu_strtod_finite_einval
);
4136 g_test_add_func("/cutils/qemu_strtod_finite/erange",
4137 test_qemu_strtod_finite_erange
);
4138 g_test_add_func("/cutils/qemu_strtod_finite/nonfinite",
4139 test_qemu_strtod_finite_nonfinite
);
4140 g_test_add_func("/cutils/qemu_strtod_finite/trailing",
4141 test_qemu_strtod_finite_trailing
);
4142 g_test_add_func("/cutils/qemu_strtod_finite/erange_junk",
4143 test_qemu_strtod_finite_erange_junk
);
4145 /* qemu_strtosz() tests */
4146 g_test_add_func("/cutils/strtosz/simple",
4147 test_qemu_strtosz_simple
);
4148 g_test_add_func("/cutils/strtosz/hex",
4149 test_qemu_strtosz_hex
);
4150 g_test_add_func("/cutils/strtosz/units",
4151 test_qemu_strtosz_units
);
4152 g_test_add_func("/cutils/strtosz/float",
4153 test_qemu_strtosz_float
);
4154 g_test_add_func("/cutils/strtosz/invalid",
4155 test_qemu_strtosz_invalid
);
4156 g_test_add_func("/cutils/strtosz/trailing",
4157 test_qemu_strtosz_trailing
);
4158 g_test_add_func("/cutils/strtosz/erange",
4159 test_qemu_strtosz_erange
);
4160 g_test_add_func("/cutils/strtosz/metric",
4161 test_qemu_strtosz_metric
);
4163 g_test_add_func("/cutils/size_to_str",
4165 g_test_add_func("/cutils/freq_to_str",
4167 g_test_add_func("/cutils/iec_binary_prefix",
4168 test_iec_binary_prefix
);
4169 g_test_add_func("/cutils/si_prefix",
4171 return g_test_run();