Skip to content

Commit 13bb7bc

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 53df9fb commit 13bb7bc

File tree

5 files changed

+117
-0
lines changed

5 files changed

+117
-0
lines changed

src/math/power.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <sof/audio/format.h>
1111
#include <sof/trace/trace.h>
1212
#include <sof/math/power.h>
13+
1314
#include <sof/lib/uuid.h>
1415
#include <ipc/trace.h>
1516
#include <user/trace.h>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
# Set SOF top directory for UUID registry generation
9+
set(sof_top_dir ${SOF_ROOT})
10+
11+
# Include SOF CMake utilities for proper SOF source compilation
12+
include(${SOF_ROOT}/scripts/cmake/misc.cmake)
13+
include(${SOF_ROOT}/scripts/cmake/uuid-registry.cmake)
14+
15+
target_include_directories(app PRIVATE
16+
${SOF_ROOT}/zephyr/include
17+
${SOF_ROOT}/src/include
18+
${SOF_ROOT}/src/platform/posix/include
19+
${SOF_ROOT}/test/cmocka/include
20+
${PROJECT_BINARY_DIR}/include/generated # For uuid-registry.h
21+
)
22+
23+
# Define SOF-specific configurations for unit testing
24+
target_compile_definitions(app PRIVATE
25+
-DCONFIG_SOF_LOG_LEVEL=CONFIG_LOG_DEFAULT_LEVEL
26+
-DCONFIG_ZEPHYR_POSIX=1
27+
-DCONFIG_LIBRARY=1
28+
-DUNIT_TEST=1
29+
)
30+
31+
target_sources(app PRIVATE
32+
test_scalar_power_ztest.c
33+
${SOF_ROOT}/src/math/power.c
34+
)
35+
36+
# Apply SOF relative path definitions for proper compilation
37+
sof_append_relative_path_definitions(app)
38+
39+
# Link math library for advanced math functions
40+
target_link_libraries(app PRIVATE m)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CONFIG_ZTEST=y
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
"Power calc error: delta=%f > %f at b[%d]=%d, e[%d]=%d",
53+
(double)delta, (double)CMP_TOLERANCE, i, b[i], j, e[j]);
54+
}
55+
}
56+
}
57+
58+
ZTEST_SUITE(math_advanced_functions_suite, NULL, NULL, NULL, NULL, NULL);
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# SPDX-License-Identifier: BSD-3-Clause
2+
#
3+
# Copyright(c) 2025 Intel Corporation. All rights reserved.
4+
#
5+
# Math advanced functions unit tests for Ztest framework
6+
#
7+
# These contents may have been developed with support from one or more Intel-operated
8+
# generative artificial intelligence solutions.
9+
#
10+
11+
tests:
12+
math.advanced.functions:
13+
tags: math advanced functions power
14+
platform_allow: native_sim
15+
integration_platforms:
16+
- native_sim
17+
build_only: false

0 commit comments

Comments
 (0)