Skip to content

Commit 8a83cdf

Browse files
committed
test: ztest: Add math basic arithmetic unit tests converted from CMock
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>
1 parent 117e460 commit 8a83cdf

File tree

9 files changed

+388
-0
lines changed

9 files changed

+388
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
cmake_minimum_required(VERSION 3.20.0)
2+
3+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
4+
project(test_math_arithmetic)
5+
6+
set(SOF_ROOT "${PROJECT_SOURCE_DIR}/../../../../../..")
7+
8+
target_include_directories(app PRIVATE
9+
${SOF_ROOT}/zephyr/include
10+
${SOF_ROOT}/src/include
11+
)
12+
13+
# Define SOF-specific configurations for unit testing
14+
target_compile_definitions(app PRIVATE
15+
-DCONFIG_SOF_LOG_LEVEL=CONFIG_LOG_DEFAULT_LEVEL
16+
-DCONFIG_ZEPHYR_POSIX=1
17+
-DCONFIG_LIBRARY=1
18+
-DCONFIG_NUMBERS_VECTOR_FIND=1
19+
-DCONFIG_NUMBERS_NORM=1
20+
-DUNIT_TEST=1
21+
)
22+
23+
target_sources(app PRIVATE
24+
test_gcd_ztest.c
25+
test_ceil_divide_ztest.c
26+
test_find_equal_int16_ztest.c
27+
test_find_min_int16_ztest.c
28+
test_find_max_abs_int32_ztest.c
29+
test_norm_int32_ztest.c
30+
${SOF_ROOT}/src/math/numbers.c
31+
)
32+
33+
# Link math library for ceil_divide test
34+
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: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
#include <zephyr/ztest.h>
11+
#include <sof/math/numbers.h>
12+
#include <math.h>
13+
14+
/**
15+
* @brief Test ceil_divide function with various parameter combinations
16+
*
17+
* Tests ceil_divide against reference ceilf implementation with multiple
18+
* positive and negative integer combinations
19+
*/
20+
ZTEST(math_arithmetic_suite, test_math_numbers_ceil_divide)
21+
{
22+
int params[8] = {
23+
-1000,
24+
300,
25+
123,
26+
-10,
27+
1337,
28+
-6,
29+
999,
30+
-2
31+
};
32+
33+
int i, j;
34+
35+
for (i = 0; i < ARRAY_SIZE(params); ++i) {
36+
for (j = 0; j < ARRAY_SIZE(params); ++j) {
37+
int ref = ceilf((float)params[i] / (float)params[j]);
38+
int r = ceil_divide(params[i], params[j]);
39+
40+
zassert_equal(r, ref,
41+
"ceil_divide(%d, %d) = %d, expected %d",
42+
params[i], params[j], r, ref);
43+
}
44+
}
45+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
#include <zephyr/ztest.h>
11+
#include <sof/math/numbers.h>
12+
13+
/**
14+
* @brief Test find_equal_int16 function with array containing matches
15+
*
16+
* Tests that find_equal_int16 correctly finds all indices where value 123
17+
* appears in the array [5, 123, 5, 10, 123, 500, 123]
18+
*/
19+
ZTEST(math_arithmetic_suite, test_math_numbers_find_equal_int16_matches)
20+
{
21+
int16_t r[4];
22+
int16_t vec[] = {5, 123, 5, 10, 123, 500, 123};
23+
int16_t template[] = {1, 4, 6};
24+
25+
int r_num = find_equal_int16(r, vec, 123, 7, 4);
26+
27+
zassert_equal(r_num, 3, "Should find 3 occurrences of 123");
28+
zassert_mem_equal(r, template, sizeof(int16_t) * 3,
29+
"Result indices should match expected template");
30+
}
31+
32+
/**
33+
* @brief Test find_equal_int16 function with no matches
34+
*
35+
* Tests that find_equal_int16 returns 0 when searching for value 0
36+
* in array [1, 2, 3, 4, 5] where it doesn't exist
37+
*/
38+
ZTEST(math_arithmetic_suite, test_math_numbers_find_equal_int16_no_matches)
39+
{
40+
int16_t r[4];
41+
int16_t vec[] = {1, 2, 3, 4, 5};
42+
43+
int r_num = find_equal_int16(r, vec, 0, 5, 4);
44+
45+
zassert_equal(r_num, 0, "Should find 0 occurrences of 0");
46+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
#include <zephyr/ztest.h>
11+
#include <sof/math/numbers.h>
12+
#include <sof/common.h>
13+
14+
/**
15+
* @brief Test find_max_abs_int32 function with negative maximum absolute value
16+
*
17+
* Tests that find_max_abs_int32 correctly finds maximum absolute value
18+
* in array [-100, 99, 98, 50] where -100 has the largest absolute value
19+
*/
20+
ZTEST(math_arithmetic_suite, test_math_numbers_find_max_abs_int32_negative_max)
21+
{
22+
int32_t vec[] = {-100, 99, 98, 50};
23+
int32_t r = find_max_abs_int32(vec, ARRAY_SIZE(vec));
24+
25+
zassert_equal(r, 100, "find_max_abs_int32([-100, 99, 98, 50]) should return 100");
26+
}
27+
28+
/**
29+
* @brief Test find_max_abs_int32 function with positive maximum absolute value
30+
*
31+
* Tests that find_max_abs_int32 correctly finds maximum absolute value
32+
* in array [-100, 99, 98, 50, 101] where 101 has the largest absolute value
33+
*/
34+
ZTEST(math_arithmetic_suite, test_math_numbers_find_max_abs_int32_positive_max)
35+
{
36+
int32_t vec[] = {-100, 99, 98, 50, 101};
37+
int32_t r = find_max_abs_int32(vec, ARRAY_SIZE(vec));
38+
39+
zassert_equal(r, 101, "find_max_abs_int32([-100, 99, 98, 50, 101]) should return 101");
40+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
#include <zephyr/ztest.h>
11+
#include <sof/math/numbers.h>
12+
#include <sof/common.h>
13+
14+
/**
15+
* @brief Test find_min_int16 function with single element array
16+
*
17+
* Tests that find_min_int16 returns the single element when array has one item
18+
*/
19+
ZTEST(math_arithmetic_suite, test_math_numbers_find_min_int16_for_2_equals_2)
20+
{
21+
int16_t vec[] = {2};
22+
int r = find_min_int16(vec, ARRAY_SIZE(vec));
23+
24+
zassert_equal(r, 2, "find_min_int16([2]) should return 2");
25+
}
26+
27+
/**
28+
* @brief Test find_min_int16 function with multiple elements
29+
*
30+
* Tests that find_min_int16 correctly finds minimum value in array [5, 2, 3, 4, 1]
31+
*/
32+
ZTEST(math_arithmetic_suite, test_math_numbers_find_min_int16_for_5_2_3_4_1_equals_1)
33+
{
34+
int16_t vec[] = {5, 2, 3, 4, 1};
35+
int r = find_min_int16(vec, ARRAY_SIZE(vec));
36+
37+
zassert_equal(r, 1, "find_min_int16([5, 2, 3, 4, 1]) should return 1");
38+
}
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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+
#include <zephyr/ztest.h>
11+
#include <sof/math/numbers.h>
12+
13+
/**
14+
* @brief Test GCD function with typical case
15+
*
16+
* Tests that gcd(5083, 391) returns 391
17+
*/
18+
ZTEST(math_arithmetic_suite, test_math_numbers_gcd_for_5083_and_391_equals_391)
19+
{
20+
int r;
21+
22+
r = gcd(5083, 391);
23+
zassert_equal(r, 391, "gcd(5083, 391) should equal 391");
24+
}
25+
26+
/**
27+
* @brief Test GCD function with small positive numbers
28+
*
29+
* Tests that gcd(12, 9) returns 3
30+
*/
31+
ZTEST(math_arithmetic_suite, test_math_numbers_gcd_for_12_and_9_equals_3)
32+
{
33+
int r;
34+
35+
r = gcd(12, 9);
36+
zassert_equal(r, 3, "gcd(12, 9) should equal 3");
37+
}
38+
39+
/**
40+
* @brief Test GCD function with zero second argument
41+
*
42+
* Tests that gcd(5, 0) returns 5
43+
*/
44+
ZTEST(math_arithmetic_suite, test_math_numbers_gcd_for_5_and_0_equals_5)
45+
{
46+
int r;
47+
48+
r = gcd(5, 0);
49+
zassert_equal(r, 5, "gcd(5, 0) should equal 5");
50+
}
51+
52+
/**
53+
* @brief Test GCD function with zero first argument
54+
*
55+
* Tests that gcd(0, 5) returns 5
56+
*/
57+
ZTEST(math_arithmetic_suite, test_math_numbers_gcd_for_0_and_5_equals_5)
58+
{
59+
int r;
60+
61+
r = gcd(0, 5);
62+
zassert_equal(r, 5, "gcd(0, 5) should equal 5");
63+
}
64+
65+
/**
66+
* @brief Test GCD function with both arguments zero
67+
*
68+
* Tests that gcd(0, 0) returns 0
69+
*/
70+
ZTEST(math_arithmetic_suite, test_math_numbers_gcd_for_0_and_0_equals_0)
71+
{
72+
int r;
73+
74+
r = gcd(0, 0);
75+
zassert_equal(r, 0, "gcd(0, 0) should equal 0");
76+
}
77+
78+
/**
79+
* @brief Test GCD function with negative first argument
80+
*
81+
* Tests that gcd(-4, 14) returns 2
82+
*/
83+
ZTEST(math_arithmetic_suite, test_math_numbers_gcd_for_neg_4_and_14_equals_2)
84+
{
85+
int r;
86+
87+
r = gcd(-4, 14);
88+
zassert_equal(r, 2, "gcd(-4, 14) should equal 2");
89+
}
90+
91+
/**
92+
* @brief Test GCD function with negative second argument
93+
*
94+
* Tests that gcd(4, -14) returns 2
95+
*/
96+
ZTEST(math_arithmetic_suite, test_math_numbers_gcd_for_4_and_neg_14_equals_2)
97+
{
98+
int r;
99+
100+
r = gcd(4, -14);
101+
zassert_equal(r, 2, "gcd(4, -14) should equal 2");
102+
}
103+
104+
/**
105+
* @brief Test GCD function with both arguments negative
106+
*
107+
* Tests that gcd(-4, -14) returns 2
108+
*/
109+
ZTEST(math_arithmetic_suite, test_math_numbers_gcd_for_neg_4_and_neg_14_equals_2)
110+
{
111+
int r;
112+
113+
r = gcd(-4, -14);
114+
zassert_equal(r, 2, "gcd(-4, -14) should equal 2");
115+
}
116+
117+
/**
118+
* @brief Define and initialize the math arithmetic test suite
119+
*/
120+
ZTEST_SUITE(math_arithmetic_suite, NULL, NULL, NULL, NULL, NULL);
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
#include <zephyr/ztest.h>
11+
#include <sof/math/numbers.h>
12+
13+
/**
14+
* @brief Test norm_int32 function with zero value
15+
*
16+
* Tests that norm_int32(0) returns 31 (number of leading zeros in 32-bit zero)
17+
*/
18+
ZTEST(math_arithmetic_suite, test_math_numbers_norm_int32_for_0_equals_31)
19+
{
20+
int r = norm_int32(0);
21+
22+
zassert_equal(r, 31, "norm_int32(0) should return 31");
23+
}
24+
25+
/**
26+
* @brief Test norm_int32 function with small positive value
27+
*
28+
* Tests that norm_int32(35) returns 25 (number of leading zeros)
29+
*/
30+
ZTEST(math_arithmetic_suite, test_math_numbers_norm_int32_for_35_equals_25)
31+
{
32+
int r = norm_int32(35);
33+
34+
zassert_equal(r, 25, "norm_int32(35) should return 25");
35+
}
36+
37+
/**
38+
* @brief Test norm_int32 function with maximum positive value
39+
*
40+
* Tests that norm_int32(INT32_MAX) returns 0 (no leading zeros in max int32)
41+
*/
42+
ZTEST(math_arithmetic_suite, test_math_numbers_norm_int32_for_int32_max_equals_0)
43+
{
44+
int r = norm_int32(INT32_MAX);
45+
46+
zassert_equal(r, 0, "norm_int32(INT32_MAX) should return 0");
47+
}
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 basic arithmetic unit tests converted from CMock to Ztest
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.basic.arithmetic:
13+
tags: math arithmetic numbers
14+
platform_allow: native_sim
15+
integration_platforms:
16+
- native_sim
17+
build_only: false

0 commit comments

Comments
 (0)