Newer
Older
libkc / modules / test / src / test_memory_entry.c
Nomura Kei on 2 Jun 2024 3 KB update
  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_entry.h>
  8. #include <kc_memory_entry_inner.h>
  9.  
  10. // プロトタイプ宣言
  11. static void test_memory_entry_new(void);
  12. static void test_memory_entry_new_alignment(void);
  13. static void test_memory_entry_new_realloc(void);
  14. static void test_memory_entry_set_null(void);
  15.  
  16. /**
  17. * memory_entry 単体テストスイート
  18. */
  19. void suite_memory_entry(void)
  20. {
  21. KcUt *ut = KcUt_get_instance();
  22. ut->add(ut, UT_TESTCASE, "memory_entry new/delete", test_memory_entry_new);
  23. ut->add(ut, UT_TESTCASE, "memory_entry new (alignment)", test_memory_entry_new_alignment);
  24. ut->add(ut, UT_TESTCASE, "memory_entry new (realloc)", test_memory_entry_new_realloc);
  25. ut->add(ut, UT_TESTCASE, "memory_entry set (NULL Entry)", test_memory_entry_set_null);
  26. }
  27.  
  28. /**
  29. * memory_entry 生成/破棄
  30. *
  31. * @process KcMemoryEntry_new を用いて KcMemoryEntry を生成する。
  32. * @result KcMemoryEntry が生成されること。各値が指定された値に設定されていること。
  33. */
  34. static void test_memory_entry_new(void)
  35. {
  36. KcMemoryEntry *entry = KcMemoryEntry_new(
  37. NULL, 0, 10, KC_MEMORY_ALLOCATED, "test_file", "test_func", 34);
  38. assert_not_null(entry);
  39. assert_equals(KC_MEMORY_ALLOCATED, entry->mark);
  40. assert_equals(10, entry->size);
  41. assert_equals("test_file", entry->file);
  42. assert_equals("test_func", entry->func);
  43. assert_equals(34, entry->line);
  44.  
  45. KcMemoryEntry_delete(entry);
  46. // entry->mark には、KC_MEMORY_DELETED を格納してから破棄されているが、
  47. // free 後のメモリ領域は、確認不可。
  48. }
  49.  
  50. /**
  51. * memory_entry 生成 (alignment指定)
  52. *
  53. * @process alignment を指定して KcMemoryEntry を生成する。
  54. * @result KcMemoryEntry が生成されること。各値が指定された値に設定されていること。
  55. */
  56. static void test_memory_entry_new_alignment(void)
  57. {
  58. KcMemoryEntry *entry = KcMemoryEntry_new(
  59. NULL, sizeof(int), 10, KC_MEMORY_ALLOCATED, "test_file", "test_func", 34);
  60. assert_not_null(entry);
  61. assert_equals(KC_MEMORY_ALLOCATED, entry->mark);
  62. assert_equals(10, entry->size);
  63. assert_equals("test_file", entry->file);
  64. assert_equals("test_func", entry->func);
  65. assert_equals(34, entry->line);
  66. KcMemoryEntry_delete(entry);
  67. }
  68.  
  69. /**
  70. * memory_entry 生成 (entry 指定)
  71. *
  72. * @process entry を指定して KcMemoryEntry を生成する。
  73. * @result KcMemoryEntry が生成されること。各値が指定された値に更新されていること。
  74. */
  75. static void test_memory_entry_new_realloc(void)
  76. {
  77. KcMemoryEntry *entry = KcMemoryEntry_new(
  78. NULL, sizeof(int), 10, KC_MEMORY_ALLOCATED, "test_file", "test_func", 34);
  79. KcMemoryEntry *new_entry = KcMemoryEntry_new(
  80. entry, 0, 250, KC_MEMORY_ALLOCATED, "test_file2", "test_func2", 123);
  81. assert_not_null(new_entry);
  82. assert_equals(KC_MEMORY_ALLOCATED, new_entry->mark);
  83. assert_equals(250, new_entry->size);
  84. assert_equals("test_file2", new_entry->file);
  85. assert_equals("test_func2", new_entry->func);
  86. assert_equals(123, new_entry->line);
  87.  
  88. KcMemoryEntry_delete(new_entry);
  89. }
  90.  
  91. /**
  92. * memory_entry 設定
  93. *
  94. * @process NULL を指定して、KcMemoryEntry_set を用いて各値を設定する。
  95. * @result エントリが NULL のため、何も処理されないこと。
  96. */
  97. static void test_memory_entry_set_null(void)
  98. {
  99. KcMemoryEntry_set(
  100. NULL, 10, KC_MEMORY_ALLOCATED, "test_file", "test_func", 123);
  101. }