From 1639a6f0b47e18d511a791ec2876b1e78f7e4698 Mon Sep 17 00:00:00 2001 From: yp9522 Date: Thu, 3 Jul 2025 15:12:49 +0800 Subject: [PATCH] add common_components tdd Issue:https://gitee.com/openharmony/arkcompiler_ets_runtime/issues/ICJQ7H Change-Id: I018804c84897f154fa3ec45e2a82e25c985dd759 Signed-off-by: yp9522 --- .../allocator/tests/region_space_test.cpp | 132 ++++++++++++++++++ 1 file changed, 132 insertions(+) diff --git a/common_components/heap/allocator/tests/region_space_test.cpp b/common_components/heap/allocator/tests/region_space_test.cpp index 9da2659698..c2743a636e 100755 --- a/common_components/heap/allocator/tests/region_space_test.cpp +++ b/common_components/heap/allocator/tests/region_space_test.cpp @@ -69,6 +69,8 @@ protected: regionMemory_ = nullptr; } } + + BaseRuntime* runtime_; }; HWTEST_F_L0(RegionSpaceTest, ShouldRetryAllocation) @@ -378,4 +380,134 @@ HWTEST_F_L0(RegionSpaceTest, CopyRegion1) theAllocator.CopyRegion(region); EXPECT_NE(theAllocator.FromSpaceSize(), 0); } + +HWTEST_F_L0(RegionSpaceTest, AllocateThreadLocalRegion1_NotGcThread_EntersElseBranch) +{ + auto& heapAllocator = Heap::GetHeap().GetAllocator(); + RegionSpace& regionSpace = reinterpret_cast(heapAllocator); + + Mutator::GetMutator()->SetMutatorPhase(GCPhase::GC_PHASE_ENUM); + + auto* region = regionSpace.AllocateThreadLocalRegion(false, AllocType::MOVEABLE_OBJECT); + EXPECT_NE(region, nullptr); +} + + +HWTEST_F_L0(RegionSpaceTest, AllocateThreadLocalRegion2_NotGcThread_EntersElseBranch) +{ + auto& heapAllocator = Heap::GetHeap().GetAllocator(); + RegionSpace& regionSpace = reinterpret_cast(heapAllocator); + + Mutator::GetMutator()->SetMutatorPhase(GCPhase::GC_PHASE_PRECOPY); + + auto* region = regionSpace.AllocateThreadLocalRegion(false, AllocType::MOVEABLE_OBJECT); + EXPECT_NE(region, nullptr); +} + +HWTEST_F_L0(RegionSpaceTest, AllocateThreadLocalRegion3_NotGcThread_EntersElseBranch) +{ + auto& heapAllocator = Heap::GetHeap().GetAllocator(); + RegionSpace& regionSpace = reinterpret_cast(heapAllocator); + + Mutator::GetMutator()->SetMutatorPhase(GCPhase::GC_PHASE_FIX); + + auto* region = regionSpace.AllocateThreadLocalRegion(false, AllocType::MOVEABLE_OBJECT); + EXPECT_NE(region, nullptr); +} + +HWTEST_F_L0(RegionSpaceTest, Allocate_ValidSize_ReturnsNonNull) +{ + RegionSpace& theAllocator = reinterpret_cast(Heap::GetHeap().GetAllocator()); + + RegionManager& manager = theAllocator.GetRegionManager(); + manager.Initialize(totalUnits_, reinterpret_cast(regionMemory_)); + + RegionDesc* region = RegionDesc::InitRegion(0, totalUnits_, RegionDesc::UnitRole::SMALL_SIZED_UNITS); + + region->SetRegionType(RegionDesc::RegionType::THREAD_LOCAL_REGION); + + RegionDesc* takenRegion = manager.TakeRegion(16, RegionDesc::UnitRole::SMALL_SIZED_UNITS, true, false); + ASSERT_NE(takenRegion, nullptr) << "Should be able to take a valid region"; + + takenRegion->SetRegionType(RegionDesc::RegionType::THREAD_LOCAL_REGION); + + EXPECT_EQ(takenRegion->GetRegionType(), RegionDesc::RegionType::THREAD_LOCAL_REGION); + + Mutator::GetMutator()->SetMutatorPhase(GCPhase::GC_PHASE_UNDEF); + + Heap::GetHeap().EnableGC(true); + Heap::GetHeap().GetCollectorResources().SetGcStarted(false); + + uintptr_t result = theAllocator.Allocate(16, AllocType::PINNED_OBJECT); + + EXPECT_NE(result, 0u); +} + +HWTEST_F_L0(RegionSpaceTest, FeedHungryBuffers_ShouldProvideValidRegions) +{ + RegionSpace& theAllocator = reinterpret_cast(Heap::GetHeap().GetAllocator()); + RegionManager& manager = theAllocator.GetRegionManager(); + + manager.Initialize(totalUnits_, reinterpret_cast(regionMemory_)); + + AllocationBuffer* buffer1 = new (std::nothrow) AllocationBuffer(); + AllocationBuffer* buffer2 = new (std::nothrow) AllocationBuffer(); + ASSERT_NE(buffer1, nullptr); + ASSERT_NE(buffer2, nullptr); + + RegionDesc* region = RegionDesc::InitRegion(0, totalUnits_, RegionDesc::UnitRole::SMALL_SIZED_UNITS); + ASSERT_NE(region, nullptr); + + region->SetRegionType(RegionDesc::RegionType::THREAD_LOCAL_REGION); + + buffer1->SetPreparedRegion(region); + + Heap::GetHeap().GetAllocator().AddHungryBuffer(*buffer1); + Heap::GetHeap().GetAllocator().AddHungryBuffer(*buffer2); + + Mutator::GetMutator()->SetMutatorPhase(GCPhase::GC_PHASE_FIX); + + Heap::GetHeap().GetAllocator().FeedHungryBuffers(); + + EXPECT_NE(buffer2->GetPreparedRegion(), nullptr); + delete buffer1; + delete buffer2; +} + +HWTEST_F_L0(RegionSpaceTest, AllocationBuffer_AllocateRawPointerObject_ValidSize_ReturnsNonNull) +{ + RegionSpace& theAllocator = reinterpret_cast(Heap::GetHeap().GetAllocator()); + RegionManager& manager = theAllocator.GetRegionManager(); + + manager.Initialize(totalUnits_, reinterpret_cast(regionMemory_)); + + RegionDesc* region = RegionDesc::InitRegion(0, totalUnits_, RegionDesc::UnitRole::SMALL_SIZED_UNITS); + ASSERT_NE(region, nullptr); + + region->SetRegionType(RegionDesc::RegionType::THREAD_LOCAL_REGION); + + RegionDesc* takenRegion = manager.TakeRegion(16, RegionDesc::UnitRole::SMALL_SIZED_UNITS, true, false); + ASSERT_NE(takenRegion, nullptr); + + takenRegion->SetRegionType(RegionDesc::RegionType::THREAD_LOCAL_REGION); + + uintptr_t bufferStart = takenRegion->GetRegionStart() + RegionDesc::UNIT_SIZE; + uintptr_t bufferEnd = takenRegion->GetRegionEnd(); + + AllocationBuffer* buffer = new (std::nothrow) AllocationBuffer(); + ASSERT_NE(buffer, nullptr); + + buffer->SetPreparedRegion(takenRegion); + + Mutator::GetMutator()->SetMutatorPhase(GCPhase::GC_PHASE_UNDEF); + + Heap::GetHeap().EnableGC(true); + Heap::GetHeap().GetCollectorResources().SetGcStarted(false); + + uintptr_t result = theAllocator.Allocate(16, AllocType::PINNED_OBJECT); + + EXPECT_NE(result, 0u); + delete buffer; +} + } -- Gitee