1 /*
2 * Copyright (c) 2021 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 <gtest/gtest.h>
17 #include "string_ex.h"
18 #include <iostream>
19 using namespace testing::ext;
20 using namespace OHOS;
21 using namespace std;
22
23 class UtilsStringTest : public testing::Test
24 {
25 public :
26 static void SetUpTestCase(void);
27 static void TearDownTestCase(void);
28 void SetUp();
29 void TearDown();
30 };
31
SetUpTestCase(void)32 void UtilsStringTest::SetUpTestCase(void)
33 {
34 }
35
TearDownTestCase(void)36 void UtilsStringTest::TearDownTestCase(void)
37 {
38 }
39
SetUp(void)40 void UtilsStringTest::SetUp(void)
41 {
42 }
43
TearDown(void)44 void UtilsStringTest::TearDown(void)
45 {
46 }
47
48 /*
49 * Feature: string_ex
50 * Function: UpperStr
51 * SubFunction: NA
52 * FunctionPoints:
53 * EnvConditions: NA
54 * CaseDescription: test for convert all letters of str to uppercase
55 */
56 HWTEST_F(UtilsStringTest, test_strupper_01, TestSize.Level0)
57 {
58 string strBase = "strbase";
59 string strTemp = "STRBASE";
60 string result = UpperStr(strBase);
61 EXPECT_EQ(result, strTemp);
62
63 strBase = "StrBase";
64 result = UpperStr(strBase);
65 EXPECT_EQ(result, strTemp);
66 }
67
68 HWTEST_F(UtilsStringTest, test_strupper_02, TestSize.Level0)
69 {
70 string strBase = "";
71 string strTemp = "";
72 string result = UpperStr(strBase);
73 EXPECT_EQ(result, strTemp);
74 }
75
76 /*
77 * Feature: string_ex
78 * Function: LowerStr
79 * SubFunction: NA
80 * FunctionPoints:
81 * EnvConditions: NA
82 * CaseDescription: test for convert all letters of str to lowercase
83 */
84 HWTEST_F(UtilsStringTest, test_strlower_01, TestSize.Level0)
85 {
86 string strBase = "STRbase";
87 string strTemp = "strbase";
88 string result = LowerStr(strBase);
89 EXPECT_EQ(result, strTemp);
90
91 strBase = "StrBase";
92 result = LowerStr(strBase);
93 EXPECT_EQ(result, strTemp);
94 }
95
96 HWTEST_F(UtilsStringTest, test_strlower_02, TestSize.Level0)
97 {
98 string strBase = "";
99 string strTemp = "";
100 string result = LowerStr(strBase);
101 EXPECT_EQ(result, strTemp);
102 }
103
104 /*
105 * Feature: string_ex
106 * Function: ReplaceStr
107 * SubFunction: NA
108 * FunctionPoints:
109 * EnvConditions: NA
110 * CaseDescription: test for replace src with dst int strBase
111 */
112 HWTEST_F(UtilsStringTest, test_strreplace_01, TestSize.Level0)
113 {
114 string strBase = "test for replace";
115 string src = "for";
116 string dst = "with";
117 string strTemp = "test with replace";
118 string result = ReplaceStr(strBase, src, dst);
119 EXPECT_EQ(result, strTemp);
120
121 src = "test for replace";
122 dst = "test";
123 strTemp = "test";
124 result = ReplaceStr(strBase, src, dst);
125 EXPECT_EQ(result, strTemp);
126
127 src = "";
128 dst = "test";
129 result = ReplaceStr(strBase, src, dst);
130 EXPECT_EQ(result, strBase);
131
132 src = "for";
133 dst = "";
134 strTemp = "test replace";
135 result = ReplaceStr(strBase, src, dst);
136 EXPECT_EQ(result, strTemp);
137 }
138
139 /*
140 * Feature: string_ex
141 * Function: TrimStr
142 * SubFunction: NA
143 * FunctionPoints:
144 * EnvConditions: NA
145 * CaseDescription: test for trim str front and end
146 */
147 HWTEST_F(UtilsStringTest, test_strtrim_01, TestSize.Level0)
148 {
149 string strBase = " test for trim ";
150 string strTemp = "test for trim";
151 string result = TrimStr(strBase);
152 EXPECT_EQ(result, strTemp);
153 }
154
155 HWTEST_F(UtilsStringTest, test_strtrim_02, TestSize.Level0)
156 {
157 string strBase = "test";
158 string strTemp = "es";
159 string result = TrimStr(strBase, 't');
160 EXPECT_EQ(result, strTemp);
161 }
162
163 /*
164 * Feature: string_ex
165 * Function: SplitStr
166 * SubFunction: NA
167 * FunctionPoints:
168 * EnvConditions: NA
169 * CaseDescription: test for split str by strSep
170 */
171 HWTEST_F(UtilsStringTest, test_strsplit_01, TestSize.Level0)
172 {
173 string strBase = "test for split";
174 string strSep = " ";
175 string splitResult[3] = { "test", "for", "split" };
176 vector<string> strsRet;
177 SplitStr(strBase, strSep, strsRet);
178
179 for (int i = 0; i < 3; i++)
180 {
181 EXPECT_EQ(splitResult[i], strsRet[i]);
182 }
183 }
184
185 HWTEST_F(UtilsStringTest, test_strsplit_02, TestSize.Level0)
186 {
187 string strBase = "test for split";
188 string strSep = "for";
189 string splitResult[2] = { "test", "split" };
190 vector<string> strsRet;
191 SplitStr(strBase, strSep, strsRet);
192
193 for (int i = 0; i < 2; i++)
194 {
195 EXPECT_EQ(splitResult[i], strsRet[i]);
196 }
197
198 splitResult[0] = "test ";
199 splitResult[1] = " split";
200 SplitStr(strBase, strSep, strsRet, false, false);
201 for (int i = 0; i < 2; i++)
202 {
203 EXPECT_EQ(splitResult[i], strsRet[i]);
204 }
205 }
206
207 HWTEST_F(UtilsStringTest, test_strsplit_03, TestSize.Level0)
208 {
209 string strBase = "test for for split";
210 string strSep = "for";
211 string splitResult[3] = { "test", "", "split" };
212 vector<string> strsRet;
213 SplitStr(strBase, strSep, strsRet, true);
214 for (int i = 0; i < (int)strsRet.size(); i++)
215 {
216 EXPECT_EQ(splitResult[i], strsRet[i]);
217 }
218 }
219
220
221 /*
222 * Feature: string_ex
223 * Function: IsNumericStr
224 * SubFunction: NA
225 * FunctionPoints:
226 * EnvConditions: NA
227 * CaseDescription: test for judge all characters of the string are numbers
228 */
229 HWTEST_F(UtilsStringTest, test_strisnumeric_01, TestSize.Level0)
230 {
231 string strBase = "1234556";
232 bool result = IsNumericStr(strBase);
233 EXPECT_EQ(result, true);
234
235 strBase = "1234,a";
236 result = IsNumericStr(strBase);
237 EXPECT_EQ(result, false);
238
239 strBase = "";
240 result = IsNumericStr(strBase);
241 EXPECT_EQ(result, false);
242 }
243
244 /*
245 * Feature: string_ex
246 * Function: IsAlphaStr
247 * SubFunction: NA
248 * FunctionPoints:
249 * EnvConditions: NA
250 * CaseDescription: test for judge all characters of the string are alphabet
251 */
252 HWTEST_F(UtilsStringTest, test_strisalpha_01, TestSize.Level0)
253 {
254 string strBase = "1234556";
255 bool result = IsAlphaStr(strBase);
256 EXPECT_EQ(result, false);
257
258 strBase = "Acedafe";
259 result = IsAlphaStr(strBase);
260 EXPECT_EQ(result, true);
261
262 strBase = "Acedafe ";
263 result = IsAlphaStr(strBase);
264 EXPECT_EQ(result, false);
265
266 strBase = "Acedafe3";
267 result = IsAlphaStr(strBase);
268 EXPECT_EQ(result, false);
269
270 strBase = "";
271 result = IsAlphaStr(strBase);
272 EXPECT_EQ(result, false);
273 }
274
275 /*
276 * Feature: string_ex
277 * Function: IsUpperStr
278 * SubFunction: NA
279 * FunctionPoints:
280 * EnvConditions: NA
281 * CaseDescription: test for judge all characters of the string are uppercase
282 */
283 HWTEST_F(UtilsStringTest, test_IsUpperStr_01, TestSize.Level0)
284 {
285 string strBase = "ABSEFAD";
286 bool result = IsUpperStr(strBase);
287 EXPECT_EQ(result, true);
288
289 strBase = "Afaefadf";
290 result = IsUpperStr(strBase);
291 EXPECT_EQ(result, false);
292
293 strBase = "12e13eaefd ";
294 result = IsUpperStr(strBase);
295 EXPECT_EQ(result, false);
296
297 strBase = "";
298 result = IsUpperStr(strBase);
299 EXPECT_EQ(result, false);
300 }
301
302 /*
303 * Feature: string_ex
304 * Function: IsLowerStr
305 * SubFunction: NA
306 * FunctionPoints:
307 * EnvConditions: NA
308 * CaseDescription: test for judge all characters of the string are lowercase
309 */
310 HWTEST_F(UtilsStringTest, test_IsLowerStr_01, TestSize.Level0)
311 {
312 string strBase = "testlower";
313 bool result = IsLowerStr(strBase);
314 EXPECT_EQ(result, true);
315
316 strBase = "AAFDeadfkl";
317 result = IsLowerStr(strBase);
318 EXPECT_EQ(result, false);
319
320 strBase = "12e";
321 result = IsLowerStr(strBase);
322 EXPECT_EQ(result, false);
323
324 strBase = "";
325 result = IsLowerStr(strBase);
326 EXPECT_EQ(result, false);
327 }
328
329 /*
330 * Feature: string_ex
331 * Function: IsSubStr
332 * SubFunction: NA
333 * FunctionPoints:
334 * EnvConditions: NA
335 * CaseDescription: test for judge the sub_str in base_str
336 */
337 HWTEST_F(UtilsStringTest, test_IsSubStr_01, TestSize.Level0)
338 {
339 string strBase = "test for issubstr";
340 string strSub = "for";
341 bool result = IsSubStr(strBase, strSub);
342 EXPECT_EQ(result, true);
343
344 strSub = "fori";
345 result = IsSubStr(strBase, strSub);
346 EXPECT_EQ(result, false);
347 }
348
349 /*
350 * Feature: string_ex
351 * Function: IsSameTextStr
352 * SubFunction: NA
353 * FunctionPoints:
354 * EnvConditions: NA
355 * CaseDescription: test for judge the strFirst's letter is same with strSecond
356 */
357 HWTEST_F(UtilsStringTest, test_IsSameTextStr_01, TestSize.Level0)
358 {
359 string strFirst = "Test For StrSameText";
360 string strSecond = "test for strsametext";
361 bool result = IsSameTextStr(strFirst, strSecond);
362 EXPECT_EQ(result, true);
363
364 strSecond = "test for strsametex";
365 result = IsSameTextStr(strFirst, strSecond);
366 EXPECT_EQ(result, false);
367 }
368
369 /*
370 * Feature: string_ex
371 * Function: ToString
372 * SubFunction: NA
373 * FunctionPoints:
374 * EnvConditions: NA
375 * CaseDescription: test for convert int to str
376 */
377 HWTEST_F(UtilsStringTest, test_ToString_01, TestSize.Level0)
378 {
379 int ivalue = 12345;
380 string strValue = "12345";
381 string result = ToString(ivalue);
382 EXPECT_EQ(result, strValue);
383
384 ivalue = -15;
385 result = ToString(ivalue);
386 EXPECT_EQ(result, "-15");
387 }
388
389 /*
390 * Feature: string_ex
391 * Function: StrToInt
392 * SubFunction: NA
393 * FunctionPoints:
394 * EnvConditions: NA
395 * CaseDescription: test for convert str to int
396 */
397 HWTEST_F(UtilsStringTest, test_StrToInt_01, TestSize.Level0)
398 {
399 string strValue = "12345";
400 int iValue = 0;
401 bool result = StrToInt(strValue, iValue);
402 EXPECT_EQ(result, true);
403 EXPECT_EQ(iValue, 12345);
404
405 strValue = "123r54";
406 result = StrToInt(strValue, iValue);
407 EXPECT_EQ(result, false);
408 }
409
410 HWTEST_F(UtilsStringTest, test_StrToInt_02, TestSize.Level0)
411 {
412 string strValue = "-12345";
413 int iValue = 0;
414 bool result = StrToInt(strValue, iValue);
415 EXPECT_EQ(result, true);
416 EXPECT_EQ(iValue, -12345);
417
418 strValue = "123= 54";
419 result = StrToInt(strValue, iValue);
420 EXPECT_EQ(result, false);
421
422 string strvalue2;
423 result = StrToInt(strvalue2, iValue);
424 EXPECT_EQ(result, false);
425 }
426
427 HWTEST_F(UtilsStringTest, test_StrToInt_03, TestSize.Level0)
428 {
429 string strValue = "2147483648";
430 int ivalue = 0;
431 bool result = StrToInt(strValue, ivalue);
432 EXPECT_EQ(result, false);
433 }
434
435 HWTEST_F(UtilsStringTest, test_StrToInt_04, TestSize.Level0)
436 {
437 string strValue = " ";
438 int iValue = 0;
439 bool result = StrToInt(strValue, iValue);
440 EXPECT_EQ(result, false);
441 }
442
443 HWTEST_F(UtilsStringTest, test_strcovertfailed_01, TestSize.Level0)
444 {
445 char test[] = {192, 157, 47, 106, 97, 18, 97, 47, 115, 1, 2};
446 string strValue(test);
447
448 bool ret = IsAsciiString(strValue);
449 EXPECT_EQ(ret, false);
450
451 strValue = "1234";
452 ret = IsAsciiString(strValue);
453 EXPECT_EQ(ret, true);
454
455 strValue = "abcde";
456 ret = IsAsciiString(strValue);
457 EXPECT_EQ(ret, true);
458 }
459
460
461 HWTEST_F(UtilsStringTest, test_strcovert_01, TestSize.Level0)
462 {
463 string strValue = "hello world!";
464 u16string str16 = Str8ToStr16(strValue);
465 EXPECT_EQ(0, strValue.compare(Str16ToStr8(str16)));
466 }
467
468 HWTEST_F(UtilsStringTest, test_strcovert_02, TestSize.Level0)
469 {
470 string str8Value = "hello world!";
471 u16string str16Result = u"hello world!";
472 u16string str16Value = Str8ToStr16(str8Value);
473 EXPECT_EQ(0, str16Result.compare(str16Value));
474
475 str16Result = u"你好";
476 string str8Result = Str16ToStr8(str16Result);
477 str16Value = Str8ToStr16(str8Result);
478 EXPECT_EQ(0, str16Result.compare(str16Value));
479
480
481 str16Result = u"某某技术有限公司";
482 str8Result = Str16ToStr8(str16Result);
483 str16Value = Str8ToStr16(str8Result);
484 EXPECT_EQ(0, str16Result.compare(str16Value));
485 }
486
487 HWTEST_F(UtilsStringTest, test_strcovert_03, TestSize.Level0)
488 {
489 string str8Value = "1234567890!@#$%^&*().";
490 u16string str16Result = u"1234567890!@#$%^&*().";
491 u16string str16Value = Str8ToStr16(str8Value);
492 EXPECT_EQ(0, str16Result.compare(str16Value));
493
494 string str8Result = Str16ToStr8(str16Value);
495 EXPECT_EQ(0, str8Result.compare(str8Value));
496 }
497
498 HWTEST_F(UtilsStringTest, test_strcovert_04, TestSize.Level0)
499 {
500 string str8Value = "1234567890!@#$%^&*().qazxswedcvfr,./;'][";
501 u16string str16Result = u"1234567890!@#$%^&*().qazxswedcvfr,./;'][";
502 u16string str16Value = Str8ToStr16(str8Value);
503 EXPECT_EQ(0, str16Result.compare(str16Value));
504
505 string str8Result = Str16ToStr8(str16Value);
506 EXPECT_EQ(0, str8Result.compare(str8Value));
507 }
508
509
510 HWTEST_F(UtilsStringTest, test_getsubstr_01, TestSize.Level0)
511 {
512 string strBase = "test for {sub str} {sub str1}";
513 string left = "{";
514 string right = "}";
515 string strResult = "sub str";
516 string strValue;
517 string::size_type pos = GetFirstSubStrBetween(strBase, left, right, strValue);
518 EXPECT_EQ(17, (int)pos);
519 EXPECT_EQ(strResult, strValue);
520
521 strBase = "test for sub str} {sub str1}";
522 strResult = "sub str1";
523 pos = GetFirstSubStrBetween(strBase, left, right, strValue);
524 EXPECT_EQ(27, (int)pos);
525 EXPECT_EQ(strResult, strValue);
526 }
527
528 HWTEST_F(UtilsStringTest, test_getsubstr_02, TestSize.Level0)
529 {
530 string strBase = "test for} {sub str {sub str1";
531 string left = "{";
532 string right = "}";
533 string strValue;
534 string::size_type pos = GetFirstSubStrBetween(strBase, left, right, strValue);
535 EXPECT_EQ(pos, string::npos);
536 }
537
538
539 HWTEST_F(UtilsStringTest, test_getsubstr_03, TestSize.Level0)
540 {
541 string strBase = "test for {sub str} {sub str1}";
542 string left = "{";
543 string right = "}";
544 string strResult[2] = { "sub str", "sub str1" };
545 vector<string> strValue;
546 GetSubStrBetween(strBase, left, right, strValue);
547 for (int i = 0; i < 2; i++) {
548 EXPECT_EQ(strResult[i], strValue[i]);
549 }
550 }
551
552 HWTEST_F(UtilsStringTest, test_getsubstr_04, TestSize.Level0)
553 {
554 string strBase = "test for } {sub str {sub str1";
555 string left = "{";
556 string right = "}";
557 string strResult[2] = { "sub str", "sub str1" };
558 vector<string> strValue;
559 GetSubStrBetween(strBase, left, right, strValue);
560 EXPECT_EQ(0, static_cast<int>(strValue.size()));
561 }
562
563 HWTEST_F(UtilsStringTest, DexToHexString_01, TestSize.Level0)
564 {
565 string result = DexToHexString(0);
566 EXPECT_EQ(result, "0");
567
568 result = DexToHexString(14);
569 EXPECT_EQ(result, "E");
570
571 result = DexToHexString(14, false);
572 EXPECT_EQ(result, "e");
573
574 result = DexToHexString(-14, false);
575 EXPECT_EQ(result, "fffffff2");
576
577 result = DexToHexString(-14);
578 EXPECT_EQ(result, "FFFFFFF2");
579
580 result = DexToHexString(11259375);
581 EXPECT_EQ(result, "ABCDEF");
582
583 result = DexToHexString(11259375, false);
584 EXPECT_EQ(result, "abcdef");
585 }
586