1 /* My bet is this was written by Chris Torek.
2 I reformatted and ansidecl-ized it, and tweaked it a little. */
13 const char *str
; /* Convert this. */
14 unsigned long long int expect
; /* To get this. */
15 int base
; /* Use this base. */
16 char left
; /* With this left over. */
17 int err
; /* And this in errno. */
19 static const struct ltest tests
[] =
21 /* First, signed numbers: */
23 {"123", 123, 0, 0, 0},
24 {"+123", 123, 0, 0, 0},
25 {" 123", 123, 0, 0, 0},
26 {" 123 ", 123, 0, ' ', 0},
27 {" -17", -17, 0, 0, 0},
29 /* implicit base... */
30 {"0123", 0123, 0, 0, 0},
31 {"0123a", 0123, 0, 'a', 0},
32 {"01239", 0123, 0, '9', 0},
33 {"0x123", 0x123, 0, 0, 0},
34 {"-0x123", -0x123, 0, 0, 0},
35 {"0x0xc", 0, 0, 'x', 0},
36 {" +0x123fg", 0x123f, 0, 'g', 0},
38 /* explicit base... */
39 {"123", 0x123, 16, 0, 0},
40 {"0x123", 0x123, 16, 0, 0},
41 {"123", 0123, 8, 0, 0},
42 {"0123", 0123, 8, 0, 0},
43 {"0123", 123, 10, 0, 0},
44 {"0x123", 0, 10, 'x', 0},
46 /* case insensitivity... */
47 {"abcd", 0xabcd, 16, 0, 0},
48 {"AbCd", 0xabcd, 16, 0, 0},
49 {"0xABCD", 0xabcd, 16, 0, 0},
50 {"0Xabcd", 0xabcd, 16, 0, 0},
53 {"0xyz", 33 * 35 + 34, 35, 'z', 0},
54 {"yz!", 34 * 36 + 35, 36, '!', 0},
55 {"-yz", -(34*36 + 35), 36, 0, 0},
56 {"GhI4", ((16*20 + 17)*20 + 18)*20 + 4, 20, 0, 0},
58 /* special case for the 32-bit version of strtoll,
59 from a ncftp configure test */
60 {"99000000001", 1000000000ll * 99ll + 1ll, 0, 0},
63 {"9223372036854775807", 9223372036854775807ll, 0, 0, 0},
64 {"9223372036854775808", 9223372036854775807ll, 0, 0, ERANGE
},
65 {"922337203685477580777", 9223372036854775807ll, 0, 0, ERANGE
},
66 {"9223372036854775810", 9223372036854775807ll, 0, 0, ERANGE
},
67 {"-2147483648", -2147483648ll, 0, 0, 0},
68 {"-9223372036854775808", -9223372036854775807ll - 1, 0, 0, 0},
69 {"-9223372036854775809", -9223372036854775807ll - 1, 0, 0, ERANGE
},
70 {"0x112233445566778899z", 9223372036854775807ll, 16, 'z', ERANGE
},
75 {"0xffffffffg", 0xffffffff, 0, 'g', 0},
76 {"0xffffffffffffffffg", 0xffffffffffffffffull
, 0, 'g', 0},
77 {"-0xfedcba987654321", 0xf0123456789abcdfull
, 0, 0, 0},
78 {"0xf1f2f3f4f5f6f7f8f9", 0xffffffffffffffffull
, 0, 0, ERANGE
},
79 {"-0x123456789abcdef01", 0xffffffffffffffffull
, 0, 0, ERANGE
},
83 static void expand
__P ((char *dst
, int c
));
86 main (int argc
, char ** argv
)
88 register const struct ltest
*lt
;
93 for (lt
= tests
; lt
->str
!= NULL
; ++lt
)
95 register long long int l
;
98 l
= strtoll (lt
->str
, &ep
, lt
->base
);
100 printf ("strtoll(\"%s\", , %d) test %u",
101 lt
->str
, lt
->base
, (unsigned int) (lt
- tests
));
102 if (l
== (long long int) lt
->expect
&& *ep
== lt
->left
103 && save_errno
== lt
->err
)
108 if (l
!= (long long int) lt
->expect
)
109 printf(" returns %lld, expected %lld\n",
110 l
, (long long int) lt
->expect
);
113 char exp1
[5], exp2
[5];
115 expand (exp2
, lt
->left
);
116 printf (" leaves '%s', expected '%s'\n", exp1
, exp2
);
118 if (save_errno
!= lt
->err
)
119 printf (" errno %d (%s) instead of %d (%s)\n",
120 save_errno
, strerror (save_errno
),
121 lt
->err
, strerror (lt
->err
));
126 for (++lt
; lt
->str
!= NULL
; lt
++)
128 register unsigned long long int ul
;
131 ul
= strtoull (lt
->str
, &ep
, lt
->base
);
133 printf ("strtoull(\"%s\", , %d) test %u",
134 lt
->str
, lt
->base
, (unsigned int) (lt
- tests
));
135 if (ul
== lt
->expect
&& *ep
== lt
->left
&& save_errno
== lt
->err
)
140 if (ul
!= lt
->expect
)
141 printf (" returns %llu, expected %llu\n",
145 char exp1
[5], exp2
[5];
147 expand (exp2
, lt
->left
);
148 printf (" leaves '%s', expected '%s'\n", exp1
, exp2
);
150 if (save_errno
!= lt
->err
)
151 printf (" errno %d (%s) instead of %d (%s)\n",
152 save_errno
, strerror (save_errno
),
153 lt
->err
, strerror (lt
->err
));
158 exit (status
? EXIT_FAILURE
: EXIT_SUCCESS
);
172 (void) sprintf (dst
, "%#.3o", (unsigned int) c
);