1 /*
2 * Copyright (C) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <gtest/gtest.h>
17
18 #include <meta/base/shared_ptr.h>
19
20 #include "src/test_runner.h"
21
22 using namespace testing::ext;
23
24 META_BEGIN_NAMESPACE()
25
26 class SharedPtrTest : public testing::Test {
27 public:
SetUpTestSuite()28 static void SetUpTestSuite()
29 {
30 SetTest();
31 }
TearDownTestSuite()32 static void TearDownTestSuite()
33 {
34 ResetTest();
35 }
36 void SetUp() override {}
37 void TearDown() override {}
38 };
39
40 /**
41 * @tc.name: SharedPtrTest
42 * @tc.desc: test SharedPtrCanBeConvertedToSharedPtrWithConstType function
43 * @tc.type: FUNC
44 * @tc.require: I7DMS1
45 */
HWTEST_F(SharedPtrTest, SharedPtrCanBeConvertedToSharedPtrWithConstType, TestSize.Level1)46 HWTEST_F(SharedPtrTest, SharedPtrCanBeConvertedToSharedPtrWithConstType, TestSize.Level1)
47 {
48 constexpr auto initialValue = 120;
49
50 BASE_NS::shared_ptr<const int> cptr = ptr;
51
52 EXPECT_EQ(*cptr, initialValue);
53 }
54
55 /**
56 * @tc.name: SharedPtrTest
57 * @tc.desc: test SharedPtrCanBeReturnedToSharedPtrWithConstType function
58 * @tc.type: FUNC
59 * @tc.require: I7DMS1
60 */
HWTEST_F(SharedPtrTest, SharedPtrCanBeReturnedAsSharedPtrWithConstType, TestSize.Level1)61 HWTEST_F(SharedPtrTest, SharedPtrCanBeReturnedAsSharedPtrWithConstType, TestSize.Level1)
62 {
63 constexpr auto initialValue = 120;
64
65 auto functionReturningSharedConstPtr = [ptr]() -> BASE_NS::shared_ptr<const int> { return ptr; };
66
67 auto cptr = functionReturningSharedConstPtr();
68
69 EXPECT_EQ(*cptr, initialValue);
70 }
71
72 /**
73 * @tc.name: SharedPtrTest
74 * @tc.desc: test DefaultConstructedSharedPtrIsEmpty function
75 * @tc.type: FUNC
76 * @tc.require: I7DMS1
77 */
HWTEST_F(SharedPtrTest, DefaultConstructedSharedPtrIsEmpty, TestSize.Level1)78 HWTEST_F(SharedPtrTest, DefaultConstructedSharedPtrIsEmpty, TestSize.Level1)
79 {
80 BASE_NS::shared_ptr<int> ptr;
81
82 const auto rawPtr = ptr.get();
83 const bool boolOperatorResult = ptr.operator bool();
84
85 EXPECT_EQ(rawPtr, nullptr);
86 EXPECT_EQ(boolOperatorResult, false);
87 }
88
89 /**
90 * @tc.name: SharedPtrTest
91 * @tc.desc: test CopiedSharedPtrExtendObjectLifetime function
92 * @tc.type: FUNC
93 * @tc.require: I7DMS1
94 */
HWTEST_F(SharedPtrTest, CopiedSharedPtrExtendObjectLifetime, TestSize.Level1)95 HWTEST_F(SharedPtrTest, CopiedSharedPtrExtendObjectLifetime, TestSize.Level1)
96 {
97 constexpr auto initialValue = 120;
98 BASE_NS::shared_ptr<const int> cptr;
99
100 {
101 cptr = ptr;
102 }
103
104 EXPECT_EQ(*cptr, initialValue);
105 }
106
107 /**
108 * @tc.name: SharedPtrTest
109 * @tc.desc: test CopyConstructedSharedPtrExtendObjectLifetime function
110 * @tc.type: FUNC
111 * @tc.require: I7DMS1
112 */
HWTEST_F(SharedPtrTest, CopyConstructedSharedPtrExtendObjectLifetime, TestSize.Level1)113 HWTEST_F(SharedPtrTest, CopyConstructedSharedPtrExtendObjectLifetime, TestSize.Level1)
114 {
115 constexpr auto initialValue = 120;
116 BASE_NS::shared_ptr<int> ptr = CreateShared<int>(initialValue);
117
118 auto copy = BASE_NS::shared_ptr<int>(ptr);
119 ptr.reset();
120
121 EXPECT_EQ(*copy, initialValue);
122 }
123
124 struct CountType {
125 int count { 1 };
126 };
127
CountDeleter(void* p)128 void CountDeleter(void* p)
129 {
130 --static_cast<CountType*>(p)->count;
131 }
132
133 /**
134 * @tc.name: SharedPtrTest
135 * @tc.desc: test MoveMemoryLeakAndAssert function
136 * @tc.type: FUNC
137 * @tc.require: I7DMS1
138 */
HWTEST_F(SharedPtrTest, MoveMemoryLeakAndAssert, TestSize.Level1)139 HWTEST_F(SharedPtrTest, MoveMemoryLeakAndAssert, TestSize.Level1)
140 {
141 CountType test;
142 {
143 BASE_NS::shared_ptr<CountType> p(&test, CountDeleter);
144 auto copy = p;
145 p = BASE_NS::move(copy);
146 }
147
148 EXPECT_EQ(test.count, 0);
149 }
150
151 /**
152 * @tc.name: SharedPtrTest
153 * @tc.desc: test Expired function
154 * @tc.type: FUNC
155 * @tc.require: I7DMS1
156 */
HWTEST_F(SharedPtrTest, Expired, TestSize.Level1)157 HWTEST_F(SharedPtrTest, Expired, TestSize.Level1)
158 {
159 BASE_NS::weak_ptr<int> w = ptr;
160 EXPECT_FALSE(w.expired());
161 ptr.reset();
162 EXPECT_TRUE(w.expired());
163 }
164
165 /**
166 * @tc.name: SharedPtrTest
167 * @tc.desc: test Comparison function
168 * @tc.type: FUNC
169 * @tc.require: I7DMS1
170 */
HWTEST_F(SharedPtrTest, Comparison, TestSize.Level1)171 HWTEST_F(SharedPtrTest, Comparison, TestSize.Level1)
172 {
173 auto p2 = p1;
174 auto p3 = BASE_NS::shared_ptr<int>(p1.get(), [](void*) {});
175
176 EXPECT_EQ(p1, p2);
177 EXPECT_EQ(p1, p3);
178 EXPECT_EQ(p2, p3);
179 }
180
181 /**
182 * @tc.name: SharedPtrTest
183 * @tc.desc: test ExplicitDeleterBasicType function
184 * @tc.type: FUNC
185 * @tc.require: I7DMS1
186 */
HWTEST_F(SharedPtrTest, ExplicitDeleterBasicType, TestSize.Level1)187 HWTEST_F(SharedPtrTest, ExplicitDeleterBasicType, TestSize.Level1)
188 {
189 int count = 0;
190 {
191 auto p = BASE_NS::shared_ptr<int>(&count, [&](int *) { ++count; });
192 }
193 EXPECT_EQ(count, 1);
194 }
195
196 namespace {
197 struct ITestInterface : CORE_NS::IInterface {
198 const IInterface* GetInterface(const BASE_NS::Uid& uid) const override
199 {
200 return this;
201 }
202
203 IInterface* GetInterface(const BASE_NS::Uid& uid) override
204 {
205 return this;
206 }
207
208 void Ref() override
209 {
210 ++count;
211 }
212
213 void Unref() override
214 {
215 --count;
216 }
217 int count { 0 };
218 };
219 } // namespace
220
221 /**
222 * @tc.name: SharedPtrTest
223 * @tc.desc: test ExplicitDeleterInterface function
224 * @tc.type: FUNC
225 * @tc.require: I7DMS1
226 */
HWTEST_F(SharedPtrTest, ExplicitDeleterInterface, TestSize.Level1)227 HWTEST_F(SharedPtrTest, ExplicitDeleterInterface, TestSize.Level1)
228 {
229 {
230 ITestInterface test;
231 {
232 auto p = BASE_NS::shared_ptr<ITestInterface>(&test);
233 EXPECT_EQ(test.count, 1);
234 }
235 EXPECT_EQ(test.count, 0);
236 }
237 {
238 ITestInterface test;
239 {
240 auto p = BASE_NS::shared_ptr<CORE_NS::IInterface>(&test);
241 EXPECT_EQ(test.count, 1);
242 }
243 EXPECT_EQ(test.count, 0);
244 }
245 {
246 int count = 0;
247 ITestInterface test;
248 {
249 auto p = BASE_NS::shared_ptr<ITestInterface>(&test, [&count](ITestInterface*) { ++count; });
250 EXPECT_EQ(test.count, 0);
251 }
252 EXPECT_EQ(count, 1);
253 EXPECT_EQ(test.count, 0);
254 }
255 }
256
257 /**
258 * @tc.name: SharedPtrTest
259 * @tc.desc: test UniquePtrConversion function
260 * @tc.type: FUNC
261 * @tc.require: I7DMS1
262 */
HWTEST_F(SharedPtrTest, UniquePtrConversion, TestSize.Level1)263 HWTEST_F(SharedPtrTest, UniquePtrConversion, TestSize.Level1)
264 {
265 {
266 BASE_NS::shared_ptr<int> shared(BASE_NS::move(unique));
267 ASSERT_TRUE(!unique);
268 ASSERT_TRUE(shared);
269 EXPECT_EQ(*shared, 2);
270 }
271 {
272 ITestInterface object;
273 object.Ref();
274 EXPECT_EQ(object.count, 1);
275 BASE_NS::unique_ptr<ITestInterface, decltype(deleter)> unique(&object, deleter);
276 BASE_NS::shared_ptr<CORE_NS::IInterface> shared(BASE_NS::move(unique));
277 ASSERT_TRUE(!unique);
278 ASSERT_TRUE(shared);
279 EXPECT_EQ(object.count, 1);
280 shared.reset();
281 EXPECT_EQ(object.count, 0);
282 }
283 {
284 int count = 0;
285 {
286 int value = 2;
287 BASE_NS::unique_ptr<int, decltype(deleter)> unique(&value, deleter);
288 BASE_NS::shared_ptr<int> shared(BASE_NS::move(unique));
289 ASSERT_TRUE(!unique);
290 ASSERT_TRUE(shared);
291 EXPECT_EQ(*shared, 2);
292 EXPECT_EQ(count, 0);
293 }
294 EXPECT_EQ(count, 1);
295 }
296 {
297 BASE_NS::shared_ptr<int> shared;
298 shared = BASE_NS::move(unique);
299 ASSERT_TRUE(!unique);
300 ASSERT_TRUE(shared);
301 EXPECT_EQ(*shared, 2);
302 }
303 {
304 ITestInterface object;
305 object.Ref();
306 EXPECT_EQ(object.count, 1);
307 BASE_NS::unique_ptr<ITestInterface, decltype(deleter)> unique(&object, deleter);
308 BASE_NS::shared_ptr<CORE_NS::IInterface> shared;
309 shared = BASE_NS::move(unique);
310 ASSERT_TRUE(!unique);
311 ASSERT_TRUE(shared);
312 EXPECT_EQ(object.count, 1);
313 shared.reset();
314 EXPECT_EQ(object.count, 0);
315 }
316 }
317
318 META_END_NAMESPACE()