From 58099bd730a398eccbe9fcbd8154e5f0b75459a9 Mon Sep 17 00:00:00 2001 From: Liam Girdwood Date: Fri, 27 Nov 2026 13:55:20 +0000 Subject: [PATCH 01/11] cmocka: Remove dft3 test The dft3 test has been converted to ztest (test/ztest/unit/math/dft3). Remove the CMocka version, the last test in test/cmocka/src/math, and move its reference vectors (ref_dft3_32.h) and their generator script (ref_dft3.m) with git mv into the ztest suite's directory, which included them from the CMocka tree. The script now finds the export helpers in test/cmocka/m from its new directory. Code coverage testing did not reveal any regression. Every line of fft_multi_generic.c that the CMocka test covers is covered by the ztest suite. CMock stats for fft_multi_generic.c (host gcc, unit_test_defconfig) - lines: 32.3 % (20/62) - branches: 22.9 % (11/48) Ztest stats for fft_multi_generic.c (native_sim, host LLVM) - lines: 27.3 % (18/66) - branches: 8.3 % (2/24) The totals differ because gcc and LLVM count lines and branches differently. Co-Authored-By: Claude Opus 5.5 Signed-off-by: Liam Girdwood --- test/cmocka/src/CMakeLists.txt | 1 - test/cmocka/src/math/CMakeLists.txt | 3 - test/cmocka/src/math/fft/CMakeLists.txt | 12 --- test/cmocka/src/math/fft/dft3.c | 96 ------------------- test/ztest/unit/math/dft3/CMakeLists.txt | 3 - .../fft => ztest/unit/math/dft3}/ref_dft3.m | 2 +- .../unit/math/dft3}/ref_dft3_32.h | 0 test/ztest/unit/math/dft3/test_dft3_ztest.c | 5 +- 8 files changed, 2 insertions(+), 120 deletions(-) delete mode 100644 test/cmocka/src/math/CMakeLists.txt delete mode 100644 test/cmocka/src/math/fft/CMakeLists.txt delete mode 100644 test/cmocka/src/math/fft/dft3.c rename test/{cmocka/src/math/fft => ztest/unit/math/dft3}/ref_dft3.m (98%) rename test/{cmocka/src/math/fft => ztest/unit/math/dft3}/ref_dft3_32.h (100%) diff --git a/test/cmocka/src/CMakeLists.txt b/test/cmocka/src/CMakeLists.txt index 2c244aea9d0d..eb66116209ca 100644 --- a/test/cmocka/src/CMakeLists.txt +++ b/test/cmocka/src/CMakeLists.txt @@ -2,4 +2,3 @@ add_subdirectory(audio) add_subdirectory(lib) -add_subdirectory(math) diff --git a/test/cmocka/src/math/CMakeLists.txt b/test/cmocka/src/math/CMakeLists.txt deleted file mode 100644 index 63fee59d926c..000000000000 --- a/test/cmocka/src/math/CMakeLists.txt +++ /dev/null @@ -1,3 +0,0 @@ -# SPDX-License-Identifier: BSD-3-Clause - -add_subdirectory(fft) diff --git a/test/cmocka/src/math/fft/CMakeLists.txt b/test/cmocka/src/math/fft/CMakeLists.txt deleted file mode 100644 index c481c7e9afee..000000000000 --- a/test/cmocka/src/math/fft/CMakeLists.txt +++ /dev/null @@ -1,12 +0,0 @@ -# SPDX-License-Identifier: BSD-3-Clause - -cmocka_test(dft3 - dft3.c - ${PROJECT_SOURCE_DIR}/src/math/fft/fft_multi.c - ${PROJECT_SOURCE_DIR}/src/math/fft/fft_multi_generic.c - ${PROJECT_SOURCE_DIR}/src/math/fft/fft_multi_hifi3.c - ${PROJECT_SOURCE_DIR}/src/math/fft/fft_32.c - ${PROJECT_SOURCE_DIR}/src/math/fft/fft_common.c - ${PROJECT_SOURCE_DIR}/test/cmocka/src/notifier_mocks.c - ${PROJECT_SOURCE_DIR}/test/cmocka/src/common_mocks.c -) diff --git a/test/cmocka/src/math/fft/dft3.c b/test/cmocka/src/math/fft/dft3.c deleted file mode 100644 index a787d8329993..000000000000 --- a/test/cmocka/src/math/fft/dft3.c +++ /dev/null @@ -1,96 +0,0 @@ -// SPDX-License-Identifier: BSD-3-Clause -// -// Copyright(c) 2025 Intel Corporation. All rights reserved. -// -// Author: Seppo Ingalsuo - -#include -#include -#include "ref_dft3_32.h" - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#define SOFM_DFT3_MAX_ERROR_ABS 3.1 -#define SOFM_DFT3_MAX_ERROR_RMS 1.1 -#define DFT_SIZE 3 - -static void dft3_32_test(const int32_t *in_real, const int32_t *in_imag, - const int32_t *ref_real, const int32_t *ref_imag, int num_tests) - -{ - 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 = in_real; - const int32_t *p_in_imag = in_imag; - const int32_t *p_ref_real = ref_real; - const int32_t *p_ref_imag = ref_imag; - int i, j; - - for (i = 0; i < 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 * num_tests)); - printf("Max absolute error = %5.2f (max %5.2f), error RMS = %5.2f (max %5.2f)\n", - delta_max, SOFM_DFT3_MAX_ERROR_ABS, error_rms, SOFM_DFT3_MAX_ERROR_RMS); - - assert_true(error_rms < SOFM_DFT3_MAX_ERROR_RMS); - assert_true(delta_max < SOFM_DFT3_MAX_ERROR_ABS); -} - -static void dft3_32_test_1(void **state) -{ - (void)state; - - dft3_32_test(input_data_real_q31, input_data_imag_q31, - ref_data_real_q31, ref_data_imag_q31, - REF_SOFM_DFT3_NUM_TESTS); -} - -int main(void) -{ - const struct CMUnitTest tests[] = { - cmocka_unit_test(dft3_32_test_1), - }; - - cmocka_set_message_output(CM_OUTPUT_TAP); - - return cmocka_run_group_tests(tests, NULL, NULL); -} diff --git a/test/ztest/unit/math/dft3/CMakeLists.txt b/test/ztest/unit/math/dft3/CMakeLists.txt index 7353560f286c..056072211185 100644 --- a/test/ztest/unit/math/dft3/CMakeLists.txt +++ b/test/ztest/unit/math/dft3/CMakeLists.txt @@ -17,9 +17,6 @@ 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 ) diff --git a/test/cmocka/src/math/fft/ref_dft3.m b/test/ztest/unit/math/dft3/ref_dft3.m similarity index 98% rename from test/cmocka/src/math/fft/ref_dft3.m rename to test/ztest/unit/math/dft3/ref_dft3.m index c3f55d91b3e3..87afe83342d6 100644 --- a/test/cmocka/src/math/fft/ref_dft3.m +++ b/test/ztest/unit/math/dft3/ref_dft3.m @@ -6,7 +6,7 @@ function ref_dft3() - path(path(), '../../../m'); + path(path(), '../../../../cmocka/m'); opt.describe = export_get_git_describe(); N = 3; diff --git a/test/cmocka/src/math/fft/ref_dft3_32.h b/test/ztest/unit/math/dft3/ref_dft3_32.h similarity index 100% rename from test/cmocka/src/math/fft/ref_dft3_32.h rename to test/ztest/unit/math/dft3/ref_dft3_32.h diff --git a/test/ztest/unit/math/dft3/test_dft3_ztest.c b/test/ztest/unit/math/dft3/test_dft3_ztest.c index fb7743c440ab..18e4d084c5a1 100644 --- a/test/ztest/unit/math/dft3/test_dft3_ztest.c +++ b/test/ztest/unit/math/dft3/test_dft3_ztest.c @@ -12,10 +12,7 @@ #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. - */ +/* Reference test vectors, generated with ref_dft3.m */ #include "ref_dft3_32.h" #include From 56f565e9f4b05e273429f579339ed26462641050 Mon Sep 17 00:00:00 2001 From: Liam Girdwood Date: Fri, 27 Nov 2026 13:56:11 +0000 Subject: [PATCH 02/11] cmocka: Remove dcblock_math_test The DC blocking filter math test has been converted to ztest (test/ztest/unit/math/dcblock). Remove the CMocka version. The dcblock_process test in the same directory is not converted yet and stays. Code coverage testing did not reveal any regression. Every line of dcblock_generic.c that the CMocka test covers is covered by the ztest suite. The ztest build also compiles the float path, which neither test exercises. CMock stats for dcblock_generic.c (host gcc, unit_test_defconfig) - lines: 100.0 % (78/78) - branches: 70.7 % (41/58) Ztest stats for dcblock_generic.c (native_sim, host LLVM) - lines: 74.0 % (74/100) - branches: 60.0 % (24/40) Co-Authored-By: Claude Opus 5.5 Signed-off-by: Liam Girdwood --- test/cmocka/src/audio/dcblock/CMakeLists.txt | 9 - .../src/audio/dcblock/dcblock_math_test.c | 345 ------------------ 2 files changed, 354 deletions(-) delete mode 100644 test/cmocka/src/audio/dcblock/dcblock_math_test.c diff --git a/test/cmocka/src/audio/dcblock/CMakeLists.txt b/test/cmocka/src/audio/dcblock/CMakeLists.txt index 0e5754b022d5..fe6294d64635 100644 --- a/test/cmocka/src/audio/dcblock/CMakeLists.txt +++ b/test/cmocka/src/audio/dcblock/CMakeLists.txt @@ -1,14 +1,5 @@ # SPDX-License-Identifier: BSD-3-Clause -cmocka_test(dcblock_math_test - dcblock_math_test.c - ${PROJECT_SOURCE_DIR}/src/audio/dcblock/dcblock_generic.c - ${PROJECT_SOURCE_DIR}/src/math/numbers.c -) - -target_include_directories(dcblock_math_test PRIVATE ${PROJECT_SOURCE_DIR}/src/audio) -target_include_directories(dcblock_math_test PRIVATE ${PROJECT_SOURCE_DIR}/src/audio/dcblock) - cmocka_test(dcblock_process dcblock_process.c ) diff --git a/test/cmocka/src/audio/dcblock/dcblock_math_test.c b/test/cmocka/src/audio/dcblock/dcblock_math_test.c deleted file mode 100644 index d41288c231ad..000000000000 --- a/test/cmocka/src/audio/dcblock/dcblock_math_test.c +++ /dev/null @@ -1,345 +0,0 @@ -// SPDX-License-Identifier: BSD-3-Clause -// -// Copyright(c) 2026 Intel Corporation. - -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include - -#include "dcblock.h" - -#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); - assert_non_null(func); - assert_int_equal(func(&cd, &csrc, &csnk, TEST_FRAMES), 0); - - 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); - - if (delta > tol_rel) { - printf("s32 mismatch idx %d ref %g out %g delta %g\n", - idx, refy, outy, delta); - assert_true(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. */ -static void test_dcblock_passthrough(void **state) -{ - (void)state; - - /* 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. */ -static void test_dcblock_dc_removal(void **state) -{ - (void)state; - - 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. */ - printf("dc_removal tail mean abs = %g\n", tail); - assert_true(tail < 0.25); -} - -/* Full scale input must not overflow (saturation path in dcblock_generic). */ -static void test_dcblock_saturation(void **state) -{ - (void)state; - - 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); - assert_non_null(func); - assert_int_equal(func(&cd, &csrc, &csnk, TEST_FRAMES), 0); - - for (i = 0; i < TEST_FRAMES * TEST_CHANNELS; i++) { - int32_t expected = ((i / TEST_CHANNELS) & 1) ? INT32_MAX : INT32_MIN; - - assert_int_equal(dst[i], expected); - } -} - -/* Bit-exactness check against the floating point reference for S32. */ -static void test_dcblock_bitexact_s32(void **state) -{ - (void)state; - - 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); - assert_non_null(func); - assert_int_equal(func(&cd, &csrc, &csnk, TEST_FRAMES), 0); - - 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); - - if (delta > tol) { - printf("s16 mismatch idx %d ref %g out %g delta %g\n", - idx, refy, outy, delta); - assert_true(delta <= tol); - } - } - } -} - -/* Bit-exactness check against the floating point reference for S16. */ -static void test_dcblock_bitexact_s16(void **state) -{ - (void)state; - - 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); - assert_non_null(func); - assert_int_equal(func(&cd, &csrc, &csnk, TEST_FRAMES), 0); - - 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); - - if (delta > tol) { - printf("s24 mismatch idx %d ref %g out %g delta %g\n", - idx, refy, outy, delta); - assert_true(delta <= tol); - } - } - } -} - -/* Bit-exactness check against the floating point reference for S24. */ -static void test_dcblock_bitexact_s24(void **state) -{ - (void)state; - - run_s24_case(R_COEF_NEAR_ONE, 0.1); -} - -int main(void) -{ - const struct CMUnitTest tests[] = { - cmocka_unit_test(test_dcblock_passthrough), - cmocka_unit_test(test_dcblock_dc_removal), - cmocka_unit_test(test_dcblock_saturation), - cmocka_unit_test(test_dcblock_bitexact_s32), - cmocka_unit_test(test_dcblock_bitexact_s16), - cmocka_unit_test(test_dcblock_bitexact_s24), - }; - - cmocka_set_message_output(CM_OUTPUT_TAP); - - return cmocka_run_group_tests(tests, NULL, NULL); -} From 53df7aa9f71a018a6e14f4f4992e17f52e6cec14 Mon Sep 17 00:00:00 2001 From: Liam Girdwood Date: Fri, 27 Nov 2026 13:56:23 +0000 Subject: [PATCH 03/11] cmocka: Remove fast-get test The fast-get tests have been converted to ztest (test/ztest/unit/fast-get). Remove the CMocka version. Code coverage testing did not reveal any regression. Every line of zephyr/lib/fast-get.c that the CMocka test covers is covered by the ztest suite, except the "Put called to unknown address" error path of fast_put(). The CMocka size mismatch test reached it by accident: it passed the address of its pointer array to fast_put(), which the ztest version corrects to the allocation it got. CMock stats for fast-get.c (host gcc, unit_test_defconfig) - lines: 96.2 % (77/80) - branches: 60.5 % (23/38) Ztest stats for fast-get.c (native_sim, host LLVM) - lines: 80.2 % (81/101) - branches: 51.9 % (27/52) The totals differ because gcc and LLVM count lines and branches differently. Co-Authored-By: Claude Opus 5.5 Signed-off-by: Liam Girdwood --- test/cmocka/src/lib/CMakeLists.txt | 1 - test/cmocka/src/lib/fast-get/CMakeLists.txt | 11 - test/cmocka/src/lib/fast-get/fast-get-tests.c | 192 ------------------ 3 files changed, 204 deletions(-) delete mode 100644 test/cmocka/src/lib/fast-get/CMakeLists.txt delete mode 100644 test/cmocka/src/lib/fast-get/fast-get-tests.c diff --git a/test/cmocka/src/lib/CMakeLists.txt b/test/cmocka/src/lib/CMakeLists.txt index ad3788bc98f0..3059c9de9068 100644 --- a/test/cmocka/src/lib/CMakeLists.txt +++ b/test/cmocka/src/lib/CMakeLists.txt @@ -2,4 +2,3 @@ add_subdirectory(alloc) add_subdirectory(lib) -add_subdirectory(fast-get) diff --git a/test/cmocka/src/lib/fast-get/CMakeLists.txt b/test/cmocka/src/lib/fast-get/CMakeLists.txt deleted file mode 100644 index cf542366bcce..000000000000 --- a/test/cmocka/src/lib/fast-get/CMakeLists.txt +++ /dev/null @@ -1,11 +0,0 @@ -# SPDX-License-Identifier: BSD-3-Clause - -cmocka_test(fast-get-tests - fast-get-tests.c - ${PROJECT_SOURCE_DIR}/src/lib/objpool.c - ${PROJECT_SOURCE_DIR}/zephyr/lib/fast-get.c - ${PROJECT_SOURCE_DIR}/src/lib/alloc.c - ${PROJECT_SOURCE_DIR}/src/kernel/platform/library/lib/memory.c -) - -target_link_libraries(fast-get-tests PRIVATE "-Wl,--wrap=rzalloc,--wrap=rmalloc,--wrap=rfree") diff --git a/test/cmocka/src/lib/fast-get/fast-get-tests.c b/test/cmocka/src/lib/fast-get/fast-get-tests.c deleted file mode 100644 index bd4a6fedfea3..000000000000 --- a/test/cmocka/src/lib/fast-get/fast-get-tests.c +++ /dev/null @@ -1,192 +0,0 @@ -// SPDX-License-Identifier: BSD-3-Clause -// -// Copyright(c) 2024 Intel Corporation. All rights reserved. -// -// Author: Jyri Sarha - -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include -#include - -static const int testdata[33][100] = { - { - 1, 2, 3, 4, 5, 6, 7, 9, 0, - 1, 2, 3, 4, 5, 6, 7, 9, 0, - 1, 2, 3, 4, 5, 6, 7, 9, 0, - 1, 2, 3, 4, 5, 6, 7, 9, 0, - 1, 2, 3, 4, 5, 6, 7, 9, 0, - 1, 2, 3, 4, 5, 6, 7, 9, 0, - 1, 2, 3, 4, 5, 6, 7, 9, 0, - 1, 2, 3, 4, 5, 6, 7, 9, 0, - 1, 2, 3, 4, 5, 6, 7, 9, 0, - 1, 2, 3, 4, 5, 6, 7, 9, 0, - }, - { 2 }, - { 3 }, - { 4 }, - { 5 }, - { 6 }, - { 7 }, - { 8 }, - { 9 }, - { 10 }, - { 11 }, - { 12 }, - { 13 }, - { 14 }, - { 15 }, - { 16 }, - { 17 }, - { 18 }, - { 19 }, - { 20 }, - { 21 }, - { 23 }, - { 24 }, - { 25 }, - { 26 }, - { 27 }, - { 28 }, - { 29 }, - { 30 }, - { 31 }, - { 32 }, - { 33 }, -}; - -static void test_simple_fast_get_put(void **state) -{ - const void *ret; - - (void)state; /* unused */ - - ret = fast_get(NULL, testdata[0], sizeof(testdata[0])); - - assert(ret); - assert(!memcmp(ret, testdata[0], sizeof(testdata[0]))); - - fast_put(NULL, NULL, ret); -} - -static void test_fast_get_size_missmatch_test(void **state) -{ - const void *ret[2]; - - (void)state; /* unused */ - - ret[0] = fast_get(NULL, testdata[0], sizeof(testdata[0])); - - assert(ret[0]); - assert(!memcmp(ret[0], testdata[0], sizeof(testdata[0]))); - - ret[1] = fast_get(NULL, testdata[0], sizeof(testdata[0]) + 1); - assert(!ret[1]); - - fast_put(NULL, NULL, ret); -} - -static void test_over_32_fast_gets_and_puts(void **state) -{ - const void *copy[ARRAY_SIZE(testdata)]; - int i; - - (void)state; /* unused */ - - for (i = 0; i < ARRAY_SIZE(copy); i++) - copy[i] = fast_get(NULL, testdata[i], sizeof(testdata[0])); - - for (i = 0; i < ARRAY_SIZE(copy); i++) - assert(!memcmp(copy[i], testdata[i], sizeof(testdata[0]))); - - for (i = 0; i < ARRAY_SIZE(copy); i++) - fast_put(NULL, NULL, copy[i]); -} - -static void test_fast_get_refcounting(void **state) -{ - const void *copy[2][ARRAY_SIZE(testdata)]; - int i; - (void)state; /* unused */ - - for (i = 0; i < ARRAY_SIZE(copy[0]); i++) - copy[0][i] = fast_get(NULL, testdata[i], sizeof(testdata[0])); - - for (i = 0; i < ARRAY_SIZE(copy[0]); i++) - copy[1][i] = fast_get(NULL, testdata[i], sizeof(testdata[0])); - - for (i = 0; i < ARRAY_SIZE(copy[0]); i++) - assert(copy[0][i] == copy[1][i]); - - for (i = 0; i < ARRAY_SIZE(copy[0]); i++) - assert(!memcmp(copy[0][i], testdata[i], sizeof(testdata[0]))); - - for (i = 0; i < ARRAY_SIZE(copy[0]); i++) - fast_put(NULL, NULL, copy[0][i]); - - for (i = 0; i < ARRAY_SIZE(copy[0]); i++) - assert(!memcmp(copy[1][i], testdata[i], sizeof(testdata[0]))); - - for (i = 0; i < ARRAY_SIZE(copy[0]); i++) - fast_put(NULL, NULL, copy[1][i]); -} - -int main(void) -{ - const struct CMUnitTest tests[] = { - cmocka_unit_test(test_simple_fast_get_put), - cmocka_unit_test(test_fast_get_size_missmatch_test), - cmocka_unit_test(test_over_32_fast_gets_and_puts), - cmocka_unit_test(test_fast_get_refcounting), - }; - - cmocka_set_message_output(CM_OUTPUT_TAP); - - return cmocka_run_group_tests(tests, NULL, NULL); -} - -void *__wrap_rzalloc(uint32_t flags, size_t bytes); -void *__wrap_rmalloc(uint32_t flags, size_t bytes); -void __wrap_rfree(void *ptr); - -void *__wrap_rzalloc(uint32_t flags, size_t bytes) -{ - void *ret; - (void)flags; - - ret = malloc(bytes); - - assert(ret); - - memset(ret, 0, bytes); - - return ret; -} - -void *__wrap_rmalloc(uint32_t flags, size_t bytes) -{ - void *ret; - (void)flags; - - ret = malloc(bytes); - - assert(ret); - - return ret; -} - -void __wrap_rfree(void *ptr) -{ - free(ptr); -} From 70b1f59f464b190a62d937be47b6eae251e30b72 Mon Sep 17 00:00:00 2001 From: Liam Girdwood Date: Fri, 27 Nov 2026 13:57:11 +0000 Subject: [PATCH 04/11] cmocka: Remove buffer tests The four component buffer tests (buffer_copy, buffer_new, buffer_wrap and buffer_write) have been converted to one ztest suite of nine cases (test/ztest/unit/audio/buffer). Remove the CMocka versions. Code coverage testing did not reveal any regression. Every line of comp_buffer.c, audio_buffer.c and audio_stream.c that the CMocka tests cover is covered by the ztest suite. CMock stats for comp_buffer.c, audio_buffer.c, audio_stream.c (host gcc, unit_test_defconfig) - lines: 26.2 % (87/332) - branches: 8.3 % (19/230) Ztest stats for comp_buffer.c, audio_buffer.c, audio_stream.c (native_sim, host LLVM) - lines: 23.6 % (87/368) - branches: 10.3 % (15/146) The totals differ because gcc and LLVM count lines and branches differently. Co-Authored-By: Claude Opus 5.5 Signed-off-by: Liam Girdwood --- test/cmocka/src/audio/CMakeLists.txt | 1 - test/cmocka/src/audio/buffer/CMakeLists.txt | 101 ------------ test/cmocka/src/audio/buffer/buffer_copy.c | 171 -------------------- test/cmocka/src/audio/buffer/buffer_new.c | 49 ------ test/cmocka/src/audio/buffer/buffer_wrap.c | 89 ---------- test/cmocka/src/audio/buffer/buffer_write.c | 96 ----------- 6 files changed, 507 deletions(-) delete mode 100644 test/cmocka/src/audio/buffer/CMakeLists.txt delete mode 100644 test/cmocka/src/audio/buffer/buffer_copy.c delete mode 100644 test/cmocka/src/audio/buffer/buffer_new.c delete mode 100644 test/cmocka/src/audio/buffer/buffer_wrap.c delete mode 100644 test/cmocka/src/audio/buffer/buffer_write.c diff --git a/test/cmocka/src/audio/CMakeLists.txt b/test/cmocka/src/audio/CMakeLists.txt index a5b300b6f232..9198bbfe6e58 100644 --- a/test/cmocka/src/audio/CMakeLists.txt +++ b/test/cmocka/src/audio/CMakeLists.txt @@ -1,6 +1,5 @@ # SPDX-License-Identifier: BSD-3-Clause -add_subdirectory(buffer) add_subdirectory(component) add_subdirectory(pcm_converter) if(CONFIG_COMP_MIXER) diff --git a/test/cmocka/src/audio/buffer/CMakeLists.txt b/test/cmocka/src/audio/buffer/CMakeLists.txt deleted file mode 100644 index 805aaa2da784..000000000000 --- a/test/cmocka/src/audio/buffer/CMakeLists.txt +++ /dev/null @@ -1,101 +0,0 @@ -# SPDX-License-Identifier: BSD-3-Clause - -cmocka_test(buffer_copy - buffer_copy.c - ${PROJECT_SOURCE_DIR}/test/cmocka/src/notifier_mocks.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}/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/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 - ${PROJECT_SOURCE_DIR}/src/math/numbers.c -) - -cmocka_test(buffer_new - buffer_new.c - ${PROJECT_SOURCE_DIR}/test/cmocka/src/common_mocks.c - ${PROJECT_SOURCE_DIR}/test/cmocka/src/notifier_mocks.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}/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/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 - ${PROJECT_SOURCE_DIR}/src/math/numbers.c -) - -cmocka_test(buffer_wrap - buffer_wrap.c - ${PROJECT_SOURCE_DIR}/test/cmocka/src/common_mocks.c - ${PROJECT_SOURCE_DIR}/test/cmocka/src/notifier_mocks.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}/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/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 - ${PROJECT_SOURCE_DIR}/src/math/numbers.c -) - -cmocka_test(buffer_write - buffer_write.c - ${PROJECT_SOURCE_DIR}/test/cmocka/src/common_mocks.c - ${PROJECT_SOURCE_DIR}/test/cmocka/src/notifier_mocks.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}/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/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 - ${PROJECT_SOURCE_DIR}/src/math/numbers.c -) diff --git a/test/cmocka/src/audio/buffer/buffer_copy.c b/test/cmocka/src/audio/buffer/buffer_copy.c deleted file mode 100644 index 35a3dbc73b0e..000000000000 --- a/test/cmocka/src/audio/buffer/buffer_copy.c +++ /dev/null @@ -1,171 +0,0 @@ -// SPDX-License-Identifier: BSD-3-Clause -// -// Copyright(c) 2018 Intel Corporation. All rights reserved. -// -// Author: Slawomir Blauciak - -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include - -static void test_audio_buffer_copy_underrun(void **state) -{ - int copy_bytes; - - (void)state; - - struct sof_ipc_buffer test_buf_desc = { - .size = 256 - }; - - struct comp_buffer *src = buffer_new(NULL, &test_buf_desc, BUFFER_USAGE_NOT_SHARED); - struct comp_buffer *snk = buffer_new(NULL, &test_buf_desc, BUFFER_USAGE_NOT_SHARED); - - assert_non_null(src); - assert_non_null(snk); - - comp_update_buffer_produce(src, 10); - copy_bytes = - audio_stream_can_copy_bytes(&src->stream, &snk->stream, 16); - - assert_int_equal(audio_stream_get_avail_bytes(&src->stream), 10); - assert_int_equal(copy_bytes, -1); - - buffer_free(src); - buffer_free(snk); -} - -static void test_audio_buffer_copy_overrun(void **state) -{ - int copy_bytes; - - (void)state; - - struct sof_ipc_buffer test_buf_desc = { - .size = 256 - }; - - struct comp_buffer *src = buffer_new(NULL, &test_buf_desc, BUFFER_USAGE_NOT_SHARED); - struct comp_buffer *snk = buffer_new(NULL, &test_buf_desc, BUFFER_USAGE_NOT_SHARED); - - assert_non_null(src); - assert_non_null(snk); - - comp_update_buffer_produce(src, 16); - comp_update_buffer_produce(snk, 246); - copy_bytes = - audio_stream_can_copy_bytes(&src->stream, &snk->stream, 16); - - assert_int_equal(audio_stream_get_avail_bytes(&src->stream), 16); - assert_int_equal(audio_stream_get_free_bytes(&snk->stream), 10); - assert_int_equal(copy_bytes, 1); - - buffer_free(src); - buffer_free(snk); -} - -static void test_audio_buffer_copy_success(void **state) -{ - int copy_bytes; - - (void)state; - - struct sof_ipc_buffer test_buf_desc = { - .size = 256 - }; - - struct comp_buffer *src = buffer_new(NULL, &test_buf_desc, BUFFER_USAGE_NOT_SHARED); - struct comp_buffer *snk = buffer_new(NULL, &test_buf_desc, BUFFER_USAGE_NOT_SHARED); - - assert_non_null(src); - assert_non_null(snk); - - comp_update_buffer_produce(src, 10); - copy_bytes = audio_stream_can_copy_bytes(&src->stream, &snk->stream, 0); - - assert_int_equal(audio_stream_get_avail_bytes(&src->stream), 10); - assert_int_equal(copy_bytes, 0); - - buffer_free(src); - buffer_free(snk); -} - -static void test_audio_buffer_copy_fit_space_constraint(void **state) -{ - int copy_bytes; - - (void)state; - - struct sof_ipc_buffer test_buf_desc = { - .size = 256 - }; - - struct comp_buffer *src = buffer_new(NULL, &test_buf_desc, BUFFER_USAGE_NOT_SHARED); - struct comp_buffer *snk = buffer_new(NULL, &test_buf_desc, BUFFER_USAGE_NOT_SHARED); - - assert_non_null(src); - assert_non_null(snk); - - comp_update_buffer_produce(src, 16); - comp_update_buffer_produce(snk, 246); - copy_bytes = audio_stream_get_copy_bytes(&src->stream, &snk->stream); - - assert_int_equal(audio_stream_get_avail_bytes(&src->stream), 16); - assert_int_equal(audio_stream_get_free_bytes(&snk->stream), 10); - assert_int_equal(copy_bytes, 10); - - buffer_free(src); - buffer_free(snk); -} - -static void test_audio_buffer_copy_fit_no_space_constraint(void **state) -{ - int copy_bytes; - - (void)state; - - struct sof_ipc_buffer test_buf_desc = { - .size = 256 - }; - - struct comp_buffer *src = buffer_new(NULL, &test_buf_desc, BUFFER_USAGE_NOT_SHARED); - struct comp_buffer *snk = buffer_new(NULL, &test_buf_desc, BUFFER_USAGE_NOT_SHARED); - - assert_non_null(src); - assert_non_null(snk); - - comp_update_buffer_produce(src, 16); - copy_bytes = audio_stream_get_copy_bytes(&src->stream, &snk->stream); - - assert_int_equal(audio_stream_get_avail_bytes(&src->stream), 16); - assert_int_equal(copy_bytes, 16); - - buffer_free(src); - buffer_free(snk); -} - -int main(void) -{ - const struct CMUnitTest tests[] = { - cmocka_unit_test(test_audio_buffer_copy_underrun), - cmocka_unit_test(test_audio_buffer_copy_overrun), - cmocka_unit_test(test_audio_buffer_copy_success), - cmocka_unit_test(test_audio_buffer_copy_fit_space_constraint), - cmocka_unit_test(test_audio_buffer_copy_fit_no_space_constraint) - }; - - cmocka_set_message_output(CM_OUTPUT_TAP); - - return cmocka_run_group_tests(tests, NULL, NULL); -} diff --git a/test/cmocka/src/audio/buffer/buffer_new.c b/test/cmocka/src/audio/buffer/buffer_new.c deleted file mode 100644 index 8aee748b029d..000000000000 --- a/test/cmocka/src/audio/buffer/buffer_new.c +++ /dev/null @@ -1,49 +0,0 @@ -// SPDX-License-Identifier: BSD-3-Clause -// -// Copyright(c) 2018 Intel Corporation. All rights reserved. -// -// Author: Slawomir Blauciak - -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include - -static void test_audio_buffer_new(void **state) -{ - (void)state; - - struct sof_ipc_buffer test_buf_desc = { - .size = 256 - }; - - struct comp_buffer *buf = buffer_new(NULL, &test_buf_desc, BUFFER_USAGE_NOT_SHARED); - - assert_non_null(buf); - assert_int_equal(audio_stream_get_avail_bytes(&buf->stream), 0); - assert_int_equal(audio_stream_get_free_bytes(&buf->stream), 256); - assert_ptr_equal(buf->stream.w_ptr, buf->stream.r_ptr); - - buffer_free(buf); -} - -int main(void) -{ - const struct CMUnitTest tests[] = { - cmocka_unit_test(test_audio_buffer_new) - }; - - cmocka_set_message_output(CM_OUTPUT_TAP); - - return cmocka_run_group_tests(tests, NULL, NULL); -} diff --git a/test/cmocka/src/audio/buffer/buffer_wrap.c b/test/cmocka/src/audio/buffer/buffer_wrap.c deleted file mode 100644 index 282249ee9a9a..000000000000 --- a/test/cmocka/src/audio/buffer/buffer_wrap.c +++ /dev/null @@ -1,89 +0,0 @@ -// SPDX-License-Identifier: BSD-3-Clause -// -// Copyright(c) 2018 Intel Corporation. All rights reserved. -// -// Author: Slawomir Blauciak - -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include - -static void test_audio_buffer_write_fill_10_bytes_and_write_5(void **state) -{ - (void)state; - - struct sof_ipc_buffer test_buf_desc = { - .size = 10 - }; - - struct comp_buffer *buf = buffer_new(NULL, &test_buf_desc, BUFFER_USAGE_NOT_SHARED); - - assert_non_null(buf); - assert_int_equal(audio_stream_get_avail_bytes(&buf->stream), 0); - assert_int_equal(audio_stream_get_free_bytes(&buf->stream), 10); - assert_ptr_equal(buf->stream.w_ptr, buf->stream.r_ptr); - - uint8_t bytes[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; - - int i; - uint8_t *ptr; - - 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)); - - assert_int_equal(audio_stream_get_avail_bytes(&buf->stream), sizeof(bytes)); - assert_int_equal(audio_stream_get_free_bytes(&buf->stream), 0); - assert_ptr_equal(buf->stream.w_ptr, buf->stream.r_ptr); - - uint8_t more_bytes[5] = {10, 11, 12, 13, 14}; - - 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)); - - uint8_t ref_1[5] = {5, 6, 7, 8, 9}; - uint8_t ref_2[5] = {10, 11, 12, 13, 14}; - - assert_int_equal(audio_stream_get_avail_bytes(&buf->stream), 10); - assert_int_equal(audio_stream_get_free_bytes(&buf->stream), 0); - assert_ptr_equal(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)); - assert_int_equal(*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)); - assert_int_equal(*ptr, ref_2[i]); - } - - buffer_free(buf); -} - -int main(void) -{ - const struct CMUnitTest tests[] = { - cmocka_unit_test - (test_audio_buffer_write_fill_10_bytes_and_write_5) - }; - - cmocka_set_message_output(CM_OUTPUT_TAP); - - return cmocka_run_group_tests(tests, NULL, NULL); -} diff --git a/test/cmocka/src/audio/buffer/buffer_write.c b/test/cmocka/src/audio/buffer/buffer_write.c deleted file mode 100644 index 7982c22fb32d..000000000000 --- a/test/cmocka/src/audio/buffer/buffer_write.c +++ /dev/null @@ -1,96 +0,0 @@ -// SPDX-License-Identifier: BSD-3-Clause -// -// Copyright(c) 2018 Intel Corporation. All rights reserved. -// -// Author: Slawomir Blauciak - -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include - -static void test_audio_buffer_write_10_bytes_out_of_256_and_read_back - (void **state) -{ - (void)state; - - struct sof_ipc_buffer test_buf_desc = { - .size = 256 - }; - - struct comp_buffer *buf = buffer_new(NULL, &test_buf_desc, BUFFER_USAGE_NOT_SHARED); - - assert_non_null(buf); - assert_int_equal(audio_stream_get_avail_bytes(&buf->stream), 0); - assert_int_equal(audio_stream_get_free_bytes(&buf->stream), 256); - assert_ptr_equal(buf->stream.w_ptr, buf->stream.r_ptr); - - uint8_t bytes[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; - - memcpy_s(buf->stream.w_ptr, test_buf_desc.size, &bytes, 10); - comp_update_buffer_produce(buf, 10); - - assert_int_equal(audio_stream_get_avail_bytes(&buf->stream), 10); - assert_int_equal(audio_stream_get_free_bytes(&buf->stream), 246); - assert_ptr_equal(buf->stream.w_ptr, (char *)buf->stream.r_ptr + 10); - - assert_int_equal(memcmp(buf->stream.r_ptr, &bytes, 10), 0); - - comp_update_buffer_consume(buf, 10); - - assert_int_equal(audio_stream_get_avail_bytes(&buf->stream), 0); - assert_int_equal(audio_stream_get_free_bytes(&buf->stream), 256); - assert_ptr_equal(buf->stream.w_ptr, buf->stream.r_ptr); - - buffer_free(buf); -} - -static void test_audio_buffer_fill_10_bytes(void **state) -{ - (void)state; - - struct sof_ipc_buffer test_buf_desc = { - .size = 10 - }; - - struct comp_buffer *buf = buffer_new(NULL, &test_buf_desc, BUFFER_USAGE_NOT_SHARED); - - assert_non_null(buf); - assert_int_equal(audio_stream_get_avail_bytes(&buf->stream), 0); - assert_int_equal(audio_stream_get_free_bytes(&buf->stream), 10); - assert_ptr_equal(buf->stream.w_ptr, buf->stream.r_ptr); - - uint8_t bytes[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; - - memcpy_s(buf->stream.w_ptr, test_buf_desc.size, &bytes, 10); - comp_update_buffer_produce(buf, 10); - - assert_int_equal(audio_stream_get_avail_bytes(&buf->stream), 10); - assert_int_equal(audio_stream_get_free_bytes(&buf->stream), 0); - assert_ptr_equal(buf->stream.w_ptr, buf->stream.r_ptr); - - buffer_free(buf); -} - -int main(void) -{ - const struct CMUnitTest tests[] = { - cmocka_unit_test - (test_audio_buffer_write_10_bytes_out_of_256_and_read_back), - cmocka_unit_test(test_audio_buffer_fill_10_bytes) - }; - - cmocka_set_message_output(CM_OUTPUT_TAP); - - return cmocka_run_group_tests(tests, NULL, NULL); -} From 40a2b0b9943ea749081d31b7d248dce1f15db705 Mon Sep 17 00:00:00 2001 From: Liam Girdwood Date: Fri, 27 Nov 2026 15:41:44 +0000 Subject: [PATCH 05/11] cmocka: Remove the host library allocator and string tests The alloc, rstrcmp and rstrlen tests check the host library's allocator (src/lib/alloc.c) and string functions (src/lib/lib.c). Both files are only built with CONFIG_LIBRARY, for the testbench and these unit tests. The firmware uses the Zephyr allocator in zephyr/lib/alloc.c and the inline rstrlen() and rstrcmp() wrappers in zephyr/include/rtos/string.h, which these tests do not cover. So they are removed rather than converted to ztest. The alloc test also added the cAVS platform include directories to sof_options for all CMocka tests. The remaining tests build and pass without them, for every CMocka defconfig. Co-Authored-By: Claude Opus 5.5 Signed-off-by: Liam Girdwood --- test/cmocka/src/CMakeLists.txt | 1 - test/cmocka/src/lib/CMakeLists.txt | 4 - test/cmocka/src/lib/alloc/CMakeLists.txt | 10 - test/cmocka/src/lib/alloc/alloc.c | 299 ----------------------- test/cmocka/src/lib/lib/CMakeLists.txt | 11 - test/cmocka/src/lib/lib/rstrcmp.c | 222 ----------------- test/cmocka/src/lib/lib/rstrlen.c | 105 -------- 7 files changed, 652 deletions(-) delete mode 100644 test/cmocka/src/lib/CMakeLists.txt delete mode 100644 test/cmocka/src/lib/alloc/CMakeLists.txt delete mode 100644 test/cmocka/src/lib/alloc/alloc.c delete mode 100644 test/cmocka/src/lib/lib/CMakeLists.txt delete mode 100644 test/cmocka/src/lib/lib/rstrcmp.c delete mode 100644 test/cmocka/src/lib/lib/rstrlen.c diff --git a/test/cmocka/src/CMakeLists.txt b/test/cmocka/src/CMakeLists.txt index eb66116209ca..1cdd0d2137a9 100644 --- a/test/cmocka/src/CMakeLists.txt +++ b/test/cmocka/src/CMakeLists.txt @@ -1,4 +1,3 @@ # SPDX-License-Identifier: BSD-3-Clause add_subdirectory(audio) -add_subdirectory(lib) diff --git a/test/cmocka/src/lib/CMakeLists.txt b/test/cmocka/src/lib/CMakeLists.txt deleted file mode 100644 index 3059c9de9068..000000000000 --- a/test/cmocka/src/lib/CMakeLists.txt +++ /dev/null @@ -1,4 +0,0 @@ -# SPDX-License-Identifier: BSD-3-Clause - -add_subdirectory(alloc) -add_subdirectory(lib) diff --git a/test/cmocka/src/lib/alloc/CMakeLists.txt b/test/cmocka/src/lib/alloc/CMakeLists.txt deleted file mode 100644 index 5e899d3955af..000000000000 --- a/test/cmocka/src/lib/alloc/CMakeLists.txt +++ /dev/null @@ -1,10 +0,0 @@ -# SPDX-License-Identifier: BSD-3-Clause - -cmocka_test(alloc - alloc.c - ${PROJECT_SOURCE_DIR}/src/lib/alloc.c - ${PROJECT_SOURCE_DIR}/src/kernel/platform/library/lib/memory.c -) - -target_include_directories(sof_options INTERFACE ${PROJECT_SOURCE_DIR}/src/kernel/platform/intel/cavs/include) -target_include_directories(sof_options INTERFACE ${PROJECT_SOURCE_DIR}/src/platform/intel/cavs/include) diff --git a/test/cmocka/src/lib/alloc/alloc.c b/test/cmocka/src/lib/alloc/alloc.c deleted file mode 100644 index d666141c45b2..000000000000 --- a/test/cmocka/src/lib/alloc/alloc.c +++ /dev/null @@ -1,299 +0,0 @@ -// SPDX-License-Identifier: BSD-3-Clause -// -// Copyright(c) 2018 Intel Corporation. All rights reserved. -// -// Author: Slawomir Blauciak - -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include - -enum test_type { - TEST_BULK = 0, - TEST_ZERO, - TEST_IMMEDIATE_FREE -}; - -struct test_case { - size_t alloc_size; - uint32_t alloc_flags; - uint16_t alloc_num; - enum test_type type; - const char *name; -}; - -#define TEST_CASE(bytes, flags, num, type, name_base) \ - {(bytes), (flags), (num), (type), \ - ("test_lib_alloc_" name_base "__" #flags "__" #bytes "x" #num)} - -static struct test_case test_cases[] = { - /* - * rmalloc tests - */ - - TEST_CASE(1, SOF_MEM_FLAG_USER, 2, TEST_BULK, - "rmalloc"), - TEST_CASE(4, SOF_MEM_FLAG_USER, 2, TEST_BULK, - "rmalloc"), - TEST_CASE(256, SOF_MEM_FLAG_USER, 2, TEST_BULK, - "rmalloc"), - - TEST_CASE(1, SOF_MEM_FLAG_USER, 4, TEST_BULK, - "rmalloc"), - TEST_CASE(4, SOF_MEM_FLAG_USER, 4, TEST_BULK, - "rmalloc"), - TEST_CASE(256, SOF_MEM_FLAG_USER, 4, TEST_BULK, - "rmalloc"), - - TEST_CASE(1, SOF_MEM_FLAG_USER, 8, TEST_BULK, - "rmalloc"), - TEST_CASE(4, SOF_MEM_FLAG_USER, 8, TEST_BULK, - "rmalloc"), - TEST_CASE(256, SOF_MEM_FLAG_USER, 8, TEST_BULK, - "rmalloc"), - - TEST_CASE(16, SOF_MEM_FLAG_USER, 128, TEST_BULK, - "rmalloc"), - - /* - * TODO: Due to recent changes in relation to multicore support - * the RZONE_BUFFER and RZONE_RUNTIME tests will not work. - */ - - TEST_CASE(1, SOF_MEM_FLAG_USER, 2, TEST_BULK, - "rmalloc"), - TEST_CASE(4, SOF_MEM_FLAG_USER, 2, TEST_BULK, - "rmalloc"), - TEST_CASE(256, SOF_MEM_FLAG_USER, 2, TEST_BULK, - "rmalloc"), - - TEST_CASE(1, SOF_MEM_FLAG_USER, 4, TEST_BULK, - "rmalloc"), - TEST_CASE(4, SOF_MEM_FLAG_USER, 4, TEST_BULK, - "rmalloc"), - TEST_CASE(256, SOF_MEM_FLAG_USER, 4, TEST_BULK, - "rmalloc"), - - TEST_CASE(1, SOF_MEM_FLAG_USER, 8, TEST_BULK, - "rmalloc"), - TEST_CASE(4, SOF_MEM_FLAG_USER, 8, TEST_BULK, - "rmalloc"), - TEST_CASE(256, SOF_MEM_FLAG_USER, 8, TEST_BULK, - "rmalloc"), - - TEST_CASE(16, SOF_MEM_FLAG_USER, 128, TEST_BULK, - "rmalloc"), - - TEST_CASE(1, (SOF_MEM_FLAG_USER | SOF_MEM_FLAG_DMA), - 2, TEST_BULK, "rmalloc_dma"), - TEST_CASE(4, (SOF_MEM_FLAG_USER | SOF_MEM_FLAG_DMA), - 2, TEST_BULK, "rmalloc_dma"), - TEST_CASE(256, (SOF_MEM_FLAG_USER | SOF_MEM_FLAG_DMA), - 2, TEST_BULK, "rmalloc_dma"), - - /* - * rzalloc tests - */ - - TEST_CASE(1, SOF_MEM_FLAG_USER, 2, TEST_ZERO, - "rzalloc"), - TEST_CASE(4, SOF_MEM_FLAG_USER, 2, TEST_ZERO, - "rzalloc"), - TEST_CASE(256, SOF_MEM_FLAG_USER, 2, TEST_ZERO, - "rzalloc"), - - TEST_CASE(1, SOF_MEM_FLAG_USER, 4, TEST_ZERO, - "rzalloc"), - TEST_CASE(4, SOF_MEM_FLAG_USER, 4, TEST_ZERO, - "rzalloc"), - TEST_CASE(256, SOF_MEM_FLAG_USER, 4, TEST_ZERO, - "rzalloc"), - - TEST_CASE(1, SOF_MEM_FLAG_USER, 8, TEST_ZERO, - "rzalloc"), - TEST_CASE(4, SOF_MEM_FLAG_USER, 8, TEST_ZERO, - "rzalloc"), - TEST_CASE(256, SOF_MEM_FLAG_USER, 8, TEST_ZERO, - "rzalloc"), - - TEST_CASE(16, SOF_MEM_FLAG_USER, 128, TEST_ZERO, - "rzalloc"), - - TEST_CASE(1, SOF_MEM_FLAG_USER, 2, TEST_ZERO, - "rzalloc"), - TEST_CASE(4, SOF_MEM_FLAG_USER, 2, TEST_ZERO, - "rzalloc"), - TEST_CASE(256, SOF_MEM_FLAG_USER, 2, TEST_ZERO, - "rzalloc"), - - TEST_CASE(1, SOF_MEM_FLAG_USER, 4, TEST_ZERO, - "rzalloc"), - TEST_CASE(4, SOF_MEM_FLAG_USER, 4, TEST_ZERO, - "rzalloc"), - TEST_CASE(256, SOF_MEM_FLAG_USER, 4, TEST_ZERO, - "rzalloc"), - - TEST_CASE(1, SOF_MEM_FLAG_USER, 8, TEST_ZERO, - "rzalloc"), - TEST_CASE(4, SOF_MEM_FLAG_USER, 8, TEST_ZERO, - "rzalloc"), - TEST_CASE(256, SOF_MEM_FLAG_USER, 8, TEST_ZERO, - "rzalloc"), - - TEST_CASE(16, SOF_MEM_FLAG_USER, 128, TEST_ZERO, - "rzalloc"), - - TEST_CASE(1, (SOF_MEM_FLAG_USER | SOF_MEM_FLAG_DMA), - 2, TEST_ZERO, "rzalloc_dma"), - TEST_CASE(4, (SOF_MEM_FLAG_USER | SOF_MEM_FLAG_DMA), - 2, TEST_ZERO, "rzalloc_dma"), - TEST_CASE(256, (SOF_MEM_FLAG_USER | SOF_MEM_FLAG_DMA), - 2, TEST_ZERO, "rzalloc_dma"), -}; - -static int setup(void **state) -{ - platform_init_memmap(sof_get()); - init_heap(sof_get()); - - return 0; -} - -static int teardown(void **state) -{ - return 0; -} - -static int clear_sys(void **state) -{ - struct mm *memmap = memmap_get(); - int sysheap_idx = 0; - - for (; sysheap_idx < ARRAY_SIZE(memmap->system); ++sysheap_idx) { - struct mm_heap *cpu_heap = &memmap->system[sysheap_idx]; - - cpu_heap->info.used = 0; - cpu_heap->info.free = cpu_heap->size; - } - - return 0; -} - -static void *alloc(struct test_case *tc) -{ - void *mem; - - mem = rmalloc(tc->alloc_flags, - tc->alloc_size); - - return mem; -} - -static void alloc_free(void **mem, struct test_case *tc) -{ - int i; - - for (i = 0; i < tc->alloc_num; ++i) - rfree(mem[i]); -} - -static void test_lib_alloc_bulk_free(struct test_case *tc) -{ - void **all_mem = malloc(sizeof(void *) * tc->alloc_num); - int i; - - for (i = 0; i < tc->alloc_num; ++i) { - void *mem = alloc(tc); - - assert_non_null(mem); - all_mem[i] = mem; - } - - alloc_free(all_mem, tc); - - free(all_mem); -} - -static void test_lib_alloc_immediate_free(struct test_case *tc) -{ - int i; - - for (i = 0; i < tc->alloc_num; ++i) { - void *mem = alloc(tc); - - assert_non_null(mem); - rfree(mem); - } -} - -static void test_lib_alloc_zero(struct test_case *tc) -{ - void **all_mem = malloc(sizeof(void *) * tc->alloc_num); - int i; - - for (i = 0; i < tc->alloc_num; ++i) { - char *mem = rzalloc(tc->alloc_flags, - tc->alloc_size); - int j; - - assert_non_null(mem); - all_mem[i] = mem; - - for (j = 0; j < tc->alloc_size; ++j) - assert_int_equal(mem[j], 0); - } - - alloc_free(all_mem, tc); - - free(all_mem); -} - -static void test_lib_alloc(void **state) -{ - struct test_case *tc = *((struct test_case **)state); - - switch (tc->type) { - case TEST_BULK: - test_lib_alloc_bulk_free(tc); - break; - - case TEST_ZERO: - test_lib_alloc_zero(tc); - break; - - case TEST_IMMEDIATE_FREE: - test_lib_alloc_immediate_free(tc); - break; - } -} - -int main(void) -{ - struct CMUnitTest tests[ARRAY_SIZE(test_cases)]; - - 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_lib_alloc; - t->initial_state = &test_cases[i]; - t->setup_func = clear_sys; - t->teardown_func = NULL; - } - - cmocka_set_message_output(CM_OUTPUT_TAP); - - return cmocka_run_group_tests(tests, setup, teardown); -} diff --git a/test/cmocka/src/lib/lib/CMakeLists.txt b/test/cmocka/src/lib/lib/CMakeLists.txt deleted file mode 100644 index 1287a1d14f94..000000000000 --- a/test/cmocka/src/lib/lib/CMakeLists.txt +++ /dev/null @@ -1,11 +0,0 @@ -# SPDX-License-Identifier: BSD-3-Clause - -cmocka_test(rstrcmp - rstrcmp.c - ${PROJECT_SOURCE_DIR}/src/lib/lib.c -) - -cmocka_test(rstrlen - rstrlen.c - ${PROJECT_SOURCE_DIR}/src/lib/lib.c -) diff --git a/test/cmocka/src/lib/lib/rstrcmp.c b/test/cmocka/src/lib/lib/rstrcmp.c deleted file mode 100644 index 8cde07bcd4a6..000000000000 --- a/test/cmocka/src/lib/lib/rstrcmp.c +++ /dev/null @@ -1,222 +0,0 @@ -// SPDX-License-Identifier: BSD-3-Clause -// -// Copyright(c) 2018 Intel Corporation. All rights reserved. -// -// Author: Michal Jerzy Wierzbicki - -#include - -#include -#include -#include -#include - -static void test_lib_lib_rstrcmp_for_a_and_a_equals_0(void **state) -{ - int r; - const char *str1 = "a"; - const char *str2 = "a"; - - (void) state; - - r = rstrcmp(str1, str2); - assert_int_equal(r, 0); -} - -static void test_lib_lib_rstrcmp_for_a_and_b_is_negative(void **state) -{ - int r; - const char *str1 = "a"; - const char *str2 = "b"; - - (void) state; - - r = rstrcmp(str1, str2); - assert_true(r < 0); -} - -static void test_lib_lib_rstrcmp_for_b_and_a_is_positive(void **state) -{ - int r; - const char *str1 = "b"; - const char *str2 = "a"; - - (void) state; - - r = rstrcmp(str1, str2); - assert_true(r > 0); -} - -static void test_lib_lib_rstrcmp_for_empty_and_null_str_equals_0(void **state) -{ - int r; - const char *str1 = ""; - const char *str2 = "\0"; - - (void) state; - - r = rstrcmp(str1, str2); - assert_int_equal(r, 0); -} - -static void test_lib_lib_rstrcmp_for_abc_and_abcd_is_negative(void **state) -{ - int r; - const char *str1 = "abc"; - const char *str2 = "abcd"; - - (void) state; - - r = rstrcmp(str1, str2); - assert_true(r < 0); -} - -static void test_lib_lib_rstrcmp_for_abcd_and_abc_is_positive(void **state) -{ - int r; - const char *str1 = "abcd"; - const char *str2 = "abc"; - - (void) state; - - r = rstrcmp(str1, str2); - - assert_true(r > 0); -} - -static void test_lib_lib_rstrcmp_for_abc_and_aBc_is_positive(void **state) -{ - int r; - const char *str1 = "abc"; - const char *str2 = "aBc"; - - (void) state; - - r = rstrcmp(str1, str2); - assert_true(r > 0); -} - -static void test_lib_lib_rstrcmp_for_same_multinull_equals_0(void **state) -{ - int r; - const char *str1 = "Lorem\0Ipsum\0"; - const char *str2 = "Lorem\0Ipsum\0"; - - (void) state; - - r = rstrcmp(str1, str2); - assert_int_equal(r, 0); -} - -static void test_lib_lib_rstrcmp_for_diff_after_null_equals_0(void **state) -{ - int r; - const char *str1 = "Lorem\0Ipsum\0"; - const char *str2 = "Lorem\0IPzum\0"; - - (void) state; - - r = rstrcmp(str1, str2); - assert_int_equal(r, 0); -} - -static void test_lib_lib_rstrcmp_for_verylongstrings_equals_0(void **state) -{ - int r; - int i; - const int size = 2048; - char str1[size+1]; - char str2[size+1]; - - for (i = 0; i < size; ++i) { - str1[i] = 'a'; - str2[i] = 'a'; - } - str1[size] = 0; - str2[size] = 0; - - (void) state; - - r = rstrcmp(str1, str2); - assert_int_equal(r, 0); -} - -static void test_lib_lib_rstrcmp_for_verylongstrings_is_positive(void **state) -{ - int r; - int i; - const int size = 2048; - char str1[size+1]; - char str2[size+1]; - - for (i = 0; i < size-1; ++i) { - str1[i] = 'a'; - str2[i] = 'a'; - } - str1[size-1] = 'a'; - str2[size-1] = 'A'; - str1[size] = 0; - str2[size] = 0; - - (void) state; - - r = rstrcmp(str1, str2); - assert_true(r > 0); -} - -static void test_lib_lib_rstrcmp_for_verylongstrings_is_negative(void **state) -{ - int r; - int i; - const int size = 2048; - char str1[size+1]; - char str2[size+1]; - - for (i = 0; i < size-1; ++i) { - str1[i] = 'a'; - str2[i] = 'a'; - } - str1[size-1] = 'A'; - str2[size-1] = 'a'; - str1[size] = 0; - str2[size] = 0; - - (void) state; - - r = rstrcmp(str1, str2); - assert_true(r < 0); -} - -int main(void) -{ - const struct CMUnitTest tests[] = { - cmocka_unit_test( - test_lib_lib_rstrcmp_for_a_and_a_equals_0), - cmocka_unit_test( - test_lib_lib_rstrcmp_for_a_and_b_is_negative), - cmocka_unit_test( - test_lib_lib_rstrcmp_for_b_and_a_is_positive), - cmocka_unit_test( - test_lib_lib_rstrcmp_for_empty_and_null_str_equals_0), - cmocka_unit_test( - test_lib_lib_rstrcmp_for_abc_and_abcd_is_negative), - cmocka_unit_test( - test_lib_lib_rstrcmp_for_abcd_and_abc_is_positive), - cmocka_unit_test( - test_lib_lib_rstrcmp_for_abc_and_aBc_is_positive), - cmocka_unit_test( - test_lib_lib_rstrcmp_for_same_multinull_equals_0), - cmocka_unit_test( - test_lib_lib_rstrcmp_for_diff_after_null_equals_0), - cmocka_unit_test( - test_lib_lib_rstrcmp_for_verylongstrings_equals_0), - cmocka_unit_test( - test_lib_lib_rstrcmp_for_verylongstrings_is_positive), - cmocka_unit_test( - test_lib_lib_rstrcmp_for_verylongstrings_is_negative), - }; - - cmocka_set_message_output(CM_OUTPUT_TAP); - - return cmocka_run_group_tests(tests, NULL, NULL); -} diff --git a/test/cmocka/src/lib/lib/rstrlen.c b/test/cmocka/src/lib/lib/rstrlen.c deleted file mode 100644 index 71e9cf8b6248..000000000000 --- a/test/cmocka/src/lib/lib/rstrlen.c +++ /dev/null @@ -1,105 +0,0 @@ -// SPDX-License-Identifier: BSD-3-Clause -// -// Copyright(c) 2018 Intel Corporation. All rights reserved. -// -// Author: Michal Jerzy Wierzbicki - -#include - -#include -#include -#include -#include - -static void test_lib_lib_rstrlen_for_empty_str_equals_0(void **state) -{ - int r; - const char *str = ""; - - (void)state; - - r = rstrlen(str); - assert_int_equal(r, 0); -} - -static void test_lib_lib_rstrlen_for_null_str_equals_0(void **state) -{ - int r; - const char *str = "\0"; - - (void)state; - - r = rstrlen(str); - assert_int_equal(r, 0); -} - -static void test_lib_lib_rstrlen_for_a_equals_1(void **state) -{ - int r; - const char *str = "a"; - - (void)state; - - r = rstrlen(str); - assert_int_equal(r, 1); -} - -static void test_lib_lib_rstrlen_for_abcd_equals_4(void **state) -{ - int r; - const char *str = "abcd"; - - (void)state; - - r = rstrlen(str); - assert_int_equal(r, 4); -} - -static void test_lib_lib_rstrlen_for_verylongstr(void **state) -{ - int r; - int i; - const int size = 2048; - char str[size + 1]; - - for (i = 0; i < size; ++i) - str[i] = 'a'; - str[size] = 0; - - (void)state; - - r = rstrlen(str); - assert_int_equal(r, size); -} - -static void test_lib_lib_rstrlen_for_multiple_nulls_equals_5(void **state) -{ - int r; - const char *str = "Lorem\0Ipsum\0"; - - (void)state; - - r = rstrlen(str); - assert_int_equal(r, 5); -} - -int main(void) -{ - #define c_u_t(x) cmocka_unit_test(x) - // CHECK: Avoid CamelCase: - #define c_m_unit_test CMUnitTest - const struct c_m_unit_test tests[] = { - c_u_t(test_lib_lib_rstrlen_for_empty_str_equals_0), - c_u_t(test_lib_lib_rstrlen_for_null_str_equals_0), - c_u_t(test_lib_lib_rstrlen_for_a_equals_1), - c_u_t(test_lib_lib_rstrlen_for_abcd_equals_4), - c_u_t(test_lib_lib_rstrlen_for_verylongstr), - c_u_t(test_lib_lib_rstrlen_for_multiple_nulls_equals_5), - }; - #undef c_m_unit_test - #undef c_u_t - - cmocka_set_message_output(CM_OUTPUT_TAP); - - return cmocka_run_group_tests(tests, NULL, NULL); -} From b2da45a684fade40bf552ecea195eb0e922fc73e Mon Sep 17 00:00:00 2001 From: Liam Girdwood Date: Fri, 27 Nov 2026 15:53:33 +0000 Subject: [PATCH 06/11] test: audio: port mux CMocka tests to ztest Add a ztest suite for the mux and demux components, porting the three CMocka tests in test/cmocka/src/audio/mux/ as three ztest suites in one application: - mux_copy: 27 cases, one per frame format (S16_LE, S24_4LE, S32_LE) and routing mask set (9 sets). Each case creates a mux through the IPC3 comp_new() path with four sources and one sink, copies one frame and checks the output against the routing masks. - demux_copy: 27 cases, the same formats and mask sets for a demux with one source and four sinks. - mux_get_processing_function: 5 cases, the invalid format, S16_LE, S24_4LE and S32_LE cases, and the FLOAT case. The FLOAT case is guarded by CONFIG_FORMAT_FLOAT as in CMocka; it is built on native_sim, which enables CONFIG_FORMAT_FLOAT, while the CMocka build did not run it. The cases use the same input vectors, mask sets and checks as the CMocka tests and keep their names, for example test_mux_copy_s16le_mask_0. The suite links the same SOF sources as the CMocka audio_mux library: mux.c, mux_ipc3.c and mux_generic.c, the module adapter with its IPC3 part, the component buffer, source and sink API, component.c and data_blob.c, the IPC3 helper for comp_new(), ipc-helper.c, objpool.c and numbers.c. The notifier mocks are not needed. The SOF heap is the real zephyr/lib/alloc.c, routed to the host malloc() of native_sim with CONFIG_SOF_NATIVE_SIM_HOST_HEAP so that ASan and LSan cover it, and sof_get() comes from test/ztest/unit/common/sof_context.c. Adapted: - The buffer helpers of test/cmocka/src/util.h and the component IPC setup, which each CMocka file had its own copy of, are shared in mux_test_util.c. - Teardown is done in the ztest after hooks, so that the component and its buffers are also freed when a case fails. - The dummy pipeline that bypasses comp_check_eos() is zero-initialised with calloc() instead of left uninitialised. - mux_test_util.c defines an idc_send_msg() that fails the test if it is called. comp_prepare() is an inline function with a branch that sends an IDC message for shared components on another core. The test components are never shared, but builds without optimization, such as the coverage build, still reference idc_send_msg(). Co-Authored-By: Claude Opus 5.5 Signed-off-by: Liam Girdwood --- test/ztest/unit/audio/mux/CMakeLists.txt | 68 ++++ test/ztest/unit/audio/mux/demux_copy_ztest.c | 298 +++++++++++++++++ test/ztest/unit/audio/mux/mux_copy_ztest.c | 314 ++++++++++++++++++ .../mux/mux_get_processing_function_ztest.c | 137 ++++++++ test/ztest/unit/audio/mux/mux_test_util.c | 167 ++++++++++ test/ztest/unit/audio/mux/mux_test_util.h | 74 +++++ test/ztest/unit/audio/mux/prj.conf | 17 + test/ztest/unit/audio/mux/testcase.yaml | 25 ++ 8 files changed, 1100 insertions(+) create mode 100644 test/ztest/unit/audio/mux/CMakeLists.txt create mode 100644 test/ztest/unit/audio/mux/demux_copy_ztest.c create mode 100644 test/ztest/unit/audio/mux/mux_copy_ztest.c create mode 100644 test/ztest/unit/audio/mux/mux_get_processing_function_ztest.c create mode 100644 test/ztest/unit/audio/mux/mux_test_util.c create mode 100644 test/ztest/unit/audio/mux/mux_test_util.h create mode 100644 test/ztest/unit/audio/mux/prj.conf create mode 100644 test/ztest/unit/audio/mux/testcase.yaml diff --git a/test/ztest/unit/audio/mux/CMakeLists.txt b/test/ztest/unit/audio/mux/CMakeLists.txt new file mode 100644 index 000000000000..3ad1ef96aad1 --- /dev/null +++ b/test/ztest/unit/audio/mux/CMakeLists.txt @@ -0,0 +1,68 @@ +# SPDX-License-Identifier: BSD-3-Clause +# +# Copyright(c) 2026 Intel Corporation. +# +# MUX and DEMUX 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_mux) + +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 + ${SOF_ROOT}/src/audio + ${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 + mux_test_util.c + mux_copy_ztest.c + demux_copy_ztest.c + mux_get_processing_function_ztest.c + ${SOF_ROOT}/test/ztest/unit/common/sof_context.c + ${SOF_ROOT}/zephyr/lib/alloc.c + ${SOF_ROOT}/src/audio/mux/mux.c + ${SOF_ROOT}/src/audio/mux/mux_ipc3.c + ${SOF_ROOT}/src/audio/mux/mux_generic.c + ${SOF_ROOT}/src/audio/module_adapter/module_adapter.c + ${SOF_ROOT}/src/audio/module_adapter/module_adapter_ipc3.c + ${SOF_ROOT}/src/audio/module_adapter/module/generic.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/audio/component.c + ${SOF_ROOT}/src/audio/data_blob.c + ${SOF_ROOT}/src/module/audio/source_api.c + ${SOF_ROOT}/src/module/audio/sink_api.c + ${SOF_ROOT}/src/kernel/ipc/ipc3/helper.c + ${SOF_ROOT}/src/ipc/ipc-helper.c + ${SOF_ROOT}/src/lib/objpool.c + ${SOF_ROOT}/src/math/numbers.c +) + +sof_append_relative_path_definitions(app) diff --git a/test/ztest/unit/audio/mux/demux_copy_ztest.c b/test/ztest/unit/audio/mux/demux_copy_ztest.c new file mode 100644 index 000000000000..4837c98e064d --- /dev/null +++ b/test/ztest/unit/audio/mux/demux_copy_ztest.c @@ -0,0 +1,298 @@ +// SPDX-License-Identifier: BSD-3-Clause +// +// Copyright(c) 2026 Intel Corporation. +// +// Converted from CMock to Ztest +// Original tests from test/cmocka/src/audio/mux/demux_copy.c: +// Author: Daniel Bogdzia +// Janusz Jankowski + +#include + +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#include "mux_test_util.h" + +struct demux_copy_test_data { + struct comp_dev *dev; + struct comp_buffer *sinks[MUX_MAX_STREAMS]; + struct comp_buffer *source; + void *outputs[MUX_MAX_STREAMS]; + uint32_t format; + const uint8_t (*mask)[CONFIG_PLATFORM_MAX_CHANNELS]; +}; + +static struct demux_copy_test_data demux_copy_td; + +static const int16_t input_16b[CONFIG_PLATFORM_MAX_CHANNELS] = { + 0x101, 0x102, 0x104, 0x108, + 0x111, 0x112, 0x114, 0x118, +}; + +static const int32_t input_24b[CONFIG_PLATFORM_MAX_CHANNELS] = { + 0x1a1001, 0x2a2002, 0x4a4004, 0x8a8008, + 0x1b1011, 0x2b2012, 0x4b4014, 0x8b8018, +}; + +static const int32_t input_32b[CONFIG_PLATFORM_MAX_CHANNELS] = { + 0xd1a1001, 0xd2a2002, 0xd4a4004, 0xd8a8008, + 0xe1b1011, 0xe2b2012, 0xe4b4014, 0xe8b8018, +}; + +static const uint8_t masks[][MUX_MAX_STREAMS][CONFIG_PLATFORM_MAX_CHANNELS] = { + { { 0x01, }, }, + { { 0x01, 0x02, 0x04, 0x10, 0x20, 0x40, 0x80}, }, + { { }, + { 0x01, 0x02, 0x04, 0x10, 0x20, 0x40, 0x80}, }, + { { }, + { }, + { 0x01, 0x02, 0x04, 0x10, 0x20, 0x40, 0x80}, }, + { { }, + { }, + { }, + { 0x01, 0x02, 0x04, 0x10, 0x20, 0x40, 0x80}, }, + { { 0x01, }, + { 0x00, 0x01, }, + { 0x00, 0x00, 0x01, }, + { 0x00, 0x00, 0x00, 0x01, 0x02, 0x04, 0x08, 0x10}, }, + { { 0x00, 0x00, 0x00, 0x01, 0x02, 0x04, 0x08, 0x10}, + { 0x00, 0x00, 0x01, }, + { 0x00, 0x01, }, + { 0x01, }, }, + { { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, }, + { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, }, + { 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, }, + { 0x10, 0x08, 0x04, 0x02, 0x01, },}, + { { 0x01, }, + { 0x00, 0x01, }, + { 0x00, 0x00, 0x01, }, + { 0x00, 0x00, 0x00, 0x01, },}, +}; + +/** + * @brief Create one sink buffer per stream, on pipeline i. + */ +static void prepare_sinks(struct demux_copy_test_data *td, size_t sample_size) +{ + uint16_t buffer_size = sample_size * CONFIG_PLATFORM_MAX_CHANNELS; + int i; + + for (i = 0; i < MUX_MAX_STREAMS; ++i) { + td->sinks[i] = mux_test_create_sink(td->dev, i, td->format, + CONFIG_PLATFORM_MAX_CHANNELS, buffer_size); + + td->outputs[i] = td->sinks[i]->stream.addr; + zassert_equal(audio_stream_get_free_bytes(&td->sinks[i]->stream), buffer_size); + } +} + +/** + * @brief Create the demux source buffer on pipeline MUX_MAX_STREAMS + 1, + * holding one frame of input. + */ +static void prepare_source(struct demux_copy_test_data *td, size_t sample_size) +{ + uint16_t buffer_size = sample_size * CONFIG_PLATFORM_MAX_CHANNELS; + + td->source = mux_test_create_source(td->dev, MUX_MAX_STREAMS + 1, td->format, + CONFIG_PLATFORM_MAX_CHANNELS, buffer_size); + + if (td->format == SOF_IPC_FRAME_S16_LE) + memcpy_s(td->source->stream.addr, buffer_size, input_16b, buffer_size); + else if (td->format == SOF_IPC_FRAME_S24_4LE) + memcpy_s(td->source->stream.addr, buffer_size, input_24b, buffer_size); + else + memcpy_s(td->source->stream.addr, buffer_size, input_32b, buffer_size); + + audio_stream_produce(&td->source->stream, buffer_size); + zassert_equal(audio_stream_get_avail_bytes(&td->source->stream), buffer_size); +} + +/** + * @brief Create and prepare a demux with the given format and routing masks. + */ +static void setup_test_case(struct demux_copy_test_data *td, uint32_t format, int mask_index) +{ + const struct sof_uuid uuid = SOF_REG_UUID(demux); + size_t sample_size = format == SOF_IPC_FRAME_S16_LE ? sizeof(int16_t) : sizeof(int32_t); + struct comp_dev *dev; + + td->format = format; + td->mask = masks[mask_index]; + + dev = mux_test_comp_new(&uuid, td->mask); + zassert_not_null(dev, "comp_new() failed"); + td->dev = dev; + + /* Add dummy pipeline to bypass comp_check_eos() */ + dev->pipeline = calloc(1, sizeof(*dev->pipeline)); + zassert_not_null(dev->pipeline); + dev->expect_eos = false; + + prepare_sinks(td, sample_size); + prepare_source(td, sample_size); + + zassert_ok(comp_prepare(td->dev)); +} + +/** + * @brief Free the demux and its buffers, also after a failed test case. + */ +static void demux_copy_after(void *fixture) +{ + struct demux_copy_test_data *td = &demux_copy_td; + int i; + + (void)fixture; + + mux_test_free_source(td->source); + + for (i = 0; i < MUX_MAX_STREAMS; ++i) + mux_test_free_sink(td->sinks[i]); + + if (td->dev) { + free(td->dev->pipeline); + td->dev->pipeline = NULL; + comp_free(td->dev); + } + + memset(td, 0, sizeof(*td)); +} + +/** + * @brief Copy one frame from the source and check every demux output. + * + * Channel j of output stream i takes the input channel k of the last bit k + * set in mask[i][j], or 0 when the mask is 0. + */ +static void test_demux_copy_proc_16(struct demux_copy_test_data *td) +{ + int16_t expected_results[MUX_MAX_STREAMS][CONFIG_PLATFORM_MAX_CHANNELS]; + int i, j, k; + + zassert_equal(comp_copy(td->dev), 0); + + for (i = 0; i < MUX_MAX_STREAMS; ++i) { + for (j = 0; j < CONFIG_PLATFORM_MAX_CHANNELS; ++j) { + int64_t sample = 0; + + for (k = 0; k < CONFIG_PLATFORM_MAX_CHANNELS; ++k) + if (td->mask[i][j] & BIT(k)) + sample = input_16b[k]; + + expected_results[i][j] = sample; + } + } + + for (i = 0; i < MUX_MAX_STREAMS; ++i) + zassert_mem_equal(td->outputs[i], expected_results[i], + sizeof(expected_results[0]), "stream %d", i); +} + +/** @brief S24_4LE variant of test_demux_copy_proc_16(). */ +static void test_demux_copy_proc_24(struct demux_copy_test_data *td) +{ + int32_t expected_results[MUX_MAX_STREAMS][CONFIG_PLATFORM_MAX_CHANNELS]; + int i, j, k; + + zassert_equal(comp_copy(td->dev), 0); + + for (i = 0; i < MUX_MAX_STREAMS; ++i) { + for (j = 0; j < CONFIG_PLATFORM_MAX_CHANNELS; ++j) { + int32_t sample = 0; + + for (k = 0; k < CONFIG_PLATFORM_MAX_CHANNELS; ++k) + if (td->mask[i][j] & BIT(k)) + sample = input_24b[k]; + + expected_results[i][j] = sample; + } + } + + for (i = 0; i < MUX_MAX_STREAMS; ++i) + zassert_mem_equal(td->outputs[i], expected_results[i], + sizeof(expected_results[0]), "stream %d", i); +} + +/** @brief S32_LE variant of test_demux_copy_proc_16(). */ +static void test_demux_copy_proc_32(struct demux_copy_test_data *td) +{ + int32_t expected_results[MUX_MAX_STREAMS][CONFIG_PLATFORM_MAX_CHANNELS]; + int i, j, k; + + zassert_equal(comp_copy(td->dev), 0); + + for (i = 0; i < MUX_MAX_STREAMS; ++i) { + for (j = 0; j < CONFIG_PLATFORM_MAX_CHANNELS; ++j) { + int32_t sample = 0; + + for (k = 0; k < CONFIG_PLATFORM_MAX_CHANNELS; ++k) + if (td->mask[i][j] & BIT(k)) + sample = input_32b[k]; + + expected_results[i][j] = sample; + } + } + + for (i = 0; i < MUX_MAX_STREAMS; ++i) + zassert_mem_equal(td->outputs[i], expected_results[i], + sizeof(expected_results[0]), "stream %d", i); +} + +/* + * One test case per format and mask, named like the CMocka cases + * test_demux_copy__mask_. + */ +#define DEMUX_COPY_TEST(fmt_name, fmt, proc, mask_index) \ + ZTEST(demux_copy, test_demux_copy_##fmt_name##_mask_##mask_index) \ + { \ + BUILD_ASSERT(mask_index < ARRAY_SIZE(masks)); \ + setup_test_case(&demux_copy_td, fmt, mask_index); \ + proc(&demux_copy_td); \ + } + +#define DEMUX_COPY_TESTS(fmt_name, fmt, proc) \ + DEMUX_COPY_TEST(fmt_name, fmt, proc, 0) \ + DEMUX_COPY_TEST(fmt_name, fmt, proc, 1) \ + DEMUX_COPY_TEST(fmt_name, fmt, proc, 2) \ + DEMUX_COPY_TEST(fmt_name, fmt, proc, 3) \ + DEMUX_COPY_TEST(fmt_name, fmt, proc, 4) \ + DEMUX_COPY_TEST(fmt_name, fmt, proc, 5) \ + DEMUX_COPY_TEST(fmt_name, fmt, proc, 6) \ + DEMUX_COPY_TEST(fmt_name, fmt, proc, 7) \ + DEMUX_COPY_TEST(fmt_name, fmt, proc, 8) + +BUILD_ASSERT(ARRAY_SIZE(masks) == 9, "DEMUX_COPY_TESTS() must cover every mask"); + +#if CONFIG_FORMAT_S16LE +DEMUX_COPY_TESTS(s16le, SOF_IPC_FRAME_S16_LE, test_demux_copy_proc_16) +#endif /* CONFIG_FORMAT_S16LE */ +#if CONFIG_FORMAT_S24LE +DEMUX_COPY_TESTS(s24_4le, SOF_IPC_FRAME_S24_4LE, test_demux_copy_proc_24) +#endif /* CONFIG_FORMAT_S24LE */ +#if CONFIG_FORMAT_S32LE +DEMUX_COPY_TESTS(s32le, SOF_IPC_FRAME_S32_LE, test_demux_copy_proc_32) +#endif /* CONFIG_FORMAT_S32LE */ + +/** + * @brief Register the demux driver for the suite. + */ +static void *demux_copy_setup(void) +{ + sys_comp_init(sof_get()); + sys_comp_module_demux_interface_init(); + + return NULL; +} + +ZTEST_SUITE(demux_copy, NULL, demux_copy_setup, NULL, demux_copy_after, NULL); diff --git a/test/ztest/unit/audio/mux/mux_copy_ztest.c b/test/ztest/unit/audio/mux/mux_copy_ztest.c new file mode 100644 index 000000000000..df17c0367928 --- /dev/null +++ b/test/ztest/unit/audio/mux/mux_copy_ztest.c @@ -0,0 +1,314 @@ +// SPDX-License-Identifier: BSD-3-Clause +// +// Copyright(c) 2026 Intel Corporation. +// +// Converted from CMock to Ztest +// Original tests from test/cmocka/src/audio/mux/mux_copy.c: +// Author: Daniel Bogdzia +// Janusz Jankowski + +#include + +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#include "mux_test_util.h" + +struct mux_copy_test_data { + struct comp_dev *dev; + struct comp_buffer *sources[MUX_MAX_STREAMS]; + struct comp_buffer *sink; + void *output; + uint32_t format; + const uint8_t (*mask)[CONFIG_PLATFORM_MAX_CHANNELS]; +}; + +static struct mux_copy_test_data mux_copy_td; + +static const int16_t input_16b[MUX_MAX_STREAMS][CONFIG_PLATFORM_MAX_CHANNELS] = { + { 0x101, 0x102, 0x104, 0x108, + 0x111, 0x112, 0x114, 0x118, }, + { 0x201, 0x202, 0x204, 0x208, + 0x211, 0x212, 0x214, 0x218, }, + { 0x301, 0x302, 0x304, 0x308, + 0x311, 0x312, 0x314, 0x318, }, + { 0x401, 0x402, 0x404, 0x408, + 0x411, 0x412, 0x414, 0x418, }, +}; + +static const int32_t input_24b[MUX_MAX_STREAMS][CONFIG_PLATFORM_MAX_CHANNELS] = { + { 0x1a1001, 0x2a2002, 0x4a4004, 0x8a8008, + 0x1b1011, 0x2b2012, 0x4b4014, 0x8b8018, }, + { 0x1a1101, 0x2a2102, 0x4a4104, 0x8a8108, + 0x1b1111, 0x2b2112, 0x4b4114, 0x8b8118, }, + { 0x1a1201, 0x2a2202, 0x4a4204, 0x8a8208, + 0x1b1211, 0x2b2212, 0x4b4214, 0x8b8218, }, + { 0x1a1401, 0x2a2402, 0x4a4404, 0x8a8408, + 0x1b1411, 0x2b2412, 0x4b4414, 0x8b8418, }, +}; + +static const int32_t input_32b[MUX_MAX_STREAMS][CONFIG_PLATFORM_MAX_CHANNELS] = { + { 0xd1a1001, 0xd2a2002, 0xd4a4004, 0xd8a8008, + 0xe1b1011, 0xe2b2012, 0xe4b4014, 0xe8b8018, }, + { 0xd1a1101, 0xd2a2102, 0xd4a4104, 0xd8a8108, + 0xe1b1111, 0xe2b2112, 0xe4b4114, 0xe8b8118, }, + { 0xd1a1201, 0xd2a2202, 0xd4a4204, 0xd8a8208, + 0xe1b1211, 0xe2b2212, 0xe4b4214, 0xe8b8218, }, + { 0xd1a1401, 0xd2a2402, 0xd4a4404, 0xd8a8408, + 0xe1b1411, 0xe2b2412, 0xe4b4414, 0xe8b8418, }, +}; + +static const uint8_t masks[][MUX_MAX_STREAMS][CONFIG_PLATFORM_MAX_CHANNELS] = { + { { 0x01, }, }, + { { 0x01, 0x02, 0x04, 0x10, 0x20, 0x40, 0x80 }, }, + { { }, + { 0x01, 0x02, 0x04, 0x10, 0x20, 0x40, 0x80 }, }, + { { }, + { }, + { 0x01, 0x02, 0x04, 0x10, 0x20, 0x40, 0x80 }, }, + { { }, + { }, + { }, + { 0x01, 0x02, 0x04, 0x10, 0x20, 0x40, 0x80 }, }, + { { 0x01, }, + { 0x00, 0x01, }, + { 0x00, 0x00, 0x01, }, + { 0x00, 0x00, 0x00, 0x01, 0x02, 0x04, 0x08, 0x10 }, }, + { { 0x00, 0x00, 0x00, 0x01, 0x02, 0x04, 0x08, 0x10 }, + { 0x00, 0x00, 0x01, }, + { 0x00, 0x01, }, + { 0x01, }, }, + { { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, }, + { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, }, + { 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, }, + { 0x10, 0x08, 0x04, 0x02, 0x01, }, }, + { { 0x01, }, + { 0x00, 0x01, }, + { 0x00, 0x00, 0x01, }, + { 0x00, 0x00, 0x00, 0x01, }, }, +}; + +/** + * @brief Create the mux sink buffer on pipeline MUX_MAX_STREAMS + 1. + */ +static void prepare_sink(struct mux_copy_test_data *td, size_t sample_size) +{ + uint16_t buffer_size = sample_size * CONFIG_PLATFORM_MAX_CHANNELS; + + td->sink = mux_test_create_sink(td->dev, MUX_MAX_STREAMS + 1, td->format, + CONFIG_PLATFORM_MAX_CHANNELS, buffer_size); + td->output = td->sink->stream.addr; + + zassert_equal(audio_stream_get_free_bytes(&td->sink->stream), buffer_size); +} + +/** + * @brief Create one source buffer per stream, on pipeline i, holding one frame + * of the input of stream i. + */ +static void prepare_sources(struct mux_copy_test_data *td, size_t sample_size) +{ + uint16_t buffer_size = sample_size * CONFIG_PLATFORM_MAX_CHANNELS; + int i; + + for (i = 0; i < MUX_MAX_STREAMS; ++i) { + td->sources[i] = mux_test_create_source(td->dev, i, td->format, + CONFIG_PLATFORM_MAX_CHANNELS, buffer_size); + + if (td->format == SOF_IPC_FRAME_S16_LE) + memcpy_s(td->sources[i]->stream.addr, buffer_size, &input_16b[i], + buffer_size); + else if (td->format == SOF_IPC_FRAME_S24_4LE) + memcpy_s(td->sources[i]->stream.addr, buffer_size, &input_24b[i], + buffer_size); + else + memcpy_s(td->sources[i]->stream.addr, buffer_size, &input_32b[i], + buffer_size); + + audio_stream_produce(&td->sources[i]->stream, buffer_size); + zassert_equal(audio_stream_get_avail_bytes(&td->sources[i]->stream), + buffer_size); + } +} + +/** + * @brief Create and prepare a mux with the given format and routing masks. + */ +static void setup_test_case(struct mux_copy_test_data *td, uint32_t format, int mask_index) +{ + const struct sof_uuid uuid = SOF_REG_UUID(mux); + size_t sample_size = format == SOF_IPC_FRAME_S16_LE ? sizeof(int16_t) : sizeof(int32_t); + struct comp_dev *dev; + + td->format = format; + td->mask = masks[mask_index]; + + dev = mux_test_comp_new(&uuid, td->mask); + zassert_not_null(dev, "comp_new() failed"); + td->dev = dev; + + /* Add dummy pipeline to bypass comp_check_eos() */ + dev->pipeline = calloc(1, sizeof(*dev->pipeline)); + zassert_not_null(dev->pipeline); + dev->expect_eos = false; + + prepare_sink(td, sample_size); + prepare_sources(td, sample_size); + + zassert_ok(comp_prepare(td->dev)); +} + +/** + * @brief Free the mux and its buffers, also after a failed test case. + */ +static void mux_copy_after(void *fixture) +{ + struct mux_copy_test_data *td = &mux_copy_td; + int i; + + (void)fixture; + + for (i = 0; i < MUX_MAX_STREAMS; ++i) + mux_test_free_source(td->sources[i]); + + mux_test_free_sink(td->sink); + + if (td->dev) { + free(td->dev->pipeline); + td->dev->pipeline = NULL; + comp_free(td->dev); + } + + memset(td, 0, sizeof(*td)); +} + +/** + * @brief Copy one frame from every source and check the mux output. + * + * Output channel i takes the sample of the last stream and input channel + * whose mask has bit i set, or 0 when no mask routes to it. + */ +static void test_mux_copy_proc_16(struct mux_copy_test_data *td) +{ + int16_t expected_result[CONFIG_PLATFORM_MAX_CHANNELS]; + int i, j, k; + + zassert_equal(comp_copy(td->dev), 0); + + for (i = 0; i < CONFIG_PLATFORM_MAX_CHANNELS; ++i) { + int32_t sample = 0; + + for (j = 0; j < MUX_MAX_STREAMS; ++j) { + for (k = 0; k < CONFIG_PLATFORM_MAX_CHANNELS; ++k) { + if (td->mask[j][k] & BIT(i)) + sample = input_16b[j][k]; + } + } + expected_result[i] = sample; + } + + zassert_mem_equal(td->output, expected_result, sizeof(expected_result)); +} + +/** @brief S24_4LE variant of test_mux_copy_proc_16(). */ +static void test_mux_copy_proc_24(struct mux_copy_test_data *td) +{ + int32_t expected_result[CONFIG_PLATFORM_MAX_CHANNELS]; + int i, j, k; + + zassert_equal(comp_copy(td->dev), 0); + + for (i = 0; i < CONFIG_PLATFORM_MAX_CHANNELS; ++i) { + int32_t sample = 0; + + for (j = 0; j < MUX_MAX_STREAMS; ++j) { + for (k = 0; k < CONFIG_PLATFORM_MAX_CHANNELS; ++k) { + if (td->mask[j][k] & BIT(i)) + sample = input_24b[j][k]; + } + } + expected_result[i] = sample; + } + + zassert_mem_equal(td->output, expected_result, sizeof(expected_result)); +} + +/** @brief S32_LE variant of test_mux_copy_proc_16(). */ +static void test_mux_copy_proc_32(struct mux_copy_test_data *td) +{ + int32_t expected_result[CONFIG_PLATFORM_MAX_CHANNELS]; + int i, j, k; + + zassert_equal(comp_copy(td->dev), 0); + + for (i = 0; i < CONFIG_PLATFORM_MAX_CHANNELS; ++i) { + int32_t sample = 0; + + for (j = 0; j < MUX_MAX_STREAMS; ++j) { + for (k = 0; k < CONFIG_PLATFORM_MAX_CHANNELS; ++k) { + if (td->mask[j][k] & BIT(i)) + sample = input_32b[j][k]; + } + } + expected_result[i] = sample; + } + + zassert_mem_equal(td->output, expected_result, sizeof(expected_result)); +} + +/* + * One test case per format and mask, named like the CMocka cases + * test_mux_copy__mask_. + */ +#define MUX_COPY_TEST(fmt_name, fmt, proc, mask_index) \ + ZTEST(mux_copy, test_mux_copy_##fmt_name##_mask_##mask_index) \ + { \ + BUILD_ASSERT(mask_index < ARRAY_SIZE(masks)); \ + setup_test_case(&mux_copy_td, fmt, mask_index); \ + proc(&mux_copy_td); \ + } + +#define MUX_COPY_TESTS(fmt_name, fmt, proc) \ + MUX_COPY_TEST(fmt_name, fmt, proc, 0) \ + MUX_COPY_TEST(fmt_name, fmt, proc, 1) \ + MUX_COPY_TEST(fmt_name, fmt, proc, 2) \ + MUX_COPY_TEST(fmt_name, fmt, proc, 3) \ + MUX_COPY_TEST(fmt_name, fmt, proc, 4) \ + MUX_COPY_TEST(fmt_name, fmt, proc, 5) \ + MUX_COPY_TEST(fmt_name, fmt, proc, 6) \ + MUX_COPY_TEST(fmt_name, fmt, proc, 7) \ + MUX_COPY_TEST(fmt_name, fmt, proc, 8) + +BUILD_ASSERT(ARRAY_SIZE(masks) == 9, "MUX_COPY_TESTS() must cover every mask"); + +#if CONFIG_FORMAT_S16LE +MUX_COPY_TESTS(s16le, SOF_IPC_FRAME_S16_LE, test_mux_copy_proc_16) +#endif /* CONFIG_FORMAT_S16LE */ +#if CONFIG_FORMAT_S24LE +MUX_COPY_TESTS(s24_4le, SOF_IPC_FRAME_S24_4LE, test_mux_copy_proc_24) +#endif /* CONFIG_FORMAT_S24LE */ +#if CONFIG_FORMAT_S32LE +MUX_COPY_TESTS(s32le, SOF_IPC_FRAME_S32_LE, test_mux_copy_proc_32) +#endif /* CONFIG_FORMAT_S32LE */ + +/** + * @brief Register the mux driver for the suite. + */ +static void *mux_copy_setup(void) +{ + sys_comp_init(sof_get()); + sys_comp_module_mux_interface_init(); + + return NULL; +} + +ZTEST_SUITE(mux_copy, NULL, mux_copy_setup, NULL, mux_copy_after, NULL); diff --git a/test/ztest/unit/audio/mux/mux_get_processing_function_ztest.c b/test/ztest/unit/audio/mux/mux_get_processing_function_ztest.c new file mode 100644 index 000000000000..153405811595 --- /dev/null +++ b/test/ztest/unit/audio/mux/mux_get_processing_function_ztest.c @@ -0,0 +1,137 @@ +// SPDX-License-Identifier: BSD-3-Clause +// +// Copyright(c) 2026 Intel Corporation. +// +// Converted from CMock to Ztest +// Original tests from test/cmocka/src/audio/mux/mux_get_processing_function.c: +// Author: Daniel Bogdzia +// Janusz Jankowski + +#include + +#include +#include +#include +#include +#include +#include + +#include +#include + +#include "mux_test_util.h" + +struct mux_get_processing_function_fixture { + struct comp_dev *dev; + struct processing_module *mod; + struct comp_buffer *sink; +}; + +static struct mux_get_processing_function_fixture mux_gpf_fixture; + +/** + * @brief Register the mux driver for the suite. + */ +static void *mux_get_processing_function_setup(void) +{ + sys_comp_init(sof_get()); + sys_comp_module_mux_interface_init(); + + return &mux_gpf_fixture; +} + +/** + * @brief Create a mux with all zero routing masks and one 4 byte sink buffer. + */ +static void mux_get_processing_function_before(void *data) +{ + struct mux_get_processing_function_fixture *td = data; + const struct sof_uuid uuid = SOF_REG_UUID(mux); + + td->dev = mux_test_comp_new(&uuid, NULL); + zassert_not_null(td->dev, "comp_new() failed"); + + td->mod = comp_mod(td->dev); + td->sink = mux_test_create_sink(td->dev, 0, 0, 0, 4); +} + +/** + * @brief Free the mux and its sink buffer. + */ +static void mux_get_processing_function_after(void *data) +{ + struct mux_get_processing_function_fixture *td = data; + + mux_test_free_sink(td->sink); + if (td->dev) + comp_free(td->dev); + + memset(td, 0, sizeof(*td)); +} + +#if CONFIG_FORMAT_FLOAT +ZTEST_F(mux_get_processing_function, test_mux_get_processing_function_valid_float) +{ + mux_func func = NULL; + + audio_stream_set_frm_fmt(&fixture->sink->stream, SOF_IPC_FRAME_FLOAT); + + func = mux_get_processing_function(fixture->mod); + + zassert_not_null(func); +} +#endif /* CONFIG_FORMAT_FLOAT */ + +ZTEST_F(mux_get_processing_function, test_mux_get_processing_function_invalid_format) +{ + mux_func func = NULL; + + /* set frame format value to unsupported value */ + audio_stream_set_frm_fmt(&fixture->sink->stream, SOF_IPC_FRAME_INVALID); + + func = mux_get_processing_function(fixture->mod); + + zassert_is_null(func); +} + +#if CONFIG_FORMAT_S16LE +ZTEST_F(mux_get_processing_function, test_mux_get_processing_function_valid_s16le) +{ + mux_func func = NULL; + + audio_stream_set_frm_fmt(&fixture->sink->stream, SOF_IPC_FRAME_S16_LE); + + func = mux_get_processing_function(fixture->mod); + + zassert_not_null(func); +} +#endif /* CONFIG_FORMAT_S16LE */ + +#if CONFIG_FORMAT_S24LE +ZTEST_F(mux_get_processing_function, test_mux_get_processing_function_valid_s24_4le) +{ + mux_func func = NULL; + + audio_stream_set_frm_fmt(&fixture->sink->stream, SOF_IPC_FRAME_S24_4LE); + + func = mux_get_processing_function(fixture->mod); + + zassert_not_null(func); +} +#endif /* CONFIG_FORMAT_S24LE */ + +#if CONFIG_FORMAT_S32LE +ZTEST_F(mux_get_processing_function, test_mux_get_processing_function_valid_s32le) +{ + mux_func func = NULL; + + audio_stream_set_frm_fmt(&fixture->sink->stream, SOF_IPC_FRAME_S32_LE); + + func = mux_get_processing_function(fixture->mod); + + zassert_not_null(func); +} +#endif /* CONFIG_FORMAT_S32LE */ + +ZTEST_SUITE(mux_get_processing_function, NULL, mux_get_processing_function_setup, + mux_get_processing_function_before, mux_get_processing_function_after, NULL); diff --git a/test/ztest/unit/audio/mux/mux_test_util.c b/test/ztest/unit/audio/mux/mux_test_util.c new file mode 100644 index 000000000000..e07141faa746 --- /dev/null +++ b/test/ztest/unit/audio/mux/mux_test_util.c @@ -0,0 +1,167 @@ +// SPDX-License-Identifier: BSD-3-Clause +// +// Copyright(c) 2026 Intel Corporation. +// +// Converted from CMock to Ztest +// Original helpers from test/cmocka/src/util.h and from the +// create_mux_comp_ipc() and create_demux_comp_ipc() functions of the tests in +// test/cmocka/src/audio/mux/ +// Author: Daniel Bogdzia +// Janusz Jankowski + +#include + +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#include "mux_test_util.h" + +/** + * @brief Allocate a component buffer. + * @param pipeline_id Pipeline ID of the buffer. + * @param frame_fmt Frame format of the buffer. + * @param channels Number of channels of the buffer. + * @param buffer_size Buffer size in bytes. + * @return The new buffer with zeroed data, never NULL. + */ +static struct comp_buffer *mux_test_create_buffer(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); + + zassert_not_null(buffer, "Failed to allocate a %u byte buffer", buffer_size); + memset(buffer->stream.addr, 0, buffer_size); + audio_stream_set_frm_fmt(&buffer->stream, frame_fmt); + audio_stream_set_channels(&buffer->stream, channels); + + return buffer; +} + +/** + * @brief Allocate the dummy component on the far end of a test buffer. + * @return A zeroed component in the PREPARE state, never NULL. + */ +static struct comp_dev *mux_test_create_far_end(void) +{ + struct comp_dev *dev = calloc(1, sizeof(*dev)); + + zassert_not_null(dev); + dev->state = COMP_STATE_PREPARE; + + return dev; +} + +struct comp_buffer *mux_test_create_sink(struct comp_dev *dev, uint32_t pipeline_id, + uint32_t frame_fmt, uint16_t channels, + uint16_t buffer_size) +{ + struct comp_buffer *buffer; + + buffer = mux_test_create_buffer(pipeline_id, frame_fmt, channels, buffer_size); + buffer->sink = mux_test_create_far_end(); + if (dev) + list_item_append(&buffer->source_list, &dev->bsink_list); + + return buffer; +} + +void mux_test_free_sink(struct comp_buffer *buffer) +{ + if (!buffer) + return; + + free(comp_buffer_get_sink_component(buffer)); + buffer_free(buffer); +} + +struct comp_buffer *mux_test_create_source(struct comp_dev *dev, uint32_t pipeline_id, + uint32_t frame_fmt, uint16_t channels, + uint16_t buffer_size) +{ + struct comp_buffer *buffer; + + buffer = mux_test_create_buffer(pipeline_id, frame_fmt, channels, buffer_size); + buffer->source = mux_test_create_far_end(); + if (dev) + list_item_append(&buffer->sink_list, &dev->bsource_list); + + return buffer; +} + +void mux_test_free_source(struct comp_buffer *buffer) +{ + if (!buffer) + return; + + free(comp_buffer_get_source_component(buffer)); + buffer_free(buffer); +} + +struct comp_dev *mux_test_comp_new(const struct sof_uuid *uuid, + const uint8_t mask[MUX_MAX_STREAMS] + [CONFIG_PLATFORM_MAX_CHANNELS]) +{ + struct sof_ipc_comp_process *ipc; + struct sof_mux_config *mux; + struct comp_dev *dev; + const size_t ipc_size = sizeof(struct sof_ipc_comp_process); + const size_t mux_size = sizeof(struct sof_mux_config) + + MUX_MAX_STREAMS * sizeof(struct mux_stream_data); + int i, j; + + ipc = calloc(1, ipc_size + mux_size + SOF_UUID_SIZE); + zassert_not_null(ipc); + + memcpy_s(ipc + 1, SOF_UUID_SIZE, uuid, SOF_UUID_SIZE); + mux = (struct sof_mux_config *)((char *)(ipc + 1) + SOF_UUID_SIZE); + ipc->comp.hdr.size = ipc_size + SOF_UUID_SIZE; + ipc->comp.type = SOF_COMP_MODULE_ADAPTER; + ipc->config.hdr.size = sizeof(struct sof_ipc_comp_config); + ipc->size = mux_size; + ipc->comp.ext_data_length = SOF_UUID_SIZE; + + mux->num_streams = MUX_MAX_STREAMS; + for (i = 0; i < MUX_MAX_STREAMS; ++i) { + mux->streams[i].pipeline_id = i; + for (j = 0; j < CONFIG_PLATFORM_MAX_CHANNELS; ++j) + mux->streams[i].mask[j] = mask ? mask[i][j] : 0; + } + + dev = comp_new((struct sof_ipc_comp *)ipc); + free(ipc); + + return dev; +} + +/** + * @brief IDC stub for the remote branch of comp_prepare(). + * + * comp_prepare() is an inline function that forwards the call to another core + * for shared components. The test components are never shared, so the branch + * is not taken, but builds without optimization (for example with --coverage) + * still reference idc_send_msg(). + */ +int idc_send_msg(struct idc_msg *msg, uint32_t mode) +{ + (void)msg; + (void)mode; + + zassert_unreachable("Unexpected IDC message"); + + return -ENOTSUP; +} diff --git a/test/ztest/unit/audio/mux/mux_test_util.h b/test/ztest/unit/audio/mux/mux_test_util.h new file mode 100644 index 000000000000..85319bf6bea9 --- /dev/null +++ b/test/ztest/unit/audio/mux/mux_test_util.h @@ -0,0 +1,74 @@ +/* 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. + */ + +#ifndef __TEST_ZTEST_UNIT_AUDIO_MUX_TEST_UTIL_H__ +#define __TEST_ZTEST_UNIT_AUDIO_MUX_TEST_UTIL_H__ + +#include +#include +#include + +#include + +#include + +/** + * @brief Create a buffer that the component under test writes to. + * @param dev Component the buffer is connected to as a sink, or NULL. + * @param pipeline_id Pipeline ID of the buffer, used by mux and demux to + * find the stream configuration of the connection. + * @param frame_fmt Frame format of the buffer. + * @param channels Number of channels of the buffer. + * @param buffer_size Buffer size in bytes. + * @return The new buffer, never NULL. + */ +struct comp_buffer *mux_test_create_sink(struct comp_dev *dev, uint32_t pipeline_id, + uint32_t frame_fmt, uint16_t channels, + uint16_t buffer_size); + +/** + * @brief Free a buffer created by mux_test_create_sink(). + * @param buffer Buffer to free, NULL is ignored. + */ +void mux_test_free_sink(struct comp_buffer *buffer); + +/** + * @brief Create a buffer that the component under test reads from. + * @param dev Component the buffer is connected to as a source, or NULL. + * @param pipeline_id Pipeline ID of the buffer, used by mux and demux to + * find the stream configuration of the connection. + * @param frame_fmt Frame format of the buffer. + * @param channels Number of channels of the buffer. + * @param buffer_size Buffer size in bytes. + * @return The new buffer, never NULL. + */ +struct comp_buffer *mux_test_create_source(struct comp_dev *dev, uint32_t pipeline_id, + uint32_t frame_fmt, uint16_t channels, + uint16_t buffer_size); + +/** + * @brief Free a buffer created by mux_test_create_source(). + * @param buffer Buffer to free, NULL is ignored. + */ +void mux_test_free_source(struct comp_buffer *buffer); + +/** + * @brief Create a mux or demux component through the IPC3 comp_new() path. + * + * The component is configured with MUX_MAX_STREAMS streams, stream i on + * pipeline i with the routing masks in @p mask. + * + * @param uuid UUID of the mux or the demux driver. + * @param mask Routing masks, one row per stream, or NULL for all zero masks. + * @return The new component, or NULL if comp_new() failed. + */ +struct comp_dev *mux_test_comp_new(const struct sof_uuid *uuid, + const uint8_t mask[MUX_MAX_STREAMS] + [CONFIG_PLATFORM_MAX_CHANNELS]); + +#endif /* __TEST_ZTEST_UNIT_AUDIO_MUX_TEST_UTIL_H__ */ diff --git a/test/ztest/unit/audio/mux/prj.conf b/test/ztest/unit/audio/mux/prj.conf new file mode 100644 index 000000000000..35b7bcce3a94 --- /dev/null +++ b/test/ztest/unit/audio/mux/prj.conf @@ -0,0 +1,17 @@ +# SPDX-License-Identifier: BSD-3-Clause +# +# Copyright(c) 2026 Intel Corporation. + +CONFIG_ZTEST=y +CONFIG_SOF_FULL_ZEPHYR_APPLICATION=n +CONFIG_COMP_MUX=y +CONFIG_FORMAT_S16LE=y +CONFIG_FORMAT_S24LE=y +CONFIG_FORMAT_S32LE=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/mux/testcase.yaml b/test/ztest/unit/audio/mux/testcase.yaml new file mode 100644 index 000000000000..cbb1ee901906 --- /dev/null +++ b/test/ztest/unit/audio/mux/testcase.yaml @@ -0,0 +1,25 @@ +# SPDX-License-Identifier: BSD-3-Clause +# +# Copyright(c) 2026 Intel Corporation. +# +# MUX and DEMUX 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 + - mux + integration_platforms: + - native_sim + arch_exclude: xtensa + +tests: + sof.unit.audio.mux: + platform_allow: + - native_sim + build_only: false From 1dd435db68e5b0a14b152d6cc4f8985ed3ff6020 Mon Sep 17 00:00:00 2001 From: Liam Girdwood Date: Fri, 27 Nov 2026 15:54:16 +0000 Subject: [PATCH 07/11] cmocka: Remove mux tests This patch removes the mux_copy, demux_copy and mux_get_processing_function tests that have been successfully converted to the new framework (from cmock to ztest), together with their CMakeLists.txt, the now empty test/cmocka/src/audio/mux directory and its add_subdirectory() entry. Code coverage testing did not reveal any regression. CMock stats for mux.c, mux_generic.c, mux_ipc3.c (host gcc, unit_test_defconfig) - lines: 86.8 % (515/593) - branches: 59.7 % (191/320) Ztest stats for mux.c, mux_generic.c, mux_ipc3.c (native_sim, host LLVM) - lines: 83.7 % (507/606) - branches: 69.2 % (180/260) Every line of the three files that the CMocka tests execute is also executed by the ztest suite. The totals differ because gcc and LLVM count lines and branches differently. Co-Authored-By: Claude Opus 5.5 Signed-off-by: Liam Girdwood --- test/cmocka/src/audio/CMakeLists.txt | 3 - test/cmocka/src/audio/mux/CMakeLists.txt | 58 --- test/cmocka/src/audio/mux/demux_copy.c | 348 ----------------- test/cmocka/src/audio/mux/mux_copy.c | 366 ------------------ .../audio/mux/mux_get_processing_function.c | 199 ---------- 5 files changed, 974 deletions(-) delete mode 100644 test/cmocka/src/audio/mux/CMakeLists.txt delete mode 100644 test/cmocka/src/audio/mux/demux_copy.c delete mode 100644 test/cmocka/src/audio/mux/mux_copy.c delete mode 100644 test/cmocka/src/audio/mux/mux_get_processing_function.c diff --git a/test/cmocka/src/audio/CMakeLists.txt b/test/cmocka/src/audio/CMakeLists.txt index 9198bbfe6e58..fc1ab136f955 100644 --- a/test/cmocka/src/audio/CMakeLists.txt +++ b/test/cmocka/src/audio/CMakeLists.txt @@ -9,9 +9,6 @@ add_subdirectory(pipeline) if(CONFIG_COMP_VOLUME) add_subdirectory(volume) endif() -if(CONFIG_COMP_MUX) - add_subdirectory(mux) -endif() if(CONFIG_COMP_SEL) add_subdirectory(selector) endif() diff --git a/test/cmocka/src/audio/mux/CMakeLists.txt b/test/cmocka/src/audio/mux/CMakeLists.txt deleted file mode 100644 index ef1eebecd1a6..000000000000 --- a/test/cmocka/src/audio/mux/CMakeLists.txt +++ /dev/null @@ -1,58 +0,0 @@ -# SPDX-License-Identifier: BSD-3-Clause - -# make small lib for stripping so we don't have to care -# about unused missing references - -add_compile_options(-fdata-sections -ffunction-sections -DUNIT_TEST) -link_libraries(-Wl,--gc-sections) - -add_library( - audio_mux - STATIC - ${PROJECT_SOURCE_DIR}/src/audio/mux/mux.c - ${PROJECT_SOURCE_DIR}/src/audio/mux/mux_ipc3.c - ${PROJECT_SOURCE_DIR}/src/audio/mux/mux_generic.c - ${PROJECT_SOURCE_DIR}/src/audio/component.c - ${PROJECT_SOURCE_DIR}/src/audio/data_blob.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}/src/math/numbers.c - ${PROJECT_SOURCE_DIR}/src/kernel/ipc/ipc3/helper.c - ${PROJECT_SOURCE_DIR}/src/ipc/ipc-helper.c - ${PROJECT_SOURCE_DIR}/src/lib/objpool.c - ${PROJECT_SOURCE_DIR}/test/cmocka/src/notifier_mocks.c - ${PROJECT_SOURCE_DIR}/src/audio/module_adapter/module_adapter.c - ${PROJECT_SOURCE_DIR}/src/audio/module_adapter/module_adapter_ipc3.c - ${PROJECT_SOURCE_DIR}/src/audio/module_adapter/module/generic.c -) -sof_append_relative_path_definitions(audio_mux) - -target_link_libraries(audio_mux PRIVATE sof_options) - -link_libraries(audio_mux) - -cmocka_test( - mux_get_processing_function - mux_get_processing_function.c -) - -cmocka_test( - mux_copy - mux_copy.c -) - -cmocka_test( - demux_copy - demux_copy.c -) - -target_include_directories(audio_mux PRIVATE ${PROJECT_SOURCE_DIR}/src/audio) -target_include_directories(mux_get_processing_function PRIVATE ${PROJECT_SOURCE_DIR}/src/audio) -target_include_directories(mux_copy PRIVATE ${PROJECT_SOURCE_DIR}/src/audio) -target_include_directories(demux_copy PRIVATE ${PROJECT_SOURCE_DIR}/src/audio) diff --git a/test/cmocka/src/audio/mux/demux_copy.c b/test/cmocka/src/audio/mux/demux_copy.c deleted file mode 100644 index 270f9f61dd04..000000000000 --- a/test/cmocka/src/audio/mux/demux_copy.c +++ /dev/null @@ -1,348 +0,0 @@ -// SPDX-License-Identifier: BSD-3-Clause -// -// Copyright(c) 2019 Intel Corporation. All rights reserved. -// -// Author: Daniel Bogdzia -// Janusz Jankowski - -#include "../../util.h" - -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include - -struct test_data { - struct comp_dev *dev; - struct processing_module *mod; - struct comp_data *cd; - struct comp_buffer *sinks[MUX_MAX_STREAMS]; - struct comp_buffer *source; - void *outputs[MUX_MAX_STREAMS]; - uint32_t format; - uint8_t mask[MUX_MAX_STREAMS][CONFIG_PLATFORM_MAX_CHANNELS]; -}; - -static int16_t input_16b[CONFIG_PLATFORM_MAX_CHANNELS] = { - 0x101, 0x102, 0x104, 0x108, - 0x111, 0x112, 0x114, 0x118, -}; - -static int32_t input_24b[CONFIG_PLATFORM_MAX_CHANNELS] = { - 0x1a1001, 0x2a2002, 0x4a4004, 0x8a8008, - 0x1b1011, 0x2b2012, 0x4b4014, 0x8b8018, -}; - -static int32_t input_32b[CONFIG_PLATFORM_MAX_CHANNELS] = { - 0xd1a1001, 0xd2a2002, 0xd4a4004, 0xd8a8008, - 0xe1b1011, 0xe2b2012, 0xe4b4014, 0xe8b8018, -}; - -static uint16_t valid_formats[] = { - SOF_IPC_FRAME_S16_LE, - SOF_IPC_FRAME_S24_4LE, - SOF_IPC_FRAME_S32_LE, -}; - -static uint8_t masks[][MUX_MAX_STREAMS][CONFIG_PLATFORM_MAX_CHANNELS] = { - { { 0x01, }, }, - { { 0x01, 0x02, 0x04, 0x10, 0x20, 0x40, 0x80}, }, - { { }, - { 0x01, 0x02, 0x04, 0x10, 0x20, 0x40, 0x80}, }, - { { }, - { }, - { 0x01, 0x02, 0x04, 0x10, 0x20, 0x40, 0x80}, }, - { { }, - { }, - { }, - { 0x01, 0x02, 0x04, 0x10, 0x20, 0x40, 0x80}, }, - { { 0x01, }, - { 0x00, 0x01, }, - { 0x00, 0x00, 0x01, }, - { 0x00, 0x00, 0x00, 0x01, 0x02, 0x04, 0x08, 0x10}, }, - { { 0x00, 0x00, 0x00, 0x01, 0x02, 0x04, 0x08, 0x10}, - { 0x00, 0x00, 0x01, }, - { 0x00, 0x01, }, - { 0x01, }, }, - { { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, }, - { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, }, - { 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, }, - { 0x10, 0x08, 0x04, 0x02, 0x01, },}, - { { 0x01, }, - { 0x00, 0x01, }, - { 0x00, 0x00, 0x01, }, - { 0x00, 0x00, 0x00, 0x01, },}, -}; - -static int setup_group(void **state) -{ - sys_comp_init(sof_get()); - sys_comp_module_demux_interface_init(); - return 0; -} - -static struct sof_ipc_comp_process *create_demux_comp_ipc(struct test_data *td) -{ - struct sof_ipc_comp_process *ipc; - struct sof_mux_config *mux; - size_t ipc_size = sizeof(struct sof_ipc_comp_process); - size_t mux_size = sizeof(struct sof_mux_config) + - MUX_MAX_STREAMS * sizeof(struct mux_stream_data); - const struct sof_uuid uuid = SOF_REG_UUID(demux); - int i, j; - - ipc = calloc(1, ipc_size + mux_size + SOF_UUID_SIZE); - memcpy_s(ipc + 1, SOF_UUID_SIZE, &uuid, SOF_UUID_SIZE); - mux = (struct sof_mux_config *)((char *)(ipc + 1) + SOF_UUID_SIZE); - ipc->comp.hdr.size = ipc_size + SOF_UUID_SIZE; - ipc->comp.type = SOF_COMP_MODULE_ADAPTER; - ipc->config.hdr.size = sizeof(struct sof_ipc_comp_config); - ipc->size = mux_size; - ipc->comp.ext_data_length = SOF_UUID_SIZE; - - mux->num_streams = MUX_MAX_STREAMS; - for (i = 0; i < MUX_MAX_STREAMS; ++i) { - mux->streams[i].pipeline_id = i; - for (j = 0; j < CONFIG_PLATFORM_MAX_CHANNELS; ++j) - mux->streams[i].mask[j] = td->mask[i][j]; - } - - return ipc; -} - -static void prepare_sinks(struct test_data *td, size_t sample_size) -{ - int i; - uint16_t buffer_size = sample_size * CONFIG_PLATFORM_MAX_CHANNELS; - - for (i = 0; i < MUX_MAX_STREAMS; ++i) { - td->sinks[i] = create_test_sink(td->dev, i, td->format, CONFIG_PLATFORM_MAX_CHANNELS, - buffer_size); - - td->outputs[i] = td->sinks[i]->stream.addr; - assert_int_equal(audio_stream_get_free_bytes(&td->sinks[i]->stream), buffer_size); - } -} - -static void prepare_source(struct test_data *td, size_t sample_size) -{ - uint16_t buffer_size = sample_size * CONFIG_PLATFORM_MAX_CHANNELS; - - td->source = create_test_source(td->dev, MUX_MAX_STREAMS + 1, td->format, - CONFIG_PLATFORM_MAX_CHANNELS, buffer_size); - - if (td->format == SOF_IPC_FRAME_S16_LE) - memcpy_s(td->source->stream.addr, buffer_size, input_16b, buffer_size); - else if (td->format == SOF_IPC_FRAME_S24_4LE) - memcpy_s(td->source->stream.addr, buffer_size, input_24b, buffer_size); - else - memcpy_s(td->source->stream.addr, buffer_size, input_32b, buffer_size); - - audio_stream_produce(&td->source->stream, buffer_size); - assert_int_equal(audio_stream_get_avail_bytes(&td->source->stream), buffer_size); -} - -static int setup_test_case(void **state) -{ - struct test_data *td = *((struct test_data **)state); - struct comp_dev *dev; - struct processing_module *mod; - struct sof_ipc_comp_process *ipc; - size_t sample_size = td->format == SOF_IPC_FRAME_S16_LE ? sizeof(int16_t) : sizeof(int32_t); - struct pipeline *dummy_pipe; - - ipc = create_demux_comp_ipc(td); - dev = comp_new((struct sof_ipc_comp *)ipc); - free(ipc); - if (!dev) - return -EINVAL; - - /* Add dummy pipeline to bypass comp_check_eos() */ - dummy_pipe = test_malloc(sizeof(*dummy_pipe)); - if (!dummy_pipe) - return -ENOMEM; - dev->expect_eos = false; - dev->pipeline = dummy_pipe; - - mod = comp_mod(dev); - td->dev = dev; - td->mod = mod; - td->cd = module_get_private_data(mod); - - prepare_sinks(td, sample_size); - prepare_source(td, sample_size); - - return comp_prepare(td->dev); -} - -static int teardown_test_case(void **state) -{ - struct test_data *td = *((struct test_data **)state); - int i; - - free_test_source(td->source); - - for (i = 0; i < MUX_MAX_STREAMS; ++i) - free_test_sink(td->sinks[i]); - - test_free(td->dev->pipeline); - td->dev->pipeline = NULL; - comp_free(td->dev); - - return 0; -} - -static void test_demux_copy_proc_16(void **state) -{ - struct test_data *td = *((struct test_data **)state); - int16_t expected_results[MUX_MAX_STREAMS][CONFIG_PLATFORM_MAX_CHANNELS]; - int i, j, k; - - assert_int_equal(comp_copy(td->dev), 0); - - for (i = 0; i < MUX_MAX_STREAMS; ++i) { - for (j = 0; j < CONFIG_PLATFORM_MAX_CHANNELS; ++j) { - int64_t sample = 0; - - for (k = 0; k < CONFIG_PLATFORM_MAX_CHANNELS; ++k) - if (td->mask[i][j] & BIT(k)) - sample = input_16b[k]; - - expected_results[i][j] = sample; - } - } - - for (i = 0; i < MUX_MAX_STREAMS; ++i) - assert_memory_equal(td->outputs[i], expected_results[i], - sizeof(expected_results[0])); -} - -static void test_demux_copy_proc_24(void **state) -{ - struct test_data *td = *((struct test_data **)state); - int32_t expected_results[MUX_MAX_STREAMS][CONFIG_PLATFORM_MAX_CHANNELS]; - int i, j, k; - - assert_int_equal(comp_copy(td->dev), 0); - - for (i = 0; i < MUX_MAX_STREAMS; ++i) { - for (j = 0; j < CONFIG_PLATFORM_MAX_CHANNELS; ++j) { - int32_t sample = 0; - - for (k = 0; k < CONFIG_PLATFORM_MAX_CHANNELS; ++k) - if (td->mask[i][j] & BIT(k)) - sample = input_24b[k]; - - expected_results[i][j] = sample; - } - } - - for (i = 0; i < MUX_MAX_STREAMS; ++i) - assert_memory_equal(td->outputs[i], expected_results[i], - sizeof(expected_results[0])); -} - -static void test_demux_copy_proc_32(void **state) -{ - struct test_data *td = *((struct test_data **)state); - int32_t expected_results[MUX_MAX_STREAMS][CONFIG_PLATFORM_MAX_CHANNELS]; - int i, j, k; - - assert_int_equal(comp_copy(td->dev), 0); - - for (i = 0; i < MUX_MAX_STREAMS; ++i) { - for (j = 0; j < CONFIG_PLATFORM_MAX_CHANNELS; ++j) { - int32_t sample = 0; - - for (k = 0; k < CONFIG_PLATFORM_MAX_CHANNELS; ++k) - if (td->mask[i][j] & BIT(k)) - sample = input_32b[k]; - - expected_results[i][j] = sample; - } - } - - for (i = 0; i < MUX_MAX_STREAMS; ++i) - assert_memory_equal(td->outputs[i], expected_results[i], - sizeof(expected_results[0])); -} - -static char *get_test_name(int mask_index, const char *format_name) -{ - int length = snprintf(NULL, 0, "test_demux_copy_%s_mask_%d", - format_name, mask_index) + 1; - char *buffer = malloc(length); - - snprintf(buffer, length, "test_demux_copy_%s_mask_%d", - format_name, mask_index); - - return buffer; -} - -int main(void) -{ - struct CMUnitTest tests[ARRAY_SIZE(valid_formats) * ARRAY_SIZE(masks)]; - struct test_data *td; - int i, j, ti, ret; - - for (i = 0; i < ARRAY_SIZE(valid_formats); ++i) { - for (j = 0; j < ARRAY_SIZE(masks); ++j) { - ti = i * ARRAY_SIZE(masks) + j; - td = malloc(sizeof(struct test_data)); - - td->format = valid_formats[i]; - - memcpy_s(td->mask, sizeof(td->mask), - masks[j], sizeof(masks[0])); - - switch (td->format) { -#if CONFIG_FORMAT_S16LE - case SOF_IPC_FRAME_S16_LE: - tests[ti].name = get_test_name(j, "s16le"); - tests[ti].test_func = test_demux_copy_proc_16; - break; -#endif /* CONFIG_FORMAT_S16LE */ -#if CONFIG_FORMAT_S24LE - case SOF_IPC_FRAME_S24_4LE: - tests[ti].name = get_test_name(j, "s24_4le"); - tests[ti].test_func = test_demux_copy_proc_24; - break; -#endif /* CONFIG_FORMAT_S24LE */ -#if CONFIG_FORMAT_S32LE - case SOF_IPC_FRAME_S32_LE: - tests[ti].name = get_test_name(j, "s32le"); - tests[ti].test_func = test_demux_copy_proc_32; - break; -#endif /* CONFIG_FORMAT_S32LE */ -#if CONFIG_FORMAT_S24_3LE - case SOF_IPC_FRAME_S24_3LE: - break; -#endif /* CONFIG_FORMAT_S24_3LE */ - default: - return -EINVAL; - } - - tests[ti].initial_state = td; - tests[ti].setup_func = setup_test_case; - tests[ti].teardown_func = teardown_test_case; - } - } - - cmocka_set_message_output(CM_OUTPUT_TAP); - ret = cmocka_run_group_tests(tests, setup_group, NULL); - - for (ti = 0; ti < ARRAY_SIZE(valid_formats) * ARRAY_SIZE(masks); ti++) { - free(tests[ti].initial_state); - free((void *)tests[ti].name); - } - - return ret; -} diff --git a/test/cmocka/src/audio/mux/mux_copy.c b/test/cmocka/src/audio/mux/mux_copy.c deleted file mode 100644 index df9d00252d15..000000000000 --- a/test/cmocka/src/audio/mux/mux_copy.c +++ /dev/null @@ -1,366 +0,0 @@ -// SPDX-License-Identifier: BSD-3-Clause -// -// Copyright(c) 2019 Intel Corporation. All rights reserved. -// -// Author: Daniel Bogdzia -// Janusz Jankowski - -#include "../../util.h" - -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include - -struct test_data { - struct comp_dev *dev; - struct processing_module *mod; - struct comp_data *cd; - struct comp_buffer *sources[MUX_MAX_STREAMS]; - struct comp_buffer *sink; - void *output; - uint32_t format; - uint8_t mask[MUX_MAX_STREAMS][CONFIG_PLATFORM_MAX_CHANNELS]; -}; - -static int16_t input_16b[MUX_MAX_STREAMS][CONFIG_PLATFORM_MAX_CHANNELS] = { - { 0x101, 0x102, 0x104, 0x108, - 0x111, 0x112, 0x114, 0x118, }, - { 0x201, 0x202, 0x204, 0x208, - 0x211, 0x212, 0x214, 0x218, }, - { 0x301, 0x302, 0x304, 0x308, - 0x311, 0x312, 0x314, 0x318, }, - { 0x401, 0x402, 0x404, 0x408, - 0x411, 0x412, 0x414, 0x418, }, -}; - -static int32_t input_24b[MUX_MAX_STREAMS][CONFIG_PLATFORM_MAX_CHANNELS] = { - { 0x1a1001, 0x2a2002, 0x4a4004, 0x8a8008, - 0x1b1011, 0x2b2012, 0x4b4014, 0x8b8018, }, - { 0x1a1101, 0x2a2102, 0x4a4104, 0x8a8108, - 0x1b1111, 0x2b2112, 0x4b4114, 0x8b8118, }, - { 0x1a1201, 0x2a2202, 0x4a4204, 0x8a8208, - 0x1b1211, 0x2b2212, 0x4b4214, 0x8b8218, }, - { 0x1a1401, 0x2a2402, 0x4a4404, 0x8a8408, - 0x1b1411, 0x2b2412, 0x4b4414, 0x8b8418, }, -}; - -static int32_t input_32b[MUX_MAX_STREAMS][CONFIG_PLATFORM_MAX_CHANNELS] = { - { 0xd1a1001, 0xd2a2002, 0xd4a4004, 0xd8a8008, - 0xe1b1011, 0xe2b2012, 0xe4b4014, 0xe8b8018, }, - { 0xd1a1101, 0xd2a2102, 0xd4a4104, 0xd8a8108, - 0xe1b1111, 0xe2b2112, 0xe4b4114, 0xe8b8118, }, - { 0xd1a1201, 0xd2a2202, 0xd4a4204, 0xd8a8208, - 0xe1b1211, 0xe2b2212, 0xe4b4214, 0xe8b8218, }, - { 0xd1a1401, 0xd2a2402, 0xd4a4404, 0xd8a8408, - 0xe1b1411, 0xe2b2412, 0xe4b4414, 0xe8b8418, }, -}; - -static uint16_t valid_formats[] = { - SOF_IPC_FRAME_S16_LE, - SOF_IPC_FRAME_S24_4LE, - SOF_IPC_FRAME_S32_LE, -}; - -static uint8_t masks[][MUX_MAX_STREAMS][CONFIG_PLATFORM_MAX_CHANNELS] = { - { { 0x01, }, }, - { { 0x01, 0x02, 0x04, 0x10, 0x20, 0x40, 0x80 }, }, - { { }, - { 0x01, 0x02, 0x04, 0x10, 0x20, 0x40, 0x80 }, }, - { { }, - { }, - { 0x01, 0x02, 0x04, 0x10, 0x20, 0x40, 0x80 }, }, - { { }, - { }, - { }, - { 0x01, 0x02, 0x04, 0x10, 0x20, 0x40, 0x80 }, }, - { { 0x01, }, - { 0x00, 0x01, }, - { 0x00, 0x00, 0x01, }, - { 0x00, 0x00, 0x00, 0x01, 0x02, 0x04, 0x08, 0x10 }, }, - { { 0x00, 0x00, 0x00, 0x01, 0x02, 0x04, 0x08, 0x10 }, - { 0x00, 0x00, 0x01, }, - { 0x00, 0x01, }, - { 0x01, }, }, - { { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, }, - { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, }, - { 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, }, - { 0x10, 0x08, 0x04, 0x02, 0x01, }, }, - { { 0x01, }, - { 0x00, 0x01, }, - { 0x00, 0x00, 0x01, }, - { 0x00, 0x00, 0x00, 0x01, }, }, -}; - -static int setup_group(void **state) -{ - sys_comp_init(sof_get()); - sys_comp_module_mux_interface_init(); - return 0; -} - -static struct sof_ipc_comp_process *create_mux_comp_ipc(struct test_data *td) -{ - struct sof_ipc_comp_process *ipc; - struct sof_mux_config *mux; - size_t ipc_size = sizeof(struct sof_ipc_comp_process); - size_t mux_size = sizeof(struct sof_mux_config) + - MUX_MAX_STREAMS * sizeof(struct mux_stream_data); - const struct sof_uuid uuid = SOF_REG_UUID(mux); - int i, j; - - ipc = calloc(1, ipc_size + mux_size + SOF_UUID_SIZE); - memcpy_s(ipc + 1, SOF_UUID_SIZE, &uuid, SOF_UUID_SIZE); - mux = (struct sof_mux_config *)((char *)(ipc + 1) + SOF_UUID_SIZE); - ipc->comp.hdr.size = ipc_size + SOF_UUID_SIZE; - ipc->comp.type = SOF_COMP_MODULE_ADAPTER; - ipc->config.hdr.size = sizeof(struct sof_ipc_comp_config); - ipc->size = mux_size; - ipc->comp.ext_data_length = SOF_UUID_SIZE; - - mux->num_streams = MUX_MAX_STREAMS; - for (i = 0; i < MUX_MAX_STREAMS; ++i) { - mux->streams[i].pipeline_id = i; - for (j = 0; j < CONFIG_PLATFORM_MAX_CHANNELS; ++j) - mux->streams[i].mask[j] = td->mask[i][j]; - } - - return ipc; -} - -static void prepare_sink(struct test_data *td, size_t sample_size) -{ - uint16_t buffer_size = sample_size * CONFIG_PLATFORM_MAX_CHANNELS; - - td->sink = create_test_sink(td->dev, MUX_MAX_STREAMS + 1, td->format, CONFIG_PLATFORM_MAX_CHANNELS, - buffer_size); - td->output = td->sink->stream.addr; - - assert_int_equal(audio_stream_get_free_bytes(&td->sink->stream), buffer_size); -} - -static void prepare_sources(struct test_data *td, size_t sample_size) -{ - int i; - uint16_t buffer_size = sample_size * CONFIG_PLATFORM_MAX_CHANNELS; - - for (i = 0; i < MUX_MAX_STREAMS; ++i) { - td->sources[i] = create_test_source(td->dev, i, td->format, CONFIG_PLATFORM_MAX_CHANNELS, - buffer_size); - - if (td->format == SOF_IPC_FRAME_S16_LE) - memcpy_s(td->sources[i]->stream.addr, buffer_size, &input_16b[i], - buffer_size); - else if (td->format == SOF_IPC_FRAME_S24_4LE) - memcpy_s(td->sources[i]->stream.addr, buffer_size, &input_24b[i], - buffer_size); - else - memcpy_s(td->sources[i]->stream.addr, buffer_size, &input_32b[i], - buffer_size); - - audio_stream_produce(&td->sources[i]->stream, buffer_size); - assert_int_equal(audio_stream_get_avail_bytes(&td->sources[i]->stream), - buffer_size); - } -} - -static int setup_test_case(void **state) -{ - struct test_data *td = *((struct test_data **)state); - struct comp_dev *dev; - struct processing_module *mod; - struct sof_ipc_comp_process *ipc; - size_t sample_size = td->format == SOF_IPC_FRAME_S16_LE ? sizeof(int16_t) : sizeof(int32_t); - struct pipeline *dummy_pipe; - - ipc = create_mux_comp_ipc(td); - dev = comp_new((struct sof_ipc_comp *)ipc); - free(ipc); - if (!dev) - return -EINVAL; - - /* Add dummy pipeline to bypass comp_check_eos() */ - dummy_pipe = test_malloc(sizeof(*dummy_pipe)); - if (!dummy_pipe) - return -ENOMEM; - dev->expect_eos = false; - dev->pipeline = dummy_pipe; - - mod = comp_mod(dev); - td->dev = dev; - td->mod = mod; - td->cd = module_get_private_data(mod); - - prepare_sink(td, sample_size); - prepare_sources(td, sample_size); - - return comp_prepare(td->dev); - -} - -static int teardown_test_case(void **state) -{ - struct test_data *td = *((struct test_data **)state); - int i; - - for (i = 0; i < MUX_MAX_STREAMS; ++i) - free_test_source(td->sources[i]); - - free_test_sink(td->sink); - test_free(td->dev->pipeline); - td->dev->pipeline = NULL; - comp_free(td->dev); - return 0; -} - -static void test_mux_copy_proc_16(void **state) -{ - struct test_data *td = *((struct test_data **)state); - int16_t expected_result[CONFIG_PLATFORM_MAX_CHANNELS]; - int i, j, k; - - assert_int_equal(comp_copy(td->dev), 0); - - for (i = 0; i < CONFIG_PLATFORM_MAX_CHANNELS; ++i) { - int32_t sample = 0; - - for (j = 0; j < MUX_MAX_STREAMS; ++j) { - for (k = 0; k < CONFIG_PLATFORM_MAX_CHANNELS; ++k) { - if (td->mask[j][k] & BIT(i)) - sample = input_16b[j][k]; - } - } - expected_result[i] = sample; - } - - assert_memory_equal(td->output, expected_result, - sizeof(expected_result)); -} - -static void test_mux_copy_proc_24(void **state) -{ - struct test_data *td = *((struct test_data **)state); - int32_t expected_result[CONFIG_PLATFORM_MAX_CHANNELS]; - int i, j, k; - - assert_int_equal(comp_copy(td->dev), 0); - - for (i = 0; i < CONFIG_PLATFORM_MAX_CHANNELS; ++i) { - int32_t sample = 0; - - for (j = 0; j < MUX_MAX_STREAMS; ++j) { - for (k = 0; k < CONFIG_PLATFORM_MAX_CHANNELS; ++k) { - if (td->mask[j][k] & BIT(i)) - sample = input_24b[j][k]; - } - } - expected_result[i] = sample; - } - - assert_memory_equal(td->output, expected_result, - sizeof(expected_result)); -} - -static void test_mux_copy_proc_32(void **state) -{ - struct test_data *td = *((struct test_data **)state); - int32_t expected_result[CONFIG_PLATFORM_MAX_CHANNELS]; - int i, j, k; - - assert_int_equal(comp_copy(td->dev), 0); - - for (i = 0; i < CONFIG_PLATFORM_MAX_CHANNELS; ++i) { - int32_t sample = 0; - - for (j = 0; j < MUX_MAX_STREAMS; ++j) { - for (k = 0; k < CONFIG_PLATFORM_MAX_CHANNELS; ++k) { - if (td->mask[j][k] & BIT(i)) - sample = input_32b[j][k]; - } - } - expected_result[i] = sample; - } - - assert_memory_equal(td->output, expected_result, - sizeof(expected_result)); -} - -static char *get_test_name(int mask_index, const char *format_name) -{ - int length = snprintf(NULL, 0, "test_mux_copy_%s_mask_%d", - format_name, mask_index) + 1; - char *buffer = malloc(length); - - snprintf(buffer, length, "test_mux_copy_%s_mask_%d", - format_name, mask_index); - - return buffer; -} - -int main(void) -{ - struct test_data *td; - struct CMUnitTest tests[ARRAY_SIZE(valid_formats) * ARRAY_SIZE(masks)]; - int i, j, ti, ret; - - for (i = 0; i < ARRAY_SIZE(valid_formats); ++i) { - for (j = 0; j < ARRAY_SIZE(masks); ++j) { - ti = i * ARRAY_SIZE(masks) + j; - td = malloc(sizeof(struct test_data)); - - td->format = valid_formats[i]; - - memcpy_s(td->mask, sizeof(td->mask), - masks[j], sizeof(masks[0])); - - switch (td->format) { -#if CONFIG_FORMAT_S16LE - case SOF_IPC_FRAME_S16_LE: - tests[ti].name = get_test_name(j, "s16le"); - tests[ti].test_func = test_mux_copy_proc_16; - break; -#endif /* CONFIG_FORMAT_S16LE */ -#if CONFIG_FORMAT_S24LE - case SOF_IPC_FRAME_S24_4LE: - tests[ti].name = get_test_name(j, "s24_4le"); - tests[ti].test_func = test_mux_copy_proc_24; - break; -#endif /* CONFIG_FORMAT_S24LE */ -#if CONFIG_FORMAT_S32LE - case SOF_IPC_FRAME_S32_LE: - tests[ti].name = get_test_name(j, "s32le"); - tests[ti].test_func = test_mux_copy_proc_32; - break; -#endif /* CONFIG_FORMAT_S32LE */ -#if CONFIG_FORMAT_S24_3LE - case SOF_IPC_FRAME_S24_3LE: - break; -#endif /* CONFIG_FORMAT_S24_3LE */ - default: - return -EINVAL; - } - - tests[ti].initial_state = td; - tests[ti].setup_func = setup_test_case; - tests[ti].teardown_func = teardown_test_case; - } - } - - cmocka_set_message_output(CM_OUTPUT_TAP); - ret = cmocka_run_group_tests(tests, setup_group, NULL); - - for (ti = 0; ti < ARRAY_SIZE(valid_formats) * ARRAY_SIZE(masks); ti++) { - free(tests[ti].initial_state); - free((void *)tests[ti].name); - } - - return ret; -} diff --git a/test/cmocka/src/audio/mux/mux_get_processing_function.c b/test/cmocka/src/audio/mux/mux_get_processing_function.c deleted file mode 100644 index 9745d4315c21..000000000000 --- a/test/cmocka/src/audio/mux/mux_get_processing_function.c +++ /dev/null @@ -1,199 +0,0 @@ -// SPDX-License-Identifier: BSD-3-Clause -// -// Copyright(c) 2019 Intel Corporation. All rights reserved. -// -// Author: Daniel Bogdzia -// Janusz Jankowski - -#include "../../util.h" - -#include -#include -#include - -#include -#include -#include -#include -#include -#include - -struct test_data { - struct comp_dev *dev; - struct processing_module *mod; - struct comp_data *cd; - struct comp_buffer *sink; -}; - -static int setup_group(void **state) -{ - sys_comp_init(sof_get()); - sys_comp_module_mux_interface_init(); - - return 0; -} - -static struct sof_ipc_comp_process *create_mux_comp_ipc(struct test_data *td) -{ - struct sof_ipc_comp_process *ipc; - struct sof_mux_config *mux; - size_t ipc_size = sizeof(struct sof_ipc_comp_process); - size_t mux_size = sizeof(struct sof_mux_config) + - MUX_MAX_STREAMS * sizeof(struct mux_stream_data); - const struct sof_uuid uuid = SOF_REG_UUID(mux); - int i, j; - - ipc = calloc(1, ipc_size + mux_size + SOF_UUID_SIZE); - memcpy_s(ipc + 1, SOF_UUID_SIZE, &uuid, SOF_UUID_SIZE); - mux = (struct sof_mux_config *)((char *)(ipc + 1) + SOF_UUID_SIZE); - ipc->comp.hdr.size = ipc_size + SOF_UUID_SIZE; - ipc->comp.type = SOF_COMP_MODULE_ADAPTER; - ipc->config.hdr.size = sizeof(struct sof_ipc_comp_config); - ipc->size = mux_size; - ipc->comp.ext_data_length = SOF_UUID_SIZE; - - mux->num_streams = MUX_MAX_STREAMS; - for (i = 0; i < MUX_MAX_STREAMS; ++i) { - mux->streams[i].pipeline_id = i; - for (j = 0; j < CONFIG_PLATFORM_MAX_CHANNELS; ++j) - mux->streams[i].mask[j] = 0; - } - - return ipc; -} - - -static int setup_test_case(void **state) -{ - struct test_data *td; - struct comp_dev *dev; - struct processing_module *mod; - struct sof_ipc_comp_process *ipc; - - td = test_malloc(sizeof(*td)); - if (!td) - return -ENOMEM; - - ipc = create_mux_comp_ipc(td); - dev = comp_new((struct sof_ipc_comp *)ipc); - free(ipc); - if (!dev) - return -EINVAL; - - mod = comp_mod(dev); - td->dev = dev; - td->mod = mod; - td->cd = module_get_private_data(mod); - td->sink = create_test_sink(dev, 0, 0, 0, 4); - - *state = td; - return 0; -} - -static int teardown_test_case(void **state) -{ - struct test_data *td = *state; - - free_test_sink(td->sink); - comp_free(td->dev); - test_free(td); - - return 0; -} - -#if CONFIG_FORMAT_FLOAT -static void test_mux_get_processing_function_valid_float(void **state) -{ - struct test_data *td = *state; - mux_func func = NULL; - - audio_stream_set_frm_fmt(&td->sink->stream, SOF_IPC_FRAME_FLOAT); - - func = mux_get_processing_function(td->mod); - - assert_ptr_not_equal(func, NULL); -} -#endif /* CONFIG_FORMAT_FLOAT */ - -static void test_mux_get_processing_function_invalid_format(void **state) -{ - struct test_data *td = *state; - mux_func func = NULL; - - /* set frame format value to unsupported value */ - audio_stream_set_frm_fmt(&td->sink->stream, SOF_IPC_FRAME_INVALID); - - func = mux_get_processing_function(td->mod); - - assert_ptr_equal(func, NULL); -} - -#if CONFIG_FORMAT_S16LE -static void test_mux_get_processing_function_valid_s16le(void **state) -{ - struct test_data *td = *state; - mux_func func = NULL; - - audio_stream_set_frm_fmt(&td->sink->stream, SOF_IPC_FRAME_S16_LE); - - func = mux_get_processing_function(td->mod); - - assert_ptr_not_equal(func, NULL); -} -#endif /* CONFIG_FORMAT_S16LE */ - -#if CONFIG_FORMAT_S24LE -static void test_mux_get_processing_function_valid_s24_4le(void **state) -{ - struct test_data *td = *state; - mux_func func = NULL; - - audio_stream_set_frm_fmt(&td->sink->stream, SOF_IPC_FRAME_S24_4LE); - - func = mux_get_processing_function(td->mod); - - assert_ptr_not_equal(func, NULL); -} -#endif /* CONFIG_FORMAT_S24LE */ - -#if CONFIG_FORMAT_S32LE -static void test_mux_get_processing_function_valid_s32le(void **state) -{ - struct test_data *td = *state; - mux_func func = NULL; - - audio_stream_set_frm_fmt(&td->sink->stream, SOF_IPC_FRAME_S32_LE); - - func = mux_get_processing_function(td->mod); - - assert_ptr_not_equal(func, NULL); -} -#endif /* CONFIG_FORMAT_S32LE */ - -#define TEST_CASE(name) \ - cmocka_unit_test_setup_teardown(name, \ - setup_test_case, \ - teardown_test_case) - -int main(void) -{ - const struct CMUnitTest tests[] = { -#if CONFIG_FORMAT_FLOAT - TEST_CASE(test_mux_get_processing_function_valid_float), -#endif /* CONFIG_FORMAT_FLOAT */ - TEST_CASE(test_mux_get_processing_function_invalid_format), -#if CONFIG_FORMAT_S16LE - TEST_CASE(test_mux_get_processing_function_valid_s16le), -#endif /* CONFIG_FORMAT_S16LE */ -#if CONFIG_FORMAT_S24LE - TEST_CASE(test_mux_get_processing_function_valid_s24_4le), -#endif /* CONFIG_FORMAT_S24LE */ -#if CONFIG_FORMAT_S32LE - TEST_CASE(test_mux_get_processing_function_valid_s32le), -#endif /* CONFIG_FORMAT_S32LE */ - }; - - cmocka_set_message_output(CM_OUTPUT_TAP); - - return cmocka_run_group_tests(tests, setup_group, NULL); -} From 46ff33a648ee4210318de669078794ba0595a415 Mon Sep 17 00:00:00 2001 From: Liam Girdwood Date: Fri, 27 Nov 2026 15:45:14 +0000 Subject: [PATCH 08/11] test: math: port drc_math_test CMocka test to ztest Add a ztest suite for the DRC fixed-point 1/x function drc_inv_fixed(), covering the same three cases as the CMocka drc_math_test: Q12 to Q30, Q22 to Q26 and Q31 to Q20, each over 500 or 501 inputs from 1 to INT32_MAX, checked against a double precision reference with the same absolute and relative error limits. The suite lives in test/ztest/unit/math/drc, as it tests math functions only; test/ztest/unit/audio/drc tests the DRC component processing. The same sources as the CMocka target are linked: drc_math_generic.c, drc_math_hifi3.c, numbers.c, exp_fcn.c and exp_fcn_hifi.c. The HiFi3 variants compile to nothing on native_sim. CONFIG_COMP_DRC=y is set, because the DRC SIMD level choice depends on it; on native_sim it selects DRC_HIFI_NONE, i.e. the generic implementation, as the CMocka unit_test_defconfig does. The values printed in the error messages are computed with ((int64_t)1 << q) instead of (1 << q), which overflowed for q = 31. They are not used by the checks. Co-Authored-By: Claude Opus 5.5 Signed-off-by: Liam Girdwood --- test/ztest/unit/math/drc/CMakeLists.txt | 41 +++++ test/ztest/unit/math/drc/prj.conf | 12 ++ .../ztest/unit/math/drc/test_drc_math_ztest.c | 153 ++++++++++++++++++ test/ztest/unit/math/drc/testcase.yaml | 25 +++ 4 files changed, 231 insertions(+) create mode 100644 test/ztest/unit/math/drc/CMakeLists.txt create mode 100644 test/ztest/unit/math/drc/prj.conf create mode 100644 test/ztest/unit/math/drc/test_drc_math_ztest.c create mode 100644 test/ztest/unit/math/drc/testcase.yaml diff --git a/test/ztest/unit/math/drc/CMakeLists.txt b/test/ztest/unit/math/drc/CMakeLists.txt new file mode 100644 index 000000000000..a9c98f21ff88 --- /dev/null +++ b/test/ztest/unit/math/drc/CMakeLists.txt @@ -0,0 +1,41 @@ +# 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_drc) + +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 + ${PROJECT_BINARY_DIR}/include/generated +) + +target_compile_definitions(app PRIVATE + -DCONFIG_ZEPHYR_POSIX=1 + -DCONFIG_LIBRARY=1 + -DUNIT_TEST=1 +) + +# The same sources as the CMocka drc_math_test target. The HiFi3 variants +# are inert on native_sim, they compile only for Xtensa HiFi targets. +target_sources(app PRIVATE + test_drc_math_ztest.c + ${SOF_ROOT}/src/audio/drc/drc_math_generic.c + ${SOF_ROOT}/src/audio/drc/drc_math_hifi3.c + ${SOF_ROOT}/src/math/numbers.c + ${SOF_ROOT}/src/math/exp_fcn.c + ${SOF_ROOT}/src/math/exp_fcn_hifi.c +) + +sof_append_relative_path_definitions(app) diff --git a/test/ztest/unit/math/drc/prj.conf b/test/ztest/unit/math/drc/prj.conf new file mode 100644 index 000000000000..0be900a6c9e4 --- /dev/null +++ b/test/ztest/unit/math/drc/prj.conf @@ -0,0 +1,12 @@ +# 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, %e); without +# this, printk silently emits the format string literally instead of the value. +CONFIG_CBPRINTF_FP_SUPPORT=y +# The DRC SIMD level choice depends on COMP_DRC. On native_sim it defaults to +# DRC_HIFI_NONE, which selects the generic drc_math_generic.c implementation. +CONFIG_COMP_DRC=y diff --git a/test/ztest/unit/math/drc/test_drc_math_ztest.c b/test/ztest/unit/math/drc/test_drc_math_ztest.c new file mode 100644 index 000000000000..9a0e4ba9e433 --- /dev/null +++ b/test/ztest/unit/math/drc/test_drc_math_ztest.c @@ -0,0 +1,153 @@ +// SPDX-License-Identifier: BSD-3-Clause +// +// Copyright(c) 2026 Intel Corporation. +// +// Converted from CMock to Ztest +// Original tests from test/cmocka/src/audio/drc/drc_math_test.c: +// Author: Seppo Ingalsuo + +#include + +#include + +#include +#include + +#define TEST_POINTS 500 + +#define ABS_DELTA_TOLERANCE_T1 1.0e-9 +#define REL_DELTA_TOLERANCE_T1 1.4e-4 +#define ABS_DELTA_TOLERANCE_T2 7.4e-7 +#define REL_DELTA_TOLERANCE_T2 7.5e-6 +#define ABS_DELTA_TOLERANCE_T3 3.8e-4 +#define REL_DELTA_TOLERANCE_T3 1.9e-6 + +/** + * @brief Reference 1/x in double precision, rounded and saturated to int32 + * @param[in] x Input value. + * @param[in] precision_x Fractional bits of x. + * @param[in] precision_y Fractional bits of the result. + * @return 1/x with precision_y fractional bits. + */ +static int32_t drc_inv_ref(int32_t x, int32_t precision_x, int32_t precision_y) +{ + double xf, yf, ys; + + xf = (double)x / ((int64_t)1 << precision_x); + yf = 1 / xf; + ys = round(yf * ((int64_t)1 << precision_y)); + if (ys > INT32_MAX) + ys = INT32_MAX; + + if (ys < INT32_MIN) + ys = INT32_MIN; + + return (int32_t)ys; +} + +/** + * @brief Compare drc_inv_fixed() with the reference over an input range + * @param[in] x_start First input value. + * @param[in] x_end Last input value, inclusive. + * @param[in] x_step Input increment. + * @param[in] q_in Fractional bits of the input. + * @param[in] q_out Fractional bits of the output. + * @param[in] abs_delta_tolerance Maximum absolute error. + * @param[in] rel_delta_tolerance Maximum relative error. + */ +static void drc_math_inv_fixed_test_helper(int32_t x_start, int32_t x_end, + int32_t x_step, int q_in, int q_out, + double abs_delta_tolerance, double rel_delta_tolerance) +{ + double fx, fy1, fy2; + double abs_delta, rel_delta; + int64_t x1; + int32_t x, y1, y2; + int32_t differences = 0; + double abs_delta_max = 0; + double rel_delta_max = 0; + + printk("%s: Testing q_in = %d q_out = %d in = %d:%d:%d\n", __func__, + q_in, q_out, x_start, x_step, x_end); + + x1 = x_start; + while (x1 <= x_end) { + x = (int32_t)x1; + y1 = drc_inv_ref(x, q_in, q_out); + y2 = drc_inv_fixed(x, q_in, q_out); + if (y1 != y2) + differences++; + + fx = (double)x / ((int64_t)1 << q_in); + fy1 = (double)y1 / ((int64_t)1 << q_out); + fy2 = (double)y2 / ((int64_t)1 << q_out); + abs_delta = fabs(fy1 - fy2); + rel_delta = abs_delta / fy1; + if (rel_delta > rel_delta_max) + rel_delta_max = rel_delta; + + if (abs_delta > abs_delta_max) + abs_delta_max = abs_delta; + + zassert_false(rel_delta > rel_delta_tolerance, + "Relative error %g exceeds limit %g, input %g output %g ref %g.", + rel_delta, rel_delta_tolerance, fx, fy2, fy1); + + zassert_false(abs_delta > abs_delta_tolerance, + "Absolute error %g exceeds limit %g, input %g output %g ref %g.", + abs_delta, abs_delta_tolerance, fx, fy2, fy1); + + x1 += x_step; + } + + printk("%s: bit exact differences count = %d\n", __func__, differences); + printk("%s: Absolute max error was %.6e.\n", __func__, abs_delta_max); + printk("%s: Relative max error was %.6e.\n", __func__, rel_delta_max); +} + +/** + * @brief Test drc_inv_fixed() with Q12 input and Q30 output + */ +ZTEST(math_drc_suite, test_function_drc_inv_fixed_q12_q30) +{ + int32_t x_start = 1; + int32_t x_end = INT32_MAX; + int32_t x_step = (int32_t)(((int64_t)x_end - (int64_t)x_start) / (TEST_POINTS - 1)); + int q_in = 12; + int q_out = 30; + + drc_math_inv_fixed_test_helper(x_start, x_end, x_step, q_in, q_out, + ABS_DELTA_TOLERANCE_T1, REL_DELTA_TOLERANCE_T1); +} + +/** + * @brief Test drc_inv_fixed() with Q22 input and Q26 output + */ +ZTEST(math_drc_suite, test_function_drc_inv_fixed_q22_q26) +{ + int32_t x_start = 1; + int32_t x_end = INT32_MAX; + int32_t x_step = (int32_t)(((int64_t)x_end - (int64_t)x_start) / TEST_POINTS); + int q_in = 22; + int q_out = 26; + + drc_math_inv_fixed_test_helper(x_start, x_end, x_step, q_in, q_out, + ABS_DELTA_TOLERANCE_T2, REL_DELTA_TOLERANCE_T2); +} + +/** + * @brief Test drc_inv_fixed() with Q31 input and Q20 output + */ +ZTEST(math_drc_suite, test_function_drc_inv_fixed_q31_q20) +{ + int32_t x_start = 1; + int32_t x_end = INT32_MAX; + int32_t x_step = (int32_t)(((int64_t)x_end - (int64_t)x_start) / TEST_POINTS); + int q_in = 31; + int q_out = 20; + + drc_math_inv_fixed_test_helper(x_start, x_end, x_step, q_in, q_out, + ABS_DELTA_TOLERANCE_T3, REL_DELTA_TOLERANCE_T3); +} + +ZTEST_SUITE(math_drc_suite, NULL, NULL, NULL, NULL, NULL); diff --git a/test/ztest/unit/math/drc/testcase.yaml b/test/ztest/unit/math/drc/testcase.yaml new file mode 100644 index 000000000000..9792e0e5134e --- /dev/null +++ b/test/ztest/unit/math/drc/testcase.yaml @@ -0,0 +1,25 @@ +# SPDX-License-Identifier: BSD-3-Clause +# +# Copyright(c) 2026 Intel Corporation. +# +# DRC 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 + - drc + integration_platforms: + - native_sim + arch_exclude: xtensa + +tests: + sof.unit.math.drc: + platform_allow: + - native_sim + build_only: false From 20d3e28353098f1ff19606467aa3faeb859d1f75 Mon Sep 17 00:00:00 2001 From: Liam Girdwood Date: Fri, 27 Nov 2026 15:45:44 +0000 Subject: [PATCH 09/11] cmocka: Remove drc_math_test test This patch removes the drc_math_test test that has been successfully converted to the new framework (from cmock to ztest) in test/ztest/unit/math/drc. It was the only test in test/cmocka/src/audio/drc, so the directory and its add_subdirectory() entry are removed too. Code coverage testing did not reveal any regression. CMock stats for drc_math_generic.c, drc_math_hifi3.c, numbers.c, exp_fcn.c, exp_fcn_hifi.c (host gcc, unit_test_defconfig) - lines: 18.6 % (44/236) - branches: 22.2 % (16/72) Ztest stats for drc_math_generic.c, drc_math_hifi3.c, numbers.c, exp_fcn.c, exp_fcn_hifi.c (native_sim, host LLVM) - lines: 21.2 % (44/208) - branches: 22.2 % (16/72) Every line covered by the CMocka test is also covered by the ztest suite. Both exercise only drc_inv_fixed(), its rexp_fixed() helper and norm_int32() from numbers.c; the HiFi3 sources have no code on the host. The totals differ because gcc and LLVM count lines and branches differently. Co-Authored-By: Claude Opus 5.5 Signed-off-by: Liam Girdwood --- test/cmocka/src/audio/CMakeLists.txt | 3 - test/cmocka/src/audio/drc/CMakeLists.txt | 12 -- test/cmocka/src/audio/drc/drc_math_test.c | 148 ---------------------- 3 files changed, 163 deletions(-) delete mode 100644 test/cmocka/src/audio/drc/CMakeLists.txt delete mode 100644 test/cmocka/src/audio/drc/drc_math_test.c diff --git a/test/cmocka/src/audio/CMakeLists.txt b/test/cmocka/src/audio/CMakeLists.txt index fc1ab136f955..321ce64282b8 100644 --- a/test/cmocka/src/audio/CMakeLists.txt +++ b/test/cmocka/src/audio/CMakeLists.txt @@ -18,9 +18,6 @@ endif() if(CONFIG_COMP_FIR) add_subdirectory(eq_fir) endif() -if(CONFIG_COMP_DRC) - add_subdirectory(drc) -endif() if(CONFIG_COMP_DCBLOCK) add_subdirectory(dcblock) endif() diff --git a/test/cmocka/src/audio/drc/CMakeLists.txt b/test/cmocka/src/audio/drc/CMakeLists.txt deleted file mode 100644 index f0c351e5c90a..000000000000 --- a/test/cmocka/src/audio/drc/CMakeLists.txt +++ /dev/null @@ -1,12 +0,0 @@ -# SPDX-License-Identifier: BSD-3-Clause - -cmocka_test(drc_math_test - drc_math_test.c - ${PROJECT_SOURCE_DIR}/src/audio/drc/drc_math_generic.c - ${PROJECT_SOURCE_DIR}/src/audio/drc/drc_math_hifi3.c - ${PROJECT_SOURCE_DIR}/src/math/numbers.c - ${PROJECT_SOURCE_DIR}/src/math/exp_fcn.c - ${PROJECT_SOURCE_DIR}/src/math/exp_fcn_hifi.c -) - -target_include_directories(drc_math_test PRIVATE ${PROJECT_SOURCE_DIR}/src/audio) diff --git a/test/cmocka/src/audio/drc/drc_math_test.c b/test/cmocka/src/audio/drc/drc_math_test.c deleted file mode 100644 index 03b23577f0c7..000000000000 --- a/test/cmocka/src/audio/drc/drc_math_test.c +++ /dev/null @@ -1,148 +0,0 @@ -// SPDX-License-Identifier: BSD-3-Clause -// -// Copyright(c) 2024 Intel Corporation. - -#include -#include -#include -#include -#include -#include -#include -#include - -#include - -#define TEST_POINTS 500 - -#define ABS_DELTA_TOLERANCE_T1 1.0e-9 -#define REL_DELTA_TOLERANCE_T1 1.4e-4 -#define ABS_DELTA_TOLERANCE_T2 7.4e-7 -#define REL_DELTA_TOLERANCE_T2 7.5e-6 -#define ABS_DELTA_TOLERANCE_T3 3.8e-4 -#define REL_DELTA_TOLERANCE_T3 1.9e-6 - -static int32_t drc_inv_ref(int32_t x, int32_t precision_x, int32_t precision_y) -{ - double xf, yf, ys; - - xf = (double)x / ((int64_t)1 << precision_x); - yf = 1 / xf; - ys = round(yf * ((int64_t)1 << precision_y)); - if (ys > INT32_MAX) - ys = INT32_MAX; - - if (ys < INT32_MIN) - ys = INT32_MIN; - - return (int32_t)ys; -} - -static void drc_math_inv_fixed_test_helper(int32_t x_start, int32_t x_end, - int32_t x_step, int q_in, int q_out, - double abs_delta_tolerance, double rel_delta_tolerance) -{ - double fx, fy1, fy2; - double abs_delta, rel_delta; - int64_t x1; - int32_t x, y1, y2; - int32_t differences = 0; - double abs_delta_max = 0; - double rel_delta_max = 0; - - printf("%s: Testing q_in = %d q_out = %d in = %d:%d:%d\n", __func__, - q_in, q_out, x_start, x_step, x_end); - - x1 = x_start; - while (x1 <= x_end) { - x = (int32_t)x1; - y1 = drc_inv_ref(x, q_in, q_out); - y2 = drc_inv_fixed(x, q_in, q_out); - if (y1 != y2) - differences++; - - fx = (double)x / (1 << q_in); - fy1 = (double)y1 / (1 << q_out); - fy2 = (double)y2 / (1 << q_out); - abs_delta = fabs(fy1 - fy2); - rel_delta = abs_delta / fy1; - if (rel_delta > rel_delta_max) - rel_delta_max = rel_delta; - - if (abs_delta > abs_delta_max) - abs_delta_max = abs_delta; - - if (rel_delta > rel_delta_tolerance) { - printf("%s: Relative error %g exceeds limit %g, input %g output %g ref %g.\n", - __func__, rel_delta, rel_delta_tolerance, fx, fy2, fy1); - assert_true(false); - } - - if (abs_delta > abs_delta_tolerance) { - printf("%s: Absolute error %g exceeds limit %g, input %g output %g ref %g.\n", - __func__, abs_delta, abs_delta_tolerance, fx, fy2, fy1); - assert_true(false); - } - - x1 += x_step; - } - - printf("%s: bit exact differences count = %d\n", __func__, differences); - printf("%s: Absolute max error was %.6e.\n", __func__, abs_delta_max); - printf("%s: Relative max error was %.6e.\n", __func__, rel_delta_max); -} - -static void test_function_drc_inv_fixed_q12_q30(void **state) -{ - (void)state; - - int32_t x_start = 1; - int32_t x_end = INT32_MAX; - int32_t x_step = (int32_t)(((int64_t)x_end - (int64_t)x_start) / (TEST_POINTS - 1)); - int q_in = 12; - int q_out = 30; - - drc_math_inv_fixed_test_helper(x_start, x_end, x_step, q_in, q_out, - ABS_DELTA_TOLERANCE_T1, REL_DELTA_TOLERANCE_T1); -} - -static void test_function_drc_inv_fixed_q22_q26(void **state) -{ - (void)state; - - int32_t x_start = 1; - int32_t x_end = INT32_MAX; - int32_t x_step = (int32_t)(((int64_t)x_end - (int64_t)x_start) / TEST_POINTS); - int q_in = 22; - int q_out = 26; - - drc_math_inv_fixed_test_helper(x_start, x_end, x_step, q_in, q_out, - ABS_DELTA_TOLERANCE_T2, REL_DELTA_TOLERANCE_T2); -} - -static void test_function_drc_inv_fixed_q31_q20(void **state) -{ - (void)state; - - int32_t x_start = 1; - int32_t x_end = INT32_MAX; - int32_t x_step = (int32_t)(((int64_t)x_end - (int64_t)x_start) / TEST_POINTS); - int q_in = 31; - int q_out = 20; - - drc_math_inv_fixed_test_helper(x_start, x_end, x_step, q_in, q_out, - ABS_DELTA_TOLERANCE_T3, REL_DELTA_TOLERANCE_T3); -} - -int main(void) -{ - const struct CMUnitTest tests[] = { - cmocka_unit_test(test_function_drc_inv_fixed_q12_q30), - cmocka_unit_test(test_function_drc_inv_fixed_q22_q26), - cmocka_unit_test(test_function_drc_inv_fixed_q31_q20), - }; - - cmocka_set_message_output(CM_OUTPUT_TAP); - - return cmocka_run_group_tests(tests, NULL, NULL); -} From 6b5ac9292246f09712d8c50a66eaa185b79d2190 Mon Sep 17 00:00:00 2001 From: Liam Girdwood Date: Fri, 27 Nov 2026 15:53:14 +0000 Subject: [PATCH 10/11] test: audio: port pcm_float CMocka test to ztest Add a ztest suite for the generic float PCM conversions, covering the same nine cases as the CMocka pcm_float_generic test: S16_LE, S24_4LE (with and without sign extension) and S32_LE to float, and float to S16_LE, S24_4LE and S32_LE, including saturation of large and infinite values. Each case converts the same sample vectors from one component buffer to another with pcm_get_conversion_function() and checks every sample, and that the byte after the last sample is left untouched. The CMocka build links twenty two SOF sources and the notifier mocks. Only fourteen are needed here: the two PCM converter sources, component.c for the cir_buf_copy() used by pcm_converter.c, numbers.c, and the heap and buffer sources the buffer suite links. The pipeline, IPC3 helper, IPC common and data blob code and the notifier mocks are dropped. As in the CMocka build, PCM_CONVERTER_GENERIC is defined, because pcm_converter.h does not select an implementation under UNIT_TEST. CONFIG_FPU is not set on native_sim, so the conversions use the integer-only float code, as they did in the CMocka build. Adapted from the CMocka version: - The buffers come from buffer_new() directly. The dummy source and sink components that test/cmocka/src/util.h attached to them are not used by the conversion functions and are not allocated. - assert_float_equal() is replaced by a helper with the same rule: the values are equal if they differ by at most epsilon, or by at most FLT_EPSILON relative to the larger of them. The integer results of the two big value cases are compared as floats, as before. - The arrays that are scaled or masked in place are automatic instead of static, so a case gives the same result if it runs again. - Int constants that do not fit a float exactly are cast to float explicitly, as clang rejects the implicit conversion with -Werror,-Wimplicit-const-int-float-conversion. The values are the same. - The number of conversion functions is printed from the suite setup instead of main(). Co-Authored-By: Claude Opus 5.5 Signed-off-by: Liam Girdwood --- .../unit/audio/pcm_converter/CMakeLists.txt | 62 ++ test/ztest/unit/audio/pcm_converter/prj.conf | 19 + .../pcm_converter/test_pcm_float_ztest.c | 629 ++++++++++++++++++ .../unit/audio/pcm_converter/testcase.yaml | 25 + 4 files changed, 735 insertions(+) create mode 100644 test/ztest/unit/audio/pcm_converter/CMakeLists.txt create mode 100644 test/ztest/unit/audio/pcm_converter/prj.conf create mode 100644 test/ztest/unit/audio/pcm_converter/test_pcm_float_ztest.c create mode 100644 test/ztest/unit/audio/pcm_converter/testcase.yaml diff --git a/test/ztest/unit/audio/pcm_converter/CMakeLists.txt b/test/ztest/unit/audio/pcm_converter/CMakeLists.txt new file mode 100644 index 000000000000..48f9c0b95293 --- /dev/null +++ b/test/ztest/unit/audio/pcm_converter/CMakeLists.txt @@ -0,0 +1,62 @@ +# SPDX-License-Identifier: BSD-3-Clause +# +# Copyright(c) 2026 Intel Corporation. +# +# PCM converter float 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_pcm_converter) + +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 +) + +# pcm_converter.h does not pick an implementation under UNIT_TEST, select the +# generic C one as the CMocka pcm_float_generic target does. +target_compile_definitions(app PRIVATE + -DCONFIG_ZEPHYR_POSIX=1 + -DCONFIG_LIBRARY=1 + -DUNIT_TEST=1 + -DPCM_CONVERTER_GENERIC +) + +# 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_pcm_float_ztest.c + ${SOF_ROOT}/src/audio/pcm_converter/pcm_converter.c + ${SOF_ROOT}/src/audio/pcm_converter/pcm_converter_generic.c + # just_copy_*() in pcm_converter.c need cir_buf_copy() + ${SOF_ROOT}/src/audio/component.c + # the test converts between two component buffers + ${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/pcm_converter/prj.conf b/test/ztest/unit/audio/pcm_converter/prj.conf new file mode 100644 index 000000000000..0b8a4f43065c --- /dev/null +++ b/test/ztest/unit/audio/pcm_converter/prj.conf @@ -0,0 +1,19 @@ +# SPDX-License-Identifier: BSD-3-Clause +# +# Copyright(c) 2026 Intel Corporation. + +CONFIG_ZTEST=y +CONFIG_SOF_FULL_ZEPHYR_APPLICATION=n +# The suite prints float values (%e, %f); without this, printk silently emits +# the format string literally instead of the value. +CONFIG_CBPRINTF_FP_SUPPORT=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 +# The CMocka test was built only with CONFIG_FORMAT_FLOAT, which is the +# native_sim default; make it explicit. +CONFIG_FORMAT_FLOAT=y diff --git a/test/ztest/unit/audio/pcm_converter/test_pcm_float_ztest.c b/test/ztest/unit/audio/pcm_converter/test_pcm_float_ztest.c new file mode 100644 index 000000000000..c0bb3ee76b59 --- /dev/null +++ b/test/ztest/unit/audio/pcm_converter/test_pcm_float_ztest.c @@ -0,0 +1,629 @@ +// SPDX-License-Identifier: BSD-3-Clause +// +// Copyright(c) 2026 Intel Corporation. +// +// Converted from CMock to Ztest +// Original tests from test/cmocka/src/audio/pcm_converter/pcm_float.c: +// Author: Karol Trzcinski + +#include + +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +/* used during float assertions */ +#define EPSILON 0.01f + +/* base number to check combinations for each data format */ +#define PCM_TEST_INT_NUMBERS \ + -0, 0, 0, 0, -1, 1, -2, 2, \ + -3, 3, -4, 4, -5, 5, -6, 6, -7, 7, -25, 25, -57, 57, -100, 100 +#define PCM_TEST_FLOAT_NUMBERS \ + -0, 0, 0.1, -0.1f, -0.8f, 0.8f, -1.9f, 1.9f, \ + -3, 3, -4, 4, -5, 5, -6, 6, -7, 7, -25, 25, -57, 57, -100, 100 + +/* generate biggest signed value on a 24 bits number */ +#define INT24_MAX ((1 << (24 - 1)) - 1) + +/* generate smallest signed value on a 24 bits number */ +#define INT24_MIN (-INT24_MAX - 1) + +/* conversion ratio */ +static const float ratio16 = 1.f / (1u << 15); +static const float ratio24 = 1.f / (1u << 23); +static const float ratio32 = 1.f / (1u << 31); + +/** + * @brief Compare two floats like the CMocka assert_float_equal() + * + * The values are equal if they differ by at most epsilon, or by at most + * FLT_EPSILON relative to the larger magnitude of the two. + */ +static bool float_equal(float left, float right, float epsilon) +{ + float diff = fabsf(left - right); + float largest; + + if (diff <= epsilon) + return true; + + largest = fabsf(right) > fabsf(left) ? fabsf(right) : fabsf(left); + + return diff <= largest * FLT_EPSILON; +} + +/** + * @brief Print an int sample and the float it converts from or to + * @param[in] it Int value. + * @param[in] fl Float value, printed also as its bits, mantissa and exponent. + * @param[in] ep Expected value. + * @param[in] func_name Name of the calling test. + */ +static void pcm_float_print_values(int32_t it, const float *fl, double ep, + const char *func_name) +{ + const uint32_t mantissa_mask = (1 << 23) - 1; + uint32_t bits; + int32_t ex; + uint32_t mt; + float mantissa_value; + + memcpy(&bits, fl, sizeof(bits)); + ex = (int32_t)((bits >> 23) & 0xFF) - 127; + mt = bits & mantissa_mask; + mantissa_value = mt * (1.0f / mantissa_mask) + 1.f; + + printk("%s: 0x%08Xf => %.3f * 2**%3d = %10.3e f <=> %7d d \t(expected: %10.3e)\n", + func_name, bits, (double)mantissa_value, ex, (double)*fl, it, ep); +} + +/** + * @brief Allocate a component buffer for the given frame format + * @param[in] frame_fmt Frame format of the stream. + * @param[in] size Buffer size in bytes. + * @return The new buffer, never NULL. + */ +static struct comp_buffer *test_buffer_new(enum sof_ipc_frame frame_fmt, 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); + + memset(buf->stream.addr, 0, size); + audio_stream_set_frm_fmt(&buf->stream, frame_fmt); + audio_stream_set_channels(&buf->stream, 1); + + return buf; +} + +/** + * @brief Convert samples between two formats with the PCM converter + * + * The sink is one sample larger than needed, its last byte is checked to be + * left untouched by the conversion. + * + * @param[in] frm_in Source frame format. + * @param[in] frm_out Sink frame format. + * @param[in] samples Number of samples to convert. + * @param[in] data Source samples. + * @return The sink buffer with the converted samples, to be freed by the caller. + */ +static struct comp_buffer *test_pcm_convert(enum sof_ipc_frame frm_in, enum sof_ipc_frame frm_out, + int samples, const void *data) +{ + struct comp_buffer *source; + struct comp_buffer *sink; + pcm_converter_func fun; + const uint8_t fillval = 0xAB; + const int inbytes = samples * get_sample_bytes(frm_in); + const int outbytes = (samples + 1) * get_sample_bytes(frm_out); + + /* create buffers - output buffer may be too big for test */ + source = test_buffer_new(frm_in, inbytes); + sink = test_buffer_new(frm_out, outbytes); + + /* fill source */ + memcpy_s(source->stream.w_ptr, source->stream.size, data, inbytes); + audio_stream_produce(&source->stream, inbytes); + + /* fill sink memory - to validate last value */ + memset(sink->stream.w_ptr, fillval, outbytes); + + /* run conversion */ + fun = pcm_get_conversion_function(frm_in, frm_out); + zassert_not_null(fun, "No conversion function from format %d to %d", frm_in, frm_out); + + struct cir_buf_source cir_src = { + .buf_start = source->stream.addr, + .buf_end = source->stream.end_addr, + .ptr = source->stream.r_ptr, + }; + struct cir_buf_sink cir_snk = { + .buf_start = sink->stream.addr, + .buf_end = sink->stream.end_addr, + .ptr = sink->stream.w_ptr, + }; + + fun(&cir_src, audio_stream_get_channels(&source->stream), + &cir_snk, audio_stream_get_channels(&sink->stream), samples, DUMMY_CHMAP); + + /* assert last value in sink is untouched */ + zassert_equal(((uint8_t *)sink->stream.w_ptr)[outbytes - 1], fillval, + "Conversion wrote past the last sample"); + + /* free source and return sink */ + buffer_free(source); + return sink; +} + +static void mask_array(int32_t mask, int32_t *parray, int cnt) +{ + zassert_not_null(parray); + while (cnt--) + parray[cnt] &= mask; +} + +static void scale_array(float ratio, float *parray, int cnt) +{ + zassert_not_null(parray); + while (cnt--) + parray[cnt] *= ratio; +} + +#if CONFIG_FORMAT_FLOAT && CONFIG_FORMAT_S16LE +/** + * @brief Test S16_LE to float conversion + */ +ZTEST(audio_pcm_float_suite, test_pcm_convert_s16_to_f) +{ + typedef int16_t Tin; + typedef float Tout; + static const Tin source_buf[] = { + PCM_TEST_INT_NUMBERS, + INT16_MIN + 1, INT16_MIN, + INT16_MAX - 1, INT16_MAX, + }; + Tout expected_buf[] = { + PCM_TEST_INT_NUMBERS, + INT16_MIN + 1, INT16_MIN, + INT16_MAX - 1, INT16_MAX, + }; + + struct comp_buffer *sink; + int i, N = ARRAY_SIZE(source_buf); + Tout *read_val; + + zassert_equal(ARRAY_SIZE(source_buf), ARRAY_SIZE(expected_buf)); + scale_array(ratio16, expected_buf, N); + + /* run test */ + sink = test_pcm_convert(SOF_IPC_FRAME_S16_LE, SOF_IPC_FRAME_FLOAT, + N, source_buf); + + /* check results */ + for (i = 0; i < N; ++i) { + read_val = audio_stream_read_frag(&sink->stream, i, sizeof(Tout)); + printk("%2d/%02d ", i + 1, N); + pcm_float_print_values(source_buf[i], read_val, (double)expected_buf[i], + __func__); + zassert_true(float_equal(*read_val, expected_buf[i], EPSILON), + "sample %d: %e != %e", i, (double)*read_val, + (double)expected_buf[i]); + } + + /* free memory */ + buffer_free(sink); +} + +/** + * @brief Test float to S16_LE conversion + */ +ZTEST(audio_pcm_float_suite, test_pcm_convert_f_to_s16) +{ + typedef float Tin; + typedef int16_t Tout; + Tin source_buf[] = { + PCM_TEST_FLOAT_NUMBERS, + INT16_MIN + 1, INT16_MIN, + INT16_MAX - 1, INT16_MAX, + }; + static const Tout expected_buf[] = { + PCM_TEST_INT_NUMBERS, + INT16_MIN + 1, INT16_MIN, + INT16_MAX - 1, INT16_MAX, + }; + + struct comp_buffer *sink; + int i, N = ARRAY_SIZE(source_buf); + Tout *read_val; + + zassert_equal(ARRAY_SIZE(source_buf), ARRAY_SIZE(expected_buf)); + scale_array(ratio16, source_buf, N); + + /* run test */ + sink = test_pcm_convert(SOF_IPC_FRAME_FLOAT, SOF_IPC_FRAME_S16_LE, + N, source_buf); + + /* check results */ + for (i = 0; i < N; ++i) { + read_val = audio_stream_read_frag(&sink->stream, i, sizeof(Tout)); + printk("%2d/%02d ", i + 1, N); + pcm_float_print_values(*read_val, &source_buf[i], (double)expected_buf[i], + __func__); + zassert_equal(*read_val, expected_buf[i], "sample %d: %d != %d", i, + *read_val, expected_buf[i]); + } + + /* free memory */ + buffer_free(sink); +} +#endif /* CONFIG_FORMAT_FLOAT && CONFIG_FORMAT_S16LE */ + +#if CONFIG_FORMAT_FLOAT && CONFIG_FORMAT_S24LE +/** + * @brief Test S24_4LE to float conversion of sign extended samples + */ +ZTEST(audio_pcm_float_suite, test_pcm_convert_s24_in_s32_to_f) +{ + typedef int32_t Tin; + typedef float Tout; + static const Tin source_buf[] = { + PCM_TEST_INT_NUMBERS, + INT16_MIN + 1, INT16_MIN, + INT16_MAX - 1, INT16_MAX, + INT24_MIN + 1, INT24_MIN, + INT24_MAX - 1, INT24_MAX, + }; + Tout expected_buf[] = { + PCM_TEST_INT_NUMBERS, + INT16_MIN + 1, INT16_MIN, + INT16_MAX - 1, INT16_MAX, + INT24_MIN + 1, INT24_MIN, + INT24_MAX - 1, INT24_MAX, + }; + + struct comp_buffer *sink; + int i, N = ARRAY_SIZE(source_buf); + Tout *read_val; + + zassert_equal(ARRAY_SIZE(source_buf), ARRAY_SIZE(expected_buf)); + scale_array(ratio24, expected_buf, N); + + /* run test */ + sink = test_pcm_convert(SOF_IPC_FRAME_S24_4LE, SOF_IPC_FRAME_FLOAT, + N, source_buf); + + /* check results */ + for (i = 0; i < N; ++i) { + read_val = audio_stream_read_frag(&sink->stream, i, sizeof(Tout)); + printk("%2d/%02d ", i + 1, N); + pcm_float_print_values(source_buf[i], read_val, (double)expected_buf[i], + __func__); + zassert_true(float_equal(*read_val, expected_buf[i], EPSILON), + "sample %d: %e != %e", i, (double)*read_val, + (double)expected_buf[i]); + } + + /* free memory */ + buffer_free(sink); +} + +/** + * @brief Test S24_4LE to float conversion of samples with a zero top byte + */ +ZTEST(audio_pcm_float_suite, test_pcm_convert_s24_to_f) +{ + typedef int32_t Tin; + typedef float Tout; + Tin source_buf[] = { + PCM_TEST_INT_NUMBERS, + INT16_MIN + 1, INT16_MIN, + INT16_MAX - 1, INT16_MAX, + INT24_MIN + 1, INT24_MIN, + INT24_MAX - 1, INT24_MAX, + }; + Tout expected_buf[] = { + PCM_TEST_INT_NUMBERS, + INT16_MIN + 1, INT16_MIN, + INT16_MAX - 1, INT16_MAX, + INT24_MIN + 1, INT24_MIN, + INT24_MAX - 1, INT24_MAX, + }; + + struct comp_buffer *sink; + int i, N = ARRAY_SIZE(source_buf); + Tout *read_val; + + zassert_equal(ARRAY_SIZE(source_buf), ARRAY_SIZE(expected_buf)); + mask_array(MASK(24, 0), source_buf, N); + scale_array(ratio24, expected_buf, N); + + /* run test */ + sink = test_pcm_convert(SOF_IPC_FRAME_S24_4LE, SOF_IPC_FRAME_FLOAT, + N, source_buf); + + /* check results */ + for (i = 0; i < N; ++i) { + read_val = audio_stream_read_frag(&sink->stream, i, sizeof(Tout)); + printk("%2d/%02d ", i + 1, N); + pcm_float_print_values(sign_extend_s24(source_buf[i]), read_val, + (double)expected_buf[i], __func__); + zassert_true(float_equal(*read_val, expected_buf[i], EPSILON), + "sample %d: %e != %e", i, (double)*read_val, + (double)expected_buf[i]); + } + + /* free memory */ + buffer_free(sink); +} + +/** + * @brief Test float to S24_4LE conversion, including saturation + */ +ZTEST(audio_pcm_float_suite, test_pcm_convert_f_to_s24) +{ + typedef float Tin; + typedef int32_t Tout; + Tin source_buf[] = { + PCM_TEST_FLOAT_NUMBERS, + INT16_MIN + 1, INT16_MIN, + INT16_MAX - 1, INT16_MAX, + INT24_MIN + 1, INT24_MIN, + INT24_MAX - 1, INT24_MAX, + INT24_MIN - 1, INT24_MAX + 1, + }; + static const Tout expected_buf[] = { + PCM_TEST_INT_NUMBERS, + INT16_MIN + 1, INT16_MIN, + INT16_MAX - 1, INT16_MAX, + INT24_MIN + 1, INT24_MIN, + INT24_MAX - 1, INT24_MAX, + INT24_MIN, INT24_MAX, + }; + + struct comp_buffer *sink; + int i, N = ARRAY_SIZE(source_buf); + Tout *read_val; + + zassert_equal(ARRAY_SIZE(source_buf), ARRAY_SIZE(expected_buf)); + scale_array(ratio24, source_buf, N); + + /* run test */ + sink = test_pcm_convert(SOF_IPC_FRAME_FLOAT, SOF_IPC_FRAME_S24_4LE, + N, source_buf); + + /* check results */ + for (i = 0; i < N; ++i) { + read_val = audio_stream_read_frag(&sink->stream, i, sizeof(Tout)); + printk("%2d/%02d ", i + 1, N); + pcm_float_print_values(*read_val, &source_buf[i], (double)expected_buf[i], + __func__); + zassert_equal(*read_val, expected_buf[i], "sample %d: %d != %d", i, + *read_val, expected_buf[i]); + } + + /* free memory */ + buffer_free(sink); +} +#endif /* CONFIG_FORMAT_FLOAT && CONFIG_FORMAT_S24LE */ + +#if CONFIG_FORMAT_FLOAT && CONFIG_FORMAT_S32LE +#define S24TOS32MULT (1 << (32 - 24)) +/** + * @brief Test S32_LE to float conversion + */ +ZTEST(audio_pcm_float_suite, test_pcm_convert_s32_to_f) +{ + typedef int32_t Tin; + typedef float Tout; + + static const Tin source_buf[] = { + PCM_TEST_INT_NUMBERS, + INT16_MIN + 1, INT16_MIN, + INT16_MAX - 1, INT16_MAX, + INT24_MIN + 1, INT24_MIN, + INT24_MAX - 1, INT24_MAX, + INT32_MIN + 1, INT32_MIN, + INT32_MAX - 1, INT32_MAX, + }; + Tout expected_buf[] = { + PCM_TEST_INT_NUMBERS, + INT16_MIN + 1, INT16_MIN, + INT16_MAX - 1, INT16_MAX, + INT24_MIN + 1, INT24_MIN, + INT24_MAX - 1, INT24_MAX, + INT24_MIN * S24TOS32MULT, INT24_MIN * S24TOS32MULT, + INT24_MAX * S24TOS32MULT, INT24_MAX * S24TOS32MULT, + }; + + struct comp_buffer *sink; + int i, N = ARRAY_SIZE(source_buf); + Tout *read_val; + + zassert_equal(ARRAY_SIZE(source_buf), ARRAY_SIZE(expected_buf)); + scale_array(ratio32, expected_buf, N); + + /* run test */ + sink = test_pcm_convert(SOF_IPC_FRAME_S32_LE, SOF_IPC_FRAME_FLOAT, + N, source_buf); + + /* check results */ + for (i = 0; i < N; ++i) { + read_val = audio_stream_read_frag(&sink->stream, i, sizeof(Tout)); + printk("%2d/%02d ", i + 1, N); + pcm_float_print_values(source_buf[i], read_val, (double)expected_buf[i], + __func__); + zassert_true(float_equal(*read_val, expected_buf[i], EPSILON), + "sample %d: %e != %e", i, (double)*read_val, + (double)expected_buf[i]); + } + + /* free memory */ + buffer_free(sink); +} + +/** + * @brief Test float to S32_LE conversion + */ +ZTEST(audio_pcm_float_suite, test_pcm_convert_f_to_s32) +{ + typedef float Tin; + typedef int32_t Tout; + Tin source_buf[] = { + PCM_TEST_FLOAT_NUMBERS, + INT16_MIN + 1, INT16_MIN, + INT16_MAX - 1, INT16_MAX, + INT24_MIN + 1, INT24_MIN, + INT24_MAX - 1, INT24_MAX, + }; + static const Tout expected_buf[] = { + PCM_TEST_INT_NUMBERS, + INT16_MIN + 1, INT16_MIN, + INT16_MAX - 1, INT16_MAX, + INT24_MIN + 1, INT24_MIN, + INT24_MAX - 1, INT24_MAX, + }; + + struct comp_buffer *sink; + int i, N = ARRAY_SIZE(source_buf); + Tout *read_val; + + zassert_equal(ARRAY_SIZE(source_buf), ARRAY_SIZE(expected_buf)); + scale_array(ratio32, source_buf, N); + + /* run test */ + sink = test_pcm_convert(SOF_IPC_FRAME_FLOAT, SOF_IPC_FRAME_S32_LE, + N, source_buf); + + /* check results */ + for (i = 0; i < N; ++i) { + read_val = audio_stream_read_frag(&sink->stream, i, sizeof(Tout)); + printk("%2d/%02d ", i + 1, N); + pcm_float_print_values(*read_val, &source_buf[i], (double)expected_buf[i], + __func__); + zassert_equal(*read_val, expected_buf[i], "sample %d: %d != %d", i, + *read_val, expected_buf[i]); + } + + /* free memory */ + buffer_free(sink); +} + +/** + * @brief Test float to S32_LE conversion of large negative values + */ +ZTEST(audio_pcm_float_suite, test_pcm_convert_f_to_s32_big_neg) +{ + typedef float Tin; + typedef int32_t Tout; + Tin source_buf[] = { + INT24_MIN - 127, INT24_MIN - 128, /* 24B mantissa trimming */ + /* 24B mantissa trimming, explicit casts as clang rejects implicit lossy ones */ + (float)(INT32_MIN + 1), INT32_MIN, + INT32_MIN * 2.f, INT32_MIN * 10.f, + -INFINITY + }; + static const Tout expected_buf[] = { + INT24_MIN - 127, INT24_MIN - 128, + INT32_MIN + 1, INT32_MIN, + INT32_MIN, INT32_MIN, + INT32_MIN + }; + + struct comp_buffer *sink; + int i, N = ARRAY_SIZE(source_buf); + Tout *read_val; + + zassert_equal(ARRAY_SIZE(source_buf), ARRAY_SIZE(expected_buf)); + scale_array(ratio32, source_buf, N); + + /* run test */ + sink = test_pcm_convert(SOF_IPC_FRAME_FLOAT, SOF_IPC_FRAME_S32_LE, + N, source_buf); + + /* check results */ + for (i = 0; i < N; ++i) { + read_val = audio_stream_read_frag(&sink->stream, i, sizeof(Tout)); + printk("%2d/%02d ", i + 1, N); + pcm_float_print_values(*read_val, &source_buf[i], (double)expected_buf[i], + __func__); + /* assert as float because of possible rounding effects */ + zassert_true(float_equal((float)*read_val, (float)expected_buf[i], 1), + "sample %d: %d != %d", i, *read_val, expected_buf[i]); + } + + /* free memory */ + buffer_free(sink); +} + +/** + * @brief Test float to S32_LE conversion of large positive values + */ +ZTEST(audio_pcm_float_suite, test_pcm_convert_f_to_s32_big_pos) +{ + typedef float Tin; + typedef int32_t Tout; + Tin source_buf[] = { + INT24_MAX + 127, INT24_MAX + 128, + INT32_MAX - 255, INT32_MAX - 127, + /* 24B mantissa trimming, explicit casts as clang rejects implicit lossy ones */ + (float)(INT32_MAX - 1), (float)INT32_MAX, + (float)INT32_MAX * 2.f, (float)INT32_MAX * 10.f, + INFINITY, + }; + /* remember about 24B mantissa trimming */ + static const Tout expected_buf[] = { + INT24_MAX + 127, INT24_MAX + 128, + INT32_MAX - 254, INT32_MAX - 126, + INT32_MAX - 1, INT32_MAX, + INT32_MAX, INT32_MAX, + INT32_MAX, + }; + + struct comp_buffer *sink; + int i, N = ARRAY_SIZE(source_buf); + Tout *read_val; + + zassert_equal(ARRAY_SIZE(source_buf), ARRAY_SIZE(expected_buf)); + scale_array(ratio32, source_buf, N); + + /* run test */ + sink = test_pcm_convert(SOF_IPC_FRAME_FLOAT, SOF_IPC_FRAME_S32_LE, + N, source_buf); + + /* check results */ + for (i = 0; i < N; ++i) { + read_val = audio_stream_read_frag(&sink->stream, i, sizeof(Tout)); + printk("%2d/%02d ", i + 1, N); + pcm_float_print_values(*read_val, &source_buf[i], (double)expected_buf[i], + __func__); + /* assert as float because of possible rounding effects */ + zassert_true(float_equal((float)*read_val, (float)expected_buf[i], 1), + "sample %d: %d != %d", i, *read_val, expected_buf[i]); + } + + /* free memory */ + buffer_free(sink); +} +#endif /* CONFIG_FORMAT_FLOAT && CONFIG_FORMAT_S32LE */ + +static void *audio_pcm_float_setup(void) +{ + /* log number of converting functions for current configuration */ + printk("%s start tests, count(pcm_func_map)=%zu\n", __FILE__, pcm_func_count); + + return NULL; +} + +ZTEST_SUITE(audio_pcm_float_suite, NULL, audio_pcm_float_setup, NULL, NULL, NULL); diff --git a/test/ztest/unit/audio/pcm_converter/testcase.yaml b/test/ztest/unit/audio/pcm_converter/testcase.yaml new file mode 100644 index 000000000000..7892d8b11b21 --- /dev/null +++ b/test/ztest/unit/audio/pcm_converter/testcase.yaml @@ -0,0 +1,25 @@ +# SPDX-License-Identifier: BSD-3-Clause +# +# Copyright(c) 2026 Intel Corporation. +# +# PCM converter float 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 + - pcm_converter + integration_platforms: + - native_sim + arch_exclude: xtensa + +tests: + sof.unit.audio.pcm_converter: + platform_allow: + - native_sim + build_only: false From 5e9d77c75714b4e9e48092d6ce6009c6a1976170 Mon Sep 17 00:00:00 2001 From: Liam Girdwood Date: Fri, 27 Nov 2026 15:53:33 +0000 Subject: [PATCH 11/11] cmocka: Remove pcm_float test This patch removes the pcm_float_generic test that has been successfully converted to the new framework (from cmock to ztest) in test/ztest/unit/audio/pcm_converter. It was the only test in test/cmocka/src/audio/pcm_converter, so the directory and its add_subdirectory() entry are removed too. The test is built only with CONFIG_FORMAT_FLOAT, which since commit 856cf1327 ("kconfig: audio: gate CONFIG_FORMAT_FLOAT on hardware floating-point support") depends on SOF_HAS_FLOAT_HW. Neither CPU_HAS_FPU nor XTENSA exists in the CMocka Kconfig, so no CMocka defconfig has built or run this test since then. The ztest suite runs it again on native_sim, where CPU_HAS_FPU is set. Code coverage testing did not reveal any regression. The CMocka numbers were measured with SOF_HAS_FLOAT_HW forced on in a local, uncommitted Kconfig change. CMock stats for pcm_converter.c, pcm_converter_generic.c (host gcc, unit_test_defconfig) - lines: 34.9 % (101/289) - branches: 33.0 % (35/106) Ztest stats for pcm_converter.c, pcm_converter_generic.c (native_sim, host LLVM) - lines: 32.9 % (96/292) - branches: 30.0 % (30/100) Every line covered by the CMocka test is also covered by the ztest suite; the lines the ztest build does not report are variable declarations that LLVM attributes no code to. The totals differ because gcc and LLVM count lines and branches differently. Co-Authored-By: Claude Opus 5.5 Signed-off-by: Liam Girdwood --- test/cmocka/src/audio/CMakeLists.txt | 1 - .../src/audio/pcm_converter/CMakeLists.txt | 33 - .../src/audio/pcm_converter/pcm_float.c | 562 ------------------ 3 files changed, 596 deletions(-) delete mode 100644 test/cmocka/src/audio/pcm_converter/CMakeLists.txt delete mode 100644 test/cmocka/src/audio/pcm_converter/pcm_float.c diff --git a/test/cmocka/src/audio/CMakeLists.txt b/test/cmocka/src/audio/CMakeLists.txt index 321ce64282b8..ea0b424069b1 100644 --- a/test/cmocka/src/audio/CMakeLists.txt +++ b/test/cmocka/src/audio/CMakeLists.txt @@ -1,7 +1,6 @@ # SPDX-License-Identifier: BSD-3-Clause add_subdirectory(component) -add_subdirectory(pcm_converter) if(CONFIG_COMP_MIXER) add_subdirectory(mixer) endif() diff --git a/test/cmocka/src/audio/pcm_converter/CMakeLists.txt b/test/cmocka/src/audio/pcm_converter/CMakeLists.txt deleted file mode 100644 index ee72311e004b..000000000000 --- a/test/cmocka/src/audio/pcm_converter/CMakeLists.txt +++ /dev/null @@ -1,33 +0,0 @@ -# SPDX-License-Identifier: BSD-3-Clause - -if(CONFIG_FORMAT_FLOAT) - cmocka_test(pcm_float_generic - pcm_float.c - ${PROJECT_SOURCE_DIR}/src/math/numbers.c - ${PROJECT_SOURCE_DIR}/src/audio/pcm_converter/pcm_converter.c - ${PROJECT_SOURCE_DIR}/src/audio/pcm_converter/pcm_converter_generic.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/component.c - ${PROJECT_SOURCE_DIR}/src/audio/data_blob.c - ${PROJECT_SOURCE_DIR}/src/module/audio/source_api.c - ${PROJECT_SOURCE_DIR}/src/module/audio/sink_api.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/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 - ) - target_include_directories(pcm_float_generic PRIVATE ${PROJECT_SOURCE_DIR}/src/include) - target_compile_definitions(pcm_float_generic PRIVATE PCM_CONVERTER_GENERIC) - target_link_libraries(pcm_float_generic PRIVATE sof_options) -endif() diff --git a/test/cmocka/src/audio/pcm_converter/pcm_float.c b/test/cmocka/src/audio/pcm_converter/pcm_float.c deleted file mode 100644 index 0fed36a98774..000000000000 --- a/test/cmocka/src/audio/pcm_converter/pcm_float.c +++ /dev/null @@ -1,562 +0,0 @@ -// SPDX-License-Identifier: BSD-3-Clause -// -// Copyright(c) 2018 Intel Corporation. All rights reserved. -// -// Author: Karol Trzcinski - -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include - -#ifdef HAVE_MALLOC_H -#include -#else -#include -#endif - -#include "../../util.h" - -/* used during float assertions */ -#define EPSILON 0.01f - -/* base number to check combinations for each data format */ -#define PCM_TEST_INT_NUMBERS \ - -0, 0, 0, 0, -1, 1, -2, 2, \ - -3, 3, -4, 4, -5, 5, -6, 6, -7, 7, -25, 25, -57, 57, -100, 100 -#define PCM_TEST_FLOAT_NUMBERS \ - -0, 0, 0.1, -0.1f, -0.8f, 0.8f, -1.9f, 1.9f, \ - -3, 3, -4, 4, -5, 5, -6, 6, -7, 7, -25, 25, -57, 57, -100, 100 - -/* generate biggest signed value on a 24 bits number */ -#define INT24_MAX ((1 << (24 - 1)) - 1) - -/* generate smallest signed value on a 24 bits number */ -#define INT24_MIN (-INT24_MAX - 1) - -/* conversion ration */ -const float ratio16 = 1.f / (1u << 15); -const float ratio24 = 1.f / (1u << 23); -const float ratio32 = 1.f / (1u << 31); - -/* - * Function design to help debugging conversion between fixed and float numbers. - * - * Usage: - * add declaration of this function for example to pcm_converter_generic.c - * And feel free to use pcm_float_print_values in functions like - * _pcm_convert_i_to_f or _pcm_convert_f_to_i during debugging - * - * param it int number to show - * param fl pointer to float number to show - * param ex expected number - */ -static void pcm_float_print_values(int32_t it, int32_t *fl, double ep, - const char *func_name) -{ - const int32_t mantissa_mask = (1 << 23) - 1; - int32_t ex = ((*fl >> 23) & 0xFF) - 127; - int32_t mt = *fl & mantissa_mask; - float mantissa_value = mt * (1.0f / mantissa_mask) + 1.f; - - print_message("%s: 0x%08Xf => %.3f * 2**%3d = %10.3e f <=> %7d d \t(expected: %10.3e)\n", - func_name, *fl, mantissa_value, ex, *((float *)fl), it, - ep); -} - -static struct comp_buffer *_test_pcm_convert(enum sof_ipc_frame frm_in, enum sof_ipc_frame frm_out, - int samples, const void *data) -{ - /* create buffers - output buffer may be too big for test */ - struct comp_buffer *source; - struct comp_buffer *sink; - pcm_converter_func fun; - const uint8_t fillval = 0xAB; - const int inbytes = samples * get_sample_bytes(frm_in); - const int outbytes = (samples + 1) * get_sample_bytes(frm_out); - - /* create buffers */ - source = create_test_source(NULL, 0, frm_in, 1, inbytes); - sink = create_test_sink(NULL, 0, frm_out, 1, outbytes); - - /* fill source */ - memcpy_s(source->stream.w_ptr, source->stream.size, data, inbytes); - audio_stream_produce(&source->stream, inbytes); - - /* fill sink memory - to validate last value */ - memset(sink->stream.w_ptr, fillval, outbytes); - - /* run conversion */ - fun = pcm_get_conversion_function(frm_in, frm_out); - assert_non_null(fun); - - struct cir_buf_source cir_src = { - .buf_start = source->stream.addr, - .buf_end = source->stream.end_addr, - .ptr = source->stream.r_ptr, - }; - struct cir_buf_sink cir_snk = { - .buf_start = sink->stream.addr, - .buf_end = sink->stream.end_addr, - .ptr = sink->stream.w_ptr, - }; - - fun(&cir_src, audio_stream_get_channels(&source->stream), - &cir_snk, audio_stream_get_channels(&sink->stream), samples, DUMMY_CHMAP); - - /* assert last value in sink is untouched */ - assert_int_equal(((uint8_t *)sink->stream.w_ptr)[outbytes - 1], fillval); - - /* free source and return sink */ - free_test_source(source); - return sink; -} - -static void mask_array(int32_t mask, int32_t *parray, int cnt) -{ - assert_non_null(parray); - while (cnt--) - parray[cnt] &= mask; -} - -static void scale_array(float ratio, float *parray, int cnt) -{ - assert_non_null(parray); - while (cnt--) - parray[cnt] *= ratio; -} - -#if CONFIG_FORMAT_FLOAT && CONFIG_FORMAT_S16LE -static void test_pcm_convert_s16_to_f(void **state) -{ - typedef int16_t Tin; - typedef float Tout; - static const Tin source_buf[] = { - PCM_TEST_INT_NUMBERS, - INT16_MIN + 1, INT16_MIN, - INT16_MAX - 1, INT16_MAX, - }; - static Tout expected_buf[] = { - PCM_TEST_INT_NUMBERS, - INT16_MIN + 1, INT16_MIN, - INT16_MAX - 1, INT16_MAX, - }; - - struct comp_buffer *sink; - int i, N = ARRAY_SIZE(source_buf); - Tout *read_val; - - assert_int_equal(ARRAY_SIZE(source_buf), ARRAY_SIZE(expected_buf)); - scale_array(ratio16, expected_buf, N); - - /* run test */ - sink = _test_pcm_convert(SOF_IPC_FRAME_S16_LE, SOF_IPC_FRAME_FLOAT, - N, source_buf); - - /* check results */ - for (i = 0; i < N; ++i) { - read_val = audio_stream_read_frag(&sink->stream, i, sizeof(Tout)); - print_message("%2d/%02d ", i + 1, N); - pcm_float_print_values(source_buf[i], (int32_t *)read_val, - expected_buf[i], __func__); - assert_float_equal(*read_val, expected_buf[i], EPSILON); - } - - /* free memory */ - free_test_sink(sink); -} - -static void test_pcm_convert_f_to_s16(void **state) -{ - typedef float Tin; - typedef int16_t Tout; - static Tin source_buf[] = { - PCM_TEST_FLOAT_NUMBERS, - INT16_MIN + 1, INT16_MIN, - INT16_MAX - 1, INT16_MAX, - }; - static const Tout expected_buf[] = { - PCM_TEST_INT_NUMBERS, - INT16_MIN + 1, INT16_MIN, - INT16_MAX - 1, INT16_MAX, - }; - - struct comp_buffer *sink; - int i, N = ARRAY_SIZE(source_buf); - Tout *read_val; - - assert_int_equal(ARRAY_SIZE(source_buf), ARRAY_SIZE(expected_buf)); - scale_array(ratio16, source_buf, N); - - /* run test */ - sink = _test_pcm_convert(SOF_IPC_FRAME_FLOAT, SOF_IPC_FRAME_S16_LE, - N, source_buf); - - /* check results */ - for (i = 0; i < N; ++i) { - read_val = audio_stream_read_frag(&sink->stream, i, sizeof(Tout)); - print_message("%2d/%02d ", i + 1, N); - pcm_float_print_values(*read_val, (int32_t *)&source_buf[i], - (float)expected_buf[i], __func__); - assert_int_equal(*read_val, expected_buf[i]); - } - - /* free memory */ - free_test_sink(sink); -} -#endif /* CONFIG_FORMAT_FLOAT && CONFIG_FORMAT_S16LE */ - -#if CONFIG_FORMAT_FLOAT && CONFIG_FORMAT_S24LE -static void test_pcm_convert_s24_in_s32_to_f(void **state) -{ - typedef int32_t Tin; - typedef float Tout; - static const Tin source_buf[] = { - PCM_TEST_INT_NUMBERS, - INT16_MIN + 1, INT16_MIN, - INT16_MAX - 1, INT16_MAX, - INT24_MIN + 1, INT24_MIN, - INT24_MAX - 1, INT24_MAX, - }; - static Tout expected_buf[] = { - PCM_TEST_INT_NUMBERS, - INT16_MIN + 1, INT16_MIN, - INT16_MAX - 1, INT16_MAX, - INT24_MIN + 1, INT24_MIN, - INT24_MAX - 1, INT24_MAX, - }; - - struct comp_buffer *sink; - int i, N = ARRAY_SIZE(source_buf); - Tout *read_val; - - assert_int_equal(ARRAY_SIZE(source_buf), ARRAY_SIZE(expected_buf)); - scale_array(ratio24, expected_buf, N); - - /* run test */ - sink = _test_pcm_convert(SOF_IPC_FRAME_S24_4LE, SOF_IPC_FRAME_FLOAT, - N, source_buf); - - /* check results */ - for (i = 0; i < N; ++i) { - read_val = audio_stream_read_frag(&sink->stream, i, sizeof(Tout)); - print_message("%2d/%02d ", i + 1, N); - pcm_float_print_values(source_buf[i], (int32_t *)read_val, - expected_buf[i], __func__); - assert_float_equal(*read_val, expected_buf[i], EPSILON); - } - - /* free memory */ - free_test_sink(sink); -} - -static void test_pcm_convert_s24_to_f(void **state) -{ - typedef int32_t Tin; - typedef float Tout; - static Tin source_buf[] = { - PCM_TEST_INT_NUMBERS, - INT16_MIN + 1, INT16_MIN, - INT16_MAX - 1, INT16_MAX, - INT24_MIN + 1, INT24_MIN, - INT24_MAX - 1, INT24_MAX, - }; - static Tout expected_buf[] = { - PCM_TEST_INT_NUMBERS, - INT16_MIN + 1, INT16_MIN, - INT16_MAX - 1, INT16_MAX, - INT24_MIN + 1, INT24_MIN, - INT24_MAX - 1, INT24_MAX, - }; - - struct comp_buffer *sink; - int i, N = ARRAY_SIZE(source_buf); - Tout *read_val; - - assert_int_equal(ARRAY_SIZE(source_buf), ARRAY_SIZE(expected_buf)); - mask_array(MASK(24, 0), source_buf, N); - scale_array(ratio24, expected_buf, N); - - /* run test */ - sink = _test_pcm_convert(SOF_IPC_FRAME_S24_4LE, SOF_IPC_FRAME_FLOAT, - N, source_buf); - - /* check results */ - for (i = 0; i < N; ++i) { - read_val = audio_stream_read_frag(&sink->stream, i, sizeof(Tout)); - print_message("%2d/%02d ", i + 1, N); - pcm_float_print_values(sign_extend_s24(source_buf[i]), - (int32_t *)read_val, expected_buf[i], - __func__); - assert_float_equal(*read_val, expected_buf[i], EPSILON); - } - - /* free memory */ - free_test_sink(sink); -} - -static void test_pcm_convert_f_to_s24(void **state) -{ - typedef float Tin; - typedef int32_t Tout; - static Tin source_buf[] = { - PCM_TEST_FLOAT_NUMBERS, - INT16_MIN + 1, INT16_MIN, - INT16_MAX - 1, INT16_MAX, - INT24_MIN + 1, INT24_MIN, - INT24_MAX - 1, INT24_MAX, - INT24_MIN - 1, INT24_MAX + 1, - }; - static const Tout expected_buf[] = { - PCM_TEST_INT_NUMBERS, - INT16_MIN + 1, INT16_MIN, - INT16_MAX - 1, INT16_MAX, - INT24_MIN + 1, INT24_MIN, - INT24_MAX - 1, INT24_MAX, - INT24_MIN, INT24_MAX, - }; - - struct comp_buffer *sink; - int i, N = ARRAY_SIZE(source_buf); - Tout *read_val; - - assert_int_equal(ARRAY_SIZE(source_buf), ARRAY_SIZE(expected_buf)); - scale_array(ratio24, source_buf, N); - - /* run test */ - sink = _test_pcm_convert(SOF_IPC_FRAME_FLOAT, SOF_IPC_FRAME_S24_4LE, - N, source_buf); - - /* check results */ - for (i = 0; i < N; ++i) { - read_val = audio_stream_read_frag(&sink->stream, i, sizeof(Tout)); - print_message("%2d/%02d ", i + 1, N); - pcm_float_print_values(*read_val, (int32_t *)&source_buf[i], - (float)expected_buf[i], __func__); - assert_int_equal(*read_val, expected_buf[i]); - } - - /* free memory */ - free_test_sink(sink); -} -#endif /* CONFIG_FORMAT_FLOAT && CONFIG_FORMAT_S24LE */ - -#if CONFIG_FORMAT_FLOAT && CONFIG_FORMAT_S32LE -#define S24TOS32MULT (1 << (32 - 24)) -static void test_pcm_convert_s32_to_f(void **state) -{ - typedef int32_t Tin; - typedef float Tout; - - static const Tin source_buf[] = { - PCM_TEST_INT_NUMBERS, - INT16_MIN + 1, INT16_MIN, - INT16_MAX - 1, INT16_MAX, - INT24_MIN + 1, INT24_MIN, - INT24_MAX - 1, INT24_MAX, - INT32_MIN + 1, INT32_MIN, - INT32_MAX - 1, INT32_MAX, - }; - static Tout expected_buf[] = { - PCM_TEST_INT_NUMBERS, - INT16_MIN + 1, INT16_MIN, - INT16_MAX - 1, INT16_MAX, - INT24_MIN + 1, INT24_MIN, - INT24_MAX - 1, INT24_MAX, - INT24_MIN*S24TOS32MULT, INT24_MIN * S24TOS32MULT, - INT24_MAX*S24TOS32MULT, INT24_MAX * S24TOS32MULT, - }; - - struct comp_buffer *sink; - int i, N = ARRAY_SIZE(source_buf); - Tout *read_val; - - assert_int_equal(ARRAY_SIZE(source_buf), ARRAY_SIZE(expected_buf)); - scale_array(ratio32, expected_buf, N); - - /* run test */ - sink = _test_pcm_convert(SOF_IPC_FRAME_S32_LE, SOF_IPC_FRAME_FLOAT, - N, source_buf); - - /* check results */ - for (i = 0; i < N; ++i) { - read_val = audio_stream_read_frag(&sink->stream, i, sizeof(Tout)); - print_message("%2d/%02d ", i + 1, N); - pcm_float_print_values(source_buf[i], (int32_t *)read_val, - expected_buf[i], __func__); - assert_float_equal(*read_val, expected_buf[i], EPSILON); - } - - /* free memory */ - free_test_sink(sink); -} - -static void test_pcm_convert_f_to_s32(void **state) -{ - typedef float Tin; - typedef int32_t Tout; - static Tin source_buf[] = { - PCM_TEST_FLOAT_NUMBERS, - INT16_MIN + 1, INT16_MIN, - INT16_MAX - 1, INT16_MAX, - INT24_MIN + 1, INT24_MIN, - INT24_MAX - 1, INT24_MAX, - }; - static const Tout expected_buf[] = { - PCM_TEST_INT_NUMBERS, - INT16_MIN + 1, INT16_MIN, - INT16_MAX - 1, INT16_MAX, - INT24_MIN + 1, INT24_MIN, - INT24_MAX - 1, INT24_MAX, - }; - - struct comp_buffer *sink; - int i, N = ARRAY_SIZE(source_buf); - Tout *read_val; - - assert_int_equal(ARRAY_SIZE(source_buf), ARRAY_SIZE(expected_buf)); - scale_array(ratio32, source_buf, N); - - /* run test */ - sink = _test_pcm_convert(SOF_IPC_FRAME_FLOAT, SOF_IPC_FRAME_S32_LE, - N, source_buf); - - /* check results */ - for (i = 0; i < N; ++i) { - read_val = audio_stream_read_frag(&sink->stream, i, sizeof(Tout)); - print_message("%2d/%02d ", i + 1, N); - pcm_float_print_values(*read_val, (int32_t *)&source_buf[i], - (float)expected_buf[i], __func__); - assert_int_equal(*read_val, expected_buf[i]); - } - - /* free memory */ - free_test_sink(sink); -} - -static void test_pcm_convert_f_to_s32_big_neg(void **state) -{ - typedef float Tin; - typedef int32_t Tout; - static Tin source_buf[] = { - INT24_MIN - 127, INT24_MIN - 128, /* 24B mantissa trimming */ - INT32_MIN + 1, INT32_MIN, /* 24B mantissa trimming */ - INT32_MIN * 2.f, INT32_MIN * 10.f, - -INFINITY - }; - static const Tout expected_buf[] = { - INT24_MIN - 127, INT24_MIN - 128, - INT32_MIN + 1, INT32_MIN, - INT32_MIN, INT32_MIN, - INT32_MIN - }; - - struct comp_buffer *sink; - int i, N = ARRAY_SIZE(source_buf); - Tout *read_val; - - assert_int_equal(ARRAY_SIZE(source_buf), ARRAY_SIZE(expected_buf)); - scale_array(ratio32, source_buf, N); - - /* run test */ - sink = _test_pcm_convert(SOF_IPC_FRAME_FLOAT, SOF_IPC_FRAME_S32_LE, - N, source_buf); - - /* check results */ - for (i = 0; i < N; ++i) { - read_val = audio_stream_read_frag(&sink->stream, i, sizeof(Tout)); - print_message("%2d/%02d ", i + 1, N); - pcm_float_print_values(*read_val, (int32_t *)&source_buf[i], - (float)expected_buf[i], __func__); - /* assert as float because of possible rounding effects */ - assert_float_equal(*read_val, expected_buf[i], 1); - } - - /* free memory */ - free_test_sink(sink); -} - -static void test_pcm_convert_f_to_s32_big_pos(void **state) -{ - typedef float Tin; - typedef int32_t Tout; - static Tin source_buf[] = { - INT24_MAX + 127, INT24_MAX + 128, - INT32_MAX - 255, INT32_MAX - 127, - INT32_MAX - 1, INT32_MAX, - INT32_MAX * 2.f, INT32_MAX * 10.f, - INFINITY, - }; - /* remember about 24B mantissa trimming */ - static const Tout expected_buf[] = { - INT24_MAX + 127, INT24_MAX + 128, - INT32_MAX - 254, INT32_MAX - 126, - INT32_MAX - 1, INT32_MAX, - INT32_MAX, INT32_MAX, - INT32_MAX, - }; - - struct comp_buffer *sink; - int i, N = ARRAY_SIZE(source_buf); - Tout *read_val; - - assert_int_equal(ARRAY_SIZE(source_buf), ARRAY_SIZE(expected_buf)); - scale_array(ratio32, source_buf, N); - - /* run test */ - sink = _test_pcm_convert(SOF_IPC_FRAME_FLOAT, SOF_IPC_FRAME_S32_LE, - N, source_buf); - - /* check results */ - for (i = 0; i < N; ++i) { - read_val = audio_stream_read_frag(&sink->stream, i, sizeof(Tout)); - print_message("%2d/%02d ", i + 1, N); - pcm_float_print_values(*read_val, (int32_t *)&source_buf[i], - (float)expected_buf[i], __func__); - /* assert as float because of possible rounding effects */ - assert_float_equal(*read_val, expected_buf[i], 1); - } - - /* free memory */ - free_test_sink(sink); -} -#endif /* CONFIG_FORMAT_FLOAT && CONFIG_FORMAT_S32LE */ - -int main(void) -{ - const struct CMUnitTest tests[] = { -#if CONFIG_FORMAT_FLOAT && CONFIG_FORMAT_S16LE - cmocka_unit_test(test_pcm_convert_s16_to_f), - cmocka_unit_test(test_pcm_convert_f_to_s16), -#endif /* CONFIG_FORMAT_FLOAT && CONFIG_FORMAT_S16LE */ -#if CONFIG_FORMAT_FLOAT && CONFIG_FORMAT_S24LE - cmocka_unit_test(test_pcm_convert_s24_to_f), - cmocka_unit_test(test_pcm_convert_s24_in_s32_to_f), - cmocka_unit_test(test_pcm_convert_f_to_s24), -#endif /* CONFIG_FORMAT_FLOAT && CONFIG_FORMAT_S24LE */ -#if CONFIG_FORMAT_FLOAT && CONFIG_FORMAT_S32LE - cmocka_unit_test(test_pcm_convert_s32_to_f), - cmocka_unit_test(test_pcm_convert_f_to_s32), - cmocka_unit_test(test_pcm_convert_f_to_s32_big_neg), - cmocka_unit_test(test_pcm_convert_f_to_s32_big_pos), -#endif /* CONFIG_FORMAT_FLOAT && CONFIG_FORMAT_S32LE */ - }; - - cmocka_set_message_output(CM_OUTPUT_TAP); - - /* log number of converting functions for current configuration */ - print_message("%s start tests, count(pcm_func_map)=%zu\n", - __FILE__, pcm_func_count); - - return cmocka_run_group_tests(tests, NULL, NULL); -}