tc_smartptr.cpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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-09-19 Rbb666 the first version
  9. */
  10. #include <rtthread.h>
  11. #include "utest.h"
  12. #include <memory>
  13. /**
  14. * @brief Test unique_ptr basic operations.
  15. */
  16. static void test_unique_ptr(void)
  17. {
  18. std::unique_ptr<int> p(new int(42));
  19. uassert_int_equal(*p, 42);
  20. *p = 24;
  21. uassert_int_equal(*p, 24);
  22. }
  23. /**
  24. * @brief Test shared_ptr basic operations.
  25. */
  26. static void test_shared_ptr(void)
  27. {
  28. std::shared_ptr<int> p1(new int(42));
  29. std::shared_ptr<int> p2 = p1;
  30. uassert_int_equal(*p1, 42);
  31. uassert_int_equal(*p2, 42);
  32. uassert_int_equal(p1.use_count(), 2);
  33. }
  34. /**
  35. * @brief Test case initialization function.
  36. * @return RT_EOK on success.
  37. */
  38. static rt_err_t utest_tc_init(void)
  39. {
  40. return RT_EOK;
  41. }
  42. /**
  43. * @brief Test case cleanup function.
  44. * @return RT_EOK on success.
  45. */
  46. static rt_err_t utest_tc_cleanup(void)
  47. {
  48. return RT_EOK;
  49. }
  50. /**
  51. * @brief Main test case function that runs the test.
  52. */
  53. static void testcase(void)
  54. {
  55. /* Test unique_ptr basic operations */
  56. UTEST_UNIT_RUN(test_unique_ptr);
  57. /* Test shared_ptr basic operations */
  58. UTEST_UNIT_RUN(test_shared_ptr);
  59. }
  60. /* Export the test case with initialization and cleanup functions and a timeout of 10 ticks. */
  61. UTEST_TC_EXPORT(testcase, "components.libc.cpp.smartptr_tc", utest_tc_init, utest_tc_cleanup, 10);