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 <algorithm>
18 #include <iostream>
19 #include <fstream>
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <fcntl.h>
23 #include <unistd.h>
24 #include "file_ex.h"
25 #include "benchmark_log.h"
26 #include "benchmark_assert.h"
27 using namespace std;
28
29 namespace OHOS {
30 namespace {
31
32 static constexpr char CONTENT_STR[] = "TTtt@#$%^&*()_+~`";
33 static constexpr char FILE_PATH[] = "./tmp1.txt";
34 static constexpr char NULL_STR[] = "";
35 static constexpr int MAX_FILE_LENGTH = 1 * 1024 * 1024;
36 static constexpr int EXCEEDS_MAXIMUM_LENGTH = 32 * 1024 * 1024 + 1;
37
38 class BenchmarkFileTest : public benchmark::Fixture {
39 public:
SetUp(const::benchmark::State & state)40 void SetUp(const ::benchmark::State& state) override
41 {
42 }
43
TearDown(const::benchmark::State & state)44 void TearDown(const ::benchmark::State& state) override
45 {
46 }
47
BenchmarkFileTest()48 BenchmarkFileTest()
49 {
50 Iterations(iterations);
51 Repetitions(repetitions);
52 ReportAggregatesOnly();
53 }
54
55 ~BenchmarkFileTest() override = default;
56
57 protected:
58 const int32_t repetitions = 3;
59 const int32_t iterations = 1000;
60 };
61
CreateTestFile(const std::string & path,const std::string & content)62 bool CreateTestFile(const std::string& path, const std::string& content)
63 {
64 ofstream out(path, ios_base::out | ios_base::trunc);
65 if (out.is_open()) {
66 out << content;
67 return true;
68 }
69
70 BENCHMARK_LOGD("open file failed! %{public}s", path.c_str());
71 return false;
72 }
73
RemoveTestFile(const std::string & path)74 int RemoveTestFile(const std::string& path)
75 {
76 return unlink(path.c_str());
77 }
78
LoadString(string & filename,string & content,benchmark::State & state)79 void LoadString(string& filename, string& content, benchmark::State& state)
80 {
81 while (state.KeepRunning()) {
82 string result;
83 CreateTestFile(filename, content);
84 int fd = open(filename.c_str(), O_RDONLY);
85 AssertTrue((LoadStringFromFd(fd, result)),
86 "LoadStringFromFd(fd, result) did not equal true as expected.", state);
87 close(fd);
88 RemoveTestFile(filename);
89 AssertEqual(result, content, "result == content did not equal true as expected.", state);
90 }
91 }
92
SaveString(string & filename,string & content,benchmark::State & state)93 void SaveString(string& filename, string& content, benchmark::State& state)
94 {
95 while (state.KeepRunning()) {
96 int fd = open(filename.c_str(), O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
97 bool ret = SaveStringToFd(fd, content);
98 close(fd);
99 AssertEqual(ret, true, "ret did not equal true as expected.", state);
100
101 string loadResult;
102 fd = open(filename.c_str(), O_RDONLY);
103 ret = LoadStringFromFd(fd, loadResult);
104 close(fd);
105 RemoveTestFile(filename);
106 AssertEqual(ret, true, "ret did not equal true as expected.", state);
107 AssertEqual(loadResult, content, "loadResult did not equal content as expected.", state);
108 }
109 }
110
111 /*
112 * @tc.name: testLoadStringFromFile001
113 * @tc.desc: Test loading an existed file 'meminfo'
114 */
BENCHMARK_F(BenchmarkFileTest,testLoadStringFromFile001)115 BENCHMARK_F(BenchmarkFileTest, testLoadStringFromFile001)(benchmark::State& state)
116 {
117 BENCHMARK_LOGD("FileTest testLoadStringFromFile001 start.");
118 string filename = "/proc/meminfo";
119 while (state.KeepRunning()) {
120 string str;
121 AssertTrue((LoadStringFromFile(filename, str)),
122 "LoadStringFromFile(filename, str) did not equal true as expected.", state);
123
124 string str2;
125 int fd = open(filename.c_str(), O_RDONLY);
126 AssertTrue((LoadStringFromFd(fd, str2)), "LoadStringFromFd(fd, str2) did not equal true as expected.", state);
127 close(fd);
128 AssertEqual(str.size(), str2.size(), "str.size() did not equal str2.size() as expected.", state);
129
130 vector<char> buff;
131 bool ret = LoadBufferFromFile(filename, buff);
132 AssertTrue((ret), "ret did not equal true as expected.", state);
133 AssertEqual(str2.size(), buff.size(), "str2.size() did not equal buff.size() as expected.", state);
134 }
135 BENCHMARK_LOGD("FileTest testLoadStringFromFile001 end.");
136 }
137
138 /*
139 * @tc.name: testLoadStringFromFile002
140 * @tc.desc: Test loading a non-existed file
141 */
BENCHMARK_F(BenchmarkFileTest,testLoadStringFromFile002)142 BENCHMARK_F(BenchmarkFileTest, testLoadStringFromFile002)(benchmark::State& state)
143 {
144 BENCHMARK_LOGD("FileTest testLoadStringFromFile002 start.");
145 string filename = NULL_STR;
146 while (state.KeepRunning()) {
147 string str;
148 AssertFalse((LoadStringFromFile(filename, str)),
149 "LoadStringFromFile(filename, str) did not equal false as expected.", state);
150 AssertTrue((str.empty()), "str.empty() did not equal true as expected.", state);
151 }
152 BENCHMARK_LOGD("FileTest testLoadStringFromFile002 end.");
153 }
154
155 /*
156 * @tc.name: testLoadStringFromFile003
157 * @tc.desc: Test loading a newly created file with null contents
158 */
BENCHMARK_F(BenchmarkFileTest,testLoadStringFromFile003)159 BENCHMARK_F(BenchmarkFileTest, testLoadStringFromFile003)(benchmark::State& state)
160 {
161 BENCHMARK_LOGD("FileTest testLoadStringFromFile003 start.");
162 string filename = FILE_PATH;
163 string content = NULL_STR;
164 while (state.KeepRunning()) {
165 string str;
166 CreateTestFile(filename, content);
167 AssertTrue((LoadStringFromFile(filename, str)),
168 "LoadStringFromFile(filename, str) did not equal true as expected.", state);
169 RemoveTestFile(filename);
170 AssertEqual(str, content, "str == content did not equal true as expected.", state);
171 }
172 BENCHMARK_LOGD("FileTest testLoadStringFromFile003 end.");
173 }
174
175 /*
176 * @tc.name: testLoadStringFromFile004
177 * @tc.desc: Test loading a newly created file with contents
178 */
BENCHMARK_F(BenchmarkFileTest,testLoadStringFromFile004)179 BENCHMARK_F(BenchmarkFileTest, testLoadStringFromFile004)(benchmark::State& state)
180 {
181 BENCHMARK_LOGD("FileTest testLoadStringFromFile004 start.");
182 string filename = FILE_PATH;
183 string content = CONTENT_STR;
184 while (state.KeepRunning()) {
185 string str;
186 CreateTestFile(filename, content);
187 AssertTrue((LoadStringFromFile(filename, str)),
188 "LoadStringFromFile(filename, str) did not equal true as expected.", state);
189 RemoveTestFile(filename);
190 AssertEqual(str, content, "str == content did not equal true as expected.", state);
191 }
192 BENCHMARK_LOGD("FileTest testLoadStringFromFile004 end.");
193 }
194
195 /*
196 * @tc.name: testLoadStringFromFile005
197 * @tc.desc: Test loading a newly created file, whose contents are of maximum length
198 */
BENCHMARK_F(BenchmarkFileTest,testLoadStringFromFile005)199 BENCHMARK_F(BenchmarkFileTest, testLoadStringFromFile005)(benchmark::State& state)
200 {
201 BENCHMARK_LOGD("FileTest testLoadStringFromFile005 start.");
202 string content(MAX_FILE_LENGTH, 't');
203 string filename = FILE_PATH;
204 while (state.KeepRunning()) {
205 string str;
206 CreateTestFile(filename, content);
207 AssertTrue((LoadStringFromFile(filename, str)),
208 "LoadStringFromFile(filename, str) did not equal true as expected.", state);
209 RemoveTestFile(filename);
210 AssertEqual(str, content, "str == content did not equal true as expected.", state);
211 }
212 BENCHMARK_LOGD("FileTest testLoadStringFromFile005 end.");
213 }
214
215 /*
216 * @tc.name: testLoadStringFromFile006
217 * @tc.desc: Test loading a newly created file, whose contents exceeds maximum length
218 */
BENCHMARK_F(BenchmarkFileTest,testLoadStringFromFile006)219 BENCHMARK_F(BenchmarkFileTest, testLoadStringFromFile006)(benchmark::State& state)
220 {
221 BENCHMARK_LOGD("FileTest testLoadStringFromFile006 start.");
222 string content(EXCEEDS_MAXIMUM_LENGTH, 't');
223 string filename = FILE_PATH;
224 while (state.KeepRunning()) {
225 string str;
226 CreateTestFile(filename, content);
227 AssertFalse((LoadStringFromFile(filename, str)),
228 "LoadStringFromFile(filename, str) did not equal false as expected.", state);
229 RemoveTestFile(filename);
230 AssertTrue((str.empty()), "str.empty() did not equal true as expected.", state);
231 }
232 BENCHMARK_LOGD("FileTest testLoadStringFromFile006 end.");
233 }
234
235 /*
236 * @tc.name: testLoadStringFromFd001
237 * @tc.desc: Test loading a file by a invalid fd -1
238 */
BENCHMARK_F(BenchmarkFileTest,testLoadStringFromFd001)239 BENCHMARK_F(BenchmarkFileTest, testLoadStringFromFd001)(benchmark::State& state)
240 {
241 BENCHMARK_LOGD("FileTest testLoadStringFromFd001 start.");
242 while (state.KeepRunning()) {
243 string result;
244 AssertFalse((LoadStringFromFd(-1, result)),
245 "LoadStringFromFd(-1, result) did not equal false as expected.", state);
246 AssertEqual(result, "", "result did not equal "" as expected.", state);
247 }
248 BENCHMARK_LOGD("FileTest testLoadStringFromFd001 end.");
249 }
250
251 /*
252 * @tc.name: testLoadStringFromFd002
253 * @tc.desc: Test loading a newly created file without contents by its fd
254 */
BENCHMARK_F(BenchmarkFileTest,testLoadStringFromFd002)255 BENCHMARK_F(BenchmarkFileTest, testLoadStringFromFd002)(benchmark::State& state)
256 {
257 BENCHMARK_LOGD("FileTest testLoadStringFromFd002 start.");
258 string filename = FILE_PATH;
259 string content = NULL_STR;
260 LoadString(filename, content, state);
261 BENCHMARK_LOGD("FileTest testLoadStringFromFd002 end.");
262 }
263
264 /*
265 * @tc.name: testLoadStringFromFd003
266 * @tc.desc: Test loading a newly created file with contents by its fd
267 */
BENCHMARK_F(BenchmarkFileTest,testLoadStringFromFd003)268 BENCHMARK_F(BenchmarkFileTest, testLoadStringFromFd003)(benchmark::State& state)
269 {
270 BENCHMARK_LOGD("FileTest testLoadStringFromFd003 start.");
271 string filename = FILE_PATH;
272 string content = CONTENT_STR;
273 LoadString(filename, content, state);
274 BENCHMARK_LOGD("FileTest testLoadStringFromFd003 end.");
275 }
276
277 /*
278 * @tc.name: testLoadStringFromFd004
279 * @tc.desc: Test loading a newly created file by fd, whose contents are of maximum length
280 */
BENCHMARK_F(BenchmarkFileTest,testLoadStringFromFd004)281 BENCHMARK_F(BenchmarkFileTest, testLoadStringFromFd004)(benchmark::State& state)
282 {
283 BENCHMARK_LOGD("FileTest testLoadStringFromFd004 start.");
284 string content(MAX_FILE_LENGTH, 't');
285 string filename = FILE_PATH;
286 LoadString(filename, content, state);
287 BENCHMARK_LOGD("FileTest testLoadStringFromFd004 end.");
288 }
289
290 /*
291 * @tc.name: testLoadStringFromFd005
292 * @tc.desc: Test loading a newly created file by fd, whose contents exceeds maximum length
293 */
BENCHMARK_F(BenchmarkFileTest,testLoadStringFromFd005)294 BENCHMARK_F(BenchmarkFileTest, testLoadStringFromFd005)(benchmark::State& state)
295 {
296 BENCHMARK_LOGD("FileTest testLoadStringFromFd005 start.");
297 string content(EXCEEDS_MAXIMUM_LENGTH, 't');
298 string filename = FILE_PATH;
299 while (state.KeepRunning()) {
300 string result;
301 CreateTestFile(filename, content);
302 int fd = open(filename.c_str(), O_RDONLY);
303 AssertFalse((LoadStringFromFd(fd, result)),
304 "LoadStringFromFd(fd, result) did not equal false as expected.", state);
305 close(fd);
306 RemoveTestFile(filename);
307 AssertUnequal(result, content, "result was not different from content as expected.", state);
308 }
309 BENCHMARK_LOGD("FileTest testLoadStringFromFd005 end.");
310 }
311
312 /*
313 * @tc.name: testLoadStringFromFd006
314 * @tc.desc: Test loading a newly created file by fd, which is closed ahead of loading.
315 */
BENCHMARK_F(BenchmarkFileTest,testLoadStringFromFd006)316 BENCHMARK_F(BenchmarkFileTest, testLoadStringFromFd006)(benchmark::State& state)
317 {
318 BENCHMARK_LOGD("FileTest testLoadStringFromFd006 start.");
319 string filename = FILE_PATH;
320 string content = CONTENT_STR;
321 while (state.KeepRunning()) {
322 string result;
323 CreateTestFile(filename, content);
324 int fd = open(filename.c_str(), O_RDONLY);
325 close(fd);
326 AssertFalse((LoadStringFromFd(fd, result)),
327 "LoadStringFromFd(fd, result) did not equal false as expected.", state);
328 RemoveTestFile(filename);
329 AssertEqual(result, "", "result did not equal "" as expected.", state);
330 }
331 BENCHMARK_LOGD("FileTest testLoadStringFromFd006 end.");
332 }
333
334 /*
335 * @tc.name: testSaveStringToFile001
336 * @tc.desc: singleton template
337 */
BENCHMARK_F(BenchmarkFileTest,testSaveStringToFile001)338 BENCHMARK_F(BenchmarkFileTest, testSaveStringToFile001)(benchmark::State& state)
339 {
340 BENCHMARK_LOGD("FileTest testSaveStringToFile001 start.");
341 string path = FILE_PATH;
342 string content = CONTENT_STR;
343 while (state.KeepRunning()) {
344 string newContent;
345 CreateTestFile(path, content);
346 bool ret = SaveStringToFile(path, newContent);
347 AssertEqual(ret, true, "ret did not equal true as expected.", state);
348
349 string loadResult;
350 AssertTrue((LoadStringFromFile(path, loadResult)),
351 "LoadStringFromFile(path, loadResult) did not equal true as expected.", state);
352 RemoveTestFile(path);
353 AssertEqual(loadResult, content, "loadResult did not equal content as expected.", state);
354 }
355 BENCHMARK_LOGD("FileTest testSaveStringToFile001 end.");
356 }
357
358 /*
359 * @tc.name: testSaveStringToFile002
360 * @tc.desc: singleton template
361 */
BENCHMARK_F(BenchmarkFileTest,testSaveStringToFile002)362 BENCHMARK_F(BenchmarkFileTest, testSaveStringToFile002)(benchmark::State& state)
363 {
364 BENCHMARK_LOGD("FileTest testSaveStringToFile002 start.");
365 string path = FILE_PATH;
366 string content = "Before truncated!";
367 while (state.KeepRunning()) {
368 CreateTestFile(path, content);
369
370 string newContent = CONTENT_STR;
371 AssertTrue((SaveStringToFile(path, newContent)),
372 "SaveStringToFile(path, newContent) did not equal true as expected.", state);
373
374 string loadResult;
375 AssertTrue((LoadStringFromFile(path, loadResult)),
376 "LoadStringFromFile(path, loadResult) did not equal true as expected.", state);
377 RemoveTestFile(path);
378 AssertEqual(loadResult, newContent, "loadResult did not equal newContent as expected.", state);
379 }
380 BENCHMARK_LOGD("FileTest testSaveStringToFile002 end.");
381 }
382
383 /*
384 * @tc.name: testSaveStringToFile003
385 * @tc.desc: Test writting an empty string to a file in truncate mode
386 */
BENCHMARK_F(BenchmarkFileTest,testSaveStringToFile003)387 BENCHMARK_F(BenchmarkFileTest, testSaveStringToFile003)(benchmark::State& state)
388 {
389 BENCHMARK_LOGD("FileTest testSaveStringToFile003 start.");
390 string path = FILE_PATH;
391 string content = "Before truncated!";
392 while (state.KeepRunning()) {
393 CreateTestFile(path, content);
394
395 string newContent;
396 bool ret = SaveStringToFile(path, newContent, true);
397 AssertEqual(ret, true, "ret did not equal true as expected.", state);
398
399 string loadResult;
400 ret = LoadStringFromFile(path, loadResult);
401 RemoveTestFile(path);
402 AssertEqual(ret, true, "ret did not equal true as expected.", state);
403 AssertEqual(strcmp(loadResult.c_str(), content.c_str()), 0,
404 "The two strings, loadResult.c_str() and content.c_str(), did not have the same content as expected.",
405 state);
406 }
407 BENCHMARK_LOGD("FileTest testSaveStringToFile003 end.");
408 }
409
410 /*
411 * @tc.name: testSaveStringToFile004
412 * @tc.desc: Test writting an empty string to a file in append mode
413 */
BENCHMARK_F(BenchmarkFileTest,testSaveStringToFile004)414 BENCHMARK_F(BenchmarkFileTest, testSaveStringToFile004)(benchmark::State& state)
415 {
416 BENCHMARK_LOGD("FileTest testSaveStringToFile004 start.");
417 string path = FILE_PATH;
418 string content = "Before appended!";
419 while (state.KeepRunning()) {
420 string newContent;
421 CreateTestFile(path, content);
422 bool ret = SaveStringToFile(path, newContent, false);
423 AssertEqual(ret, true, "ret did not equal true as expected.", state);
424
425 string loadResult;
426 ret = LoadStringFromFile(path, loadResult);
427 RemoveTestFile(path);
428 AssertEqual(ret, true, "ret did not equal true as expected.", state);
429 AssertEqual(strcmp(loadResult.c_str(), content.c_str()), 0,
430 "The two strings, loadResult.c_str() and content.c_str(), did not have the same content as expected.",
431 state);
432 }
433 BENCHMARK_LOGD("FileTest testSaveStringToFile004 end.");
434 }
435
436 /*
437 * @tc.name: testSaveStringToFile005
438 * @tc.desc: Test writting a string to a file in append mode
439 */
BENCHMARK_F(BenchmarkFileTest,testSaveStringToFile005)440 BENCHMARK_F(BenchmarkFileTest, testSaveStringToFile005)(benchmark::State& state)
441 {
442 BENCHMARK_LOGD("FileTest testSaveStringToFile005 start.");
443 string path = FILE_PATH;
444 string content = "Before appended!";
445 while (state.KeepRunning()) {
446 CreateTestFile(path, content);
447
448 string newContent = CONTENT_STR;
449 bool ret = SaveStringToFile(path, newContent, false);
450 AssertEqual(ret, true, "ret did not equal true as expected.", state);
451
452 string loadResult;
453 ret = LoadStringFromFile(path, loadResult);
454 RemoveTestFile(path);
455 AssertEqual(ret, true, "ret did not equal true as expected.", state);
456 AssertEqual(loadResult, content + newContent,
457 "loadResult did not equal content + newContent as expected.", state);
458 }
459 BENCHMARK_LOGD("FileTest testSaveStringToFile005 end.");
460 }
461
462 /*
463 * @tc.name: testSaveStringToFd001
464 * @tc.desc: Test writting an empty string to files with invalid fds
465 */
BENCHMARK_F(BenchmarkFileTest,testSaveStringToFd001)466 BENCHMARK_F(BenchmarkFileTest, testSaveStringToFd001)(benchmark::State& state)
467 {
468 BENCHMARK_LOGD("FileTest testSaveStringToFd001 start.");
469 while (state.KeepRunning()) {
470 string content;
471 bool ret = SaveStringToFd(0, content);
472 AssertEqual(ret, false, "ret did not equal false as expected.", state);
473 ret = SaveStringToFd(-1, content);
474 AssertEqual(ret, false, "ret did not equal false as expected.", state);
475
476 content = CONTENT_STR;
477 ret = SaveStringToFd(0, content);
478 AssertEqual(ret, false, "ret did not equal false as expected.", state);
479 ret = SaveStringToFd(-1, content);
480 AssertEqual(ret, false, "ret did not equal false as expected.", state);
481 }
482 BENCHMARK_LOGD("FileTest testSaveStringToFd001 end.");
483 }
484
485 /*
486 * @tc.name: testSaveStringToFd002
487 * @tc.desc: Test writting an empty string to a file specified by its fd
488 */
BENCHMARK_F(BenchmarkFileTest,testSaveStringToFd002)489 BENCHMARK_F(BenchmarkFileTest, testSaveStringToFd002)(benchmark::State& state)
490 {
491 BENCHMARK_LOGD("FileTest testSaveStringToFd002 start.");
492 string filename = FILE_PATH;
493 string content;
494 SaveString(filename, content, state);
495 BENCHMARK_LOGD("FileTest testSaveStringToFd002 end.");
496 }
497
498 /*
499 * @tc.name: testSaveStringToFd003
500 * @tc.desc: Test loading a non-empty string to a file specified by its fd
501 */
BENCHMARK_F(BenchmarkFileTest,testSaveStringToFd003)502 BENCHMARK_F(BenchmarkFileTest, testSaveStringToFd003)(benchmark::State& state)
503 {
504 BENCHMARK_LOGD("FileTest testSaveStringToFd003 start.");
505 string content = CONTENT_STR;
506 string filename = FILE_PATH;
507 SaveString(filename, content, state);
508 BENCHMARK_LOGD("FileTest testSaveStringToFd003 end.");
509 }
510
511 /*
512 * @tc.name: testSaveStringToFd004
513 * @tc.desc: Test loading a non-empty string to a file without write-authority specified by its fd
514 */
BENCHMARK_F(BenchmarkFileTest,testSaveStringToFd004)515 BENCHMARK_F(BenchmarkFileTest, testSaveStringToFd004)(benchmark::State& state)
516 {
517 BENCHMARK_LOGD("FileTest testSaveStringToFd004 start.");
518 string content = CONTENT_STR;
519 string filename = FILE_PATH;
520 while (state.KeepRunning()) {
521 int fd = open(filename.c_str(), O_RDONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
522 bool ret = SaveStringToFd(fd, content);
523 close(fd);
524 AssertEqual(ret, false, "ret did not equal false as expected.", state);
525
526 string loadResult;
527 fd = open(filename.c_str(), O_RDONLY);
528 ret = LoadStringFromFd(fd, loadResult);
529 close(fd);
530 RemoveTestFile(filename);
531 AssertEqual(ret, true, "ret did not equal true as expected.", state);
532 AssertEqual(loadResult, "", "loadResult did not equal "" as expected.", state);
533 }
534 BENCHMARK_LOGD("FileTest testSaveStringToFd004 end.");
535 }
536
537 /*
538 * @tc.name: testLoadBufferFromFile001
539 * @tc.desc: singleton template
540 */
BENCHMARK_F(BenchmarkFileTest,testLoadBufferFromFile001)541 BENCHMARK_F(BenchmarkFileTest, testLoadBufferFromFile001)(benchmark::State& state)
542 {
543 BENCHMARK_LOGD("FileTest testLoadBufferFromFile001 start.");
544 string filename = "";
545 while (state.KeepRunning()) {
546 vector<char> buff;
547 bool ret = LoadBufferFromFile(filename, buff);
548 AssertFalse((ret), "ret did not equal false as expected.", state);
549 AssertEqual(0, static_cast<int>(buff.size()),
550 "static_cast<int>(buff.size()) did not equal 0 as expected.", state);
551 }
552 BENCHMARK_LOGD("FileTest testLoadBufferFromFile001 end.");
553 }
554
555 /*
556 * @tc.name: testLoadBufferFromFile002
557 * @tc.desc: singleton template
558 */
BENCHMARK_F(BenchmarkFileTest,testLoadBufferFromFile002)559 BENCHMARK_F(BenchmarkFileTest, testLoadBufferFromFile002)(benchmark::State& state)
560 {
561 BENCHMARK_LOGD("FileTest testLoadBufferFromFile002 start.");
562 string filename = FILE_PATH;
563 while (state.KeepRunning()) {
564 vector<char> buff;
565 string content;
566 CreateTestFile(filename, content);
567 bool ret = LoadBufferFromFile(filename, buff);
568 RemoveTestFile(filename);
569 AssertTrue((ret), "ret did not equal true as expected.", state);
570 AssertEqual(0, static_cast<int>(buff.size()),
571 "static_cast<int>(buff.size()) did not equal 0 as expected.", state);
572 }
573 BENCHMARK_LOGD("FileTest testLoadBufferFromFile002 end.");
574 }
575
576 /*
577 * @tc.name: testLoadBufferFromFile003
578 * @tc.desc: singleton template
579 */
BENCHMARK_F(BenchmarkFileTest,testLoadBufferFromFile003)580 BENCHMARK_F(BenchmarkFileTest, testLoadBufferFromFile003)(benchmark::State& state)
581 {
582 BENCHMARK_LOGD("FileTest testLoadBufferFromFile003 start.");
583 string filename = FILE_PATH;
584 string content = "TXB";
585 while (state.KeepRunning()) {
586 vector<char> buff;
587 CreateTestFile(filename, content);
588 bool ret = LoadBufferFromFile(filename, buff);
589 RemoveTestFile(filename);
590 AssertTrue((ret), "ret did not equal true as expected.", state);
591 AssertEqual(3, (int)buff.size(), "3 did not equal (int)buff.size() as expected.", state);
592 AssertEqual('T', buff[0], "'T' did not equal buff[0] as expected.", state);
593 AssertEqual('X', buff[1], "'X' did not equal buff[1] as expected.", state);
594 AssertEqual('B', buff[2], "'B' did not equal buff[2] as expected.", state);
595 }
596 BENCHMARK_LOGD("FileTest testLoadBufferFromFile003 end.");
597 }
598
599 /*
600 * @tc.name: testLoadBufferFromFile004
601 * @tc.desc: singleton template
602 */
BENCHMARK_F(BenchmarkFileTest,testLoadBufferFromFile004)603 BENCHMARK_F(BenchmarkFileTest, testLoadBufferFromFile004)(benchmark::State& state)
604 {
605 BENCHMARK_LOGD("FileTest testLoadBufferFromFile004 start.");
606 string content(EXCEEDS_MAXIMUM_LENGTH, 't');
607 string filename = FILE_PATH;
608 while (state.KeepRunning()) {
609 vector<char> buff;
610 CreateTestFile(filename, content);
611 bool ret = LoadBufferFromFile(filename, buff);
612 RemoveTestFile(filename);
613 AssertEqual(ret, false, "ret did not equal false as expected.", state);
614 AssertEqual(0, static_cast<int>(buff.size()),
615 "static_cast<int>(buff.size()) did not equal 0 as expected.", state);
616 }
617 BENCHMARK_LOGD("FileTest testLoadBufferFromFile004 end.");
618 }
619
620 /*
621 * @tc.name: testSaveBufferToFile001
622 * @tc.desc: singleton template
623 */
BENCHMARK_F(BenchmarkFileTest,testSaveBufferToFile001)624 BENCHMARK_F(BenchmarkFileTest, testSaveBufferToFile001)(benchmark::State& state)
625 {
626 BENCHMARK_LOGD("FileTest testSaveBufferToFile001 start.");
627 string path = FILE_PATH;
628 string content = "ttxx";
629 while (state.KeepRunning()) {
630 vector<char> buff;
631 CreateTestFile(path, content);
632 bool ret = SaveBufferToFile(path, buff, false);
633 AssertEqual(ret, true, "ret did not equal true as expected.", state);
634
635 string loadResult;
636 ret = LoadStringFromFile(path, loadResult);
637 RemoveTestFile(path);
638 AssertEqual(ret, true, "ret did not equal true as expected.", state);
639 AssertEqual(loadResult, content, "loadResult did not equal content as expected.", state);
640 }
641 BENCHMARK_LOGD("FileTest testSaveBufferToFile001 end.");
642 }
643
644 /*
645 * @tc.name: testSaveBufferToFile002
646 * @tc.desc: singleton template
647 */
BENCHMARK_F(BenchmarkFileTest,testSaveBufferToFile002)648 BENCHMARK_F(BenchmarkFileTest, testSaveBufferToFile002)(benchmark::State& state)
649 {
650 BENCHMARK_LOGD("FileTest testSaveBufferToFile002 start.");
651 string path = FILE_PATH;
652 string content = "ttxx";
653 while (state.KeepRunning()) {
654 CreateTestFile(path, content);
655
656 vector<char> newContent = {'x', 'x', 't', 't'};
657 bool ret = SaveBufferToFile(path, newContent);
658 AssertEqual(ret, true, "ret did not equal true as expected.", state);
659
660 string loadResult;
661 ret = LoadStringFromFile(path, loadResult);
662 RemoveTestFile(path);
663 AssertEqual(ret, true, "ret did not equal true as expected.", state);
664 AssertEqual(loadResult, std::string(newContent.begin(), newContent.end()),
665 "loadResult did not equal std::string(newContent.begin(), newContent.end()) as expected.", state);
666 }
667 BENCHMARK_LOGD("FileTest testSaveBufferToFile002 end.");
668 }
669
670 /*
671 * @tc.name: testSaveBufferToFile003
672 * @tc.desc: singleton template
673 */
BENCHMARK_F(BenchmarkFileTest,testSaveBufferToFile003)674 BENCHMARK_F(BenchmarkFileTest, testSaveBufferToFile003)(benchmark::State& state)
675 {
676 BENCHMARK_LOGD("FileTest testSaveBufferToFile003 start.");
677 string path = FILE_PATH;
678 string content = "ttxx";
679 while (state.KeepRunning()) {
680 CreateTestFile(path, content);
681
682 vector<char> newContent = {'x', 'x', 't', 't'};
683 bool ret = SaveBufferToFile(path, newContent, false);
684 AssertEqual(ret, true, "ret did not equal true as expected.", state);
685
686 string loadResult;
687 ret = LoadStringFromFile(path, loadResult);
688 RemoveTestFile(path);
689 AssertEqual(ret, true, "ret did not equal true as expected.", state);
690 AssertEqual(loadResult, content + std::string(newContent.begin(), newContent.end()),
691 "loadResult did not equal content + std::string(newContent.begin(), newContent.end()).", state);
692 }
693 BENCHMARK_LOGD("FileTest testSaveBufferToFile003 end.");
694 }
695
696 /*
697 * @tc.name: testStringExistsInFile001
698 * @tc.desc: singleton template
699 */
BENCHMARK_F(BenchmarkFileTest,testStringExistsInFile001)700 BENCHMARK_F(BenchmarkFileTest, testStringExistsInFile001)(benchmark::State& state)
701 {
702 BENCHMARK_LOGD("FileTest testStringExistsInFile001 start.");
703 string str = "abc";
704 string filename = "";
705 while (state.KeepRunning()) {
706 AssertFalse((StringExistsInFile(filename, str, true)),
707 "StringExistsInFile(filename, str, true) did not equal false as expected.", state);
708 AssertFalse((str.empty()), "str.empty() did not equal false as expected.", state);
709 }
710 BENCHMARK_LOGD("FileTest testStringExistsInFile001 end.");
711 }
712
713 /*
714 * @tc.name: testStringExistsInFile002
715 * @tc.desc: singleton template
716 */
BENCHMARK_F(BenchmarkFileTest,testStringExistsInFile002)717 BENCHMARK_F(BenchmarkFileTest, testStringExistsInFile002)(benchmark::State& state)
718 {
719 BENCHMARK_LOGD("FileTest testStringExistsInFile002 start.");
720 string str = NULL_STR;
721 string filename = FILE_PATH;
722 string content = "hello world!";
723 while (state.KeepRunning()) {
724 CreateTestFile(filename, content);
725 AssertFalse((StringExistsInFile(filename, str, true)),
726 "StringExistsInFile(filename, str, true) did not equal false as expected.", state);
727 RemoveTestFile(filename);
728 }
729 BENCHMARK_LOGD("FileTest testStringExistsInFile002 end.");
730 }
731
732 /*
733 * @tc.name: testStringExistsInFile003
734 * @tc.desc: singleton template
735 */
BENCHMARK_F(BenchmarkFileTest,testStringExistsInFile003)736 BENCHMARK_F(BenchmarkFileTest, testStringExistsInFile003)(benchmark::State& state)
737 {
738 BENCHMARK_LOGD("FileTest testStringExistsInFile003 start.");
739 string str = "world";
740 string filename = FILE_PATH;
741 string content = "hello world!";
742 while (state.KeepRunning()) {
743 CreateTestFile(filename, content);
744 AssertTrue((StringExistsInFile(filename, str, true)),
745 "StringExistsInFile(filename, str, true) did not equal true as expected.", state);
746 RemoveTestFile(filename);
747 }
748 BENCHMARK_LOGD("FileTest testStringExistsInFile003 end.");
749 }
750
751 /*
752 * @tc.name: testStringExistsInFile004
753 * @tc.desc: singleton template
754 */
BENCHMARK_F(BenchmarkFileTest,testStringExistsInFile004)755 BENCHMARK_F(BenchmarkFileTest, testStringExistsInFile004)(benchmark::State& state)
756 {
757 BENCHMARK_LOGD("FileTest testStringExistsInFile004 start.");
758 string str1(MAX_FILE_LENGTH + 1, 't');
759 string str2(MAX_FILE_LENGTH, 't');
760 string content(MAX_FILE_LENGTH, 't');
761 string filename = FILE_PATH;
762 while (state.KeepRunning()) {
763 CreateTestFile(filename, content);
764 AssertFalse((StringExistsInFile(filename, str1, true)),
765 "StringExistsInFile(filename, str1, true) did not equal false as expected.", state);
766 AssertTrue((StringExistsInFile(filename, str2, true)),
767 "StringExistsInFile(filename, str2, true) did not equal true as expected.", state);
768 RemoveTestFile(filename);
769 }
770 BENCHMARK_LOGD("FileTest testStringExistsInFile004 end.");
771 }
772
773 /*
774 * @tc.name: testStringExistsInFile005
775 * @tc.desc: singleton template
776 */
BENCHMARK_F(BenchmarkFileTest,testStringExistsInFile005)777 BENCHMARK_F(BenchmarkFileTest, testStringExistsInFile005)(benchmark::State& state)
778 {
779 BENCHMARK_LOGD("FileTest testStringExistsInFile005 start.");
780 string str = "woRld";
781 string filename = FILE_PATH;
782 string content = "hello world!";
783 while (state.KeepRunning()) {
784 CreateTestFile(filename, content);
785 AssertTrue((StringExistsInFile(filename, str, false)),
786 "StringExistsInFile(filename, str, false) did not equal true as expected.", state);
787 AssertFalse((StringExistsInFile(filename, str, true)),
788 "StringExistsInFile(filename, str, true) did not equal false as expected.", state);
789 RemoveTestFile(filename);
790 }
791 BENCHMARK_LOGD("FileTest testStringExistsInFile005 end.");
792 }
793
794 /*
795 * @tc.name: testStringExistsInFile006
796 * @tc.desc: singleton template
797 */
BENCHMARK_F(BenchmarkFileTest,testStringExistsInFile006)798 BENCHMARK_F(BenchmarkFileTest, testStringExistsInFile006)(benchmark::State& state)
799 {
800 BENCHMARK_LOGD("FileTest testStringExistsInFile006 start.");
801 string str1 = "woRld!";
802 string str2 = "123";
803 string str3 = "llo ";
804 string str4 = "123 w";
805 string str5 = "hi";
806 string filename = FILE_PATH;
807 string content = "Test, hello 123 World!";
808 while (state.KeepRunning()) {
809 CreateTestFile(filename, content);
810 AssertTrue((StringExistsInFile(filename, str1, false)),
811 "StringExistsInFile(filename, str1, false) did not equal true as expected.", state);
812 AssertFalse((StringExistsInFile(filename, str1, true)),
813 "StringExistsInFile(filename, str1, true) did not equal false as expected.", state);
814
815 AssertTrue((StringExistsInFile(filename, str2, false)),
816 "StringExistsInFile(filename, str2, false) did not equal true as expected.", state);
817 AssertTrue((StringExistsInFile(filename, str2, true)),
818 "StringExistsInFile(filename, str2, true) did not equal true as expected.", state);
819
820 AssertTrue((StringExistsInFile(filename, str3, false)),
821 "StringExistsInFile(filename, str3, false) did not equal true as expected.", state);
822 AssertTrue((StringExistsInFile(filename, str3, true)),
823 "StringExistsInFile(filename, str3, true) did not equal true as expected.", state);
824
825 AssertTrue((StringExistsInFile(filename, str4, false)),
826 "StringExistsInFile(filename, str4, false) did not equal true as expected.", state);
827 AssertFalse((StringExistsInFile(filename, str4, true)),
828 "StringExistsInFile(filename, str4, true) did not equal false as expected.", state);
829
830 AssertFalse((StringExistsInFile(filename, str5, false)),
831 "StringExistsInFile(filename, str5, false) did not equal false as expected.", state);
832 AssertFalse((StringExistsInFile(filename, str5, true)),
833 "StringExistsInFile(filename, str5, true) did not equal false as expected.", state);
834 RemoveTestFile(filename);
835 }
836 BENCHMARK_LOGD("FileTest testStringExistsInFile006 end.");
837 }
838
839 /*
840 * @tc.name: testStringExistsInFile007
841 * @tc.desc: singleton template
842 */
BENCHMARK_F(BenchmarkFileTest,testStringExistsInFile007)843 BENCHMARK_F(BenchmarkFileTest, testStringExistsInFile007)(benchmark::State& state)
844 {
845 BENCHMARK_LOGD("FileTest testStringExistsInFile007 start.");
846 string str1 = "is";
847 string str2 = "\n\ris";
848 string filename = FILE_PATH;
849 string content = "Test, special string\n\ris ok";
850 while (state.KeepRunning()) {
851 CreateTestFile(filename, content);
852 AssertTrue((StringExistsInFile(filename, str1, false)),
853 "StringExistsInFile(filename, str1, false) did not equal true as expected.", state);
854 AssertTrue((StringExistsInFile(filename, str1, true)),
855 "StringExistsInFile(filename, str1, true) did not equal true as expected.", state);
856
857 AssertTrue((StringExistsInFile(filename, str2, false)),
858 "StringExistsInFile(filename, str2, false) did not equal true as expected.", state);
859 AssertTrue((StringExistsInFile(filename, str2, true)),
860 "StringExistsInFile(filename, str2, true) did not equal true as expected.", state);
861 RemoveTestFile(filename);
862 }
863 BENCHMARK_LOGD("FileTest testStringExistsInFile007 end.");
864 }
865
866 /*
867 * @tc.name: testFileExist001
868 * @tc.desc: singleton template
869 */
BENCHMARK_F(BenchmarkFileTest,testFileExist001)870 BENCHMARK_F(BenchmarkFileTest, testFileExist001)(benchmark::State& state)
871 {
872 BENCHMARK_LOGD("FileTest testFileExist001 start.");
873 string filepath = "/proc/meminfo";
874 string filepath1 = "/proc/meminfo1";
875 while (state.KeepRunning()) {
876 AssertTrue((FileExists(filepath)), "FileExists(filepath) did not equal true as expected.", state);
877 AssertFalse((FileExists(filepath1)), "FileExists(filepath1) did not equal false as expected.", state);
878 }
879 BENCHMARK_LOGD("FileTest testFileExist001 end.");
880 }
881
882 /*
883 * @tc.name: testCountStrInFile001
884 * @tc.desc: singleton template
885 */
BENCHMARK_F(BenchmarkFileTest,testCountStrInFile001)886 BENCHMARK_F(BenchmarkFileTest, testCountStrInFile001)(benchmark::State& state)
887 {
888 BENCHMARK_LOGD("FileTest testCountStrInFile001 start.");
889 string str = "abc";
890 string filename = "";
891 while (state.KeepRunning()) {
892 AssertEqual(CountStrInFile(filename, str, true), -1,
893 "CountStrInFile(filename, str, true) did not equal -1 as expected.", state);
894 AssertFalse((str.empty()), "str.empty() did not equal false as expected.", state);
895 }
896 BENCHMARK_LOGD("FileTest testCountStrInFile001 end.");
897 }
898
899 /*
900 * @tc.name: testCountStrInFile002
901 * @tc.desc: singleton template
902 */
BENCHMARK_F(BenchmarkFileTest,testCountStrInFile002)903 BENCHMARK_F(BenchmarkFileTest, testCountStrInFile002)(benchmark::State& state)
904 {
905 BENCHMARK_LOGD("FileTest testCountStrInFile002 start.");
906 string str = NULL_STR;
907 string filename = FILE_PATH;
908 string content = "hello world!";
909 while (state.KeepRunning()) {
910 CreateTestFile(filename, content);
911 AssertEqual(CountStrInFile(filename, str, true), -1,
912 "CountStrInFile(filename, str, true) did not equal -1 as expected.", state);
913 RemoveTestFile(filename);
914 }
915 BENCHMARK_LOGD("FileTest testCountStrInFile002 end.");
916 }
917
918 /*
919 * @tc.name: testCountStrInFile003
920 * @tc.desc: singleton template
921 */
BENCHMARK_F(BenchmarkFileTest,testCountStrInFile003)922 BENCHMARK_F(BenchmarkFileTest, testCountStrInFile003)(benchmark::State& state)
923 {
924 BENCHMARK_LOGD("FileTest testCountStrInFile003 start.");
925 string str1(MAX_FILE_LENGTH + 1, 't');
926 string str2(MAX_FILE_LENGTH, 't');
927 string content(MAX_FILE_LENGTH, 't');
928 string filename = FILE_PATH;
929 while (state.KeepRunning()) {
930 CreateTestFile(filename, content);
931 AssertEqual(CountStrInFile(filename, str1, true), 0,
932 "CountStrInFile(filename, str1, true) did not equal 0 as expected.", state);
933 AssertEqual(CountStrInFile(filename, str2, true), 1,
934 "CountStrInFile(filename, str2, true) did not equal 1 as expected.", state);
935 RemoveTestFile(filename);
936 }
937 BENCHMARK_LOGD("FileTest testCountStrInFile003 end.");
938 }
939
940 /*
941 * @tc.name: testCountStrInFile004
942 * @tc.desc: singleton template
943 */
BENCHMARK_F(BenchmarkFileTest,testCountStrInFile004)944 BENCHMARK_F(BenchmarkFileTest, testCountStrInFile004)(benchmark::State& state)
945 {
946 BENCHMARK_LOGD("FileTest testCountStrInFile004 start.");
947 string str1 = "very";
948 string str2 = "VERY";
949 string str3 = "abc";
950 string filename = FILE_PATH;
951 string content = "This is very very long string for test.\n Very Good,\r VERY HAPPY.";
952 while (state.KeepRunning()) {
953 CreateTestFile(filename, content);
954 AssertEqual(CountStrInFile(filename, str1, true), 2,
955 "CountStrInFile(filename, str1, true) did not equal 2 as expected.", state);
956 AssertEqual(CountStrInFile(filename, str1, false), 4,
957 "CountStrInFile(filename, str1, false) did not equal 4 as expected.", state);
958
959 AssertEqual(CountStrInFile(filename, str2, true), 1,
960 "CountStrInFile(filename, str2, true) did not equal 1 as expected.", state);
961 AssertEqual(CountStrInFile(filename, str2, false), 4,
962 "CountStrInFile(filename, str2, false) did not equal 4 as expected.", state);
963
964 AssertEqual(CountStrInFile(filename, str3, true), 0,
965 "CountStrInFile(filename, str3, true) did not equal 0 as expected.", state);
966 RemoveTestFile(filename);
967 }
968 BENCHMARK_LOGD("FileTest testCountStrInFile004 end.");
969 }
970
971 /*
972 * @tc.name: testCountStrInFile005
973 * @tc.desc: singleton template
974 */
BENCHMARK_F(BenchmarkFileTest,testCountStrInFile005)975 BENCHMARK_F(BenchmarkFileTest, testCountStrInFile005)(benchmark::State& state)
976 {
977 BENCHMARK_LOGD("FileTest testCountStrInFile005 start.");
978 string str1 = "aba";
979 string filename = FILE_PATH;
980 string content = "This is abababaBABa.";
981 while (state.KeepRunning()) {
982 CreateTestFile(filename, content);
983 AssertEqual(CountStrInFile(filename, str1, true), 2,
984 "CountStrInFile(filename, str1, true) did not equal 2 as expected.", state);
985 AssertEqual(CountStrInFile(filename, str1, false), 3,
986 "CountStrInFile(filename, str1, false) did not equal 3 as expected.", state);
987 RemoveTestFile(filename);
988 }
989 BENCHMARK_LOGD("FileTest testCountStrInFile005 end.");
990 }
991 } // namespace
992 } // namespace OHOS
993 // Run the benchmark
994 BENCHMARK_MAIN();