106f6ba60Sopenharmony_ci/* 206f6ba60Sopenharmony_ci * Copyright (c) Huawei Technologies Co., Ltd. 2023. All rights reserved. 306f6ba60Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 406f6ba60Sopenharmony_ci * you may not use this file except in compliance with the License. 506f6ba60Sopenharmony_ci * You may obtain a copy of the License at 606f6ba60Sopenharmony_ci * 706f6ba60Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 806f6ba60Sopenharmony_ci * 906f6ba60Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 1006f6ba60Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 1106f6ba60Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1206f6ba60Sopenharmony_ci * See the License for the specific language governing permissions and 1306f6ba60Sopenharmony_ci * limitations under the License. 1406f6ba60Sopenharmony_ci */ 1506f6ba60Sopenharmony_ci#include <limits> 1606f6ba60Sopenharmony_ci#include <memory> 1706f6ba60Sopenharmony_ci#include <vector> 1806f6ba60Sopenharmony_ci#include <gtest/gtest.h> 1906f6ba60Sopenharmony_ci 2006f6ba60Sopenharmony_ci#include "base_message.h" 2106f6ba60Sopenharmony_ci#include "example.pb.h" 2206f6ba60Sopenharmony_ci#include "example.pbencoder.h" 2306f6ba60Sopenharmony_ci 2406f6ba60Sopenharmony_cinamespace OHOS { 2506f6ba60Sopenharmony_cinamespace Developtools { 2606f6ba60Sopenharmony_cinamespace Profiler { 2706f6ba60Sopenharmony_ciusing namespace testing::ext; 2806f6ba60Sopenharmony_ci 2906f6ba60Sopenharmony_ciconstexpr uint32_t SIZE_BUFFER = 1024 * 1024; 3006f6ba60Sopenharmony_cistatic uint8_t g_buf[SIZE_BUFFER] = {0}; 3106f6ba60Sopenharmony_cistatic uint32_t g_writePos = 0; 3206f6ba60Sopenharmony_ci 3306f6ba60Sopenharmony_cibool GetMemory(RandomWriteCtx* ctx, uint32_t size, uint8_t** memory, uint32_t* offset) 3406f6ba60Sopenharmony_ci{ 3506f6ba60Sopenharmony_ci if (g_writePos + size > sizeof(g_buf)) { 3606f6ba60Sopenharmony_ci return false; 3706f6ba60Sopenharmony_ci } 3806f6ba60Sopenharmony_ci 3906f6ba60Sopenharmony_ci *memory = &g_buf[g_writePos]; 4006f6ba60Sopenharmony_ci *offset = g_writePos; 4106f6ba60Sopenharmony_ci return true; 4206f6ba60Sopenharmony_ci} 4306f6ba60Sopenharmony_ci 4406f6ba60Sopenharmony_cibool Seek(RandomWriteCtx* ctx, uint32_t offset) 4506f6ba60Sopenharmony_ci{ 4606f6ba60Sopenharmony_ci if (offset >= sizeof(g_buf)) { 4706f6ba60Sopenharmony_ci return false; 4806f6ba60Sopenharmony_ci } 4906f6ba60Sopenharmony_ci 5006f6ba60Sopenharmony_ci g_writePos = offset; 5106f6ba60Sopenharmony_ci return true; 5206f6ba60Sopenharmony_ci} 5306f6ba60Sopenharmony_ci 5406f6ba60Sopenharmony_cistatic RandomWriteCtx g_writeCtx = {GetMemory, Seek}; 5506f6ba60Sopenharmony_ci 5606f6ba60Sopenharmony_ciclass BaseMessageUnittest : public ::testing::Test { 5706f6ba60Sopenharmony_cipublic: 5806f6ba60Sopenharmony_ci static void SetUpTestCase() {}; 5906f6ba60Sopenharmony_ci static void TearDownTestCase() {}; 6006f6ba60Sopenharmony_ci 6106f6ba60Sopenharmony_ci void SetUp() 6206f6ba60Sopenharmony_ci { 6306f6ba60Sopenharmony_ci g_writePos = 0; 6406f6ba60Sopenharmony_ci } 6506f6ba60Sopenharmony_ci void TearDown() {}; 6606f6ba60Sopenharmony_ci}; 6706f6ba60Sopenharmony_ci 6806f6ba60Sopenharmony_ciHWTEST_F(BaseMessageUnittest, Size, TestSize.Level1) 6906f6ba60Sopenharmony_ci{ 7006f6ba60Sopenharmony_ci ProtoEncoder::ExampleMessage msgOpt(&g_writeCtx); 7106f6ba60Sopenharmony_ci g_writeCtx.seek(&g_writeCtx, 0); 7206f6ba60Sopenharmony_ci msgOpt.set_vint_int32(1); 7306f6ba60Sopenharmony_ci msgOpt.set_vint_int64(1); 7406f6ba60Sopenharmony_ci msgOpt.set_vint_uint32(1); 7506f6ba60Sopenharmony_ci msgOpt.set_vint_uint64(1); 7606f6ba60Sopenharmony_ci msgOpt.set_vint_sint32(1); 7706f6ba60Sopenharmony_ci msgOpt.set_vint_sint64(1); 7806f6ba60Sopenharmony_ci msgOpt.set_vint_bool(true); 7906f6ba60Sopenharmony_ci msgOpt.set_vint_enum(ProtoEncoder::NUM::ONE); 8006f6ba60Sopenharmony_ci ASSERT_EQ(g_writePos, (uint32_t)msgOpt.Size()); 8106f6ba60Sopenharmony_ci 8206f6ba60Sopenharmony_ci const std::string str = "this is test."; 8306f6ba60Sopenharmony_ci msgOpt.set_len_string(str); 8406f6ba60Sopenharmony_ci msgOpt.set_len_bytes(str.data(), str.size()); 8506f6ba60Sopenharmony_ci ASSERT_EQ(g_writePos, (uint32_t)msgOpt.Size()); 8606f6ba60Sopenharmony_ci 8706f6ba60Sopenharmony_ci ProtoEncoder::SubMessage* msgSub = msgOpt.mutable_len_sub(); 8806f6ba60Sopenharmony_ci ASSERT_TRUE(msgSub != nullptr); 8906f6ba60Sopenharmony_ci msgSub->set_vint_int32(1); 9006f6ba60Sopenharmony_ci msgSub->set_vint_int64(1); 9106f6ba60Sopenharmony_ci msgSub->set_vint_uint32(1); 9206f6ba60Sopenharmony_ci msgSub->set_vint_uint64(1); 9306f6ba60Sopenharmony_ci // has not backfill the length of subMessage 9406f6ba60Sopenharmony_ci ASSERT_GT(g_writePos, (uint32_t)msgOpt.Size()); 9506f6ba60Sopenharmony_ci 9606f6ba60Sopenharmony_ci msgOpt.set_i64_fixed64(1); 9706f6ba60Sopenharmony_ci msgOpt.set_i64_sfixed64(1); 9806f6ba60Sopenharmony_ci msgOpt.set_i64_double(1); 9906f6ba60Sopenharmony_ci ASSERT_EQ(g_writePos, (uint32_t)msgOpt.Size()); 10006f6ba60Sopenharmony_ci} 10106f6ba60Sopenharmony_ci 10206f6ba60Sopenharmony_ciHWTEST_F(BaseMessageUnittest, SetVarintInt32, TestSize.Level1) 10306f6ba60Sopenharmony_ci{ 10406f6ba60Sopenharmony_ci ProtoEncoder::ExampleMessage msgOpt(&g_writeCtx); 10506f6ba60Sopenharmony_ci ExampleMessage msgProtobuf; 10606f6ba60Sopenharmony_ci 10706f6ba60Sopenharmony_ci g_writeCtx.seek(&g_writeCtx, 0); 10806f6ba60Sopenharmony_ci msgOpt.set_vint_int32(0); 10906f6ba60Sopenharmony_ci printf("set_vint_int32(0):\n"); 11006f6ba60Sopenharmony_ci for (uint32_t i = 0; i < g_writePos; i++) { 11106f6ba60Sopenharmony_ci printf("%02X ", g_buf[i]); 11206f6ba60Sopenharmony_ci } 11306f6ba60Sopenharmony_ci printf("\n"); 11406f6ba60Sopenharmony_ci ASSERT_TRUE(msgProtobuf.ParseFromArray(g_buf, g_writePos)); 11506f6ba60Sopenharmony_ci ASSERT_EQ(msgProtobuf.vint_int32(), 0); 11606f6ba60Sopenharmony_ci 11706f6ba60Sopenharmony_ci // min 11806f6ba60Sopenharmony_ci g_writeCtx.seek(&g_writeCtx, 0); 11906f6ba60Sopenharmony_ci msgOpt.set_vint_int32(std::numeric_limits<int32_t>::min()); 12006f6ba60Sopenharmony_ci printf("set_vint_int32(%" PRId32 "):\n", std::numeric_limits<int32_t>::min()); 12106f6ba60Sopenharmony_ci for (uint32_t i = 0; i < g_writePos; i++) { 12206f6ba60Sopenharmony_ci printf("%02X ", g_buf[i]); 12306f6ba60Sopenharmony_ci } 12406f6ba60Sopenharmony_ci printf("\n"); 12506f6ba60Sopenharmony_ci ASSERT_TRUE(msgProtobuf.ParseFromArray(g_buf, g_writePos)); 12606f6ba60Sopenharmony_ci ASSERT_EQ(msgProtobuf.vint_int32(), std::numeric_limits<int32_t>::min()); 12706f6ba60Sopenharmony_ci 12806f6ba60Sopenharmony_ci // max 12906f6ba60Sopenharmony_ci g_writeCtx.seek(&g_writeCtx, 0); 13006f6ba60Sopenharmony_ci msgOpt.set_vint_int32(std::numeric_limits<int32_t>::max()); 13106f6ba60Sopenharmony_ci printf("set_vint_int32(%" PRId32 "):\n", std::numeric_limits<int32_t>::max()); 13206f6ba60Sopenharmony_ci for (uint32_t i = 0; i < g_writePos; i++) { 13306f6ba60Sopenharmony_ci printf("%02X ", g_buf[i]); 13406f6ba60Sopenharmony_ci } 13506f6ba60Sopenharmony_ci printf("\n"); 13606f6ba60Sopenharmony_ci 13706f6ba60Sopenharmony_ci ASSERT_TRUE(msgProtobuf.ParseFromArray(g_buf, g_writePos)); 13806f6ba60Sopenharmony_ci ASSERT_EQ(msgProtobuf.vint_int32(), std::numeric_limits<int32_t>::max()); 13906f6ba60Sopenharmony_ci} 14006f6ba60Sopenharmony_ci 14106f6ba60Sopenharmony_ciHWTEST_F(BaseMessageUnittest, SetVarintInt64, TestSize.Level1) 14206f6ba60Sopenharmony_ci{ 14306f6ba60Sopenharmony_ci ProtoEncoder::ExampleMessage msgOpt(&g_writeCtx); 14406f6ba60Sopenharmony_ci ExampleMessage msgProtobuf; 14506f6ba60Sopenharmony_ci 14606f6ba60Sopenharmony_ci g_writeCtx.seek(&g_writeCtx, 0); 14706f6ba60Sopenharmony_ci msgOpt.set_vint_int64(0); 14806f6ba60Sopenharmony_ci printf("set_vint_int64(0):\n"); 14906f6ba60Sopenharmony_ci for (uint32_t i = 0; i < g_writePos; i++) { 15006f6ba60Sopenharmony_ci printf("%02X ", g_buf[i]); 15106f6ba60Sopenharmony_ci } 15206f6ba60Sopenharmony_ci printf("\n"); 15306f6ba60Sopenharmony_ci ASSERT_TRUE(msgProtobuf.ParseFromArray(g_buf, g_writePos)); 15406f6ba60Sopenharmony_ci ASSERT_EQ(msgProtobuf.vint_int64(), 0); 15506f6ba60Sopenharmony_ci 15606f6ba60Sopenharmony_ci // min 15706f6ba60Sopenharmony_ci g_writeCtx.seek(&g_writeCtx, 0); 15806f6ba60Sopenharmony_ci msgOpt.set_vint_int64(std::numeric_limits<int64_t>::min()); 15906f6ba60Sopenharmony_ci printf("set_vint_int64(%" PRId64 "):\n", std::numeric_limits<int64_t>::max()); 16006f6ba60Sopenharmony_ci for (uint32_t i = 0; i < g_writePos; i++) { 16106f6ba60Sopenharmony_ci printf("%02X ", g_buf[i]); 16206f6ba60Sopenharmony_ci } 16306f6ba60Sopenharmony_ci printf("\n"); 16406f6ba60Sopenharmony_ci ASSERT_TRUE(msgProtobuf.ParseFromArray(g_buf, g_writePos)); 16506f6ba60Sopenharmony_ci ASSERT_EQ(msgProtobuf.vint_int64(), std::numeric_limits<int64_t>::min()); 16606f6ba60Sopenharmony_ci 16706f6ba60Sopenharmony_ci // max 16806f6ba60Sopenharmony_ci g_writeCtx.seek(&g_writeCtx, 0); 16906f6ba60Sopenharmony_ci msgOpt.set_vint_int64(std::numeric_limits<int64_t>::max()); 17006f6ba60Sopenharmony_ci printf("set_vint_int64(%" PRId64 "):\n", std::numeric_limits<int64_t>::max()); 17106f6ba60Sopenharmony_ci for (uint32_t i = 0; i < g_writePos; i++) { 17206f6ba60Sopenharmony_ci printf("%02X ", g_buf[i]); 17306f6ba60Sopenharmony_ci } 17406f6ba60Sopenharmony_ci printf("\n"); 17506f6ba60Sopenharmony_ci 17606f6ba60Sopenharmony_ci ASSERT_TRUE(msgProtobuf.ParseFromArray(g_buf, g_writePos)); 17706f6ba60Sopenharmony_ci ASSERT_EQ(msgProtobuf.vint_int64(), std::numeric_limits<int64_t>::max()); 17806f6ba60Sopenharmony_ci} 17906f6ba60Sopenharmony_ci 18006f6ba60Sopenharmony_ciHWTEST_F(BaseMessageUnittest, SetVarintUint32, TestSize.Level1) 18106f6ba60Sopenharmony_ci{ 18206f6ba60Sopenharmony_ci ProtoEncoder::ExampleMessage msgOpt(&g_writeCtx); 18306f6ba60Sopenharmony_ci ExampleMessage msgProtobuf; 18406f6ba60Sopenharmony_ci 18506f6ba60Sopenharmony_ci g_writeCtx.seek(&g_writeCtx, 0); 18606f6ba60Sopenharmony_ci msgOpt.set_vint_uint32(0); 18706f6ba60Sopenharmony_ci printf("set_vint_uint32(0):\n"); 18806f6ba60Sopenharmony_ci for (uint32_t i = 0; i < g_writePos; i++) { 18906f6ba60Sopenharmony_ci printf("%02X ", g_buf[i]); 19006f6ba60Sopenharmony_ci } 19106f6ba60Sopenharmony_ci printf("\n"); 19206f6ba60Sopenharmony_ci ASSERT_TRUE(msgProtobuf.ParseFromArray(g_buf, g_writePos)); 19306f6ba60Sopenharmony_ci ASSERT_EQ(msgProtobuf.vint_uint32(), (uint32_t)0); 19406f6ba60Sopenharmony_ci 19506f6ba60Sopenharmony_ci // min 19606f6ba60Sopenharmony_ci g_writeCtx.seek(&g_writeCtx, 0); 19706f6ba60Sopenharmony_ci msgOpt.set_vint_uint32(std::numeric_limits<uint32_t>::min()); 19806f6ba60Sopenharmony_ci printf("set_vint_uint32(%" PRIu32 "):\n", std::numeric_limits<uint32_t>::min()); 19906f6ba60Sopenharmony_ci for (uint32_t i = 0; i < g_writePos; i++) { 20006f6ba60Sopenharmony_ci printf("%02X ", g_buf[i]); 20106f6ba60Sopenharmony_ci } 20206f6ba60Sopenharmony_ci printf("\n"); 20306f6ba60Sopenharmony_ci ASSERT_TRUE(msgProtobuf.ParseFromArray(g_buf, g_writePos)); 20406f6ba60Sopenharmony_ci ASSERT_EQ(msgProtobuf.vint_uint32(), std::numeric_limits<uint32_t>::min()); 20506f6ba60Sopenharmony_ci 20606f6ba60Sopenharmony_ci // max 20706f6ba60Sopenharmony_ci g_writeCtx.seek(&g_writeCtx, 0); 20806f6ba60Sopenharmony_ci msgOpt.set_vint_uint32(std::numeric_limits<uint32_t>::max()); 20906f6ba60Sopenharmony_ci printf("set_vint_uint32(%" PRIu32 "):\n", std::numeric_limits<uint32_t>::max()); 21006f6ba60Sopenharmony_ci for (uint32_t i = 0; i < g_writePos; i++) { 21106f6ba60Sopenharmony_ci printf("%02X ", g_buf[i]); 21206f6ba60Sopenharmony_ci } 21306f6ba60Sopenharmony_ci printf("\n"); 21406f6ba60Sopenharmony_ci 21506f6ba60Sopenharmony_ci ASSERT_TRUE(msgProtobuf.ParseFromArray(g_buf, g_writePos)); 21606f6ba60Sopenharmony_ci ASSERT_EQ(msgProtobuf.vint_uint32(), std::numeric_limits<uint32_t>::max()); 21706f6ba60Sopenharmony_ci} 21806f6ba60Sopenharmony_ci 21906f6ba60Sopenharmony_ciHWTEST_F(BaseMessageUnittest, SetVarintUint64, TestSize.Level1) 22006f6ba60Sopenharmony_ci{ 22106f6ba60Sopenharmony_ci ProtoEncoder::ExampleMessage msgOpt(&g_writeCtx); 22206f6ba60Sopenharmony_ci ExampleMessage msgProtobuf; 22306f6ba60Sopenharmony_ci 22406f6ba60Sopenharmony_ci g_writeCtx.seek(&g_writeCtx, 0); 22506f6ba60Sopenharmony_ci msgOpt.set_vint_uint64(0); 22606f6ba60Sopenharmony_ci printf("set_vint_uint64(0):\n"); 22706f6ba60Sopenharmony_ci for (uint32_t i = 0; i < g_writePos; i++) { 22806f6ba60Sopenharmony_ci printf("%02X ", g_buf[i]); 22906f6ba60Sopenharmony_ci } 23006f6ba60Sopenharmony_ci printf("\n"); 23106f6ba60Sopenharmony_ci ASSERT_TRUE(msgProtobuf.ParseFromArray(g_buf, g_writePos)); 23206f6ba60Sopenharmony_ci ASSERT_EQ(msgProtobuf.vint_uint64(), (uint64_t)0); 23306f6ba60Sopenharmony_ci 23406f6ba60Sopenharmony_ci // min 23506f6ba60Sopenharmony_ci g_writeCtx.seek(&g_writeCtx, 0); 23606f6ba60Sopenharmony_ci msgOpt.set_vint_uint64(std::numeric_limits<uint64_t>::min()); 23706f6ba60Sopenharmony_ci printf("set_vint_uint64(%" PRIu64 "):\n", std::numeric_limits<uint64_t>::min()); 23806f6ba60Sopenharmony_ci for (uint32_t i = 0; i < g_writePos; i++) { 23906f6ba60Sopenharmony_ci printf("%02X ", g_buf[i]); 24006f6ba60Sopenharmony_ci } 24106f6ba60Sopenharmony_ci printf("\n"); 24206f6ba60Sopenharmony_ci ASSERT_TRUE(msgProtobuf.ParseFromArray(g_buf, g_writePos)); 24306f6ba60Sopenharmony_ci ASSERT_EQ(msgProtobuf.vint_uint64(), std::numeric_limits<uint64_t>::min()); 24406f6ba60Sopenharmony_ci 24506f6ba60Sopenharmony_ci // max 24606f6ba60Sopenharmony_ci g_writeCtx.seek(&g_writeCtx, 0); 24706f6ba60Sopenharmony_ci msgOpt.set_vint_uint64(std::numeric_limits<uint64_t>::max()); 24806f6ba60Sopenharmony_ci printf("set_vint_uint64(%" PRIu64 "):\n", std::numeric_limits<uint64_t>::max()); 24906f6ba60Sopenharmony_ci for (uint32_t i = 0; i < g_writePos; i++) { 25006f6ba60Sopenharmony_ci printf("%02X ", g_buf[i]); 25106f6ba60Sopenharmony_ci } 25206f6ba60Sopenharmony_ci printf("\n"); 25306f6ba60Sopenharmony_ci 25406f6ba60Sopenharmony_ci ASSERT_TRUE(msgProtobuf.ParseFromArray(g_buf, g_writePos)); 25506f6ba60Sopenharmony_ci ASSERT_EQ(msgProtobuf.vint_uint64(), std::numeric_limits<uint64_t>::max()); 25606f6ba60Sopenharmony_ci} 25706f6ba60Sopenharmony_ci 25806f6ba60Sopenharmony_ciHWTEST_F(BaseMessageUnittest, SetVarintSint32, TestSize.Level1) 25906f6ba60Sopenharmony_ci{ 26006f6ba60Sopenharmony_ci ProtoEncoder::ExampleMessage msgOpt(&g_writeCtx); 26106f6ba60Sopenharmony_ci ExampleMessage msgProtobuf; 26206f6ba60Sopenharmony_ci 26306f6ba60Sopenharmony_ci g_writeCtx.seek(&g_writeCtx, 0); 26406f6ba60Sopenharmony_ci msgOpt.set_vint_sint32(0); 26506f6ba60Sopenharmony_ci printf("set_vint_sint32(0):\n"); 26606f6ba60Sopenharmony_ci for (uint32_t i = 0; i < g_writePos; i++) { 26706f6ba60Sopenharmony_ci printf("%02X ", g_buf[i]); 26806f6ba60Sopenharmony_ci } 26906f6ba60Sopenharmony_ci printf("\n"); 27006f6ba60Sopenharmony_ci ASSERT_TRUE(msgProtobuf.ParseFromArray(g_buf, g_writePos)); 27106f6ba60Sopenharmony_ci ASSERT_EQ(msgProtobuf.vint_sint32(), (int32_t)0); 27206f6ba60Sopenharmony_ci 27306f6ba60Sopenharmony_ci // min 27406f6ba60Sopenharmony_ci g_writeCtx.seek(&g_writeCtx, 0); 27506f6ba60Sopenharmony_ci msgOpt.set_vint_sint32(std::numeric_limits<int32_t>::min()); 27606f6ba60Sopenharmony_ci printf("set_vint_sint32(%" PRId32 "):\n", std::numeric_limits<int32_t>::min()); 27706f6ba60Sopenharmony_ci for (uint32_t i = 0; i < g_writePos; i++) { 27806f6ba60Sopenharmony_ci printf("%02X ", g_buf[i]); 27906f6ba60Sopenharmony_ci } 28006f6ba60Sopenharmony_ci printf("\n"); 28106f6ba60Sopenharmony_ci ASSERT_TRUE(msgProtobuf.ParseFromArray(g_buf, g_writePos)); 28206f6ba60Sopenharmony_ci ASSERT_EQ(msgProtobuf.vint_sint32(), std::numeric_limits<int32_t>::min()); 28306f6ba60Sopenharmony_ci 28406f6ba60Sopenharmony_ci // max 28506f6ba60Sopenharmony_ci g_writeCtx.seek(&g_writeCtx, 0); 28606f6ba60Sopenharmony_ci msgOpt.set_vint_sint32(std::numeric_limits<int32_t>::max()); 28706f6ba60Sopenharmony_ci printf("set_vint_sint32(%" PRId32 "):\n", std::numeric_limits<int32_t>::max()); 28806f6ba60Sopenharmony_ci for (uint32_t i = 0; i < g_writePos; i++) { 28906f6ba60Sopenharmony_ci printf("%02X ", g_buf[i]); 29006f6ba60Sopenharmony_ci } 29106f6ba60Sopenharmony_ci printf("\n"); 29206f6ba60Sopenharmony_ci 29306f6ba60Sopenharmony_ci ASSERT_TRUE(msgProtobuf.ParseFromArray(g_buf, g_writePos)); 29406f6ba60Sopenharmony_ci ASSERT_EQ(msgProtobuf.vint_sint32(), std::numeric_limits<int32_t>::max()); 29506f6ba60Sopenharmony_ci} 29606f6ba60Sopenharmony_ci 29706f6ba60Sopenharmony_ciHWTEST_F(BaseMessageUnittest, SetVarintSint64, TestSize.Level1) 29806f6ba60Sopenharmony_ci{ 29906f6ba60Sopenharmony_ci ProtoEncoder::ExampleMessage msgOpt(&g_writeCtx); 30006f6ba60Sopenharmony_ci ExampleMessage msgProtobuf; 30106f6ba60Sopenharmony_ci 30206f6ba60Sopenharmony_ci g_writeCtx.seek(&g_writeCtx, 0); 30306f6ba60Sopenharmony_ci msgOpt.set_vint_sint64(0); 30406f6ba60Sopenharmony_ci printf("set_vint_sint64(0):\n"); 30506f6ba60Sopenharmony_ci for (uint32_t i = 0; i < g_writePos; i++) { 30606f6ba60Sopenharmony_ci printf("%02X ", g_buf[i]); 30706f6ba60Sopenharmony_ci } 30806f6ba60Sopenharmony_ci printf("\n"); 30906f6ba60Sopenharmony_ci ASSERT_TRUE(msgProtobuf.ParseFromArray(g_buf, g_writePos)); 31006f6ba60Sopenharmony_ci ASSERT_EQ(msgProtobuf.vint_sint64(), (int64_t)0); 31106f6ba60Sopenharmony_ci 31206f6ba60Sopenharmony_ci // min 31306f6ba60Sopenharmony_ci g_writeCtx.seek(&g_writeCtx, 0); 31406f6ba60Sopenharmony_ci msgOpt.set_vint_sint64(std::numeric_limits<int64_t>::min()); 31506f6ba60Sopenharmony_ci printf("set_vint_sint64(%" PRId64 "):\n", std::numeric_limits<int64_t>::min()); 31606f6ba60Sopenharmony_ci for (uint32_t i = 0; i < g_writePos; i++) { 31706f6ba60Sopenharmony_ci printf("%02X ", g_buf[i]); 31806f6ba60Sopenharmony_ci } 31906f6ba60Sopenharmony_ci printf("\n"); 32006f6ba60Sopenharmony_ci ASSERT_TRUE(msgProtobuf.ParseFromArray(g_buf, g_writePos)); 32106f6ba60Sopenharmony_ci ASSERT_EQ(msgProtobuf.vint_sint64(), std::numeric_limits<int64_t>::min()); 32206f6ba60Sopenharmony_ci 32306f6ba60Sopenharmony_ci // max 32406f6ba60Sopenharmony_ci g_writeCtx.seek(&g_writeCtx, 0); 32506f6ba60Sopenharmony_ci msgOpt.set_vint_sint64(std::numeric_limits<int64_t>::max()); 32606f6ba60Sopenharmony_ci printf("set_vint_sint64(%" PRId64 "):\n", std::numeric_limits<int64_t>::max()); 32706f6ba60Sopenharmony_ci for (uint32_t i = 0; i < g_writePos; i++) { 32806f6ba60Sopenharmony_ci printf("%02X ", g_buf[i]); 32906f6ba60Sopenharmony_ci } 33006f6ba60Sopenharmony_ci printf("\n"); 33106f6ba60Sopenharmony_ci 33206f6ba60Sopenharmony_ci ASSERT_TRUE(msgProtobuf.ParseFromArray(g_buf, g_writePos)); 33306f6ba60Sopenharmony_ci ASSERT_EQ(msgProtobuf.vint_sint64(), std::numeric_limits<int64_t>::max()); 33406f6ba60Sopenharmony_ci} 33506f6ba60Sopenharmony_ci 33606f6ba60Sopenharmony_ciHWTEST_F(BaseMessageUnittest, SetVarintBool, TestSize.Level1) 33706f6ba60Sopenharmony_ci{ 33806f6ba60Sopenharmony_ci ProtoEncoder::ExampleMessage msgOpt(&g_writeCtx); 33906f6ba60Sopenharmony_ci ExampleMessage msgProtobuf; 34006f6ba60Sopenharmony_ci 34106f6ba60Sopenharmony_ci // min 34206f6ba60Sopenharmony_ci g_writeCtx.seek(&g_writeCtx, 0); 34306f6ba60Sopenharmony_ci msgOpt.set_vint_bool(std::numeric_limits<bool>::min()); 34406f6ba60Sopenharmony_ci printf("set_vint_bool(%d):\n", std::numeric_limits<bool>::min()); 34506f6ba60Sopenharmony_ci for (uint32_t i = 0; i < g_writePos; i++) { 34606f6ba60Sopenharmony_ci printf("%02X ", g_buf[i]); 34706f6ba60Sopenharmony_ci } 34806f6ba60Sopenharmony_ci printf("\n"); 34906f6ba60Sopenharmony_ci ASSERT_TRUE(msgProtobuf.ParseFromArray(g_buf, g_writePos)); 35006f6ba60Sopenharmony_ci ASSERT_EQ(msgProtobuf.vint_bool(), std::numeric_limits<bool>::min()); 35106f6ba60Sopenharmony_ci 35206f6ba60Sopenharmony_ci // max 35306f6ba60Sopenharmony_ci g_writeCtx.seek(&g_writeCtx, 0); 35406f6ba60Sopenharmony_ci msgOpt.set_vint_bool(std::numeric_limits<int64_t>::max()); 35506f6ba60Sopenharmony_ci printf("set_vint_bool(%d):\n", std::numeric_limits<bool>::max()); 35606f6ba60Sopenharmony_ci for (uint32_t i = 0; i < g_writePos; i++) { 35706f6ba60Sopenharmony_ci printf("%02X ", g_buf[i]); 35806f6ba60Sopenharmony_ci } 35906f6ba60Sopenharmony_ci printf("\n"); 36006f6ba60Sopenharmony_ci 36106f6ba60Sopenharmony_ci ASSERT_TRUE(msgProtobuf.ParseFromArray(g_buf, g_writePos)); 36206f6ba60Sopenharmony_ci ASSERT_EQ(msgProtobuf.vint_bool(), std::numeric_limits<bool>::max()); 36306f6ba60Sopenharmony_ci} 36406f6ba60Sopenharmony_ci 36506f6ba60Sopenharmony_ciHWTEST_F(BaseMessageUnittest, SetVarintEnum, TestSize.Level1) 36606f6ba60Sopenharmony_ci{ 36706f6ba60Sopenharmony_ci ProtoEncoder::ExampleMessage msgOpt(&g_writeCtx); 36806f6ba60Sopenharmony_ci ExampleMessage msgProtobuf; 36906f6ba60Sopenharmony_ci 37006f6ba60Sopenharmony_ci // min 37106f6ba60Sopenharmony_ci g_writeCtx.seek(&g_writeCtx, 0); 37206f6ba60Sopenharmony_ci msgOpt.set_vint_enum(ProtoEncoder::NUM::ZERO); 37306f6ba60Sopenharmony_ci printf("set_vint_enum(%d):\n", static_cast<int>(ProtoEncoder::NUM::ZERO)); 37406f6ba60Sopenharmony_ci for (uint32_t i = 0; i < g_writePos; i++) { 37506f6ba60Sopenharmony_ci printf("%02X ", g_buf[i]); 37606f6ba60Sopenharmony_ci } 37706f6ba60Sopenharmony_ci printf("\n"); 37806f6ba60Sopenharmony_ci ASSERT_TRUE(msgProtobuf.ParseFromArray(g_buf, g_writePos)); 37906f6ba60Sopenharmony_ci ASSERT_EQ(msgProtobuf.vint_enum(), static_cast<int>(ProtoEncoder::NUM::ZERO)); 38006f6ba60Sopenharmony_ci 38106f6ba60Sopenharmony_ci // max 38206f6ba60Sopenharmony_ci g_writeCtx.seek(&g_writeCtx, 0); 38306f6ba60Sopenharmony_ci msgOpt.set_vint_enum(ProtoEncoder::NUM::FOUR); 38406f6ba60Sopenharmony_ci printf("set_vint_enum(%d):\n", static_cast<int>(ProtoEncoder::NUM::FOUR)); 38506f6ba60Sopenharmony_ci for (uint32_t i = 0; i < g_writePos; i++) { 38606f6ba60Sopenharmony_ci printf("%02X ", g_buf[i]); 38706f6ba60Sopenharmony_ci } 38806f6ba60Sopenharmony_ci printf("\n"); 38906f6ba60Sopenharmony_ci 39006f6ba60Sopenharmony_ci ASSERT_TRUE(msgProtobuf.ParseFromArray(g_buf, g_writePos)); 39106f6ba60Sopenharmony_ci ASSERT_EQ(msgProtobuf.vint_enum(), static_cast<int>(ProtoEncoder::NUM::FOUR)); 39206f6ba60Sopenharmony_ci} 39306f6ba60Sopenharmony_ci 39406f6ba60Sopenharmony_ciHWTEST_F(BaseMessageUnittest, SetFixed64, TestSize.Level1) 39506f6ba60Sopenharmony_ci{ 39606f6ba60Sopenharmony_ci ProtoEncoder::ExampleMessage msgOpt(&g_writeCtx); 39706f6ba60Sopenharmony_ci ExampleMessage msgProtobuf; 39806f6ba60Sopenharmony_ci 39906f6ba60Sopenharmony_ci g_writeCtx.seek(&g_writeCtx, 0); 40006f6ba60Sopenharmony_ci msgOpt.set_i64_fixed64(0); 40106f6ba60Sopenharmony_ci printf("set_i64_fixed64(0):\n"); 40206f6ba60Sopenharmony_ci for (uint32_t i = 0; i < g_writePos; i++) { 40306f6ba60Sopenharmony_ci printf("%02X ", g_buf[i]); 40406f6ba60Sopenharmony_ci } 40506f6ba60Sopenharmony_ci printf("\n"); 40606f6ba60Sopenharmony_ci ASSERT_TRUE(msgProtobuf.ParseFromArray(g_buf, g_writePos)); 40706f6ba60Sopenharmony_ci ASSERT_EQ(msgProtobuf.i64_fixed64(), (uint64_t)0); 40806f6ba60Sopenharmony_ci 40906f6ba60Sopenharmony_ci // min 41006f6ba60Sopenharmony_ci g_writeCtx.seek(&g_writeCtx, 0); 41106f6ba60Sopenharmony_ci msgOpt.set_i64_fixed64(std::numeric_limits<uint64_t>::min()); 41206f6ba60Sopenharmony_ci printf("set_i64_fixed64(%" PRIu64 "):\n", std::numeric_limits<uint64_t>::min()); 41306f6ba60Sopenharmony_ci for (uint32_t i = 0; i < g_writePos; i++) { 41406f6ba60Sopenharmony_ci printf("%02X ", g_buf[i]); 41506f6ba60Sopenharmony_ci } 41606f6ba60Sopenharmony_ci printf("\n"); 41706f6ba60Sopenharmony_ci ASSERT_TRUE(msgProtobuf.ParseFromArray(g_buf, g_writePos)); 41806f6ba60Sopenharmony_ci ASSERT_EQ(msgProtobuf.i64_fixed64(), std::numeric_limits<uint64_t>::min()); 41906f6ba60Sopenharmony_ci 42006f6ba60Sopenharmony_ci // max 42106f6ba60Sopenharmony_ci g_writeCtx.seek(&g_writeCtx, 0); 42206f6ba60Sopenharmony_ci msgOpt.set_i64_fixed64(std::numeric_limits<uint64_t>::max()); 42306f6ba60Sopenharmony_ci printf("set_i64_fixed64(%" PRIu64 "):\n", std::numeric_limits<uint64_t>::max()); 42406f6ba60Sopenharmony_ci for (uint32_t i = 0; i < g_writePos; i++) { 42506f6ba60Sopenharmony_ci printf("%02X ", g_buf[i]); 42606f6ba60Sopenharmony_ci } 42706f6ba60Sopenharmony_ci printf("\n"); 42806f6ba60Sopenharmony_ci 42906f6ba60Sopenharmony_ci ASSERT_TRUE(msgProtobuf.ParseFromArray(g_buf, g_writePos)); 43006f6ba60Sopenharmony_ci ASSERT_EQ(msgProtobuf.i64_fixed64(), std::numeric_limits<uint64_t>::max()); 43106f6ba60Sopenharmony_ci} 43206f6ba60Sopenharmony_ci 43306f6ba60Sopenharmony_ciHWTEST_F(BaseMessageUnittest, SetSfixed64, TestSize.Level1) 43406f6ba60Sopenharmony_ci{ 43506f6ba60Sopenharmony_ci ProtoEncoder::ExampleMessage msgOpt(&g_writeCtx); 43606f6ba60Sopenharmony_ci ExampleMessage msgProtobuf; 43706f6ba60Sopenharmony_ci 43806f6ba60Sopenharmony_ci g_writeCtx.seek(&g_writeCtx, 0); 43906f6ba60Sopenharmony_ci msgOpt.set_i64_sfixed64(0); 44006f6ba60Sopenharmony_ci printf("set_i64_sfixed64(0):\n"); 44106f6ba60Sopenharmony_ci for (uint32_t i = 0; i < g_writePos; i++) { 44206f6ba60Sopenharmony_ci printf("%02X ", g_buf[i]); 44306f6ba60Sopenharmony_ci } 44406f6ba60Sopenharmony_ci printf("\n"); 44506f6ba60Sopenharmony_ci ASSERT_TRUE(msgProtobuf.ParseFromArray(g_buf, g_writePos)); 44606f6ba60Sopenharmony_ci ASSERT_EQ(msgProtobuf.i64_sfixed64(), (int64_t)0); 44706f6ba60Sopenharmony_ci 44806f6ba60Sopenharmony_ci // min 44906f6ba60Sopenharmony_ci g_writeCtx.seek(&g_writeCtx, 0); 45006f6ba60Sopenharmony_ci msgOpt.set_i64_sfixed64(std::numeric_limits<int64_t>::min()); 45106f6ba60Sopenharmony_ci printf("set_i64_sfixed64(%" PRId64 "):\n", std::numeric_limits<int64_t>::min()); 45206f6ba60Sopenharmony_ci for (uint32_t i = 0; i < g_writePos; i++) { 45306f6ba60Sopenharmony_ci printf("%02X ", g_buf[i]); 45406f6ba60Sopenharmony_ci } 45506f6ba60Sopenharmony_ci printf("\n"); 45606f6ba60Sopenharmony_ci ASSERT_TRUE(msgProtobuf.ParseFromArray(g_buf, g_writePos)); 45706f6ba60Sopenharmony_ci ASSERT_EQ(msgProtobuf.i64_sfixed64(), std::numeric_limits<int64_t>::min()); 45806f6ba60Sopenharmony_ci 45906f6ba60Sopenharmony_ci // max 46006f6ba60Sopenharmony_ci g_writeCtx.seek(&g_writeCtx, 0); 46106f6ba60Sopenharmony_ci msgOpt.set_i64_sfixed64(std::numeric_limits<int64_t>::max()); 46206f6ba60Sopenharmony_ci printf("set_i64_sfixed64(%" PRId64 "):\n", std::numeric_limits<int64_t>::max()); 46306f6ba60Sopenharmony_ci for (uint32_t i = 0; i < g_writePos; i++) { 46406f6ba60Sopenharmony_ci printf("%02X ", g_buf[i]); 46506f6ba60Sopenharmony_ci } 46606f6ba60Sopenharmony_ci printf("\n"); 46706f6ba60Sopenharmony_ci 46806f6ba60Sopenharmony_ci ASSERT_TRUE(msgProtobuf.ParseFromArray(g_buf, g_writePos)); 46906f6ba60Sopenharmony_ci ASSERT_EQ(msgProtobuf.i64_sfixed64(), std::numeric_limits<int64_t>::max()); 47006f6ba60Sopenharmony_ci} 47106f6ba60Sopenharmony_ci 47206f6ba60Sopenharmony_ciHWTEST_F(BaseMessageUnittest, SetDouble, TestSize.Level1) 47306f6ba60Sopenharmony_ci{ 47406f6ba60Sopenharmony_ci ProtoEncoder::ExampleMessage msgOpt(&g_writeCtx); 47506f6ba60Sopenharmony_ci ExampleMessage msgProtobuf; 47606f6ba60Sopenharmony_ci 47706f6ba60Sopenharmony_ci g_writeCtx.seek(&g_writeCtx, 0); 47806f6ba60Sopenharmony_ci msgOpt.set_i64_double(0); 47906f6ba60Sopenharmony_ci printf("set_i64_double(0):\n"); 48006f6ba60Sopenharmony_ci for (uint32_t i = 0; i < g_writePos; i++) { 48106f6ba60Sopenharmony_ci printf("%02X ", g_buf[i]); 48206f6ba60Sopenharmony_ci } 48306f6ba60Sopenharmony_ci printf("\n"); 48406f6ba60Sopenharmony_ci ASSERT_TRUE(msgProtobuf.ParseFromArray(g_buf, g_writePos)); 48506f6ba60Sopenharmony_ci ASSERT_EQ(msgProtobuf.i64_double(), (double)0); 48606f6ba60Sopenharmony_ci 48706f6ba60Sopenharmony_ci // min 48806f6ba60Sopenharmony_ci g_writeCtx.seek(&g_writeCtx, 0); 48906f6ba60Sopenharmony_ci msgOpt.set_i64_double(std::numeric_limits<double>::min()); 49006f6ba60Sopenharmony_ci printf("set_i64_double(%e):\n", std::numeric_limits<double>::min()); 49106f6ba60Sopenharmony_ci for (uint32_t i = 0; i < g_writePos; i++) { 49206f6ba60Sopenharmony_ci printf("%02X ", g_buf[i]); 49306f6ba60Sopenharmony_ci } 49406f6ba60Sopenharmony_ci printf("\n"); 49506f6ba60Sopenharmony_ci ASSERT_TRUE(msgProtobuf.ParseFromArray(g_buf, g_writePos)); 49606f6ba60Sopenharmony_ci ASSERT_EQ(msgProtobuf.i64_double(), std::numeric_limits<double>::min()); 49706f6ba60Sopenharmony_ci 49806f6ba60Sopenharmony_ci // max 49906f6ba60Sopenharmony_ci g_writeCtx.seek(&g_writeCtx, 0); 50006f6ba60Sopenharmony_ci msgOpt.set_i64_double(std::numeric_limits<double>::max()); 50106f6ba60Sopenharmony_ci printf("set_i64_double(%e):\n", std::numeric_limits<double>::max()); 50206f6ba60Sopenharmony_ci for (uint32_t i = 0; i < g_writePos; i++) { 50306f6ba60Sopenharmony_ci printf("%02X ", g_buf[i]); 50406f6ba60Sopenharmony_ci } 50506f6ba60Sopenharmony_ci printf("\n"); 50606f6ba60Sopenharmony_ci 50706f6ba60Sopenharmony_ci ASSERT_TRUE(msgProtobuf.ParseFromArray(g_buf, g_writePos)); 50806f6ba60Sopenharmony_ci ASSERT_EQ(msgProtobuf.i64_double(), std::numeric_limits<double>::max()); 50906f6ba60Sopenharmony_ci} 51006f6ba60Sopenharmony_ci 51106f6ba60Sopenharmony_ciHWTEST_F(BaseMessageUnittest, SetFixed32, TestSize.Level1) 51206f6ba60Sopenharmony_ci{ 51306f6ba60Sopenharmony_ci ProtoEncoder::ExampleMessage msgOpt(&g_writeCtx); 51406f6ba60Sopenharmony_ci ExampleMessage msgProtobuf; 51506f6ba60Sopenharmony_ci 51606f6ba60Sopenharmony_ci g_writeCtx.seek(&g_writeCtx, 0); 51706f6ba60Sopenharmony_ci msgOpt.set_i32_fixed32(0); 51806f6ba60Sopenharmony_ci printf("set_i32_fixed32(0):\n"); 51906f6ba60Sopenharmony_ci for (uint32_t i = 0; i < g_writePos; i++) { 52006f6ba60Sopenharmony_ci printf("%02X ", g_buf[i]); 52106f6ba60Sopenharmony_ci } 52206f6ba60Sopenharmony_ci printf("\n"); 52306f6ba60Sopenharmony_ci ASSERT_TRUE(msgProtobuf.ParseFromArray(g_buf, g_writePos)); 52406f6ba60Sopenharmony_ci ASSERT_EQ(msgProtobuf.i32_fixed32(), (uint32_t)0); 52506f6ba60Sopenharmony_ci 52606f6ba60Sopenharmony_ci // min 52706f6ba60Sopenharmony_ci g_writeCtx.seek(&g_writeCtx, 0); 52806f6ba60Sopenharmony_ci msgOpt.set_i32_fixed32(std::numeric_limits<uint32_t>::min()); 52906f6ba60Sopenharmony_ci printf("set_i32_fixed32(%" PRIu32 "):\n", std::numeric_limits<uint32_t>::min()); 53006f6ba60Sopenharmony_ci for (uint32_t i = 0; i < g_writePos; i++) { 53106f6ba60Sopenharmony_ci printf("%02X ", g_buf[i]); 53206f6ba60Sopenharmony_ci } 53306f6ba60Sopenharmony_ci printf("\n"); 53406f6ba60Sopenharmony_ci ASSERT_TRUE(msgProtobuf.ParseFromArray(g_buf, g_writePos)); 53506f6ba60Sopenharmony_ci ASSERT_EQ(msgProtobuf.i32_fixed32(), std::numeric_limits<uint32_t>::min()); 53606f6ba60Sopenharmony_ci 53706f6ba60Sopenharmony_ci // max 53806f6ba60Sopenharmony_ci g_writeCtx.seek(&g_writeCtx, 0); 53906f6ba60Sopenharmony_ci msgOpt.set_i32_fixed32(std::numeric_limits<uint32_t>::max()); 54006f6ba60Sopenharmony_ci printf("set_i32_fixed32(%" PRIu32 "):\n", std::numeric_limits<uint32_t>::max()); 54106f6ba60Sopenharmony_ci for (uint32_t i = 0; i < g_writePos; i++) { 54206f6ba60Sopenharmony_ci printf("%02X ", g_buf[i]); 54306f6ba60Sopenharmony_ci } 54406f6ba60Sopenharmony_ci printf("\n"); 54506f6ba60Sopenharmony_ci 54606f6ba60Sopenharmony_ci ASSERT_TRUE(msgProtobuf.ParseFromArray(g_buf, g_writePos)); 54706f6ba60Sopenharmony_ci ASSERT_EQ(msgProtobuf.i32_fixed32(), std::numeric_limits<uint32_t>::max()); 54806f6ba60Sopenharmony_ci} 54906f6ba60Sopenharmony_ci 55006f6ba60Sopenharmony_ciHWTEST_F(BaseMessageUnittest, SetSfixed32, TestSize.Level1) 55106f6ba60Sopenharmony_ci{ 55206f6ba60Sopenharmony_ci ProtoEncoder::ExampleMessage msgOpt(&g_writeCtx); 55306f6ba60Sopenharmony_ci ExampleMessage msgProtobuf; 55406f6ba60Sopenharmony_ci 55506f6ba60Sopenharmony_ci g_writeCtx.seek(&g_writeCtx, 0); 55606f6ba60Sopenharmony_ci msgOpt.set_i32_sfixed32(0); 55706f6ba60Sopenharmony_ci printf("set_i32_sfixed32(0):\n"); 55806f6ba60Sopenharmony_ci for (uint32_t i = 0; i < g_writePos; i++) { 55906f6ba60Sopenharmony_ci printf("%02X ", g_buf[i]); 56006f6ba60Sopenharmony_ci } 56106f6ba60Sopenharmony_ci printf("\n"); 56206f6ba60Sopenharmony_ci ASSERT_TRUE(msgProtobuf.ParseFromArray(g_buf, g_writePos)); 56306f6ba60Sopenharmony_ci ASSERT_EQ(msgProtobuf.i32_sfixed32(), (int32_t)0); 56406f6ba60Sopenharmony_ci 56506f6ba60Sopenharmony_ci // min 56606f6ba60Sopenharmony_ci g_writeCtx.seek(&g_writeCtx, 0); 56706f6ba60Sopenharmony_ci msgOpt.set_i32_sfixed32(std::numeric_limits<int32_t>::min()); 56806f6ba60Sopenharmony_ci printf("set_i32_sfixed32(%" PRId32 "):\n", std::numeric_limits<int32_t>::min()); 56906f6ba60Sopenharmony_ci for (uint32_t i = 0; i < g_writePos; i++) { 57006f6ba60Sopenharmony_ci printf("%02X ", g_buf[i]); 57106f6ba60Sopenharmony_ci } 57206f6ba60Sopenharmony_ci printf("\n"); 57306f6ba60Sopenharmony_ci ASSERT_TRUE(msgProtobuf.ParseFromArray(g_buf, g_writePos)); 57406f6ba60Sopenharmony_ci ASSERT_EQ(msgProtobuf.i32_sfixed32(), std::numeric_limits<int32_t>::min()); 57506f6ba60Sopenharmony_ci 57606f6ba60Sopenharmony_ci // max 57706f6ba60Sopenharmony_ci g_writeCtx.seek(&g_writeCtx, 0); 57806f6ba60Sopenharmony_ci msgOpt.set_i32_sfixed32(std::numeric_limits<int32_t>::max()); 57906f6ba60Sopenharmony_ci printf("set_i32_sfixed32(%" PRId32 "):\n", std::numeric_limits<int32_t>::max()); 58006f6ba60Sopenharmony_ci for (uint32_t i = 0; i < g_writePos; i++) { 58106f6ba60Sopenharmony_ci printf("%02X ", g_buf[i]); 58206f6ba60Sopenharmony_ci } 58306f6ba60Sopenharmony_ci printf("\n"); 58406f6ba60Sopenharmony_ci 58506f6ba60Sopenharmony_ci ASSERT_TRUE(msgProtobuf.ParseFromArray(g_buf, g_writePos)); 58606f6ba60Sopenharmony_ci ASSERT_EQ(msgProtobuf.i32_sfixed32(), std::numeric_limits<int32_t>::max()); 58706f6ba60Sopenharmony_ci} 58806f6ba60Sopenharmony_ci 58906f6ba60Sopenharmony_ciHWTEST_F(BaseMessageUnittest, SetFloat, TestSize.Level1) 59006f6ba60Sopenharmony_ci{ 59106f6ba60Sopenharmony_ci ProtoEncoder::ExampleMessage msgOpt(&g_writeCtx); 59206f6ba60Sopenharmony_ci ExampleMessage msgProtobuf; 59306f6ba60Sopenharmony_ci 59406f6ba60Sopenharmony_ci g_writeCtx.seek(&g_writeCtx, 0); 59506f6ba60Sopenharmony_ci msgOpt.set_i32_float(0); 59606f6ba60Sopenharmony_ci printf("set_i32_float(0):\n"); 59706f6ba60Sopenharmony_ci for (uint32_t i = 0; i < g_writePos; i++) { 59806f6ba60Sopenharmony_ci printf("%02X ", g_buf[i]); 59906f6ba60Sopenharmony_ci } 60006f6ba60Sopenharmony_ci printf("\n"); 60106f6ba60Sopenharmony_ci ASSERT_TRUE(msgProtobuf.ParseFromArray(g_buf, g_writePos)); 60206f6ba60Sopenharmony_ci ASSERT_EQ(msgProtobuf.i32_float(), (float)0); 60306f6ba60Sopenharmony_ci 60406f6ba60Sopenharmony_ci // min 60506f6ba60Sopenharmony_ci g_writeCtx.seek(&g_writeCtx, 0); 60606f6ba60Sopenharmony_ci msgOpt.set_i32_float(std::numeric_limits<float>::min()); 60706f6ba60Sopenharmony_ci printf("set_i32_float(%e):\n", std::numeric_limits<float>::min()); 60806f6ba60Sopenharmony_ci for (uint32_t i = 0; i < g_writePos; i++) { 60906f6ba60Sopenharmony_ci printf("%02X ", g_buf[i]); 61006f6ba60Sopenharmony_ci } 61106f6ba60Sopenharmony_ci printf("\n"); 61206f6ba60Sopenharmony_ci ASSERT_TRUE(msgProtobuf.ParseFromArray(g_buf, g_writePos)); 61306f6ba60Sopenharmony_ci ASSERT_EQ(msgProtobuf.i32_float(), std::numeric_limits<float>::min()); 61406f6ba60Sopenharmony_ci 61506f6ba60Sopenharmony_ci // max 61606f6ba60Sopenharmony_ci g_writeCtx.seek(&g_writeCtx, 0); 61706f6ba60Sopenharmony_ci msgOpt.set_i32_float(std::numeric_limits<float>::max()); 61806f6ba60Sopenharmony_ci printf("set_i32_float(%e):\n", std::numeric_limits<float>::max()); 61906f6ba60Sopenharmony_ci for (uint32_t i = 0; i < g_writePos; i++) { 62006f6ba60Sopenharmony_ci printf("%02X ", g_buf[i]); 62106f6ba60Sopenharmony_ci } 62206f6ba60Sopenharmony_ci printf("\n"); 62306f6ba60Sopenharmony_ci 62406f6ba60Sopenharmony_ci ASSERT_TRUE(msgProtobuf.ParseFromArray(g_buf, g_writePos)); 62506f6ba60Sopenharmony_ci ASSERT_EQ(msgProtobuf.i32_float(), std::numeric_limits<float>::max()); 62606f6ba60Sopenharmony_ci} 62706f6ba60Sopenharmony_ci 62806f6ba60Sopenharmony_ciHWTEST_F(BaseMessageUnittest, SetString, TestSize.Level1) 62906f6ba60Sopenharmony_ci{ 63006f6ba60Sopenharmony_ci const std::string emptyStr = ""; 63106f6ba60Sopenharmony_ci const std::string str = "this is test."; 63206f6ba60Sopenharmony_ci ProtoEncoder::ExampleMessage msgOpt(&g_writeCtx); 63306f6ba60Sopenharmony_ci ExampleMessage msgProtobuf; 63406f6ba60Sopenharmony_ci 63506f6ba60Sopenharmony_ci g_writeCtx.seek(&g_writeCtx, 0); 63606f6ba60Sopenharmony_ci msgOpt.set_len_string(emptyStr); 63706f6ba60Sopenharmony_ci printf("set_len_string(empty string):\n"); 63806f6ba60Sopenharmony_ci for (uint32_t i = 0; i < g_writePos; i++) { 63906f6ba60Sopenharmony_ci printf("%02X ", g_buf[i]); 64006f6ba60Sopenharmony_ci } 64106f6ba60Sopenharmony_ci printf("\n"); 64206f6ba60Sopenharmony_ci // data of g_buf should is "AA 01 00" 64306f6ba60Sopenharmony_ci const int32_t emptyStrFieldLen = 3; 64406f6ba60Sopenharmony_ci ASSERT_EQ(msgOpt.Size(), emptyStrFieldLen); 64506f6ba60Sopenharmony_ci ASSERT_TRUE(msgProtobuf.ParseFromArray(g_buf, g_writePos)); 64606f6ba60Sopenharmony_ci ASSERT_EQ(msgProtobuf.len_string(), emptyStr); 64706f6ba60Sopenharmony_ci 64806f6ba60Sopenharmony_ci g_writeCtx.seek(&g_writeCtx, 0); 64906f6ba60Sopenharmony_ci msgOpt.set_len_string(str); 65006f6ba60Sopenharmony_ci printf("set_len_string(string: %s):\n", str.c_str()); 65106f6ba60Sopenharmony_ci for (uint32_t i = 0; i < g_writePos; i++) { 65206f6ba60Sopenharmony_ci printf("%02X ", g_buf[i]); 65306f6ba60Sopenharmony_ci } 65406f6ba60Sopenharmony_ci printf("\n"); 65506f6ba60Sopenharmony_ci ASSERT_TRUE(msgProtobuf.ParseFromArray(g_buf, g_writePos)); 65606f6ba60Sopenharmony_ci ASSERT_EQ(msgProtobuf.len_string(), str); 65706f6ba60Sopenharmony_ci 65806f6ba60Sopenharmony_ci g_writeCtx.seek(&g_writeCtx, 0); 65906f6ba60Sopenharmony_ci msgOpt.set_len_string(str.c_str()); 66006f6ba60Sopenharmony_ci printf("set_len_string(char*: %s):\n", str.c_str()); 66106f6ba60Sopenharmony_ci for (uint32_t i = 0; i < g_writePos; i++) { 66206f6ba60Sopenharmony_ci printf("%02X ", g_buf[i]); 66306f6ba60Sopenharmony_ci } 66406f6ba60Sopenharmony_ci printf("\n"); 66506f6ba60Sopenharmony_ci 66606f6ba60Sopenharmony_ci ASSERT_TRUE(msgProtobuf.ParseFromArray(g_buf, g_writePos)); 66706f6ba60Sopenharmony_ci ASSERT_EQ(msgProtobuf.len_string(), str); 66806f6ba60Sopenharmony_ci} 66906f6ba60Sopenharmony_ci 67006f6ba60Sopenharmony_ciHWTEST_F(BaseMessageUnittest, SetSubMessage, TestSize.Level1) 67106f6ba60Sopenharmony_ci{ 67206f6ba60Sopenharmony_ci ProtoEncoder::ExampleMessage msgOpt(&g_writeCtx); 67306f6ba60Sopenharmony_ci ExampleMessage msgProtobuf; 67406f6ba60Sopenharmony_ci 67506f6ba60Sopenharmony_ci // empty 67606f6ba60Sopenharmony_ci g_writeCtx.seek(&g_writeCtx, 0); 67706f6ba60Sopenharmony_ci ProtoEncoder::SubMessage* subMsgOpt = msgOpt.mutable_len_sub(); 67806f6ba60Sopenharmony_ci ASSERT_TRUE(subMsgOpt != nullptr); 67906f6ba60Sopenharmony_ci msgOpt.set_vint_int32(std::numeric_limits<int32_t>::min()); 68006f6ba60Sopenharmony_ci printf("mutable_len_sub: nothing:\n"); 68106f6ba60Sopenharmony_ci for (uint32_t i = 0; i < g_writePos; i++) { 68206f6ba60Sopenharmony_ci printf("%02X ", g_buf[i]); 68306f6ba60Sopenharmony_ci } 68406f6ba60Sopenharmony_ci printf("\n"); 68506f6ba60Sopenharmony_ci 68606f6ba60Sopenharmony_ci ASSERT_TRUE(msgProtobuf.ParseFromArray(g_buf, g_writePos)); 68706f6ba60Sopenharmony_ci ASSERT_TRUE(msgProtobuf.has_len_sub()); 68806f6ba60Sopenharmony_ci 68906f6ba60Sopenharmony_ci // min 69006f6ba60Sopenharmony_ci g_writeCtx.seek(&g_writeCtx, 0); 69106f6ba60Sopenharmony_ci subMsgOpt = msgOpt.mutable_len_sub(); 69206f6ba60Sopenharmony_ci ASSERT_TRUE(subMsgOpt != nullptr); 69306f6ba60Sopenharmony_ci subMsgOpt->set_vint_int32(std::numeric_limits<int32_t>::min()); 69406f6ba60Sopenharmony_ci subMsgOpt->set_vint_int64(std::numeric_limits<int64_t>::min()); 69506f6ba60Sopenharmony_ci subMsgOpt->set_vint_uint32(std::numeric_limits<uint32_t>::min()); 69606f6ba60Sopenharmony_ci subMsgOpt->set_vint_uint64(std::numeric_limits<uint64_t>::min()); 69706f6ba60Sopenharmony_ci 69806f6ba60Sopenharmony_ci msgOpt.set_vint_int32(std::numeric_limits<int32_t>::min()); 69906f6ba60Sopenharmony_ci printf("mutable_len_sub: min:\n"); 70006f6ba60Sopenharmony_ci for (uint32_t i = 0; i < g_writePos; i++) { 70106f6ba60Sopenharmony_ci printf("%02X ", g_buf[i]); 70206f6ba60Sopenharmony_ci } 70306f6ba60Sopenharmony_ci printf("\n"); 70406f6ba60Sopenharmony_ci 70506f6ba60Sopenharmony_ci ASSERT_TRUE(msgProtobuf.ParseFromArray(g_buf, g_writePos)); 70606f6ba60Sopenharmony_ci ASSERT_TRUE(msgProtobuf.has_len_sub()); 70706f6ba60Sopenharmony_ci SubMessage subMsgProtobuf = msgProtobuf.len_sub(); 70806f6ba60Sopenharmony_ci ASSERT_EQ(subMsgProtobuf.vint_int32(), std::numeric_limits<int32_t>::min()); 70906f6ba60Sopenharmony_ci ASSERT_EQ(subMsgProtobuf.vint_int64(), std::numeric_limits<int64_t>::min()); 71006f6ba60Sopenharmony_ci ASSERT_EQ(subMsgProtobuf.vint_uint32(), std::numeric_limits<uint32_t>::min()); 71106f6ba60Sopenharmony_ci ASSERT_EQ(subMsgProtobuf.vint_uint64(), std::numeric_limits<uint64_t>::min()); 71206f6ba60Sopenharmony_ci 71306f6ba60Sopenharmony_ci // max 71406f6ba60Sopenharmony_ci g_writeCtx.seek(&g_writeCtx, 0); 71506f6ba60Sopenharmony_ci subMsgOpt = msgOpt.mutable_len_sub(); 71606f6ba60Sopenharmony_ci ASSERT_TRUE(subMsgOpt != nullptr); 71706f6ba60Sopenharmony_ci subMsgOpt->set_vint_int32(std::numeric_limits<int32_t>::max()); 71806f6ba60Sopenharmony_ci subMsgOpt->set_vint_int64(std::numeric_limits<int64_t>::max()); 71906f6ba60Sopenharmony_ci subMsgOpt->set_vint_uint32(std::numeric_limits<uint32_t>::max()); 72006f6ba60Sopenharmony_ci subMsgOpt->set_vint_uint64(std::numeric_limits<uint64_t>::max()); 72106f6ba60Sopenharmony_ci 72206f6ba60Sopenharmony_ci msgOpt.set_vint_int32(std::numeric_limits<int32_t>::min()); 72306f6ba60Sopenharmony_ci printf("mutable_len_sub: max:\n"); 72406f6ba60Sopenharmony_ci for (uint32_t i = 0; i < g_writePos; i++) { 72506f6ba60Sopenharmony_ci printf("%02X ", g_buf[i]); 72606f6ba60Sopenharmony_ci } 72706f6ba60Sopenharmony_ci printf("\n"); 72806f6ba60Sopenharmony_ci 72906f6ba60Sopenharmony_ci ASSERT_TRUE(msgProtobuf.ParseFromArray(g_buf, g_writePos)); 73006f6ba60Sopenharmony_ci ASSERT_TRUE(msgProtobuf.has_len_sub()); 73106f6ba60Sopenharmony_ci subMsgProtobuf = msgProtobuf.len_sub(); 73206f6ba60Sopenharmony_ci ASSERT_EQ(subMsgProtobuf.vint_int32(), std::numeric_limits<int32_t>::max()); 73306f6ba60Sopenharmony_ci ASSERT_EQ(subMsgProtobuf.vint_int64(), std::numeric_limits<int64_t>::max()); 73406f6ba60Sopenharmony_ci ASSERT_EQ(subMsgProtobuf.vint_uint32(), std::numeric_limits<uint32_t>::max()); 73506f6ba60Sopenharmony_ci ASSERT_EQ(subMsgProtobuf.vint_uint64(), std::numeric_limits<uint64_t>::max()); 73606f6ba60Sopenharmony_ci} 73706f6ba60Sopenharmony_ci 73806f6ba60Sopenharmony_ciHWTEST_F(BaseMessageUnittest, SetOneOf, TestSize.Level1) 73906f6ba60Sopenharmony_ci{ 74006f6ba60Sopenharmony_ci const std::string str = "this is test."; 74106f6ba60Sopenharmony_ci ProtoEncoder::ExampleMessage msgOpt(&g_writeCtx); 74206f6ba60Sopenharmony_ci ExampleMessage msgProtobuf; 74306f6ba60Sopenharmony_ci 74406f6ba60Sopenharmony_ci // oneof_fixed64 74506f6ba60Sopenharmony_ci g_writeCtx.seek(&g_writeCtx, 0); 74606f6ba60Sopenharmony_ci msgOpt.set_oneof_fixed64(std::numeric_limits<uint64_t>::max()); 74706f6ba60Sopenharmony_ci printf("set_oneof_fixed64(%" PRIu64"):\n", std::numeric_limits<uint64_t>::max()); 74806f6ba60Sopenharmony_ci for (uint32_t i = 0; i < g_writePos; i++) { 74906f6ba60Sopenharmony_ci printf("%02X ", g_buf[i]); 75006f6ba60Sopenharmony_ci } 75106f6ba60Sopenharmony_ci printf("\n"); 75206f6ba60Sopenharmony_ci 75306f6ba60Sopenharmony_ci ASSERT_TRUE(msgProtobuf.ParseFromArray(g_buf, g_writePos)); 75406f6ba60Sopenharmony_ci ASSERT_EQ(msgProtobuf.oneof_fixed64(), std::numeric_limits<uint64_t>::max()); 75506f6ba60Sopenharmony_ci 75606f6ba60Sopenharmony_ci // oneof_sub 75706f6ba60Sopenharmony_ci ProtoEncoder::SubMessage* subMsgOpt = msgOpt.mutable_oneof_sub(); 75806f6ba60Sopenharmony_ci ASSERT_TRUE(subMsgOpt != nullptr); 75906f6ba60Sopenharmony_ci subMsgOpt->set_vint_int32(std::numeric_limits<int32_t>::max()); 76006f6ba60Sopenharmony_ci subMsgOpt->set_vint_int64(std::numeric_limits<int64_t>::max()); 76106f6ba60Sopenharmony_ci subMsgOpt->set_vint_uint32(std::numeric_limits<uint32_t>::max()); 76206f6ba60Sopenharmony_ci subMsgOpt->set_vint_uint64(std::numeric_limits<uint64_t>::max()); 76306f6ba60Sopenharmony_ci msgOpt.set_i32_fixed32(0); 76406f6ba60Sopenharmony_ci printf("mutable_oneof_sub: max:\n"); 76506f6ba60Sopenharmony_ci for (uint32_t i = 0; i < g_writePos; i++) { 76606f6ba60Sopenharmony_ci printf("%02X ", g_buf[i]); 76706f6ba60Sopenharmony_ci } 76806f6ba60Sopenharmony_ci printf("\n"); 76906f6ba60Sopenharmony_ci 77006f6ba60Sopenharmony_ci ASSERT_TRUE(msgProtobuf.ParseFromArray(g_buf, g_writePos)); 77106f6ba60Sopenharmony_ci ASSERT_TRUE(msgProtobuf.has_oneof_sub()); 77206f6ba60Sopenharmony_ci SubMessage subMsgProtobuf = msgProtobuf.oneof_sub(); 77306f6ba60Sopenharmony_ci ASSERT_EQ(subMsgProtobuf.vint_int32(), std::numeric_limits<int32_t>::max()); 77406f6ba60Sopenharmony_ci ASSERT_EQ(subMsgProtobuf.vint_int64(), std::numeric_limits<int64_t>::max()); 77506f6ba60Sopenharmony_ci ASSERT_EQ(subMsgProtobuf.vint_uint32(), std::numeric_limits<uint32_t>::max()); 77606f6ba60Sopenharmony_ci ASSERT_EQ(subMsgProtobuf.vint_uint64(), std::numeric_limits<uint64_t>::max()); 77706f6ba60Sopenharmony_ci 77806f6ba60Sopenharmony_ci // oneof_string 77906f6ba60Sopenharmony_ci msgOpt.set_oneof_string(str); 78006f6ba60Sopenharmony_ci printf("set_oneof_string(%s):\n", str.c_str()); 78106f6ba60Sopenharmony_ci for (uint32_t i = 0; i < g_writePos; i++) { 78206f6ba60Sopenharmony_ci printf("%02X ", g_buf[i]); 78306f6ba60Sopenharmony_ci } 78406f6ba60Sopenharmony_ci printf("\n"); 78506f6ba60Sopenharmony_ci 78606f6ba60Sopenharmony_ci ASSERT_TRUE(msgProtobuf.ParseFromArray(g_buf, g_writePos)); 78706f6ba60Sopenharmony_ci ASSERT_EQ(msgProtobuf.oneof_string(), str); 78806f6ba60Sopenharmony_ci 78906f6ba60Sopenharmony_ci // last one wins 79006f6ba60Sopenharmony_ci ASSERT_NE(msgProtobuf.oneof_fixed64(), std::numeric_limits<uint64_t>::max()); 79106f6ba60Sopenharmony_ci ASSERT_FALSE(msgProtobuf.has_oneof_sub()); 79206f6ba60Sopenharmony_ci} 79306f6ba60Sopenharmony_ci 79406f6ba60Sopenharmony_ciHWTEST_F(BaseMessageUnittest, SetRepeatedPackedVarintSigned, TestSize.Level1) 79506f6ba60Sopenharmony_ci{ 79606f6ba60Sopenharmony_ci ProtoEncoder::ExampleMessage msgOpt(&g_writeCtx); 79706f6ba60Sopenharmony_ci ExampleMessage msgProtobuf; 79806f6ba60Sopenharmony_ci const int32_t array[] = {0, 1, 79906f6ba60Sopenharmony_ci std::numeric_limits<int32_t>::min(), 80006f6ba60Sopenharmony_ci std::numeric_limits<int32_t>::max()}; 80106f6ba60Sopenharmony_ci std::vector<int32_t> vec; 80206f6ba60Sopenharmony_ci vec.emplace_back(0); 80306f6ba60Sopenharmony_ci vec.emplace_back(1); 80406f6ba60Sopenharmony_ci vec.emplace_back(std::numeric_limits<int32_t>::min()); 80506f6ba60Sopenharmony_ci vec.emplace_back(std::numeric_limits<int32_t>::max()); 80606f6ba60Sopenharmony_ci 80706f6ba60Sopenharmony_ci // vector 80806f6ba60Sopenharmony_ci g_writeCtx.seek(&g_writeCtx, 0); 80906f6ba60Sopenharmony_ci msgOpt.add_len_repeated_packed_signed_vint(vec); 81006f6ba60Sopenharmony_ci printf("add_len_repeated_packed_signed_vint(vector):\n"); 81106f6ba60Sopenharmony_ci for (uint32_t i = 0; i < g_writePos; i++) { 81206f6ba60Sopenharmony_ci printf("%02X ", g_buf[i]); 81306f6ba60Sopenharmony_ci } 81406f6ba60Sopenharmony_ci printf("\n"); 81506f6ba60Sopenharmony_ci 81606f6ba60Sopenharmony_ci ASSERT_TRUE(msgProtobuf.ParseFromArray(g_buf, g_writePos)); 81706f6ba60Sopenharmony_ci int count = msgProtobuf.len_repeated_packed_signed_vint_size(); 81806f6ba60Sopenharmony_ci ASSERT_EQ(count, static_cast<int>(vec.size())); 81906f6ba60Sopenharmony_ci for (int i = 0; i < count; i++) { 82006f6ba60Sopenharmony_ci ASSERT_EQ(msgProtobuf.len_repeated_packed_signed_vint(i), vec[i]); 82106f6ba60Sopenharmony_ci } 82206f6ba60Sopenharmony_ci 82306f6ba60Sopenharmony_ci // array 82406f6ba60Sopenharmony_ci msgOpt.add_len_repeated_packed_signed_vint(array, sizeof(array) / sizeof(array[0])); 82506f6ba60Sopenharmony_ci printf("add_len_repeated_packed_signed_vint(array):\n"); 82606f6ba60Sopenharmony_ci for (uint32_t i = 0; i < g_writePos; i++) { 82706f6ba60Sopenharmony_ci printf("%02X ", g_buf[i]); 82806f6ba60Sopenharmony_ci } 82906f6ba60Sopenharmony_ci printf("\n"); 83006f6ba60Sopenharmony_ci 83106f6ba60Sopenharmony_ci ASSERT_TRUE(msgProtobuf.ParseFromArray(g_buf, g_writePos)); 83206f6ba60Sopenharmony_ci int countAll = msgProtobuf.len_repeated_packed_signed_vint_size(); 83306f6ba60Sopenharmony_ci ASSERT_EQ(countAll, static_cast<int>(vec.size() + sizeof(array) / sizeof(array[0]))); 83406f6ba60Sopenharmony_ci for (int i = 0; i < count; i++) { 83506f6ba60Sopenharmony_ci ASSERT_EQ(msgProtobuf.len_repeated_packed_signed_vint(i), vec[i]); 83606f6ba60Sopenharmony_ci } 83706f6ba60Sopenharmony_ci for (int i = count; i < countAll; i++) { 83806f6ba60Sopenharmony_ci ASSERT_EQ(msgProtobuf.len_repeated_packed_signed_vint(i), array[i - count]); 83906f6ba60Sopenharmony_ci } 84006f6ba60Sopenharmony_ci} 84106f6ba60Sopenharmony_ci 84206f6ba60Sopenharmony_ciHWTEST_F(BaseMessageUnittest, SetRepeatedPackedVarintUnsigned, TestSize.Level1) 84306f6ba60Sopenharmony_ci{ 84406f6ba60Sopenharmony_ci ProtoEncoder::ExampleMessage msgOpt(&g_writeCtx); 84506f6ba60Sopenharmony_ci ExampleMessage msgProtobuf; 84606f6ba60Sopenharmony_ci const uint32_t array[] = {0, 1, 84706f6ba60Sopenharmony_ci std::numeric_limits<uint32_t>::min(), 84806f6ba60Sopenharmony_ci std::numeric_limits<uint32_t>::max()}; 84906f6ba60Sopenharmony_ci std::vector<uint32_t> vec; 85006f6ba60Sopenharmony_ci vec.emplace_back(0); 85106f6ba60Sopenharmony_ci vec.emplace_back(1); 85206f6ba60Sopenharmony_ci vec.emplace_back(std::numeric_limits<uint32_t>::min()); 85306f6ba60Sopenharmony_ci vec.emplace_back(std::numeric_limits<uint32_t>::max()); 85406f6ba60Sopenharmony_ci 85506f6ba60Sopenharmony_ci // vector 85606f6ba60Sopenharmony_ci g_writeCtx.seek(&g_writeCtx, 0); 85706f6ba60Sopenharmony_ci msgOpt.add_len_repeated_packed_unsigned_vint(vec); 85806f6ba60Sopenharmony_ci printf("add_len_repeated_packed_unsigned_vint(vec):\n"); 85906f6ba60Sopenharmony_ci for (uint32_t i = 0; i < g_writePos; i++) { 86006f6ba60Sopenharmony_ci printf("%02X ", g_buf[i]); 86106f6ba60Sopenharmony_ci } 86206f6ba60Sopenharmony_ci printf("\n"); 86306f6ba60Sopenharmony_ci 86406f6ba60Sopenharmony_ci ASSERT_TRUE(msgProtobuf.ParseFromArray(g_buf, g_writePos)); 86506f6ba60Sopenharmony_ci int count = msgProtobuf.len_repeated_packed_unsigned_vint_size(); 86606f6ba60Sopenharmony_ci ASSERT_EQ(count, static_cast<int>(vec.size())); 86706f6ba60Sopenharmony_ci for (int i = 0; i < count; i++) { 86806f6ba60Sopenharmony_ci ASSERT_EQ(msgProtobuf.len_repeated_packed_unsigned_vint(i), vec[i]); 86906f6ba60Sopenharmony_ci } 87006f6ba60Sopenharmony_ci 87106f6ba60Sopenharmony_ci // array 87206f6ba60Sopenharmony_ci msgOpt.add_len_repeated_packed_unsigned_vint(array, sizeof(array) / sizeof(array[0])); 87306f6ba60Sopenharmony_ci printf("add_len_repeated_packed_unsigned_vint(array):\n"); 87406f6ba60Sopenharmony_ci for (uint32_t i = 0; i < g_writePos; i++) { 87506f6ba60Sopenharmony_ci printf("%02X ", g_buf[i]); 87606f6ba60Sopenharmony_ci } 87706f6ba60Sopenharmony_ci printf("\n"); 87806f6ba60Sopenharmony_ci 87906f6ba60Sopenharmony_ci ASSERT_TRUE(msgProtobuf.ParseFromArray(g_buf, g_writePos)); 88006f6ba60Sopenharmony_ci int countAll = msgProtobuf.len_repeated_packed_unsigned_vint_size(); 88106f6ba60Sopenharmony_ci ASSERT_EQ(countAll, static_cast<int>(vec.size() + sizeof(array) / sizeof(array[0]))); 88206f6ba60Sopenharmony_ci for (int i = 0; i < count; i++) { 88306f6ba60Sopenharmony_ci ASSERT_EQ(msgProtobuf.len_repeated_packed_unsigned_vint(i), vec[i]); 88406f6ba60Sopenharmony_ci } 88506f6ba60Sopenharmony_ci for (int i = count; i < countAll; i++) { 88606f6ba60Sopenharmony_ci ASSERT_EQ(msgProtobuf.len_repeated_packed_unsigned_vint(i), array[i - count]); 88706f6ba60Sopenharmony_ci } 88806f6ba60Sopenharmony_ci} 88906f6ba60Sopenharmony_ci 89006f6ba60Sopenharmony_ciHWTEST_F(BaseMessageUnittest, SetRepeatedPackedFixed64, TestSize.Level1) 89106f6ba60Sopenharmony_ci{ 89206f6ba60Sopenharmony_ci ProtoEncoder::ExampleMessage msgOpt(&g_writeCtx); 89306f6ba60Sopenharmony_ci ExampleMessage msgProtobuf; 89406f6ba60Sopenharmony_ci const uint64_t array[] = {0, 1, 89506f6ba60Sopenharmony_ci std::numeric_limits<uint64_t>::min(), 89606f6ba60Sopenharmony_ci std::numeric_limits<uint64_t>::max()}; 89706f6ba60Sopenharmony_ci std::vector<uint64_t> vec; 89806f6ba60Sopenharmony_ci vec.emplace_back(0); 89906f6ba60Sopenharmony_ci vec.emplace_back(1); 90006f6ba60Sopenharmony_ci vec.emplace_back(std::numeric_limits<uint64_t>::min()); 90106f6ba60Sopenharmony_ci vec.emplace_back(std::numeric_limits<uint64_t>::max()); 90206f6ba60Sopenharmony_ci 90306f6ba60Sopenharmony_ci // vector 90406f6ba60Sopenharmony_ci g_writeCtx.seek(&g_writeCtx, 0); 90506f6ba60Sopenharmony_ci msgOpt.add_len_repeated_packed_fixed(vec); 90606f6ba60Sopenharmony_ci printf("add_len_repeated_packed_fixed(vec):\n"); 90706f6ba60Sopenharmony_ci for (uint32_t i = 0; i < g_writePos; i++) { 90806f6ba60Sopenharmony_ci printf("%02X ", g_buf[i]); 90906f6ba60Sopenharmony_ci } 91006f6ba60Sopenharmony_ci printf("\n"); 91106f6ba60Sopenharmony_ci 91206f6ba60Sopenharmony_ci ASSERT_TRUE(msgProtobuf.ParseFromArray(g_buf, g_writePos)); 91306f6ba60Sopenharmony_ci int count = msgProtobuf.len_repeated_packed_fixed_size(); 91406f6ba60Sopenharmony_ci ASSERT_EQ(count, static_cast<int>(vec.size())); 91506f6ba60Sopenharmony_ci for (int i = 0; i < count; i++) { 91606f6ba60Sopenharmony_ci ASSERT_EQ(msgProtobuf.len_repeated_packed_fixed(i), vec[i]); 91706f6ba60Sopenharmony_ci } 91806f6ba60Sopenharmony_ci 91906f6ba60Sopenharmony_ci // array 92006f6ba60Sopenharmony_ci msgOpt.add_len_repeated_packed_fixed(array, sizeof(array) / sizeof(array[0])); 92106f6ba60Sopenharmony_ci printf("add_len_repeated_packed_fixed(array):\n"); 92206f6ba60Sopenharmony_ci for (uint32_t i = 0; i < g_writePos; i++) { 92306f6ba60Sopenharmony_ci printf("%02X ", g_buf[i]); 92406f6ba60Sopenharmony_ci } 92506f6ba60Sopenharmony_ci printf("\n"); 92606f6ba60Sopenharmony_ci 92706f6ba60Sopenharmony_ci ASSERT_TRUE(msgProtobuf.ParseFromArray(g_buf, g_writePos)); 92806f6ba60Sopenharmony_ci int countAll = msgProtobuf.len_repeated_packed_fixed_size(); 92906f6ba60Sopenharmony_ci ASSERT_EQ(countAll, static_cast<int>(vec.size() + sizeof(array) / sizeof(array[0]))); 93006f6ba60Sopenharmony_ci for (int i = 0; i < count; i++) { 93106f6ba60Sopenharmony_ci ASSERT_EQ(msgProtobuf.len_repeated_packed_fixed(i), vec[i]); 93206f6ba60Sopenharmony_ci } 93306f6ba60Sopenharmony_ci for (int i = count; i < countAll; i++) { 93406f6ba60Sopenharmony_ci ASSERT_EQ(msgProtobuf.len_repeated_packed_fixed(i), array[i - count]); 93506f6ba60Sopenharmony_ci } 93606f6ba60Sopenharmony_ci} 93706f6ba60Sopenharmony_ci 93806f6ba60Sopenharmony_cistatic const char BYTES_DATA[] = {0x00, 0x01, 0x02, 0xFD, 0xFE, 0xFF}; 93906f6ba60Sopenharmony_ci 94006f6ba60Sopenharmony_ciHWTEST_F(BaseMessageUnittest, SetBytes, TestSize.Level1) 94106f6ba60Sopenharmony_ci{ 94206f6ba60Sopenharmony_ci ProtoEncoder::ExampleMessage msgOpt(&g_writeCtx); 94306f6ba60Sopenharmony_ci ExampleMessage msgProtobuf; 94406f6ba60Sopenharmony_ci 94506f6ba60Sopenharmony_ci g_writeCtx.seek(&g_writeCtx, 0); 94606f6ba60Sopenharmony_ci msgOpt.set_len_bytes(BYTES_DATA, sizeof(BYTES_DATA)); 94706f6ba60Sopenharmony_ci printf("set_len_bytes(void*, size_t):\n"); 94806f6ba60Sopenharmony_ci for (uint32_t i = 0; i < g_writePos; i++) { 94906f6ba60Sopenharmony_ci printf("%02X ", g_buf[i]); 95006f6ba60Sopenharmony_ci } 95106f6ba60Sopenharmony_ci printf("\n"); 95206f6ba60Sopenharmony_ci 95306f6ba60Sopenharmony_ci ASSERT_TRUE(msgProtobuf.ParseFromArray(g_buf, g_writePos)); 95406f6ba60Sopenharmony_ci ASSERT_EQ(msgProtobuf.len_bytes(), std::string(&BYTES_DATA[0], sizeof(BYTES_DATA))); 95506f6ba60Sopenharmony_ci} 95606f6ba60Sopenharmony_ci 95706f6ba60Sopenharmony_ciHWTEST_F(BaseMessageUnittest, SetBytesCallback, TestSize.Level1) 95806f6ba60Sopenharmony_ci{ 95906f6ba60Sopenharmony_ci ProtoEncoder::ExampleMessage msgOpt(&g_writeCtx); 96006f6ba60Sopenharmony_ci ExampleMessage msgProtobuf; 96106f6ba60Sopenharmony_ci 96206f6ba60Sopenharmony_ci // bytes stram 96306f6ba60Sopenharmony_ci g_writeCtx.seek(&g_writeCtx, 0); 96406f6ba60Sopenharmony_ci msgOpt.set_len_bytes( 96506f6ba60Sopenharmony_ci [](RandomWriteCtx* ctx) -> int32_t { 96606f6ba60Sopenharmony_ci uint8_t* memory = nullptr; 96706f6ba60Sopenharmony_ci uint32_t offset = 0; 96806f6ba60Sopenharmony_ci if (ctx->getMemory(ctx, sizeof(BYTES_DATA), &memory, &offset)) { 96906f6ba60Sopenharmony_ci if (memcpy_s(memory, sizeof(BYTES_DATA), BYTES_DATA, sizeof(BYTES_DATA)) == EOK) { 97006f6ba60Sopenharmony_ci ctx->seek(ctx, offset + sizeof(BYTES_DATA)); 97106f6ba60Sopenharmony_ci return sizeof(BYTES_DATA); 97206f6ba60Sopenharmony_ci } 97306f6ba60Sopenharmony_ci } 97406f6ba60Sopenharmony_ci return 0; 97506f6ba60Sopenharmony_ci }); 97606f6ba60Sopenharmony_ci printf("set_len_bytes(CallbackFunc(bytes)):\n"); 97706f6ba60Sopenharmony_ci for (uint32_t i = 0; i < g_writePos; i++) { 97806f6ba60Sopenharmony_ci printf("%02X ", g_buf[i]); 97906f6ba60Sopenharmony_ci } 98006f6ba60Sopenharmony_ci printf("\n"); 98106f6ba60Sopenharmony_ci 98206f6ba60Sopenharmony_ci ASSERT_TRUE(msgProtobuf.ParseFromArray(g_buf, g_writePos)); 98306f6ba60Sopenharmony_ci ASSERT_EQ(msgProtobuf.len_bytes(), std::string(&BYTES_DATA[0], sizeof(BYTES_DATA))); 98406f6ba60Sopenharmony_ci 98506f6ba60Sopenharmony_ci // protobufOpt message 98606f6ba60Sopenharmony_ci g_writeCtx.seek(&g_writeCtx, 0); 98706f6ba60Sopenharmony_ci msgOpt.set_len_bytes( 98806f6ba60Sopenharmony_ci [](RandomWriteCtx* ctx) -> int32_t { 98906f6ba60Sopenharmony_ci ProtoEncoder::SubMessage subMsg(ctx); 99006f6ba60Sopenharmony_ci subMsg.set_vint_int32(std::numeric_limits<int32_t>::max()); 99106f6ba60Sopenharmony_ci subMsg.set_vint_int64(std::numeric_limits<int64_t>::max()); 99206f6ba60Sopenharmony_ci subMsg.set_vint_uint32(std::numeric_limits<uint32_t>::max()); 99306f6ba60Sopenharmony_ci subMsg.set_vint_uint64(std::numeric_limits<uint64_t>::max()); 99406f6ba60Sopenharmony_ci return subMsg.Finish(); 99506f6ba60Sopenharmony_ci }); 99606f6ba60Sopenharmony_ci printf("set_len_bytes(CallbackFunc(message)):\n"); 99706f6ba60Sopenharmony_ci for (uint32_t i = 0; i < g_writePos; i++) { 99806f6ba60Sopenharmony_ci printf("%02X ", g_buf[i]); 99906f6ba60Sopenharmony_ci } 100006f6ba60Sopenharmony_ci printf("\n"); 100106f6ba60Sopenharmony_ci 100206f6ba60Sopenharmony_ci ASSERT_TRUE(msgProtobuf.ParseFromArray(g_buf, g_writePos)); 100306f6ba60Sopenharmony_ci auto& bytes = msgProtobuf.len_bytes(); 100406f6ba60Sopenharmony_ci SubMessage subMsgProtobuf; 100506f6ba60Sopenharmony_ci ASSERT_TRUE(subMsgProtobuf.ParseFromArray(bytes.data(), bytes.size())); 100606f6ba60Sopenharmony_ci ASSERT_EQ(subMsgProtobuf.vint_int32(), std::numeric_limits<int32_t>::max()); 100706f6ba60Sopenharmony_ci ASSERT_EQ(subMsgProtobuf.vint_int64(), std::numeric_limits<int64_t>::max()); 100806f6ba60Sopenharmony_ci ASSERT_EQ(subMsgProtobuf.vint_uint32(), std::numeric_limits<uint32_t>::max()); 100906f6ba60Sopenharmony_ci ASSERT_EQ(subMsgProtobuf.vint_uint64(), std::numeric_limits<uint64_t>::max()); 101006f6ba60Sopenharmony_ci} 101106f6ba60Sopenharmony_ci 101206f6ba60Sopenharmony_ciHWTEST_F(BaseMessageUnittest, AddBytes, TestSize.Level1) 101306f6ba60Sopenharmony_ci{ 101406f6ba60Sopenharmony_ci const char data[] = {0x00, 0x01, 0x02, 0xFD, 0xFE, 0xFF}; 101506f6ba60Sopenharmony_ci ProtoEncoder::ExampleMessage msgOpt(&g_writeCtx); 101606f6ba60Sopenharmony_ci ExampleMessage msgProtobuf; 101706f6ba60Sopenharmony_ci 101806f6ba60Sopenharmony_ci // bytes stram 101906f6ba60Sopenharmony_ci g_writeCtx.seek(&g_writeCtx, 0); 102006f6ba60Sopenharmony_ci RandomWriteCtx* writeCtx = msgOpt.startAdd_len_bytes(); 102106f6ba60Sopenharmony_ci uint8_t* memory = nullptr; 102206f6ba60Sopenharmony_ci uint32_t offset = 0; 102306f6ba60Sopenharmony_ci if (writeCtx->getMemory(writeCtx, sizeof(data), &memory, &offset)) { 102406f6ba60Sopenharmony_ci if (memcpy_s(memory, sizeof(data), data, sizeof(data)) != EOK) { 102506f6ba60Sopenharmony_ci writeCtx->seek(writeCtx, offset + sizeof(data)); 102606f6ba60Sopenharmony_ci } 102706f6ba60Sopenharmony_ci } 102806f6ba60Sopenharmony_ci msgOpt.finishAdd_len_bytes(sizeof(data)); 102906f6ba60Sopenharmony_ci printf("Add_LEN_bytes(Customize(bytes)):\n"); 103006f6ba60Sopenharmony_ci for (uint32_t i = 0; i < g_writePos; i++) { 103106f6ba60Sopenharmony_ci printf("%02X ", g_buf[i]); 103206f6ba60Sopenharmony_ci } 103306f6ba60Sopenharmony_ci printf("\n"); 103406f6ba60Sopenharmony_ci 103506f6ba60Sopenharmony_ci ASSERT_TRUE(msgProtobuf.ParseFromArray(g_buf, g_writePos)); 103606f6ba60Sopenharmony_ci ASSERT_EQ(msgProtobuf.len_bytes(), std::string(&data[0], sizeof(data))); 103706f6ba60Sopenharmony_ci 103806f6ba60Sopenharmony_ci // protobufOpt message 103906f6ba60Sopenharmony_ci g_writeCtx.seek(&g_writeCtx, 0); 104006f6ba60Sopenharmony_ci writeCtx = msgOpt.startAdd_len_bytes(); 104106f6ba60Sopenharmony_ci ProtoEncoder::SubMessage subMsg(writeCtx); 104206f6ba60Sopenharmony_ci subMsg.set_vint_int32(std::numeric_limits<int32_t>::max()); 104306f6ba60Sopenharmony_ci subMsg.set_vint_int64(std::numeric_limits<int64_t>::max()); 104406f6ba60Sopenharmony_ci subMsg.set_vint_uint32(std::numeric_limits<uint32_t>::max()); 104506f6ba60Sopenharmony_ci subMsg.set_vint_uint64(std::numeric_limits<uint64_t>::max()); 104606f6ba60Sopenharmony_ci msgOpt.finishAdd_len_bytes(subMsg.Finish()); 104706f6ba60Sopenharmony_ci printf("Add_LEN_bytes(Customize(message)):\n"); 104806f6ba60Sopenharmony_ci for (uint32_t i = 0; i < g_writePos; i++) { 104906f6ba60Sopenharmony_ci printf("%02X ", g_buf[i]); 105006f6ba60Sopenharmony_ci } 105106f6ba60Sopenharmony_ci printf("\n"); 105206f6ba60Sopenharmony_ci 105306f6ba60Sopenharmony_ci ASSERT_TRUE(msgProtobuf.ParseFromArray(g_buf, g_writePos)); 105406f6ba60Sopenharmony_ci auto& bytes = msgProtobuf.len_bytes(); 105506f6ba60Sopenharmony_ci SubMessage subMsgProtobuf; 105606f6ba60Sopenharmony_ci ASSERT_TRUE(subMsgProtobuf.ParseFromArray(bytes.data(), bytes.size())); 105706f6ba60Sopenharmony_ci ASSERT_EQ(subMsgProtobuf.vint_int32(), std::numeric_limits<int32_t>::max()); 105806f6ba60Sopenharmony_ci ASSERT_EQ(subMsgProtobuf.vint_int64(), std::numeric_limits<int64_t>::max()); 105906f6ba60Sopenharmony_ci ASSERT_EQ(subMsgProtobuf.vint_uint32(), std::numeric_limits<uint32_t>::max()); 106006f6ba60Sopenharmony_ci ASSERT_EQ(subMsgProtobuf.vint_uint64(), std::numeric_limits<uint64_t>::max()); 106106f6ba60Sopenharmony_ci} 106206f6ba60Sopenharmony_ci 106306f6ba60Sopenharmony_ciHWTEST_F(BaseMessageUnittest, SetAll, TestSize.Level1) 106406f6ba60Sopenharmony_ci{ 106506f6ba60Sopenharmony_ci ProtoEncoder::ExampleMessage msgOpt(&g_writeCtx); 106606f6ba60Sopenharmony_ci ExampleMessage msgProtobuf; 106706f6ba60Sopenharmony_ci 106806f6ba60Sopenharmony_ci g_writeCtx.seek(&g_writeCtx, 0); 106906f6ba60Sopenharmony_ci msgOpt.set_vint_int32(1); 107006f6ba60Sopenharmony_ci msgOpt.set_vint_int64(1); 107106f6ba60Sopenharmony_ci msgOpt.set_vint_uint32(1); 107206f6ba60Sopenharmony_ci msgOpt.set_vint_uint64(1); 107306f6ba60Sopenharmony_ci msgOpt.set_vint_sint32(1); 107406f6ba60Sopenharmony_ci msgOpt.set_vint_sint64(1); 107506f6ba60Sopenharmony_ci msgOpt.set_vint_bool(true); 107606f6ba60Sopenharmony_ci msgOpt.set_vint_enum(ProtoEncoder::NUM::ONE); 107706f6ba60Sopenharmony_ci msgOpt.set_i64_fixed64(1); 107806f6ba60Sopenharmony_ci msgOpt.set_i64_sfixed64(1); 107906f6ba60Sopenharmony_ci msgOpt.set_i64_double(1); 108006f6ba60Sopenharmony_ci 108106f6ba60Sopenharmony_ci const std::string str = "this is test."; 108206f6ba60Sopenharmony_ci msgOpt.set_len_string(str); 108306f6ba60Sopenharmony_ci msgOpt.set_len_bytes(BYTES_DATA, sizeof(BYTES_DATA)); 108406f6ba60Sopenharmony_ci 108506f6ba60Sopenharmony_ci ProtoEncoder::SubMessage* msgSub = msgOpt.mutable_len_sub(); 108606f6ba60Sopenharmony_ci ASSERT_TRUE(msgSub != nullptr); 108706f6ba60Sopenharmony_ci msgSub->set_vint_int32(1); 108806f6ba60Sopenharmony_ci msgSub->set_vint_int64(1); 108906f6ba60Sopenharmony_ci msgSub->set_vint_uint32(1); 109006f6ba60Sopenharmony_ci msgSub->set_vint_uint64(1); 109106f6ba60Sopenharmony_ci 109206f6ba60Sopenharmony_ci const int32_t arraySigned[] = {0, 1, 109306f6ba60Sopenharmony_ci std::numeric_limits<int32_t>::min(), 109406f6ba60Sopenharmony_ci std::numeric_limits<int32_t>::max()}; 109506f6ba60Sopenharmony_ci std::vector<int32_t> vecSigned; 109606f6ba60Sopenharmony_ci vecSigned.emplace_back(0); 109706f6ba60Sopenharmony_ci vecSigned.emplace_back(1); 109806f6ba60Sopenharmony_ci vecSigned.emplace_back(std::numeric_limits<int32_t>::min()); 109906f6ba60Sopenharmony_ci vecSigned.emplace_back(std::numeric_limits<int32_t>::max()); 110006f6ba60Sopenharmony_ci msgOpt.add_len_repeated_packed_signed_vint(vecSigned); 110106f6ba60Sopenharmony_ci msgOpt.add_len_repeated_packed_signed_vint(arraySigned, sizeof(arraySigned) / sizeof(arraySigned[0])); 110206f6ba60Sopenharmony_ci 110306f6ba60Sopenharmony_ci const uint32_t arrayUnsigned[] = {0, 1, 110406f6ba60Sopenharmony_ci std::numeric_limits<uint32_t>::min(), 110506f6ba60Sopenharmony_ci std::numeric_limits<uint32_t>::max()}; 110606f6ba60Sopenharmony_ci std::vector<uint32_t> vecUnsigned; 110706f6ba60Sopenharmony_ci vecUnsigned.emplace_back(0); 110806f6ba60Sopenharmony_ci vecUnsigned.emplace_back(1); 110906f6ba60Sopenharmony_ci vecUnsigned.emplace_back(std::numeric_limits<uint32_t>::min()); 111006f6ba60Sopenharmony_ci vecUnsigned.emplace_back(std::numeric_limits<uint32_t>::max()); 111106f6ba60Sopenharmony_ci msgOpt.add_len_repeated_packed_unsigned_vint(vecUnsigned); 111206f6ba60Sopenharmony_ci msgOpt.add_len_repeated_packed_unsigned_vint(arrayUnsigned, sizeof(arrayUnsigned) / sizeof(arrayUnsigned[0])); 111306f6ba60Sopenharmony_ci 111406f6ba60Sopenharmony_ci const uint64_t arrayFixed[] = {0, 1, 111506f6ba60Sopenharmony_ci std::numeric_limits<uint64_t>::min(), 111606f6ba60Sopenharmony_ci std::numeric_limits<uint64_t>::max()}; 111706f6ba60Sopenharmony_ci std::vector<uint64_t> vecFixed; 111806f6ba60Sopenharmony_ci vecFixed.emplace_back(0); 111906f6ba60Sopenharmony_ci vecFixed.emplace_back(1); 112006f6ba60Sopenharmony_ci vecFixed.emplace_back(std::numeric_limits<uint64_t>::min()); 112106f6ba60Sopenharmony_ci vecFixed.emplace_back(std::numeric_limits<uint64_t>::max()); 112206f6ba60Sopenharmony_ci msgOpt.add_len_repeated_packed_fixed(vecFixed); 112306f6ba60Sopenharmony_ci msgOpt.add_len_repeated_packed_fixed(arrayFixed, sizeof(arrayFixed) / sizeof(arrayFixed[0])); 112406f6ba60Sopenharmony_ci 112506f6ba60Sopenharmony_ci msgOpt.set_i32_fixed32(1); 112606f6ba60Sopenharmony_ci msgOpt.set_i32_sfixed32(1); 112706f6ba60Sopenharmony_ci msgOpt.set_i32_float(1); 112806f6ba60Sopenharmony_ci 112906f6ba60Sopenharmony_ci msgOpt.set_oneof_fixed64(1); 113006f6ba60Sopenharmony_ci msgOpt.set_oneof_string(str); 113106f6ba60Sopenharmony_ci msgSub = msgOpt.mutable_oneof_sub(); 113206f6ba60Sopenharmony_ci ASSERT_TRUE(msgSub != nullptr); 113306f6ba60Sopenharmony_ci msgSub->set_vint_int32(1); 113406f6ba60Sopenharmony_ci msgSub->set_vint_int64(1); 113506f6ba60Sopenharmony_ci msgSub->set_vint_uint32(1); 113606f6ba60Sopenharmony_ci msgSub->set_vint_uint64(1); 113706f6ba60Sopenharmony_ci 113806f6ba60Sopenharmony_ci ASSERT_EQ((uint32_t)msgOpt.Finish(), g_writePos); 113906f6ba60Sopenharmony_ci printf("SetAll size(%" PRIu32 "):\n", g_writePos); 114006f6ba60Sopenharmony_ci for (uint32_t i = 0; i < g_writePos; i++) { 114106f6ba60Sopenharmony_ci printf("%02X ", g_buf[i]); 114206f6ba60Sopenharmony_ci } 114306f6ba60Sopenharmony_ci printf("\n"); 114406f6ba60Sopenharmony_ci 114506f6ba60Sopenharmony_ci // check result by protobuf 114606f6ba60Sopenharmony_ci ASSERT_TRUE(msgProtobuf.ParseFromArray(g_buf, g_writePos)); 114706f6ba60Sopenharmony_ci 114806f6ba60Sopenharmony_ci ASSERT_EQ(msgProtobuf.vint_int32(), (int32_t)1); 114906f6ba60Sopenharmony_ci ASSERT_EQ(msgProtobuf.vint_int64(), (int64_t)1); 115006f6ba60Sopenharmony_ci ASSERT_EQ(msgProtobuf.vint_uint32(), (uint32_t)1); 115106f6ba60Sopenharmony_ci ASSERT_EQ(msgProtobuf.vint_uint64(), (uint64_t)1); 115206f6ba60Sopenharmony_ci ASSERT_EQ(msgProtobuf.vint_sint32(), (int32_t)1); 115306f6ba60Sopenharmony_ci ASSERT_EQ(msgProtobuf.vint_sint64(), (int64_t)1); 115406f6ba60Sopenharmony_ci ASSERT_EQ(msgProtobuf.vint_sint32(), (int32_t)1); 115506f6ba60Sopenharmony_ci ASSERT_TRUE(msgProtobuf.vint_bool()); 115606f6ba60Sopenharmony_ci ASSERT_EQ(msgProtobuf.vint_enum(), NUM::ONE); 115706f6ba60Sopenharmony_ci 115806f6ba60Sopenharmony_ci ASSERT_EQ(msgProtobuf.i64_fixed64(), (uint64_t)1); 115906f6ba60Sopenharmony_ci ASSERT_EQ(msgProtobuf.i64_sfixed64(), (int64_t)1); 116006f6ba60Sopenharmony_ci ASSERT_EQ(msgProtobuf.i64_double(), (double)1); 116106f6ba60Sopenharmony_ci 116206f6ba60Sopenharmony_ci ASSERT_EQ(msgProtobuf.len_string(), str); 116306f6ba60Sopenharmony_ci ASSERT_EQ(msgProtobuf.len_bytes(), std::string(&BYTES_DATA[0], sizeof(BYTES_DATA))); 116406f6ba60Sopenharmony_ci 116506f6ba60Sopenharmony_ci ASSERT_TRUE(msgProtobuf.has_len_sub()); 116606f6ba60Sopenharmony_ci SubMessage subMsgProtobuf = msgProtobuf.len_sub(); 116706f6ba60Sopenharmony_ci ASSERT_EQ(subMsgProtobuf.vint_int32(), (int32_t)1); 116806f6ba60Sopenharmony_ci ASSERT_EQ(subMsgProtobuf.vint_int64(), (int64_t)1); 116906f6ba60Sopenharmony_ci ASSERT_EQ(subMsgProtobuf.vint_uint32(), (uint32_t)1); 117006f6ba60Sopenharmony_ci ASSERT_EQ(subMsgProtobuf.vint_uint64(), (uint64_t)1); 117106f6ba60Sopenharmony_ci 117206f6ba60Sopenharmony_ci int countAll = msgProtobuf.len_repeated_packed_signed_vint_size(); 117306f6ba60Sopenharmony_ci ASSERT_EQ(countAll, static_cast<int>(vecSigned.size() + sizeof(arraySigned) / sizeof(arraySigned[0]))); 117406f6ba60Sopenharmony_ci for (size_t i = 0; i < vecSigned.size(); i++) { 117506f6ba60Sopenharmony_ci ASSERT_EQ(msgProtobuf.len_repeated_packed_signed_vint(i), vecSigned[i]); 117606f6ba60Sopenharmony_ci } 117706f6ba60Sopenharmony_ci for (size_t i = vecSigned.size(); i < static_cast<size_t>(countAll); i++) { 117806f6ba60Sopenharmony_ci ASSERT_EQ(msgProtobuf.len_repeated_packed_signed_vint(i), arraySigned[i - vecSigned.size()]); 117906f6ba60Sopenharmony_ci } 118006f6ba60Sopenharmony_ci 118106f6ba60Sopenharmony_ci countAll = msgProtobuf.len_repeated_packed_unsigned_vint_size(); 118206f6ba60Sopenharmony_ci ASSERT_EQ(countAll, static_cast<int>(vecUnsigned.size() + sizeof(arrayUnsigned) / sizeof(arrayUnsigned[0]))); 118306f6ba60Sopenharmony_ci for (size_t i = 0; i < vecUnsigned.size(); i++) { 118406f6ba60Sopenharmony_ci ASSERT_EQ(msgProtobuf.len_repeated_packed_unsigned_vint(i), vecUnsigned[i]); 118506f6ba60Sopenharmony_ci } 118606f6ba60Sopenharmony_ci for (size_t i = vecUnsigned.size(); i < static_cast<size_t>(countAll); i++) { 118706f6ba60Sopenharmony_ci ASSERT_EQ(msgProtobuf.len_repeated_packed_unsigned_vint(i), arrayUnsigned[i - vecUnsigned.size()]); 118806f6ba60Sopenharmony_ci } 118906f6ba60Sopenharmony_ci 119006f6ba60Sopenharmony_ci countAll = msgProtobuf.len_repeated_packed_fixed_size(); 119106f6ba60Sopenharmony_ci ASSERT_EQ(countAll, static_cast<int>(vecFixed.size() + sizeof(arrayFixed) / sizeof(arrayFixed[0]))); 119206f6ba60Sopenharmony_ci for (size_t i = 0; i < vecFixed.size(); i++) { 119306f6ba60Sopenharmony_ci ASSERT_EQ(msgProtobuf.len_repeated_packed_fixed(i), vecFixed[i]); 119406f6ba60Sopenharmony_ci } 119506f6ba60Sopenharmony_ci for (size_t i = vecFixed.size(); i < static_cast<size_t>(countAll); i++) { 119606f6ba60Sopenharmony_ci ASSERT_EQ(msgProtobuf.len_repeated_packed_fixed(i), arrayFixed[i - vecFixed.size()]); 119706f6ba60Sopenharmony_ci } 119806f6ba60Sopenharmony_ci 119906f6ba60Sopenharmony_ci ASSERT_EQ(msgProtobuf.i32_fixed32(), (uint32_t)1); 120006f6ba60Sopenharmony_ci ASSERT_EQ(msgProtobuf.i32_sfixed32(), (int32_t)1); 120106f6ba60Sopenharmony_ci ASSERT_EQ(msgProtobuf.i32_float(), (float)1); 120206f6ba60Sopenharmony_ci 120306f6ba60Sopenharmony_ci // last one wins 120406f6ba60Sopenharmony_ci ASSERT_NE(msgProtobuf.oneof_fixed64(), (uint64_t)1); 120506f6ba60Sopenharmony_ci ASSERT_NE(msgProtobuf.oneof_string(), str); 120606f6ba60Sopenharmony_ci ASSERT_TRUE(msgProtobuf.has_oneof_sub()); 120706f6ba60Sopenharmony_ci subMsgProtobuf = msgProtobuf.oneof_sub(); 120806f6ba60Sopenharmony_ci ASSERT_EQ(subMsgProtobuf.vint_int32(), (int32_t)1); 120906f6ba60Sopenharmony_ci ASSERT_EQ(subMsgProtobuf.vint_int64(), (int64_t)1); 121006f6ba60Sopenharmony_ci ASSERT_EQ(subMsgProtobuf.vint_uint32(), (uint32_t)1); 121106f6ba60Sopenharmony_ci ASSERT_EQ(subMsgProtobuf.vint_uint64(), (uint64_t)1); 121206f6ba60Sopenharmony_ci} 121306f6ba60Sopenharmony_ci 121406f6ba60Sopenharmony_ciHWTEST_F(BaseMessageUnittest, SubMessageLen0, TestSize.Level1) 121506f6ba60Sopenharmony_ci{ 121606f6ba60Sopenharmony_ci ProtoEncoder::ExampleMessage msgOpt(&g_writeCtx); 121706f6ba60Sopenharmony_ci ExampleMessage msgProtobuf; 121806f6ba60Sopenharmony_ci 121906f6ba60Sopenharmony_ci // empty 122006f6ba60Sopenharmony_ci g_writeCtx.seek(&g_writeCtx, 0); 122106f6ba60Sopenharmony_ci ProtoEncoder::SubMessage* subMsgOpt = msgOpt.mutable_len_sub(); 122206f6ba60Sopenharmony_ci ASSERT_TRUE(subMsgOpt != nullptr); 122306f6ba60Sopenharmony_ci msgOpt.set_vint_int32(std::numeric_limits<int32_t>::min()); 122406f6ba60Sopenharmony_ci printf("sub message length is 0:\n"); 122506f6ba60Sopenharmony_ci for (uint32_t i = 0; i < g_writePos; i++) { 122606f6ba60Sopenharmony_ci printf("%02X ", g_buf[i]); 122706f6ba60Sopenharmony_ci } 122806f6ba60Sopenharmony_ci printf("\n"); 122906f6ba60Sopenharmony_ci 123006f6ba60Sopenharmony_ci ASSERT_TRUE(msgProtobuf.ParseFromArray(g_buf, g_writePos)); 123106f6ba60Sopenharmony_ci ASSERT_TRUE(msgProtobuf.has_len_sub()); 123206f6ba60Sopenharmony_ci 123306f6ba60Sopenharmony_ci g_writeCtx.seek(&g_writeCtx, 0); 123406f6ba60Sopenharmony_ci int subMsgOptRepeatedCount = 0; 123506f6ba60Sopenharmony_ci ProtoEncoder::SubMessage* subMsgOptRepeated = msgOpt.add_repeated_len_sub(); 123606f6ba60Sopenharmony_ci ASSERT_TRUE(subMsgOptRepeated != nullptr); 123706f6ba60Sopenharmony_ci subMsgOptRepeatedCount++; 123806f6ba60Sopenharmony_ci msgOpt.set_vint_int32(std::numeric_limits<int32_t>::min()); 123906f6ba60Sopenharmony_ci printf("repeated sub message length is 0:\n"); 124006f6ba60Sopenharmony_ci for (uint32_t i = 0; i < g_writePos; i++) { 124106f6ba60Sopenharmony_ci printf("%02X ", g_buf[i]); 124206f6ba60Sopenharmony_ci } 124306f6ba60Sopenharmony_ci printf("\n"); 124406f6ba60Sopenharmony_ci 124506f6ba60Sopenharmony_ci ASSERT_TRUE(msgProtobuf.ParseFromArray(g_buf, g_writePos)); 124606f6ba60Sopenharmony_ci ASSERT_EQ(msgProtobuf.repeated_len_sub_size(), subMsgOptRepeatedCount); 124706f6ba60Sopenharmony_ci 124806f6ba60Sopenharmony_ci subMsgOptRepeated = msgOpt.add_repeated_len_sub(); 124906f6ba60Sopenharmony_ci ASSERT_TRUE(subMsgOptRepeated != nullptr); 125006f6ba60Sopenharmony_ci subMsgOptRepeatedCount++; 125106f6ba60Sopenharmony_ci subMsgOptRepeated->set_vint_int32(1); 125206f6ba60Sopenharmony_ci msgOpt.set_vint_int32(std::numeric_limits<int32_t>::min()); 125306f6ba60Sopenharmony_ci printf("repeated sub message length > 0:\n"); 125406f6ba60Sopenharmony_ci for (uint32_t i = 0; i < g_writePos; i++) { 125506f6ba60Sopenharmony_ci printf("%02X ", g_buf[i]); 125606f6ba60Sopenharmony_ci } 125706f6ba60Sopenharmony_ci printf("\n"); 125806f6ba60Sopenharmony_ci 125906f6ba60Sopenharmony_ci ASSERT_TRUE(msgProtobuf.ParseFromArray(g_buf, g_writePos)); 126006f6ba60Sopenharmony_ci ASSERT_EQ(msgProtobuf.repeated_len_sub_size(), subMsgOptRepeatedCount); 126106f6ba60Sopenharmony_ci 126206f6ba60Sopenharmony_ci // set bytes by callback 126306f6ba60Sopenharmony_ci g_writeCtx.seek(&g_writeCtx, 0); 126406f6ba60Sopenharmony_ci msgOpt.set_len_bytes( 126506f6ba60Sopenharmony_ci [](RandomWriteCtx* randomWriteCtx) -> int32_t { 126606f6ba60Sopenharmony_ci return 0; 126706f6ba60Sopenharmony_ci }); 126806f6ba60Sopenharmony_ci printf("set_len_bytes(CallbackFunc(len = 0)):\n"); 126906f6ba60Sopenharmony_ci for (uint32_t i = 0; i < g_writePos; i++) { 127006f6ba60Sopenharmony_ci printf("%02X ", g_buf[i]); 127106f6ba60Sopenharmony_ci } 127206f6ba60Sopenharmony_ci printf("\n"); 127306f6ba60Sopenharmony_ci 127406f6ba60Sopenharmony_ci ASSERT_TRUE(msgProtobuf.ParseFromArray(g_buf, g_writePos)); 127506f6ba60Sopenharmony_ci ASSERT_EQ(msgProtobuf.len_bytes(), std::string("")); 127606f6ba60Sopenharmony_ci 127706f6ba60Sopenharmony_ci // add bytes 127806f6ba60Sopenharmony_ci g_writeCtx.seek(&g_writeCtx, 0); 127906f6ba60Sopenharmony_ci msgOpt.startAdd_len_bytes(); 128006f6ba60Sopenharmony_ci msgOpt.finishAdd_len_bytes(0); 128106f6ba60Sopenharmony_ci printf("Add_LEN_bytes(len = 0):\n"); 128206f6ba60Sopenharmony_ci for (uint32_t i = 0; i < g_writePos; i++) { 128306f6ba60Sopenharmony_ci printf("%02X ", g_buf[i]); 128406f6ba60Sopenharmony_ci } 128506f6ba60Sopenharmony_ci printf("\n"); 128606f6ba60Sopenharmony_ci 128706f6ba60Sopenharmony_ci ASSERT_TRUE(msgProtobuf.ParseFromArray(g_buf, g_writePos)); 128806f6ba60Sopenharmony_ci ASSERT_EQ(msgProtobuf.len_bytes(), std::string("")); 128906f6ba60Sopenharmony_ci} 129006f6ba60Sopenharmony_ci 129106f6ba60Sopenharmony_ciHWTEST_F(BaseMessageUnittest, AutoFinishSubMessage, TestSize.Level1) 129206f6ba60Sopenharmony_ci{ 129306f6ba60Sopenharmony_ci ProtoEncoder::ExampleMessage msgOpt(&g_writeCtx); 129406f6ba60Sopenharmony_ci 129506f6ba60Sopenharmony_ci ASSERT_TRUE(msgOpt.add_repeated_example() != nullptr); 129606f6ba60Sopenharmony_ci msgOpt.set_i32_fixed32(1); 129706f6ba60Sopenharmony_ci ASSERT_TRUE(msgOpt.add_repeated_example() != nullptr); 129806f6ba60Sopenharmony_ci msgOpt.set_i64_fixed64(1); 129906f6ba60Sopenharmony_ci ASSERT_TRUE(msgOpt.add_repeated_example() != nullptr); 130006f6ba60Sopenharmony_ci msgOpt.set_vint_uint32(1); 130106f6ba60Sopenharmony_ci ASSERT_TRUE(msgOpt.add_repeated_example() != nullptr); 130206f6ba60Sopenharmony_ci msgOpt.set_vint_sint32(1); 130306f6ba60Sopenharmony_ci ASSERT_TRUE(msgOpt.add_repeated_example() != nullptr); 130406f6ba60Sopenharmony_ci msgOpt.set_len_string("\n"); 130506f6ba60Sopenharmony_ci ASSERT_TRUE(msgOpt.add_repeated_example() != nullptr); 130606f6ba60Sopenharmony_ci ASSERT_TRUE(msgOpt.mutable_len_sub() != nullptr); 130706f6ba60Sopenharmony_ci ASSERT_TRUE(msgOpt.add_repeated_example() != nullptr); 130806f6ba60Sopenharmony_ci ASSERT_TRUE(msgOpt.startAdd_len_bytes() != nullptr); 130906f6ba60Sopenharmony_ci msgOpt.finishAdd_len_bytes(0); 131006f6ba60Sopenharmony_ci ASSERT_TRUE(msgOpt.add_repeated_example() != nullptr); 131106f6ba60Sopenharmony_ci msgOpt.set_len_bytes( 131206f6ba60Sopenharmony_ci [](RandomWriteCtx* randomWriteCtx) -> int32_t { 131306f6ba60Sopenharmony_ci return 0; 131406f6ba60Sopenharmony_ci }); 131506f6ba60Sopenharmony_ci ASSERT_TRUE(msgOpt.add_repeated_example() != nullptr); 131606f6ba60Sopenharmony_ci std::vector<int32_t> arraySint32(1, -1); 131706f6ba60Sopenharmony_ci msgOpt.add_len_repeated_packed_signed_vint(arraySint32); 131806f6ba60Sopenharmony_ci ASSERT_TRUE(msgOpt.add_repeated_example() != nullptr); 131906f6ba60Sopenharmony_ci std::vector<uint32_t> arrayUint32(1, 1); 132006f6ba60Sopenharmony_ci msgOpt.add_len_repeated_packed_unsigned_vint(arrayUint32); 132106f6ba60Sopenharmony_ci ASSERT_TRUE(msgOpt.add_repeated_example() != nullptr); 132206f6ba60Sopenharmony_ci std::vector<uint64_t> arrayfint64(1, 1); 132306f6ba60Sopenharmony_ci msgOpt.add_len_repeated_packed_fixed(arrayfint64); 132406f6ba60Sopenharmony_ci 132506f6ba60Sopenharmony_ci ExampleMessage msgProtobuf; 132606f6ba60Sopenharmony_ci ASSERT_TRUE(msgProtobuf.ParseFromArray(g_buf, g_writePos)); 132706f6ba60Sopenharmony_ci ASSERT_GT(msgProtobuf.repeated_example_size(), 0); 132806f6ba60Sopenharmony_ci} 132906f6ba60Sopenharmony_ci 133006f6ba60Sopenharmony_ciHWTEST_F(BaseMessageUnittest, WriteFailedDrop, TestSize.Level1) 133106f6ba60Sopenharmony_ci{ 133206f6ba60Sopenharmony_ci RandomWriteCtx writeCtx = { 133306f6ba60Sopenharmony_ci [](RandomWriteCtx*, uint32_t, uint8_t**, uint32_t*) -> bool { return false; }, 133406f6ba60Sopenharmony_ci [](RandomWriteCtx*, uint32_t) -> bool { return false; } 133506f6ba60Sopenharmony_ci }; 133606f6ba60Sopenharmony_ci 133706f6ba60Sopenharmony_ci std::unique_ptr<ProtoEncoder::ExampleMessage> msgOpt = 133806f6ba60Sopenharmony_ci std::make_unique<ProtoEncoder::ExampleMessage>(&writeCtx); 133906f6ba60Sopenharmony_ci msgOpt->set_i32_fixed32(1); 134006f6ba60Sopenharmony_ci ASSERT_EQ(msgOpt->Finish(), (int32_t)-1); 134106f6ba60Sopenharmony_ci 134206f6ba60Sopenharmony_ci msgOpt = std::make_unique<ProtoEncoder::ExampleMessage>(&writeCtx); 134306f6ba60Sopenharmony_ci msgOpt->set_i64_fixed64(1); 134406f6ba60Sopenharmony_ci ASSERT_EQ(msgOpt->Finish(), (int32_t)-1); 134506f6ba60Sopenharmony_ci 134606f6ba60Sopenharmony_ci msgOpt = std::make_unique<ProtoEncoder::ExampleMessage>(&writeCtx); 134706f6ba60Sopenharmony_ci msgOpt->set_vint_uint32(1); 134806f6ba60Sopenharmony_ci ASSERT_EQ(msgOpt->Finish(), (int32_t)-1); 134906f6ba60Sopenharmony_ci 135006f6ba60Sopenharmony_ci msgOpt = std::make_unique<ProtoEncoder::ExampleMessage>(&writeCtx); 135106f6ba60Sopenharmony_ci msgOpt->set_vint_sint32(1); 135206f6ba60Sopenharmony_ci ASSERT_EQ(msgOpt->Finish(), (int32_t)-1); 135306f6ba60Sopenharmony_ci 135406f6ba60Sopenharmony_ci msgOpt = std::make_unique<ProtoEncoder::ExampleMessage>(&writeCtx); 135506f6ba60Sopenharmony_ci msgOpt->set_len_string("\n"); 135606f6ba60Sopenharmony_ci ASSERT_EQ(msgOpt->Finish(), (int32_t)-1); 135706f6ba60Sopenharmony_ci 135806f6ba60Sopenharmony_ci msgOpt = std::make_unique<ProtoEncoder::ExampleMessage>(&writeCtx); 135906f6ba60Sopenharmony_ci ASSERT_TRUE(msgOpt->mutable_len_sub() != nullptr); 136006f6ba60Sopenharmony_ci ASSERT_EQ(msgOpt->Finish(), (int32_t)-1); 136106f6ba60Sopenharmony_ci 136206f6ba60Sopenharmony_ci msgOpt = std::make_unique<ProtoEncoder::ExampleMessage>(&writeCtx); 136306f6ba60Sopenharmony_ci ASSERT_TRUE(msgOpt->startAdd_len_bytes() == nullptr); 136406f6ba60Sopenharmony_ci ASSERT_EQ(msgOpt->Finish(), (int32_t)-1); 136506f6ba60Sopenharmony_ci 136606f6ba60Sopenharmony_ci msgOpt = std::make_unique<ProtoEncoder::ExampleMessage>(&writeCtx); 136706f6ba60Sopenharmony_ci msgOpt->set_len_bytes( 136806f6ba60Sopenharmony_ci [](RandomWriteCtx* randomWriteCtx) -> int32_t { 136906f6ba60Sopenharmony_ci return 0; 137006f6ba60Sopenharmony_ci }); 137106f6ba60Sopenharmony_ci ASSERT_EQ(msgOpt->Finish(), (int32_t)-1); 137206f6ba60Sopenharmony_ci 137306f6ba60Sopenharmony_ci std::vector<int32_t> arraySint32(1, -1); 137406f6ba60Sopenharmony_ci msgOpt = std::make_unique<ProtoEncoder::ExampleMessage>(&writeCtx); 137506f6ba60Sopenharmony_ci msgOpt->add_len_repeated_packed_signed_vint(arraySint32); 137606f6ba60Sopenharmony_ci ASSERT_EQ(msgOpt->Finish(), (int32_t)-1); 137706f6ba60Sopenharmony_ci 137806f6ba60Sopenharmony_ci std::vector<uint32_t> arrayUint32(1, 1); 137906f6ba60Sopenharmony_ci msgOpt = std::make_unique<ProtoEncoder::ExampleMessage>(&writeCtx); 138006f6ba60Sopenharmony_ci msgOpt->add_len_repeated_packed_unsigned_vint(arrayUint32); 138106f6ba60Sopenharmony_ci ASSERT_EQ(msgOpt->Finish(), (int32_t)-1); 138206f6ba60Sopenharmony_ci 138306f6ba60Sopenharmony_ci msgOpt = std::make_unique<ProtoEncoder::ExampleMessage>(&writeCtx); 138406f6ba60Sopenharmony_ci std::vector<uint64_t> arrayfint64(1, 1); 138506f6ba60Sopenharmony_ci msgOpt->add_len_repeated_packed_fixed(arrayfint64); 138606f6ba60Sopenharmony_ci ASSERT_EQ(msgOpt->Finish(), (int32_t)-1); 138706f6ba60Sopenharmony_ci} 138806f6ba60Sopenharmony_ci 138906f6ba60Sopenharmony_ciHWTEST_F(BaseMessageUnittest, NoWrite, TestSize.Level1) 139006f6ba60Sopenharmony_ci{ 139106f6ba60Sopenharmony_ci ProtoEncoder::ExampleMessage msgOpt(nullptr); 139206f6ba60Sopenharmony_ci 139306f6ba60Sopenharmony_ci msgOpt.set_i32_fixed32(1); 139406f6ba60Sopenharmony_ci msgOpt.set_i64_fixed64(1); 139506f6ba60Sopenharmony_ci msgOpt.set_vint_uint32(1); 139606f6ba60Sopenharmony_ci msgOpt.set_vint_sint32(1); 139706f6ba60Sopenharmony_ci msgOpt.set_len_string("\n"); 139806f6ba60Sopenharmony_ci ASSERT_TRUE(msgOpt.startAdd_len_bytes() == nullptr); 139906f6ba60Sopenharmony_ci msgOpt.set_len_bytes(nullptr); 140006f6ba60Sopenharmony_ci msgOpt.set_len_bytes( 140106f6ba60Sopenharmony_ci [](RandomWriteCtx* randomWriteCtx) -> int32_t { 140206f6ba60Sopenharmony_ci return 0; 140306f6ba60Sopenharmony_ci }); 140406f6ba60Sopenharmony_ci 140506f6ba60Sopenharmony_ci // arrayCount is 0 140606f6ba60Sopenharmony_ci msgOpt.add_len_repeated_packed_signed_vint(nullptr, 0); 140706f6ba60Sopenharmony_ci msgOpt.add_len_repeated_packed_unsigned_vint(nullptr, 0); 140806f6ba60Sopenharmony_ci msgOpt.add_len_repeated_packed_fixed(nullptr, 0); 140906f6ba60Sopenharmony_ci std::vector<int32_t> arraySint32(1, -1); 141006f6ba60Sopenharmony_ci msgOpt.add_len_repeated_packed_signed_vint(arraySint32); 141106f6ba60Sopenharmony_ci std::vector<uint32_t> arrayUint32(1, 1); 141206f6ba60Sopenharmony_ci msgOpt.add_len_repeated_packed_unsigned_vint(arrayUint32); 141306f6ba60Sopenharmony_ci std::vector<uint64_t> arrayfint64(1, 1); 141406f6ba60Sopenharmony_ci msgOpt.add_len_repeated_packed_fixed(arrayfint64); 141506f6ba60Sopenharmony_ci ASSERT_EQ(msgOpt.Finish(), (int32_t)-1); 141606f6ba60Sopenharmony_ci} 141706f6ba60Sopenharmony_ci 141806f6ba60Sopenharmony_ciHWTEST_F(BaseMessageUnittest, SubMessageAlwaysAvailabe, TestSize.Level1) 141906f6ba60Sopenharmony_ci{ 142006f6ba60Sopenharmony_ci ProtoEncoder::ExampleMessage msgOpt(nullptr); 142106f6ba60Sopenharmony_ci 142206f6ba60Sopenharmony_ci ProtoEncoder::SubMessage* subMsgOpt = msgOpt.mutable_len_sub(); 142306f6ba60Sopenharmony_ci ASSERT_TRUE(subMsgOpt != nullptr); 142406f6ba60Sopenharmony_ci subMsgOpt->set_vint_int32(1); 142506f6ba60Sopenharmony_ci subMsgOpt->set_vint_int64(1); 142606f6ba60Sopenharmony_ci subMsgOpt->set_vint_uint32(1); 142706f6ba60Sopenharmony_ci subMsgOpt->set_vint_uint64(1); 142806f6ba60Sopenharmony_ci ASSERT_EQ(subMsgOpt->Finish(), (int32_t)-1); 142906f6ba60Sopenharmony_ci 143006f6ba60Sopenharmony_ci ProtoEncoder::ExampleMessage* nested1 = msgOpt.add_repeated_example(); 143106f6ba60Sopenharmony_ci ASSERT_TRUE(nested1 != nullptr); 143206f6ba60Sopenharmony_ci ProtoEncoder::ExampleMessage* nested2 = nested1->add_repeated_example(); 143306f6ba60Sopenharmony_ci ASSERT_TRUE(nested2 != nullptr); 143406f6ba60Sopenharmony_ci ProtoEncoder::ExampleMessage* nested3 = nested2->add_repeated_example(); 143506f6ba60Sopenharmony_ci ASSERT_TRUE(nested3 != nullptr); 143606f6ba60Sopenharmony_ci 143706f6ba60Sopenharmony_ci ASSERT_EQ(nested3->Finish(), (int32_t)-1); 143806f6ba60Sopenharmony_ci ASSERT_EQ(nested2->Finish(), (int32_t)-1); 143906f6ba60Sopenharmony_ci ASSERT_EQ(nested1->Finish(), (int32_t)-1); 144006f6ba60Sopenharmony_ci 144106f6ba60Sopenharmony_ci msgOpt.set_vint_int32(1); 144206f6ba60Sopenharmony_ci ASSERT_EQ(msgOpt.Finish(), (int32_t)-1); 144306f6ba60Sopenharmony_ci} 144406f6ba60Sopenharmony_ci 144506f6ba60Sopenharmony_ciHWTEST_F(BaseMessageUnittest, MessagePool, TestSize.Level1) 144606f6ba60Sopenharmony_ci{ 144706f6ba60Sopenharmony_ci ProtoEncoder::MessagePool msgStack; 144806f6ba60Sopenharmony_ci ProtoEncoder::BaseMessage* pMsg = msgStack.Get(); 144906f6ba60Sopenharmony_ci ASSERT_TRUE(pMsg != nullptr); 145006f6ba60Sopenharmony_ci 145106f6ba60Sopenharmony_ci msgStack.Reset(0); 145206f6ba60Sopenharmony_ci pMsg = msgStack.Get(); 145306f6ba60Sopenharmony_ci ASSERT_TRUE(pMsg != nullptr); 145406f6ba60Sopenharmony_ci 145506f6ba60Sopenharmony_ci const uint32_t testRepeat = 1000; 145606f6ba60Sopenharmony_ci const uint32_t testDepth = 5; 145706f6ba60Sopenharmony_ci msgStack.Reset(testDepth); 145806f6ba60Sopenharmony_ci for (uint32_t i = 0; i < testRepeat; i++) { 145906f6ba60Sopenharmony_ci pMsg = msgStack.Get(); 146006f6ba60Sopenharmony_ci ASSERT_TRUE(pMsg != nullptr); 146106f6ba60Sopenharmony_ci msgStack.Release(); 146206f6ba60Sopenharmony_ci } 146306f6ba60Sopenharmony_ci 146406f6ba60Sopenharmony_ci for (uint32_t i = 0; i < testRepeat; i++) { 146506f6ba60Sopenharmony_ci for (uint32_t j = 0; j < testDepth; j++) { 146606f6ba60Sopenharmony_ci pMsg = msgStack.Get(); 146706f6ba60Sopenharmony_ci ASSERT_TRUE(pMsg != nullptr); 146806f6ba60Sopenharmony_ci } 146906f6ba60Sopenharmony_ci for (uint32_t j = 0; j < testDepth; j++) { 147006f6ba60Sopenharmony_ci msgStack.Release(); 147106f6ba60Sopenharmony_ci } 147206f6ba60Sopenharmony_ci } 147306f6ba60Sopenharmony_ci 147406f6ba60Sopenharmony_ci for (uint32_t i = 0; i < testRepeat; i++) { 147506f6ba60Sopenharmony_ci pMsg = msgStack.Get(); 147606f6ba60Sopenharmony_ci ASSERT_TRUE(pMsg != nullptr); 147706f6ba60Sopenharmony_ci } 147806f6ba60Sopenharmony_ci for (uint32_t i = 0; i < testRepeat; i++) { 147906f6ba60Sopenharmony_ci msgStack.Release(); 148006f6ba60Sopenharmony_ci } 148106f6ba60Sopenharmony_ci 148206f6ba60Sopenharmony_ci msgStack.Reset(1); 148306f6ba60Sopenharmony_ci for (uint32_t i = 0; i < testDepth; i++) { 148406f6ba60Sopenharmony_ci pMsg = msgStack.Get(); 148506f6ba60Sopenharmony_ci ASSERT_TRUE(pMsg != nullptr); 148606f6ba60Sopenharmony_ci } 148706f6ba60Sopenharmony_ci ProtoEncoder::ExampleMessage* pExampleMsg = static_cast<ProtoEncoder::ExampleMessage*>(pMsg); 148806f6ba60Sopenharmony_ci g_writeCtx.seek(&g_writeCtx, 0); 148906f6ba60Sopenharmony_ci pExampleMsg->Reset(&g_writeCtx); 149006f6ba60Sopenharmony_ci pExampleMsg->set_vint_int32(1); 149106f6ba60Sopenharmony_ci ExampleMessage msgProtobuf; 149206f6ba60Sopenharmony_ci ASSERT_TRUE(msgProtobuf.ParseFromArray(g_buf, g_writePos)); 149306f6ba60Sopenharmony_ci ASSERT_EQ(msgProtobuf.vint_int32(), (int32_t)1); 149406f6ba60Sopenharmony_ci 149506f6ba60Sopenharmony_ci msgStack.Reset(1); 149606f6ba60Sopenharmony_ci ProtoEncoder::ExampleMessage msgOpt(&g_writeCtx, &msgStack); 149706f6ba60Sopenharmony_ci g_writeCtx.seek(&g_writeCtx, 0); 149806f6ba60Sopenharmony_ci ProtoEncoder::SubMessage* subMsgOpt = msgOpt.mutable_len_sub(); 149906f6ba60Sopenharmony_ci ASSERT_TRUE(subMsgOpt != nullptr); 150006f6ba60Sopenharmony_ci msgOpt.set_vint_int32(1); 150106f6ba60Sopenharmony_ci ASSERT_TRUE(msgProtobuf.ParseFromArray(g_buf, g_writePos)); 150206f6ba60Sopenharmony_ci ASSERT_TRUE(msgProtobuf.has_len_sub()); 150306f6ba60Sopenharmony_ci ASSERT_EQ(msgProtobuf.vint_int32(), (int32_t)1); 150406f6ba60Sopenharmony_ci} 150506f6ba60Sopenharmony_ci 150606f6ba60Sopenharmony_ci// reference from https://perfetto.dev/docs/design-docs/protozero 150706f6ba60Sopenharmony_ci// For the full code of the benchmark see /src/protozero/test/protozero_benchmark.cc 150806f6ba60Sopenharmony_ciconst int TEST_TIMES = 1000 * 100; 150906f6ba60Sopenharmony_ciuint64_t g_fakeInputSimple[] = {0x12345678, 151006f6ba60Sopenharmony_ci 0x90ABCDEF, 151106f6ba60Sopenharmony_ci 0x11111111, 151206f6ba60Sopenharmony_ci 0xFFFFFFFF, 151306f6ba60Sopenharmony_ci 0x6666666666666666ULL, 151406f6ba60Sopenharmony_ci 0x6666666666666666ULL, 151506f6ba60Sopenharmony_ci 0x6666666666666666ULL, 151606f6ba60Sopenharmony_ci 0x0066666666666666ULL}; 151706f6ba60Sopenharmony_ci 151806f6ba60Sopenharmony_ciHWTEST_F(BaseMessageUnittest, ProtobufferSimple, TestSize.Level1) 151906f6ba60Sopenharmony_ci{ 152006f6ba60Sopenharmony_ci for (int count = 0; count < TEST_TIMES; count++) { 152106f6ba60Sopenharmony_ci int index = 0; 152206f6ba60Sopenharmony_ci ExampleMessage msgProtobuf; 152306f6ba60Sopenharmony_ci 152406f6ba60Sopenharmony_ci msgProtobuf.set_vint_int32(static_cast<int32_t>(g_fakeInputSimple[index++])); 152506f6ba60Sopenharmony_ci msgProtobuf.set_vint_uint32(static_cast<uint32_t>(g_fakeInputSimple[index++])); 152606f6ba60Sopenharmony_ci msgProtobuf.set_vint_int64(static_cast<int64_t>(g_fakeInputSimple[index++])); 152706f6ba60Sopenharmony_ci msgProtobuf.set_vint_uint64(static_cast<uint64_t>(g_fakeInputSimple[index++])); 152806f6ba60Sopenharmony_ci msgProtobuf.set_len_string(reinterpret_cast<const char*>(&g_fakeInputSimple[index++])); 152906f6ba60Sopenharmony_ci 153006f6ba60Sopenharmony_ci ASSERT_TRUE(msgProtobuf.SerializeToArray(&g_buf[0], SIZE_BUFFER) > 0); 153106f6ba60Sopenharmony_ci } 153206f6ba60Sopenharmony_ci printf("%d times\n", TEST_TIMES); 153306f6ba60Sopenharmony_ci} 153406f6ba60Sopenharmony_ci 153506f6ba60Sopenharmony_ciHWTEST_F(BaseMessageUnittest, ProtoEncoderSimple, TestSize.Level1) 153606f6ba60Sopenharmony_ci{ 153706f6ba60Sopenharmony_ci for (int count = 0; count < TEST_TIMES; count++) { 153806f6ba60Sopenharmony_ci int index = 0; 153906f6ba60Sopenharmony_ci g_writeCtx.seek(&g_writeCtx, 0); 154006f6ba60Sopenharmony_ci 154106f6ba60Sopenharmony_ci ProtoEncoder::ExampleMessage msgOpt(&g_writeCtx); 154206f6ba60Sopenharmony_ci 154306f6ba60Sopenharmony_ci msgOpt.set_vint_int32(static_cast<int32_t>(g_fakeInputSimple[index++])); 154406f6ba60Sopenharmony_ci msgOpt.set_vint_uint32(static_cast<uint32_t>(g_fakeInputSimple[index++])); 154506f6ba60Sopenharmony_ci msgOpt.set_vint_int64(static_cast<int64_t>(g_fakeInputSimple[index++])); 154606f6ba60Sopenharmony_ci msgOpt.set_vint_uint64(static_cast<uint64_t>(g_fakeInputSimple[index++])); 154706f6ba60Sopenharmony_ci msgOpt.set_len_string(reinterpret_cast<const char*>(&g_fakeInputSimple[index++])); 154806f6ba60Sopenharmony_ci 154906f6ba60Sopenharmony_ci ASSERT_TRUE(msgOpt.Finish() > 0); 155006f6ba60Sopenharmony_ci } 155106f6ba60Sopenharmony_ci printf("%d times\n", TEST_TIMES); 155206f6ba60Sopenharmony_ci} 155306f6ba60Sopenharmony_ci 155406f6ba60Sopenharmony_ciHWTEST_F(BaseMessageUnittest, ProtobufferNested, TestSize.Level1) 155506f6ba60Sopenharmony_ci{ 155606f6ba60Sopenharmony_ci for (int count = 0; count < TEST_TIMES; count++) { 155706f6ba60Sopenharmony_ci int index = 0; 155806f6ba60Sopenharmony_ci ExampleMessage msgProtobuf; 155906f6ba60Sopenharmony_ci 156006f6ba60Sopenharmony_ci msgProtobuf.set_vint_int32(static_cast<int32_t>(g_fakeInputSimple[index++])); 156106f6ba60Sopenharmony_ci msgProtobuf.set_vint_uint32(static_cast<uint32_t>(g_fakeInputSimple[index++])); 156206f6ba60Sopenharmony_ci msgProtobuf.set_vint_int64(static_cast<int64_t>(g_fakeInputSimple[index++])); 156306f6ba60Sopenharmony_ci msgProtobuf.set_vint_uint64(static_cast<uint64_t>(g_fakeInputSimple[index++])); 156406f6ba60Sopenharmony_ci msgProtobuf.set_len_string(reinterpret_cast<const char*>(&g_fakeInputSimple[index++])); 156506f6ba60Sopenharmony_ci // fisrt nested 156606f6ba60Sopenharmony_ci ExampleMessage* nested1 = msgProtobuf.add_repeated_example(); 156706f6ba60Sopenharmony_ci ASSERT_TRUE(nested1 != nullptr); 156806f6ba60Sopenharmony_ci index = 0; 156906f6ba60Sopenharmony_ci nested1->set_vint_int32(static_cast<int32_t>(g_fakeInputSimple[index++])); 157006f6ba60Sopenharmony_ci nested1->set_vint_uint32(static_cast<uint32_t>(g_fakeInputSimple[index++])); 157106f6ba60Sopenharmony_ci nested1->set_vint_int64(static_cast<int64_t>(g_fakeInputSimple[index++])); 157206f6ba60Sopenharmony_ci nested1->set_vint_uint64(static_cast<uint64_t>(g_fakeInputSimple[index++])); 157306f6ba60Sopenharmony_ci nested1->set_len_string(reinterpret_cast<const char*>(&g_fakeInputSimple[index++])); 157406f6ba60Sopenharmony_ci // second nested 157506f6ba60Sopenharmony_ci ExampleMessage* nested2 = nested1->add_repeated_example(); 157606f6ba60Sopenharmony_ci ASSERT_TRUE(nested2 != nullptr); 157706f6ba60Sopenharmony_ci index = 0; 157806f6ba60Sopenharmony_ci nested2->set_vint_int32(static_cast<int32_t>(g_fakeInputSimple[index++])); 157906f6ba60Sopenharmony_ci nested2->set_vint_uint32(static_cast<uint32_t>(g_fakeInputSimple[index++])); 158006f6ba60Sopenharmony_ci nested2->set_vint_int64(static_cast<int64_t>(g_fakeInputSimple[index++])); 158106f6ba60Sopenharmony_ci nested2->set_vint_uint64(static_cast<uint64_t>(g_fakeInputSimple[index++])); 158206f6ba60Sopenharmony_ci nested2->set_len_string(reinterpret_cast<const char*>(&g_fakeInputSimple[index++])); 158306f6ba60Sopenharmony_ci // third nested 158406f6ba60Sopenharmony_ci ExampleMessage* nested3 = nested2->add_repeated_example(); 158506f6ba60Sopenharmony_ci ASSERT_TRUE(nested3 != nullptr); 158606f6ba60Sopenharmony_ci index = 0; 158706f6ba60Sopenharmony_ci nested3->set_vint_int32(static_cast<int32_t>(g_fakeInputSimple[index++])); 158806f6ba60Sopenharmony_ci nested3->set_vint_uint32(static_cast<uint32_t>(g_fakeInputSimple[index++])); 158906f6ba60Sopenharmony_ci nested3->set_vint_int64(static_cast<int64_t>(g_fakeInputSimple[index++])); 159006f6ba60Sopenharmony_ci nested3->set_vint_uint64(static_cast<uint64_t>(g_fakeInputSimple[index++])); 159106f6ba60Sopenharmony_ci nested3->set_len_string(reinterpret_cast<const char*>(&g_fakeInputSimple[index++])); 159206f6ba60Sopenharmony_ci 159306f6ba60Sopenharmony_ci ASSERT_TRUE(msgProtobuf.SerializeToArray(&g_buf[0], SIZE_BUFFER) > 0); 159406f6ba60Sopenharmony_ci } 159506f6ba60Sopenharmony_ci printf("%d times\n", TEST_TIMES); 159606f6ba60Sopenharmony_ci} 159706f6ba60Sopenharmony_ci 159806f6ba60Sopenharmony_ciHWTEST_F(BaseMessageUnittest, ProtoEncoderNested, TestSize.Level1) 159906f6ba60Sopenharmony_ci{ 160006f6ba60Sopenharmony_ci for (int count = 0; count < TEST_TIMES; count++) { 160106f6ba60Sopenharmony_ci int index = 0; 160206f6ba60Sopenharmony_ci g_writeCtx.seek(&g_writeCtx, 0); 160306f6ba60Sopenharmony_ci 160406f6ba60Sopenharmony_ci ProtoEncoder::ExampleMessage msgOpt(&g_writeCtx); 160506f6ba60Sopenharmony_ci 160606f6ba60Sopenharmony_ci msgOpt.set_vint_int32(static_cast<int32_t>(g_fakeInputSimple[index++])); 160706f6ba60Sopenharmony_ci msgOpt.set_vint_uint32(static_cast<uint32_t>(g_fakeInputSimple[index++])); 160806f6ba60Sopenharmony_ci msgOpt.set_vint_int64(static_cast<int64_t>(g_fakeInputSimple[index++])); 160906f6ba60Sopenharmony_ci msgOpt.set_vint_uint64(static_cast<uint64_t>(g_fakeInputSimple[index++])); 161006f6ba60Sopenharmony_ci msgOpt.set_len_string(reinterpret_cast<const char*>(&g_fakeInputSimple[index++])); 161106f6ba60Sopenharmony_ci // fisrt nested 161206f6ba60Sopenharmony_ci ProtoEncoder::ExampleMessage* nested1 = msgOpt.add_repeated_example(); 161306f6ba60Sopenharmony_ci ASSERT_TRUE(nested1 != nullptr); 161406f6ba60Sopenharmony_ci index = 0; 161506f6ba60Sopenharmony_ci nested1->set_vint_int32(static_cast<int32_t>(g_fakeInputSimple[index++])); 161606f6ba60Sopenharmony_ci nested1->set_vint_uint32(static_cast<uint32_t>(g_fakeInputSimple[index++])); 161706f6ba60Sopenharmony_ci nested1->set_vint_int64(static_cast<int64_t>(g_fakeInputSimple[index++])); 161806f6ba60Sopenharmony_ci nested1->set_vint_uint64(static_cast<uint64_t>(g_fakeInputSimple[index++])); 161906f6ba60Sopenharmony_ci nested1->set_len_string(reinterpret_cast<const char*>(&g_fakeInputSimple[index++])); 162006f6ba60Sopenharmony_ci // second nested 162106f6ba60Sopenharmony_ci ProtoEncoder::ExampleMessage* nested2 = nested1->add_repeated_example(); 162206f6ba60Sopenharmony_ci ASSERT_TRUE(nested2 != nullptr); 162306f6ba60Sopenharmony_ci index = 0; 162406f6ba60Sopenharmony_ci nested2->set_vint_int32(static_cast<int32_t>(g_fakeInputSimple[index++])); 162506f6ba60Sopenharmony_ci nested2->set_vint_uint32(static_cast<uint32_t>(g_fakeInputSimple[index++])); 162606f6ba60Sopenharmony_ci nested2->set_vint_int64(static_cast<int64_t>(g_fakeInputSimple[index++])); 162706f6ba60Sopenharmony_ci nested2->set_vint_uint64(static_cast<uint64_t>(g_fakeInputSimple[index++])); 162806f6ba60Sopenharmony_ci nested2->set_len_string(reinterpret_cast<const char*>(&g_fakeInputSimple[index++])); 162906f6ba60Sopenharmony_ci // third nested 163006f6ba60Sopenharmony_ci ProtoEncoder::ExampleMessage* nested3 = nested2->add_repeated_example(); 163106f6ba60Sopenharmony_ci ASSERT_TRUE(nested3 != nullptr); 163206f6ba60Sopenharmony_ci index = 0; 163306f6ba60Sopenharmony_ci nested3->set_vint_int32(static_cast<int32_t>(g_fakeInputSimple[index++])); 163406f6ba60Sopenharmony_ci nested3->set_vint_uint32(static_cast<uint32_t>(g_fakeInputSimple[index++])); 163506f6ba60Sopenharmony_ci nested3->set_vint_int64(static_cast<int64_t>(g_fakeInputSimple[index++])); 163606f6ba60Sopenharmony_ci nested3->set_vint_uint64(static_cast<uint64_t>(g_fakeInputSimple[index++])); 163706f6ba60Sopenharmony_ci nested3->set_len_string(reinterpret_cast<const char*>(&g_fakeInputSimple[index++])); 163806f6ba60Sopenharmony_ci 163906f6ba60Sopenharmony_ci ASSERT_TRUE(msgOpt.Finish() > 0); 164006f6ba60Sopenharmony_ci } 164106f6ba60Sopenharmony_ci printf("%d times\n", TEST_TIMES); 164206f6ba60Sopenharmony_ci} 164306f6ba60Sopenharmony_ci} // namespace Profiler 164406f6ba60Sopenharmony_ci} // namespace Developtools 164506f6ba60Sopenharmony_ci} // namespace OHOS 1646