unity_platform.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4. #include <stdio.h>
  5. #include "unity.h"
  6. #include "rom/ets_sys.h"
  7. #include "rom/uart.h"
  8. #include "freertos/FreeRTOS.h"
  9. #include "freertos/task.h"
  10. #include "esp_log.h"
  11. #include "esp_clk.h"
  12. #include "soc/cpu.h"
  13. #include "esp_heap_caps.h"
  14. #include "test_utils.h"
  15. #ifdef CONFIG_HEAP_TRACING
  16. #include "esp_heap_trace.h"
  17. #endif
  18. #define unity_printf ets_printf
  19. // Pointers to the head and tail of linked list of test description structs:
  20. static struct test_desc_t* s_unity_tests_first = NULL;
  21. static struct test_desc_t* s_unity_tests_last = NULL;
  22. // Inverse of the filter
  23. static bool s_invert = false;
  24. static size_t before_free_8bit;
  25. static size_t before_free_32bit;
  26. /* Each unit test is allowed to "leak" this many bytes.
  27. TODO: Make this value editable by the test.
  28. Will always need to be some value here, as fragmentation can reduce free space even when no leak is occuring.
  29. */
  30. const size_t WARN_LEAK_THRESHOLD = 256;
  31. const size_t CRITICAL_LEAK_THRESHOLD = 4096;
  32. /* setUp runs before every test */
  33. void setUp(void)
  34. {
  35. // If heap tracing is enabled in kconfig, leak trace the test
  36. #ifdef CONFIG_HEAP_TRACING
  37. const size_t num_heap_records = 80;
  38. static heap_trace_record_t *record_buffer;
  39. if (!record_buffer) {
  40. record_buffer = malloc(sizeof(heap_trace_record_t) * num_heap_records);
  41. assert(record_buffer);
  42. heap_trace_init_standalone(record_buffer, num_heap_records);
  43. }
  44. #endif
  45. printf("%s", ""); /* sneakily lazy-allocate the reent structure for this test task */
  46. get_test_data_partition(); /* allocate persistent partition table structures */
  47. before_free_8bit = heap_caps_get_free_size(MALLOC_CAP_8BIT);
  48. before_free_32bit = heap_caps_get_free_size(MALLOC_CAP_32BIT);
  49. #ifdef CONFIG_HEAP_TRACING
  50. heap_trace_start(HEAP_TRACE_LEAKS);
  51. #endif
  52. }
  53. static void check_leak(size_t before_free, size_t after_free, const char *type)
  54. {
  55. if (before_free <= after_free) {
  56. return;
  57. }
  58. size_t leaked = before_free - after_free;
  59. if (leaked < WARN_LEAK_THRESHOLD) {
  60. return;
  61. }
  62. printf("MALLOC_CAP_%s %s leak: Before %u bytes free, After %u bytes free (delta %u)\n",
  63. type,
  64. leaked < CRITICAL_LEAK_THRESHOLD ? "potential" : "critical",
  65. before_free, after_free, leaked);
  66. fflush(stdout);
  67. TEST_ASSERT_MESSAGE(leaked < CRITICAL_LEAK_THRESHOLD, "The test leaked too much memory");
  68. }
  69. /* tearDown runs after every test */
  70. void tearDown(void)
  71. {
  72. /* some FreeRTOS stuff is cleaned up by idle task */
  73. vTaskDelay(5);
  74. /* We want the teardown to have this file in the printout if TEST_ASSERT fails */
  75. const char *real_testfile = Unity.TestFile;
  76. Unity.TestFile = __FILE__;
  77. /* check if unit test has caused heap corruption in any heap */
  78. TEST_ASSERT_MESSAGE( heap_caps_check_integrity(MALLOC_CAP_INVALID, true), "The test has corrupted the heap");
  79. /* check for leaks */
  80. #ifdef CONFIG_HEAP_TRACING
  81. heap_trace_stop();
  82. heap_trace_dump();
  83. #endif
  84. size_t after_free_8bit = heap_caps_get_free_size(MALLOC_CAP_8BIT);
  85. size_t after_free_32bit = heap_caps_get_free_size(MALLOC_CAP_32BIT);
  86. check_leak(before_free_8bit, after_free_8bit, "8BIT");
  87. check_leak(before_free_32bit, after_free_32bit, "32BIT");
  88. Unity.TestFile = real_testfile; // go back to the real filename
  89. }
  90. void unity_putc(int c)
  91. {
  92. if (c == '\n')
  93. {
  94. uart_tx_one_char('\r');
  95. uart_tx_one_char('\n');
  96. }
  97. else if (c == '\r')
  98. {
  99. }
  100. else
  101. {
  102. uart_tx_one_char(c);
  103. }
  104. }
  105. void unity_flush()
  106. {
  107. uart_tx_wait_idle(0); // assume that output goes to UART0
  108. }
  109. void unity_testcase_register(struct test_desc_t* desc)
  110. {
  111. if (!s_unity_tests_first)
  112. {
  113. s_unity_tests_first = desc;
  114. s_unity_tests_last = desc;
  115. }
  116. else
  117. {
  118. struct test_desc_t* temp = s_unity_tests_first;
  119. s_unity_tests_first = desc;
  120. s_unity_tests_first->next = temp;
  121. }
  122. }
  123. static void unity_run_single_test(const struct test_desc_t* test)
  124. {
  125. printf("Running %s...\n", test->name);
  126. Unity.TestFile = test->file;
  127. Unity.CurrentDetail1 = test->desc;
  128. UnityDefaultTestRun(test->fn, test->name, test->line);
  129. }
  130. static void unity_run_single_test_by_index(int index)
  131. {
  132. const struct test_desc_t* test;
  133. for (test = s_unity_tests_first; test != NULL && index != 0; test = test->next, --index)
  134. {
  135. }
  136. if (test != NULL)
  137. {
  138. unity_run_single_test(test);
  139. }
  140. }
  141. static void unity_run_single_test_by_index_parse(const char* filter, int index_max)
  142. {
  143. if (s_invert)
  144. {
  145. printf("Inverse is not supported for that kind of filter\n");
  146. return;
  147. }
  148. int test_index = strtol(filter, NULL, 10);
  149. if (test_index >= 1 && test_index <= index_max)
  150. {
  151. uint32_t start;
  152. RSR(CCOUNT, start);
  153. unity_run_single_test_by_index(test_index - 1);
  154. uint32_t end;
  155. RSR(CCOUNT, end);
  156. uint32_t ms = (end - start) / (esp_clk_cpu_freq() / 1000);
  157. printf("Test ran in %dms\n", ms);
  158. }
  159. }
  160. static void unity_run_single_test_by_name(const char* filter)
  161. {
  162. if (s_invert)
  163. {
  164. printf("Inverse is not supported for that kind of filter\n");
  165. return;
  166. }
  167. char tmp[256];
  168. strncpy(tmp, filter + 1, sizeof(tmp) - 1);
  169. tmp[strlen(filter) - 2] = 0;
  170. for (const struct test_desc_t* test = s_unity_tests_first; test != NULL; test = test->next)
  171. {
  172. if (strcmp(test->name, tmp) == 0)
  173. {
  174. unity_run_single_test(test);
  175. }
  176. }
  177. }
  178. void unity_run_all_tests()
  179. {
  180. if (s_invert)
  181. {
  182. printf("Inverse is not supported for that kind of filter\n");
  183. return;
  184. }
  185. for (const struct test_desc_t* test = s_unity_tests_first; test != NULL; test = test->next)
  186. {
  187. unity_run_single_test(test);
  188. }
  189. }
  190. void unity_run_tests_with_filter(const char* filter)
  191. {
  192. if (s_invert)
  193. {
  194. ++filter;
  195. }
  196. printf("Running tests %smatching '%s'...\n", s_invert ? "NOT " : "", filter);
  197. for (const struct test_desc_t* test = s_unity_tests_first; test != NULL; test = test->next)
  198. {
  199. if ((strstr(test->desc, filter) != NULL) == !s_invert)
  200. {
  201. unity_run_single_test(test);
  202. }
  203. }
  204. }
  205. static void trim_trailing_space(char* str)
  206. {
  207. char* end = str + strlen(str) - 1;
  208. while (end >= str && isspace((int) *end))
  209. {
  210. *end = 0;
  211. --end;
  212. }
  213. }
  214. static int print_test_menu(void)
  215. {
  216. int test_counter = 0;
  217. unity_printf("\n\nHere's the test menu, pick your combo:\n");
  218. for (const struct test_desc_t* test = s_unity_tests_first;
  219. test != NULL;
  220. test = test->next, ++test_counter)
  221. {
  222. unity_printf("(%d)\t\"%s\" %s\n", test_counter + 1, test->name, test->desc);
  223. }
  224. return test_counter;
  225. }
  226. void unity_run_menu()
  227. {
  228. int test_count = print_test_menu();
  229. while (true)
  230. {
  231. char cmdline[256] = { 0 };
  232. while(strlen(cmdline) == 0)
  233. {
  234. /* Flush anything already in the RX buffer */
  235. while(uart_rx_one_char((uint8_t *) cmdline) == OK) {
  236. }
  237. /* Read input */
  238. UartRxString((uint8_t*) cmdline, sizeof(cmdline) - 1);
  239. trim_trailing_space(cmdline);
  240. if(strlen(cmdline) == 0) {
  241. /* if input was newline, print a new menu */
  242. print_test_menu();
  243. }
  244. }
  245. UNITY_BEGIN();
  246. size_t idx = 0;
  247. if (cmdline[idx] == '!')
  248. {
  249. s_invert = true;
  250. ++idx;
  251. }
  252. else
  253. {
  254. s_invert = false;
  255. }
  256. if (cmdline[idx] == '*')
  257. {
  258. unity_run_all_tests();
  259. }
  260. else if (cmdline[idx] =='[')
  261. {
  262. unity_run_tests_with_filter(cmdline + idx);
  263. }
  264. else if (cmdline[idx] =='"')
  265. {
  266. unity_run_single_test_by_name(cmdline + idx);
  267. }
  268. else if (isdigit((unsigned char)cmdline[idx]))
  269. {
  270. unity_run_single_test_by_index_parse(cmdline + idx, test_count);
  271. }
  272. UNITY_END();
  273. printf("Enter next test, or 'enter' to see menu\n");
  274. }
  275. }