1 #include <inttypes.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <string.h>
5
6 #define TEST(r, f, x, m) uassert_int_equal(f, x)
7 #define TEST2(r, f, x, m) uassert_int_equal(f, x)
8 #define TEST3(r, f, x, m) uassert_int_not_equal(f, x)
9
10 #include <utest.h>
11 #define ERANGE 34
12 #define EINVAL 22
13
strtol_entry(void)14 int strtol_entry(void)
15 {
16 __attribute__((unused)) int i;
17 __attribute__((unused)) long l;
18 __attribute__((unused)) unsigned long ul;
19 __attribute__((unused)) long long ll;
20 __attribute__((unused)) unsigned long long ull;
21 __attribute__((unused)) char *msg = "";
22 __attribute__((unused)) char *s, *c;
23
24 TEST(l, atol("2147483647"), 2147483647L, "max 32bit signed %ld != %ld");
25 TEST(l, strtol("2147483647", 0, 0), 2147483647L, "max 32bit signed %ld != %ld");
26 TEST(ul, strtoul("4294967295", 0, 0), 4294967295UL, "max 32bit unsigned %lu != %lu");
27
28 if (sizeof(long) == 4)
29 {
30 TEST(l, strtol(s = "2147483648", &c, 0), 2147483647L, "uncaught overflow %ld != %ld");
31 TEST2(i, c - s, 10, "wrong final position %d != %d");
32 TEST(l, strtol(s = "-2147483649", &c, 0), -2147483647L - 1, "uncaught overflow %ld != %ld");
33 TEST2(i, c - s, 11, "wrong final position %d != %d");
34 TEST(ul, strtoul(s = "4294967296", &c, 0), 4294967295UL, "uncaught overflow %lu != %lu");
35 TEST2(i, c - s, 10, "wrong final position %d != %d");
36 TEST(ul, strtoul(s = "-1", &c, 0), -1UL, "rejected negative %lu != %lu");
37 TEST2(i, c - s, 2, "wrong final position %d != %d");
38 TEST(ul, strtoul(s = "-2", &c, 0), -2UL, "rejected negative %lu != %lu");
39 TEST2(i, c - s, 2, "wrong final position %d != %d");
40 TEST(ul, strtoul(s = "-2147483648", &c, 0), -2147483648UL, "rejected negative %lu != %lu");
41 TEST2(i, c - s, 11, "wrong final position %d != %d");
42 TEST(ul, strtoul(s = "-2147483649", &c, 0), -2147483649UL, "rejected negative %lu != %lu");
43 TEST2(i, c - s, 11, "wrong final position %d != %d");
44 TEST(ul, strtoul(s = "-4294967296", &c, 0), 4294967295UL, "uncaught negative overflow %lu != %lu");
45 TEST2(i, c - s, 11, "wrong final position %d != %d");
46 }
47 else if (sizeof(long) == 8)
48 {
49 TEST(l, strtol(s = "9223372036854775808", &c, 0), 9223372036854775807L, "uncaught overflow %ld != %ld");
50 TEST2(i, c - s, 19, "wrong final position %d != %d");
51 TEST(l, strtol(s = "-9223372036854775809", &c, 0), -9223372036854775807L - 1, "uncaught overflow %ld != %ld");
52 TEST2(i, c - s, 20, "wrong final position %d != %d");
53 TEST(ul, strtoul(s = "18446744073709551616", &c, 0), 18446744073709551615UL, "uncaught overflow %lu != %lu");
54 TEST2(i, c - s, 20, "wrong final position %d != %d");
55 TEST(ul, strtoul(s = "-1", &c, 0), -1UL, "rejected negative %lu != %lu");
56 TEST2(i, c - s, 2, "wrong final position %d != %d");
57 TEST(ul, strtoul(s = "-2", &c, 0), -2UL, "rejected negative %lu != %lu");
58 TEST2(i, c - s, 2, "wrong final position %d != %d");
59 TEST(ul, strtoul(s = "-9223372036854775808", &c, 0), -9223372036854775808UL, "rejected negative %lu != %lu");
60 TEST2(i, c - s, 20, "wrong final position %d != %d");
61 TEST(ul, strtoul(s = "-9223372036854775809", &c, 0), -9223372036854775809UL, "rejected negative %lu != %lu");
62 TEST2(i, c - s, 20, "wrong final position %d != %d");
63 TEST(ul, strtoul(s = "-18446744073709551616", &c, 0), 18446744073709551615UL, "uncaught negative overflow %lu != %lu");
64 TEST2(i, c - s, 21, "wrong final position %d != %d");
65 }
66 else
67 {
68 printf("sizeof(long) == %d, not implemented\n", (int)sizeof(long));
69 }
70
71 if (sizeof(long long) == 8)
72 {
73 TEST(ll, strtoll(s = "9223372036854775808", &c, 0), 9223372036854775807LL, "uncaught overflow %lld != %lld");
74 TEST2(i, c - s, 19, "wrong final position %d != %d");
75 TEST(ll, strtoll(s = "-9223372036854775809", &c, 0), -9223372036854775807LL - 1, "uncaught overflow %lld != %lld");
76 TEST2(i, c - s, 20, "wrong final position %d != %d");
77 TEST(ull, strtoull(s = "18446744073709551616", &c, 0), 18446744073709551615ULL, "uncaught overflow %llu != %llu");
78 TEST2(i, c - s, 20, "wrong final position %d != %d");
79 TEST(ull, strtoull(s = "-1", &c, 0), -1ULL, "rejected negative %llu != %llu");
80 TEST2(i, c - s, 2, "wrong final position %d != %d");
81 TEST(ull, strtoull(s = "-2", &c, 0), -2ULL, "rejected negative %llu != %llu");
82 TEST2(i, c - s, 2, "wrong final position %d != %d");
83 TEST(ull, strtoull(s = "-9223372036854775808", &c, 0), -9223372036854775808ULL, "rejected negative %llu != %llu");
84 TEST2(i, c - s, 20, "wrong final position %d != %d");
85 TEST(ull, strtoull(s = "-9223372036854775809", &c, 0), -9223372036854775809ULL, "rejected negative %llu != %llu");
86 TEST2(i, c - s, 20, "wrong final position %d != %d");
87 TEST(ull, strtoull(s = "-18446744073709551616", &c, 0), 18446744073709551615ULL, "uncaught negative overflow %llu != %llu");
88 TEST2(i, c - s, 21, "wrong final position %d != %d");
89 }
90 else
91 {
92 printf("sizeof(long long) == %d, not implemented\n", (int)sizeof(long long));
93 }
94
95 TEST(l, strtol("z", 0, 36), 35L, "%ld != %ld");
96 TEST(l, strtol("00010010001101000101011001111000", 0, 2), 0x12345678L, "%ld != %ld");
97 TEST(l, strtol(s = "0F5F", &c, 16), 0x0f5fL, "%ld != %ld");
98
99 TEST(l, strtol(s = "0xz", &c, 16), 0L, "%ld != %ld");
100 TEST2(i, c - s, 1, "wrong final position %ld != %ld");
101
102 TEST(l, strtol(s = "0x1234", &c, 16), 0x1234, "%ld != %ld");
103 TEST2(i, c - s, 6, "wrong final position %ld != %ld");
104
105 c = NULL;
106 TEST3(l, strtol(s = "123", &c, 36), 0, "%ld != %ld");
107 TEST3(i, c - s, 0, "wrong final position %d != %d");
108
109 TEST(l, strtol(s = " 15437", &c, 8), 015437, "%ld != %ld");
110 TEST2(i, c - s, 7, "wrong final position %d != %d");
111
112 TEST(l, strtol(s = " 1", &c, 0), 1, "%ld != %ld");
113 TEST2(i, c - s, 3, "wrong final position %d != %d");
114 return 0;
115 }
test_strtol(void)116 static void test_strtol(void)
117 {
118 strtol_entry();
119 }
testcase(void)120 static void testcase(void)
121 {
122 UTEST_UNIT_RUN(test_strtol);
123 }
124 UTEST_TC_EXPORT(testcase, "posix.stdlib_h.strtol_tc.c", RT_NULL, RT_NULL, 10);
125
126