]> git.proxmox.com Git - mirror_qemu.git/blame - tests/test-cutils.c
Merge remote-tracking branch 'remotes/aperard/tags/pull-xen-20200407' into staging
[mirror_qemu.git] / tests / test-cutils.c
CommitLineData
e3f9fe2d
EH
1/*
2 * cutils.c unit-tests
3 *
4 * Copyright (C) 2013 Red Hat Inc.
5 *
6 * Authors:
7 * Eduardo Habkost <ehabkost@redhat.com>
8 *
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:
15 *
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
18 *
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.
26 */
27
681c28a3 28#include "qemu/osdep.h"
d23b6caa 29#include "qemu/units.h"
f348b6d1 30#include "qemu/cutils.h"
d23b6caa 31#include "qemu/units.h"
e3f9fe2d
EH
32
33static void test_parse_uint_null(void)
34{
35 unsigned long long i = 999;
36 char f = 'X';
37 char *endptr = &f;
38 int r;
39
40 r = parse_uint(NULL, &i, &endptr, 0);
41
42 g_assert_cmpint(r, ==, -EINVAL);
43 g_assert_cmpint(i, ==, 0);
44 g_assert(endptr == NULL);
45}
46
47static void test_parse_uint_empty(void)
48{
49 unsigned long long i = 999;
50 char f = 'X';
51 char *endptr = &f;
52 const char *str = "";
53 int r;
54
55 r = parse_uint(str, &i, &endptr, 0);
56
57 g_assert_cmpint(r, ==, -EINVAL);
58 g_assert_cmpint(i, ==, 0);
59 g_assert(endptr == str);
60}
61
62static void test_parse_uint_whitespace(void)
63{
64 unsigned long long i = 999;
65 char f = 'X';
66 char *endptr = &f;
67 const char *str = " \t ";
68 int r;
69
70 r = parse_uint(str, &i, &endptr, 0);
71
72 g_assert_cmpint(r, ==, -EINVAL);
73 g_assert_cmpint(i, ==, 0);
74 g_assert(endptr == str);
75}
76
77
78static void test_parse_uint_invalid(void)
79{
80 unsigned long long i = 999;
81 char f = 'X';
82 char *endptr = &f;
83 const char *str = " \t xxx";
84 int r;
85
86 r = parse_uint(str, &i, &endptr, 0);
87
88 g_assert_cmpint(r, ==, -EINVAL);
89 g_assert_cmpint(i, ==, 0);
90 g_assert(endptr == str);
91}
92
93
94static void test_parse_uint_trailing(void)
95{
96 unsigned long long i = 999;
97 char f = 'X';
98 char *endptr = &f;
99 const char *str = "123xxx";
100 int r;
101
102 r = parse_uint(str, &i, &endptr, 0);
103
104 g_assert_cmpint(r, ==, 0);
105 g_assert_cmpint(i, ==, 123);
106 g_assert(endptr == str + 3);
107}
108
109static void test_parse_uint_correct(void)
110{
111 unsigned long long i = 999;
112 char f = 'X';
113 char *endptr = &f;
114 const char *str = "123";
115 int r;
116
117 r = parse_uint(str, &i, &endptr, 0);
118
119 g_assert_cmpint(r, ==, 0);
120 g_assert_cmpint(i, ==, 123);
121 g_assert(endptr == str + strlen(str));
122}
123
124static void test_parse_uint_octal(void)
125{
126 unsigned long long i = 999;
127 char f = 'X';
128 char *endptr = &f;
129 const char *str = "0123";
130 int r;
131
132 r = parse_uint(str, &i, &endptr, 0);
133
134 g_assert_cmpint(r, ==, 0);
135 g_assert_cmpint(i, ==, 0123);
136 g_assert(endptr == str + strlen(str));
137}
138
139static void test_parse_uint_decimal(void)
140{
141 unsigned long long i = 999;
142 char f = 'X';
143 char *endptr = &f;
144 const char *str = "0123";
145 int r;
146
147 r = parse_uint(str, &i, &endptr, 10);
148
149 g_assert_cmpint(r, ==, 0);
150 g_assert_cmpint(i, ==, 123);
151 g_assert(endptr == str + strlen(str));
152}
153
154
155static void test_parse_uint_llong_max(void)
156{
157 unsigned long long i = 999;
158 char f = 'X';
159 char *endptr = &f;
160 char *str = g_strdup_printf("%llu", (unsigned long long)LLONG_MAX + 1);
161 int r;
162
163 r = parse_uint(str, &i, &endptr, 0);
164
165 g_assert_cmpint(r, ==, 0);
166 g_assert_cmpint(i, ==, (unsigned long long)LLONG_MAX + 1);
167 g_assert(endptr == str + strlen(str));
168
169 g_free(str);
170}
171
172static void test_parse_uint_overflow(void)
173{
174 unsigned long long i = 999;
175 char f = 'X';
176 char *endptr = &f;
177 const char *str = "99999999999999999999999999999999999999";
178 int r;
179
180 r = parse_uint(str, &i, &endptr, 0);
181
182 g_assert_cmpint(r, ==, -ERANGE);
183 g_assert_cmpint(i, ==, ULLONG_MAX);
184 g_assert(endptr == str + strlen(str));
185}
186
187static void test_parse_uint_negative(void)
188{
189 unsigned long long i = 999;
190 char f = 'X';
191 char *endptr = &f;
192 const char *str = " \t -321";
193 int r;
194
195 r = parse_uint(str, &i, &endptr, 0);
196
197 g_assert_cmpint(r, ==, -ERANGE);
198 g_assert_cmpint(i, ==, 0);
199 g_assert(endptr == str + strlen(str));
200}
201
202
203static void test_parse_uint_full_trailing(void)
204{
205 unsigned long long i = 999;
206 const char *str = "123xxx";
207 int r;
208
209 r = parse_uint_full(str, &i, 0);
210
211 g_assert_cmpint(r, ==, -EINVAL);
212 g_assert_cmpint(i, ==, 0);
213}
214
215static void test_parse_uint_full_correct(void)
216{
217 unsigned long long i = 999;
218 const char *str = "123";
219 int r;
220
221 r = parse_uint_full(str, &i, 0);
222
223 g_assert_cmpint(r, ==, 0);
224 g_assert_cmpint(i, ==, 123);
225}
226
473a2a33
DB
227static void test_qemu_strtoi_correct(void)
228{
229 const char *str = "12345 foo";
230 char f = 'X';
231 const char *endptr = &f;
232 int res = 999;
233 int err;
234
235 err = qemu_strtoi(str, &endptr, 0, &res);
236
237 g_assert_cmpint(err, ==, 0);
238 g_assert_cmpint(res, ==, 12345);
239 g_assert(endptr == str + 5);
240}
241
242static void test_qemu_strtoi_null(void)
243{
244 char f = 'X';
245 const char *endptr = &f;
246 int res = 999;
247 int err;
248
249 err = qemu_strtoi(NULL, &endptr, 0, &res);
250
251 g_assert_cmpint(err, ==, -EINVAL);
252 g_assert(endptr == NULL);
253}
254
255static void test_qemu_strtoi_empty(void)
256{
257 const char *str = "";
258 char f = 'X';
259 const char *endptr = &f;
260 int res = 999;
261 int err;
262
263 err = qemu_strtoi(str, &endptr, 0, &res);
264
265 g_assert_cmpint(err, ==, -EINVAL);
266 g_assert(endptr == str);
267}
268
269static void test_qemu_strtoi_whitespace(void)
270{
271 const char *str = " \t ";
272 char f = 'X';
273 const char *endptr = &f;
274 int res = 999;
275 int err;
276
277 err = qemu_strtoi(str, &endptr, 0, &res);
278
279 g_assert_cmpint(err, ==, -EINVAL);
280 g_assert(endptr == str);
281}
282
283static void test_qemu_strtoi_invalid(void)
284{
285 const char *str = " xxxx \t abc";
286 char f = 'X';
287 const char *endptr = &f;
288 int res = 999;
289 int err;
290
291 err = qemu_strtoi(str, &endptr, 0, &res);
292
293 g_assert_cmpint(err, ==, -EINVAL);
294 g_assert(endptr == str);
295}
296
297static void test_qemu_strtoi_trailing(void)
298{
299 const char *str = "123xxx";
300 char f = 'X';
301 const char *endptr = &f;
302 int res = 999;
303 int err;
304
305 err = qemu_strtoi(str, &endptr, 0, &res);
306
307 g_assert_cmpint(err, ==, 0);
308 g_assert_cmpint(res, ==, 123);
309 g_assert(endptr == str + 3);
310}
311
312static void test_qemu_strtoi_octal(void)
313{
314 const char *str = "0123";
315 char f = 'X';
316 const char *endptr = &f;
317 int res = 999;
318 int err;
319
320 err = qemu_strtoi(str, &endptr, 8, &res);
321
322 g_assert_cmpint(err, ==, 0);
323 g_assert_cmpint(res, ==, 0123);
324 g_assert(endptr == str + strlen(str));
325
326 res = 999;
327 endptr = &f;
328 err = qemu_strtoi(str, &endptr, 0, &res);
329
330 g_assert_cmpint(err, ==, 0);
331 g_assert_cmpint(res, ==, 0123);
332 g_assert(endptr == str + strlen(str));
333}
334
335static void test_qemu_strtoi_decimal(void)
336{
337 const char *str = "0123";
338 char f = 'X';
339 const char *endptr = &f;
340 int res = 999;
341 int err;
342
343 err = qemu_strtoi(str, &endptr, 10, &res);
344
345 g_assert_cmpint(err, ==, 0);
346 g_assert_cmpint(res, ==, 123);
347 g_assert(endptr == str + strlen(str));
348
349 str = "123";
350 res = 999;
351 endptr = &f;
352 err = qemu_strtoi(str, &endptr, 0, &res);
353
354 g_assert_cmpint(err, ==, 0);
355 g_assert_cmpint(res, ==, 123);
356 g_assert(endptr == str + strlen(str));
357}
358
359static void test_qemu_strtoi_hex(void)
360{
361 const char *str = "0123";
362 char f = 'X';
363 const char *endptr = &f;
364 int res = 999;
365 int err;
366
367 err = qemu_strtoi(str, &endptr, 16, &res);
368
369 g_assert_cmpint(err, ==, 0);
370 g_assert_cmpint(res, ==, 0x123);
371 g_assert(endptr == str + strlen(str));
372
373 str = "0x123";
374 res = 999;
375 endptr = &f;
376 err = qemu_strtoi(str, &endptr, 0, &res);
377
378 g_assert_cmpint(err, ==, 0);
379 g_assert_cmpint(res, ==, 0x123);
380 g_assert(endptr == str + strlen(str));
381}
382
383static void test_qemu_strtoi_max(void)
384{
385 char *str = g_strdup_printf("%d", INT_MAX);
386 char f = 'X';
387 const char *endptr = &f;
388 int res = 999;
389 int err;
390
391 err = qemu_strtoi(str, &endptr, 0, &res);
392
393 g_assert_cmpint(err, ==, 0);
394 g_assert_cmpint(res, ==, INT_MAX);
395 g_assert(endptr == str + strlen(str));
396 g_free(str);
397}
398
399static void test_qemu_strtoi_overflow(void)
400{
401 char *str = g_strdup_printf("%lld", (long long)INT_MAX + 1ll);
402 char f = 'X';
403 const char *endptr = &f;
404 int res = 999;
405 int err;
406
407 err = qemu_strtoi(str, &endptr, 0, &res);
408
409 g_assert_cmpint(err, ==, -ERANGE);
410 g_assert_cmpint(res, ==, INT_MAX);
411 g_assert(endptr == str + strlen(str));
412 g_free(str);
413}
414
415static void test_qemu_strtoi_underflow(void)
416{
417 char *str = g_strdup_printf("%lld", (long long)INT_MIN - 1ll);
418 char f = 'X';
419 const char *endptr = &f;
420 int res = 999;
421 int err;
422
423 err = qemu_strtoi(str, &endptr, 0, &res);
424
425 g_assert_cmpint(err, ==, -ERANGE);
426 g_assert_cmpint(res, ==, INT_MIN);
427 g_assert(endptr == str + strlen(str));
428 g_free(str);
429}
430
431static void test_qemu_strtoi_negative(void)
432{
433 const char *str = " \t -321";
434 char f = 'X';
435 const char *endptr = &f;
436 int res = 999;
437 int err;
438
439 err = qemu_strtoi(str, &endptr, 0, &res);
440
441 g_assert_cmpint(err, ==, 0);
442 g_assert_cmpint(res, ==, -321);
443 g_assert(endptr == str + strlen(str));
444}
445
446static void test_qemu_strtoi_full_correct(void)
447{
448 const char *str = "123";
449 int res = 999;
450 int err;
451
452 err = qemu_strtoi(str, NULL, 0, &res);
453
454 g_assert_cmpint(err, ==, 0);
455 g_assert_cmpint(res, ==, 123);
456}
457
458static void test_qemu_strtoi_full_null(void)
459{
460 char f = 'X';
461 const char *endptr = &f;
462 int res = 999;
463 int err;
464
465 err = qemu_strtoi(NULL, &endptr, 0, &res);
466
467 g_assert_cmpint(err, ==, -EINVAL);
468 g_assert(endptr == NULL);
469}
470
471static void test_qemu_strtoi_full_empty(void)
472{
473 const char *str = "";
474 int res = 999L;
475 int err;
476
477 err = qemu_strtoi(str, NULL, 0, &res);
478
479 g_assert_cmpint(err, ==, -EINVAL);
480}
481
482static void test_qemu_strtoi_full_negative(void)
483{
484 const char *str = " \t -321";
485 int res = 999;
486 int err;
487
488 err = qemu_strtoi(str, NULL, 0, &res);
489
490 g_assert_cmpint(err, ==, 0);
491 g_assert_cmpint(res, ==, -321);
492}
493
494static void test_qemu_strtoi_full_trailing(void)
495{
496 const char *str = "123xxx";
497 int res;
498 int err;
499
500 err = qemu_strtoi(str, NULL, 0, &res);
501
502 g_assert_cmpint(err, ==, -EINVAL);
503}
504
505static void test_qemu_strtoi_full_max(void)
506{
507 char *str = g_strdup_printf("%d", INT_MAX);
508 int res;
509 int err;
510
511 err = qemu_strtoi(str, NULL, 0, &res);
512
513 g_assert_cmpint(err, ==, 0);
514 g_assert_cmpint(res, ==, INT_MAX);
515 g_free(str);
516}
517
518static void test_qemu_strtoui_correct(void)
519{
520 const char *str = "12345 foo";
521 char f = 'X';
522 const char *endptr = &f;
523 unsigned int res = 999;
524 int err;
525
526 err = qemu_strtoui(str, &endptr, 0, &res);
527
528 g_assert_cmpint(err, ==, 0);
529 g_assert_cmpuint(res, ==, 12345);
530 g_assert(endptr == str + 5);
531}
532
533static void test_qemu_strtoui_null(void)
534{
535 char f = 'X';
536 const char *endptr = &f;
537 unsigned int res = 999;
538 int err;
539
540 err = qemu_strtoui(NULL, &endptr, 0, &res);
541
542 g_assert_cmpint(err, ==, -EINVAL);
543 g_assert(endptr == NULL);
544}
545
546static void test_qemu_strtoui_empty(void)
547{
548 const char *str = "";
549 char f = 'X';
550 const char *endptr = &f;
551 unsigned int res = 999;
552 int err;
553
554 err = qemu_strtoui(str, &endptr, 0, &res);
555
556 g_assert_cmpint(err, ==, -EINVAL);
557 g_assert(endptr == str);
558}
559
560static void test_qemu_strtoui_whitespace(void)
561{
562 const char *str = " \t ";
563 char f = 'X';
564 const char *endptr = &f;
565 unsigned int res = 999;
566 int err;
567
568 err = qemu_strtoui(str, &endptr, 0, &res);
569
570 g_assert_cmpint(err, ==, -EINVAL);
571 g_assert(endptr == str);
572}
573
574static void test_qemu_strtoui_invalid(void)
575{
576 const char *str = " xxxx \t abc";
577 char f = 'X';
578 const char *endptr = &f;
579 unsigned int res = 999;
580 int err;
581
582 err = qemu_strtoui(str, &endptr, 0, &res);
583
584 g_assert_cmpint(err, ==, -EINVAL);
585 g_assert(endptr == str);
586}
587
588static void test_qemu_strtoui_trailing(void)
589{
590 const char *str = "123xxx";
591 char f = 'X';
592 const char *endptr = &f;
593 unsigned int res = 999;
594 int err;
595
596 err = qemu_strtoui(str, &endptr, 0, &res);
597
598 g_assert_cmpint(err, ==, 0);
599 g_assert_cmpuint(res, ==, 123);
600 g_assert(endptr == str + 3);
601}
602
603static void test_qemu_strtoui_octal(void)
604{
605 const char *str = "0123";
606 char f = 'X';
607 const char *endptr = &f;
608 unsigned int res = 999;
609 int err;
610
611 err = qemu_strtoui(str, &endptr, 8, &res);
612
613 g_assert_cmpint(err, ==, 0);
614 g_assert_cmpuint(res, ==, 0123);
615 g_assert(endptr == str + strlen(str));
616
617 res = 999;
618 endptr = &f;
619 err = qemu_strtoui(str, &endptr, 0, &res);
620
621 g_assert_cmpint(err, ==, 0);
622 g_assert_cmpuint(res, ==, 0123);
623 g_assert(endptr == str + strlen(str));
624}
625
626static void test_qemu_strtoui_decimal(void)
627{
628 const char *str = "0123";
629 char f = 'X';
630 const char *endptr = &f;
631 unsigned int res = 999;
632 int err;
633
634 err = qemu_strtoui(str, &endptr, 10, &res);
635
636 g_assert_cmpint(err, ==, 0);
637 g_assert_cmpuint(res, ==, 123);
638 g_assert(endptr == str + strlen(str));
639
640 str = "123";
641 res = 999;
642 endptr = &f;
643 err = qemu_strtoui(str, &endptr, 0, &res);
644
645 g_assert_cmpint(err, ==, 0);
646 g_assert_cmpuint(res, ==, 123);
647 g_assert(endptr == str + strlen(str));
648}
649
650static void test_qemu_strtoui_hex(void)
651{
652 const char *str = "0123";
653 char f = 'X';
654 const char *endptr = &f;
655 unsigned int res = 999;
656 int err;
657
658 err = qemu_strtoui(str, &endptr, 16, &res);
659
660 g_assert_cmpint(err, ==, 0);
661 g_assert_cmphex(res, ==, 0x123);
662 g_assert(endptr == str + strlen(str));
663
664 str = "0x123";
665 res = 999;
666 endptr = &f;
667 err = qemu_strtoui(str, &endptr, 0, &res);
668
669 g_assert_cmpint(err, ==, 0);
670 g_assert_cmphex(res, ==, 0x123);
671 g_assert(endptr == str + strlen(str));
672}
673
674static void test_qemu_strtoui_max(void)
675{
676 char *str = g_strdup_printf("%u", UINT_MAX);
677 char f = 'X';
678 const char *endptr = &f;
679 unsigned int res = 999;
680 int err;
681
682 err = qemu_strtoui(str, &endptr, 0, &res);
683
684 g_assert_cmpint(err, ==, 0);
685 g_assert_cmphex(res, ==, UINT_MAX);
686 g_assert(endptr == str + strlen(str));
687 g_free(str);
688}
689
690static void test_qemu_strtoui_overflow(void)
691{
692 char *str = g_strdup_printf("%lld", (long long)UINT_MAX + 1ll);
693 char f = 'X';
694 const char *endptr = &f;
695 unsigned int res = 999;
696 int err;
697
698 err = qemu_strtoui(str, &endptr, 0, &res);
699
700 g_assert_cmpint(err, ==, -ERANGE);
701 g_assert_cmphex(res, ==, UINT_MAX);
702 g_assert(endptr == str + strlen(str));
703 g_free(str);
704}
705
706static void test_qemu_strtoui_underflow(void)
707{
708 char *str = g_strdup_printf("%lld", (long long)INT_MIN - 1ll);
709 char f = 'X';
710 const char *endptr = &f;
711 unsigned int res = 999;
712 int err;
713
714 err = qemu_strtoui(str, &endptr, 0, &res);
715
716 g_assert_cmpint(err, ==, -ERANGE);
717 g_assert_cmpuint(res, ==, (unsigned int)-1);
718 g_assert(endptr == str + strlen(str));
719 g_free(str);
720}
721
722static void test_qemu_strtoui_negative(void)
723{
724 const char *str = " \t -321";
725 char f = 'X';
726 const char *endptr = &f;
727 unsigned int res = 999;
728 int err;
729
730 err = qemu_strtoui(str, &endptr, 0, &res);
731
732 g_assert_cmpint(err, ==, 0);
733 g_assert_cmpuint(res, ==, (unsigned int)-321);
734 g_assert(endptr == str + strlen(str));
735}
736
737static void test_qemu_strtoui_full_correct(void)
738{
739 const char *str = "123";
740 unsigned int res = 999;
741 int err;
742
743 err = qemu_strtoui(str, NULL, 0, &res);
744
745 g_assert_cmpint(err, ==, 0);
746 g_assert_cmpuint(res, ==, 123);
747}
748
749static void test_qemu_strtoui_full_null(void)
750{
751 unsigned int res = 999;
752 int err;
753
754 err = qemu_strtoui(NULL, NULL, 0, &res);
755
756 g_assert_cmpint(err, ==, -EINVAL);
757}
758
759static void test_qemu_strtoui_full_empty(void)
760{
761 const char *str = "";
762 unsigned int res = 999;
763 int err;
764
765 err = qemu_strtoui(str, NULL, 0, &res);
766
767 g_assert_cmpint(err, ==, -EINVAL);
768}
769static void test_qemu_strtoui_full_negative(void)
770{
771 const char *str = " \t -321";
772 unsigned int res = 999;
773 int err;
774
775 err = qemu_strtoui(str, NULL, 0, &res);
776 g_assert_cmpint(err, ==, 0);
777 g_assert_cmpuint(res, ==, (unsigned int)-321);
778}
779
780static void test_qemu_strtoui_full_trailing(void)
781{
782 const char *str = "123xxx";
783 unsigned int res;
784 int err;
785
786 err = qemu_strtoui(str, NULL, 0, &res);
787
788 g_assert_cmpint(err, ==, -EINVAL);
789}
790
791static void test_qemu_strtoui_full_max(void)
792{
793 char *str = g_strdup_printf("%u", UINT_MAX);
794 unsigned int res = 999;
795 int err;
796
797 err = qemu_strtoui(str, NULL, 0, &res);
798
799 g_assert_cmpint(err, ==, 0);
800 g_assert_cmphex(res, ==, UINT_MAX);
801 g_free(str);
802}
803
764e0fa4
CT
804static void test_qemu_strtol_correct(void)
805{
806 const char *str = "12345 foo";
807 char f = 'X';
808 const char *endptr = &f;
809 long res = 999;
810 int err;
811
812 err = qemu_strtol(str, &endptr, 0, &res);
813
814 g_assert_cmpint(err, ==, 0);
815 g_assert_cmpint(res, ==, 12345);
816 g_assert(endptr == str + 5);
817}
818
819static void test_qemu_strtol_null(void)
820{
821 char f = 'X';
822 const char *endptr = &f;
823 long res = 999;
824 int err;
825
826 err = qemu_strtol(NULL, &endptr, 0, &res);
827
828 g_assert_cmpint(err, ==, -EINVAL);
829 g_assert(endptr == NULL);
830}
831
832static void test_qemu_strtol_empty(void)
833{
834 const char *str = "";
835 char f = 'X';
836 const char *endptr = &f;
837 long res = 999;
838 int err;
839
840 err = qemu_strtol(str, &endptr, 0, &res);
841
47d4be12 842 g_assert_cmpint(err, ==, -EINVAL);
73245450 843 g_assert(endptr == str);
764e0fa4
CT
844}
845
846static void test_qemu_strtol_whitespace(void)
847{
848 const char *str = " \t ";
849 char f = 'X';
850 const char *endptr = &f;
851 long res = 999;
852 int err;
853
854 err = qemu_strtol(str, &endptr, 0, &res);
855
47d4be12 856 g_assert_cmpint(err, ==, -EINVAL);
73245450 857 g_assert(endptr == str);
764e0fa4
CT
858}
859
860static void test_qemu_strtol_invalid(void)
861{
862 const char *str = " xxxx \t abc";
863 char f = 'X';
864 const char *endptr = &f;
865 long res = 999;
866 int err;
867
868 err = qemu_strtol(str, &endptr, 0, &res);
869
47d4be12 870 g_assert_cmpint(err, ==, -EINVAL);
73245450 871 g_assert(endptr == str);
764e0fa4
CT
872}
873
874static void test_qemu_strtol_trailing(void)
875{
876 const char *str = "123xxx";
877 char f = 'X';
878 const char *endptr = &f;
879 long res = 999;
880 int err;
881
882 err = qemu_strtol(str, &endptr, 0, &res);
883
884 g_assert_cmpint(err, ==, 0);
885 g_assert_cmpint(res, ==, 123);
886 g_assert(endptr == str + 3);
887}
888
889static void test_qemu_strtol_octal(void)
890{
891 const char *str = "0123";
892 char f = 'X';
893 const char *endptr = &f;
894 long res = 999;
895 int err;
896
897 err = qemu_strtol(str, &endptr, 8, &res);
898
899 g_assert_cmpint(err, ==, 0);
900 g_assert_cmpint(res, ==, 0123);
901 g_assert(endptr == str + strlen(str));
902
903 res = 999;
904 endptr = &f;
905 err = qemu_strtol(str, &endptr, 0, &res);
906
907 g_assert_cmpint(err, ==, 0);
908 g_assert_cmpint(res, ==, 0123);
909 g_assert(endptr == str + strlen(str));
910}
911
912static void test_qemu_strtol_decimal(void)
913{
914 const char *str = "0123";
915 char f = 'X';
916 const char *endptr = &f;
917 long res = 999;
918 int err;
919
920 err = qemu_strtol(str, &endptr, 10, &res);
921
922 g_assert_cmpint(err, ==, 0);
923 g_assert_cmpint(res, ==, 123);
924 g_assert(endptr == str + strlen(str));
925
926 str = "123";
927 res = 999;
928 endptr = &f;
929 err = qemu_strtol(str, &endptr, 0, &res);
930
931 g_assert_cmpint(err, ==, 0);
932 g_assert_cmpint(res, ==, 123);
933 g_assert(endptr == str + strlen(str));
934}
935
936static void test_qemu_strtol_hex(void)
937{
938 const char *str = "0123";
939 char f = 'X';
940 const char *endptr = &f;
941 long res = 999;
942 int err;
943
944 err = qemu_strtol(str, &endptr, 16, &res);
945
946 g_assert_cmpint(err, ==, 0);
947 g_assert_cmpint(res, ==, 0x123);
948 g_assert(endptr == str + strlen(str));
949
950 str = "0x123";
951 res = 999;
952 endptr = &f;
953 err = qemu_strtol(str, &endptr, 0, &res);
954
955 g_assert_cmpint(err, ==, 0);
956 g_assert_cmpint(res, ==, 0x123);
957 g_assert(endptr == str + strlen(str));
958}
959
960static void test_qemu_strtol_max(void)
961{
d6f723b5 962 char *str = g_strdup_printf("%ld", LONG_MAX);
764e0fa4
CT
963 char f = 'X';
964 const char *endptr = &f;
965 long res = 999;
966 int err;
967
968 err = qemu_strtol(str, &endptr, 0, &res);
969
970 g_assert_cmpint(err, ==, 0);
971 g_assert_cmpint(res, ==, LONG_MAX);
972 g_assert(endptr == str + strlen(str));
d6f723b5 973 g_free(str);
764e0fa4
CT
974}
975
976static void test_qemu_strtol_overflow(void)
977{
978 const char *str = "99999999999999999999999999999999999999999999";
979 char f = 'X';
980 const char *endptr = &f;
981 long res = 999;
982 int err;
983
984 err = qemu_strtol(str, &endptr, 0, &res);
985
986 g_assert_cmpint(err, ==, -ERANGE);
987 g_assert_cmpint(res, ==, LONG_MAX);
988 g_assert(endptr == str + strlen(str));
989}
990
991static void test_qemu_strtol_underflow(void)
992{
993 const char *str = "-99999999999999999999999999999999999999999999";
994 char f = 'X';
995 const char *endptr = &f;
996 long res = 999;
997 int err;
998
999 err = qemu_strtol(str, &endptr, 0, &res);
1000
1001 g_assert_cmpint(err, ==, -ERANGE);
1002 g_assert_cmpint(res, ==, LONG_MIN);
1003 g_assert(endptr == str + strlen(str));
1004}
1005
1006static void test_qemu_strtol_negative(void)
1007{
1008 const char *str = " \t -321";
1009 char f = 'X';
1010 const char *endptr = &f;
1011 long res = 999;
1012 int err;
1013
1014 err = qemu_strtol(str, &endptr, 0, &res);
1015
1016 g_assert_cmpint(err, ==, 0);
1017 g_assert_cmpint(res, ==, -321);
1018 g_assert(endptr == str + strlen(str));
1019}
1020
1021static void test_qemu_strtol_full_correct(void)
1022{
1023 const char *str = "123";
1024 long res = 999;
1025 int err;
1026
1027 err = qemu_strtol(str, NULL, 0, &res);
1028
1029 g_assert_cmpint(err, ==, 0);
1030 g_assert_cmpint(res, ==, 123);
1031}
1032
1033static void test_qemu_strtol_full_null(void)
1034{
1035 char f = 'X';
1036 const char *endptr = &f;
1037 long res = 999;
1038 int err;
1039
1040 err = qemu_strtol(NULL, &endptr, 0, &res);
1041
1042 g_assert_cmpint(err, ==, -EINVAL);
1043 g_assert(endptr == NULL);
1044}
1045
1046static void test_qemu_strtol_full_empty(void)
1047{
1048 const char *str = "";
1049 long res = 999L;
1050 int err;
1051
1052 err = qemu_strtol(str, NULL, 0, &res);
1053
47d4be12 1054 g_assert_cmpint(err, ==, -EINVAL);
764e0fa4
CT
1055}
1056
1057static void test_qemu_strtol_full_negative(void)
1058{
1059 const char *str = " \t -321";
1060 long res = 999;
1061 int err;
1062
1063 err = qemu_strtol(str, NULL, 0, &res);
1064
1065 g_assert_cmpint(err, ==, 0);
1066 g_assert_cmpint(res, ==, -321);
1067}
1068
1069static void test_qemu_strtol_full_trailing(void)
1070{
1071 const char *str = "123xxx";
1072 long res;
1073 int err;
1074
1075 err = qemu_strtol(str, NULL, 0, &res);
1076
1077 g_assert_cmpint(err, ==, -EINVAL);
1078}
1079
1080static void test_qemu_strtol_full_max(void)
1081{
d6f723b5 1082 char *str = g_strdup_printf("%ld", LONG_MAX);
764e0fa4
CT
1083 long res;
1084 int err;
1085
1086 err = qemu_strtol(str, NULL, 0, &res);
1087
1088 g_assert_cmpint(err, ==, 0);
1089 g_assert_cmpint(res, ==, LONG_MAX);
d6f723b5 1090 g_free(str);
764e0fa4 1091}
c817c015
CT
1092
1093static void test_qemu_strtoul_correct(void)
1094{
1095 const char *str = "12345 foo";
1096 char f = 'X';
1097 const char *endptr = &f;
1098 unsigned long res = 999;
1099 int err;
1100
1101 err = qemu_strtoul(str, &endptr, 0, &res);
1102
1103 g_assert_cmpint(err, ==, 0);
bc7c08a2 1104 g_assert_cmpuint(res, ==, 12345);
c817c015
CT
1105 g_assert(endptr == str + 5);
1106}
1107
1108static void test_qemu_strtoul_null(void)
1109{
1110 char f = 'X';
1111 const char *endptr = &f;
1112 unsigned long res = 999;
1113 int err;
1114
1115 err = qemu_strtoul(NULL, &endptr, 0, &res);
1116
1117 g_assert_cmpint(err, ==, -EINVAL);
1118 g_assert(endptr == NULL);
1119}
1120
1121static void test_qemu_strtoul_empty(void)
1122{
1123 const char *str = "";
1124 char f = 'X';
1125 const char *endptr = &f;
1126 unsigned long res = 999;
1127 int err;
1128
1129 err = qemu_strtoul(str, &endptr, 0, &res);
1130
47d4be12 1131 g_assert_cmpint(err, ==, -EINVAL);
73245450 1132 g_assert(endptr == str);
c817c015
CT
1133}
1134
1135static void test_qemu_strtoul_whitespace(void)
1136{
1137 const char *str = " \t ";
1138 char f = 'X';
1139 const char *endptr = &f;
1140 unsigned long res = 999;
1141 int err;
1142
1143 err = qemu_strtoul(str, &endptr, 0, &res);
1144
47d4be12 1145 g_assert_cmpint(err, ==, -EINVAL);
73245450 1146 g_assert(endptr == str);
c817c015
CT
1147}
1148
1149static void test_qemu_strtoul_invalid(void)
1150{
1151 const char *str = " xxxx \t abc";
1152 char f = 'X';
1153 const char *endptr = &f;
1154 unsigned long res = 999;
1155 int err;
1156
1157 err = qemu_strtoul(str, &endptr, 0, &res);
1158
47d4be12 1159 g_assert_cmpint(err, ==, -EINVAL);
73245450 1160 g_assert(endptr == str);
c817c015
CT
1161}
1162
1163static void test_qemu_strtoul_trailing(void)
1164{
1165 const char *str = "123xxx";
1166 char f = 'X';
1167 const char *endptr = &f;
1168 unsigned long res = 999;
1169 int err;
1170
1171 err = qemu_strtoul(str, &endptr, 0, &res);
1172
1173 g_assert_cmpint(err, ==, 0);
bc7c08a2 1174 g_assert_cmpuint(res, ==, 123);
c817c015
CT
1175 g_assert(endptr == str + 3);
1176}
1177
1178static void test_qemu_strtoul_octal(void)
1179{
1180 const char *str = "0123";
1181 char f = 'X';
1182 const char *endptr = &f;
1183 unsigned long res = 999;
1184 int err;
1185
1186 err = qemu_strtoul(str, &endptr, 8, &res);
1187
1188 g_assert_cmpint(err, ==, 0);
bc7c08a2 1189 g_assert_cmpuint(res, ==, 0123);
c817c015
CT
1190 g_assert(endptr == str + strlen(str));
1191
1192 res = 999;
1193 endptr = &f;
1194 err = qemu_strtoul(str, &endptr, 0, &res);
1195
1196 g_assert_cmpint(err, ==, 0);
bc7c08a2 1197 g_assert_cmpuint(res, ==, 0123);
c817c015
CT
1198 g_assert(endptr == str + strlen(str));
1199}
1200
1201static void test_qemu_strtoul_decimal(void)
1202{
1203 const char *str = "0123";
1204 char f = 'X';
1205 const char *endptr = &f;
1206 unsigned long res = 999;
1207 int err;
1208
1209 err = qemu_strtoul(str, &endptr, 10, &res);
1210
1211 g_assert_cmpint(err, ==, 0);
bc7c08a2 1212 g_assert_cmpuint(res, ==, 123);
c817c015
CT
1213 g_assert(endptr == str + strlen(str));
1214
1215 str = "123";
1216 res = 999;
1217 endptr = &f;
1218 err = qemu_strtoul(str, &endptr, 0, &res);
1219
1220 g_assert_cmpint(err, ==, 0);
bc7c08a2 1221 g_assert_cmpuint(res, ==, 123);
c817c015
CT
1222 g_assert(endptr == str + strlen(str));
1223}
1224
1225static void test_qemu_strtoul_hex(void)
1226{
1227 const char *str = "0123";
1228 char f = 'X';
1229 const char *endptr = &f;
1230 unsigned long res = 999;
1231 int err;
1232
1233 err = qemu_strtoul(str, &endptr, 16, &res);
1234
1235 g_assert_cmpint(err, ==, 0);
bc7c08a2 1236 g_assert_cmphex(res, ==, 0x123);
c817c015
CT
1237 g_assert(endptr == str + strlen(str));
1238
1239 str = "0x123";
1240 res = 999;
1241 endptr = &f;
1242 err = qemu_strtoul(str, &endptr, 0, &res);
1243
1244 g_assert_cmpint(err, ==, 0);
bc7c08a2 1245 g_assert_cmphex(res, ==, 0x123);
c817c015
CT
1246 g_assert(endptr == str + strlen(str));
1247}
1248
1249static void test_qemu_strtoul_max(void)
1250{
d6f723b5 1251 char *str = g_strdup_printf("%lu", ULONG_MAX);
c817c015
CT
1252 char f = 'X';
1253 const char *endptr = &f;
1254 unsigned long res = 999;
1255 int err;
1256
1257 err = qemu_strtoul(str, &endptr, 0, &res);
1258
1259 g_assert_cmpint(err, ==, 0);
bc7c08a2 1260 g_assert_cmphex(res, ==, ULONG_MAX);
c817c015 1261 g_assert(endptr == str + strlen(str));
d6f723b5 1262 g_free(str);
c817c015
CT
1263}
1264
1265static void test_qemu_strtoul_overflow(void)
1266{
1267 const char *str = "99999999999999999999999999999999999999999999";
1268 char f = 'X';
1269 const char *endptr = &f;
1270 unsigned long res = 999;
1271 int err;
1272
1273 err = qemu_strtoul(str, &endptr, 0, &res);
1274
1275 g_assert_cmpint(err, ==, -ERANGE);
bc7c08a2 1276 g_assert_cmphex(res, ==, ULONG_MAX);
c817c015
CT
1277 g_assert(endptr == str + strlen(str));
1278}
1279
1280static void test_qemu_strtoul_underflow(void)
1281{
1282 const char *str = "-99999999999999999999999999999999999999999999";
1283 char f = 'X';
1284 const char *endptr = &f;
1285 unsigned long res = 999;
1286 int err;
1287
1288 err = qemu_strtoul(str, &endptr, 0, &res);
1289
1290 g_assert_cmpint(err, ==, -ERANGE);
bc7c08a2 1291 g_assert_cmpuint(res, ==, -1ul);
c817c015
CT
1292 g_assert(endptr == str + strlen(str));
1293}
1294
1295static void test_qemu_strtoul_negative(void)
1296{
1297 const char *str = " \t -321";
1298 char f = 'X';
1299 const char *endptr = &f;
1300 unsigned long res = 999;
1301 int err;
1302
1303 err = qemu_strtoul(str, &endptr, 0, &res);
1304
1305 g_assert_cmpint(err, ==, 0);
bc7c08a2 1306 g_assert_cmpuint(res, ==, -321ul);
c817c015
CT
1307 g_assert(endptr == str + strlen(str));
1308}
1309
1310static void test_qemu_strtoul_full_correct(void)
1311{
1312 const char *str = "123";
1313 unsigned long res = 999;
1314 int err;
1315
1316 err = qemu_strtoul(str, NULL, 0, &res);
1317
1318 g_assert_cmpint(err, ==, 0);
bc7c08a2 1319 g_assert_cmpuint(res, ==, 123);
c817c015
CT
1320}
1321
1322static void test_qemu_strtoul_full_null(void)
1323{
1324 unsigned long res = 999;
1325 int err;
1326
1327 err = qemu_strtoul(NULL, NULL, 0, &res);
1328
1329 g_assert_cmpint(err, ==, -EINVAL);
1330}
1331
1332static void test_qemu_strtoul_full_empty(void)
1333{
1334 const char *str = "";
1335 unsigned long res = 999;
1336 int err;
1337
1338 err = qemu_strtoul(str, NULL, 0, &res);
1339
47d4be12 1340 g_assert_cmpint(err, ==, -EINVAL);
c817c015
CT
1341}
1342static void test_qemu_strtoul_full_negative(void)
1343{
1344 const char *str = " \t -321";
1345 unsigned long res = 999;
1346 int err;
1347
1348 err = qemu_strtoul(str, NULL, 0, &res);
1349 g_assert_cmpint(err, ==, 0);
bc7c08a2 1350 g_assert_cmpuint(res, ==, -321ul);
c817c015
CT
1351}
1352
1353static void test_qemu_strtoul_full_trailing(void)
1354{
1355 const char *str = "123xxx";
1356 unsigned long res;
1357 int err;
1358
1359 err = qemu_strtoul(str, NULL, 0, &res);
1360
1361 g_assert_cmpint(err, ==, -EINVAL);
1362}
1363
1364static void test_qemu_strtoul_full_max(void)
1365{
d6f723b5 1366 char *str = g_strdup_printf("%lu", ULONG_MAX);
c817c015
CT
1367 unsigned long res = 999;
1368 int err;
1369
1370 err = qemu_strtoul(str, NULL, 0, &res);
1371
1372 g_assert_cmpint(err, ==, 0);
bc7c08a2 1373 g_assert_cmphex(res, ==, ULONG_MAX);
d6f723b5 1374 g_free(str);
c817c015
CT
1375}
1376
b30d1886 1377static void test_qemu_strtoi64_correct(void)
8ac4df40
CT
1378{
1379 const char *str = "12345 foo";
1380 char f = 'X';
1381 const char *endptr = &f;
1382 int64_t res = 999;
1383 int err;
1384
b30d1886 1385 err = qemu_strtoi64(str, &endptr, 0, &res);
8ac4df40
CT
1386
1387 g_assert_cmpint(err, ==, 0);
1388 g_assert_cmpint(res, ==, 12345);
1389 g_assert(endptr == str + 5);
1390}
1391
b30d1886 1392static void test_qemu_strtoi64_null(void)
8ac4df40
CT
1393{
1394 char f = 'X';
1395 const char *endptr = &f;
1396 int64_t res = 999;
1397 int err;
1398
b30d1886 1399 err = qemu_strtoi64(NULL, &endptr, 0, &res);
8ac4df40
CT
1400
1401 g_assert_cmpint(err, ==, -EINVAL);
1402 g_assert(endptr == NULL);
1403}
1404
b30d1886 1405static void test_qemu_strtoi64_empty(void)
8ac4df40
CT
1406{
1407 const char *str = "";
1408 char f = 'X';
1409 const char *endptr = &f;
1410 int64_t res = 999;
1411 int err;
1412
b30d1886 1413 err = qemu_strtoi64(str, &endptr, 0, &res);
8ac4df40 1414
47d4be12 1415 g_assert_cmpint(err, ==, -EINVAL);
73245450 1416 g_assert(endptr == str);
8ac4df40
CT
1417}
1418
b30d1886 1419static void test_qemu_strtoi64_whitespace(void)
8ac4df40
CT
1420{
1421 const char *str = " \t ";
1422 char f = 'X';
1423 const char *endptr = &f;
1424 int64_t res = 999;
1425 int err;
1426
b30d1886 1427 err = qemu_strtoi64(str, &endptr, 0, &res);
8ac4df40 1428
47d4be12 1429 g_assert_cmpint(err, ==, -EINVAL);
73245450 1430 g_assert(endptr == str);
8ac4df40
CT
1431}
1432
b30d1886 1433static void test_qemu_strtoi64_invalid(void)
8ac4df40
CT
1434{
1435 const char *str = " xxxx \t abc";
1436 char f = 'X';
1437 const char *endptr = &f;
1438 int64_t res = 999;
1439 int err;
1440
b30d1886 1441 err = qemu_strtoi64(str, &endptr, 0, &res);
8ac4df40 1442
47d4be12 1443 g_assert_cmpint(err, ==, -EINVAL);
73245450 1444 g_assert(endptr == str);
8ac4df40
CT
1445}
1446
b30d1886 1447static void test_qemu_strtoi64_trailing(void)
8ac4df40
CT
1448{
1449 const char *str = "123xxx";
1450 char f = 'X';
1451 const char *endptr = &f;
1452 int64_t res = 999;
1453 int err;
1454
b30d1886 1455 err = qemu_strtoi64(str, &endptr, 0, &res);
8ac4df40
CT
1456
1457 g_assert_cmpint(err, ==, 0);
1458 g_assert_cmpint(res, ==, 123);
1459 g_assert(endptr == str + 3);
1460}
1461
b30d1886 1462static void test_qemu_strtoi64_octal(void)
8ac4df40
CT
1463{
1464 const char *str = "0123";
1465 char f = 'X';
1466 const char *endptr = &f;
1467 int64_t res = 999;
1468 int err;
1469
b30d1886 1470 err = qemu_strtoi64(str, &endptr, 8, &res);
8ac4df40
CT
1471
1472 g_assert_cmpint(err, ==, 0);
1473 g_assert_cmpint(res, ==, 0123);
1474 g_assert(endptr == str + strlen(str));
1475
1476 endptr = &f;
1477 res = 999;
b30d1886 1478 err = qemu_strtoi64(str, &endptr, 0, &res);
8ac4df40
CT
1479
1480 g_assert_cmpint(err, ==, 0);
1481 g_assert_cmpint(res, ==, 0123);
1482 g_assert(endptr == str + strlen(str));
1483}
1484
b30d1886 1485static void test_qemu_strtoi64_decimal(void)
8ac4df40
CT
1486{
1487 const char *str = "0123";
1488 char f = 'X';
1489 const char *endptr = &f;
1490 int64_t res = 999;
1491 int err;
1492
b30d1886 1493 err = qemu_strtoi64(str, &endptr, 10, &res);
8ac4df40
CT
1494
1495 g_assert_cmpint(err, ==, 0);
1496 g_assert_cmpint(res, ==, 123);
1497 g_assert(endptr == str + strlen(str));
1498
1499 str = "123";
1500 endptr = &f;
1501 res = 999;
b30d1886 1502 err = qemu_strtoi64(str, &endptr, 0, &res);
8ac4df40
CT
1503
1504 g_assert_cmpint(err, ==, 0);
1505 g_assert_cmpint(res, ==, 123);
1506 g_assert(endptr == str + strlen(str));
1507}
1508
b30d1886 1509static void test_qemu_strtoi64_hex(void)
8ac4df40
CT
1510{
1511 const char *str = "0123";
1512 char f = 'X';
1513 const char *endptr = &f;
1514 int64_t res = 999;
1515 int err;
1516
b30d1886 1517 err = qemu_strtoi64(str, &endptr, 16, &res);
8ac4df40
CT
1518
1519 g_assert_cmpint(err, ==, 0);
1520 g_assert_cmpint(res, ==, 0x123);
1521 g_assert(endptr == str + strlen(str));
1522
1523 str = "0x123";
1524 endptr = &f;
1525 res = 999;
b30d1886 1526 err = qemu_strtoi64(str, &endptr, 0, &res);
8ac4df40
CT
1527
1528 g_assert_cmpint(err, ==, 0);
1529 g_assert_cmpint(res, ==, 0x123);
1530 g_assert(endptr == str + strlen(str));
1531}
1532
b30d1886 1533static void test_qemu_strtoi64_max(void)
8ac4df40 1534{
d6f723b5 1535 char *str = g_strdup_printf("%lld", LLONG_MAX);
8ac4df40
CT
1536 char f = 'X';
1537 const char *endptr = &f;
1538 int64_t res = 999;
1539 int err;
1540
b30d1886 1541 err = qemu_strtoi64(str, &endptr, 0, &res);
8ac4df40
CT
1542
1543 g_assert_cmpint(err, ==, 0);
1544 g_assert_cmpint(res, ==, LLONG_MAX);
1545 g_assert(endptr == str + strlen(str));
d6f723b5 1546 g_free(str);
8ac4df40
CT
1547}
1548
b30d1886 1549static void test_qemu_strtoi64_overflow(void)
8ac4df40
CT
1550{
1551 const char *str = "99999999999999999999999999999999999999999999";
1552 char f = 'X';
1553 const char *endptr = &f;
1554 int64_t res = 999;
1555 int err;
1556
b30d1886 1557 err = qemu_strtoi64(str, &endptr, 0, &res);
8ac4df40
CT
1558
1559 g_assert_cmpint(err, ==, -ERANGE);
1560 g_assert_cmpint(res, ==, LLONG_MAX);
1561 g_assert(endptr == str + strlen(str));
1562}
1563
b30d1886 1564static void test_qemu_strtoi64_underflow(void)
8ac4df40
CT
1565{
1566 const char *str = "-99999999999999999999999999999999999999999999";
1567 char f = 'X';
1568 const char *endptr = &f;
1569 int64_t res = 999;
1570 int err;
1571
b30d1886 1572 err = qemu_strtoi64(str, &endptr, 0, &res);
8ac4df40
CT
1573
1574 g_assert_cmpint(err, ==, -ERANGE);
1575 g_assert_cmpint(res, ==, LLONG_MIN);
1576 g_assert(endptr == str + strlen(str));
1577}
1578
b30d1886 1579static void test_qemu_strtoi64_negative(void)
8ac4df40
CT
1580{
1581 const char *str = " \t -321";
1582 char f = 'X';
1583 const char *endptr = &f;
1584 int64_t res = 999;
1585 int err;
1586
b30d1886 1587 err = qemu_strtoi64(str, &endptr, 0, &res);
8ac4df40
CT
1588
1589 g_assert_cmpint(err, ==, 0);
1590 g_assert_cmpint(res, ==, -321);
1591 g_assert(endptr == str + strlen(str));
1592}
1593
b30d1886 1594static void test_qemu_strtoi64_full_correct(void)
8ac4df40
CT
1595{
1596 const char *str = "123";
1597 int64_t res = 999;
1598 int err;
1599
b30d1886 1600 err = qemu_strtoi64(str, NULL, 0, &res);
8ac4df40
CT
1601
1602 g_assert_cmpint(err, ==, 0);
1603 g_assert_cmpint(res, ==, 123);
1604}
1605
b30d1886 1606static void test_qemu_strtoi64_full_null(void)
8ac4df40
CT
1607{
1608 int64_t res = 999;
1609 int err;
1610
b30d1886 1611 err = qemu_strtoi64(NULL, NULL, 0, &res);
8ac4df40
CT
1612
1613 g_assert_cmpint(err, ==, -EINVAL);
1614}
1615
b30d1886 1616static void test_qemu_strtoi64_full_empty(void)
8ac4df40
CT
1617{
1618 const char *str = "";
1619 int64_t res = 999;
1620 int err;
1621
b30d1886 1622 err = qemu_strtoi64(str, NULL, 0, &res);
8ac4df40 1623
47d4be12 1624 g_assert_cmpint(err, ==, -EINVAL);
8ac4df40
CT
1625}
1626
b30d1886 1627static void test_qemu_strtoi64_full_negative(void)
8ac4df40
CT
1628{
1629 const char *str = " \t -321";
1630 int64_t res = 999;
1631 int err;
1632
b30d1886 1633 err = qemu_strtoi64(str, NULL, 0, &res);
8ac4df40
CT
1634
1635 g_assert_cmpint(err, ==, 0);
1636 g_assert_cmpint(res, ==, -321);
1637}
1638
b30d1886 1639static void test_qemu_strtoi64_full_trailing(void)
8ac4df40
CT
1640{
1641 const char *str = "123xxx";
1642 int64_t res = 999;
1643 int err;
1644
b30d1886 1645 err = qemu_strtoi64(str, NULL, 0, &res);
8ac4df40
CT
1646
1647 g_assert_cmpint(err, ==, -EINVAL);
1648}
1649
b30d1886 1650static void test_qemu_strtoi64_full_max(void)
8ac4df40
CT
1651{
1652
d6f723b5 1653 char *str = g_strdup_printf("%lld", LLONG_MAX);
8ac4df40
CT
1654 int64_t res;
1655 int err;
1656
b30d1886 1657 err = qemu_strtoi64(str, NULL, 0, &res);
8ac4df40
CT
1658
1659 g_assert_cmpint(err, ==, 0);
1660 g_assert_cmpint(res, ==, LLONG_MAX);
d6f723b5 1661 g_free(str);
8ac4df40
CT
1662}
1663
b30d1886 1664static void test_qemu_strtou64_correct(void)
3904e6bf
CT
1665{
1666 const char *str = "12345 foo";
1667 char f = 'X';
1668 const char *endptr = &f;
1669 uint64_t res = 999;
1670 int err;
1671
b30d1886 1672 err = qemu_strtou64(str, &endptr, 0, &res);
3904e6bf
CT
1673
1674 g_assert_cmpint(err, ==, 0);
bc7c08a2 1675 g_assert_cmpuint(res, ==, 12345);
3904e6bf
CT
1676 g_assert(endptr == str + 5);
1677}
1678
b30d1886 1679static void test_qemu_strtou64_null(void)
3904e6bf
CT
1680{
1681 char f = 'X';
1682 const char *endptr = &f;
1683 uint64_t res = 999;
1684 int err;
1685
b30d1886 1686 err = qemu_strtou64(NULL, &endptr, 0, &res);
3904e6bf
CT
1687
1688 g_assert_cmpint(err, ==, -EINVAL);
1689 g_assert(endptr == NULL);
1690}
1691
b30d1886 1692static void test_qemu_strtou64_empty(void)
3904e6bf
CT
1693{
1694 const char *str = "";
1695 char f = 'X';
1696 const char *endptr = &f;
1697 uint64_t res = 999;
1698 int err;
1699
b30d1886 1700 err = qemu_strtou64(str, &endptr, 0, &res);
3904e6bf 1701
47d4be12 1702 g_assert_cmpint(err, ==, -EINVAL);
73245450 1703 g_assert(endptr == str);
3904e6bf
CT
1704}
1705
b30d1886 1706static void test_qemu_strtou64_whitespace(void)
3904e6bf
CT
1707{
1708 const char *str = " \t ";
1709 char f = 'X';
1710 const char *endptr = &f;
1711 uint64_t res = 999;
1712 int err;
1713
b30d1886 1714 err = qemu_strtou64(str, &endptr, 0, &res);
3904e6bf 1715
47d4be12 1716 g_assert_cmpint(err, ==, -EINVAL);
73245450 1717 g_assert(endptr == str);
3904e6bf
CT
1718}
1719
b30d1886 1720static void test_qemu_strtou64_invalid(void)
3904e6bf
CT
1721{
1722 const char *str = " xxxx \t abc";
1723 char f = 'X';
1724 const char *endptr = &f;
1725 uint64_t res = 999;
1726 int err;
1727
b30d1886 1728 err = qemu_strtou64(str, &endptr, 0, &res);
3904e6bf 1729
47d4be12 1730 g_assert_cmpint(err, ==, -EINVAL);
73245450 1731 g_assert(endptr == str);
3904e6bf
CT
1732}
1733
b30d1886 1734static void test_qemu_strtou64_trailing(void)
3904e6bf
CT
1735{
1736 const char *str = "123xxx";
1737 char f = 'X';
1738 const char *endptr = &f;
1739 uint64_t res = 999;
1740 int err;
1741
b30d1886 1742 err = qemu_strtou64(str, &endptr, 0, &res);
3904e6bf
CT
1743
1744 g_assert_cmpint(err, ==, 0);
bc7c08a2 1745 g_assert_cmpuint(res, ==, 123);
3904e6bf
CT
1746 g_assert(endptr == str + 3);
1747}
1748
b30d1886 1749static void test_qemu_strtou64_octal(void)
3904e6bf
CT
1750{
1751 const char *str = "0123";
1752 char f = 'X';
1753 const char *endptr = &f;
1754 uint64_t res = 999;
1755 int err;
1756
b30d1886 1757 err = qemu_strtou64(str, &endptr, 8, &res);
3904e6bf
CT
1758
1759 g_assert_cmpint(err, ==, 0);
bc7c08a2 1760 g_assert_cmpuint(res, ==, 0123);
3904e6bf
CT
1761 g_assert(endptr == str + strlen(str));
1762
1763 endptr = &f;
1764 res = 999;
b30d1886 1765 err = qemu_strtou64(str, &endptr, 0, &res);
3904e6bf
CT
1766
1767 g_assert_cmpint(err, ==, 0);
bc7c08a2 1768 g_assert_cmpuint(res, ==, 0123);
3904e6bf
CT
1769 g_assert(endptr == str + strlen(str));
1770}
1771
b30d1886 1772static void test_qemu_strtou64_decimal(void)
3904e6bf
CT
1773{
1774 const char *str = "0123";
1775 char f = 'X';
1776 const char *endptr = &f;
1777 uint64_t res = 999;
1778 int err;
1779
b30d1886 1780 err = qemu_strtou64(str, &endptr, 10, &res);
3904e6bf
CT
1781
1782 g_assert_cmpint(err, ==, 0);
bc7c08a2 1783 g_assert_cmpuint(res, ==, 123);
3904e6bf
CT
1784 g_assert(endptr == str + strlen(str));
1785
1786 str = "123";
1787 endptr = &f;
1788 res = 999;
b30d1886 1789 err = qemu_strtou64(str, &endptr, 0, &res);
3904e6bf
CT
1790
1791 g_assert_cmpint(err, ==, 0);
bc7c08a2 1792 g_assert_cmpuint(res, ==, 123);
3904e6bf
CT
1793 g_assert(endptr == str + strlen(str));
1794}
1795
b30d1886 1796static void test_qemu_strtou64_hex(void)
3904e6bf
CT
1797{
1798 const char *str = "0123";
1799 char f = 'X';
1800 const char *endptr = &f;
1801 uint64_t res = 999;
1802 int err;
1803
b30d1886 1804 err = qemu_strtou64(str, &endptr, 16, &res);
3904e6bf
CT
1805
1806 g_assert_cmpint(err, ==, 0);
bc7c08a2 1807 g_assert_cmphex(res, ==, 0x123);
3904e6bf
CT
1808 g_assert(endptr == str + strlen(str));
1809
1810 str = "0x123";
1811 endptr = &f;
1812 res = 999;
b30d1886 1813 err = qemu_strtou64(str, &endptr, 0, &res);
3904e6bf
CT
1814
1815 g_assert_cmpint(err, ==, 0);
bc7c08a2 1816 g_assert_cmphex(res, ==, 0x123);
3904e6bf
CT
1817 g_assert(endptr == str + strlen(str));
1818}
1819
b30d1886 1820static void test_qemu_strtou64_max(void)
3904e6bf 1821{
d6f723b5 1822 char *str = g_strdup_printf("%llu", ULLONG_MAX);
3904e6bf
CT
1823 char f = 'X';
1824 const char *endptr = &f;
1825 uint64_t res = 999;
1826 int err;
1827
b30d1886 1828 err = qemu_strtou64(str, &endptr, 0, &res);
3904e6bf
CT
1829
1830 g_assert_cmpint(err, ==, 0);
bc7c08a2 1831 g_assert_cmphex(res, ==, ULLONG_MAX);
3904e6bf 1832 g_assert(endptr == str + strlen(str));
d6f723b5 1833 g_free(str);
3904e6bf
CT
1834}
1835
b30d1886 1836static void test_qemu_strtou64_overflow(void)
3904e6bf
CT
1837{
1838 const char *str = "99999999999999999999999999999999999999999999";
1839 char f = 'X';
1840 const char *endptr = &f;
1841 uint64_t res = 999;
1842 int err;
1843
b30d1886 1844 err = qemu_strtou64(str, &endptr, 0, &res);
3904e6bf
CT
1845
1846 g_assert_cmpint(err, ==, -ERANGE);
bc7c08a2 1847 g_assert_cmphex(res, ==, ULLONG_MAX);
3904e6bf
CT
1848 g_assert(endptr == str + strlen(str));
1849}
1850
b30d1886 1851static void test_qemu_strtou64_underflow(void)
3904e6bf
CT
1852{
1853 const char *str = "-99999999999999999999999999999999999999999999";
1854 char f = 'X';
1855 const char *endptr = &f;
1856 uint64_t res = 999;
1857 int err;
1858
b30d1886 1859 err = qemu_strtou64(str, &endptr, 0, &res);
3904e6bf
CT
1860
1861 g_assert_cmpint(err, ==, -ERANGE);
bc7c08a2 1862 g_assert_cmphex(res, ==, -1ull);
3904e6bf
CT
1863 g_assert(endptr == str + strlen(str));
1864}
1865
b30d1886 1866static void test_qemu_strtou64_negative(void)
3904e6bf
CT
1867{
1868 const char *str = " \t -321";
1869 char f = 'X';
1870 const char *endptr = &f;
1871 uint64_t res = 999;
1872 int err;
1873
b30d1886 1874 err = qemu_strtou64(str, &endptr, 0, &res);
3904e6bf
CT
1875
1876 g_assert_cmpint(err, ==, 0);
bc7c08a2 1877 g_assert_cmpuint(res, ==, -321ull);
3904e6bf
CT
1878 g_assert(endptr == str + strlen(str));
1879}
1880
b30d1886 1881static void test_qemu_strtou64_full_correct(void)
3904e6bf
CT
1882{
1883 const char *str = "18446744073709551614";
1884 uint64_t res = 999;
1885 int err;
1886
b30d1886 1887 err = qemu_strtou64(str, NULL, 0, &res);
3904e6bf
CT
1888
1889 g_assert_cmpint(err, ==, 0);
bc7c08a2 1890 g_assert_cmpuint(res, ==, 18446744073709551614ull);
3904e6bf
CT
1891}
1892
b30d1886 1893static void test_qemu_strtou64_full_null(void)
3904e6bf
CT
1894{
1895 uint64_t res = 999;
1896 int err;
1897
b30d1886 1898 err = qemu_strtou64(NULL, NULL, 0, &res);
3904e6bf
CT
1899
1900 g_assert_cmpint(err, ==, -EINVAL);
1901}
1902
b30d1886 1903static void test_qemu_strtou64_full_empty(void)
3904e6bf
CT
1904{
1905 const char *str = "";
1906 uint64_t res = 999;
1907 int err;
1908
b30d1886 1909 err = qemu_strtou64(str, NULL, 0, &res);
3904e6bf 1910
47d4be12 1911 g_assert_cmpint(err, ==, -EINVAL);
3904e6bf
CT
1912}
1913
b30d1886 1914static void test_qemu_strtou64_full_negative(void)
3904e6bf
CT
1915{
1916 const char *str = " \t -321";
1917 uint64_t res = 999;
1918 int err;
1919
b30d1886 1920 err = qemu_strtou64(str, NULL, 0, &res);
3904e6bf
CT
1921
1922 g_assert_cmpint(err, ==, 0);
bc7c08a2 1923 g_assert_cmpuint(res, ==, -321ull);
3904e6bf
CT
1924}
1925
b30d1886 1926static void test_qemu_strtou64_full_trailing(void)
3904e6bf
CT
1927{
1928 const char *str = "18446744073709551614xxxxxx";
1929 uint64_t res = 999;
1930 int err;
1931
b30d1886 1932 err = qemu_strtou64(str, NULL, 0, &res);
3904e6bf
CT
1933
1934 g_assert_cmpint(err, ==, -EINVAL);
1935}
1936
b30d1886 1937static void test_qemu_strtou64_full_max(void)
3904e6bf 1938{
d6f723b5 1939 char *str = g_strdup_printf("%lld", ULLONG_MAX);
3904e6bf
CT
1940 uint64_t res = 999;
1941 int err;
1942
b30d1886 1943 err = qemu_strtou64(str, NULL, 0, &res);
3904e6bf
CT
1944
1945 g_assert_cmpint(err, ==, 0);
bc7c08a2 1946 g_assert_cmphex(res, ==, ULLONG_MAX);
d6f723b5 1947 g_free(str);
3904e6bf
CT
1948}
1949
fe854538
MAL
1950static void test_qemu_strtosz_simple(void)
1951{
0b742797 1952 const char *str;
af02f4c5 1953 const char *endptr;
f17fd4fd 1954 int err;
f46bfdbf 1955 uint64_t res = 0xbaadf00d;
fe854538 1956
0b742797 1957 str = "0";
f17fd4fd
MA
1958 err = qemu_strtosz(str, &endptr, &res);
1959 g_assert_cmpint(err, ==, 0);
0b742797
MA
1960 g_assert_cmpint(res, ==, 0);
1961 g_assert(endptr == str + 1);
1962
dab9cc92 1963 str = "12345";
f17fd4fd
MA
1964 err = qemu_strtosz(str, &endptr, &res);
1965 g_assert_cmpint(err, ==, 0);
dab9cc92
MA
1966 g_assert_cmpint(res, ==, 12345);
1967 g_assert(endptr == str + 5);
fe854538 1968
f17fd4fd
MA
1969 err = qemu_strtosz(str, NULL, &res);
1970 g_assert_cmpint(err, ==, 0);
dab9cc92 1971 g_assert_cmpint(res, ==, 12345);
0b742797
MA
1972
1973 /* Note: precision is 53 bits since we're parsing with strtod() */
1974
1975 str = "9007199254740991"; /* 2^53-1 */
f17fd4fd
MA
1976 err = qemu_strtosz(str, &endptr, &res);
1977 g_assert_cmpint(err, ==, 0);
0b742797
MA
1978 g_assert_cmpint(res, ==, 0x1fffffffffffff);
1979 g_assert(endptr == str + 16);
1980
1981 str = "9007199254740992"; /* 2^53 */
f17fd4fd
MA
1982 err = qemu_strtosz(str, &endptr, &res);
1983 g_assert_cmpint(err, ==, 0);
0b742797
MA
1984 g_assert_cmpint(res, ==, 0x20000000000000);
1985 g_assert(endptr == str + 16);
1986
1987 str = "9007199254740993"; /* 2^53+1 */
f17fd4fd
MA
1988 err = qemu_strtosz(str, &endptr, &res);
1989 g_assert_cmpint(err, ==, 0);
0b742797
MA
1990 g_assert_cmpint(res, ==, 0x20000000000000); /* rounded to 53 bits */
1991 g_assert(endptr == str + 16);
1992
f46bfdbf 1993 str = "18446744073709549568"; /* 0xfffffffffffff800 (53 msbs set) */
f17fd4fd
MA
1994 err = qemu_strtosz(str, &endptr, &res);
1995 g_assert_cmpint(err, ==, 0);
f46bfdbf
MA
1996 g_assert_cmpint(res, ==, 0xfffffffffffff800);
1997 g_assert(endptr == str + 20);
0b742797 1998
f46bfdbf 1999 str = "18446744073709550591"; /* 0xfffffffffffffbff */
f17fd4fd
MA
2000 err = qemu_strtosz(str, &endptr, &res);
2001 g_assert_cmpint(err, ==, 0);
f46bfdbf
MA
2002 g_assert_cmpint(res, ==, 0xfffffffffffff800); /* rounded to 53 bits */
2003 g_assert(endptr == str + 20);
0b742797
MA
2004
2005 /* 0x7ffffffffffffe00..0x7fffffffffffffff get rounded to
2006 * 0x8000000000000000, thus -ERANGE; see test_qemu_strtosz_erange() */
fe854538
MAL
2007}
2008
2009static void test_qemu_strtosz_units(void)
2010{
2011 const char *none = "1";
2012 const char *b = "1B";
2013 const char *k = "1K";
2014 const char *m = "1M";
2015 const char *g = "1G";
2016 const char *t = "1T";
2017 const char *p = "1P";
2018 const char *e = "1E";
f17fd4fd 2019 int err;
af02f4c5 2020 const char *endptr;
f46bfdbf 2021 uint64_t res = 0xbaadf00d;
fe854538
MAL
2022
2023 /* default is M */
f17fd4fd
MA
2024 err = qemu_strtosz_MiB(none, &endptr, &res);
2025 g_assert_cmpint(err, ==, 0);
d23b6caa 2026 g_assert_cmpint(res, ==, MiB);
019144b2 2027 g_assert(endptr == none + 1);
fe854538 2028
f17fd4fd
MA
2029 err = qemu_strtosz(b, &endptr, &res);
2030 g_assert_cmpint(err, ==, 0);
fe854538 2031 g_assert_cmpint(res, ==, 1);
019144b2 2032 g_assert(endptr == b + 2);
fe854538 2033
f17fd4fd
MA
2034 err = qemu_strtosz(k, &endptr, &res);
2035 g_assert_cmpint(err, ==, 0);
d23b6caa 2036 g_assert_cmpint(res, ==, KiB);
019144b2 2037 g_assert(endptr == k + 2);
fe854538 2038
f17fd4fd
MA
2039 err = qemu_strtosz(m, &endptr, &res);
2040 g_assert_cmpint(err, ==, 0);
d23b6caa 2041 g_assert_cmpint(res, ==, MiB);
019144b2 2042 g_assert(endptr == m + 2);
fe854538 2043
f17fd4fd
MA
2044 err = qemu_strtosz(g, &endptr, &res);
2045 g_assert_cmpint(err, ==, 0);
d23b6caa 2046 g_assert_cmpint(res, ==, GiB);
019144b2 2047 g_assert(endptr == g + 2);
fe854538 2048
f17fd4fd
MA
2049 err = qemu_strtosz(t, &endptr, &res);
2050 g_assert_cmpint(err, ==, 0);
d23b6caa 2051 g_assert_cmpint(res, ==, TiB);
019144b2 2052 g_assert(endptr == t + 2);
fe854538 2053
f17fd4fd
MA
2054 err = qemu_strtosz(p, &endptr, &res);
2055 g_assert_cmpint(err, ==, 0);
d23b6caa 2056 g_assert_cmpint(res, ==, PiB);
019144b2 2057 g_assert(endptr == p + 2);
fe854538 2058
f17fd4fd
MA
2059 err = qemu_strtosz(e, &endptr, &res);
2060 g_assert_cmpint(err, ==, 0);
d23b6caa 2061 g_assert_cmpint(res, ==, EiB);
019144b2 2062 g_assert(endptr == e + 2);
fe854538
MAL
2063}
2064
2065static void test_qemu_strtosz_float(void)
2066{
2067 const char *str = "12.345M";
f17fd4fd 2068 int err;
af02f4c5 2069 const char *endptr;
f46bfdbf 2070 uint64_t res = 0xbaadf00d;
fe854538 2071
f17fd4fd
MA
2072 err = qemu_strtosz(str, &endptr, &res);
2073 g_assert_cmpint(err, ==, 0);
d23b6caa 2074 g_assert_cmpint(res, ==, 12.345 * MiB);
019144b2 2075 g_assert(endptr == str + 7);
fe854538
MAL
2076}
2077
18aec479
MA
2078static void test_qemu_strtosz_invalid(void)
2079{
2080 const char *str;
af02f4c5 2081 const char *endptr;
f17fd4fd 2082 int err;
f46bfdbf 2083 uint64_t res = 0xbaadf00d;
18aec479
MA
2084
2085 str = "";
f17fd4fd
MA
2086 err = qemu_strtosz(str, &endptr, &res);
2087 g_assert_cmpint(err, ==, -EINVAL);
18aec479
MA
2088 g_assert(endptr == str);
2089
2090 str = " \t ";
f17fd4fd
MA
2091 err = qemu_strtosz(str, &endptr, &res);
2092 g_assert_cmpint(err, ==, -EINVAL);
18aec479
MA
2093 g_assert(endptr == str);
2094
2095 str = "crap";
f17fd4fd
MA
2096 err = qemu_strtosz(str, &endptr, &res);
2097 g_assert_cmpint(err, ==, -EINVAL);
18aec479 2098 g_assert(endptr == str);
af02f4c5
DH
2099
2100 str = "inf";
2101 err = qemu_strtosz(str, &endptr, &res);
2102 g_assert_cmpint(err, ==, -EINVAL);
2103 g_assert(endptr == str);
2104
2105 str = "NaN";
2106 err = qemu_strtosz(str, &endptr, &res);
2107 g_assert_cmpint(err, ==, -EINVAL);
2108 g_assert(endptr == str);
18aec479
MA
2109}
2110
a6b4373f
MA
2111static void test_qemu_strtosz_trailing(void)
2112{
2113 const char *str;
af02f4c5 2114 const char *endptr;
f17fd4fd 2115 int err;
f46bfdbf 2116 uint64_t res = 0xbaadf00d;
a6b4373f
MA
2117
2118 str = "123xxx";
f17fd4fd 2119 err = qemu_strtosz_MiB(str, &endptr, &res);
d23b6caa 2120 g_assert_cmpint(res, ==, 123 * MiB);
a6b4373f
MA
2121 g_assert(endptr == str + 3);
2122
f17fd4fd
MA
2123 err = qemu_strtosz(str, NULL, &res);
2124 g_assert_cmpint(err, ==, -EINVAL);
4fcdf65a 2125
a6b4373f 2126 str = "1kiB";
f17fd4fd
MA
2127 err = qemu_strtosz(str, &endptr, &res);
2128 g_assert_cmpint(err, ==, 0);
a6b4373f
MA
2129 g_assert_cmpint(res, ==, 1024);
2130 g_assert(endptr == str + 2);
4fcdf65a 2131
f17fd4fd
MA
2132 err = qemu_strtosz(str, NULL, &res);
2133 g_assert_cmpint(err, ==, -EINVAL);
a6b4373f
MA
2134}
2135
fe854538
MAL
2136static void test_qemu_strtosz_erange(void)
2137{
0b742797 2138 const char *str;
af02f4c5 2139 const char *endptr;
f17fd4fd 2140 int err;
f46bfdbf 2141 uint64_t res = 0xbaadf00d;
fe854538 2142
0b742797 2143 str = "-1";
f17fd4fd
MA
2144 err = qemu_strtosz(str, &endptr, &res);
2145 g_assert_cmpint(err, ==, -ERANGE);
0b742797
MA
2146 g_assert(endptr == str + 2);
2147
f46bfdbf 2148 str = "18446744073709550592"; /* 0xfffffffffffffc00 */
f17fd4fd
MA
2149 err = qemu_strtosz(str, &endptr, &res);
2150 g_assert_cmpint(err, ==, -ERANGE);
f46bfdbf 2151 g_assert(endptr == str + 20);
0b742797 2152
f46bfdbf 2153 str = "18446744073709551615"; /* 2^64-1 */
f17fd4fd
MA
2154 err = qemu_strtosz(str, &endptr, &res);
2155 g_assert_cmpint(err, ==, -ERANGE);
f46bfdbf 2156 g_assert(endptr == str + 20);
0b742797 2157
f46bfdbf 2158 str = "18446744073709551616"; /* 2^64 */
f17fd4fd
MA
2159 err = qemu_strtosz(str, &endptr, &res);
2160 g_assert_cmpint(err, ==, -ERANGE);
f46bfdbf 2161 g_assert(endptr == str + 20);
0b742797 2162
f46bfdbf 2163 str = "20E";
f17fd4fd
MA
2164 err = qemu_strtosz(str, &endptr, &res);
2165 g_assert_cmpint(err, ==, -ERANGE);
019144b2 2166 g_assert(endptr == str + 3);
fe854538
MAL
2167}
2168
d2734d26 2169static void test_qemu_strtosz_metric(void)
fe854538 2170{
d2734d26 2171 const char *str = "12345k";
f17fd4fd 2172 int err;
af02f4c5 2173 const char *endptr;
f46bfdbf 2174 uint64_t res = 0xbaadf00d;
fe854538 2175
f17fd4fd
MA
2176 err = qemu_strtosz_metric(str, &endptr, &res);
2177 g_assert_cmpint(err, ==, 0);
fe854538 2178 g_assert_cmpint(res, ==, 12345000);
d2734d26 2179 g_assert(endptr == str + 6);
fe854538
MAL
2180}
2181
e3f9fe2d
EH
2182int main(int argc, char **argv)
2183{
2184 g_test_init(&argc, &argv, NULL);
2185
2186 g_test_add_func("/cutils/parse_uint/null", test_parse_uint_null);
2187 g_test_add_func("/cutils/parse_uint/empty", test_parse_uint_empty);
2188 g_test_add_func("/cutils/parse_uint/whitespace",
2189 test_parse_uint_whitespace);
2190 g_test_add_func("/cutils/parse_uint/invalid", test_parse_uint_invalid);
2191 g_test_add_func("/cutils/parse_uint/trailing", test_parse_uint_trailing);
2192 g_test_add_func("/cutils/parse_uint/correct", test_parse_uint_correct);
2193 g_test_add_func("/cutils/parse_uint/octal", test_parse_uint_octal);
2194 g_test_add_func("/cutils/parse_uint/decimal", test_parse_uint_decimal);
2195 g_test_add_func("/cutils/parse_uint/llong_max", test_parse_uint_llong_max);
2196 g_test_add_func("/cutils/parse_uint/overflow", test_parse_uint_overflow);
2197 g_test_add_func("/cutils/parse_uint/negative", test_parse_uint_negative);
2198 g_test_add_func("/cutils/parse_uint_full/trailing",
2199 test_parse_uint_full_trailing);
2200 g_test_add_func("/cutils/parse_uint_full/correct",
2201 test_parse_uint_full_correct);
2202
473a2a33
DB
2203 /* qemu_strtoi() tests */
2204 g_test_add_func("/cutils/qemu_strtoi/correct",
2205 test_qemu_strtoi_correct);
2206 g_test_add_func("/cutils/qemu_strtoi/null",
2207 test_qemu_strtoi_null);
2208 g_test_add_func("/cutils/qemu_strtoi/empty",
2209 test_qemu_strtoi_empty);
2210 g_test_add_func("/cutils/qemu_strtoi/whitespace",
2211 test_qemu_strtoi_whitespace);
2212 g_test_add_func("/cutils/qemu_strtoi/invalid",
2213 test_qemu_strtoi_invalid);
2214 g_test_add_func("/cutils/qemu_strtoi/trailing",
2215 test_qemu_strtoi_trailing);
2216 g_test_add_func("/cutils/qemu_strtoi/octal",
2217 test_qemu_strtoi_octal);
2218 g_test_add_func("/cutils/qemu_strtoi/decimal",
2219 test_qemu_strtoi_decimal);
2220 g_test_add_func("/cutils/qemu_strtoi/hex",
2221 test_qemu_strtoi_hex);
2222 g_test_add_func("/cutils/qemu_strtoi/max",
2223 test_qemu_strtoi_max);
2224 g_test_add_func("/cutils/qemu_strtoi/overflow",
2225 test_qemu_strtoi_overflow);
2226 g_test_add_func("/cutils/qemu_strtoi/underflow",
2227 test_qemu_strtoi_underflow);
2228 g_test_add_func("/cutils/qemu_strtoi/negative",
2229 test_qemu_strtoi_negative);
2230 g_test_add_func("/cutils/qemu_strtoi_full/correct",
2231 test_qemu_strtoi_full_correct);
2232 g_test_add_func("/cutils/qemu_strtoi_full/null",
2233 test_qemu_strtoi_full_null);
2234 g_test_add_func("/cutils/qemu_strtoi_full/empty",
2235 test_qemu_strtoi_full_empty);
2236 g_test_add_func("/cutils/qemu_strtoi_full/negative",
2237 test_qemu_strtoi_full_negative);
2238 g_test_add_func("/cutils/qemu_strtoi_full/trailing",
2239 test_qemu_strtoi_full_trailing);
2240 g_test_add_func("/cutils/qemu_strtoi_full/max",
2241 test_qemu_strtoi_full_max);
2242
2243 /* qemu_strtoui() tests */
2244 g_test_add_func("/cutils/qemu_strtoui/correct",
2245 test_qemu_strtoui_correct);
2246 g_test_add_func("/cutils/qemu_strtoui/null",
2247 test_qemu_strtoui_null);
2248 g_test_add_func("/cutils/qemu_strtoui/empty",
2249 test_qemu_strtoui_empty);
2250 g_test_add_func("/cutils/qemu_strtoui/whitespace",
2251 test_qemu_strtoui_whitespace);
2252 g_test_add_func("/cutils/qemu_strtoui/invalid",
2253 test_qemu_strtoui_invalid);
2254 g_test_add_func("/cutils/qemu_strtoui/trailing",
2255 test_qemu_strtoui_trailing);
2256 g_test_add_func("/cutils/qemu_strtoui/octal",
2257 test_qemu_strtoui_octal);
2258 g_test_add_func("/cutils/qemu_strtoui/decimal",
2259 test_qemu_strtoui_decimal);
2260 g_test_add_func("/cutils/qemu_strtoui/hex",
2261 test_qemu_strtoui_hex);
2262 g_test_add_func("/cutils/qemu_strtoui/max",
2263 test_qemu_strtoui_max);
2264 g_test_add_func("/cutils/qemu_strtoui/overflow",
2265 test_qemu_strtoui_overflow);
2266 g_test_add_func("/cutils/qemu_strtoui/underflow",
2267 test_qemu_strtoui_underflow);
2268 g_test_add_func("/cutils/qemu_strtoui/negative",
2269 test_qemu_strtoui_negative);
2270 g_test_add_func("/cutils/qemu_strtoui_full/correct",
2271 test_qemu_strtoui_full_correct);
2272 g_test_add_func("/cutils/qemu_strtoui_full/null",
2273 test_qemu_strtoui_full_null);
2274 g_test_add_func("/cutils/qemu_strtoui_full/empty",
2275 test_qemu_strtoui_full_empty);
2276 g_test_add_func("/cutils/qemu_strtoui_full/negative",
2277 test_qemu_strtoui_full_negative);
2278 g_test_add_func("/cutils/qemu_strtoui_full/trailing",
2279 test_qemu_strtoui_full_trailing);
2280 g_test_add_func("/cutils/qemu_strtoui_full/max",
2281 test_qemu_strtoui_full_max);
2282
764e0fa4 2283 /* qemu_strtol() tests */
b30d1886
MA
2284 g_test_add_func("/cutils/qemu_strtol/correct",
2285 test_qemu_strtol_correct);
2286 g_test_add_func("/cutils/qemu_strtol/null",
2287 test_qemu_strtol_null);
2288 g_test_add_func("/cutils/qemu_strtol/empty",
2289 test_qemu_strtol_empty);
764e0fa4
CT
2290 g_test_add_func("/cutils/qemu_strtol/whitespace",
2291 test_qemu_strtol_whitespace);
b30d1886
MA
2292 g_test_add_func("/cutils/qemu_strtol/invalid",
2293 test_qemu_strtol_invalid);
2294 g_test_add_func("/cutils/qemu_strtol/trailing",
2295 test_qemu_strtol_trailing);
2296 g_test_add_func("/cutils/qemu_strtol/octal",
2297 test_qemu_strtol_octal);
2298 g_test_add_func("/cutils/qemu_strtol/decimal",
2299 test_qemu_strtol_decimal);
2300 g_test_add_func("/cutils/qemu_strtol/hex",
2301 test_qemu_strtol_hex);
2302 g_test_add_func("/cutils/qemu_strtol/max",
2303 test_qemu_strtol_max);
2304 g_test_add_func("/cutils/qemu_strtol/overflow",
2305 test_qemu_strtol_overflow);
764e0fa4
CT
2306 g_test_add_func("/cutils/qemu_strtol/underflow",
2307 test_qemu_strtol_underflow);
b30d1886
MA
2308 g_test_add_func("/cutils/qemu_strtol/negative",
2309 test_qemu_strtol_negative);
764e0fa4
CT
2310 g_test_add_func("/cutils/qemu_strtol_full/correct",
2311 test_qemu_strtol_full_correct);
2312 g_test_add_func("/cutils/qemu_strtol_full/null",
2313 test_qemu_strtol_full_null);
2314 g_test_add_func("/cutils/qemu_strtol_full/empty",
2315 test_qemu_strtol_full_empty);
2316 g_test_add_func("/cutils/qemu_strtol_full/negative",
2317 test_qemu_strtol_full_negative);
2318 g_test_add_func("/cutils/qemu_strtol_full/trailing",
2319 test_qemu_strtol_full_trailing);
2320 g_test_add_func("/cutils/qemu_strtol_full/max",
2321 test_qemu_strtol_full_max);
2322
c817c015 2323 /* qemu_strtoul() tests */
b30d1886
MA
2324 g_test_add_func("/cutils/qemu_strtoul/correct",
2325 test_qemu_strtoul_correct);
2326 g_test_add_func("/cutils/qemu_strtoul/null",
2327 test_qemu_strtoul_null);
2328 g_test_add_func("/cutils/qemu_strtoul/empty",
2329 test_qemu_strtoul_empty);
c817c015
CT
2330 g_test_add_func("/cutils/qemu_strtoul/whitespace",
2331 test_qemu_strtoul_whitespace);
b30d1886
MA
2332 g_test_add_func("/cutils/qemu_strtoul/invalid",
2333 test_qemu_strtoul_invalid);
c817c015
CT
2334 g_test_add_func("/cutils/qemu_strtoul/trailing",
2335 test_qemu_strtoul_trailing);
b30d1886
MA
2336 g_test_add_func("/cutils/qemu_strtoul/octal",
2337 test_qemu_strtoul_octal);
2338 g_test_add_func("/cutils/qemu_strtoul/decimal",
2339 test_qemu_strtoul_decimal);
2340 g_test_add_func("/cutils/qemu_strtoul/hex",
2341 test_qemu_strtoul_hex);
2342 g_test_add_func("/cutils/qemu_strtoul/max",
2343 test_qemu_strtoul_max);
c817c015
CT
2344 g_test_add_func("/cutils/qemu_strtoul/overflow",
2345 test_qemu_strtoul_overflow);
2346 g_test_add_func("/cutils/qemu_strtoul/underflow",
2347 test_qemu_strtoul_underflow);
2348 g_test_add_func("/cutils/qemu_strtoul/negative",
2349 test_qemu_strtoul_negative);
2350 g_test_add_func("/cutils/qemu_strtoul_full/correct",
2351 test_qemu_strtoul_full_correct);
2352 g_test_add_func("/cutils/qemu_strtoul_full/null",
2353 test_qemu_strtoul_full_null);
2354 g_test_add_func("/cutils/qemu_strtoul_full/empty",
2355 test_qemu_strtoul_full_empty);
2356 g_test_add_func("/cutils/qemu_strtoul_full/negative",
2357 test_qemu_strtoul_full_negative);
2358 g_test_add_func("/cutils/qemu_strtoul_full/trailing",
2359 test_qemu_strtoul_full_trailing);
2360 g_test_add_func("/cutils/qemu_strtoul_full/max",
2361 test_qemu_strtoul_full_max);
2362
b30d1886
MA
2363 /* qemu_strtoi64() tests */
2364 g_test_add_func("/cutils/qemu_strtoi64/correct",
2365 test_qemu_strtoi64_correct);
2366 g_test_add_func("/cutils/qemu_strtoi64/null",
2367 test_qemu_strtoi64_null);
2368 g_test_add_func("/cutils/qemu_strtoi64/empty",
2369 test_qemu_strtoi64_empty);
2370 g_test_add_func("/cutils/qemu_strtoi64/whitespace",
2371 test_qemu_strtoi64_whitespace);
2372 g_test_add_func("/cutils/qemu_strtoi64/invalid"
2373 ,
2374 test_qemu_strtoi64_invalid);
2375 g_test_add_func("/cutils/qemu_strtoi64/trailing",
2376 test_qemu_strtoi64_trailing);
2377 g_test_add_func("/cutils/qemu_strtoi64/octal",
2378 test_qemu_strtoi64_octal);
2379 g_test_add_func("/cutils/qemu_strtoi64/decimal",
2380 test_qemu_strtoi64_decimal);
2381 g_test_add_func("/cutils/qemu_strtoi64/hex",
2382 test_qemu_strtoi64_hex);
2383 g_test_add_func("/cutils/qemu_strtoi64/max",
2384 test_qemu_strtoi64_max);
2385 g_test_add_func("/cutils/qemu_strtoi64/overflow",
2386 test_qemu_strtoi64_overflow);
2387 g_test_add_func("/cutils/qemu_strtoi64/underflow",
2388 test_qemu_strtoi64_underflow);
2389 g_test_add_func("/cutils/qemu_strtoi64/negative",
2390 test_qemu_strtoi64_negative);
2391 g_test_add_func("/cutils/qemu_strtoi64_full/correct",
2392 test_qemu_strtoi64_full_correct);
2393 g_test_add_func("/cutils/qemu_strtoi64_full/null",
2394 test_qemu_strtoi64_full_null);
2395 g_test_add_func("/cutils/qemu_strtoi64_full/empty",
2396 test_qemu_strtoi64_full_empty);
2397 g_test_add_func("/cutils/qemu_strtoi64_full/negative",
2398 test_qemu_strtoi64_full_negative);
2399 g_test_add_func("/cutils/qemu_strtoi64_full/trailing",
2400 test_qemu_strtoi64_full_trailing);
2401 g_test_add_func("/cutils/qemu_strtoi64_full/max",
2402 test_qemu_strtoi64_full_max);
2403
2404 /* qemu_strtou64() tests */
2405 g_test_add_func("/cutils/qemu_strtou64/correct",
2406 test_qemu_strtou64_correct);
2407 g_test_add_func("/cutils/qemu_strtou64/null",
2408 test_qemu_strtou64_null);
2409 g_test_add_func("/cutils/qemu_strtou64/empty",
2410 test_qemu_strtou64_empty);
2411 g_test_add_func("/cutils/qemu_strtou64/whitespace",
2412 test_qemu_strtou64_whitespace);
2413 g_test_add_func("/cutils/qemu_strtou64/invalid",
2414 test_qemu_strtou64_invalid);
2415 g_test_add_func("/cutils/qemu_strtou64/trailing",
2416 test_qemu_strtou64_trailing);
2417 g_test_add_func("/cutils/qemu_strtou64/octal",
2418 test_qemu_strtou64_octal);
2419 g_test_add_func("/cutils/qemu_strtou64/decimal",
2420 test_qemu_strtou64_decimal);
2421 g_test_add_func("/cutils/qemu_strtou64/hex",
2422 test_qemu_strtou64_hex);
2423 g_test_add_func("/cutils/qemu_strtou64/max",
2424 test_qemu_strtou64_max);
2425 g_test_add_func("/cutils/qemu_strtou64/overflow",
2426 test_qemu_strtou64_overflow);
2427 g_test_add_func("/cutils/qemu_strtou64/underflow",
2428 test_qemu_strtou64_underflow);
2429 g_test_add_func("/cutils/qemu_strtou64/negative",
2430 test_qemu_strtou64_negative);
2431 g_test_add_func("/cutils/qemu_strtou64_full/correct",
2432 test_qemu_strtou64_full_correct);
2433 g_test_add_func("/cutils/qemu_strtou64_full/null",
2434 test_qemu_strtou64_full_null);
2435 g_test_add_func("/cutils/qemu_strtou64_full/empty",
2436 test_qemu_strtou64_full_empty);
2437 g_test_add_func("/cutils/qemu_strtou64_full/negative",
2438 test_qemu_strtou64_full_negative);
2439 g_test_add_func("/cutils/qemu_strtou64_full/trailing",
2440 test_qemu_strtou64_full_trailing);
2441 g_test_add_func("/cutils/qemu_strtou64_full/max",
2442 test_qemu_strtou64_full_max);
3904e6bf 2443
fe854538
MAL
2444 g_test_add_func("/cutils/strtosz/simple",
2445 test_qemu_strtosz_simple);
2446 g_test_add_func("/cutils/strtosz/units",
2447 test_qemu_strtosz_units);
2448 g_test_add_func("/cutils/strtosz/float",
2449 test_qemu_strtosz_float);
18aec479
MA
2450 g_test_add_func("/cutils/strtosz/invalid",
2451 test_qemu_strtosz_invalid);
a6b4373f
MA
2452 g_test_add_func("/cutils/strtosz/trailing",
2453 test_qemu_strtosz_trailing);
fe854538
MAL
2454 g_test_add_func("/cutils/strtosz/erange",
2455 test_qemu_strtosz_erange);
d2734d26
MA
2456 g_test_add_func("/cutils/strtosz/metric",
2457 test_qemu_strtosz_metric);
fe854538 2458
e3f9fe2d
EH
2459 return g_test_run();
2460}