2
0

util.c 8.5 KB

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