Skip to content

test: unit: Port new cases from cmocka to ztest.#10590

Open
tmleman wants to merge 4 commits intothesofproject:mainfrom
tmleman:topic/upstream/pr/unit_test/ztest/math/base2_logarithm
Open

test: unit: Port new cases from cmocka to ztest.#10590
tmleman wants to merge 4 commits intothesofproject:mainfrom
tmleman:topic/upstream/pr/unit_test/ztest/math/base2_logarithm

Conversation

@tmleman
Copy link
Contributor

@tmleman tmleman commented Mar 3, 2026

This PR migrates SOF math unit tests from CMock to Zephyr Ztest framework and standardizes test naming conventions.

Standardize all test suite names to follow 'sof.unit.*' pattern:

- fast_get.basic_functionality → sof.unit.fast_get
- sof.list → sof.unit.list
- sof.objpool → sof.unit.objpool
- math.basic.arithmetic → sof.unit.math.basic.arithmetic
- math.advanced.functions → sof.unit.math.advanced.functions
- sof.unit.math.trigonometry → sof.unit.math.basic.trigonometry
- sof.unit.math.complex → sof.unit.math.basic.complex

Signed-off-by: Tomasz Leman <tomasz.m.leman@intel.com>
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Migrates SOF unit tests from CMocka to Zephyr Ztest and aligns test identifiers/tags with a standardized naming scheme.

Changes:

  • Renamed several Ztest testcase.yaml entries to sof.unit.*-prefixed identifiers.
  • Added Ztest-based math “advanced functions” test coverage (exponential + base2 log) and updated build sources.
  • Expanded tags for advanced math tests to improve test selection/filtering.

Reviewed changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
test/ztest/unit/objpool/testcase.yaml Renames objpool test ID to the standardized sof.unit.* namespace.
test/ztest/unit/math/basic/trigonometry/testcase.yaml Renames trigonometry test ID under sof.unit.math.basic.*.
test/ztest/unit/math/basic/complex/testcase.yaml Renames complex test ID under sof.unit.math.basic.*.
test/ztest/unit/math/basic/arithmetic/testcase.yaml Renames arithmetic test ID to sof.unit.math.basic.arithmetic.
test/ztest/unit/math/advanced/functions/testcase.yaml Renames advanced functions test ID and expands tags for filtering.
test/ztest/unit/math/advanced/functions/test_exponential_ztest.c Adds Ztest port of exponential-related unit tests.
test/ztest/unit/math/advanced/functions/test_base2_logarithm_ztest.c Adds Ztest port of base2 logarithm unit test.
test/ztest/unit/math/advanced/functions/CMakeLists.txt Extends sources for new tests and math implementations.
test/ztest/unit/list/testcase.yaml Renames list test ID to sof.unit.list.
test/ztest/unit/fast-get/testcase.yaml Renames fast-get test ID to sof.unit.fast_get.
Comments suppressed due to low confidence (1)

test/ztest/unit/math/advanced/functions/test_exponential_ztest.c:55

  • saturate32() returns int32_t but returns x (an int64_t) directly. While the preceding bounds checks ensure it’s in range, adding an explicit cast (e.g., (int32_t)x) avoids implicit-narrowing warnings and makes the intent clearer.
	return x;

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@lgirdwood
Copy link
Member

lgirdwood commented Mar 3, 2026

@tmleman I guess you have fixed the copilot comments ? If so, can you resolve. Btw, its OK to click the sign-off from copilot if needed to save effort.

@tmleman tmleman force-pushed the topic/upstream/pr/unit_test/ztest/math/base2_logarithm branch from 6defa2e to d8d37f3 Compare March 4, 2026 08:45
@tmleman
Copy link
Contributor Author

tmleman commented Mar 4, 2026

@tmleman I guess you have fixed the copilot comments ? If so, can you resolve. Btw, its OK to click the sign-off from copilot if needed to save effort.

@lgirdwood now I have

tmleman added 3 commits March 4, 2026 11:07
This patch converts 1 existing math advanced function unit test from
CMock/Unity to Zephyr's Ztest framework, maintaining the same test
coverage and functionality:

- test_math_arithmetic_base2log_fixed: Fixed-point base-2 logarithm

Original test converted from
sof/test/cmocka/src/math/arithmetic/base2_logarithm.c authored by:
- Shriram Shastry <malladi.sastry@linux.intel.com>

The converted test validates the same base2_logarithm() function from
src/math/base2log.c as the original CMock test, ensuring no regression
in test coverage during the migration to Ztest framework. Reference
tables and tolerance values are preserved to maintain identical test
accuracy and validation criteria.

Test validates 100 uniformly distributed input values across the full
uint32_t range using MATLAB-generated reference data with fixed-point
arithmetic:
- Input values: Q32.0 format (unsigned 32-bit integers)
- Results: Q16.16 format
- Reference: MATLAB log2() function results
- Tolerance: max error 0.0000236785999981, THD -92.5128795787487235

This continues the SOF unit test migration from CMock to Zephyr Ztest
framework as part of the math/advanced/functions test suite.

Signed-off-by: Tomasz Leman <tomasz.m.leman@intel.com>
Remove explicit linking of math library (-lm) from advanced math
function tests to fix native_sim build failures.

The explicit target_link_libraries(app PRIVATE m) caused linker errors
with __x86.get_pc_thunk.bx symbols in Zephyr's native_sim builds. This
occurs because native_sim's build system already handles math library
linkage, and explicitly adding it creates conflicting symbol references
during the final native simulator linking stage.

The math library functions (exp, pow, log2, etc.) used in tests are
still available as Zephyr automatically links libm for native_sim
targets.

Error example:
  `__x86.get_pc_thunk.bx' referenced in section `.text' of
  zephyr.elf.loc_cpusw.o: defined in discarded section
  `.text.__x86.get_pc_thunk.bx[__x86.get_pc_thunk.bx]'

Related: zephyrproject-rtos/zephyr#97999

Signed-off-by: Tomasz Leman <tomasz.m.leman@intel.com>
Port exponential function tests from CMock/CMocka to Zephyr Ztest
framework. Migrates tests from
test/cmocka/src/math/arithmetic/exponential.c to
test/ztest/unit/math/advanced/functions/test_exponential_ztest.c.

Test coverage includes:
- sofm_exp_approx(): ULP-based validation of Q28 -> Q19 approximation
  (256 samples, range -8 to 8)
- sofm_exp_fixed(): Fixed-point exponential Q27 -> Q20
  - Full range: -16 to 16 (100 samples, coarse grid)
  - Middle range: -11.5 to 7.6245 (100 samples, fine grid)
- sofm_db2lin_fixed(): dB to linear conversion Q24 -> Q20
  (100 samples, range -128 to 128 dB)

Changes:
- Add test_exponential_ztest.c with 3 test functions (291 lines)
- Update CMakeLists.txt to build exp_fcn.c and exp_fcn_hifi.c
- Add comment noting exp_fcn_hifi.c is Xtensa HiFi-specific
- Update testcase.yaml tags: exponential, exp, db2lin
- Use Zephyr LOG_INF for diagnostic output instead of printf

All tests validate against reference implementations using absolute and
relative error tolerances appropriate for fixed-point arithmetic.

Signed-off-by: Tomasz Leman <tomasz.m.leman@intel.com>
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.

4 participants