Repository navigation
[FEATURE] Migrate SOF Unit Tests from CMock to Zephyr ztest Framework #10110
Description
Activity
@lgirdwood topic for discussion: At what point can we start removing tests that have already been ported?
Skipping migration of
rstrcmpandrstrlentests for now. In Zephyr builds, these functions are just simple wrappers around standard C library functions (strlenandstrcmp), so the tests wouldn't be validating our custom implementations. The original unit tests were designed to test the custom implementations in sof/src/lib/lib.c which are only used in non-Zephyr builds, making these tests redundant in our primary target environment.Math Tests Migration Strategy
Current CMock Structure Analysis
The math tests are organized into 8 subcategories with clear separation:
sof/test/cmocka/src/math/ ├── arithmetic/ # Codecs & Advanced Math (6 tests) ├── numbers/ # Basic Arithmetic (6 tests) ├── trig/ # Trigonometry (9 tests) ├── fft/ # FFT (1 test) ├── window/ # Window Functions (1 test) ├── matrix/ # Matrix Operations (1 test) ├── auditory/ # Auditory Processing (1 test) └── dct/ # DCT (1 test)Proposed Ztest Directory Structure
Based on the logical grouping and complexity analysis, the following structure is proposed:
sof/test/ztest/unit/math/ ├── basic/ # Simple, fast unit tests │ ├── arithmetic/ # Basic number operations (6 tests) │ │ ├── test_gcd_ztest.c │ │ ├── test_ceil_divide_ztest.c │ │ ├── test_find_equal_int16_ztest.c │ │ ├── test_find_min_int16_ztest.c │ │ ├── test_find_max_abs_int32_ztest.c │ │ ├── test_norm_int32_ztest.c │ │ ├── CMakeLists.txt │ │ ├── testcase.yaml │ │ └── prj.conf │ └── trigonometry/ # Trig functions (9 tests) │ ├── test_sin_32b_fixed_ztest.c │ ├── test_cos_32b_fixed_ztest.c │ ├── test_sin_16b_fixed_ztest.c │ ├── test_cos_16b_fixed_ztest.c │ ├── test_asin_32b_fixed_ztest.c │ ├── test_acos_32b_fixed_ztest.c │ ├── test_asin_16b_fixed_ztest.c │ ├── test_acos_16b_fixed_ztest.c │ ├── test_lut_sin_16b_fixed_ztest.c │ ├── CMakeLists.txt │ ├── testcase.yaml │ └── prj.conf ├── advanced/ # Complex mathematical operations │ ├── functions/ # Advanced math functions (6 tests) │ │ ├── test_scalar_power_ztest.c │ │ ├── test_base2_logarithm_ztest.c │ │ ├── test_exponential_ztest.c │ │ ├── test_square_root_ztest.c │ │ ├── test_base_10_logarithm_ztest.c │ │ ├── test_base_e_logarithm_ztest.c │ │ ├── CMakeLists.txt │ │ ├── testcase.yaml │ │ └── prj.conf │ └── codecs/ # Audio codec functions (2 tests) │ ├── test_a_law_codec_ztest.c │ ├── test_mu_law_codec_ztest.c │ ├── CMakeLists.txt │ ├── testcase.yaml │ └── prj.conf └── dsp/ # Digital Signal Processing (5 tests) ├── transforms/ # FFT, DCT (2 tests) │ ├── test_fft_ztest.c │ ├── test_dct_ztest.c │ ├── CMakeLists.txt │ ├── testcase.yaml │ └── prj.conf ├── processing/ # Window, Matrix, Auditory (3 tests) │ ├── test_window_ztest.c │ ├── test_matrix_ztest.c │ ├── test_auditory_ztest.c │ ├── CMakeLists.txt │ ├── testcase.yaml │ └── prj.conf └── shared/ # Common DSP test utilities ├── math_test_data.h # Shared test vectors └── dsp_test_utils.h # Common DSP test functionsMigration Phases
Phase 2A: Basic Math Functions (12 tests)
- Priority: High (foundation for other math tests)
- Target Platform:
native_simonly - Complexity: Low - simple numerical operations
- Dependencies: Minimal SOF dependencies
math/basic/arithmetic/ (6 tests) math/basic/trigonometry/ (9 tests)Phase 2B: Advanced Math Functions (8 tests)
- Priority: Medium
- Target Platform:
native_simonly - Complexity: Medium - more complex algorithms
- Dependencies: SOF math libraries
math/advanced/functions/ (6 tests) math/advanced/codecs/ (2 tests)Phase 2C: DSP Functions (8 tests)
- Priority: Medium-High (used by audio components)
- Target Platform:
native_simprimarily, may need additional platforms - Complexity: High - complex algorithms with reference data
- Dependencies: SOF DSP libraries, test reference data
math/dsp/transforms/ (2 tests) math/dsp/processing/ (3 tests)To be done after the
native_simport is completed:- Refactor functions in sof/src/math/numbers.c: constants as function arguments + update documentation
- Add HiFi platform-specific tests for exp_fcn_hifi.c optimized code paths
- Extend UT for fat-get with
userspacecases as described in the comment: fast-get: fix partition leak for multi-thread usage #10606 (comment)
@tmleman fyi - the test bench will still run the modules and infra on host meaning we will still have the valgrind testing available for most code.
@lgirdwood - First PR submitted for legacy test cleanup #10367
Removes legacy cmocka tests for
listandfast-getcomponents after coverage verification:- List: 25/25 lines (ztest) vs 22/22 lines (legacy) + function tracking
- Fast-get: 70/76 lines + 5 functions (ztest) vs 69/72 lines + 3 functions (legacy)
No regression detected. All removed tests have equivalent ztest implementations with identical test logic.
Feature Request
Problem Description
The current SOF unit test infrastructure uses CMock/CMocka framework, which creates problems:
Proposed Solution
Migrate all 56 existing CMock-based unit tests to Zephyr's native ztest framework:
Goals:
Key Features:
Current State
not yet integrated)Platform Configurations
unit_test_defconfigacp_6_3_defconfigacp_7_0_defconfigimx8_defconfigimx8m_defconfigimx8ulp_defconfigimx8x_defconfigmt8186_defconfigmt8188_defconfigmt8195_defconfigmt8196_defconfigmt8365_defconfigrembrandt_defconfigrenoir_defconfigvangogh_defconfigNote: All platform configurations are built in CI using native host compilers (not cross-compilation). The tests executed across different platform configurations are largely identical, with minimal platform-specific differences. This results in significant redundancy in CI builds, where the same test logic is compiled and executed multiple times with only minor configuration variations. The proposed ztest architecture aims to eliminate this redundancy by distinguishing between basic unit tests (native_sim only) and platform-specific integration tests.
Complete Test Migration Status
Audio Components (18 tests)
buffer_copybuffer_newbuffer_wrapbuffer_writecomp_set_statepcm_float_genericmixerpipeline_newpipeline_connect_upstreampipeline_freevolume_processmux_get_processing_functionmux_copydemux_copyselector_testeq_iir_processeq_fir_processdrc_math_testLibrary Functions (10 tests)
rstrcmprstrlenfast-get-testslist_initlist_is_emptylist_item_appendlist_item_dellist_item_is_lastlist_item_prependlist_itemMathematical Functions (28 tests)
Arithmetic Functions (6 tests)
gcdceil_dividefind_equal_int16find_min_int16find_max_abs_int32norm_int32Trigonometry Functions (9 tests)
sin_32b_fixedcos_32b_fixedsin_16b_fixedcos_16b_fixedasin_32b_fixedacos_32b_fixedasin_16b_fixedacos_16b_fixedlut_sin_16b_fixedAdvanced Math Functions (6 tests)
scalar_powerbase2_logarithmexponentialsquare_rootbase_10_logarithmbase_e_logarithmAudio Processing Math (7 tests)
a_law_codecmu_law_codecfftwindowmatrixauditorydctStatus Legend:
Implementation Plan
Phase-Based Approach
Phase 1PoC IntegrationMerge PoC to main, establish dual architecturePhase 2Basic Unit TestsMath, string, library functions (native_sim only)Test Architecture
Basic Unit Tests (
sof/test/ztest/unit/)native_simonlyIntegration Tests (
sof/test/ztest/integration/)native_simin CI, with additional platforms enabled as needed (subset of current 15 configurations)Migration Approach:
native_simplatform and executed in CISuccess Criteria
Benefits