Skip to content

test: math: port matrix CMocka test to ztest - #11373

Merged
kv2019i merged 1 commit into
thesofproject:mainfrom
tmleman:topic/upstream/pr/ut/ztest/math/matrix
Oct 29, 2026
Merged

kv2019i merged 1 commit into
thesofproject:mainfrom
tmleman:topic/upstream/pr/ut/ztest/math/matrix

Conversation

@tmleman

@tmleman tmleman commented Oct 29, 2026

Copy link
Copy Markdown
Contributor

Port the legacy CMocka unit test test/cmocka/src/math/matrix/matrix.c to a Zephyr ztest suite under test/ztest/unit/math/matrix/.

sof.unit.math.matrix mirrors matrix.c: it links src/math/matrix.c and converts the 4 fixed-point multiply checks into one ZTEST case each, all against the same 1.5 LSB absolute and 0.5 LSB RMS error limits, computed by the same float math. The reference vectors (ref_matrix_mult_16_test*.h) are #included in place from the CMocka tree via the include path, shared verbatim between both frameworks, as test/ztest/unit/math/{a_law,mu_law,fft} already do. The CMocka test keeps running unchanged.

The four CMocka test functions differed only in the test number of the 13 reference vector and geometry names they passed, so they are replaced by a MATRIX_MULT_16_TEST(n) macro that pastes the number into the names. The test data and the geometries are unchanged.

The suite exercises the mod_alloc() API through mod_mat_matrix_alloc_16b(), so it links test/ztest/unit/common/mod_alloc.c on top of the real SOF allocator, zephyr/lib/alloc.c, with the heap routed to the native simulator host malloc() so ASan and LSan can see it. mod_alloc_test_after() is the ztest after-hook, so a test case leaking a matrix is named. Unlike the CMocka test, which asserted before freeing and so leaked all three matrices on any failure, the matrices are now freed before the error is checked.

CONFIG_CBPRINTF_FP_SUPPORT=y is needed because printk() otherwise emits the %f conversions of the log and of the failure messages literally. Zephyr also compiles with -Wdouble-promotion, so the RMS uses sqrtf() and casts its float arguments to double explicitly at the printk() varargs call sites. Neither changes the error math.

Verified with:
west twister -T sof/test/ztest/unit/math/matrix/ --platform native_sim
-> sof.unit.math.matrix: 4/4 cases PASSED
-> whole unit tree: 16/16 configurations, 126/126 cases PASSED, with
and without --enable-asan --enable-lsan

All four cases reproduce the reference exactly, 0.00 absolute and 0.00 RMS error, as they do under CMocka. That the checks are live was confirmed by mutation: skipping mat_multiply() fails tests 1, 2 and 4, skipping mat_multiply_elementwise() fails test 3, and dropping one mod_free() is reported by mod_alloc_test_after() as a 48 byte leak in one allocation.

Assisted-by: Copilot:claude-opus-5

Copilot AI balanced review requested due to automatic review settings October 29, 2026 08:27

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues remain for the matrix ztest additions.

No open issues 🎉

What changed in this PR

Adds a Zephyr ztest suite for SOF’s fixed-point matrix multiplication, preserving the four legacy CMocka cases and leaving the existing suite unchanged.

Changes:

  • Reuses the CMocka reference vectors and error limits in four ztest cases.
  • Adds per-test allocation leak checks and frees matrices before validating numerical errors.
  • Configures native simulator execution, sanitizer-visible allocations, and floating-point diagnostics.
File Description
test/​ztest/​unit/​math/​matrix/​testcase.yaml Registers the matrix suite for native_sim.
test/​ztest/​unit/​math/​matrix/​test_matrix_ztest.c Implements four matrix tests using shared vectors and allocation checks.
test/​ztest/​unit/​math/​matrix/​prj.conf Enables host-heap allocation and floating-point output.
test/​ztest/​unit/​math/​matrix/​CMakeLists.txt Links matrix code, allocator support, and shared reference headers.

Review effort: Balanced

Port the legacy CMocka unit test test/cmocka/src/math/matrix/matrix.c to a
Zephyr ztest suite under test/ztest/unit/math/matrix/.

sof.unit.math.matrix mirrors matrix.c: it links src/math/matrix.c and
converts the 4 fixed-point multiply checks into one ZTEST case each, all
against the same 1.5 LSB absolute and 0.5 LSB RMS error limits, computed by
the same float math. The reference vectors (ref_matrix_mult_16_test*.h) are
#included in place from the CMocka tree via the include path, shared
verbatim between both frameworks, as test/ztest/unit/math/{a_law,mu_law,fft}
already do. The CMocka test keeps running unchanged.

The four CMocka test functions differed only in the test number of the 13
reference vector and geometry names they passed, so they are replaced by a
MATRIX_MULT_16_TEST(n) macro that pastes the number into the names. The
test data and the geometries are unchanged.

The suite exercises the mod_alloc() API through mod_mat_matrix_alloc_16b(),
so it links test/ztest/unit/common/mod_alloc.c on top of the real SOF
allocator, zephyr/lib/alloc.c, with the heap routed to the native simulator
host malloc() so ASan and LSan can see it. mod_alloc_test_after() is the
ztest after-hook, so a test case leaking a matrix is named. Unlike the
CMocka test, which asserted before freeing and so leaked all three matrices
on any failure, the matrices are now freed before the error is checked.

CONFIG_CBPRINTF_FP_SUPPORT=y is needed because printk() otherwise emits the
%f conversions of the log and of the failure messages literally. Zephyr
also compiles with -Wdouble-promotion, so the RMS uses sqrtf() and casts
its float arguments to double explicitly at the printk() varargs call
sites. Neither changes the error math.

Verified with:
  west twister -T sof/test/ztest/unit/math/matrix/ --platform native_sim
  -> sof.unit.math.matrix: 4/4 cases PASSED
  -> whole unit tree: 16/16 configurations, 126/126 cases PASSED, with
     and without --enable-asan --enable-lsan

All four cases reproduce the reference exactly, 0.00 absolute and 0.00 RMS
error, as they do under CMocka. That the checks are live was confirmed by
mutation: skipping mat_multiply() fails tests 1, 2 and 4, skipping
mat_multiply_elementwise() fails test 3, and dropping one mod_free() is
reported by mod_alloc_test_after() as a 48 byte leak in one allocation.

Assisted-by: Copilot:claude-opus-5
Signed-off-by: Tomasz Leman <tomasz.m.leman@intel.com>
@intel-sofci

intel-sofci commented Oct 29, 2026 •

Copy link
Copy Markdown

PR 11373: test results

Run date: 2026-10-29 14:43 UTC

Tested commit: 2bb5c86d6e3f019b1c92488a24041005321cbc41

mtl pass rate lnl pass rate ptl pass rate wcl pass rate nvl pass rate

@kv2019i
kv2019i merged commit db1c442 into thesofproject:main Oct 29, 2026
45 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants