• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include "test/jemalloc_test.h"
2 
3 #define	TEST_POW2_CEIL(t, suf, pri) do {				\
4 	unsigned i, pow2;						\
5 	t x;								\
6 									\
7 	assert_zu_eq(pow2_ceil_##suf(0), 0, "Unexpected result");	\
8 									\
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");				\
12 	}								\
13 									\
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");			\
17 	}								\
18 									\
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");		\
22 	}								\
23 									\
24 	for (pow2 = 1; pow2 < 25; pow2++) {				\
25 		for (x = (((t)1) << (pow2-1)) + 1; x <= ((t)1) << pow2;	\
26 		    x++) {						\
27 			assert_zu_eq(pow2_ceil_##suf(x),		\
28 			    ((t)1) << pow2,				\
29 			    "Unexpected result, x=%"pri, x);		\
30 		}							\
31 	}								\
32 } while (0)
33 
TEST_BEGIN(test_pow2_ceil_u64)34 TEST_BEGIN(test_pow2_ceil_u64)
35 {
36 
37 	TEST_POW2_CEIL(uint64_t, u64, FMTu64);
38 }
39 TEST_END
40 
TEST_BEGIN(test_pow2_ceil_u32)41 TEST_BEGIN(test_pow2_ceil_u32)
42 {
43 
44 	TEST_POW2_CEIL(uint32_t, u32, FMTu32);
45 }
46 TEST_END
47 
TEST_BEGIN(test_pow2_ceil_zu)48 TEST_BEGIN(test_pow2_ceil_zu)
49 {
50 
51 	TEST_POW2_CEIL(size_t, zu, "zu");
52 }
53 TEST_END
54 
TEST_BEGIN(test_malloc_strtoumax_no_endptr)55 TEST_BEGIN(test_malloc_strtoumax_no_endptr)
56 {
57 	int err;
58 
59 	set_errno(0);
60 	assert_ju_eq(malloc_strtoumax("0", NULL, 0), 0, "Unexpected result");
61 	err = get_errno();
62 	assert_d_eq(err, 0, "Unexpected failure");
63 }
64 TEST_END
65 
TEST_BEGIN(test_malloc_strtoumax)66 TEST_BEGIN(test_malloc_strtoumax)
67 {
68 	struct test_s {
69 		const char *input;
70 		const char *expected_remainder;
71 		int base;
72 		int expected_errno;
73 		const char *expected_errno_name;
74 		uintmax_t expected_x;
75 	};
76 #define	ERR(e)		e, #e
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},
82 
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},
87 
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)},
97 
98 		{"0",		"",	0,	ERR(0),		KUMAX(0)},
99 		{"1",		"",	0,	ERR(0),		KUMAX(1)},
100 
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)},
106 
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)},
111 
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)},
116 
117 		{"010",		"",	10,	ERR(0),		KUMAX(10)},
118 		{"0x3",		"x3",	10,	ERR(0),		KUMAX(0)},
119 
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)}
130 	};
131 #undef ERR
132 #undef KUMAX
133 	unsigned i;
134 
135 	for (i = 0; i < sizeof(tests)/sizeof(struct test_s); i++) {
136 		struct test_s *test = &tests[i];
137 		int err;
138 		uintmax_t result;
139 		char *remainder;
140 
141 		set_errno(0);
142 		result = malloc_strtoumax(test->input, &remainder, test->base);
143 		err = get_errno();
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);
150 		if (err == 0) {
151 			assert_ju_eq(result, test->expected_x,
152 			    "Unexpected result for \"%s\", base %d",
153 			    test->input, test->base);
154 		}
155 	}
156 }
157 TEST_END
158 
TEST_BEGIN(test_malloc_snprintf_truncated)159 TEST_BEGIN(test_malloc_snprintf_truncated)
160 {
161 #define	BUFLEN	15
162 	char buf[BUFLEN];
163 	int result;
164 	size_t len;
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");					\
172 } while (0)
173 
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);
186 	}
187 #undef BUFLEN
188 #undef TEST
189 }
190 TEST_END
191 
TEST_BEGIN(test_malloc_snprintf)192 TEST_BEGIN(test_malloc_snprintf)
193 {
194 #define	BUFLEN	128
195 	char buf[BUFLEN];
196 	int result;
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");	\
201 } while (0)
202 
203 	TEST("hello", "hello");
204 
205 	TEST("50%, 100%", "50%%, %d%%", 100);
206 
207 	TEST("a0123b", "a%sb", "0123");
208 
209 	TEST("a 0123b", "a%5sb", "0123");
210 	TEST("a 0123b", "a%*sb", 5, "0123");
211 
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");
216 
217 	TEST("a012b", "a%.3sb", "0123");
218 	TEST("a012b", "a%.*sb", 3, "0123");
219 	TEST("a0123b", "a%.*sb", -3, "0123");
220 
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");
226 
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);
232 
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);
239 
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));
251 
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));
260 
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));
269 
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));
278 
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));
287 
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));
292 
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));
301 #undef BUFLEN
302 }
303 TEST_END
304 
305 int
main(void)306 main(void)
307 {
308 
309 	return (test(
310 	    test_pow2_ceil_u64,
311 	    test_pow2_ceil_u32,
312 	    test_pow2_ceil_zu,
313 	    test_malloc_strtoumax_no_endptr,
314 	    test_malloc_strtoumax,
315 	    test_malloc_snprintf_truncated,
316 	    test_malloc_snprintf));
317 }
318