Skip to content

Conversation

@tmleman
Copy link
Contributor

@tmleman tmleman commented Jul 25, 2025

Convert legacy CMock-based math basic arithmetic unit tests to Zephyr Ztest framework.

This patch converts 6 existing math arithmetic unit tests from CMock/Unity to Zephyr's Ztest framework, maintaining the same test coverage and functionality:

  • test_gcd_ztest.c: Greatest common divisor (8 test cases)
  • test_ceil_divide_ztest.c: Ceiling division (1 test case)
  • test_find_equal_int16_ztest.c: Find equal int16 values (2 test cases)
  • test_find_min_int16_ztest.c: Find minimum int16 (2 test cases)
  • test_find_max_abs_int32_ztest.c: Find max absolute int32 (2 test cases)
  • test_norm_int32_ztest.c: Normalize int32 (3 test cases)

The converted tests validate the same mathematical functions from src/math/numbers.c as the original CMock tests, ensuring no regression in test coverage during the migration to Ztest framework.

This is part of the broader SOF unit test migration from CMock to Zephyr Ztest framework, establishing the foundation for math/basic/arithmetic tests in the new directory structure.

@tmleman tmleman force-pushed the topic/upstream/pr/unit_test/ztest/math/arithmetic branch from db3d0e6 to a503091 Compare July 31, 2025 14:27
@tmleman tmleman marked this pull request as ready for review July 31, 2025 14:27
Copilot AI review requested due to automatic review settings July 31, 2025 14:27
Copy link

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

This PR converts legacy CMock-based math basic arithmetic unit tests to Zephyr's Ztest framework, maintaining the same test coverage while modernizing the test infrastructure.

  • Migrates 6 math arithmetic test files from CMock/Unity to Ztest format
  • Preserves original test functionality with 18 total test cases covering GCD, ceiling division, and array operations
  • Establishes new directory structure for math unit tests under ztest framework

Reviewed Changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
testcase.yaml Defines test configuration for math.basic.arithmetic test suite
CMakeLists.txt Build configuration linking SOF math sources and test files
prj.conf Minimal Zephyr project configuration enabling Ztest
test_gcd_ztest.c GCD function tests with 8 test cases including edge cases
test_ceil_divide_ztest.c Ceiling division test with comprehensive parameter combinations
test_find_equal_int16_ztest.c Array search tests for matching int16 values
test_find_min_int16_ztest.c Minimum value finding tests for int16 arrays
test_find_max_abs_int32_ztest.c Maximum absolute value tests for int32 arrays
test_norm_int32_ztest.c Integer normalization tests with various input values

/**
* @brief Define and initialize the math arithmetic test suite
*/
ZTEST_SUITE(math_arithmetic_suite, NULL, NULL, NULL, NULL, NULL);
Copy link

Copilot AI Jul 31, 2025

Choose a reason for hiding this comment

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

The test suite is defined in test_gcd_ztest.c but used across multiple test files. This creates a dependency where other test files rely on this suite definition, making the code less maintainable. Consider moving the ZTEST_SUITE definition to a separate common file or define it in each test file that uses it.

Copilot uses AI. Check for mistakes.
Copy link
Member

@lgirdwood lgirdwood left a comment

Choose a reason for hiding this comment

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

LGTM, but will need @singalsu to review the maths.

find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(test_math_arithmetic)

set(SOF_ROOT "${PROJECT_SOURCE_DIR}/../../../../../..")
Copy link
Member

Choose a reason for hiding this comment

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

Longterm we should be able to get this from SOF_WORKSPACE environment variable.

@lgirdwood
Copy link
Member

@tmleman btw, do you plan to remove the cmocka test once this is merged ?

@tmleman
Copy link
Contributor Author

tmleman commented Jul 31, 2025

@tmleman btw, do you plan to remove the cmocka test once this is merged ?

@lgirdwood I was the first to ask this question: #10110 (comment) :)

