unity_platform.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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. /* 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. /* print the multiple function case name and its sub-menu
  123. * e.g:
  124. * (1) spi master/slave case
  125. * (1)master case
  126. * (2)slave case
  127. * */
  128. static void print_multiple_function_test_menu(const struct test_desc_t* test_ms)
  129. {
  130. printf("%s\n", test_ms->name);
  131. for (int i = 0; i < test_ms->test_fn_count; i++)
  132. {
  133. printf("\t(%d)\t\"%s\"\n", i+1, test_ms->test_fn_name[i]);
  134. }
  135. }
  136. void multiple_function_option(const struct test_desc_t* test_ms)
  137. {
  138. int selection;
  139. char cmdline[256] = {0};
  140. print_multiple_function_test_menu(test_ms);
  141. while(strlen(cmdline) == 0)
  142. {
  143. /* Flush anything already in the RX buffer */
  144. while(uart_rx_one_char((uint8_t *) cmdline) == OK) {
  145. }
  146. UartRxString((uint8_t*) cmdline, sizeof(cmdline) - 1);
  147. if(strlen(cmdline) == 0) {
  148. /* if input was newline, print a new menu */
  149. print_multiple_function_test_menu(test_ms);
  150. }
  151. }
  152. selection = atoi((const char *) cmdline) - 1;
  153. if(selection >= 0 && selection < test_ms->test_fn_count) {
  154. UnityDefaultTestRun(test_ms->fn[selection], test_ms->name, test_ms->line);
  155. } else {
  156. printf("Invalid selection, your should input number 1-%d!", test_ms->test_fn_count);
  157. }
  158. }
  159. static void unity_run_single_test(const struct test_desc_t* test)
  160. {
  161. printf("Running %s...\n", test->name);
  162. // Unit test runner expects to see test name before the test starts
  163. fflush(stdout);
  164. uart_tx_wait_idle(CONFIG_CONSOLE_UART_NUM);
  165. Unity.TestFile = test->file;
  166. Unity.CurrentDetail1 = test->desc;
  167. if(test->test_fn_count == 1) {
  168. UnityDefaultTestRun(test->fn[0], test->name, test->line);
  169. } else {
  170. multiple_function_option(test);
  171. }
  172. }
  173. static void unity_run_single_test_by_index(int index)
  174. {
  175. const struct test_desc_t* test;
  176. for (test = s_unity_tests_first; test != NULL && index != 0; test = test->next, --index)
  177. {
  178. }
  179. if (test != NULL)
  180. {
  181. unity_run_single_test(test);
  182. }
  183. }
  184. static void unity_run_single_test_by_index_parse(const char* filter, int index_max)
  185. {
  186. if (s_invert)
  187. {
  188. printf("Inverse is not supported for that kind of filter\n");
  189. return;
  190. }
  191. int test_index = strtol(filter, NULL, 10);
  192. if (test_index >= 1 && test_index <= index_max)
  193. {
  194. uint32_t start;
  195. RSR(CCOUNT, start);
  196. unity_run_single_test_by_index(test_index - 1);
  197. uint32_t end;
  198. RSR(CCOUNT, end);
  199. uint32_t ms = (end - start) / (esp_clk_cpu_freq() / 1000);
  200. printf("Test ran in %dms\n", ms);
  201. }
  202. }
  203. static void unity_run_single_test_by_name(const char* filter)
  204. {
  205. if (s_invert)
  206. {
  207. printf("Inverse is not supported for that kind of filter\n");
  208. return;
  209. }
  210. char tmp[256];
  211. strncpy(tmp, filter + 1, sizeof(tmp) - 1);
  212. tmp[strlen(filter) - 2] = 0;
  213. for (const struct test_desc_t* test = s_unity_tests_first; test != NULL; test = test->next)
  214. {
  215. if (strcmp(test->name, tmp) == 0)
  216. {
  217. unity_run_single_test(test);
  218. }
  219. }
  220. }
  221. void unity_run_all_tests()
  222. {
  223. if (s_invert)
  224. {
  225. printf("Inverse is not supported for that kind of filter\n");
  226. return;
  227. }
  228. for (const struct test_desc_t* test = s_unity_tests_first; test != NULL; test = test->next)
  229. {
  230. unity_run_single_test(test);
  231. }
  232. }
  233. void unity_run_tests_with_filter(const char* filter)
  234. {
  235. if (s_invert)
  236. {
  237. ++filter;
  238. }
  239. printf("Running tests %smatching '%s'...\n", s_invert ? "NOT " : "", filter);
  240. for (const struct test_desc_t* test = s_unity_tests_first; test != NULL; test = test->next)
  241. {
  242. if ((strstr(test->desc, filter) != NULL) == !s_invert)
  243. {
  244. unity_run_single_test(test);
  245. }
  246. }
  247. }
  248. static void trim_trailing_space(char* str)
  249. {
  250. char* end = str + strlen(str) - 1;
  251. while (end >= str && isspace((int) *end))
  252. {
  253. *end = 0;
  254. --end;
  255. }
  256. }
  257. static int print_test_menu(void)
  258. {
  259. int test_counter = 0;
  260. printf("\n\nHere's the test menu, pick your combo:\n");
  261. for (const struct test_desc_t* test = s_unity_tests_first;
  262. test != NULL;
  263. test = test->next, ++test_counter)
  264. {
  265. printf("(%d)\t\"%s\" %s\n", test_counter + 1, test->name, test->desc);
  266. if(test->test_fn_count > 1)
  267. {
  268. for (int i = 0; i < test->test_fn_count; i++)
  269. {
  270. printf("\t(%d)\t\"%s\"\n", i+1, test->test_fn_name[i]);
  271. }
  272. }
  273. }
  274. return test_counter;
  275. }
  276. static int get_test_count(void)
  277. {
  278. int test_counter = 0;
  279. for (const struct test_desc_t* test = s_unity_tests_first;
  280. test != NULL;
  281. test = test->next)
  282. {
  283. ++test_counter;
  284. }
  285. return test_counter;
  286. }
  287. void unity_run_menu()
  288. {
  289. printf("\n\nPress ENTER to see the list of tests.\n");
  290. int test_count = get_test_count();
  291. while (true)
  292. {
  293. char cmdline[256] = { 0 };
  294. while(strlen(cmdline) == 0)
  295. {
  296. /* Flush anything already in the RX buffer */
  297. while(uart_rx_one_char((uint8_t *) cmdline) == OK) {
  298. }
  299. /* Read input */
  300. UartRxString((uint8_t*) cmdline, sizeof(cmdline) - 1);
  301. trim_trailing_space(cmdline);
  302. if(strlen(cmdline) == 0) {
  303. /* if input was newline, print a new menu */
  304. print_test_menu();
  305. }
  306. }
  307. /*use '-' to show test history. Need to do it before UNITY_BEGIN cleanup history */
  308. if (cmdline[0] == '-')
  309. {
  310. UNITY_END();
  311. continue;
  312. }
  313. UNITY_BEGIN();
  314. size_t idx = 0;
  315. if (cmdline[idx] == '!')
  316. {
  317. s_invert = true;
  318. ++idx;
  319. }
  320. else
  321. {
  322. s_invert = false;
  323. }
  324. if (cmdline[idx] == '*')
  325. {
  326. unity_run_all_tests();
  327. }
  328. else if (cmdline[idx] =='[')
  329. {
  330. unity_run_tests_with_filter(cmdline + idx);
  331. }
  332. else if (cmdline[idx] =='"')
  333. {
  334. unity_run_single_test_by_name(cmdline + idx);
  335. }
  336. else if (isdigit((unsigned char)cmdline[idx]))
  337. {
  338. unity_run_single_test_by_index_parse(cmdline + idx, test_count);
  339. }
  340. UNITY_END();
  341. printf("Enter next test, or 'enter' to see menu\n");
  342. }
  343. }