Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions test/ztest/unit/fast-get/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
cmake_minimum_required(VERSION 3.20.0)

find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(test_fast_get)

set(SOF_ROOT "${PROJECT_SOURCE_DIR}/../../../..")

# Include SOF CMake functions
include(${SOF_ROOT}/scripts/cmake/misc.cmake)

target_include_directories(app PRIVATE
${SOF_ROOT}/zephyr/include
${SOF_ROOT}/src/include
${SOF_ROOT}/src/platform/posix/include
)

# Define SOF-specific configurations for unit testing
target_compile_definitions(app PRIVATE
-DCONFIG_SOF_LOG_LEVEL=CONFIG_LOG_DEFAULT_LEVEL
-DCONFIG_ZEPHYR_POSIX=1
)

target_sources(app PRIVATE
test_fast_get_ztest.c
${SOF_ROOT}/zephyr/lib/fast-get.c
)

target_link_libraries(app PRIVATE "-Wl,--wrap=rzalloc,--wrap=rmalloc,--wrap=rfree")

# Add RELATIVE_FILE definitions for SOF trace functionality
sof_append_relative_path_definitions(app)
1 change: 1 addition & 0 deletions test/ztest/unit/fast-get/prj.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CONFIG_ZTEST=y
197 changes: 197 additions & 0 deletions test/ztest/unit/fast-get/test_fast_get_ztest.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
// SPDX-License-Identifier: BSD-3-Clause
//
// Copyright(c) 2025 Intel Corporation. All rights reserved.
//
// These contents may have been developed with support from one or more Intel-operated
// generative artificial intelligence solutions.

#include <zephyr/ztest.h>
#include <sof/lib/fast-get.h>
#include <stdlib.h>

static const int testdata[33][100] = {
{
1, 2, 3, 4, 5, 6, 7, 9, 0,
1, 2, 3, 4, 5, 6, 7, 9, 0,
1, 2, 3, 4, 5, 6, 7, 9, 0,
1, 2, 3, 4, 5, 6, 7, 9, 0,
1, 2, 3, 4, 5, 6, 7, 9, 0,
1, 2, 3, 4, 5, 6, 7, 9, 0,
1, 2, 3, 4, 5, 6, 7, 9, 0,
1, 2, 3, 4, 5, 6, 7, 9, 0,
1, 2, 3, 4, 5, 6, 7, 9, 0,
1, 2, 3, 4, 5, 6, 7, 9, 0,
},
{ 2 },
{ 3 },
{ 4 },
{ 5 },
{ 6 },
{ 7 },
{ 8 },
{ 9 },
{ 10 },
{ 11 },
{ 12 },
{ 13 },
{ 14 },
{ 15 },
{ 16 },
{ 17 },
{ 18 },
{ 19 },
{ 20 },
{ 21 },
Copy link

Copilot AI Jul 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The testdata array has a gap in the sequence - it jumps from 21 to 23, skipping 22. This could indicate a copy-paste error or intentional omission that should be documented.

Suggested change
{ 21 },
{ 21 },
{ 22 },

Copilot uses AI. Check for mistakes.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tmleman If intentional skip, this would warrant a comment.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The data comes from the original test: https://github.com/thesofproject/sof/blob/main/test/cmocka/src/lib/fast-get/fast-get-tests.c#L54-L57
I'll check the history of those tests to see if I can find any justification for this. There's nothing in the code.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kv2019i git didn't tell me anything interesting. The test has looked like this from the beginning, and there doesn't seem to be any hidden meaning behind it.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ack @tmleman . It looks like a bug, but maybe fix in a follow-up PR and move the test code as-is in this one.

{ 23 },
{ 24 },
{ 25 },
{ 26 },
{ 27 },
{ 28 },
{ 29 },
{ 30 },
{ 31 },
{ 32 },
{ 33 },
};

/* Mock memory allocation functions for testing purposes */

