Skip to content

Commit

Permalink
Merge branch 'feature/heap_pytest' into 'master'
Browse files Browse the repository at this point in the history
heap: move unit tests to pytest

Closes IDF-5591

See merge request espressif/esp-idf!20229
  • Loading branch information
SoucheSouche committed Sep 23, 2022
2 parents 76834a7 + bf18a05 commit 1ecddf5
Show file tree
Hide file tree
Showing 50 changed files with 248 additions and 63 deletions.
4 changes: 0 additions & 4 deletions components/heap/test/CMakeLists.txt

This file was deleted.

6 changes: 6 additions & 0 deletions components/heap/test_apps/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# The following lines of boilerplate have to be in your project's
# CMakeLists in this exact order for cmake to work correctly
cmake_minimum_required(VERSION 3.16)

include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(test_heap)
2 changes: 2 additions & 0 deletions components/heap/test_apps/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-S2 | ESP32-S3 |
| ----------------- | ----- | -------- | -------- | -------- | -------- |
14 changes: 14 additions & 0 deletions components/heap/test_apps/main/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
set(src_test "test_app_main.c"
"test_aligned_alloc_caps.c"
"test_allocator_timings.c"
"test_corruption_check.c"
"test_diram.c"
"test_heap_trace.c"
"test_malloc_caps.c"
"test_malloc.c"
"test_realloc.c"
"test_runtime_heap_reg.c")

idf_component_register(SRCS ${src_test}
INCLUDE_DIRS "."
WHOLE_ARCHIVE)
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/*
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Unlicense OR CC0-1.0
*/
/*
Tests for the capabilities-based memory allocator.
*/
Expand All @@ -12,6 +17,7 @@
#include <sys/param.h>
#include <string.h>
#include <malloc.h>
#include <inttypes.h>

