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