1 /*
2 * Copyright (c) 2021-2024 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 "mem/code_allocator.h"
17 #include "mem/pool_manager.h"
18 #include "mem/base_mem_stats.h"
19
20 #include "gtest/gtest.h"
21 #include "utils/logger.h"
22
23 namespace ark {
24
25 // NOLINTBEGIN(readability-magic-numbers)
26
27 class CodeAllocatorTest : public testing::Test {
28 public:
29 CodeAllocatorTest() = default;
30
~CodeAllocatorTest()31 ~CodeAllocatorTest() override
32 {
33 Logger::Destroy();
34 }
35 NO_COPY_SEMANTIC(CodeAllocatorTest);
36 NO_MOVE_SEMANTIC(CodeAllocatorTest);
37
38 protected:
39 static constexpr size_t K1_K = 1024;
40 static constexpr size_t TEST_HEAP_SIZE = K1_K * K1_K;
41
IsAligned(const void * ptr,size_t alignment)42 static bool IsAligned(const void *ptr, size_t alignment)
43 {
44 return reinterpret_cast<uintptr_t>(ptr) % alignment == 0;
45 }
46
SetUp()47 void SetUp() override
48 {
49 ark::mem::MemConfig::Initialize(0U, 32_MB, 0U, 32_MB, 0U, 0U);
50 PoolManager::Initialize();
51 }
52
TearDown()53 void TearDown() override
54 {
55 PoolManager::Finalize();
56 ark::mem::MemConfig::Finalize();
57 }
58 };
59
TEST_F(CodeAllocatorTest,AllocateBuffTest)60 TEST_F(CodeAllocatorTest, AllocateBuffTest)
61 {
62 BaseMemStats stats;
63 CodeAllocator ca(&stats);
64 // NOLINTNEXTLINE(modernize-avoid-c-arrays)
65 uint8_t buff[] = {0xCCU, 0xCCU};
66 void *codeBuff = ca.AllocateCode(sizeof(buff), static_cast<void *>(&buff[0U]));
67 for (size_t i = 0; i < sizeof(buff); i++) {
68 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
69 ASSERT_EQ(static_cast<uint8_t *>(codeBuff)[i], 0xCCU);
70 }
71 ASSERT_TRUE(IsAligned(codeBuff, 4U * SIZE_1K));
72 }
73
74 // NOLINTEND(readability-magic-numbers)
75
76 } // namespace ark
77