Merge remote-tracking branch 'remotes/armbru/tags/pull-qapi-2015-09-21' into staging
[qemu/ar7.git] / tests / test-cutils.c
blob0046c61fe935392f01f98599d973f9242819600a
1 /*
2 * cutils.c unit-tests
4 * Copyright (C) 2013 Red Hat Inc.
6 * Authors:
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
25 * THE SOFTWARE.
28 #include <glib.h>
29 #include <errno.h>
30 #include <string.h>
32 #include "qemu-common.h"
35 static void test_parse_uint_null(void)
37 unsigned long long i = 999;
38 char f = 'X';
39 char *endptr = &f;
40 int r;
42 r = parse_uint(NULL, &i, &endptr, 0);
44 g_assert_cmpint(r, ==, -EINVAL);
45 g_assert_cmpint(i, ==, 0);
46 g_assert(endptr == NULL);
49 static void test_parse_uint_empty(void)
51 unsigned long long i = 999;
52 char f = 'X';
53 char *endptr = &f;
54 const char *str = "";
55 int r;
57 r = parse_uint(str, &i, &endptr, 0);
59 g_assert_cmpint(r, ==, -EINVAL);
60 g_assert_cmpint(i, ==, 0);
61 g_assert(endptr == str);
64 static void test_parse_uint_whitespace(void)
66 unsigned long long i = 999;
67 char f = 'X';
68 char *endptr = &f;
69 const char *str = " \t ";
70 int r;
72 r = parse_uint(str, &i, &endptr, 0);
74 g_assert_cmpint(r, ==, -EINVAL);
75 g_assert_cmpint(i, ==, 0);
76 g_assert(endptr == str);
80 static void test_parse_uint_invalid(void)
82 unsigned long long i = 999;
83 char f = 'X';
84 char *endptr = &f;
85 const char *str = " \t xxx";
86 int r;
88 r = parse_uint(str, &i, &endptr, 0);
90 g_assert_cmpint(r, ==, -EINVAL);
91 g_assert_cmpint(i, ==, 0);
92 g_assert(endptr == str);
96 static void test_parse_uint_trailing(void)
98 unsigned long long i = 999;
99 char f = 'X';
100 char *endptr = &f;
101 const char *str = "123xxx";
102 int r;
104 r = parse_uint(str, &i, &endptr, 0);
106 g_assert_cmpint(r, ==, 0);
107 g_assert_cmpint(i, ==, 123);
108 g_assert(endptr == str + 3);
111 static void test_parse_uint_correct(void)
113 unsigned long long i = 999;
114 char f = 'X';
115 char *endptr = &f;
116 const char *str = "123";
117 int r;
119 r = parse_uint(str, &i, &endptr, 0);
121 g_assert_cmpint(r, ==, 0);
122 g_assert_cmpint(i, ==, 123);
123 g_assert(endptr == str + strlen(str));
126 static void test_parse_uint_octal(void)
128 unsigned long long i = 999;
129 char f = 'X';
130 char *endptr = &f;
131 const char *str = "0123";
132 int r;
134 r = parse_uint(str, &i, &endptr, 0);
136 g_assert_cmpint(r, ==, 0);
137 g_assert_cmpint(i, ==, 0123);
138 g_assert(endptr == str + strlen(str));
141 static void test_parse_uint_decimal(void)
143 unsigned long long i = 999;
144 char f = 'X';
145 char *endptr = &f;
146 const char *str = "0123";
147 int r;
149 r = parse_uint(str, &i, &endptr, 10);
151 g_assert_cmpint(r, ==, 0);
152 g_assert_cmpint(i, ==, 123);
153 g_assert(endptr == str + strlen(str));
157 static void test_parse_uint_llong_max(void)
159 unsigned long long i = 999;
160 char f = 'X';
161 char *endptr = &f;
162 char *str = g_strdup_printf("%llu", (unsigned long long)LLONG_MAX + 1);
163 int r;
165 r = parse_uint(str, &i, &endptr, 0);
167 g_assert_cmpint(r, ==, 0);
168 g_assert_cmpint(i, ==, (unsigned long long)LLONG_MAX + 1);
169 g_assert(endptr == str + strlen(str));
171 g_free(str);
174 static void test_parse_uint_overflow(void)
176 unsigned long long i = 999;
177 char f = 'X';
178 char *endptr = &f;
179 const char *str = "99999999999999999999999999999999999999";
180 int r;
182 r = parse_uint(str, &i, &endptr, 0);
184 g_assert_cmpint(r, ==, -ERANGE);
185 g_assert_cmpint(i, ==, ULLONG_MAX);
186 g_assert(endptr == str + strlen(str));
189 static void test_parse_uint_negative(void)
191 unsigned long long i = 999;
192 char f = 'X';
193 char *endptr = &f;
194 const char *str = " \t -321";
195 int r;
197 r = parse_uint(str, &i, &endptr, 0);
199 g_assert_cmpint(r, ==, -ERANGE);
200 g_assert_cmpint(i, ==, 0);
201 g_assert(endptr == str + strlen(str));
205 static void test_parse_uint_full_trailing(void)
207 unsigned long long i = 999;
208 const char *str = "123xxx";
209 int r;
211 r = parse_uint_full(str, &i, 0);
213 g_assert_cmpint(r, ==, -EINVAL);
214 g_assert_cmpint(i, ==, 0);
217 static void test_parse_uint_full_correct(void)
219 unsigned long long i = 999;
220 const char *str = "123";
221 int r;
223 r = parse_uint_full(str, &i, 0);
225 g_assert_cmpint(r, ==, 0);
226 g_assert_cmpint(i, ==, 123);
229 static void test_qemu_strtol_correct(void)
231 const char *str = "12345 foo";
232 char f = 'X';
233 const char *endptr = &f;
234 long res = 999;
235 int err;
237 err = qemu_strtol(str, &endptr, 0, &res);
239 g_assert_cmpint(err, ==, 0);
240 g_assert_cmpint(res, ==, 12345);
241 g_assert(endptr == str + 5);
244 static void test_qemu_strtol_null(void)
246 char f = 'X';
247 const char *endptr = &f;
248 long res = 999;
249 int err;
251 err = qemu_strtol(NULL, &endptr, 0, &res);
253 g_assert_cmpint(err, ==, -EINVAL);
254 g_assert(endptr == NULL);
257 static void test_qemu_strtol_empty(void)
259 const char *str = "";
260 char f = 'X';
261 const char *endptr = &f;
262 long res = 999;
263 int err;
265 err = qemu_strtol(str, &endptr, 0, &res);
267 g_assert_cmpint(err, ==, -EINVAL);
270 static void test_qemu_strtol_whitespace(void)
272 const char *str = " \t ";
273 char f = 'X';
274 const char *endptr = &f;
275 long res = 999;
276 int err;
278 err = qemu_strtol(str, &endptr, 0, &res);
280 g_assert_cmpint(err, ==, -EINVAL);
283 static void test_qemu_strtol_invalid(void)
285 const char *str = " xxxx \t abc";
286 char f = 'X';
287 const char *endptr = &f;
288 long res = 999;
289 int err;
291 err = qemu_strtol(str, &endptr, 0, &res);
293 g_assert_cmpint(err, ==, -EINVAL);
296 static void test_qemu_strtol_trailing(void)
298 const char *str = "123xxx";
299 char f = 'X';
300 const char *endptr = &f;
301 long res = 999;
302 int err;
304 err = qemu_strtol(str, &endptr, 0, &res);
306 g_assert_cmpint(err, ==, 0);
307 g_assert_cmpint(res, ==, 123);
308 g_assert(endptr == str + 3);
311 static void test_qemu_strtol_octal(void)
313 const char *str = "0123";
314 char f = 'X';
315 const char *endptr = &f;
316 long res = 999;
317 int err;
319 err = qemu_strtol(str, &endptr, 8, &res);
321 g_assert_cmpint(err, ==, 0);
322 g_assert_cmpint(res, ==, 0123);
323 g_assert(endptr == str + strlen(str));
325 res = 999;
326 endptr = &f;
327 err = qemu_strtol(str, &endptr, 0, &res);
329 g_assert_cmpint(err, ==, 0);
330 g_assert_cmpint(res, ==, 0123);
331 g_assert(endptr == str + strlen(str));
334 static void test_qemu_strtol_decimal(void)
336 const char *str = "0123";
337 char f = 'X';
338 const char *endptr = &f;
339 long res = 999;
340 int err;
342 err = qemu_strtol(str, &endptr, 10, &res);
344 g_assert_cmpint(err, ==, 0);
345 g_assert_cmpint(res, ==, 123);
346 g_assert(endptr == str + strlen(str));
348 str = "123";
349 res = 999;
350 endptr = &f;
351 err = qemu_strtol(str, &endptr, 0, &res);
353 g_assert_cmpint(err, ==, 0);
354 g_assert_cmpint(res, ==, 123);
355 g_assert(endptr == str + strlen(str));
358 static void test_qemu_strtol_hex(void)
360 const char *str = "0123";
361 char f = 'X';
362 const char *endptr = &f;
363 long res = 999;
364 int err;
366 err = qemu_strtol(str, &endptr, 16, &res);
368 g_assert_cmpint(err, ==, 0);
369 g_assert_cmpint(res, ==, 0x123);
370 g_assert(endptr == str + strlen(str));
372 str = "0x123";
373 res = 999;
374 endptr = &f;
375 err = qemu_strtol(str, &endptr, 0, &res);
377 g_assert_cmpint(err, ==, 0);
378 g_assert_cmpint(res, ==, 0x123);
379 g_assert(endptr == str + strlen(str));
382 static void test_qemu_strtol_max(void)
384 const char *str = g_strdup_printf("%ld", LONG_MAX);
385 char f = 'X';
386 const char *endptr = &f;
387 long res = 999;
388 int err;
390 err = qemu_strtol(str, &endptr, 0, &res);
392 g_assert_cmpint(err, ==, 0);
393 g_assert_cmpint(res, ==, LONG_MAX);
394 g_assert(endptr == str + strlen(str));
397 static void test_qemu_strtol_overflow(void)
399 const char *str = "99999999999999999999999999999999999999999999";
400 char f = 'X';
401 const char *endptr = &f;
402 long res = 999;
403 int err;
405 err = qemu_strtol(str, &endptr, 0, &res);
407 g_assert_cmpint(err, ==, -ERANGE);
408 g_assert_cmpint(res, ==, LONG_MAX);
409 g_assert(endptr == str + strlen(str));
412 static void test_qemu_strtol_underflow(void)
414 const char *str = "-99999999999999999999999999999999999999999999";
415 char f = 'X';
416 const char *endptr = &f;
417 long res = 999;
418 int err;
420 err = qemu_strtol(str, &endptr, 0, &res);
422 g_assert_cmpint(err, ==, -ERANGE);
423 g_assert_cmpint(res, ==, LONG_MIN);
424 g_assert(endptr == str + strlen(str));
427 static void test_qemu_strtol_negative(void)
429 const char *str = " \t -321";
430 char f = 'X';
431 const char *endptr = &f;
432 long res = 999;
433 int err;
435 err = qemu_strtol(str, &endptr, 0, &res);
437 g_assert_cmpint(err, ==, 0);
438 g_assert_cmpint(res, ==, -321);
439 g_assert(endptr == str + strlen(str));
442 static void test_qemu_strtol_full_correct(void)
444 const char *str = "123";
445 long res = 999;
446 int err;
448 err = qemu_strtol(str, NULL, 0, &res);
450 g_assert_cmpint(err, ==, 0);
451 g_assert_cmpint(res, ==, 123);
454 static void test_qemu_strtol_full_null(void)
456 char f = 'X';
457 const char *endptr = &f;
458 long res = 999;
459 int err;
461 err = qemu_strtol(NULL, &endptr, 0, &res);
463 g_assert_cmpint(err, ==, -EINVAL);
464 g_assert(endptr == NULL);
467 static void test_qemu_strtol_full_empty(void)
469 const char *str = "";
470 long res = 999L;
471 int err;
473 err = qemu_strtol(str, NULL, 0, &res);
475 g_assert_cmpint(err, ==, -EINVAL);
478 static void test_qemu_strtol_full_negative(void)
480 const char *str = " \t -321";
481 long res = 999;
482 int err;
484 err = qemu_strtol(str, NULL, 0, &res);
486 g_assert_cmpint(err, ==, 0);
487 g_assert_cmpint(res, ==, -321);
490 static void test_qemu_strtol_full_trailing(void)
492 const char *str = "123xxx";
493 long res;
494 int err;
496 err = qemu_strtol(str, NULL, 0, &res);
498 g_assert_cmpint(err, ==, -EINVAL);
501 static void test_qemu_strtol_full_max(void)
503 const char *str = g_strdup_printf("%ld", LONG_MAX);
504 long res;
505 int err;
507 err = qemu_strtol(str, NULL, 0, &res);
509 g_assert_cmpint(err, ==, 0);
510 g_assert_cmpint(res, ==, LONG_MAX);
513 static void test_qemu_strtoul_correct(void)
515 const char *str = "12345 foo";
516 char f = 'X';
517 const char *endptr = &f;
518 unsigned long res = 999;
519 int err;
521 err = qemu_strtoul(str, &endptr, 0, &res);
523 g_assert_cmpint(err, ==, 0);
524 g_assert_cmpint(res, ==, 12345);
525 g_assert(endptr == str + 5);
528 static void test_qemu_strtoul_null(void)
530 char f = 'X';
531 const char *endptr = &f;
532 unsigned long res = 999;
533 int err;
535 err = qemu_strtoul(NULL, &endptr, 0, &res);
537 g_assert_cmpint(err, ==, -EINVAL);
538 g_assert(endptr == NULL);
541 static void test_qemu_strtoul_empty(void)
543 const char *str = "";
544 char f = 'X';
545 const char *endptr = &f;
546 unsigned long res = 999;
547 int err;
549 err = qemu_strtoul(str, &endptr, 0, &res);
551 g_assert_cmpint(err, ==, -EINVAL);
554 static void test_qemu_strtoul_whitespace(void)
556 const char *str = " \t ";
557 char f = 'X';
558 const char *endptr = &f;
559 unsigned long res = 999;
560 int err;
562 err = qemu_strtoul(str, &endptr, 0, &res);
564 g_assert_cmpint(err, ==, -EINVAL);
567 static void test_qemu_strtoul_invalid(void)
569 const char *str = " xxxx \t abc";
570 char f = 'X';
571 const char *endptr = &f;
572 unsigned long res = 999;
573 int err;
575 err = qemu_strtoul(str, &endptr, 0, &res);
577 g_assert_cmpint(err, ==, -EINVAL);
580 static void test_qemu_strtoul_trailing(void)
582 const char *str = "123xxx";
583 char f = 'X';
584 const char *endptr = &f;
585 unsigned long res = 999;
586 int err;
588 err = qemu_strtoul(str, &endptr, 0, &res);
590 g_assert_cmpint(err, ==, 0);
591 g_assert_cmpint(res, ==, 123);
592 g_assert(endptr == str + 3);
595 static void test_qemu_strtoul_octal(void)
597 const char *str = "0123";
598 char f = 'X';
599 const char *endptr = &f;
600 unsigned long res = 999;
601 int err;
603 err = qemu_strtoul(str, &endptr, 8, &res);
605 g_assert_cmpint(err, ==, 0);
606 g_assert_cmpint(res, ==, 0123);
607 g_assert(endptr == str + strlen(str));
609 res = 999;
610 endptr = &f;
611 err = qemu_strtoul(str, &endptr, 0, &res);
613 g_assert_cmpint(err, ==, 0);
614 g_assert_cmpint(res, ==, 0123);
615 g_assert(endptr == str + strlen(str));
618 static void test_qemu_strtoul_decimal(void)
620 const char *str = "0123";
621 char f = 'X';
622 const char *endptr = &f;
623 unsigned long res = 999;
624 int err;
626 err = qemu_strtoul(str, &endptr, 10, &res);
628 g_assert_cmpint(err, ==, 0);
629 g_assert_cmpint(res, ==, 123);
630 g_assert(endptr == str + strlen(str));
632 str = "123";
633 res = 999;
634 endptr = &f;
635 err = qemu_strtoul(str, &endptr, 0, &res);
637 g_assert_cmpint(err, ==, 0);
638 g_assert_cmpint(res, ==, 123);
639 g_assert(endptr == str + strlen(str));
642 static void test_qemu_strtoul_hex(void)
644 const char *str = "0123";
645 char f = 'X';
646 const char *endptr = &f;
647 unsigned long res = 999;
648 int err;
650 err = qemu_strtoul(str, &endptr, 16, &res);
652 g_assert_cmpint(err, ==, 0);
653 g_assert_cmpint(res, ==, 0x123);
654 g_assert(endptr == str + strlen(str));
656 str = "0x123";
657 res = 999;
658 endptr = &f;
659 err = qemu_strtoul(str, &endptr, 0, &res);
661 g_assert_cmpint(err, ==, 0);
662 g_assert_cmpint(res, ==, 0x123);
663 g_assert(endptr == str + strlen(str));
666 static void test_qemu_strtoul_max(void)
668 const char *str = g_strdup_printf("%lu", ULONG_MAX);
669 char f = 'X';
670 const char *endptr = &f;
671 unsigned long res = 999;
672 int err;
674 err = qemu_strtoul(str, &endptr, 0, &res);
676 g_assert_cmpint(err, ==, 0);
677 g_assert_cmpint(res, ==, ULONG_MAX);
678 g_assert(endptr == str + strlen(str));
681 static void test_qemu_strtoul_overflow(void)
683 const char *str = "99999999999999999999999999999999999999999999";
684 char f = 'X';
685 const char *endptr = &f;
686 unsigned long res = 999;
687 int err;
689 err = qemu_strtoul(str, &endptr, 0, &res);
691 g_assert_cmpint(err, ==, -ERANGE);
692 g_assert_cmpint(res, ==, ULONG_MAX);
693 g_assert(endptr == str + strlen(str));
696 static void test_qemu_strtoul_underflow(void)
698 const char *str = "-99999999999999999999999999999999999999999999";
699 char f = 'X';
700 const char *endptr = &f;
701 unsigned long res = 999;
702 int err;
704 err = qemu_strtoul(str, &endptr, 0, &res);
706 g_assert_cmpint(err, ==, -ERANGE);
707 g_assert_cmpint(res, ==, -1ul);
708 g_assert(endptr == str + strlen(str));
711 static void test_qemu_strtoul_negative(void)
713 const char *str = " \t -321";
714 char f = 'X';
715 const char *endptr = &f;
716 unsigned long res = 999;
717 int err;
719 err = qemu_strtoul(str, &endptr, 0, &res);
721 g_assert_cmpint(err, ==, 0);
722 g_assert_cmpint(res, ==, -321ul);
723 g_assert(endptr == str + strlen(str));
726 static void test_qemu_strtoul_full_correct(void)
728 const char *str = "123";
729 unsigned long res = 999;
730 int err;
732 err = qemu_strtoul(str, NULL, 0, &res);
734 g_assert_cmpint(err, ==, 0);
735 g_assert_cmpint(res, ==, 123);
738 static void test_qemu_strtoul_full_null(void)
740 unsigned long res = 999;
741 int err;
743 err = qemu_strtoul(NULL, NULL, 0, &res);
745 g_assert_cmpint(err, ==, -EINVAL);
748 static void test_qemu_strtoul_full_empty(void)
750 const char *str = "";
751 unsigned long res = 999;
752 int err;
754 err = qemu_strtoul(str, NULL, 0, &res);
756 g_assert_cmpint(err, ==, -EINVAL);
758 static void test_qemu_strtoul_full_negative(void)
760 const char *str = " \t -321";
761 unsigned long res = 999;
762 int err;
764 err = qemu_strtoul(str, NULL, 0, &res);
765 g_assert_cmpint(err, ==, 0);
766 g_assert_cmpint(res, ==, -321ul);
769 static void test_qemu_strtoul_full_trailing(void)
771 const char *str = "123xxx";
772 unsigned long res;
773 int err;
775 err = qemu_strtoul(str, NULL, 0, &res);
777 g_assert_cmpint(err, ==, -EINVAL);
780 static void test_qemu_strtoul_full_max(void)
782 const char *str = g_strdup_printf("%lu", ULONG_MAX);
783 unsigned long res = 999;
784 int err;
786 err = qemu_strtoul(str, NULL, 0, &res);
788 g_assert_cmpint(err, ==, 0);
789 g_assert_cmpint(res, ==, ULONG_MAX);
792 static void test_qemu_strtoll_correct(void)
794 const char *str = "12345 foo";
795 char f = 'X';
796 const char *endptr = &f;
797 int64_t res = 999;
798 int err;
800 err = qemu_strtoll(str, &endptr, 0, &res);
802 g_assert_cmpint(err, ==, 0);
803 g_assert_cmpint(res, ==, 12345);
804 g_assert(endptr == str + 5);
807 static void test_qemu_strtoll_null(void)
809 char f = 'X';
810 const char *endptr = &f;
811 int64_t res = 999;
812 int err;
814 err = qemu_strtoll(NULL, &endptr, 0, &res);
816 g_assert_cmpint(err, ==, -EINVAL);
817 g_assert(endptr == NULL);
820 static void test_qemu_strtoll_empty(void)
822 const char *str = "";
823 char f = 'X';
824 const char *endptr = &f;
825 int64_t res = 999;
826 int err;
828 err = qemu_strtoll(str, &endptr, 0, &res);
830 g_assert_cmpint(err, ==, -EINVAL);
833 static void test_qemu_strtoll_whitespace(void)
835 const char *str = " \t ";
836 char f = 'X';
837 const char *endptr = &f;
838 int64_t res = 999;
839 int err;
841 err = qemu_strtoll(str, &endptr, 0, &res);
843 g_assert_cmpint(err, ==, -EINVAL);
846 static void test_qemu_strtoll_invalid(void)
848 const char *str = " xxxx \t abc";
849 char f = 'X';
850 const char *endptr = &f;
851 int64_t res = 999;
852 int err;
854 err = qemu_strtoll(str, &endptr, 0, &res);
856 g_assert_cmpint(err, ==, -EINVAL);
859 static void test_qemu_strtoll_trailing(void)
861 const char *str = "123xxx";
862 char f = 'X';
863 const char *endptr = &f;
864 int64_t res = 999;
865 int err;
867 err = qemu_strtoll(str, &endptr, 0, &res);
869 g_assert_cmpint(err, ==, 0);
870 g_assert_cmpint(res, ==, 123);
871 g_assert(endptr == str + 3);
874 static void test_qemu_strtoll_octal(void)
876 const char *str = "0123";
877 char f = 'X';
878 const char *endptr = &f;
879 int64_t res = 999;
880 int err;
882 err = qemu_strtoll(str, &endptr, 8, &res);
884 g_assert_cmpint(err, ==, 0);
885 g_assert_cmpint(res, ==, 0123);
886 g_assert(endptr == str + strlen(str));
888 endptr = &f;
889 res = 999;
890 err = qemu_strtoll(str, &endptr, 0, &res);
892 g_assert_cmpint(err, ==, 0);
893 g_assert_cmpint(res, ==, 0123);
894 g_assert(endptr == str + strlen(str));
897 static void test_qemu_strtoll_decimal(void)
899 const char *str = "0123";
900 char f = 'X';
901 const char *endptr = &f;
902 int64_t res = 999;
903 int err;
905 err = qemu_strtoll(str, &endptr, 10, &res);
907 g_assert_cmpint(err, ==, 0);
908 g_assert_cmpint(res, ==, 123);
909 g_assert(endptr == str + strlen(str));
911 str = "123";
912 endptr = &f;
913 res = 999;
914 err = qemu_strtoll(str, &endptr, 0, &res);
916 g_assert_cmpint(err, ==, 0);
917 g_assert_cmpint(res, ==, 123);
918 g_assert(endptr == str + strlen(str));
921 static void test_qemu_strtoll_hex(void)
923 const char *str = "0123";
924 char f = 'X';
925 const char *endptr = &f;
926 int64_t res = 999;
927 int err;
929 err = qemu_strtoll(str, &endptr, 16, &res);
931 g_assert_cmpint(err, ==, 0);
932 g_assert_cmpint(res, ==, 0x123);
933 g_assert(endptr == str + strlen(str));
935 str = "0x123";
936 endptr = &f;
937 res = 999;
938 err = qemu_strtoll(str, &endptr, 0, &res);
940 g_assert_cmpint(err, ==, 0);
941 g_assert_cmpint(res, ==, 0x123);
942 g_assert(endptr == str + strlen(str));
945 static void test_qemu_strtoll_max(void)
947 const char *str = g_strdup_printf("%lld", LLONG_MAX);
948 char f = 'X';
949 const char *endptr = &f;
950 int64_t res = 999;
951 int err;
953 err = qemu_strtoll(str, &endptr, 0, &res);
955 g_assert_cmpint(err, ==, 0);
956 g_assert_cmpint(res, ==, LLONG_MAX);
957 g_assert(endptr == str + strlen(str));
960 static void test_qemu_strtoll_overflow(void)
962 const char *str = "99999999999999999999999999999999999999999999";
963 char f = 'X';
964 const char *endptr = &f;
965 int64_t res = 999;
966 int err;
968 err = qemu_strtoll(str, &endptr, 0, &res);
970 g_assert_cmpint(err, ==, -ERANGE);
971 g_assert_cmpint(res, ==, LLONG_MAX);
972 g_assert(endptr == str + strlen(str));
975 static void test_qemu_strtoll_underflow(void)
977 const char *str = "-99999999999999999999999999999999999999999999";
978 char f = 'X';
979 const char *endptr = &f;
980 int64_t res = 999;
981 int err;
983 err = qemu_strtoll(str, &endptr, 0, &res);
985 g_assert_cmpint(err, ==, -ERANGE);
986 g_assert_cmpint(res, ==, LLONG_MIN);
987 g_assert(endptr == str + strlen(str));
990 static void test_qemu_strtoll_negative(void)
992 const char *str = " \t -321";
993 char f = 'X';
994 const char *endptr = &f;
995 int64_t res = 999;
996 int err;
998 err = qemu_strtoll(str, &endptr, 0, &res);
1000 g_assert_cmpint(err, ==, 0);
1001 g_assert_cmpint(res, ==, -321);
1002 g_assert(endptr == str + strlen(str));
1005 static void test_qemu_strtoll_full_correct(void)
1007 const char *str = "123";
1008 int64_t res = 999;
1009 int err;
1011 err = qemu_strtoll(str, NULL, 0, &res);
1013 g_assert_cmpint(err, ==, 0);
1014 g_assert_cmpint(res, ==, 123);
1017 static void test_qemu_strtoll_full_null(void)
1019 int64_t res = 999;
1020 int err;
1022 err = qemu_strtoll(NULL, NULL, 0, &res);
1024 g_assert_cmpint(err, ==, -EINVAL);
1027 static void test_qemu_strtoll_full_empty(void)
1029 const char *str = "";
1030 int64_t res = 999;
1031 int err;
1033 err = qemu_strtoll(str, NULL, 0, &res);
1035 g_assert_cmpint(err, ==, -EINVAL);
1038 static void test_qemu_strtoll_full_negative(void)
1040 const char *str = " \t -321";
1041 int64_t res = 999;
1042 int err;
1044 err = qemu_strtoll(str, NULL, 0, &res);
1046 g_assert_cmpint(err, ==, 0);
1047 g_assert_cmpint(res, ==, -321);
1050 static void test_qemu_strtoll_full_trailing(void)
1052 const char *str = "123xxx";
1053 int64_t res = 999;
1054 int err;
1056 err = qemu_strtoll(str, NULL, 0, &res);
1058 g_assert_cmpint(err, ==, -EINVAL);
1061 static void test_qemu_strtoll_full_max(void)
1064 const char *str = g_strdup_printf("%lld", LLONG_MAX);
1065 int64_t res;
1066 int err;
1068 err = qemu_strtoll(str, NULL, 0, &res);
1070 g_assert_cmpint(err, ==, 0);
1071 g_assert_cmpint(res, ==, LLONG_MAX);
1074 static void test_qemu_strtoull_correct(void)
1076 const char *str = "12345 foo";
1077 char f = 'X';
1078 const char *endptr = &f;
1079 uint64_t res = 999;
1080 int err;
1082 err = qemu_strtoull(str, &endptr, 0, &res);
1084 g_assert_cmpint(err, ==, 0);
1085 g_assert_cmpint(res, ==, 12345);
1086 g_assert(endptr == str + 5);
1089 static void test_qemu_strtoull_null(void)
1091 char f = 'X';
1092 const char *endptr = &f;
1093 uint64_t res = 999;
1094 int err;
1096 err = qemu_strtoull(NULL, &endptr, 0, &res);
1098 g_assert_cmpint(err, ==, -EINVAL);
1099 g_assert(endptr == NULL);
1102 static void test_qemu_strtoull_empty(void)
1104 const char *str = "";
1105 char f = 'X';
1106 const char *endptr = &f;
1107 uint64_t res = 999;
1108 int err;
1110 err = qemu_strtoull(str, &endptr, 0, &res);
1112 g_assert_cmpint(err, ==, -EINVAL);
1115 static void test_qemu_strtoull_whitespace(void)
1117 const char *str = " \t ";
1118 char f = 'X';
1119 const char *endptr = &f;
1120 uint64_t res = 999;
1121 int err;
1123 err = qemu_strtoull(str, &endptr, 0, &res);
1125 g_assert_cmpint(err, ==, -EINVAL);
1128 static void test_qemu_strtoull_invalid(void)
1130 const char *str = " xxxx \t abc";
1131 char f = 'X';
1132 const char *endptr = &f;
1133 uint64_t res = 999;
1134 int err;
1136 err = qemu_strtoull(str, &endptr, 0, &res);
1138 g_assert_cmpint(err, ==, -EINVAL);
1141 static void test_qemu_strtoull_trailing(void)
1143 const char *str = "123xxx";
1144 char f = 'X';
1145 const char *endptr = &f;
1146 uint64_t res = 999;
1147 int err;
1149 err = qemu_strtoull(str, &endptr, 0, &res);
1151 g_assert_cmpint(err, ==, 0);
1152 g_assert_cmpint(res, ==, 123);
1153 g_assert(endptr == str + 3);
1156 static void test_qemu_strtoull_octal(void)
1158 const char *str = "0123";
1159 char f = 'X';
1160 const char *endptr = &f;
1161 uint64_t res = 999;
1162 int err;
1164 err = qemu_strtoull(str, &endptr, 8, &res);
1166 g_assert_cmpint(err, ==, 0);
1167 g_assert_cmpint(res, ==, 0123);
1168 g_assert(endptr == str + strlen(str));
1170 endptr = &f;
1171 res = 999;
1172 err = qemu_strtoull(str, &endptr, 0, &res);
1174 g_assert_cmpint(err, ==, 0);
1175 g_assert_cmpint(res, ==, 0123);
1176 g_assert(endptr == str + strlen(str));
1179 static void test_qemu_strtoull_decimal(void)
1181 const char *str = "0123";
1182 char f = 'X';
1183 const char *endptr = &f;
1184 uint64_t res = 999;
1185 int err;
1187 err = qemu_strtoull(str, &endptr, 10, &res);
1189 g_assert_cmpint(err, ==, 0);
1190 g_assert_cmpint(res, ==, 123);
1191 g_assert(endptr == str + strlen(str));
1193 str = "123";
1194 endptr = &f;
1195 res = 999;
1196 err = qemu_strtoull(str, &endptr, 0, &res);
1198 g_assert_cmpint(err, ==, 0);
1199 g_assert_cmpint(res, ==, 123);
1200 g_assert(endptr == str + strlen(str));
1203 static void test_qemu_strtoull_hex(void)
1205 const char *str = "0123";
1206 char f = 'X';
1207 const char *endptr = &f;
1208 uint64_t res = 999;
1209 int err;
1211 err = qemu_strtoull(str, &endptr, 16, &res);
1213 g_assert_cmpint(err, ==, 0);
1214 g_assert_cmpint(res, ==, 0x123);
1215 g_assert(endptr == str + strlen(str));
1217 str = "0x123";
1218 endptr = &f;
1219 res = 999;
1220 err = qemu_strtoull(str, &endptr, 0, &res);
1222 g_assert_cmpint(err, ==, 0);
1223 g_assert_cmpint(res, ==, 0x123);
1224 g_assert(endptr == str + strlen(str));
1227 static void test_qemu_strtoull_max(void)
1229 const char *str = g_strdup_printf("%llu", ULLONG_MAX);
1230 char f = 'X';
1231 const char *endptr = &f;
1232 uint64_t res = 999;
1233 int err;
1235 err = qemu_strtoull(str, &endptr, 0, &res);
1237 g_assert_cmpint(err, ==, 0);
1238 g_assert_cmpint(res, ==, ULLONG_MAX);
1239 g_assert(endptr == str + strlen(str));
1242 static void test_qemu_strtoull_overflow(void)
1244 const char *str = "99999999999999999999999999999999999999999999";
1245 char f = 'X';
1246 const char *endptr = &f;
1247 uint64_t res = 999;
1248 int err;
1250 err = qemu_strtoull(str, &endptr, 0, &res);
1252 g_assert_cmpint(err, ==, -ERANGE);
1253 g_assert_cmpint(res, ==, ULLONG_MAX);
1254 g_assert(endptr == str + strlen(str));
1257 static void test_qemu_strtoull_underflow(void)
1259 const char *str = "-99999999999999999999999999999999999999999999";
1260 char f = 'X';
1261 const char *endptr = &f;
1262 uint64_t res = 999;
1263 int err;
1265 err = qemu_strtoull(str, &endptr, 0, &res);
1267 g_assert_cmpint(err, ==, -ERANGE);
1268 g_assert_cmpint(res, ==, -1);
1269 g_assert(endptr == str + strlen(str));
1272 static void test_qemu_strtoull_negative(void)
1274 const char *str = " \t -321";
1275 char f = 'X';
1276 const char *endptr = &f;
1277 uint64_t res = 999;
1278 int err;
1280 err = qemu_strtoull(str, &endptr, 0, &res);
1282 g_assert_cmpint(err, ==, 0);
1283 g_assert_cmpint(res, ==, -321);
1284 g_assert(endptr == str + strlen(str));
1287 static void test_qemu_strtoull_full_correct(void)
1289 const char *str = "18446744073709551614";
1290 uint64_t res = 999;
1291 int err;
1293 err = qemu_strtoull(str, NULL, 0, &res);
1295 g_assert_cmpint(err, ==, 0);
1296 g_assert_cmpint(res, ==, 18446744073709551614LLU);
1299 static void test_qemu_strtoull_full_null(void)
1301 uint64_t res = 999;
1302 int err;
1304 err = qemu_strtoull(NULL, NULL, 0, &res);
1306 g_assert_cmpint(err, ==, -EINVAL);
1309 static void test_qemu_strtoull_full_empty(void)
1311 const char *str = "";
1312 uint64_t res = 999;
1313 int err;
1315 err = qemu_strtoull(str, NULL, 0, &res);
1317 g_assert_cmpint(err, ==, -EINVAL);
1320 static void test_qemu_strtoull_full_negative(void)
1322 const char *str = " \t -321";
1323 uint64_t res = 999;
1324 int err;
1326 err = qemu_strtoull(str, NULL, 0, &res);
1328 g_assert_cmpint(err, ==, 0);
1329 g_assert_cmpint(res, ==, 18446744073709551295LLU);
1332 static void test_qemu_strtoull_full_trailing(void)
1334 const char *str = "18446744073709551614xxxxxx";
1335 uint64_t res = 999;
1336 int err;
1338 err = qemu_strtoull(str, NULL, 0, &res);
1340 g_assert_cmpint(err, ==, -EINVAL);
1343 static void test_qemu_strtoull_full_max(void)
1345 const char *str = g_strdup_printf("%lld", ULLONG_MAX);
1346 uint64_t res = 999;
1347 int err;
1349 err = qemu_strtoull(str, NULL, 0, &res);
1351 g_assert_cmpint(err, ==, 0);
1352 g_assert_cmpint(res, ==, ULLONG_MAX);
1355 int main(int argc, char **argv)
1357 g_test_init(&argc, &argv, NULL);
1359 g_test_add_func("/cutils/parse_uint/null", test_parse_uint_null);
1360 g_test_add_func("/cutils/parse_uint/empty", test_parse_uint_empty);
1361 g_test_add_func("/cutils/parse_uint/whitespace",
1362 test_parse_uint_whitespace);
1363 g_test_add_func("/cutils/parse_uint/invalid", test_parse_uint_invalid);
1364 g_test_add_func("/cutils/parse_uint/trailing", test_parse_uint_trailing);
1365 g_test_add_func("/cutils/parse_uint/correct", test_parse_uint_correct);
1366 g_test_add_func("/cutils/parse_uint/octal", test_parse_uint_octal);
1367 g_test_add_func("/cutils/parse_uint/decimal", test_parse_uint_decimal);
1368 g_test_add_func("/cutils/parse_uint/llong_max", test_parse_uint_llong_max);
1369 g_test_add_func("/cutils/parse_uint/overflow", test_parse_uint_overflow);
1370 g_test_add_func("/cutils/parse_uint/negative", test_parse_uint_negative);
1371 g_test_add_func("/cutils/parse_uint_full/trailing",
1372 test_parse_uint_full_trailing);
1373 g_test_add_func("/cutils/parse_uint_full/correct",
1374 test_parse_uint_full_correct);
1376 /* qemu_strtol() tests */
1377 g_test_add_func("/cutils/qemu_strtol/correct", test_qemu_strtol_correct);
1378 g_test_add_func("/cutils/qemu_strtol/null", test_qemu_strtol_null);
1379 g_test_add_func("/cutils/qemu_strtol/empty", test_qemu_strtol_empty);
1380 g_test_add_func("/cutils/qemu_strtol/whitespace",
1381 test_qemu_strtol_whitespace);
1382 g_test_add_func("/cutils/qemu_strtol/invalid", test_qemu_strtol_invalid);
1383 g_test_add_func("/cutils/qemu_strtol/trailing", test_qemu_strtol_trailing);
1384 g_test_add_func("/cutils/qemu_strtol/octal", test_qemu_strtol_octal);
1385 g_test_add_func("/cutils/qemu_strtol/decimal", test_qemu_strtol_decimal);
1386 g_test_add_func("/cutils/qemu_strtol/hex", test_qemu_strtol_hex);
1387 g_test_add_func("/cutils/qemu_strtol/max", test_qemu_strtol_max);
1388 g_test_add_func("/cutils/qemu_strtol/overflow", test_qemu_strtol_overflow);
1389 g_test_add_func("/cutils/qemu_strtol/underflow",
1390 test_qemu_strtol_underflow);
1391 g_test_add_func("/cutils/qemu_strtol/negative", test_qemu_strtol_negative);
1392 g_test_add_func("/cutils/qemu_strtol_full/correct",
1393 test_qemu_strtol_full_correct);
1394 g_test_add_func("/cutils/qemu_strtol_full/null",
1395 test_qemu_strtol_full_null);
1396 g_test_add_func("/cutils/qemu_strtol_full/empty",
1397 test_qemu_strtol_full_empty);
1398 g_test_add_func("/cutils/qemu_strtol_full/negative",
1399 test_qemu_strtol_full_negative);
1400 g_test_add_func("/cutils/qemu_strtol_full/trailing",
1401 test_qemu_strtol_full_trailing);
1402 g_test_add_func("/cutils/qemu_strtol_full/max",
1403 test_qemu_strtol_full_max);
1405 /* qemu_strtoul() tests */
1406 g_test_add_func("/cutils/qemu_strtoul/correct", test_qemu_strtoul_correct);
1407 g_test_add_func("/cutils/qemu_strtoul/null", test_qemu_strtoul_null);
1408 g_test_add_func("/cutils/qemu_strtoul/empty", test_qemu_strtoul_empty);
1409 g_test_add_func("/cutils/qemu_strtoul/whitespace",
1410 test_qemu_strtoul_whitespace);
1411 g_test_add_func("/cutils/qemu_strtoul/invalid", test_qemu_strtoul_invalid);
1412 g_test_add_func("/cutils/qemu_strtoul/trailing",
1413 test_qemu_strtoul_trailing);
1414 g_test_add_func("/cutils/qemu_strtoul/octal", test_qemu_strtoul_octal);
1415 g_test_add_func("/cutils/qemu_strtoul/decimal", test_qemu_strtoul_decimal);
1416 g_test_add_func("/cutils/qemu_strtoul/hex", test_qemu_strtoul_hex);
1417 g_test_add_func("/cutils/qemu_strtoul/max", test_qemu_strtoul_max);
1418 g_test_add_func("/cutils/qemu_strtoul/overflow",
1419 test_qemu_strtoul_overflow);
1420 g_test_add_func("/cutils/qemu_strtoul/underflow",
1421 test_qemu_strtoul_underflow);
1422 g_test_add_func("/cutils/qemu_strtoul/negative",
1423 test_qemu_strtoul_negative);
1424 g_test_add_func("/cutils/qemu_strtoul_full/correct",
1425 test_qemu_strtoul_full_correct);
1426 g_test_add_func("/cutils/qemu_strtoul_full/null",
1427 test_qemu_strtoul_full_null);
1428 g_test_add_func("/cutils/qemu_strtoul_full/empty",
1429 test_qemu_strtoul_full_empty);
1430 g_test_add_func("/cutils/qemu_strtoul_full/negative",
1431 test_qemu_strtoul_full_negative);
1432 g_test_add_func("/cutils/qemu_strtoul_full/trailing",
1433 test_qemu_strtoul_full_trailing);
1434 g_test_add_func("/cutils/qemu_strtoul_full/max",
1435 test_qemu_strtoul_full_max);
1437 /* qemu_strtoll() tests */
1438 g_test_add_func("/cutils/qemu_strtoll/correct", test_qemu_strtoll_correct);
1439 g_test_add_func("/cutils/qemu_strtoll/null", test_qemu_strtoll_null);
1440 g_test_add_func("/cutils/qemu_strtoll/empty", test_qemu_strtoll_empty);
1441 g_test_add_func("/cutils/qemu_strtoll/whitespace",
1442 test_qemu_strtoll_whitespace);
1443 g_test_add_func("/cutils/qemu_strtoll/invalid", test_qemu_strtoll_invalid);
1444 g_test_add_func("/cutils/qemu_strtoll/trailing",
1445 test_qemu_strtoll_trailing);
1446 g_test_add_func("/cutils/qemu_strtoll/octal", test_qemu_strtoll_octal);
1447 g_test_add_func("/cutils/qemu_strtoll/decimal", test_qemu_strtoll_decimal);
1448 g_test_add_func("/cutils/qemu_strtoll/hex", test_qemu_strtoll_hex);
1449 g_test_add_func("/cutils/qemu_strtoll/max", test_qemu_strtoll_max);
1450 g_test_add_func("/cutils/qemu_strtoll/overflow",
1451 test_qemu_strtoll_overflow);
1452 g_test_add_func("/cutils/qemu_strtoll/underflow",
1453 test_qemu_strtoll_underflow);
1454 g_test_add_func("/cutils/qemu_strtoll/negative",
1455 test_qemu_strtoll_negative);
1456 g_test_add_func("/cutils/qemu_strtoll_full/correct",
1457 test_qemu_strtoll_full_correct);
1458 g_test_add_func("/cutils/qemu_strtoll_full/null",
1459 test_qemu_strtoll_full_null);
1460 g_test_add_func("/cutils/qemu_strtoll_full/empty",
1461 test_qemu_strtoll_full_empty);
1462 g_test_add_func("/cutils/qemu_strtoll_full/negative",
1463 test_qemu_strtoll_full_negative);
1464 g_test_add_func("/cutils/qemu_strtoll_full/trailing",
1465 test_qemu_strtoll_full_trailing);
1466 g_test_add_func("/cutils/qemu_strtoll_full/max",
1467 test_qemu_strtoll_full_max);
1469 /* qemu_strtoull() tests */
1470 g_test_add_func("/cutils/qemu_strtoull/correct",
1471 test_qemu_strtoull_correct);
1472 g_test_add_func("/cutils/qemu_strtoull/null",
1473 test_qemu_strtoull_null);
1474 g_test_add_func("/cutils/qemu_strtoull/empty", test_qemu_strtoull_empty);
1475 g_test_add_func("/cutils/qemu_strtoull/whitespace",
1476 test_qemu_strtoull_whitespace);
1477 g_test_add_func("/cutils/qemu_strtoull/invalid",
1478 test_qemu_strtoull_invalid);
1479 g_test_add_func("/cutils/qemu_strtoull/trailing",
1480 test_qemu_strtoull_trailing);
1481 g_test_add_func("/cutils/qemu_strtoull/octal", test_qemu_strtoull_octal);
1482 g_test_add_func("/cutils/qemu_strtoull/decimal",
1483 test_qemu_strtoull_decimal);
1484 g_test_add_func("/cutils/qemu_strtoull/hex", test_qemu_strtoull_hex);
1485 g_test_add_func("/cutils/qemu_strtoull/max", test_qemu_strtoull_max);
1486 g_test_add_func("/cutils/qemu_strtoull/overflow",
1487 test_qemu_strtoull_overflow);
1488 g_test_add_func("/cutils/qemu_strtoull/underflow",
1489 test_qemu_strtoull_underflow);
1490 g_test_add_func("/cutils/qemu_strtoull/negative",
1491 test_qemu_strtoull_negative);
1492 g_test_add_func("/cutils/qemu_strtoull_full/correct",
1493 test_qemu_strtoull_full_correct);
1494 g_test_add_func("/cutils/qemu_strtoull_full/null",
1495 test_qemu_strtoull_full_null);
1496 g_test_add_func("/cutils/qemu_strtoull_full/empty",
1497 test_qemu_strtoull_full_empty);
1498 g_test_add_func("/cutils/qemu_strtoull_full/negative",
1499 test_qemu_strtoull_full_negative);
1500 g_test_add_func("/cutils/qemu_strtoull_full/trailing",
1501 test_qemu_strtoull_full_trailing);
1502 g_test_add_func("/cutils/qemu_strtoull_full/max",
1503 test_qemu_strtoull_full_max);
1505 return g_test_run();