mirror of
https://github.com/espressif/esp-idf.git
synced 2025-08-10 04:43:33 +00:00
unit tests: Check heap integrity after each test, check for obvious leaks
This commit is contained in:

committed by
Angus Gratton

parent
1cb0f30933
commit
5222428dde
@@ -27,7 +27,7 @@ void *pvPortMallocCaps(size_t xWantedSize, uint32_t caps) asm("heap_caps_malloc"
|
|||||||
/* Please use heap_caps_get_minimum_free_heap_size() instead of this function */
|
/* Please use heap_caps_get_minimum_free_heap_size() instead of this function */
|
||||||
size_t xPortGetMinimumEverFreeHeapSizeCaps( uint32_t caps ) asm("heap_caps_get_minimum_free_heap_size") __attribute__((deprecated));
|
size_t xPortGetMinimumEverFreeHeapSizeCaps( uint32_t caps ) asm("heap_caps_get_minimum_free_heap_size") __attribute__((deprecated));
|
||||||
|
|
||||||
/* Please use heap_caps_get_free_heap_size() instead of this function */
|
/* Please use heap_caps_get_free_size() instead of this function */
|
||||||
size_t xPortGetFreeHeapSizeCaps( uint32_t caps ) asm("heap_caps_get_free_heap_size") __attribute__((deprecated));
|
size_t xPortGetFreeHeapSizeCaps( uint32_t caps ) asm("heap_caps_get_free_heap_size") __attribute__((deprecated));
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
|
@@ -9,6 +9,7 @@
|
|||||||
#include "freertos/task.h"
|
#include "freertos/task.h"
|
||||||
#include "esp_log.h"
|
#include "esp_log.h"
|
||||||
#include "soc/cpu.h"
|
#include "soc/cpu.h"
|
||||||
|
#include "esp_heap_caps.h"
|
||||||
|
|
||||||
#define unity_printf ets_printf
|
#define unity_printf ets_printf
|
||||||
|
|
||||||
@@ -19,6 +20,63 @@ static struct test_desc_t* s_unity_tests_last = NULL;
|
|||||||
// Inverse of the filter
|
// Inverse of the filter
|
||||||
static bool s_invert = false;
|
static bool s_invert = false;
|
||||||
|
|
||||||
|
|
||||||
|
static size_t before_free_8bit;
|
||||||
|
static size_t before_free_32bit;
|
||||||
|
|
||||||
|
/* Each unit test is allowed to "leak" this many bytes.
|
||||||
|
|
||||||
|
TODO: Make this value editable by the test.
|
||||||
|
|
||||||
|
Will always need to be some value here, as fragmentation can reduce free space even when no leak is occuring.
|
||||||
|
*/
|
||||||
|
const size_t WARN_LEAK_THRESHOLD = 256;
|
||||||
|
const size_t CRITICAL_LEAK_THRESHOLD = 4096;
|
||||||
|
|
||||||
|
/* setUp runs before every test */
|
||||||
|
void setUp(void)
|
||||||
|
{
|
||||||
|
printf("%s", ""); /* sneakily lazy-allocate the reent structure for this test task */
|
||||||
|
|
||||||
|
before_free_8bit = heap_caps_get_free_size(MALLOC_CAP_8BIT);
|
||||||
|
before_free_32bit = heap_caps_get_free_size(MALLOC_CAP_32BIT);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void check_leak(size_t before_free, size_t after_free, const char *type)
|
||||||
|
{
|
||||||
|
if (before_free <= after_free) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
size_t leaked = before_free - after_free;
|
||||||
|
if (leaked < WARN_LEAK_THRESHOLD) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("MALLOC_CAP_%s %s leak: Before %u bytes free, After %u bytes free (delta %u)\n",
|
||||||
|
type,
|
||||||
|
leaked < CRITICAL_LEAK_THRESHOLD ? "potential" : "critical",
|
||||||
|
before_free, after_free, leaked);
|
||||||
|
fflush(stdout);
|
||||||
|
TEST_ASSERT(leaked < CRITICAL_LEAK_THRESHOLD); /* fail the test if it leaks too much */
|
||||||
|
}
|
||||||
|
|
||||||
|
/* tearDown runs after every test */
|
||||||
|
void tearDown(void)
|
||||||
|
{
|
||||||
|
/* some FreeRTOS stuff is cleaned up by idle task */
|
||||||
|
vTaskDelay(5);
|
||||||
|
|
||||||
|
/* check if unit test has caused heap corruption in any heap */
|
||||||
|
TEST_ASSERT( heap_caps_check_integrity(MALLOC_CAP_INVALID, true) );
|
||||||
|
|
||||||
|
/* check for leaks */
|
||||||
|
size_t after_free_8bit = heap_caps_get_free_size(MALLOC_CAP_8BIT);
|
||||||
|
size_t after_free_32bit = heap_caps_get_free_size(MALLOC_CAP_32BIT);
|
||||||
|
|
||||||
|
check_leak(before_free_8bit, after_free_8bit, "8BIT");
|
||||||
|
check_leak(before_free_32bit, after_free_32bit, "32BIT");
|
||||||
|
}
|
||||||
|
|
||||||
void unity_putc(int c)
|
void unity_putc(int c)
|
||||||
{
|
{
|
||||||
if (c == '\n')
|
if (c == '\n')
|
||||||
|
Reference in New Issue
Block a user