• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 
16 #include "thermal_utils_test.h"
17 
18 #include "errors.h"
19 #include "file_operation.h"
20 #include "string_operation.h"
21 #include "thermal_hisysevent.h"
22 #include "thermal_log.h"
23 
24 using namespace testing::ext;
25 using namespace OHOS::PowerMgr;
26 using namespace OHOS;
27 using namespace std;
28 
29 namespace {
30 /**
31  * @tc.name: ThermalUtilsTest001
32  * @tc.desc: utils test
33  * @tc.type: FUNC
34  * @tc.require: issueI5YZQ2
35  */
36 HWTEST_F(ThermalUtilsTest, ThermalUtilsTest001, TestSize.Level0)
37 {
38     THERMAL_HILOGI(LABEL_TEST, "ThermalUtilsTest001 start.");
39     std::string dir = "";
40     char* buf = nullptr;
41     EXPECT_EQ(true, FileOperation::CreateNodeDir(dir) != ERR_OK);
42     EXPECT_EQ(true, FileOperation::CreateNodeFile(dir) != ERR_OK);
43     dir = "/data/thermaltest/";
44     EXPECT_EQ(true, FileOperation::CreateNodeDir(dir) == ERR_OK);
45     dir = "/data/thermaltest/thermaltest.txt";
46     EXPECT_EQ(true, FileOperation::CreateNodeFile(dir) == ERR_OK);
47     size_t size = 1;
48     EXPECT_EQ(true, FileOperation::WriteFile(dir, dir, size) == ERR_OK);
49     dir = "";
50     EXPECT_EQ(true, FileOperation::ReadFile(dir.c_str(), buf, size) != ERR_OK);
51     dir = "/data/thermaltest/thermaltest.txt";
52     EXPECT_EQ(false, FileOperation::ReadFile(dir.c_str(), buf, size) == ERR_OK);
53     dir = "";
54     EXPECT_EQ(true, FileOperation::WriteFile(dir, dir, size) != ERR_OK);
55     dir = "1";
56     EXPECT_EQ(1, FileOperation::ConvertInt(dir));
57     THERMAL_HILOGI(LABEL_TEST, "ThermalUtilsTest001 end.");
58 }
59 
60 /**
61  * @tc.name: ThermalUtilsTest002
62  * @tc.desc: utils test
63  * @tc.require: issueI5YZQ2
64  * @tc.type: FUNC
65  */
66 HWTEST_F(ThermalUtilsTest, ThermalUtilsTest002, TestSize.Level0)
67 {
68     THERMAL_HILOGI(LABEL_TEST, "ThermalUtilsTest002 start.");
69     std::string str;
70     std::vector<std::string> ret;
71     std::string sep;
72     StringOperation::SplitString(str, ret, sep);
73     StringOperation::ClearAllSpace(str);
74     str = "str";
75     uint32_t val = 1;
76     double value = 2;
77     StringOperation::StrToUint("", val);
78     StringOperation::StrToDouble("", value);
79     StringOperation::ClearAllSpace(str);
80     EXPECT_EQ(false, StringOperation::Compare(str, sep));
81     sep = "str";
82     EXPECT_EQ(true, StringOperation::Compare(str, sep));
83     EXPECT_EQ(true, StringOperation::Find(str, sep));
84     str = "";
85     EXPECT_EQ(false, StringOperation::Find(str, sep));
86     std::string configDir = "/data/service/el0/thermal/config/%s";
87     WriteLevelChangedHiSysEvent(true, 1);
88     WriteLevelChangedHiSysEvent(false, 1);
89     WriteActionTriggeredHiSysEvent(true, configDir, 1);
90     WriteActionTriggeredHiSysEvent(false, configDir, 1);
91     WriteActionTriggeredHiSysEventWithRatio(true, configDir, 1);
92     WriteActionTriggeredHiSysEventWithRatio(false, configDir, 1);
93     THERMAL_HILOGI(LABEL_TEST, "ThermalUtilsTest002 end.");
94 }
95 
96 /**
97  * @tc.name: ThermalUtilsTest003
98  * @tc.desc: utils test ParseStrtollResult
99  * @tc.require: issueI5YZQ2
100  * @tc.type: FUNC
101  */
102 HWTEST_F(ThermalUtilsTest, ThermalUtilsTest003, TestSize.Level0)
103 {
104     THERMAL_HILOGI(LABEL_TEST, "ThermalUtilsTest003 function start!");
105     int64_t result = 0;
106     std::string strfirst = "";
107     EXPECT_EQ(false, StringOperation::ParseStrtollResult(strfirst, result));
108     result = 0;
109     std::string strsecond = "abc";
110     EXPECT_EQ(false, StringOperation::ParseStrtollResult(strsecond, result));
111     result = 0;
112     std::string strthird = "abc123";
113     EXPECT_EQ(false, StringOperation::ParseStrtollResult(strthird, result));
114     result = 0;
115     std::string strfourth = "123abc";
116     EXPECT_EQ(true, StringOperation::ParseStrtollResult(strfourth, result));
117     result = 0;
118     std::string strfifth = "123";
119     EXPECT_EQ(true, StringOperation::ParseStrtollResult(strfifth, result));
120     result = 0;
121     std::string strsixth = "12345678999987654321";
122     EXPECT_EQ(false, StringOperation::ParseStrtollResult(strsixth, result));
123     THERMAL_HILOGI(LABEL_TEST, "ThermalUtilsTest003 function end!");
124 }
125 
126 /**
127  * @tc.name: ThermalUtilsTest004
128  * @tc.desc: utils test ParseStrtoulResult
129  * @tc.require: issueI5YZQ2
130  * @tc.type: FUNC
131  */
132 HWTEST_F(ThermalUtilsTest, ThermalUtilsTest004, TestSize.Level0)
133 {
134     THERMAL_HILOGI(LABEL_TEST, "ThermalUtilsTest004 function start!");
135     unsigned long result = 0;
136     std::string strfirst = "";
137     EXPECT_EQ(false, StringOperation::ParseStrtoulResult(strfirst, result));
138     result = 0;
139     std::string strsecond = "abc";
140     EXPECT_EQ(false, StringOperation::ParseStrtoulResult(strsecond, result));
141     result = 0;
142     std::string strthird = "abc123";
143     EXPECT_EQ(false, StringOperation::ParseStrtoulResult(strthird, result));
144     result = 0;
145     std::string strfourth = "123abc";
146     EXPECT_EQ(true, StringOperation::ParseStrtoulResult(strfourth, result));
147     result = 0;
148     std::string strfifth = "123";
149     EXPECT_EQ(true, StringOperation::ParseStrtoulResult(strfifth, result));
150     result = 0;
151     std::string strsixth = "123456789999987654321";
152     EXPECT_EQ(false, StringOperation::ParseStrtoulResult(strsixth, result));
153     THERMAL_HILOGI(LABEL_TEST, "ThermalUtilsTest004 function end!");
154 }
155 } // namespace
156