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