From 505a512256e8fa9caeb152589b59ab3489594084 Mon Sep 17 00:00:00 2001 From: Tomasz Leman Date: Tue, 10 Nov 2026 16:58:37 +0100 Subject: [PATCH] test: math: port dft3 CMocka test to ztest Add a ztest suite for dft3_32(), the 3-point DFT used by some FFT plan sizes, covering the same single case as the CMocka test. The reference vectors are included in place from the CMocka tree so both frameworks are driven by identical fixed-point data. dft3_32() is implemented in fft_multi_generic.c (and fft_multi_hifi3.c for Xtensa, gated out on native_sim), so the suite links the same five math sources as the CMocka target. No heap allocation or module-adapter functionality is exercised by this test, so the suite needs no allocator hook. Code coverage testing did not reveal any regression. CMock stats for fft_multi_generic.c - lines: 32.3 % (20/62) - branches: 22.9 % (11/48) Ztest stats for fft_multi_generic.c - lines: 27.3 % (18/66) - branches: 8.3 % (2/24) Both frameworks exercise only dft3_32() in this file; the remaining functions (fft_multi, ifft_multi and their helpers) are covered by the separate fft_multi suite/test, not by dft3. The lower ztest percentage is a smaller denominator effect from clang's finer-grained branch counting for the same source, not a drop in tested behavior: the mutation test below confirms the port's assertions are as strict as the original. Mutation-tested by tightening SOFM_DFT3_MAX_ERROR_RMS from 1.1 to 0.0001, confirming the suite fails, then reverting. Verified clean under --enable-asan --enable-lsan. Assisted-by: Copilot:claude-sonnet-5 Signed-off-by: Tomasz Leman --- test/ztest/unit/math/dft3/CMakeLists.txt | 43 +++++++++ test/ztest/unit/math/dft3/prj.conf | 6 ++ test/ztest/unit/math/dft3/test_dft3_ztest.c | 96 +++++++++++++++++++++ test/ztest/unit/math/dft3/testcase.yaml | 25 ++++++ 4 files changed, 170 insertions(+) create mode 100644 test/ztest/unit/math/dft3/CMakeLists.txt create mode 100644 test/ztest/unit/math/dft3/prj.conf create mode 100644 test/ztest/unit/math/dft3/test_dft3_ztest.c create mode 100644 test/ztest/unit/math/dft3/testcase.yaml diff --git a/test/ztest/unit/math/dft3/CMakeLists.txt b/test/ztest/unit/math/dft3/CMakeLists.txt new file mode 100644 index 000000000000..b97d6e33b2a5 --- /dev/null +++ b/test/ztest/unit/math/dft3/CMakeLists.txt @@ -0,0 +1,43 @@ +# 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_dft3) + +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 + # Reference test vectors are #included in place from the CMocka tree + # rather than copied, so both frameworks share identical data. + ${SOF_ROOT}/test/cmocka/src/math/fft + ${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_dft3_ztest.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 + ${SOF_ROOT}/src/math/fft/fft_common.c + ${SOF_ROOT}/src/math/fft/fft_32.c +) + +target_link_libraries(app PRIVATE m) + +sof_append_relative_path_definitions(app) diff --git a/test/ztest/unit/math/dft3/prj.conf b/test/ztest/unit/math/dft3/prj.conf new file mode 100644 index 000000000000..848e53d24787 --- /dev/null +++ b/test/ztest/unit/math/dft3/prj.conf @@ -0,0 +1,6 @@ +# SPDX-License-Identifier: BSD-3-Clause +# +# Copyright(c) 2026 Intel Corporation. + +CONFIG_ZTEST=y +CONFIG_SOF_FULL_ZEPHYR_APPLICATION=n diff --git a/test/ztest/unit/math/dft3/test_dft3_ztest.c b/test/ztest/unit/math/dft3/test_dft3_ztest.c new file mode 100644 index 000000000000..fb7743c440ab --- /dev/null +++ b/test/ztest/unit/math/dft3/test_dft3_ztest.c @@ -0,0 +1,96 @@ +// SPDX-License-Identifier: BSD-3-Clause +// +// Copyright(c) 2026 Intel Corporation. +// +// Converted from CMock to Ztest +// Original tests from test/cmocka/src/math/fft/dft3.c: +// Author: Seppo Ingalsuo + +#include + +#include +#include +#include + +/* Reference test vectors shared with the legacy CMocka test. They are + * #included in place from the CMocka tree (added to the include path in + * CMakeLists.txt) so the exact same fixed-point data is exercised. + */ +#include "ref_dft3_32.h" + +#include +#include +#include + +#define SOFM_DFT3_MAX_ERROR_ABS 3.1 +#define SOFM_DFT3_MAX_ERROR_RMS 1.1 +#define DFT_SIZE 3 + +BUILD_ASSERT(ARRAY_SIZE(input_data_real_q31) == ARRAY_SIZE(input_data_imag_q31)); +BUILD_ASSERT(ARRAY_SIZE(ref_data_real_q31) == ARRAY_SIZE(ref_data_imag_q31)); +BUILD_ASSERT(ARRAY_SIZE(input_data_real_q31) == ARRAY_SIZE(ref_data_real_q31)); +BUILD_ASSERT(ARRAY_SIZE(input_data_real_q31) == DFT_SIZE * REF_SOFM_DFT3_NUM_TESTS); + +/** + * @brief Run the 3-point DFT against its Q1.31 reference vector. + * + * Feeds each of the REF_SOFM_DFT3_NUM_TESTS blocks of DFT_SIZE complex + * samples through dft3_32() and accumulates the absolute and RMS error + * against the pre-computed reference, preserving the fixed-point semantics + * of the original CMocka test. + */ +ZTEST(math_dft3_suite, test_dft3_32) +{ + struct icomplex32 x[DFT_SIZE]; + struct icomplex32 y[DFT_SIZE]; + double delta; + double error_rms; + double delta_max = 0; + double sum_squares = 0; + const int32_t *p_in_real = input_data_real_q31; + const int32_t *p_in_imag = input_data_imag_q31; + const int32_t *p_ref_real = ref_data_real_q31; + const int32_t *p_ref_imag = ref_data_imag_q31; + int i, j; + + for (i = 0; i < REF_SOFM_DFT3_NUM_TESTS; i++) { + for (j = 0; j < DFT_SIZE; j++) { + x[j].real = *p_in_real++; + x[j].imag = *p_in_imag++; + } + + dft3_32(x, y); + + for (j = 0; j < DFT_SIZE; j++) { + delta = (double)*p_ref_real - (double)y[j].real; + sum_squares += delta * delta; + if (delta > delta_max) + delta_max = delta; + else if (-delta > delta_max) + delta_max = -delta; + + delta = (double)*p_ref_imag - (double)y[j].imag; + sum_squares += delta * delta; + if (delta > delta_max) + delta_max = delta; + else if (-delta > delta_max) + delta_max = -delta; + + p_ref_real++; + p_ref_imag++; + } + } + + error_rms = sqrt(sum_squares / (double)(2 * DFT_SIZE * REF_SOFM_DFT3_NUM_TESTS)); + printk("Max absolute error = %5.2f (limit %5.2f), error RMS = %5.2f (limit %5.2f)\n", + (double)delta_max, (double)SOFM_DFT3_MAX_ERROR_ABS, (double)error_rms, + (double)SOFM_DFT3_MAX_ERROR_RMS); + + zassert_true(error_rms < SOFM_DFT3_MAX_ERROR_RMS, + "error RMS %5.2f exceeds limit %5.2f", error_rms, SOFM_DFT3_MAX_ERROR_RMS); + zassert_true(delta_max < SOFM_DFT3_MAX_ERROR_ABS, + "Max absolute error %5.2f exceeds limit %5.2f", delta_max, + SOFM_DFT3_MAX_ERROR_ABS); +} + +ZTEST_SUITE(math_dft3_suite, NULL, NULL, NULL, NULL, NULL); diff --git a/test/ztest/unit/math/dft3/testcase.yaml b/test/ztest/unit/math/dft3/testcase.yaml new file mode 100644 index 000000000000..6f34ca1f04ba --- /dev/null +++ b/test/ztest/unit/math/dft3/testcase.yaml @@ -0,0 +1,25 @@ +# SPDX-License-Identifier: BSD-3-Clause +# +# Copyright(c) 2026 Intel Corporation. +# +# Math 3-point DFT unit test 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 + - fft + integration_platforms: + - native_sim + arch_exclude: xtensa + +tests: + sof.unit.math.dft3: + platform_allow: + - native_sim + build_only: false