2
0

malloc_io.c 7.8 KB

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