feat(heap): Update test to CMake and use Catch2 component

This commit is contained in:
Guillaume Souchere
2025-07-08 09:40:49 +02:00
parent 903d613a4e
commit f8cb539e29
17 changed files with 99 additions and 107 deletions

2
.gitignore vendored
View File

@@ -66,8 +66,6 @@ build_summary_*.xml
coverage.info
coverage_report/
test_multi_heap_host
# VS Code Settings
.vscode/

View File

@@ -77,12 +77,6 @@ test_fatfsgen_on_host:
- ./test_wl_fatfsgen.py
- ./test_fatfsparse.py
test_multi_heap_on_host:
extends: .host_test_template
script:
- cd components/heap/test_multi_heap_host
- ./test_all_configs.sh
test_certificate_bundle_on_host:
extends: .host_test_template
script:

View File

@@ -0,0 +1,3 @@
components/heap/test_multi_heap_host:
enable:
- if: IDF_TARGET == "linux"

View File

@@ -0,0 +1,12 @@
cmake_minimum_required(VERSION 3.16)
add_compile_options(-m32)
add_link_options(-m32)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
set(COMPONENTS main)
# This test app doesn't require FreeRTOS, using mock instead
list(APPEND EXTRA_COMPONENT_DIRS "$ENV{IDF_PATH}/tools/mocks/freertos/")
project(multi_heap_test)

View File

@@ -1,54 +0,0 @@
TEST_PROGRAM=test_multi_heap
all: $(TEST_PROGRAM)
ifneq ($(filter clean,$(MAKECMDGOALS)),)
.NOTPARALLEL: # prevent make clean racing the other targets
endif
SOURCE_FILES = $(abspath \
test_multi_heap.cpp \
../multi_heap_poisoning.c \
../multi_heap.c \
../tlsf/tlsf.c \
main.cpp \
)
INCLUDE_FLAGS = -I../include -I../../../tools/catch -I../tlsf -I../tlsf/include
GCOV ?= gcov
CPPFLAGS += $(INCLUDE_FLAGS) -D CONFIG_LOG_DEFAULT_LEVEL -g -fstack-protector-all -m32
CFLAGS += -Wall -Werror -fprofile-arcs -ftest-coverage
CXXFLAGS += -std=c++11 -Wall -Werror -fprofile-arcs -ftest-coverage
LDFLAGS += -lstdc++ -fprofile-arcs -ftest-coverage -m32
OBJ_FILES = $(filter %.o, $(SOURCE_FILES:.cpp=.o) $(SOURCE_FILES:.c=.o))
COVERAGE_FILES = $(OBJ_FILES:.o=.gc*)
$(TEST_PROGRAM): $(OBJ_FILES)
g++ $(LDFLAGS) -o $(TEST_PROGRAM) $(OBJ_FILES)
$(OUTPUT_DIR):
mkdir -p $(OUTPUT_DIR)
test: $(TEST_PROGRAM)
./$(TEST_PROGRAM)
$(COVERAGE_FILES): $(TEST_PROGRAM) test
coverage.info: $(COVERAGE_FILES)
find ../ -name "*.gcno" -exec $(GCOV) -r -pb {} +
lcov --capture --directory $(abspath ../) --no-external --output-file coverage.info --gcov-tool $(GCOV)
coverage_report: coverage.info
genhtml coverage.info --output-directory coverage_report
@echo "Coverage report is in coverage_report/index.html"
clean:
rm -f $(OBJ_FILES) $(TEST_PROGRAM)
rm -f $(COVERAGE_FILES) *.gcov
rm -rf coverage_report/
rm -f coverage.info
.PHONY: clean all test

View File

@@ -0,0 +1,2 @@
| Supported Targets | Linux |
| ----------------- | ----- |

View File

