1 /*
2 * Copyright (c) 2023 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 <functional>
17 #include <mutex>
18
19 #include "gtest/gtest.h"
20 #include "pool.h"
21 #include "log_print.h"
22
23 using namespace testing::ext;
24 using namespace OHOS;
25 namespace OHOS::Test {
26 class PoolTest : public testing::Test {
27 public:
28 struct Node {
29 int value;
operator ==OHOS::Test::PoolTest::Node30 bool operator==(Node &other)
31 {
32 return value == other.value;
33 }
34 };
35 static void SetUpTestCase(void);
36 static void TearDownTestCase(void);
37 void SetUp();
38 void TearDown();
39
40 protected:
41 static constexpr uint32_t CAPABILITY_TEST = 3; // capability
42 static constexpr uint32_t MIN_TEST = 1; // min
43 static Pool<PoolTest::Node> pool_;
44 };
45 Pool<PoolTest::Node> PoolTest::pool_ = Pool<PoolTest::Node>(CAPABILITY_TEST, MIN_TEST);
46
SetUpTestCase(void)47 void PoolTest::SetUpTestCase(void)
48 {}
49
TearDownTestCase(void)50 void PoolTest::TearDownTestCase(void)
51 {}
52
SetUp(void)53 void PoolTest::SetUp(void)
54 {}
55
TearDown(void)56 void PoolTest::TearDown(void)
57 {
58 auto close = [](std::shared_ptr<PoolTest::Node> data) {
59 pool_.Idle(data);
60 pool_.Release(data);
61 };
62 pool_.Clean(close);
63 }
64
65 /**
66 * @tc.name: Get_001
67 * @tc.desc: test the std::shared_ptr<T> Get(bool isForce = false) function.
68 * @tc.type: FUNC
69 * @tc.require:
70 * @tc.author: suoqilong
71 */
72 HWTEST_F(PoolTest, Get_001, TestSize.Level1)
73 {
74 int index = 0;
75 auto ret = pool_.Get();
76 EXPECT_NE(ret, nullptr);
77 ret->value = index++;
78
79 ret = pool_.Get();
80 EXPECT_NE(ret, nullptr);
81 ret->value = index++;
82
83 ret = pool_.Get();
84 EXPECT_NE(ret, nullptr);
85 ret->value = index++;
86
87 ret = pool_.Get();
88 EXPECT_EQ(ret, nullptr);
89 }
90
91 /**
92 * @tc.name: Get_002
93 * @tc.desc: test the std::shared_ptr<T> Get(bool isForce = false) function.
94 * @tc.type: FUNC
95 * @tc.require:
96 * @tc.author: suoqilong
97 */
98 HWTEST_F(PoolTest, Get_002, TestSize.Level1)
99 {
100 int index = 0;
101 auto ret = pool_.Get();
102 EXPECT_NE(ret, nullptr);
103 ret->value = index++;
104
105 ret = pool_.Get();
106 EXPECT_NE(ret, nullptr);
107 ret->value = index++;
108
109 ret = pool_.Get();
110 EXPECT_NE(ret, nullptr);
111 ret->value = index++;
112
113 ret = pool_.Get(true);
114 EXPECT_NE(ret, nullptr);
115 ret->value = index++;
116
117 ret = pool_.Get();
118 EXPECT_EQ(ret, nullptr);
119 }
120
121 /**
122 * @tc.name: Release_001
123 * @tc.desc: test the int32_t Release(std::shared_ptr<T> data, bool force = false) function.
124 * @tc.type: FUNC
125 * @tc.require:
126 * @tc.author: suoqilong
127 */
128 HWTEST_F(PoolTest, Release_001, TestSize.Level1)
129 {
130 int index = 0;
131 auto ret = pool_.Get();
132 EXPECT_NE(ret, nullptr);
133 ret->value = index++;
134
135 ret = pool_.Get();
136 EXPECT_NE(ret, nullptr);
137
138 pool_.Idle(ret);
139 auto retRelease = pool_.Release(ret);
140 EXPECT_EQ(retRelease, true);
141 }
142
143 /**
144 * @tc.name: Release_002
145 * @tc.desc: test the int32_t Release(std::shared_ptr<T> data, bool force = false) function.
146 * @tc.type: FUNC
147 * @tc.require:
148 * @tc.author: suoqilong
149 */
150 HWTEST_F(PoolTest, Release_002, TestSize.Level1)
151 {
152 auto ret = pool_.Get();
153 EXPECT_NE(ret, nullptr);
154
155 pool_.Idle(ret);
156 auto retRelease = pool_.Release(ret);
157 EXPECT_EQ(retRelease, false);
158 }
159
160 /**
161 * @tc.name: Release_003
162 * @tc.desc: test the int32_t Release(std::shared_ptr<T> data, bool force = false) function.
163 * @tc.type: FUNC
164 * @tc.require:
165 * @tc.author: suoqilong
166 */
167 HWTEST_F(PoolTest, Release_003, TestSize.Level1)
168 {
169 int index = 0;
170 auto ret = pool_.Get();
171 EXPECT_NE(ret, nullptr);
172 ret->value = index++;
173
174 ret = pool_.Get();
175 EXPECT_NE(ret, nullptr);
176
177 pool_.Idle(ret);
178 auto retRelease = pool_.Release(ret);
179 EXPECT_EQ(retRelease, true);
180 }
181
182 /**
183 * @tc.name: Release_004
184 * @tc.desc: test the int32_t Release(std::shared_ptr<T> data, bool force = false) function.
185 * @tc.type: FUNC
186 * @tc.require:
187 * @tc.author: suoqilong
188 */
189 HWTEST_F(PoolTest, Release_004, TestSize.Level1)
190 {
191 int index = 0;
192 auto ret = pool_.Get();
193 EXPECT_NE(ret, nullptr);
194 ret->value = index++;
195
196 ret = pool_.Get();
197 EXPECT_NE(ret, nullptr);
198 ret->value = index++;
199
200 ret = pool_.Get();
201 EXPECT_NE(ret, nullptr);
202 ret->value = index++;
203
204 ret = pool_.Get(true);
205 EXPECT_NE(ret, nullptr);
206 ret->value = index++;
207
208 ret = pool_.Get();
209 EXPECT_EQ(ret, nullptr);
210
211 pool_.Idle(ret);
212 auto retRelease = pool_.Release(ret);
213 EXPECT_EQ(retRelease, false);
214 }
215
216 /**
217 * @tc.name: Release_005
218 * @tc.desc: test the int32_t Release(std::shared_ptr<T> data, bool force = false) function.
219 * @tc.type: FUNC
220 * @tc.require:
221 * @tc.author: suoqilong
222 */
223 HWTEST_F(PoolTest, Release_005, TestSize.Level1)
224 {
225 int index = 0;
226 auto ret = pool_.Get();
227 EXPECT_NE(ret, nullptr);
228 ret->value = index++;
229
230 ret = pool_.Get();
231 EXPECT_NE(ret, nullptr);
232
233 auto data = std::make_shared<PoolTest::Node>();
234 pool_.Idle(ret);
235 auto retRelease = pool_.Release(data);
236 EXPECT_EQ(retRelease, false);
237 }
238
239 /**
240 * @tc.name: Release_006
241 * @tc.desc: test the int32_t Release(std::shared_ptr<T> data, bool force = false) function.
242 * @tc.type: FUNC
243 * @tc.require:
244 * @tc.author: suoqilong
245 */
246 HWTEST_F(PoolTest, Release_006, TestSize.Level1)
247 {
248 auto ret = pool_.Get();
249 EXPECT_NE(ret, nullptr);
250
251 pool_.Idle(ret);
252 auto retRelease = pool_.Release(ret, true);
253 EXPECT_EQ(retRelease, true);
254 }
255
256 /**
257 * @tc.name: Release_007
258 * @tc.desc: test the int32_t Release(std::shared_ptr<T> data, bool force = false) function.
259 * @tc.type: FUNC
260 * @tc.require:
261 * @tc.author: suoqilong
262 */
263 HWTEST_F(PoolTest, Release_007, TestSize.Level1)
264 {
265 auto ret = nullptr;
266 auto retRelease = pool_.Release(ret, true);
267 EXPECT_EQ(retRelease, false);
268 }
269
270 /**
271 * @tc.name: Idle_001
272 * @tc.desc: test the void Idle(std::shared_ptr<T> data) function.
273 * @tc.type: FUNC
274 * @tc.require:
275 * @tc.author: suoqilong
276 */
277 HWTEST_F(PoolTest, Idle_001, TestSize.Level1)
278 {
279 int index = 0;
280 auto ret = pool_.Get();
281 EXPECT_NE(ret, nullptr);
282 ret->value = index++;
283
284 ret = pool_.Get();
285 EXPECT_NE(ret, nullptr);
286 ret->value = index++;
287
288 ret = pool_.Get();
289 EXPECT_NE(ret, nullptr);
290
291 pool_.Idle(ret);
292 auto retRelease = pool_.Release(ret);
293 EXPECT_EQ(retRelease, true);
294 }
295
296 /**
297 * @tc.name: Clean_001
298 * @tc.desc: test the int32_t Clean(std::function<void(std::shared_ptr<T>)> close) noexcept function.
299 * @tc.type: FUNC
300 * @tc.require:
301 * @tc.author: suoqilong
302 */
303 HWTEST_F(PoolTest, Clean_001, TestSize.Level1)
304 {
305 int index = 0;
306 auto ret = pool_.Get();
307 EXPECT_NE(ret, nullptr);
308 ret->value = index++;
309
310 ret = pool_.Get();
311 EXPECT_NE(ret, nullptr);
312 ret->value = index++;
313
314 ret = pool_.Get();
315 EXPECT_NE(ret, nullptr);
316
__anondf14fbff0202(std::shared_ptr<PoolTest::Node> data) 317 auto close = [](std::shared_ptr<PoolTest::Node> data) {
318 pool_.Idle(data);
319 pool_.Release(data);
320 };
321 auto retClean = pool_.Clean(close);
322 EXPECT_EQ(retClean, true);
323 }
324 } // namespace OHOS::Test
325