[Concept,16/21] test: Add a private buffer for tests
Commit Message
From: Simon Glass <simon.glass@canonical.com>
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>
---
doc/develop/tests_writing.rst | 14 ++++++++++++++
include/test/test.h | 3 +++
2 files changed, 17 insertions(+)
@@ -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
--------------------
@@ -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 */