Skip to content

Commit 0bac709

Browse files
committed
test: ztest: Add scalar power unit test converted from CMock
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_power_fixed: Fixed-point scalar power function Original test converted from sof/test/cmocka/src/math/arithmetic/scalar_power.c authored by: - Shriram Shastry <malladi.sastry@linux.intel.com> The converted test validates the same power_int32() function from src/math/power.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 384 combinations (64 base values × 6 exponent values) using MATLAB-generated reference data with fixed-point arithmetic: - Base values: Q6.25 format (range -1.0 to 1.0) - Exponent values: Q2.29 format - Results: Q16.15 format - Tolerance: max error 0.000034912111005, THD+N -96.457180359025074 This is part of the broader SOF unit test migration from CMock to Zephyr Ztest framework, establishing the foundation for math/advanced/functions tests in the new directory structure. Signed-off-by: Tomasz Leman <tomasz.m.leman@intel.com>
1 parent e3b54a3 commit 0bac709

2 files changed

Lines changed: 91 additions & 0 deletions

File tree

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
cmake_minimum_required(VERSION 3.20.0)
2+
3+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
4+
project(test_math_advanced_functions)
5+
6+
set(SOF_ROOT "${PROJECT_SOURCE_DIR}/../../../../../..")
7+
8+
# Include SOF CMake utilities for proper SOF source compilation
9+
include(${SOF_ROOT}/scripts/cmake/misc.cmake)
10+
11+
target_include_directories(app PRIVATE
12+
${SOF_ROOT}/zephyr/include
13+
${SOF_ROOT}/src/include
14+
${SOF_ROOT}/src/platform/library/include
15+
${SOF_ROOT}/test/cmocka/include
16+
)
17+
18+
# Define SOF-specific configurations for unit testing
19+
target_compile_definitions(app PRIVATE
20+
-DCONFIG_SOF_LOG_LEVEL=CONFIG_LOG_DEFAULT_LEVEL
21+
-DCONFIG_ZEPHYR_POSIX=1
22+
-DCONFIG_LIBRARY=1
23+
-DUNIT_TEST=1
24+
)
25+
26+
target_sources(app PRIVATE
27+
test_scalar_power_ztest.c
28+
${SOF_ROOT}/src/math/power.c
29+
)
30+
31+
# Apply SOF relative path definitions for proper compilation
32+
sof_append_relative_path_definitions(app)
33+
34+
# Link math library for advanced math functions
35+
target_link_libraries(app PRIVATE m)
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// SPDX-License-Identifier: BSD-3-Clause
2+
//
3+
// Copyright(c) 2025 Intel Corporation. All rights reserved.
4+
//
5+
// These contents may have been developed with support from one or more Intel-operated
6+
// generative artificial intelligence solutions.
7+
//
8+
// Converted from CMock to Ztest
9+
//
10+
// Original test from sof/test/cmocka/src/math/arithmetic/scalar_power.c:
11+
// Author: Shriram Shastry <malladi.sastry@linux.intel.com>
12+
13+
#include <zephyr/ztest.h>
14+
#include <zephyr/logging/log.h>
15+
#include <sof/audio/format.h>
16+
#include <sof/math/power.h>
17+
#include <sof/common.h>
18+
#include <math.h>
19+
20+
LOG_MODULE_REGISTER(test_scalar_power, LOG_LEVEL_INF);
21+
22+
/* Test data tables from MATLAB-generated reference */
23+
#include "power_tables.h"
24+
25+
/* Error tolerance: max error = 0.000034912111005, THD+N = -96.457180359025074 */
26+
#define CMP_TOLERANCE 0.0000150363575813f
27+
28+
/**
29+
* @brief Test scalar power function with fixed-point arithmetic
30+
*
31+
* This test validates the power_int32() function against MATLAB-generated
32+
* reference values. It tests 64 base values against 6 exponent values,
33+
* checking that the fixed-point power calculation stays within acceptable
34+
* tolerance.
35+
*
36+
* Base values: Fixed-point Q6.25 format (range -1.0 to 1.0)
37+
* Exponent values: Fixed-point Q2.29 format
38+
* Result: Fixed-point Q16.15 format
39+
*/
40+
ZTEST(math_advanced_functions_suite, test_math_arithmetic_power_fixed)
41+
{
42+
double p;
43+
int i;
44+
int j;
45+
46+
for (i = 0; i < ARRAY_SIZE(b); i++) {
47+
for (j = 0; j < ARRAY_SIZE(e); j++) {
48+
p = power_int32(b[i], e[j]);
49+
float delta = fabsf((float)(power_table[i][j] - (double)p / (1 << 15)));
50+
51+
zassert_true(delta <= CMP_TOLERANCE);
52+
}
53+
}
54+
}
55+
56+
ZTEST_SUITE(math_advanced_functions_suite, NULL, NULL, NULL, NULL, NULL);

0 commit comments

Comments
 (0)