From 02d07c56d1cbeab8f3ec7c7f69a508774805f4e5 Mon Sep 17 00:00:00 2001 From: yp9522 Date: Mon, 7 Jul 2025 10:34:05 +0800 Subject: [PATCH] add common_components tdd Issue: https://gitee.com/openharmony/arkcompiler_ets_runtime/issues/ICKDBN Change-Id: I0c4b1d1252613e7c4ce0dbbcb6cee17584b7436c Signed-off-by: yp9522 --- BUILD.gn | 4 + common_components/mutator/tests/BUILD.gn | 25 ++++ .../mutator/tests/thread_local_test.cpp | 31 +++++ common_components/objects/tests/BUILD.gn | 28 +++++ .../tests/composite_base_class_test.cpp | 114 ++++++++++++++++++ .../platform/unix/tests/BUILD.gn | 57 +++++++++ .../platform/unix/tests/map_test.cpp | 44 +++++++ common_components/taskpool/tests/BUILD.gn | 82 +++++++++++++ .../taskpool/tests/runner_test.cpp | 65 ++++++++++ .../taskpool/tests/taskpool_test.cpp | 92 ++++++++++++++ common_components/tests/ohos_test.xml | 25 ++++ 11 files changed, 567 insertions(+) create mode 100644 common_components/mutator/tests/thread_local_test.cpp create mode 100644 common_components/objects/tests/composite_base_class_test.cpp create mode 100644 common_components/platform/unix/tests/BUILD.gn create mode 100644 common_components/platform/unix/tests/map_test.cpp create mode 100644 common_components/taskpool/tests/BUILD.gn create mode 100644 common_components/taskpool/tests/runner_test.cpp create mode 100644 common_components/taskpool/tests/taskpool_test.cpp diff --git a/BUILD.gn b/BUILD.gn index 950996df5b..d5461e60d7 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -168,6 +168,8 @@ group("common_components_js_unittest") { "common_components/mutator/tests:unittest", "common_components/objects/tests:unittest", "common_components/thread/tests:unittest", + "common_components/platform/unix/tests:unittest", + "common_components/taskpool/tests:unittest", ] } } @@ -189,6 +191,8 @@ group("common_components_unittest") { "common_components/mutator/tests:host_unittest", "common_components/objects/tests:host_unittest", "common_components/thread/tests:host_unittest", + "common_components/platform/unix/tests:host_unittest", + "common_components/taskpool/tests:host_unittest", ] } } diff --git a/common_components/mutator/tests/BUILD.gn b/common_components/mutator/tests/BUILD.gn index 1671269613..cbc967f703 100755 --- a/common_components/mutator/tests/BUILD.gn +++ b/common_components/mutator/tests/BUILD.gn @@ -84,6 +84,29 @@ host_unittest_action("Satb_Buffer_Test") { ] } +host_unittest_action("Thread_Local_Test") { + module_out_path = module_output_path + + sources = [ + # test file + "thread_local_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 @@ -92,6 +115,7 @@ group("unittest") { ":Mutator_Test", ":Mutator_Manager_Test", ":Satb_Buffer_Test", + ":Thread_Local_Test", ] } @@ -103,5 +127,6 @@ group("host_unittest") { ":Mutator_TestAction", ":Mutator_Manager_TestAction", ":Satb_Buffer_TestAction", + ":Thread_Local_TestAction", ] } diff --git a/common_components/mutator/tests/thread_local_test.cpp b/common_components/mutator/tests/thread_local_test.cpp new file mode 100644 index 0000000000..8ee8f5cbf9 --- /dev/null +++ b/common_components/mutator/tests/thread_local_test.cpp @@ -0,0 +1,31 @@ +/* + * 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/mutator/thread_local.h" +#include "common_components/tests/test_helper.h" + +namespace common { + +class ThreadLocalTest : public common::test::BaseTestWithScope { +protected: + void SetUp() override {} + void TearDown() override {} +}; + +HWTEST_F_L0(ThreadLocalTest, GetThreadLocalData_ReturnsNonNull) { + ThreadLocalData* data = ThreadLocal::GetThreadLocalData(); + EXPECT_NE(data, nullptr); +} +} \ No newline at end of file diff --git a/common_components/objects/tests/BUILD.gn b/common_components/objects/tests/BUILD.gn index e6d55ad7b4..b311ebe412 100755 --- a/common_components/objects/tests/BUILD.gn +++ b/common_components/objects/tests/BUILD.gn @@ -40,12 +40,39 @@ host_unittest_action("Base_String_Test") { ] } +host_unittest_action("Composite_Base_Class_Test") { + module_out_path = module_output_path + + sources = [ + # test file + "composite_base_class_test.cpp", + "//arkcompiler/ets_runtime/common_components/objects/base_string.cpp", + "//arkcompiler/ets_runtime/common_components/base/utf_helper.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", + ":Composite_Base_Class_Test", ] } @@ -55,5 +82,6 @@ group("host_unittest") { # deps file deps = [ ":Base_String_TestAction", + ":Composite_Base_Class_TestAction", ] } diff --git a/common_components/objects/tests/composite_base_class_test.cpp b/common_components/objects/tests/composite_base_class_test.cpp new file mode 100644 index 0000000000..d5a1c91b7f --- /dev/null +++ b/common_components/objects/tests/composite_base_class_test.cpp @@ -0,0 +1,114 @@ +/* +* 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/composite_base_class.h" +#include "common_interfaces/objects/base_object.h" + +#include +#include +#include + +namespace common { +class CompositeBaseClassTest : public test::BaseTestWithScope { +protected: + static CompositeBaseClass* MockAllocator() + { + void* memory = ::operator new(sizeof(uint64_t) * 16); + + auto* baseClass = reinterpret_cast(memory); + baseClass->SetObjectType(CommonType::LINE_STRING); + baseClass->ClearBitField(); + + return reinterpret_cast(baseClass); + } + + void SetUp() override + { + roots_ = std::make_unique(); + } + + void TearDown() override + { + roots_.reset(); + } + + std::unique_ptr roots_; +}; + +HWTEST_F_L0(CompositeBaseClassTest, InitializeOnce) +{ + auto allocator = []() -> CompositeBaseClass* { + return CompositeBaseClassTest::MockAllocator(); + }; + + roots_->InitializeCompositeBaseClass(allocator); + roots_->InitializeCompositeBaseClass(allocator); + + EXPECT_TRUE(true); +} + +HWTEST_F_L0(CompositeBaseClassTest, CreateAndGetType) +{ + auto allocator = []() -> CompositeBaseClass* { + return CompositeBaseClassTest::MockAllocator(); + }; + + roots_->InitializeCompositeBaseClass(allocator); + + auto* baseClass = roots_->GetBaseClass(CommonType::LINE_STRING); + ASSERT_NE(baseClass, nullptr); + EXPECT_EQ(baseClass->GetObjectType(), CommonType::LINE_STRING); +} + +HWTEST_F_L0(CompositeBaseClassTest, GetBaseClassReturnsCorrectType) +{ + auto allocator = []() -> CompositeBaseClass* { + return CompositeBaseClassTest::MockAllocator(); + }; + + roots_->InitializeCompositeBaseClass(allocator); + + auto* lineString = roots_->GetBaseClass(CommonType::LINE_STRING); + auto* slicedString = roots_->GetBaseClass(CommonType::SLICED_STRING); + auto* treeString = roots_->GetBaseClass(CommonType::TREE_STRING); + + ASSERT_NE(lineString, nullptr); + ASSERT_NE(slicedString, nullptr); + ASSERT_NE(treeString, nullptr); + + EXPECT_EQ(lineString->GetObjectType(), CommonType::LINE_STRING); + EXPECT_EQ(slicedString->GetObjectType(), CommonType::SLICED_STRING); + EXPECT_EQ(treeString->GetObjectType(), CommonType::TREE_STRING); +} + +HWTEST_F_L0(CompositeBaseClassTest, IterateCompositeBaseClass) +{ + auto allocator = []() -> CompositeBaseClass* { + return CompositeBaseClassTest::MockAllocator(); + }; + + roots_->InitializeCompositeBaseClass(allocator); + + std::vector visited; + + roots_->IterateCompositeBaseClass([&visited](RefField<>& field) { + auto* ptr = reinterpret_cast(const_cast(static_cast(&field))); + visited.push_back(ptr); + }); + + EXPECT_EQ(visited.size(), 3); +} +} \ No newline at end of file diff --git a/common_components/platform/unix/tests/BUILD.gn b/common_components/platform/unix/tests/BUILD.gn new file mode 100644 index 0000000000..645bc239e7 --- /dev/null +++ b/common_components/platform/unix/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("Map_Test") { + module_out_path = module_output_path + + sources = [ + # test file + "map_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 = [ + ":Map_Test", + ] +} + +group("host_unittest") { + testonly = true + + # deps file + deps = [ + ":Map_TestAction", + ] +} \ No newline at end of file diff --git a/common_components/platform/unix/tests/map_test.cpp b/common_components/platform/unix/tests/map_test.cpp new file mode 100644 index 0000000000..4032e3a186 --- /dev/null +++ b/common_components/platform/unix/tests/map_test.cpp @@ -0,0 +1,44 @@ +/* + * 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_components/platform/map.h" + +#include +#include + +class PageProtectTest : public common::test::BaseTestWithScope { +protected: + void SetUp() override {} + void TearDown() override {} +}; + +HWTEST_F_L0(PageProtectTest, TestPageProtect) +{ + size_t pageSize = getpagesize(); + void* mem = mmap(nullptr, pageSize, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + ASSERT_NE(mem, MAP_FAILED); + + bool success = common::PageProtect(mem, pageSize, PROT_READ); + EXPECT_TRUE(success); + + munmap(mem, pageSize); +} + +HWTEST_F_L0(PageProtectTest, TestNullptrMemory) +{ + bool success = common::PageProtect(nullptr, getpagesize(), PROT_READ); + EXPECT_FALSE(success); +} \ No newline at end of file diff --git a/common_components/taskpool/tests/BUILD.gn b/common_components/taskpool/tests/BUILD.gn new file mode 100644 index 0000000000..08785eb844 --- /dev/null +++ b/common_components/taskpool/tests/BUILD.gn @@ -0,0 +1,82 @@ +# 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("Runner_Test") { + module_out_path = module_output_path + + sources = [ + # test file + "runner_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", + ] +} + +host_unittest_action("Taskpool_Test") { + module_out_path = module_output_path + + sources = [ + # test file + "taskpool_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 = [ + ":Runner_Test", + ":Taskpool_Test", + ] +} + +group("host_unittest") { + testonly = true + + # deps file + deps = [ + ":Runner_TestAction", + ":Taskpool_TestAction", + ] +} \ No newline at end of file diff --git a/common_components/taskpool/tests/runner_test.cpp b/common_components/taskpool/tests/runner_test.cpp new file mode 100644 index 0000000000..4e005bdccb --- /dev/null +++ b/common_components/taskpool/tests/runner_test.cpp @@ -0,0 +1,65 @@ +/* + * 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_components/taskpool/runner.h" + +#include +#include +#include +#include + +namespace common { +class RunnerTest : public common::test::BaseTestWithScope { +protected: + void SetUp() override {} + void TearDown() override {} + + static std::function CreateMockPrologueHook() + { + return [](native_handle_type handle) {}; + } + + static std::function CreateMockEpilogueHook() + { + return [](native_handle_type handle) {}; + } +}; + +class MockTask : public Task { +public: + explicit MockTask(int32_t id) + : Task(id), executed_(false) {} + + bool Run(uint32_t threadId) override + { + executed_ = true; + return true; + } + + bool IsExecuted() const { return executed_; } + +private: + std::atomic executed_; +}; + +HWTEST_F_L0(RunnerTest, InitializeRunnerWithThreads) { + constexpr uint32_t threadNum = 4; + Runner runner(threadNum, RunnerTest::CreateMockPrologueHook(), RunnerTest::CreateMockEpilogueHook()); + EXPECT_EQ(runner.GetTotalThreadNum(), threadNum); + + runner.TerminateThread(); +} +} \ No newline at end of file diff --git a/common_components/taskpool/tests/taskpool_test.cpp b/common_components/taskpool/tests/taskpool_test.cpp new file mode 100644 index 0000000000..5e22daf35c --- /dev/null +++ b/common_components/taskpool/tests/taskpool_test.cpp @@ -0,0 +1,92 @@ +/* + * 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_components/taskpool/taskpool.h" +#include "common_components/taskpool/task.h" + +#include +#include +#include + +namespace common { + +class MockTask : public Task { +public: + explicit MockTask(int32_t id) + : Task(id), executed_(false) {} + + bool Run(uint32_t threadId) override + { + executed_ = true; + return true; + } + + bool IsExecuted() const { return executed_; } + +private: + std::atomic executed_; +}; + +class TaskpoolTest : public common::test::BaseTestWithScope { +protected: + void SetUp() override {} + void TearDown() override {} + + class ScopedTaskpool { + public: + explicit ScopedTaskpool(int threadNum = DEFAULT_TASKPOOL_THREAD_NUM) + : isInitialized_(false) + { + isInitialized_ = true; + pool_.Initialize(threadNum); + } + + Taskpool* Get() + { + return &pool_; + } + + private: + Taskpool pool_; + bool isInitialized_; + }; +}; + +HWTEST_F_L0(TaskpoolTest, InitializeAndDestroy) { + TaskpoolTest::ScopedTaskpool pool(2); + EXPECT_NE(pool.Get(), nullptr); + EXPECT_GT(pool.Get()->GetTotalThreadNum(), 0U); +} + +HWTEST_F_L0(TaskpoolTest, SetQosPriority) { + TaskpoolTest::ScopedTaskpool pool(1); + pool.Get()->SetThreadPriority(PriorityMode::BACKGROUND); +} + +HWTEST_F_L0(TaskpoolTest, ForEachTask) { + TaskpoolTest::ScopedTaskpool pool(1); + + auto task = std::make_unique(1); + pool.Get()->PostTask(std::move(task)); + + std::atomic count(0); + pool.Get()->ForEachTask([&count](Task* t) { + count++; + }); + + EXPECT_EQ(count.load(), 1); +} +} \ No newline at end of file diff --git a/common_components/tests/ohos_test.xml b/common_components/tests/ohos_test.xml index e43733dde5..dfab618df7 100644 --- a/common_components/tests/ohos_test.xml +++ b/common_components/tests/ohos_test.xml @@ -83,4 +83,29 @@