blob: 5ca3e6477c0150bad1df63edfedeb59bf75e6c72 [file] [log] [blame]
Yi Jinc3d4b282018-04-23 16:02:20 -07001// Copyright (C) 2018 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14#include <android/util/protobuf.h>
15#include <gmock/gmock.h>
16#include <gtest/gtest.h>
17
18using namespace android::util;
19
20TEST(ProtobufTest, All) {
21 EXPECT_EQ(read_wire_type(UINT32_C(17)), 1);
22 EXPECT_EQ(read_field_id(UINT32_C(17)), 2);
23 EXPECT_EQ(get_varint_size(UINT64_C(234134)), 3);
24 EXPECT_EQ(get_varint_size(UINT64_C(-1)), 10);
25
26 constexpr uint8_t UNSET_BYTE = 0xAB;
27
28 uint8_t buf[11];
29 memset(buf, UNSET_BYTE, sizeof(buf));
30 EXPECT_EQ(write_raw_varint(buf, UINT64_C(150)) - buf, 2);
31 EXPECT_EQ(buf[0], 0x96);
32 EXPECT_EQ(buf[1], 0x01);
33 EXPECT_EQ(buf[2], UNSET_BYTE);
34
35 memset(buf, UNSET_BYTE, sizeof(buf));
36 EXPECT_EQ(write_raw_varint(buf, UINT64_C(-2)) - buf, 10);
37 EXPECT_EQ(buf[0], 0xfe);
38 for (int i = 1; i < 9; i++) {
39 EXPECT_EQ(buf[i], 0xff);
40 }
41 EXPECT_EQ(buf[9], 0x01);
42 EXPECT_EQ(buf[10], UNSET_BYTE);
43
44 uint8_t header[20];
45 memset(header, UNSET_BYTE, sizeof(header));
46 EXPECT_EQ(write_length_delimited_tag_header(header, 3, 150) - header, 3);
47 EXPECT_EQ(header[0], 26);
48 EXPECT_EQ(header[1], 0x96);
49 EXPECT_EQ(header[2], 0x01);
50 EXPECT_EQ(header[3], UNSET_BYTE);
51}