1 #include "test/jemalloc_test.h"
2
TEST_BEGIN(test_malloc_strtoumax_no_endptr)3 TEST_BEGIN(test_malloc_strtoumax_no_endptr) {
4 int err;
5
6 set_errno(0);
7 assert_ju_eq(malloc_strtoumax("0", NULL, 0), 0, "Unexpected result");
8 err = get_errno();
9 assert_d_eq(err, 0, "Unexpected failure");
10 }
11 TEST_END
12
TEST_BEGIN(test_malloc_strtoumax)13 TEST_BEGIN(test_malloc_strtoumax) {
14 struct test_s {
15 const char *input;
16 const char *expected_remainder;
17 int base;
18 int expected_errno;
19 const char *expected_errno_name;
20 uintmax_t expected_x;
21 };
22 #define ERR(e) e, #e
23 #define KUMAX(x) ((uintmax_t)x##ULL)
24 #define KSMAX(x) ((uintmax_t)(intmax_t)x##LL)
25 struct test_s tests[] = {
26 {"0", "0", -1, ERR(EINVAL), UINTMAX_MAX},
27 {"0", "0", 1, ERR(EINVAL), UINTMAX_MAX},
28 {"0", "0", 37, ERR(EINVAL), UINTMAX_MAX},
29
30 {"", "", 0, ERR(EINVAL), UINTMAX_MAX},
31 {"+", "+", 0, ERR(EINVAL), UINTMAX_MAX},
32 {"++3", "++3", 0, ERR(EINVAL), UINTMAX_MAX},
33 {"-", "-", 0, ERR(EINVAL), UINTMAX_MAX},
34
35 {"42", "", 0, ERR(0), KUMAX(42)},
36 {"+42", "", 0, ERR(0), KUMAX(42)},
37 {"-42", "", 0, ERR(0), KSMAX(-42)},
38 {"042", "", 0, ERR(0), KUMAX(042)},
39 {"+042", "", 0, ERR(0), KUMAX(042)},
40 {"-042", "", 0, ERR(0), KSMAX(-042)},
41 {"0x42", "", 0, ERR(0), KUMAX(0x42)},
42 {"+0x42", "", 0, ERR(0), KUMAX(0x42)},
43 {"-0x42", "", 0, ERR(0), KSMAX(-0x42)},
44
45 {"0", "", 0, ERR(0), KUMAX(0)},
46 {"1", "", 0, ERR(0), KUMAX(1)},
47
48 {"42", "", 0, ERR(0), KUMAX(42)},
49 {" 42", "", 0, ERR(0), KUMAX(42)},
50 {"42 ", " ", 0, ERR(0), KUMAX(42)},
51 {"0x", "x", 0, ERR(0), KUMAX(0)},
52 {"42x", "x", 0, ERR(0), KUMAX(42)},
53
54 {"07", "", 0, ERR(0), KUMAX(7)},
55 {"010", "", 0, ERR(0), KUMAX(8)},
56 {"08", "8", 0, ERR(0), KUMAX(0)},
57 {"0_", "_", 0, ERR(0), KUMAX(0)},
58
59 {"0x", "x", 0, ERR(0), KUMAX(0)},
60 {"0X", "X", 0, ERR(0), KUMAX(0)},
61 {"0xg", "xg", 0, ERR(0), KUMAX(0)},
62 {"0XA", "", 0, ERR(0), KUMAX(10)},
63
64 {"010", "", 10, ERR(0), KUMAX(10)},
65 {"0x3", "x3", 10, ERR(0), KUMAX(0)},
66
67 {"12", "2", 2, ERR(0), KUMAX(1)},
68 {"78", "8", 8, ERR(0), KUMAX(7)},
69 {"9a", "a", 10, ERR(0), KUMAX(9)},
70 {"9A", "A", 10, ERR(0), KUMAX(9)},
71 {"fg", "g", 16, ERR(0), KUMAX(15)},
72 {"FG", "G", 16, ERR(0), KUMAX(15)},
73 {"0xfg", "g", 16, ERR(0), KUMAX(15)},
74 {"0XFG", "G", 16, ERR(0), KUMAX(15)},
75 {"z_", "_", 36, ERR(0), KUMAX(35)},
76 {"Z_", "_", 36, ERR(0), KUMAX(35)}
77 };
78 #undef ERR
79 #undef KUMAX
80 #undef KSMAX
81 unsigned i;
82
83 for (i = 0; i < sizeof(tests)/sizeof(struct test_s); i++) {
84 struct test_s *test = &tests[i];
85 int err;
86 uintmax_t result;
87 char *remainder;
88
89 set_errno(0);
90 result = malloc_strtoumax(test->input, &remainder, test->base);
91 err = get_errno();
92 assert_d_eq(err, test->expected_errno,
93 "Expected errno %s for \"%s\", base %d",
94 test->expected_errno_name, test->input, test->base);
95 assert_str_eq(remainder, test->expected_remainder,
96 "Unexpected remainder for \"%s\", base %d",
97 test->input, test->base);
98 if (err == 0) {
99 assert_ju_eq(result, test->expected_x,
100 "Unexpected result for \"%s\", base %d",
101 test->input, test->base);
102 }
103 }
104 }
105 TEST_END
106
TEST_BEGIN(test_malloc_snprintf_truncated)107 TEST_BEGIN(test_malloc_snprintf_truncated) {
108 #define BUFLEN 15
109 char buf[BUFLEN];
110 size_t result;
111 size_t len;
112 #define TEST(expected_str_untruncated, ...) do { \
113 result = malloc_snprintf(buf, len, __VA_ARGS__); \
114 assert_d_eq(strncmp(buf, expected_str_untruncated, len-1), 0, \
115 "Unexpected string inequality (\"%s\" vs \"%s\")", \
116 buf, expected_str_untruncated); \
117 assert_zu_eq(result, strlen(expected_str_untruncated), \
118 "Unexpected result"); \
119 } while (0)
120
121 for (len = 1; len < BUFLEN; len++) {
122 TEST("012346789", "012346789");
123 TEST("a0123b", "a%sb", "0123");
124 TEST("a01234567", "a%s%s", "0123", "4567");
125 TEST("a0123 ", "a%-6s", "0123");
126 TEST("a 0123", "a%6s", "0123");
127 TEST("a 012", "a%6.3s", "0123");
128 TEST("a 012", "a%*.*s", 6, 3, "0123");
129 TEST("a 123b", "a% db", 123);
130 TEST("a123b", "a%-db", 123);
131 TEST("a-123b", "a%-db", -123);
132 TEST("a+123b", "a%+db", 123);
133 }
134 #undef BUFLEN
135 #undef TEST
136 }
137 TEST_END
138
TEST_BEGIN(test_malloc_snprintf)139 TEST_BEGIN(test_malloc_snprintf) {
140 #define BUFLEN 128
141 char buf[BUFLEN];
142 size_t result;
143 #define TEST(expected_str, ...) do { \
144 result = malloc_snprintf(buf, sizeof(buf), __VA_ARGS__); \
145 assert_str_eq(buf, expected_str, "Unexpected output"); \
146 assert_zu_eq(result, strlen(expected_str), "Unexpected result");\
147 } while (0)
148
149 TEST("hello", "hello");
150
151 TEST("50%, 100%", "50%%, %d%%", 100);
152
153 TEST("a0123b", "a%sb", "0123");
154
155 TEST("a 0123b", "a%5sb", "0123");
156 TEST("a 0123b", "a%*sb", 5, "0123");
157
158 TEST("a0123 b", "a%-5sb", "0123");
159 TEST("a0123b", "a%*sb", -1, "0123");
160 TEST("a0123 b", "a%*sb", -5, "0123");
161 TEST("a0123 b", "a%-*sb", -5, "0123");
162
163 TEST("a012b", "a%.3sb", "0123");
164 TEST("a012b", "a%.*sb", 3, "0123");
165 TEST("a0123b", "a%.*sb", -3, "0123");
166
167 TEST("a 012b", "a%5.3sb", "0123");
168 TEST("a 012b", "a%5.*sb", 3, "0123");
169 TEST("a 012b", "a%*.3sb", 5, "0123");
170 TEST("a 012b", "a%*.*sb", 5, 3, "0123");
171 TEST("a 0123b", "a%*.*sb", 5, -3, "0123");
172
173 TEST("_abcd_", "_%x_", 0xabcd);
174 TEST("_0xabcd_", "_%#x_", 0xabcd);
175 TEST("_1234_", "_%o_", 01234);
176 TEST("_01234_", "_%#o_", 01234);
177 TEST("_1234_", "_%u_", 1234);
178
179 TEST("_1234_", "_%d_", 1234);
180 TEST("_ 1234_", "_% d_", 1234);
181 TEST("_+1234_", "_%+d_", 1234);
182 TEST("_-1234_", "_%d_", -1234);
183 TEST("_-1234_", "_% d_", -1234);
184 TEST("_-1234_", "_%+d_", -1234);
185
186 TEST("_-1234_", "_%d_", -1234);
187 TEST("_1234_", "_%d_", 1234);
188 TEST("_-1234_", "_%i_", -1234);
189 TEST("_1234_", "_%i_", 1234);
190 TEST("_01234_", "_%#o_", 01234);
191 TEST("_1234_", "_%u_", 1234);
192 TEST("_0x1234abc_", "_%#x_", 0x1234abc);
193 TEST("_0X1234ABC_", "_%#X_", 0x1234abc);
194 TEST("_c_", "_%c_", 'c');
195 TEST("_string_", "_%s_", "string");
196 TEST("_0x42_", "_%p_", ((void *)0x42));
197
198 TEST("_-1234_", "_%ld_", ((long)-1234));
199 TEST("_1234_", "_%ld_", ((long)1234));
200 TEST("_-1234_", "_%li_", ((long)-1234));
201 TEST("_1234_", "_%li_", ((long)1234));
202 TEST("_01234_", "_%#lo_", ((long)01234));
203 TEST("_1234_", "_%lu_", ((long)1234));
204 TEST("_0x1234abc_", "_%#lx_", ((long)0x1234abc));
205 TEST("_0X1234ABC_", "_%#lX_", ((long)0x1234ABC));
206
207 TEST("_-1234_", "_%lld_", ((long long)-1234));
208 TEST("_1234_", "_%lld_", ((long long)1234));
209 TEST("_-1234_", "_%lli_", ((long long)-1234));
210 TEST("_1234_", "_%lli_", ((long long)1234));
211 TEST("_01234_", "_%#llo_", ((long long)01234));
212 TEST("_1234_", "_%llu_", ((long long)1234));
213 TEST("_0x1234abc_", "_%#llx_", ((long long)0x1234abc));
214 TEST("_0X1234ABC_", "_%#llX_", ((long long)0x1234ABC));
215
216 TEST("_-1234_", "_%qd_", ((long long)-1234));
217 TEST("_1234_", "_%qd_", ((long long)1234));
218 TEST("_-1234_", "_%qi_", ((long long)-1234));
219 TEST("_1234_", "_%qi_", ((long long)1234));
220 TEST("_01234_", "_%#qo_", ((long long)01234));
221 TEST("_1234_", "_%qu_", ((long long)1234));
222 TEST("_0x1234abc_", "_%#qx_", ((long long)0x1234abc));
223 TEST("_0X1234ABC_", "_%#qX_", ((long long)0x1234ABC));
224
225 TEST("_-1234_", "_%jd_", ((intmax_t)-1234));
226 TEST("_1234_", "_%jd_", ((intmax_t)1234));
227 TEST("_-1234_", "_%ji_", ((intmax_t)-1234));
228 TEST("_1234_", "_%ji_", ((intmax_t)1234));
229 TEST("_01234_", "_%#jo_", ((intmax_t)01234));
230 TEST("_1234_", "_%ju_", ((intmax_t)1234));
231 TEST("_0x1234abc_", "_%#jx_", ((intmax_t)0x1234abc));
232 TEST("_0X1234ABC_", "_%#jX_", ((intmax_t)0x1234ABC));
233
234 TEST("_1234_", "_%td_", ((ptrdiff_t)1234));
235 TEST("_-1234_", "_%td_", ((ptrdiff_t)-1234));
236 TEST("_1234_", "_%ti_", ((ptrdiff_t)1234));
237 TEST("_-1234_", "_%ti_", ((ptrdiff_t)-1234));
238
239 TEST("_-1234_", "_%zd_", ((ssize_t)-1234));
240 TEST("_1234_", "_%zd_", ((ssize_t)1234));
241 TEST("_-1234_", "_%zi_", ((ssize_t)-1234));
242 TEST("_1234_", "_%zi_", ((ssize_t)1234));
243 TEST("_01234_", "_%#zo_", ((ssize_t)01234));
244 TEST("_1234_", "_%zu_", ((ssize_t)1234));
245 TEST("_0x1234abc_", "_%#zx_", ((ssize_t)0x1234abc));
246 TEST("_0X1234ABC_", "_%#zX_", ((ssize_t)0x1234ABC));
247 #undef BUFLEN
248 }
249 TEST_END
250
251 int
main(void)252 main(void) {
253 return test(
254 test_malloc_strtoumax_no_endptr,
255 test_malloc_strtoumax,
256 test_malloc_snprintf_truncated,
257 test_malloc_snprintf);
258 }
259