1 /* 2 * Copyright (c) 2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 #include <gtest/gtest.h> 16 17 #include "common_util.h" 18 19 using namespace testing::ext; 20 using namespace OHOS::HiviewDFX; 21 using namespace OHOS::HiviewDFX::UCollectUtil; 22 23 class CommonUtilTest : public testing::Test { 24 public: SetUp()25 void SetUp() {}; TearDown()26 void TearDown() {}; SetUpTestCase()27 static void SetUpTestCase() {}; TearDownTestCase()28 static void TearDownTestCase() {}; 29 }; 30 31 /** 32 * @tc.name: CommonUtilTest001 33 * @tc.desc: used to test common util in UCollect 34 * @tc.type: FUNC 35 */ 36 HWTEST_F(CommonUtilTest, CommonUtilTest001, TestSize.Level1) 37 { 38 std::string testStr; 39 std::string type; 40 int64_t value = 0; 41 ASSERT_EQ(CommonUtil::ParseTypeAndValue(testStr, type, value), false); 42 testStr = "key: 1"; 43 ASSERT_EQ(CommonUtil::ParseTypeAndValue(testStr, type, value), true); 44 ASSERT_EQ(type, "key"); 45 ASSERT_EQ(value, 1); 46 testStr = "key: 1 kB"; 47 ASSERT_EQ(CommonUtil::ParseTypeAndValue(testStr, type, value), true); 48 ASSERT_EQ(type, "key"); 49 ASSERT_EQ(value, 1); 50 } 51 52 /** 53 * @tc.name: CommonUtilTest002 54 * @tc.desc: used to test common util in UCollect 55 * @tc.type: FUNC 56 */ 57 HWTEST_F(CommonUtilTest, CommonUtilTest002, TestSize.Level1) 58 { 59 int ret = CommonUtil::GetFileNameNum("fileName", ".txt"); 60 ASSERT_EQ(ret, 0); 61 ret = CommonUtil::GetFileNameNum("fileName_1", ".txt"); 62 ASSERT_EQ(ret, 0); 63 ret = CommonUtil::GetFileNameNum("fileName.txt_", ".txt"); 64 ASSERT_EQ(ret, 0); 65 ret = CommonUtil::GetFileNameNum("fileName_.txt", ".txt"); 66 ASSERT_EQ(ret, 0); 67 ret = CommonUtil::GetFileNameNum("fileName_1.txt", ".txt"); 68 ASSERT_EQ(ret, 1); 69 } 70 71 /** 72 * @tc.name: CommonUtilTest003 73 * @tc.desc: used to test common util in UCollect 74 * @tc.type: FUNC 75 */ 76 HWTEST_F(CommonUtilTest, CommonUtilTest003, TestSize.Level1) 77 { 78 int32_t ret = CommonUtil::ReadNodeWithOnlyNumber("invalid_path"); 79 ASSERT_EQ(ret, 0); 80 ret = CommonUtil::ReadNodeWithOnlyNumber("/proc/1/oom_score_adj"); 81 ASSERT_NE(ret, 0); 82 } 83