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