unity_platform.c 8.0 KB

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