MCPcopy Create free account
hub / github.com/RT-Thread/rt-thread / generate_unique_name

Function generate_unique_name

src/utest/object_tc.c:34–58  ·  view source on GitHub ↗

Generate unique name to avoid conflicts, respecting TEST_RT_NAME_MAX */

Source from the content-addressed store, hash-verified

32
33/* Generate unique name to avoid conflicts, respecting TEST_RT_NAME_MAX */
34static rt_err_t generate_unique_name(char *buf, rt_size_t size, const char *prefix)
35{
36 if (!buf || !prefix || size < TEST_RT_NAME_MAX)
37 return -RT_EINVAL;
38
39 for (int i = 0; i < 1000; i++) /* Limit attempts to prevent infinite loop */
40 {
41 rt_snprintf(buf, size, "%s%d", prefix, name_counter++);
42 rt_size_t len = rt_strlen(buf);
43 if (len >= TEST_RT_NAME_MAX)
44 {
45 buf[TEST_RT_NAME_MAX - 1] = '\0';
46 len = TEST_RT_NAME_MAX - 1;
47 }
48
49 /* Check if name (or truncated name) is unique */
50 if (rt_object_find(buf, RT_Object_Class_Unknown) == RT_NULL &&
51 rt_thread_find(buf) == RT_NULL &&
52 rt_device_find(buf) == RT_NULL)
53 {
54 return RT_EOK;
55 }
56 }
57 return -RT_ENOMEM;
58}
59
60static void test_object_name_handling(void)
61{

Callers 4

Calls 5

rt_snprintfFunction · 0.85
rt_strlenFunction · 0.85
rt_object_findFunction · 0.85
rt_thread_findFunction · 0.85
rt_device_findFunction · 0.85

Tested by 4