Skip to content

Commit bed24a7

Browse files
tmlemankv2019i
authored andcommitted
test: ztest: Add fast-get unit tests converted from CMock
Convert legacy CMock-based fast-get unit tests to Zephyr Ztest framework. This patch converts the existing fast-get unit tests from CMock/Unity to Zephyr's Ztest framework, maintaining the same test coverage and functionality: - test_simple_fast_get_put: Basic fast_get/fast_put functionality - test_fast_get_size_missmatch_test: Size mismatch error handling - test_over_32_fast_gets_and_puts: Multiple allocation handling (>32) - test_fast_get_refcounting: Reference counting validation The converted tests validate the same fast-get library functionality as the original CMock tests, ensuring no regression in test coverage during the migration to Ztest framework. Signed-off-by: Tomasz Leman <tomasz.m.leman@intel.com>
1 parent 9a52302 commit bed24a7

File tree

4 files changed

+243
-0
lines changed

4 files changed

+243
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
cmake_minimum_required(VERSION 3.20.0)
2+
3+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
4+
project(test_fast_get)
5+
6+
set(SOF_ROOT "${PROJECT_SOURCE_DIR}/../../../..")
7+
8+
# Include SOF CMake functions
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/posix/include
15+
)
16+
17+
# Define SOF-specific configurations for unit testing
18+
target_compile_definitions(app PRIVATE
19+
-DCONFIG_SOF_LOG_LEVEL=CONFIG_LOG_DEFAULT_LEVEL
20+
-DCONFIG_ZEPHYR_POSIX=1
21+
)
22+
23+
target_sources(app PRIVATE
24+
test_fast_get_ztest.c
25+
${SOF_ROOT}/zephyr/lib/fast-get.c
26+
)
27+
28+
target_link_libraries(app PRIVATE "-Wl,--wrap=rzalloc,--wrap=rmalloc,--wrap=rfree")
29+
30+
# Add RELATIVE_FILE definitions for SOF trace functionality
31+
sof_append_relative_path_definitions(app)

