From fe585630d7721f18da92da4e0c12cb30acd2df5e Mon Sep 17 00:00:00 2001 From: Tomasz Leman Date: Wed, 28 Oct 2026 17:25:44 +0100 Subject: [PATCH] test: audio: port buffer CMocka tests to ztest Add a ztest suite for the component buffer, merging the four CMocka buffer tests into one suite of nine cases: allocation, a write and read back, filling a buffer completely, wrap around on overwrite, and the five copy size and over/underrun cases. The CMocka builds link twenty one SOF sources and two mock files each. Only eleven sources are actually needed here, so the pipeline, component and IPC3 topology code are dropped along with the notifier mocks. The repeated buffer allocation and empty buffer checks are factored into two helpers. Buffers allocated with a NULL mod_alloc_ctx come from the SOF heap rather than the module allocator, so the suite needs no allocator hook. With the heap routed to the host malloc(), LSan covers the frees instead. Assisted-by: Copilot:claude-opus-5 Signed-off-by: Tomasz Leman --- test/ztest/unit/audio/buffer/CMakeLists.txt | 52 ++++ test/ztest/unit/audio/buffer/prj.conf | 13 + .../unit/audio/buffer/test_buffer_ztest.c | 264 ++++++++++++++++++ test/ztest/unit/audio/buffer/testcase.yaml | 21 ++ 4 files changed, 350 insertions(+) create mode 100644 test/ztest/unit/audio/buffer/CMakeLists.txt create mode 100644 test/ztest/unit/audio/buffer/prj.conf create mode 100644 test/ztest/unit/audio/buffer/test_buffer_ztest.c create mode 100644 test/ztest/unit/audio/buffer/testcase.yaml diff --git a/test/ztest/unit/audio/buffer/CMakeLists.txt b/test/ztest/unit/audio/buffer/CMakeLists.txt new file mode 100644 index 000000000000..da237415ea5f --- /dev/null +++ b/test/ztest/unit/audio/buffer/CMakeLists.txt @@ -0,0 +1,52 @@ +# SPDX-License-Identifier: BSD-3-Clause +# +# Copyright(c) 2026 Intel Corporation. +# +# Audio buffer 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_buffer) + +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 + ${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 + test_buffer_ztest.c + ${SOF_ROOT}/zephyr/lib/alloc.c + ${SOF_ROOT}/src/audio/buffers/comp_buffer.c + ${SOF_ROOT}/src/audio/buffers/audio_buffer.c + ${SOF_ROOT}/src/audio/source_api_helper.c + ${SOF_ROOT}/src/audio/sink_api_helper.c + ${SOF_ROOT}/src/audio/sink_source_utils.c + ${SOF_ROOT}/src/audio/audio_stream.c + ${SOF_ROOT}/src/module/audio/source_api.c + ${SOF_ROOT}/src/module/audio/sink_api.c + ${SOF_ROOT}/src/ipc/ipc-helper.c + # audio_stream_recalc_align() needs gcd() + ${SOF_ROOT}/src/math/numbers.c +) + +sof_append_relative_path_definitions(app) diff --git a/test/ztest/unit/audio/buffer/prj.conf b/test/ztest/unit/audio/buffer/prj.conf new file mode 100644 index 000000000000..3065b2ce422f --- /dev/null +++ b/test/ztest/unit/audio/buffer/prj.conf @@ -0,0 +1,13 @@ +# SPDX-License-Identifier: BSD-3-Clause +# +# Copyright(c) 2026 Intel Corporation. + +CONFIG_ZTEST=y +CONFIG_SOF_FULL_ZEPHYR_APPLICATION=n +# 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/buffer/test_buffer_ztest.c b/test/ztest/unit/audio/buffer/test_buffer_ztest.c new file mode 100644 index 000000000000..e74d568cc909 --- /dev/null +++ b/test/ztest/unit/audio/buffer/test_buffer_ztest.c @@ -0,0 +1,264 @@ +// SPDX-License-Identifier: BSD-3-Clause +// +// Copyright(c) 2026 Intel Corporation. +// +// Converted from CMock to Ztest +// Original tests from test/cmocka/src/audio/buffer/: +// buffer_new.c, buffer_wrap.c, buffer_write.c and buffer_copy.c +// Author: Slawomir Blauciak + +#include + +#include +#include +#include + +#include +#include + +#define TEST_BUFFER_SIZE 256 +#define TEST_SMALL_BUFFER_SIZE 10 + +/** + * @brief Allocate a non shared component buffer of the given size. + * @param[in] size Buffer size in bytes. + * @return The new buffer, never NULL. + */ +static struct comp_buffer *test_buffer_new(uint32_t size) +{ + struct sof_ipc_buffer desc = {.size = size}; + struct comp_buffer *buf = buffer_new(NULL, &desc, BUFFER_USAGE_NOT_SHARED); + + zassert_not_null(buf, "Failed to allocate a %u byte buffer.", size); + + return buf; +} + +/** + * @brief Check that a buffer is empty and its pointers are coincident. + * @param[in] buf Buffer to check. + * @param[in] size Expected free space in bytes. + */ +static void assert_buffer_empty(struct comp_buffer *buf, uint32_t size) +{ + zassert_equal(audio_stream_get_avail_bytes(&buf->stream), 0); + zassert_equal(audio_stream_get_free_bytes(&buf->stream), size); + zassert_equal_ptr(buf->stream.w_ptr, buf->stream.r_ptr); +} + +/** + * @brief Verify that a new buffer is empty and has the requested free space + */ +ZTEST(audio_buffer_suite, test_audio_buffer_new) +{ + struct comp_buffer *buf = test_buffer_new(TEST_BUFFER_SIZE); + + assert_buffer_empty(buf, TEST_BUFFER_SIZE); + + buffer_free(buf); +} + +/** + * @brief Verify a write and read back of 10 bytes in a 256 byte buffer + */ +ZTEST(audio_buffer_suite, test_audio_buffer_write_10_bytes_out_of_256_and_read_back) +{ + uint8_t bytes[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; + struct comp_buffer *buf = test_buffer_new(TEST_BUFFER_SIZE); + + assert_buffer_empty(buf, TEST_BUFFER_SIZE); + + memcpy_s(buf->stream.w_ptr, TEST_BUFFER_SIZE, &bytes, sizeof(bytes)); + comp_update_buffer_produce(buf, sizeof(bytes)); + + zassert_equal(audio_stream_get_avail_bytes(&buf->stream), sizeof(bytes)); + zassert_equal(audio_stream_get_free_bytes(&buf->stream), TEST_BUFFER_SIZE - sizeof(bytes)); + zassert_equal_ptr(buf->stream.w_ptr, (char *)buf->stream.r_ptr + sizeof(bytes)); + zassert_mem_equal(buf->stream.r_ptr, &bytes, sizeof(bytes)); + + comp_update_buffer_consume(buf, sizeof(bytes)); + + assert_buffer_empty(buf, TEST_BUFFER_SIZE); + + buffer_free(buf); +} + +/** + * @brief Verify that filling a 10 byte buffer leaves no free space + */ +ZTEST(audio_buffer_suite, test_audio_buffer_fill_10_bytes) +{ + uint8_t bytes[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; + struct comp_buffer *buf = test_buffer_new(TEST_SMALL_BUFFER_SIZE); + + assert_buffer_empty(buf, TEST_SMALL_BUFFER_SIZE); + + memcpy_s(buf->stream.w_ptr, TEST_SMALL_BUFFER_SIZE, &bytes, sizeof(bytes)); + comp_update_buffer_produce(buf, sizeof(bytes)); + + zassert_equal(audio_stream_get_avail_bytes(&buf->stream), sizeof(bytes)); + zassert_equal(audio_stream_get_free_bytes(&buf->stream), 0); + zassert_equal_ptr(buf->stream.w_ptr, buf->stream.r_ptr); + + buffer_free(buf); +} + +/** + * @brief Verify that writing past the end of a full buffer wraps around + * + * The first ten bytes fill the buffer, the next five overwrite its head, and + * the oldest five bytes must then read back before the five new ones. + */ +ZTEST(audio_buffer_suite, test_audio_buffer_write_fill_10_bytes_and_write_5) +{ + uint8_t bytes[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; + uint8_t more_bytes[5] = {10, 11, 12, 13, 14}; + uint8_t ref_1[5] = {5, 6, 7, 8, 9}; + uint8_t ref_2[5] = {10, 11, 12, 13, 14}; + struct comp_buffer *buf = test_buffer_new(TEST_SMALL_BUFFER_SIZE); + uint8_t *ptr; + int i; + + assert_buffer_empty(buf, TEST_SMALL_BUFFER_SIZE); + + for (i = 0; i < ARRAY_SIZE(bytes); i++) { + ptr = audio_stream_write_frag(&buf->stream, i, sizeof(uint8_t)); + *ptr = bytes[i]; + } + comp_update_buffer_produce(buf, sizeof(bytes)); + + zassert_equal(audio_stream_get_avail_bytes(&buf->stream), sizeof(bytes)); + zassert_equal(audio_stream_get_free_bytes(&buf->stream), 0); + zassert_equal_ptr(buf->stream.w_ptr, buf->stream.r_ptr); + + for (i = 0; i < ARRAY_SIZE(more_bytes); i++) { + ptr = audio_stream_write_frag(&buf->stream, i, sizeof(uint8_t)); + *ptr = more_bytes[i]; + } + comp_update_buffer_produce(buf, sizeof(more_bytes)); + + zassert_equal(audio_stream_get_avail_bytes(&buf->stream), TEST_SMALL_BUFFER_SIZE); + zassert_equal(audio_stream_get_free_bytes(&buf->stream), 0); + zassert_equal_ptr(buf->stream.w_ptr, buf->stream.r_ptr); + + for (i = 0; i < ARRAY_SIZE(ref_1); i++) { + ptr = audio_stream_read_frag(&buf->stream, i, sizeof(uint8_t)); + zassert_equal(*ptr, ref_1[i], "Byte %d of the first half is %u, expected %u", i, + *ptr, ref_1[i]); + } + + comp_update_buffer_consume(buf, sizeof(ref_1)); + + for (i = 0; i < ARRAY_SIZE(ref_2); i++) { + ptr = audio_stream_read_frag(&buf->stream, i, sizeof(uint8_t)); + zassert_equal(*ptr, ref_2[i], "Byte %d of the second half is %u, expected %u", i, + *ptr, ref_2[i]); + } + + buffer_free(buf); +} + +/** + * @brief Verify that a source with too few bytes to copy reports underrun + */ +ZTEST(audio_buffer_suite, test_audio_buffer_copy_underrun) +{ + struct comp_buffer *src = test_buffer_new(TEST_BUFFER_SIZE); + struct comp_buffer *snk = test_buffer_new(TEST_BUFFER_SIZE); + int copy_bytes; + + comp_update_buffer_produce(src, 10); + copy_bytes = audio_stream_can_copy_bytes(&src->stream, &snk->stream, 16); + + zassert_equal(audio_stream_get_avail_bytes(&src->stream), 10); + zassert_equal(copy_bytes, -1); + + buffer_free(src); + buffer_free(snk); +} + +/** + * @brief Verify that a sink with too little free space reports overrun + */ +ZTEST(audio_buffer_suite, test_audio_buffer_copy_overrun) +{ + struct comp_buffer *src = test_buffer_new(TEST_BUFFER_SIZE); + struct comp_buffer *snk = test_buffer_new(TEST_BUFFER_SIZE); + int copy_bytes; + + comp_update_buffer_produce(src, 16); + comp_update_buffer_produce(snk, 246); + copy_bytes = audio_stream_can_copy_bytes(&src->stream, &snk->stream, 16); + + zassert_equal(audio_stream_get_avail_bytes(&src->stream), 16); + zassert_equal(audio_stream_get_free_bytes(&snk->stream), 10); + zassert_equal(copy_bytes, 1); + + buffer_free(src); + buffer_free(snk); +} + +/** + * @brief Verify that a copy of zero bytes is always possible + */ +ZTEST(audio_buffer_suite, test_audio_buffer_copy_success) +{ + struct comp_buffer *src = test_buffer_new(TEST_BUFFER_SIZE); + struct comp_buffer *snk = test_buffer_new(TEST_BUFFER_SIZE); + int copy_bytes; + + comp_update_buffer_produce(src, 10); + copy_bytes = audio_stream_can_copy_bytes(&src->stream, &snk->stream, 0); + + zassert_equal(audio_stream_get_avail_bytes(&src->stream), 10); + zassert_equal(copy_bytes, 0); + + buffer_free(src); + buffer_free(snk); +} + +/** + * @brief Verify that the copy size is limited by the free space of the sink + */ +ZTEST(audio_buffer_suite, test_audio_buffer_copy_fit_space_constraint) +{ + struct comp_buffer *src = test_buffer_new(TEST_BUFFER_SIZE); + struct comp_buffer *snk = test_buffer_new(TEST_BUFFER_SIZE); + int copy_bytes; + + comp_update_buffer_produce(src, 16); + comp_update_buffer_produce(snk, 246); + copy_bytes = audio_stream_get_copy_bytes(&src->stream, &snk->stream); + + zassert_equal(audio_stream_get_avail_bytes(&src->stream), 16); + zassert_equal(audio_stream_get_free_bytes(&snk->stream), 10); + zassert_equal(copy_bytes, 10); + + buffer_free(src); + buffer_free(snk); +} + +/** + * @brief Verify that the copy size is limited by the data available in the + * source when the sink is empty + */ +ZTEST(audio_buffer_suite, test_audio_buffer_copy_fit_no_space_constraint) +{ + struct comp_buffer *src = test_buffer_new(TEST_BUFFER_SIZE); + struct comp_buffer *snk = test_buffer_new(TEST_BUFFER_SIZE); + int copy_bytes; + + comp_update_buffer_produce(src, 16); + copy_bytes = audio_stream_get_copy_bytes(&src->stream, &snk->stream); + + zassert_equal(audio_stream_get_avail_bytes(&src->stream), 16); + zassert_equal(copy_bytes, 16); + + buffer_free(src); + buffer_free(snk); +} + +/** + * @brief Define and initialize the audio buffer test suite + */ +ZTEST_SUITE(audio_buffer_suite, NULL, NULL, NULL, NULL, NULL); diff --git a/test/ztest/unit/audio/buffer/testcase.yaml b/test/ztest/unit/audio/buffer/testcase.yaml new file mode 100644 index 000000000000..7508774286e3 --- /dev/null +++ b/test/ztest/unit/audio/buffer/testcase.yaml @@ -0,0 +1,21 @@ +# SPDX-License-Identifier: BSD-3-Clause +# +# Copyright(c) 2026 Intel Corporation. +# +# Audio buffer unit tests converted from CMock to Ztest + +common: + tags: + - SOF + - unit_test + - audio + - buffer + integration_platforms: + - native_sim + arch_exclude: xtensa + +tests: + sof.unit.audio.buffer: + platform_allow: + - native_sim + build_only: false