[Concept,16/21] test: Add a private buffer for tests

Message ID 20251214175449.3799539-17-sjg@u-boot.org
State New
Headers
Series test: Add support for passing arguments to C unit tests |

Commit Message

Simon Glass Dec. 14, 2025, 5:54 p.m. UTC
  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(+)
  

Patch

diff --git a/doc/develop/tests_writing.rst b/doc/develop/tests_writing.rst
index 41612a9e21b..6842533e2c6 100644
--- a/doc/develop/tests_writing.rst
+++ b/doc/develop/tests_writing.rst
@@ -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
 --------------------
diff --git a/include/test/test.h b/include/test/test.h
index 15ed8b37890..2facf5b3675 100644
--- a/include/test/test.h
+++ b/include/test/test.h
@@ -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 */