device_tc.c 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * Copyright (c) 2006-2024, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2024-05-20 Shell the first version
  9. * 2025-11-13 ChuanN Add standardized utest documentation block
  10. */
  11. /**
  12. * Test Case Name: Drivers Core Test
  13. *
  14. * Test Objectives:
  15. * - Validate the correctness and consistency of the rt_device_find() API in the RT-Thread device management subsystem.
  16. * - Test rt_device_find() and rt_console_get_device().
  17. *
  18. * Test Scenarios:
  19. * - Locate a known console device by its actual name retrieved at runtime.
  20. * - Attempt to find the same device using the predefined macro RT_CONSOLE_DEVICE_NAME.
  21. * - Verify identity consistency when querying the same device through different but equivalent name sources.
  22. *
  23. * Verification Metrics:
  24. * - rt_device_find() must return a non-NULL pointer when a valid device name is provided.
  25. * - Pointers returned for the same logical device (via different name expressions) must be identical.
  26. * - When RT_CONSOLE_DEVICE_NAME differs from the actual console name, two distinct but valid devices must be found.
  27. * - All assertions must pass without triggering test failure.
  28. *
  29. * Dependencies:
  30. * - Hardware requirements: QEMU emulator or any hardware platform that supports RT-Thread with console device registration.
  31. * - Software configuration:
  32. * - RT_USING_UTEST must be enabled (select "RT-Thread Utestcases" in menuconfig).
  33. * - BSP_UTEST_DRIVERS_CORE must be enabled (enable via: RT-Thread Utestcases -> Kernel components -> drivers -> Driver core Test).
  34. * - Environmental assumptions: The system has initialized the console device before test execution.
  35. *
  36. * Expected Results:
  37. * - The test will pass silently if all assertions hold.
  38. * - Console will output: "[ PASSED ] [ result ] testcase (components.drivers.core.device_find)".
  39. */
  40. #include <rtthread.h>
  41. #include <stdlib.h>
  42. #include "utest.h"
  43. static void test_rt_device_find(void)
  44. {
  45. char _device_name[RT_NAME_MAX + 1] = {0};
  46. rt_device_t console;
  47. rt_device_t device1, device2, device3;
  48. console = rt_console_get_device();
  49. uassert_not_null(console);
  50. rt_memcpy(_device_name, console->parent.name, RT_NAME_MAX);
  51. /* Test finding a device */
  52. device1 = rt_device_find(_device_name);
  53. uassert_true(device1 == console);
  54. /* Test finding another device */
  55. device2 = rt_device_find(RT_CONSOLE_DEVICE_NAME);
  56. if (rt_strcmp(RT_CONSOLE_DEVICE_NAME, _device_name) == 0)
  57. {
  58. uassert_true(device2 == device1);
  59. }
  60. else
  61. {
  62. uassert_not_null(device2);
  63. uassert_true(device2 != device1);
  64. }
  65. /* Test finding a device 3 */
  66. device3 = rt_device_find(console->parent.name);
  67. uassert_true(device1 == device3);
  68. }
  69. static rt_err_t utest_tc_init(void)
  70. {
  71. return RT_EOK;
  72. }
  73. static rt_err_t utest_tc_cleanup(void)
  74. {
  75. return RT_EOK;
  76. }
  77. static void testcase(void)
  78. {
  79. UTEST_UNIT_RUN(test_rt_device_find);
  80. }
  81. UTEST_TC_EXPORT(testcase, "components.drivers.core.device_find", utest_tc_init, utest_tc_cleanup, 5);