lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame^] | 1 | |
| 2 | #include <stdlib.h> |
| 3 | #include <stdio.h> |
| 4 | |
| 5 | |
| 6 | const char *strings[]={ |
| 7 | /* some simple stuff */ |
| 8 | "0", "1", "10", |
| 9 | "100", "1000", "10000", "100000", "1000000", |
| 10 | "10000000", "100000000", "1000000000", |
| 11 | |
| 12 | /* negative */ |
| 13 | "-0", "-1", "-10", |
| 14 | "-100", "-1000", "-10000", "-100000", "-1000000", |
| 15 | "-10000000", "-100000000", "-1000000000", |
| 16 | |
| 17 | /* test base>10 */ |
| 18 | "a", "b", "f", "g", "z", |
| 19 | |
| 20 | /* test hex */ |
| 21 | "0x0", "0x1", "0xa", "0xf", "0x10", |
| 22 | |
| 23 | /* test octal */ |
| 24 | "00", "01", "07", "08", "0a", "010", |
| 25 | |
| 26 | /* other */ |
| 27 | "0x8000000", |
| 28 | |
| 29 | /* check overflow cases: (for 32 bit) */ |
| 30 | "2147483645", |
| 31 | "2147483646", |
| 32 | "2147483647", |
| 33 | "2147483648", |
| 34 | "2147483649", |
| 35 | "-2147483645", |
| 36 | "-2147483646", |
| 37 | "-2147483647", |
| 38 | "-2147483648", |
| 39 | "-2147483649", |
| 40 | "4294967293", |
| 41 | "4294967294", |
| 42 | "4294967295", |
| 43 | "4294967296", |
| 44 | "4294967297", |
| 45 | "-4294967293", |
| 46 | "-4294967294", |
| 47 | "-4294967295", |
| 48 | "-4294967296", |
| 49 | "-4294967297", |
| 50 | |
| 51 | /* bad input tests */ |
| 52 | "", |
| 53 | "00", |
| 54 | "0x", |
| 55 | "0x0", |
| 56 | "-", |
| 57 | "+", |
| 58 | " ", |
| 59 | " -", |
| 60 | " - 0", |
| 61 | }; |
| 62 | int n_tests=sizeof(strings)/sizeof(strings[0]); |
| 63 | |
| 64 | |
| 65 | |
| 66 | void do_test(int base); |
| 67 | void do_test(int base) |
| 68 | { |
| 69 | int i; |
| 70 | quad_t n; |
| 71 | char *endptr; |
| 72 | |
| 73 | for(i=0;i<n_tests;i++){ |
| 74 | n=strtoq(strings[i],&endptr,base); |
| 75 | printf("strtoq(\"%s\",%d) len=%lu res=%qd\n", |
| 76 | strings[i],base,(unsigned long)(endptr-strings[i]),n); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | int main(int argc,char *argv[]) |
| 81 | { |
| 82 | do_test(0); |
| 83 | do_test(8); |
| 84 | do_test(10); |
| 85 | do_test(16); |
| 86 | do_test(36); |
| 87 | |
| 88 | return 0; |
| 89 | } |