tc_smartptr.cpp 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. static rt_err_t utest_tc_init(void)
  35. {
  36. return RT_EOK;
  37. }
  38. static rt_err_t utest_tc_cleanup(void)
  39. {
  40. return RT_EOK;
  41. }
  42. static void testcase(void)
  43. {
  44. /* Test unique_ptr basic operations */
  45. UTEST_UNIT_RUN(test_unique_ptr);
  46. /* Test shared_ptr basic operations */
  47. UTEST_UNIT_RUN(test_shared_ptr);
  48. }
  49. UTEST_TC_EXPORT(testcase, "components.libc.cpp.smartptr_tc", utest_tc_init, utest_tc_cleanup, 10);