• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 #include <fstream>
16 #include <hwext/gtest-ext.h>
17 #include <hwext/gtest-tag.h>
18 #include <random>
19 #include <string>
20 
21 #include "paged_mem_pool.h"
22 
23 using FTRACE_NS::PagedMemPool;
24 using namespace testing::ext;
25 
26 namespace {
27 constexpr uint32_t TEST_PAGE_SIZE = 4096;
28 constexpr int RAND_INT_MIN = 1;
29 constexpr int RAND_INT_MAX = 1024;
30 class PagedMemPoolTest : public ::testing::Test {
31     std::mt19937 gen_;
32 
33 protected:
SetUp()34     void SetUp() override {}
35 
TearDown()36     void TearDown() override {}
37 
RandomNumber()38     size_t RandomNumber()
39     {
40         std::uniform_int_distribution<int> distrib(RAND_INT_MIN, RAND_INT_MAX);
41         return distrib(gen_);
42     }
43 };
44 
45 /*
46  * @tc.name: normal PagedMemPool size
47  * @tc.desc: test PagedMemPool:: normal with normal case.
48  * @tc.type: FUNC
49  */
50 HWTEST_F(PagedMemPoolTest, PagedMemPoolNormal, TestSize.Level1)
51 {
52     size_t blockSize = 8;
53     size_t maxCacheSize = 2;
54     std::unique_ptr<PagedMemPool> memPool = std::make_unique<PagedMemPool>(blockSize, maxCacheSize);
55 
56     ASSERT_NE(memPool, nullptr);
57 
58     void* block = memPool->Allocate();
59     EXPECT_NE(block, nullptr);
60 
61     EXPECT_EQ(memPool->GetBlockSize(), blockSize * TEST_PAGE_SIZE);
62 
63     EXPECT_TRUE(memPool->Recycle(block));
64 }
65 
66 /*
67  * @tc.name: false PagedMemPool size
68  * @tc.desc: test PagedMemPool:: normal with normal case.
69  * @tc.type: FUNC
70  */
71 HWTEST_F(PagedMemPoolTest, PagedMemPoolfreeListSize, TestSize.Level1)
72 {
73     size_t blockSize = 8;
74     size_t maxCacheSize = 2;
75     std::unique_ptr<PagedMemPool> memPool = std::make_unique<PagedMemPool>(blockSize, maxCacheSize);
76 
77     ASSERT_NE(memPool, nullptr);
78 
79     void* block = memPool->Allocate();
80     EXPECT_NE(block, nullptr);
81 
82     EXPECT_EQ(memPool->GetBlockSize(), blockSize * TEST_PAGE_SIZE);
83 
84     EXPECT_TRUE(memPool->Recycle(block));
85 
86     block = memPool->Allocate();
87     EXPECT_NE(block, nullptr);
88 
89     EXPECT_EQ(memPool->GetBlockSize(), blockSize * TEST_PAGE_SIZE);
90 
91     EXPECT_TRUE(memPool->Recycle(block));
92 }
93 
94 /*
95  * @tc.name: rand PagedMemPool size
96  * @tc.desc: test PagedMemPool:: normal with rand case.
97  * @tc.type: FUNC
98  */
99 HWTEST_F(PagedMemPoolTest, PagedMemPoolNormalRand, TestSize.Level1)
100 {
101     size_t blockSize = RandomNumber();
102     size_t maxCacheSize = RandomNumber();
103     std::unique_ptr<PagedMemPool> memPool = std::make_unique<PagedMemPool>(blockSize, maxCacheSize);
104 
105     ASSERT_NE(memPool, nullptr);
106 
107     void* block = memPool->Allocate();
108     EXPECT_NE(block, nullptr);
109 
110     EXPECT_EQ(memPool->GetBlockSize(), blockSize * TEST_PAGE_SIZE);
111 
112     EXPECT_TRUE(memPool->Recycle(block));
113 }
114 } // namespace