64909136abbf2494b8b83206248b347bd6538865
[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         char *msg="";
27         char *s, *c;
28
29         TEST(l, atol("2147483647"), 2147483647L, "max 32bit signed %ld != %ld");
30         TEST(l, strtol("2147483647", 0, 0), 2147483647L, "max 32bit signed %ld != %ld");
31         TEST(ul, strtoul("4294967295", 0, 0), 4294967295UL, "max 32bit unsigned %lu != %lu");
32
33         if (sizeof(long) == 4) {
34                 TEST(l, strtol(s="2147483648", &c, 0), 2147483647L, "uncaught overflow %ld != %ld");
35                 TEST2(i, c-s, 10, "wrong final position %d != %d");
36                 TEST2(i, errno, ERANGE, "missing errno %d != %d");
37                 TEST(l, strtol(s="-2147483649", &c, 0), -2147483647L-1, "uncaught overflow %ld != %ld");
38                 TEST2(i, c-s, 11, "wrong final position %d != %d");
39                 TEST2(i, errno, ERANGE, "missing errno %d != %d");
40                 TEST(ul, strtoul(s="4294967296", &c, 0), 4294967295UL, "uncaught overflow %lu != %lu");
41                 TEST2(i, c-s, 10, "wrong final position %d != %d");
42                 TEST2(i, errno, ERANGE, "missing errno %d != %d");
43                 TEST(ul, strtoul(s="-1", &c, 0), -1UL, "rejected negative %lu != %lu");
44                 TEST2(i, c-s, 2, "wrong final position %d != %d");
45                 TEST2(i, errno, 0, "spurious errno %d != %d");
46                 TEST(ul, strtoul(s="-2", &c, 0), -2UL, "rejected negative %lu != %lu");
47                 TEST2(i, c-s, 2, "wrong final position %d != %d");
48                 TEST2(i, errno, 0, "spurious errno %d != %d");
49                 TEST(ul, strtoul(s="-2147483648", &c, 0), -2147483648UL, "rejected negative %lu != %lu");
50                 TEST2(i, c-s, 11, "wrong final position %d != %d");
51                 TEST2(i, errno, 0, "spurious errno %d != %d");
52                 TEST(ul, strtoul(s="-2147483649", &c, 0), -2147483649UL, "rejected negative %lu != %lu");
53                 TEST2(i, c-s, 11, "wrong final position %d != %d");
54                 TEST2(i, errno, 0, "spurious errno %d != %d");
55         } else {
56                 TEST(i, 0, 1, "64bit tests not implemented");
57         }
58
59         TEST(l, strtol("z", 0, 36), 35, "%ld != %ld");
60         TEST(l, strtol("00010010001101000101011001111000", 0, 2), 0x12345678, "%ld != %ld");
61         TEST(l, strtol(s="0F5F", &c, 16), 0x0f5f, "%ld != %ld");
62
63         TEST(l, strtol(s="0xz", &c, 16), 0, "%ld != %ld");
64         TEST2(i, c-s, 1, "wrong final position %ld != %ld");
65
66         TEST(l, strtol(s="0x1234", &c, 16), 0x1234, "%ld != %ld");
67         TEST2(i, c-s, 6, "wrong final position %ld != %ld");
68
69         c = NULL;
70         TEST(l, strtol(s="123", &c, 37), 0, "%ld != %ld");
71         TEST2(i, c-s, 0, "wrong final position %d != %d");
72         TEST2(i, errno, EINVAL, "%d != %d");
73
74         TEST(l, strtol(s="  15437", &c, 8), 015437, "%ld != %ld");
75         TEST2(i, c-s, 7, "wrong final position %d != %d");
76
77         TEST(l, strtol(s="  1", &c, 0), 1, "%ld != %ld");
78         TEST2(i, c-s, 3, "wrong final position %d != %d");
79         return t_status;
80 }