add 64bit strtol/strtoul and strtoll/strtoull tests
[libc-test] / src / functional / strtol.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <errno.h>
5 #include "test.h"
6
7 /* r = place to store result
8  * f = function call to test (or any expression)
9  * x = expected result
10  * m = message to print on failure (with formats for r & x)
11 **/
12
13 #define TEST(r, f, x, m) ( \
14         errno = 0, msg = #f, ((r) = (f)) == (x) || \
15         (t_error("%s failed (" m ")\n", #f, r, x), 0) )
16
17 #define TEST2(r, f, x, m) ( \
18         ((r) = (f)) == (x) || \
19         (t_error("%s failed (" m ")\n", msg, r, x), 0) )
20
21 int main(void)
22 {
23         int i;
24         long l;
25         unsigned long ul;
26         long long ll;
27         unsigned long long ull;
28         char *msg="";
29         char *s, *c;
30
31         TEST(l, atol("2147483647"), 2147483647L, "max 32bit signed %ld != %ld");
32         TEST(l, strtol("2147483647", 0, 0), 2147483647L, "max 32bit signed %ld != %ld");
33         TEST(ul, strtoul("4294967295", 0, 0), 4294967295UL, "max 32bit unsigned %lu != %lu");
34
35         if (sizeof(long) == 4) {
36                 TEST(l, strtol(s="2147483648", &c, 0), 2147483647L, "uncaught overflow %ld != %ld");
37                 TEST2(i, c-s, 10, "wrong final position %d != %d");
38                 TEST2(i, errno, ERANGE, "missing errno %d != %d");
39                 TEST(l, strtol(s="-2147483649", &c, 0), -2147483647L-1, "uncaught overflow %ld != %ld");
40                 TEST2(i, c-s, 11, "wrong final position %d != %d");
41                 TEST2(i, errno, ERANGE, "missing errno %d != %d");
42                 TEST(ul, strtoul(s="4294967296", &c, 0), 4294967295UL, "uncaught overflow %lu != %lu");
43                 TEST2(i, c-s, 10, "wrong final position %d != %d");
44                 TEST2(i, errno, ERANGE, "missing errno %d != %d");
45                 TEST(ul, strtoul(s="-1", &c, 0), -1UL, "rejected negative %lu != %lu");
46                 TEST2(i, c-s, 2, "wrong final position %d != %d");
47                 TEST2(i, errno, 0, "spurious errno %d != %d");
48                 TEST(ul, strtoul(s="-2", &c, 0), -2UL, "rejected negative %lu != %lu");
49                 TEST2(i, c-s, 2, "wrong final position %d != %d");
50                 TEST2(i, errno, 0, "spurious errno %d != %d");
51                 TEST(ul, strtoul(s="-2147483648", &c, 0), -2147483648UL, "rejected negative %lu != %lu");
52                 TEST2(i, c-s, 11, "wrong final position %d != %d");
53                 TEST2(i, errno, 0, "spurious errno %d != %d");
54                 TEST(ul, strtoul(s="-2147483649", &c, 0), -2147483649UL, "rejected negative %lu != %lu");
55                 TEST2(i, c-s, 11, "wrong final position %d != %d");
56                 TEST2(i, errno, 0, "spurious errno %d != %d");
57                 TEST(ul, strtoul(s="-4294967296", &c, 0), 4294967295UL, "uncaught negative overflow %lu != %lu");
58                 TEST2(i, c-s, 11, "wrong final position %d != %d");
59                 TEST2(i, errno, ERANGE, "spurious errno %d != %d");
60         } else if (sizeof(long) == 8) {
61                 TEST(l, strtol(s="9223372036854775808", &c, 0), 9223372036854775807L, "uncaught overflow %ld != %ld");
62                 TEST2(i, c-s, 19, "wrong final position %d != %d");
63                 TEST2(i, errno, ERANGE, "missing errno %d != %d");
64                 TEST(l, strtol(s="-9223372036854775809", &c, 0), -9223372036854775807L-1, "uncaught overflow %ld != %ld");
65                 TEST2(i, c-s, 20, "wrong final position %d != %d");
66                 TEST2(i, errno, ERANGE, "missing errno %d != %d");
67                 TEST(ul, strtoul(s="18446744073709551616", &c, 0), 18446744073709551615UL, "uncaught overflow %lu != %lu");
68                 TEST2(i, c-s, 20, "wrong final position %d != %d");
69                 TEST2(i, errno, ERANGE, "missing errno %d != %d");
70                 TEST(ul, strtoul(s="-1", &c, 0), -1UL, "rejected negative %lu != %lu");
71                 TEST2(i, c-s, 2, "wrong final position %d != %d");
72                 TEST2(i, errno, 0, "spurious errno %d != %d");
73                 TEST(ul, strtoul(s="-2", &c, 0), -2UL, "rejected negative %lu != %lu");
74                 TEST2(i, c-s, 2, "wrong final position %d != %d");
75                 TEST2(i, errno, 0, "spurious errno %d != %d");
76                 TEST(ul, strtoul(s="-9223372036854775808", &c, 0), -9223372036854775808UL, "rejected negative %lu != %lu");
77                 TEST2(i, c-s, 20, "wrong final position %d != %d");
78                 TEST2(i, errno, 0, "spurious errno %d != %d");
79                 TEST(ul, strtoul(s="-9223372036854775809", &c, 0), -9223372036854775809UL, "rejected negative %lu != %lu");
80                 TEST2(i, c-s, 20, "wrong final position %d != %d");
81                 TEST2(i, errno, 0, "spurious errno %d != %d");
82                 TEST(ul, strtoul(s="-18446744073709551616", &c, 0), 18446744073709551615UL, "uncaught negative overflow %lu != %lu");
83                 TEST2(i, c-s, 21, "wrong final position %d != %d");
84                 TEST2(i, errno, ERANGE, "spurious errno %d != %d");
85         } else {
86                 t_error("sizeof(long) == %d, not implemented\n", (int)sizeof(long));
87         }
88
89         if (sizeof(long long) == 8) {
90                 TEST(ll, strtoll(s="9223372036854775808", &c, 0), 9223372036854775807LL, "uncaught overflow %lld != %lld");
91                 TEST2(i, c-s, 19, "wrong final position %d != %d");
92                 TEST2(i, errno, ERANGE, "missing errno %d != %d");
93                 TEST(ll, strtoll(s="-9223372036854775809", &c, 0), -9223372036854775807LL-1, "uncaught overflow %lld != %lld");
94                 TEST2(i, c-s, 20, "wrong final position %d != %d");
95                 TEST2(i, errno, ERANGE, "missing errno %d != %d");
96                 TEST(ull, strtoull(s="18446744073709551616", &c, 0), 18446744073709551615ULL, "uncaught overflow %llu != %llu");
97                 TEST2(i, c-s, 20, "wrong final position %d != %d");
98                 TEST2(i, errno, ERANGE, "missing errno %d != %d");
99                 TEST(ull, strtoull(s="-1", &c, 0), -1ULL, "rejected negative %llu != %llu");
100                 TEST2(i, c-s, 2, "wrong final position %d != %d");
101                 TEST2(i, errno, 0, "spurious errno %d != %d");
102                 TEST(ull, strtoull(s="-2", &c, 0), -2ULL, "rejected negative %llu != %llu");
103                 TEST2(i, c-s, 2, "wrong final position %d != %d");
104                 TEST2(i, errno, 0, "spurious errno %d != %d");
105                 TEST(ull, strtoull(s="-9223372036854775808", &c, 0), -9223372036854775808ULL, "rejected negative %llu != %llu");
106                 TEST2(i, c-s, 20, "wrong final position %d != %d");
107                 TEST2(i, errno, 0, "spurious errno %d != %d");
108                 TEST(ull, strtoull(s="-9223372036854775809", &c, 0), -9223372036854775809ULL, "rejected negative %llu != %llu");
109                 TEST2(i, c-s, 20, "wrong final position %d != %d");
110                 TEST2(i, errno, 0, "spurious errno %d != %d");
111                 TEST(ull, strtoull(s="-18446744073709551616", &c, 0), 18446744073709551615ULL, "uncaught negative overflow %llu != %llu");
112                 TEST2(i, c-s, 21, "wrong final position %d != %d");
113                 TEST2(i, errno, ERANGE, "spurious errno %d != %d");
114         } else {
115                 t_error("sizeof(long long) == %d, not implemented\n", (int)sizeof(long long));
116         }
117
118         TEST(l, strtol("z", 0, 36), 35, "%ld != %ld");
119         TEST(l, strtol("00010010001101000101011001111000", 0, 2), 0x12345678, "%ld != %ld");
120         TEST(l, strtol(s="0F5F", &c, 16), 0x0f5f, "%ld != %ld");
121
122         TEST(l, strtol(s="0xz", &c, 16), 0, "%ld != %ld");
123         TEST2(i, c-s, 1, "wrong final position %ld != %ld");
124
125         TEST(l, strtol(s="0x1234", &c, 16), 0x1234, "%ld != %ld");
126         TEST2(i, c-s, 6, "wrong final position %ld != %ld");
127
128         c = NULL;
129         TEST(l, strtol(s="123", &c, 37), 0, "%ld != %ld");
130         TEST2(i, c-s, 0, "wrong final position %d != %d");
131         TEST2(i, errno, EINVAL, "%d != %d");
132
133         TEST(l, strtol(s="  15437", &c, 8), 015437, "%ld != %ld");
134         TEST2(i, c-s, 7, "wrong final position %d != %d");
135
136         TEST(l, strtol(s="  1", &c, 0), 1, "%ld != %ld");
137         TEST2(i, c-s, 3, "wrong final position %d != %d");
138         return t_status;
139 }