From bd1455b9895c436d25da766874975e7fd8f5f723 Mon Sep 17 00:00:00 2001 From: yp9522 Date: Tue, 15 Jul 2025 16:50:54 +0800 Subject: [PATCH] add common_components tdd Issue:https://gitee.com/openharmony/arkcompiler_ets_runtime/issues/ICMHF0?from=project-issue Signed-off-by: yp9522 --- BUILD.gn | 2 + .../objects/tests/base_string_test.cpp | 144 ++++++++++++++++++ common_components/profiler/tests/BUILD.gn | 57 +++++++ .../tests/common_profiler_interface_test.cpp | 96 ++++++++++++ common_components/tests/ohos_test.xml | 5 + 5 files changed, 304 insertions(+) create mode 100644 common_components/profiler/tests/BUILD.gn create mode 100644 common_components/profiler/tests/common_profiler_interface_test.cpp diff --git a/BUILD.gn b/BUILD.gn index 7a00f3610d..e712570af8 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -167,6 +167,7 @@ group("common_components_js_unittest") { "common_components/heap/w_collector/tests:unittest", "common_components/mutator/tests:unittest", "common_components/objects/tests:unittest", + "common_components/profiler/tests:unittest", "common_components/thread/tests:unittest", "common_components/log/tests:unittest", ] @@ -189,6 +190,7 @@ group("common_components_unittest") { "common_components/heap/w_collector/tests:host_unittest", "common_components/mutator/tests:host_unittest", "common_components/objects/tests:host_unittest", + "common_components/profiler/tests:host_unittest", "common_components/thread/tests:host_unittest", "common_components/log/tests:host_unittest", ] diff --git a/common_components/objects/tests/base_string_test.cpp b/common_components/objects/tests/base_string_test.cpp index ae004c97c0..fec5afd133 100755 --- a/common_components/objects/tests/base_string_test.cpp +++ b/common_components/objects/tests/base_string_test.cpp @@ -106,4 +106,148 @@ HWTEST_F_L0(BaseStringTest, LastIndexOf_TEST4) EXPECT_EQ(BaseString::LastIndexOf(lhsSp, rhsSp, 3), -1); } + +HWTEST_F_L0(BaseStringTest, IsSubStringAtSpan_TEST3) +{ + const uint8_t lhs[] = {'a', 'b', 'c'}; + const uint16_t rhs[] = {'x', 'y'}; + Span lhsSp(lhs, 3); + Span rhsSp(rhs, 2); + ASSERT_FALSE(IsSubStringAtSpan(lhsSp, rhsSp, 1)); + + const uint8_t lhs1[] = {'a', 'b'}; + const uint16_t rhs1[] = {'b'}; + Span lhsSp1(lhs1, 2); + Span rhsSp1(rhs1, 1); + ASSERT_TRUE(IsSubStringAtSpan(lhsSp1, rhsSp1, 1)); +} + +HWTEST_F_L0(BaseStringTest, IsSubStringAtSpan_TEST4) +{ + const uint16_t lhs[] = {'a', 'b', 'c'}; + const uint8_t rhs[] = {'x', 'y'}; + Span lhsSp(lhs, 3); + Span rhsSp(rhs, 2); + ASSERT_FALSE(IsSubStringAtSpan(lhsSp, rhsSp, 1)); + + const uint16_t lhs1[] = {'a', 'b'}; + const uint8_t rhs1[] = {'b'}; + Span lhsSp1(lhs1, 2); + Span rhsSp1(rhs1, 1); + ASSERT_TRUE(IsSubStringAtSpan(lhsSp1, rhsSp1, 1)); +} + +HWTEST_F_L0(BaseStringTest, IsSubStringAtSpan_TEST5) +{ + const uint16_t lhs[] = {'a', 'b', 'c'}; + const uint16_t rhs[] = {'x', 'y'}; + Span lhsSp(lhs, 3); + Span rhsSp(rhs, 2); + ASSERT_FALSE(IsSubStringAtSpan(lhsSp, rhsSp, 1)); + + const uint16_t lhs1[] = {'a', 'b'}; + const uint16_t rhs1[] = {'b'}; + Span lhsSp1(lhs1, 2); + Span rhsSp1(rhs1, 1); + ASSERT_TRUE(IsSubStringAtSpan(lhsSp1, rhsSp1, 1)); +} + +HWTEST_F_L0(BaseStringTest, LastIndexOf_TEST5) +{ + const uint8_t lhs[] = {'a', 'b', 'c', 'd', 'e'}; + const uint16_t rhs[] = {'c', 'd'}; + Span lhsSp(lhs, 5); + Span rhsSp(rhs, 2); + EXPECT_EQ(BaseString::LastIndexOf(lhsSp, rhsSp, 4), 2); + + const uint8_t lhs1[] = {'a', 'b', 'c', 'x', 'e'}; + const uint16_t rhs1[] = {'c', 'd'}; + Span lhsSp1(lhs1, 5); + Span rhsSp1(rhs1, 2); + EXPECT_EQ(BaseString::LastIndexOf(lhsSp1, rhsSp1, 4), -1); + + const uint8_t lhs2[] = {'a', 'b', 'c', 'd', 'e'}; + const uint16_t rhs2[] = {'c', 'x'}; + Span lhsSp2(lhs2, 5); + Span rhsSp2(rhs2, 2); + EXPECT_EQ(BaseString::LastIndexOf(lhsSp2, rhsSp2, 4), -1); + + const uint8_t lhs3[] = {'a', 'b', 'a', 'b', 'c'}; + const uint16_t rhs3[] = {'a', 'b'}; + Span lhsSp3(lhs3, 5); + Span rhsSp3(rhs3, 2); + EXPECT_EQ(BaseString::LastIndexOf(lhsSp3, rhsSp3, 4), 2); + + const uint8_t lhs4[] = {'a', 'b', 'c', 'd'}; + const uint16_t rhs4[] = {'x', 'y'}; + Span lhsSp4(lhs4, 4); + Span rhsSp4(rhs4, 2); + EXPECT_EQ(BaseString::LastIndexOf(lhsSp4, rhsSp4, 3), -1); +} + +HWTEST_F_L0(BaseStringTest, LastIndexOf_TEST6) +{ + const uint16_t lhs[] = {'a', 'b', 'c', 'd', 'e'}; + const uint16_t rhs[] = {'c', 'd'}; + Span lhsSp(lhs, 5); + Span rhsSp(rhs, 2); + EXPECT_EQ(BaseString::LastIndexOf(lhsSp, rhsSp, 4), 2); + + const uint16_t lhs1[] = {'a', 'b', 'c', 'x', 'e'}; + const uint16_t rhs1[] = {'c', 'd'}; + Span lhsSp1(lhs1, 5); + Span rhsSp1(rhs1, 2); + EXPECT_EQ(BaseString::LastIndexOf(lhsSp1, rhsSp1, 4), -1); + + const uint16_t lhs2[] = {'a', 'b', 'c', 'd', 'e'}; + const uint16_t rhs2[] = {'c', 'x'}; + Span lhsSp2(lhs2, 5); + Span rhsSp2(rhs2, 2); + EXPECT_EQ(BaseString::LastIndexOf(lhsSp2, rhsSp2, 4), -1); + + const uint16_t lhs3[] = {'a', 'b', 'a', 'b', 'c'}; + const uint16_t rhs3[] = {'a', 'b'}; + Span lhsSp3(lhs3, 5); + Span rhsSp3(rhs3, 2); + EXPECT_EQ(BaseString::LastIndexOf(lhsSp3, rhsSp3, 4), 2); + + const uint16_t lhs4[] = {'a', 'b', 'c', 'd'}; + const uint16_t rhs4[] = {'x', 'y'}; + Span lhsSp4(lhs4, 4); + Span rhsSp4(rhs4, 2); + EXPECT_EQ(BaseString::LastIndexOf(lhsSp4, rhsSp4, 3), -1); +} + +HWTEST_F_L0(BaseStringTest, LastIndexOf_TEST7) +{ + const uint16_t lhs[] = {'a', 'b', 'c', 'd', 'e'}; + const uint8_t rhs[] = {'c', 'd'}; + Span lhsSp(lhs, 5); + Span rhsSp(rhs, 2); + EXPECT_EQ(BaseString::LastIndexOf(lhsSp, rhsSp, 4), 2); + + const uint16_t lhs1[] = {'a', 'b', 'c', 'x', 'e'}; + const uint8_t rhs1[] = {'c', 'd'}; + Span lhsSp1(lhs1, 5); + Span rhsSp1(rhs1, 2); + EXPECT_EQ(BaseString::LastIndexOf(lhsSp1, rhsSp1, 4), -1); + + const uint16_t lhs2[] = {'a', 'b', 'c', 'd', 'e'}; + const uint8_t rhs2[] = {'c', 'x'}; + Span lhsSp2(lhs2, 5); + Span rhsSp2(rhs2, 2); + EXPECT_EQ(BaseString::LastIndexOf(lhsSp2, rhsSp2, 4), -1); + + const uint16_t lhs3[] = {'a', 'b', 'a', 'b', 'c'}; + const uint8_t rhs3[] = {'a', 'b'}; + Span lhsSp3(lhs3, 5); + Span rhsSp3(rhs3, 2); + EXPECT_EQ(BaseString::LastIndexOf(lhsSp3, rhsSp3, 4), 2); + + const uint16_t lhs4[] = {'a', 'b', 'c', 'd'}; + const uint8_t rhs4[] = {'x', 'y'}; + Span lhsSp4(lhs4, 4); + Span rhsSp4(rhs4, 2); + EXPECT_EQ(BaseString::LastIndexOf(lhsSp4, rhsSp4, 3), -1); +} } // namespace common::test diff --git a/common_components/profiler/tests/BUILD.gn b/common_components/profiler/tests/BUILD.gn new file mode 100644 index 0000000000..0dc10d69e6 --- /dev/null +++ b/common_components/profiler/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("Common_Profiler_Interface_Test") { + module_out_path = module_output_path + + sources = [ + # test file + "common_profiler_interface_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 = [ + ":Common_Profiler_Interface_Test", + ] +} + +group("host_unittest") { + testonly = true + + # deps file + deps = [ + ":Common_Profiler_Interface_TestAction", + ] +} diff --git a/common_components/profiler/tests/common_profiler_interface_test.cpp b/common_components/profiler/tests/common_profiler_interface_test.cpp new file mode 100644 index 0000000000..6f85a0e981 --- /dev/null +++ b/common_components/profiler/tests/common_profiler_interface_test.cpp @@ -0,0 +1,96 @@ +/* +* 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/profiler/common_profiler_interface.h" +#include "common_components/profiler/heap_profiler_listener.cpp" +#include "common_components/tests/test_helper.h" + +using namespace common; + +namespace common::test { +class MockCommonProfiler : public CommonHeapProfilerInterface { +public: + bool isSingleInstance = false; + void DumpHeapSnapshotForCMCOOM() override + { + isSingleInstance = true; + } +}; + +class CommonProfilerInterfaceTest : public common::test::BaseTestWithScope { +}; + +HWTEST_F_L0(CommonProfilerInterfaceTest, DumpHeapSnapshotBeforeOOM) +{ + MockCommonProfiler *instance = new MockCommonProfiler(); + ASSERT_NE(instance, nullptr); + CommonHeapProfilerInterface::SetSingleInstance(nullptr); + CommonHeapProfilerInterface::DumpHeapSnapshotBeforeOOM(); + CommonHeapProfilerInterface::SetSingleInstance(instance); + CommonHeapProfilerInterface::DumpHeapSnapshotBeforeOOM(); + ASSERT_TRUE(instance->isSingleInstance); + delete instance; +} +} // namespace common::test + +namespace common::test { +class HeapProfilerListenerTest : public common::test::BaseTestWithScope { +protected: + void SetUp() override + { + listener_ = &HeapProfilerListener::GetInstance(); + moveEventCallbackCalled = false; + callbackId = 0; + } + + void TearDown() override + { + if (callbackId != 0) { + listener_->UnRegisterMoveEventCb(callbackId); + } + } + + HeapProfilerListener* listener_; + bool moveEventCallbackCalled; + uint32_t callbackId; +}; + +uintptr_t capturedFromObj = 0; +uintptr_t capturedToObj = 0; +size_t capturedSize = 0; + +HWTEST_F_L0(HeapProfilerListenerTest, OnMoveEvent) +{ + std::function emptyCallback; + callbackId = listener_->RegisterMoveEventCb(emptyCallback); + listener_->OnMoveEvent(0x1000, 0x2000, 64); + uintptr_t fromObj = 0x12345678; + uintptr_t toObj = 0x87654321; + size_t size = 1024; + auto callback = [this](uintptr_t from, uintptr_t to, size_t sz) { + moveEventCallbackCalled = true; + capturedFromObj = from; + capturedToObj = to; + capturedSize = sz; + }; + + callbackId = listener_->RegisterMoveEventCb(callback); + listener_->OnMoveEvent(fromObj, toObj, size); + EXPECT_TRUE(moveEventCallbackCalled); + EXPECT_EQ(capturedFromObj, fromObj); + EXPECT_EQ(capturedToObj, toObj); + EXPECT_EQ(capturedSize, size); +} +} //namespace common::test diff --git a/common_components/tests/ohos_test.xml b/common_components/tests/ohos_test.xml index a9332b249e..f51fec747e 100644 --- a/common_components/tests/ohos_test.xml +++ b/common_components/tests/ohos_test.xml @@ -38,6 +38,11 @@