test/ztest/unit/fast-get/prj.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CONFIG_ZTEST=y
Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
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+
#include <zephyr/ztest.h>
9+
#include <sof/lib/fast-get.h>
10+
#include <stdlib.h>
11+
12+
static const int testdata[33][100] = {
13+
{
14+
1, 2, 3, 4, 5, 6, 7, 9, 0,
15+
1, 2, 3, 4, 5, 6, 7, 9, 0,
16+
1, 2, 3, 4, 5, 6, 7, 9, 0,
17+
1, 2, 3, 4, 5, 6, 7, 9, 0,
18+
1, 2, 3, 4, 5, 6, 7, 9, 0,
19+
1, 2, 3, 4, 5, 6, 7, 9, 0,
20+
1, 2, 3, 4, 5, 6, 7, 9, 0,
21+
1, 2, 3, 4, 5, 6, 7, 9, 0,
22+
1, 2, 3, 4, 5, 6, 7, 9, 0,
23+
1, 2, 3, 4, 5, 6, 7, 9, 0,
24+
},
25+
{ 2 },
26+
{ 3 },
27+
{ 4 },
28+
{ 5 },
29+
{ 6 },
30+
{ 7 },
31+
{ 8 },
32+
{ 9 },
33+
{ 10 },
34+
{ 11 },
35+
{ 12 },
36+
{ 13 },
37+
{ 14 },
38+
{ 15 },
39+
{ 16 },
40+
{ 17 },
41+
{ 18 },
42+
{ 19 },
43+
{ 20 },
44+
{ 21 },
45+
{ 23 },
46+
{ 24 },
47+
{ 25 },
48+
{ 26 },
49+
{ 27 },
50+
{ 28 },
51+
{ 29 },
52+
{ 30 },
53+
{ 31 },
54+
{ 32 },
55+
{ 33 },
56+
};
57+
58+
/* Mock memory allocation functions for testing purposes */
59+
60+
void *__wrap_rzalloc(uint32_t flags, size_t bytes)
61+
{
62+
void *ret;
63+
(void)flags;
64+
65+
ret = malloc(bytes);
66+
67+
zassert_not_null(ret, "Memory allocation should not fail");
68+
69+
memset(ret, 0, bytes);
70+
71+
return ret;
72+
}
73+
74+
void *__wrap_rmalloc(uint32_t flags, size_t bytes)
75+
{
76+
void *ret;
77+
(void)flags;
78+
79+
ret = malloc(bytes);
80+
81+
zassert_not_null(ret, "Memory allocation should not fail");
82+
83+
return ret;
84+
}
85+
86+
void __wrap_rfree(void *ptr)
87+
{
88+
free(ptr);
89+
}
90+
91+
/**
92+
* @brief Test basic fast_get and fast_put functionality
93+
*
94+
* Tests that fast_get can allocate memory for data and fast_put can free it.
95+
* Verifies that the returned pointer is valid and data is correctly copied.
96+
*/
97+
ZTEST(fast_get_suite, test_simple_fast_get_put)
98+
{
99+
const void *ret;
100+
101+
ret = fast_get(testdata[0], sizeof(testdata[0]));
102+
103+
zassert_not_null(ret, "fast_get should return valid pointer");
104+
zassert_mem_equal(ret, testdata[0], sizeof(testdata[0]),
105+
"Returned data should match original data");
106+
107+
fast_put(ret);
108+
}
109+
110+
/**
111+
* @brief Test fast_get size mismatch behavior
112+
*
113+
* Tests that fast_get returns NULL when requested size doesn't match
114+
* previously allocated size for the same data pointer.
115+
*/
116+
ZTEST(fast_get_suite, test_fast_get_size_missmatch_test)
117+
{
118+
const void *ret[2];
119+
120+
ret[0] = fast_get(testdata[0], sizeof(testdata[0]));
121+
122+
zassert_not_null(ret[0], "First fast_get should succeed");
123+
zassert_mem_equal(ret[0], testdata[0], sizeof(testdata[0]),
124+
"Returned data should match original data");
125+
126+
ret[1] = fast_get(testdata[0], sizeof(testdata[0]) + 1);
127+
zassert_is_null(ret[1], "fast_get with different size should return NULL");
128+
129+
fast_put(ret[0]);
130+
}
131+
132+
/**
133+
* @brief Test multiple fast_get and fast_put operations
134+
*
135+
* Tests that fast_get can handle more than 32 allocations and that
136+
* all data is correctly stored and can be retrieved.
137+
*/
138+
ZTEST(fast_get_suite, test_over_32_fast_gets_and_puts)
139+
{
140+
const void *copy[ARRAY_SIZE(testdata)];
141+
int i;
142+
143+
for (i = 0; i < ARRAY_SIZE(copy); i++)
144+
copy[i] = fast_get(testdata[i], sizeof(testdata[0]));
145+
146+
for (i = 0; i < ARRAY_SIZE(copy); i++)
147+
zassert_mem_equal(copy[i], testdata[i], sizeof(testdata[0]),
148+
"Data at index %d should match original", i);
149+
150+
for (i = 0; i < ARRAY_SIZE(copy); i++)
151+
fast_put(copy[i]);
152+
}
153+
154+
/**
155+
* @brief Test fast_get reference counting functionality
156+
*
157+
* Tests that fast_get implements proper reference counting - multiple
158+
* fast_get calls for the same data should return the same pointer,
159+
* and the data should remain valid until all references are released.
160+
*/
161+
ZTEST(fast_get_suite, test_fast_get_refcounting)
162+
{
163+
const void *copy[2][ARRAY_SIZE(testdata)];
164+
int i;
165+
166+
for (i = 0; i < ARRAY_SIZE(copy[0]); i++)
167+
copy[0][i] = fast_get(testdata[i], sizeof(testdata[0]));
168+
169+
for (i = 0; i < ARRAY_SIZE(copy[0]); i++)
170+
copy[1][i] = fast_get(testdata[i], sizeof(testdata[0]));
171+
172+
for (i = 0; i < ARRAY_SIZE(copy[0]); i++)
173+
zassert_equal_ptr(copy[0][i], copy[1][i],
174+
"Same data should return same pointer (refcounting)");
175+
176+
for (i = 0; i < ARRAY_SIZE(copy[0]); i++)
177+
zassert_mem_equal(copy[0][i], testdata[i], sizeof(testdata[0]),
178+
"Data should match original after multiple fast_get calls");
179+
180+
/* Release first set of references */
181+
for (i = 0; i < ARRAY_SIZE(copy[0]); i++)
182+
fast_put(copy[0][i]);
183+
184+
/* Data should still be valid through second set of references */
185+
for (i = 0; i < ARRAY_SIZE(copy[0]); i++)
186+
zassert_mem_equal(copy[1][i], testdata[i], sizeof(testdata[0]),
187+
"Data should remain valid after partial fast_put");
188+
189+
/* Release second set of references */
190+
for (i = 0; i < ARRAY_SIZE(copy[0]); i++)
191+
fast_put(copy[1][i]);
192+
}
193+
194+
/**
195+
* @brief Define and initialize the fast_get test suite
196+
*/
197+
ZTEST_SUITE(fast_get_suite, NULL, NULL, NULL, NULL, NULL);
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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+
tests:
9+
fast_get.basic_functionality:
10+
tags: fast_get memory cache
11+
platform_allow: native_sim
12+
integration_platforms:
13+
- native_sim
14+
build_only: false

0 commit comments

Comments
 (0)