xref: /openbmc/qemu/tests/unit/test-cutils.c (revision bd1386cc)
1 /*
2  * cutils.c unit-tests
3  *
4  * Copyright Red Hat
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 
28 #include "qemu/osdep.h"
29 #include "qemu/cutils.h"
30 #include "qemu/units.h"
31 
32 static void test_parse_uint_null(void)
33 {
34     uint64_t i = 999;
35     const char *endptr = "somewhere";
36     int r;
37 
38     r = parse_uint(NULL, &endptr, 0, &i);
39 
40     g_assert_cmpint(r, ==, -EINVAL);
41     g_assert_cmpuint(i, ==, 0);
42     g_assert_null(endptr);
43 }
44 
45 static void test_parse_uint_empty(void)
46 {
47     uint64_t i = 999;
48     const char *endptr = "somewhere";
49     const char *str = "";
50     int r;
51 
52     r = parse_uint(str, &endptr, 0, &i);
53 
54     g_assert_cmpint(r, ==, -EINVAL);
55     g_assert_cmpuint(i, ==, 0);
56     g_assert_true(endptr == str);
57 }
58 
59 static void test_parse_uint_whitespace(void)
60 {
61     uint64_t i = 999;
62     const char *endptr = "somewhere";
63     const char *str = "   \t   ";
64     int r;
65 
66     r = parse_uint(str, &endptr, 0, &i);
67 
68     g_assert_cmpint(r, ==, -EINVAL);
69     g_assert_cmpuint(i, ==, 0);
70     g_assert_true(endptr == str);
71 }
72 
73 
74 static void test_parse_uint_invalid(void)
75 {
76     uint64_t i = 999;
77     const char *endptr = "somewhere";
78     const char *str = " \t xxx";
79     int r;
80 
81     r = parse_uint(str, &endptr, 0, &i);
82 
83     g_assert_cmpint(r, ==, -EINVAL);
84     g_assert_cmpuint(i, ==, 0);
85     g_assert_true(endptr == str);
86 }
87 
88 
89 static void test_parse_uint_trailing(void)
90 {
91     uint64_t i = 999;
92     const char *endptr = "somewhere";
93     const char *str = "123xxx";
94     int r;
95 
96     r = parse_uint(str, &endptr, 0, &i);
97 
98     g_assert_cmpint(r, ==, 0);
99     g_assert_cmpuint(i, ==, 123);
100     g_assert_true(endptr == str + 3);
101 }
102 
103 static void test_parse_uint_correct(void)
104 {
105     uint64_t i = 999;
106     const char *endptr = "somewhere";
107     const char *str = "123";
108     int r;
109 
110     r = parse_uint(str, &endptr, 0, &i);
111 
112     g_assert_cmpint(r, ==, 0);
113     g_assert_cmpuint(i, ==, 123);
114     g_assert_true(endptr == str + strlen(str));
115 }
116 
117 static void test_parse_uint_octal(void)
118 {
119     uint64_t i = 999;
120     const char *endptr = "somewhere";
121     const char *str = "0123";
122     int r;
123 
124     r = parse_uint(str, &endptr, 0, &i);
125 
126     g_assert_cmpint(r, ==, 0);
127     g_assert_cmpuint(i, ==, 0123);
128     g_assert_true(endptr == str + strlen(str));
129 }
130 
131 static void test_parse_uint_decimal(void)
132 {
133     uint64_t i = 999;
134     const char *endptr = "somewhere";
135     const char *str = "0123";
136     int r;
137 
138     r = parse_uint(str, &endptr, 10, &i);
139 
140     g_assert_cmpint(r, ==, 0);
141     g_assert_cmpuint(i, ==, 123);
142     g_assert_true(endptr == str + strlen(str));
143 }
144 
145 static void test_parse_uint_llong_max(void)
146 {
147     uint64_t i = 999;
148     const char *endptr = "somewhere";
149     char *str = g_strdup_printf("%llu", (unsigned long long)LLONG_MAX + 1);
150     int r;
151 
152     r = parse_uint(str, &endptr, 0, &i);
153 
154     g_assert_cmpint(r, ==, 0);
155     g_assert_cmpuint(i, ==, (unsigned long long)LLONG_MAX + 1);
156     g_assert_true(endptr == str + strlen(str));
157 
158     g_free(str);
159 }
160 
161 static void test_parse_uint_max(void)
162 {
163     uint64_t i = 999;
164     const char *endptr = "somewhere";
165     char *str = g_strdup_printf("%llu", ULLONG_MAX);
166     int r;
167 
168     r = parse_uint(str, &endptr, 0, &i);
169 
170     g_assert_cmpint(r, ==, 0);
171     g_assert_cmpuint(i, ==, ULLONG_MAX);
172     g_assert_true(endptr == str + strlen(str));
173 
174     g_free(str);
175 }
176 
177 static void test_parse_uint_overflow(void)
178 {
179     uint64_t i;
180     const char *endptr;
181     const char *str;
182     int r;
183 
184     i = 999;
185     endptr = "somewhere";
186     str = "99999999999999999999999999999999999999";
187     r = parse_uint(str, &endptr, 0, &i);
188     g_assert_cmpint(r, ==, -ERANGE);
189     g_assert_cmpuint(i, ==, ULLONG_MAX);
190     g_assert_true(endptr == str + strlen(str));
191 
192     i = 999;
193     endptr = "somewhere";
194     str = "0x10000000000000000"; /* 65 bits, 64-bit sign bit clear */
195     r = parse_uint(str, &endptr, 0, &i);
196     g_assert_cmpint(r, ==, -ERANGE);
197     g_assert_cmpuint(i, ==, ULLONG_MAX);
198     g_assert_true(endptr == str + strlen(str));
199 
200     i = 999;
201     endptr = "somewhere";
202     str = "0x18000000080000000"; /* 65 bits, 64-bit sign bit set */
203     r = parse_uint(str, &endptr, 0, &i);
204     g_assert_cmpint(r, ==, -ERANGE);
205     g_assert_cmpuint(i, ==, ULLONG_MAX);
206     g_assert_true(endptr == str + strlen(str));
207 }
208 
209 static void test_parse_uint_negative(void)
210 {
211     uint64_t i;
212     const char *endptr;
213     const char *str;
214     int r;
215 
216     i = 999;
217     endptr = "somewhere";
218     str = " \t -321";
219     r = parse_uint(str, &endptr, 0, &i);
220     g_assert_cmpint(r, ==, -ERANGE);
221     g_assert_cmpuint(i, ==, 0);
222     g_assert_true(endptr == str + strlen(str));
223 
224     i = 999;
225     endptr = "somewhere";
226     str = "-0xffffffff00000001";
227     r = parse_uint(str, &endptr, 0, &i);
228     g_assert_cmpint(r, ==, -ERANGE);
229     g_assert_cmpuint(i, ==, 0);
230     g_assert_true(endptr == str + strlen(str));
231 }
232 
233 static void test_parse_uint_negzero(void)
234 {
235     uint64_t i = 999;
236     const char *endptr = "somewhere";
237     const char *str = " -0";
238     int r;
239 
240     r = parse_uint(str, &endptr, 0, &i);
241 
242     g_assert_cmpint(r, ==, -ERANGE);
243     g_assert_cmpuint(i, ==, 0);
244     g_assert_true(endptr == str + strlen(str));
245 }
246 
247 static void test_parse_uint_full_trailing(void)
248 {
249     uint64_t i = 999;
250     const char *str = "123xxx";
251     int r;
252 
253     r = parse_uint_full(str, 0, &i);
254 
255     g_assert_cmpint(r, ==, -EINVAL);
256     g_assert_cmpuint(i, ==, 0);
257 }
258 
259 static void test_parse_uint_full_correct(void)
260 {
261     uint64_t i = 999;
262     const char *str = "123";
263     int r;
264 
265     r = parse_uint_full(str, 0, &i);
266 
267     g_assert_cmpint(r, ==, 0);
268     g_assert_cmpuint(i, ==, 123);
269 }
270 
271 static void test_parse_uint_full_erange_junk(void)
272 {
273     /* FIXME - inconsistent with qemu_strto* which favors EINVAL */
274     uint64_t i = 999;
275     const char *str = "-2junk";
276     int r;
277 
278     r = parse_uint_full(str, 0, &i);
279 
280     g_assert_cmpint(r, ==, -ERANGE /* FIXME -EINVAL */);
281     g_assert_cmpuint(i, ==, 0);
282 }
283 
284 static void test_qemu_strtoi_correct(void)
285 {
286     const char *str = "12345 foo";
287     char f = 'X';
288     const char *endptr = &f;
289     int res = 999;
290     int err;
291 
292     err = qemu_strtoi(str, &endptr, 0, &res);
293 
294     g_assert_cmpint(err, ==, 0);
295     g_assert_cmpint(res, ==, 12345);
296     g_assert_true(endptr == str + 5);
297 }
298 
299 static void test_qemu_strtoi_null(void)
300 {
301     char f = 'X';
302     const char *endptr = &f;
303     int res = 999;
304     int err;
305 
306     err = qemu_strtoi(NULL, &endptr, 0, &res);
307 
308     g_assert_cmpint(err, ==, -EINVAL);
309     g_assert_cmpint(res, ==, 999);
310     g_assert_null(endptr);
311 }
312 
313 static void test_qemu_strtoi_empty(void)
314 {
315     const char *str = "";
316     char f = 'X';
317     const char *endptr = &f;
318     int res = 999;
319     int err;
320 
321     err = qemu_strtoi(str, &endptr, 0, &res);
322 
323     g_assert_cmpint(err, ==, -EINVAL);
324     g_assert_cmpint(res, ==, 0);
325     g_assert_true(endptr == str);
326 }
327 
328 static void test_qemu_strtoi_whitespace(void)
329 {
330     const char *str = "  \t  ";
331     char f = 'X';
332     const char *endptr = &f;
333     int res = 999;
334     int err;
335 
336     err = qemu_strtoi(str, &endptr, 0, &res);
337 
338     g_assert_cmpint(err, ==, -EINVAL);
339     g_assert_cmpint(res, ==, 0);
340     g_assert_true(endptr == str);
341 }
342 
343 static void test_qemu_strtoi_invalid(void)
344 {
345     const char *str = "   xxxx  \t abc";
346     char f = 'X';
347     const char *endptr = &f;
348     int res = 999;
349     int err;
350 
351     err = qemu_strtoi(str, &endptr, 0, &res);
352 
353     g_assert_cmpint(err, ==, -EINVAL);
354     g_assert_cmpint(res, ==, 0);
355     g_assert_true(endptr == str);
356 }
357 
358 static void test_qemu_strtoi_trailing(void)
359 {
360     const char *str = "123xxx";
361     char f = 'X';
362     const char *endptr = &f;
363     int res = 999;
364     int err;
365 
366     err = qemu_strtoi(str, &endptr, 0, &res);
367 
368     g_assert_cmpint(err, ==, 0);
369     g_assert_cmpint(res, ==, 123);
370     g_assert_true(endptr == str + 3);
371 }
372 
373 static void test_qemu_strtoi_octal(void)
374 {
375     const char *str = "0123";
376     char f = 'X';
377     const char *endptr = &f;
378     int res = 999;
379     int err;
380 
381     err = qemu_strtoi(str, &endptr, 8, &res);
382 
383     g_assert_cmpint(err, ==, 0);
384     g_assert_cmpint(res, ==, 0123);
385     g_assert_true(endptr == str + strlen(str));
386 
387     res = 999;
388     endptr = &f;
389     err = qemu_strtoi(str, &endptr, 0, &res);
390 
391     g_assert_cmpint(err, ==, 0);
392     g_assert_cmpint(res, ==, 0123);
393     g_assert_true(endptr == str + strlen(str));
394 }
395 
396 static void test_qemu_strtoi_decimal(void)
397 {
398     const char *str = "0123";
399     char f = 'X';
400     const char *endptr = &f;
401     int res = 999;
402     int err;
403 
404     err = qemu_strtoi(str, &endptr, 10, &res);
405 
406     g_assert_cmpint(err, ==, 0);
407     g_assert_cmpint(res, ==, 123);
408     g_assert_true(endptr == str + strlen(str));
409 
410     str = "123";
411     res = 999;
412     endptr = &f;
413     err = qemu_strtoi(str, &endptr, 0, &res);
414 
415     g_assert_cmpint(err, ==, 0);
416     g_assert_cmpint(res, ==, 123);
417     g_assert_true(endptr == str + strlen(str));
418 }
419 
420 static void test_qemu_strtoi_hex(void)
421 {
422     const char *str = "0123";
423     char f = 'X';
424     const char *endptr = &f;
425     int res = 999;
426     int err;
427 
428     err = qemu_strtoi(str, &endptr, 16, &res);
429 
430     g_assert_cmpint(err, ==, 0);
431     g_assert_cmpint(res, ==, 0x123);
432     g_assert_true(endptr == str + strlen(str));
433 
434     str = "0x123";
435     res = 999;
436     endptr = &f;
437     err = qemu_strtoi(str, &endptr, 0, &res);
438 
439     g_assert_cmpint(err, ==, 0);
440     g_assert_cmpint(res, ==, 0x123);
441     g_assert_true(endptr == str + strlen(str));
442 
443     str = "0x";
444     res = 999;
445     endptr = &f;
446     err = qemu_strtoi(str, &endptr, 16, &res);
447 
448     g_assert_cmpint(err, ==, 0);
449     g_assert_cmpint(res, ==, 0);
450     g_assert_true(endptr == str + 1);
451 }
452 
453 static void test_qemu_strtoi_max(void)
454 {
455     char *str = g_strdup_printf("%d", INT_MAX);
456     char f = 'X';
457     const char *endptr = &f;
458     int res = 999;
459     int err;
460 
461     err = qemu_strtoi(str, &endptr, 0, &res);
462 
463     g_assert_cmpint(err, ==, 0);
464     g_assert_cmpint(res, ==, INT_MAX);
465     g_assert_true(endptr == str + strlen(str));
466     g_free(str);
467 }
468 
469 static void test_qemu_strtoi_overflow(void)
470 {
471     const char *str;
472     const char *endptr;
473     int res;
474     int err;
475 
476     str = "2147483648"; /* INT_MAX + 1ll */
477     endptr = "somewhere";
478     res = 999;
479     err = qemu_strtoi(str, &endptr, 0, &res);
480     g_assert_cmpint(err, ==, -ERANGE);
481     g_assert_cmpint(res, ==, INT_MAX);
482     g_assert_true(endptr == str + strlen(str));
483 
484     str = "0x7fffffffffffffff"; /* LLONG_MAX */
485     endptr = "somewhere";
486     res = 999;
487     err = qemu_strtoi(str, &endptr, 0, &res);
488     g_assert_cmpint(err, ==, -ERANGE);
489     g_assert_cmpint(res, ==, INT_MAX);
490     g_assert_true(endptr == str + strlen(str));
491 
492     str = "0x8000000000000000"; /* (uint64_t)LLONG_MIN */
493     endptr = "somewhere";
494     res = 999;
495     err = qemu_strtoi(str, &endptr, 0, &res);
496     g_assert_cmpint(err, ==, -ERANGE);
497     g_assert_cmpint(res, ==, INT_MAX);
498     g_assert_true(endptr == str + strlen(str));
499 
500     str = "0x10000000000000000"; /* 65 bits, 32-bit sign bit clear */
501     endptr = "somewhere";
502     res = 999;
503     err = qemu_strtoi(str, &endptr, 0, &res);
504     g_assert_cmpint(err, ==, -ERANGE);
505     g_assert_cmpint(res, ==, INT_MAX);
506     g_assert_true(endptr == str + strlen(str));
507 
508     str = "0x18000000080000000"; /* 65 bits, 32-bit sign bit set */
509     endptr = "somewhere";
510     res = 999;
511     err = qemu_strtoi(str, &endptr, 0, &res);
512     g_assert_cmpint(err, ==, -ERANGE);
513     g_assert_cmpint(res, ==, INT_MAX);
514     g_assert_true(endptr == str + strlen(str));
515 }
516 
517 static void test_qemu_strtoi_min(void)
518 {
519     char *str = g_strdup_printf("%d", INT_MIN);
520     char f = 'X';
521     const char *endptr = &f;
522     int res = 999;
523     int err;
524 
525     err = qemu_strtoi(str, &endptr, 0, &res);
526 
527     g_assert_cmpint(err, ==, 0);
528     g_assert_cmpint(res, ==, INT_MIN);
529     g_assert_true(endptr == str + strlen(str));
530     g_free(str);
531 }
532 
533 static void test_qemu_strtoi_underflow(void)
534 {
535     const char *str;
536     const char *endptr;
537     int res;
538     int err;
539 
540     str = "-2147483649"; /* INT_MIN - 1ll */
541     endptr = "somewhere";
542     res = 999;
543     err = qemu_strtoi(str, &endptr, 0, &res);
544     g_assert_cmpint(err, ==, -ERANGE);
545     g_assert_cmpint(res, ==, INT_MIN);
546     g_assert_true(endptr == str + strlen(str));
547 
548     str = "-0x7fffffffffffffff"; /* -LLONG_MAX */
549     endptr = "somewhere";
550     res = 999;
551     err = qemu_strtoi(str, &endptr, 0, &res);
552     g_assert_cmpint(err, ==, -ERANGE);
553     g_assert_cmpint(res, ==, INT_MIN);
554     g_assert_true(endptr == str + strlen(str));
555 
556     str = "-0x8000000000000000"; /* (uint64_t)LLONG_MIN */
557     endptr = "somewhere";
558     res = 999;
559     err = qemu_strtoi(str, &endptr, 0, &res);
560     g_assert_cmpint(err, ==, -ERANGE);
561     g_assert_cmpint(res, ==, INT_MIN);
562     g_assert_true(endptr == str + strlen(str));
563 
564     str = "-18446744073709551615"; /* -UINT64_MAX (not 1) */
565     endptr = "somewhere";
566     res = 999;
567     err = qemu_strtoi(str, &endptr, 0, &res);
568     g_assert_cmpint(err, ==, -ERANGE);
569     g_assert_cmpint(res, ==, INT_MIN);
570     g_assert_true(endptr == str + strlen(str));
571 
572     str = "-0x10000000000000000"; /* 65 bits, 32-bit sign bit clear */
573     endptr = "somewhere";
574     res = 999;
575     err = qemu_strtoi(str, &endptr, 0, &res);
576     g_assert_cmpint(err, ==, -ERANGE);
577     g_assert_cmpint(res, ==, INT_MIN);
578     g_assert_true(endptr == str + strlen(str));
579 
580     str = "-0x18000000080000000"; /* 65 bits, 32-bit sign bit set */
581     endptr = "somewhere";
582     res = 999;
583     err = qemu_strtoi(str, &endptr, 0, &res);
584     g_assert_cmpint(err, ==, -ERANGE);
585     g_assert_cmpint(res, ==, INT_MIN);
586     g_assert_true(endptr == str + strlen(str));
587 }
588 
589 static void test_qemu_strtoi_negative(void)
590 {
591     const char *str;
592     const char *endptr;
593     int res;
594     int err;
595 
596     str = "  \t -321";
597     endptr = "somewhere";
598     res = 999;
599     err = qemu_strtoi(str, &endptr, 0, &res);
600     g_assert_cmpint(err, ==, 0);
601     g_assert_cmpint(res, ==, -321);
602     g_assert_true(endptr == str + strlen(str));
603 
604     str = "-2147483648"; /* INT_MIN */
605     endptr = "somewhere";
606     res = 999;
607     err = qemu_strtoi(str, &endptr, 0, &res);
608     g_assert_cmpint(err, ==, 0);
609     g_assert_cmpint(res, ==, INT_MIN);
610     g_assert_true(endptr == str + strlen(str));
611 }
612 
613 static void test_qemu_strtoi_negzero(void)
614 {
615     const char *str = " -0";
616     char f = 'X';
617     const char *endptr = &f;
618     int res = 999;
619     int err;
620 
621     err = qemu_strtoi(str, &endptr, 0, &res);
622 
623     g_assert_cmpint(err, ==, 0);
624     g_assert_cmpint(res, ==, 0);
625     g_assert_true(endptr == str + strlen(str));
626 }
627 
628 static void test_qemu_strtoi_full_correct(void)
629 {
630     const char *str = "123";
631     int res = 999;
632     int err;
633 
634     err = qemu_strtoi(str, NULL, 0, &res);
635 
636     g_assert_cmpint(err, ==, 0);
637     g_assert_cmpint(res, ==, 123);
638 }
639 
640 static void test_qemu_strtoi_full_null(void)
641 {
642     char f = 'X';
643     const char *endptr = &f;
644     int res = 999;
645     int err;
646 
647     err = qemu_strtoi(NULL, &endptr, 0, &res);
648 
649     g_assert_cmpint(err, ==, -EINVAL);
650     g_assert_cmpint(res, ==, 999);
651     g_assert_null(endptr);
652 }
653 
654 static void test_qemu_strtoi_full_empty(void)
655 {
656     const char *str = "";
657     int res = 999;
658     int err;
659 
660     err = qemu_strtoi(str, NULL, 0, &res);
661 
662     g_assert_cmpint(err, ==, -EINVAL);
663     g_assert_cmpint(res, ==, 0);
664 }
665 
666 static void test_qemu_strtoi_full_negative(void)
667 {
668     const char *str = " \t -321";
669     int res = 999;
670     int err;
671 
672     err = qemu_strtoi(str, NULL, 0, &res);
673 
674     g_assert_cmpint(err, ==, 0);
675     g_assert_cmpint(res, ==, -321);
676 }
677 
678 static void test_qemu_strtoi_full_negzero(void)
679 {
680     const char *str = " -0";
681     int res = 999;
682     int err;
683 
684     err = qemu_strtoi(str, NULL, 0, &res);
685 
686     g_assert_cmpint(err, ==, 0);
687     g_assert_cmpint(res, ==, 0);
688 }
689 
690 static void test_qemu_strtoi_full_trailing(void)
691 {
692     const char *str = "123xxx";
693     int res = 999;
694     int err;
695 
696     err = qemu_strtoi(str, NULL, 0, &res);
697 
698     g_assert_cmpint(err, ==, -EINVAL);
699     g_assert_cmpint(res, ==, 123);
700 }
701 
702 static void test_qemu_strtoi_full_max(void)
703 {
704     char *str = g_strdup_printf("%d", INT_MAX);
705     int res = 999;
706     int err;
707 
708     err = qemu_strtoi(str, NULL, 0, &res);
709 
710     g_assert_cmpint(err, ==, 0);
711     g_assert_cmpint(res, ==, INT_MAX);
712     g_free(str);
713 }
714 
715 static void test_qemu_strtoi_full_erange_junk(void)
716 {
717     /* EINVAL has priority over ERANGE */
718     const char *str = "-9999999999junk";
719     int res = 999;
720     int err;
721 
722     err = qemu_strtoi(str, NULL, 0, &res);
723 
724     g_assert_cmpint(err, ==, -EINVAL);
725     g_assert_cmpint(res, ==, INT_MIN);
726 }
727 
728 static void test_qemu_strtoui_correct(void)
729 {
730     const char *str = "12345 foo";
731     char f = 'X';
732     const char *endptr = &f;
733     unsigned int res = 999;
734     int err;
735 
736     err = qemu_strtoui(str, &endptr, 0, &res);
737 
738     g_assert_cmpint(err, ==, 0);
739     g_assert_cmpuint(res, ==, 12345);
740     g_assert_true(endptr == str + 5);
741 }
742 
743 static void test_qemu_strtoui_null(void)
744 {
745     char f = 'X';
746     const char *endptr = &f;
747     unsigned int res = 999;
748     int err;
749 
750     err = qemu_strtoui(NULL, &endptr, 0, &res);
751 
752     g_assert_cmpint(err, ==, -EINVAL);
753     g_assert_cmpuint(res, ==, 999);
754     g_assert_null(endptr);
755 }
756 
757 static void test_qemu_strtoui_empty(void)
758 {
759     const char *str = "";
760     char f = 'X';
761     const char *endptr = &f;
762     unsigned int res = 999;
763     int err;
764 
765     err = qemu_strtoui(str, &endptr, 0, &res);
766 
767     g_assert_cmpint(err, ==, -EINVAL);
768     g_assert_cmpuint(res, ==, 0);
769     g_assert_true(endptr == str);
770 }
771 
772 static void test_qemu_strtoui_whitespace(void)
773 {
774     const char *str = "  \t  ";
775     char f = 'X';
776     const char *endptr = &f;
777     unsigned int res = 999;
778     int err;
779 
780     err = qemu_strtoui(str, &endptr, 0, &res);
781 
782     g_assert_cmpint(err, ==, -EINVAL);
783     g_assert_cmpuint(res, ==, 0);
784     g_assert_true(endptr == str);
785 }
786 
787 static void test_qemu_strtoui_invalid(void)
788 {
789     const char *str = "   xxxx  \t abc";
790     char f = 'X';
791     const char *endptr = &f;
792     unsigned int res = 999;
793     int err;
794 
795     err = qemu_strtoui(str, &endptr, 0, &res);
796 
797     g_assert_cmpint(err, ==, -EINVAL);
798     g_assert_cmpuint(res, ==, 0);
799     g_assert_true(endptr == str);
800 }
801 
802 static void test_qemu_strtoui_trailing(void)
803 {
804     const char *str = "123xxx";
805     char f = 'X';
806     const char *endptr = &f;
807     unsigned int res = 999;
808     int err;
809 
810     err = qemu_strtoui(str, &endptr, 0, &res);
811 
812     g_assert_cmpint(err, ==, 0);
813     g_assert_cmpuint(res, ==, 123);
814     g_assert_true(endptr == str + 3);
815 }
816 
817 static void test_qemu_strtoui_octal(void)
818 {
819     const char *str = "0123";
820     char f = 'X';
821     const char *endptr = &f;
822     unsigned int res = 999;
823     int err;
824 
825     err = qemu_strtoui(str, &endptr, 8, &res);
826 
827     g_assert_cmpint(err, ==, 0);
828     g_assert_cmpuint(res, ==, 0123);
829     g_assert_true(endptr == str + strlen(str));
830 
831     res = 999;
832     endptr = &f;
833     err = qemu_strtoui(str, &endptr, 0, &res);
834 
835     g_assert_cmpint(err, ==, 0);
836     g_assert_cmpuint(res, ==, 0123);
837     g_assert_true(endptr == str + strlen(str));
838 }
839 
840 static void test_qemu_strtoui_decimal(void)
841 {
842     const char *str = "0123";
843     char f = 'X';
844     const char *endptr = &f;
845     unsigned int res = 999;
846     int err;
847 
848     err = qemu_strtoui(str, &endptr, 10, &res);
849 
850     g_assert_cmpint(err, ==, 0);
851     g_assert_cmpuint(res, ==, 123);
852     g_assert_true(endptr == str + strlen(str));
853 
854     str = "123";
855     res = 999;
856     endptr = &f;
857     err = qemu_strtoui(str, &endptr, 0, &res);
858 
859     g_assert_cmpint(err, ==, 0);
860     g_assert_cmpuint(res, ==, 123);
861     g_assert_true(endptr == str + strlen(str));
862 }
863 
864 static void test_qemu_strtoui_hex(void)
865 {
866     const char *str = "0123";
867     char f = 'X';
868     const char *endptr = &f;
869     unsigned int res = 999;
870     int err;
871 
872     err = qemu_strtoui(str, &endptr, 16, &res);
873 
874     g_assert_cmpint(err, ==, 0);
875     g_assert_cmphex(res, ==, 0x123);
876     g_assert_true(endptr == str + strlen(str));
877 
878     str = "0x123";
879     res = 999;
880     endptr = &f;
881     err = qemu_strtoui(str, &endptr, 0, &res);
882 
883     g_assert_cmpint(err, ==, 0);
884     g_assert_cmphex(res, ==, 0x123);
885     g_assert_true(endptr == str + strlen(str));
886 
887     str = "0x";
888     res = 999;
889     endptr = &f;
890     err = qemu_strtoui(str, &endptr, 16, &res);
891 
892     g_assert_cmpint(err, ==, 0);
893     g_assert_cmphex(res, ==, 0);
894     g_assert_true(endptr == str + 1);
895 }
896 
897 static void test_qemu_strtoui_wrap(void)
898 {
899     /* wraparound is consistent with 32-bit strtoul */
900     const char *str = "-4294967295"; /* 1 mod 2^32 */
901     char f = 'X';
902     const char *endptr = &f;
903     unsigned int res = 999;
904     int err;
905 
906     err = qemu_strtoui(str, &endptr, 0, &res);
907 
908     g_assert_cmpint(err, ==, 0);
909     g_assert_cmphex(res, ==, 1);
910     g_assert_true(endptr == str + strlen(str));
911 }
912 
913 static void test_qemu_strtoui_max(void)
914 {
915     char *str = g_strdup_printf("%u", UINT_MAX);
916     char f = 'X';
917     const char *endptr = &f;
918     unsigned int res = 999;
919     int err;
920 
921     err = qemu_strtoui(str, &endptr, 0, &res);
922 
923     g_assert_cmpint(err, ==, 0);
924     g_assert_cmphex(res, ==, UINT_MAX);
925     g_assert_true(endptr == str + strlen(str));
926     g_free(str);
927 }
928 
929 static void test_qemu_strtoui_overflow(void)
930 {
931     const char *str;
932     const char *endptr;
933     unsigned int res;
934     int err;
935 
936     str = "4294967296"; /* UINT_MAX + 1ll */
937     endptr = "somewhere";
938     res = 999;
939     err = qemu_strtoui(str, &endptr, 0, &res);
940     g_assert_cmpint(err, ==, -ERANGE);
941     g_assert_cmpuint(res, ==, UINT_MAX);
942     g_assert_true(endptr == str + strlen(str));
943 
944     str = "0x7fffffffffffffff"; /* LLONG_MAX */
945     endptr = "somewhere";
946     res = 999;
947     err = qemu_strtoui(str, &endptr, 0, &res);
948     g_assert_cmpint(err, ==, -ERANGE);
949     g_assert_cmpuint(res, ==, UINT_MAX);
950     g_assert_true(endptr == str + strlen(str));
951 
952     str = "0x8000000000000000"; /* (uint64_t)LLONG_MIN */
953     endptr = "somewhere";
954     res = 999;
955     err = qemu_strtoui(str, &endptr, 0, &res);
956     g_assert_cmpint(err, ==, -ERANGE);
957     g_assert_cmpuint(res, ==, UINT_MAX);
958     g_assert_true(endptr == str + strlen(str));
959 
960     str = "0xffffffff00000001"; /* ULLONG_MAX - UINT_MAX + 1 (not 1) */
961     endptr = "somewhere";
962     res = 999;
963     err = qemu_strtoui(str, &endptr, 0, &res);
964     g_assert_cmpint(err, ==, -ERANGE);
965     g_assert_cmpuint(res, ==, UINT_MAX);
966     g_assert_true(endptr == str + strlen(str));
967 
968     str = "0xfffffffffffffffe"; /* ULLONG_MAX - 1 (not UINT_MAX - 1) */
969     endptr = "somewhere";
970     res = 999;
971     err = qemu_strtoui(str, &endptr, 0, &res);
972     g_assert_cmpint(err, ==, -ERANGE);
973     g_assert_cmpuint(res, ==, UINT_MAX);
974     g_assert_true(endptr == str + strlen(str));
975 
976     str = "0x10000000000000000"; /* 65 bits, 32-bit sign bit clear */
977     endptr = "somewhere";
978     res = 999;
979     err = qemu_strtoui(str, &endptr, 0, &res);
980     g_assert_cmpint(err, ==, -ERANGE);
981     g_assert_cmpuint(res, ==, UINT_MAX);
982     g_assert_true(endptr == str + strlen(str));
983 
984     str = "0x18000000080000000"; /* 65 bits, 32-bit sign bit set */
985     endptr = "somewhere";
986     res = 999;
987     err = qemu_strtoui(str, &endptr, 0, &res);
988     g_assert_cmpint(err, ==, -ERANGE);
989     g_assert_cmpuint(res, ==, UINT_MAX);
990     g_assert_true(endptr == str + strlen(str));
991 }
992 
993 static void test_qemu_strtoui_underflow(void)
994 {
995     const char *str;
996     const char *endptr;
997     unsigned int res;
998     int err;
999 
1000     str = "-4294967296"; /* -(long long)UINT_MAX - 1ll */
1001     endptr = "somewhere";
1002     res = 999;
1003     err = qemu_strtoui(str, &endptr, 0, &res);
1004     g_assert_cmpint(err, ==, -ERANGE);
1005     g_assert_cmpuint(res, ==, UINT_MAX);
1006     g_assert_true(endptr == str + strlen(str));
1007 
1008     str = "-18446744073709551615"; /* -UINT64_MAX (not -(-1)) */
1009     endptr = "somewhere";
1010     res = 999;
1011     err = qemu_strtoui(str, &endptr, 0, &res);
1012     g_assert_cmpint(err, ==, -ERANGE);
1013     g_assert_cmpuint(res, ==, UINT_MAX);
1014     g_assert_true(endptr == str + strlen(str));
1015 
1016     str = "-0xffffffff00000002";
1017     endptr = "somewhere";
1018     res = 999;
1019     err = qemu_strtoui(str, &endptr, 0, &res);
1020     g_assert_cmpint(err, ==, -ERANGE);
1021     g_assert_cmpuint(res, ==, UINT_MAX);
1022     g_assert_true(endptr == str + strlen(str));
1023 
1024     str = "-0x10000000000000000"; /* 65 bits, 32-bit sign bit clear */
1025     endptr = "somewhere";
1026     res = 999;
1027     err = qemu_strtoui(str, &endptr, 0, &res);
1028     g_assert_cmpint(err, ==, -ERANGE);
1029     g_assert_cmpuint(res, ==, UINT_MAX);
1030     g_assert_true(endptr == str + strlen(str));
1031 
1032     str = "-0x18000000080000000"; /* 65 bits, 32-bit sign bit set */
1033     endptr = "somewhere";
1034     res = 999;
1035     err = qemu_strtoui(str, &endptr, 0, &res);
1036     g_assert_cmpint(err, ==, -ERANGE);
1037     g_assert_cmpuint(res, ==, UINT_MAX);
1038     g_assert_true(endptr == str + strlen(str));
1039 }
1040 
1041 static void test_qemu_strtoui_negative(void)
1042 {
1043     const char *str = "  \t -321";
1044     char f = 'X';
1045     const char *endptr = &f;
1046     unsigned int res = 999;
1047     int err;
1048 
1049     err = qemu_strtoui(str, &endptr, 0, &res);
1050 
1051     g_assert_cmpint(err, ==, 0);
1052     g_assert_cmpuint(res, ==, (unsigned int)-321);
1053     g_assert_true(endptr == str + strlen(str));
1054 }
1055 
1056 static void test_qemu_strtoui_negzero(void)
1057 {
1058     const char *str = " -0";
1059     char f = 'X';
1060     const char *endptr = &f;
1061     unsigned int res = 999;
1062     int err;
1063 
1064     err = qemu_strtoui(str, &endptr, 0, &res);
1065 
1066     g_assert_cmpint(err, ==, 0);
1067     g_assert_cmpuint(res, ==, 0);
1068     g_assert_true(endptr == str + strlen(str));
1069 }
1070 
1071 static void test_qemu_strtoui_full_correct(void)
1072 {
1073     const char *str = "123";
1074     unsigned int res = 999;
1075     int err;
1076 
1077     err = qemu_strtoui(str, NULL, 0, &res);
1078 
1079     g_assert_cmpint(err, ==, 0);
1080     g_assert_cmpuint(res, ==, 123);
1081 }
1082 
1083 static void test_qemu_strtoui_full_null(void)
1084 {
1085     unsigned int res = 999;
1086     int err;
1087 
1088     err = qemu_strtoui(NULL, NULL, 0, &res);
1089 
1090     g_assert_cmpint(err, ==, -EINVAL);
1091     g_assert_cmpuint(res, ==, 999);
1092 }
1093 
1094 static void test_qemu_strtoui_full_empty(void)
1095 {
1096     const char *str = "";
1097     unsigned int res = 999;
1098     int err;
1099 
1100     err = qemu_strtoui(str, NULL, 0, &res);
1101 
1102     g_assert_cmpint(err, ==, -EINVAL);
1103     g_assert_cmpuint(res, ==, 0);
1104 }
1105 
1106 static void test_qemu_strtoui_full_negative(void)
1107 {
1108     const char *str = " \t -321";
1109     unsigned int res = 999;
1110     int err;
1111 
1112     err = qemu_strtoui(str, NULL, 0, &res);
1113     g_assert_cmpint(err, ==, 0);
1114     g_assert_cmpuint(res, ==, (unsigned int)-321);
1115 }
1116 
1117 static void test_qemu_strtoui_full_negzero(void)
1118 {
1119     const char *str = " -0";
1120     unsigned int res = 999;
1121     int err;
1122 
1123     err = qemu_strtoui(str, NULL, 0, &res);
1124     g_assert_cmpint(err, ==, 0);
1125     g_assert_cmpuint(res, ==, 0);
1126 }
1127 
1128 static void test_qemu_strtoui_full_trailing(void)
1129 {
1130     const char *str = "123xxx";
1131     unsigned int res = 999;
1132     int err;
1133 
1134     err = qemu_strtoui(str, NULL, 0, &res);
1135 
1136     g_assert_cmpint(err, ==, -EINVAL);
1137     g_assert_cmpuint(res, ==, 123);
1138 }
1139 
1140 static void test_qemu_strtoui_full_max(void)
1141 {
1142     char *str = g_strdup_printf("%u", UINT_MAX);
1143     unsigned int res = 999;
1144     int err;
1145 
1146     err = qemu_strtoui(str, NULL, 0, &res);
1147 
1148     g_assert_cmpint(err, ==, 0);
1149     g_assert_cmphex(res, ==, UINT_MAX);
1150     g_free(str);
1151 }
1152 
1153 static void test_qemu_strtoui_full_erange_junk(void)
1154 {
1155     /* EINVAL has priority over ERANGE */
1156     const char *str = "-9999999999junk";
1157     unsigned int res = 999;
1158     int err;
1159 
1160     err = qemu_strtoui(str, NULL, 0, &res);
1161 
1162     g_assert_cmpint(err, ==, -EINVAL);
1163     g_assert_cmpuint(res, ==, UINT_MAX);
1164 }
1165 
1166 static void test_qemu_strtol_correct(void)
1167 {
1168     const char *str = "12345 foo";
1169     char f = 'X';
1170     const char *endptr = &f;
1171     long res = 999;
1172     int err;
1173 
1174     err = qemu_strtol(str, &endptr, 0, &res);
1175 
1176     g_assert_cmpint(err, ==, 0);
1177     g_assert_cmpint(res, ==, 12345);
1178     g_assert_true(endptr == str + 5);
1179 }
1180 
1181 static void test_qemu_strtol_null(void)
1182 {
1183     char f = 'X';
1184     const char *endptr = &f;
1185     long res = 999;
1186     int err;
1187 
1188     err = qemu_strtol(NULL, &endptr, 0, &res);
1189 
1190     g_assert_cmpint(err, ==, -EINVAL);
1191     g_assert_cmpint(res, ==, 999);
1192     g_assert_null(endptr);
1193 }
1194 
1195 static void test_qemu_strtol_empty(void)
1196 {
1197     const char *str = "";
1198     char f = 'X';
1199     const char *endptr = &f;
1200     long res = 999;
1201     int err;
1202 
1203     err = qemu_strtol(str, &endptr, 0, &res);
1204 
1205     g_assert_cmpint(err, ==, -EINVAL);
1206     g_assert_cmpint(res, ==, 0);
1207     g_assert_true(endptr == str);
1208 }
1209 
1210 static void test_qemu_strtol_whitespace(void)
1211 {
1212     const char *str = "  \t  ";
1213     char f = 'X';
1214     const char *endptr = &f;
1215     long res = 999;
1216     int err;
1217 
1218     err = qemu_strtol(str, &endptr, 0, &res);
1219 
1220     g_assert_cmpint(err, ==, -EINVAL);
1221     g_assert_cmpint(res, ==, 0);
1222     g_assert_true(endptr == str);
1223 }
1224 
1225 static void test_qemu_strtol_invalid(void)
1226 {
1227     const char *str = "   xxxx  \t abc";
1228     char f = 'X';
1229     const char *endptr = &f;
1230     long res = 999;
1231     int err;
1232 
1233     err = qemu_strtol(str, &endptr, 0, &res);
1234 
1235     g_assert_cmpint(err, ==, -EINVAL);
1236     g_assert_cmpint(res, ==, 0);
1237     g_assert_true(endptr == str);
1238 }
1239 
1240 static void test_qemu_strtol_trailing(void)
1241 {
1242     const char *str = "123xxx";
1243     char f = 'X';
1244     const char *endptr = &f;
1245     long res = 999;
1246     int err;
1247 
1248     err = qemu_strtol(str, &endptr, 0, &res);
1249 
1250     g_assert_cmpint(err, ==, 0);
1251     g_assert_cmpint(res, ==, 123);
1252     g_assert_true(endptr == str + 3);
1253 }
1254 
1255 static void test_qemu_strtol_octal(void)
1256 {
1257     const char *str = "0123";
1258     char f = 'X';
1259     const char *endptr = &f;
1260     long res = 999;
1261     int err;
1262 
1263     err = qemu_strtol(str, &endptr, 8, &res);
1264 
1265     g_assert_cmpint(err, ==, 0);
1266     g_assert_cmpint(res, ==, 0123);
1267     g_assert_true(endptr == str + strlen(str));
1268 
1269     res = 999;
1270     endptr = &f;
1271     err = qemu_strtol(str, &endptr, 0, &res);
1272 
1273     g_assert_cmpint(err, ==, 0);
1274     g_assert_cmpint(res, ==, 0123);
1275     g_assert_true(endptr == str + strlen(str));
1276 }
1277 
1278 static void test_qemu_strtol_decimal(void)
1279 {
1280     const char *str = "0123";
1281     char f = 'X';
1282     const char *endptr = &f;
1283     long res = 999;
1284     int err;
1285 
1286     err = qemu_strtol(str, &endptr, 10, &res);
1287 
1288     g_assert_cmpint(err, ==, 0);
1289     g_assert_cmpint(res, ==, 123);
1290     g_assert_true(endptr == str + strlen(str));
1291 
1292     str = "123";
1293     res = 999;
1294     endptr = &f;
1295     err = qemu_strtol(str, &endptr, 0, &res);
1296 
1297     g_assert_cmpint(err, ==, 0);
1298     g_assert_cmpint(res, ==, 123);
1299     g_assert_true(endptr == str + strlen(str));
1300 }
1301 
1302 static void test_qemu_strtol_hex(void)
1303 {
1304     const char *str = "0123";
1305     char f = 'X';
1306     const char *endptr = &f;
1307     long res = 999;
1308     int err;
1309 
1310     err = qemu_strtol(str, &endptr, 16, &res);
1311 
1312     g_assert_cmpint(err, ==, 0);
1313     g_assert_cmpint(res, ==, 0x123);
1314     g_assert_true(endptr == str + strlen(str));
1315 
1316     str = "0x123";
1317     res = 999;
1318     endptr = &f;
1319     err = qemu_strtol(str, &endptr, 0, &res);
1320 
1321     g_assert_cmpint(err, ==, 0);
1322     g_assert_cmpint(res, ==, 0x123);
1323     g_assert_true(endptr == str + strlen(str));
1324 
1325     str = "0x";
1326     res = 999;
1327     endptr = &f;
1328     err = qemu_strtol(str, &endptr, 16, &res);
1329 
1330     g_assert_cmpint(err, ==, 0);
1331     g_assert_cmpint(res, ==, 0);
1332     g_assert_true(endptr == str + 1);
1333 }
1334 
1335 static void test_qemu_strtol_max(void)
1336 {
1337     char *str = g_strdup_printf("%ld", LONG_MAX);
1338     char f = 'X';
1339     const char *endptr = &f;
1340     long res = 999;
1341     int err;
1342 
1343     err = qemu_strtol(str, &endptr, 0, &res);
1344 
1345     g_assert_cmpint(err, ==, 0);
1346     g_assert_cmpint(res, ==, LONG_MAX);
1347     g_assert_true(endptr == str + strlen(str));
1348     g_free(str);
1349 }
1350 
1351 static void test_qemu_strtol_overflow(void)
1352 {
1353     const char *str;
1354     const char *endptr;
1355     long res;
1356     int err;
1357 
1358     /* 1 more than LONG_MAX */
1359     str = LONG_MAX == INT_MAX ? "2147483648" : "9223372036854775808";
1360     endptr = "somewhere";
1361     res = 999;
1362     err = qemu_strtol(str, &endptr, 0, &res);
1363     g_assert_cmpint(err, ==, -ERANGE);
1364     g_assert_cmpint(res, ==, LONG_MAX);
1365     g_assert_true(endptr == str + strlen(str));
1366 
1367     if (LONG_MAX == INT_MAX) {
1368         str = "0xffffffff00000001"; /* ULLONG_MAX - UINT_MAX + 1 (not 1) */
1369         endptr = "somewhere";
1370         res = 999;
1371         err = qemu_strtol(str, &endptr, 0, &res);
1372         g_assert_cmpint(err, ==, -ERANGE);
1373         g_assert_cmpint(res, ==, LONG_MAX);
1374         g_assert_true(endptr == str + strlen(str));
1375     }
1376 
1377     str = "0x10000000000000000"; /* 65 bits, either sign bit position clear */
1378     endptr = "somewhere";
1379     res = 999;
1380     err = qemu_strtol(str, &endptr, 0, &res);
1381     g_assert_cmpint(err, ==, -ERANGE);
1382     g_assert_cmpint(res, ==, LONG_MAX);
1383     g_assert_true(endptr == str + strlen(str));
1384 
1385     str = "0x18000000080000000"; /* 65 bits, either sign bit position set */
1386     endptr = "somewhere";
1387     res = 999;
1388     err = qemu_strtol(str, &endptr, 0, &res);
1389     g_assert_cmpint(err, ==, -ERANGE);
1390     g_assert_cmpint(res, ==, LONG_MAX);
1391     g_assert_true(endptr == str + strlen(str));
1392 }
1393 
1394 static void test_qemu_strtol_min(void)
1395 {
1396     char *str = g_strdup_printf("%ld", LONG_MIN);
1397     char f = 'X';
1398     const char *endptr = &f;
1399     long res = 999;
1400     int err;
1401 
1402     err = qemu_strtol(str, &endptr, 0, &res);
1403 
1404     g_assert_cmpint(err, ==, 0);
1405     g_assert_cmpint(res, ==, LONG_MIN);
1406     g_assert_true(endptr == str + strlen(str));
1407     g_free(str);
1408 }
1409 
1410 static void test_qemu_strtol_underflow(void)
1411 {
1412     const char *str;
1413     const char *endptr;
1414     long res;
1415     int err;
1416 
1417     /* 1 less than LONG_MIN */
1418     str = LONG_MIN == INT_MIN ? "-2147483649" : "-9223372036854775809";
1419     endptr = "somewhere";
1420     res = 999;
1421     err = qemu_strtol(str, &endptr, 0, &res);
1422     g_assert_cmpint(err, ==, -ERANGE);
1423     g_assert_cmpint(res, ==, LONG_MIN);
1424     g_assert_true(endptr == str + strlen(str));
1425 
1426     if (LONG_MAX == INT_MAX) {
1427         str = "-18446744073709551615"; /* -UINT64_MAX (not 1) */
1428         endptr = "somewhere";
1429         res = 999;
1430         err = qemu_strtol(str, &endptr, 0, &res);
1431         g_assert_cmpint(err, ==, -ERANGE);
1432         g_assert_cmpint(res, ==, LONG_MIN);
1433         g_assert_true(endptr == str + strlen(str));
1434     }
1435 
1436     str = "-0x10000000000000000"; /* 65 bits, either sign bit position clear */
1437     endptr = "somewhere";
1438     res = 999;
1439     err = qemu_strtol(str, &endptr, 0, &res);
1440     g_assert_cmpint(err, ==, -ERANGE);
1441     g_assert_cmpint(res, ==, LONG_MIN);
1442     g_assert_true(endptr == str + strlen(str));
1443 
1444     str = "-0x18000000080000000"; /* 65 bits, either sign bit position set */
1445     endptr = "somewhere";
1446     res = 999;
1447     err = qemu_strtol(str, &endptr, 0, &res);
1448     g_assert_cmpint(err, ==, -ERANGE);
1449     g_assert_cmpint(res, ==, LONG_MIN);
1450     g_assert_true(endptr == str + strlen(str));
1451 }
1452 
1453 static void test_qemu_strtol_negative(void)
1454 {
1455     const char *str = "  \t -321";
1456     char f = 'X';
1457     const char *endptr = &f;
1458     long res = 999;
1459     int err;
1460 
1461     err = qemu_strtol(str, &endptr, 0, &res);
1462 
1463     g_assert_cmpint(err, ==, 0);
1464     g_assert_cmpint(res, ==, -321);
1465     g_assert_true(endptr == str + strlen(str));
1466 }
1467 
1468 static void test_qemu_strtol_negzero(void)
1469 {
1470     const char *str = " -0";
1471     char f = 'X';
1472     const char *endptr = &f;
1473     long res = 999;
1474     int err;
1475 
1476     err = qemu_strtol(str, &endptr, 0, &res);
1477 
1478     g_assert_cmpint(err, ==, 0);
1479     g_assert_cmpint(res, ==, 0);
1480     g_assert_true(endptr == str + strlen(str));
1481 }
1482 
1483 static void test_qemu_strtol_full_correct(void)
1484 {
1485     const char *str = "123";
1486     long res = 999;
1487     int err;
1488 
1489     err = qemu_strtol(str, NULL, 0, &res);
1490 
1491     g_assert_cmpint(err, ==, 0);
1492     g_assert_cmpint(res, ==, 123);
1493 }
1494 
1495 static void test_qemu_strtol_full_null(void)
1496 {
1497     char f = 'X';
1498     const char *endptr = &f;
1499     long res = 999;
1500     int err;
1501 
1502     err = qemu_strtol(NULL, &endptr, 0, &res);
1503 
1504     g_assert_cmpint(err, ==, -EINVAL);
1505     g_assert_cmpint(res, ==, 999);
1506     g_assert_null(endptr);
1507 }
1508 
1509 static void test_qemu_strtol_full_empty(void)
1510 {
1511     const char *str = "";
1512     long res = 999L;
1513     int err;
1514 
1515     err = qemu_strtol(str, NULL, 0, &res);
1516 
1517     g_assert_cmpint(err, ==, -EINVAL);
1518     g_assert_cmpint(res, ==, 0);
1519 }
1520 
1521 static void test_qemu_strtol_full_negative(void)
1522 {
1523     const char *str = " \t -321";
1524     long res = 999;
1525     int err;
1526 
1527     err = qemu_strtol(str, NULL, 0, &res);
1528 
1529     g_assert_cmpint(err, ==, 0);
1530     g_assert_cmpint(res, ==, -321);
1531 }
1532 
1533 static void test_qemu_strtol_full_negzero(void)
1534 {
1535     const char *str = " -0";
1536     long res = 999;
1537     int err;
1538 
1539     err = qemu_strtol(str, NULL, 0, &res);
1540 
1541     g_assert_cmpint(err, ==, 0);
1542     g_assert_cmpint(res, ==, 0);
1543 }
1544 
1545 static void test_qemu_strtol_full_trailing(void)
1546 {
1547     const char *str = "123xxx";
1548     long res = 999;
1549     int err;
1550 
1551     err = qemu_strtol(str, NULL, 0, &res);
1552 
1553     g_assert_cmpint(err, ==, -EINVAL);
1554     g_assert_cmpint(res, ==, 123);
1555 }
1556 
1557 static void test_qemu_strtol_full_max(void)
1558 {
1559     char *str = g_strdup_printf("%ld", LONG_MAX);
1560     long res = 999;
1561     int err;
1562 
1563     err = qemu_strtol(str, NULL, 0, &res);
1564 
1565     g_assert_cmpint(err, ==, 0);
1566     g_assert_cmpint(res, ==, LONG_MAX);
1567     g_free(str);
1568 }
1569 
1570 static void test_qemu_strtol_full_erange_junk(void)
1571 {
1572     /* EINVAL has priority over ERANGE */
1573     const char *str = "-99999999999999999999junk";
1574     long res = 999;
1575     int err;
1576 
1577     err = qemu_strtol(str, NULL, 0, &res);
1578 
1579     g_assert_cmpint(err, ==, -EINVAL);
1580     g_assert_cmpint(res, ==, LONG_MIN);
1581 }
1582 
1583 static void test_qemu_strtoul_correct(void)
1584 {
1585     const char *str = "12345 foo";
1586     char f = 'X';
1587     const char *endptr = &f;
1588     unsigned long res = 999;
1589     int err;
1590 
1591     err = qemu_strtoul(str, &endptr, 0, &res);
1592 
1593     g_assert_cmpint(err, ==, 0);
1594     g_assert_cmpuint(res, ==, 12345);
1595     g_assert_true(endptr == str + 5);
1596 }
1597 
1598 static void test_qemu_strtoul_null(void)
1599 {
1600     char f = 'X';
1601     const char *endptr = &f;
1602     unsigned long res = 999;
1603     int err;
1604 
1605     err = qemu_strtoul(NULL, &endptr, 0, &res);
1606 
1607     g_assert_cmpint(err, ==, -EINVAL);
1608     g_assert_cmpuint(res, ==, 999);
1609     g_assert_null(endptr);
1610 }
1611 
1612 static void test_qemu_strtoul_empty(void)
1613 {
1614     const char *str = "";
1615     char f = 'X';
1616     const char *endptr = &f;
1617     unsigned long res = 999;
1618     int err;
1619 
1620     err = qemu_strtoul(str, &endptr, 0, &res);
1621 
1622     g_assert_cmpint(err, ==, -EINVAL);
1623     g_assert_cmpuint(res, ==, 0);
1624     g_assert_true(endptr == str);
1625 }
1626 
1627 static void test_qemu_strtoul_whitespace(void)
1628 {
1629     const char *str = "  \t  ";
1630     char f = 'X';
1631     const char *endptr = &f;
1632     unsigned long res = 999;
1633     int err;
1634 
1635     err = qemu_strtoul(str, &endptr, 0, &res);
1636 
1637     g_assert_cmpint(err, ==, -EINVAL);
1638     g_assert_cmpuint(res, ==, 0);
1639     g_assert_true(endptr == str);
1640 }
1641 
1642 static void test_qemu_strtoul_invalid(void)
1643 {
1644     const char *str = "   xxxx  \t abc";
1645     char f = 'X';
1646     const char *endptr = &f;
1647     unsigned long res = 999;
1648     int err;
1649 
1650     err = qemu_strtoul(str, &endptr, 0, &res);
1651 
1652     g_assert_cmpint(err, ==, -EINVAL);
1653     g_assert_cmpuint(res, ==, 0);
1654     g_assert_true(endptr == str);
1655 }
1656 
1657 static void test_qemu_strtoul_trailing(void)
1658 {
1659     const char *str = "123xxx";
1660     char f = 'X';
1661     const char *endptr = &f;
1662     unsigned long res = 999;
1663     int err;
1664 
1665     err = qemu_strtoul(str, &endptr, 0, &res);
1666 
1667     g_assert_cmpint(err, ==, 0);
1668     g_assert_cmpuint(res, ==, 123);
1669     g_assert_true(endptr == str + 3);
1670 }
1671 
1672 static void test_qemu_strtoul_octal(void)
1673 {
1674     const char *str = "0123";
1675     char f = 'X';
1676     const char *endptr = &f;
1677     unsigned long res = 999;
1678     int err;
1679 
1680     err = qemu_strtoul(str, &endptr, 8, &res);
1681 
1682     g_assert_cmpint(err, ==, 0);
1683     g_assert_cmpuint(res, ==, 0123);
1684     g_assert_true(endptr == str + strlen(str));
1685 
1686     res = 999;
1687     endptr = &f;
1688     err = qemu_strtoul(str, &endptr, 0, &res);
1689 
1690     g_assert_cmpint(err, ==, 0);
1691     g_assert_cmpuint(res, ==, 0123);
1692     g_assert_true(endptr == str + strlen(str));
1693 }
1694 
1695 static void test_qemu_strtoul_decimal(void)
1696 {
1697     const char *str = "0123";
1698     char f = 'X';
1699     const char *endptr = &f;
1700     unsigned long res = 999;
1701     int err;
1702 
1703     err = qemu_strtoul(str, &endptr, 10, &res);
1704 
1705     g_assert_cmpint(err, ==, 0);
1706     g_assert_cmpuint(res, ==, 123);
1707     g_assert_true(endptr == str + strlen(str));
1708 
1709     str = "123";
1710     res = 999;
1711     endptr = &f;
1712     err = qemu_strtoul(str, &endptr, 0, &res);
1713 
1714     g_assert_cmpint(err, ==, 0);
1715     g_assert_cmpuint(res, ==, 123);
1716     g_assert_true(endptr == str + strlen(str));
1717 }
1718 
1719 static void test_qemu_strtoul_hex(void)
1720 {
1721     const char *str = "0123";
1722     char f = 'X';
1723     const char *endptr = &f;
1724     unsigned long res = 999;
1725     int err;
1726 
1727     err = qemu_strtoul(str, &endptr, 16, &res);
1728 
1729     g_assert_cmpint(err, ==, 0);
1730     g_assert_cmphex(res, ==, 0x123);
1731     g_assert_true(endptr == str + strlen(str));
1732 
1733     str = "0x123";
1734     res = 999;
1735     endptr = &f;
1736     err = qemu_strtoul(str, &endptr, 0, &res);
1737 
1738     g_assert_cmpint(err, ==, 0);
1739     g_assert_cmphex(res, ==, 0x123);
1740     g_assert_true(endptr == str + strlen(str));
1741 
1742     str = "0x";
1743     res = 999;
1744     endptr = &f;
1745     err = qemu_strtoul(str, &endptr, 16, &res);
1746 
1747     g_assert_cmpint(err, ==, 0);
1748     g_assert_cmphex(res, ==, 0);
1749     g_assert_true(endptr == str + 1);
1750 }
1751 
1752 static void test_qemu_strtoul_wrap(void)
1753 {
1754     const char *str;
1755     char f = 'X';
1756     const char *endptr = &f;
1757     unsigned long res = 999;
1758     int err;
1759 
1760     /* 1 mod 2^(sizeof(long)*8) */
1761     str = LONG_MAX == INT_MAX ? "-4294967295" : "-18446744073709551615";
1762     err = qemu_strtoul(str, &endptr, 0, &res);
1763 
1764     g_assert_cmpint(err, ==, 0);
1765     g_assert_cmphex(res, ==, 1);
1766     g_assert_true(endptr == str + strlen(str));
1767 }
1768 
1769 static void test_qemu_strtoul_max(void)
1770 {
1771     char *str = g_strdup_printf("%lu", ULONG_MAX);
1772     char f = 'X';
1773     const char *endptr = &f;
1774     unsigned long res = 999;
1775     int err;
1776 
1777     err = qemu_strtoul(str, &endptr, 0, &res);
1778 
1779     g_assert_cmpint(err, ==, 0);
1780     g_assert_cmphex(res, ==, ULONG_MAX);
1781     g_assert_true(endptr == str + strlen(str));
1782     g_free(str);
1783 }
1784 
1785 static void test_qemu_strtoul_overflow(void)
1786 {
1787     const char *str;
1788     const char *endptr;
1789     unsigned long res;
1790     int err;
1791 
1792     /* 1 more than ULONG_MAX */
1793     str = ULONG_MAX == UINT_MAX ? "4294967296" : "18446744073709551616";
1794     endptr = "somewhere";
1795     res = 999;
1796     err = qemu_strtoul(str, &endptr, 0, &res);
1797     g_assert_cmpint(err, ==, -ERANGE);
1798     g_assert_cmpuint(res, ==, ULONG_MAX);
1799     g_assert_true(endptr == str + strlen(str));
1800 
1801     if (LONG_MAX == INT_MAX) {
1802         str = "0xffffffff00000001"; /* UINT64_MAX - UINT_MAX + 1 (not 1) */
1803         endptr = "somewhere";
1804         res = 999;
1805         err = qemu_strtoul(str, &endptr, 0, &res);
1806         g_assert_cmpint(err, ==, -ERANGE);
1807         g_assert_cmpuint(res, ==, ULONG_MAX);
1808         g_assert_true(endptr == str + strlen(str));
1809     }
1810 
1811     str = "0x10000000000000000"; /* 65 bits, either sign bit position clear */
1812     endptr = "somewhere";
1813     res = 999;
1814     err = qemu_strtoul(str, &endptr, 0, &res);
1815     g_assert_cmpint(err, ==, -ERANGE);
1816     g_assert_cmpuint(res, ==, ULONG_MAX);
1817     g_assert_true(endptr == str + strlen(str));
1818 
1819     str = "0x18000000080000000"; /* 65 bits, either sign bit position set */
1820     endptr = "somewhere";
1821     res = 999;
1822     err = qemu_strtoul(str, &endptr, 0, &res);
1823     g_assert_cmpint(err, ==, -ERANGE);
1824     g_assert_cmpuint(res, ==, ULONG_MAX);
1825     g_assert_true(endptr == str + strlen(str));
1826 }
1827 
1828 static void test_qemu_strtoul_underflow(void)
1829 {
1830     const char *str;
1831     const char *endptr;
1832     unsigned long res;
1833     int err;
1834 
1835     /* 1 less than -ULONG_MAX */
1836     str = ULONG_MAX == UINT_MAX ? "-4294967296" : "-18446744073709551616";
1837     endptr = "somewhere";
1838     res = 999;
1839     err = qemu_strtoul(str, &endptr, 0, &res);
1840     g_assert_cmpint(err, ==, -ERANGE);
1841     g_assert_cmpuint(res, ==, ULONG_MAX);
1842     g_assert_true(endptr == str + strlen(str));
1843 
1844     if (LONG_MAX == INT_MAX) {
1845         str = "-0xffffffff00000002";
1846         endptr = "somewhere";
1847         res = 999;
1848         err = qemu_strtoul(str, &endptr, 0, &res);
1849         g_assert_cmpint(err, ==, -ERANGE);
1850         g_assert_cmpuint(res, ==, ULONG_MAX);
1851         g_assert_true(endptr == str + strlen(str));
1852     }
1853 
1854     str = "-0x10000000000000000"; /* 65 bits, either sign bit position clear */
1855     endptr = "somewhere";
1856     res = 999;
1857     err = qemu_strtoul(str, &endptr, 0, &res);
1858     g_assert_cmpint(err, ==, -ERANGE);
1859     g_assert_cmpuint(res, ==, ULONG_MAX);
1860     g_assert_true(endptr == str + strlen(str));
1861 
1862     str = "-0x18000000080000000"; /* 65 bits, either sign bit position set */
1863     endptr = "somewhere";
1864     res = 999;
1865     err = qemu_strtoul(str, &endptr, 0, &res);
1866     g_assert_cmpint(err, ==, -ERANGE);
1867     g_assert_cmpuint(res, ==, ULONG_MAX);
1868     g_assert_true(endptr == str + strlen(str));
1869 }
1870 
1871 static void test_qemu_strtoul_negative(void)
1872 {
1873     const char *str = "  \t -321";
1874     char f = 'X';
1875     const char *endptr = &f;
1876     unsigned long res = 999;
1877     int err;
1878 
1879     err = qemu_strtoul(str, &endptr, 0, &res);
1880 
1881     g_assert_cmpint(err, ==, 0);
1882     g_assert_cmpuint(res, ==, -321ul);
1883     g_assert_true(endptr == str + strlen(str));
1884 }
1885 
1886 static void test_qemu_strtoul_negzero(void)
1887 {
1888     const char *str = " -0";
1889     char f = 'X';
1890     const char *endptr = &f;
1891     unsigned long res = 999;
1892     int err;
1893 
1894     err = qemu_strtoul(str, &endptr, 0, &res);
1895 
1896     g_assert_cmpint(err, ==, 0);
1897     g_assert_cmpuint(res, ==, 0);
1898     g_assert_true(endptr == str + strlen(str));
1899 }
1900 
1901 static void test_qemu_strtoul_full_correct(void)
1902 {
1903     const char *str = "123";
1904     unsigned long res = 999;
1905     int err;
1906 
1907     err = qemu_strtoul(str, NULL, 0, &res);
1908 
1909     g_assert_cmpint(err, ==, 0);
1910     g_assert_cmpuint(res, ==, 123);
1911 }
1912 
1913 static void test_qemu_strtoul_full_null(void)
1914 {
1915     unsigned long res = 999;
1916     int err;
1917 
1918     err = qemu_strtoul(NULL, NULL, 0, &res);
1919 
1920     g_assert_cmpint(err, ==, -EINVAL);
1921     g_assert_cmpuint(res, ==, 999);
1922 }
1923 
1924 static void test_qemu_strtoul_full_empty(void)
1925 {
1926     const char *str = "";
1927     unsigned long res = 999;
1928     int err;
1929 
1930     err = qemu_strtoul(str, NULL, 0, &res);
1931 
1932     g_assert_cmpint(err, ==, -EINVAL);
1933     g_assert_cmpuint(res, ==, 0);
1934 }
1935 
1936 static void test_qemu_strtoul_full_negative(void)
1937 {
1938     const char *str = " \t -321";
1939     unsigned long res = 999;
1940     int err;
1941 
1942     err = qemu_strtoul(str, NULL, 0, &res);
1943     g_assert_cmpint(err, ==, 0);
1944     g_assert_cmpuint(res, ==, -321ul);
1945 }
1946 
1947 static void test_qemu_strtoul_full_negzero(void)
1948 {
1949     const char *str = " -0";
1950     unsigned long res = 999;
1951     int err;
1952 
1953     err = qemu_strtoul(str, NULL, 0, &res);
1954     g_assert_cmpint(err, ==, 0);
1955     g_assert_cmpuint(res, ==, 0);
1956 }
1957 
1958 static void test_qemu_strtoul_full_trailing(void)
1959 {
1960     const char *str = "123xxx";
1961     unsigned long res = 999;
1962     int err;
1963 
1964     err = qemu_strtoul(str, NULL, 0, &res);
1965 
1966     g_assert_cmpint(err, ==, -EINVAL);
1967     g_assert_cmpuint(res, ==, 123);
1968 }
1969 
1970 static void test_qemu_strtoul_full_max(void)
1971 {
1972     char *str = g_strdup_printf("%lu", ULONG_MAX);
1973     unsigned long res = 999;
1974     int err;
1975 
1976     err = qemu_strtoul(str, NULL, 0, &res);
1977 
1978     g_assert_cmpint(err, ==, 0);
1979     g_assert_cmphex(res, ==, ULONG_MAX);
1980     g_free(str);
1981 }
1982 
1983 static void test_qemu_strtoul_full_erange_junk(void)
1984 {
1985     /* EINVAL has priority over ERANGE */
1986     const char *str = "-99999999999999999999junk";
1987     unsigned long res = 999;
1988     int err;
1989 
1990     err = qemu_strtoul(str, NULL, 0, &res);
1991 
1992     g_assert_cmpint(err, ==, -EINVAL);
1993     g_assert_cmpuint(res, ==, ULONG_MAX);
1994 }
1995 
1996 static void test_qemu_strtoi64_correct(void)
1997 {
1998     const char *str = "12345 foo";
1999     char f = 'X';
2000     const char *endptr = &f;
2001     int64_t res = 999;
2002     int err;
2003 
2004     err = qemu_strtoi64(str, &endptr, 0, &res);
2005 
2006     g_assert_cmpint(err, ==, 0);
2007     g_assert_cmpint(res, ==, 12345);
2008     g_assert_true(endptr == str + 5);
2009 }
2010 
2011 static void test_qemu_strtoi64_null(void)
2012 {
2013     char f = 'X';
2014     const char *endptr = &f;
2015     int64_t res = 999;
2016     int err;
2017 
2018     err = qemu_strtoi64(NULL, &endptr, 0, &res);
2019 
2020     g_assert_cmpint(err, ==, -EINVAL);
2021     g_assert_cmpint(res, ==, 999);
2022     g_assert_null(endptr);
2023 }
2024 
2025 static void test_qemu_strtoi64_empty(void)
2026 {
2027     const char *str = "";
2028     char f = 'X';
2029     const char *endptr = &f;
2030     int64_t res = 999;
2031     int err;
2032 
2033     err = qemu_strtoi64(str, &endptr, 0, &res);
2034 
2035     g_assert_cmpint(err, ==, -EINVAL);
2036     g_assert_cmpint(res, ==, 0);
2037     g_assert_true(endptr == str);
2038 }
2039 
2040 static void test_qemu_strtoi64_whitespace(void)
2041 {
2042     const char *str = "  \t  ";
2043     char f = 'X';
2044     const char *endptr = &f;
2045     int64_t res = 999;
2046     int err;
2047 
2048     err = qemu_strtoi64(str, &endptr, 0, &res);
2049 
2050     g_assert_cmpint(err, ==, -EINVAL);
2051     g_assert_cmpint(res, ==, 0);
2052     g_assert_true(endptr == str);
2053 }
2054 
2055 static void test_qemu_strtoi64_invalid(void)
2056 {
2057     const char *str = "   xxxx  \t abc";
2058     char f = 'X';
2059     const char *endptr = &f;
2060     int64_t res = 999;
2061     int err;
2062 
2063     err = qemu_strtoi64(str, &endptr, 0, &res);
2064 
2065     g_assert_cmpint(err, ==, -EINVAL);
2066     g_assert_cmpint(res, ==, 0);
2067     g_assert_true(endptr == str);
2068 }
2069 
2070 static void test_qemu_strtoi64_trailing(void)
2071 {
2072     const char *str = "123xxx";
2073     char f = 'X';
2074     const char *endptr = &f;
2075     int64_t res = 999;
2076     int err;
2077 
2078     err = qemu_strtoi64(str, &endptr, 0, &res);
2079 
2080     g_assert_cmpint(err, ==, 0);
2081     g_assert_cmpint(res, ==, 123);
2082     g_assert_true(endptr == str + 3);
2083 }
2084 
2085 static void test_qemu_strtoi64_octal(void)
2086 {
2087     const char *str = "0123";
2088     char f = 'X';
2089     const char *endptr = &f;
2090     int64_t res = 999;
2091     int err;
2092 
2093     err = qemu_strtoi64(str, &endptr, 8, &res);
2094 
2095     g_assert_cmpint(err, ==, 0);
2096     g_assert_cmpint(res, ==, 0123);
2097     g_assert_true(endptr == str + strlen(str));
2098 
2099     endptr = &f;
2100     res = 999;
2101     err = qemu_strtoi64(str, &endptr, 0, &res);
2102 
2103     g_assert_cmpint(err, ==, 0);
2104     g_assert_cmpint(res, ==, 0123);
2105     g_assert_true(endptr == str + strlen(str));
2106 }
2107 
2108 static void test_qemu_strtoi64_decimal(void)
2109 {
2110     const char *str = "0123";
2111     char f = 'X';
2112     const char *endptr = &f;
2113     int64_t res = 999;
2114     int err;
2115 
2116     err = qemu_strtoi64(str, &endptr, 10, &res);
2117 
2118     g_assert_cmpint(err, ==, 0);
2119     g_assert_cmpint(res, ==, 123);
2120     g_assert_true(endptr == str + strlen(str));
2121 
2122     str = "123";
2123     endptr = &f;
2124     res = 999;
2125     err = qemu_strtoi64(str, &endptr, 0, &res);
2126 
2127     g_assert_cmpint(err, ==, 0);
2128     g_assert_cmpint(res, ==, 123);
2129     g_assert_true(endptr == str + strlen(str));
2130 }
2131 
2132 static void test_qemu_strtoi64_hex(void)
2133 {
2134     const char *str = "0123";
2135     char f = 'X';
2136     const char *endptr = &f;
2137     int64_t res = 999;
2138     int err;
2139 
2140     err = qemu_strtoi64(str, &endptr, 16, &res);
2141 
2142     g_assert_cmpint(err, ==, 0);
2143     g_assert_cmpint(res, ==, 0x123);
2144     g_assert_true(endptr == str + strlen(str));
2145 
2146     str = "0x123";
2147     endptr = &f;
2148     res = 999;
2149     err = qemu_strtoi64(str, &endptr, 0, &res);
2150 
2151     g_assert_cmpint(err, ==, 0);
2152     g_assert_cmpint(res, ==, 0x123);
2153     g_assert_true(endptr == str + strlen(str));
2154 
2155     str = "0x";
2156     endptr = &f;
2157     res = 999;
2158     err = qemu_strtoi64(str, &endptr, 16, &res);
2159 
2160     g_assert_cmpint(err, ==, 0);
2161     g_assert_cmpint(res, ==, 0);
2162     g_assert_true(endptr == str + 1);
2163 }
2164 
2165 static void test_qemu_strtoi64_max(void)
2166 {
2167     char *str = g_strdup_printf("%lld", LLONG_MAX);
2168     char f = 'X';
2169     const char *endptr = &f;
2170     int64_t res = 999;
2171     int err;
2172 
2173     err = qemu_strtoi64(str, &endptr, 0, &res);
2174 
2175     g_assert_cmpint(err, ==, 0);
2176     g_assert_cmpint(res, ==, LLONG_MAX);
2177     g_assert_true(endptr == str + strlen(str));
2178     g_free(str);
2179 }
2180 
2181 static void test_qemu_strtoi64_overflow(void)
2182 {
2183     const char *str;
2184     const char *endptr;
2185     int64_t res;
2186     int err;
2187 
2188     str = "9223372036854775808"; /* 1 more than INT64_MAX */
2189     endptr = "somewhere";
2190     res = 999;
2191     err = qemu_strtoi64(str, &endptr, 0, &res);
2192     g_assert_cmpint(err, ==, -ERANGE);
2193     g_assert_cmpint(res, ==, INT64_MAX);
2194     g_assert_true(endptr == str + strlen(str));
2195 
2196     str = "0x10000000000000000"; /* 65 bits, 64-bit sign bit clear */
2197     endptr = "somewhere";
2198     res = 999;
2199     err = qemu_strtoi64(str, &endptr, 0, &res);
2200     g_assert_cmpint(err, ==, -ERANGE);
2201     g_assert_cmpint(res, ==, INT64_MAX);
2202     g_assert_true(endptr == str + strlen(str));
2203 
2204     str = "0x18000000080000000"; /* 65 bits, 64-bit sign bit set */
2205     endptr = "somewhere";
2206     res = 999;
2207     err = qemu_strtoi64(str, &endptr, 0, &res);
2208     g_assert_cmpint(err, ==, -ERANGE);
2209     g_assert_cmpint(res, ==, INT64_MAX);
2210     g_assert_true(endptr == str + strlen(str));
2211 }
2212 
2213 static void test_qemu_strtoi64_min(void)
2214 {
2215     char *str = g_strdup_printf("%lld", LLONG_MIN);
2216     char f = 'X';
2217     const char *endptr = &f;
2218     int64_t res = 999;
2219     int err;
2220 
2221     err = qemu_strtoi64(str, &endptr, 0, &res);
2222 
2223     g_assert_cmpint(err, ==, 0);
2224     g_assert_cmpint(res, ==, LLONG_MIN);
2225     g_assert_true(endptr == str + strlen(str));
2226     g_free(str);
2227 }
2228 
2229 static void test_qemu_strtoi64_underflow(void)
2230 {
2231     const char *str;
2232     const char *endptr;
2233     int64_t res;
2234     int err;
2235 
2236     str = "-9223372036854775809"; /* 1 less than INT64_MIN */
2237     endptr = "somewhere";
2238     res = 999;
2239     err = qemu_strtoi64(str, &endptr, 0, &res);
2240     g_assert_cmpint(err, ==, -ERANGE);
2241     g_assert_cmpint(res, ==, INT64_MIN);
2242     g_assert_true(endptr == str + strlen(str));
2243 
2244     str = "-0x10000000000000000"; /* 65 bits, 64-bit sign bit clear */
2245     endptr = "somewhere";
2246     res = 999;
2247     err = qemu_strtoi64(str, &endptr, 0, &res);
2248     g_assert_cmpint(err, ==, -ERANGE);
2249     g_assert_cmpint(res, ==, INT64_MIN);
2250     g_assert_true(endptr == str + strlen(str));
2251 
2252     str = "-0x18000000080000000"; /* 65 bits, 64-bit sign bit set */
2253     endptr = "somewhere";
2254     res = 999;
2255     err = qemu_strtoi64(str, &endptr, 0, &res);
2256     g_assert_cmpint(err, ==, -ERANGE);
2257     g_assert_cmpint(res, ==, INT64_MIN);
2258     g_assert_true(endptr == str + strlen(str));
2259 }
2260 
2261 static void test_qemu_strtoi64_negative(void)
2262 {
2263     const char *str = "  \t -321";
2264     char f = 'X';
2265     const char *endptr = &f;
2266     int64_t res = 999;
2267     int err;
2268 
2269     err = qemu_strtoi64(str, &endptr, 0, &res);
2270 
2271     g_assert_cmpint(err, ==, 0);
2272     g_assert_cmpint(res, ==, -321);
2273     g_assert_true(endptr == str + strlen(str));
2274 }
2275 
2276 static void test_qemu_strtoi64_negzero(void)
2277 {
2278     const char *str = " -0";
2279     char f = 'X';
2280     const char *endptr = &f;
2281     int64_t res = 999;
2282     int err;
2283 
2284     err = qemu_strtoi64(str, &endptr, 0, &res);
2285 
2286     g_assert_cmpint(err, ==, 0);
2287     g_assert_cmpint(res, ==, 0);
2288     g_assert_true(endptr == str + strlen(str));
2289 }
2290 
2291 static void test_qemu_strtoi64_full_correct(void)
2292 {
2293     const char *str = "123";
2294     int64_t res = 999;
2295     int err;
2296 
2297     err = qemu_strtoi64(str, NULL, 0, &res);
2298 
2299     g_assert_cmpint(err, ==, 0);
2300     g_assert_cmpint(res, ==, 123);
2301 }
2302 
2303 static void test_qemu_strtoi64_full_null(void)
2304 {
2305     int64_t res = 999;
2306     int err;
2307 
2308     err = qemu_strtoi64(NULL, NULL, 0, &res);
2309 
2310     g_assert_cmpint(err, ==, -EINVAL);
2311     g_assert_cmpint(res, ==, 999);
2312 }
2313 
2314 static void test_qemu_strtoi64_full_empty(void)
2315 {
2316     const char *str = "";
2317     int64_t res = 999;
2318     int err;
2319 
2320     err = qemu_strtoi64(str, NULL, 0, &res);
2321 
2322     g_assert_cmpint(err, ==, -EINVAL);
2323     g_assert_cmpint(res, ==, 0);
2324 }
2325 
2326 static void test_qemu_strtoi64_full_negative(void)
2327 {
2328     const char *str = " \t -321";
2329     int64_t res = 999;
2330     int err;
2331 
2332     err = qemu_strtoi64(str, NULL, 0, &res);
2333 
2334     g_assert_cmpint(err, ==, 0);
2335     g_assert_cmpint(res, ==, -321);
2336 }
2337 
2338 static void test_qemu_strtoi64_full_negzero(void)
2339 {
2340     const char *str = " -0";
2341     int64_t res = 999;
2342     int err;
2343 
2344     err = qemu_strtoi64(str, NULL, 0, &res);
2345 
2346     g_assert_cmpint(err, ==, 0);
2347     g_assert_cmpint(res, ==, 0);
2348 }
2349 
2350 static void test_qemu_strtoi64_full_trailing(void)
2351 {
2352     const char *str = "123xxx";
2353     int64_t res = 999;
2354     int err;
2355 
2356     err = qemu_strtoi64(str, NULL, 0, &res);
2357 
2358     g_assert_cmpint(err, ==, -EINVAL);
2359     g_assert_cmpint(res, ==, 123);
2360 }
2361 
2362 static void test_qemu_strtoi64_full_max(void)
2363 {
2364 
2365     char *str = g_strdup_printf("%lld", LLONG_MAX);
2366     int64_t res = 999;
2367     int err;
2368 
2369     err = qemu_strtoi64(str, NULL, 0, &res);
2370 
2371     g_assert_cmpint(err, ==, 0);
2372     g_assert_cmpint(res, ==, LLONG_MAX);
2373     g_free(str);
2374 }
2375 
2376 static void test_qemu_strtoi64_full_erange_junk(void)
2377 {
2378     /* EINVAL has priority over ERANGE */
2379     const char *str = "-99999999999999999999junk";
2380     int64_t res = 999;
2381     int err;
2382 
2383     err = qemu_strtoi64(str, NULL, 0, &res);
2384 
2385     g_assert_cmpint(err, ==, -EINVAL);
2386     g_assert_cmpint(res, ==, INT64_MIN);
2387 }
2388 
2389 static void test_qemu_strtou64_correct(void)
2390 {
2391     const char *str = "12345 foo";
2392     char f = 'X';
2393     const char *endptr = &f;
2394     uint64_t res = 999;
2395     int err;
2396 
2397     err = qemu_strtou64(str, &endptr, 0, &res);
2398 
2399     g_assert_cmpint(err, ==, 0);
2400     g_assert_cmpuint(res, ==, 12345);
2401     g_assert_true(endptr == str + 5);
2402 }
2403 
2404 static void test_qemu_strtou64_null(void)
2405 {
2406     char f = 'X';
2407     const char *endptr = &f;
2408     uint64_t res = 999;
2409     int err;
2410 
2411     err = qemu_strtou64(NULL, &endptr, 0, &res);
2412 
2413     g_assert_cmpint(err, ==, -EINVAL);
2414     g_assert_cmpuint(res, ==, 999);
2415     g_assert_null(endptr);
2416 }
2417 
2418 static void test_qemu_strtou64_empty(void)
2419 {
2420     const char *str = "";
2421     char f = 'X';
2422     const char *endptr = &f;
2423     uint64_t res = 999;
2424     int err;
2425 
2426     err = qemu_strtou64(str, &endptr, 0, &res);
2427 
2428     g_assert_cmpint(err, ==, -EINVAL);
2429     g_assert_cmpuint(res, ==, 0);
2430     g_assert_true(endptr == str);
2431 }
2432 
2433 static void test_qemu_strtou64_whitespace(void)
2434 {
2435     const char *str = "  \t  ";
2436     char f = 'X';
2437     const char *endptr = &f;
2438     uint64_t res = 999;
2439     int err;
2440 
2441     err = qemu_strtou64(str, &endptr, 0, &res);
2442 
2443     g_assert_cmpint(err, ==, -EINVAL);
2444     g_assert_cmpuint(res, ==, 0);
2445     g_assert_true(endptr == str);
2446 }
2447 
2448 static void test_qemu_strtou64_invalid(void)
2449 {
2450     const char *str = "   xxxx  \t abc";
2451     char f = 'X';
2452     const char *endptr = &f;
2453     uint64_t res = 999;
2454     int err;
2455 
2456     err = qemu_strtou64(str, &endptr, 0, &res);
2457 
2458     g_assert_cmpint(err, ==, -EINVAL);
2459     g_assert_cmpuint(res, ==, 0);
2460     g_assert_true(endptr == str);
2461 }
2462 
2463 static void test_qemu_strtou64_trailing(void)
2464 {
2465     const char *str = "123xxx";
2466     char f = 'X';
2467     const char *endptr = &f;
2468     uint64_t res = 999;
2469     int err;
2470 
2471     err = qemu_strtou64(str, &endptr, 0, &res);
2472 
2473     g_assert_cmpint(err, ==, 0);
2474     g_assert_cmpuint(res, ==, 123);
2475     g_assert_true(endptr == str + 3);
2476 }
2477 
2478 static void test_qemu_strtou64_octal(void)
2479 {
2480     const char *str = "0123";
2481     char f = 'X';
2482     const char *endptr = &f;
2483     uint64_t res = 999;
2484     int err;
2485 
2486     err = qemu_strtou64(str, &endptr, 8, &res);
2487 
2488     g_assert_cmpint(err, ==, 0);
2489     g_assert_cmpuint(res, ==, 0123);
2490     g_assert_true(endptr == str + strlen(str));
2491 
2492     endptr = &f;
2493     res = 999;
2494     err = qemu_strtou64(str, &endptr, 0, &res);
2495 
2496     g_assert_cmpint(err, ==, 0);
2497     g_assert_cmpuint(res, ==, 0123);
2498     g_assert_true(endptr == str + strlen(str));
2499 }
2500 
2501 static void test_qemu_strtou64_decimal(void)
2502 {
2503     const char *str = "0123";
2504     char f = 'X';
2505     const char *endptr = &f;
2506     uint64_t res = 999;
2507     int err;
2508 
2509     err = qemu_strtou64(str, &endptr, 10, &res);
2510 
2511     g_assert_cmpint(err, ==, 0);
2512     g_assert_cmpuint(res, ==, 123);
2513     g_assert_true(endptr == str + strlen(str));
2514 
2515     str = "123";
2516     endptr = &f;
2517     res = 999;
2518     err = qemu_strtou64(str, &endptr, 0, &res);
2519 
2520     g_assert_cmpint(err, ==, 0);
2521     g_assert_cmpuint(res, ==, 123);
2522     g_assert_true(endptr == str + strlen(str));
2523 }
2524 
2525 static void test_qemu_strtou64_hex(void)
2526 {
2527     const char *str = "0123";
2528     char f = 'X';
2529     const char *endptr = &f;
2530     uint64_t res = 999;
2531     int err;
2532 
2533     err = qemu_strtou64(str, &endptr, 16, &res);
2534 
2535     g_assert_cmpint(err, ==, 0);
2536     g_assert_cmphex(res, ==, 0x123);
2537     g_assert_true(endptr == str + strlen(str));
2538 
2539     str = "0x123";
2540     endptr = &f;
2541     res = 999;
2542     err = qemu_strtou64(str, &endptr, 0, &res);
2543 
2544     g_assert_cmpint(err, ==, 0);
2545     g_assert_cmphex(res, ==, 0x123);
2546     g_assert_true(endptr == str + strlen(str));
2547 
2548     str = "0x";
2549     endptr = &f;
2550     res = 999;
2551     err = qemu_strtou64(str, &endptr, 16, &res);
2552 
2553     g_assert_cmpint(err, ==, 0);
2554     g_assert_cmphex(res, ==, 0);
2555     g_assert_true(endptr == str + 1);
2556 }
2557 
2558 static void test_qemu_strtou64_wrap(void)
2559 {
2560     const char *str = "-18446744073709551615"; /* 1 mod 2^64 */
2561     char f = 'X';
2562     const char *endptr = &f;
2563     uint64_t res = 999;
2564     int err;
2565 
2566     err = qemu_strtou64(str, &endptr, 0, &res);
2567 
2568     g_assert_cmpint(err, ==, 0);
2569     g_assert_cmpuint(res, ==, 1);
2570     g_assert_true(endptr == str + strlen(str));
2571 }
2572 
2573 static void test_qemu_strtou64_max(void)
2574 {
2575     char *str = g_strdup_printf("%llu", ULLONG_MAX);
2576     char f = 'X';
2577     const char *endptr = &f;
2578     uint64_t res = 999;
2579     int err;
2580 
2581     err = qemu_strtou64(str, &endptr, 0, &res);
2582 
2583     g_assert_cmpint(err, ==, 0);
2584     g_assert_cmphex(res, ==, ULLONG_MAX);
2585     g_assert_true(endptr == str + strlen(str));
2586     g_free(str);
2587 }
2588 
2589 static void test_qemu_strtou64_overflow(void)
2590 {
2591     const char *str;
2592     const char *endptr;
2593     uint64_t res;
2594     int err;
2595 
2596     str = "18446744073709551616"; /* 1 more than UINT64_MAX */
2597     endptr = "somewhere";
2598     res = 999;
2599     err = qemu_strtou64(str, &endptr, 0, &res);
2600     g_assert_cmpint(err, ==, -ERANGE);
2601     g_assert_cmpuint(res, ==, UINT64_MAX);
2602     g_assert_true(endptr == str + strlen(str));
2603 
2604     str = "0x10000000000000000"; /* 65 bits, 64-bit sign bit clear */
2605     endptr = "somewhere";
2606     res = 999;
2607     err = qemu_strtou64(str, &endptr, 0, &res);
2608     g_assert_cmpint(err, ==, -ERANGE);
2609     g_assert_cmpuint(res, ==, UINT64_MAX);
2610     g_assert_true(endptr == str + strlen(str));
2611 
2612     str = "0x18000000080000000"; /* 65 bits, 64-bit sign bit set */
2613     endptr = "somewhere";
2614     res = 999;
2615     err = qemu_strtou64(str, &endptr, 0, &res);
2616     g_assert_cmpint(err, ==, -ERANGE);
2617     g_assert_cmpuint(res, ==, UINT64_MAX);
2618     g_assert_true(endptr == str + strlen(str));
2619 }
2620 
2621 static void test_qemu_strtou64_underflow(void)
2622 {
2623     const char *str;
2624     const char *endptr;
2625     uint64_t res;
2626     int err;
2627 
2628     str = "-99999999999999999999999999999999999999999999";
2629     endptr = "somewhere";
2630     res = 999;
2631     err = qemu_strtou64(str, &endptr, 0, &res);
2632     g_assert_cmpint(err, ==, -ERANGE);
2633     g_assert_cmpuint(res, ==, UINT64_MAX);
2634     g_assert_true(endptr == str + strlen(str));
2635 
2636     str = "-0x10000000000000000"; /* 65 bits, 64-bit sign bit clear */
2637     endptr = "somewhere";
2638     res = 999;
2639     err = qemu_strtou64(str, &endptr, 0, &res);
2640     g_assert_cmpint(err, ==, -ERANGE);
2641     g_assert_cmpuint(res, ==, UINT64_MAX);
2642     g_assert_true(endptr == str + strlen(str));
2643 
2644     str = "-0x18000000080000000"; /* 65 bits, 64-bit sign bit set */
2645     endptr = "somewhere";
2646     res = 999;
2647     err = qemu_strtou64(str, &endptr, 0, &res);
2648     g_assert_cmpint(err, ==, -ERANGE);
2649     g_assert_cmpuint(res, ==, UINT64_MAX);
2650     g_assert_true(endptr == str + strlen(str));
2651 }
2652 
2653 static void test_qemu_strtou64_negative(void)
2654 {
2655     const char *str = "  \t -321";
2656     char f = 'X';
2657     const char *endptr = &f;
2658     uint64_t res = 999;
2659     int err;
2660 
2661     err = qemu_strtou64(str, &endptr, 0, &res);
2662 
2663     g_assert_cmpint(err, ==, 0);
2664     g_assert_cmpuint(res, ==, -321ull);
2665     g_assert_true(endptr == str + strlen(str));
2666 }
2667 
2668 static void test_qemu_strtou64_negzero(void)
2669 {
2670     const char *str = " -0";
2671     char f = 'X';
2672     const char *endptr = &f;
2673     uint64_t res = 999;
2674     int err;
2675 
2676     err = qemu_strtou64(str, &endptr, 0, &res);
2677 
2678     g_assert_cmpint(err, ==, 0);
2679     g_assert_cmpuint(res, ==, 0);
2680     g_assert_true(endptr == str + strlen(str));
2681 }
2682 
2683 static void test_qemu_strtou64_full_correct(void)
2684 {
2685     const char *str = "18446744073709551614";
2686     uint64_t res = 999;
2687     int err;
2688 
2689     err = qemu_strtou64(str, NULL, 0, &res);
2690 
2691     g_assert_cmpint(err, ==, 0);
2692     g_assert_cmpuint(res, ==, 18446744073709551614ull);
2693 }
2694 
2695 static void test_qemu_strtou64_full_null(void)
2696 {
2697     uint64_t res = 999;
2698     int err;
2699 
2700     err = qemu_strtou64(NULL, NULL, 0, &res);
2701 
2702     g_assert_cmpint(err, ==, -EINVAL);
2703     g_assert_cmpuint(res, ==, 999);
2704 }
2705 
2706 static void test_qemu_strtou64_full_empty(void)
2707 {
2708     const char *str = "";
2709     uint64_t res = 999;
2710     int err;
2711 
2712     err = qemu_strtou64(str, NULL, 0, &res);
2713 
2714     g_assert_cmpint(err, ==, -EINVAL);
2715     g_assert_cmpuint(res, ==, 0);
2716 }
2717 
2718 static void test_qemu_strtou64_full_negative(void)
2719 {
2720     const char *str = " \t -321";
2721     uint64_t res = 999;
2722     int err;
2723 
2724     err = qemu_strtou64(str, NULL, 0, &res);
2725 
2726     g_assert_cmpint(err, ==, 0);
2727     g_assert_cmpuint(res, ==, -321ull);
2728 }
2729 
2730 static void test_qemu_strtou64_full_negzero(void)
2731 {
2732     const char *str = " -0";
2733     uint64_t res = 999;
2734     int err;
2735 
2736     err = qemu_strtou64(str, NULL, 0, &res);
2737 
2738     g_assert_cmpint(err, ==, 0);
2739     g_assert_cmpuint(res, ==, 0);
2740 }
2741 
2742 static void test_qemu_strtou64_full_trailing(void)
2743 {
2744     const char *str = "18446744073709551614xxxxxx";
2745     uint64_t res = 999;
2746     int err;
2747 
2748     err = qemu_strtou64(str, NULL, 0, &res);
2749 
2750     g_assert_cmpint(err, ==, -EINVAL);
2751     g_assert_cmpuint(res, ==, 18446744073709551614ULL);
2752 }
2753 
2754 static void test_qemu_strtou64_full_max(void)
2755 {
2756     char *str = g_strdup_printf("%lld", ULLONG_MAX);
2757     uint64_t res = 999;
2758     int err;
2759 
2760     err = qemu_strtou64(str, NULL, 0, &res);
2761 
2762     g_assert_cmpint(err, ==, 0);
2763     g_assert_cmphex(res, ==, ULLONG_MAX);
2764     g_free(str);
2765 }
2766 
2767 static void test_qemu_strtou64_full_erange_junk(void)
2768 {
2769     /* EINVAL has priority over ERANGE */
2770     const char *str = "-99999999999999999999junk";
2771     uint64_t res = 999;
2772     int err;
2773 
2774     err = qemu_strtou64(str, NULL, 0, &res);
2775 
2776     g_assert_cmpint(err, ==, -EINVAL);
2777     g_assert_cmpuint(res, ==, UINT64_MAX);
2778 }
2779 
2780 static void test_qemu_strtosz_simple(void)
2781 {
2782     const char *str;
2783     const char *endptr;
2784     int err;
2785     uint64_t res;
2786 
2787     str = "0";
2788     endptr = str;
2789     res = 0xbaadf00d;
2790     err = qemu_strtosz(str, &endptr, &res);
2791     g_assert_cmpint(err, ==, 0);
2792     g_assert_cmpuint(res, ==, 0);
2793     g_assert_true(endptr == str + 1);
2794 
2795     /* Leading 0 gives decimal results, not octal */
2796     str = "08";
2797     endptr = str;
2798     res = 0xbaadf00d;
2799     err = qemu_strtosz(str, &endptr, &res);
2800     g_assert_cmpint(err, ==, 0);
2801     g_assert_cmpuint(res, ==, 8);
2802     g_assert_true(endptr == str + 2);
2803 
2804     /* Leading space is ignored */
2805     str = " 12345";
2806     endptr = str;
2807     res = 0xbaadf00d;
2808     err = qemu_strtosz(str, &endptr, &res);
2809     g_assert_cmpint(err, ==, 0);
2810     g_assert_cmpuint(res, ==, 12345);
2811     g_assert_true(endptr == str + 6);
2812 
2813     res = 0xbaadf00d;
2814     err = qemu_strtosz(str, NULL, &res);
2815     g_assert_cmpint(err, ==, 0);
2816     g_assert_cmpuint(res, ==, 12345);
2817 
2818     str = "9007199254740991"; /* 2^53-1 */
2819     endptr = str;
2820     res = 0xbaadf00d;
2821     err = qemu_strtosz(str, &endptr, &res);
2822     g_assert_cmpint(err, ==, 0);
2823     g_assert_cmphex(res, ==, 0x1fffffffffffffULL);
2824     g_assert_true(endptr == str + 16);
2825 
2826     str = "9007199254740992"; /* 2^53 */
2827     endptr = str;
2828     res = 0xbaadf00d;
2829     err = qemu_strtosz(str, &endptr, &res);
2830     g_assert_cmpint(err, ==, 0);
2831     g_assert_cmphex(res, ==, 0x20000000000000ULL);
2832     g_assert_true(endptr == str + 16);
2833 
2834     str = "9007199254740993"; /* 2^53+1 */
2835     endptr = str;
2836     res = 0xbaadf00d;
2837     err = qemu_strtosz(str, &endptr, &res);
2838     g_assert_cmpint(err, ==, 0);
2839     g_assert_cmphex(res, ==, 0x20000000000001ULL);
2840     g_assert_true(endptr == str + 16);
2841 
2842     str = "18446744073709549568"; /* 0xfffffffffffff800 (53 msbs set) */
2843     endptr = str;
2844     res = 0xbaadf00d;
2845     err = qemu_strtosz(str, &endptr, &res);
2846     g_assert_cmpint(err, ==, 0);
2847     g_assert_cmphex(res, ==, 0xfffffffffffff800ULL);
2848     g_assert_true(endptr == str + 20);
2849 
2850     str = "18446744073709550591"; /* 0xfffffffffffffbff */
2851     endptr = str;
2852     res = 0xbaadf00d;
2853     err = qemu_strtosz(str, &endptr, &res);
2854     g_assert_cmpint(err, ==, 0);
2855     g_assert_cmphex(res, ==, 0xfffffffffffffbffULL);
2856     g_assert_true(endptr == str + 20);
2857 
2858     str = "18446744073709551615"; /* 0xffffffffffffffff */
2859     endptr = str;
2860     res = 0xbaadf00d;
2861     err = qemu_strtosz(str, &endptr, &res);
2862     g_assert_cmpint(err, ==, 0);
2863     g_assert_cmphex(res, ==, 0xffffffffffffffffULL);
2864     g_assert_true(endptr == str + 20);
2865 }
2866 
2867 static void test_qemu_strtosz_hex(void)
2868 {
2869     const char *str;
2870     const char *endptr;
2871     int err;
2872     uint64_t res;
2873 
2874     str = "0x0";
2875     endptr = str;
2876     res = 0xbaadf00d;
2877     err = qemu_strtosz(str, &endptr, &res);
2878     g_assert_cmpint(err, ==, 0);
2879     g_assert_cmpuint(res, ==, 0);
2880     g_assert_true(endptr == str + 3);
2881 
2882     str = "0xab";
2883     endptr = str;
2884     res = 0xbaadf00d;
2885     err = qemu_strtosz(str, &endptr, &res);
2886     g_assert_cmpint(err, ==, 0);
2887     g_assert_cmpuint(res, ==, 171);
2888     g_assert_true(endptr == str + 4);
2889 
2890     str = "0xae";
2891     endptr = str;
2892     res = 0xbaadf00d;
2893     err = qemu_strtosz(str, &endptr, &res);
2894     g_assert_cmpint(err, ==, 0);
2895     g_assert_cmpuint(res, ==, 174);
2896     g_assert_true(endptr == str + 4);
2897 }
2898 
2899 static void test_qemu_strtosz_units(void)
2900 {
2901     const char *none = "1";
2902     const char *b = "1B";
2903     const char *k = "1K";
2904     const char *m = "1M";
2905     const char *g = "1G";
2906     const char *t = "1T";
2907     const char *p = "1P";
2908     const char *e = "1E";
2909     int err;
2910     const char *endptr;
2911     uint64_t res;
2912 
2913     /* default is M */
2914     endptr = NULL;
2915     res = 0xbaadf00d;
2916     err = qemu_strtosz_MiB(none, &endptr, &res);
2917     g_assert_cmpint(err, ==, 0);
2918     g_assert_cmpuint(res, ==, MiB);
2919     g_assert_true(endptr == none + 1);
2920 
2921     endptr = NULL;
2922     res = 0xbaadf00d;
2923     err = qemu_strtosz(b, &endptr, &res);
2924     g_assert_cmpint(err, ==, 0);
2925     g_assert_cmpuint(res, ==, 1);
2926     g_assert_true(endptr == b + 2);
2927 
2928     endptr = NULL;
2929     res = 0xbaadf00d;
2930     err = qemu_strtosz(k, &endptr, &res);
2931     g_assert_cmpint(err, ==, 0);
2932     g_assert_cmpuint(res, ==, KiB);
2933     g_assert_true(endptr == k + 2);
2934 
2935     endptr = NULL;
2936     res = 0xbaadf00d;
2937     err = qemu_strtosz(m, &endptr, &res);
2938     g_assert_cmpint(err, ==, 0);
2939     g_assert_cmpuint(res, ==, MiB);
2940     g_assert_true(endptr == m + 2);
2941 
2942     endptr = NULL;
2943     res = 0xbaadf00d;
2944     err = qemu_strtosz(g, &endptr, &res);
2945     g_assert_cmpint(err, ==, 0);
2946     g_assert_cmpuint(res, ==, GiB);
2947     g_assert_true(endptr == g + 2);
2948 
2949     endptr = NULL;
2950     res = 0xbaadf00d;
2951     err = qemu_strtosz(t, &endptr, &res);
2952     g_assert_cmpint(err, ==, 0);
2953     g_assert_cmpuint(res, ==, TiB);
2954     g_assert_true(endptr == t + 2);
2955 
2956     endptr = NULL;
2957     res = 0xbaadf00d;
2958     err = qemu_strtosz(p, &endptr, &res);
2959     g_assert_cmpint(err, ==, 0);
2960     g_assert_cmpuint(res, ==, PiB);
2961     g_assert_true(endptr == p + 2);
2962 
2963     endptr = NULL;
2964     res = 0xbaadf00d;
2965     err = qemu_strtosz(e, &endptr, &res);
2966     g_assert_cmpint(err, ==, 0);
2967     g_assert_cmpuint(res, ==, EiB);
2968     g_assert_true(endptr == e + 2);
2969 }
2970 
2971 static void test_qemu_strtosz_float(void)
2972 {
2973     const char *str;
2974     int err;
2975     const char *endptr;
2976     uint64_t res;
2977 
2978     str = "0.5E";
2979     endptr = str;
2980     res = 0xbaadf00d;
2981     err = qemu_strtosz(str, &endptr, &res);
2982     g_assert_cmpint(err, ==, 0);
2983     g_assert_cmpuint(res, ==, EiB / 2);
2984     g_assert_true(endptr == str + 4);
2985 
2986     /* For convenience, a fraction of 0 is tolerated even on bytes */
2987     str = "1.0B";
2988     endptr = str;
2989     res = 0xbaadf00d;
2990     err = qemu_strtosz(str, &endptr, &res);
2991     g_assert_cmpint(err, ==, 0);
2992     g_assert_cmpuint(res, ==, 1);
2993     g_assert_true(endptr == str + 4);
2994 
2995     /* An empty fraction is tolerated */
2996     str = "1.k";
2997     endptr = str;
2998     res = 0xbaadf00d;
2999     err = qemu_strtosz(str, &endptr, &res);
3000     g_assert_cmpint(err, ==, 0);
3001     g_assert_cmpuint(res, ==, 1024);
3002     g_assert_true(endptr == str + 3);
3003 
3004     /* For convenience, we permit values that are not byte-exact */
3005     str = "12.345M";
3006     endptr = str;
3007     res = 0xbaadf00d;
3008     err = qemu_strtosz(str, &endptr, &res);
3009     g_assert_cmpint(err, ==, 0);
3010     g_assert_cmpuint(res, ==, (uint64_t) (12.345 * MiB + 0.5));
3011     g_assert_true(endptr == str + 7);
3012 }
3013 
3014 static void test_qemu_strtosz_invalid(void)
3015 {
3016     const char *str;
3017     const char *endptr;
3018     int err;
3019     uint64_t res = 0xbaadf00d;
3020 
3021     str = "";
3022     endptr = NULL;
3023     err = qemu_strtosz(str, &endptr, &res);
3024     g_assert_cmpint(err, ==, -EINVAL);
3025     g_assert_cmphex(res, ==, 0xbaadf00d);
3026     g_assert_true(endptr == str);
3027 
3028     str = " \t ";
3029     endptr = NULL;
3030     err = qemu_strtosz(str, &endptr, &res);
3031     g_assert_cmpint(err, ==, -EINVAL);
3032     g_assert_cmphex(res, ==, 0xbaadf00d);
3033     g_assert_true(endptr == str);
3034 
3035     str = "crap";
3036     endptr = NULL;
3037     err = qemu_strtosz(str, &endptr, &res);
3038     g_assert_cmpint(err, ==, -EINVAL);
3039     g_assert_cmphex(res, ==, 0xbaadf00d);
3040     g_assert_true(endptr == str);
3041 
3042     str = "inf";
3043     endptr = NULL;
3044     err = qemu_strtosz(str, &endptr, &res);
3045     g_assert_cmpint(err, ==, -EINVAL);
3046     g_assert_cmphex(res, ==, 0xbaadf00d);
3047     g_assert_true(endptr == str);
3048 
3049     str = "NaN";
3050     endptr = NULL;
3051     err = qemu_strtosz(str, &endptr, &res);
3052     g_assert_cmpint(err, ==, -EINVAL);
3053     g_assert_cmphex(res, ==, 0xbaadf00d);
3054     g_assert_true(endptr == str);
3055 
3056     /* Fractional values require scale larger than bytes */
3057     str = "1.1B";
3058     endptr = NULL;
3059     err = qemu_strtosz(str, &endptr, &res);
3060     g_assert_cmpint(err, ==, -EINVAL);
3061     g_assert_cmphex(res, ==, 0xbaadf00d);
3062     g_assert_true(endptr == str);
3063 
3064     str = "1.1";
3065     endptr = NULL;
3066     err = qemu_strtosz(str, &endptr, &res);
3067     g_assert_cmpint(err, ==, -EINVAL);
3068     g_assert_cmphex(res, ==, 0xbaadf00d);
3069     g_assert_true(endptr == str);
3070 
3071     /* No floating point exponents */
3072     str = "1.5e1k";
3073     endptr = NULL;
3074     err = qemu_strtosz(str, &endptr, &res);
3075     g_assert_cmpint(err, ==, -EINVAL);
3076     g_assert_cmphex(res, ==, 0xbaadf00d);
3077     g_assert_true(endptr == str);
3078 
3079     str = "1.5E+0k";
3080     endptr = NULL;
3081     err = qemu_strtosz(str, &endptr, &res);
3082     g_assert_cmpint(err, ==, -EINVAL);
3083     g_assert_cmphex(res, ==, 0xbaadf00d);
3084     g_assert_true(endptr == str);
3085 
3086     /* No hex fractions */
3087     str = "0x1.8k";
3088     endptr = NULL;
3089     err = qemu_strtosz(str, &endptr, &res);
3090     g_assert_cmpint(err, ==, -EINVAL);
3091     g_assert_cmphex(res, ==, 0xbaadf00d);
3092     g_assert_true(endptr == str);
3093 
3094     /* No suffixes */
3095     str = "0x18M";
3096     endptr = NULL;
3097     err = qemu_strtosz(str, &endptr, &res);
3098     g_assert_cmpint(err, ==, -EINVAL);
3099     g_assert_cmphex(res, ==, 0xbaadf00d);
3100     g_assert_true(endptr == str);
3101 
3102     /* No negative values */
3103     str = "-0";
3104     endptr = NULL;
3105     err = qemu_strtosz(str, &endptr, &res);
3106     g_assert_cmpint(err, ==, -EINVAL);
3107     g_assert_cmphex(res, ==, 0xbaadf00d);
3108     g_assert_true(endptr == str);
3109 
3110     str = "-1";
3111     endptr = NULL;
3112     err = qemu_strtosz(str, &endptr, &res);
3113     g_assert_cmpint(err, ==, -EINVAL);
3114     g_assert_cmphex(res, ==, 0xbaadf00d);
3115     g_assert_true(endptr == str);
3116 }
3117 
3118 static void test_qemu_strtosz_trailing(void)
3119 {
3120     const char *str;
3121     const char *endptr;
3122     int err;
3123     uint64_t res;
3124 
3125     str = "123xxx";
3126     endptr = NULL;
3127     res = 0xbaadf00d;
3128     err = qemu_strtosz_MiB(str, &endptr, &res);
3129     g_assert_cmpint(err, ==, 0);
3130     g_assert_cmpuint(res, ==, 123 * MiB);
3131     g_assert_true(endptr == str + 3);
3132 
3133     res = 0xbaadf00d;
3134     err = qemu_strtosz(str, NULL, &res);
3135     g_assert_cmpint(err, ==, -EINVAL);
3136     g_assert_cmphex(res, ==, 0xbaadf00d);
3137 
3138     str = "1kiB";
3139     endptr = NULL;
3140     res = 0xbaadf00d;
3141     err = qemu_strtosz(str, &endptr, &res);
3142     g_assert_cmpint(err, ==, 0);
3143     g_assert_cmpuint(res, ==, 1024);
3144     g_assert_true(endptr == str + 2);
3145 
3146     res = 0xbaadf00d;
3147     err = qemu_strtosz(str, NULL, &res);
3148     g_assert_cmpint(err, ==, -EINVAL);
3149     g_assert_cmphex(res, ==, 0xbaadf00d);
3150 
3151     str = "0x";
3152     endptr = NULL;
3153     res = 0xbaadf00d;
3154     err = qemu_strtosz(str, &endptr, &res);
3155     g_assert_cmpint(err, ==, 0);
3156     g_assert_cmpuint(res, ==, 0);
3157     g_assert_true(endptr == str + 1);
3158 
3159     res = 0xbaadf00d;
3160     err = qemu_strtosz(str, NULL, &res);
3161     g_assert_cmpint(err, ==, -EINVAL);
3162     g_assert_cmphex(res, ==, 0xbaadf00d);
3163 
3164     str = "0.NaN";
3165     endptr = NULL;
3166     res = 0xbaadf00d;
3167     err = qemu_strtosz(str, &endptr, &res);
3168     g_assert_cmpint(err, ==, 0);
3169     g_assert_cmpuint(res, ==, 0);
3170     g_assert_true(endptr == str + 2);
3171 
3172     res = 0xbaadf00d;
3173     err = qemu_strtosz(str, NULL, &res);
3174     g_assert_cmpint(err, ==, -EINVAL);
3175     g_assert_cmphex(res, ==, 0xbaadf00d);
3176 
3177     str = "123-45";
3178     endptr = NULL;
3179     res = 0xbaadf00d;
3180     err = qemu_strtosz(str, &endptr, &res);
3181     g_assert_cmpint(err, ==, 0);
3182     g_assert_cmpuint(res, ==, 123);
3183     g_assert_true(endptr == str + 3);
3184 
3185     res = 0xbaadf00d;
3186     err = qemu_strtosz(str, NULL, &res);
3187     g_assert_cmpint(err, ==, -EINVAL);
3188     g_assert_cmphex(res, ==, 0xbaadf00d);
3189 }
3190 
3191 static void test_qemu_strtosz_erange(void)
3192 {
3193     const char *str;
3194     const char *endptr;
3195     int err;
3196     uint64_t res = 0xbaadf00d;
3197 
3198     str = "18446744073709551616"; /* 2^64; see strtosz_simple for 2^64-1 */
3199     endptr = NULL;
3200     err = qemu_strtosz(str, &endptr, &res);
3201     g_assert_cmpint(err, ==, -ERANGE);
3202     g_assert_cmphex(res, ==, 0xbaadf00d);
3203     g_assert_true(endptr == str + 20);
3204 
3205     str = "20E";
3206     endptr = NULL;
3207     err = qemu_strtosz(str, &endptr, &res);
3208     g_assert_cmpint(err, ==, -ERANGE);
3209     g_assert_cmphex(res, ==, 0xbaadf00d);
3210     g_assert_true(endptr == str + 3);
3211 }
3212 
3213 static void test_qemu_strtosz_metric(void)
3214 {
3215     const char *str;
3216     int err;
3217     const char *endptr;
3218     uint64_t res;
3219 
3220     str = "12345k";
3221     endptr = str;
3222     res = 0xbaadf00d;
3223     err = qemu_strtosz_metric(str, &endptr, &res);
3224     g_assert_cmpint(err, ==, 0);
3225     g_assert_cmpuint(res, ==, 12345000);
3226     g_assert_true(endptr == str + 6);
3227 
3228     str = "12.345M";
3229     endptr = str;
3230     res = 0xbaadf00d;
3231     err = qemu_strtosz_metric(str, &endptr, &res);
3232     g_assert_cmpint(err, ==, 0);
3233     g_assert_cmpuint(res, ==, 12345000);
3234     g_assert_true(endptr == str + 7);
3235 }
3236 
3237 static void test_freq_to_str(void)
3238 {
3239     char *str;
3240 
3241     str = freq_to_str(999);
3242     g_assert_cmpstr(str, ==, "999 Hz");
3243     g_free(str);
3244 
3245     str = freq_to_str(1000);
3246     g_assert_cmpstr(str, ==, "1 KHz");
3247     g_free(str);
3248 
3249     str = freq_to_str(1010);
3250     g_assert_cmpstr(str, ==, "1.01 KHz");
3251     g_free(str);
3252 }
3253 
3254 static void test_size_to_str(void)
3255 {
3256     char *str;
3257 
3258     str = size_to_str(0);
3259     g_assert_cmpstr(str, ==, "0 B");
3260     g_free(str);
3261 
3262     str = size_to_str(1);
3263     g_assert_cmpstr(str, ==, "1 B");
3264     g_free(str);
3265 
3266     str = size_to_str(1016);
3267     g_assert_cmpstr(str, ==, "0.992 KiB");
3268     g_free(str);
3269 
3270     str = size_to_str(1024);
3271     g_assert_cmpstr(str, ==, "1 KiB");
3272     g_free(str);
3273 
3274     str = size_to_str(512ull << 20);
3275     g_assert_cmpstr(str, ==, "512 MiB");
3276     g_free(str);
3277 }
3278 
3279 static void test_iec_binary_prefix(void)
3280 {
3281     g_assert_cmpstr(iec_binary_prefix(0), ==, "");
3282     g_assert_cmpstr(iec_binary_prefix(10), ==, "Ki");
3283     g_assert_cmpstr(iec_binary_prefix(20), ==, "Mi");
3284     g_assert_cmpstr(iec_binary_prefix(30), ==, "Gi");
3285     g_assert_cmpstr(iec_binary_prefix(40), ==, "Ti");
3286     g_assert_cmpstr(iec_binary_prefix(50), ==, "Pi");
3287     g_assert_cmpstr(iec_binary_prefix(60), ==, "Ei");
3288 }
3289 
3290 static void test_si_prefix(void)
3291 {
3292     g_assert_cmpstr(si_prefix(-18), ==, "a");
3293     g_assert_cmpstr(si_prefix(-15), ==, "f");
3294     g_assert_cmpstr(si_prefix(-12), ==, "p");
3295     g_assert_cmpstr(si_prefix(-9), ==, "n");
3296     g_assert_cmpstr(si_prefix(-6), ==, "u");
3297     g_assert_cmpstr(si_prefix(-3), ==, "m");
3298     g_assert_cmpstr(si_prefix(0), ==, "");
3299     g_assert_cmpstr(si_prefix(3), ==, "K");
3300     g_assert_cmpstr(si_prefix(6), ==, "M");
3301     g_assert_cmpstr(si_prefix(9), ==, "G");
3302     g_assert_cmpstr(si_prefix(12), ==, "T");
3303     g_assert_cmpstr(si_prefix(15), ==, "P");
3304     g_assert_cmpstr(si_prefix(18), ==, "E");
3305 }
3306 
3307 int main(int argc, char **argv)
3308 {
3309     g_test_init(&argc, &argv, NULL);
3310 
3311     g_test_add_func("/cutils/parse_uint/null", test_parse_uint_null);
3312     g_test_add_func("/cutils/parse_uint/empty", test_parse_uint_empty);
3313     g_test_add_func("/cutils/parse_uint/whitespace",
3314                     test_parse_uint_whitespace);
3315     g_test_add_func("/cutils/parse_uint/invalid", test_parse_uint_invalid);
3316     g_test_add_func("/cutils/parse_uint/trailing", test_parse_uint_trailing);
3317     g_test_add_func("/cutils/parse_uint/correct", test_parse_uint_correct);
3318     g_test_add_func("/cutils/parse_uint/octal", test_parse_uint_octal);
3319     g_test_add_func("/cutils/parse_uint/decimal", test_parse_uint_decimal);
3320     g_test_add_func("/cutils/parse_uint/llong_max", test_parse_uint_llong_max);
3321     g_test_add_func("/cutils/parse_uint/max", test_parse_uint_max);
3322     g_test_add_func("/cutils/parse_uint/overflow", test_parse_uint_overflow);
3323     g_test_add_func("/cutils/parse_uint/negative", test_parse_uint_negative);
3324     g_test_add_func("/cutils/parse_uint/negzero", test_parse_uint_negzero);
3325     g_test_add_func("/cutils/parse_uint_full/trailing",
3326                     test_parse_uint_full_trailing);
3327     g_test_add_func("/cutils/parse_uint_full/correct",
3328                     test_parse_uint_full_correct);
3329     g_test_add_func("/cutils/parse_uint_full/erange_junk",
3330                     test_parse_uint_full_erange_junk);
3331 
3332     /* qemu_strtoi() tests */
3333     g_test_add_func("/cutils/qemu_strtoi/correct",
3334                     test_qemu_strtoi_correct);
3335     g_test_add_func("/cutils/qemu_strtoi/null",
3336                     test_qemu_strtoi_null);
3337     g_test_add_func("/cutils/qemu_strtoi/empty",
3338                     test_qemu_strtoi_empty);
3339     g_test_add_func("/cutils/qemu_strtoi/whitespace",
3340                     test_qemu_strtoi_whitespace);
3341     g_test_add_func("/cutils/qemu_strtoi/invalid",
3342                     test_qemu_strtoi_invalid);
3343     g_test_add_func("/cutils/qemu_strtoi/trailing",
3344                     test_qemu_strtoi_trailing);
3345     g_test_add_func("/cutils/qemu_strtoi/octal",
3346                     test_qemu_strtoi_octal);
3347     g_test_add_func("/cutils/qemu_strtoi/decimal",
3348                     test_qemu_strtoi_decimal);
3349     g_test_add_func("/cutils/qemu_strtoi/hex",
3350                     test_qemu_strtoi_hex);
3351     g_test_add_func("/cutils/qemu_strtoi/max",
3352                     test_qemu_strtoi_max);
3353     g_test_add_func("/cutils/qemu_strtoi/overflow",
3354                     test_qemu_strtoi_overflow);
3355     g_test_add_func("/cutils/qemu_strtoi/min",
3356                     test_qemu_strtoi_min);
3357     g_test_add_func("/cutils/qemu_strtoi/underflow",
3358                     test_qemu_strtoi_underflow);
3359     g_test_add_func("/cutils/qemu_strtoi/negative",
3360                     test_qemu_strtoi_negative);
3361     g_test_add_func("/cutils/qemu_strtoi/negzero",
3362                     test_qemu_strtoi_negzero);
3363     g_test_add_func("/cutils/qemu_strtoi_full/correct",
3364                     test_qemu_strtoi_full_correct);
3365     g_test_add_func("/cutils/qemu_strtoi_full/null",
3366                     test_qemu_strtoi_full_null);
3367     g_test_add_func("/cutils/qemu_strtoi_full/empty",
3368                     test_qemu_strtoi_full_empty);
3369     g_test_add_func("/cutils/qemu_strtoi_full/negative",
3370                     test_qemu_strtoi_full_negative);
3371     g_test_add_func("/cutils/qemu_strtoi_full/negzero",
3372                     test_qemu_strtoi_full_negzero);
3373     g_test_add_func("/cutils/qemu_strtoi_full/trailing",
3374                     test_qemu_strtoi_full_trailing);
3375     g_test_add_func("/cutils/qemu_strtoi_full/max",
3376                     test_qemu_strtoi_full_max);
3377     g_test_add_func("/cutils/qemu_strtoi_full/erange_junk",
3378                     test_qemu_strtoi_full_erange_junk);
3379 
3380     /* qemu_strtoui() tests */
3381     g_test_add_func("/cutils/qemu_strtoui/correct",
3382                     test_qemu_strtoui_correct);
3383     g_test_add_func("/cutils/qemu_strtoui/null",
3384                     test_qemu_strtoui_null);
3385     g_test_add_func("/cutils/qemu_strtoui/empty",
3386                     test_qemu_strtoui_empty);
3387     g_test_add_func("/cutils/qemu_strtoui/whitespace",
3388                     test_qemu_strtoui_whitespace);
3389     g_test_add_func("/cutils/qemu_strtoui/invalid",
3390                     test_qemu_strtoui_invalid);
3391     g_test_add_func("/cutils/qemu_strtoui/trailing",
3392                     test_qemu_strtoui_trailing);
3393     g_test_add_func("/cutils/qemu_strtoui/octal",
3394                     test_qemu_strtoui_octal);
3395     g_test_add_func("/cutils/qemu_strtoui/decimal",
3396                     test_qemu_strtoui_decimal);
3397     g_test_add_func("/cutils/qemu_strtoui/hex",
3398                     test_qemu_strtoui_hex);
3399     g_test_add_func("/cutils/qemu_strtoui/wrap",
3400                     test_qemu_strtoui_wrap);
3401     g_test_add_func("/cutils/qemu_strtoui/max",
3402                     test_qemu_strtoui_max);
3403     g_test_add_func("/cutils/qemu_strtoui/overflow",
3404                     test_qemu_strtoui_overflow);
3405     g_test_add_func("/cutils/qemu_strtoui/underflow",
3406                     test_qemu_strtoui_underflow);
3407     g_test_add_func("/cutils/qemu_strtoui/negative",
3408                     test_qemu_strtoui_negative);
3409     g_test_add_func("/cutils/qemu_strtoui/negzero",
3410                     test_qemu_strtoui_negzero);
3411     g_test_add_func("/cutils/qemu_strtoui_full/correct",
3412                     test_qemu_strtoui_full_correct);
3413     g_test_add_func("/cutils/qemu_strtoui_full/null",
3414                     test_qemu_strtoui_full_null);
3415     g_test_add_func("/cutils/qemu_strtoui_full/empty",
3416                     test_qemu_strtoui_full_empty);
3417     g_test_add_func("/cutils/qemu_strtoui_full/negative",
3418                     test_qemu_strtoui_full_negative);
3419     g_test_add_func("/cutils/qemu_strtoui_full/negzero",
3420                     test_qemu_strtoui_full_negzero);
3421     g_test_add_func("/cutils/qemu_strtoui_full/trailing",
3422                     test_qemu_strtoui_full_trailing);
3423     g_test_add_func("/cutils/qemu_strtoui_full/max",
3424                     test_qemu_strtoui_full_max);
3425     g_test_add_func("/cutils/qemu_strtoui_full/erange_junk",
3426                     test_qemu_strtoui_full_erange_junk);
3427 
3428     /* qemu_strtol() tests */
3429     g_test_add_func("/cutils/qemu_strtol/correct",
3430                     test_qemu_strtol_correct);
3431     g_test_add_func("/cutils/qemu_strtol/null",
3432                     test_qemu_strtol_null);
3433     g_test_add_func("/cutils/qemu_strtol/empty",
3434                     test_qemu_strtol_empty);
3435     g_test_add_func("/cutils/qemu_strtol/whitespace",
3436                     test_qemu_strtol_whitespace);
3437     g_test_add_func("/cutils/qemu_strtol/invalid",
3438                     test_qemu_strtol_invalid);
3439     g_test_add_func("/cutils/qemu_strtol/trailing",
3440                     test_qemu_strtol_trailing);
3441     g_test_add_func("/cutils/qemu_strtol/octal",
3442                     test_qemu_strtol_octal);
3443     g_test_add_func("/cutils/qemu_strtol/decimal",
3444                     test_qemu_strtol_decimal);
3445     g_test_add_func("/cutils/qemu_strtol/hex",
3446                     test_qemu_strtol_hex);
3447     g_test_add_func("/cutils/qemu_strtol/max",
3448                     test_qemu_strtol_max);
3449     g_test_add_func("/cutils/qemu_strtol/overflow",
3450                     test_qemu_strtol_overflow);
3451     g_test_add_func("/cutils/qemu_strtol/min",
3452                     test_qemu_strtol_min);
3453     g_test_add_func("/cutils/qemu_strtol/underflow",
3454                     test_qemu_strtol_underflow);
3455     g_test_add_func("/cutils/qemu_strtol/negative",
3456                     test_qemu_strtol_negative);
3457     g_test_add_func("/cutils/qemu_strtol/negzero",
3458                     test_qemu_strtol_negzero);
3459     g_test_add_func("/cutils/qemu_strtol_full/correct",
3460                     test_qemu_strtol_full_correct);
3461     g_test_add_func("/cutils/qemu_strtol_full/null",
3462                     test_qemu_strtol_full_null);
3463     g_test_add_func("/cutils/qemu_strtol_full/empty",
3464                     test_qemu_strtol_full_empty);
3465     g_test_add_func("/cutils/qemu_strtol_full/negative",
3466                     test_qemu_strtol_full_negative);
3467     g_test_add_func("/cutils/qemu_strtol_full/negzero",
3468                     test_qemu_strtol_full_negzero);
3469     g_test_add_func("/cutils/qemu_strtol_full/trailing",
3470                     test_qemu_strtol_full_trailing);
3471     g_test_add_func("/cutils/qemu_strtol_full/max",
3472                     test_qemu_strtol_full_max);
3473     g_test_add_func("/cutils/qemu_strtol_full/erange_junk",
3474                     test_qemu_strtol_full_erange_junk);
3475 
3476     /* qemu_strtoul() tests */
3477     g_test_add_func("/cutils/qemu_strtoul/correct",
3478                     test_qemu_strtoul_correct);
3479     g_test_add_func("/cutils/qemu_strtoul/null",
3480                     test_qemu_strtoul_null);
3481     g_test_add_func("/cutils/qemu_strtoul/empty",
3482                     test_qemu_strtoul_empty);
3483     g_test_add_func("/cutils/qemu_strtoul/whitespace",
3484                     test_qemu_strtoul_whitespace);
3485     g_test_add_func("/cutils/qemu_strtoul/invalid",
3486                     test_qemu_strtoul_invalid);
3487     g_test_add_func("/cutils/qemu_strtoul/trailing",
3488                     test_qemu_strtoul_trailing);
3489     g_test_add_func("/cutils/qemu_strtoul/octal",
3490                     test_qemu_strtoul_octal);
3491     g_test_add_func("/cutils/qemu_strtoul/decimal",
3492                     test_qemu_strtoul_decimal);
3493     g_test_add_func("/cutils/qemu_strtoul/hex",
3494                     test_qemu_strtoul_hex);
3495     g_test_add_func("/cutils/qemu_strtoul/wrap",
3496                     test_qemu_strtoul_wrap);
3497     g_test_add_func("/cutils/qemu_strtoul/max",
3498                     test_qemu_strtoul_max);
3499     g_test_add_func("/cutils/qemu_strtoul/overflow",
3500                     test_qemu_strtoul_overflow);
3501     g_test_add_func("/cutils/qemu_strtoul/underflow",
3502                     test_qemu_strtoul_underflow);
3503     g_test_add_func("/cutils/qemu_strtoul/negative",
3504                     test_qemu_strtoul_negative);
3505     g_test_add_func("/cutils/qemu_strtoul/negzero",
3506                     test_qemu_strtoul_negzero);
3507     g_test_add_func("/cutils/qemu_strtoul_full/correct",
3508                     test_qemu_strtoul_full_correct);
3509     g_test_add_func("/cutils/qemu_strtoul_full/null",
3510                     test_qemu_strtoul_full_null);
3511     g_test_add_func("/cutils/qemu_strtoul_full/empty",
3512                     test_qemu_strtoul_full_empty);
3513     g_test_add_func("/cutils/qemu_strtoul_full/negative",
3514                     test_qemu_strtoul_full_negative);
3515     g_test_add_func("/cutils/qemu_strtoul_full/negzero",
3516                     test_qemu_strtoul_full_negzero);
3517     g_test_add_func("/cutils/qemu_strtoul_full/trailing",
3518                     test_qemu_strtoul_full_trailing);
3519     g_test_add_func("/cutils/qemu_strtoul_full/max",
3520                     test_qemu_strtoul_full_max);
3521     g_test_add_func("/cutils/qemu_strtoul_full/erange_junk",
3522                     test_qemu_strtoul_full_erange_junk);
3523 
3524     /* qemu_strtoi64() tests */
3525     g_test_add_func("/cutils/qemu_strtoi64/correct",
3526                     test_qemu_strtoi64_correct);
3527     g_test_add_func("/cutils/qemu_strtoi64/null",
3528                     test_qemu_strtoi64_null);
3529     g_test_add_func("/cutils/qemu_strtoi64/empty",
3530                     test_qemu_strtoi64_empty);
3531     g_test_add_func("/cutils/qemu_strtoi64/whitespace",
3532                     test_qemu_strtoi64_whitespace);
3533     g_test_add_func("/cutils/qemu_strtoi64/invalid",
3534                     test_qemu_strtoi64_invalid);
3535     g_test_add_func("/cutils/qemu_strtoi64/trailing",
3536                     test_qemu_strtoi64_trailing);
3537     g_test_add_func("/cutils/qemu_strtoi64/octal",
3538                     test_qemu_strtoi64_octal);
3539     g_test_add_func("/cutils/qemu_strtoi64/decimal",
3540                     test_qemu_strtoi64_decimal);
3541     g_test_add_func("/cutils/qemu_strtoi64/hex",
3542                     test_qemu_strtoi64_hex);
3543     g_test_add_func("/cutils/qemu_strtoi64/max",
3544                     test_qemu_strtoi64_max);
3545     g_test_add_func("/cutils/qemu_strtoi64/overflow",
3546                     test_qemu_strtoi64_overflow);
3547     g_test_add_func("/cutils/qemu_strtoi64/min",
3548                     test_qemu_strtoi64_min);
3549     g_test_add_func("/cutils/qemu_strtoi64/underflow",
3550                     test_qemu_strtoi64_underflow);
3551     g_test_add_func("/cutils/qemu_strtoi64/negative",
3552                     test_qemu_strtoi64_negative);
3553     g_test_add_func("/cutils/qemu_strtoi64/negzero",
3554                     test_qemu_strtoi64_negzero);
3555     g_test_add_func("/cutils/qemu_strtoi64_full/correct",
3556                     test_qemu_strtoi64_full_correct);
3557     g_test_add_func("/cutils/qemu_strtoi64_full/null",
3558                     test_qemu_strtoi64_full_null);
3559     g_test_add_func("/cutils/qemu_strtoi64_full/empty",
3560                     test_qemu_strtoi64_full_empty);
3561     g_test_add_func("/cutils/qemu_strtoi64_full/negative",
3562                     test_qemu_strtoi64_full_negative);
3563     g_test_add_func("/cutils/qemu_strtoi64_full/negzero",
3564                     test_qemu_strtoi64_full_negzero);
3565     g_test_add_func("/cutils/qemu_strtoi64_full/trailing",
3566                     test_qemu_strtoi64_full_trailing);
3567     g_test_add_func("/cutils/qemu_strtoi64_full/max",
3568                     test_qemu_strtoi64_full_max);
3569     g_test_add_func("/cutils/qemu_strtoi64_full/erange_junk",
3570                     test_qemu_strtoi64_full_erange_junk);
3571 
3572     /* qemu_strtou64() tests */
3573     g_test_add_func("/cutils/qemu_strtou64/correct",
3574                     test_qemu_strtou64_correct);
3575     g_test_add_func("/cutils/qemu_strtou64/null",
3576                     test_qemu_strtou64_null);
3577     g_test_add_func("/cutils/qemu_strtou64/empty",
3578                     test_qemu_strtou64_empty);
3579     g_test_add_func("/cutils/qemu_strtou64/whitespace",
3580                     test_qemu_strtou64_whitespace);
3581     g_test_add_func("/cutils/qemu_strtou64/invalid",
3582                     test_qemu_strtou64_invalid);
3583     g_test_add_func("/cutils/qemu_strtou64/trailing",
3584                     test_qemu_strtou64_trailing);
3585     g_test_add_func("/cutils/qemu_strtou64/octal",
3586                     test_qemu_strtou64_octal);
3587     g_test_add_func("/cutils/qemu_strtou64/decimal",
3588                     test_qemu_strtou64_decimal);
3589     g_test_add_func("/cutils/qemu_strtou64/hex",
3590                     test_qemu_strtou64_hex);
3591     g_test_add_func("/cutils/qemu_strtou64/wrap",
3592                     test_qemu_strtou64_wrap);
3593     g_test_add_func("/cutils/qemu_strtou64/max",
3594                     test_qemu_strtou64_max);
3595     g_test_add_func("/cutils/qemu_strtou64/overflow",
3596                     test_qemu_strtou64_overflow);
3597     g_test_add_func("/cutils/qemu_strtou64/underflow",
3598                     test_qemu_strtou64_underflow);
3599     g_test_add_func("/cutils/qemu_strtou64/negative",
3600                     test_qemu_strtou64_negative);
3601     g_test_add_func("/cutils/qemu_strtou64/negzero",
3602                     test_qemu_strtou64_negzero);
3603     g_test_add_func("/cutils/qemu_strtou64_full/correct",
3604                     test_qemu_strtou64_full_correct);
3605     g_test_add_func("/cutils/qemu_strtou64_full/null",
3606                     test_qemu_strtou64_full_null);
3607     g_test_add_func("/cutils/qemu_strtou64_full/empty",
3608                     test_qemu_strtou64_full_empty);
3609     g_test_add_func("/cutils/qemu_strtou64_full/negative",
3610                     test_qemu_strtou64_full_negative);
3611     g_test_add_func("/cutils/qemu_strtou64_full/negzero",
3612                     test_qemu_strtou64_full_negzero);
3613     g_test_add_func("/cutils/qemu_strtou64_full/trailing",
3614                     test_qemu_strtou64_full_trailing);
3615     g_test_add_func("/cutils/qemu_strtou64_full/max",
3616                     test_qemu_strtou64_full_max);
3617     g_test_add_func("/cutils/qemu_strtou64_full/erange_junk",
3618                     test_qemu_strtou64_full_erange_junk);
3619 
3620     g_test_add_func("/cutils/strtosz/simple",
3621                     test_qemu_strtosz_simple);
3622     g_test_add_func("/cutils/strtosz/hex",
3623                     test_qemu_strtosz_hex);
3624     g_test_add_func("/cutils/strtosz/units",
3625                     test_qemu_strtosz_units);
3626     g_test_add_func("/cutils/strtosz/float",
3627                     test_qemu_strtosz_float);
3628     g_test_add_func("/cutils/strtosz/invalid",
3629                     test_qemu_strtosz_invalid);
3630     g_test_add_func("/cutils/strtosz/trailing",
3631                     test_qemu_strtosz_trailing);
3632     g_test_add_func("/cutils/strtosz/erange",
3633                     test_qemu_strtosz_erange);
3634     g_test_add_func("/cutils/strtosz/metric",
3635                     test_qemu_strtosz_metric);
3636 
3637     g_test_add_func("/cutils/size_to_str",
3638                     test_size_to_str);
3639     g_test_add_func("/cutils/freq_to_str",
3640                     test_freq_to_str);
3641     g_test_add_func("/cutils/iec_binary_prefix",
3642                     test_iec_binary_prefix);
3643     g_test_add_func("/cutils/si_prefix",
3644                     test_si_prefix);
3645     return g_test_run();
3646 }
3647