emitter.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. #include "test/jemalloc_test.h"
  2. #include "jemalloc/internal/emitter.h"
  3. /*
  4. * This is so useful for debugging and feature work, we'll leave printing
  5. * functionality committed but disabled by default.
  6. */
  7. /* Print the text as it will appear. */
  8. static bool print_raw = false;
  9. /* Print the text escaped, so it can be copied back into the test case. */
  10. static bool print_escaped = false;
  11. typedef struct buf_descriptor_s buf_descriptor_t;
  12. struct buf_descriptor_s {
  13. char *buf;
  14. size_t len;
  15. bool mid_quote;
  16. };
  17. /*
  18. * Forwards all writes to the passed-in buf_v (which should be cast from a
  19. * buf_descriptor_t *).
  20. */
  21. static void
  22. forwarding_cb(void *buf_descriptor_v, const char *str) {
  23. buf_descriptor_t *buf_descriptor = (buf_descriptor_t *)buf_descriptor_v;
  24. if (print_raw) {
  25. malloc_printf("%s", str);
  26. }
  27. if (print_escaped) {
  28. const char *it = str;
  29. while (*it != '\0') {
  30. if (!buf_descriptor->mid_quote) {
  31. malloc_printf("\"");
  32. buf_descriptor->mid_quote = true;
  33. }
  34. switch (*it) {
  35. case '\\':
  36. malloc_printf("\\");
  37. break;
  38. case '\"':
  39. malloc_printf("\\\"");
  40. break;
  41. case '\t':
  42. malloc_printf("\\t");
  43. break;
  44. case '\n':
  45. malloc_printf("\\n\"\n");
  46. buf_descriptor->mid_quote = false;
  47. break;
  48. default:
  49. malloc_printf("%c", *it);
  50. }
  51. it++;
  52. }
  53. }
  54. size_t written = malloc_snprintf(buf_descriptor->buf,
  55. buf_descriptor->len, "%s", str);
  56. expect_zu_eq(written, strlen(str), "Buffer overflow!");
  57. buf_descriptor->buf += written;
  58. buf_descriptor->len -= written;
  59. expect_zu_gt(buf_descriptor->len, 0, "Buffer out of space!");
  60. }
  61. static void
  62. expect_emit_output(void (*emit_fn)(emitter_t *),
  63. const char *expected_json_output,
  64. const char *expected_json_compact_output,
  65. const char *expected_table_output) {
  66. emitter_t emitter;
  67. char buf[MALLOC_PRINTF_BUFSIZE];
  68. buf_descriptor_t buf_descriptor;
  69. buf_descriptor.buf = buf;
  70. buf_descriptor.len = MALLOC_PRINTF_BUFSIZE;
  71. buf_descriptor.mid_quote = false;
  72. emitter_init(&emitter, emitter_output_json, &forwarding_cb,
  73. &buf_descriptor);
  74. (*emit_fn)(&emitter);
  75. expect_str_eq(expected_json_output, buf, "json output failure");
  76. buf_descriptor.buf = buf;
  77. buf_descriptor.len = MALLOC_PRINTF_BUFSIZE;
  78. buf_descriptor.mid_quote = false;
  79. emitter_init(&emitter, emitter_output_json_compact, &forwarding_cb,
  80. &buf_descriptor);
  81. (*emit_fn)(&emitter);
  82. expect_str_eq(expected_json_compact_output, buf,
  83. "compact json output failure");
  84. buf_descriptor.buf = buf;
  85. buf_descriptor.len = MALLOC_PRINTF_BUFSIZE;
  86. buf_descriptor.mid_quote = false;
  87. emitter_init(&emitter, emitter_output_table, &forwarding_cb,
  88. &buf_descriptor);
  89. (*emit_fn)(&emitter);
  90. expect_str_eq(expected_table_output, buf, "table output failure");
  91. }
  92. static void
  93. emit_dict(emitter_t *emitter) {
  94. bool b_false = false;
  95. bool b_true = true;
  96. int i_123 = 123;
  97. const char *str = "a string";
  98. emitter_begin(emitter);
  99. emitter_dict_begin(emitter, "foo", "This is the foo table:");
  100. emitter_kv(emitter, "abc", "ABC", emitter_type_bool, &b_false);
  101. emitter_kv(emitter, "def", "DEF", emitter_type_bool, &b_true);
  102. emitter_kv_note(emitter, "ghi", "GHI", emitter_type_int, &i_123,
  103. "note_key1", emitter_type_string, &str);
  104. emitter_kv_note(emitter, "jkl", "JKL", emitter_type_string, &str,
  105. "note_key2", emitter_type_bool, &b_false);
  106. emitter_dict_end(emitter);
  107. emitter_end(emitter);
  108. }
  109. static const char *dict_json =
  110. "{\n"
  111. "\t\"foo\": {\n"
  112. "\t\t\"abc\": false,\n"
  113. "\t\t\"def\": true,\n"
  114. "\t\t\"ghi\": 123,\n"
  115. "\t\t\"jkl\": \"a string\"\n"
  116. "\t}\n"
  117. "}\n";
  118. static const char *dict_json_compact =
  119. "{"
  120. "\"foo\":{"
  121. "\"abc\":false,"
  122. "\"def\":true,"
  123. "\"ghi\":123,"
  124. "\"jkl\":\"a string\""
  125. "}"
  126. "}";
  127. static const char *dict_table =
  128. "This is the foo table:\n"
  129. " ABC: false\n"
  130. " DEF: true\n"
  131. " GHI: 123 (note_key1: \"a string\")\n"
  132. " JKL: \"a string\" (note_key2: false)\n";
  133. static void
  134. emit_table_printf(emitter_t *emitter) {
  135. emitter_begin(emitter);
  136. emitter_table_printf(emitter, "Table note 1\n");
  137. emitter_table_printf(emitter, "Table note 2 %s\n",
  138. "with format string");
  139. emitter_end(emitter);
  140. }
  141. static const char *table_printf_json =
  142. "{\n"
  143. "}\n";
  144. static const char *table_printf_json_compact = "{}";
  145. static const char *table_printf_table =
  146. "Table note 1\n"
  147. "Table note 2 with format string\n";
  148. static void emit_nested_dict(emitter_t *emitter) {
  149. int val = 123;
  150. emitter_begin(emitter);
  151. emitter_dict_begin(emitter, "json1", "Dict 1");
  152. emitter_dict_begin(emitter, "json2", "Dict 2");
  153. emitter_kv(emitter, "primitive", "A primitive", emitter_type_int, &val);
  154. emitter_dict_end(emitter); /* Close 2 */
  155. emitter_dict_begin(emitter, "json3", "Dict 3");
  156. emitter_dict_end(emitter); /* Close 3 */
  157. emitter_dict_end(emitter); /* Close 1 */
  158. emitter_dict_begin(emitter, "json4", "Dict 4");
  159. emitter_kv(emitter, "primitive", "Another primitive",
  160. emitter_type_int, &val);
  161. emitter_dict_end(emitter); /* Close 4 */
  162. emitter_end(emitter);
  163. }
  164. static const char *nested_dict_json =
  165. "{\n"
  166. "\t\"json1\": {\n"
  167. "\t\t\"json2\": {\n"
  168. "\t\t\t\"primitive\": 123\n"
  169. "\t\t},\n"
  170. "\t\t\"json3\": {\n"
  171. "\t\t}\n"
  172. "\t},\n"
  173. "\t\"json4\": {\n"
  174. "\t\t\"primitive\": 123\n"
  175. "\t}\n"
  176. "}\n";
  177. static const char *nested_dict_json_compact =
  178. "{"
  179. "\"json1\":{"
  180. "\"json2\":{"
  181. "\"primitive\":123"
  182. "},"
  183. "\"json3\":{"
  184. "}"
  185. "},"
  186. "\"json4\":{"
  187. "\"primitive\":123"
  188. "}"
  189. "}";
  190. static const char *nested_dict_table =
  191. "Dict 1\n"
  192. " Dict 2\n"
  193. " A primitive: 123\n"
  194. " Dict 3\n"
  195. "Dict 4\n"
  196. " Another primitive: 123\n";
  197. static void
  198. emit_types(emitter_t *emitter) {
  199. bool b = false;
  200. int i = -123;
  201. unsigned u = 123;
  202. ssize_t zd = -456;
  203. size_t zu = 456;
  204. const char *str = "string";
  205. uint32_t u32 = 789;
  206. uint64_t u64 = 10000000000ULL;
  207. emitter_begin(emitter);
  208. emitter_kv(emitter, "k1", "K1", emitter_type_bool, &b);
  209. emitter_kv(emitter, "k2", "K2", emitter_type_int, &i);
  210. emitter_kv(emitter, "k3", "K3", emitter_type_unsigned, &u);
  211. emitter_kv(emitter, "k4", "K4", emitter_type_ssize, &zd);
  212. emitter_kv(emitter, "k5", "K5", emitter_type_size, &zu);
  213. emitter_kv(emitter, "k6", "K6", emitter_type_string, &str);
  214. emitter_kv(emitter, "k7", "K7", emitter_type_uint32, &u32);
  215. emitter_kv(emitter, "k8", "K8", emitter_type_uint64, &u64);
  216. /*
  217. * We don't test the title type, since it's only used for tables. It's
  218. * tested in the emitter_table_row tests.
  219. */
  220. emitter_end(emitter);
  221. }
  222. static const char *types_json =
  223. "{\n"
  224. "\t\"k1\": false,\n"
  225. "\t\"k2\": -123,\n"
  226. "\t\"k3\": 123,\n"
  227. "\t\"k4\": -456,\n"
  228. "\t\"k5\": 456,\n"
  229. "\t\"k6\": \"string\",\n"
  230. "\t\"k7\": 789,\n"
  231. "\t\"k8\": 10000000000\n"
  232. "}\n";
  233. static const char *types_json_compact =
  234. "{"
  235. "\"k1\":false,"
  236. "\"k2\":-123,"
  237. "\"k3\":123,"
  238. "\"k4\":-456,"
  239. "\"k5\":456,"
  240. "\"k6\":\"string\","
  241. "\"k7\":789,"
  242. "\"k8\":10000000000"
  243. "}";
  244. static const char *types_table =
  245. "K1: false\n"
  246. "K2: -123\n"
  247. "K3: 123\n"
  248. "K4: -456\n"
  249. "K5: 456\n"
  250. "K6: \"string\"\n"
  251. "K7: 789\n"
  252. "K8: 10000000000\n";
  253. static void
  254. emit_modal(emitter_t *emitter) {
  255. int val = 123;
  256. emitter_begin(emitter);
  257. emitter_dict_begin(emitter, "j0", "T0");
  258. emitter_json_key(emitter, "j1");
  259. emitter_json_object_begin(emitter);
  260. emitter_kv(emitter, "i1", "I1", emitter_type_int, &val);
  261. emitter_json_kv(emitter, "i2", emitter_type_int, &val);
  262. emitter_table_kv(emitter, "I3", emitter_type_int, &val);
  263. emitter_table_dict_begin(emitter, "T1");
  264. emitter_kv(emitter, "i4", "I4", emitter_type_int, &val);
  265. emitter_json_object_end(emitter); /* Close j1 */
  266. emitter_kv(emitter, "i5", "I5", emitter_type_int, &val);
  267. emitter_table_dict_end(emitter); /* Close T1 */
  268. emitter_kv(emitter, "i6", "I6", emitter_type_int, &val);
  269. emitter_dict_end(emitter); /* Close j0 / T0 */
  270. emitter_end(emitter);
  271. }
  272. const char *modal_json =
  273. "{\n"
  274. "\t\"j0\": {\n"
  275. "\t\t\"j1\": {\n"
  276. "\t\t\t\"i1\": 123,\n"
  277. "\t\t\t\"i2\": 123,\n"
  278. "\t\t\t\"i4\": 123\n"
  279. "\t\t},\n"
  280. "\t\t\"i5\": 123,\n"
  281. "\t\t\"i6\": 123\n"
  282. "\t}\n"
  283. "}\n";
  284. const char *modal_json_compact =
  285. "{"
  286. "\"j0\":{"
  287. "\"j1\":{"
  288. "\"i1\":123,"
  289. "\"i2\":123,"
  290. "\"i4\":123"
  291. "},"
  292. "\"i5\":123,"
  293. "\"i6\":123"
  294. "}"
  295. "}";
  296. const char *modal_table =
  297. "T0\n"
  298. " I1: 123\n"
  299. " I3: 123\n"
  300. " T1\n"
  301. " I4: 123\n"
  302. " I5: 123\n"
  303. " I6: 123\n";
  304. static void
  305. emit_json_array(emitter_t *emitter) {
  306. int ival = 123;
  307. emitter_begin(emitter);
  308. emitter_json_key(emitter, "dict");
  309. emitter_json_object_begin(emitter);
  310. emitter_json_key(emitter, "arr");
  311. emitter_json_array_begin(emitter);
  312. emitter_json_object_begin(emitter);
  313. emitter_json_kv(emitter, "foo", emitter_type_int, &ival);
  314. emitter_json_object_end(emitter); /* Close arr[0] */
  315. /* arr[1] and arr[2] are primitives. */
  316. emitter_json_value(emitter, emitter_type_int, &ival);
  317. emitter_json_value(emitter, emitter_type_int, &ival);
  318. emitter_json_object_begin(emitter);
  319. emitter_json_kv(emitter, "bar", emitter_type_int, &ival);
  320. emitter_json_kv(emitter, "baz", emitter_type_int, &ival);
  321. emitter_json_object_end(emitter); /* Close arr[3]. */
  322. emitter_json_array_end(emitter); /* Close arr. */
  323. emitter_json_object_end(emitter); /* Close dict. */
  324. emitter_end(emitter);
  325. }
  326. static const char *json_array_json =
  327. "{\n"
  328. "\t\"dict\": {\n"
  329. "\t\t\"arr\": [\n"
  330. "\t\t\t{\n"
  331. "\t\t\t\t\"foo\": 123\n"
  332. "\t\t\t},\n"
  333. "\t\t\t123,\n"
  334. "\t\t\t123,\n"
  335. "\t\t\t{\n"
  336. "\t\t\t\t\"bar\": 123,\n"
  337. "\t\t\t\t\"baz\": 123\n"
  338. "\t\t\t}\n"
  339. "\t\t]\n"
  340. "\t}\n"
  341. "}\n";
  342. static const char *json_array_json_compact =
  343. "{"
  344. "\"dict\":{"
  345. "\"arr\":["
  346. "{"
  347. "\"foo\":123"
  348. "},"
  349. "123,"
  350. "123,"
  351. "{"
  352. "\"bar\":123,"
  353. "\"baz\":123"
  354. "}"
  355. "]"
  356. "}"
  357. "}";
  358. static const char *json_array_table = "";
  359. static void
  360. emit_json_nested_array(emitter_t *emitter) {
  361. int ival = 123;
  362. char *sval = "foo";
  363. emitter_begin(emitter);
  364. emitter_json_array_begin(emitter);
  365. emitter_json_array_begin(emitter);
  366. emitter_json_value(emitter, emitter_type_int, &ival);
  367. emitter_json_value(emitter, emitter_type_string, &sval);
  368. emitter_json_value(emitter, emitter_type_int, &ival);
  369. emitter_json_value(emitter, emitter_type_string, &sval);
  370. emitter_json_array_end(emitter);
  371. emitter_json_array_begin(emitter);
  372. emitter_json_value(emitter, emitter_type_int, &ival);
  373. emitter_json_array_end(emitter);
  374. emitter_json_array_begin(emitter);
  375. emitter_json_value(emitter, emitter_type_string, &sval);
  376. emitter_json_value(emitter, emitter_type_int, &ival);
  377. emitter_json_array_end(emitter);
  378. emitter_json_array_begin(emitter);
  379. emitter_json_array_end(emitter);
  380. emitter_json_array_end(emitter);
  381. emitter_end(emitter);
  382. }
  383. static const char *json_nested_array_json =
  384. "{\n"
  385. "\t[\n"
  386. "\t\t[\n"
  387. "\t\t\t123,\n"
  388. "\t\t\t\"foo\",\n"
  389. "\t\t\t123,\n"
  390. "\t\t\t\"foo\"\n"
  391. "\t\t],\n"
  392. "\t\t[\n"
  393. "\t\t\t123\n"
  394. "\t\t],\n"
  395. "\t\t[\n"
  396. "\t\t\t\"foo\",\n"
  397. "\t\t\t123\n"
  398. "\t\t],\n"
  399. "\t\t[\n"
  400. "\t\t]\n"
  401. "\t]\n"
  402. "}\n";
  403. static const char *json_nested_array_json_compact =
  404. "{"
  405. "["
  406. "["
  407. "123,"
  408. "\"foo\","
  409. "123,"
  410. "\"foo\""
  411. "],"
  412. "["
  413. "123"
  414. "],"
  415. "["
  416. "\"foo\","
  417. "123"
  418. "],"
  419. "["
  420. "]"
  421. "]"
  422. "}";
  423. static const char *json_nested_array_table = "";
  424. static void
  425. emit_table_row(emitter_t *emitter) {
  426. emitter_begin(emitter);
  427. emitter_row_t row;
  428. emitter_col_t abc = {emitter_justify_left, 10, emitter_type_title, {0}, {0, 0}};
  429. abc.str_val = "ABC title";
  430. emitter_col_t def = {emitter_justify_right, 15, emitter_type_title, {0}, {0, 0}};
  431. def.str_val = "DEF title";
  432. emitter_col_t ghi = {emitter_justify_right, 5, emitter_type_title, {0}, {0, 0}};
  433. ghi.str_val = "GHI";
  434. emitter_row_init(&row);
  435. emitter_col_init(&abc, &row);
  436. emitter_col_init(&def, &row);
  437. emitter_col_init(&ghi, &row);
  438. emitter_table_row(emitter, &row);
  439. abc.type = emitter_type_int;
  440. def.type = emitter_type_bool;
  441. ghi.type = emitter_type_int;
  442. abc.int_val = 123;
  443. def.bool_val = true;
  444. ghi.int_val = 456;
  445. emitter_table_row(emitter, &row);
  446. abc.int_val = 789;
  447. def.bool_val = false;
  448. ghi.int_val = 1011;
  449. emitter_table_row(emitter, &row);
  450. abc.type = emitter_type_string;
  451. abc.str_val = "a string";
  452. def.bool_val = false;
  453. ghi.type = emitter_type_title;
  454. ghi.str_val = "ghi";
  455. emitter_table_row(emitter, &row);
  456. emitter_end(emitter);
  457. }
  458. static const char *table_row_json =
  459. "{\n"
  460. "}\n";
  461. static const char *table_row_json_compact = "{}";
  462. static const char *table_row_table =
  463. "ABC title DEF title GHI\n"
  464. "123 true 456\n"
  465. "789 false 1011\n"
  466. "\"a string\" false ghi\n";
  467. #define GENERATE_TEST(feature) \
  468. TEST_BEGIN(test_##feature) { \
  469. expect_emit_output(emit_##feature, feature##_json, \
  470. feature##_json_compact, feature##_table); \
  471. } \
  472. TEST_END
  473. GENERATE_TEST(dict)
  474. GENERATE_TEST(table_printf)
  475. GENERATE_TEST(nested_dict)
  476. GENERATE_TEST(types)
  477. GENERATE_TEST(modal)
  478. GENERATE_TEST(json_array)
  479. GENERATE_TEST(json_nested_array)
  480. GENERATE_TEST(table_row)
  481. int
  482. main(void) {
  483. return test_no_reentrancy(
  484. test_dict,
  485. test_table_printf,
  486. test_nested_dict,
  487. test_types,
  488. test_modal,
  489. test_json_array,
  490. test_json_nested_array,
  491. test_table_row);
  492. }