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 <fstream>
19 #include <iostream>
20 #include "directory_ex.h"
21 #include "parcel.h"
22 #include "refbase.h"
23 #include "securec.h"
24 #include "benchmark_log.h"
25 #include "benchmark_assert.h"
26 using namespace std;
27 
28 namespace OHOS {
29 namespace {
30 
31 static constexpr size_t DEFAULT_CPACITY = 204800; // 200K
32 static constexpr size_t CAPACITY_THRESHOLD = 4096; // 4k
33 static constexpr int32_t COMPARE_STRING_RESULT = 0;
34 static constexpr size_t DATA_CAPACITY_INIT_SIZE = 0;
35 static constexpr size_t DATA_OFFSETS_INIT_SIZE = 0;
36 static constexpr size_t DATA_READBYTES_INIT_SIZE = 0;
37 static constexpr size_t REWIND_INIT_VALUE = 0;
38 static constexpr size_t REWINDWRITE003_VECTOR_LENGTH = 5;
39 static constexpr int32_t WRITE_AND_CMP_INT32_VALUE = 5;
40 
41 #define PARCEL_TEST_CHAR_ARRAY_SIZE 48
42 #define PARCEL_TEST1_CHAR_ARRAY_SIZE 205780
43 #define PARCEL_WRITEPOINTER_CHAR_ARRAY_SIZE 128
44 #define PARCEL_INJECTOFFSETS_CHAR_ARRAY_SIZE 256
45 
46 class BenchmarkParcelTest : public benchmark::Fixture {
47 public:
SetUp(const::benchmark::State & state)48     void SetUp(const ::benchmark::State& state) override
49     {
50     }
51 
TearDown(const::benchmark::State & state)52     void TearDown(const ::benchmark::State& state) override
53     {
54     }
55 
BenchmarkParcelTest()56     BenchmarkParcelTest()
57     {
58         Iterations(iterations);
59         Repetitions(repetitions);
60         ReportAggregatesOnly();
61     }
62 
63     ~BenchmarkParcelTest() override = default;
64 
65 protected:
66     const int32_t repetitions = 3;
67     const int32_t iterations = 1000;
68 };
69 
70 
71 class RemoteObject : public virtual Parcelable {
72 public:
RemoteObject()73     RemoteObject()
74     {
75         asRemote_ = true;
76     }
77     bool Marshalling(Parcel &parcel) const override;
78     static sptr<RemoteObject> Unmarshalling(Parcel &parcel);
79     static bool Marshalling(Parcel &parcel, const sptr<RemoteObject> &object);
80 };
81 
Marshalling(Parcel & parcel,const sptr<RemoteObject> & object)82 bool RemoteObject::Marshalling(Parcel &parcel, const sptr<RemoteObject> &object)
83 {
84     return false;
85 }
86 
Marshalling(Parcel & parcel) const87 bool RemoteObject::Marshalling(Parcel &parcel) const
88 {
89     BENCHMARK_LOGD("ParcelTest (bool RemoteObject::Marshalling(Parcel &parcel) const) is called.");
90     parcel_flat_binder_object flat;
91     flat.hdr.type = 0xff;
92     flat.flags = 0x7f;
93     flat.binder = 0;
94     flat.handle = (uint32_t)(-1);
95     flat.cookie = reinterpret_cast<uintptr_t>(this);
96     bool status = parcel.WriteBuffer(&flat, sizeof(parcel_flat_binder_object));
97     if (!status) {
98         return false;
99     }
100     return true;
101 }
102 
Unmarshalling(Parcel & parcel)103 sptr<RemoteObject> RemoteObject::Unmarshalling(Parcel &parcel)
104 {
105     BENCHMARK_LOGD("ParcelTest sptr<RemoteObject> RemoteObject::Unmarshalling(Parcel &parcel) is called.");
106     const uint8_t *buffer = parcel.ReadBuffer(sizeof(parcel_flat_binder_object), false);
107     if (buffer == nullptr) {
108         return nullptr;
109     }
110     sptr<RemoteObject> obj = new RemoteObject();
111     return obj;
112 }
113 
114 /*-------------------------------base data------------------------------------*/
115 
116 struct TestData {
117     bool booltest;
118     int8_t int8test;
119     int16_t int16test;
120     int32_t int32test;
121     uint8_t uint8test;
122     uint16_t uint16test;
123     uint32_t uint32test;
124 };
125 
WriteTestData(Parcel & parcel,const struct TestData & data,benchmark::State & state)126 void WriteTestData(Parcel &parcel, const struct TestData &data, benchmark::State& state)
127 {
128     BENCHMARK_LOGD("ParcelTest void WriteTestData is called.");
129     bool result = false;
130     result = parcel.WriteBool(data.booltest);
131     AssertEqual(result, true, "result did not equal true as expected.", state);
132 
133     result = parcel.WriteInt8(data.int8test);
134     AssertEqual(result, true, "result did not equal true as expected.", state);
135 
136     result = parcel.WriteInt16(data.int16test);
137     AssertEqual(result, true, "result did not equal true as expected.", state);
138 
139     result = parcel.WriteInt32(data.int32test);
140     AssertEqual(result, true, "result did not equal true as expected.", state);
141 
142     result = parcel.WriteUint8(data.uint8test);
143     AssertEqual(result, true, "result did not equal true as expected.", state);
144 
145     result = parcel.WriteUint16(data.uint16test);
146     AssertEqual(result, true, "result did not equal true as expected.", state);
147 
148     result = parcel.WriteUint32(data.uint32test);
149     AssertEqual(result, true, "result did not equal true as expected.", state);
150 }
151 
WriteUnalignedTestData(Parcel & parcel,const struct TestData & data,benchmark::State & state)152 void WriteUnalignedTestData(Parcel &parcel, const struct TestData &data, benchmark::State& state)
153 {
154     BENCHMARK_LOGD("ParcelTest void WriteUnalignedTestData is called.");
155     bool result = false;
156     result = parcel.WriteBoolUnaligned(data.booltest);
157     AssertEqual(result, true, "result did not equal true as expected.", state);
158 
159     result = parcel.WriteInt8Unaligned(data.int8test);
160     AssertEqual(result, true, "result did not equal true as expected.", state);
161 
162     result = parcel.WriteInt16Unaligned(data.int16test);
163     AssertEqual(result, true, "result did not equal true as expected.", state);
164 
165     result = parcel.WriteUint8Unaligned(data.uint8test);
166     AssertEqual(result, true, "result did not equal true as expected.", state);
167 
168     result = parcel.WriteUint16Unaligned(data.uint16test);
169     AssertEqual(result, true, "result did not equal true as expected.", state);
170 }
171 
ReadTestData(Parcel & parcel,const struct TestData & data,benchmark::State & state)172 void ReadTestData(Parcel &parcel, const struct TestData &data, benchmark::State& state)
173 {
174     BENCHMARK_LOGD("ParcelTest void ReadTestData is called.");
175     bool readbool = parcel.ReadBool();
176     AssertEqual(readbool, data.booltest, "readbool did not equal data.booltest as expected.", state);
177 
178     int8_t readint8 = parcel.ReadInt8();
179     AssertEqual(readint8, data.int8test, "readint8 did not equal data.int8test as expected.", state);
180 
181     int16_t readint16 = parcel.ReadInt16();
182     AssertEqual(readint16, data.int16test, "readint16 did not equal data.int16test as expected.", state);
183 
184     int32_t readint32 = parcel.ReadInt32();
185     AssertEqual(readint32, data.int32test, "readint32 did not equal data.int32test as expected.", state);
186 
187     uint8_t readuint8 = parcel.ReadUint8();
188     AssertEqual(readuint8, data.uint8test, "readuint8 did not equal data.uint8test as expected.", state);
189 
190     uint16_t readuint16 = parcel.ReadUint16();
191     AssertEqual(readuint16, data.uint16test, "readuint16 did not equal data.uint16test as expected.", state);
192 
193     uint32_t readuint32 = parcel.ReadUint32();
194     AssertEqual(readuint32, data.uint32test, "readuint32 did not equal data.uint32test as expected.", state);
195 }
196 
ReadUnalignedTestData(Parcel & parcel,const struct TestData & data,benchmark::State & state)197 void ReadUnalignedTestData(Parcel &parcel, const struct TestData &data, benchmark::State& state)
198 {
199     BENCHMARK_LOGD("ParcelTest void ReadUnalignedTestData is called.");
200     bool readbool = parcel.ReadBoolUnaligned();
201     AssertEqual(readbool, data.booltest, "readbool did not equal data.booltest as expected.", state);
202 
203     int8_t readint8;
204     AssertTrue(parcel.ReadInt8Unaligned(readint8),
205         "parcel.ReadInt8Unaligned(readint8) did not equal true as expected.", state);
206     AssertEqual(readint8, data.int8test, "readint8 did not equal data.int8test as expected.", state);
207 
208     int16_t readint16;
209     AssertTrue(parcel.ReadInt16Unaligned(readint16),
210         "parcel.ReadInt16Unaligned(readint16) did not equal true as expected.", state);
211     AssertEqual(readint16, data.int16test, "readint16 did not equal data.int16test as expected.", state);
212 
213     uint8_t readuint8;
214     AssertTrue(parcel.ReadUint8Unaligned(readuint8),
215         "parcel.ReadUint8Unaligned(readuint8) did not equal true as expected.", state);
216     AssertEqual(readuint8, data.uint8test, "readuint8 did not equal data.uint8test as expected.", state);
217 
218     uint16_t readuint16;
219     AssertTrue(parcel.ReadUint16Unaligned(readuint16),
220         "parcel.ReadUint16Unaligned(readuint16) did not equal true as expected.", state);
221     AssertEqual(readuint16, data.uint16test, "readuint16 did not equal data.uint16test as expected.", state);
222 }
223 
ReadTestDataWithTarget(Parcel & parcel,const struct TestData & data,benchmark::State & state)224 void ReadTestDataWithTarget(Parcel &parcel, const struct TestData &data, benchmark::State& state)
225 {
226     BENCHMARK_LOGD("ParcelTest void ReadTestDataWithTarget is called.");
227     bool result = false;
228     bool boolVal = true;
229     result = parcel.ReadBool(boolVal);
230     AssertEqual(result, true, "result did not equal true as expected.", state);
231     AssertEqual(boolVal, data.booltest, "boolVal did not equal data.booltest as expected.", state);
232 
233     int8_t int8Val;
234     result = parcel.ReadInt8(int8Val);
235     AssertEqual(result, true, "result did not equal true as expected.", state);
236     AssertEqual(int8Val, data.int8test, "int8Val did not equal data.int8test as expected.", state);
237 
238     int16_t int16Val;
239     result = parcel.ReadInt16(int16Val);
240     AssertEqual(result, true, "result did not equal true as expected.", state);
241     AssertEqual(int16Val, data.int16test, "int16Val did not equal data.int16test as expected.", state);
242 
243     int32_t int32Val;
244     result = parcel.ReadInt32(int32Val);
245     AssertEqual(result, true, "result did not equal true as expected.", state);
246     AssertEqual(int32Val, data.int32test, "int32Val did not equal data.int32test as expected.", state);
247 
248     uint8_t uint8Val;
249     result = parcel.ReadUint8(uint8Val);
250     AssertEqual(result, true, "result did not equal true as expected.", state);
251     AssertEqual(uint8Val, data.uint8test, "uint8Val did not equal data.uint8test as expected.", state);
252 
253     uint16_t uint16Val;
254     result = parcel.ReadUint16(uint16Val);
255     AssertEqual(result, true, "result did not equal true as expected.", state);
256     AssertEqual(uint16Val, data.uint16test, "uint16Val did not equal data.uint16test as expected.", state);
257 
258     uint32_t uint32Val;
259     result = parcel.ReadUint32(uint32Val);
260     AssertEqual(result, true, "result did not equal true as expected.", state);
261     AssertEqual(uint32Val, data.uint32test, "uint32Val did not equal data.uint32test as expected.", state);
262 }
263 
264 /**
265  * Here to simulate the scenario of ipc sending data,
266  * the buffer will be released when the Parcel object is destructed.
267  */
SendData(void * & buffer,size_t size,const uint8_t * data)268 bool SendData(void *&buffer, size_t size, const uint8_t *data)
269 {
270     BENCHMARK_LOGD("ParcelTest bool SendData is called.");
271     const size_t parcelMinSizeValue = 1;
272     if (size < parcelMinSizeValue) {
273         return false;
274     }
275 
276     buffer = malloc(size);
277     if (buffer == nullptr) {
278         return false;
279     }
280 
281     if (memcpy_s(buffer, size, data, size) != EOK) {
282         return false;
283     }
284     return true;
285 }
286 
CopyOldParcelToNewParcel(Parcel & oldParcel,Parcel & newParcel,benchmark::State & state)287 bool CopyOldParcelToNewParcel(Parcel &oldParcel, Parcel &newParcel, benchmark::State& state)
288 {
289     void *buffer = nullptr;
290     size_t size = oldParcel.GetDataSize();
291 
292     AssertTrue(SendData(buffer, size, reinterpret_cast<const uint8_t *>(oldParcel.GetData())),
293         "SendData(buffer, size, reinterpret_cast<const uint8_t *>(oldParcel.GetData())) \
294         did not equal true as expected.", state);
295 
296     bool result = newParcel.ParseFrom(reinterpret_cast<uintptr_t>(buffer), size);
297     AssertEqual(result, true, "result did not equal true as expected.", state);
298 
299     return true;
300 }
301 
302 /**
303  * @tc.name: test_parcel_001
304  * @tc.desc: test parcel CheckOffsets, WriteRemoteObject, RewindRead and
305  * RewindWrite failed.
306  * @tc.type: FUNC
307  */
BENCHMARK_F(BenchmarkParcelTest,test_parcel_001)308 BENCHMARK_F(BenchmarkParcelTest, test_parcel_001)(benchmark::State& state)
309 {
310     BENCHMARK_LOGD("ParcelTest test_parcel_001 start.");
311     while (state.KeepRunning()) {
312         Parcel parcel;
313         bool result = parcel.CheckOffsets();
314         AssertEqual(result, false, "result did not equal false as expected.", state);
315 
316         result = parcel.WriteRemoteObject(nullptr);
317         AssertEqual(result, false, "result did not equal false as expected.", state);
318 
319         size_t rewindReadPos = parcel.GetDataSize() + 1;
320         result = parcel.RewindRead(rewindReadPos);
321         AssertEqual(result, false, "result did not equal false as expected.", state);
322 
323         result = parcel.RewindWrite(rewindReadPos);
324         AssertEqual(result, false, "result did not equal false as expected.", state);
325     }
326     BENCHMARK_LOGD("ParcelTest test_parcel_001 end.");
327 }
328 
329 /**
330  * @tc.name: test_parcel_readvec_001
331  * @tc.desc: test parcel read vector failed with invlalid input.
332  * @tc.type: FUNC
333  */
BENCHMARK_F(BenchmarkParcelTest,test_parcel_readvec_001)334 BENCHMARK_F(BenchmarkParcelTest, test_parcel_readvec_001)(benchmark::State& state)
335 {
336     BENCHMARK_LOGD("ParcelTest test_parcel_readvec_001 start.");
337     while (state.KeepRunning()) {
338         Parcel parcel;
339 
340         bool result = parcel.ReadBoolVector(nullptr);
341         AssertEqual(result, false, "result did not equal false as expected.", state);
342 
343         result = parcel.ReadInt8Vector(nullptr);
344         AssertEqual(result, false, "result did not equal false as expected.", state);
345 
346         result = parcel.ReadInt16Vector(nullptr);
347         AssertEqual(result, false, "result did not equal false as expected.", state);
348 
349         result = parcel.ReadInt32Vector(nullptr);
350         AssertEqual(result, false, "result did not equal false as expected.", state);
351 
352         result = parcel.ReadInt64Vector(nullptr);
353         AssertEqual(result, false, "result did not equal false as expected.", state);
354 
355         result = parcel.ReadUInt8Vector(nullptr);
356         AssertEqual(result, false, "result did not equal false as expected.", state);
357 
358         result = parcel.ReadUInt16Vector(nullptr);
359         AssertEqual(result, false, "result did not equal false as expected.", state);
360 
361         result = parcel.ReadUInt32Vector(nullptr);
362         AssertEqual(result, false, "result did not equal false as expected.", state);
363 
364         result = parcel.ReadUInt64Vector(nullptr);
365         AssertEqual(result, false, "result did not equal false as expected.", state);
366 
367         result = parcel.ReadFloatVector(nullptr);
368         AssertEqual(result, false, "result did not equal false as expected.", state);
369 
370         result = parcel.ReadDoubleVector(nullptr);
371         AssertEqual(result, false, "result did not equal false as expected.", state);
372 
373         result = parcel.ReadStringVector(nullptr);
374         AssertEqual(result, false, "result did not equal false as expected.", state);
375 
376         result = parcel.ReadString16Vector(nullptr);
377         AssertEqual(result, false, "result did not equal false as expected.", state);
378     }
379     BENCHMARK_LOGD("ParcelTest test_parcel_readvec_001 end.");
380 }
381 
382 /**
383  * @tc.name: test_parcel_readvec_002
384  * @tc.desc: test parcel read vector failed with invlalid vector length -1.
385  * @tc.type: FUNC
386  */
387 template <typename T1, typename F>
WriteSignalDataToVector002(T1 & value,benchmark::State & state,const F executeFunc)388 void WriteSignalDataToVector002(T1 &value, benchmark::State& state, const F executeFunc)
389 {
390     int32_t writeInt32Value = -1;
391     Parcel parcel;
392     std::vector<T1> vec;
393     vec.push_back(value);
394     parcel.WriteInt32(writeInt32Value);
395 
396     bool result = executeFunc(parcel, vec);
397     AssertEqual(result, false, "result did not equal false as expected.", state);
398 }
399 
BENCHMARK_F(BenchmarkParcelTest,test_parcel_readvec_002)400 BENCHMARK_F(BenchmarkParcelTest, test_parcel_readvec_002)(benchmark::State& state)
401 {
402     BENCHMARK_LOGD("ParcelTest test_parcel_readvec_002 start.");
403     while (state.KeepRunning()) {
404         bool writeBoolValue = true;
405         WriteSignalDataToVector002(writeBoolValue, state,
406             [](auto &parcel, auto &vec) {return parcel.ReadBoolVector(&vec);});
407 
408         int8_t writeInt8Value = 1;
409         WriteSignalDataToVector002(writeInt8Value, state,
410             [](auto &parcel, auto &vec) {return parcel.ReadInt8Vector(&vec);});
411 
412         int16_t writeInt16Value = 1;
413         WriteSignalDataToVector002(writeInt16Value, state,
414             [](auto &parcel, auto &vec) {return parcel.ReadInt16Vector(&vec);});
415 
416         int32_t writeInt32Value = 1;
417         WriteSignalDataToVector002(writeInt32Value, state,
418             [](auto &parcel, auto &vec) {return parcel.ReadInt32Vector(&vec);});
419 
420         int64_t writeInt64Value = 1;
421         WriteSignalDataToVector002(writeInt64Value, state,
422             [](auto &parcel, auto &vec) {return parcel.ReadInt64Vector(&vec);});
423 
424         uint8_t writeUint8Value = 1;
425         WriteSignalDataToVector002(writeUint8Value, state,
426             [](auto &parcel, auto &vec) {return parcel.ReadUInt8Vector(&vec);});
427 
428         uint16_t writeUint16Value = 1;
429         WriteSignalDataToVector002(writeUint16Value, state,
430             [](auto &parcel, auto &vec) {return parcel.ReadUInt16Vector(&vec);});
431 
432         uint32_t writeUint32Value = 1;
433         WriteSignalDataToVector002(writeUint32Value, state,
434             [](auto &parcel, auto &vec) {return parcel.ReadUInt32Vector(&vec);});
435 
436         uint64_t writeUint64Value = 1;
437         WriteSignalDataToVector002(writeUint64Value, state,
438             [](auto &parcel, auto &vec) {return parcel.ReadUInt64Vector(&vec);});
439 
440         float writeFloatValue = 1;
441         WriteSignalDataToVector002(writeFloatValue, state,
442             [](auto &parcel, auto &vec) {return parcel.ReadFloatVector(&vec);});
443 
444         double writeDoubleValue = 1;
445         WriteSignalDataToVector002(writeDoubleValue, state,
446             [](auto &parcel, auto &vec) {return parcel.ReadDoubleVector(&vec);});
447 
448         std::string writeStringValue = "test";
449         WriteSignalDataToVector002(writeStringValue, state,
450             [](auto &parcel, auto &vec) {return parcel.ReadStringVector(&vec);});
451 
452         std::u16string writeU16StringValue = u"test";
453         WriteSignalDataToVector002(writeU16StringValue, state,
454             [](auto &parcel, auto &vec) {return parcel.ReadString16Vector(&vec);});
455     }
456     BENCHMARK_LOGD("ParcelTest test_parcel_readvec_002 end.");
457 }
458 
459 /**
460  * @tc.name: test_parcel_readvec_003
461  * @tc.desc: test parcel read vector failed with invlalid vector length
462  * std::vector::max_size().
463  * @tc.type: FUNC
464  */
465 template <typename T1, typename F>
WriteSignalDataToVector003(T1 & value,benchmark::State & state,const F executeFunc)466 void WriteSignalDataToVector003(T1 &value, benchmark::State& state, const F executeFunc)
467 {
468     Parcel parcel;
469     std::vector<T1> vec;
470     vec.push_back(value);
471     parcel.WriteInt32(vec.max_size());
472 
473     bool result = executeFunc(parcel, vec);
474     AssertEqual(result, false, "result did not equal false as expected.", state);
475 }
476 
BENCHMARK_F(BenchmarkParcelTest,test_parcel_readvec_003)477 BENCHMARK_F(BenchmarkParcelTest, test_parcel_readvec_003)(benchmark::State& state)
478 {
479     BENCHMARK_LOGD("ParcelTest test_parcel_readvec_003 start.");
480     while (state.KeepRunning()) {
481         bool writeBoolValue = true;
482         WriteSignalDataToVector003(writeBoolValue, state,
483             [](auto &parcel, auto &vec) {return parcel.ReadBoolVector(&vec);});
484 
485         int8_t writeInt8Value = 1;
486         WriteSignalDataToVector003(writeInt8Value, state,
487             [](auto &parcel, auto &vec) {return parcel.ReadInt8Vector(&vec);});
488 
489         int16_t writeInt16Value = 1;
490         WriteSignalDataToVector003(writeInt16Value, state,
491             [](auto &parcel, auto &vec) {return parcel.ReadInt16Vector(&vec);});
492 
493         int32_t writeInt32Value = 1;
494         WriteSignalDataToVector003(writeInt32Value, state,
495             [](auto &parcel, auto &vec) {return parcel.ReadInt32Vector(&vec);});
496 
497         int64_t writeInt64Value = 1;
498         WriteSignalDataToVector003(writeInt64Value, state,
499             [](auto &parcel, auto &vec) {return parcel.ReadInt64Vector(&vec);});
500 
501         uint8_t writeUint8Value = 1;
502         WriteSignalDataToVector003(writeUint8Value, state,
503             [](auto &parcel, auto &vec) {return parcel.ReadUInt8Vector(&vec);});
504 
505         uint16_t writeUint16Value = 1;
506         WriteSignalDataToVector003(writeUint16Value, state,
507             [](auto &parcel, auto &vec) {return parcel.ReadUInt16Vector(&vec);});
508 
509         uint32_t writeUint32Value = 1;
510         WriteSignalDataToVector003(writeUint32Value, state,
511             [](auto &parcel, auto &vec) {return parcel.ReadUInt32Vector(&vec);});
512 
513         uint64_t writeUint64Value = 1;
514         WriteSignalDataToVector003(writeUint64Value, state,
515             [](auto &parcel, auto &vec) {return parcel.ReadUInt64Vector(&vec);});
516 
517         float writeFloatValue = 1;
518         WriteSignalDataToVector003(writeFloatValue, state,
519             [](auto &parcel, auto &vec) {return parcel.ReadFloatVector(&vec);});
520 
521         double writeDoubleValue = 1;
522         WriteSignalDataToVector003(writeDoubleValue, state,
523             [](auto &parcel, auto &vec) {return parcel.ReadDoubleVector(&vec);});
524 
525         std::string writeStringValue = "test";
526         WriteSignalDataToVector003(writeStringValue, state,
527             [](auto &parcel, auto &vec) {return parcel.ReadStringVector(&vec);});
528 
529         std::u16string writeU16StringValue = u"test";
530         WriteSignalDataToVector003(writeU16StringValue, state,
531             [](auto &parcel, auto &vec) {return parcel.ReadString16Vector(&vec);});
532     }
533     BENCHMARK_LOGD("ParcelTest test_parcel_readvec_003 end.");
534 }
535 
536 /**
537  * @tc.name: test_parcel_writevec_001
538  * @tc.desc: test parcel write vector failed with writting data out of the
539  * maximum capacity.
540  * @tc.type: FUNC
541  */
542 template <typename T1, typename F>
WriteDataToVector(T1 & value,benchmark::State & state,const F executeFunc)543 void WriteDataToVector(T1 &value, benchmark::State& state, const F executeFunc)
544 {
545     std::vector<T1> vec;
546     for (unsigned int idx = 0; idx < DEFAULT_CPACITY / sizeof(T1); idx++) {
547         vec.push_back(value);
548     }
549     bool result = executeFunc(vec);
550     AssertEqual(result, false, "result did not equal false as expected.", state);
551 }
552 
553 template <typename F>
WriteDataToVector(std::string & value,benchmark::State & state,const F executeFunc)554 void WriteDataToVector(std::string &value, benchmark::State& state, const F executeFunc)
555 {
556     std::vector<std::string> vec;
557     vec.push_back(value);
558 
559     bool result = executeFunc(vec);
560     AssertEqual(result, false, "result did not equal false as expected.", state);
561 }
562 
563 template <typename F>
WriteDataToVector(std::u16string & value,benchmark::State & state,const F executeFunc)564 void WriteDataToVector(std::u16string &value, benchmark::State& state, const F executeFunc)
565 {
566     std::vector<std::u16string> vec;
567     vec.push_back(value);
568 
569     bool result = executeFunc(vec);
570     AssertEqual(result, false, "result did not equal false as expected.", state);
571 }
572 
BENCHMARK_F(BenchmarkParcelTest,test_parcel_writevec_001)573 BENCHMARK_F(BenchmarkParcelTest, test_parcel_writevec_001)(benchmark::State& state)
574 {
575     BENCHMARK_LOGD("ParcelTest test_parcel_writevec_001 start.");
576     while (state.KeepRunning()) {
577         Parcel parcel(nullptr);
578         bool writeBoolValue = true;
579         WriteDataToVector(writeBoolValue, state, [&](auto &vec) {return parcel.WriteBoolVector(vec);});
580 
581         int8_t writeInt8Value = 1;
582         WriteDataToVector(writeInt8Value, state,
583             [&](auto &vec) {parcel.FlushBuffer(); return parcel.WriteInt8Vector(vec);});
584 
585         int16_t writeInt16Value = 1;
586         WriteDataToVector(writeInt16Value, state,
587             [&](auto &vec) {parcel.FlushBuffer(); return parcel.WriteInt16Vector(vec);});
588 
589         int32_t writeInt32Value = 1;
590         WriteDataToVector(writeInt32Value, state,
591             [&](auto &vec) {parcel.FlushBuffer(); return parcel.WriteInt32Vector(vec);});
592 
593         int64_t writeInt64Value = 1;
594         WriteDataToVector(writeInt64Value, state,
595             [&](auto &vec) {parcel.FlushBuffer(); return parcel.WriteInt64Vector(vec);});
596 
597         uint8_t writeUint8Value = 1;
598         WriteDataToVector(writeUint8Value, state,
599             [&](auto &vec) {parcel.FlushBuffer(); return parcel.WriteUInt8Vector(vec);});
600 
601         uint16_t writeUint16Value = 1;
602         WriteDataToVector(writeUint16Value, state,
603             [&](auto &vec) {parcel.FlushBuffer(); return parcel.WriteUInt16Vector(vec);});
604 
605         uint32_t writeUint32Value = 1;
606         WriteDataToVector(writeUint32Value, state,
607             [&](auto &vec) {parcel.FlushBuffer(); return parcel.WriteUInt32Vector(vec);});
608 
609         uint64_t writeUint64Value = 1;
610         WriteDataToVector(writeUint64Value, state,
611             [&](auto &vec) {parcel.FlushBuffer(); return parcel.WriteUInt64Vector(vec);});
612 
613         float writeFloatValue = 1;
614         WriteDataToVector(writeFloatValue, state,
615             [&](auto &vec) {parcel.FlushBuffer(); return parcel.WriteFloatVector(vec);});
616 
617         double writeDoubleValue = 1;
618         WriteDataToVector(writeDoubleValue, state,
619             [&](auto &vec) {parcel.FlushBuffer(); return parcel.WriteDoubleVector(vec);});
620 
621         char insertCharValue = 't';
622         std::string x12((DEFAULT_CPACITY / sizeof(char)), insertCharValue);
623         WriteDataToVector(x12, state,
624             [&](auto &vec) {parcel.FlushBuffer(); return parcel.WriteStringVector(vec);});
625 
626         char16_t insertCharTValue = u't';
627         std::u16string x13((DEFAULT_CPACITY / sizeof(char16_t)), insertCharTValue);
628         WriteDataToVector(x13, state,
629             [&] (auto &vec) {parcel.FlushBuffer(); return parcel.WriteString16Vector(vec);});
630     }
631     BENCHMARK_LOGD("ParcelTest test_parcel_writevec_001 end.");
632 }
633 
634 /**
635  * @tc.name: test_parcel_SetMaxCapacity_001
636  * @tc.desc: test parcel primary type read write.
637  * @tc.type: FUNC
638  */
BENCHMARK_F(BenchmarkParcelTest,test_parcel_SetMaxCapacity_001)639 BENCHMARK_F(BenchmarkParcelTest, test_parcel_SetMaxCapacity_001)(benchmark::State& state)
640 {
641     BENCHMARK_LOGD("ParcelTest test_parcel_SetMaxCapacity_001 start.");
642     const size_t setMaxCapacityValue = 1;
643     while (state.KeepRunning()) {
644         size_t cap = DEFAULT_CPACITY;
645         Parcel parcel(nullptr);
646         AssertTrue(parcel.SetMaxCapacity(cap + setMaxCapacityValue),
647             "parcel.SetMaxCapacity(cap + 1) did not equal true as expected.", state);
648         AssertFalse(parcel.SetMaxCapacity(cap - setMaxCapacityValue),
649             "parcel.SetMaxCapacity(cap - 1) did not equal false as expected.", state);
650     }
651     BENCHMARK_LOGD("ParcelTest test_parcel_SetMaxCapacity_001 end.");
652 }
653 
654 /**
655  * @tc.name: test_parcel_SetAllocator_001
656  * @tc.desc: test setting allocator to parcels with and without existed allocator.
657  * @tc.type: FUNC
658  */
BENCHMARK_F(BenchmarkParcelTest,test_parcel_SetAllocator_001)659 BENCHMARK_F(BenchmarkParcelTest, test_parcel_SetAllocator_001)(benchmark::State& state)
660 {
661     BENCHMARK_LOGD("ParcelTest test_parcel_SetAllocator_001 start.");
662     while (state.KeepRunning()) {
663         Allocator* alloc = new DefaultAllocator();
664         Parcel parcel(alloc);
665         AssertFalse(parcel.SetAllocator(alloc),
666             "parcel.SetAllocator(alloc) did not equal false as expected.", state);
667         AssertFalse(parcel.SetAllocator(nullptr),
668             "parcel.SetAllocator(nullptr) did not equal false as expected.", state);
669 
670         struct TestData data = { true, -0x34, 0x5634, -0x12345678, 0x34, 0x5634, 0x12345678 };
671 
672         WriteTestData(parcel, data, state);
673         parcel.SetAllocator(new DefaultAllocator());
674         ReadTestData(parcel, data, state);
675     }
676     BENCHMARK_LOGD("ParcelTest test_parcel_SetAllocator_001 end.");
677 }
678 
679 /**
680  * @tc.name: test_parcel_write_001
681  * @tc.desc: test parcel write failed.
682  * @tc.type: FUNC
683  */
684 template <typename T1, typename F>
TestParcelWrite001(T1 value,benchmark::State & state,const F executeFunc)685 void TestParcelWrite001(T1 value, benchmark::State& state, const F executeFunc)
686 {
687     bool result = executeFunc(value);
688     AssertEqual(result, false, "result did not equal false as expected.", state);
689 }
690 
BENCHMARK_F(BenchmarkParcelTest,test_parcel_write_001)691 BENCHMARK_F(BenchmarkParcelTest, test_parcel_write_001)(benchmark::State& state)
692 {
693     BENCHMARK_LOGD("ParcelTest test_parcel_write_001 start.");
694     while (state.KeepRunning()) {
695         Parcel parcel1;
696         parcel1.WriteBool(true);
697         Parcel parcel2;
698         AssertTrue(CopyOldParcelToNewParcel(parcel1, parcel2, state),
699             "CopyOldParcelToNewParcel(parcel1, parcel2, state) did not equal true as expected.", state);
700 
701         string str8write;
702         TestParcelWrite001(str8write, state, [&](auto &value) {return parcel2.WriteString(value);});
703         u16string str16Write;
704         TestParcelWrite001(str16Write, state, [&](auto &value) {return parcel2.WriteString16(value);});
705 
706         TestParcelWrite001(false, state, [&](auto &value) {return parcel2.WriteBool(value);});
707         TestParcelWrite001(false, state, [&](auto &value) {return parcel2.WriteBoolUnaligned(value);});
708 
709         TestParcelWrite001(false, state, [&](auto &value) {return parcel2.WriteInt8(value);});
710         TestParcelWrite001(false, state, [&](auto &value) {return parcel2.WriteInt8Unaligned(value);});
711 
712         TestParcelWrite001(false, state, [&](auto &value) {return parcel2.WriteInt32(value);});
713 
714         TestParcelWrite001(false, state, [&](auto &value) {return parcel2.WriteInt64(value);});
715 
716         TestParcelWrite001(false, state, [&](auto &value) {return parcel2.WriteUint8(value);});
717 
718         TestParcelWrite001(false, state, [&](auto &value) {return parcel2.WriteUint16(value);});
719 
720         TestParcelWrite001(false, state, [&](auto &value) {return parcel2.WriteUint8Unaligned(value);});
721 
722         TestParcelWrite001(false, state, [&](auto &value) {return parcel2.WriteUint16Unaligned(value);});
723 
724         TestParcelWrite001(false, state, [&](auto &value) {return parcel2.WriteUint32(value);});
725 
726         TestParcelWrite001(false, state, [&](auto &value) {return parcel2.WriteUint64(value);});
727 
728         TestParcelWrite001(false, state, [&](auto &value) {return parcel2.WriteFloat(value);});
729 
730         TestParcelWrite001(false, state, [&](auto &value) {return parcel2.WriteDouble(value);});
731 
732         TestParcelWrite001(false, state, [&](auto &value) {return parcel2.WritePointer(value);});
733     }
734     BENCHMARK_LOGD("ParcelTest test_parcel_write_001 end.");
735 }
736 
737 /**
738  * @tc.name: test_parcel_WriteAndRead_001
739  * @tc.desc: test parcel primary type read write.
740  * @tc.type: FUNC
741  */
BENCHMARK_F(BenchmarkParcelTest,test_parcel_WriteAndRead_001)742 BENCHMARK_F(BenchmarkParcelTest, test_parcel_WriteAndRead_001)(benchmark::State& state)
743 {
744     BENCHMARK_LOGD("ParcelTest test_parcel_WriteAndRead_001 start.");
745     while (state.KeepRunning()) {
746         Parcel parcel(nullptr);
747         struct TestData data = { true, -0x34, 0x5634, -0x12345678, 0x34, 0x5634, 0x12345678 };
748         WriteTestData(parcel, data, state);
749         ReadTestData(parcel, data, state);
750 
751         WriteUnalignedTestData(parcel, data, state);
752         ReadUnalignedTestData(parcel, data, state);
753     }
754     BENCHMARK_LOGD("ParcelTest test_parcel_WriteAndRead_001 end.");
755 }
756 
757 /**
758  * @tc.name: test_parcel_WriteAndRead_002
759  * @tc.desc: test parcel primary type read write.
760  * @tc.type: FUNC
761  */
BENCHMARK_F(BenchmarkParcelTest,test_parcel_WriteAndRead_002)762 BENCHMARK_F(BenchmarkParcelTest, test_parcel_WriteAndRead_002)(benchmark::State& state)
763 {
764     BENCHMARK_LOGD("ParcelTest test_parcel_WriteAndRead_002 start.");
765     while (state.KeepRunning()) {
766         Parcel parcel1(nullptr);
767         Parcel parcel2(nullptr);
768         struct TestData data = { true, -0x34, 0x5634, -0x12345678, 0x34, 0x5634, 0x12345678 };
769         WriteTestData(parcel1, data, state);
770         WriteUnalignedTestData(parcel1, data, state);
771 
772         AssertTrue(CopyOldParcelToNewParcel(parcel1, parcel2, state),
773             "CopyOldParcelToNewParcel(parcel1, parcel2, state) did not equal true as expected.", state);
774         ReadTestData(parcel2, data, state);
775         ReadUnalignedTestData(parcel2, data, state);
776     }
777     BENCHMARK_LOGD("ParcelTest test_parcel_WriteAndRead_002 end.");
778 }
779 
780 /**
781  * @tc.name: test_parcel_WriteAndRead_003
782  * @tc.desc: test parcel primary type read write.
783  * @tc.type: FUNC
784  */
BENCHMARK_F(BenchmarkParcelTest,test_parcel_WriteAndRead_003)785 BENCHMARK_F(BenchmarkParcelTest, test_parcel_WriteAndRead_003)(benchmark::State& state)
786 {
787     BENCHMARK_LOGD("ParcelTest test_parcel_WriteAndRead_003 start.");
788     while (state.KeepRunning()) {
789         Parcel parcel1(nullptr);
790         Parcel parcel2(nullptr);
791         struct TestData data = { true, -0x34, 0x5634, -0x12345678, 0x34, 0x5634, 0x12345678 };
792         WriteTestData(parcel1, data, state);
793 
794         AssertTrue(CopyOldParcelToNewParcel(parcel1, parcel2, state),
795             "CopyOldParcelToNewParcel(parcel1, parcel2, state) did not equal true as expected.", state);
796         ReadTestDataWithTarget(parcel2, data, state);
797     }
798     BENCHMARK_LOGD("ParcelTest test_parcel_WriteAndRead_003 end.");
799 }
800 
801 /**
802  * @tc.name: test_parcel_WriteAndRead_004
803  * @tc.desc: test parcel primary type read write.
804  * @tc.type: FUNC
805  */
BENCHMARK_F(BenchmarkParcelTest,test_parcel_WriteAndRead_004)806 BENCHMARK_F(BenchmarkParcelTest, test_parcel_WriteAndRead_004)(benchmark::State& state)
807 {
808     BENCHMARK_LOGD("ParcelTest test_parcel_WriteAndRead_004 start.");
809     while (state.KeepRunning()) {
810         Parcel parcel1(nullptr);
811         int64_t int64test = -0x1234567887654321;
812         bool result = parcel1.WriteInt64(int64test);
813         AssertEqual(result, true, "result did not equal true as expected.", state);
814 
815         uint64_t uint64test = 0x1234567887654321;
816         result = parcel1.WriteUint64(uint64test);
817         AssertEqual(result, true, "result did not equal true as expected.", state);
818 
819         int64_t readint64 = parcel1.ReadInt64();
820         AssertEqual(readint64, int64test, "readint64 did not equal int64test as expected.", state);
821 
822         uint64_t readuint64 = parcel1.ReadUint64();
823         AssertEqual(readuint64, uint64test, "readuint64 did not equal uint64test as expected.", state);
824 
825         Parcel parcel2(nullptr);
826         AssertTrue(CopyOldParcelToNewParcel(parcel1, parcel2, state),
827             "CopyOldParcelToNewParcel(parcel1, parcel2, state) did not equal true as expected.", state);
828 
829         readint64 = parcel2.ReadInt64();
830         AssertEqual(readint64, int64test, "readint64 did not equal int64test as expected.", state);
831 
832         readuint64 = parcel2.ReadUint64();
833         AssertEqual(readuint64, uint64test, "readuint64 did not equal uint64test as expected.", state);
834     }
835     BENCHMARK_LOGD("ParcelTest test_parcel_WriteAndRead_004 end.");
836 }
837 
838 /**
839  * @tc.name: test_parcel_WriteAndRead_String_001
840  * @tc.desc: test parcel string read write.
841  * @tc.type: FUNC
842  */
BENCHMARK_F(BenchmarkParcelTest,test_parcel_WriteAndRead_String_001)843 BENCHMARK_F(BenchmarkParcelTest, test_parcel_WriteAndRead_String_001)(benchmark::State& state)
844 {
845     BENCHMARK_LOGD("ParcelTest test_parcel_WriteAndRead_String_001 start.");
846     while (state.KeepRunning()) {
847         Parcel parcel1(nullptr);
848         string strWrite = "test";
849         bool result = parcel1.WriteString(strWrite);
850         AssertEqual(result, true, "result did not equal true as expected.", state);
851         string strWrite1 =
852             "test for write string padded**********************************************************##################";
853         result = parcel1.WriteString(strWrite1);
854         AssertEqual(result, true, "result did not equal true as expected.", state);
855 
856         string strWrite2 =
857             "test for write string padded**********************************************************##################";
858         result = parcel1.WriteString(strWrite2);
859         AssertEqual(result, true, "result did not equal true as expected.", state);
860         string strRead = parcel1.ReadString();
861         string strRead1 = parcel1.ReadString();
862         string strRead2 = parcel1.ReadString();
863         AssertEqual(COMPARE_STRING_RESULT, strcmp(strRead.c_str(), strWrite.c_str()),
864             "strcmp(strRead.c_str(), strWrite.c_str()) did not equal 0 as expected.", state);
865         AssertEqual(COMPARE_STRING_RESULT, strcmp(strRead1.c_str(), strWrite1.c_str()),
866             "strcmp(strRead1.c_str(), strWrite1.c_str()) did not equal 0 as expected.", state);
867         AssertEqual(COMPARE_STRING_RESULT, strcmp(strRead2.c_str(), strWrite2.c_str()),
868             "strcmp(strRead2.c_str(), strWrite2.c_str()) did not equal 0 as expected.", state);
869 
870         Parcel parcel2(nullptr);
871         AssertTrue(CopyOldParcelToNewParcel(parcel1, parcel2, state),
872             "CopyOldParcelToNewParcel(parcel1, parcel2, state) did not equal true as expected.", state);
873 
874         strRead = parcel2.ReadString();
875         strRead1 = parcel2.ReadString();
876         strRead2 = parcel2.ReadString();
877         AssertEqual(COMPARE_STRING_RESULT, strcmp(strRead.c_str(), strWrite.c_str()),
878             "strcmp(strRead.c_str(), strWrite.c_str()) did not equal 0 as expected.", state);
879         AssertEqual(COMPARE_STRING_RESULT, strcmp(strRead1.c_str(), strWrite1.c_str()),
880             "strcmp(strRead1.c_str(), strWrite1.c_str()) did not equal 0 as expected.", state);
881         AssertEqual(COMPARE_STRING_RESULT, strcmp(strRead2.c_str(), strWrite2.c_str()),
882             "strcmp(strRead2.c_str(), strWrite2.c_str()) did not equal 0 as expected.", state);
883     }
884     BENCHMARK_LOGD("ParcelTest test_parcel_WriteAndRead_String_001 end.");
885 }
886 
887 /**
888  * @tc.name: test_parcel_WriteAndRead_String_002
889  * @tc.desc: test parcel string read write.
890  * @tc.type: FUNC
891  */
BENCHMARK_F(BenchmarkParcelTest,test_parcel_WriteAndRead_String_002)892 BENCHMARK_F(BenchmarkParcelTest, test_parcel_WriteAndRead_String_002)(benchmark::State& state)
893 {
894     BENCHMARK_LOGD("ParcelTest test_parcel_WriteAndRead_String_002 start.");
895     while (state.KeepRunning()) {
896         Parcel parcel1(nullptr);
897         u16string str16Write = u"12345";
898         bool result = parcel1.WriteString16(str16Write);
899         AssertEqual(result, true, "result did not equal true as expected.", state);
900 
901         u16string str16Write2 = u"12345 test for write16string padded*********";
902         result = parcel1.WriteString16(str16Write2);
903         AssertEqual(result, true, "result did not equal true as expected.", state);
904 
905         u16string str16Read = parcel1.ReadString16();
906         u16string str16Read2 = parcel1.ReadString16();
907         AssertEqual(COMPARE_STRING_RESULT, str16Read.compare(str16Write),
908             "str16Read.compare(str16Write) did not equal 0 as expected.", state);
909         AssertEqual(COMPARE_STRING_RESULT, str16Read2.compare(str16Write2),
910             "str16Read2.compare(str16Write2) did not equal 0 as expected.", state);
911 
912         Parcel parcel2(nullptr);
913         AssertTrue(CopyOldParcelToNewParcel(parcel1, parcel2, state),
914             "CopyOldParcelToNewParcel(parcel1, parcel2, state) did not equal true as expected.", state);
915 
916         str16Read = parcel2.ReadString16();
917         str16Read2 = parcel2.ReadString16();
918         AssertEqual(COMPARE_STRING_RESULT, str16Read.compare(str16Write),
919             "str16Read.compare(str16Write) did not equal 0 as expected.", state);
920         AssertEqual(COMPARE_STRING_RESULT, str16Read2.compare(str16Write2),
921             "str16Read2.compare(str16Write2) did not equal 0 as expected.", state);
922     }
923     BENCHMARK_LOGD("ParcelTest test_parcel_WriteAndRead_String_002 end.");
924 }
925 
926 /**
927  * @tc.name: test_parcel_WriteAndRead_String_003
928  * @tc.desc: test parcel CString read write.
929  * @tc.type: FUNC
930  */
BENCHMARK_F(BenchmarkParcelTest,test_parcel_WriteAndRead_String_003)931 BENCHMARK_F(BenchmarkParcelTest, test_parcel_WriteAndRead_String_003)(benchmark::State& state)
932 {
933     BENCHMARK_LOGD("ParcelTest test_parcel_WriteAndRead_String_003 start.");
934     while (state.KeepRunning()) {
935         Parcel parcel(nullptr);
936         string test1 = "12345";
937         string test2 = "23456";
938         string test3 = "34567";
939         string test4 = "45678";
940         bool result = parcel.WriteCString(nullptr);
941         AssertEqual(result, false, "result did not equal false as expected.", state);
942 
943         result = parcel.WriteCString(test1.c_str());
944         AssertEqual(result, true, "result did not equal true as expected.", state);
945 
946         result = parcel.WriteCString(test2.c_str());
947         AssertEqual(result, true, "result did not equal true as expected.", state);
948 
949         result = parcel.WriteCString(test3.c_str());
950         AssertEqual(result, true, "result did not equal true as expected.", state);
951 
952         result = parcel.WriteCString(test4.c_str());
953         AssertEqual(result, true, "result did not equal true as expected.", state);
954 
955         AssertEqual(COMPARE_STRING_RESULT, strcmp(test1.c_str(), parcel.ReadCString()),
956             "strcmp(test1.c_str(), parcel.ReadCString()) did not equal 0 as expected.", state);
957         AssertEqual(COMPARE_STRING_RESULT, strcmp(test2.c_str(), parcel.ReadCString()),
958             "strcmp(test2.c_str(), parcel.ReadCString()) did not equal 0 as expected.", state);
959         AssertEqual(COMPARE_STRING_RESULT, strcmp(test3.c_str(), parcel.ReadCString()),
960             "strcmp(test3.c_str(), parcel.ReadCString()) did not equal 0 as expected.", state);
961         AssertEqual(COMPARE_STRING_RESULT, strcmp(test4.c_str(), parcel.ReadCString()),
962             "strcmp(test4.c_str(), parcel.ReadCString()) did not equal 0 as expected.", state);
963     }
964     BENCHMARK_LOGD("ParcelTest test_parcel_WriteAndRead_String_003 end.");
965 }
966 
967 /**
968  * @tc.name: test_parcel_WriteAndRead_String004
969  * @tc.desc: test parcel CString read write.
970  * @tc.type: FUNC
971  */
BENCHMARK_F(BenchmarkParcelTest,test_parcel_WriteAndRead_String004)972 BENCHMARK_F(BenchmarkParcelTest, test_parcel_WriteAndRead_String004)(benchmark::State& state)
973 {
974     BENCHMARK_LOGD("ParcelTest test_parcel_WriteAndRead_String004 start.");
975     while (state.KeepRunning()) {
976         Parcel parcel1(nullptr);
977         bool result = false;
978         size_t writeString16Length = 0;
979         // write from Java, read from C++
980         result = parcel1.WriteString16WithLength(nullptr, writeString16Length);
981         AssertEqual(result, true, "result did not equal true as expected.", state);
982 
983         u16string str16write = u"12345";
984         char16_t *value1 = str16write.data();
985         result = parcel1.WriteString16WithLength(value1, str16write.length());
986         AssertEqual(result, true, "result did not equal true as expected.", state);
987 
988         u16string str16write2 = u"12345 test for write16string padded*********";
989         char16_t *value2 = str16write2.data();
990         result = parcel1.WriteString16WithLength(value2, str16write2.length());
991         AssertEqual(result, true, "result did not equal true as expected.", state);
992 
993         u16string str16readNull = parcel1.ReadString16();
994         u16string str16read1 = parcel1.ReadString16();
995         u16string str16read2 = parcel1.ReadString16();
996         AssertEqual(COMPARE_STRING_RESULT, str16readNull.compare(std::u16string()),
997             "str16readNull.compare(std::u16string()) did not equal 0 as expected.", state);
998         AssertEqual(COMPARE_STRING_RESULT, str16read1.compare(str16write),
999             "str16read1.compare(str16write) did not equal 0 as expected.", state);
1000         AssertEqual(COMPARE_STRING_RESULT, str16read2.compare(str16write2),
1001             "str16read2.compare(str16write2) did not equal 0 as expected.", state);
1002 
1003         // write from C++, read from Java
1004         result = parcel1.WriteString16(str16write);
1005         AssertEqual(result, true, "result did not equal true as expected.", state);
1006 
1007         result = parcel1.WriteString16(str16write2);
1008         AssertEqual(result, true, "result did not equal true as expected.", state);
1009 
1010         int32_t readLength1 = 0;
1011         u16string str16read3 = parcel1.ReadString16WithLength(readLength1);
1012         AssertEqual(readLength1, static_cast<int32_t>(str16write.length()),
1013             "readLength1 did not equal static_cast<int32_t>(str16write.length()) as expected.", state);
1014 
1015         int32_t readLength2 = 0;
1016         u16string str16read4 = parcel1.ReadString16WithLength(readLength2);
1017         AssertEqual(readLength2, static_cast<int32_t>(str16write2.length()),
1018             "readLength2 did not equal static_cast<int32_t>(str16write2.length()) as expected.", state);
1019 
1020         AssertEqual(COMPARE_STRING_RESULT, str16read3.compare(str16write),
1021             "str16read3.compare(str16write) did not equal 0 as expected.", state);
1022 
1023         AssertEqual(COMPARE_STRING_RESULT, str16read4.compare(str16write2),
1024             "str16read4.compare(str16write2) did not equal 0 as expected.", state);
1025     }
1026     BENCHMARK_LOGD("ParcelTest test_parcel_WriteAndRead_String004 end.");
1027 }
1028 
1029 /**
1030  * @tc.name: test_parcel_WriteAndRead_String005
1031  * @tc.desc: test parcel CString read write.
1032  * @tc.type: FUNC
1033  */
BENCHMARK_F(BenchmarkParcelTest,test_parcel_WriteAndRead_String005)1034 BENCHMARK_F(BenchmarkParcelTest, test_parcel_WriteAndRead_String005)(benchmark::State& state)
1035 {
1036     BENCHMARK_LOGD("ParcelTest test_parcel_WriteAndRead_String005 start.");
1037     while (state.KeepRunning()) {
1038         Parcel parcel1(nullptr);
1039         bool result = false;
1040         size_t writeString8Length = 0;
1041         // write from Java, read from C++
1042         result = parcel1.WriteString8WithLength(nullptr, writeString8Length);
1043         AssertEqual(result, true, "result did not equal true as expected.", state);
1044 
1045         string str8write = "12345";
1046         char *value1 = str8write.data();
1047         result = parcel1.WriteString8WithLength(value1, str8write.length());
1048         AssertEqual(result, true, "result did not equal true as expected.", state);
1049 
1050         string str8write2 = "12345 test for write16string padded*********";
1051         char *value2 = str8write2.data();
1052         result = parcel1.WriteString8WithLength(value2, str8write2.length());
1053         AssertEqual(result, true, "result did not equal true as expected.", state);
1054 
1055         string str8readNull = parcel1.ReadString();
1056         string str8read1 = parcel1.ReadString();
1057         string str8read2 = parcel1.ReadString();
1058         AssertEqual(COMPARE_STRING_RESULT, str8readNull.compare(std::string()),
1059             "str8readNull.compare(std::string()) did not equal 0 as expected.", state);
1060         AssertEqual(COMPARE_STRING_RESULT, str8read1.compare(str8write),
1061             "str8read1.compare(str8write) did not equal 0 as expected.", state);
1062         AssertEqual(COMPARE_STRING_RESULT, str8read2.compare(str8write2),
1063             "str8read2.compare(str8write2) did not equal 0 as expected.", state);
1064 
1065         // write from C++, read from Java
1066         result = parcel1.WriteString(str8write);
1067         AssertEqual(result, true, "result did not equal true as expected.", state);
1068 
1069         result = parcel1.WriteString(str8write2);
1070         AssertEqual(result, true, "result did not equal true as expected.", state);
1071 
1072         int32_t readLength1 = 0;
1073         string str8read3 = parcel1.ReadString8WithLength(readLength1);
1074         AssertEqual(readLength1, static_cast<int32_t>(str8write.length()),
1075             "readLength1 did not equal static_cast<int32_t>(str8write.length()) as expected.", state);
1076 
1077         int32_t readLength2 = 0;
1078         string str8read4 = parcel1.ReadString8WithLength(readLength2);
1079         AssertEqual(readLength2, static_cast<int32_t>(str8write2.length()),
1080             "readLength2 did not equal static_cast<int32_t>(str8write2.length()) as expected.", state);
1081         AssertEqual(COMPARE_STRING_RESULT, str8read3.compare(str8write),
1082             "str8read3.compare(str8write) did not equal 0 as expected.", state);
1083         AssertEqual(COMPARE_STRING_RESULT, str8read4.compare(str8write2),
1084             "str8read4.compare(str8write2) did not equal 0 as expected.", state);
1085     }
1086     BENCHMARK_LOGD("ParcelTest test_parcel_WriteAndRead_String005 end.");
1087 }
1088 
1089 /**
1090  * @tc.name: test_parcel_WriteAndRead_Float_001
1091  * @tc.desc: test parcel float types read write.
1092  * @tc.type: FUNC
1093  */
BENCHMARK_F(BenchmarkParcelTest,test_parcel_WriteAndRead_Float_001)1094 BENCHMARK_F(BenchmarkParcelTest, test_parcel_WriteAndRead_Float_001)(benchmark::State& state)
1095 {
1096     BENCHMARK_LOGD("ParcelTest test_parcel_WriteAndRead_Float_001 start.");
1097     while (state.KeepRunning()) {
1098         Parcel parcel1(nullptr);
1099         float floatwrite = 12.345678f;
1100         bool result = parcel1.WriteFloat(floatwrite);
1101         AssertEqual(result, true, "result did not equal true as expected.", state);
1102 
1103         double doublewrite = 1345.7653;
1104         result = parcel1.WriteDouble(doublewrite);
1105         AssertEqual(result, true, "result did not equal true as expected.", state);
1106 
1107         float floatread;
1108         result = parcel1.ReadFloat(floatread);
1109         AssertEqual(result, true, "result did not equal true as expected.", state);
1110         AssertEqual(floatwrite, floatread, "floatwrite did not equal floatread as expected.", state);
1111 
1112         double doubleread;
1113         doubleread = parcel1.ReadDouble();
1114         AssertEqual(doublewrite, doubleread, "doublewrite did not equal doubleread as expected.", state);
1115 
1116         Parcel parcel2(nullptr);
1117         AssertTrue(CopyOldParcelToNewParcel(parcel1, parcel2, state),
1118             "CopyOldParcelToNewParcel(parcel1, parcel2, state) did not equal true as expected.", state);
1119 
1120         result = parcel2.ReadFloat(floatread);
1121         AssertEqual(result, true, "result did not equal true as expected.", state);
1122         AssertEqual(floatwrite, floatread, "floatwrite did not equal floatread as expected.", state);
1123 
1124         doubleread = parcel2.ReadDouble();
1125         AssertEqual(doublewrite, doubleread, "doublewrite did not equal doubleread as expected.", state);
1126     }
1127     BENCHMARK_LOGD("ParcelTest test_parcel_WriteAndRead_Float_001 end.");
1128 }
1129 
1130 /**
1131  * @tc.name: test_parcel_WriteAndRead_String_005
1132  * @tc.desc: test parcel String type read write.
1133  * @tc.type: FUNC
1134  */
BENCHMARK_F(BenchmarkParcelTest,test_parcel_WriteAndRead_String_005)1135 BENCHMARK_F(BenchmarkParcelTest, test_parcel_WriteAndRead_String_005)(benchmark::State& state)
1136 {
1137     BENCHMARK_LOGD("ParcelTest test_parcel_WriteAndRead_String_005 start.");
1138     while (state.KeepRunning()) {
1139         Parcel parcel1(nullptr);
1140         string strwrite = "test";
1141         bool result = parcel1.WriteString(strwrite);
1142         AssertEqual(result, true, "result did not equal true as expected.", state);
1143         string strwrite1 =
1144             "test for write string padded**********************************************************##################";
1145         result = parcel1.WriteString(strwrite1);
1146         AssertEqual(result, true, "result did not equal true as expected.", state);
1147         string strwrite2 =
1148             "test for write string padded**********************************************************##################";
1149         result = parcel1.WriteString(strwrite2);
1150         AssertEqual(result, true, "result did not equal true as expected.", state);
1151 
1152         string strread;
1153         string strread1;
1154         string strread2;
1155         result = parcel1.ReadString(strread);
1156         AssertEqual(result, true, "result did not equal true as expected.", state);
1157         result = parcel1.ReadString(strread1);
1158         AssertEqual(result, true, "result did not equal true as expected.", state);
1159         result = parcel1.ReadString(strread2);
1160         AssertEqual(result, true, "result did not equal true as expected.", state);
1161 
1162         AssertEqual(COMPARE_STRING_RESULT, strcmp(strread.c_str(), strwrite.c_str()),
1163             "strcmp(strread.c_str(), strwrite.c_str()) did not equal 0 as expected.", state);
1164         AssertEqual(COMPARE_STRING_RESULT, strcmp(strread1.c_str(), strwrite1.c_str()),
1165             "strcmp(strread1.c_str(), strwrite1.c_str()) did not equal 0 as expected.", state);
1166         AssertEqual(COMPARE_STRING_RESULT, strcmp(strread2.c_str(), strwrite2.c_str()),
1167             "strcmp(strread2.c_str(), strwrite2.c_str()) did not equal 0 as expected.", state);
1168 
1169         Parcel parcel2(nullptr);
1170         AssertTrue(CopyOldParcelToNewParcel(parcel1, parcel2, state),
1171             "CopyOldParcelToNewParcel(parcel1, parcel2, state) did not equal true as expected.", state);
1172 
1173         result = parcel2.ReadString(strread);
1174         AssertEqual(result, true, "result did not equal true as expected.", state);
1175         result = parcel2.ReadString(strread1);
1176         AssertEqual(result, true, "result did not equal true as expected.", state);
1177         result = parcel2.ReadString(strread2);
1178         AssertEqual(result, true, "result did not equal true as expected.", state);
1179 
1180         AssertEqual(COMPARE_STRING_RESULT, strcmp(strread.c_str(), strwrite.c_str()),
1181             "strcmp(strread.c_str(), strwrite.c_str()) did not equal 0 as expected.", state);
1182         AssertEqual(COMPARE_STRING_RESULT, strcmp(strread1.c_str(), strwrite1.c_str()),
1183             "strcmp(strread1.c_str(), strwrite1.c_str()) did not equal 0 as expected.", state);
1184         AssertEqual(COMPARE_STRING_RESULT, strcmp(strread2.c_str(), strwrite2.c_str()),
1185             "strcmp(strread2.c_str(), strwrite2.c_str()) did not equal 0 as expected.", state);
1186     }
1187     BENCHMARK_LOGD("ParcelTest test_parcel_WriteAndRead_String_005 end.");
1188 }
1189 
1190 struct Padded {
1191     char title;
1192     int32_t handle;
1193     uint64_t cookie;
1194 };
1195 
1196 struct Unpadded {
1197     char tip;
1198 };
1199 
ValidatePadded(const struct Padded & left,const struct Padded & right,benchmark::State & state)1200 void ValidatePadded(const struct Padded &left, const struct Padded &right, benchmark::State& state)
1201 {
1202     BENCHMARK_LOGD("ParcelTest void ValidatePadded is called.");
1203     AssertEqual(left.title, right.title, "left.title did not equal right.title as expected.", state);
1204 
1205     AssertEqual(left.handle, right.handle, "left.handle did not equal right.handle as expected.", state);
1206 
1207     AssertEqual(left.cookie, right.cookie, "left.cookie did not equal right.cookie as expected.", state);
1208 }
1209 
ValidateUnpadded(const struct Unpadded & left,const struct Unpadded & right,benchmark::State & state)1210 void ValidateUnpadded(const struct Unpadded &left, const struct Unpadded &right, benchmark::State& state)
1211 {
1212     BENCHMARK_LOGD("ParcelTest void ValidateUnpadded is called.");
1213     AssertEqual(left.tip, right.tip, "left.tip did not equal right.tip as expected.", state);
1214 }
1215 
1216 /**
1217  * @tc.name: test_CalcNewCapacity_001
1218  * @tc.desc: test kinds of input to CalcNewCapacity.
1219  * @tc.type: FUNC
1220  */
BENCHMARK_F(BenchmarkParcelTest,test_CalcNewCapacity_001)1221 BENCHMARK_F(BenchmarkParcelTest, test_CalcNewCapacity_001)(benchmark::State& state)
1222 {
1223     BENCHMARK_LOGD("ParcelTest test_CalcNewCapacity_001 start.");
1224     while (state.KeepRunning()) {
1225         Parcel parcel;
1226         size_t newMaxCapacity;
1227         size_t minNewCapacity = CAPACITY_THRESHOLD;
1228         const string strLenThreshd = string(minNewCapacity, 't');
1229         bool ret = parcel.WriteUnpadBuffer(static_cast<const void *>(strLenThreshd.data()), minNewCapacity);
1230         AssertEqual(ret, true, "ret did not equal true as expected.", state);
1231 
1232         newMaxCapacity = CAPACITY_THRESHOLD - 1;
1233         minNewCapacity = newMaxCapacity;
1234         const string strLessThreshd = string(minNewCapacity, 'l');
1235         parcel.SetMaxCapacity(newMaxCapacity);
1236         ret = parcel.WriteUnpadBuffer(static_cast<const void *>(strLessThreshd.data()), minNewCapacity);
1237         AssertEqual(ret, true, "ret did not equal true as expected.", state);
1238 
1239         newMaxCapacity = -1; // minNewCapacity = CAPACITY_THRESHOLD - 1
1240         const string strNoMaxCap = string(minNewCapacity, 'n');
1241         parcel.SetMaxCapacity(newMaxCapacity);
1242         ret = parcel.WriteUnpadBuffer(static_cast<const void *>(strNoMaxCap.data()), minNewCapacity);
1243         AssertEqual(ret, true, "ret did not equal true as expected.", state);
1244 
1245         minNewCapacity = CAPACITY_THRESHOLD + 1; // newMaxCapacity = -1
1246         const string strExtThreshd = string(minNewCapacity, 'e');
1247         parcel.SetMaxCapacity(newMaxCapacity);
1248         ret = parcel.WriteUnpadBuffer(static_cast<const void *>(strExtThreshd.data()), minNewCapacity);
1249         AssertEqual(ret, true, "ret did not equal true as expected.", state);
1250 
1251         newMaxCapacity = CAPACITY_THRESHOLD; // minNewCapacity = CAPACITY_THRESHOLD + 1
1252         const string strCapThreshd = string(minNewCapacity, 'e');
1253         parcel.SetMaxCapacity(newMaxCapacity);
1254         ret = parcel.WriteUnpadBuffer(static_cast<const void *>(strCapThreshd.data()), minNewCapacity);
1255         AssertEqual(ret, true, "ret did not equal true as expected.", state);
1256     }
1257     BENCHMARK_LOGD("ParcelTest test_CalcNewCapacity_001 end.");
1258 }
1259 
1260 /**
1261  * @tc.name: test_SetDataCapacity_001
1262  * @tc.desc: test kinds of input to SetDataCapacity.
1263  * @tc.type: FUNC
1264  */
BENCHMARK_F(BenchmarkParcelTest,test_SetDataCapacity_001)1265 BENCHMARK_F(BenchmarkParcelTest, test_SetDataCapacity_001)(benchmark::State& state)
1266 {
1267     BENCHMARK_LOGD("ParcelTest test_SetDataCapacity_001 start.");
1268     while (state.KeepRunning()) {
1269         Parcel parcel;
1270         struct TestData data = { true, -0x34, 0x5634, -0x12345678, 0x34, 0x5634, 0x12345678 };
1271         WriteTestData(parcel, data, state);
1272 
1273         bool result = parcel.SetDataCapacity(0);
1274         AssertFalse(result, "result did not equal false as expected.", state);
1275     }
1276     BENCHMARK_LOGD("ParcelTest test_SetDataCapacity_001 end.");
1277 }
1278 
1279 /**
1280  * @tc.name: test_SetDataSize_001
1281  * @tc.desc: test kinds of input to SetDataSize.
1282  * @tc.type: FUNC
1283  */
BENCHMARK_F(BenchmarkParcelTest,test_SetDataSize_001)1284 BENCHMARK_F(BenchmarkParcelTest, test_SetDataSize_001)(benchmark::State& state)
1285 {
1286     BENCHMARK_LOGD("ParcelTest test_SetDataSize_001 start.");
1287     while (state.KeepRunning()) {
1288         Parcel parcel;
1289         bool result = parcel.SetDataCapacity(sizeof(bool));
1290         AssertEqual(result, true, "result did not equal true as expected.", state);
1291 
1292         result = parcel.WriteBool(true);
1293         AssertEqual(result, true, "result did not equal true as expected.", state);
1294 
1295         result = parcel.SetDataSize(DEFAULT_CPACITY + 1);
1296         AssertEqual(result, false, "result did not equal false as expected.", state);
1297     }
1298     BENCHMARK_LOGD("ParcelTest test_SetDataSize_001 end.");
1299 }
1300 
1301 /**
1302  * @tc.name: test_parcel_Data_Structure_001
1303  * @tc.desc: test parcel struct data related function.
1304  * @tc.type: FUNC
1305  */
BENCHMARK_F(BenchmarkParcelTest,test_parcel_Data_Structure_001)1306 BENCHMARK_F(BenchmarkParcelTest, test_parcel_Data_Structure_001)(benchmark::State& state)
1307 {
1308     BENCHMARK_LOGD("ParcelTest test_parcel_Data_Structure_001 start.");
1309     while (state.KeepRunning()) {
1310         Parcel parcel(nullptr);
1311         const struct Padded pad = { 'p', 0x34567890, -0x2345678998765432 };
1312         const struct Unpadded unpad = { 'u' };
1313 
1314         bool result = parcel.WriteBuffer(static_cast<const void *>(&pad), sizeof(struct Padded));
1315         AssertEqual(result, true, "result did not equal true as expected.", state);
1316 
1317         const struct Padded *padRead =
1318             reinterpret_cast<const struct Padded *>(parcel.ReadBuffer(sizeof(struct Padded)));
1319         ValidatePadded(*padRead, pad, state);
1320         AssertEqual(parcel.GetWritePosition(), parcel.GetReadPosition(),
1321             "parcel.GetWritePosition() did not equal parcel.GetReadPosition() as expected.", state);
1322 
1323         result = parcel.WriteBuffer(static_cast<const void *>(&unpad), sizeof(struct Unpadded));
1324         const struct Unpadded *unpadRead =
1325             reinterpret_cast<const struct Unpadded *>(parcel.ReadBuffer(sizeof(struct Unpadded)));
1326         ValidateUnpadded(*unpadRead, unpad, state);
1327         AssertUnequal(parcel.GetWritePosition(), parcel.GetReadPosition(),
1328             "parcel.GetWritePosition() was not different from parcel.GetReadPosition() as expected.", state);
1329 
1330         parcel.RewindRead(REWIND_INIT_VALUE);
1331         parcel.RewindWrite(REWIND_INIT_VALUE);
1332         AssertEqual(parcel.GetWritePosition(), parcel.GetReadPosition(),
1333             "parcel.GetWritePosition() did not equal parcel.GetReadPosition() as expected.", state);
1334 
1335         result = parcel.WriteUnpadBuffer(static_cast<const void *>(&pad), sizeof(struct Padded));
1336         AssertEqual(result, true, "result did not equal true as expected.", state);
1337 
1338         const struct Padded *padReadNew =
1339             reinterpret_cast<const struct Padded *>(parcel.ReadUnpadBuffer(sizeof(struct Padded)));
1340         ValidatePadded(*padReadNew, pad, state);
1341         AssertEqual(parcel.GetWritePosition(), parcel.GetReadPosition(),
1342             "parcel.GetWritePosition() did not equal parcel.GetReadPosition() as expected.", state);
1343 
1344         result = parcel.WriteUnpadBuffer(static_cast<const void *>(&unpad), sizeof(struct Unpadded));
1345         AssertEqual(result, true, "result did not equal true as expected.", state);
1346 
1347         const struct Unpadded *unpadReadNew =
1348             reinterpret_cast<const struct Unpadded *>(parcel.ReadUnpadBuffer(sizeof(struct Unpadded)));
1349         ValidateUnpadded(*unpadReadNew, unpad, state);
1350         AssertEqual(parcel.GetWritePosition(), parcel.GetReadPosition(),
1351             "parcel.GetWritePosition() did not equal parcel.GetReadPosition() as expected.", state);
1352     }
1353     BENCHMARK_LOGD("ParcelTest test_parcel_Data_Structure_001 end.");
1354 }
1355 
1356 /**
1357  * @tc.name: test_parcel_Data_Structure_002
1358  * @tc.desc: test invalid input to WriteBuffer and WriteBufferAddTerminator.
1359  * @tc.type: FUNC
1360  */
BENCHMARK_F(BenchmarkParcelTest,test_parcel_Data_Structure_002)1361 BENCHMARK_F(BenchmarkParcelTest, test_parcel_Data_Structure_002)(benchmark::State& state)
1362 {
1363     BENCHMARK_LOGD("ParcelTest test_parcel_Data_Structure_002 start.");
1364     while (state.KeepRunning()) {
1365         Parcel parcel(nullptr);
1366         const string str = "test invalid input";
1367         const string strOverflow = "test write with SIZE_MAX bytes";
1368         const string strWriteFail = string((DEFAULT_CPACITY + 1) / sizeof(char), 'f');
1369         const string strWriteTermFail = string((DEFAULT_CPACITY - 2) / sizeof(char), 't');
1370         bool result = parcel.WriteBuffer(nullptr, sizeof(string));
1371         AssertEqual(result, false, "result did not equal false as expected.", state);
1372 
1373         result = parcel.WriteBufferAddTerminator(nullptr, sizeof(string), sizeof(char));
1374         AssertEqual(result, false, "result did not equal false as expected.", state);
1375 
1376         result = parcel.WriteBuffer(static_cast<const void *>(str.data()), 0);
1377         AssertEqual(result, false, "result did not equal false as expected.", state);
1378 
1379         size_t writeBufferSize = 0;
1380         result = parcel.WriteBufferAddTerminator(static_cast<const void *>(str.data()), writeBufferSize, sizeof(char));
1381         AssertEqual(result, false, "result did not equal false as expected.", state);
1382 
1383         result = parcel.WriteBuffer(static_cast<const void *>(strWriteFail.data()), strWriteFail.length());
1384         AssertEqual(result, false, "result did not equal false as expected.", state);
1385 
1386         result = parcel.WriteBufferAddTerminator(static_cast<const void *>(strWriteFail.data()),
1387                                                 strWriteFail.length(), sizeof(char));
1388         AssertEqual(result, false, "result did not equal false as expected.", state);
1389 
1390         result = parcel.WriteBufferAddTerminator(static_cast<const void *>(str.data()), str.length(), sizeof(char));
1391         AssertEqual(result, true, "result did not equal true as expected.", state);
1392 
1393         Parcel recvParcel(nullptr);
1394         AssertTrue(CopyOldParcelToNewParcel(parcel, recvParcel, state),
1395             "CopyOldParcelToNewParcel(parcel, recvParcel, state) did not equal true as expected.", state);
1396 
1397         result = recvParcel.WriteBufferAddTerminator(static_cast<const void *>(&str), str.length() + 1, sizeof(char));
1398         AssertEqual(result, false, "result did not equal false as expected.", state);
1399     }
1400 }
1401 
1402 struct VectorTestData {
1403     vector<bool> booltest = { false, false, true, false, true };
1404     vector<int8_t> int8test = { 0x01, 0x10, -0x20, 0x30, 0x40 };
1405     vector<int16_t> int16test = { 0x1234, -0x2345, 0x3456, -0x4567, 0x5678 };
1406     vector<int32_t> int32test = { 0x12345678, -0x23456789, 0x34567890, -0x45678901 };
1407     vector<int64_t> int64test = { 0x1234567887654321, -0x2345678998765432 };
1408     vector<uint8_t> uint8test = { 0x01, 0x10, 0x20, 0x30, 0x40 };
1409     vector<uint16_t> uint16test = { 0x1234, 0x2345, 0x3456, 0x4567, 0x5678 };
1410     vector<uint32_t> uint32test = { 0x12345678, 0x23456789, 0x34567890, 0x45678901 };
1411     vector<uint64_t> uint64test = { 0x1234567887654321, 0x2345678998765432 };
1412 };
1413 
WriteVectorTestData(Parcel & parcel,const VectorTestData & data,benchmark::State & state)1414 void WriteVectorTestData(Parcel &parcel, const VectorTestData &data, benchmark::State& state)
1415 {
1416     BENCHMARK_LOGD("ParcelTest void WriteVectorTestData is called.");
1417     bool result = parcel.WriteBoolVector(data.booltest);
1418     AssertEqual(result, true, "result did not equal true as expected.", state);
1419 
1420     result = parcel.WriteInt8Vector(data.int8test);
1421     AssertEqual(result, true, "result did not equal true as expected.", state);
1422 
1423     result = parcel.WriteInt16Vector(data.int16test);
1424     AssertEqual(result, true, "result did not equal true as expected.", state);
1425 
1426     result = parcel.WriteInt32Vector(data.int32test);
1427     AssertEqual(result, true, "result did not equal true as expected.", state);
1428 
1429     result = parcel.WriteInt64Vector(data.int64test);
1430     AssertEqual(result, true, "result did not equal true as expected.", state);
1431 
1432     result = parcel.WriteUInt8Vector(data.uint8test);
1433     AssertEqual(result, true, "result did not equal true as expected.", state);
1434 
1435     result = parcel.WriteUInt16Vector(data.uint16test);
1436     AssertEqual(result, true, "result did not equal true as expected.", state);
1437 
1438     result = parcel.WriteUInt32Vector(data.uint32test);
1439     AssertEqual(result, true, "result did not equal true as expected.", state);
1440 
1441     result = parcel.WriteUInt64Vector(data.uint64test);
1442     AssertEqual(result, true, "result did not equal true as expected.", state);
1443 }
1444 
1445 template <typename T1>
LoopReadVectorCmpData(const vector<T1> & vec1,const vector<T1> & vec2,const char * printInfo,benchmark::State & state)1446 void LoopReadVectorCmpData(const vector<T1> &vec1, const vector<T1> &vec2,
1447     const char* printInfo, benchmark::State& state)
1448 {
1449     for (size_t idx = 0; idx < vec1.size(); idx++) {
1450         AssertEqual(vec1[idx], vec2[idx], printInfo, state);
1451     }
1452 }
1453 
1454 template <> void LoopReadVectorCmpData<u16string>(const vector<u16string> &vec1, const vector<u16string> &vec2,
1455     const char* printInfo, benchmark::State& state)
1456 {
1457     for (size_t idx = 0; idx < vec1.size(); idx++) {
1458         AssertEqual(COMPARE_STRING_RESULT, vec1[idx].compare(vec2[idx]), printInfo, state);
1459     }
1460 }
1461 
ReadVectorTestData(Parcel & parcel,const VectorTestData & data,benchmark::State & state)1462 void ReadVectorTestData(Parcel &parcel, const VectorTestData &data, benchmark::State& state)
1463 {
1464     BENCHMARK_LOGD("ParcelTest void ReadVectorTestData is called.");
1465     vector<bool> boolread;
1466     vector<int8_t> int8read;
1467     vector<int16_t> int16read;
1468     vector<int32_t> int32read;
1469     vector<int64_t> int64read;
1470     vector<uint8_t> uint8read;
1471     vector<uint16_t> uint16read;
1472     vector<uint32_t> uint32read;
1473     vector<uint64_t> uint64read;
1474     bool result = parcel.ReadBoolVector(&boolread);
1475     AssertEqual(result, true, "result did not equal true as expected.", state);
1476     LoopReadVectorCmpData(data.booltest, boolread,
1477         "data.booltest[i] did not equal boolread[i] as expected.", state);
1478 
1479     result = parcel.ReadInt8Vector(&int8read);
1480     AssertEqual(result, true, "result did not equal true as expected.", state);
1481     LoopReadVectorCmpData(data.int8test, int8read,
1482         "data.int8test[i] did not equal int8read[i] as expected.", state);
1483 
1484     result = parcel.ReadInt16Vector(&int16read);
1485     AssertEqual(result, true, "result did not equal true as expected.", state);
1486     LoopReadVectorCmpData(data.int16test, int16read,
1487         "data.int16test[i] did not equal int16read[i] as expected.", state);
1488 
1489     result = parcel.ReadInt32Vector(&int32read);
1490     AssertEqual(result, true, "result did not equal true as expected.", state);
1491     LoopReadVectorCmpData(data.int32test, int32read,
1492         "data.int32test[i] did not equal int32read[i] as expected.", state);
1493 
1494     result = parcel.ReadInt64Vector(&int64read);
1495     AssertEqual(result, true, "result did not equal true as expected.", state);
1496     LoopReadVectorCmpData(data.int64test, int64read,
1497         "data.int64test[i] did not equal int64read[i] as expected.", state);
1498 
1499     result = parcel.ReadUInt8Vector(&uint8read);
1500     AssertEqual(result, true, "result did not equal true as expected.", state);
1501     LoopReadVectorCmpData(data.uint8test, uint8read,
1502         "data.uint8test[i] did not equal uint8read[i] as expected.", state);
1503 
1504     result = parcel.ReadUInt16Vector(&uint16read);
1505     AssertEqual(result, true, "result did not equal true as expected.", state);
1506     LoopReadVectorCmpData(data.uint16test, uint16read,
1507         "data.uint16test[i] did not equal uint16read[i] as expected.", state);
1508 
1509     result = parcel.ReadUInt32Vector(&uint32read);
1510     AssertEqual(result, true, "result did not equal true as expected.", state);
1511     LoopReadVectorCmpData(data.uint32test, uint32read,
1512         "data.uint32test[i] did not equal uint32read[i] as expected.", state);
1513 
1514     result = parcel.ReadUInt64Vector(&uint64read);
1515     AssertEqual(result, true, "result did not equal true as expected.", state);
1516     LoopReadVectorCmpData(data.uint64test, uint64read,
1517         "data.uint64test[i] did not equal uint64read[i] as expected.", state);
1518 }
1519 
1520 /**
1521  * @tc.name: test_parcel_WriteAndReadVector_001
1522  * @tc.desc: test vector parcel read and write.
1523  * @tc.type: FUNC
1524  */
BENCHMARK_F(BenchmarkParcelTest,test_parcel_WriteAndReadVector_001)1525 BENCHMARK_F(BenchmarkParcelTest, test_parcel_WriteAndReadVector_001)(benchmark::State& state)
1526 {
1527     BENCHMARK_LOGD("ParcelTest test_parcel_WriteAndReadVector_001 start.");
1528     while (state.KeepRunning()) {
1529         Parcel parcel(nullptr);
1530         struct VectorTestData data;
1531 
1532         WriteVectorTestData(parcel, data, state);
1533         ReadVectorTestData(parcel, data, state);
1534     }
1535     BENCHMARK_LOGD("ParcelTest test_parcel_WriteAndReadVector_001 end.");
1536 }
1537 
1538 /**
1539  * @tc.name: test_parcel_WriteAndReadVector_002
1540  * @tc.desc: test vector parcel read and write.
1541  * @tc.type: FUNC
1542  */
BENCHMARK_F(BenchmarkParcelTest,test_parcel_WriteAndReadVector_002)1543 BENCHMARK_F(BenchmarkParcelTest, test_parcel_WriteAndReadVector_002)(benchmark::State& state)
1544 {
1545     BENCHMARK_LOGD("ParcelTest test_parcel_WriteAndReadVector_002 start.");
1546     while (state.KeepRunning()) {
1547         Parcel parcel1(nullptr);
1548         struct VectorTestData data;
1549         WriteVectorTestData(parcel1, data, state);
1550 
1551         Parcel parcel2(nullptr);
1552         AssertTrue(CopyOldParcelToNewParcel(parcel1, parcel2, state),
1553             "CopyOldParcelToNewParcel(parcel1, parcel2, state) did not equal true as expected.", state);
1554         ReadVectorTestData(parcel2, data, state);
1555     }
1556     BENCHMARK_LOGD("ParcelTest test_parcel_WriteAndReadVector_002 end.");
1557 }
1558 
1559 /**
1560  * @tc.name: test_parcel_WriteAndReadVector_003
1561  * @tc.desc: test vector parcel read and write.
1562  * @tc.type: FUNC
1563  */
BENCHMARK_F(BenchmarkParcelTest,test_parcel_WriteAndReadVector_003)1564 BENCHMARK_F(BenchmarkParcelTest, test_parcel_WriteAndReadVector_003)(benchmark::State& state)
1565 {
1566     BENCHMARK_LOGD("ParcelTest test_parcel_WriteAndReadVector_003 start.");
1567     while (state.KeepRunning()) {
1568         Parcel parcel1(nullptr);
1569         vector<string> stringtest{ "test", "test for", "test for write", "test for write vector" };
1570         vector<u16string> string16test{ u"test", u"test for", u"test for write", u"test for write vector" };
1571         bool result = parcel1.WriteStringVector(stringtest);
1572         AssertEqual(result, true, "result did not equal true as expected.", state);
1573 
1574         result = parcel1.WriteString16Vector(string16test);
1575         AssertEqual(result, true, "result did not equal true as expected.", state);
1576 
1577         vector<string> stringread;
1578         result = parcel1.ReadStringVector(&stringread);
1579         AssertEqual(result, true, "result did not equal true as expected.", state);
1580         LoopReadVectorCmpData(stringtest, stringread,
1581             "stringtest[i] did not equal stringread[i] as expected.", state);
1582 
1583         vector<u16string> u16stringread;
1584         result = parcel1.ReadString16Vector(&u16stringread);
1585         AssertEqual(result, true, "result did not equal true as expected.", state);
1586         LoopReadVectorCmpData(string16test, u16stringread,
1587             "string16test[i].compare(u16stringread[i]) did not equal 0 as expected.", state);
1588 
1589         Parcel parcel2(nullptr);
1590         AssertTrue(CopyOldParcelToNewParcel(parcel1, parcel2, state),
1591             "CopyOldParcelToNewParcel(parcel1, parcel2, state) did not equal true as expected.", state);
1592 
1593         result = parcel2.ReadStringVector(&stringread);
1594         AssertEqual(result, true, "result did not equal true as expected.", state);
1595         LoopReadVectorCmpData(stringtest, stringread,
1596             "stringtest[i] did not equal stringread[i] as expected.", state);
1597 
1598         result = parcel2.ReadString16Vector(&u16stringread);
1599         AssertEqual(result, true, "result did not equal true as expected.", state);
1600         LoopReadVectorCmpData(string16test, u16stringread,
1601             "string16test[i].compare(u16stringread[i]) did not equal 0 as expected.", state);
1602     }
1603     BENCHMARK_LOGD("ParcelTest test_parcel_WriteAndReadVector_003 end.");
1604 }
1605 
1606 /**
1607  * @tc.name: test_parcel_WriteAndReadVector_004
1608  * @tc.desc: test vector parcel read and write.
1609  * @tc.type: FUNC
1610  */
BENCHMARK_F(BenchmarkParcelTest,test_parcel_WriteAndReadVector_004)1611 BENCHMARK_F(BenchmarkParcelTest, test_parcel_WriteAndReadVector_004)(benchmark::State& state)
1612 {
1613     BENCHMARK_LOGD("ParcelTest test_parcel_WriteAndReadVector_004 start.");
1614     while (state.KeepRunning()) {
1615         Parcel parcel1(nullptr);
1616         Parcel parcel2(nullptr);
1617         vector<float> floattest{ 11221.132313, 11221.45678 };
1618         vector<double> doubletest{ 1122.132313, 1122.45678 };
1619 
1620         bool result = parcel1.WriteFloatVector(floattest);
1621         AssertEqual(result, true, "result did not equal true as expected.", state);
1622 
1623         result = parcel1.WriteDoubleVector(doubletest);
1624         AssertEqual(result, true, "result did not equal true as expected.", state);
1625 
1626         vector<float> floatread;
1627         vector<double> doubleread;
1628 
1629         result = parcel1.ReadFloatVector(&floatread);
1630         AssertEqual(result, true, "result did not equal true as expected.", state);
1631         LoopReadVectorCmpData(floattest, floatread,
1632             "floattest[i] did not equal floatread[i] as expected.", state);
1633 
1634         result = parcel1.ReadDoubleVector(&doubleread);
1635         AssertEqual(result, true, "result did not equal true as expected.", state);
1636         LoopReadVectorCmpData(doubletest, doubleread,
1637             "doubletest[i] did not equal doubleread[i] as expected.", state);
1638     }
1639 }
1640 
CallWriteVector(Parcel & parcel,const std::vector<bool> & vectorTest)1641 bool CallWriteVector(Parcel &parcel, const std::vector<bool> &vectorTest)
1642 {
1643     BENCHMARK_LOGD("ParcelTest bool CallWriteVector is called.");
1644     return parcel.WriteBoolVector(vectorTest);
1645 }
1646 
CallWriteVector(Parcel & parcel,const std::vector<int8_t> & vectorTest)1647 bool CallWriteVector(Parcel &parcel, const std::vector<int8_t> &vectorTest)
1648 {
1649     BENCHMARK_LOGD("ParcelTest bool CallWriteVector is called.");
1650     return parcel.WriteInt8Vector(vectorTest);
1651 }
1652 
CallWriteVector(Parcel & parcel,const std::vector<int16_t> & vectorTest)1653 bool CallWriteVector(Parcel &parcel, const std::vector<int16_t> &vectorTest)
1654 {
1655     BENCHMARK_LOGD("ParcelTest bool CallWriteVector is called.");
1656     return parcel.WriteInt16Vector(vectorTest);
1657 }
1658 
CallWriteVector(Parcel & parcel,const std::vector<int32_t> & vectorTest)1659 bool CallWriteVector(Parcel &parcel, const std::vector<int32_t> &vectorTest)
1660 {
1661     BENCHMARK_LOGD("ParcelTest bool CallWriteVector is called.");
1662     return parcel.WriteInt32Vector(vectorTest);
1663 }
1664 
CallWriteVector(Parcel & parcel,const std::vector<int64_t> & vectorTest)1665 bool CallWriteVector(Parcel &parcel, const std::vector<int64_t> &vectorTest)
1666 {
1667     BENCHMARK_LOGD("ParcelTest bool CallWriteVector is called.");
1668     return parcel.WriteInt64Vector(vectorTest);
1669 }
1670 
CallWriteVector(Parcel & parcel,const std::vector<uint8_t> & vectorTest)1671 bool CallWriteVector(Parcel &parcel, const std::vector<uint8_t> &vectorTest)
1672 {
1673     BENCHMARK_LOGD("ParcelTest bool CallWriteVector is called.");
1674     return parcel.WriteUInt8Vector(vectorTest);
1675 }
1676 
CallWriteVector(Parcel & parcel,const std::vector<uint16_t> & vectorTest)1677 bool CallWriteVector(Parcel &parcel, const std::vector<uint16_t> &vectorTest)
1678 {
1679     BENCHMARK_LOGD("ParcelTest bool CallWriteVector is called.");
1680     return parcel.WriteUInt16Vector(vectorTest);
1681 }
1682 
CallWriteVector(Parcel & parcel,const std::vector<uint32_t> & vectorTest)1683 bool CallWriteVector(Parcel &parcel, const std::vector<uint32_t> &vectorTest)
1684 {
1685     BENCHMARK_LOGD("ParcelTest bool CallWriteVector is called.");
1686     return parcel.WriteUInt32Vector(vectorTest);
1687 }
1688 
CallWriteVector(Parcel & parcel,const std::vector<uint64_t> & vectorTest)1689 bool CallWriteVector(Parcel &parcel, const std::vector<uint64_t> &vectorTest)
1690 {
1691     BENCHMARK_LOGD("ParcelTest bool CallWriteVector is called.");
1692     return parcel.WriteUInt64Vector(vectorTest);
1693 }
1694 
CallWriteVector(Parcel & parcel,const std::vector<float> & vectorTest)1695 bool CallWriteVector(Parcel &parcel, const std::vector<float> &vectorTest)
1696 {
1697     BENCHMARK_LOGD("ParcelTest bool CallWriteVector is called.");
1698     return parcel.WriteFloatVector(vectorTest);
1699 }
1700 
CallWriteVector(Parcel & parcel,const std::vector<double> & vectorTest)1701 bool CallWriteVector(Parcel &parcel, const std::vector<double> &vectorTest)
1702 {
1703     BENCHMARK_LOGD("ParcelTest bool CallWriteVector is called.");
1704     return parcel.WriteDoubleVector(vectorTest);
1705 }
1706 
CallWriteVector(Parcel & parcel,const std::vector<std::string> & vectorTest)1707 bool CallWriteVector(Parcel &parcel, const std::vector<std::string> &vectorTest)
1708 {
1709     BENCHMARK_LOGD("ParcelTest bool CallWriteVector is called.");
1710     return parcel.WriteStringVector(vectorTest);
1711 }
1712 
CallWriteVector(Parcel & parcel,const std::vector<std::u16string> & vectorTest)1713 bool CallWriteVector(Parcel &parcel, const std::vector<std::u16string> &vectorTest)
1714 {
1715     BENCHMARK_LOGD("ParcelTest bool CallWriteVector is called.");
1716     return parcel.WriteString16Vector(vectorTest);
1717 }
1718 
1719 template <typename T>
ParcelWriteVector(const std::vector<T> & vectorTest,benchmark::State & state)1720 void ParcelWriteVector(const std::vector<T> &vectorTest, benchmark::State& state)
1721 {
1722     BENCHMARK_LOGD("ParcelTest void ParcelWriteVector is called.");
1723     Parcel parcel1(nullptr);
1724     Parcel parcel2(nullptr);
1725     bool result = CallWriteVector(parcel1, vectorTest);
1726     AssertEqual(result, true, "result did not equal true as expected.", state);
1727     AssertTrue(CopyOldParcelToNewParcel(parcel1, parcel2, state),
1728         "CopyOldParcelToNewParcel(parcel1, parcel2, state) did not equal true as expected.", state);
1729 
1730     result = CallWriteVector(parcel2, vectorTest);
1731     AssertEqual(result, false, "result did not equal false as expected.", state);
1732 }
1733 
1734 /**
1735  * @tc.name: test_parcel_WriteAndReadVector_005
1736  * @tc.desc: test vector parcel write failed.
1737  * @tc.type: FUNC
1738  */
BENCHMARK_F(BenchmarkParcelTest,test_parcel_WriteAndReadVector_005)1739 BENCHMARK_F(BenchmarkParcelTest, test_parcel_WriteAndReadVector_005)(benchmark::State& state)
1740 {
1741     BENCHMARK_LOGD("ParcelTest test_parcel_WriteAndReadVector_005 start.");
1742     while (state.KeepRunning()) {
1743         vector<bool> boolVectorTest { true, false };
1744         vector<int8_t> int8VectorTest { 1, 0 };
1745         vector<int16_t> int16VectorTest { 1, 0 };
1746         vector<int32_t> int32VectorTest { 1, 0 };
1747         vector<int64_t> int64VectorTest { 1, 0 };
1748         vector<uint8_t> uint8VectorTest { 1, 0 };
1749         vector<uint16_t> uint16VectorTest { 1, 0 };
1750         vector<uint32_t> uint32VectorTest { 1, 0 };
1751         vector<uint64_t> uint64VectorTest { 1, 0 };
1752         vector<float> floatVectorTest { 1.1, 0 };
1753         vector<double> doubleVectorTest { 1.1, 0 };
1754         vector<std::string> stringVectorTest { "true", "false" };
1755         vector<std::u16string> string16VectorTest { u"true", u"false" };
1756 
1757         ParcelWriteVector(boolVectorTest, state);
1758         ParcelWriteVector(int8VectorTest, state);
1759         ParcelWriteVector(int16VectorTest, state);
1760         ParcelWriteVector(int32VectorTest, state);
1761         ParcelWriteVector(int64VectorTest, state);
1762         ParcelWriteVector(uint8VectorTest, state);
1763         ParcelWriteVector(uint16VectorTest, state);
1764         ParcelWriteVector(uint32VectorTest, state);
1765         ParcelWriteVector(uint64VectorTest, state);
1766         ParcelWriteVector(floatVectorTest, state);
1767         ParcelWriteVector(doubleVectorTest, state);
1768         ParcelWriteVector(stringVectorTest, state);
1769         ParcelWriteVector(string16VectorTest, state);
1770     }
1771     BENCHMARK_LOGD("ParcelTest test_parcel_WriteAndReadVector_005 end.");
1772 }
1773 
1774 class TestParcelable : public virtual Parcelable {
1775 public:
1776     TestParcelable() = default;
1777     ~TestParcelable() = default;
1778 
1779     bool Marshalling(Parcel &parcel) const override;
1780     static TestParcelable *Unmarshalling(Parcel &parcel);
1781 
1782 public:
1783     int32_t int32Write_ = -0x12345678;
1784     int32_t int32Read_;
1785 };
1786 
Marshalling(Parcel & parcel) const1787 bool TestParcelable::Marshalling(Parcel &parcel) const
1788 {
1789     BENCHMARK_LOGD("ParcelTest bool TestParcelable::Marshalling is called.");
1790     bool result = parcel.WriteInt32(this->int32Write_);
1791     return result;
1792 }
1793 
Unmarshalling(Parcel & parcel)1794 TestParcelable *TestParcelable::Unmarshalling(Parcel &parcel)
1795 {
1796     BENCHMARK_LOGD("ParcelTest TestParcelable *TestParcelable::Unmarshalling is called.");
1797     auto *read = new TestParcelable();
1798     read->int32Read_ = parcel.ReadInt32();
1799     return read;
1800 }
1801 
1802 /**
1803  * @tc.name: test_parcel_parcelable_001
1804  * @tc.desc: test parcel read and write parcelable obj.
1805  * @tc.type: FUNC
1806  */
BENCHMARK_F(BenchmarkParcelTest,test_parcel_parcelable_001)1807 BENCHMARK_F(BenchmarkParcelTest, test_parcel_parcelable_001)(benchmark::State& state)
1808 {
1809     BENCHMARK_LOGD("ParcelTest test_parcel_parcelable_001 start.");
1810     while (state.KeepRunning()) {
1811         Parcel parcel(nullptr);
1812         sptr<TestParcelable> parcelableWrite = new TestParcelable();
1813         bool result = false;
1814 
1815         result = parcel.WriteParcelable(parcelableWrite);
1816         AssertEqual(result, true, "result did not equal true as expected.", state);
1817         AssertEqual(parcel.GetWritePosition(), parcel.GetDataSize(),
1818             "parcel.GetWritePosition() did not equal parcel.GetDataSize() as expected.", state);
1819 
1820         sptr<TestParcelable> parcelableRead = parcel.ReadParcelable<TestParcelable>();
1821         AssertEqual(parcelableWrite->int32Write_, parcelableRead->int32Read_,
1822             "parcelableWrite->int32Write_ did not equal parcelableRead->int32Read_ as expected.", state);
1823         AssertEqual(parcel.GetReadPosition(), parcel.GetDataSize(),
1824             "parcel.GetReadPosition() did not equal parcel.GetDataSize() as expected.", state);
1825     }
1826     BENCHMARK_LOGD("ParcelTest test_parcel_parcelable_001 end.");
1827 }
1828 
1829 /**
1830  * @tc.name: test_parcel_parcelable_002
1831  * @tc.desc: test parcel read and write parcelable obj.
1832  * @tc.type: FUNC
1833  */
BENCHMARK_F(BenchmarkParcelTest,test_parcel_parcelable_002)1834 BENCHMARK_F(BenchmarkParcelTest, test_parcel_parcelable_002)(benchmark::State& state)
1835 {
1836     BENCHMARK_LOGD("ParcelTest test_parcel_parcelable_002 start.");
1837     while (state.KeepRunning()) {
1838         Parcel parcel(nullptr);
1839         bool result = parcel.WriteParcelable(nullptr);
1840         AssertEqual(result, true, "result did not equal true as expected.", state);
1841 
1842         sptr<TestParcelable> parcelableRead = parcel.ReadParcelable<TestParcelable>();
1843         AssertEqual(nullptr, parcelableRead, "nullptr did not equal parcelableRead as expected.", state);
1844     }
1845     BENCHMARK_LOGD("ParcelTest test_parcel_parcelable_002 end.");
1846 }
1847 
1848 /**
1849  * @tc.name: test_parcel_parcelable_003
1850  * @tc.desc: test parcel read and write parcelable obj.
1851  * @tc.type: FUNC
1852  */
BENCHMARK_F(BenchmarkParcelTest,test_parcel_parcelable_003)1853 BENCHMARK_F(BenchmarkParcelTest, test_parcel_parcelable_003)(benchmark::State& state)
1854 {
1855     BENCHMARK_LOGD("ParcelTest test_parcel_parcelable_003 start.");
1856     while (state.KeepRunning()) {
1857         Parcel parcel(nullptr);
1858         sptr<TestParcelable> parcelableWriteNull;
1859         bool result = parcel.WriteStrongParcelable(parcelableWriteNull);
1860         AssertEqual(result, true, "result did not equal true as expected.", state);
1861 
1862         sptr<TestParcelable> parcelableWrite = new TestParcelable();
1863 
1864         bool test = parcelableWrite->TestBehavior(Parcelable::BehaviorFlag::HOLD_OBJECT);
1865         AssertEqual(test, false, "test did not equal false as expected.", state);
1866 
1867         test = parcelableWrite->TestBehavior(Parcelable::BehaviorFlag::IPC);
1868         AssertEqual(test, false, "test did not equal false as expected.", state);
1869 
1870         test = parcelableWrite->TestBehavior(Parcelable::BehaviorFlag::RPC);
1871         AssertEqual(test, false, "test did not equal false as expected.", state);
1872 
1873         result = parcel.WriteStrongParcelable(parcelableWrite);
1874         AssertEqual(result, true, "result did not equal true as expected.", state);
1875 
1876         sptr<TestParcelable> parcelableReadNull = parcel.ReadParcelable<TestParcelable>();
1877         AssertEqual(nullptr, parcelableReadNull,
1878             "nullptr did not equal parcelableReadNull as expected.", state);
1879 
1880         sptr<TestParcelable> parcelableRead = parcel.ReadParcelable<TestParcelable>();
1881         AssertEqual(parcelableWrite->int32Write_, parcelableRead->int32Read_,
1882             "parcelableWrite->int32Write_ did not equal parcelableRead->int32Read_ as expected.", state);
1883 
1884         test = parcelableWrite->TestBehavior(Parcelable::BehaviorFlag::HOLD_OBJECT);
1885         AssertEqual(test, true, "test did not equal true as expected.", state);
1886 
1887         test = parcelableWrite->TestBehavior(Parcelable::BehaviorFlag::IPC);
1888         AssertEqual(test, false, "test did not equal false as expected.", state);
1889 
1890         test = parcelableWrite->TestBehavior(Parcelable::BehaviorFlag::RPC);
1891         AssertEqual(test, false, "test did not equal false as expected.", state);
1892 
1893         parcelableWrite->ClearBehavior(Parcelable::BehaviorFlag::HOLD_OBJECT);
1894         test = parcelableWrite->TestBehavior(Parcelable::BehaviorFlag::HOLD_OBJECT);
1895         AssertEqual(test, false, "test did not equal false as expected.", state);
1896     }
1897     BENCHMARK_LOGD("ParcelTest test_parcel_parcelable_003 end.");
1898 }
1899 
1900 /**
1901  * @tc.name: test_SetMaxCapacity_001
1902  * @tc.desc: test parcel capacity function.
1903  * @tc.type: FUNC
1904  */
BENCHMARK_F(BenchmarkParcelTest,test_SetMaxCapacity_001)1905 BENCHMARK_F(BenchmarkParcelTest, test_SetMaxCapacity_001)(benchmark::State& state)
1906 {
1907     BENCHMARK_LOGD("ParcelTest test_SetMaxCapacity_001 start.");
1908     while (state.KeepRunning()) {
1909         Parcel parcel(nullptr);
1910         char test[PARCEL_TEST_CHAR_ARRAY_SIZE] = {0};
1911         bool ret = parcel.WriteBuffer(test, PARCEL_TEST_CHAR_ARRAY_SIZE);
1912         AssertEqual(ret, true, "ret did not equal true as expected.", state);
1913 
1914         // because default maxCap is 200 * 1024, so reset it more
1915         const size_t maxCapacitySize = 201 * 1024;
1916         parcel.SetMaxCapacity(maxCapacitySize);
1917         // test write data over max capacity: 205780 + 48 > 201 * 1024
1918         char test2[PARCEL_TEST1_CHAR_ARRAY_SIZE] = {0};
1919         ret = parcel.WriteBuffer(test2, PARCEL_TEST1_CHAR_ARRAY_SIZE);
1920         AssertEqual(ret, false, "ret did not equal false as expected.", state);
1921     }
1922     BENCHMARK_LOGD("ParcelTest test_SetMaxCapacity_001 end.");
1923 }
1924 
1925 /**
1926  * @tc.name: test_SetMaxCapacity_002
1927  * @tc.desc: test parcel capacity function.
1928  * @tc.type: FUNC
1929  */
BENCHMARK_F(BenchmarkParcelTest,test_SetMaxCapacity_002)1930 BENCHMARK_F(BenchmarkParcelTest, test_SetMaxCapacity_002)(benchmark::State& state)
1931 {
1932     BENCHMARK_LOGD("ParcelTest test_SetMaxCapacity_002 start.");
1933     while (state.KeepRunning()) {
1934         Parcel parcel(nullptr);
1935         char test[PARCEL_TEST_CHAR_ARRAY_SIZE] = {0};
1936         bool ret = parcel.WriteInt32(5767168);
1937         AssertEqual(ret, true, "ret did not equal true as expected.", state);
1938 
1939         ret = parcel.WriteBuffer(test, PARCEL_TEST_CHAR_ARRAY_SIZE);
1940         AssertEqual(ret, true, "ret did not equal true as expected.", state);
1941 
1942         vector<std::u16string> val;
1943         ret = parcel.ReadString16Vector(&val);
1944         AssertEqual(ret, false, "ret did not equal false as expected.", state);
1945     }
1946     BENCHMARK_LOGD("ParcelTest test_SetMaxCapacity_002 end.");
1947 }
1948 
ParcelWriteData(Parcel & parcel,u16string & str16Write,string & strWrite,benchmark::State & state)1949 static void ParcelWriteData(Parcel& parcel, u16string& str16Write, string& strWrite, benchmark::State& state)
1950 {
1951     parcel.WriteBool(true);
1952     bool result = parcel.WriteString(strWrite);
1953     AssertEqual(result, true, "result did not equal true as expected.", state);
1954 
1955     RemoteObject obj1;
1956     result = parcel.WriteRemoteObject(&obj1);
1957     AssertEqual(result, true, "result did not equal true as expected.", state);
1958 
1959     parcel.WriteInt32(WRITE_AND_CMP_INT32_VALUE);
1960     RemoteObject obj2;
1961     result = parcel.WriteRemoteObject(&obj2);
1962     AssertEqual(result, true, "result did not equal true as expected.", state);
1963 
1964     result = parcel.WriteString16(str16Write);
1965     AssertEqual(result, true, "result did not equal true as expected.", state);
1966 }
1967 
BENCHMARK_F(BenchmarkParcelTest,test_ValidateReadData_001)1968 BENCHMARK_F(BenchmarkParcelTest, test_ValidateReadData_001)(benchmark::State& state)
1969 {
1970     BENCHMARK_LOGD("ParcelTest test_ValidateReadData_001 start.");
1971     while (state.KeepRunning()) {
1972         Parcel parcel(nullptr);
1973         u16string str16Write = u"12345";
1974         string strWrite = "test";
1975         ParcelWriteData(parcel, str16Write, strWrite, state);
1976 
1977         bool readBool = parcel.ReadBool();
1978         AssertEqual(readBool, true, "readBool did not equal true as expected.", state);
1979 
1980         string strRead = parcel.ReadString();
1981         AssertEqual(COMPARE_STRING_RESULT, strcmp(strRead.c_str(), strWrite.c_str()),
1982             "strcmp(strRead.c_str(), strWrite.c_str()) did not equal 0 as expected.", state);
1983 
1984         sptr<RemoteObject> readObj1 = parcel.ReadObject<RemoteObject>();
1985         AssertEqual(true, (readObj1.GetRefPtr() != nullptr),
1986             "(readObj1.GetRefPtr() != nullptr) did not equal true as expected.", state);
1987 
1988         int32_t readInt32 = parcel.ReadInt32();
1989         AssertEqual(readInt32, WRITE_AND_CMP_INT32_VALUE, "readInt32 did not equal 5 as expected.", state);
1990 
1991         sptr<RemoteObject> readObj2 = parcel.ReadObject<RemoteObject>();
1992         AssertEqual(true, (readObj2.GetRefPtr() != nullptr),
1993             "(readObj2.GetRefPtr() != nullptr) did not equal true as expected.", state);
1994 
1995         u16string str16Read = parcel.ReadString16();
1996         AssertEqual(COMPARE_STRING_RESULT, str16Read.compare(str16Write),
1997             "str16Read.compare(str16Write) did not equal 0 as expected.", state);
1998     }
1999     BENCHMARK_LOGD("ParcelTest test_ValidateReadData_001 end.");
2000 }
2001 
BENCHMARK_F(BenchmarkParcelTest,test_ValidateReadData_002)2002 BENCHMARK_F(BenchmarkParcelTest, test_ValidateReadData_002)(benchmark::State& state)
2003 {
2004     BENCHMARK_LOGD("ParcelTest test_ValidateReadData_002 start.");
2005     while (state.KeepRunning()) {
2006         Parcel parcel(nullptr);
2007         string strWrite = "test";
2008         u16string str16Write = u"12345";
2009         ParcelWriteData(parcel, str16Write, strWrite, state);
2010 
2011         bool readBool = parcel.ReadBool();
2012         AssertEqual(readBool, true, "readBool did not equal true as expected.", state);
2013 
2014         string strRead = parcel.ReadString();
2015         AssertEqual(COMPARE_STRING_RESULT, strcmp(strRead.c_str(), strWrite.c_str()),
2016             "strcmp(strRead.c_str(), strWrite.c_str()) did not equal 0 as expected.", state);
2017 
2018         int32_t readInt32 = parcel.ReadInt32();
2019         int32_t compareInt32Value = 0;
2020         AssertEqual(readInt32, compareInt32Value, "readInt32 did not equal 0 as expected.", state);
2021 
2022         u16string str16Read = parcel.ReadString16();
2023         AssertEqual(COMPARE_STRING_RESULT, str16Read.compare(std::u16string()),
2024             "str16Read.compare(std::u16string()) did not equal 0 as expected.", state);
2025 
2026         sptr<RemoteObject> readObj1 = parcel.ReadObject<RemoteObject>();
2027         AssertEqual(true, (readObj1.GetRefPtr() == nullptr),
2028             "(readObj1.GetRefPtr() == nullptr) did not equal true as expected.", state);
2029     }
2030     BENCHMARK_LOGD("ParcelTest test_ValidateReadData_002 end.");
2031 }
2032 
BENCHMARK_F(BenchmarkParcelTest,test_RewindWrite_001)2033 BENCHMARK_F(BenchmarkParcelTest, test_RewindWrite_001)(benchmark::State& state)
2034 {
2035     BENCHMARK_LOGD("ParcelTest test_RewindWrite_001 start.");
2036     while (state.KeepRunning()) {
2037         Parcel parcel(nullptr);
2038         parcel.WriteInt32(WRITE_AND_CMP_INT32_VALUE);
2039         string strWrite = "test";
2040         parcel.WriteString(strWrite);
2041         RemoteObject obj1;
2042         parcel.WriteRemoteObject(&obj1);
2043         size_t pos = parcel.GetWritePosition();
2044         parcel.WriteInt32(WRITE_AND_CMP_INT32_VALUE);
2045         RemoteObject obj2;
2046         parcel.WriteRemoteObject(&obj2);
2047         u16string str16Write = u"12345";
2048         parcel.WriteString16(str16Write);
2049 
2050         bool result = parcel.RewindWrite(pos);
2051         AssertEqual(result, true, "result did not equal true as expected.", state);
2052         parcel.WriteInt32(WRITE_AND_CMP_INT32_VALUE);
2053         parcel.WriteInt32(WRITE_AND_CMP_INT32_VALUE);
2054 
2055         int32_t readint32 = parcel.ReadInt32();
2056         AssertEqual(readint32, WRITE_AND_CMP_INT32_VALUE, "readint32 did not equal 5 as expected.", state);
2057 
2058         string strRead = parcel.ReadString();
2059         AssertEqual(COMPARE_STRING_RESULT, strcmp(strRead.c_str(), strWrite.c_str()),
2060             "strcmp(strRead.c_str(), strWrite.c_str()) did not equal 0 as expected.", state);
2061 
2062         sptr<RemoteObject> readObj1 = parcel.ReadObject<RemoteObject>();
2063         AssertEqual(true, (readObj1.GetRefPtr() != nullptr),
2064             "(readObj1.GetRefPtr() != nullptr) did not equal true as expected.", state);
2065 
2066         readint32 = parcel.ReadInt32();
2067         AssertEqual(readint32, WRITE_AND_CMP_INT32_VALUE, "readint32 did not equal 5 as expected.", state);
2068 
2069         sptr<RemoteObject> readObj2 = parcel.ReadObject<RemoteObject>();
2070         AssertEqual(true, (readObj2.GetRefPtr() == nullptr),
2071             "(readObj2.GetRefPtr() == nullptr) did not equal true as expected.", state);
2072         readint32 = parcel.ReadInt32();
2073         AssertEqual(readint32, WRITE_AND_CMP_INT32_VALUE, "readint32 did not equal 5 as expected.", state);
2074     }
2075     BENCHMARK_LOGD("ParcelTest test_RewindWrite_001 end.");
2076 }
2077 
BENCHMARK_F(BenchmarkParcelTest,test_RewindWrite_002)2078 BENCHMARK_F(BenchmarkParcelTest, test_RewindWrite_002)(benchmark::State& state)
2079 {
2080     BENCHMARK_LOGD("ParcelTest test_RewindWrite_002 start.");
2081     while (state.KeepRunning()) {
2082         Parcel parcel(nullptr);
2083         parcel.WriteInt32(WRITE_AND_CMP_INT32_VALUE);
2084         string strWrite = "test";
2085         parcel.WriteString(strWrite);
2086         RemoteObject obj1;
2087         parcel.WriteRemoteObject(&obj1);
2088         parcel.WriteInt32(WRITE_AND_CMP_INT32_VALUE);
2089         RemoteObject obj2;
2090         parcel.WriteRemoteObject(&obj2);
2091         size_t pos = parcel.GetWritePosition();
2092         u16string str16Write = u"12345";
2093         parcel.WriteString16(str16Write);
2094         bool result = parcel.RewindWrite(pos);
2095         AssertEqual(result, true, "result did not equal true as expected.", state);
2096 
2097         int32_t readint32 = parcel.ReadInt32();
2098         AssertEqual(readint32, WRITE_AND_CMP_INT32_VALUE, "readint32 did not equal 5 as expected.", state);
2099 
2100         string strRead = parcel.ReadString();
2101         AssertEqual(COMPARE_STRING_RESULT, strcmp(strRead.c_str(), strWrite.c_str()),
2102             "strcmp(strRead.c_str(), strWrite.c_str()) did not equal 0 as expected.", state);
2103 
2104         uint32_t readUint32 = parcel.ReadUint32();
2105         uint32_t compareUint32Value = 0;
2106         AssertEqual(readUint32, compareUint32Value, "readUint32 did not equal 0 as expected.", state);
2107 
2108         string strRead2 = parcel.ReadString();
2109         AssertEqual(COMPARE_STRING_RESULT, strRead2.compare(std::string()),
2110             "strRead2.compare(std::string()) did not equal 0 as expected.", state);
2111 
2112         sptr<RemoteObject> readObj1 = parcel.ReadObject<RemoteObject>();
2113         AssertEqual(true, (readObj1.GetRefPtr() == nullptr),
2114             "(readObj1.GetRefPtr() == nullptr) did not equal true as expected.", state);
2115 
2116         double compareDoubleValue = 0;
2117         double readDouble = parcel.ReadDouble();
2118         AssertEqual(readDouble, compareDoubleValue, "readDouble did not equal 0 as expected.", state);
2119     }
2120     BENCHMARK_LOGD("ParcelTest test_RewindWrite_002 end.");
2121 }
2122 
2123 enum ParcelVectorValue {
2124     VALUE_ZERO = 0,
2125     VALUE_ONE = 1,
2126     VALUE_TWO = 2,
2127     VALUE_THREE = 3,
2128     VALUE_FORE = 4,
2129     VALUE_FIVE = 5
2130 };
2131 enum ParcelVectorIndex {
2132     INDEX_ZERO = 0,
2133     INDEX_ONE = 1,
2134     INDEX_TWO = 2,
2135     INDEX_THREE = 3,
2136     INDEX_FORE = 4,
2137     INDEX_FIVE = 5
2138 };
2139 
BENCHMARK_F(BenchmarkParcelTest,test_RewindWrite_003)2140 BENCHMARK_F(BenchmarkParcelTest, test_RewindWrite_003)(benchmark::State& state)
2141 {
2142     BENCHMARK_LOGD("ParcelTest test_RewindWrite_003 start.");
2143     while (state.KeepRunning()) {
2144         Parcel parcel(nullptr);
2145         std::vector<int32_t> val{VALUE_ONE, VALUE_TWO, VALUE_THREE, VALUE_FORE, VALUE_FIVE};
2146         AssertEqual(val.size(), REWINDWRITE003_VECTOR_LENGTH, "val.size() did not equal 5 as expected.", state);
2147         bool result = parcel.WriteInt32Vector(val);
2148         AssertEqual(result, true, "result did not equal true as expected.", state);
2149         size_t pos = parcel.GetWritePosition() - sizeof(int32_t);
2150         result = parcel.RewindWrite(pos);
2151         AssertEqual(result, true, "result did not equal true as expected.", state);
2152 
2153         RemoteObject obj;
2154         parcel.WriteRemoteObject(&obj);
2155         std::vector<int32_t> int32Read;
2156         result = parcel.ReadInt32Vector(&int32Read);
2157         AssertEqual(result, false, "result did not equal false as expected.", state);
2158         AssertEqual(int32Read.size(), REWINDWRITE003_VECTOR_LENGTH,
2159             "int32Read.size() did not equal 5 as expected.", state);
2160         AssertEqual(int32Read[INDEX_ZERO], VALUE_ONE, "int32Read[0] did not equal 1 as expected.", state);
2161         AssertEqual(int32Read[INDEX_ONE], VALUE_TWO, "int32Read[1] did not equal 2 as expected.", state);
2162         AssertEqual(int32Read[INDEX_TWO], VALUE_THREE, "int32Read[2] did not equal 3 as expected.", state);
2163         AssertEqual(int32Read[INDEX_THREE], VALUE_FORE, "int32Read[3] did not equal 4 as expected.", state);
2164         AssertEqual(int32Read[INDEX_FORE], VALUE_ZERO, "int32Read[4] did not equal 0 as expected.", state);
2165     }
2166     BENCHMARK_LOGD("ParcelTest test_RewindWrite_003 end.");
2167 }
2168 
2169 /**
2170  * @tc.name: test_GetDataCapacity_001
2171  * @tc.desc: test kinds of input to SetDataCapacity.
2172  * @tc.type: FUNC
2173  */
BENCHMARK_F(BenchmarkParcelTest,test_GetDataCapacity_001)2174 BENCHMARK_F(BenchmarkParcelTest, test_GetDataCapacity_001)(benchmark::State& state)
2175 {
2176     BENCHMARK_LOGD("ParcelTest test_GetDataCapacity_001 start.");
2177     while (state.KeepRunning()) {
2178         Parcel parcel;
2179         size_t size = parcel.GetDataCapacity();
2180         AssertEqual(size, DATA_CAPACITY_INIT_SIZE,
2181             "test_GetDataCapacity_001 size did not equal zero as expected.", state);
2182 
2183         struct TestData data = { true, -0x34, 0x5634, -0x12345678, 0x34, 0x5634, 0x12345678 };
2184         WriteTestData(parcel, data, state);
2185         size = parcel.GetDataCapacity();
2186         AssertUnequal(size, DATA_CAPACITY_INIT_SIZE,
2187             "test_GetDataCapacity_001 size equal zero as expected.", state);
2188     }
2189     BENCHMARK_LOGD("ParcelTest test_GetDataCapacity_001 end.");
2190 }
2191 
2192 /**
2193  * @tc.name: test_GetMaxCapacity_001
2194  * @tc.desc: test parcel capacity function.
2195  * @tc.type: FUNC
2196  */
BENCHMARK_F(BenchmarkParcelTest,test_GetMaxCapacity_001)2197 BENCHMARK_F(BenchmarkParcelTest, test_GetMaxCapacity_001)(benchmark::State& state)
2198 {
2199     BENCHMARK_LOGD("ParcelTest test_GetMaxCapacity_001 start.");
2200     while (state.KeepRunning()) {
2201         Parcel parcel(nullptr);
2202         size_t size = parcel.GetMaxCapacity();
2203         AssertEqual(size, DEFAULT_CPACITY,
2204             "test_GetMaxCapacity_001 size did not equal DEFAULT_CPACITY as expected.", state);
2205 
2206         size_t maxCapacity = DEFAULT_CPACITY * 2;
2207         bool result = parcel.SetMaxCapacity(maxCapacity);
2208         AssertEqual(result, true, "test_GetMaxCapacity_001 result did not equal true as expected.", state);
2209 
2210         size = parcel.GetMaxCapacity();
2211         AssertEqual(size, maxCapacity,
2212             "test_GetMaxCapacity_001 size did not equal to maxCapacity as expected.", state);
2213     }
2214     BENCHMARK_LOGD("ParcelTest test_GetMaxCapacity_001 end.");
2215 }
2216 
2217 /**
2218  * @tc.name: test_GetObjectOffsets_001
2219  * @tc.desc: test parcel Object offsets function.
2220  * @tc.type: FUNC
2221  */
BENCHMARK_F(BenchmarkParcelTest,test_GetObjectOffsets_001)2222 BENCHMARK_F(BenchmarkParcelTest, test_GetObjectOffsets_001)(benchmark::State& state)
2223 {
2224     BENCHMARK_LOGD("ParcelTest test_GetObjectOffsets_001 start.");
2225     while (state.KeepRunning()) {
2226         Parcel parcel(nullptr);
2227         binder_size_t* pObjectOffsets = reinterpret_cast<binder_size_t*>(parcel.GetObjectOffsets());
2228         AssertEqual(pObjectOffsets, nullptr,
2229             "test_GetObjectOffsets_001 pObjectOffsets did not equal to nullptr as expected.", state);
2230 
2231         RemoteObject obj;
2232         bool result = parcel.WriteRemoteObject(&obj);
2233         AssertEqual(result, true,
2234             "test_GetObjectOffsets_001 result did not equal true as expected.", state);
2235 
2236         pObjectOffsets = reinterpret_cast<binder_size_t*>(parcel.GetObjectOffsets());
2237         AssertUnequal(pObjectOffsets, nullptr,
2238             "test_GetObjectOffsets_001 pObjectOffsets equal nullptr as expected.", state);
2239     }
2240     BENCHMARK_LOGD("ParcelTest test_GetObjectOffsets_001 end.");
2241 }
2242 
2243 /**
2244  * @tc.name: test_GetOffsetsSize_001
2245  * @tc.desc: test parcel Object offsets Size function.
2246  * @tc.type: FUNC
2247  */
BENCHMARK_F(BenchmarkParcelTest,test_GetOffsetsSize_001)2248 BENCHMARK_F(BenchmarkParcelTest, test_GetOffsetsSize_001)(benchmark::State& state)
2249 {
2250     BENCHMARK_LOGD("ParcelTest test_GetOffsetsSize_001 start.");
2251     while (state.KeepRunning()) {
2252         Parcel parcel(nullptr);
2253         size_t size = parcel.GetOffsetsSize();
2254         AssertEqual(size, DATA_OFFSETS_INIT_SIZE,
2255             "test_GetOffsetsSize_001 size did not equal to 0 as expected.", state);
2256 
2257         RemoteObject obj;
2258         bool result = parcel.WriteRemoteObject(&obj);
2259         AssertEqual(result, true, "test_GetOffsetsSize_001 result did not equal true as expected.", state);
2260 
2261         size = parcel.GetOffsetsSize();
2262         AssertUnequal(size, DATA_OFFSETS_INIT_SIZE, "test_GetOffsetsSize_001 size equal 0 as expected.", state);
2263     }
2264     BENCHMARK_LOGD("ParcelTest test_GetOffsetsSize_001 end.");
2265 }
2266 
2267 /**
2268  * @tc.name: test_GetReadableBytes_001
2269  * @tc.desc: test parcel readable bytes function.
2270  * @tc.type: FUNC
2271  */
BENCHMARK_F(BenchmarkParcelTest,test_GetReadableBytes_001)2272 BENCHMARK_F(BenchmarkParcelTest, test_GetReadableBytes_001)(benchmark::State& state)
2273 {
2274     BENCHMARK_LOGD("ParcelTest test_GetReadableBytes_001 start.");
2275     while (state.KeepRunning()) {
2276         Parcel parcel(nullptr);
2277         int8_t writeInt8 = 0x34;
2278         bool result = parcel.WriteInt8(writeInt8);
2279         AssertEqual(result, true, "test_GetReadableBytes_001 result did not equal true as expected.", state);
2280 
2281         size_t size = parcel.GetReadableBytes();
2282         AssertUnequal(size, DATA_READBYTES_INIT_SIZE, "test_GetReadableBytes_001 size equal 0 as expected.", state);
2283 
2284         int8_t readint8 = parcel.ReadInt8();
2285         AssertEqual(readint8, writeInt8,
2286             "test_GetReadableBytes_001 readint8 not equal writeInt8 as expected.", state);
2287 
2288         size = parcel.GetReadableBytes();
2289         AssertEqual(size, DATA_READBYTES_INIT_SIZE,
2290             "test_GetReadableBytes_001 size did not equal 0 as expected.", state);
2291     }
2292     BENCHMARK_LOGD("ParcelTest test_GetReadableBytes_001 end.");
2293 }
2294 
2295 /**
2296  * @tc.name: test_GetWritableBytes_001
2297  * @tc.desc: test parcel writable bytes function.
2298  * @tc.type: FUNC
2299  */
BENCHMARK_F(BenchmarkParcelTest,test_GetWritableBytes_001)2300 BENCHMARK_F(BenchmarkParcelTest, test_GetWritableBytes_001)(benchmark::State& state)
2301 {
2302     BENCHMARK_LOGD("ParcelTest test_GetWritableBytes_001 start.");
2303     while (state.KeepRunning()) {
2304         Parcel parcel(nullptr);
2305         int8_t writeInt8 = 0x34;
2306         bool result = parcel.WriteInt8(writeInt8);
2307         AssertEqual(result, true, "test_GetWritableBytes_001 result did not equal true as expected.", state);
2308 
2309         size_t size = parcel.GetDataCapacity() - sizeof(int32_t);
2310         size_t writablesize = parcel.GetWritableBytes();
2311         AssertEqual(writablesize, size,
2312             "test_GetWritableBytes_001 writablesize did not equal size as expected.", state);
2313 
2314         int16_t writeInt16 = 0x1234;
2315         result = parcel.WriteInt16(writeInt16);
2316         AssertEqual(result, true, "test_GetWritableBytes_001 result did not equal true as expected.", state);
2317 
2318         size = parcel.GetDataCapacity() - (sizeof(int32_t)*2);
2319         writablesize = parcel.GetWritableBytes();
2320         AssertEqual(writablesize, size,
2321             "test_GetWritableBytes_001 writablesize did not equal size as expected.", state);
2322     }
2323     BENCHMARK_LOGD("ParcelTest test_GetWritableBytes_001 end.");
2324 }
2325 
2326 /**
2327  * @tc.name: test_InjectOffsets_001
2328  * @tc.desc: test parcel inject offsets function.
2329  * @tc.type: FUNC
2330  */
BENCHMARK_F(BenchmarkParcelTest,test_InjectOffsets_001)2331 BENCHMARK_F(BenchmarkParcelTest, test_InjectOffsets_001)(benchmark::State& state)
2332 {
2333     BENCHMARK_LOGD("ParcelTest test_InjectOffsets_001 start.");
2334     while (state.KeepRunning()) {
2335         Parcel parcel(nullptr);
2336         int32_t size = 256;
2337         bool result = parcel.WriteInt32(size);
2338         AssertEqual(result, true, "test_InjectOffsets_001 result did not equal true as expected.", state);
2339 
2340         binder_size_t objectOffsets[PARCEL_INJECTOFFSETS_CHAR_ARRAY_SIZE] = {0};
2341         result = parcel.WriteBuffer(static_cast<const void *>(objectOffsets), sizeof(binder_size_t) * size);
2342         AssertEqual(result, true, "test_InjectOffsets_001 result did not equal true as expected.", state);
2343 
2344         int32_t offsetSize = parcel.ReadInt32();
2345         AssertEqual(offsetSize, size, "test_InjectOffsets_001 offsetSize did not equal size as expected.", state);
2346 
2347         const uint8_t* offsets = parcel.ReadBuffer(sizeof(binder_size_t) * offsetSize);
2348         AssertUnequal(offsets, nullptr, "test_InjectOffsets_001 offsets equal nullptr as expected.", state);
2349 
2350         Parcel newParcel(nullptr);
2351         newParcel.InjectOffsets(reinterpret_cast<binder_size_t>(offsets), offsetSize);
2352 
2353         binder_size_t* pObjectOffsets = reinterpret_cast<binder_size_t*>(newParcel.GetObjectOffsets());
2354         AssertUnequal(pObjectOffsets, nullptr,
2355             "test_InjectOffsets_001 pObjectOffsets equal nullptr as expected.", state);
2356     }
2357     BENCHMARK_LOGD("ParcelTest test_InjectOffsets_001 end.");
2358 }
2359 
2360 /**
2361  * @tc.name: test_parcel_WriteAndRead_Float_002
2362  * @tc.desc: test parcel write and read write.
2363  * @tc.type: FUNC
2364  */
BENCHMARK_F(BenchmarkParcelTest,test_parcel_WriteAndRead_Float_002)2365 BENCHMARK_F(BenchmarkParcelTest, test_parcel_WriteAndRead_Float_002)(benchmark::State& state)
2366 {
2367     BENCHMARK_LOGD("ParcelTest test_parcel_WriteAndRead_Float_002 start.");
2368     while (state.KeepRunning()) {
2369         Parcel parcel1(nullptr);
2370         float floatwrite = 12.345678f;
2371         bool result = parcel1.WriteFloat(floatwrite);
2372         AssertEqual(result, true, "test_parcel_WriteAndRead_Float_002 result did not equal true as expected.", state);
2373 
2374         double doublewrite = 1345.7653;
2375         result = parcel1.WriteDouble(doublewrite);
2376         AssertEqual(result, true, "test_parcel_WriteAndRead_Float_002 result did not equal true as expected.", state);
2377 
2378         float floatread = parcel1.ReadFloat();
2379         AssertEqual(floatread, floatwrite,
2380             "test_parcel_WriteAndRead_Float_002 floatread did not equal floatwrite as expected.", state);
2381 
2382         double doubleread;
2383         result = parcel1.ReadDouble(doubleread);
2384         AssertEqual(result, true, "test_parcel_WriteAndRead_Float_002 result did not equal true as expected.", state);
2385         AssertEqual(doubleread, doublewrite,
2386             "test_parcel_WriteAndRead_Float_002 doublewrite did not equal doubleread as expected.", state);
2387     }
2388     BENCHMARK_LOGD("ParcelTest test_parcel_WriteAndRead_Float_002 end.");
2389 }
2390 /**
2391  * @tc.name: test_parcel_WriteAndRead_005
2392  * @tc.desc: test parcel write and read write.
2393  * @tc.type: FUNC
2394  */
BENCHMARK_F(BenchmarkParcelTest,test_parcel_WriteAndRead_005)2395 BENCHMARK_F(BenchmarkParcelTest, test_parcel_WriteAndRead_005)(benchmark::State& state)
2396 {
2397     BENCHMARK_LOGD("ParcelTest test_parcel_WriteAndRead_005 start.");
2398     while (state.KeepRunning()) {
2399         Parcel parcel1(nullptr);
2400         int64_t int64test = -0x1234567887654321;
2401         bool result = parcel1.WriteInt64(int64test);
2402         AssertEqual(result, true, "test_parcel_WriteAndRead_005 result did not equal true as expected.", state);
2403 
2404         uint64_t uint64test = 0x1234567887654321;
2405         result = parcel1.WriteUint64(uint64test);
2406         AssertEqual(result, true, "test_parcel_WriteAndRead_005 result did not equal true as expected.", state);
2407 
2408         int64_t readint64;
2409         result = parcel1.ReadInt64(readint64);
2410         AssertEqual(result, true, "test_parcel_WriteAndRead_005 result did not equal true as expected.", state);
2411         AssertEqual(readint64, int64test,
2412             "test_parcel_WriteAndRead_005 readint64 did not equal int64test as expected.", state);
2413 
2414         uint64_t readuint64;
2415         result = parcel1.ReadUint64(readuint64);
2416         AssertEqual(result, true, "test_parcel_WriteAndRead_005 result did not equal true as expected.", state);
2417         AssertEqual(readuint64, uint64test,
2418             "test_parcel_WriteAndRead_005 readuint64 did not equal uint64test as expected.", state);
2419     }
2420     BENCHMARK_LOGD("ParcelTest test_parcel_WriteAndRead_005 end.");
2421 }
2422 
2423 /**
2424  * @tc.name: test_ReadPointer_001
2425  * @tc.desc: test parcel read pointer function.
2426  * @tc.type: FUNC
2427  */
BENCHMARK_F(BenchmarkParcelTest,test_ReadPointer_001)2428 BENCHMARK_F(BenchmarkParcelTest, test_ReadPointer_001)(benchmark::State& state)
2429 {
2430     BENCHMARK_LOGD("ParcelTest test_ReadPointer_001 start.");
2431     while (state.KeepRunning()) {
2432         Parcel parcel(nullptr);
2433         char writePointer[PARCEL_WRITEPOINTER_CHAR_ARRAY_SIZE] = {0};
2434         bool result = parcel.WritePointer((uintptr_t)writePointer);
2435         AssertEqual(result, true, "test_ReadPointer_001 result did not equal true as expected.", state);
2436 
2437         char* readPointer = reinterpret_cast<char*>(parcel.ReadPointer());
2438         AssertEqual(readPointer, writePointer,
2439             "test_ReadPointer_001 readPointer did not equal writePointer as expected.", state);
2440     }
2441     BENCHMARK_LOGD("ParcelTest test_ReadPointer_001 end.");
2442 }
2443 
2444 /**
2445  * @tc.name: test_parcel_WriteAndRead_String006
2446  * @tc.desc: test parcel CString read write.
2447  * @tc.type: FUNC
2448  */
BENCHMARK_F(BenchmarkParcelTest,test_parcel_WriteAndRead_String006)2449 BENCHMARK_F(BenchmarkParcelTest, test_parcel_WriteAndRead_String006)(benchmark::State& state)
2450 {
2451     BENCHMARK_LOGD("ParcelTest test_parcel_WriteAndRead_String006 start.");
2452     while (state.KeepRunning()) {
2453         Parcel parcel1(nullptr);
2454         u16string str16Write = u"12345";
2455         bool result = parcel1.WriteString16(str16Write);
2456         AssertEqual(result, true, "test_parcel_WriteAndRead_String006 result did not equal true as expected.", state);
2457 
2458         u16string str16Read;
2459         result = parcel1.ReadString16(str16Read);
2460         AssertEqual(result, true, "test_parcel_WriteAndRead_String006 result did not equal true as expected.", state);
2461         AssertEqual(COMPARE_STRING_RESULT, str16Read.compare(str16Write),
2462             "test_parcel_WriteAndRead_String006 str16Read.compare(str16Write) did not equal 0 as expected.", state);
2463 
2464         Parcel parcel2(nullptr);
2465         AssertTrue(CopyOldParcelToNewParcel(parcel1, parcel2, state),
2466             "CopyOldParcelToNewParcel(parcel1, parcel2, state) did not equal true as expected.", state);
2467 
2468         result = parcel2.ReadString16(str16Read);
2469         AssertEqual(result, true, "test_parcel_WriteAndRead_String006 result did not equal true as expected.", state);
2470         AssertEqual(COMPARE_STRING_RESULT, str16Read.compare(str16Write),
2471             "test_parcel_WriteAndRead_String006 str16Read.compare(str16Write) did not equal 0 as expected.", state);
2472     }
2473     BENCHMARK_LOGD("ParcelTest test_parcel_WriteAndRead_String006 end.");
2474 }
2475 
2476 /**
2477  * @tc.name: test_parcel_parcelable_004
2478  * @tc.desc: test parcel ReadStrongParcelable obj.
2479  * @tc.type: FUNC
2480  */
BENCHMARK_F(BenchmarkParcelTest,test_parcel_parcelable_004)2481 BENCHMARK_F(BenchmarkParcelTest, test_parcel_parcelable_004)(benchmark::State& state)
2482 {
2483     BENCHMARK_LOGD("ParcelTest test_parcel_parcelable_004 start.");
2484     while (state.KeepRunning()) {
2485         Parcel parcel(nullptr);
2486         sptr<TestParcelable> parcelableWrite = new TestParcelable();
2487         bool result = parcel.WriteStrongParcelable(parcelableWrite);
2488         AssertEqual(result, true, "test_parcel_parcelable_004 result did not equal true as expected.", state);
2489 
2490         sptr<TestParcelable> parcelableRead = parcel.ReadStrongParcelable<TestParcelable>();
2491         AssertUnequal(nullptr, parcelableRead,
2492             "test_parcel_parcelable_004 nullptr equal to parcelableRead as expected.", state);
2493     }
2494     BENCHMARK_LOGD("ParcelTest test_parcel_parcelable_004 end.");
2495 }
2496 
2497 /**
2498  * @tc.name: test_SkipBytes_001
2499  * @tc.desc: test parcel Skip Bytes function.
2500  * @tc.type: FUNC
2501  */
BENCHMARK_F(BenchmarkParcelTest,test_SkipBytes_001)2502 BENCHMARK_F(BenchmarkParcelTest, test_SkipBytes_001)(benchmark::State& state)
2503 {
2504     BENCHMARK_LOGD("ParcelTest test_SkipBytes_001 start.");
2505     while (state.KeepRunning()) {
2506         Parcel parcel(nullptr);
2507         char buffer[CAPACITY_THRESHOLD] = {0};
2508         bool result = parcel.WriteBuffer(static_cast<const void *>(buffer), CAPACITY_THRESHOLD);
2509         AssertEqual(result, true, "test_SkipBytes_001 result did not equal true as expected.", state);
2510 
2511         size_t skipSize = 1024;
2512         parcel.SkipBytes(skipSize);
2513         size_t readableSize = parcel.GetReadableBytes();
2514         AssertEqual((skipSize+readableSize), CAPACITY_THRESHOLD,
2515             "test_SkipBytes_001 (skipSize+readableSize) did not equal CAPACITY_THRESHOLD as expected.", state);
2516 
2517         skipSize = CAPACITY_THRESHOLD;
2518         parcel.SkipBytes(skipSize);
2519         readableSize = parcel.GetReadableBytes();
2520         AssertEqual(readableSize, DATA_READBYTES_INIT_SIZE,
2521             "test_SkipBytes_001 readableSize did not equal 0 as expected.", state);
2522     }
2523     BENCHMARK_LOGD("ParcelTest test_SkipBytes_001 end.");
2524 }
2525 
2526 /**
2527  * @tc.name: test_WriteObject_001
2528  * @tc.desc: test parcel Write Object function.
2529  * @tc.type: FUNC
2530  */
BENCHMARK_F(BenchmarkParcelTest,test_WriteObject_001)2531 BENCHMARK_F(BenchmarkParcelTest, test_WriteObject_001)(benchmark::State& state)
2532 {
2533     BENCHMARK_LOGD("ParcelTest test_WriteObject_001 start.");
2534     while (state.KeepRunning()) {
2535         Parcel parcel(nullptr);
2536         sptr<RemoteObject> remoteWriteNUll;
2537         bool result = parcel.WriteObject<RemoteObject>(remoteWriteNUll);
2538         AssertEqual(result, false, "test_WriteObject_001 result did not equal false as expected.", state);
2539 
2540         sptr<RemoteObject> parcelableWrite = new RemoteObject();
2541         result = parcel.WriteObject<RemoteObject>(parcelableWrite);
2542         AssertEqual(result, true, "test_WriteObject_001 result did not equal true as expected.", state);
2543 
2544         sptr<RemoteObject> parcelableRead = parcel.ReadObject<RemoteObject>();
2545         AssertEqual((parcelableRead.GetRefPtr() != nullptr), true,
2546             "test_WriteObject_001 (parcelableRead.GetRefPtr() != nullptr) did not equal true as expected.", state);
2547     }
2548     BENCHMARK_LOGD("ParcelTest test_WriteObject_001 end.");
2549 }
2550 }  // namespace
2551 }  // namespace OHOS
2552 // Run the benchmark
2553 BENCHMARK_MAIN();