From 4daf81bc7336d19a46539317ae342db53460cd46 Mon Sep 17 00:00:00 2001 From: Tomasz Leman Date: Tue, 10 Nov 2026 16:58:48 +0100 Subject: [PATCH] test: math: port dcblock_math_test CMocka test to ztest Add a ztest suite for the DC-blocking filter's fixed-point math (dcblock_generic.c), covering the same six cases as the CMocka test: passthrough, DC removal convergence, saturation, and bit-exact checks for S32, S16 and S24 sample formats. This closes one of the two "misfiled" math tests identified in the Phase 3 plan (filed under test/cmocka/src/audio/dcblock/ despite testing pure math, not the component), with drc_math_test deferred to Phase 4. Only dcblock_generic.c and numbers.c are linked, matching the CMocka target's source list. M_PI is not exposed by under this toolchain's strict -std=c17 build, so it is defined locally guarded by #ifndef, matching the value already used elsewhere without relying on glibc feature-test macros. Code coverage testing did not reveal any regression. CMock stats for dcblock_generic.c - lines: 100.0 % (78/78) - branches: 67.2 % (39/58) Ztest stats for dcblock_generic.c - lines: 74.0 % (74/100) - branches: 60.0 % (24/40) The ztest denominator is larger because CONFIG_FORMAT_FLOAT=y on native_sim compiles in dcblock_float_default(), an inert float code path that neither framework's test exercises (legacy CMocka scope is integer formats only); the cmocka build never compiles that function in at all. Restricted to the int-format code shared by both builds, coverage is equivalent. Mutation-tested by dropping the pole term from dcblock_generic()'s difference equation, confirming 5 of 6 cases fail, then reverting. Verified clean under --enable-asan --enable-lsan. Assisted-by: Copilot:claude-sonnet-5 Signed-off-by: Tomasz Leman --- test/ztest/unit/math/dcblock/CMakeLists.txt | 38 +++ test/ztest/unit/math/dcblock/prj.conf | 9 + .../unit/math/dcblock/test_dcblock_ztest.c | 321 ++++++++++++++++++ test/ztest/unit/math/dcblock/testcase.yaml | 25 ++ 4 files changed, 393 insertions(+) create mode 100644 test/ztest/unit/math/dcblock/CMakeLists.txt create mode 100644 test/ztest/unit/math/dcblock/prj.conf create mode 100644 test/ztest/unit/math/dcblock/test_dcblock_ztest.c create mode 100644 test/ztest/unit/math/dcblock/testcase.yaml diff --git a/test/ztest/unit/math/dcblock/CMakeLists.txt b/test/ztest/unit/math/dcblock/CMakeLists.txt new file mode 100644 index 000000000000..94525a51a84d --- /dev/null +++ b/test/ztest/unit/math/dcblock/CMakeLists.txt @@ -0,0 +1,38 @@ +# SPDX-License-Identifier: BSD-3-Clause +# +# Copyright(c) 2026 Intel Corporation. + +cmake_minimum_required(VERSION 3.20.0) + +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) +project(sof_unit_math_dcblock) + +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/platform/posix/include + ${SOF_ROOT}/src/audio/dcblock + ${PROJECT_BINARY_DIR}/include/generated +) + +target_compile_definitions(app PRIVATE + -DCONFIG_ZEPHYR_POSIX=1 + -DCONFIG_LIBRARY=1 + -DUNIT_TEST=1 +) + +target_sources(app PRIVATE + test_dcblock_ztest.c + ${SOF_ROOT}/src/audio/dcblock/dcblock_generic.c + ${SOF_ROOT}/src/math/numbers.c +) + +target_link_libraries(app PRIVATE m) + +sof_append_relative_path_definitions(app) diff --git a/test/ztest/unit/math/dcblock/prj.conf b/test/ztest/unit/math/dcblock/prj.conf new file mode 100644 index 000000000000..29e0b1d1b1fd --- /dev/null +++ b/test/ztest/unit/math/dcblock/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 +# printk()/zassert messages in this suite print doubles (%g); without this, +# printk silently emits the format string literally instead of the value. +CONFIG_CBPRINTF_FP_SUPPORT=y diff --git a/test/ztest/unit/math/dcblock/test_dcblock_ztest.c b/test/ztest/unit/math/dcblock/test_dcblock_ztest.c new file mode 100644 index 000000000000..9a1f6f1874ff --- /dev/null +++ b/test/ztest/unit/math/dcblock/test_dcblock_ztest.c @@ -0,0 +1,321 @@ +// SPDX-License-Identifier: BSD-3-Clause +// +// Copyright(c) 2026 Intel Corporation. +// +// Converted from CMock to Ztest +// Original tests from test/cmocka/src/audio/dcblock/dcblock_math_test.c: +// Author: Piotr Hoppe + +#include + +#include +#include +#include + +#include "dcblock.h" + +#include +#include +#include + +/* math.h's M_PI is only exposed under glibc's GNU/POSIX feature test macros, + * which Zephyr's strict -std=c17 build does not define. + */ +#ifndef M_PI +#define M_PI 3.14159265358979323846 +#endif + +#define TEST_CHANNELS 2 +#define TEST_FRAMES 256 + +/* + * The dcblock processing functions operate on struct cir_buf_source / + * struct cir_buf_sink circular buffer views. The tests use static, non + * wrapping sample buffers, so the views simply span the whole array. + */ +static void cir_buf_src_setup(struct cir_buf_source *s, const void *data, size_t bytes) +{ + s->buf_start = data; + s->buf_end = (const char *)data + bytes; + s->ptr = data; +} + +static void cir_buf_snk_setup(struct cir_buf_sink *s, void *data, size_t bytes) +{ + s->buf_start = data; + s->buf_end = (char *)data + bytes; + s->ptr = data; +} + +/* Q2.30 coefficient close to 1.0 used for the DC removal test cases. */ +#define R_COEF_NEAR_ONE 1063004406 /* ~0.99 in Q2.30 */ + +/* + * Reference DC blocking filter implemented in floating point. Mirrors the + * fixed point recurrence y[n] = x[n] - x[n-1] + R * y[n-1] where R is the + * Q2.30 coefficient converted to a real number. + */ +struct ref_state { + double x_prev; + double y_prev; +}; + +static double dcblock_ref(struct ref_state *s, double r, double x) +{ + double y = x - s->x_prev + r * s->y_prev; + + s->x_prev = x; + s->y_prev = y; + return y; +} + +/* Fill the source buffer with a per-channel sinusoid plus a DC offset. */ +static void gen_input(double *ref_in, int channels, int frames, double dc) +{ + int ch, i; + + for (i = 0; i < frames; i++) { + for (ch = 0; ch < channels; ch++) { + double phase = 2.0 * M_PI * (i + 1) * (ch + 1) / 64.0; + + ref_in[i * channels + ch] = dc + 0.3 * sin(phase); + } + } +} + +/* + * Runs the S32 processing function and compares it to the floating point + * reference. Returns the mean absolute value of the last quarter of the + * output which is used to check DC convergence. + */ +static double run_s32_case(int32_t r_coeff, double dc, double tol_rel) +{ + struct comp_data cd; + struct cir_buf_source csrc; + struct cir_buf_sink csnk; + int32_t src[TEST_FRAMES * TEST_CHANNELS]; + int32_t dst[TEST_FRAMES * TEST_CHANNELS]; + double ref_in[TEST_FRAMES * TEST_CHANNELS]; + struct ref_state rstate[TEST_CHANNELS]; + double r = (double)r_coeff / (double)ONE_Q2_30; + double tail_abs_sum = 0.0; + int tail_count = 0; + dcblock_func func; + int ch, i; + + memset(&cd, 0, sizeof(cd)); + cd.channels = TEST_CHANNELS; + for (ch = 0; ch < TEST_CHANNELS; ch++) + cd.R_coeffs[ch] = r_coeff; + + memset(rstate, 0, sizeof(rstate)); + gen_input(ref_in, TEST_CHANNELS, TEST_FRAMES, dc); + + for (i = 0; i < TEST_FRAMES * TEST_CHANNELS; i++) + src[i] = (int32_t)round(ref_in[i] * 2147483647.0); + + cir_buf_src_setup(&csrc, src, sizeof(src)); + cir_buf_snk_setup(&csnk, dst, sizeof(dst)); + + func = dcblock_find_func(SOF_IPC_FRAME_S32_LE); + zassert_not_null(func, "No S32 dcblock processing function found"); + zassert_equal(func(&cd, &csrc, &csnk, TEST_FRAMES), 0, "dcblock S32 processing failed"); + + for (i = 0; i < TEST_FRAMES; i++) { + for (ch = 0; ch < TEST_CHANNELS; ch++) { + int idx = i * TEST_CHANNELS + ch; + double refy = dcblock_ref(&rstate[ch], r, + (double)src[idx] / 2147483648.0); + double outy = (double)dst[idx] / 2147483648.0; + double delta = fabs(refy - outy); + + zassert_true(delta <= tol_rel, + "s32 mismatch idx %d ref %g out %g delta %g exceeds %g", + idx, refy, outy, delta, tol_rel); + + if (i >= TEST_FRAMES * 3 / 4) { + tail_abs_sum += fabs(outy); + tail_count++; + } + } + } + + return tail_count ? tail_abs_sum / tail_count : 0.0; +} + +/* Passthrough: R = 1.0 gives y[n] = x[n] - x[n-1], a pure differentiator. */ +ZTEST(math_dcblock_suite, test_dcblock_passthrough) +{ + /* With no DC and R=1.0 the reference matches bit-close. */ + run_s32_case(ONE_Q2_30, 0.0, 1.0e-6); +} + +/* A strong DC offset must be attenuated towards zero in steady state. */ +ZTEST(math_dcblock_suite, test_dcblock_dc_removal) +{ + double tail = run_s32_case(R_COEF_NEAR_ONE, 0.5, 1.0e-6); + + /* The residual DC plus small sinusoid must be well below the input DC. */ + printk("dc_removal tail mean abs = %g\n", tail); + zassert_true(tail < 0.25, "tail mean abs %g not below 0.25", tail); +} + +/* Full scale input must not overflow (saturation path in dcblock_generic). */ +ZTEST(math_dcblock_suite, test_dcblock_saturation) +{ + struct comp_data cd; + struct cir_buf_source csrc; + struct cir_buf_sink csnk; + int32_t src[TEST_FRAMES * TEST_CHANNELS]; + int32_t dst[TEST_FRAMES * TEST_CHANNELS]; + dcblock_func func; + int i; + + memset(&cd, 0, sizeof(cd)); + cd.channels = TEST_CHANNELS; + for (i = 0; i < TEST_CHANNELS; i++) + cd.R_coeffs[i] = R_COEF_NEAR_ONE; + + /* + * Alternate per frame so that every channel sees the full-scale + * transitions and exercises the saturation path. + */ + for (i = 0; i < TEST_FRAMES * TEST_CHANNELS; i++) + src[i] = ((i / TEST_CHANNELS) & 1) ? INT32_MAX : INT32_MIN; + + cir_buf_src_setup(&csrc, src, sizeof(src)); + cir_buf_snk_setup(&csnk, dst, sizeof(dst)); + + func = dcblock_find_func(SOF_IPC_FRAME_S32_LE); + zassert_not_null(func, "No S32 dcblock processing function found"); + zassert_equal(func(&cd, &csrc, &csnk, TEST_FRAMES), 0, "dcblock S32 processing failed"); + + for (i = 0; i < TEST_FRAMES * TEST_CHANNELS; i++) { + int32_t expected = ((i / TEST_CHANNELS) & 1) ? INT32_MAX : INT32_MIN; + + zassert_equal(dst[i], expected, "saturation mismatch at idx %d", i); + } +} + +/* Bit-exactness check against the floating point reference for S32. */ +ZTEST(math_dcblock_suite, test_dcblock_bitexact_s32) +{ + run_s32_case(R_COEF_NEAR_ONE, 0.1, 1.0e-6); +} + +/* + * Runs the S16 processing function and compares it to the floating point + * reference. The component internally works in Q1.31, so the tolerance must + * account for the 16-bit output quantization step (2 LSB of S16). + */ +static void run_s16_case(int32_t r_coeff, double dc) +{ + struct comp_data cd; + struct cir_buf_source csrc; + struct cir_buf_sink csnk; + int16_t src[TEST_FRAMES * TEST_CHANNELS]; + int16_t dst[TEST_FRAMES * TEST_CHANNELS]; + double ref_in[TEST_FRAMES * TEST_CHANNELS]; + struct ref_state rstate[TEST_CHANNELS]; + double r = (double)r_coeff / (double)ONE_Q2_30; + double tol = 2.0 / 32768.0; + dcblock_func func; + int ch, i; + + memset(&cd, 0, sizeof(cd)); + cd.channels = TEST_CHANNELS; + for (ch = 0; ch < TEST_CHANNELS; ch++) + cd.R_coeffs[ch] = r_coeff; + + memset(rstate, 0, sizeof(rstate)); + gen_input(ref_in, TEST_CHANNELS, TEST_FRAMES, dc); + + for (i = 0; i < TEST_FRAMES * TEST_CHANNELS; i++) + src[i] = (int16_t)round(ref_in[i] * 32767.0); + + cir_buf_src_setup(&csrc, src, sizeof(src)); + cir_buf_snk_setup(&csnk, dst, sizeof(dst)); + + func = dcblock_find_func(SOF_IPC_FRAME_S16_LE); + zassert_not_null(func, "No S16 dcblock processing function found"); + zassert_equal(func(&cd, &csrc, &csnk, TEST_FRAMES), 0, "dcblock S16 processing failed"); + + for (i = 0; i < TEST_FRAMES; i++) { + for (ch = 0; ch < TEST_CHANNELS; ch++) { + int idx = i * TEST_CHANNELS + ch; + double refy = dcblock_ref(&rstate[ch], r, + (double)src[idx] / 32768.0); + double outy = (double)dst[idx] / 32768.0; + double delta = fabs(refy - outy); + + zassert_true(delta <= tol, + "s16 mismatch idx %d ref %g out %g delta %g exceeds %g", + idx, refy, outy, delta, tol); + } + } +} + +/* Bit-exactness check against the floating point reference for S16. */ +ZTEST(math_dcblock_suite, test_dcblock_bitexact_s16) +{ + run_s16_case(R_COEF_NEAR_ONE, 0.1); +} + +/* + * Runs the S24 (in 32-bit container) processing function and compares it to + * the floating point reference with a tolerance of 2 LSB of S24. + */ +static void run_s24_case(int32_t r_coeff, double dc) +{ + struct comp_data cd; + struct cir_buf_source csrc; + struct cir_buf_sink csnk; + int32_t src[TEST_FRAMES * TEST_CHANNELS]; + int32_t dst[TEST_FRAMES * TEST_CHANNELS]; + double ref_in[TEST_FRAMES * TEST_CHANNELS]; + struct ref_state rstate[TEST_CHANNELS]; + double r = (double)r_coeff / (double)ONE_Q2_30; + double tol = 2.0 / 8388608.0; + dcblock_func func; + int ch, i; + + memset(&cd, 0, sizeof(cd)); + cd.channels = TEST_CHANNELS; + for (ch = 0; ch < TEST_CHANNELS; ch++) + cd.R_coeffs[ch] = r_coeff; + + memset(rstate, 0, sizeof(rstate)); + gen_input(ref_in, TEST_CHANNELS, TEST_FRAMES, dc); + + for (i = 0; i < TEST_FRAMES * TEST_CHANNELS; i++) + src[i] = (int32_t)round(ref_in[i] * 8388607.0); + + cir_buf_src_setup(&csrc, src, sizeof(src)); + cir_buf_snk_setup(&csnk, dst, sizeof(dst)); + + func = dcblock_find_func(SOF_IPC_FRAME_S24_4LE); + zassert_not_null(func, "No S24 dcblock processing function found"); + zassert_equal(func(&cd, &csrc, &csnk, TEST_FRAMES), 0, "dcblock S24 processing failed"); + + for (i = 0; i < TEST_FRAMES; i++) { + for (ch = 0; ch < TEST_CHANNELS; ch++) { + int idx = i * TEST_CHANNELS + ch; + double refy = dcblock_ref(&rstate[ch], r, + (double)src[idx] / 8388608.0); + double outy = (double)dst[idx] / 8388608.0; + double delta = fabs(refy - outy); + + zassert_true(delta <= tol, + "s24 mismatch idx %d ref %g out %g delta %g exceeds %g", + idx, refy, outy, delta, tol); + } + } +} + +/* Bit-exactness check against the floating point reference for S24. */ +ZTEST(math_dcblock_suite, test_dcblock_bitexact_s24) +{ + run_s24_case(R_COEF_NEAR_ONE, 0.1); +} + +ZTEST_SUITE(math_dcblock_suite, NULL, NULL, NULL, NULL, NULL); diff --git a/test/ztest/unit/math/dcblock/testcase.yaml b/test/ztest/unit/math/dcblock/testcase.yaml new file mode 100644 index 000000000000..842d49b1428b --- /dev/null +++ b/test/ztest/unit/math/dcblock/testcase.yaml @@ -0,0 +1,25 @@ +# SPDX-License-Identifier: BSD-3-Clause +# +# Copyright(c) 2026 Intel Corporation. +# +# DC blocking filter math 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 + - math + - dcblock + integration_platforms: + - native_sim + arch_exclude: xtensa + +tests: + sof.unit.math.dcblock: + platform_allow: + - native_sim + build_only: false