1 #include "test/jemalloc_test.h"
3 #define TEST_POW2_CEIL(t, suf, pri) do { \
7 assert_zu_eq(pow2_ceil_##suf(0), 0, "Unexpected result"); \
9 for (i = 0; i < sizeof(t) * 8; i++) { \
10 assert_zu_eq(pow2_ceil_##suf(((t)1) << i), ((t)1) << i, \
11 "Unexpected result"); \
14 for (i = 2; i < sizeof(t) * 8; i++) { \
15 assert_zu_eq(pow2_ceil_##suf((((t)1) << i) - 1), \
16 ((t)1) << i, "Unexpected result"); \
19 for (i = 0; i < sizeof(t) * 8 - 1; i++) { \
20 assert_zu_eq(pow2_ceil_##suf((((t)1) << i) + 1), \
21 ((t)1) << (i+1), "Unexpected result"); \
24 for (pow2 = 1; pow2 < 25; pow2++) { \
25 for (x = (((t)1) << (pow2-1)) + 1; x <= ((t)1) << pow2; \
27 assert_zu_eq(pow2_ceil_##suf(x), \
29 "Unexpected result, x=%"pri, x); \
34 TEST_BEGIN(test_pow2_ceil_u64
)
37 TEST_POW2_CEIL(uint64_t, u64
, FMTu64
);
41 TEST_BEGIN(test_pow2_ceil_u32
)
44 TEST_POW2_CEIL(uint32_t, u32
, FMTu32
);
48 TEST_BEGIN(test_pow2_ceil_zu
)
51 TEST_POW2_CEIL(size_t, zu
, "zu");
55 TEST_BEGIN(test_malloc_strtoumax_no_endptr
)
60 assert_ju_eq(malloc_strtoumax("0", NULL
, 0), 0, "Unexpected result");
62 assert_d_eq(err
, 0, "Unexpected failure");
66 TEST_BEGIN(test_malloc_strtoumax
)
70 const char *expected_remainder
;
73 const char *expected_errno_name
;
77 #define KUMAX(x) ((uintmax_t)x##ULL)
78 struct test_s tests
[] = {
79 {"0", "0", -1, ERR(EINVAL
), UINTMAX_MAX
},
80 {"0", "0", 1, ERR(EINVAL
), UINTMAX_MAX
},
81 {"0", "0", 37, ERR(EINVAL
), UINTMAX_MAX
},
83 {"", "", 0, ERR(EINVAL
), UINTMAX_MAX
},
84 {"+", "+", 0, ERR(EINVAL
), UINTMAX_MAX
},
85 {"++3", "++3", 0, ERR(EINVAL
), UINTMAX_MAX
},
86 {"-", "-", 0, ERR(EINVAL
), UINTMAX_MAX
},
88 {"42", "", 0, ERR(0), KUMAX(42)},
89 {"+42", "", 0, ERR(0), KUMAX(42)},
90 {"-42", "", 0, ERR(0), KUMAX(-42)},
91 {"042", "", 0, ERR(0), KUMAX(042)},
92 {"+042", "", 0, ERR(0), KUMAX(042)},
93 {"-042", "", 0, ERR(0), KUMAX(-042)},
94 {"0x42", "", 0, ERR(0), KUMAX(0x42)},
95 {"+0x42", "", 0, ERR(0), KUMAX(0x42)},
96 {"-0x42", "", 0, ERR(0), KUMAX(-0x42)},
98 {"0", "", 0, ERR(0), KUMAX(0)},
99 {"1", "", 0, ERR(0), KUMAX(1)},
101 {"42", "", 0, ERR(0), KUMAX(42)},
102 {" 42", "", 0, ERR(0), KUMAX(42)},
103 {"42 ", " ", 0, ERR(0), KUMAX(42)},
104 {"0x", "x", 0, ERR(0), KUMAX(0)},
105 {"42x", "x", 0, ERR(0), KUMAX(42)},
107 {"07", "", 0, ERR(0), KUMAX(7)},
108 {"010", "", 0, ERR(0), KUMAX(8)},
109 {"08", "8", 0, ERR(0), KUMAX(0)},
110 {"0_", "_", 0, ERR(0), KUMAX(0)},
112 {"0x", "x", 0, ERR(0), KUMAX(0)},
113 {"0X", "X", 0, ERR(0), KUMAX(0)},
114 {"0xg", "xg", 0, ERR(0), KUMAX(0)},
115 {"0XA", "", 0, ERR(0), KUMAX(10)},
117 {"010", "", 10, ERR(0), KUMAX(10)},
118 {"0x3", "x3", 10, ERR(0), KUMAX(0)},
120 {"12", "2", 2, ERR(0), KUMAX(1)},
121 {"78", "8", 8, ERR(0), KUMAX(7)},
122 {"9a", "a", 10, ERR(0), KUMAX(9)},
123 {"9A", "A", 10, ERR(0), KUMAX(9)},
124 {"fg", "g", 16, ERR(0), KUMAX(15)},
125 {"FG", "G", 16, ERR(0), KUMAX(15)},
126 {"0xfg", "g", 16, ERR(0), KUMAX(15)},
127 {"0XFG", "G", 16, ERR(0), KUMAX(15)},
128 {"z_", "_", 36, ERR(0), KUMAX(35)},
129 {"Z_", "_", 36, ERR(0), KUMAX(35)}
135 for (i
= 0; i
< sizeof(tests
)/sizeof(struct test_s
); i
++) {
136 struct test_s
*test
= &tests
[i
];
142 result
= malloc_strtoumax(test
->input
, &remainder
, test
->base
);
144 assert_d_eq(err
, test
->expected_errno
,
145 "Expected errno %s for \"%s\", base %d",
146 test
->expected_errno_name
, test
->input
, test
->base
);
147 assert_str_eq(remainder
, test
->expected_remainder
,
148 "Unexpected remainder for \"%s\", base %d",
149 test
->input
, test
->base
);
151 assert_ju_eq(result
, test
->expected_x
,
152 "Unexpected result for \"%s\", base %d",
153 test
->input
, test
->base
);
159 TEST_BEGIN(test_malloc_snprintf_truncated
)
165 #define TEST(expected_str_untruncated, ...) do { \
166 result = malloc_snprintf(buf, len, __VA_ARGS__); \
167 assert_d_eq(strncmp(buf, expected_str_untruncated, len-1), 0, \
168 "Unexpected string inequality (\"%s\" vs \"%s\")", \
169 buf, expected_str_untruncated); \
170 assert_d_eq(result, strlen(expected_str_untruncated), \
171 "Unexpected result"); \
174 for (len
= 1; len
< BUFLEN
; len
++) {
175 TEST("012346789", "012346789");
176 TEST("a0123b", "a%sb", "0123");
177 TEST("a01234567", "a%s%s", "0123", "4567");
178 TEST("a0123 ", "a%-6s", "0123");
179 TEST("a 0123", "a%6s", "0123");
180 TEST("a 012", "a%6.3s", "0123");
181 TEST("a 012", "a%*.*s", 6, 3, "0123");
182 TEST("a 123b", "a% db", 123);
183 TEST("a123b", "a%-db", 123);
184 TEST("a-123b", "a%-db", -123);
185 TEST("a+123b", "a%+db", 123);
192 TEST_BEGIN(test_malloc_snprintf
)
197 #define TEST(expected_str, ...) do { \
198 result = malloc_snprintf(buf, sizeof(buf), __VA_ARGS__); \
199 assert_str_eq(buf, expected_str, "Unexpected output"); \
200 assert_d_eq(result, strlen(expected_str), "Unexpected result"); \
203 TEST("hello", "hello");
205 TEST("50%, 100%", "50%%, %d%%", 100);
207 TEST("a0123b", "a%sb", "0123");
209 TEST("a 0123b", "a%5sb", "0123");
210 TEST("a 0123b", "a%*sb", 5, "0123");
212 TEST("a0123 b", "a%-5sb", "0123");
213 TEST("a0123b", "a%*sb", -1, "0123");
214 TEST("a0123 b", "a%*sb", -5, "0123");
215 TEST("a0123 b", "a%-*sb", -5, "0123");
217 TEST("a012b", "a%.3sb", "0123");
218 TEST("a012b", "a%.*sb", 3, "0123");
219 TEST("a0123b", "a%.*sb", -3, "0123");
221 TEST("a 012b", "a%5.3sb", "0123");
222 TEST("a 012b", "a%5.*sb", 3, "0123");
223 TEST("a 012b", "a%*.3sb", 5, "0123");
224 TEST("a 012b", "a%*.*sb", 5, 3, "0123");
225 TEST("a 0123b", "a%*.*sb", 5, -3, "0123");
227 TEST("_abcd_", "_%x_", 0xabcd);
228 TEST("_0xabcd_", "_%#x_", 0xabcd);
229 TEST("_1234_", "_%o_", 01234);
230 TEST("_01234_", "_%#o_", 01234);
231 TEST("_1234_", "_%u_", 1234);
233 TEST("_1234_", "_%d_", 1234);
234 TEST("_ 1234_", "_% d_", 1234);
235 TEST("_+1234_", "_%+d_", 1234);
236 TEST("_-1234_", "_%d_", -1234);
237 TEST("_-1234_", "_% d_", -1234);
238 TEST("_-1234_", "_%+d_", -1234);
240 TEST("_-1234_", "_%d_", -1234);
241 TEST("_1234_", "_%d_", 1234);
242 TEST("_-1234_", "_%i_", -1234);
243 TEST("_1234_", "_%i_", 1234);
244 TEST("_01234_", "_%#o_", 01234);
245 TEST("_1234_", "_%u_", 1234);
246 TEST("_0x1234abc_", "_%#x_", 0x1234abc);
247 TEST("_0X1234ABC_", "_%#X_", 0x1234abc);
248 TEST("_c_", "_%c_", 'c');
249 TEST("_string_", "_%s_", "string");
250 TEST("_0x42_", "_%p_", ((void *)0x42));
252 TEST("_-1234_", "_%ld_", ((long)-1234));
253 TEST("_1234_", "_%ld_", ((long)1234));
254 TEST("_-1234_", "_%li_", ((long)-1234));
255 TEST("_1234_", "_%li_", ((long)1234));
256 TEST("_01234_", "_%#lo_", ((long)01234));
257 TEST("_1234_", "_%lu_", ((long)1234));
258 TEST("_0x1234abc_", "_%#lx_", ((long)0x1234abc));
259 TEST("_0X1234ABC_", "_%#lX_", ((long)0x1234ABC));
261 TEST("_-1234_", "_%lld_", ((long long)-1234));
262 TEST("_1234_", "_%lld_", ((long long)1234));
263 TEST("_-1234_", "_%lli_", ((long long)-1234));
264 TEST("_1234_", "_%lli_", ((long long)1234));
265 TEST("_01234_", "_%#llo_", ((long long)01234));
266 TEST("_1234_", "_%llu_", ((long long)1234));
267 TEST("_0x1234abc_", "_%#llx_", ((long long)0x1234abc));
268 TEST("_0X1234ABC_", "_%#llX_", ((long long)0x1234ABC));
270 TEST("_-1234_", "_%qd_", ((long long)-1234));
271 TEST("_1234_", "_%qd_", ((long long)1234));
272 TEST("_-1234_", "_%qi_", ((long long)-1234));
273 TEST("_1234_", "_%qi_", ((long long)1234));
274 TEST("_01234_", "_%#qo_", ((long long)01234));
275 TEST("_1234_", "_%qu_", ((long long)1234));
276 TEST("_0x1234abc_", "_%#qx_", ((long long)0x1234abc));
277 TEST("_0X1234ABC_", "_%#qX_", ((long long)0x1234ABC));
279 TEST("_-1234_", "_%jd_", ((intmax_t)-1234));
280 TEST("_1234_", "_%jd_", ((intmax_t)1234));
281 TEST("_-1234_", "_%ji_", ((intmax_t)-1234));
282 TEST("_1234_", "_%ji_", ((intmax_t)1234));
283 TEST("_01234_", "_%#jo_", ((intmax_t)01234));
284 TEST("_1234_", "_%ju_", ((intmax_t)1234));
285 TEST("_0x1234abc_", "_%#jx_", ((intmax_t)0x1234abc));
286 TEST("_0X1234ABC_", "_%#jX_", ((intmax_t)0x1234ABC));
288 TEST("_1234_", "_%td_", ((ptrdiff_t)1234));
289 TEST("_-1234_", "_%td_", ((ptrdiff_t)-1234));
290 TEST("_1234_", "_%ti_", ((ptrdiff_t)1234));
291 TEST("_-1234_", "_%ti_", ((ptrdiff_t)-1234));
293 TEST("_-1234_", "_%zd_", ((ssize_t
)-1234));
294 TEST("_1234_", "_%zd_", ((ssize_t
)1234));
295 TEST("_-1234_", "_%zi_", ((ssize_t
)-1234));
296 TEST("_1234_", "_%zi_", ((ssize_t
)1234));
297 TEST("_01234_", "_%#zo_", ((ssize_t
)01234));
298 TEST("_1234_", "_%zu_", ((ssize_t
)1234));
299 TEST("_0x1234abc_", "_%#zx_", ((ssize_t
)0x1234abc));
300 TEST("_0X1234ABC_", "_%#zX_", ((ssize_t
)0x1234ABC));
313 test_malloc_strtoumax_no_endptr
,
314 test_malloc_strtoumax
,
315 test_malloc_snprintf_truncated
,
316 test_malloc_snprintf
));