unity_platform.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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. // 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. void unity_reset_leak_checks(void)
  32. {
  33. before_free_8bit = heap_caps_get_free_size(MALLOC_CAP_8BIT);
  34. before_free_32bit = heap_caps_get_free_size(MALLOC_CAP_32BIT);
  35. #ifdef CONFIG_HEAP_TRACING
  36. heap_trace_start(HEAP_TRACE_LEAKS);
  37. #endif
  38. }
  39. /* setUp runs before every test */
  40. void setUp(void)
  41. {
  42. // If heap tracing is enabled in kconfig, leak trace the test
  43. #ifdef CONFIG_HEAP_TRACING
  44. const size_t num_heap_records = 80;
  45. static heap_trace_record_t *record_buffer;
  46. if (!record_buffer) {
  47. record_buffer = malloc(sizeof(heap_trace_record_t) * num_heap_records);
  48. assert(record_buffer);
  49. heap_trace_init_standalone(record_buffer, num_heap_records);
  50. }
  51. #endif
  52. printf("%s", ""); /* sneakily lazy-allocate the reent structure for this test task */
  53. get_test_data_partition(); /* allocate persistent partition table structures */
  54. unity_reset_leak_checks();
  55. }
  56. static void check_leak(size_t before_free, size_t after_free, const char *type)
  57. {
  58. if (before_free <= after_free) {
  59. return;
  60. }
  61. size_t leaked = before_free - after_free;
  62. if (leaked < WARN_LEAK_THRESHOLD) {
  63. return;
  64. }
  65. printf("MALLOC_CAP_%s %s leak: Before %u bytes free, After %u bytes free (delta %u)\n",
  66. type,
  67. leaked < CRITICAL_LEAK_THRESHOLD ? "potential" : "critical",
  68. before_free, after_free, leaked);
  69. fflush(stdout);
  70. TEST_ASSERT_MESSAGE(leaked < CRITICAL_LEAK_THRESHOLD, "The test leaked too much memory");
  71. }
  72. /* tearDown runs after every test */
  73. void tearDown(void)
  74. {
  75. /* some FreeRTOS stuff is cleaned up by idle task */
  76. vTaskDelay(5);
  77. /* We want the teardown to have this file in the printout if TEST_ASSERT fails */
  78. const char *real_testfile = Unity.TestFile;
  79. Unity.TestFile = __FILE__;
  80. /* check if unit test has caused heap corruption in any heap */
  81. TEST_ASSERT_MESSAGE( heap_caps_check_integrity(MALLOC_CAP_INVALID, true), "The test has corrupted the heap");
  82. /* check for leaks */
  83. #ifdef CONFIG_HEAP_TRACING
  84. heap_trace_stop();
  85. heap_trace_dump();
  86. #endif
  87. size_t after_free_8bit = heap_caps_get_free_size(MALLOC_CAP_8BIT);
  88. size_t after_free_32bit = heap_caps_get_free_size(MALLOC_CAP_32BIT);
  89. check_leak(before_free_8bit, after_free_8bit, "8BIT");
  90. check_leak(before_free_32bit, after_free_32bit, "32BIT");
  91. Unity.TestFile = real_testfile; // go back to the real filename
  92. }
  93. void unity_putc(int c)
  94. {
  95. if (c == '\n')
  96. {
  97. uart_tx_one_char('\r');
  98. uart_tx_one_char('\n');
  99. }
  100. else if (c == '\r')
  101. {
  102. }
  103. else
  104. {
  105. uart_tx_one_char(c);
  106. }
  107. }
  108. void unity_flush()
  109. {
  110. uart_tx_wait_idle(0); // assume that output goes to UART0
  111. }
  112. void unity_testcase_register(struct test_desc_t* desc)
  113. {
  114. if (!s_unity_tests_first)
  115. {
  116. s_unity_tests_first = desc;
  117. s_unity_tests_last = desc;
  118. }
  119. else
  120. {
  121. struct test_desc_t* temp = s_unity_tests_first;
  122. s_unity_tests_first = desc;
  123. s_unity_tests_first->next = temp;
  124. }
  125. }
  126. /* print the multiple function case name and its sub-menu
  127. * e.g:
  128. * (1) spi master/slave case
  129. * (1)master case
  130. * (2)slave case
  131. * */
  132. static void print_multiple_function_test_menu(const struct test_desc_t* test_ms)
  133. {
  134. printf("%s\n", test_ms->name);
  135. for (int i = 0; i < test_ms->test_fn_count; i++)
  136. {
  137. printf("\t(%d)\t\"%s\"\n", i+1, test_ms->test_fn_name[i]);
  138. }
  139. }
  140. void multiple_function_option(const struct test_desc_t* test_ms)
  141. {
  142. int selection;
  143. char cmdline[256] = {0};
  144. print_multiple_function_test_menu(test_ms);
  145. while(strlen(cmdline) == 0)
  146. {
  147. /* Flush anything already in the RX buffer */
  148. while(uart_rx_one_char((uint8_t *) cmdline) == OK) {
  149. }
  150. UartRxString((uint8_t*) cmdline, sizeof(cmdline) - 1);
  151. if(strlen(cmdline) == 0) {
  152. /* if input was newline, print a new menu */
  153. print_multiple_function_test_menu(test_ms);
  154. }
  155. }
  156. selection = atoi((const char *) cmdline) - 1;
  157. if(selection >= 0 && selection < test_ms->test_fn_count) {
  158. UnityDefaultTestRun(test_ms->fn[selection], test_ms->name, test_ms->line);
  159. } else {
  160. printf("Invalid selection, your should input number 1-%d!", test_ms->test_fn_count);
  161. }
  162. }
  163. static void unity_run_single_test(const struct test_desc_t* test)
  164. {
  165. printf("Running %s...\n", test->name);
  166. // Unit test runner expects to see test name before the test starts
  167. fflush(stdout);
  168. uart_tx_wait_idle(CONFIG_CONSOLE_UART_NUM);
  169. Unity.TestFile = test->file;
  170. Unity.CurrentDetail1 = test->desc;
  171. if(test->test_fn_count == 1) {
  172. UnityDefaultTestRun(test->fn[0], test->name, test->line);
  173. } else {
  174. multiple_function_option(test);
  175. }
  176. }
  177. static void unity_run_single_test_by_index(int index)
  178. {
  179. const struct test_desc_t* test;
  180. for (test = s_unity_tests_first; test != NULL && index != 0; test = test->next, --index)
  181. {
  182. }
  183. if (test != NULL)
  184. {
  185. unity_run_single_test(test);
  186. }
  187. }
  188. static void unity_run_single_test_by_index_parse(const char* filter, int index_max)
  189. {
  190. if (s_invert)
  191. {
  192. printf("Inverse is not supported for that kind of filter\n");
  193. return;
  194. }
  195. int test_index = strtol(filter, NULL, 10);
  196. if (test_index >= 1 && test_index <= index_max)
  197. {
  198. uint32_t start;
  199. RSR(CCOUNT, start);
  200. unity_run_single_test_by_index(test_index - 1);
  201. uint32_t end;
  202. RSR(CCOUNT, end);
  203. uint32_t ms = (end - start) / (esp_clk_cpu_freq() / 1000);
  204. printf("Test ran in %dms\n", ms);
  205. }
  206. }
  207. static void unity_run_single_test_by_name(const char* filter)
  208. {
  209. if (s_invert)
  210. {
  211. printf("Inverse is not supported for that kind of filter\n");
  212. return;
  213. }
  214. char tmp[256];
  215. strncpy(tmp, filter + 1, sizeof(tmp) - 1);
  216. tmp[strlen(filter) - 2] = 0;
  217. for (const struct test_desc_t* test = s_unity_tests_first; test != NULL; test = test->next)
  218. {
  219. if (strcmp(test->name, tmp) == 0)
  220. {
  221. unity_run_single_test(test);
  222. }
  223. }
  224. }
  225. void unity_run_all_tests()
  226. {
  227. if (s_invert)
  228. {
  229. printf("Inverse is not supported for that kind of filter\n");
  230. return;
  231. }
  232. for (const struct test_desc_t* test = s_unity_tests_first; test != NULL; test = test->next)
  233. {
  234. unity_run_single_test(test);
  235. }
  236. }
  237. void unity_run_tests_with_filter(const char* filter)
  238. {
  239. if (s_invert)
  240. {
  241. ++filter;
  242. }
  243. printf("Running tests %smatching '%s'...\n", s_invert ? "NOT " : "", filter);
  244. for (const struct test_desc_t* test = s_unity_tests_first; test != NULL; test = test->next)
  245. {
  246. if ((strstr(test->desc, filter) != NULL) == !s_invert)
  247. {
  248. unity_run_single_test(test);
  249. }
  250. }
  251. }
  252. static void trim_trailing_space(char* str)
  253. {
  254. char* end = str + strlen(str) - 1;
  255. while (end >= str && isspace((int) *end))
  256. {
  257. *end = 0;
  258. --end;
  259. }
  260. }
  261. static int print_test_menu(void)
  262. {
  263. int test_counter = 0;
  264. printf("\n\nHere's the test menu, pick your combo:\n");
  265. for (const struct test_desc_t* test = s_unity_tests_first;
  266. test != NULL;
  267. test = test->next, ++test_counter)
  268. {
  269. printf("(%d)\t\"%s\" %s\n", test_counter + 1, test->name, test->desc);
  270. if(test->test_fn_count > 1)
  271. {
  272. for (int i = 0; i < test->test_fn_count; i++)
  273. {
  274. printf("\t(%d)\t\"%s\"\n", i+1, test->test_fn_name[i]);
  275. }
  276. }
  277. }
  278. printf("\nEnter test for running.\n"); /* unit_test.py needs it for finding the end of test menu */
  279. return test_counter;
  280. }
  281. static int get_test_count(void)
  282. {
  283. int test_counter = 0;
  284. for (const struct test_desc_t* test = s_unity_tests_first;
  285. test != NULL;
  286. test = test->next)
  287. {
  288. ++test_counter;
  289. }
  290. return test_counter;
  291. }
  292. void unity_run_menu()
  293. {
  294. printf("\n\nPress ENTER to see the list of tests.\n");
  295. int test_count = get_test_count();
  296. while (true)
  297. {
  298. char cmdline[256] = { 0 };
  299. while(strlen(cmdline) == 0)
  300. {
  301. /* Flush anything already in the RX buffer */
  302. while(uart_rx_one_char((uint8_t *) cmdline) == OK) {
  303. }
  304. /* Read input */
  305. UartRxString((uint8_t*) cmdline, sizeof(cmdline) - 1);
  306. trim_trailing_space(cmdline);
  307. if(strlen(cmdline) == 0) {
  308. /* if input was newline, print a new menu */
  309. print_test_menu();
  310. }
  311. }
  312. /*use '-' to show test history. Need to do it before UNITY_BEGIN cleanup history */
  313. if (cmdline[0] == '-')
  314. {
  315. UNITY_END();
  316. continue;
  317. }
  318. UNITY_BEGIN();
  319. size_t idx = 0;
  320. if (cmdline[idx] == '!')
  321. {
  322. s_invert = true;
  323. ++idx;
  324. }
  325. else
  326. {
  327. s_invert = false;
  328. }
  329. if (cmdline[idx] == '*')
  330. {
  331. unity_run_all_tests();
  332. }
  333. else if (cmdline[idx] =='[')
  334. {
  335. unity_run_tests_with_filter(cmdline + idx);
  336. }
  337. else if (cmdline[idx] =='"')
  338. {
  339. unity_run_single_test_by_name(cmdline + idx);
  340. }
  341. else if (isdigit((unsigned char)cmdline[idx]))
  342. {
  343. unity_run_single_test_by_index_parse(cmdline + idx, test_count);
  344. }
  345. UNITY_END();
  346. printf("Enter next test, or 'enter' to see menu\n");
  347. }
  348. }