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