Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions test/ztest/unit/audio/buffer/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
13 changes: 13 additions & 0 deletions test/ztest/unit/audio/buffer/prj.conf
Original file line number Diff line number Diff line change
@@ -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
264 changes: 264 additions & 0 deletions test/ztest/unit/audio/buffer/test_buffer_ztest.c
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>

#include <zephyr/ztest.h>

#include <sof/audio/buffer.h>
#include <sof/audio/component.h>
#include <rtos/string.h>

#include <stdint.h>
#include <string.h>

#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);
21 changes: 21 additions & 0 deletions test/ztest/unit/audio/buffer/testcase.yaml
Original file line number Diff line number Diff line change
@@ -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
Loading