unity_platform.c 10 KB

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