7d9213fe14e467ae9f1a0f20d13118cf081701fb
[libc-test] / src / stdlib / 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         (error("%s failed (" m ")\n", #f, r, x), 0) )
16
17 #define TEST2(r, f, x, m) ( \
18         ((r) = (f)) == (x) || \
19         (error("%s failed (" m ")\n", msg, r, x), 0) )
20
21 void test_strtol(void) {
22         int i;
23         long l;
24         unsigned long ul;
25         char *msg="";
26         char *s, *c;
27
28         TEST(l, atol("2147483647"), 2147483647L, "max 32bit signed %ld != %ld");
29         TEST(l, strtol("2147483647", 0, 0), 2147483647L, "max 32bit signed %ld != %ld");
30         TEST(ul, strtoul("4294967295", 0, 0), 4294967295UL, "max 32bit unsigned %lu != %lu");
31
32         if (sizeof(long) == 4) {
33                 TEST(l, strtol(s="2147483648", &c, 0), 2147483647L, "uncaught overflow %ld != %ld");
34                 TEST2(i, c-s, 10, "wrong final position %d != %d");
35                 TEST2(i, errno, ERANGE, "missing errno %d != %d");
36                 TEST(l, strtol(s="-2147483649", &c, 0), -2147483647L-1, "uncaught overflow %ld != %ld");
37                 TEST2(i, c-s, 11, "wrong final position %d != %d");
38                 TEST2(i, errno, ERANGE, "missing errno %d != %d");
39                 TEST(ul, strtoul(s="4294967296", &c, 0), 4294967295UL, "uncaught overflow %lu != %lu");
40                 TEST2(i, c-s, 10, "wrong final position %d != %d");
41                 TEST2(i, errno, ERANGE, "missing errno %d != %d");
42                 TEST(ul, strtoul(s="-1", &c, 0), 4294967295UL, "uncaught overflow %lu != %lu");
43                 TEST2(i, c-s, 2, "wrong final position %d != %d");
44                 TEST2(i, errno, ERANGE, "missing errno %d != %d");
45                 TEST(ul, strtoul(s="-2", &c, 0), 4294967295UL, "uncaught overflow %lu != %lu");
46                 TEST2(i, c-s, 2, "wrong final position %d != %d");
47                 TEST2(i, errno, ERANGE, "missing errno %d != %d");
48                 TEST(ul, strtoul(s="-2147483648", &c, 0), 4294967295UL, "uncaught overflow %lu != %lu");
49                 TEST2(i, c-s, 11, "wrong final position %d != %d");
50                 TEST2(i, errno, ERANGE, "missing errno %d != %d");
51                 TEST(ul, strtoul(s="-2147483649", &c, 0), 4294967295UL, "uncaught overflow %lu != %lu");
52                 TEST2(i, c-s, 11, "wrong final position %d != %d");
53                 TEST2(i, errno, ERANGE, "missing errno %d != %d");
54         } else {
55                 TEST(i, 0, 1, "64bit tests not implemented");
56         }
57
58         TEST(l, strtol("z", 0, 36), 35, "%ld != %ld");
59         TEST(l, strtol("00010010001101000101011001111000", 0, 2), 0x12345678, "%ld != %ld");
60         TEST(l, strtol(s="0F5F", &c, 16), 0x0f5f, "%ld != %ld");
61
62         TEST(l, strtol(s="0xz", &c, 16), 0, "%ld != %ld");
63         TEST2(i, c-s, 1, "wrong final position %ld != %ld");
64
65         TEST(l, strtol(s="0x1234", &c, 16), 0x1234, "%ld != %ld");
66         TEST2(i, c-s, 6, "wrong final position %ld != %ld");
67
68         c = NULL;
69         TEST(l, strtol(s="123", &c, 37), 0, "%ld != %ld");
70         TEST2(i, c-s, 0, "wrong final position %d != %d");
71         TEST2(i, errno, EINVAL, "%d != %d");
72 }