unity_platform.c 7.2 KB

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