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