void *__wrap_rzalloc(uint32_t flags, size_t bytes)
{
void *ret;
(void)flags;

ret = malloc(bytes);

zassert_not_null(ret, "Memory allocation should not fail");

memset(ret, 0, bytes);

return ret;
}

void *__wrap_rmalloc(uint32_t flags, size_t bytes)
{
void *ret;
(void)flags;

ret = malloc(bytes);

zassert_not_null(ret, "Memory allocation should not fail");

return ret;
}

void __wrap_rfree(void *ptr)
{
free(ptr);
}

/**
* @brief Test basic fast_get and fast_put functionality
*
* Tests that fast_get can allocate memory for data and fast_put can free it.
* Verifies that the returned pointer is valid and data is correctly copied.
*/
ZTEST(fast_get_suite, test_simple_fast_get_put)
{
const void *ret;

ret = fast_get(testdata[0], sizeof(testdata[0]));

zassert_not_null(ret, "fast_get should return valid pointer");
zassert_mem_equal(ret, testdata[0], sizeof(testdata[0]),
"Returned data should match original data");

fast_put(ret);
}

/**
* @brief Test fast_get size mismatch behavior
*
* Tests that fast_get returns NULL when requested size doesn't match
* previously allocated size for the same data pointer.
*/
ZTEST(fast_get_suite, test_fast_get_size_missmatch_test)
Copy link

Copilot AI Jul 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function name contains a spelling error: 'missmatch' should be 'mismatch'.

Suggested change
ZTEST(fast_get_suite, test_fast_get_size_missmatch_test)
ZTEST(fast_get_suite, test_fast_get_size_mismatch_test)

Copilot uses AI. Check for mistakes.
{
const void *ret[2];

ret[0] = fast_get(testdata[0], sizeof(testdata[0]));

zassert_not_null(ret[0], "First fast_get should succeed");
zassert_mem_equal(ret[0], testdata[0], sizeof(testdata[0]),
"Returned data should match original data");

ret[1] = fast_get(testdata[0], sizeof(testdata[0]) + 1);
zassert_is_null(ret[1], "fast_get with different size should return NULL");

fast_put(ret[0]);
}

