From d3daa656d8dd6e14639d94512de49e9bf13ac846 Mon Sep 17 00:00:00 2001 From: yp9522 Date: Wed, 9 Jul 2025 10:36:58 +0800 Subject: [PATCH] add common_components tdd Issue:https://gitee.com/openharmony/arkcompiler_ets_runtime/issues/ICKYWD Change-Id: I94d52d81e255a389bf90c82f017d5a0f6903c778 Signed-off-by: yp9522 --- common_components/objects/tests/BUILD.gn | 25 ++++ .../objects/tests/base_string_table_test.cpp | 112 ++++++++++++++++++ common_components/tests/ohos_test.xml | 5 + 3 files changed, 142 insertions(+) create mode 100644 common_components/objects/tests/base_string_table_test.cpp diff --git a/common_components/objects/tests/BUILD.gn b/common_components/objects/tests/BUILD.gn index e6d55ad7b4..5e2becfc18 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 0000000000..aa165055f1 --- /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 a9332b249e..4a023d31a0 100644 --- a/common_components/tests/ohos_test.xml +++ b/common_components/tests/ohos_test.xml @@ -103,4 +103,9 @@