From 3878d151c0bc7a6ac95d9fa6581b3bbf856cb825 Mon Sep 17 00:00:00 2001 From: Tomasz Leman Date: Fri, 25 Sep 2026 15:13:02 +0200 Subject: [PATCH 1/2] test: math: port a_law_codec CMocka test to ztest Convert the legacy CMocka a_law_codec unit test to a Zephyr ztest suite under test/ztest/unit/math/a_law, running on native_sim via twister as scenario sof.unit.math.a_law. The two ported cases keep identical intent, test data and fixed-point semantics: test_a_law_encode encodes every s16 chirp sample with sofm_a_law_encode() and test_a_law_decode expands every reference code word with sofm_a_law_decode(), both compared against the same pre-computed reference vectors. The reference-data headers (ref_chirp_mono_8k_s16.h, a_law_codec.h) are #included in place from the CMocka tree rather than copied, so both frameworks share the exact same data. Coverage of src/math/a_law.c (lcov hit/total; CMocka unit_test_defconfig gcc vs ztest native_sim clang): src/math/a_law.c CMocka: lines 32/32 functions 2/2 branches 8/8 ztest: lines 28/29 functions 2/2 branches 9/10 Functions match 2/2 and ztest covers one extra branch (9/10 vs 8/8). Every line gcc executes is also executed by ztest, the differing line totals are pure gcc/clang bucketing. The only line clang marks unhit (the saturation clamp sample = SOFM_ALAW_MAX) is unreachable with the shared chirp data, so gcc emits no counter for it either, there is no real coverage regression. Signed-off-by: Tomasz Leman --- test/ztest/unit/math/a_law/CMakeLists.txt | 30 ++++++++ test/ztest/unit/math/a_law/prj.conf | 6 ++ .../unit/math/a_law/test_a_law_codec_ztest.c | 74 +++++++++++++++++++ test/ztest/unit/math/a_law/testcase.yaml | 17 +++++ 4 files changed, 127 insertions(+) create mode 100644 test/ztest/unit/math/a_law/CMakeLists.txt create mode 100644 test/ztest/unit/math/a_law/prj.conf create mode 100644 test/ztest/unit/math/a_law/test_a_law_codec_ztest.c create mode 100644 test/ztest/unit/math/a_law/testcase.yaml diff --git a/test/ztest/unit/math/a_law/CMakeLists.txt b/test/ztest/unit/math/a_law/CMakeLists.txt new file mode 100644 index 000000000000..b1ffae46d4d3 --- /dev/null +++ b/test/ztest/unit/math/a_law/CMakeLists.txt @@ -0,0 +1,30 @@ +# SPDX-License-Identifier: BSD-3-Clause +# +# Copyright(c) 2026 Intel Corporation. All rights reserved. + +cmake_minimum_required(VERSION 3.20.0) + +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) +project(test_math_a_law) + +set(SOF_ROOT "${PROJECT_SOURCE_DIR}/../../../../..") + +target_include_directories(app PRIVATE + ${SOF_ROOT}/zephyr/include + ${SOF_ROOT}/src/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/arithmetic +) + +# Define SOF-specific configurations for unit testing +target_compile_definitions(app PRIVATE + -DCONFIG_ZEPHYR_POSIX=1 + -DCONFIG_LIBRARY=1 + -DUNIT_TEST=1 +) + +target_sources(app PRIVATE + test_a_law_codec_ztest.c + ${SOF_ROOT}/src/math/a_law.c +) diff --git a/test/ztest/unit/math/a_law/prj.conf b/test/ztest/unit/math/a_law/prj.conf new file mode 100644 index 000000000000..cf9860e289e2 --- /dev/null +++ b/test/ztest/unit/math/a_law/prj.conf @@ -0,0 +1,6 @@ +# SPDX-License-Identifier: BSD-3-Clause +# +# Copyright(c) 2026 Intel Corporation. All rights reserved. + +CONFIG_ZTEST=y +CONFIG_SOF_FULL_ZEPHYR_APPLICATION=n diff --git a/test/ztest/unit/math/a_law/test_a_law_codec_ztest.c b/test/ztest/unit/math/a_law/test_a_law_codec_ztest.c new file mode 100644 index 000000000000..be7ccf735bd0 --- /dev/null +++ b/test/ztest/unit/math/a_law/test_a_law_codec_ztest.c @@ -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. +// +// Converted from CMock to Ztest +// Original tests from test/cmocka/src/math/arithmetic/a_law_codec.c: +// Author: Seppo Ingalsuo + +#include +#include +#include +#include + +/* Reference test vectors shared with the legacy CMocka test. They are + * #included in place from the CMocka tree (added to the include path in + * CMakeLists.txt) so the exact same fixed-point data is exercised. + */ +#include "ref_chirp_mono_8k_s16.h" +#include "a_law_codec.h" + +/** + * @brief Verify A-law encoding against the reference vector + * + * Encodes every s16 chirp sample with sofm_a_law_encode() and checks the + * 8-bit code word matches the pre-computed reference, preserving the + * fixed-point semantics of the original CMocka test. + */ +ZTEST(math_a_law_suite, test_a_law_encode) +{ + uint8_t a_law_sample, a_law_ref; + int i; + + for (i = 0; i < REF_DATA_SAMPLE_COUNT; i++) { + a_law_sample = sofm_a_law_encode(chirp_mono_8k_s16[i]); + a_law_ref = ref_alaw_enc_data[i]; + + if (a_law_sample != a_law_ref) { + printf("%s: difference found at %d, encoded %d, ref %d, lin %d\n", __func__, + i, a_law_sample, a_law_ref, chirp_mono_8k_s16[i]); + zassert_true(false, "A-law encode mismatch"); + } + } +} + +/** + * @brief Verify A-law decoding against the reference vector + * + * Decodes every reference code word with sofm_a_law_decode() and checks the + * expanded s16 sample matches the pre-computed reference, preserving the + * fixed-point semantics of the original CMocka test. + */ +ZTEST(math_a_law_suite, test_a_law_decode) +{ + int16_t s16_sample, s16_ref; + int i; + + for (i = 0; i < REF_DATA_SAMPLE_COUNT; i++) { + s16_sample = sofm_a_law_decode(ref_alaw_enc_data[i]); + s16_ref = ref_alaw_dec_data[i]; + if (s16_sample != s16_ref) { + printf("%s: difference found at %d, decoded %d, ref %d\n", __func__, i, + s16_sample, s16_ref); + zassert_true(false, "A-law decode mismatch"); + } + } +} + +/** + * @brief Define and initialize the A-law codec test suite + */ +ZTEST_SUITE(math_a_law_suite, NULL, NULL, NULL, NULL, NULL); diff --git a/test/ztest/unit/math/a_law/testcase.yaml b/test/ztest/unit/math/a_law/testcase.yaml new file mode 100644 index 000000000000..a1da07f448f3 --- /dev/null +++ b/test/ztest/unit/math/a_law/testcase.yaml @@ -0,0 +1,17 @@ +# SPDX-License-Identifier: BSD-3-Clause +# +# Copyright(c) 2026 Intel Corporation. All rights reserved. +# +# Math A-law codec 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. +# + +tests: + sof.unit.math.a_law: + tags: [math, a_law, codec, g711] + platform_allow: native_sim + integration_platforms: + - native_sim + build_only: false From 39e74a91b7441109e0aeec00510da65a537185fc Mon Sep 17 00:00:00 2001 From: Tomasz Leman Date: Fri, 25 Sep 2026 15:18:40 +0000 Subject: [PATCH 2/2] test: math: port mu_law_codec CMocka test to ztest Port both mu-law codec cases to native_sim ztest. Encode every s16 chirp sample and decode every reference code word. Keep the original fixed-point comparisons and share the legacy vectors in place. Coverage of src/math/mu_law.c (gcc CMocka vs clang ztest): CMocka: lines 31/31 functions 2/2 branches 6/6 ztest: lines 26/26 functions 2/2 branches 6/6 Both have full source coverage; totals differ by instrumentation. Signed-off-by: Tomasz Leman Assisted-by: GitHub Copilot CLI:gpt-6-sol --- test/ztest/unit/math/mu_law/CMakeLists.txt | 28 +++++++++ test/ztest/unit/math/mu_law/prj.conf | 6 ++ .../math/mu_law/test_mu_law_codec_ztest.c | 61 +++++++++++++++++++ test/ztest/unit/math/mu_law/testcase.yaml | 11 ++++ 4 files changed, 106 insertions(+) create mode 100644 test/ztest/unit/math/mu_law/CMakeLists.txt create mode 100644 test/ztest/unit/math/mu_law/prj.conf create mode 100644 test/ztest/unit/math/mu_law/test_mu_law_codec_ztest.c create mode 100644 test/ztest/unit/math/mu_law/testcase.yaml diff --git a/test/ztest/unit/math/mu_law/CMakeLists.txt b/test/ztest/unit/math/mu_law/CMakeLists.txt new file mode 100644 index 000000000000..2b2ac8f580af --- /dev/null +++ b/test/ztest/unit/math/mu_law/CMakeLists.txt @@ -0,0 +1,28 @@ +# SPDX-License-Identifier: BSD-3-Clause +# +# Copyright(c) 2026 Intel Corporation. All rights reserved. + +cmake_minimum_required(VERSION 3.20.0) + +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) +project(test_math_mu_law) + +set(SOF_ROOT "${PROJECT_SOURCE_DIR}/../../../../..") + +target_include_directories(app PRIVATE + ${SOF_ROOT}/zephyr/include + ${SOF_ROOT}/src/include + # Share the reference vectors with the legacy CMocka test. + ${SOF_ROOT}/test/cmocka/src/math/arithmetic +) + +target_compile_definitions(app PRIVATE + -DCONFIG_ZEPHYR_POSIX=1 + -DCONFIG_LIBRARY=1 + -DUNIT_TEST=1 +) + +target_sources(app PRIVATE + test_mu_law_codec_ztest.c + ${SOF_ROOT}/src/math/mu_law.c +) diff --git a/test/ztest/unit/math/mu_law/prj.conf b/test/ztest/unit/math/mu_law/prj.conf new file mode 100644 index 000000000000..cf9860e289e2 --- /dev/null +++ b/test/ztest/unit/math/mu_law/prj.conf @@ -0,0 +1,6 @@ +# SPDX-License-Identifier: BSD-3-Clause +# +# Copyright(c) 2026 Intel Corporation. All rights reserved. + +CONFIG_ZTEST=y +CONFIG_SOF_FULL_ZEPHYR_APPLICATION=n diff --git a/test/ztest/unit/math/mu_law/test_mu_law_codec_ztest.c b/test/ztest/unit/math/mu_law/test_mu_law_codec_ztest.c new file mode 100644 index 000000000000..c277222afe6e --- /dev/null +++ b/test/ztest/unit/math/mu_law/test_mu_law_codec_ztest.c @@ -0,0 +1,61 @@ +// SPDX-License-Identifier: BSD-3-Clause +// +// Copyright(c) 2026 Intel Corporation. +// +// Original tests from test/cmocka/src/math/arithmetic/mu_law_codec.c: +// Author: Seppo Ingalsuo + +#include +#include +#include +#include + +/* Include the same fixed-point reference vectors as the CMocka test. */ +#include "ref_chirp_mono_8k_s16.h" +#include "mu_law_codec.h" + +/** + * @brief Verify mu-law encoding of every chirp sample against its reference. + * + * Keep the legacy s16 input and 8-bit reference code words unchanged. + */ +ZTEST(math_mu_law_suite, test_mu_law_encode) +{ + uint8_t mu_law_sample, mu_law_ref; + int i; + + for (i = 0; i < REF_DATA_SAMPLE_COUNT; i++) { + mu_law_sample = sofm_mu_law_encode(chirp_mono_8k_s16[i]); + mu_law_ref = ref_mulaw_enc_data[i]; + + if (mu_law_sample != mu_law_ref) { + printf("%s: difference found at %d, encoded %d, ref %d, lin %d\n", __func__, + i, mu_law_sample, mu_law_ref, chirp_mono_8k_s16[i]); + zassert_true(false, "mu-law encode mismatch"); + } + } +} + +/** + * @brief Verify mu-law decoding of every reference code word. + * + * Compare the expanded s16 samples with the legacy fixed-point vector. + */ +ZTEST(math_mu_law_suite, test_mu_law_decode) +{ + int16_t s16_sample, s16_ref; + int i; + + for (i = 0; i < REF_DATA_SAMPLE_COUNT; i++) { + s16_sample = sofm_mu_law_decode(ref_mulaw_enc_data[i]); + s16_ref = ref_mulaw_dec_data[i]; + if (s16_sample != s16_ref) { + printf("%s: difference found at %d, byte %d, decoded %d, ref %d\n", + __func__, i, ref_mulaw_enc_data[i], s16_sample, s16_ref); + zassert_true(false, "mu-law decode mismatch"); + } + } +} + +/** @brief Register the mu-law codec tests. */ +ZTEST_SUITE(math_mu_law_suite, NULL, NULL, NULL, NULL, NULL); diff --git a/test/ztest/unit/math/mu_law/testcase.yaml b/test/ztest/unit/math/mu_law/testcase.yaml new file mode 100644 index 000000000000..ad1a79ecfcbc --- /dev/null +++ b/test/ztest/unit/math/mu_law/testcase.yaml @@ -0,0 +1,11 @@ +# SPDX-License-Identifier: BSD-3-Clause +# +# Copyright(c) 2026 Intel Corporation. All rights reserved. + +tests: + sof.unit.math.mu_law: + tags: [math, mu_law, codec, g711] + platform_allow: native_sim + integration_platforms: + - native_sim + build_only: false