diff --git a/common_components/heap/collector/tests/trace_collector_test.cpp b/common_components/heap/collector/tests/trace_collector_test.cpp index 5b579b4ff2eeda1a74ce2debc4d0a29c4837d92d..cb40cf357bb99a49f5262609d6fcbeea401f75ff 100755 --- a/common_components/heap/collector/tests/trace_collector_test.cpp +++ b/common_components/heap/collector/tests/trace_collector_test.cpp @@ -54,6 +54,10 @@ protected: table.VisitRoots(visitor); return found; } + class TableTraceCollctor : public TraceCollector { + public: + using TraceCollector::UpdateNativeThreshold; + }; }; std::unique_ptr GetWCollector() @@ -63,4 +67,23 @@ std::unique_ptr GetWCollector() return std::make_unique(allocator, resources); } +HWTEST_F_L0(TraceCollectorTest, RunGarbageCollectionTest2) +{ + TraceCollector& collector = reinterpret_cast(Heap::GetHeap().GetCollector()); + Heap::GetHeap().SetGCReason(GCReason::GC_REASON_YOUNG); + collector.RunGarbageCollection(0, GCReason::GC_REASON_YOUNG, common::GC_TYPE_FULL); + ASSERT_TRUE(Heap::GetHeap().GetCollector().GetGCStats().isYoungGC()); +} + +HWTEST_F_L0(TraceCollectorTest, UpdateNativeThresholdTest) +{ + TableTraceCollctor& collector = reinterpret_cast(Heap::GetHeap().GetCollector()); + GCParam gcParam; + gcParam.minGrowBytes = 1024; + Heap::GetHeap().SetNativeHeapThreshold(512); + auto oldThreshold = Heap::GetHeap().GetNativeHeapThreshold(); + collector.UpdateNativeThreshold(gcParam); + auto newThreshold = Heap::GetHeap().GetNativeHeapThreshold(); + EXPECT_NE(newThreshold, oldThreshold); +} } \ No newline at end of file diff --git a/common_components/heap/space/tests/from_space_test.cpp b/common_components/heap/space/tests/from_space_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..177670045345011d78a191487f56ee66cb72d659 --- /dev/null +++ b/common_components/heap/space/tests/from_space_test.cpp @@ -0,0 +1,89 @@ +/* +* Copyright (c) 2025 Huawei Device Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#include "common_components/heap/allocator/region_space.h" +#include "common_components/heap/space/from_space.h" +#include "common_components/mutator/thread_local.h" +#include "common_components/tests/test_helper.h" + +using namespace common; +namespace common::test { +class FromSpaceTest : public common::test::BaseTestWithScope { +protected: + class TestRegionList : public RegionList { + public: + TestRegionList() : RegionList("TestList") {} + void setHeadRegion(RegionDesc* head) { listHead_ = head; } + }; + static void SetUpTestCase() + { + BaseRuntime::GetInstance()->Init(); + } + + static void TearDownTestCase() + { + BaseRuntime::GetInstance()->Fini(); + } + + void SetUp() override {} + void TearDown() override {} +}; + +HWTEST_F_L0(FromSpaceTest, CopyFromRegions) +{ + RegionManager regionManager; + RegionSpace heap; + FromSpace fromSpace(regionManager, heap); + ThreadLocal::SetAllocBuffer(nullptr); + fromSpace.CopyFromRegions(nullptr); + ASSERT_FALSE(fromSpace.GetFromRegionList().GetHeadRegion() != nullptr); +} + +HWTEST_F_L0(FromSpaceTest, CopyFromRegionsTest) +{ + size_t unitIdx = 0; + size_t nUnit = 4; + RegionDesc* region = RegionDesc::InitRegion(unitIdx, nUnit, RegionDesc::UnitRole::LARGE_SIZED_UNITS); + TestRegionList list; + list.setHeadRegion(region); + ASSERT_TRUE(list.GetHeadRegion() != nullptr); + + RegionSpace heap; + RegionManager manager; + FromSpace fromSpace(manager, heap); + fromSpace.AssembleGarbageCandidates(list); + + AllocationBuffer* buffer1 = new (std::nothrow) AllocationBuffer(); + ThreadLocal::SetAllocBuffer(buffer1); + fromSpace.CopyFromRegions(); + fromSpace.ExemptFromRegions(); + ASSERT_TRUE(AllocationBuffer::GetAllocBuffer() != nullptr); +} + +HWTEST_F_L0(FromSpaceTest, ParallelCopyFromRegions) +{ + RegionManager regionManager; + RegionSpace heap; + FromSpace fromSpace(regionManager, heap); + AllocationBuffer* buffer1 = new (std::nothrow) AllocationBuffer(); + ThreadLocal::SetAllocBuffer(buffer1); + fromSpace.ParallelCopyFromRegions(nullptr, 5); + ASSERT_TRUE(AllocationBuffer::GetAllocBuffer() != nullptr); + + ThreadLocal::SetAllocBuffer(nullptr); + fromSpace.ParallelCopyFromRegions(nullptr, 5); + ASSERT_FALSE(AllocationBuffer::GetAllocBuffer() != nullptr); +} +} // namespace common::test \ No newline at end of file