test_runner.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // Copyright 2016-2018 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include <stdio.h>
  15. #include "esp_heap_caps.h"
  16. #include "freertos/FreeRTOS.h"
  17. #include "freertos/task.h"
  18. #include "unity.h"
  19. #include "test_utils.h"
  20. #ifdef CONFIG_HEAP_TRACING
  21. #include "esp_heap_trace.h"
  22. #endif
  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 occurring.
  28. */
  29. const size_t WARN_LEAK_THRESHOLD = 256;
  30. const size_t CRITICAL_LEAK_THRESHOLD = 4096;
  31. static void unity_task(void *pvParameters)
  32. {
  33. vTaskDelay(2); /* Delay a bit to let the main task be deleted */
  34. unity_run_menu(); /* Doesn't return */
  35. }
  36. void test_main()
  37. {
  38. // Note: if unpinning this task, change the way run times are calculated in
  39. // unity_port_esp32.c
  40. xTaskCreatePinnedToCore(unity_task, "unityTask", UNITY_FREERTOS_STACK_SIZE, NULL,
  41. UNITY_FREERTOS_PRIORITY, NULL, UNITY_FREERTOS_CPU);
  42. }
  43. void unity_reset_leak_checks(void)
  44. {
  45. before_free_8bit = heap_caps_get_free_size(MALLOC_CAP_8BIT);
  46. before_free_32bit = heap_caps_get_free_size(MALLOC_CAP_32BIT);
  47. #ifdef CONFIG_HEAP_TRACING
  48. heap_trace_start(HEAP_TRACE_LEAKS);
  49. #endif
  50. }
  51. /* setUp runs before every test */
  52. void setUp(void)
  53. {
  54. // If heap tracing is enabled in kconfig, leak trace the test
  55. #ifdef CONFIG_HEAP_TRACING
  56. const size_t num_heap_records = 80;
  57. static heap_trace_record_t *record_buffer;
  58. if (!record_buffer) {
  59. record_buffer = malloc(sizeof(heap_trace_record_t) * num_heap_records);
  60. assert(record_buffer);
  61. heap_trace_init_standalone(record_buffer, num_heap_records);
  62. }
  63. #endif
  64. printf("%s", ""); /* sneakily lazy-allocate the reent structure for this test task */
  65. get_test_data_partition(); /* allocate persistent partition table structures */
  66. unity_reset_leak_checks();
  67. }
  68. static void check_leak(size_t before_free, size_t after_free, const char *type)
  69. {
  70. if (before_free <= after_free) {
  71. return;
  72. }
  73. size_t leaked = before_free - after_free;
  74. if (leaked < WARN_LEAK_THRESHOLD) {
  75. return;
  76. }
  77. printf("MALLOC_CAP_%s %s leak: Before %u bytes free, After %u bytes free (delta %u)\n",
  78. type,
  79. leaked < CRITICAL_LEAK_THRESHOLD ? "potential" : "critical",
  80. before_free, after_free, leaked);
  81. fflush(stdout);
  82. TEST_ASSERT_MESSAGE(leaked < CRITICAL_LEAK_THRESHOLD, "The test leaked too much memory");
  83. }
  84. /* tearDown runs after every test */
  85. void tearDown(void)
  86. {
  87. /* some FreeRTOS stuff is cleaned up by idle task */
  88. vTaskDelay(5);
  89. /* We want the teardown to have this file in the printout if TEST_ASSERT fails */
  90. const char *real_testfile = Unity.TestFile;
  91. Unity.TestFile = __FILE__;
  92. /* check if unit test has caused heap corruption in any heap */
  93. TEST_ASSERT_MESSAGE( heap_caps_check_integrity(MALLOC_CAP_INVALID, true), "The test has corrupted the heap");
  94. /* check for leaks */
  95. #ifdef CONFIG_HEAP_TRACING
  96. heap_trace_stop();
  97. heap_trace_dump();
  98. #endif
  99. size_t after_free_8bit = heap_caps_get_free_size(MALLOC_CAP_8BIT);
  100. size_t after_free_32bit = heap_caps_get_free_size(MALLOC_CAP_32BIT);
  101. check_leak(before_free_8bit, after_free_8bit, "8BIT");
  102. check_leak(before_free_32bit, after_free_32bit, "32BIT");
  103. Unity.TestFile = real_testfile; // go back to the real filename
  104. }