1 /*
2 * Copyright (c) 2024 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 "res_sched_string_util_test.h"
17
18 #include "res_sched_string_util.h"
19
20 using namespace std;
21 using namespace testing::ext;
22
23 namespace OHOS {
24 namespace ResourceSchedule {
25
26
SetUpTestCase()27 void ResSchedStringUtilTest::SetUpTestCase() {}
28
TearDownTestCase()29 void ResSchedStringUtilTest::TearDownTestCase() {}
30
SetUp()31 void ResSchedStringUtilTest::SetUp() {}
32
TearDown()33 void ResSchedStringUtilTest::TearDown() {}
34
35 /**
36 * @tc.name: ResSchedStringUtilTest StrToFloat_001
37 * @tc.desc: test StrToFloat
38 * @tc.type: FUNC
39 * @tc.require: issueIB8Y0E
40 */
41 HWTEST_F(ResSchedStringUtilTest, StrToFloat_001, Function | MediumTest | Level0)
42 {
43 float result = 0.0f;
44 float fMax = std::numeric_limits<float>::max();
45 long double fMin = std::numeric_limits<float>::min() / 2.0l;
46 EXPECT_TRUE(ResCommonUtil::StrToFloat(std::to_string(fMax), result));
47 EXPECT_EQ(result, fMax);
48 result = 0.0f;
49 EXPECT_FALSE(ResCommonUtil::StrToFloat(std::to_string(fMin), result));
50 EXPECT_EQ(result, 0.0f);
51 result = 0.0f;
52 EXPECT_FALSE(ResCommonUtil::StrToFloat("1" + std::to_string(fMax), result));
53 EXPECT_EQ(result, 0.0f);
54 result = 0.0f;
55 EXPECT_FALSE(ResCommonUtil::StrToFloat("abc", result));
56 EXPECT_EQ(result, 0.0f);
57 result = 0.0f;
58 EXPECT_FALSE(ResCommonUtil::StrToFloat("1123abc", result));
59 EXPECT_EQ(result, 0.0f);
60 }
61
62
63 /**
64 * @tc.name: ResSchedStringUtilTest StrToInt32_001
65 * @tc.desc: test StrToInt32
66 * @tc.type: FUNC
67 * @tc.require: issueIB8Y0E
68 */
69 HWTEST_F(ResSchedStringUtilTest, StrToInt32_001, Function | MediumTest | Level0)
70 {
71 int32_t result = -1;
72 EXPECT_TRUE(ResCommonUtil::StrToInt32(std::to_string(INT32_MAX), result));
73 EXPECT_EQ(result, INT32_MAX);
74 result = -1;
75 EXPECT_TRUE(ResCommonUtil::StrToInt32(std::to_string(INT32_MIN), result));
76 EXPECT_EQ(result, INT32_MIN);
77 result = -1;
78 EXPECT_FALSE(ResCommonUtil::StrToInt32(std::to_string(LONG_MAX) + "1", result));
79 EXPECT_EQ(result, -1);
80 result = -1;
81 EXPECT_FALSE(ResCommonUtil::StrToInt32("abc", result));
82 EXPECT_EQ(result, -1);
83 result = -1;
84 EXPECT_FALSE(ResCommonUtil::StrToInt32("1123abc", result));
85 EXPECT_EQ(result, -1);
86 }
87
88 /**
89 * @tc.name: ResSchedStringUtilTest StrToInt64_001
90 * @tc.desc: test StrToInt64
91 * @tc.type: FUNC
92 * @tc.require: issueIB8Y0E
93 */
94 HWTEST_F(ResSchedStringUtilTest, StrToInt64_001, Function | MediumTest | Level0)
95 {
96 int64_t result = -1;
97 EXPECT_TRUE(ResCommonUtil::StrToInt64(std::to_string(INT64_MAX), result));
98 EXPECT_EQ(result, INT64_MAX);
99 result = -1;
100 EXPECT_TRUE(ResCommonUtil::StrToInt64(std::to_string(INT64_MIN), result));
101 EXPECT_EQ(result, INT64_MIN);
102 result = -1;
103 EXPECT_FALSE(ResCommonUtil::StrToInt64(std::to_string(INT64_MAX) + "1", result));
104 EXPECT_EQ(result, -1);
105 result = -1;
106 EXPECT_FALSE(ResCommonUtil::StrToInt64("abc", result));
107 EXPECT_EQ(result, -1);
108 result = -1;
109 EXPECT_FALSE(ResCommonUtil::StrToInt64("1123abc", result));
110 EXPECT_EQ(result, -1);
111 }
112
113 /**
114 * @tc.name: ResSchedStringUtilTest CheckBundleName_001
115 * @tc.desc: test CheckBundleName
116 * @tc.type: FUNC
117 * @tc.require: issueIB8Y0E
118 */
119 HWTEST_F(ResSchedStringUtilTest, CheckBundleName_001, Function | MediumTest | Level0)
120 {
121 EXPECT_FALSE(ResCommonUtil::CheckBundleName(""));
122 EXPECT_FALSE(ResCommonUtil::CheckBundleName("a"));
123 EXPECT_FALSE(ResCommonUtil::CheckBundleName("aaaaaa"));
124 std::string maxBundleName = "";
125 for (int i = 0;i <= 127; ++i) {
126 maxBundleName.append("a");
127 }
128 EXPECT_FALSE(ResCommonUtil::CheckBundleName(maxBundleName));
129 EXPECT_FALSE(ResCommonUtil::CheckBundleName("11111111.1111"));
130 EXPECT_FALSE(ResCommonUtil::CheckBundleName("com.test.**.**"));
131 EXPECT_FALSE(ResCommonUtil::CheckBundleName("com.test.123_!@#"));
132 EXPECT_TRUE(ResCommonUtil::CheckBundleName("com.test"));
133 EXPECT_TRUE(ResCommonUtil::CheckBundleName("com.test_"));
134 }
135
136 /**
137 * @tc.name: ResSchedStringUtilTest StrToUInt32_001
138 * @tc.desc: test StrToUInt32
139 * @tc.type: FUNC
140 * @tc.require: issueIB8Y0E
141 */
142 HWTEST_F(ResSchedStringUtilTest, StrToUInt32_001, Function | MediumTest | Level0)
143 {
144 uint32_t result = 0;
145 EXPECT_TRUE(ResCommonUtil::StrToUInt32(std::to_string(UINT_MAX), result));
146 EXPECT_EQ(result, UINT_MAX);
147 result = 0;
148 EXPECT_FALSE(ResCommonUtil::StrToUInt32("abc", result));
149 EXPECT_EQ(result, 0);
150 result = 0;
151 EXPECT_FALSE(ResCommonUtil::StrToUInt32("1123abc", result));
152 EXPECT_EQ(result, 0);
153 }
154
155 /**
156 * @tc.name: ResSchedStringUtilTest IsNumericString_001
157 * @tc.desc: test IsNumericString
158 * @tc.type: FUNC
159 * @tc.require: issueIB8Y0E
160 */
161 HWTEST_F(ResSchedStringUtilTest, IsNumericString_001, Function | MediumTest | Level0)
162 {
163 EXPECT_FALSE(ResCommonUtil::IsNumericString("11111111111"));
164 EXPECT_FALSE(ResCommonUtil::IsNumericString(""));
165 EXPECT_FALSE(ResCommonUtil::IsNumericString("-"));
166 EXPECT_FALSE(ResCommonUtil::IsNumericString("$"));
167 EXPECT_FALSE(ResCommonUtil::IsNumericString("-111-1"));
168 EXPECT_FALSE(ResCommonUtil::IsNumericString("-**123"));
169 EXPECT_TRUE(ResCommonUtil::IsNumericString("-1"));
170 EXPECT_TRUE(ResCommonUtil::IsNumericString("1"));
171 EXPECT_TRUE(ResCommonUtil::IsNumericString("-123"));
172 EXPECT_TRUE(ResCommonUtil::IsNumericString("0000012500"));
173 }
174
175 /**
176 * @tc.name: ResSchedStringUtilTest StringTrim_001
177 * @tc.desc: test StringTrim
178 * @tc.type: FUNC
179 * @tc.require: issueIB8Y0E
180 */
181 HWTEST_F(ResSchedStringUtilTest, StringTrim_001, Function | MediumTest | Level0)
182 {
183 EXPECT_EQ(ResCommonUtil::StringTrim(" test "), "test");
184 EXPECT_EQ(ResCommonUtil::StringTrim("test "), "test");
185 EXPECT_EQ(ResCommonUtil::StringTrim(" test"), "test");
186 EXPECT_EQ(ResCommonUtil::StringTrim("\f\vtest "), "test");
187 EXPECT_EQ(ResCommonUtil::StringTrim("test\f\v"), "test");
188 EXPECT_EQ(ResCommonUtil::StringTrim("\f\v\r\t\n test"), "test");
189 EXPECT_EQ(ResCommonUtil::StringTrim("t est "), "t est");
190 EXPECT_EQ(ResCommonUtil::StringTrim(" t\fest "), "t\fest");
191 EXPECT_EQ(ResCommonUtil::StringTrim("\f\v\r\t\n test \n\t\r\v\f"), "test");
192 }
193
194 /**
195 * @tc.name: ResSchedStringUtilTest StringTrimSpace_001
196 * @tc.desc: test StringTrimSpace
197 * @tc.type: FUNC
198 * @tc.require: issueIB8Y0E
199 */
200 HWTEST_F(ResSchedStringUtilTest, StringTrimSpace_001, Function | MediumTest | Level0)
201 {
202 EXPECT_EQ(ResCommonUtil::StringTrimSpace(" test "), "test");
203 EXPECT_EQ(ResCommonUtil::StringTrimSpace(" test"), "test");
204 EXPECT_EQ(ResCommonUtil::StringTrimSpace("test "), "test");
205 EXPECT_EQ(ResCommonUtil::StringTrimSpace(" t est "), "t est");
206 }
207
208 /**
209 * @tc.name: ResSchedStringUtilTest StringToJson_001
210 * @tc.desc: test StringToJson
211 * @tc.type: FUNC
212 * @tc.require: issueIB8Y0E
213 */
214 HWTEST_F(ResSchedStringUtilTest, StringToJson_001, Function | MediumTest | Level0)
215 {
216 nlohmann::json testJson;
217 EXPECT_FALSE(ResCommonUtil::StringToJson("", testJson));
218 EXPECT_FALSE(ResCommonUtil::StringToJson("test{test}test", testJson));
219 EXPECT_TRUE(ResCommonUtil::StringToJson("{ \"testKey1\": \"test1\", \"testKey2\": \"test2\" }",
220 testJson));
221 }
222 } // namespace ResourceSchedule
223 } // namespace OHOS
224