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
},
71 {"0xFFFFFFFFFFFF00FF" , 9223372036854775807ll, 0, 0, ERANGE
},
76 {"0xffffffffg", 0xffffffff, 0, 'g', 0},
77 {"0xffffffffffffffffg", 0xffffffffffffffffull
, 0, 'g', 0},
78 {"-0xfedcba987654321", 0xf0123456789abcdfull
, 0, 0, 0},
79 {"0xf1f2f3f4f5f6f7f8f9", 0xffffffffffffffffull
, 0, 0, ERANGE
},
80 {"-0x123456789abcdef01", 0xffffffffffffffffull
, 0, 0, ERANGE
},
84 /* Prototypes for local functions. */
85 static void expand (char *dst
, int c
);
90 register const struct ltest
*lt
;
95 for (lt
= tests
; lt
->str
!= NULL
; ++lt
)
97 register long long int l
;
100 l
= strtoll (lt
->str
, &ep
, lt
->base
);
102 printf ("strtoll(\"%s\", , %d) test %u",
103 lt
->str
, lt
->base
, (unsigned int) (lt
- tests
));
104 if (l
== (long long int) lt
->expect
&& *ep
== lt
->left
105 && save_errno
== lt
->err
)
110 if (l
!= (long long int) lt
->expect
)
111 printf(" returns %lld, expected %lld\n",
112 l
, (long long int) lt
->expect
);
115 char exp1
[5], exp2
[5];
117 expand (exp2
, lt
->left
);
118 printf (" leaves '%s', expected '%s'\n", exp1
, exp2
);
120 if (save_errno
!= lt
->err
)
121 printf (" errno %d (%s) instead of %d (%s)\n",
122 save_errno
, strerror (save_errno
),
123 lt
->err
, strerror (lt
->err
));
128 for (++lt
; lt
->str
!= NULL
; lt
++)
130 register unsigned long long int ul
;
133 ul
= strtoull (lt
->str
, &ep
, lt
->base
);
135 printf ("strtoull(\"%s\", , %d) test %u",
136 lt
->str
, lt
->base
, (unsigned int) (lt
- tests
));
137 if (ul
== lt
->expect
&& *ep
== lt
->left
&& save_errno
== lt
->err
)
142 if (ul
!= lt
->expect
)
143 printf (" returns %llu, expected %llu\n",
147 char exp1
[5], exp2
[5];
149 expand (exp2
, lt
->left
);
150 printf (" leaves '%s', expected '%s'\n", exp1
, exp2
);
152 if (save_errno
!= lt
->err
)
153 printf (" errno %d (%s) instead of %d (%s)\n",
154 save_errno
, strerror (save_errno
),
155 lt
->err
, strerror (lt
->err
));
160 exit (status
? EXIT_FAILURE
: EXIT_SUCCESS
);
174 (void) sprintf (dst
, "%#.3o", (unsigned int) c
);