1+ /*
2+ * Copyright (c) 2006-2025, RT-Thread Development Team
3+ *
4+ * SPDX-License-Identifier: Apache-2.0
5+ *
6+ * Change Logs:
7+ * Date Author Notes
8+ * 2025-07-18 kurisaW First commit
9+ */
10+
11+ #include <utest.h>
12+ #include <rtthread.h>
13+ #include <string.h>
14+
15+ /**
16+ * @brief Test case for verifying object name handling functionality
17+ *
18+ * @note This test suite validates:
19+ * 1. Proper truncation of long object names
20+ * 2. Correct NULL name handling
21+ * 3. Exact length name preservation
22+ * 4. Both static and dynamic object initialization
23+ * 5. Memory safety and boundary conditions
24+ */
25+
26+ static void test_object_name_handling (void )
27+ {
28+ struct rt_object static_obj1 ;
29+ struct rt_object static_obj2 ;
30+ struct rt_object static_obj3 ;
31+ rt_object_t dyn_obj = RT_NULL ;
32+ char test_name [RT_NAME_MAX + 5 ];
33+
34+ for (int i = 0 ; i < sizeof (test_name ) - 1 ; i ++ )
35+ {
36+ test_name [i ] = 'A' + (i % 26 );
37+ }
38+ test_name [sizeof (test_name ) - 1 ] = '\0' ;
39+
40+ /* Test 1: Static Object Initialization - Extra Long Name */
41+ rt_object_init (& static_obj1 , RT_Object_Class_Thread , test_name );
42+ uassert_true (rt_strlen (static_obj1 .name ) <= RT_NAME_MAX - 1 );
43+ uassert_true (static_obj1 .name [RT_NAME_MAX - 1 ] == '\0' );
44+
45+ /* Test 2: Dynamic Object Allocation */
46+ dyn_obj = rt_object_allocate (RT_Object_Class_Thread , test_name );
47+ uassert_not_null (dyn_obj );
48+ if (dyn_obj )
49+ {
50+ uassert_true (rt_strlen (dyn_obj -> name ) <= RT_NAME_MAX - 1 );
51+ uassert_true (dyn_obj -> name [RT_NAME_MAX - 1 ] == '\0' );
52+ rt_object_delete (dyn_obj );
53+ dyn_obj = RT_NULL ;
54+ }
55+
56+ /* Test 3: NULL Name Handling - Using New Static Object */
57+ rt_object_init (& static_obj2 , RT_Object_Class_Thread , NULL );
58+ uassert_true (static_obj2 .name [0 ] == '\0' );
59+
60+ /* Test 4: Dynamic Object with NULL Name */
61+ dyn_obj = rt_object_allocate (RT_Object_Class_Thread , NULL );
62+ uassert_not_null (dyn_obj );
63+ if (dyn_obj )
64+ {
65+ uassert_true (dyn_obj -> name [0 ] == '\0' );
66+ rt_object_delete (dyn_obj );
67+ dyn_obj = RT_NULL ;
68+ }
69+
70+ /* Test 5: Fixed-Length Name - Using Third Static Object */
71+ char exact_name [RT_NAME_MAX ];
72+ rt_memset (exact_name , 'B' , RT_NAME_MAX - 1 );
73+ exact_name [RT_NAME_MAX - 1 ] = '\0' ;
74+
75+ rt_object_init (& static_obj3 , RT_Object_Class_Thread , exact_name );
76+ uassert_str_equal (static_obj3 .name , exact_name );
77+
78+ rt_object_detach (& static_obj1 );
79+ rt_object_detach (& static_obj2 );
80+ rt_object_detach (& static_obj3 );
81+ }
82+
83+ static rt_err_t testcase_init (void )
84+ {
85+ return RT_EOK ;
86+ }
87+
88+ static rt_err_t testcase_cleanup (void )
89+ {
90+ return RT_EOK ;
91+ }
92+
93+ static void test_object_suite (void )
94+ {
95+ UTEST_UNIT_RUN (test_object_name_handling );
96+ }
97+
98+ UTEST_TC_EXPORT (test_object_suite , "testcases.kernel.object_test" , testcase_init , testcase_cleanup , 10 );
0 commit comments