[utest] add device find (#8959)

* [utest] add device find

Signed-off-by: Shell <smokewood@qq.com>

* fixup: add test

* fixup: ptr aliasing

* quality: meaning parameter

---------

Signed-off-by: Shell <smokewood@qq.com>
This commit is contained in:
Shell 2024-05-21 08:56:35 +08:00 committed by GitHub
parent 2f559906d6
commit e82c53559b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 87 additions and 10 deletions

View File

@ -38,6 +38,7 @@ jobs:
- {UTEST: "kernel/irq", RTT_BSP: "bsp/qemu-vexpress-a9", QEMU_ARCH: "arm", QEMU_MACHINE: "vexpress-a9", CONFIG_FILE: "kernel/irq.conf", SD_FILE: "sd.bin", RUN: "yes"} - {UTEST: "kernel/irq", RTT_BSP: "bsp/qemu-vexpress-a9", QEMU_ARCH: "arm", QEMU_MACHINE: "vexpress-a9", CONFIG_FILE: "kernel/irq.conf", SD_FILE: "sd.bin", RUN: "yes"}
- {UTEST: "kernel/timer", RTT_BSP: "bsp/qemu-vexpress-a9", QEMU_ARCH: "arm", QEMU_MACHINE: "vexpress-a9", CONFIG_FILE: "kernel/timer.conf", SD_FILE: "sd.bin", RUN: "yes"} - {UTEST: "kernel/timer", RTT_BSP: "bsp/qemu-vexpress-a9", QEMU_ARCH: "arm", QEMU_MACHINE: "vexpress-a9", CONFIG_FILE: "kernel/timer.conf", SD_FILE: "sd.bin", RUN: "yes"}
- {UTEST: "kernel/thread", RTT_BSP: "bsp/qemu-vexpress-a9", QEMU_ARCH: "arm", QEMU_MACHINE: "vexpress-a9", CONFIG_FILE: "kernel/thread.conf", SD_FILE: "sd.bin", RUN: "yes"} - {UTEST: "kernel/thread", RTT_BSP: "bsp/qemu-vexpress-a9", QEMU_ARCH: "arm", QEMU_MACHINE: "vexpress-a9", CONFIG_FILE: "kernel/thread.conf", SD_FILE: "sd.bin", RUN: "yes"}
- {UTEST: "kernel/device", RTT_BSP: "bsp/qemu-vexpress-a9", QEMU_ARCH: "arm", QEMU_MACHINE: "vexpress-a9", CONFIG_FILE: "kernel/device.conf", SD_FILE: "sd.bin", RUN: "yes"}
- {UTEST: "kernel/atomic", RTT_BSP: "bsp/qemu-vexpress-a9", QEMU_ARCH: "arm", QEMU_MACHINE: "vexpress-a9", CONFIG_FILE: "kernel/atomic.conf", SD_FILE: "sd.bin", RUN: "yes"} - {UTEST: "kernel/atomic", RTT_BSP: "bsp/qemu-vexpress-a9", QEMU_ARCH: "arm", QEMU_MACHINE: "vexpress-a9", CONFIG_FILE: "kernel/atomic.conf", SD_FILE: "sd.bin", RUN: "yes"}
- {UTEST: "kernel/atomic_c11", RTT_BSP: "bsp/qemu-vexpress-a9", QEMU_ARCH: "arm", QEMU_MACHINE: "vexpress-a9", CONFIG_FILE: "kernel/atomic_c11.conf", SD_FILE: "sd.bin", RUN: "yes"} - {UTEST: "kernel/atomic_c11", RTT_BSP: "bsp/qemu-vexpress-a9", QEMU_ARCH: "arm", QEMU_MACHINE: "vexpress-a9", CONFIG_FILE: "kernel/atomic_c11.conf", SD_FILE: "sd.bin", RUN: "yes"}
- {UTEST: "components/utest", RTT_BSP: "bsp/qemu-vexpress-a9", QEMU_ARCH: "arm", QEMU_MACHINE: "vexpress-a9", CONFIG_FILE: "utest_self/self.conf", SD_FILE: "sd.bin", RUN: "yes"} - {UTEST: "components/utest", RTT_BSP: "bsp/qemu-vexpress-a9", QEMU_ARCH: "arm", QEMU_MACHINE: "vexpress-a9", CONFIG_FILE: "utest_self/self.conf", SD_FILE: "sd.bin", RUN: "yes"}

View File

@ -0,0 +1,4 @@
CONFIG_UTEST_DEVICE_TC=y
# dependencies
CONFIG_RT_USING_TIMER_SOFT=y
CONFIG_RT_USING_THREAD=y

View File

@ -56,6 +56,10 @@ config UTEST_THREAD_TC
select RT_USING_TIMER_SOFT select RT_USING_TIMER_SOFT
select RT_USING_THREAD select RT_USING_THREAD
config UTEST_DEVICE_TC
bool "device test"
default n
config UTEST_ATOMIC_TC config UTEST_ATOMIC_TC
bool "atomic test" bool "atomic test"
default n default n

View File

@ -41,6 +41,9 @@ if GetDepend(['UTEST_MAILBOX_TC']):
if GetDepend(['UTEST_THREAD_TC']): if GetDepend(['UTEST_THREAD_TC']):
src += ['thread_tc.c'] src += ['thread_tc.c']
if GetDepend(['UTEST_DEVICE_TC']):
src += ['device_tc.c']
if GetDepend(['UTEST_ATOMIC_TC']): if GetDepend(['UTEST_ATOMIC_TC']):
src += ['atomic_tc.c'] src += ['atomic_tc.c']

View File

@ -0,0 +1,59 @@
/*
* Copyright (c) 2006-2024, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2024-05-20 Shell the first version
*/
#include <rtthread.h>
#include <stdlib.h>
#include "utest.h"
static void test_rt_device_find(void)
{
char _device_name[RT_NAME_MAX + 1] = {0};
rt_device_t console;
rt_device_t device1, device2, device3;
console = rt_console_get_device();
uassert_not_null(console);
rt_memcpy(_device_name, console->parent.name, RT_NAME_MAX);
/* Test finding a device */
device1 = rt_device_find(_device_name);
uassert_true(device1 == console);
/* Test finding another device */
device2 = rt_device_find(RT_CONSOLE_DEVICE_NAME);
if (rt_strcmp(RT_CONSOLE_DEVICE_NAME, _device_name) == 0)
{
uassert_true(device2 == device1);
}
else
{
uassert_not_null(device2);
uassert_true(device2 != device1);
}
/* Test finding a device 3 */
device3 = rt_device_find(console->parent.name);
uassert_true(device1 == device3);
}
static rt_err_t utest_tc_init(void)
{
return RT_EOK;
}
static rt_err_t utest_tc_cleanup(void)
{
return RT_EOK;
}
static void testcase(void)
{
UTEST_UNIT_RUN(test_rt_device_find);
}
UTEST_TC_EXPORT(testcase, "testcases.kernel.device.find", utest_tc_init, utest_tc_cleanup, 5);

View File

@ -625,12 +625,19 @@ rt_err_t rt_object_for_each(rt_uint8_t type, rt_object_iter_t iter, void *data)
return RT_EOK; return RT_EOK;
} }
struct _obj_find_param
{
const char *match_name;
rt_object_t matched_obj;
};
static rt_err_t _match_name(struct rt_object *obj, void *data) static rt_err_t _match_name(struct rt_object *obj, void *data)
{ {
const char *name = *(const char **)data; struct _obj_find_param *param = data;
const char *name = param->match_name;
if (rt_strncmp(obj->name, name, RT_NAME_MAX) == 0) if (rt_strncmp(obj->name, name, RT_NAME_MAX) == 0)
{ {
*(rt_object_t *)data = obj; param->matched_obj = obj;
/* notify an early break of loop, but not on error */ /* notify an early break of loop, but not on error */
return 1; return 1;
@ -654,7 +661,11 @@ static rt_err_t _match_name(struct rt_object *obj, void *data)
*/ */
rt_object_t rt_object_find(const char *name, rt_uint8_t type) rt_object_t rt_object_find(const char *name, rt_uint8_t type)
{ {
void *data = (void *)name; struct _obj_find_param param =
{
.match_name = name,
.matched_obj = RT_NULL,
};
/* parameter check */ /* parameter check */
if (name == RT_NULL) return RT_NULL; if (name == RT_NULL) return RT_NULL;
@ -662,13 +673,8 @@ rt_object_t rt_object_find(const char *name, rt_uint8_t type)
/* which is invoke in interrupt status */ /* which is invoke in interrupt status */
RT_DEBUG_NOT_IN_INTERRUPT; RT_DEBUG_NOT_IN_INTERRUPT;
rt_object_for_each(type, _match_name, &data); rt_object_for_each(type, _match_name, &param);
if (data != name) return param.matched_obj;
{
return data;
}
return RT_NULL;
} }
/** /**