diff --git a/.github/workflows/daily-tests.yml b/.github/workflows/daily-tests.yml index 8ea74fefa358..4d4ed8da4a44 100644 --- a/.github/workflows/daily-tests.yml +++ b/.github/workflows/daily-tests.yml @@ -47,8 +47,5 @@ jobs: user-space-other-tools: uses: ./.github/workflows/tools.yml - cmocka-unit-tests: - uses: ./.github/workflows/unit-tests.yml - zephyr-builds: uses: ./.github/workflows/zephyr.yml diff --git a/.github/workflows/unit-tests.yml b/.github/workflows/unit-tests.yml deleted file mode 100644 index 3965c73168d0..000000000000 --- a/.github/workflows/unit-tests.yml +++ /dev/null @@ -1,30 +0,0 @@ ---- - -# https://thesofproject.github.io/latest/developer_guides/unit_tests.html - -name: Unit tests - -# 'workflow_dispatch' allows running this workflow manually from the -# 'Actions' tab - -# yamllint disable-line rule:truthy -on: [pull_request, workflow_dispatch, workflow_call] - -permissions: - contents: read - -# Group workflows together to cancel outdated runs, do not cancel manual runs -concurrency: - group: "${{ github.workflow }}-${{ github.event_name }}-\ - ${{ github.event_name == 'pull_request' && github.ref || github.run_id }}" - cancel-in-progress: true - -jobs: - cmocka_utests: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - with: {fetch-depth: 2, filter: 'tree:0'} - - - name: build and run all defconfigs - run: ./test/test-all-defconfigs.sh diff --git a/ARCHITECTURE.md b/ARCHITECTURE.md index d13985b5fa3f..322947fdbb20 100644 --- a/ARCHITECTURE.md +++ b/ARCHITECTURE.md @@ -15,7 +15,7 @@ The SOF firmware is built with a modular, layered architecture to support multip ### 2.1 OS Abstraction and RTOS * **Zephyr RTOS:** Modern SOF extensively leverages the Zephyr RTOS for core operating system services, including thread scheduling, synchronization, logging, and hardware abstraction. -* **Host builds:** The host library, testbench and unit tests run on the host, with POSIX replacements for the RTOS headers (`posix/include`). +* **Host builds:** The host library and testbench run on the host, with POSIX replacements for the RTOS headers (`posix/include`). ### 2.2 Inter-Process Communication (IPC) The IPC subsystem is responsible for all communication between the Host OS and the DSP Firmware. diff --git a/CMakeLists.txt b/CMakeLists.txt index 225a8fc35bcc..0a6be674ff41 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,19 +13,12 @@ endif() find_package(Python3 COMPONENTS Interpreter) set(PYTHON3 "${Python3_EXECUTABLE}") -option(BUILD_UNIT_TESTS "Build unit tests" OFF) -option(BUILD_UNIT_TESTS_HOST "Build unit tests for host" OFF) option(BUILD_COUNTERS "Enable build counter(s), timestamps and any other non-deterministic build features" OFF) -if(BUILD_UNIT_TESTS AND NOT BUILD_UNIT_TESTS_HOST) - message(FATAL_ERROR "Unit tests can only be built for the host, " - "use -DBUILD_UNIT_TESTS_HOST=ON") -endif() - # The firmware is built with Zephyr (scripts/xtensa-build-zephyr.py), this -# top-level project only builds the host library and the unit tests. -if(NOT (CONFIG_LIBRARY OR BUILD_UNIT_TESTS_HOST OR CONFIG_ZEPHYR_POSIX)) +# top-level project only builds the host library. +if(NOT (CONFIG_LIBRARY OR CONFIG_ZEPHYR_POSIX)) message(FATAL_ERROR "The XTOS firmware build has been removed, " "use scripts/xtensa-build-zephyr.py to build the firmware") endif() @@ -144,9 +137,3 @@ if(CONFIG_LIBRARY) # rest of this file is not needed for host build return() endif() - -if(BUILD_UNIT_TESTS) - enable_testing() - add_subdirectory(src/kernel/arch/${ARCH}) - add_subdirectory(test) -endif() diff --git a/CODEOWNERS b/CODEOWNERS index 810c969b2977..6870d744ab10 100644 --- a/CODEOWNERS +++ b/CODEOWNERS @@ -96,7 +96,6 @@ zephyr/lib/regions_mm.c @dabekjakub # dumb list of source files and generate too much noise. # /CMakeLists.txt TODO #/*/CMakeLists.txt TODO -# /test/cmocka/CMakeLists.txt TODO # There's a small enough number of files in tools/ and little # churn: keep it simple and take them all. diff --git a/README.md b/README.md index a5b739b1480b..51bf30ffdd83 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,15 @@ The script will guide you through the process of installing system dependencies, ## Running the tests -See [unit testing documentation](https://thesofproject.github.io/latest/developer_guides/unit_tests.html) +The unit tests are Zephyr ztest suites in `test/ztest/unit`, run on +`native_sim`. From the west workspace, as in CI +(`.github/workflows/zephyr-unit-tests.yml`): + +``` +west twister --testsuite-root sof/test/ztest/unit/ --platform native_sim --enable-asan --enable-lsan +``` + +See also the [unit testing documentation](https://thesofproject.github.io/latest/developer_guides/unit_tests.html). ## Deployment diff --git a/scripts/cmake/git-submodules.cmake b/scripts/cmake/git-submodules.cmake index 56c559491b90..95816fff0237 100644 --- a/scripts/cmake/git-submodules.cmake +++ b/scripts/cmake/git-submodules.cmake @@ -38,8 +38,7 @@ if(GIT_FOUND AND EXISTS "${SOF_ROOT_SOURCE_DIRECTORY}/.git") "There are submodule changes, check git status and git diff\n${stdout}") endif() - elseif(NOT BUILD_UNIT_TESTS AND - NOT CONFIG_LIBRARY) + elseif(NOT CONFIG_LIBRARY) message(FATAL_ERROR "${TOMLC99_MAKE} not found. You should have used 'git clone --recursive'. \ diff --git a/scripts/cmake/kconfig.cmake b/scripts/cmake/kconfig.cmake index 7d3dd4481b18..549bf64791b6 100644 --- a/scripts/cmake/kconfig.cmake +++ b/scripts/cmake/kconfig.cmake @@ -15,12 +15,9 @@ set(INIT_CONFIG "initial.config" CACHE STRING "Initial .config file") # - ".' is the top source directory. # - "src/kernel/arch/host/configs" holds the library (testbench) configurations # and is also used by defconfigs.cmake. -# - "test/cmocka/configs" holds the unit test configurations. # - First found wins. -# - If two directories ever use the same defconfig name then a full -# path must be used, e.g.: -DINIT_CONFIG=test/cmocka/configs/collision_defconfig -set(init_config_search_list "." "src/kernel/arch/host/configs" "test/cmocka/configs") +set(init_config_search_list "." "src/kernel/arch/host/configs") find_file(INIT_CONFIG_found NAMES ${INIT_CONFIG} diff --git a/scripts/run-mocks.sh b/scripts/run-mocks.sh deleted file mode 100755 index a73f2d3e29a5..000000000000 --- a/scripts/run-mocks.sh +++ /dev/null @@ -1,84 +0,0 @@ -#!/bin/bash -# SPDX-License-Identifier: BSD-3-Clause -# Copyright(c) 2021 Intel Corporation. All rights reserved. - - -# Stop on most errors -set -e - -SOFTOP=$(cd "$(dirname "$0")"/.. && pwd) - -usage() -{ - cat < - */ - -/* Memory mock for memmap */ -#define HEAP_SYSTEM_0_BASE 0xBE200000 -#define HEAP_SYS_RUNTIME_0_BASE 0xBE220000 -#define SOF_CORE_S_START 0xBE280000 -#define HEAP_RUNTIME_SHARED_BASE 0xBE290000 -#define HEAP_SYSTEM_SHARED_BASE 0xBE2B0000 -#define HEAP_RUNTIME_BASE 0xBE2C0000 -#define HEAP_BUFFER_BASE 0xBE3B0000 diff --git a/test/cmocka/include/test_group_generator.h b/test/cmocka/include/test_group_generator.h deleted file mode 100644 index b3cba0c320ae..000000000000 --- a/test/cmocka/include/test_group_generator.h +++ /dev/null @@ -1,27 +0,0 @@ -/* SPDX-License-Identifier: BSD-3-Clause - * - * Copyright(c) 2018 Intel Corporation. All rights reserved. - * - * Author: Michal Jerzy Wierzbicki - */ - -#include -#include -#include -#include - -/* CMOCKA SETUP */ -#define setup_alloc(ptr, type, size, offset) do {\ - if (ptr)\ - free(ptr);\ - ptr = malloc(sizeof(type) * (size) + offset);\ - if (ptr == NULL)\ - return -1;\ -} while (0) - -#define setup_part(result, setup_func) do {\ - result |= setup_func;\ - if (result)\ - return result;\ -} while (0) - diff --git a/test/cmocka/memory_mock.x.in b/test/cmocka/memory_mock.x.in deleted file mode 100644 index add0b217b077..000000000000 --- a/test/cmocka/memory_mock.x.in +++ /dev/null @@ -1,26 +0,0 @@ -#include "include/mock_memory.h" - -SECTIONS -{ - .static_uuid_entries (COPY) : ALIGN(1024) - { - *(*.static_uuids) - } - - .static_log_entries (COPY) : ALIGN(1024) - { - *(*.static_log*) - } - - _comp_init_start = .; - _comp_init_end = .; - _system_heap = HEAP_SYSTEM_0_BASE; - _system_heap_start = HEAP_SYSTEM_0_BASE; - _system_runtime_heap = HEAP_SYS_RUNTIME_0_BASE; - _sof_core_s_start = SOF_CORE_S_START; - _runtime_shared_heap = HEAP_RUNTIME_SHARED_BASE; - _system_shared_heap = HEAP_SYSTEM_SHARED_BASE; - _module_heap = HEAP_RUNTIME_BASE; - _buffer_heap = HEAP_BUFFER_BASE; -} -INSERT AFTER .text; diff --git a/test/cmocka/src/CMakeLists.txt b/test/cmocka/src/CMakeLists.txt deleted file mode 100644 index 1cdd0d2137a9..000000000000 --- a/test/cmocka/src/CMakeLists.txt +++ /dev/null @@ -1,3 +0,0 @@ -# SPDX-License-Identifier: BSD-3-Clause - -add_subdirectory(audio) diff --git a/test/cmocka/src/audio/CMakeLists.txt b/test/cmocka/src/audio/CMakeLists.txt deleted file mode 100644 index 1f1f9205bf9a..000000000000 --- a/test/cmocka/src/audio/CMakeLists.txt +++ /dev/null @@ -1,4 +0,0 @@ -# SPDX-License-Identifier: BSD-3-Clause - -add_subdirectory(component) -add_subdirectory(pipeline) diff --git a/test/cmocka/src/audio/component/CMakeLists.txt b/test/cmocka/src/audio/component/CMakeLists.txt deleted file mode 100644 index 50389cfe4535..000000000000 --- a/test/cmocka/src/audio/component/CMakeLists.txt +++ /dev/null @@ -1,26 +0,0 @@ -# SPDX-License-Identifier: BSD-3-Clause - -cmocka_test(comp_set_state - comp_set_state.c - ${PROJECT_SOURCE_DIR}/src/math/numbers.c - ${PROJECT_SOURCE_DIR}/src/audio/component.c - ${PROJECT_SOURCE_DIR}/src/audio/data_blob.c - ${PROJECT_SOURCE_DIR}/src/kernel/ipc/ipc3/helper.c - ${PROJECT_SOURCE_DIR}/test/cmocka/src/notifier_mocks.c - ${PROJECT_SOURCE_DIR}/src/ipc/ipc-common.c - ${PROJECT_SOURCE_DIR}/src/kernel/ipc/ipc-common-kernel.c - ${PROJECT_SOURCE_DIR}/src/ipc/ipc-helper.c - ${PROJECT_SOURCE_DIR}/src/audio/buffers/comp_buffer.c - ${PROJECT_SOURCE_DIR}/src/audio/buffers/audio_buffer.c - ${PROJECT_SOURCE_DIR}/src/audio/source_api_helper.c - ${PROJECT_SOURCE_DIR}/src/audio/sink_api_helper.c - ${PROJECT_SOURCE_DIR}/src/audio/sink_source_utils.c - ${PROJECT_SOURCE_DIR}/src/audio/audio_stream.c - ${PROJECT_SOURCE_DIR}/src/audio/pipeline/pipeline-graph.c - ${PROJECT_SOURCE_DIR}/src/audio/pipeline/pipeline-params.c - ${PROJECT_SOURCE_DIR}/src/audio/pipeline/pipeline-schedule.c - ${PROJECT_SOURCE_DIR}/src/audio/pipeline/pipeline-stream.c - ${PROJECT_SOURCE_DIR}/src/audio/pipeline/pipeline-xrun.c - ${PROJECT_SOURCE_DIR}/src/module/audio/source_api.c - ${PROJECT_SOURCE_DIR}/src/module/audio/sink_api.c -) diff --git a/test/cmocka/src/audio/pipeline/CMakeLists.txt b/test/cmocka/src/audio/pipeline/CMakeLists.txt deleted file mode 100644 index 27d27757d6ae..000000000000 --- a/test/cmocka/src/audio/pipeline/CMakeLists.txt +++ /dev/null @@ -1,78 +0,0 @@ -# SPDX-License-Identifier: BSD-3-Clause - -cmocka_test(pipeline_new - pipeline_new.c - ${PROJECT_SOURCE_DIR}/src/math/numbers.c - ${PROJECT_SOURCE_DIR}/src/audio/component.c - ${PROJECT_SOURCE_DIR}/src/kernel/ipc/ipc3/helper.c - ${PROJECT_SOURCE_DIR}/src/ipc/ipc-common.c - ${PROJECT_SOURCE_DIR}/src/kernel/ipc/ipc-common-kernel.c - ${PROJECT_SOURCE_DIR}/src/ipc/ipc-helper.c - ${PROJECT_SOURCE_DIR}/src/audio/buffers/comp_buffer.c - ${PROJECT_SOURCE_DIR}/src/audio/buffers/audio_buffer.c - ${PROJECT_SOURCE_DIR}/src/audio/source_api_helper.c - ${PROJECT_SOURCE_DIR}/src/audio/sink_api_helper.c - ${PROJECT_SOURCE_DIR}/src/audio/sink_source_utils.c - ${PROJECT_SOURCE_DIR}/src/audio/audio_stream.c - ${PROJECT_SOURCE_DIR}/src/module/audio/source_api.c - ${PROJECT_SOURCE_DIR}/src/module/audio/sink_api.c - ${PROJECT_SOURCE_DIR}/test/cmocka/src/notifier_mocks.c - ${PROJECT_SOURCE_DIR}/src/audio/pipeline/pipeline-graph.c - ${PROJECT_SOURCE_DIR}/src/audio/pipeline/pipeline-params.c - ${PROJECT_SOURCE_DIR}/src/audio/pipeline/pipeline-schedule.c - ${PROJECT_SOURCE_DIR}/src/audio/pipeline/pipeline-stream.c - ${PROJECT_SOURCE_DIR}/src/audio/pipeline/pipeline-xrun.c - ${PROJECT_SOURCE_DIR}/src/audio/component.c -) - -cmocka_test(pipeline_connect_upstream - pipeline_connect_upstream.c - pipeline_connection_mocks.c - ${PROJECT_SOURCE_DIR}/src/math/numbers.c - ${PROJECT_SOURCE_DIR}/src/audio/component.c - ${PROJECT_SOURCE_DIR}/src/kernel/ipc/ipc3/helper.c - ${PROJECT_SOURCE_DIR}/src/ipc/ipc-common.c - ${PROJECT_SOURCE_DIR}/src/kernel/ipc/ipc-common-kernel.c - ${PROJECT_SOURCE_DIR}/src/ipc/ipc-helper.c - ${PROJECT_SOURCE_DIR}/src/audio/buffers/comp_buffer.c - ${PROJECT_SOURCE_DIR}/src/audio/buffers/audio_buffer.c - ${PROJECT_SOURCE_DIR}/src/audio/source_api_helper.c - ${PROJECT_SOURCE_DIR}/src/audio/sink_api_helper.c - ${PROJECT_SOURCE_DIR}/src/audio/sink_source_utils.c - ${PROJECT_SOURCE_DIR}/src/audio/audio_stream.c - ${PROJECT_SOURCE_DIR}/src/module/audio/source_api.c - ${PROJECT_SOURCE_DIR}/src/module/audio/sink_api.c - ${PROJECT_SOURCE_DIR}/test/cmocka/src/notifier_mocks.c - ${PROJECT_SOURCE_DIR}/src/audio/pipeline/pipeline-graph.c - ${PROJECT_SOURCE_DIR}/src/audio/pipeline/pipeline-params.c - ${PROJECT_SOURCE_DIR}/src/audio/pipeline/pipeline-schedule.c - ${PROJECT_SOURCE_DIR}/src/audio/pipeline/pipeline-stream.c - ${PROJECT_SOURCE_DIR}/src/audio/pipeline/pipeline-xrun.c - ${PROJECT_SOURCE_DIR}/src/audio/component.c -) - -cmocka_test(pipeline_free - pipeline_free.c - pipeline_connection_mocks.c - ${PROJECT_SOURCE_DIR}/src/math/numbers.c - ${PROJECT_SOURCE_DIR}/src/audio/component.c - ${PROJECT_SOURCE_DIR}/src/kernel/ipc/ipc3/helper.c - ${PROJECT_SOURCE_DIR}/src/ipc/ipc-common.c - ${PROJECT_SOURCE_DIR}/src/kernel/ipc/ipc-common-kernel.c - ${PROJECT_SOURCE_DIR}/src/ipc/ipc-helper.c - ${PROJECT_SOURCE_DIR}/src/audio/buffers/comp_buffer.c - ${PROJECT_SOURCE_DIR}/src/audio/buffers/audio_buffer.c - ${PROJECT_SOURCE_DIR}/src/audio/source_api_helper.c - ${PROJECT_SOURCE_DIR}/src/audio/sink_api_helper.c - ${PROJECT_SOURCE_DIR}/src/audio/sink_source_utils.c - ${PROJECT_SOURCE_DIR}/src/audio/audio_stream.c - ${PROJECT_SOURCE_DIR}/src/module/audio/source_api.c - ${PROJECT_SOURCE_DIR}/src/module/audio/sink_api.c - ${PROJECT_SOURCE_DIR}/test/cmocka/src/notifier_mocks.c - ${PROJECT_SOURCE_DIR}/src/audio/pipeline/pipeline-graph.c - ${PROJECT_SOURCE_DIR}/src/audio/pipeline/pipeline-params.c - ${PROJECT_SOURCE_DIR}/src/audio/pipeline/pipeline-schedule.c - ${PROJECT_SOURCE_DIR}/src/audio/pipeline/pipeline-stream.c - ${PROJECT_SOURCE_DIR}/src/audio/pipeline/pipeline-xrun.c - ${PROJECT_SOURCE_DIR}/src/audio/component.c -) diff --git a/test/cmocka/src/audio/pipeline/pipeline_connection_mocks.c b/test/cmocka/src/audio/pipeline/pipeline_connection_mocks.c deleted file mode 100644 index db8b1e79cbf2..000000000000 --- a/test/cmocka/src/audio/pipeline/pipeline_connection_mocks.c +++ /dev/null @@ -1,102 +0,0 @@ -// SPDX-License-Identifier: BSD-3-Clause -// -// Copyright(c) 2018 Intel Corporation. All rights reserved. -// -// Author: Jakub Dabek - -#include -#include -#include "pipeline_connection_mocks.h" - -extern struct schedulers *schedulers; - -struct scheduler_ops schedule_mock_ops = { - .schedule_task_free = &schedule_task_mock_free, -}; - -void cleanup_test_data(struct pipeline_connect_data *data) -{ - list_init(&data->first->bsource_list); - list_init(&data->second->bsource_list); - comp_buffer_reset_sink_list(data->b1); - comp_buffer_reset_source_list(data->b1); - list_init(&data->first->bsink_list); - list_init(&data->second->bsink_list); - comp_buffer_reset_sink_list(data->b2); - comp_buffer_reset_source_list(data->b2); -} - -struct pipeline_connect_data *get_standard_connect_objects(void) -{ - struct pipeline_connect_data *pipeline_connect_data = calloc - (sizeof(struct pipeline_connect_data), 1); - - struct pipeline *pipe = &pipeline_connect_data->p; - - pipe->alloc = calloc(sizeof(*pipe->alloc), 1); - pipe->frames_per_sched = 5; - pipe->pipeline_id = PIPELINE_ID_SAME; - pipe->status = COMP_STATE_INIT; - - pipe->pipe_task = calloc(sizeof(struct task), 1); - pipe->pipe_task->type = SOF_SCHEDULE_EDF; - - schedulers = calloc(sizeof(struct schedulers), 1); - list_init(&schedulers->list); - - struct schedule_data *sch = calloc(sizeof(struct schedule_data), 1); - - list_init(&sch->list); - sch->type = SOF_SCHEDULE_EDF; - sch->ops = &schedule_mock_ops; - list_item_append(&sch->list, &schedulers->list); - - /* Bind the scheduler to the task, as schedule_task_init() would */ - pipe->pipe_task->sch = sch; - - struct comp_dev *first = calloc(sizeof(struct comp_dev), 1); - struct comp_ipc_config *first_comp = &first->ipc_config; - - first_comp->id = 3; - first_comp->pipeline_id = PIPELINE_ID_SAME; - list_init(&first->bsink_list); - list_init(&first->bsource_list); - pipeline_connect_data->first = first; - pipe->sched_comp = first; - - struct comp_dev *second = calloc(sizeof(struct comp_dev), 1); - struct comp_ipc_config *second_comp = &second->ipc_config; - - second_comp->id = 4; - second_comp->pipeline_id = PIPELINE_ID_DIFFERENT; - list_init(&second->bsink_list); - list_init(&second->bsource_list); - pipeline_connect_data->second = second; - - struct comp_buffer *buffer = calloc(sizeof(struct comp_buffer), 1); - - comp_buffer_set_source_component(buffer, first); - comp_buffer_set_sink_component(buffer, second); - comp_buffer_reset_sink_list(buffer); - comp_buffer_reset_source_list(buffer); - pipeline_connect_data->b1 = buffer; - - struct comp_buffer *buffer_2 = calloc(sizeof(struct comp_buffer), 1); - - comp_buffer_set_source_component(buffer_2, second); - comp_buffer_reset_sink_list(buffer_2); - comp_buffer_reset_source_list(buffer_2); - pipeline_connect_data->b2 = buffer_2; - - return pipeline_connect_data; -} - -void free_standard_connect_objects(struct pipeline_connect_data *data) -{ - free(data->p.alloc); - free(data->p.pipe_task); - free(data->p.sched_comp); - free(data->second); - free(data->b1); - free(data->b2); -} diff --git a/test/cmocka/src/audio/pipeline/pipeline_connection_mocks.h b/test/cmocka/src/audio/pipeline/pipeline_connection_mocks.h deleted file mode 100644 index 60eedabbd43b..000000000000 --- a/test/cmocka/src/audio/pipeline/pipeline_connection_mocks.h +++ /dev/null @@ -1,46 +0,0 @@ -/* SPDX-License-Identifier: BSD-3-Clause - * - * Copyright(c) 2018 Intel Corporation. All rights reserved. - * - * Author: Jakub Dabek - */ - -#include -#include -#include -#include -#include -#include -#include -#include - -#ifdef HAVE_MALLOC_H -#include -#else -#include -#endif - -#define PIPELINE_ID_SAME 3 -#define PIPELINE_ID_DIFFERENT 4 - -struct pipeline_connect_data { - struct pipeline p; - struct comp_dev *first; - struct comp_dev *second; - struct comp_buffer *b1; - struct comp_buffer *b2; -}; - -struct pipeline_connect_data *get_standard_connect_objects(void); -void free_standard_connect_objects(struct pipeline_connect_data *data); - -void cleanup_test_data(struct pipeline_connect_data *data); - -static inline int schedule_task_mock_free(void *data, struct task *task) -{ - task->state = SOF_TASK_STATE_FREE; - task->ops.run = NULL; - task->data = NULL; - - return 0; -} diff --git a/test/cmocka/src/audio/pipeline/pipeline_free.c b/test/cmocka/src/audio/pipeline/pipeline_free.c deleted file mode 100644 index 9878f4f5e42a..000000000000 --- a/test/cmocka/src/audio/pipeline/pipeline_free.c +++ /dev/null @@ -1,86 +0,0 @@ -// SPDX-License-Identifier: BSD-3-Clause -// -// Copyright(c) 2018 Intel Corporation. All rights reserved. -// -// Author: Jakub Dabek - -#include -#include -#include -#include -#include -#include -#include "pipeline_connection_mocks.h" -#include -#include -#include -#include - -#ifdef HAVE_MALLOC_H -#include -#else -#include -#endif - -/* mock free() - dont free as we inspect contents */ -void sof_heap_free(struct k_heap *heap, void *addr) -{ -} - -static int setup(void **state) -{ - pipeline_posn_init(sof_get()); - *state = get_standard_connect_objects(); - return 0; -} - -static int teardown(void **state) -{ - free_standard_connect_objects(*state); - free(*state); - return 0; -} - -static void test_audio_pipeline_free_return_value(void **state) -{ - struct pipeline_connect_data *test_data = *state; - struct pipeline result = test_data->p; - - cleanup_test_data(test_data); - - /*Testing component*/ - int err = pipeline_free(&result); - - assert_int_equal(err, 0); -} - -static void test_audio_pipeline_free_sheduler_task_free(void **state) -{ - struct pipeline_connect_data *test_data = *state; - struct pipeline result = test_data->p; - - cleanup_test_data(test_data); - - /*Testing component*/ - pipeline_free(&result); - - assert_int_equal(result.pipe_task->state, SOF_TASK_STATE_FREE); - assert_ptr_equal(NULL, result.pipe_task->data); - assert_ptr_equal(NULL, result.pipe_task->ops.run); -} - -int main(void) -{ - const struct CMUnitTest tests[] = { - cmocka_unit_test( - test_audio_pipeline_free_return_value - ), - cmocka_unit_test( - test_audio_pipeline_free_sheduler_task_free - ), - }; - - cmocka_set_message_output(CM_OUTPUT_TAP); - - return cmocka_run_group_tests(tests, setup, teardown); -} diff --git a/test/cmocka/src/audio/pipeline/pipeline_mocks.h b/test/cmocka/src/audio/pipeline/pipeline_mocks.h deleted file mode 100644 index 8a97813fc24b..000000000000 --- a/test/cmocka/src/audio/pipeline/pipeline_mocks.h +++ /dev/null @@ -1,29 +0,0 @@ -/* SPDX-License-Identifier: BSD-3-Clause - * - * Copyright(c) 2018 Intel Corporation. All rights reserved. - * - * Author: Jakub Dabek - */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -struct pipeline_new_setup_data { - uint32_t pipe_id; - uint32_t priority; - uint32_t comp_id; - struct comp_dev *comp_data; -}; diff --git a/test/cmocka/src/audio/pipeline/pipeline_new.c b/test/cmocka/src/audio/pipeline/pipeline_new.c deleted file mode 100644 index a68afe49e4be..000000000000 --- a/test/cmocka/src/audio/pipeline/pipeline_new.c +++ /dev/null @@ -1,75 +0,0 @@ -// SPDX-License-Identifier: BSD-3-Clause -// -// Copyright(c) 2018 Intel Corporation. All rights reserved. -// -// Author: Jakub Dabek - -#include -#include -#include -#include -#include "pipeline_mocks.h" -#include -#include -#include -#include - -#ifdef HAVE_MALLOC_H -#include -#else -#include -#endif - -static int setup(void **state) -{ - struct pipeline_new_setup_data *pipeline_data = malloc( - sizeof(struct pipeline_new_setup_data)); - - pipeline_posn_init(sof_get()); - - if (!pipeline_data) - return -1; - - pipeline_data->pipe_id = 1; - pipeline_data->priority = 2; - pipeline_data->comp_id = 3; - - *state = pipeline_data; - return 0; -} - -static int teardown(void **state) -{ - free(*state); - return 0; -} - -static void test_audio_pipeline_pipeline_new_creation(void **state) -{ - struct pipeline_new_setup_data *test_data = *state; - - /*Testing component*/ - struct pipeline *result = pipeline_new(NULL, - test_data->pipe_id, - test_data->priority, - test_data->comp_id, - NULL); - - /*Pipeline should have been created so pointer can't be null*/ - assert_non_null(result); - - rfree(result->msg->tx_data); - rfree(result->msg); - rfree(result); -} - -int main(void) -{ - const struct CMUnitTest tests[] = { - cmocka_unit_test(test_audio_pipeline_pipeline_new_creation), - }; - - cmocka_set_message_output(CM_OUTPUT_TAP); - - return cmocka_run_group_tests(tests, setup, teardown); -} diff --git a/test/cmocka/src/common_mocks.c b/test/cmocka/src/common_mocks.c deleted file mode 100644 index e911fd5cce46..000000000000 --- a/test/cmocka/src/common_mocks.c +++ /dev/null @@ -1,516 +0,0 @@ -// SPDX-License-Identifier: BSD-3-Clause -// -// Copyright(c) 2019-2020 Intel Corporation. All rights reserved. -// -// Author: Jakub Dabek -// Author: Karol Trzcinski - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#define WEAK __attribute__((weak)) - -/* global contexts */ -WEAK struct ipc *_ipc; -WEAK struct timer *platform_timer; -WEAK struct schedulers *schedulers; -WEAK struct sof sof; -WEAK struct tr_ctx buffer_tr; -WEAK struct tr_ctx comp_tr; -WEAK struct tr_ctx ipc_tr; - -int host_trace_level = LOG_LEVEL_ERROR; - -void WEAK *rballoc_align(uint32_t flags, size_t bytes, - uint32_t alignment) -{ - (void)flags; - - return calloc(bytes, 1); -} - -void WEAK *rzalloc(uint32_t flags, - size_t bytes) -{ - (void)flags; - - return calloc(bytes, 1); -} - -void WEAK *rmalloc_align(uint32_t flags, size_t bytes, uint32_t alignment) -{ - (void)flags; - (void)alignment; - - return malloc(bytes); -} - -void WEAK *rmalloc(uint32_t flags, size_t bytes) -{ - (void)flags; - - return malloc(bytes); -} - -void WEAK rfree(void *ptr) -{ - free(ptr); -} - -void WEAK *mod_balloc_align(struct processing_module *mod, size_t size, size_t alignment) -{ - void *ret; - (void)mod; - (void)alignment; - - ret = malloc(size); - - assert(ret); - - return ret; -} - -void WEAK *mod_alloc_ext(struct processing_module *mod, uint32_t flags, size_t size, - size_t alignment) -{ - void *ret; - (void)mod; - (void)flags; - (void)alignment; - - ret = malloc(size); - - assert(ret); - - return ret; -} - -int WEAK mod_free(struct processing_module *mod, const void *ptr) -{ - (void)mod; - free((void *)ptr); - return 0; -} - -struct k_heap * WEAK sof_sys_heap_get(void) -{ - return NULL; -} - -struct k_heap * WEAK sof_sys_user_heap_get(void) -{ - return NULL; -} - -void WEAK *sof_heap_alloc(struct k_heap *heap, uint32_t flags, size_t bytes, - size_t alignment) -{ - (void)heap; - (void)flags; - (void)alignment; - - return malloc(bytes); -} - -void WEAK sof_heap_free(struct k_heap *heap, void *addr) -{ - (void)heap; - - free(addr); -} - -int WEAK memcpy_s(void *dest, size_t dest_size, - const void *src, size_t count) -{ - if (!dest || !src) - return -EINVAL; - - if ((dest >= src && (char *)dest < ((char *)src + count)) || - (src >= dest && (char *)src < ((char *)dest + dest_size))) - return -EINVAL; - - if (count > dest_size) - return -EINVAL; - - memcpy(dest, src, count); - - return 0; -} - -int WEAK memset_s(void *dest, size_t size1, - int c, size_t size2) -{ - if (!dest) - return -EINVAL; - - memset(dest, c, size1); - - return 0; -} - -int WEAK rstrlen(const char *s) -{ - return strlen(s); -} - -void WEAK __panic(uint32_t p, const char *filename, uint32_t linenum) -{ - fail_msg("panic: %s:%d (code 0x%x)\n", filename, linenum, p); - exit(EXIT_FAILURE); -} - - -#if CONFIG_LIBRARY -volatile void *task_context_get(void); -#endif -volatile void * WEAK task_context_get(void) -{ - return NULL; -} - -uint64_t WEAK platform_timer_get(struct timer *timer) -{ - (void)timer; - - return 0; -} - -#if !CONFIG_LIBRARY -uint64_t WEAK arch_timer_get_system(struct timer *timer) -{ - (void)timer; - - return 0; -} -#endif - -#if CONFIG_LIBRARY -void WEAK arch_dump_regs_a(void *dump_buf); -#endif -void WEAK arch_dump_regs_a(void *dump_buf) -{ - (void)dump_buf; -} - -void WEAK heap_trace_all(int force) -{ - (void)force; -} - -void WEAK ipc_msg_send(struct ipc_msg *msg, void *data, bool high_priority) -{ - (void)msg; - (void)data; - (void)high_priority; -} - -int WEAK platform_ipc_init(struct ipc *ipc) -{ - return 0; -} - -enum task_state WEAK ipc_platform_do_cmd(struct ipc *ipc) -{ - return 0; -} - -void WEAK ipc_platform_complete_cmd(struct ipc *ipc) -{ -} - -#if !CONFIG_LIBRARY -int WEAK ipc_platform_send_msg(const struct ipc_msg *msg) -{ - return 0; -} - -void WEAK wait_delay(uint64_t number_of_clks) -{ -} - -void WEAK wait_delay_ms(uint64_t ms) -{ -} - -void WEAK wait_delay_us(uint64_t us) -{ -} - -void WEAK xthal_icache_region_invalidate(void *addr, unsigned size) -{ -} - -void WEAK xthal_dcache_region_invalidate(void *addr, unsigned size) -{ -} - -void WEAK xthal_dcache_region_writeback(void *addr, unsigned size) -{ -} - -void WEAK xthal_dcache_region_writeback_inv(void *addr, unsigned size) -{ -} -#endif - -struct sof * WEAK sof_get(void) -{ - return &sof; -} - -struct schedulers ** WEAK arch_schedulers_get(void) -{ - return &schedulers; -} - -int WEAK schedule_task_init(struct task *task, - const struct sof_uuid_entry *uid, uint16_t type, - int16_t priority, enum task_state (*run)(void *data), - void *data, uint16_t core, uint32_t flags) -{ - (void)task; - (void)uid; - (void)type; - (void)priority; - (void)run; - (void)data; - (void)core; - (void)flags; - - return 0; -} - -int WEAK schedule_task_init_ll(struct task *task, - const struct sof_uuid_entry *uid, uint16_t type, - int16_t priority, enum task_state (*run)(void *data), - void *data, uint16_t core, uint32_t flags) -{ - return 0; -} - -void WEAK platform_host_timestamp(struct comp_dev *host, - struct sof_ipc_stream_posn *posn) -{ - (void)host; - (void)posn; -} - -void WEAK platform_dai_timestamp(struct comp_dev *dai, - struct sof_ipc_stream_posn *posn) -{ - (void)dai; - (void)posn; -} - -struct ipc_comp_dev *WEAK ipc_get_comp_dev(struct ipc *ipc, uint16_t type, uint32_t id) -{ - (void)ipc; - (void)type; - (void)id; - - return NULL; -} - -struct ipc_comp_dev *WEAK ipc_get_comp_by_ppl_id(struct ipc *ipc, uint16_t type, - uint32_t ppl_id, uint32_t ignore_remote) -{ - (void)ipc; - (void)type; - (void)ppl_id; - (void)ignore_remote; - - return NULL; -} - -uint32_t WEAK crc32(uint32_t base, const void *data, uint32_t bytes) -{ - return 0; -} - -int WEAK comp_set_state(struct comp_dev *dev, int cmd) -{ - return 0; -} - -uint64_t WEAK clock_ms_to_ticks(int clock, uint64_t ms) -{ - (void)clock; - (void)ms; - - return 0; -} - -uint64_t WEAK clock_us_to_ticks(int clock, uint64_t us) -{ - (void)clock; - (void)us; - - return 0; -} - -uint64_t WEAK clock_ns_to_ticks(int clock, uint64_t us) -{ - (void)clock; - (void)us; - - return 0; -} - -#if CONFIG_MULTICORE && !CONFIG_LIBRARY - -int WEAK idc_send_msg(struct idc_msg *msg, uint32_t mode) -{ - (void)msg; - (void)mode; - - return 0; -} - -int WEAK arch_cpu_is_core_enabled(int id) -{ - return 0; -} - -#endif - -#if CONFIG_LIBRARY -/* enable trace by default in testbench */ -int WEAK test_bench_trace = 1; -int WEAK debug; - - -uint8_t * WEAK get_library_mailbox(void) -{ - return NULL; -} -#endif - -/* - * GCC xtensa requires us to fake some of the standard C library calls - * as this is "bare metal" support (i.e. with no host OS implementation - * methods for these calls). - * - * None of these IO calls are used in the Mocks for testing. - */ -#if !CONFIG_LIBRARY && !__XCC__ -void _exit(int status); -unsigned int _getpid_r(void); -int _kill_r(int id, int sig); -void _sbrk_r(int id); -int _fstat_r(int fd, void *buf); -int _open_r(const char *pathname, int flags); -int _write_r(int fd, char *buf, int count); -int _read_r(int fd, char *buf, int count); -int _lseek_r(int fd, int count); -int _close_r(int fd); -int _isatty(int fd); - -void _exit(int status) -{ - while (1); -} - -unsigned int _getpid_r(void) -{ - return 0; -} - -int _kill_r(int id, int sig) -{ - return 0; -} - -void _sbrk_r(int id) -{ -} - -int _fstat_r(int fd, void *buf) -{ - return 0; -} - -int _open_r(const char *pathname, int flags) -{ - return 0; -} - -int _write_r(int fd, char *buf, int count) -{ - return 0; -} - -int _read_r(int fd, char *buf, int count) -{ - return 0; -} - -int _lseek_r(int fd, int count) -{ - return 0; -} - -int _close_r(int fd) -{ - return 0; -} - -int _isatty(int fd) -{ - return 0; -} - -/* TODO: work around some linker warnings. Both will need fixed for qemu. */ -int _start = 0; -int *__errno _PARAMS ((void)); - -/* - * TODO: Math support for GCC xtensa requires a little more work to use the - * newlib versions. This is just to build test only today ! - */ -double __floatsidf(int i); -double __divdf3(double a, double b); -int __ledf2(double a, double b); -int __eqdf2(double a, double b); - -double __floatsidf(int i) -{ - return i; -} - -double __divdf3(double a, double b) -{ - return a / b; -} - -int WEAK __ledf2(double a, double b) -{ - return a <= b ? 0 : 1; -} - -int WEAK __eqdf2(double a, double b) -{ - return a == b ? 0 : 1; -} -#endif diff --git a/test/cmocka/src/util.h b/test/cmocka/src/util.h deleted file mode 100644 index 38561e81e42e..000000000000 --- a/test/cmocka/src/util.h +++ /dev/null @@ -1,82 +0,0 @@ -// SPDX-License-Identifier: BSD-3-Clause -// -// Copyright(c) 2019 Intel Corporation. All rights reserved. -// -// Author: Daniel Bogdzia -// Janusz Jankowski - -#include -#include -#include - -#include - -static inline struct comp_buffer *create_test_sink(struct comp_dev *dev, - uint32_t pipeline_id, - uint32_t frame_fmt, - uint16_t channels, - uint16_t buffer_size) -{ - struct sof_ipc_buffer desc = { - .comp = { - .pipeline_id = pipeline_id, - }, - .size = buffer_size, - }; - struct comp_buffer *buffer = buffer_new(NULL, &desc, BUFFER_USAGE_NOT_SHARED); - - memset(buffer->stream.addr, 0, buffer_size); - - /* set bsink list */ - if (dev) - list_item_append(&buffer->source_list, &dev->bsink_list); - - /* alloc sink and set default parameters */ - buffer->sink = calloc(1, sizeof(struct comp_dev)); - buffer->sink->state = COMP_STATE_PREPARE; - audio_stream_set_frm_fmt(&buffer->stream, frame_fmt); - audio_stream_set_channels(&buffer->stream, channels); - - return buffer; -} - -static inline void free_test_sink(struct comp_buffer *buffer) -{ - free(comp_buffer_get_sink_component(buffer)); - buffer_free(buffer); -} - -static inline struct comp_buffer *create_test_source(struct comp_dev *dev, - uint32_t pipeline_id, - uint32_t frame_fmt, - uint16_t channels, - uint16_t buffer_size) -{ - struct sof_ipc_buffer desc = { - .comp = { - .pipeline_id = pipeline_id, - }, - .size = buffer_size, - }; - struct comp_buffer *buffer = buffer_new(NULL, &desc, BUFFER_USAGE_NOT_SHARED); - - memset(buffer->stream.addr, 0, buffer_size); - - /*set bsource list */ - if (dev) - list_item_append(&buffer->sink_list, &dev->bsource_list); - - /* alloc source and set default parameters */ - buffer->source = calloc(1, sizeof(struct comp_dev)); - buffer->source->state = COMP_STATE_PREPARE; - audio_stream_set_frm_fmt(&buffer->stream, frame_fmt); - audio_stream_set_channels(&buffer->stream, channels); - - return buffer; -} - -static inline void free_test_source(struct comp_buffer *buffer) -{ - free(buffer->source); - buffer_free(buffer); -} diff --git a/test/test-all-defconfigs.sh b/test/test-all-defconfigs.sh deleted file mode 100755 index 6060c8de194f..000000000000 --- a/test/test-all-defconfigs.sh +++ /dev/null @@ -1,45 +0,0 @@ -#!/bin/sh - -set -e - -# Absolute -SOFTOP=$(cd "$(dirname "$0")"/.. && pwd) - -# Relative -BUILDTOP=build_ut_defs - -rebuild_config() -{ - local conf="$1" - mkdir -p "$BUILDTOP" - printf '\n ========= Building native cmocka tests for %s ======\n\n' "$conf" - - ( set -x - cmake -DINIT_CONFIG="$conf" -S "$SOFTOP" -B "$BUILDTOP"/"$conf" \ - -DBUILD_UNIT_TESTS=ON \ - -DBUILD_UNIT_TESTS_HOST=ON - - cmake --build "$BUILDTOP"/"$conf" -- -j"$(nproc)" - ) -} - -main() -{ - local defconfig - - # First make sure all configurations build - for d in "$SOFTOP"/test/cmocka/configs/*_defconfig; do - defconfig=$(basename "$d") - rebuild_config "$defconfig" - done - - # Now run all the tests - for d in "$BUILDTOP"/*_defconfig; do - printf '\n\n ========= Running native cmocka tests in %s ======\n\n' "$d" - ( set -x - cmake --build "$d" -- test - ) - done -} - -main "$@" diff --git a/test/cmocka/m/export_comment.m b/test/ztest/m/export_comment.m similarity index 100% rename from test/cmocka/m/export_comment.m rename to test/ztest/m/export_comment.m diff --git a/test/cmocka/m/export_get_git_describe.m b/test/ztest/m/export_get_git_describe.m similarity index 100% rename from test/cmocka/m/export_get_git_describe.m rename to test/ztest/m/export_get_git_describe.m diff --git a/test/cmocka/m/export_headerfile_open.m b/test/ztest/m/export_headerfile_open.m similarity index 100% rename from test/cmocka/m/export_headerfile_open.m rename to test/ztest/m/export_headerfile_open.m diff --git a/test/cmocka/m/export_ndefine.m b/test/ztest/m/export_ndefine.m similarity index 100% rename from test/cmocka/m/export_ndefine.m rename to test/ztest/m/export_ndefine.m diff --git a/test/cmocka/m/export_quant_qxy.m b/test/ztest/m/export_quant_qxy.m similarity index 100% rename from test/cmocka/m/export_quant_qxy.m rename to test/ztest/m/export_quant_qxy.m diff --git a/test/cmocka/m/export_sdefine.m b/test/ztest/m/export_sdefine.m similarity index 100% rename from test/cmocka/m/export_sdefine.m rename to test/ztest/m/export_sdefine.m diff --git a/test/cmocka/m/export_vector.m b/test/ztest/m/export_vector.m similarity index 100% rename from test/cmocka/m/export_vector.m rename to test/ztest/m/export_vector.m diff --git a/test/ztest/unit/audio/component/CMakeLists.txt b/test/ztest/unit/audio/component/CMakeLists.txt new file mode 100644 index 000000000000..c07226323279 --- /dev/null +++ b/test/ztest/unit/audio/component/CMakeLists.txt @@ -0,0 +1,37 @@ +# SPDX-License-Identifier: BSD-3-Clause +# +# Copyright(c) 2026 Intel Corporation. +# +# Audio component unit tests converted from CMock to Ztest + +cmake_minimum_required(VERSION 3.20.0) + +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) +project(sof_unit_audio_component) + +set(SOF_ROOT "${PROJECT_SOURCE_DIR}/../../../../..") +set(sof_top_dir ${SOF_ROOT}) + +include(${SOF_ROOT}/scripts/cmake/misc.cmake) +include(${SOF_ROOT}/scripts/cmake/uuid-registry.cmake) + +target_include_directories(app PRIVATE + ${SOF_ROOT}/zephyr/include + ${SOF_ROOT}/src/include + ${SOF_ROOT}/src/kernel/platform/posix/include + ${SOF_ROOT}/src/platform/posix/include + ${PROJECT_BINARY_DIR}/include/generated +) + +target_compile_definitions(app PRIVATE + -DCONFIG_ZEPHYR_POSIX=1 + -DCONFIG_LIBRARY=1 + -DUNIT_TEST=1 +) + +target_sources(app PRIVATE + comp_set_state_ztest.c + ${SOF_ROOT}/src/audio/component.c +) + +sof_append_relative_path_definitions(app) diff --git a/test/cmocka/src/audio/component/comp_set_state.c b/test/ztest/unit/audio/component/comp_set_state_ztest.c similarity index 76% rename from test/cmocka/src/audio/component/comp_set_state.c rename to test/ztest/unit/audio/component/comp_set_state_ztest.c index c643110e167d..1cd1717e5bdf 100644 --- a/test/cmocka/src/audio/component/comp_set_state.c +++ b/test/ztest/unit/audio/component/comp_set_state_ztest.c @@ -1,24 +1,26 @@ // SPDX-License-Identifier: BSD-3-Clause // -// Copyright(c) 2018 Intel Corporation. All rights reserved. +// Copyright(c) 2026 Intel Corporation. // +// These contents may have been developed with support from one or more +// Intel-operated generative artificial intelligence solutions. +// +// Converted from CMock to Ztest +// Original tests from test/cmocka/src/audio/component/comp_set_state.c: // Author: Bartosz Kokoszko +#include + #include -#include +#include -#include -#include -#include +#include #include -#include -#include enum test_type { SUCCEED = 0, FAIL, CORRECT_OUTPUT_STATE, - }; struct test_case { @@ -42,7 +44,7 @@ enum { NULL_STATE = -1 }; -struct test_case test_cases[] = { +static const struct test_case test_cases[] = { /*succeed set state*/ TEST_CASE(SUCCEED, COMP_STATE_PREPARE, @@ -141,7 +143,7 @@ struct test_case test_cases[] = { NULL_STATE), TEST_CASE(FAIL, COMP_STATE_PREPARE, COMP_TRIGGER_PRE_RELEASE, NULL_STATE), - + /*correct output state*/ TEST_CASE(CORRECT_OUTPUT_STATE, COMP_STATE_PREPARE, COMP_TRIGGER_PRE_START, COMP_STATE_PRE_ACTIVE), @@ -185,67 +187,68 @@ struct test_case test_cases[] = { COMP_TRIGGER_PREPARE, COMP_STATE_PREPARE), }; -static void test_audio_component_comp_set_state_succeed(struct test_case *tc) +/** + * @brief Run comp_set_state() on a component in the input state of a test case. + * @param[in] tc Test case. + * @param[out] state State of the component after the call. + * @return Return value of comp_set_state(). + */ +static int run_comp_set_state(const struct test_case *tc, uint16_t *state) { - struct comp_dev test_dev; + struct comp_dev test_dev = { + .state = tc->in_state, + }; + int ret = comp_set_state(&test_dev, tc->cmd); - test_dev.state = tc->in_state; + *state = test_dev.state; - assert_int_equal(comp_set_state(&test_dev, tc->cmd), 0); + return ret; } -static void test_audio_component_comp_set_state_correct_output_state(struct test_case *tc) +/** + * @brief Verify that comp_set_state() accepts the valid state transitions + */ +ZTEST(audio_component_suite, test_audio_component_comp_set_state_succeed) { - struct comp_dev test_dev; + uint16_t state; - test_dev.state = tc->in_state; - comp_set_state(&test_dev, tc->cmd); + ARRAY_FOR_EACH_PTR(test_cases, tc) { + if (tc->type != SUCCEED) + continue; - assert_int_equal(test_dev.state, tc->out_state); + zassert_equal(run_comp_set_state(tc, &state), 0, "%s", tc->name); + } } -static void test_audio_component_comp_set_state_fail(struct test_case *tc) +/** + * @brief Verify that comp_set_state() rejects the invalid state transitions + */ +ZTEST(audio_component_suite, test_audio_component_comp_set_state_fail) { - struct comp_dev test_drv; + uint16_t state; - test_drv.state = tc->in_state; - assert_int_equal(comp_set_state(&test_drv, tc->cmd), -EINVAL); -} + ARRAY_FOR_EACH_PTR(test_cases, tc) { + if (tc->type != FAIL) + continue; -static void test_audio_component_comp_set_state(void **state) -{ - struct test_case *tc = *((struct test_case **) state); - - switch (tc->type) { - case SUCCEED: - test_audio_component_comp_set_state_succeed(tc); - break; - case FAIL: - test_audio_component_comp_set_state_fail(tc); - break; - case CORRECT_OUTPUT_STATE: - test_audio_component_comp_set_state_correct_output_state(tc); - break; + zassert_equal(run_comp_set_state(tc, &state), -EINVAL, "%s", tc->name); } } -int main(void) +/** + * @brief Verify the component state after comp_set_state() + */ +ZTEST(audio_component_suite, test_audio_component_comp_set_state_correct_output_state) { - struct CMUnitTest tests[ARRAY_SIZE(test_cases)]; + uint16_t state; - int i; - - for (i = 0; i < ARRAY_SIZE(test_cases); i++) { - struct CMUnitTest *t = &tests[i]; - - t->name = test_cases[i].name; - t->test_func = test_audio_component_comp_set_state; - t->initial_state = &test_cases[i]; - t->setup_func = NULL; - t->teardown_func = NULL; + ARRAY_FOR_EACH_PTR(test_cases, tc) { + if (tc->type != CORRECT_OUTPUT_STATE) + continue; + run_comp_set_state(tc, &state); + zassert_equal(state, tc->out_state, "%s", tc->name); } - cmocka_set_message_output(CM_OUTPUT_TAP); - - return cmocka_run_group_tests(tests, NULL, NULL); } + +ZTEST_SUITE(audio_component_suite, NULL, NULL, NULL, NULL, NULL); diff --git a/test/ztest/unit/audio/component/prj.conf b/test/ztest/unit/audio/component/prj.conf new file mode 100644 index 000000000000..4c9488ad0f35 --- /dev/null +++ b/test/ztest/unit/audio/component/prj.conf @@ -0,0 +1,9 @@ +# SPDX-License-Identifier: BSD-3-Clause +# +# Copyright(c) 2026 Intel Corporation. + +CONFIG_ZTEST=y +CONFIG_SOF_FULL_ZEPHYR_APPLICATION=n +# Same IPC version as the CMocka unit_test_defconfig build: comp_set_state() +# returns COMP_STATUS_STATE_ALREADY_SET for the current state only with IPC3 +CONFIG_IPC_MAJOR_3=y diff --git a/test/ztest/unit/audio/component/testcase.yaml b/test/ztest/unit/audio/component/testcase.yaml new file mode 100644 index 000000000000..dbec6b2242a0 --- /dev/null +++ b/test/ztest/unit/audio/component/testcase.yaml @@ -0,0 +1,25 @@ +# SPDX-License-Identifier: BSD-3-Clause +# +# Copyright(c) 2026 Intel Corporation. +# +# Audio component unit tests converted from CMock to Ztest +# +# These contents may have been developed with support from one or more Intel-operated +# generative artificial intelligence solutions. +# + +common: + tags: + - SOF + - unit_test + - audio + - component + integration_platforms: + - native_sim + arch_exclude: xtensa + +tests: + sof.unit.audio.component: + platform_allow: + - native_sim + build_only: false diff --git a/test/ztest/unit/audio/dcblock/CMakeLists.txt b/test/ztest/unit/audio/dcblock/CMakeLists.txt index d38e4a45c4de..10d133065aed 100644 --- a/test/ztest/unit/audio/dcblock/CMakeLists.txt +++ b/test/ztest/unit/audio/dcblock/CMakeLists.txt @@ -23,8 +23,7 @@ target_include_directories(app PRIVATE ${SOF_ROOT}/src/platform/posix/include ${SOF_ROOT}/src/audio ${SOF_ROOT}/src/audio/dcblock - # cmocka_chirp_2ch.h, still shared with the remaining CMocka tests - ${SOF_ROOT}/test/cmocka/include + ${SOF_ROOT}/test/ztest/unit/include ${PROJECT_BINARY_DIR}/include/generated ) diff --git a/test/ztest/unit/audio/dcblock/dcblock_process_ztest.c b/test/ztest/unit/audio/dcblock/dcblock_process_ztest.c index b188e2d539b9..4992b7b9264d 100644 --- a/test/ztest/unit/audio/dcblock/dcblock_process_ztest.c +++ b/test/ztest/unit/audio/dcblock/dcblock_process_ztest.c @@ -34,7 +34,7 @@ #include "dcblock.h" -/* Shared with the remaining CMocka tests, from test/cmocka/include */ +/* Shared test signal, from test/ztest/unit/include */ #include /* Allow a few LSB of error due to the internal Q-format rounding. */ diff --git a/test/ztest/unit/audio/drc/CMakeLists.txt b/test/ztest/unit/audio/drc/CMakeLists.txt index 7acd0f49104d..369bab3553e5 100644 --- a/test/ztest/unit/audio/drc/CMakeLists.txt +++ b/test/ztest/unit/audio/drc/CMakeLists.txt @@ -21,7 +21,7 @@ target_include_directories(app PRIVATE ${SOF_ROOT}/src/kernel/platform/posix/include ${SOF_ROOT}/src/platform/posix/include ${SOF_ROOT}/src/audio - ${SOF_ROOT}/test/cmocka/include + ${SOF_ROOT}/test/ztest/unit/include ${PROJECT_BINARY_DIR}/include/generated ) diff --git a/test/ztest/unit/audio/eq_fir/CMakeLists.txt b/test/ztest/unit/audio/eq_fir/CMakeLists.txt index 6e6744395c4f..a020633bbdaf 100644 --- a/test/ztest/unit/audio/eq_fir/CMakeLists.txt +++ b/test/ztest/unit/audio/eq_fir/CMakeLists.txt @@ -22,8 +22,7 @@ target_include_directories(app PRIVATE ${SOF_ROOT}/src/kernel/platform/posix/include ${SOF_ROOT}/src/platform/posix/include ${SOF_ROOT}/src/audio - # cmocka_chirp_2ch.h, still shared with the remaining CMocka tests - ${SOF_ROOT}/test/cmocka/include + ${SOF_ROOT}/test/ztest/unit/include ${PROJECT_BINARY_DIR}/include/generated ) diff --git a/test/ztest/unit/audio/eq_fir/eq_fir_process_ztest.c b/test/ztest/unit/audio/eq_fir/eq_fir_process_ztest.c index d7dc1ff41c49..1203b4baa9d0 100644 --- a/test/ztest/unit/audio/eq_fir/eq_fir_process_ztest.c +++ b/test/ztest/unit/audio/eq_fir/eq_fir_process_ztest.c @@ -35,7 +35,7 @@ #include -/* Shared with the remaining CMocka tests, from test/cmocka/include */ +/* Shared test signal, from test/ztest/unit/include */ #include #include "cmocka_fir_ref.h" diff --git a/test/ztest/unit/audio/eq_iir/CMakeLists.txt b/test/ztest/unit/audio/eq_iir/CMakeLists.txt index f19a9b0d2483..482a3f3aa01b 100644 --- a/test/ztest/unit/audio/eq_iir/CMakeLists.txt +++ b/test/ztest/unit/audio/eq_iir/CMakeLists.txt @@ -22,8 +22,7 @@ target_include_directories(app PRIVATE ${SOF_ROOT}/src/kernel/platform/posix/include ${SOF_ROOT}/src/platform/posix/include ${SOF_ROOT}/src/audio - # cmocka_chirp_2ch.h, still shared with the remaining CMocka tests - ${SOF_ROOT}/test/cmocka/include + ${SOF_ROOT}/test/ztest/unit/include ${PROJECT_BINARY_DIR}/include/generated ) diff --git a/test/ztest/unit/audio/eq_iir/eq_iir_process_ztest.c b/test/ztest/unit/audio/eq_iir/eq_iir_process_ztest.c index 32cf5d3eb6b3..d33c5912d424 100644 --- a/test/ztest/unit/audio/eq_iir/eq_iir_process_ztest.c +++ b/test/ztest/unit/audio/eq_iir/eq_iir_process_ztest.c @@ -34,7 +34,7 @@ #include -/* Shared with the remaining CMocka tests, from test/cmocka/include */ +/* Shared test signal, from test/ztest/unit/include */ #include #include "cmocka_chirp_iir_ref_2ch.h" diff --git a/test/ztest/unit/audio/pipeline/CMakeLists.txt b/test/ztest/unit/audio/pipeline/CMakeLists.txt new file mode 100644 index 000000000000..f48b785534ca --- /dev/null +++ b/test/ztest/unit/audio/pipeline/CMakeLists.txt @@ -0,0 +1,60 @@ +# SPDX-License-Identifier: BSD-3-Clause +# +# Copyright(c) 2026 Intel Corporation. +# +# Audio pipeline unit tests converted from CMock to Ztest + +cmake_minimum_required(VERSION 3.20.0) + +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) +project(sof_unit_audio_pipeline) + +set(SOF_ROOT "${PROJECT_SOURCE_DIR}/../../../../..") +set(sof_top_dir ${SOF_ROOT}) + +include(${SOF_ROOT}/scripts/cmake/misc.cmake) +include(${SOF_ROOT}/scripts/cmake/uuid-registry.cmake) + +target_include_directories(app PRIVATE + ${SOF_ROOT}/zephyr/include + ${SOF_ROOT}/src/include + ${SOF_ROOT}/src/kernel/include + ${SOF_ROOT}/src/kernel/platform/posix/include + ${SOF_ROOT}/src/platform/posix/include + ${PROJECT_BINARY_DIR}/include/generated +) + +target_compile_definitions(app PRIVATE + -DCONFIG_ZEPHYR_POSIX=1 + -DCONFIG_LIBRARY=1 + -DUNIT_TEST=1 +) + +# Keep full ASan/LSan allocation stacks (twister --enable-asan --enable-lsan) +if(CONFIG_ASAN) + target_compile_options(app PRIVATE -fno-omit-frame-pointer) +endif() + +target_sources(app PRIVATE + pipeline_new_ztest.c + pipeline_connect_upstream_ztest.c + pipeline_free_ztest.c + pipeline_connection_mocks.c + ${SOF_ROOT}/test/ztest/unit/common/sof_context.c + ${SOF_ROOT}/zephyr/lib/alloc.c + # sof_context.c needs sys_comp_init() + ${SOF_ROOT}/src/audio/component.c + ${SOF_ROOT}/src/audio/pipeline/pipeline-graph.c + # pipeline_free() needs pipeline_comp_ll_task_free() + ${SOF_ROOT}/src/audio/pipeline/pipeline-schedule.c + # pipeline_new() needs ipc_build_stream_posn() and ipc_msg_init(), + # pipeline_free() needs ipc_msg_list_remove() + ${SOF_ROOT}/src/kernel/ipc/ipc3/helper.c + ${SOF_ROOT}/src/ipc/ipc-common.c + ${SOF_ROOT}/src/kernel/ipc/ipc-common-kernel.c +) + +# pipeline_free_ztest.c turns sof_heap_free() into a no-op while its suite runs +target_link_libraries(app PRIVATE "-Wl,--wrap=sof_heap_free") + +sof_append_relative_path_definitions(app) diff --git a/test/cmocka/src/audio/pipeline/pipeline_connect_upstream.c b/test/ztest/unit/audio/pipeline/pipeline_connect_upstream_ztest.c similarity index 50% rename from test/cmocka/src/audio/pipeline/pipeline_connect_upstream.c rename to test/ztest/unit/audio/pipeline/pipeline_connect_upstream_ztest.c index e911f0238162..e60f090a676c 100644 --- a/test/cmocka/src/audio/pipeline/pipeline_connect_upstream.c +++ b/test/ztest/unit/audio/pipeline/pipeline_connect_upstream_ztest.c @@ -1,37 +1,51 @@ // SPDX-License-Identifier: BSD-3-Clause // -// Copyright(c) 2018 Intel Corporation. All rights reserved. +// Copyright(c) 2026 Intel Corporation. // +// These contents may have been developed with support from one or more +// Intel-operated generative artificial intelligence solutions. +// +// Converted from CMock to Ztest +// Original tests from test/cmocka/src/audio/pipeline/pipeline_connect_upstream.c: // Author: Jakub Dabek -#include +#include + +#include #include #include -#include -#include -#include "pipeline_mocks.h" +#include + +#include + #include "pipeline_connection_mocks.h" -#include -#include -#include -#include -static int setup(void **state) +/* + * Every case gets new test objects, so that the cases do not depend on each + * other or on their order: ztest runs them in alphabetical order. + */ +static struct pipeline_connect_data *test_data; + +static void pipeline_connect_upstream_before(void *fixture) { - *state = get_standard_connect_objects(); - return 0; + (void)fixture; + + test_data = get_standard_connect_objects(); } -static int teardown(void **state) +static void pipeline_connect_upstream_after(void *fixture) { - free_standard_connect_objects(*state); - free(*state); - return 0; + (void)fixture; + + free_standard_connect_objects(test_data); + test_data = NULL; } -static void test_audio_pipeline_complete_wrong_status(void **state) +/** + * @brief Verify that pipeline_complete() fails for a pipeline not in INIT state + */ +ZTEST(pipeline_connect_upstream_suite, test_audio_pipeline_complete_wrong_status) { - struct pipeline_connect_data *test_data = *state; struct pipeline result = test_data->p; cleanup_test_data(test_data); @@ -42,12 +56,14 @@ static void test_audio_pipeline_complete_wrong_status(void **state) int error_code = pipeline_complete(&result, test_data->first, test_data->second); - assert_int_equal(error_code, -EINVAL); + zassert_equal(error_code, -EINVAL); } -static void test_audio_pipeline_complete_ready_state(void **state) +/** + * @brief Verify that pipeline_complete() moves the pipeline to READY state + */ +ZTEST(pipeline_connect_upstream_suite, test_audio_pipeline_complete_ready_state) { - struct pipeline_connect_data *test_data = *state; struct pipeline result = test_data->p; cleanup_test_data(test_data); @@ -56,14 +72,16 @@ static void test_audio_pipeline_complete_ready_state(void **state) int error_code = pipeline_complete(&result, test_data->first, test_data->second); - assert_int_equal(error_code, 0); - assert_int_equal(result.status, COMP_STATE_READY); - + zassert_equal(error_code, 0); + zassert_equal(result.status, COMP_STATE_READY); } -static void test_audio_pipeline_complete_connect_is_endpoint(void **state) +/** + * @brief Verify that pipeline_complete() does not connect anything to an + * unconnected scheduling component + */ +ZTEST(pipeline_connect_upstream_suite, test_audio_pipeline_complete_connect_is_endpoint) { - struct pipeline_connect_data *test_data = *state; struct pipeline result = test_data->p; cleanup_test_data(test_data); @@ -72,13 +90,15 @@ static void test_audio_pipeline_complete_connect_is_endpoint(void **state) pipeline_complete(&result, test_data->first, test_data->second); /*Was not marked as endpoint (bsink and bsource lists empty)*/ - assert_true(list_is_empty(&result.sched_comp->bsource_list)); + zassert_true(list_is_empty(&result.sched_comp->bsource_list)); } -static void test_audio_pipeline_complete_connect_downstream_variable_set - (void **state) +/** + * @brief Verify that pipeline_complete() sets the pipeline of the source + * component + */ +ZTEST(pipeline_connect_upstream_suite, test_audio_pipeline_complete_connect_downstream_variable_set) { - struct pipeline_connect_data *test_data = *state; struct pipeline result = test_data->p; cleanup_test_data(test_data); @@ -86,15 +106,15 @@ static void test_audio_pipeline_complete_connect_downstream_variable_set /*Testing component*/ pipeline_complete(&result, test_data->first, test_data->second); - assert_ptr_equal(result.sched_comp->pipeline, &result); - assert_ptr_equal(test_data->first->pipeline, &result); + zassert_equal_ptr(result.sched_comp->pipeline, &result); + zassert_equal_ptr(test_data->first->pipeline, &result); } -/*Test going downstream ignoring sink from other pipeline*/ -static void test_audio_pipeline_complete_connect_downstream_ignore_sink - (void **state) +/** + * @brief Test going downstream ignoring sink from other pipeline + */ +ZTEST(pipeline_connect_upstream_suite, test_audio_pipeline_complete_connect_downstream_ignore_sink) { - struct pipeline_connect_data *test_data = *state; struct pipeline result = test_data->p; cleanup_test_data(test_data); @@ -103,24 +123,24 @@ static void test_audio_pipeline_complete_connect_downstream_ignore_sink *and first has no upstream components */ list_item_append(&result.sched_comp->bsink_list, - &test_data->b1->source_list); + &test_data->b1->source_list); list_item_append(&test_data->b1->source_list, - &result.sched_comp->bsink_list); + &result.sched_comp->bsink_list); list_item_append(&test_data->b1->sink_list, - &test_data->second->bsource_list); + &test_data->second->bsource_list); /*Testing component*/ pipeline_complete(&result, test_data->first, test_data->second); - assert_true(list_is_empty(&test_data->first->bsource_list)); - assert_false(list_is_empty(&test_data->second->bsource_list)); + zassert_true(list_is_empty(&test_data->first->bsource_list)); + zassert_false(list_is_empty(&test_data->second->bsource_list)); } -/*Test going downstream ignoring source from other pipeline*/ -static void test_audio_pipeline_complete_connect_upstream_ignore_source - (void **state) +/** + * @brief Test going downstream ignoring source from other pipeline + */ +ZTEST(pipeline_connect_upstream_suite, test_audio_pipeline_complete_connect_upstream_ignore_source) { - struct pipeline_connect_data *test_data = *state; struct pipeline result = test_data->p; cleanup_test_data(test_data); @@ -129,26 +149,27 @@ static void test_audio_pipeline_complete_connect_upstream_ignore_source *and first has no downstream components */ list_item_append(&result.sched_comp->bsource_list, - &test_data->b1->sink_list); + &test_data->b1->sink_list); comp_buffer_set_sink_component(test_data->b1, result.sched_comp); comp_buffer_set_source_component(test_data->b1, test_data->second); list_item_append(&test_data->b1->source_list, - &test_data->second->bsink_list); + &test_data->second->bsink_list); list_item_append(&test_data->second->bsource_list, - &test_data->b2->sink_list); + &test_data->b2->sink_list); comp_buffer_set_sink_component(test_data->b2, test_data->second); /*Testing component*/ pipeline_complete(&result, test_data->first, test_data->second); - assert_true(list_is_empty(&test_data->first->bsink_list)); - assert_false(list_is_empty(&test_data->second->bsink_list)); + zassert_true(list_is_empty(&test_data->first->bsink_list)); + zassert_false(list_is_empty(&test_data->second->bsink_list)); } -/*Test going downstream all the way*/ -static void test_audio_pipeline_complete_connect_downstream_full(void **state) +/** + * @brief Test going downstream all the way + */ +ZTEST(pipeline_connect_upstream_suite, test_audio_pipeline_complete_connect_downstream_full) { - struct pipeline_connect_data *test_data = *state; struct pipeline result = test_data->p; struct comp_ipc_config *comp; @@ -169,14 +190,15 @@ static void test_audio_pipeline_complete_connect_downstream_full(void **state) /*Testing component*/ pipeline_complete(&result, test_data->first, test_data->second); - assert_ptr_equal(test_data->first->pipeline, &result); - assert_ptr_equal(test_data->second->pipeline, &result); + zassert_equal_ptr(test_data->first->pipeline, &result); + zassert_equal_ptr(test_data->second->pipeline, &result); } -/*Test going upstream all the way*/ -static void test_audio_pipeline_complete_connect_upstream_full(void **state) +/** + * @brief Test going upstream all the way + */ +ZTEST(pipeline_connect_upstream_suite, test_audio_pipeline_complete_connect_upstream_full) { - struct pipeline_connect_data *test_data = *state; struct pipeline result = test_data->p; struct comp_ipc_config *comp; @@ -186,22 +208,29 @@ static void test_audio_pipeline_complete_connect_upstream_full(void **state) comp = &test_data->second->ipc_config; comp->pipeline_id = PIPELINE_ID_SAME; list_item_append(&result.sched_comp->bsource_list, - &test_data->b1->sink_list); + &test_data->b1->sink_list); comp_buffer_set_sink_component(test_data->b1, test_data->first); comp_buffer_set_source_component(test_data->b1, test_data->second); /*Testing component*/ pipeline_complete(&result, test_data->first, test_data->second); - assert_ptr_equal(test_data->first->pipeline, &result); - assert_ptr_equal(test_data->second->pipeline, &result); + zassert_equal_ptr(test_data->first->pipeline, &result); + /* + * pipeline_complete() only walks downstream from the source component, + * so it does not reach the upstream component. The CMocka test expected + * &result here, which only held because the CMocka cases shared their + * objects: the previous case had set the pipeline of the second + * component to its own stack copy, at the same address as this one. + */ + zassert_is_null(test_data->second->pipeline); } -/*Test going upstream all the way*/ -static void test_audio_pipeline_complete_connect_upstream_other_pipeline - (void **state) +/** + * @brief Test going upstream to a component of another pipeline + */ +ZTEST(pipeline_connect_upstream_suite, test_audio_pipeline_complete_connect_upstream_other_pipeline) { - struct pipeline_connect_data *test_data = *state; struct pipeline result = test_data->p; struct comp_ipc_config *comp; @@ -218,42 +247,8 @@ static void test_audio_pipeline_complete_connect_upstream_other_pipeline /*Testing component*/ pipeline_complete(&result, test_data->first, test_data->second); - assert_ptr_equal(test_data->first, result.source_comp); + zassert_equal_ptr(test_data->first, result.source_comp); } -int main(void) -{ - const struct CMUnitTest tests[] = { - cmocka_unit_test( - test_audio_pipeline_complete_wrong_status - ), - cmocka_unit_test( - test_audio_pipeline_complete_ready_state - ), - cmocka_unit_test( - test_audio_pipeline_complete_connect_is_endpoint - ), - cmocka_unit_test( - test_audio_pipeline_complete_connect_downstream_variable_set - ), - cmocka_unit_test( - test_audio_pipeline_complete_connect_downstream_ignore_sink - ), - cmocka_unit_test( - test_audio_pipeline_complete_connect_upstream_ignore_source - ), - cmocka_unit_test( - test_audio_pipeline_complete_connect_downstream_full - ), - cmocka_unit_test( - test_audio_pipeline_complete_connect_upstream_full - ), - cmocka_unit_test( - test_audio_pipeline_complete_connect_upstream_other_pipeline - ), - }; - - cmocka_set_message_output(CM_OUTPUT_TAP); - - return cmocka_run_group_tests(tests, setup, teardown); -} +ZTEST_SUITE(pipeline_connect_upstream_suite, NULL, NULL, pipeline_connect_upstream_before, + pipeline_connect_upstream_after, NULL); diff --git a/test/ztest/unit/audio/pipeline/pipeline_connection_mocks.c b/test/ztest/unit/audio/pipeline/pipeline_connection_mocks.c new file mode 100644 index 000000000000..ead047efda78 --- /dev/null +++ b/test/ztest/unit/audio/pipeline/pipeline_connection_mocks.c @@ -0,0 +1,153 @@ +// SPDX-License-Identifier: BSD-3-Clause +// +// Copyright(c) 2026 Intel Corporation. +// +// These contents may have been developed with support from one or more +// Intel-operated generative artificial intelligence solutions. +// +// Converted from CMock to Ztest +// Original helpers from test/cmocka/src/audio/pipeline/pipeline_connection_mocks.c: +// Author: Jakub Dabek + +#include + +#include +#include +#include +#include +#include +#include + +#include + +#include "pipeline_connection_mocks.h" + +/* The scheduler list, normally created by scheduler_init() */ +static struct schedulers *schedulers; + +struct schedulers **arch_schedulers_get(void) +{ + return &schedulers; +} + +/** + * @brief Mock scheduler task free, marks the task as freed. + * @param data Scheduler private data, unused. + * @param task Task to free. + * @return 0. + */ +static int schedule_task_mock_free(void *data, struct task *task) +{ + (void)data; + + task->state = SOF_TASK_STATE_FREE; + task->ops.run = NULL; + task->data = NULL; + + return 0; +} + +static struct scheduler_ops schedule_mock_ops = { + .schedule_task_free = &schedule_task_mock_free, +}; + +static void *test_calloc(size_t size) +{ + void *ptr = calloc(1, size); + + zassert_not_null(ptr, "Failed to allocate %zu bytes", size); + + return ptr; +} + +void cleanup_test_data(struct pipeline_connect_data *data) +{ + list_init(&data->first->bsource_list); + list_init(&data->second->bsource_list); + comp_buffer_reset_sink_list(data->b1); + comp_buffer_reset_source_list(data->b1); + list_init(&data->first->bsink_list); + list_init(&data->second->bsink_list); + comp_buffer_reset_sink_list(data->b2); + comp_buffer_reset_source_list(data->b2); +} + +struct pipeline_connect_data *get_standard_connect_objects(void) +{ + struct pipeline_connect_data *pipeline_connect_data = + test_calloc(sizeof(struct pipeline_connect_data)); + struct pipeline *pipe = &pipeline_connect_data->p; + struct schedule_data *sch; + struct comp_dev *first; + struct comp_dev *second; + struct comp_buffer *buffer; + struct comp_buffer *buffer_2; + + pipe->alloc = test_calloc(sizeof(*pipe->alloc)); + pipe->frames_per_sched = 5; + pipe->pipeline_id = PIPELINE_ID_SAME; + pipe->status = COMP_STATE_INIT; + + pipe->pipe_task = test_calloc(sizeof(struct task)); + pipe->pipe_task->type = SOF_SCHEDULE_EDF; + + schedulers = test_calloc(sizeof(struct schedulers)); + list_init(&schedulers->list); + + sch = test_calloc(sizeof(struct schedule_data)); + list_init(&sch->list); + sch->type = SOF_SCHEDULE_EDF; + sch->ops = &schedule_mock_ops; + list_item_append(&sch->list, &schedulers->list); + + /* Bind the scheduler to the task, as schedule_task_init() would */ + pipe->pipe_task->sch = sch; + + first = test_calloc(sizeof(struct comp_dev)); + first->ipc_config.id = 3; + first->ipc_config.pipeline_id = PIPELINE_ID_SAME; + list_init(&first->bsink_list); + list_init(&first->bsource_list); + pipeline_connect_data->first = first; + pipe->sched_comp = first; + + second = test_calloc(sizeof(struct comp_dev)); + second->ipc_config.id = 4; + second->ipc_config.pipeline_id = PIPELINE_ID_DIFFERENT; + list_init(&second->bsink_list); + list_init(&second->bsource_list); + pipeline_connect_data->second = second; + + buffer = test_calloc(sizeof(struct comp_buffer)); + comp_buffer_set_source_component(buffer, first); + comp_buffer_set_sink_component(buffer, second); + comp_buffer_reset_sink_list(buffer); + comp_buffer_reset_source_list(buffer); + pipeline_connect_data->b1 = buffer; + + buffer_2 = test_calloc(sizeof(struct comp_buffer)); + comp_buffer_set_source_component(buffer_2, second); + comp_buffer_reset_sink_list(buffer_2); + comp_buffer_reset_source_list(buffer_2); + pipeline_connect_data->b2 = buffer_2; + + return pipeline_connect_data; +} + +void free_standard_connect_objects(struct pipeline_connect_data *data) +{ + struct schedule_data *sch = data->p.pipe_task->sch; + + list_item_del(&sch->list); + free(sch); + free(schedulers); + schedulers = NULL; + + free(data->p.alloc); + free(data->p.pipe_task); + free(data->first); + free(data->second); + free(data->b1); + free(data->b2); + free(data); +} diff --git a/test/ztest/unit/audio/pipeline/pipeline_connection_mocks.h b/test/ztest/unit/audio/pipeline/pipeline_connection_mocks.h new file mode 100644 index 000000000000..9c8df917e25e --- /dev/null +++ b/test/ztest/unit/audio/pipeline/pipeline_connection_mocks.h @@ -0,0 +1,60 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * + * Copyright(c) 2026 Intel Corporation. + * + * These contents may have been developed with support from one or more + * Intel-operated generative artificial intelligence solutions. + * + * Converted from CMock to Ztest + * Original helpers from test/cmocka/src/audio/pipeline/pipeline_connection_mocks.h: + * Author: Jakub Dabek + */ + +#ifndef __TEST_ZTEST_UNIT_AUDIO_PIPELINE_CONNECTION_MOCKS_H__ +#define __TEST_ZTEST_UNIT_AUDIO_PIPELINE_CONNECTION_MOCKS_H__ + +#include +#include +#include +#include + +#define PIPELINE_ID_SAME 3 +#define PIPELINE_ID_DIFFERENT 4 + +/** + * @brief Pipeline, two components and two buffers used by the pipeline tests. + * + * The pipeline has the scheduling component @ref first, which belongs to + * pipeline PIPELINE_ID_SAME, and an EDF pipeline task bound to a mock + * scheduler. The component @ref second belongs to pipeline + * PIPELINE_ID_DIFFERENT. Buffer @ref b1 goes from @ref first to @ref second and + * buffer @ref b2 has @ref second as its source. Only the component pointers of + * the buffers are set, none of the objects is linked to another. + */ +struct pipeline_connect_data { + struct pipeline p; + struct comp_dev *first; + struct comp_dev *second; + struct comp_buffer *b1; + struct comp_buffer *b2; +}; + +/** + * @brief Allocate and initialise the standard set of test objects. + * @return The test objects, never NULL. + */ +struct pipeline_connect_data *get_standard_connect_objects(void); + +/** + * @brief Free the test objects returned by get_standard_connect_objects(). + * @param data Test objects, also freed. + */ +void free_standard_connect_objects(struct pipeline_connect_data *data); + +/** + * @brief Empty the component and buffer lists of the test objects. + * @param data Test objects. + */ +void cleanup_test_data(struct pipeline_connect_data *data); + +#endif /* __TEST_ZTEST_UNIT_AUDIO_PIPELINE_CONNECTION_MOCKS_H__ */ diff --git a/test/ztest/unit/audio/pipeline/pipeline_free_ztest.c b/test/ztest/unit/audio/pipeline/pipeline_free_ztest.c new file mode 100644 index 000000000000..fe5f45dad912 --- /dev/null +++ b/test/ztest/unit/audio/pipeline/pipeline_free_ztest.c @@ -0,0 +1,101 @@ +// SPDX-License-Identifier: BSD-3-Clause +// +// Copyright(c) 2026 Intel Corporation. +// +// These contents may have been developed with support from one or more +// Intel-operated generative artificial intelligence solutions. +// +// Converted from CMock to Ztest +// Original tests from test/cmocka/src/audio/pipeline/pipeline_free.c: +// Author: Jakub Dabek + +#include + +#include +#include +#include +#include + +#include +#include + +#include "pipeline_connection_mocks.h" + +static struct pipeline_connect_data *test_data; + +/* + * pipeline_free() frees the pipeline, its task and its alloc context. Here + * these are a copy of the test pipeline on the stack and objects owned by the + * test, which are inspected after the call and freed by the after hook, so + * sof_heap_free() does nothing while this suite runs. + */ +static bool sof_heap_free_disabled; + +void __real_sof_heap_free(struct k_heap *heap, void *addr); + +void __wrap_sof_heap_free(struct k_heap *heap, void *addr) +{ + if (sof_heap_free_disabled) + return; + + __real_sof_heap_free(heap, addr); +} + +static void *pipeline_free_setup(void) +{ + pipeline_posn_init(sof_get()); + + return NULL; +} + +static void pipeline_free_before(void *fixture) +{ + (void)fixture; + + test_data = get_standard_connect_objects(); + sof_heap_free_disabled = true; +} + +static void pipeline_free_after(void *fixture) +{ + (void)fixture; + + sof_heap_free_disabled = false; + free_standard_connect_objects(test_data); + test_data = NULL; +} + +/** + * @brief Verify that pipeline_free() returns 0 + */ +ZTEST(pipeline_free_suite, test_audio_pipeline_free_return_value) +{ + struct pipeline result = test_data->p; + + cleanup_test_data(test_data); + + /*Testing component*/ + int err = pipeline_free(&result); + + zassert_equal(err, 0); +} + +/** + * @brief Verify that pipeline_free() frees the pipeline task in its scheduler + */ +ZTEST(pipeline_free_suite, test_audio_pipeline_free_sheduler_task_free) +{ + struct pipeline result = test_data->p; + + cleanup_test_data(test_data); + + /*Testing component*/ + pipeline_free(&result); + + zassert_equal(result.pipe_task->state, SOF_TASK_STATE_FREE); + zassert_is_null(result.pipe_task->data); + zassert_is_null(result.pipe_task->ops.run); +} + +ZTEST_SUITE(pipeline_free_suite, NULL, pipeline_free_setup, pipeline_free_before, + pipeline_free_after, NULL); diff --git a/test/ztest/unit/audio/pipeline/pipeline_new_ztest.c b/test/ztest/unit/audio/pipeline/pipeline_new_ztest.c new file mode 100644 index 000000000000..81318baafbfd --- /dev/null +++ b/test/ztest/unit/audio/pipeline/pipeline_new_ztest.c @@ -0,0 +1,53 @@ +// SPDX-License-Identifier: BSD-3-Clause +// +// Copyright(c) 2026 Intel Corporation. +// +// These contents may have been developed with support from one or more +// Intel-operated generative artificial intelligence solutions. +// +// Converted from CMock to Ztest +// Original tests from test/cmocka/src/audio/pipeline/pipeline_new.c: +// Author: Jakub Dabek + +#include + +#include +#include +#include +#include + +#include + +#define TEST_PIPE_ID 1 +#define TEST_PRIORITY 2 +#define TEST_COMP_ID 3 + +static void *pipeline_new_setup(void) +{ + pipeline_posn_init(sof_get()); + + return NULL; +} + +/** + * @brief Verify that pipeline_new() creates a pipeline + */ +ZTEST(pipeline_new_suite, test_audio_pipeline_pipeline_new_creation) +{ + /*Testing component*/ + struct pipeline *result = pipeline_new(NULL, TEST_PIPE_ID, TEST_PRIORITY, + TEST_COMP_ID, NULL); + + /*Pipeline should have been created so pointer can't be null*/ + zassert_not_null(result); + /* IPC3 pipelines get a stream position message */ + zassert_not_null(result->msg); + + rfree(result->msg->tx_data); + rfree(result->msg); + /* The alloc context is allocated from the SOF heap with a NULL heap */ + sof_heap_free(NULL, result->alloc); + rfree(result); +} + +ZTEST_SUITE(pipeline_new_suite, NULL, pipeline_new_setup, NULL, NULL, NULL); diff --git a/test/ztest/unit/audio/pipeline/prj.conf b/test/ztest/unit/audio/pipeline/prj.conf new file mode 100644 index 000000000000..720ce8f58304 --- /dev/null +++ b/test/ztest/unit/audio/pipeline/prj.conf @@ -0,0 +1,16 @@ +# SPDX-License-Identifier: BSD-3-Clause +# +# Copyright(c) 2026 Intel Corporation. + +CONFIG_ZTEST=y +CONFIG_SOF_FULL_ZEPHYR_APPLICATION=n +# Same IPC version as the CMocka unit_test_defconfig build: pipeline_new() +# only allocates the stream position message with IPC3 +CONFIG_IPC_MAJOR_3=y +# Route the SOF heap to the host malloc()/free() of the native simulator, so +# that ASan and LSan can see it. They cannot see blocks carved out of the +# static sys_heap arena that zephyr/lib/alloc.c uses by default. +CONFIG_SOF_NATIVE_SIM_HOST_HEAP=y +# The sys_heap arena is still reserved and initialised by heap_init(), and its +# 256 KiB are too large for SYS_HEAP_SMALL_ONLY (see app/boards/native_sim.conf) +CONFIG_SYS_HEAP_BIG_ONLY=y diff --git a/test/ztest/unit/audio/pipeline/testcase.yaml b/test/ztest/unit/audio/pipeline/testcase.yaml new file mode 100644 index 000000000000..2b960c42644d --- /dev/null +++ b/test/ztest/unit/audio/pipeline/testcase.yaml @@ -0,0 +1,25 @@ +# SPDX-License-Identifier: BSD-3-Clause +# +# Copyright(c) 2026 Intel Corporation. +# +# Audio pipeline unit tests converted from CMock to Ztest +# +# These contents may have been developed with support from one or more Intel-operated +# generative artificial intelligence solutions. +# + +common: + tags: + - SOF + - unit_test + - audio + - pipeline + integration_platforms: + - native_sim + arch_exclude: xtensa + +tests: + sof.unit.audio.pipeline: + platform_allow: + - native_sim + build_only: false diff --git a/test/cmocka/src/notifier_mocks.c b/test/ztest/unit/common/notifier_mocks.c similarity index 100% rename from test/cmocka/src/notifier_mocks.c rename to test/ztest/unit/common/notifier_mocks.c diff --git a/test/cmocka/include/cmocka_chirp_2ch.h b/test/ztest/unit/include/cmocka_chirp_2ch.h similarity index 100% rename from test/cmocka/include/cmocka_chirp_2ch.h rename to test/ztest/unit/include/cmocka_chirp_2ch.h diff --git a/test/cmocka/include/log2_tables.h b/test/ztest/unit/include/log2_tables.h similarity index 100% rename from test/cmocka/include/log2_tables.h rename to test/ztest/unit/include/log2_tables.h diff --git a/test/cmocka/include/power_tables.h b/test/ztest/unit/include/power_tables.h similarity index 100% rename from test/cmocka/include/power_tables.h rename to test/ztest/unit/include/power_tables.h diff --git a/test/ztest/unit/math/advanced/functions/CMakeLists.txt b/test/ztest/unit/math/advanced/functions/CMakeLists.txt index 797133172e30..316da3d41b4e 100644 --- a/test/ztest/unit/math/advanced/functions/CMakeLists.txt +++ b/test/ztest/unit/math/advanced/functions/CMakeLists.txt @@ -17,7 +17,7 @@ target_include_directories(app PRIVATE ${SOF_ROOT}/src/include ${SOF_ROOT}/src/kernel/platform/posix/include ${SOF_ROOT}/src/platform/posix/include - ${SOF_ROOT}/test/cmocka/include + ${SOF_ROOT}/test/ztest/unit/include ${PROJECT_BINARY_DIR}/include/generated # For uuid-registry.h ) diff --git a/test/ztest/unit/math/advanced/pcan/ref_pcan.m b/test/ztest/unit/math/advanced/pcan/ref_pcan.m index 7aac8c3883f3..a66e8b5cec5b 100644 --- a/test/ztest/unit/math/advanced/pcan/ref_pcan.m +++ b/test/ztest/unit/math/advanced/pcan/ref_pcan.m @@ -5,7 +5,7 @@ function ref_pcan() % Add path to export helper functions - path(path(), '../../../../../cmocka/m'); + path(path(), '../../../../m'); try opt.describe = export_get_git_describe(); diff --git a/test/ztest/unit/math/dft3/ref_dft3.m b/test/ztest/unit/math/dft3/ref_dft3.m index 87afe83342d6..c3f55d91b3e3 100644 --- a/test/ztest/unit/math/dft3/ref_dft3.m +++ b/test/ztest/unit/math/dft3/ref_dft3.m @@ -6,7 +6,7 @@ function ref_dft3() - path(path(), '../../../../cmocka/m'); + path(path(), '../../../m'); opt.describe = export_get_git_describe(); N = 3; diff --git a/test/ztest/unit/math/fft/CMakeLists.txt b/test/ztest/unit/math/fft/CMakeLists.txt index a74612dbced6..8c08dd07329a 100644 --- a/test/ztest/unit/math/fft/CMakeLists.txt +++ b/test/ztest/unit/math/fft/CMakeLists.txt @@ -21,7 +21,7 @@ target_include_directories(app PRIVATE ${SOF_ROOT}/src/platform/posix/include ${SOF_ROOT}/src/audio ${SOF_ROOT}/test/ztest/unit/common - ${SOF_ROOT}/test/cmocka/include + ${SOF_ROOT}/test/ztest/unit/include ${PROJECT_BINARY_DIR}/include/generated ) @@ -46,7 +46,7 @@ target_sources(app PRIVATE ${SOF_ROOT}/test/ztest/unit/common/mod_alloc.c ${SOF_ROOT}/test/ztest/unit/common/sof_context.c ${SOF_ROOT}/zephyr/lib/alloc.c - ${SOF_ROOT}/test/cmocka/src/notifier_mocks.c + ${SOF_ROOT}/test/ztest/unit/common/notifier_mocks.c ${SOF_ROOT}/src/math/fft/fft_common.c ${SOF_ROOT}/src/math/fft/fft_16.c ${SOF_ROOT}/src/math/fft/fft_16_hifi3.c diff --git a/test/ztest/unit/math/fft_multi/CMakeLists.txt b/test/ztest/unit/math/fft_multi/CMakeLists.txt index cd67aca5757a..9afbbc08249b 100644 --- a/test/ztest/unit/math/fft_multi/CMakeLists.txt +++ b/test/ztest/unit/math/fft_multi/CMakeLists.txt @@ -20,7 +20,7 @@ target_include_directories(app PRIVATE ${SOF_ROOT}/src/kernel/platform/posix/include ${SOF_ROOT}/src/platform/posix/include ${SOF_ROOT}/test/ztest/unit/common - ${SOF_ROOT}/test/cmocka/include + ${SOF_ROOT}/test/ztest/unit/include ${PROJECT_BINARY_DIR}/include/generated ) @@ -44,7 +44,7 @@ target_sources(app PRIVATE test_fft_multi_ztest.c ${SOF_ROOT}/test/ztest/unit/common/mod_alloc.c ${SOF_ROOT}/zephyr/lib/alloc.c - ${SOF_ROOT}/test/cmocka/src/notifier_mocks.c + ${SOF_ROOT}/test/ztest/unit/common/notifier_mocks.c ${SOF_ROOT}/src/math/fft/fft_multi.c ${SOF_ROOT}/src/math/fft/fft_multi_generic.c ${SOF_ROOT}/src/math/fft/fft_multi_hifi3.c