Newer
Older
libkc / modules / test / src / test_memory_dump.c
  1. #include <stdio.h>
  2. #include <errno.h>
  3.  
  4. #include <kc.h>
  5. #include <kc_ut.h>
  6. #include <kc_assert.h>
  7. #include <kc_memory_dump.h>
  8.  
  9. // プロトタイプ宣言
  10. static void test_memory_dump(void);
  11. static void test_memory_dump_no_binary(void);
  12. static void test_memory_dump_no_ascii(void);
  13. static void test_memory_dump_no_binary_no_ascii(void);
  14. static void test_memory_dump_no_buffer(void);
  15. static void test_memory_dump_no_column(void);
  16. static void test_memory_dump_no_column_show_binary(void);
  17. static void test_memory_dump_no_column_no_binary(void);
  18. static void test_memory_dump_no_column_show_ascii(void);
  19. static void test_memory_dump_no_column_no_ascii(void);
  20. static void test_memory_dump_padding(void);
  21. static void test_memory_dump_over_kb(void);
  22. static void test_memory_dump_message_size_zero(void);
  23. static void test_memory_dump_message_few_buf(void);
  24.  
  25. /**
  26. * memory_dump 単体テストスイート
  27. */
  28. void suite_memory_dump(void)
  29. {
  30. KcUt *ut = KcUt_get_instance();
  31. ut->add(ut, UT_TESTCASE, "memory_dump dump", test_memory_dump);
  32. ut->add(ut, UT_TESTCASE, "memory_dump dump (no binary)", test_memory_dump_no_binary);
  33. ut->add(ut, UT_TESTCASE, "memory_dump dump (no ASCII)", test_memory_dump_no_ascii);
  34. ut->add(ut, UT_TESTCASE, "memory_dump dump (no binary, no ASCII)", test_memory_dump_no_binary_no_ascii);
  35. ut->add(ut, UT_TESTCASE, "memory_dump dump (no buffer)", test_memory_dump_no_buffer);
  36. ut->add(ut, UT_TESTCASE, "memory_dump dump (no column)", test_memory_dump_no_column);
  37. ut->add(ut, UT_TESTCASE, "memory_dump dump (no column, show binary)", test_memory_dump_no_column_show_binary);
  38. ut->add(ut, UT_TESTCASE, "memory_dump dump (no column, no binary)", test_memory_dump_no_column_no_binary);
  39. ut->add(ut, UT_TESTCASE, "memory_dump dump (no column, show ASCII)", test_memory_dump_no_column_show_ascii);
  40. ut->add(ut, UT_TESTCASE, "memory_dump dump (no column, no ASCII)", test_memory_dump_no_column_no_ascii);
  41. ut->add(ut, UT_TESTCASE, "memory_dump dump (padding)", test_memory_dump_padding);
  42. ut->add(ut, UT_TESTCASE, "memory_dump dump (over KB)", test_memory_dump_over_kb);
  43. ut->add(ut, UT_TESTCASE, "memory_dump_message dump message (size zero)", test_memory_dump_message_size_zero);
  44. ut->add(ut, UT_TESTCASE, "memory_dump_message dump message (few buffer)", test_memory_dump_message_few_buf);
  45. }
  46.  
  47. /**
  48. * memory_dump テスト
  49. *
  50. * @process 適宜データを用意し、KcMemoryDump_dump を実行する。
  51. * @result データのダンプが指定したバッファに出力されること。
  52. */
  53. static void test_memory_dump(void)
  54. {
  55. char test_data[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
  56. KcMemoryEntry entry = {
  57. .size = 5,
  58. .mark = KC_MEMORY_ALLOCATED,
  59. .file = "test_file",
  60. .func = "test_func",
  61. .line = 123,
  62. ._prev = NULL,
  63. ._next = NULL,
  64. .data = test_data};
  65.  
  66. char buff[256];
  67. bool ret = KcMemoryDump_dump(buff, sizeof(buff), &entry, 16, true, true, 130);
  68. assert_true(ret);
  69. assert_equals("test_file:123 ( 5.000 B) [test_func] "
  70. " | 41 42 43 44 45 -- -- -- -- -- -- -- -- -- -- -- "
  71. " | ABCDE "
  72. "\n",
  73. buff);
  74. }
  75.  
  76. /**
  77. * memory_dump テスト(バイナリ表示無し)
  78. *
  79. * @process 適宜データを用意し、バイナリ表示無しにて、KcMemoryDump_dump を実行する。
  80. * @result データのダンプが指定したバッファに出力されること。
  81. */
  82. static void test_memory_dump_no_binary(void)
  83. {
  84. char test_data[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
  85. KcMemoryEntry entry = {
  86. .size = 5,
  87. .mark = KC_MEMORY_ALLOCATED,
  88. .file = "test_file",
  89. .func = "test_func",
  90. .line = 123,
  91. ._prev = NULL,
  92. ._next = NULL,
  93. .data = test_data};
  94.  
  95. char buff[256];
  96. bool ret = KcMemoryDump_dump(buff, sizeof(buff), &entry, 16, false, true, 130);
  97. assert_true(ret);
  98. assert_equals("test_file:123 ( 5.000 B) [test_func] "
  99. " "
  100. " | ABCDE "
  101. "\n",
  102. buff);
  103. }
  104.  
  105. /**
  106. * memory_dump テスト(ASCII 表示無し)
  107. *
  108. * @process 適宜データを用意し、ASCII 表示無しにて、KcMemoryDump_dump を実行する。
  109. * @result データのダンプが指定したバッファに出力されること。
  110. */
  111. static void test_memory_dump_no_ascii(void)
  112. {
  113. char test_data[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
  114. KcMemoryEntry entry = {
  115. .size = 5,
  116. .mark = KC_MEMORY_ALLOCATED,
  117. .file = "test_file",
  118. .func = "test_func",
  119. .line = 123,
  120. ._prev = NULL,
  121. ._next = NULL,
  122. .data = test_data};
  123.  
  124. char buff[256];
  125. bool ret = KcMemoryDump_dump(buff, sizeof(buff), &entry, 16, true, false, 130);
  126. assert_true(ret);
  127. assert_equals("test_file:123 ( 5.000 B) [test_func] "
  128. " "
  129. " | 41 42 43 44 45 -- -- -- -- -- -- -- -- -- -- -- "
  130. "\n",
  131. buff);
  132. }
  133.  
  134. /**
  135. * memory_dump テスト(バイナリ表示無し、ASCII 表示無し)
  136. *
  137. * @process 適宜データを用意し、バイナリ表示無し、ASCII 表示無しにて、KcMemoryDump_dump を実行する。
  138. * @result データのダンプが指定したバッファに出力されること。
  139. */
  140. static void test_memory_dump_no_binary_no_ascii(void)
  141. {
  142. char test_data[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
  143. KcMemoryEntry entry = {
  144. .size = 5,
  145. .mark = KC_MEMORY_ALLOCATED,
  146. .file = "test_file",
  147. .func = "test_func",
  148. .line = 123,
  149. ._prev = NULL,
  150. ._next = NULL,
  151. .data = test_data};
  152.  
  153. char buff[256];
  154. bool ret = KcMemoryDump_dump(buff, sizeof(buff), &entry, 16, false, false, 130);
  155. assert_true(ret);
  156. assert_equals("test_file:123 ( 5.000 B) [test_func] "
  157. " "
  158. " "
  159. "\n",
  160. buff);
  161. }
  162.  
  163. /**
  164. * memory_dump テスト(バッファ不足 [指定カラム数に満たない])
  165. *
  166. * @process 指定カラム数に対してバッファサイズが小さい状態で、KcMemoryDump_dump を実行する。
  167. * @result データダンプに失敗すること。
  168. */
  169. static void test_memory_dump_no_buffer(void)
  170. {
  171. char test_data[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
  172. KcMemoryEntry entry = {
  173. .size = 5,
  174. .mark = KC_MEMORY_ALLOCATED,
  175. .file = "test_file",
  176. .func = "test_func",
  177. .line = 123,
  178. ._prev = NULL,
  179. ._next = NULL,
  180. .data = test_data};
  181.  
  182. char buff[80];
  183. bool ret = KcMemoryDump_dump(buff, sizeof(buff), &entry, 16, true, true, 130);
  184. assert_false(ret);
  185. }
  186.  
  187. /**
  188. * memory_dump テスト(バイナリ表示指定、ASCII 表示指定 -> カラム不足により表示不可)
  189. *
  190. * @process 小さいカラム数を指定し、KcMemoryDump_dump を実行する。
  191. * @result バイナリ表示、ASCII 表示のないダンプが表示されること。
  192. */
  193. static void test_memory_dump_no_column(void)
  194. {
  195. char test_data[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
  196. KcMemoryEntry entry = {
  197. .size = 5,
  198. .mark = KC_MEMORY_ALLOCATED,
  199. .file = "test_file",
  200. .func = "test_func",
  201. .line = 123,
  202. ._prev = NULL,
  203. ._next = NULL,
  204. .data = test_data};
  205.  
  206. char buff[256];
  207. bool ret = KcMemoryDump_dump(buff, sizeof(buff), &entry, 16, true, true, 45);
  208. assert_true(ret);
  209. // 10 20 30 40 50
  210. // | | | | |
  211. // 1234567890123456789012345678901234567890123456789012345
  212. assert_equals("test_file:123 ( 5.000 B) [test_func] "
  213. "\n",
  214. buff);
  215. }
  216.  
  217. /**
  218. * memory_dump テスト(バイナリ表示指定、-> カラム不足により、一部表示不可)
  219. *
  220. * @process 小さいカラム数を指定し、KcMemoryDump_dump を実行する。
  221. * @result バイナリ表示、ASCII 表示のないダンプが表示されること。
  222. */
  223. static void test_memory_dump_no_column_show_binary(void)
  224. {
  225. char test_data[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
  226. KcMemoryEntry entry = {
  227. .size = 5,
  228. .mark = KC_MEMORY_ALLOCATED,
  229. .file = "test_file",
  230. .func = "test_func",
  231. .line = 123,
  232. ._prev = NULL,
  233. ._next = NULL,
  234. .data = test_data};
  235.  
  236. char buff[256];
  237. bool ret = KcMemoryDump_dump(buff, sizeof(buff), &entry, 16, true, false, 55);
  238. assert_true(ret);
  239. // 10 20 30 40 50
  240. // | | | | |
  241. // 1234567890123456789012345678901234567890123456789012345
  242. assert_equals("test | 41 42 43 44 45 -- -- -- -- -- -- -- -- -- -- -- "
  243. "\n",
  244. buff);
  245. }
  246.  
  247. /**
  248. * memory_dump テスト(バイナリ表示指定、-> カラム不足により、バイナリ表示不可)
  249. *
  250. * @process 小さいカラム数を指定し、KcMemoryDump_dump を実行する。
  251. * @result バイナリ表示、ASCII 表示のないダンプが表示されること。
  252. */
  253. static void test_memory_dump_no_column_no_binary(void)
  254. {
  255. char test_data[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
  256. KcMemoryEntry entry = {
  257. .size = 5,
  258. .mark = KC_MEMORY_ALLOCATED,
  259. .file = "test_file",
  260. .func = "test_func",
  261. .line = 123,
  262. ._prev = NULL,
  263. ._next = NULL,
  264. .data = test_data};
  265.  
  266. char buff[256];
  267. bool ret = KcMemoryDump_dump(buff, sizeof(buff), &entry, 16, true, false, 45);
  268. assert_true(ret);
  269. // 10 20 30 40 50
  270. // | | | | |
  271. // 1234567890123456789012345678901234567890123456789012345
  272. assert_equals("test_file:123 ( 5.000 B) [test_func] "
  273. "\n",
  274. buff);
  275. }
  276.  
  277. /**
  278. * memory_dump テスト(ASCII表示指定、-> カラム不足により、一部表示不可)
  279. *
  280. * @process 小さいカラム数を指定し、KcMemoryDump_dump を実行する。
  281. * @result バイナリ表示、ASCII 表示のないダンプが表示されること。
  282. */
  283. static void test_memory_dump_no_column_show_ascii(void)
  284. {
  285. char test_data[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
  286. KcMemoryEntry entry = {
  287. .size = 5,
  288. .mark = KC_MEMORY_ALLOCATED,
  289. .file = "test_file",
  290. .func = "test_func",
  291. .line = 123,
  292. ._prev = NULL,
  293. ._next = NULL,
  294. .data = test_data};
  295.  
  296. char buff[256];
  297. bool ret = KcMemoryDump_dump(buff, sizeof(buff), &entry, 16, false, true, 35);
  298. assert_true(ret);
  299. // 10 20 30 40 50
  300. // | | | | |
  301. // 1234567890123456789012345678901234567890123456789012345
  302. assert_equals("test_file:123 ( | ABCDE "
  303. "\n",
  304. buff);
  305. }
  306.  
  307. /**
  308. * memory_dump テスト(ASCII表示指定、-> カラム不足により、ASCII表示不可)
  309. *
  310. * @process 小さいカラム数を指定し、KcMemoryDump_dump を実行する。
  311. * @result バイナリ表示、ASCII 表示のないダンプが表示されること。
  312. */
  313. static void test_memory_dump_no_column_no_ascii(void)
  314. {
  315. char test_data[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
  316. KcMemoryEntry entry = {
  317. .size = 5,
  318. .mark = KC_MEMORY_ALLOCATED,
  319. .file = "test_file",
  320. .func = "test_func",
  321. .line = 123,
  322. ._prev = NULL,
  323. ._next = NULL,
  324. .data = test_data};
  325.  
  326. char buff[256];
  327. bool ret = KcMemoryDump_dump(buff, sizeof(buff), &entry, 16, true, false, 16);
  328. assert_true(ret);
  329. // 10 20 30 40 50
  330. // | | | | |
  331. // 1234567890123456789012345678901234567890123456789012345
  332. assert_equals("test_file:123 ( "
  333. "\n",
  334. buff);
  335. }
  336.  
  337. /**
  338. * memory_dump テスト(padding)
  339. *
  340. * @process カラムサイズを大きくとり、パディングされる状態とする。
  341. * @result 出力が指定されたカラムサイズとなるようにパディングされること。
  342. */
  343. static void test_memory_dump_padding(void)
  344. {
  345. char test_data[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
  346. KcMemoryEntry entry = {
  347. .size = 5,
  348. .mark = KC_MEMORY_ALLOCATED,
  349. .file = "test_file",
  350. .func = "test_func",
  351. .line = 123,
  352. ._prev = NULL,
  353. ._next = NULL,
  354. .data = test_data};
  355.  
  356. char buff[256];
  357. bool ret = KcMemoryDump_dump(buff, sizeof(buff), &entry, 16, false, false, 60);
  358. assert_true(ret);
  359. // 10 20 30 40 50 60
  360. // | | | | | |
  361. // 1234567890123456789012345678901234567890123456789012345678901234
  362. assert_equals("test_file:123 ( 5.000 B) [test_func] "
  363. "\n",
  364. buff);
  365. }
  366.  
  367. /**
  368. * memory_dump テスト(KB以上のメモリ確保)
  369. *
  370. * @process KB 以上のメモリを確保し、KcMemoryDump_dump を実行する。
  371. * @result サイズが単位付きで表示されること。
  372. */
  373. static void test_memory_dump_over_kb(void)
  374. {
  375. char test_data[] = "ABCDEFGHIJK \x00\x81\x82\x83\x84";
  376. KcMemoryEntry entry = {
  377. .size = (int)((double)12.056 * 1024),
  378. .mark = KC_MEMORY_ALLOCATED,
  379. .file = "test_file",
  380. .func = "test_func",
  381. .line = 123,
  382. ._prev = NULL,
  383. ._next = NULL,
  384. .data = test_data};
  385.  
  386. // KB
  387. char buff[256];
  388. bool ret = KcMemoryDump_dump(buff, sizeof(buff), &entry, 16, true, true, 130);
  389. assert_true(ret);
  390. assert_equals("test_file:123 ( 12.056 KB) [test_func] "
  391. " | 41 42 43 44 45 46 47 48 49 4A 4B 20 00 81 82 83 "
  392. " | ABCDEFGHIJK ...."
  393. "\n",
  394. buff);
  395. // MB
  396. entry.size = (int)((double)65.432 * (1024 * 1024));
  397. ret = KcMemoryDump_dump(buff, sizeof(buff), &entry, 16, true, true, 130);
  398. assert_true(ret);
  399. assert_equals("test_file:123 ( 65.432 MB) [test_func] "
  400. " | 41 42 43 44 45 46 47 48 49 4A 4B 20 00 81 82 83 "
  401. " | ABCDEFGHIJK ...."
  402. "\n",
  403. buff);
  404. // GB
  405. entry.size = (int)((double)1.012 * (1024 * 1024 * 1024));
  406. ret = KcMemoryDump_dump(buff, sizeof(buff), &entry, 16, true, true, 130);
  407. assert_true(ret);
  408. assert_equals("test_file:123 ( 1.012 GB) [test_func] "
  409. " | 41 42 43 44 45 46 47 48 49 4A 4B 20 00 81 82 83 "
  410. " | ABCDEFGHIJK ...."
  411. "\n",
  412. buff);
  413. }
  414.  
  415. extern void _UT_KcMemoryDump_dump_message(char *info_write_ptr, int info_rest_size, const char *msg);
  416. /**
  417. * [内部関数] dump_message テスト (バッファサイズ0)
  418. *
  419. * @process 残りサイズ 0 のバッファを渡して、KcMemoryDump_dump_message を実行する。
  420. * @result バッファに何も格納されないこと。
  421. */
  422. static void test_memory_dump_message_size_zero(void)
  423. {
  424. char info_write_buf[5];
  425. int info_rest_size = 0;
  426. info_write_buf[0] = '\0';
  427. _UT_KcMemoryDump_dump_message(info_write_buf, info_rest_size, "MESSAGE");
  428. assert_equals("", info_write_buf);
  429. }
  430.  
  431. /**
  432. * [内部関数] dump_message テスト (バッファ不足)
  433. *
  434. * @process バッファより多いメッセージを渡す。
  435. * @result バッファに入る分のメッセージが格納されること。
  436. */
  437. static void test_memory_dump_message_few_buf(void)
  438. {
  439. char info_write_buf[5];
  440. int info_rest_size = (int)sizeof(info_write_buf);
  441. info_write_buf[0] = '\0';
  442. _UT_KcMemoryDump_dump_message(info_write_buf, info_rest_size, "MESSAGE");
  443. assert_equals("MESS", info_write_buf);
  444. }
  445. // test_file:123 ( 5.000 B) [test_func] | 41 42 43 44 45 -- -- -- -- -- -- -- -- -- -- -- | ABCDE
  446. // bool KcMemoryDump_dump(char *buff, size_t buff_size, const KcMemoryEntry *entry,
  447. // int bytes, bool binary, bool ascii, int column);
  448. // 0x01, 0x19, 0x20, 0x21, 0x31, 0x7e, 0x7f, 0x80,
  449. // normal
  450. // column 数が
  451. // ファイル名:行番号 (size bytes) [関数名] が長い。
  452. // binary が true / false
  453. // ascii が true/ false
  454.  
  455. // KcMemoryDump_dump_info(&buff_info, entry, info_column);
  456. //