test: Add a private buffer for tests

Add a priv[] buffer to struct unit_test_state that tests can use for
their own data. This avoids the need to allocate memory or use global
variables for test-specific state.

Co-developed-by: Claude <noreply@anthropic.com>
Signed-off-by: Simon Glass <simon.glass@canonical.com>
This commit is contained in:
Simon Glass
2025-12-12 15:18:38 -07:00
parent a9d6c0322e
commit f4e11a4052
2 changed files with 17 additions and 0 deletions

View File

@@ -486,6 +486,20 @@ ut_check_delta(last)
Return the change in free memory since ``last`` was obtained from
``ut_check_free()``. A positive value means more memory has been allocated.
Private buffer
~~~~~~~~~~~~~~
Each test has access to a private buffer ``uts->priv`` (256 bytes) for temporary
data. This avoids the need to allocate memory or use global variables::
static int my_test(struct unit_test_state *uts)
{
snprintf(uts->priv, sizeof(uts->priv), "/%s", filename);
/* use uts->priv as a path string */
return 0;
}
Writing Python tests
--------------------

View File

@@ -10,6 +10,7 @@
#include <linux/bitops.h>
#define UT_MAX_ARGS 8
#define UT_PRIV_SIZE 256
/**
* struct ut_stats - Statistics about tests run
@@ -95,6 +96,7 @@ struct ut_arg {
* @args: Parsed argument values for current test
* @arg_count: Number of parsed arguments
* @arg_error: Set if ut_str/int/bool() detects a type mismatch
* @priv: Private data for tests to use as needed
*/
struct unit_test_state {
struct ut_stats cur;
@@ -124,6 +126,7 @@ struct unit_test_state {
struct ut_arg args[UT_MAX_ARGS];
int arg_count;
bool arg_error;
char priv[UT_PRIV_SIZE];
};
/* Test flags for each test */