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