• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025 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 "avshared_memory_pool_unittest.h"
17 
18 using namespace testing;
19 using namespace testing::ext;
20 using namespace OHOS::Media;
21 
22 /**
23  * @tc.name  : Test CheckSize
24  * @tc.number: CheckSize_001
25  * @tc.desc  : Test ReleaseMemory notifier_ != nullptr
26  *             Test ReleaseMemory *iter != memory
27  *             Test CheckSize !option_.enableFixedSize && size == -1
28  */
29 HWTEST_F(AVSharedMemoryPoolUnitTest, CheckSize_001, TestSize.Level1)
30 {
31     // Test ReleaseMemory notifier_ != nullptr
__anon326314bd0102()32     auto notify = [](){};
33     AVSharedMemory *memory = new (std::nothrow) MockAVSharedMemory();
34     pool_->busyList_.push_back(memory);
35     pool_->notifier_ = notify;
36     pool_->ReleaseMemory(memory);
37 
38     // Test ReleaseMemory *iter != memory
39     MockAVSharedMemory *memory1 = new (std::nothrow) MockAVSharedMemory();
40     MockAVSharedMemory *memory2 = new (std::nothrow) MockAVSharedMemory();
41     pool_->busyList_.push_back(memory1);
42     pool_->busyList_.push_back(memory2);
43     pool_->ReleaseMemory(memory2);
44     pool_->ReleaseMemory(memory1);
45 
46     // Test CheckSize !option_.enableFixedSize && size == -1
47     initOption_.enableFixedSize = false;
48     pool_->option_ = initOption_;
49     int32_t size = TEST_SIZE;
50     bool result = pool_->CheckSize(size);
51     EXPECT_EQ(false, result);
52     pool_->Reset();
53 }
54 
55 /**
56  * @tc.name  : Test DoAcquireMemory
57  * @tc.number: DoAcquireMemory_001
58  * @tc.desc  : Test DoAcquireMemory (*iter)->GetSize() >= size
59  *             Test DoAcquireMemory result != nullptr
60  */
61 HWTEST_F(AVSharedMemoryPoolUnitTest, DoAcquireMemory_001, TestSize.Level1)
62 {
63     pool_->Init(initOption_);
64     AVSharedMemory *memory = nullptr;
65     bool result = pool_->DoAcquireMemory(MIN_MEM_SIZE, &memory);
66     EXPECT_TRUE(nullptr != memory);
67     delete memory;
68     EXPECT_EQ(true, result);
69 }
70 
71 /**
72  * @tc.name  : Test DoAcquireMemory
73  * @tc.number: DoAcquireMemory_002
74  * @tc.desc  : Test DoAcquireMemory (*iter)->GetSize() < minIdleSize
75  */
76 HWTEST_F(AVSharedMemoryPoolUnitTest, DoAcquireMemory_002, TestSize.Level1)
77 {
78     pool_->Init(initOption_);
79     AVSharedMemory *memory = nullptr;
80     bool result = pool_->DoAcquireMemory(MAX_MEM_SIZE, &memory);
81     delete memory;
82     EXPECT_EQ(true, result);
83 }
84 
85 /**
86  * @tc.name  : Test DoAcquireMemory
87  * @tc.number: DoAcquireMemory_003
88  * @tc.desc  : Test DoAcquireMemory (*iter)->GetSize() > minIdleSize
89  */
90 HWTEST_F(AVSharedMemoryPoolUnitTest, DoAcquireMemory_003, TestSize.Level1)
91 {
92     MockAVSharedMemory *mockMemory = new (std::nothrow) MockAVSharedMemory();
93     int32_t maxSize = std::numeric_limits<int32_t>::max();
94     EXPECT_CALL(*(mockMemory), GetSize()).WillRepeatedly(Return(maxSize));
95     pool_->idleList_.push_back(mockMemory);
96     AVSharedMemory *memory = nullptr;
97     bool result = pool_->DoAcquireMemory(MEM_SIZE, &memory);
98     delete memory;
99     EXPECT_EQ(true, result);
100 }
101 
102 /**
103  * @tc.name  : Test DoAcquireMemory
104  * @tc.number: DoAcquireMemory_004
105  * @tc.desc  : Test DoAcquireMemory !option_.enableFixedSize && minSizeIdleMem != idleList_.end()
106  */
107 HWTEST_F(AVSharedMemoryPoolUnitTest, DoAcquireMemory_004, TestSize.Level1)
108 {
109     initOption_.enableFixedSize = false;
110     pool_->Init(initOption_);
111     AVSharedMemory *memory = nullptr;
112     bool result = pool_->DoAcquireMemory(MAX_MEM_SIZE, &memory);
113     delete memory;
114     EXPECT_EQ(true, result);
115 }
116 
117 /**
118  * @tc.name  : Test AcquireMemory
119  * @tc.number: AcquireMemory_001
120  * @tc.desc  : Test AcquireMemory !option_.enableFixedSize
121  */
122 HWTEST_F(AVSharedMemoryPoolUnitTest, AcquireMemory_001, TestSize.Level1)
123 {
124     initOption_.enableFixedSize = false;
125     pool_->Init(initOption_);
126     auto memory = pool_->AcquireMemory(MEM_SIZE, true);
127     EXPECT_TRUE(memory != nullptr);
128 }
129 
130 /**
131  * @tc.name  : Test AcquireMemory
132  * @tc.number: AcquireMemory_002
133  * @tc.desc  : Test AcquireMemory !blocking || forceNonBlocking_
134  */
135 HWTEST_F(AVSharedMemoryPoolUnitTest, AcquireMemory_002, TestSize.Level1)
136 {
137     initOption_.enableFixedSize = false;
138     pool_->Init(initOption_);
139     auto memory1 = pool_->AcquireMemory(MEM_SIZE, false);
140     EXPECT_TRUE(memory1 != nullptr);
141     auto memory2 = pool_->AcquireMemory(MEM_SIZE, true);
142     EXPECT_TRUE(memory2 != nullptr);
143 
144     pool_->SetNonBlocking(true);
145     auto memory3 = pool_->AcquireMemory(MEM_SIZE, false);
146     EXPECT_TRUE(memory3 != nullptr);
147     auto memory4 = pool_->AcquireMemory(MEM_SIZE, true);
148     EXPECT_TRUE(memory4 != nullptr);
149 }