1 /*
2 * Copyright (c) 2021-2021 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 #include "common/any.h"
18 #define private public
19 #define protected public
20 #define UNIT_TEST 1
21
22 #include <vector>
23
24 #include "utils/buffer_pool.h"
25 #include "utils/constants.h"
26 #include "pipeline/core/type_define.h"
27
28 namespace OHOS::Media::Test {
29 class BufferPoolTest : public ::testing::Test {
30 public:
SetUp()31 void SetUp() override
32 {
33 pool = std::make_shared<BufferPool<AVBuffer>>(DEFAULT_POOL_SIZE);
34 pool->Init();
35 }
36
TearDown()37 void TearDown() override
38 {
39 }
40
41 std::shared_ptr<BufferPool<AVBuffer>> pool;
42 };
43
TEST_F(BufferPoolTest,buffer_pool_init_buffer)44 TEST_F(BufferPoolTest, buffer_pool_init_buffer)
45 {
46 EXPECT_EQ(DEFAULT_POOL_SIZE, pool->Size());
47 }
48
TEST_F(BufferPoolTest,buffer_pool_recycle_succ)49 TEST_F(BufferPoolTest, buffer_pool_recycle_succ)
50 {
51 EXPECT_EQ(DEFAULT_POOL_SIZE, pool->Size());
52 auto buffPtr = pool->AllocateBuffer();
53 EXPECT_EQ(DEFAULT_POOL_SIZE - 1, pool->Size());
54 buffPtr.reset();
55 EXPECT_EQ(DEFAULT_POOL_SIZE, pool->Size());
56 }
57
TEST_F(BufferPoolTest,buffer_pool_return_nullptr_after_buffer_exhausted)58 TEST_F(BufferPoolTest, buffer_pool_return_nullptr_after_buffer_exhausted)
59 {
60 EXPECT_EQ(DEFAULT_POOL_SIZE, pool->Size());
61 std::vector<std::shared_ptr<AVBuffer>> buffers;
62 buffers.reserve(DEFAULT_POOL_SIZE);
63 for (size_t i = 0; i < DEFAULT_POOL_SIZE; ++i) {
64 buffers.emplace_back(pool->AllocateBuffer());
65 }
66 EXPECT_EQ(true, pool->Empty());
67 EXPECT_EQ(nullptr, pool->AllocateBufferNonBlocking());
68 }
69 } // namespace