Skip to content

Commit 1c56e1a

Browse files
tmlemanlgirdwood
authored andcommitted
test: ztest: Add base-2 logarithm 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_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>
1 parent 2bad4ec commit 1c56e1a

3 files changed

Lines changed: 79 additions & 1 deletion

File tree

test/ztest/unit/math/advanced/functions/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ target_compile_definitions(app PRIVATE
2929

3030
target_sources(app PRIVATE
3131
test_scalar_power_ztest.c
32+
test_base2_logarithm_ztest.c
3233
${SOF_ROOT}/src/math/power.c
34+
${SOF_ROOT}/src/math/base2log.c
3335
)
3436

3537
# Apply SOF relative path definitions for proper compilation
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// SPDX-License-Identifier: BSD-3-Clause
2+
//
3+
// Copyright(c) 2026 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/base2_logarithm.c
11+
// Author: Shriram Shastry <malladi.sastry@linux.intel.com>
12+
13+
#include <zephyr/ztest.h>
14+
#include <sof/audio/format.h>
15+
#include <sof/math/log.h>
16+
#include <math.h>
17+
#include <rtos/string.h>
18+
19+
/* Test data tables from MATLAB-generated reference */
20+
#include "log2_tables.h"
21+
22+
/* 'Error[max] = 0.0000236785999981,THD(-dBc) = -92.5128795787487235' */
23+
#define CMP_TOLERANCE 0.0000236785691029f
24+
25+
/* testvector in Q32.0 */
26+
static const uint32_t uv[100] = {
27+
1ULL, 43383509ULL, 86767017ULL, 130150525ULL, 173534033ULL, 216917541ULL,
28+
260301049ULL, 303684557ULL, 347068065ULL, 390451573ULL, 433835081ULL, 477218589ULL,
29+
520602097ULL, 563985605ULL, 607369113ULL, 650752621ULL, 694136129ULL, 737519638ULL,
30+
780903146ULL, 824286654ULL, 867670162ULL, 911053670ULL, 954437178ULL, 997820686ULL,
31+
1041204194ULL, 1084587702ULL, 1127971210ULL, 1171354718ULL, 1214738226ULL, 1258121734ULL,
32+
1301505242ULL, 1344888750ULL, 1388272258ULL, 1431655766ULL, 1475039274ULL, 1518422782ULL,
33+
1561806290ULL, 1605189798ULL, 1648573306ULL, 1691956814ULL, 1735340322ULL, 1778723830ULL,
34+
1822107338ULL, 1865490846ULL, 1908874354ULL, 1952257862ULL, 1995641370ULL, 2039024878ULL,
35+
2082408386ULL, 2125791894ULL, 2169175403ULL, 2212558911ULL, 2255942419ULL, 2299325927ULL,
36+
2342709435ULL, 2386092943ULL, 2429476451ULL, 2472859959ULL, 2516243467ULL, 2559626975ULL,
37+
2603010483ULL, 2646393991ULL, 2689777499ULL, 2733161007ULL, 2776544515ULL, 2819928023ULL,
38+
2863311531ULL, 2906695039ULL, 2950078547ULL, 2993462055ULL, 3036845563ULL, 3080229071ULL,
39+
3123612579ULL, 3166996087ULL, 3210379595ULL, 3253763103ULL, 3297146611ULL, 3340530119ULL,
40+
3383913627ULL, 3427297135ULL, 3470680643ULL, 3514064151ULL, 3557447659ULL, 3600831168ULL,
41+
3644214676ULL, 3687598184ULL, 3730981692ULL, 3774365200ULL, 3817748708ULL, 3861132216ULL,
42+
3904515724ULL, 3947899232ULL, 3991282740ULL, 4034666248ULL, 4078049756ULL, 4121433264ULL,
43+
4164816772ULL, 4208200280ULL, 4251583788ULL, 4294967295ULL};
44+
45+
/**
46+
* @brief Test base-2 logarithm function with fixed-point arithmetic
47+
*
48+
* This test validates the base2_logarithm() function against MATLAB-generated
49+
* reference values. It tests 100 uniformly distributed input values across
50+
* the full uint32_t range, checking that the fixed-point logarithm calculation
51+
* stays within acceptable tolerance.
52+
*
53+
* Input values: Q32.0 format (unsigned 32-bit integers)
54+
* Result: Q16.16 fixed-point format
55+
* Reference: MATLAB log2() function results
56+
*/
57+
ZTEST(math_advanced_functions_suite, test_math_arithmetic_base2log_fixed)
58+
{
59+
uint32_t u[100];
60+
int i, ret;
61+
62+
BUILD_ASSERT(ARRAY_SIZE(uv) == ARRAY_SIZE(log2_lookup_table),
63+
"Test vector size must match reference table size");
64+
65+
ret = memcpy_s((void *)&u[0], sizeof(u), (void *)&uv[0], 100U * sizeof(uint32_t));
66+
zassert_equal(ret, 0, "memcpy_s failed with error code %d", ret);
67+
68+
for (i = 0; i < ARRAY_SIZE(u); i++) {
69+
float y = Q_CONVERT_QTOF(base2_logarithm(u[i]), 0);
70+
float delta = fabsf((float)(log2_lookup_table[i] - (double)y / (1 << 16)));
71+
72+
zassert_true(delta <= CMP_TOLERANCE,
73+
"base2_logarithm(%u): delta %.16f > tolerance (expected %.16f, got %.16f)",
74+
u[i], (double)delta, log2_lookup_table[i], (double)y / (1 << 16));
75+
}
76+
}

test/ztest/unit/math/advanced/functions/testcase.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
tests:
1212
sof.unit.math.advanced.functions:
13-
tags: math advanced functions power
13+
tags: math advanced functions power logarithm base2
1414
platform_allow: native_sim
1515
integration_platforms:
1616
- native_sim

0 commit comments

Comments
 (0)