@@ -1,8 +0,0 @@
/*
* SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#define CATCH_CONFIG_MAIN
#include "catch.hpp"

View File

@@ -0,0 +1,13 @@
idf_component_register(SRCS "test_multi_heap.cpp"
"../../multi_heap_poisoning.c"
"../../multi_heap.c"
"../../tlsf/tlsf.c"
INCLUDE_DIRS
"../../include"
"../../tlsf"
"../../tlsf/include"
WHOLE_ARCHIVE)
# Currently 'main' for IDF_TARGET=linux is defined in freertos component.
# Since we are using a freertos mock here, need to let Catch2 provide 'main'.
target_link_libraries(${COMPONENT_LIB} PRIVATE Catch2WithMain)

View File

@@ -0,0 +1,2 @@
dependencies:
espressif/catch2: "^3.4.0"

View File

@@ -1,10 +1,10 @@
/*
* SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2024-2025 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "catch.hpp"
#include "catch2/catch_test_macros.hpp"
#include "multi_heap.h"
#include "../multi_heap_config.h"
@@ -42,11 +42,28 @@ static void __free__(void *ptr)
#undef realloc
#define realloc #error
static uint8_t s_small_heap[4 * 1024];
static multi_heap_handle_t s_small_heap_hdl = NULL;
void get_small_heap(uint8_t **heap, multi_heap_handle_t *heap_hdl, size_t *heap_size)
{
if (s_small_heap_hdl == NULL) {
memset(s_small_heap, 0, sizeof(s_small_heap));
s_small_heap_hdl = multi_heap_register(s_small_heap, sizeof(s_small_heap));
}
*heap = s_small_heap;
*heap_hdl = s_small_heap_hdl;
*heap_size = sizeof(s_small_heap);
multi_heap_dump(*heap_hdl);
}
TEST_CASE("multi_heap simple allocations", "[multi_heap]")
{
uint8_t small_heap[4 * 1024];
multi_heap_handle_t heap = multi_heap_register(small_heap, sizeof(small_heap));
uint8_t *small_heap = NULL;
multi_heap_handle_t heap = NULL;
size_t small_heap_size = 0;
get_small_heap(&small_heap, &heap, &small_heap_size);
size_t test_alloc_size = (multi_heap_free_size(heap) + 4) / 2;
@@ -59,7 +76,7 @@ TEST_CASE("multi_heap simple allocations", "[multi_heap]")
printf("small_heap %p buf %p\n", small_heap, buf);
REQUIRE( buf != NULL );
REQUIRE((intptr_t)buf >= (intptr_t)small_heap);
REQUIRE( (intptr_t)buf < (intptr_t)(small_heap + sizeof(small_heap)));
REQUIRE( (intptr_t)buf < (intptr_t)(small_heap + small_heap_size));
REQUIRE( multi_heap_get_allocated_size(heap, buf) >= test_alloc_size );
printf("test alloc size %d\n", test_alloc_size);
@@ -82,12 +99,12 @@ TEST_CASE("multi_heap simple allocations", "[multi_heap]")
REQUIRE( multi_heap_free_size(heap) > multi_heap_minimum_free_size(heap) );
}
TEST_CASE("multi_heap fragmentation", "[multi_heap]")
{
const size_t HEAP_SIZE = 4 * 1024;
uint8_t small_heap[HEAP_SIZE];
multi_heap_handle_t heap = multi_heap_register(small_heap, sizeof(small_heap));
uint8_t *small_heap = NULL;
multi_heap_handle_t heap = NULL;
size_t small_heap_size = 0;
get_small_heap(&small_heap, &heap, &small_heap_size);
const size_t alloc_size = 500;
@@ -135,13 +152,15 @@ TEST_CASE("multi_heap fragmentation", "[multi_heap]")
/* Test that malloc/free does not leave free space fragmented */
TEST_CASE("multi_heap defrag", "[multi_heap]")
{
uint8_t *small_heap = NULL;
multi_heap_handle_t heap = NULL;
size_t small_heap_size = 0;
get_small_heap(&small_heap, &heap, &small_heap_size);
void *p[4];
uint8_t small_heap[4 * 1024];
multi_heap_info_t info, info2;
multi_heap_handle_t heap = multi_heap_register(small_heap, sizeof(small_heap));
printf("0 ---\n");
multi_heap_dump(heap);
REQUIRE( multi_heap_check(heap, true) );
multi_heap_get_info(heap, &info);
REQUIRE( 0 == info.allocated_blocks );
@@ -149,9 +168,13 @@ TEST_CASE("multi_heap defrag", "[multi_heap]")
printf("1 ---\n");
p[0] = multi_heap_malloc(heap, 128);
p[1] = multi_heap_malloc(heap, 32);
multi_heap_dump(heap);
REQUIRE(p[0] != NULL);
REQUIRE( multi_heap_check(heap, true) );
multi_heap_dump(heap);
p[1] = multi_heap_malloc(heap, 32);
REQUIRE(p[1] != NULL);
REQUIRE( multi_heap_check(heap, true) );
multi_heap_dump(heap);
printf("2 ---\n");
multi_heap_free(heap, p[0]);
@@ -388,7 +411,6 @@ TEST_CASE("multi_heap minimum-size allocations", "[multi_heap]")
uint8_t heapdata[4096];
void *p[sizeof(heapdata) / sizeof(void *)] = {NULL};
const size_t NUM_P = sizeof(p) / sizeof(void *);
size_t allocated_size = 0;
multi_heap_handle_t heap = multi_heap_register(heapdata, sizeof(heapdata));
size_t before_free = multi_heap_free_size(heap);

View File

@@ -0,0 +1,19 @@
# SPDX-FileCopyrightText: 2023-2025 Espressif Systems (Shanghai) CO LTD
# SPDX-License-Identifier: Unlicense OR CC0-1.0
import pytest
from pytest_embedded import Dut
from pytest_embedded_idf.utils import idf_parametrize
@pytest.mark.host_test
@pytest.mark.parametrize(
'config',
[
'no_poisoning',
'light_poisoning',
'comprehensive_poisoning',
],
)
@idf_parametrize('target', ['linux'], indirect=['target'])
def test_multi_heap_linux(dut: Dut) -> None:
dut.expect_exact('All tests passed', timeout=180)

View File

@@ -0,0 +1,3 @@
CONFIG_HEAP_POISONING_COMPREHENSIVE=y
CONFIG_HEAP_POISONING_LIGHT=n
CONFIG_HEAP_POISONING_DISABLED=n

View File

@@ -0,0 +1,3 @@
CONFIG_HEAP_POISONING_COMPREHENSIVE=n
CONFIG_HEAP_POISONING_LIGHT=y
CONFIG_HEAP_POISONING_DISABLED=n

View File

@@ -0,0 +1,3 @@
CONFIG_HEAP_POISONING_COMPREHENSIVE=n
CONFIG_HEAP_POISONING_LIGHT=n
CONFIG_HEAP_POISONING_DISABLED=y

View File

@@ -0,0 +1 @@
CONFIG_ESP_TASK_WDT_EN=n

View File

@@ -1,20 +0,0 @@
#!/usr/bin/env bash
#
# Run the test suite with all configurations enabled
#
FAIL=0
for FLAGS in "CONFIG_HEAP_POISONING_NONE" "CONFIG_HEAP_POISONING_LIGHT" "CONFIG_HEAP_POISONING_COMPREHENSIVE" ; do
echo "==== Testing with config: ${FLAGS} ===="
CPPFLAGS="-D${FLAGS}" make clean test || FAIL=1
done
make clean
if [ $FAIL == 0 ]; then
echo "All configurations passed"
else
echo "Some configurations failed, see log."
exit 1
fi

View File

@@ -12,7 +12,6 @@ components/fatfs/test_fatfsgen/test_fatfsgen.py
components/fatfs/test_fatfsgen/test_fatfsparse.py
components/fatfs/test_fatfsgen/test_wl_fatfsgen.py
components/fatfs/wl_fatfsgen.py
components/heap/test_multi_heap_host/test_all_configs.sh
components/mbedtls/esp_crt_bundle/gen_crt_bundle.py
components/mbedtls/esp_crt_bundle/test_gen_crt_bundle/test_gen_crt_bundle.py
components/nvs_flash/nvs_partition_generator/nvs_partition_gen.py