• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 <benchmark/benchmark.h>
17 #include "string_ex.h"
18 #include <iostream>
19 #include "../log.h"
20 #include "../assert.h"
21 using namespace std;
22 
23 namespace OHOS {
24 namespace {
25 
26 static constexpr int32_t COMPARE_STRING_RESULT = 0;
27 static constexpr uint32_t STRSPLIT04_STRING_SIZE = 0;
28 static constexpr int STRTOINT01_INT_VALUE = 12345;
29 static constexpr int STRTOINT02_INT_VALUE = -12345;
30 static constexpr int GETSUBSTR01_POS_VALUE1 = 17;
31 static constexpr int GETSUBSTR01_POS_VALUE2 = 27;
32 static constexpr int GETSUBSTR04_STRING_SIZE = 0;
33 
34 #define STRSPLIT01_CHAR_ARRAY_SIZE 3
35 #define STRSPLIT02_CHAR_ARRAY_SIZE 2
36 #define STRSPLIT03_CHAR_ARRAY_SIZE 3
37 #define GETSUBSTR03_CHAR_ARRAY_SIZE 2
38 #define GETSUBSTR04_CHAR_ARRAY_SIZE 2
39 
40 class BenchmarkStringTest : public benchmark::Fixture {
41 public:
BenchmarkStringTest()42     BenchmarkStringTest()
43     {
44         Iterations(iterations);
45         Repetitions(repetitions);
46         ReportAggregatesOnly();
47     }
48 
49     ~BenchmarkStringTest() override = default;
SetUp(const::benchmark::State & state)50     void SetUp(const ::benchmark::State& state) override
51     {
52     }
53 
TearDown(const::benchmark::State & state)54     void TearDown(const ::benchmark::State& state) override
55     {
56     }
57 
58 protected:
59     const int32_t repetitions = 3;
60     const int32_t iterations = 1000;
61 };
62 
63 /*
64 * Feature: string_ex
65 * Function: UpperStr
66 * SubFunction: NA
67 * FunctionPoints:
68 * EnvConditions: NA
69 * CaseDescription: test for convert all letters of str  to uppercase
70 */
BENCHMARK_F(BenchmarkStringTest,test_strupper_01)71 BENCHMARK_F(BenchmarkStringTest, test_strupper_01)(benchmark::State& state)
72 {
73     BENCHMARK_LOGD("StringTest test_strupper_01 start.");
74     while (state.KeepRunning()) {
75         string strBase = "strbase";
76         string strTemp = "STRBASE";
77         string result = UpperStr(strBase);
78         AssertEqual(result, strTemp, "result did not equal strTemp as expected.", state);
79 
80         strBase = "StrBase";
81         result = UpperStr(strBase);
82         AssertEqual(result, strTemp, "result did not equal strTemp as expected.", state);
83     }
84     BENCHMARK_LOGD("StringTest test_strupper_01 end.");
85 }
86 
BENCHMARK_F(BenchmarkStringTest,test_strupper_02)87 BENCHMARK_F(BenchmarkStringTest, test_strupper_02)(benchmark::State& state)
88 {
89     BENCHMARK_LOGD("StringTest test_strupper_02 start.");
90     while (state.KeepRunning()) {
91         string strBase = "";
92         string strTemp = "";
93         string result = UpperStr(strBase);
94         AssertEqual(result, strTemp, "result did not equal strTemp as expected.", state);
95     }
96     BENCHMARK_LOGD("StringTest test_strupper_02 end.");
97 }
98 
99 /*
100 * Feature: string_ex
101 * Function: LowerStr
102 * SubFunction: NA
103 * FunctionPoints:
104 * EnvConditions: NA
105 * CaseDescription: test for convert all letters of str  to lowercase
106 */
BENCHMARK_F(BenchmarkStringTest,test_strlower_01)107 BENCHMARK_F(BenchmarkStringTest, test_strlower_01)(benchmark::State& state)
108 {
109     BENCHMARK_LOGD("StringTest test_strlower_01 start.");
110     while (state.KeepRunning()) {
111         string strBase = "STRbase";
112         string strTemp = "strbase";
113         string result = LowerStr(strBase);
114         AssertEqual(result, strTemp, "result did not equal strTemp as expected.", state);
115 
116         strBase = "StrBase";
117         result = LowerStr(strBase);
118         AssertEqual(result, strTemp, "result did not equal strTemp as expected.", state);
119     }
120     BENCHMARK_LOGD("StringTest test_strlower_01 end.");
121 }
122 
BENCHMARK_F(BenchmarkStringTest,test_strlower_02)123 BENCHMARK_F(BenchmarkStringTest, test_strlower_02)(benchmark::State& state)
124 {
125     BENCHMARK_LOGD("StringTest test_strlower_02 start.");
126     while (state.KeepRunning()) {
127         string strBase = "";
128         string strTemp = "";
129         string result = LowerStr(strBase);
130         AssertEqual(result, strTemp, "result did not equal strTemp as expected.", state);
131     }
132     BENCHMARK_LOGD("StringTest test_strlower_02 end.");
133 }
134 
135 /*
136 * Feature: string_ex
137 * Function: ReplaceStr
138 * SubFunction: NA
139 * FunctionPoints:
140 * EnvConditions: NA
141 * CaseDescription: test for replace src with dst int strBase
142 */
BENCHMARK_F(BenchmarkStringTest,test_strreplace_01)143 BENCHMARK_F(BenchmarkStringTest, test_strreplace_01)(benchmark::State& state)
144 {
145     BENCHMARK_LOGD("StringTest test_strreplace_01 start.");
146     while (state.KeepRunning()) {
147         string strBase = "test for replace";
148         string src = "for";
149         string dst = "with";
150         string strTemp = "test with replace";
151         string result = ReplaceStr(strBase, src, dst);
152         AssertEqual(result, strTemp, "result did not equal strTemp as expected.", state);
153 
154         src = "test for replace";
155         dst = "test";
156         strTemp = "test";
157         result = ReplaceStr(strBase, src, dst);
158         AssertEqual(result, strTemp, "result did not equal strTemp as expected.", state);
159 
160         src = "";
161         dst = "test";
162         result = ReplaceStr(strBase, src, dst);
163         AssertEqual(result, strBase, "result did not equal strBase as expected.", state);
164 
165         src = "for";
166         dst = "";
167         strTemp = "test  replace";
168         result = ReplaceStr(strBase, src, dst);
169         AssertEqual(result, strTemp, "result did not equal strTemp as expected.", state);
170     }
171     BENCHMARK_LOGD("StringTest test_strreplace_01 end.");
172 }
173 
174 /*
175 * Feature: string_ex
176 * Function: TrimStr
177 * SubFunction: NA
178 * FunctionPoints:
179 * EnvConditions: NA
180 * CaseDescription: test for trim str front and end
181 */
BENCHMARK_F(BenchmarkStringTest,test_strtrim_01)182 BENCHMARK_F(BenchmarkStringTest, test_strtrim_01)(benchmark::State& state)
183 {
184     BENCHMARK_LOGD("StringTest test_strtrim_01 start.");
185     while (state.KeepRunning()) {
186         string strBase = "              test for trim ";
187         string strTemp = "test for trim";
188         string result = TrimStr(strBase);
189         AssertEqual(result, strTemp, "result did not equal strTemp as expected.", state);
190     }
191     BENCHMARK_LOGD("StringTest test_strtrim_01 end.");
192 }
193 
BENCHMARK_F(BenchmarkStringTest,test_strtrim_02)194 BENCHMARK_F(BenchmarkStringTest, test_strtrim_02)(benchmark::State& state)
195 {
196     BENCHMARK_LOGD("StringTest test_strtrim_02 start.");
197     while (state.KeepRunning()) {
198         string strBase = "test";
199         string strTemp = "es";
200         string result = TrimStr(strBase, 't');
201         AssertEqual(result, strTemp, "result did not equal strTemp as expected.", state);
202     }
203     BENCHMARK_LOGD("StringTest test_strtrim_02 end.");
204 }
205 
206 /*
207 * Feature: string_ex
208 * Function: SplitStr
209 * SubFunction: NA
210 * FunctionPoints:
211 * EnvConditions: NA
212 * CaseDescription: test for split str by strSep
213 */
BENCHMARK_F(BenchmarkStringTest,test_strsplit_01)214 BENCHMARK_F(BenchmarkStringTest, test_strsplit_01)(benchmark::State& state)
215 {
216     BENCHMARK_LOGD("StringTest test_strsplit_01 start.");
217     while (state.KeepRunning()) {
218         string strBase = "test for split";
219         string strSep = " ";
220         string splitResult[STRSPLIT01_CHAR_ARRAY_SIZE] = { "test", "for", "split" };
221         vector<string> strsRet;
222         SplitStr(strBase, strSep, strsRet);
223 
224         for (int i = 0; i < STRSPLIT01_CHAR_ARRAY_SIZE; i++) {
225             AssertEqual(splitResult[i], strsRet[i], "splitResult[i] did not equal strsRet[i] as expected.", state);
226         }
227     }
228     BENCHMARK_LOGD("StringTest test_strsplit_01 end.");
229 }
230 
BENCHMARK_F(BenchmarkStringTest,test_strsplit_02)231 BENCHMARK_F(BenchmarkStringTest, test_strsplit_02)(benchmark::State& state)
232 {
233     BENCHMARK_LOGD("StringTest test_strsplit_02 start.");
234     while (state.KeepRunning()) {
235         string strBase = "test for split";
236         string strSep = "for";
237         string splitResult[STRSPLIT02_CHAR_ARRAY_SIZE] = { "test", "split" };
238         vector<string> strsRet;
239         SplitStr(strBase, strSep, strsRet);
240 
241         for (int i = 0; i < STRSPLIT02_CHAR_ARRAY_SIZE; i++) {
242             AssertEqual(splitResult[i], strsRet[i], "splitResult[i] did not equal strsRet[i] as expected.", state);
243         }
244 
245         splitResult[0] = "test ";
246         splitResult[1] = " split";
247         SplitStr(strBase, strSep, strsRet, false, false);
248         for (int i = 0; i < STRSPLIT02_CHAR_ARRAY_SIZE; i++) {
249             AssertEqual(splitResult[i], strsRet[i], "splitResult[i] did not equal strsRet[i] as expected.", state);
250         }
251     }
252     BENCHMARK_LOGD("StringTest test_strsplit_02 end.");
253 }
254 
BENCHMARK_F(BenchmarkStringTest,test_strsplit_03)255 BENCHMARK_F(BenchmarkStringTest, test_strsplit_03)(benchmark::State& state)
256 {
257     BENCHMARK_LOGD("StringTest test_strsplit_03 start.");
258     while (state.KeepRunning()) {
259         string strBase = "test for for split";
260         string strSep = "for";
261         string splitResult[STRSPLIT03_CHAR_ARRAY_SIZE] = { "test", "", "split" };
262         vector<string> strsRet;
263         SplitStr(strBase, strSep, strsRet, true);
264         for (int i = 0; i < (int)strsRet.size(); i++) {
265             AssertEqual(splitResult[i], strsRet[i], "splitResult[i] did not equal strsRet[i] as expected.", state);
266         }
267     }
268     BENCHMARK_LOGD("StringTest test_strsplit_03 end.");
269 }
270 
271 /*
272 * Feature: string_ex
273 * Function: SplitStr
274 * SubFunction: NA
275 * FunctionPoints:
276 * EnvConditions: NA
277 * CaseDescription: test splitting a null string with a null seperator
278 */
BENCHMARK_F(BenchmarkStringTest,test_strsplit_04)279 BENCHMARK_F(BenchmarkStringTest, test_strsplit_04)(benchmark::State& state)
280 {
281     BENCHMARK_LOGD("StringTest test_strsplit_04 start.");
282     while (state.KeepRunning()) {
283         string strBase = "";
284         string strSep = "";
285         vector<string> strsRet1;
286         SplitStr(strBase, strSep, strsRet1);
287         AssertEqual(strsRet1.size(), STRSPLIT04_STRING_SIZE, "strsRet1.size() did not equal 0 as expected.", state);
288 
289         vector<string> strsRet2;
290         SplitStr(strBase, strSep, strsRet2, true);
291         AssertEqual(strsRet2[0], "", "strsRet2[0] did not equal \"\" as expected.", state);
292     }
293     BENCHMARK_LOGD("StringTest test_strsplit_04 end.");
294 }
295 
296 /*
297 * Feature: string_ex
298 * Function: IsNumericStr
299 * SubFunction: NA
300 * FunctionPoints:
301 * EnvConditions: NA
302 * CaseDescription: test for judge all characters of the string are numbers
303 */
BENCHMARK_F(BenchmarkStringTest,test_strisnumeric_01)304 BENCHMARK_F(BenchmarkStringTest, test_strisnumeric_01)(benchmark::State& state)
305 {
306     BENCHMARK_LOGD("StringTest test_strisnumeric_01 start.");
307     while (state.KeepRunning()) {
308         string strBase = "1234556";
309         bool result = IsNumericStr(strBase);
310         AssertEqual(result, true, "result did not equal true as expected.", state);
311 
312         strBase = "1234,a";
313         result = IsNumericStr(strBase);
314         AssertEqual(result, false, "result did not equal false as expected.", state);
315 
316         strBase = "";
317         result = IsNumericStr(strBase);
318         AssertEqual(result, false, "result did not equal false as expected.", state);
319     }
320     BENCHMARK_LOGD("StringTest test_strisnumeric_01 end.");
321 }
322 
323 /*
324 * Feature: string_ex
325 * Function: IsAlphaStr
326 * SubFunction: NA
327 * FunctionPoints:
328 * EnvConditions: NA
329 * CaseDescription: test for judge all characters of the string are alphabet
330 */
BENCHMARK_F(BenchmarkStringTest,test_strisalpha_01)331 BENCHMARK_F(BenchmarkStringTest, test_strisalpha_01)(benchmark::State& state)
332 {
333     BENCHMARK_LOGD("StringTest test_strisalpha_01 start.");
334     while (state.KeepRunning()) {
335         string strBase = "1234556";
336         bool result = IsAlphaStr(strBase);
337         AssertEqual(result, false, "result did not equal false as expected.", state);
338 
339         strBase = "Acedafe";
340         result = IsAlphaStr(strBase);
341         AssertEqual(result, true, "result did not equal true as expected.", state);
342 
343         strBase = "Acedafe  ";
344         result = IsAlphaStr(strBase);
345         AssertEqual(result, false, "result did not equal false as expected.", state);
346 
347         strBase = "Acedafe3";
348         result = IsAlphaStr(strBase);
349         AssertEqual(result, false, "result did not equal false as expected.", state);
350 
351         strBase = "";
352         result = IsAlphaStr(strBase);
353         AssertEqual(result, false, "result did not equal false as expected.", state);
354     }
355     BENCHMARK_LOGD("StringTest test_strisalpha_01 end.");
356 }
357 
358 /*
359 * Feature: string_ex
360 * Function: IsUpperStr
361 * SubFunction: NA
362 * FunctionPoints:
363 * EnvConditions: NA
364 * CaseDescription: test for judge all characters of the string are uppercase
365 */
BENCHMARK_F(BenchmarkStringTest,test_IsUpperStr_01)366 BENCHMARK_F(BenchmarkStringTest, test_IsUpperStr_01)(benchmark::State& state)
367 {
368     BENCHMARK_LOGD("StringTest test_IsUpperStr_01 start.");
369     while (state.KeepRunning()) {
370         string strBase = "ABSEFAD";
371         bool result = IsUpperStr(strBase);
372         AssertEqual(result, true, "result did not equal true as expected.", state);
373 
374         strBase = "Afaefadf";
375         result = IsUpperStr(strBase);
376         AssertEqual(result, false, "result did not equal false as expected.", state);
377 
378         strBase = "12e13eaefd     ";
379         result = IsUpperStr(strBase);
380         AssertEqual(result, false, "result did not equal false as expected.", state);
381 
382         strBase = "";
383         result = IsUpperStr(strBase);
384         AssertEqual(result, false, "result did not equal false as expected.", state);
385     }
386     BENCHMARK_LOGD("StringTest test_IsUpperStr_01 end.");
387 }
388 
389 /*
390 * Feature: string_ex
391 * Function: IsLowerStr
392 * SubFunction: NA
393 * FunctionPoints:
394 * EnvConditions: NA
395 * CaseDescription: test for judge all characters of the string are lowercase
396 */
BENCHMARK_F(BenchmarkStringTest,test_IsLowerStr_01)397 BENCHMARK_F(BenchmarkStringTest, test_IsLowerStr_01)(benchmark::State& state)
398 {
399     BENCHMARK_LOGD("StringTest test_IsLowerStr_01 start.");
400     while (state.KeepRunning()) {
401         string strBase = "testlower";
402         bool result = IsLowerStr(strBase);
403         AssertEqual(result, true, "result did not equal true as expected.", state);
404 
405         strBase = "AAFDeadfkl";
406         result = IsLowerStr(strBase);
407         AssertEqual(result, false, "result did not equal false as expected.", state);
408 
409         strBase = "12e";
410         result = IsLowerStr(strBase);
411         AssertEqual(result, false, "result did not equal false as expected.", state);
412 
413         strBase = "";
414         result = IsLowerStr(strBase);
415         AssertEqual(result, false, "result did not equal false as expected.", state);
416     }
417     BENCHMARK_LOGD("StringTest test_IsLowerStr_01 end.");
418 }
419 
420 /*
421 * Feature: string_ex
422 * Function: IsSubStr
423 * SubFunction: NA
424 * FunctionPoints:
425 * EnvConditions: NA
426 * CaseDescription: test for judge the sub_str in base_str
427 */
BENCHMARK_F(BenchmarkStringTest,test_IsSubStr_01)428 BENCHMARK_F(BenchmarkStringTest, test_IsSubStr_01)(benchmark::State& state)
429 {
430     BENCHMARK_LOGD("StringTest test_IsSubStr_01 start.");
431     while (state.KeepRunning()) {
432         string strBase = "test for issubstr";
433         string strSub = "for";
434         bool result = IsSubStr(strBase, strSub);
435         AssertEqual(result, true, "result did not equal true as expected.", state);
436 
437         strBase = "";
438         strSub = "";
439         result = IsSubStr(strBase, strSub);
440         AssertEqual(result, false, "result did not equal false as expected.", state);
441 
442         strSub = "fori";
443         result = IsSubStr(strBase, strSub);
444         AssertEqual(result, false, "result did not equal false as expected.", state);
445     }
446     BENCHMARK_LOGD("StringTest test_IsSubStr_01 end.");
447 }
448 
449 /*
450 * Feature: string_ex
451 * Function: IsSameTextStr
452 * SubFunction: NA
453 * FunctionPoints:
454 * EnvConditions: NA
455 * CaseDescription: test for judge the strFirst's letter is same with strSecond
456 */
BENCHMARK_F(BenchmarkStringTest,test_IsSameTextStr_01)457 BENCHMARK_F(BenchmarkStringTest, test_IsSameTextStr_01)(benchmark::State& state)
458 {
459     BENCHMARK_LOGD("StringTest test_IsSameTextStr_01 start.");
460     while (state.KeepRunning()) {
461         string strFirst = "Test For StrSameText";
462         string strSecond = "test for strsametext";
463         bool result = IsSameTextStr(strFirst, strSecond);
464         AssertEqual(result, true, "result did not equal true as expected.", state);
465 
466         strSecond = "test for strsametex";
467         result = IsSameTextStr(strFirst, strSecond);
468         AssertEqual(result, false, "result did not equal false as expected.", state);
469     }
470     BENCHMARK_LOGD("StringTest test_IsSameTextStr_01 end.");
471 }
472 
473 /*
474 * Feature: string_ex
475 * Function: ToString
476 * SubFunction: NA
477 * FunctionPoints:
478 * EnvConditions: NA
479 * CaseDescription: test for convert int to str
480 */
BENCHMARK_F(BenchmarkStringTest,test_ToString_01)481 BENCHMARK_F(BenchmarkStringTest, test_ToString_01)(benchmark::State& state)
482 {
483     BENCHMARK_LOGD("StringTest test_ToString_01 start.");
484     while (state.KeepRunning()) {
485         int ivalue = 12345;
486         string strValue = "12345";
487         string result = ToString(ivalue);
488         AssertEqual(result, strValue, "result did not equal strValue as expected.", state);
489 
490         ivalue = -15;
491         result = ToString(ivalue);
492         AssertEqual(result, "-15", "result did not equal \"-15\" as expected.", state);
493     }
494     BENCHMARK_LOGD("StringTest test_ToString_01 end.");
495 }
496 
497 /*
498 * Feature: string_ex
499 * Function: StrToInt
500 * SubFunction: NA
501 * FunctionPoints:
502 * EnvConditions: NA
503 * CaseDescription: test for convert str to int
504 */
BENCHMARK_F(BenchmarkStringTest,test_StrToInt_01)505 BENCHMARK_F(BenchmarkStringTest, test_StrToInt_01)(benchmark::State& state)
506 {
507     BENCHMARK_LOGD("StringTest test_StrToInt_01 start.");
508     while (state.KeepRunning()) {
509         string strValue = "12345";
510         int iValue = 0;
511         bool result = StrToInt(strValue, iValue);
512         AssertEqual(result, true, "result did not equal true as expected.", state);
513         AssertEqual(iValue, STRTOINT01_INT_VALUE, "iValue did not equal 12345 as expected.", state);
514 
515         strValue = "123r54";
516         result = StrToInt(strValue, iValue);
517         AssertEqual(result, false, "result did not equal false as expected.", state);
518     }
519     BENCHMARK_LOGD("StringTest test_StrToInt_01 end.");
520 }
521 
BENCHMARK_F(BenchmarkStringTest,test_StrToInt_02)522 BENCHMARK_F(BenchmarkStringTest, test_StrToInt_02)(benchmark::State& state)
523 {
524     BENCHMARK_LOGD("StringTest test_StrToInt_02 start.");
525     while (state.KeepRunning()) {
526         string strValue = "-12345";
527         int iValue = 0;
528         bool result = StrToInt(strValue, iValue);
529         AssertEqual(result, true, "result did not equal true as expected.", state);
530         AssertEqual(iValue, STRTOINT02_INT_VALUE, "iValue did not equal -12345 as expected.", state);
531 
532         strValue = "123=     54";
533         result = StrToInt(strValue, iValue);
534         AssertEqual(result, false, "result did not equal false as expected.", state);
535 
536         string strvalue2;
537         result = StrToInt(strvalue2, iValue);
538         AssertEqual(result, false, "result did not equal false as expected.", state);
539     }
540     BENCHMARK_LOGD("StringTest test_StrToInt_02 end.");
541 }
542 
BENCHMARK_F(BenchmarkStringTest,test_StrToInt_03)543 BENCHMARK_F(BenchmarkStringTest, test_StrToInt_03)(benchmark::State& state)
544 {
545     BENCHMARK_LOGD("StringTest test_StrToInt_03 start.");
546     while (state.KeepRunning()) {
547         string strValue = "2147483648";
548         int ivalue = 0;
549         bool result = StrToInt(strValue, ivalue);
550         AssertEqual(result, false, "result did not equal false as expected.", state);
551     }
552     BENCHMARK_LOGD("StringTest test_StrToInt_03 end.");
553 }
554 
BENCHMARK_F(BenchmarkStringTest,test_StrToInt_04)555 BENCHMARK_F(BenchmarkStringTest, test_StrToInt_04)(benchmark::State& state)
556 {
557     BENCHMARK_LOGD("StringTest test_StrToInt_04 start.");
558     while (state.KeepRunning()) {
559         string strValue = "             ";
560         int iValue = 0;
561         bool result = StrToInt(strValue, iValue);
562         AssertEqual(result, false, "result did not equal false as expected.", state);
563     }
564     BENCHMARK_LOGD("StringTest test_StrToInt_04 end.");
565 }
566 
BENCHMARK_F(BenchmarkStringTest,test_strcovertfailed_01)567 BENCHMARK_F(BenchmarkStringTest, test_strcovertfailed_01)(benchmark::State& state)
568 {
569     BENCHMARK_LOGD("StringTest test_strcovertfailed_01 start.");
570     while (state.KeepRunning()) {
571         char test[] = {192, 157, 47, 106, 97, 18, 97, 47, 115, 1, 2};
572         string strValue(test);
573 
574         bool ret = IsAsciiString(strValue);
575         AssertEqual(ret, false, "ret did not equal false as expected.", state);
576 
577         strValue = "1234";
578         ret = IsAsciiString(strValue);
579         AssertEqual(ret, true, "ret did not equal true as expected.", state);
580 
581         strValue = "abcde";
582         ret = IsAsciiString(strValue);
583         AssertEqual(ret, true, "ret did not equal true as expected.", state);
584     }
585     BENCHMARK_LOGD("StringTest test_strcovertfailed_01 end.");
586 }
587 
588 
BENCHMARK_F(BenchmarkStringTest,test_strcovert_01)589 BENCHMARK_F(BenchmarkStringTest, test_strcovert_01)(benchmark::State& state)
590 {
591     BENCHMARK_LOGD("StringTest test_strcovert_01 start.");
592     while (state.KeepRunning()) {
593         string strValue = "hello world!";
594         u16string str16 = Str8ToStr16(strValue);
595         AssertEqual(COMPARE_STRING_RESULT, strValue.compare(Str16ToStr8(str16)),
596             "strValue.compare(Str16ToStr8(str16)) did not equal 0 as expected.", state);
597     }
598     BENCHMARK_LOGD("StringTest test_strcovert_01 end.");
599 }
600 
BENCHMARK_F(BenchmarkStringTest,test_strcovert_02)601 BENCHMARK_F(BenchmarkStringTest, test_strcovert_02)(benchmark::State& state)
602 {
603     BENCHMARK_LOGD("StringTest test_strcovert_02 start.");
604     while (state.KeepRunning()) {
605         string str8Value = "hello world!";
606         u16string str16Result = u"hello world!";
607         u16string str16Value = Str8ToStr16(str8Value);
608         AssertEqual(COMPARE_STRING_RESULT, str16Result.compare(str16Value),
609             "str16Result.compare(str16Value) did not equal 0 as expected.", state);
610 
611         str16Result = u"你好";
612         string str8Result = Str16ToStr8(str16Result);
613         str16Value = Str8ToStr16(str8Result);
614         AssertEqual(COMPARE_STRING_RESULT, str16Result.compare(str16Value),
615             "str16Result.compare(str16Value) did not equal 0 as expected.", state);
616 
617 
618         str16Result = u"某某技术有限公司";
619         str8Result = Str16ToStr8(str16Result);
620         str16Value = Str8ToStr16(str8Result);
621         AssertEqual(COMPARE_STRING_RESULT, str16Result.compare(str16Value),
622             "str16Result.compare(str16Value) did not equal 0 as expected.", state);
623     }
624     BENCHMARK_LOGD("StringTest test_strcovert_02 end.");
625 }
626 
BENCHMARK_F(BenchmarkStringTest,test_strcovert_03)627 BENCHMARK_F(BenchmarkStringTest, test_strcovert_03)(benchmark::State& state)
628 {
629     BENCHMARK_LOGD("StringTest test_strcovert_03 start.");
630     while (state.KeepRunning()) {
631         string str8Value = "1234567890!@#$%^&*().";
632         u16string str16Result = u"1234567890!@#$%^&*().";
633         u16string str16Value = Str8ToStr16(str8Value);
634         AssertEqual(COMPARE_STRING_RESULT, str16Result.compare(str16Value),
635             "str16Result.compare(str16Value) did not equal 0 as expected.", state);
636 
637         string str8Result = Str16ToStr8(str16Value);
638         AssertEqual(COMPARE_STRING_RESULT, str8Result.compare(str8Value),
639             "str8Result.compare(str8Value) did not equal 0 as expected.", state);
640     }
641     BENCHMARK_LOGD("StringTest test_strcovert_03 end.");
642 }
643 
BENCHMARK_F(BenchmarkStringTest,test_strcovert_04)644 BENCHMARK_F(BenchmarkStringTest, test_strcovert_04)(benchmark::State& state)
645 {
646     BENCHMARK_LOGD("StringTest test_strcovert_04 start.");
647     while (state.KeepRunning()) {
648         string str8Value = "1234567890!@#$%^&*().qazxswedcvfr,./;'][";
649         u16string str16Result = u"1234567890!@#$%^&*().qazxswedcvfr,./;'][";
650         u16string str16Value = Str8ToStr16(str8Value);
651         AssertEqual(COMPARE_STRING_RESULT, str16Result.compare(str16Value),
652             "str16Result.compare(str16Value) did not equal 0 as expected.", state);
653 
654         string str8Result = Str16ToStr8(str16Value);
655         AssertEqual(COMPARE_STRING_RESULT, str8Result.compare(str8Value),
656             "str8Result.compare(str8Value) did not equal 0 as expected.", state);
657     }
658     BENCHMARK_LOGD("StringTest test_strcovert_04 end.");
659 }
660 
BENCHMARK_F(BenchmarkStringTest,test_getsubstr_01)661 BENCHMARK_F(BenchmarkStringTest, test_getsubstr_01)(benchmark::State& state)
662 {
663     BENCHMARK_LOGD("StringTest test_getsubstr_01 start.");
664     while (state.KeepRunning()) {
665         string strBase = "test for {sub str} {sub str1}";
666         string left = "{";
667         string right = "}";
668         string strResult = "sub str";
669         string strValue;
670         string::size_type pos = GetFirstSubStrBetween(strBase, left, right, strValue);
671         AssertEqual(GETSUBSTR01_POS_VALUE1, (int)pos, "17 did not equal (int)pos as expected.", state);
672         AssertEqual(strResult, strValue, "strResult did not equal strValue as expected.", state);
673 
674         strBase = "test for sub str} {sub str1}";
675         strResult = "sub str1";
676         pos = GetFirstSubStrBetween(strBase, left, right, strValue);
677         AssertEqual(GETSUBSTR01_POS_VALUE2, (int)pos, "27 did not equal (int)pos as expected.", state);
678         AssertEqual(strResult, strValue, "strResult did not equal strValue as expected.", state);
679     }
680     BENCHMARK_LOGD("StringTest test_getsubstr_01 end.");
681 }
682 
BENCHMARK_F(BenchmarkStringTest,test_getsubstr_02)683 BENCHMARK_F(BenchmarkStringTest, test_getsubstr_02)(benchmark::State& state)
684 {
685     BENCHMARK_LOGD("StringTest test_getsubstr_02 start.");
686     while (state.KeepRunning()) {
687         string strBase = "test for} {sub str {sub str1";
688         string left = "{";
689         string right = "}";
690         string strValue;
691         string::size_type pos = GetFirstSubStrBetween(strBase, left, right, strValue);
692         AssertEqual(pos, string::npos, "pos did not equal string::npos as expected.", state);
693     }
694     BENCHMARK_LOGD("StringTest test_getsubstr_02 end.");
695 }
696 
BENCHMARK_F(BenchmarkStringTest,test_getsubstr_03)697 BENCHMARK_F(BenchmarkStringTest, test_getsubstr_03)(benchmark::State& state)
698 {
699     BENCHMARK_LOGD("StringTest test_getsubstr_03 start.");
700     while (state.KeepRunning()) {
701         string strBase = "test for {sub str} {sub str1}";
702         string left = "{";
703         string right = "}";
704         string strResult[GETSUBSTR03_CHAR_ARRAY_SIZE] = { "sub str", "sub str1" };
705         vector<string> strValue;
706         GetSubStrBetween(strBase, left, right, strValue);
707         for (int i = 0; i < GETSUBSTR03_CHAR_ARRAY_SIZE; i++) {
708             AssertEqual(strResult[i], strValue[i], "strResult[i] did not equal strValue[i] as expected.", state);
709         }
710     }
711     BENCHMARK_LOGD("StringTest test_getsubstr_03 end.");
712 }
713 
BENCHMARK_F(BenchmarkStringTest,test_getsubstr_04)714 BENCHMARK_F(BenchmarkStringTest, test_getsubstr_04)(benchmark::State& state)
715 {
716     BENCHMARK_LOGD("StringTest test_getsubstr_04 start.");
717     while (state.KeepRunning()) {
718         string strBase = "test for } {sub str {sub str1";
719         string left = "{";
720         string right = "}";
721         string strResult[GETSUBSTR04_CHAR_ARRAY_SIZE] = { "sub str", "sub str1" };
722         vector<string> strValue;
723         GetSubStrBetween(strBase, left, right, strValue);
724         AssertEqual(GETSUBSTR04_STRING_SIZE, static_cast<int>(strValue.size()),
725             "static_cast<int>(strValue.size()) did not equal 0 as expected.", state);
726     }
727     BENCHMARK_LOGD("StringTest test_getsubstr_04 end.");
728 }
729 
BENCHMARK_F(BenchmarkStringTest,DexToHexString_01)730 BENCHMARK_F(BenchmarkStringTest, DexToHexString_01)(benchmark::State& state)
731 {
732     BENCHMARK_LOGD("StringTest DexToHexString_01 start.");
733     while (state.KeepRunning()) {
734         int zeroValue = 0;
735         string result = DexToHexString(zeroValue);
736         AssertEqual(result, "0", "result did not equal \"0\" as expected.", state);
737 
738         int positiveValue = 14;
739         result = DexToHexString(positiveValue);
740         AssertEqual(result, "E", "result did not equal \"E\" as expected.", state);
741 
742         result = DexToHexString(positiveValue, false);
743         AssertEqual(result, "e", "result did not equal \"e\" as expected.", state);
744 
745         int negativeValue = -14;
746         result = DexToHexString(negativeValue, false);
747         AssertEqual(result, "fffffff2", "result did not equal \"fffffff2\" as expected.", state);
748 
749         result = DexToHexString(negativeValue);
750         AssertEqual(result, "FFFFFFF2", "result did not equal \"FFFFFFF2\" as expected.", state);
751 
752         int largeValue = 11259375;
753         result = DexToHexString(largeValue);
754         AssertEqual(result, "ABCDEF", "result did not equal \"ABCDEF\" as expected.", state);
755 
756         result = DexToHexString(largeValue, false);
757         AssertEqual(result, "abcdef", "result did not equal \"abcdef\" as expected.", state);
758     }
759     BENCHMARK_LOGD("StringTest DexToHexString_01 end.");
760 }
761 }  // namespace
762 }  // namespace OHOS
763 // Run the benchmark
764 BENCHMARK_MAIN();