From a66a1ee97b71afe008f0658ab206c18da4382373 Mon Sep 17 00:00:00 2001 From: yp9522 Date: Wed, 16 Jul 2025 17:25:01 +0800 Subject: [PATCH] add common_components tdd Issue:https://gitee.com/openharmony/arkcompiler_ets_runtime/issues/ICMRJE?from=project-issue Signed-off-by: yp9522 --- BUILD.gn | 4 + common_components/heap/tests/BUILD.gn | 59 ++++++++ .../heap/tests/verification_test.cpp | 142 ++++++++++++++++++ common_components/serialize/tests/BUILD.gn | 57 +++++++ .../serialize/tests/serialize_utils_test.cpp | 57 +++++++ 5 files changed, 319 insertions(+) create mode 100644 common_components/heap/tests/BUILD.gn create mode 100644 common_components/heap/tests/verification_test.cpp create mode 100644 common_components/serialize/tests/BUILD.gn create mode 100644 common_components/serialize/tests/serialize_utils_test.cpp diff --git a/BUILD.gn b/BUILD.gn index 7a00f3610d..5c15e633c8 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -164,9 +164,11 @@ group("common_components_js_unittest") { "common_components/heap/barrier/tests:unittest", #"common_components/heap/collector/tests:unittest", + "common_components/heap/tests:unittest", "common_components/heap/w_collector/tests:unittest", "common_components/mutator/tests:unittest", "common_components/objects/tests:unittest", + "common_components/serialize/tests:unittest", "common_components/thread/tests:unittest", "common_components/log/tests:unittest", ] @@ -186,9 +188,11 @@ group("common_components_unittest") { "common_components/heap/barrier/tests:host_unittest", #"common_components/heap/collector/tests:host_unittest", + "common_components/heap/tests:host_unittest", "common_components/heap/w_collector/tests:host_unittest", "common_components/mutator/tests:host_unittest", "common_components/objects/tests:host_unittest", + "common_components/serialize/tests:host_unittest", "common_components/thread/tests:host_unittest", "common_components/log/tests:host_unittest", ] diff --git a/common_components/heap/tests/BUILD.gn b/common_components/heap/tests/BUILD.gn new file mode 100644 index 0000000000..00ebea53a0 --- /dev/null +++ b/common_components/heap/tests/BUILD.gn @@ -0,0 +1,59 @@ +# 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. + +import("//arkcompiler/ets_runtime/common_components/tests/test_helper.gni") + +module_output_path = "ets_runtime" + +host_unittest_action("Verification_Test") { + module_out_path = module_output_path + + sources = [ + # test file + "verification_test.cpp", + ] + + include_dirs = [ "//arkcompiler/ets_runtime/common_components/" ] + + 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 = [ + ":Verification_Test", + ] +} + +group("host_unittest") { + testonly = true + + # deps file + deps = [ + ":Verification_TestAction", + ] +} \ No newline at end of file diff --git a/common_components/heap/tests/verification_test.cpp b/common_components/heap/tests/verification_test.cpp new file mode 100644 index 0000000000..1f6361f765 --- /dev/null +++ b/common_components/heap/tests/verification_test.cpp @@ -0,0 +1,142 @@ +/* +* 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/verification.cpp" +#include "common_components/heap/heap_manager.h" +#include "common_components/heap/w_collector/w_collector.h" +#include "common_components/tests/test_helper.h" +#include "common_interfaces/objects/base_object_operator.h" + +using namespace common; +namespace common::test { +class TestBaseObjectOperator : public common::BaseObjectOperatorInterfaces { +public: + bool IsValidObject([[maybe_unused]] const BaseObject *object) const override { return enbaleValidObject_; } + void ForEachRefField(const BaseObject *object, const common::RefFieldVisitor &visitor) const override {} + size_t GetSize(const BaseObject *object) const override{ return size_; } + BaseObject *GetForwardingPointer(const BaseObject *object) const override { return nullptr; } + void SetForwardingPointerAfterExclusive(BaseObject *object, BaseObject *fwdPtr) override {} + void SetValidObject(bool value) { enbaleValidObject_ = value; } + void SetSize(size_t size) { size_ = size; } +private: + bool enbaleValidObject_ = false; + size_t size_ = 0; +}; +class VerificationTest : public common::test::BaseTestWithScope { +protected: + static void SetUpTestCase() + { + BaseRuntime::GetInstance()->Init(); + } + + static void TearDownTestCase() + { + BaseRuntime::GetInstance()->Fini(); + } + + void SetUp() override + { + MutatorManager::Instance().CreateRuntimeMutator(ThreadType::GC_THREAD); + } + + void TearDown() override + { + MutatorManager::Instance().DestroyRuntimeMutator(ThreadType::GC_THREAD); + } +}; + +HWTEST_F_L0(VerificationTest, GetObjectInfoTest3) +{ + HeapAddress addr = HeapManager::Allocate(sizeof(BaseObject), AllocType::PINNED_OBJECT, true); + BaseObject *obj = reinterpret_cast(addr); + std::string result = GetObjectInfo(obj); + EXPECT_NE(result.find("address: 0x"), std::string::npos); + EXPECT_NE(result.find("Type: 0x"), std::string::npos); + EXPECT_NE(result.find("Base: 0x"), std::string::npos); + EXPECT_NE(result.find("Start: 0x"), std::string::npos); + EXPECT_NE(result.find("End: 0x"), std::string::npos); + EXPECT_NE(result.find("AllocPtr: 0x"), std::string::npos); + EXPECT_NE(result.find("TraceLine: 0x"), std::string::npos); + EXPECT_NE(result.find("CopyLine: 0x"), std::string::npos); +} + +HWTEST_F_L0(VerificationTest, GetRefInfoTest2) +{ + RefField field(nullptr); + uintptr_t taggedValue = 0x04; + field.SetFieldValue(static_cast(taggedValue)); + std::string result = GetRefInfo(field); + EXPECT_NE(result.find("> Raw memory:"), std::string::npos); + EXPECT_NE(result.find("Skip: primitive"), std::string::npos); +} + +HWTEST_F_L0(VerificationTest, VerifyRefImplTest) +{ + HeapAddress addr = HeapManager::Allocate(sizeof(BaseObject), AllocType::PINNED_OBJECT, true); + BaseObject *obj = reinterpret_cast(addr); + RefField oldField(obj); + TestBaseObjectOperator operatorImpl; + BaseObject::RegisterDynamic(&operatorImpl); + operatorImpl.SetValidObject(true); + Heap::GetHeap().SetGCReason(GCReason::GC_REASON_YOUNG); + operatorImpl.SetSize(BaseObject::BaseObjectSize()); + AfterMarkVisitor visitor; + visitor.VerifyRefImpl(nullptr, oldField); + ASSERT_TRUE(Heap::GetHeap().GetGCReason() == GCReason::GC_REASON_YOUNG); + ASSERT_TRUE(Heap::IsTaggedObject(oldField.GetFieldValue())); + + AfterMarkVisitor visitor1; + visitor1.VerifyRefImpl(nullptr, oldField); + ASSERT_TRUE(Heap::IsTaggedObject(oldField.GetFieldValue())); +} + +HWTEST_F_L0(VerificationTest, VerifyRefImplTest1) +{ + HeapAddress addr = HeapManager::Allocate(sizeof(BaseObject), AllocType::PINNED_OBJECT, true); + BaseObject *obj = reinterpret_cast(addr); + RefField oldField(obj); + TestBaseObjectOperator operatorImpl; + BaseObject::RegisterDynamic(&operatorImpl); + operatorImpl.SetValidObject(true); + Heap::GetHeap().SetGCReason(GCReason::GC_REASON_YOUNG); + operatorImpl.SetSize(BaseObject::BaseObjectSize()); + AfterMarkVisitor visitor; + visitor.VerifyRefImpl(obj, oldField); + ASSERT_TRUE(Heap::GetHeap().GetGCReason() == GCReason::GC_REASON_YOUNG); + ASSERT_TRUE(Heap::IsTaggedObject(oldField.GetFieldValue())); +} + +static BaseObject* testObj = nullptr; +static void CustomVisitRoot(const RefFieldVisitor& visitorFunc) +{ + RefField<> field(testObj); + visitorFunc(field); +} +HWTEST_F_L0(VerificationTest, IterateRetraced_VerifyAllRefs) +{ + RegionSpace regionSpace; + VerifyIterator verify(regionSpace); + AfterForwardVisitor visitor; + std::unordered_set markSet; + HeapAddress addr = HeapManager::Allocate(sizeof(BaseObject), AllocType::PINNED_OBJECT, true); + testObj = reinterpret_cast(addr); + markSet.insert(testObj); + + verify.IterateRetraced(visitor, markSet, true); + verify.IterateRetraced(visitor, markSet, false); + EXPECT_EQ(markSet.size(), 1); + EXPECT_TRUE(markSet.find(testObj) != markSet.end()); +} +} // namespace common::test diff --git a/common_components/serialize/tests/BUILD.gn b/common_components/serialize/tests/BUILD.gn new file mode 100644 index 0000000000..bfcdc43839 --- /dev/null +++ b/common_components/serialize/tests/BUILD.gn @@ -0,0 +1,57 @@ +# 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. + +import("//arkcompiler/ets_runtime/common_components/tests/test_helper.gni") + +module_output_path = "ets_runtime" + +host_unittest_action("Serialize_Utils_Test") { + module_out_path = module_output_path + + sources = [ + # test file + "serialize_utils_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 = [ + ":Serialize_Utils_Test", + ] +} + +group("host_unittest") { + testonly = true + + # deps file + deps = [ + ":Serialize_Utils_TestAction", + ] +} diff --git a/common_components/serialize/tests/serialize_utils_test.cpp b/common_components/serialize/tests/serialize_utils_test.cpp new file mode 100644 index 0000000000..680b592484 --- /dev/null +++ b/common_components/serialize/tests/serialize_utils_test.cpp @@ -0,0 +1,57 @@ +/* +* 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_desc.h" +#include "common_components/heap/allocator/region_space.h" +#include "common_components/serialize/serialize_utils.h" +#include "common_components/serialize/serialize_utils.cpp" +#include "common_components/tests/test_helper.h" + +using namespace common; + +namespace common::test { +class SerializeUtilsTest : public common::test::BaseTestWithScope { +protected: + static void SetUpTestCase() + { + BaseRuntime::GetInstance()->Init(); + } + + static void TearDownTestCase() + { + BaseRuntime::GetInstance()->Fini(); + } + void SetUp() override + { + MutatorManager::Instance().CreateRuntimeMutator(ThreadType::GC_THREAD); + } + + void TearDown() override + { + MutatorManager::Instance().DestroyRuntimeMutator(ThreadType::GC_THREAD); + } +}; + +HWTEST_F_L0(SerializeUtilsTest, GetSerializeObjectSpace) +{ + RegionSpace& theAllocator = reinterpret_cast(Heap::GetHeap().GetAllocator()); + uintptr_t addr = theAllocator.AllocOldRegion(); + ASSERT_NE(addr, 0); + RegionDesc* region = RegionDesc::GetRegionDescAt(addr); + region->SetRegionType(RegionDesc::RegionType::END_OF_REGION_TYPE); + SerializedBaseObjectSpace spaceType = SerializeUtils::GetSerializeObjectSpace(addr); + EXPECT_EQ(spaceType, SerializedBaseObjectSpace::OTHER); +} +} // namespace common::test \ No newline at end of file -- Gitee