/**
* @brief Test multiple fast_get and fast_put operations
*
* Tests that fast_get can handle more than 32 allocations and that
* all data is correctly stored and can be retrieved.
*/
ZTEST(fast_get_suite, test_over_32_fast_gets_and_puts)
{
const void *copy[ARRAY_SIZE(testdata)];
int i;

for (i = 0; i < ARRAY_SIZE(copy); i++)
copy[i] = fast_get(testdata[i], sizeof(testdata[0]));

for (i = 0; i < ARRAY_SIZE(copy); i++)
zassert_mem_equal(copy[i], testdata[i], sizeof(testdata[0]),
Comment on lines +144 to +147
Copy link

Copilot AI Jul 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using sizeof(testdata[0]) for all array elements is incorrect. testdata[0] has 100 elements while testdata[1] through testdata[32] only have 1 element each. This should use sizeof(testdata[i]) to get the correct size for each test data array.

Suggested change
copy[i] = fast_get(testdata[i], sizeof(testdata[0]));
for (i = 0; i < ARRAY_SIZE(copy); i++)
zassert_mem_equal(copy[i], testdata[i], sizeof(testdata[0]),
copy[i] = fast_get(testdata[i], sizeof(testdata[i]));
for (i = 0; i < ARRAY_SIZE(copy); i++)
zassert_mem_equal(copy[i], testdata[i], sizeof(testdata[i]),

Copilot uses AI. Check for mistakes.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

copilot forgot which language it's reviewing. False positive.

Comment on lines +144 to +147
Copy link

Copilot AI Jul 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using sizeof(testdata[0]) for memory comparison is incorrect for elements beyond testdata[0]. This will read beyond the bounds of testdata[1] through testdata[32] which only contain 1 element each. Should use sizeof(testdata[i]).

Suggested change
copy[i] = fast_get(testdata[i], sizeof(testdata[0]));
for (i = 0; i < ARRAY_SIZE(copy); i++)
zassert_mem_equal(copy[i], testdata[i], sizeof(testdata[0]),
copy[i] = fast_get(testdata[i], sizeof(testdata[i]));
for (i = 0; i < ARRAY_SIZE(copy); i++)
zassert_mem_equal(copy[i], testdata[i], sizeof(testdata[i]),

Copilot uses AI. Check for mistakes.
"Data at index %d should match original", i);

for (i = 0; i < ARRAY_SIZE(copy); i++)
fast_put(copy[i]);
}

/**
* @brief Test fast_get reference counting functionality
*
* Tests that fast_get implements proper reference counting - multiple
* fast_get calls for the same data should return the same pointer,
* and the data should remain valid until all references are released.
*/
ZTEST(fast_get_suite, test_fast_get_refcounting)
{
const void *copy[2][ARRAY_SIZE(testdata)];
int i;

for (i = 0; i < ARRAY_SIZE(copy[0]); i++)
copy[0][i] = fast_get(testdata[i], sizeof(testdata[0]));

for (i = 0; i < ARRAY_SIZE(copy[0]); i++)
copy[1][i] = fast_get(testdata[i], sizeof(testdata[0]));

for (i = 0; i < ARRAY_SIZE(copy[0]); i++)
zassert_equal_ptr(copy[0][i], copy[1][i],
"Same data should return same pointer (refcounting)");

for (i = 0; i < ARRAY_SIZE(copy[0]); i++)
zassert_mem_equal(copy[0][i], testdata[i], sizeof(testdata[0]),
Comment on lines +167 to +177
Copy link

Copilot AI Jul 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using sizeof(testdata[0]) for all elements is incorrect. Should use sizeof(testdata[i]) to get the correct size for each individual test data array.

Suggested change
copy[0][i] = fast_get(testdata[i], sizeof(testdata[0]));
for (i = 0; i < ARRAY_SIZE(copy[0]); i++)
copy[1][i] = fast_get(testdata[i], sizeof(testdata[0]));
for (i = 0; i < ARRAY_SIZE(copy[0]); i++)
zassert_equal_ptr(copy[0][i], copy[1][i],
"Same data should return same pointer (refcounting)");
for (i = 0; i < ARRAY_SIZE(copy[0]); i++)
zassert_mem_equal(copy[0][i], testdata[i], sizeof(testdata[0]),
copy[0][i] = fast_get(testdata[i], sizeof(testdata[i]));
for (i = 0; i < ARRAY_SIZE(copy[0]); i++)
copy[1][i] = fast_get(testdata[i], sizeof(testdata[i]));
for (i = 0; i < ARRAY_SIZE(copy[0]); i++)
zassert_equal_ptr(copy[0][i], copy[1][i],
"Same data should return same pointer (refcounting)");
for (i = 0; i < ARRAY_SIZE(copy[0]); i++)
zassert_mem_equal(copy[0][i], testdata[i], sizeof(testdata[i]),

Copilot uses AI. Check for mistakes.
Comment on lines +167 to +177
Copy link

Copilot AI Jul 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using sizeof(testdata[0]) for all elements is incorrect. Should use sizeof(testdata[i]) to get the correct size for each individual test data array.

Suggested change
copy[0][i] = fast_get(testdata[i], sizeof(testdata[0]));
for (i = 0; i < ARRAY_SIZE(copy[0]); i++)
copy[1][i] = fast_get(testdata[i], sizeof(testdata[0]));
for (i = 0; i < ARRAY_SIZE(copy[0]); i++)
zassert_equal_ptr(copy[0][i], copy[1][i],
"Same data should return same pointer (refcounting)");
for (i = 0; i < ARRAY_SIZE(copy[0]); i++)
zassert_mem_equal(copy[0][i], testdata[i], sizeof(testdata[0]),
copy[0][i] = fast_get(testdata[i], sizeof(testdata[i]));
for (i = 0; i < ARRAY_SIZE(copy[0]); i++)
copy[1][i] = fast_get(testdata[i], sizeof(testdata[i]));
for (i = 0; i < ARRAY_SIZE(copy[0]); i++)
zassert_equal_ptr(copy[0][i], copy[1][i],
"Same data should return same pointer (refcounting)");
for (i = 0; i < ARRAY_SIZE(copy[0]); i++)
zassert_mem_equal(copy[0][i], testdata[i], sizeof(testdata[i]),

Copilot uses AI. Check for mistakes.
Comment on lines +167 to +177
Copy link

Copilot AI Jul 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using sizeof(testdata[0]) for memory comparison will cause buffer overread for testdata[1] through testdata[32]. Should use sizeof(testdata[i]).

Suggested change
copy[0][i] = fast_get(testdata[i], sizeof(testdata[0]));
for (i = 0; i < ARRAY_SIZE(copy[0]); i++)
copy[1][i] = fast_get(testdata[i], sizeof(testdata[0]));
for (i = 0; i < ARRAY_SIZE(copy[0]); i++)
zassert_equal_ptr(copy[0][i], copy[1][i],
"Same data should return same pointer (refcounting)");
for (i = 0; i < ARRAY_SIZE(copy[0]); i++)
zassert_mem_equal(copy[0][i], testdata[i], sizeof(testdata[0]),
copy[0][i] = fast_get(testdata[i], sizeof(testdata[i]));
for (i = 0; i < ARRAY_SIZE(copy[0]); i++)
copy[1][i] = fast_get(testdata[i], sizeof(testdata[i]));
for (i = 0; i < ARRAY_SIZE(copy[0]); i++)
zassert_equal_ptr(copy[0][i], copy[1][i],
"Same data should return same pointer (refcounting)");
for (i = 0; i < ARRAY_SIZE(copy[0]); i++)
zassert_mem_equal(copy[0][i], testdata[i], sizeof(testdata[i]),

Copilot uses AI. Check for mistakes.
"Data should match original after multiple fast_get calls");

/* Release first set of references */
for (i = 0; i < ARRAY_SIZE(copy[0]); i++)
fast_put(copy[0][i]);

/* Data should still be valid through second set of references */
for (i = 0; i < ARRAY_SIZE(copy[0]); i++)
zassert_mem_equal(copy[1][i], testdata[i], sizeof(testdata[0]),
Copy link

Copilot AI Jul 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using sizeof(testdata[0]) for memory comparison will cause buffer overread for testdata[1] through testdata[32]. Should use sizeof(testdata[i]).

Suggested change
zassert_mem_equal(copy[1][i], testdata[i], sizeof(testdata[0]),
zassert_mem_equal(copy[1][i], testdata[i], sizeof(testdata[i]),

Copilot uses AI. Check for mistakes.
"Data should remain valid after partial fast_put");

/* Release second set of references */
for (i = 0; i < ARRAY_SIZE(copy[0]); i++)
fast_put(copy[1][i]);
}

/**
* @brief Define and initialize the fast_get test suite
*/
ZTEST_SUITE(fast_get_suite, NULL, NULL, NULL, NULL, NULL);
14 changes: 14 additions & 0 deletions test/ztest/unit/fast-get/testcase.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# SPDX-License-Identifier: BSD-3-Clause
#
# Copyright(c) 2025 Intel Corporation. All rights reserved.
#
# These contents may have been developed with support from one or more Intel-operated
# generative artificial intelligence solutions.

tests:
fast_get.basic_functionality:
tags: fast_get memory cache
platform_allow: native_sim
integration_platforms:
- native_sim
build_only: false
Loading