Skip to content

Commit c14f35f

Browse files
committed
[utest][cpp]add cpp-smartptr testcase.
1 parent 8005901 commit c14f35f

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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+
11+
#include <rtthread.h>
12+
#include "utest.h"
13+
#include <memory>
14+
15+
/**
16+
* @brief Test unique_ptr basic operations.
17+
*/
18+
static void test_unique_ptr(void)
19+
{
20+
std::unique_ptr<int> p(new int(42));
21+
if (*p != 42)
22+
{
23+
uassert_false(true);
24+
}
25+
*p = 24;
26+
if (*p != 24)
27+
{
28+
uassert_false(true);
29+
}
30+
uassert_true(true);
31+
}
32+
33+
/**
34+
* @brief Test shared_ptr basic operations.
35+
*/
36+
static void test_shared_ptr(void)
37+
{
38+
std::shared_ptr<int> p1(new int(42));
39+
std::shared_ptr<int> p2 = p1;
40+
if (*p1 != 42 || *p2 != 42)
41+
{
42+
uassert_false(true);
43+
}
44+
if (p1.use_count() != 2)
45+
{
46+
uassert_false(true);
47+
}
48+
uassert_true(true);
49+
}
50+
51+
static rt_err_t utest_tc_init(void)
52+
{
53+
return RT_EOK;
54+
}
55+
56+
static rt_err_t utest_tc_cleanup(void)
57+
{
58+
return RT_EOK;
59+
}
60+
61+
static void testcase(void)
62+
{
63+
/* Test unique_ptr basic operations */
64+
UTEST_UNIT_RUN(test_unique_ptr);
65+
/* Test shared_ptr basic operations */
66+
UTEST_UNIT_RUN(test_shared_ptr);
67+
}
68+
UTEST_TC_EXPORT(testcase, "testcases.cpp11.smartptr_tc", utest_tc_init, utest_tc_cleanup, 10);

0 commit comments

Comments
 (0)