From 227ccb65ab74353cc978c70f36b7e0a9466a001f Mon Sep 17 00:00:00 2001 From: hellohyh001 Date: Tue, 16 Apr 2024 08:57:29 +0000 Subject: [PATCH] add test Signed-off-by: hellohyh001 --- BUILD.gn | 3 +- test/unittest/BUILD.gn | 21 ++ test/unittest/utils/BUILD.gn | 53 +++++ test/unittest/utils/src/utility_test.cpp | 241 +++++++++++++++++++++++ 4 files changed, 317 insertions(+), 1 deletion(-) create mode 100644 test/unittest/BUILD.gn create mode 100644 test/unittest/utils/BUILD.gn create mode 100644 test/unittest/utils/src/utility_test.cpp diff --git a/BUILD.gn b/BUILD.gn index b18b34670..6bf2bab45 100755 --- a/BUILD.gn +++ b/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2022-2023 Huawei Device Co., Ltd. +# Copyright (c) 2022-2024 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 @@ -40,6 +40,7 @@ group("device_status_tests") { "intention/common/channel:intention_channel_test", "services/native/test:devicestatussrv_test", "test/fuzztest:device_status_fuzztest", + "test/unittest:device_status_unittest", ] if (fusion_rust_enabled) { diff --git a/test/unittest/BUILD.gn b/test/unittest/BUILD.gn new file mode 100644 index 000000000..4c1219597 --- /dev/null +++ b/test/unittest/BUILD.gn @@ -0,0 +1,21 @@ +# Copyright (c) 2024 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("../../device_status.gni") + +group("device_status_unittest") { + testonly = true + deps = [] + + deps += [ "utils:UtilityTest" ] +} diff --git a/test/unittest/utils/BUILD.gn b/test/unittest/utils/BUILD.gn new file mode 100644 index 000000000..0ec13c3f8 --- /dev/null +++ b/test/unittest/utils/BUILD.gn @@ -0,0 +1,53 @@ +# Copyright (c) 2024 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("//build/test.gni") +import("../../../device_status.gni") + +module_output_path = "${device_status_part_name}/unit_out" + +ohos_unittest("UtilityTest") { + sanitize = { + cfi = true + cfi_cross_dso = true + debug = false + } + + branch_protector_ret = "pac_ret" + + module_out_path = module_output_path + include_dirs = [ + "${device_status_interfaces_path}/innerkits/interaction/include", + "${device_status_utils_path}/include", + ] + + defines = [] + + sources = [ "src/utility_test.cpp" ] + + configs = [] + + deps = [ "${device_status_utils_path}:devicestatus_util" ] + external_deps = [ + "c_utils:utils", + "hilog:libhilog", + ] +} + +group("unittest") { + testonly = true + deps = [] + if (build_variant == "root" && root_perf_main != "root_main") { + deps += [ ":UtilityTest" ] + } +} diff --git a/test/unittest/utils/src/utility_test.cpp b/test/unittest/utils/src/utility_test.cpp new file mode 100644 index 000000000..c0efcda6f --- /dev/null +++ b/test/unittest/utils/src/utility_test.cpp @@ -0,0 +1,241 @@ +/* + * Copyright (c) 2024 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 +#include +#include +#include +#include + +#include + +#include "devicestatus_define.h" +#include "devicestatus_errors.h" +#include "utility.h" + +#undef LOG_TAG +#define LOG_TAG "UtilityTest" + +namespace OHOS { +namespace Msdp { +namespace DeviceStatus { +using namespace testing::ext; +namespace { +constexpr int32_t TIME_WAIT_FOR_OP_MS { 20 }; +} // namespace + +class UtilityTest : public testing::Test { +public: + void SetUp(); + void TearDown(); + static void SetUpTestCase(); + static void TearDownTestCase(void); +}; + +void UtilityTest::SetUpTestCase() {} + +void UtilityTest::TearDownTestCase() {} + +void UtilityTest::SetUp() {} + +void UtilityTest::TearDown() +{ + std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP_MS)); +} + +/** + * @tc.name: UtityTest_IsInteger_001 + * @tc.desc: Checks whether a string is an integer. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(UtilityTest, UtityTest_IsInteger_001, TestSize.Level1) +{ + CALL_TEST_DEBUG; + std::string target = "0"; + bool ret = Utility::IsInteger(target); + ASSERT_TRUE(ret); +} + +/** + * @tc.name: UtityTest_IsInteger_002 + * @tc.desc: Checks whether a string is an integer. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(UtilityTest, UtityTest_IsInteger_002, TestSize.Level1) +{ + CALL_TEST_DEBUG; + std::string target = "123"; + bool ret = Utility::IsInteger(target); + ASSERT_TRUE(ret); +} + +/** + * @tc.name: UtityTest_IsInteger_003 + * @tc.desc: Checks whether a string is an integer. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(UtilityTest, UtityTest_IsInteger_003, TestSize.Level1) +{ + CALL_TEST_DEBUG; + std::string target = " 0"; + bool ret = Utility::IsInteger(target); + ASSERT_TRUE(ret); +} + +/** + * @tc.name: UtityTest_IsInteger_004 + * @tc.desc: Checks whether a string is an integer. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(UtilityTest, UtityTest_IsInteger_004, TestSize.Level1) +{ + CALL_TEST_DEBUG; + std::string target = "-0"; + bool ret = Utility::IsInteger(target); + ASSERT_TRUE(ret); +} + +/** + * @tc.name: UtityTest_IsInteger_005 + * @tc.desc: Checks whether a string is an integer. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(UtilityTest, UtityTest_IsInteger_005, TestSize.Level1) +{ + CALL_TEST_DEBUG; + std::string target = "-1"; + bool ret = Utility::IsInteger(target); + ASSERT_TRUE(ret); +} + +/** + * @tc.name: UtityTest_IsInteger_006 + * @tc.desc: Checks whether a string is an integer. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(UtilityTest, UtityTest_IsInteger_006, TestSize.Level1) +{ + CALL_TEST_DEBUG; + std::string target = "-10"; + bool ret = Utility::IsInteger(target); + ASSERT_TRUE(ret); +} + +/** + * @tc.name: UtityTest_IsInteger_007 + * @tc.desc: Checks whether a string is an integer. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(UtilityTest, UtityTest_IsInteger_007, TestSize.Level1) +{ + CALL_TEST_DEBUG; + std::string target = "123"; + bool ret = Utility::IsInteger(target); + ASSERT_TRUE(ret); +} + +/** + * @tc.name: UtityTest_IsInteger_008 + * @tc.desc: Checks whether a string is an integer. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(UtilityTest, UtityTest_IsInteger_008, TestSize.Level1) +{ + CALL_TEST_DEBUG; + std::string target = "-123"; + bool ret = Utility::IsInteger(target); + ASSERT_TRUE(ret); +} + +/** + * @tc.name: UtityTest_IsInteger_009 + * @tc.desc: Checks whether a string is an integer. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(UtilityTest, UtityTest_IsInteger_009, TestSize.Level1) +{ + CALL_TEST_DEBUG; + std::string target = "0123"; + bool ret = Utility::IsInteger(target); + ASSERT_FALSE(ret); +} + +/** + * @tc.name: UtityTest_IsInteger_010 + * @tc.desc: Checks whether a string is an integer. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(UtilityTest, UtityTest_IsInteger_010, TestSize.Level1) +{ + CALL_TEST_DEBUG; + std::string target = "A01"; + bool ret = Utility::IsInteger(target); + ASSERT_FALSE(ret); +} + +/** + * @tc.name: UtityTest_IsInteger_011 + * @tc.desc: Checks whether a string is an integer. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(UtilityTest, UtityTest_IsInteger_011, TestSize.Level1) +{ + CALL_TEST_DEBUG; + std::string target = "A-10"; + bool ret = Utility::IsInteger(target); + ASSERT_FALSE(ret); +} + +/** + * @tc.name: UtityTest_IsInteger_012 + * @tc.desc: Checks whether a string is an integer. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(UtilityTest, UtityTest_IsInteger_012, TestSize.Level1) +{ + CALL_TEST_DEBUG; + std::string target = " 123A"; + bool ret = Utility::IsInteger(target); + ASSERT_FALSE(ret); +} + +/** + * @tc.name: UtityTest_IsInteger_013 + * @tc.desc: Checks whether a string is an integer. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(UtilityTest, UtityTest_IsInteger_013, TestSize.Level1) +{ + CALL_TEST_DEBUG; + std::string target = " 123 A"; + bool ret = Utility::IsInteger(target); + ASSERT_FALSE(ret); +} +} // namespace DeviceStatus +} // namespace Msdp +} // namespace OHOS -- Gitee