int16_t vec[] = {5, 123, 5, 10, 123, 500, 123};
int16_t template[] = {1, 4, 6};

int r_num = find_equal_int16(r, vec, 123, 7, 4);
Copy link
Collaborator

Choose a reason for hiding this comment

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

find_equal_int16() should have const arguments, then the above arrays could be const

Copy link
Contributor Author

Choose a reason for hiding this comment

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

More functions could undergo such treatment, so it can be done in a later refactor (#10110 (comment)).

ZTEST(math_arithmetic_suite, test_math_numbers_find_max_abs_int32_negative_max)
{
int32_t vec[] = {-100, 99, 98, 50};
int r = find_max_abs_int32(vec, ARRAY_SIZE(vec));
Copy link
Collaborator

Choose a reason for hiding this comment

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

also should be const. Also to be perfectly strict r should be int32_t

@singalsu
Copy link
Collaborator

singalsu commented Aug 4, 2025

@tmleman Do we have instructions how to run tests under ztest? I'm curious since I'm not familiar with it.

@tmleman tmleman force-pushed the topic/upstream/pr/unit_test/ztest/math/arithmetic branch from a503091 to 8a83cdf Compare August 4, 2025 10:55
@tmleman
Copy link
Contributor Author

tmleman commented Aug 4, 2025

@tmleman Do we have instructions how to run tests under ztest? I'm curious since I'm not familiar with it.

@singalsu I haven't prepared any documentation yet. Ultimately, it will rather be a brief description because Zephyr already has documentation for the entire framework: https://docs.zephyrproject.org/latest/develop/test/index.html

Building and running can be done with a single command: west twister --testsuite-root sof/test/ztest/unit/ --platform native_sim

If you have the environment to build FW, you're almost there, just install Clang. The GH workflow contains all the required steps: https://github.com/thesofproject/sof/blob/main/.github/workflows/zephyr-unit-tests.yml

@tmleman tmleman requested review from lyakh and singalsu August 4, 2025 11:17
Convert legacy CMock-based math basic arithmetic unit tests to Zephyr
Ztest framework.

This patch converts 6 existing math arithmetic unit tests from
CMock/Unity to Zephyr's Ztest framework, maintaining the same test
coverage and functionality:

- test_gcd_ztest.c: Greatest common divisor (8 test cases)
- test_ceil_divide_ztest.c: Ceiling division (1 test case)
- test_find_equal_int16_ztest.c: Find equal int16 values (2 test cases)
- test_find_min_int16_ztest.c: Find minimum int16 (2 test cases)
- test_find_max_abs_int32_ztest.c: Find max absolute int32 (2 test
  cases)
- test_norm_int32_ztest.c: Normalize int32 (3 test cases)

The converted tests validate the same mathematical functions from
src/math/numbers.c as the original CMock tests, ensuring no regression
in test coverage during the migration to Ztest framework.

This is part of the broader SOF unit test migration from CMock to Zephyr
Ztest framework, establishing the foundation for math/basic/arithmetic
tests in the new directory structure.

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

singalsu commented Aug 4, 2025

@tmleman Do we have instructions how to run tests under ztest? I'm curious since I'm not familiar with it.

@singalsu I haven't prepared any documentation yet. Ultimately, it will rather be a brief description because Zephyr already has documentation for the entire framework: https://docs.zephyrproject.org/latest/develop/test/index.html

Building and running can be done with a single command: west twister --testsuite-root sof/test/ztest/unit/ --platform native_sim

If you have the environment to build FW, you're almost there, just install Clang. The GH workflow contains all the required steps: https://github.com/thesofproject/sof/blob/main/.github/workflows/zephyr-unit-tests.yml

Thanks, sounds good, I'll try the above!

Copy link
Collaborator

@kv2019i kv2019i left a comment

Choose a reason for hiding this comment

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

Given this is forklift of the cmock versions, I'm good with this version.

@lgirdwood lgirdwood merged commit bd08ae0 into thesofproject:main Aug 4, 2025
37 of 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