diff --git a/common_components/heap/w_collector/tests/w_collector_test.cpp b/common_components/heap/w_collector/tests/w_collector_test.cpp index 1534f3d9b1343e990960dce124f1e02db7fa09fc..7903ba40bfd1afed4e23bc22075bf63051c58811 100644 --- a/common_components/heap/w_collector/tests/w_collector_test.cpp +++ b/common_components/heap/w_collector/tests/w_collector_test.cpp @@ -21,8 +21,70 @@ #include "common_components/heap/allocator/region_desc.h" #include "common_components/mutator/mutator_manager-inl.h" +#include "common_interfaces/objects/base_state_word.h" +#include "common_components/heap/collector/gc_stats.h" +#include "ecmascript/napi/include/jsnapi_expo.h" +#include "common_interfaces/base_runtime.h" + using namespace common; +class DumbObject : public BaseObject { +public: + size_t GetSize() const { return sizeof(DumbObject); } +}; + +class TestObject : public BaseObject { +public: + RefField<> ref_; + TestObject() : ref_(nullptr) {} + void SetRefField(BaseObject* target) + { + RefField<> oldField(ref_); + RefField<> newField(target); + bool success = ref_.CompareExchange(oldField.GetFieldValue(), newField.GetFieldValue()); + ASSERT(success); + } +}; + +class TestableWCollector : public WCollector { +public: + using WCollector::WCollector; + + void PublicDoGarbageCollection() + { + DoGarbageCollection(); + } + + void PublicSetGCReason(GCReason reason) + { + SetGCReason(reason); + } + + BaseObject* CallCopyObjectAfterExclusiveForTest(BaseObject* obj) + { + return CopyObjectAfterExclusive(obj); + } + + void PublicCollectSmallSpace() + { + CollectSmallSpace(); + } + + GCStats& GetGCStatsForTest() + { + return collectorResources_.GetGCStats(); + } + + void AddRoot(ObjectRef* root) + { + ObjectVisitor visitor = [root](ObjectPtr obj) { + reinterpret_cast(*root) = obj; + }; + BaseObject* rawRoot = reinterpret_cast(root); + ForwardUpdateRawRef(*root); + } +}; + namespace common::test { using SuspensionType = MutatorBase::SuspensionType; class WCollectorTest : public BaseTestWithScope { @@ -135,4 +197,32 @@ HWTEST_F_L0(WCollectorTest, FlipTest) std::thread t1(FlipTest); t1.join(); } + +HWTEST_F_L0(WCollectorTest, CollectSmallSpaceTest) +{ + std::unique_ptr wcollector = std::make_unique( + Heap::GetHeap().GetAllocator(), Heap::GetHeap().GetCollectorResources()); + + HeapAddress addrLive = HeapManager::Allocate(sizeof(DumbObject), AllocType::MOVEABLE_OBJECT, true); + DumbObject* liveObj = new (reinterpret_cast(addrLive)) DumbObject(); + ObjectRef root = {liveObj}; + + wcollector->AddRoot(&root); + + HeapAddress addrDead = HeapManager::Allocate(sizeof(DumbObject), AllocType::MOVEABLE_OBJECT, true); + DumbObject* deadObj = new (reinterpret_cast(addrDead)) DumbObject(); + + GCStats& statsBefore = wcollector->GetGCStatsForTest(); + size_t beforeFromSpaceSize = statsBefore.fromSpaceSize; + + wcollector->PublicSetGCReason(GCReason::GC_REASON_APPSPAWN); + wcollector->PublicCollectSmallSpace(); + + GCStats& statsAfter = wcollector->GetGCStatsForTest(); + + EXPECT_TRUE(liveObj != nullptr); + + SUCCEED(); +} + } // namespace common::test diff --git a/common_components/objects/tests/BUILD.gn b/common_components/objects/tests/BUILD.gn index e6d55ad7b48d80040124371fc808a3d69a2713c7..5e2becfc18ef4135c82d2aa4ba2bd202502f085a 100755 --- a/common_components/objects/tests/BUILD.gn +++ b/common_components/objects/tests/BUILD.gn @@ -40,12 +40,36 @@ host_unittest_action("Base_String_Test") { ] } +host_unittest_action("Base_String_Table_Test") { + module_out_path = module_output_path + + sources = [ + # test file + "base_string_table_test.cpp", + ] + + configs = [ + "//arkcompiler/ets_runtime/common_components:common_components_test_config", + "//arkcompiler/ets_runtime:icu_path_test_config", + ] + + deps = [ "//arkcompiler/ets_runtime/common_components:libark_common_components_test" ] + + # hiviewdfx libraries + external_deps = [ + "icu:shared_icui18n", + "icu:shared_icuuc", + "zlib:libz", + ] +} + group("unittest") { testonly = true # deps file deps = [ ":Base_String_Test", + ":Base_String_Table_Test", ] } @@ -55,5 +79,6 @@ group("host_unittest") { # deps file deps = [ ":Base_String_TestAction", + ":Base_String_Table_TestAction", ] } diff --git a/common_components/objects/tests/base_string_table_test.cpp b/common_components/objects/tests/base_string_table_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..aa165055f1b83685b0faa961d924c2bb8705d48a --- /dev/null +++ b/common_components/objects/tests/base_string_table_test.cpp @@ -0,0 +1,112 @@ +/* + * 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/tests/test_helper.h" +#include "common_interfaces/objects/base_string_table.h" +#include "common_components/objects/string_table_internal.h" +#include "common_interfaces/thread/mutator_base.h" +#include "common_interfaces/objects/base_string.h" +#include "common_interfaces/thread/thread_holder.h" +#include "common_interfaces/base_runtime.h" +#include "common_interfaces/heap/heap_allocator.h" +#include "common_interfaces/objects/string/base_string-inl2.h" + + +namespace common { + +struct DummyMutator : public MutatorBase { + explicit DummyMutator(LanguageType lang) : lang_(lang) {} + LanguageType lang_; +}; + +class BaseStringTableTest : public common::test::BaseTestWithScope { +protected: + using TableType = BaseStringTableInternal; + BaseRuntime* runtime_; + std::unique_ptr mutator_; + std::unique_ptr table_; + ThreadHolder* threadHolder_; + + void SetUp() override + { + mutator_ = std::make_unique(LanguageType::DYNAMIC); + threadHolder_ = new ThreadHolder(mutator_.get()); + + runtime_ = BaseRuntime::GetInstance(); + ASSERT_TRUE(runtime_ != nullptr); + + runtime_->Init(); + + table_ = std::make_unique(); + } + + void TearDown() override + { + table_.reset(); + + if (runtime_) { + runtime_->Fini(); + } + + mutator_.reset(); + threadHolder_ = nullptr; + } + + BaseString* CreateUtf8String(const char* utf8Data, uint32_t length, bool canBeCompress) + { + auto allocator = [](size_t size, CommonType type) -> BaseString* { + void* mem = reinterpret_cast(HeapAllocator::AllocateInOldOrHuge(size, LanguageType::DYNAMIC)); + if (mem == nullptr) { + return nullptr; + } + return reinterpret_cast(mem); + }; + + BaseString* str = BaseString::CreateFromUtf8(allocator, + reinterpret_cast(utf8Data), length, canBeCompress); + + if (str == nullptr) { + return nullptr; + } + return str; + } + + static ReadOnlyHandle MockHandleCreator(ThreadHolder* holder, BaseString* str) + { + uintptr_t handleValue = reinterpret_cast(str); + return ReadOnlyHandle(handleValue); + } +}; + +HWTEST_F_L0(BaseStringTableTest, SweepWeakRef) +{ + WeakRefFieldVisitor mockVisitor = [](RefField& field) { + return true; + }; + + table_->GetHashTrieMap().StartSweeping(); + table_->SweepWeakRef(mockVisitor); + table_->GetHashTrieMap().FinishSweeping(); + + EXPECT_TRUE(true); +} + +HWTEST_F_L0(BaseStringTableTest, CleanUp) +{ + table_->GetHashTrieMap().Clear(); + EXPECT_TRUE(true); +} + +} \ No newline at end of file diff --git a/common_components/tests/ohos_test.xml b/common_components/tests/ohos_test.xml index a9332b249edfadefb88244a85a60ce144a938823..4a023d31a0d1bd85160071861c54666a46fbbecf 100644 --- a/common_components/tests/ohos_test.xml +++ b/common_components/tests/ohos_test.xml @@ -103,4 +103,9 @@