Protocol Buffers - Google's data interchange format (grpc依赖)
https://developers.google.com/protocol-buffers/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
173 lines
6.7 KiB
173 lines
6.7 KiB
3 years ago
|
/*
|
||
|
* Copyright (c) 2009-2021, Google LLC
|
||
|
* All rights reserved.
|
||
|
*
|
||
|
* Redistribution and use in source and binary forms, with or without
|
||
|
* modification, are permitted provided that the following conditions are met:
|
||
|
* * Redistributions of source code must retain the above copyright
|
||
|
* notice, this list of conditions and the following disclaimer.
|
||
|
* * Redistributions in binary form must reproduce the above copyright
|
||
|
* notice, this list of conditions and the following disclaimer in the
|
||
|
* documentation and/or other materials provided with the distribution.
|
||
|
* * Neither the name of Google LLC nor the
|
||
|
* names of its contributors may be used to endorse or promote products
|
||
|
* derived from this software without specific prior written permission.
|
||
|
*
|
||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||
|
* ARE DISCLAIMED. IN NO EVENT SHALL Google LLC BE LIABLE FOR ANY DIRECT,
|
||
|
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||
|
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||
|
*/
|
||
|
|
||
|
#include "upb/mini_table.hpp"
|
||
|
|
||
|
#include "absl/container/flat_hash_set.h"
|
||
|
#include "gmock/gmock.h"
|
||
|
#include "gtest/gtest.h"
|
||
|
#include "upb/msg_internal.h"
|
||
|
#include "upb/upb.hpp"
|
||
|
|
||
|
class MiniTableTest : public testing::TestWithParam<upb_MiniTablePlatform> {};
|
||
|
|
||
|
TEST_P(MiniTableTest, Empty) {
|
||
|
upb::Arena arena;
|
||
|
upb::Status status;
|
||
|
upb_MiniTable* table =
|
||
|
upb_MiniTable_Build(NULL, 0, GetParam(), arena.ptr(), status.ptr());
|
||
|
ASSERT_NE(nullptr, table);
|
||
|
EXPECT_EQ(0, table->field_count);
|
||
|
EXPECT_EQ(0, table->required_count);
|
||
|
}
|
||
|
|
||
|
TEST_P(MiniTableTest, AllScalarTypes) {
|
||
|
upb::Arena arena;
|
||
|
upb::MtDataEncoder e;
|
||
|
ASSERT_TRUE(e.StartMessage(0));
|
||
|
int count = 0;
|
||
|
for (int i = kUpb_FieldType_Double ; i < kUpb_FieldType_SInt64; i++) {
|
||
|
ASSERT_TRUE(e.PutField(static_cast<upb_FieldType>(i), i, 0));
|
||
|
count++;
|
||
|
}
|
||
|
fprintf(stderr, "YO: %s\n", e.data().c_str());
|
||
|
upb::Status status;
|
||
|
upb_MiniTable* table = upb_MiniTable_Build(
|
||
|
e.data().data(), e.data().size(), GetParam(), arena.ptr(), status.ptr());
|
||
|
ASSERT_NE(nullptr, table);
|
||
|
EXPECT_EQ(count, table->field_count);
|
||
|
absl::flat_hash_set<size_t> offsets;
|
||
|
for (int i = 0; i < 16; i++) {
|
||
|
const upb_MiniTable_Field* f = &table->fields[i];
|
||
|
EXPECT_EQ(i + 1, f->number);
|
||
|
EXPECT_EQ(i + kUpb_FieldType_Double, f->descriptortype);
|
||
|
EXPECT_EQ(kUpb_FieldMode_Scalar, f->mode & kUpb_FieldMode_Mask);
|
||
|
EXPECT_TRUE(offsets.insert(f->offset).second);
|
||
|
EXPECT_TRUE(f->offset < table->size);
|
||
|
}
|
||
|
EXPECT_EQ(0, table->required_count);
|
||
|
}
|
||
|
|
||
|
TEST_P(MiniTableTest, AllRepeatedTypes) {
|
||
|
upb::Arena arena;
|
||
|
upb::MtDataEncoder e;
|
||
|
ASSERT_TRUE(e.StartMessage(0));
|
||
|
int count = 0;
|
||
|
for (int i = kUpb_FieldType_Double ; i < kUpb_FieldType_SInt64; i++) {
|
||
|
ASSERT_TRUE(e.PutField(static_cast<upb_FieldType>(i), i,
|
||
|
kUpb_FieldModifier_IsRepeated));
|
||
|
count++;
|
||
|
}
|
||
|
fprintf(stderr, "YO: %s\n", e.data().c_str());
|
||
|
upb::Status status;
|
||
|
upb_MiniTable* table = upb_MiniTable_Build(
|
||
|
e.data().data(), e.data().size(), GetParam(), arena.ptr(), status.ptr());
|
||
|
ASSERT_NE(nullptr, table);
|
||
|
EXPECT_EQ(count, table->field_count);
|
||
|
absl::flat_hash_set<size_t> offsets;
|
||
|
for (int i = 0; i < 16; i++) {
|
||
|
const upb_MiniTable_Field* f = &table->fields[i];
|
||
|
EXPECT_EQ(i + 1, f->number);
|
||
|
EXPECT_EQ(i + kUpb_FieldType_Double, f->descriptortype);
|
||
|
EXPECT_EQ(kUpb_FieldMode_Array, f->mode & kUpb_FieldMode_Mask);
|
||
|
EXPECT_TRUE(offsets.insert(f->offset).second);
|
||
|
EXPECT_TRUE(f->offset < table->size);
|
||
|
}
|
||
|
EXPECT_EQ(0, table->required_count);
|
||
|
}
|
||
|
|
||
|
TEST_P(MiniTableTest, Skips) {
|
||
|
upb::Arena arena;
|
||
|
upb::MtDataEncoder e;
|
||
|
ASSERT_TRUE(e.StartMessage(0));
|
||
|
int count = 0;
|
||
|
std::vector<int> field_numbers;
|
||
|
for (int i = 0; i < 25; i++) {
|
||
|
int field_number = 1 << i;
|
||
|
field_numbers.push_back(field_number);
|
||
|
ASSERT_TRUE(e.PutField(kUpb_FieldType_Float, field_number, 0));
|
||
|
count++;
|
||
|
}
|
||
|
fprintf(stderr, "YO: %s, %zu\n", e.data().c_str(), e.data().size());
|
||
|
upb::Status status;
|
||
|
upb_MiniTable* table = upb_MiniTable_Build(
|
||
|
e.data().data(), e.data().size(), GetParam(), arena.ptr(), status.ptr());
|
||
|
ASSERT_NE(nullptr, table);
|
||
|
EXPECT_EQ(count, table->field_count);
|
||
|
absl::flat_hash_set<size_t> offsets;
|
||
|
for (size_t i = 0; i < field_numbers.size(); i++) {
|
||
|
const upb_MiniTable_Field* f = &table->fields[i];
|
||
|
EXPECT_EQ(field_numbers[i], f->number);
|
||
|
EXPECT_EQ(kUpb_FieldType_Float, f->descriptortype);
|
||
|
EXPECT_EQ(kUpb_FieldMode_Scalar, f->mode & kUpb_FieldMode_Mask);
|
||
|
EXPECT_TRUE(offsets.insert(f->offset).second);
|
||
|
EXPECT_TRUE(f->offset < table->size);
|
||
|
}
|
||
|
EXPECT_EQ(0, table->required_count);
|
||
|
}
|
||
|
|
||
|
TEST_P(MiniTableTest, AllScalarTypesOneof) {
|
||
|
upb::Arena arena;
|
||
|
upb::MtDataEncoder e;
|
||
|
ASSERT_TRUE(e.StartMessage(0));
|
||
|
int count = 0;
|
||
|
for (int i = kUpb_FieldType_Double ; i < kUpb_FieldType_SInt64; i++) {
|
||
|
ASSERT_TRUE(e.PutField(static_cast<upb_FieldType>(i), i, 0));
|
||
|
count++;
|
||
|
}
|
||
|
ASSERT_TRUE(e.StartOneof());
|
||
|
for (int i = kUpb_FieldType_Double ; i < kUpb_FieldType_SInt64; i++) {
|
||
|
ASSERT_TRUE(e.PutOneofField(i));
|
||
|
}
|
||
|
fprintf(stderr, "YO: %s\n", e.data().c_str());
|
||
|
upb::Status status;
|
||
|
upb_MiniTable* table = upb_MiniTable_Build(
|
||
|
e.data().data(), e.data().size(), GetParam(), arena.ptr(), status.ptr());
|
||
|
ASSERT_NE(nullptr, table) << status.error_message();
|
||
|
EXPECT_EQ(count, table->field_count);
|
||
|
absl::flat_hash_set<size_t> offsets;
|
||
|
for (int i = 0; i < 16; i++) {
|
||
|
const upb_MiniTable_Field* f = &table->fields[i];
|
||
|
EXPECT_EQ(i + 1, f->number);
|
||
|
EXPECT_EQ(i + kUpb_FieldType_Double, f->descriptortype);
|
||
|
EXPECT_EQ(kUpb_FieldMode_Scalar, f->mode & kUpb_FieldMode_Mask);
|
||
|
// For a oneof all fields have the same offset.
|
||
|
EXPECT_EQ(table->fields[0].offset, f->offset);
|
||
|
// All presence fields should point to the same oneof case offset.
|
||
|
size_t case_ofs = _upb_oneofcase_ofs(f);
|
||
|
EXPECT_EQ(table->fields[0].presence, f->presence);
|
||
|
EXPECT_TRUE(f->offset < table->size);
|
||
|
EXPECT_TRUE(case_ofs < table->size);
|
||
|
EXPECT_TRUE(case_ofs != f->offset);
|
||
|
}
|
||
|
EXPECT_EQ(0, table->required_count);
|
||
|
}
|
||
|
|
||
|
INSTANTIATE_TEST_SUITE_P(Platforms, MiniTableTest,
|
||
|
testing::Values(kUpb_MiniTablePlatform_32Bit,
|
||
|
kUpb_MiniTablePlatform_64Bit));
|