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