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 <cstdlib>
1606f6ba60Sopenharmony_ci#include <limits>
1706f6ba60Sopenharmony_ci#include <vector>
1806f6ba60Sopenharmony_ci#include <gtest/gtest.h>
1906f6ba60Sopenharmony_ci
2006f6ba60Sopenharmony_ci#include "varint_encode.h"
2106f6ba60Sopenharmony_ci
2206f6ba60Sopenharmony_cinamespace OHOS {
2306f6ba60Sopenharmony_cinamespace Developtools {
2406f6ba60Sopenharmony_cinamespace Profiler {
2506f6ba60Sopenharmony_ciusing namespace testing::ext;
2606f6ba60Sopenharmony_ciusing namespace ProtoEncoder;
2706f6ba60Sopenharmony_ci
2806f6ba60Sopenharmony_ciclass VarintEncodeUnittest : public ::testing::Test {
2906f6ba60Sopenharmony_cipublic:
3006f6ba60Sopenharmony_ci    static void SetUpTestCase() {};
3106f6ba60Sopenharmony_ci    static void TearDownTestCase() {};
3206f6ba60Sopenharmony_ci
3306f6ba60Sopenharmony_ci    void SetUp() {};
3406f6ba60Sopenharmony_ci    void TearDown() {};
3506f6ba60Sopenharmony_ci
3606f6ba60Sopenharmony_ci    const uint32_t two = 2;
3706f6ba60Sopenharmony_ci    const uint32_t three = 3;
3806f6ba60Sopenharmony_ci    const uint32_t four = 4;
3906f6ba60Sopenharmony_ci};
4006f6ba60Sopenharmony_ci
4106f6ba60Sopenharmony_ciHWTEST_F(VarintEncodeUnittest, GetPackedVarintLenSize, TestSize.Level1)
4206f6ba60Sopenharmony_ci{
4306f6ba60Sopenharmony_ci    uint32_t len = 0;
4406f6ba60Sopenharmony_ci    uint32_t itemCount = 0;
4506f6ba60Sopenharmony_ci    uint32_t itemSize = sizeof(char);
4606f6ba60Sopenharmony_ci    EXPECT_EQ(GetPackedVarintLenSize(itemCount, itemSize, len), (uint32_t)1);
4706f6ba60Sopenharmony_ci    EXPECT_EQ(len, (uint32_t)0);
4806f6ba60Sopenharmony_ci
4906f6ba60Sopenharmony_ci    itemSize = sizeof(int32_t);
5006f6ba60Sopenharmony_ci    EXPECT_EQ(GetPackedVarintLenSize(itemCount, itemSize, len), (uint32_t)1);
5106f6ba60Sopenharmony_ci    EXPECT_EQ(len, itemCount * itemSize);
5206f6ba60Sopenharmony_ci
5306f6ba60Sopenharmony_ci    itemSize = sizeof(int64_t);
5406f6ba60Sopenharmony_ci    EXPECT_EQ(GetPackedVarintLenSize(itemCount, itemSize, len), (uint32_t)1);
5506f6ba60Sopenharmony_ci    EXPECT_EQ(len, itemCount * itemSize);
5606f6ba60Sopenharmony_ci
5706f6ba60Sopenharmony_ci    itemSize = sizeof(char);
5806f6ba60Sopenharmony_ci    itemCount = VARINT_MAX_1BYTE;
5906f6ba60Sopenharmony_ci    EXPECT_EQ(GetPackedVarintLenSize(itemCount, itemSize, len), (uint32_t)1);
6006f6ba60Sopenharmony_ci    EXPECT_EQ(len, itemCount * itemSize);
6106f6ba60Sopenharmony_ci
6206f6ba60Sopenharmony_ci    itemCount++;
6306f6ba60Sopenharmony_ci    EXPECT_EQ(GetPackedVarintLenSize(itemCount, itemSize, len), two);
6406f6ba60Sopenharmony_ci    EXPECT_EQ(len, itemCount * itemSize);
6506f6ba60Sopenharmony_ci
6606f6ba60Sopenharmony_ci    itemCount = VARINT_MAX_2BYTE;
6706f6ba60Sopenharmony_ci    EXPECT_EQ(GetPackedVarintLenSize(itemCount, itemSize, len), two);
6806f6ba60Sopenharmony_ci    EXPECT_EQ(len, itemCount * itemSize);
6906f6ba60Sopenharmony_ci
7006f6ba60Sopenharmony_ci    itemCount++;
7106f6ba60Sopenharmony_ci    EXPECT_EQ(GetPackedVarintLenSize(itemCount, itemSize, len), three);
7206f6ba60Sopenharmony_ci    EXPECT_EQ(len, itemCount * itemSize);
7306f6ba60Sopenharmony_ci
7406f6ba60Sopenharmony_ci    itemCount = VARINT_MAX_3BYTE;
7506f6ba60Sopenharmony_ci    EXPECT_EQ(GetPackedVarintLenSize(itemCount, itemSize, len), three);
7606f6ba60Sopenharmony_ci    EXPECT_EQ(len, itemCount * itemSize);
7706f6ba60Sopenharmony_ci
7806f6ba60Sopenharmony_ci    itemCount++;
7906f6ba60Sopenharmony_ci    EXPECT_EQ(GetPackedVarintLenSize(itemCount, itemSize, len), four);
8006f6ba60Sopenharmony_ci    EXPECT_EQ(len, itemCount * itemSize);
8106f6ba60Sopenharmony_ci
8206f6ba60Sopenharmony_ci    itemCount = VARINT_MAX_4BYTE;
8306f6ba60Sopenharmony_ci    EXPECT_EQ(GetPackedVarintLenSize(itemCount, itemSize, len), four);
8406f6ba60Sopenharmony_ci    EXPECT_EQ(len, itemCount * itemSize);
8506f6ba60Sopenharmony_ci
8606f6ba60Sopenharmony_ci    itemCount++;
8706f6ba60Sopenharmony_ci    EXPECT_EQ(GetPackedVarintLenSize(itemCount, itemSize, len), (uint32_t)0);
8806f6ba60Sopenharmony_ci    EXPECT_EQ(len, itemCount * itemSize);
8906f6ba60Sopenharmony_ci
9006f6ba60Sopenharmony_ci    itemSize = sizeof(int32_t);
9106f6ba60Sopenharmony_ci    itemCount = VARINT_MAX_1BYTE / (itemSize + 1);
9206f6ba60Sopenharmony_ci    EXPECT_EQ(GetPackedVarintLenSize(itemCount, itemSize, len), (uint32_t)1);
9306f6ba60Sopenharmony_ci    EXPECT_EQ(len, itemCount * (itemSize + 1));
9406f6ba60Sopenharmony_ci
9506f6ba60Sopenharmony_ci    itemCount++;
9606f6ba60Sopenharmony_ci    EXPECT_EQ(GetPackedVarintLenSize(itemCount, itemSize, len), two);
9706f6ba60Sopenharmony_ci    EXPECT_EQ(len, itemCount * (itemSize + 1));
9806f6ba60Sopenharmony_ci
9906f6ba60Sopenharmony_ci    itemCount = VARINT_MAX_2BYTE / (itemSize + 1);
10006f6ba60Sopenharmony_ci    EXPECT_EQ(GetPackedVarintLenSize(itemCount, itemSize, len), two);
10106f6ba60Sopenharmony_ci    EXPECT_EQ(len, itemCount * (itemSize + 1));
10206f6ba60Sopenharmony_ci
10306f6ba60Sopenharmony_ci    itemCount++;
10406f6ba60Sopenharmony_ci    EXPECT_EQ(GetPackedVarintLenSize(itemCount, itemSize, len), three);
10506f6ba60Sopenharmony_ci    EXPECT_EQ(len, itemCount * (itemSize + 1));
10606f6ba60Sopenharmony_ci
10706f6ba60Sopenharmony_ci    itemCount = VARINT_MAX_3BYTE / (itemSize + 1);
10806f6ba60Sopenharmony_ci    EXPECT_EQ(GetPackedVarintLenSize(itemCount, itemSize, len), three);
10906f6ba60Sopenharmony_ci    EXPECT_EQ(len, itemCount * (itemSize + 1));
11006f6ba60Sopenharmony_ci
11106f6ba60Sopenharmony_ci    itemCount++;
11206f6ba60Sopenharmony_ci    EXPECT_EQ(GetPackedVarintLenSize(itemCount, itemSize, len), four);
11306f6ba60Sopenharmony_ci    EXPECT_EQ(len, itemCount * (itemSize + 1));
11406f6ba60Sopenharmony_ci
11506f6ba60Sopenharmony_ci    itemCount = VARINT_MAX_4BYTE / (itemSize + 1);
11606f6ba60Sopenharmony_ci    EXPECT_EQ(GetPackedVarintLenSize(itemCount, itemSize, len), four);
11706f6ba60Sopenharmony_ci    EXPECT_EQ(len, itemCount * (itemSize + 1));
11806f6ba60Sopenharmony_ci
11906f6ba60Sopenharmony_ci    itemCount++;
12006f6ba60Sopenharmony_ci    EXPECT_EQ(GetPackedVarintLenSize(itemCount, itemSize, len), (uint32_t)0);
12106f6ba60Sopenharmony_ci    EXPECT_EQ(len, itemCount * (itemSize + 1));
12206f6ba60Sopenharmony_ci
12306f6ba60Sopenharmony_ci    itemSize = sizeof(int64_t);
12406f6ba60Sopenharmony_ci    itemCount = VARINT_MAX_1BYTE / (itemSize + two);
12506f6ba60Sopenharmony_ci    EXPECT_EQ(GetPackedVarintLenSize(itemCount, itemSize, len), (uint32_t)1);
12606f6ba60Sopenharmony_ci    EXPECT_EQ(len, itemCount * (itemSize + two));
12706f6ba60Sopenharmony_ci
12806f6ba60Sopenharmony_ci    itemCount++;
12906f6ba60Sopenharmony_ci    EXPECT_EQ(GetPackedVarintLenSize(itemCount, itemSize, len), two);
13006f6ba60Sopenharmony_ci    EXPECT_EQ(len, itemCount * (itemSize + two));
13106f6ba60Sopenharmony_ci
13206f6ba60Sopenharmony_ci    itemCount = VARINT_MAX_2BYTE / (itemSize + two);
13306f6ba60Sopenharmony_ci    EXPECT_EQ(GetPackedVarintLenSize(itemCount, itemSize, len), two);
13406f6ba60Sopenharmony_ci    EXPECT_EQ(len, itemCount * (itemSize + two));
13506f6ba60Sopenharmony_ci
13606f6ba60Sopenharmony_ci    itemCount++;
13706f6ba60Sopenharmony_ci    EXPECT_EQ(GetPackedVarintLenSize(itemCount, itemSize, len), three);
13806f6ba60Sopenharmony_ci    EXPECT_EQ(len, itemCount * (itemSize + two));
13906f6ba60Sopenharmony_ci
14006f6ba60Sopenharmony_ci    itemCount = VARINT_MAX_3BYTE / (itemSize + two);
14106f6ba60Sopenharmony_ci    EXPECT_EQ(GetPackedVarintLenSize(itemCount, itemSize, len), three);
14206f6ba60Sopenharmony_ci    EXPECT_EQ(len, itemCount * (itemSize + two));
14306f6ba60Sopenharmony_ci
14406f6ba60Sopenharmony_ci    itemCount++;
14506f6ba60Sopenharmony_ci    EXPECT_EQ(GetPackedVarintLenSize(itemCount, itemSize, len), four);
14606f6ba60Sopenharmony_ci    EXPECT_EQ(len, itemCount * (itemSize + two));
14706f6ba60Sopenharmony_ci
14806f6ba60Sopenharmony_ci    itemCount = VARINT_MAX_4BYTE / (itemSize + two);
14906f6ba60Sopenharmony_ci    EXPECT_EQ(GetPackedVarintLenSize(itemCount, itemSize, len), four);
15006f6ba60Sopenharmony_ci    EXPECT_EQ(len, itemCount * (itemSize + two));
15106f6ba60Sopenharmony_ci
15206f6ba60Sopenharmony_ci    itemCount++;
15306f6ba60Sopenharmony_ci    EXPECT_EQ(GetPackedVarintLenSize(itemCount, itemSize, len), (uint32_t)0);
15406f6ba60Sopenharmony_ci    EXPECT_EQ(len, itemCount * (itemSize + two));
15506f6ba60Sopenharmony_ci}
15606f6ba60Sopenharmony_ci
15706f6ba60Sopenharmony_ciHWTEST_F(VarintEncodeUnittest, EncodeZigZag, TestSize.Level1)
15806f6ba60Sopenharmony_ci{
15906f6ba60Sopenharmony_ci    int8_t signed8 = 0;
16006f6ba60Sopenharmony_ci    EXPECT_EQ(EncodeZigZag(signed8), (uint8_t)0);
16106f6ba60Sopenharmony_ci    signed8 = -1;
16206f6ba60Sopenharmony_ci    EXPECT_EQ(EncodeZigZag(signed8), (uint8_t)1);
16306f6ba60Sopenharmony_ci    signed8 = 1;
16406f6ba60Sopenharmony_ci    EXPECT_EQ(EncodeZigZag(signed8), (uint8_t)(signed8 * two));
16506f6ba60Sopenharmony_ci    std::srand(std::time(nullptr));
16606f6ba60Sopenharmony_ci    int rand = std::rand();
16706f6ba60Sopenharmony_ci    signed8 = static_cast<int8_t>(rand);
16806f6ba60Sopenharmony_ci    if (signed8 >= 0) {
16906f6ba60Sopenharmony_ci        // n are encoded as 2 * n
17006f6ba60Sopenharmony_ci        EXPECT_EQ(EncodeZigZag(signed8), (uint8_t)(signed8 * two));
17106f6ba60Sopenharmony_ci    } else {
17206f6ba60Sopenharmony_ci        // -n are encoded as 2 * n + 1
17306f6ba60Sopenharmony_ci        EXPECT_EQ(EncodeZigZag(signed8), (uint8_t)(-signed8 - 1 + -signed8));
17406f6ba60Sopenharmony_ci    }
17506f6ba60Sopenharmony_ci    signed8 = std::numeric_limits<int8_t>::min();
17606f6ba60Sopenharmony_ci    EXPECT_EQ(EncodeZigZag(signed8), std::numeric_limits<uint8_t>::max());
17706f6ba60Sopenharmony_ci    signed8 = std::numeric_limits<int8_t>::max();
17806f6ba60Sopenharmony_ci    EXPECT_EQ(EncodeZigZag(signed8), std::numeric_limits<uint8_t>::max() - 1);
17906f6ba60Sopenharmony_ci
18006f6ba60Sopenharmony_ci    int32_t signed32 = 0;
18106f6ba60Sopenharmony_ci    EXPECT_EQ(EncodeZigZag(signed32), (uint32_t)0);
18206f6ba60Sopenharmony_ci    signed32 = -1;
18306f6ba60Sopenharmony_ci    EXPECT_EQ(EncodeZigZag(signed32), (uint32_t)1);
18406f6ba60Sopenharmony_ci    signed32 = 1;
18506f6ba60Sopenharmony_ci    EXPECT_EQ(EncodeZigZag(signed32), (uint32_t)(signed32 * two));
18606f6ba60Sopenharmony_ci    std::srand(rand);
18706f6ba60Sopenharmony_ci    rand = std::rand();
18806f6ba60Sopenharmony_ci    signed32 = static_cast<int32_t>(rand);
18906f6ba60Sopenharmony_ci    if (signed32 >= 0) {
19006f6ba60Sopenharmony_ci        // n are encoded as 2 * n
19106f6ba60Sopenharmony_ci        EXPECT_EQ(EncodeZigZag(signed32), (uint32_t)(signed32 * two));
19206f6ba60Sopenharmony_ci    } else {
19306f6ba60Sopenharmony_ci        // -n are encoded as 2 * n + 1
19406f6ba60Sopenharmony_ci        EXPECT_EQ(EncodeZigZag(signed32), (uint32_t)(-signed32 - 1 + -signed32));
19506f6ba60Sopenharmony_ci    }
19606f6ba60Sopenharmony_ci    signed32 = std::numeric_limits<int32_t>::min();
19706f6ba60Sopenharmony_ci    EXPECT_EQ(EncodeZigZag(signed32), std::numeric_limits<uint32_t>::max());
19806f6ba60Sopenharmony_ci    signed32 = std::numeric_limits<int32_t>::max();
19906f6ba60Sopenharmony_ci    EXPECT_EQ(EncodeZigZag(signed32), std::numeric_limits<uint32_t>::max() - 1);
20006f6ba60Sopenharmony_ci
20106f6ba60Sopenharmony_ci    int64_t signed64 = 0;
20206f6ba60Sopenharmony_ci    EXPECT_EQ(EncodeZigZag(signed64), (uint64_t)0);
20306f6ba60Sopenharmony_ci    signed64 = -1;
20406f6ba60Sopenharmony_ci    EXPECT_EQ(EncodeZigZag(signed64), (uint64_t)1);
20506f6ba60Sopenharmony_ci    signed64 = 1;
20606f6ba60Sopenharmony_ci    EXPECT_EQ(EncodeZigZag(signed64), (uint64_t)(signed64 * two));
20706f6ba60Sopenharmony_ci    std::srand(rand);
20806f6ba60Sopenharmony_ci    rand = std::rand();
20906f6ba60Sopenharmony_ci    signed64 = static_cast<int64_t>(rand);
21006f6ba60Sopenharmony_ci    if (signed64 >= 0) {
21106f6ba60Sopenharmony_ci        // n are encoded as 2 * n
21206f6ba60Sopenharmony_ci        EXPECT_EQ(EncodeZigZag(signed64), (uint64_t)(signed64 * two));
21306f6ba60Sopenharmony_ci    } else {
21406f6ba60Sopenharmony_ci        // -n are encoded as 2 * n + 1
21506f6ba60Sopenharmony_ci        EXPECT_EQ(EncodeZigZag(signed64), (uint64_t)(-signed64 - 1 + -signed64));
21606f6ba60Sopenharmony_ci    }
21706f6ba60Sopenharmony_ci    signed64 = std::numeric_limits<int64_t>::min();
21806f6ba60Sopenharmony_ci    EXPECT_EQ(EncodeZigZag(signed64), std::numeric_limits<uint64_t>::max());
21906f6ba60Sopenharmony_ci    signed64 = std::numeric_limits<int64_t>::max();
22006f6ba60Sopenharmony_ci    EXPECT_EQ(EncodeZigZag(signed64), std::numeric_limits<uint64_t>::max() - 1);
22106f6ba60Sopenharmony_ci}
22206f6ba60Sopenharmony_ci
22306f6ba60Sopenharmony_cibool CompareBytes(const uint8_t* a, const uint8_t* b, size_t size)
22406f6ba60Sopenharmony_ci{
22506f6ba60Sopenharmony_ci    for (size_t i = 0; i < size; i++) {
22606f6ba60Sopenharmony_ci        if (a[i] != b[i]) {
22706f6ba60Sopenharmony_ci            return false;
22806f6ba60Sopenharmony_ci        }
22906f6ba60Sopenharmony_ci    }
23006f6ba60Sopenharmony_ci    return true;
23106f6ba60Sopenharmony_ci}
23206f6ba60Sopenharmony_ci
23306f6ba60Sopenharmony_ciconst uint8_t ENCODE_BYTES_MIN_BOOL[] = {0x00};
23406f6ba60Sopenharmony_ciconst uint8_t ENCODE_BYTES_MAX_BOOL[] = {0x01};
23506f6ba60Sopenharmony_ciconst uint8_t ENCODE_BYTES_MIN_S8[] = {0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x01};
23606f6ba60Sopenharmony_ciconst uint8_t ENCODE_BYTES_MAX_S8[] = {0x7F};
23706f6ba60Sopenharmony_ciconst uint8_t ENCODE_BYTES_MIN_U8[] = {0x00};
23806f6ba60Sopenharmony_ciconst uint8_t ENCODE_BYTES_MAX_U8[] = {0xFF, 0x01};
23906f6ba60Sopenharmony_ciconst uint8_t ENCODE_BYTES_MIN_S32[] = {0x80, 0x80, 0x80, 0x80, 0xF8, 0xFF, 0xFF, 0xFF, 0xFF, 0x01};
24006f6ba60Sopenharmony_ciconst uint8_t ENCODE_BYTES_MAX_S32[] = {0xFF, 0xFF, 0xFF, 0xFF, 0x07};
24106f6ba60Sopenharmony_ciconst uint8_t ENCODE_BYTES_MIN_U32[] = {0x00};
24206f6ba60Sopenharmony_ciconst uint8_t ENCODE_BYTES_MAX_U32[] = {0xFF, 0xFF, 0xFF, 0xFF, 0x0F};
24306f6ba60Sopenharmony_ciconst uint8_t ENCODE_BYTES_MIN_S64[] = {0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x01};
24406f6ba60Sopenharmony_ciconst uint8_t ENCODE_BYTES_MAX_S64[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F};
24506f6ba60Sopenharmony_ciconst uint8_t ENCODE_BYTES_MIN_U64[] = {0x00};
24606f6ba60Sopenharmony_ciconst uint8_t ENCODE_BYTES_MAX_U64[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x01};
24706f6ba60Sopenharmony_ci
24806f6ba60Sopenharmony_ciHWTEST_F(VarintEncodeUnittest, EncodeVarint, TestSize.Level1)
24906f6ba60Sopenharmony_ci{
25006f6ba60Sopenharmony_ci    uint8_t buf[VARINT_ENCODE_MAX_SIZE] = {0};
25106f6ba60Sopenharmony_ci    bool b = std::numeric_limits<bool>::min();
25206f6ba60Sopenharmony_ci    EXPECT_EQ(EncodeVarint(buf, b), (uint32_t)sizeof(ENCODE_BYTES_MIN_BOOL));
25306f6ba60Sopenharmony_ci    EXPECT_TRUE(CompareBytes(buf, ENCODE_BYTES_MIN_BOOL, sizeof(ENCODE_BYTES_MIN_BOOL)));
25406f6ba60Sopenharmony_ci    b = std::numeric_limits<bool>::max();
25506f6ba60Sopenharmony_ci    EXPECT_EQ(EncodeVarint(buf, b), (uint32_t)sizeof(ENCODE_BYTES_MAX_BOOL));
25606f6ba60Sopenharmony_ci    EXPECT_TRUE(CompareBytes(buf, ENCODE_BYTES_MAX_BOOL, sizeof(ENCODE_BYTES_MAX_BOOL)));
25706f6ba60Sopenharmony_ci
25806f6ba60Sopenharmony_ci    int8_t s8 = std::numeric_limits<int8_t>::min();
25906f6ba60Sopenharmony_ci    EXPECT_EQ(EncodeVarint(buf, s8), (uint32_t)sizeof(ENCODE_BYTES_MIN_S8));
26006f6ba60Sopenharmony_ci    EXPECT_TRUE(CompareBytes(buf, ENCODE_BYTES_MIN_S8, sizeof(ENCODE_BYTES_MIN_S8)));
26106f6ba60Sopenharmony_ci    s8 = std::numeric_limits<int8_t>::max();
26206f6ba60Sopenharmony_ci    EXPECT_EQ(EncodeVarint(buf, s8), (uint32_t)sizeof(ENCODE_BYTES_MAX_S8));
26306f6ba60Sopenharmony_ci    EXPECT_TRUE(CompareBytes(buf, ENCODE_BYTES_MAX_S8, sizeof(ENCODE_BYTES_MAX_S8)));
26406f6ba60Sopenharmony_ci
26506f6ba60Sopenharmony_ci    uint8_t u8 = std::numeric_limits<uint8_t>::min();
26606f6ba60Sopenharmony_ci    EXPECT_EQ(EncodeVarint(buf, u8), (uint32_t)sizeof(ENCODE_BYTES_MIN_U8));
26706f6ba60Sopenharmony_ci    EXPECT_TRUE(CompareBytes(buf, ENCODE_BYTES_MIN_U8, sizeof(ENCODE_BYTES_MIN_U8)));
26806f6ba60Sopenharmony_ci    u8 = std::numeric_limits<uint8_t>::max();
26906f6ba60Sopenharmony_ci    EXPECT_EQ(EncodeVarint(buf, u8), (uint32_t)sizeof(ENCODE_BYTES_MAX_U8));
27006f6ba60Sopenharmony_ci    EXPECT_TRUE(CompareBytes(buf, ENCODE_BYTES_MAX_U8, sizeof(ENCODE_BYTES_MAX_U8)));
27106f6ba60Sopenharmony_ci
27206f6ba60Sopenharmony_ci    int32_t s32 = std::numeric_limits<int32_t>::min();
27306f6ba60Sopenharmony_ci    EXPECT_EQ(EncodeVarint(buf, s32), (uint32_t)sizeof(ENCODE_BYTES_MIN_S32));
27406f6ba60Sopenharmony_ci    EXPECT_TRUE(CompareBytes(buf, ENCODE_BYTES_MIN_S32, sizeof(ENCODE_BYTES_MIN_S32)));
27506f6ba60Sopenharmony_ci    s32 = std::numeric_limits<int32_t>::max();
27606f6ba60Sopenharmony_ci    EXPECT_EQ(EncodeVarint(buf, s32), (uint32_t)sizeof(ENCODE_BYTES_MAX_S32));
27706f6ba60Sopenharmony_ci    EXPECT_TRUE(CompareBytes(buf, ENCODE_BYTES_MAX_S32, sizeof(ENCODE_BYTES_MAX_S32)));
27806f6ba60Sopenharmony_ci
27906f6ba60Sopenharmony_ci    uint32_t u32 = std::numeric_limits<uint32_t>::min();
28006f6ba60Sopenharmony_ci    EXPECT_EQ(EncodeVarint(buf, u32), (uint32_t)sizeof(ENCODE_BYTES_MIN_U32));
28106f6ba60Sopenharmony_ci    EXPECT_TRUE(CompareBytes(buf, ENCODE_BYTES_MIN_U32, sizeof(ENCODE_BYTES_MIN_U32)));
28206f6ba60Sopenharmony_ci    u32 = std::numeric_limits<uint32_t>::max();
28306f6ba60Sopenharmony_ci    EXPECT_EQ(EncodeVarint(buf, u32), (uint32_t)sizeof(ENCODE_BYTES_MAX_U32));
28406f6ba60Sopenharmony_ci    EXPECT_TRUE(CompareBytes(buf, ENCODE_BYTES_MAX_U32, sizeof(ENCODE_BYTES_MAX_U32)));
28506f6ba60Sopenharmony_ci
28606f6ba60Sopenharmony_ci    int64_t s64 = std::numeric_limits<int64_t>::min();
28706f6ba60Sopenharmony_ci    EXPECT_EQ(EncodeVarint(buf, s64), (uint32_t)sizeof(ENCODE_BYTES_MIN_S64));
28806f6ba60Sopenharmony_ci    EXPECT_TRUE(CompareBytes(buf, ENCODE_BYTES_MIN_S64, sizeof(ENCODE_BYTES_MIN_S64)));
28906f6ba60Sopenharmony_ci    s64 = std::numeric_limits<int64_t>::max();
29006f6ba60Sopenharmony_ci    EXPECT_EQ(EncodeVarint(buf, s64), (uint32_t)sizeof(ENCODE_BYTES_MAX_S64));
29106f6ba60Sopenharmony_ci    EXPECT_TRUE(CompareBytes(buf, ENCODE_BYTES_MAX_S64, sizeof(ENCODE_BYTES_MAX_S64)));
29206f6ba60Sopenharmony_ci
29306f6ba60Sopenharmony_ci    uint64_t u64 = std::numeric_limits<uint64_t>::min();
29406f6ba60Sopenharmony_ci    EXPECT_EQ(EncodeVarint(buf, u64), (uint32_t)sizeof(ENCODE_BYTES_MIN_U64));
29506f6ba60Sopenharmony_ci    EXPECT_TRUE(CompareBytes(buf, ENCODE_BYTES_MIN_U64, sizeof(ENCODE_BYTES_MIN_U64)));
29606f6ba60Sopenharmony_ci    u64 = std::numeric_limits<uint64_t>::max();
29706f6ba60Sopenharmony_ci    EXPECT_EQ(EncodeVarint(buf, u64), (uint32_t)sizeof(ENCODE_BYTES_MAX_U64));
29806f6ba60Sopenharmony_ci    EXPECT_TRUE(CompareBytes(buf, ENCODE_BYTES_MAX_U64, sizeof(ENCODE_BYTES_MAX_U64)));
29906f6ba60Sopenharmony_ci}
30006f6ba60Sopenharmony_ci
30106f6ba60Sopenharmony_ciHWTEST_F(VarintEncodeUnittest, EncodeVarintPadding, TestSize.Level1)
30206f6ba60Sopenharmony_ci{
30306f6ba60Sopenharmony_ci    const uint8_t paddingByte = 0x80;
30406f6ba60Sopenharmony_ci    uint8_t buf[VARINT_ENCODE_MAX_SIZE] = {0};
30506f6ba60Sopenharmony_ci    uint64_t u64 = 1;
30606f6ba60Sopenharmony_ci    EncodeVarintPadding(buf, u64, VARINT_ENCODE_MAX_SIZE);
30706f6ba60Sopenharmony_ci    uint32_t i = 1;
30806f6ba60Sopenharmony_ci    while (i < VARINT_ENCODE_MAX_SIZE - 1) {
30906f6ba60Sopenharmony_ci        EXPECT_EQ(buf[i++], paddingByte);
31006f6ba60Sopenharmony_ci    }
31106f6ba60Sopenharmony_ci    EXPECT_EQ(buf[i], (uint8_t)0);
31206f6ba60Sopenharmony_ci
31306f6ba60Sopenharmony_ci    u64 = std::numeric_limits<uint64_t>::max();
31406f6ba60Sopenharmony_ci    EncodeVarintPadding(buf, u64, VARINT_ENCODE_MAX_SIZE);
31506f6ba60Sopenharmony_ci    EXPECT_TRUE(CompareBytes(buf, ENCODE_BYTES_MAX_U64, sizeof(ENCODE_BYTES_MAX_U64)));
31606f6ba60Sopenharmony_ci}
31706f6ba60Sopenharmony_ci} // namespace Profiler
31806f6ba60Sopenharmony_ci} // namespace Developtools
31906f6ba60Sopenharmony_ci} // namespace OHOS
320