• 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 "common_components/heap/allocator/region_space.h"
17 #include "common_components/heap/space/from_space.h"
18 #include "common_components/mutator/thread_local.h"
19 #include "common_components/tests/test_helper.h"
20 
21 using namespace common;
22 namespace common::test {
23 class FromSpaceTest : public common::test::BaseTestWithScope {
24 protected:
25     class TestRegionList : public RegionList {
26     public:
TestRegionList()27         TestRegionList() : RegionList("TestList") {}
setHeadRegion(RegionDesc * head)28         void setHeadRegion(RegionDesc* head) { listHead_ = head; }
29     };
SetUpTestCase()30     static void SetUpTestCase()
31     {
32         BaseRuntime::GetInstance()->Init();
33     }
34 
TearDownTestCase()35     static void TearDownTestCase()
36     {
37         BaseRuntime::GetInstance()->Fini();
38     }
39 
SetUp()40     void SetUp() override {}
TearDown()41     void TearDown() override {}
42 };
43 
HWTEST_F_L0(FromSpaceTest,CopyFromRegions)44 HWTEST_F_L0(FromSpaceTest, CopyFromRegions)
45 {
46     RegionManager regionManager;
47     RegionSpace heap;
48     FromSpace fromSpace(regionManager, heap);
49     ThreadLocal::SetAllocBuffer(nullptr);
50     fromSpace.CopyFromRegions(nullptr);
51     ASSERT_FALSE(fromSpace.GetFromRegionList().GetHeadRegion() != nullptr);
52 }
53 
HWTEST_F_L0(FromSpaceTest,CopyFromRegionsTest)54 HWTEST_F_L0(FromSpaceTest, CopyFromRegionsTest)
55 {
56     size_t unitIdx = 0;
57     size_t nUnit = 4;
58     RegionDesc* region = RegionDesc::InitRegion(unitIdx, nUnit, RegionDesc::UnitRole::LARGE_SIZED_UNITS);
59     TestRegionList list;
60     list.setHeadRegion(region);
61     ASSERT_TRUE(list.GetHeadRegion() != nullptr);
62 
63     RegionSpace heap;
64     RegionManager manager;
65     FromSpace fromSpace(manager, heap);
66     fromSpace.AssembleGarbageCandidates(list);
67 
68     AllocationBuffer* buffer1 = new (std::nothrow) AllocationBuffer();
69     ThreadLocal::SetAllocBuffer(buffer1);
70     fromSpace.CopyFromRegions();
71     fromSpace.ExemptFromRegions();
72     ASSERT_TRUE(AllocationBuffer::GetAllocBuffer() != nullptr);
73 }
74 
HWTEST_F_L0(FromSpaceTest,ParallelCopyFromRegions)75 HWTEST_F_L0(FromSpaceTest, ParallelCopyFromRegions)
76 {
77     RegionManager regionManager;
78     RegionSpace heap;
79     FromSpace fromSpace(regionManager, heap);
80     AllocationBuffer* buffer1 = new (std::nothrow) AllocationBuffer();
81     ThreadLocal::SetAllocBuffer(buffer1);
82     fromSpace.ParallelCopyFromRegions(nullptr, 5);
83     ASSERT_TRUE(AllocationBuffer::GetAllocBuffer() != nullptr);
84 
85     ThreadLocal::SetAllocBuffer(nullptr);
86     fromSpace.ParallelCopyFromRegions(nullptr, 5);
87     ASSERT_FALSE(AllocationBuffer::GetAllocBuffer() != nullptr);
88 }
89 } // namespace common::test