• 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 /*
223 * Feature: string_ex
224 * Function: IsNumericStr
225 * SubFunction: NA
226 * FunctionPoints:
227 * EnvConditions: NA
228 * CaseDescription: test for judge all characters of the string are numbers
229 */
230 HWTEST_F(UtilsStringTest, test_strisnumeric_01, TestSize.Level0)
231 {
232     string strBase = "1234556";
233     bool result = IsNumericStr(strBase);
234     EXPECT_EQ(result, true);
235 
236     strBase = "1234,a";
237     result = IsNumericStr(strBase);
238     EXPECT_EQ(result, false);
239 
240     strBase = "";
241     result = IsNumericStr(strBase);
242     EXPECT_EQ(result, false);
243 }
244 
245 /*
246 * Feature: string_ex
247 * Function: IsAlphaStr
248 * SubFunction: NA
249 * FunctionPoints:
250 * EnvConditions: NA
251 * CaseDescription: test for judge all characters of the string are alphabet
252 */
253 HWTEST_F(UtilsStringTest, test_strisalpha_01, TestSize.Level0)
254 {
255     string strBase = "1234556";
256     bool result = IsAlphaStr(strBase);
257     EXPECT_EQ(result, false);
258 
259     strBase = "Acedafe";
260     result = IsAlphaStr(strBase);
261     EXPECT_EQ(result, true);
262 
263     strBase = "Acedafe  ";
264     result = IsAlphaStr(strBase);
265     EXPECT_EQ(result, false);
266 
267     strBase = "Acedafe3";
268     result = IsAlphaStr(strBase);
269     EXPECT_EQ(result, false);
270 
271     strBase = "";
272     result = IsAlphaStr(strBase);
273     EXPECT_EQ(result, false);
274 }
275 
276 /*
277 * Feature: string_ex
278 * Function: IsUpperStr
279 * SubFunction: NA
280 * FunctionPoints:
281 * EnvConditions: NA
282 * CaseDescription: test for judge all characters of the string are uppercase
283 */
284 HWTEST_F(UtilsStringTest, test_IsUpperStr_01, TestSize.Level0)
285 {
286     string strBase = "ABSEFAD";
287     bool result = IsUpperStr(strBase);
288     EXPECT_EQ(result, true);
289 
290     strBase = "Afaefadf";
291     result = IsUpperStr(strBase);
292     EXPECT_EQ(result, false);
293 
294     strBase = "12e13eaefd     ";
295     result = IsUpperStr(strBase);
296     EXPECT_EQ(result, false);
297 
298     strBase = "";
299     result = IsUpperStr(strBase);
300     EXPECT_EQ(result, false);
301 }
302 
303 /*
304 * Feature: string_ex
305 * Function: IsLowerStr
306 * SubFunction: NA
307 * FunctionPoints:
308 * EnvConditions: NA
309 * CaseDescription: test for judge all characters of the string are lowercase
310 */
311 HWTEST_F(UtilsStringTest, test_IsLowerStr_01, TestSize.Level0)
312 {
313     string strBase = "testlower";
314     bool result = IsLowerStr(strBase);
315     EXPECT_EQ(result, true);
316 
317     strBase = "AAFDeadfkl";
318     result = IsLowerStr(strBase);
319     EXPECT_EQ(result, false);
320 
321     strBase = "12e";
322     result = IsLowerStr(strBase);
323     EXPECT_EQ(result, false);
324 
325     strBase = "";
326     result = IsLowerStr(strBase);
327     EXPECT_EQ(result, false);
328 }
329 
330 /*
331 * Feature: string_ex
332 * Function: IsSubStr
333 * SubFunction: NA
334 * FunctionPoints:
335 * EnvConditions: NA
336 * CaseDescription: test for judge the sub_str in base_str
337 */
338 HWTEST_F(UtilsStringTest, test_IsSubStr_01, TestSize.Level0)
339 {
340     string strBase = "test for issubstr";
341     string strSub = "for";
342     bool result = IsSubStr(strBase, strSub);
343     EXPECT_EQ(result, true);
344 
345     strSub = "fori";
346     result = IsSubStr(strBase, strSub);
347     EXPECT_EQ(result, false);
348 }
349 
350 /*
351 * Feature: string_ex
352 * Function: IsSameTextStr
353 * SubFunction: NA
354 * FunctionPoints:
355 * EnvConditions: NA
356 * CaseDescription: test for judge the strFirst's letter is same with strSecond
357 */
358 HWTEST_F(UtilsStringTest, test_IsSameTextStr_01, TestSize.Level0)
359 {
360     string strFirst = "Test For StrSameText";
361     string strSecond = "test for strsametext";
362     bool result = IsSameTextStr(strFirst, strSecond);
363     EXPECT_EQ(result, true);
364 
365     strSecond = "test for strsametex";
366     result = IsSameTextStr(strFirst, strSecond);
367     EXPECT_EQ(result, false);
368 }
369 
370 /*
371 * Feature: string_ex
372 * Function: ToString
373 * SubFunction: NA
374 * FunctionPoints:
375 * EnvConditions: NA
376 * CaseDescription: test for convert int to str
377 */
378 HWTEST_F(UtilsStringTest, test_ToString_01, TestSize.Level0)
379 {
380     int ivalue = 12345;
381     string strValue = "12345";
382     string result = ToString(ivalue);
383     EXPECT_EQ(result, strValue);
384 
385     ivalue = -15;
386     result = ToString(ivalue);
387     EXPECT_EQ(result, "-15");
388 }
389 
390 /*
391 * Feature: string_ex
392 * Function: StrToInt
393 * SubFunction: NA
394 * FunctionPoints:
395 * EnvConditions: NA
396 * CaseDescription: test for convert str to int
397 */
398 HWTEST_F(UtilsStringTest, test_StrToInt_01, TestSize.Level0)
399 {
400     string strValue = "12345";
401     int iValue = 0;
402     bool result = StrToInt(strValue, iValue);
403     EXPECT_EQ(result, true);
404     EXPECT_EQ(iValue, 12345);
405 
406     strValue = "123r54";
407     result = StrToInt(strValue, iValue);
408     EXPECT_EQ(result, false);
409 }
410 
411 HWTEST_F(UtilsStringTest, test_StrToInt_02, TestSize.Level0)
412 {
413     string strValue = "-12345";
414     int iValue = 0;
415     bool result = StrToInt(strValue, iValue);
416     EXPECT_EQ(result, true);
417     EXPECT_EQ(iValue, -12345);
418 
419     strValue = "123=     54";
420     result = StrToInt(strValue, iValue);
421     EXPECT_EQ(result, false);
422 
423     string strvalue2;
424     result = StrToInt(strvalue2, iValue);
425     EXPECT_EQ(result, false);
426 }
427 
428 HWTEST_F(UtilsStringTest, test_StrToInt_03, TestSize.Level0)
429 {
430     string strValue = "2147483648";
431     int ivalue = 0;
432     bool result = StrToInt(strValue, ivalue);
433     EXPECT_EQ(result, false);
434 }
435 
436 HWTEST_F(UtilsStringTest, test_StrToInt_04, TestSize.Level0)
437 {
438     string strValue = "             ";
439     int iValue = 0;
440     bool result = StrToInt(strValue, iValue);
441     EXPECT_EQ(result, false);
442 }
443 
444 HWTEST_F(UtilsStringTest, test_strcovertfailed_01, TestSize.Level0)
445 {
446     char test[] = {192, 157, 47, 106, 97, 18, 97, 47, 115, 1, 2};
447     string strValue(test);
448 
449     bool ret = IsAsciiString(strValue);
450     EXPECT_EQ(ret, false);
451 
452     strValue = "1234";
453     ret = IsAsciiString(strValue);
454     EXPECT_EQ(ret, true);
455 
456     strValue = "abcde";
457     ret = IsAsciiString(strValue);
458     EXPECT_EQ(ret, true);
459 }
460 
461 
462 HWTEST_F(UtilsStringTest, test_strcovert_01, TestSize.Level0)
463 {
464     string strValue = "hello world!";
465     u16string str16 = Str8ToStr16(strValue);
466     EXPECT_EQ(0, strValue.compare(Str16ToStr8(str16)));
467 }
468 
469 HWTEST_F(UtilsStringTest, test_strcovert_02, TestSize.Level0)
470 {
471     string str8Value = "hello world!";
472     u16string str16Result = u"hello world!";
473     u16string str16Value = Str8ToStr16(str8Value);
474     EXPECT_EQ(0, str16Result.compare(str16Value));
475 
476     str16Result = u"你好";
477     string str8Result = Str16ToStr8(str16Result);
478     str16Value = Str8ToStr16(str8Result);
479     EXPECT_EQ(0, str16Result.compare(str16Value));
480 
481 
482     str16Result = u"某某技术有限公司";
483     str8Result = Str16ToStr8(str16Result);
484     str16Value = Str8ToStr16(str8Result);
485     EXPECT_EQ(0, str16Result.compare(str16Value));
486 }
487 
488 HWTEST_F(UtilsStringTest, test_strcovert_03, TestSize.Level0)
489 {
490     string str8Value = "1234567890!@#$%^&*().";
491     u16string str16Result = u"1234567890!@#$%^&*().";
492     u16string str16Value = Str8ToStr16(str8Value);
493     EXPECT_EQ(0, str16Result.compare(str16Value));
494 
495     string str8Result = Str16ToStr8(str16Value);
496     EXPECT_EQ(0, str8Result.compare(str8Value));
497 }
498 
499 HWTEST_F(UtilsStringTest, test_strcovert_04, TestSize.Level0)
500 {
501     string str8Value = "1234567890!@#$%^&*().qazxswedcvfr,./;'][";
502     u16string str16Result = u"1234567890!@#$%^&*().qazxswedcvfr,./;'][";
503     u16string str16Value = Str8ToStr16(str8Value);
504     EXPECT_EQ(0, str16Result.compare(str16Value));
505 
506     string str8Result = Str16ToStr8(str16Value);
507     EXPECT_EQ(0, str8Result.compare(str8Value));
508 }
509 
510 
511 HWTEST_F(UtilsStringTest, test_getsubstr_01, TestSize.Level0)
512 {
513     string strBase = "test for {sub str} {sub str1}";
514     string left = "{";
515     string right = "}";
516     string strResult = "sub str";
517     string strValue;
518     string::size_type pos = GetFirstSubStrBetween(strBase, left, right, strValue);
519     EXPECT_EQ(17, (int)pos);
520     EXPECT_EQ(strResult, strValue);
521 
522     strBase = "test for sub str} {sub str1}";
523     strResult = "sub str1";
524     pos = GetFirstSubStrBetween(strBase, left, right, strValue);
525     EXPECT_EQ(27, (int)pos);
526     EXPECT_EQ(strResult, strValue);
527 }
528 
529 HWTEST_F(UtilsStringTest, test_getsubstr_02, TestSize.Level0)
530 {
531     string strBase = "test for} {sub str {sub str1";
532     string left = "{";
533     string right = "}";
534     string strValue;
535     string::size_type pos = GetFirstSubStrBetween(strBase, left, right, strValue);
536     EXPECT_EQ(pos, string::npos);
537 }
538 
539 
540 HWTEST_F(UtilsStringTest, test_getsubstr_03, TestSize.Level0)
541 {
542     string strBase = "test for {sub str} {sub str1}";
543     string left = "{";
544     string right = "}";
545     string strResult[2] = { "sub str", "sub str1" };
546     vector<string> strValue;
547     GetSubStrBetween(strBase, left, right, strValue);
548     for (int i = 0; i < 2; i++) {
549         EXPECT_EQ(strResult[i], strValue[i]);
550     }
551 }
552 
553 HWTEST_F(UtilsStringTest, test_getsubstr_04, TestSize.Level0)
554 {
555     string strBase = "test for } {sub str {sub str1";
556     string left = "{";
557     string right = "}";
558     string strResult[2] = { "sub str", "sub str1" };
559     vector<string> strValue;
560     GetSubStrBetween(strBase, left, right, strValue);
561     EXPECT_EQ(0, static_cast<int>(strValue.size()));
562 }
563 
564 HWTEST_F(UtilsStringTest, DexToHexString_01, TestSize.Level0)
565 {
566     string result = DexToHexString(0);
567     EXPECT_EQ(result, "0");
568 
569     result = DexToHexString(14);
570     EXPECT_EQ(result, "E");
571 
572     result = DexToHexString(14, false);
573     EXPECT_EQ(result, "e");
574 
575     result = DexToHexString(-14, false);
576     EXPECT_EQ(result, "fffffff2");
577 
578     result = DexToHexString(-14);
579     EXPECT_EQ(result, "FFFFFFF2");
580 
581     result = DexToHexString(11259375);
582     EXPECT_EQ(result, "ABCDEF");
583 
584     result = DexToHexString(11259375, false);
585     EXPECT_EQ(result, "abcdef");
586 }
587 }  // namespace
588 }  // namespace OHOS