TEST_CASE("Capabilities aligned allocator test", "[heap]")
{
Expand All @@ -23,10 +29,10 @@ TEST_CASE("Capabilities aligned allocator test", "[heap]")
uint8_t *buf = (uint8_t *)memalign(alignments, (alignments + 137));
if(((alignments & (alignments - 1)) != 0) || (!alignments)) {
TEST_ASSERT( buf == NULL );
//printf("[ALIGNED_ALLOC] alignment: %u is not a power of two, don't allow allocation \n", aligments);
//printf("[ALIGNED_ALLOC] alignment: %"PRIu32" is not a power of two, don't allow allocation \n", aligments);
} else {
TEST_ASSERT( buf != NULL );
printf("[ALIGNED_ALLOC] alignment required: %u \n", alignments);
printf("[ALIGNED_ALLOC] alignment required: %"PRIu32" \n", alignments);
printf("[ALIGNED_ALLOC] address of allocated memory: %p \n\n", (void *)buf);
//Address of obtained block must be aligned with selected value
TEST_ASSERT(((intptr_t)buf & (alignments - 1)) == 0);
Expand All @@ -52,10 +58,10 @@ TEST_CASE("Capabilities aligned allocator test", "[heap]")
uint8_t *buf = (uint8_t *)heap_caps_aligned_alloc(alignments, 10*1024, MALLOC_CAP_SPIRAM);
if(((alignments & (alignments - 1)) != 0) || (!alignments)) {
TEST_ASSERT( buf == NULL );
//printf("[ALIGNED_ALLOC] alignment: %u is not a power of two, don't allow allocation \n", aligments);
//printf("[ALIGNED_ALLOC] alignment: %"PRIu32" is not a power of two, don't allow allocation \n", aligments);
} else {
TEST_ASSERT( buf != NULL );
printf("[ALIGNED_ALLOC] alignment required: %u \n", alignments);
printf("[ALIGNED_ALLOC] alignment required: %"PRIu32" \n", alignments);
printf("[ALIGNED_ALLOC] address of allocated memory: %p \n\n", (void *)buf);
//Address of obtained block must be aligned with selected value
TEST_ASSERT(((intptr_t)buf & (alignments - 1)) == 0);
Expand All @@ -80,10 +86,10 @@ TEST_CASE("Capabilities aligned calloc test", "[heap]")
uint8_t *buf = (uint8_t *)heap_caps_aligned_calloc(alignments, 1, (alignments + 137), MALLOC_CAP_DEFAULT);
if(((alignments & (alignments - 1)) != 0) || (!alignments)) {
TEST_ASSERT( buf == NULL );
//printf("[ALIGNED_ALLOC] alignment: %u is not a power of two, don't allow allocation \n", aligments);
//printf("[ALIGNED_ALLOC] alignment: %"PRIu32" is not a power of two, don't allow allocation \n", aligments);
} else {
TEST_ASSERT( buf != NULL );
printf("[ALIGNED_ALLOC] alignment required: %u \n", alignments);
printf("[ALIGNED_ALLOC] alignment required: %"PRIu32" \n", alignments);
printf("[ALIGNED_ALLOC] address of allocated memory: %p \n\n", (void *)buf);
//Address of obtained block must be aligned with selected value
TEST_ASSERT(((intptr_t)buf & (alignments - 1)) == 0);
Expand Down Expand Up @@ -120,10 +126,10 @@ TEST_CASE("Capabilities aligned calloc test", "[heap]")
uint8_t *buf = (uint8_t *)(uint8_t *)heap_caps_aligned_calloc(alignments, 1, 10*1024, MALLOC_CAP_SPIRAM);
if(((alignments & (alignments - 1)) != 0) || (!alignments)) {
TEST_ASSERT( buf == NULL );
//printf("[ALIGNED_ALLOC] alignment: %u is not a power of two, don't allow allocation \n", aligments);
//printf("[ALIGNED_ALLOC] alignment: %"PRIu32" is not a power of two, don't allow allocation \n", aligments);
} else {
TEST_ASSERT( buf != NULL );
printf("[ALIGNED_ALLOC] alignment required: %u \n", alignments);
printf("[ALIGNED_ALLOC] alignment required: %"PRIu32" \n", alignments);
printf("[ALIGNED_ALLOC] address of allocated memory: %p \n\n", (void *)buf);
//Address of obtained block must be aligned with selected value
TEST_ASSERT(((intptr_t)buf & (alignments - 1)) == 0);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/*
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Unlicense OR CC0-1.0
*/
#include "freertos/FreeRTOS.h"
#include <esp_types.h>
#include <stdio.h>
Expand All @@ -7,7 +12,6 @@
#include <stdlib.h>
#include <sys/param.h>
#include <string.h>
#include <test_utils.h>

//This test only makes sense with poisoning disabled (light or comprehensive)
#if !defined(CONFIG_HEAP_POISONING_COMPREHENSIVE) && !defined(CONFIG_HEAP_POISONING_LIGHT)
Expand All @@ -26,13 +30,13 @@ TEST_CASE("Heap many random allocations timings", "[heap]")
uint64_t realloc_time_average = 0;

for (int i = 0; i < ITERATIONS; i++) {
uint8_t n = esp_random() % NUM_POINTERS;
uint8_t n = (uint32_t)rand() % NUM_POINTERS;

if (esp_random() % 4 == 0) {
if (ITERATIONS % 4 == 0) {
/* 1 in 4 iterations, try to realloc the buffer instead
of using malloc/free
*/
size_t new_size = esp_random() % 1024;
size_t new_size = (uint32_t)rand() % 1024;

cycles_before = portGET_RUN_TIME_COUNTER_VALUE();
void *new_p = heap_caps_realloc(p[n], new_size, MALLOC_CAP_DEFAULT);
Expand Down
41 changes: 41 additions & 0 deletions components/heap/test_apps/main/test_app_main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/

#include "unity.h"
#include "unity_test_runner.h"
#include "esp_heap_caps.h"

#define TEST_MEMORY_LEAK_THRESHOLD (-1024)

static size_t before_free_8bit;
static size_t before_free_32bit;

static void check_leak(size_t before_free, size_t after_free, const char *type)
{
ssize_t delta = after_free - before_free;
printf("MALLOC_CAP_%s: Before %u bytes free, After %u bytes free (delta %d)\n", type, before_free, after_free, delta);
TEST_ASSERT_MESSAGE(delta >= TEST_MEMORY_LEAK_THRESHOLD, "memory leak");
}

void setUp(void)
{
before_free_8bit = heap_caps_get_free_size(MALLOC_CAP_8BIT);
before_free_32bit = heap_caps_get_free_size(MALLOC_CAP_32BIT);
}

void tearDown(void)
{
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 app_main(void)
{
printf("Running heap component tests\n");
unity_run_menu();
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

#include "esp_heap_caps.h"

//This test only makes sense with poisoning enabled (light or comprehensive)
#if defined(CONFIG_HEAP_POISONING_COMPREHENSIVE) || defined(CONFIG_HEAP_POISONING_LIGHT)

/* executing multi_heap_internal_check_block_poisoning()
* takes longer on external RAM and therefore the timeout
* in the test of 30 seconds is exceeded. Execute the test
Expand Down Expand Up @@ -66,3 +69,5 @@ TEST_CASE("multi_heap poisoning detection", "[heap]")
TEST_ASSERT_TRUE(is_heap_ok);
}
}

#endif
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/*
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Unlicense OR CC0-1.0
*/
/*
Tests for D/IRAM support in heap capability allocator
*/
Expand All @@ -12,6 +17,7 @@

#if !TEMPORARY_DISABLED_FOR_TARGETS(ESP32C2)
//IDF-5167
#ifndef CONFIG_ESP_SYSTEM_MEMPROT_FEATURE
static void *malloc_block_diram(uint32_t caps)
{
void *attempts[256] = { 0 }; // Allocate up to 256 ALLOC_SZ blocks to exhaust all non-D/IRAM memory temporarily
Expand Down Expand Up @@ -74,4 +80,5 @@ TEST_CASE("Allocate D/IRAM as IRAM", "[heap]")

free(iram);
}
#endif // CONFIG_ESP_SYSTEM_MEMPROT_FEATURE
#endif //!TEMPORARY_DISABLED_FOR_TARGETS(ESP32C2)
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/*
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Unlicense OR CC0-1.0
*/
/*
Generic test for heap tracing support
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/*
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Unlicense OR CC0-1.0
*/
/*
Generic test for malloc/free
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/*
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Unlicense OR CC0-1.0
*/
/*
Tests for the capabilities-based memory allocator.
*/
Expand Down Expand Up @@ -106,7 +111,7 @@ TEST_CASE("Capabilities allocator test", "[heap]")
free(m1);
printf("Done.\n");
}
#endif
#endif // CONFIG_ESP_SYSTEM_MEMPROT_FEATURE
#endif //!TEMPORARY_DISABLED_FOR_TARGETS(ESP32C2)

#ifdef CONFIG_ESP32_IRAM_AS_8BIT_ACCESSIBLE_MEMORY
Expand Down Expand Up @@ -175,6 +180,7 @@ TEST_CASE("heap_caps metadata test", "[heap]")
/* Small function runs from IRAM to check that malloc/free/realloc
all work OK when cache is disabled...
*/
#ifndef CONFIG_ESP_SYSTEM_MEMPROT_FEATURE
static IRAM_ATTR __attribute__((noinline)) bool iram_malloc_test(void)
{
spi_flash_guard_get()->start(); // Disables flash cache
Expand All @@ -196,6 +202,7 @@ TEST_CASE("heap_caps_xxx functions work with flash cache disabled", "[heap]")
{
TEST_ASSERT( iram_malloc_test() );
}
#endif // CONFIG_ESP_SYSTEM_MEMPROT_FEATURE
#endif //!TEMPORARY_DISABLED_FOR_TARGETS(ESP32C2)

#ifdef CONFIG_HEAP_ABORT_WHEN_ALLOCATION_FAILS
Expand All @@ -212,7 +219,7 @@ static bool called_user_failed_hook = false;

void heap_caps_alloc_failed_hook(size_t requested_size, uint32_t caps, const char *function_name)
{
printf("%s was called but failed to allocate %d bytes with 0x%X capabilities. \n",function_name, requested_size, caps);
printf("%s was called but failed to allocate %d bytes with 0x%lX capabilities. \n",function_name, requested_size, caps);
called_user_failed_hook = true;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/*
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Unlicense OR CC0-1.0
*/
/*
Generic test for realloc
*/
Expand Down Expand Up @@ -64,5 +69,5 @@ TEST_CASE("realloc move data to a new heap type", "[heap]")

free(c);
}
#endif
#endif // CONFIG_ESP_SYSTEM_MEMPROT_FEATURE
#endif //!TEMPORARY_DISABLED_FOR_TARGETS(ESP32C2)
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
/*
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Unlicense OR CC0-1.0
*/
/*
Tests for registering new heap memory at runtime
*/

#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#include "unity.h"
#include "esp_heap_caps_init.h"
#include "esp_system.h"
Expand All @@ -20,7 +26,7 @@ TEST_CASE("Allocate new heap at runtime", "[heap][ignore]")
uint32_t before_free = esp_get_free_heap_size();
TEST_ESP_OK( heap_caps_add_region((intptr_t)buffer, (intptr_t)buffer + BUF_SZ) );
uint32_t after_free = esp_get_free_heap_size();
printf("Before %u after %u\n", before_free, after_free);
printf("Before %"PRIu32" after %"PRIu32"\n", before_free, after_free);
/* allow for some 'heap overhead' from accounting structures */
TEST_ASSERT(after_free >= before_free + BUF_SZ - HEAP_OVERHEAD_MAX);
}
Expand Down Expand Up @@ -64,7 +70,7 @@ TEST_CASE("Add .bss memory to heap region runtime", "[heap][ignore]")
uint32_t before_free = esp_get_free_heap_size();
TEST_ESP_OK( heap_caps_add_region((intptr_t)s_buffer, (intptr_t)s_buffer + BUF_SZ) );
uint32_t after_free = esp_get_free_heap_size();
printf("Before %u after %u\n", before_free, after_free);
printf("Before %"PRIu32" after %"PRIu32"\n", before_free, after_free);
/* allow for some 'heap overhead' from accounting structures */
TEST_ASSERT(after_free >= before_free + BUF_SZ - HEAP_OVERHEAD_MAX);

Expand Down
66 changes: 66 additions & 0 deletions components/heap/test_apps/pytest_heap.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
# SPDX-License-Identifier: CC0-1.0

import pytest
from pytest_embedded import Dut


@pytest.mark.generic
@pytest.mark.supported_targets
@pytest.mark.parametrize(
'config',
[
'no_poisoning',
'light_poisoning',
'comprehensive_poisoning'
]
)
def test_heap_poisoning(dut: Dut) -> None:
dut.expect_exact('Press ENTER to see the list of tests')
dut.write('![ignore]')
dut.expect_unity_test_output(timeout=300)


@pytest.mark.generic
@pytest.mark.esp32
@pytest.mark.esp32s2
@pytest.mark.esp32s3
@pytest.mark.parametrize(
'config',
[
'psram',
'psram_all_ext'
]
)
def test_heap(dut: Dut) -> None:
dut.expect_exact('Press ENTER to see the list of tests')
dut.write('![ignore]')
dut.expect_unity_test_output(timeout=300)


@pytest.mark.generic
@pytest.mark.esp32
@pytest.mark.parametrize(
'config',
[
'abort_alloc_fail'
]
)
def test_heap_abort_on_alloc_failure(dut: Dut) -> None:
dut.expect_exact('Press ENTER to see the list of tests')
dut.write('"When enabled, allocation operation failure generates an abort"')
dut.expect('Backtrace: ')


@pytest.mark.generic
@pytest.mark.esp32
@pytest.mark.parametrize(
'config',
[
'8bit_access'
]
)
def test_heap_8bit_access(dut: Dut) -> None:
dut.expect_exact('Press ENTER to see the list of tests')
dut.write('"IRAM_8BIT capability test"')
dut.expect_unity_test_output(timeout=300)
2 changes: 2 additions & 0 deletions components/heap/test_apps/sdkconfig.ci.8bit_access
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
CONFIG_FREERTOS_UNICORE=y
CONFIG_ESP32_IRAM_AS_8BIT_ACCESSIBLE_MEMORY=y
1 change: 1 addition & 0 deletions components/heap/test_apps/sdkconfig.ci.abort_alloc_fail
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CONFIG_HEAP_ABORT_WHEN_ALLOCATION_FAILS=y
Loading

0 comments on commit 1ecddf5

Please sign in to comment.