mini_table_test.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2009-2021, Google LLC
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of Google LLC nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL Google LLC BE LIABLE FOR ANY DIRECT,
20  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include "upb/mini_table.hpp"
29 
30 #include "absl/container/flat_hash_set.h"
31 #include "gmock/gmock.h"
32 #include "google/protobuf/descriptor.h"
33 #include "gtest/gtest.h"
34 #include "upb/msg_internal.h"
35 #include "upb/upb.hpp"
36 
37 namespace protobuf = ::google::protobuf;
38 
39 class MiniTableTest : public testing::TestWithParam<upb_MiniTablePlatform> {};
40 
45  upb_MiniTable_Build(NULL, 0, GetParam(), arena.ptr(), status.ptr());
46  ASSERT_NE(nullptr, table);
47  EXPECT_EQ(0, table->field_count);
48  EXPECT_EQ(0, table->required_count);
49 }
50 
51 TEST_P(MiniTableTest, AllScalarTypes) {
54  ASSERT_TRUE(e.StartMessage(0));
55  int count = 0;
56  for (int i = kUpb_FieldType_Double; i < kUpb_FieldType_SInt64; i++) {
57  ASSERT_TRUE(e.PutField(static_cast<upb_FieldType>(i), i, 0));
58  count++;
59  }
62  e.data().data(), e.data().size(), GetParam(), arena.ptr(), status.ptr());
63  ASSERT_NE(nullptr, table);
64  EXPECT_EQ(count, table->field_count);
66  for (int i = 0; i < 16; i++) {
67  const upb_MiniTable_Field* f = &table->fields[i];
68  EXPECT_EQ(i + 1, f->number);
70  EXPECT_TRUE(offsets.insert(f->offset).second);
71  EXPECT_TRUE(f->offset < table->size);
72  }
73  EXPECT_EQ(0, table->required_count);
74 }
75 
76 TEST_P(MiniTableTest, AllRepeatedTypes) {
79  ASSERT_TRUE(e.StartMessage(0));
80  int count = 0;
81  for (int i = kUpb_FieldType_Double; i < kUpb_FieldType_SInt64; i++) {
82  ASSERT_TRUE(e.PutField(static_cast<upb_FieldType>(i), i,
84  count++;
85  }
88  e.data().data(), e.data().size(), GetParam(), arena.ptr(), status.ptr());
89  ASSERT_NE(nullptr, table);
90  EXPECT_EQ(count, table->field_count);
92  for (int i = 0; i < 16; i++) {
93  const upb_MiniTable_Field* f = &table->fields[i];
94  EXPECT_EQ(i + 1, f->number);
96  EXPECT_TRUE(offsets.insert(f->offset).second);
97  EXPECT_TRUE(f->offset < table->size);
98  }
99  EXPECT_EQ(0, table->required_count);
100 }
101 
105  ASSERT_TRUE(e.StartMessage(0));
106  int count = 0;
107  std::vector<int> field_numbers;
108  for (int i = 0; i < 25; i++) {
109  int field_number = 1 << i;
110  field_numbers.push_back(field_number);
111  ASSERT_TRUE(e.PutField(kUpb_FieldType_Float, field_number, 0));
112  count++;
113  }
116  e.data().data(), e.data().size(), GetParam(), arena.ptr(), status.ptr());
117  ASSERT_NE(nullptr, table);
118  EXPECT_EQ(count, table->field_count);
120  for (size_t i = 0; i < field_numbers.size(); i++) {
121  const upb_MiniTable_Field* f = &table->fields[i];
122  EXPECT_EQ(field_numbers[i], f->number);
123  EXPECT_EQ(kUpb_FieldType_Float, f->descriptortype);
125  EXPECT_TRUE(offsets.insert(f->offset).second);
126  EXPECT_TRUE(f->offset < table->size);
127  }
128  EXPECT_EQ(0, table->required_count);
129 }
130 
131 TEST_P(MiniTableTest, AllScalarTypesOneof) {
134  ASSERT_TRUE(e.StartMessage(0));
135  int count = 0;
136  for (int i = kUpb_FieldType_Double; i < kUpb_FieldType_SInt64; i++) {
137  ASSERT_TRUE(e.PutField(static_cast<upb_FieldType>(i), i, 0));
138  count++;
139  }
140  ASSERT_TRUE(e.StartOneof());
141  for (int i = kUpb_FieldType_Double; i < kUpb_FieldType_SInt64; i++) {
142  ASSERT_TRUE(e.PutOneofField(i));
143  }
146  e.data().data(), e.data().size(), GetParam(), arena.ptr(), status.ptr());
147  ASSERT_NE(nullptr, table) << status.error_message();
148  EXPECT_EQ(count, table->field_count);
150  for (int i = 0; i < 16; i++) {
151  const upb_MiniTable_Field* f = &table->fields[i];
152  EXPECT_EQ(i + 1, f->number);
154  // For a oneof all fields have the same offset.
155  EXPECT_EQ(table->fields[0].offset, f->offset);
156  // All presence fields should point to the same oneof case offset.
157  size_t case_ofs = _upb_oneofcase_ofs(f);
158  EXPECT_EQ(table->fields[0].presence, f->presence);
159  EXPECT_TRUE(f->offset < table->size);
160  EXPECT_TRUE(case_ofs < table->size);
161  EXPECT_TRUE(case_ofs != f->offset);
162  }
163  EXPECT_EQ(0, table->required_count);
164 }
165 
169 
170 TEST(MiniTablePlatformIndependentTest, Base92Roundtrip) {
171  for (char i = 0; i < 92; i++) {
173  }
174 }
175 
176 TEST(MiniTablePlatformIndependentTest, IsTypePackable) {
177  for (int i = 1; i <= protobuf::FieldDescriptor::MAX_TYPE; i++) {
180  static_cast<protobuf::FieldDescriptor::Type>(i)));
181  }
182 }
google::protobuf::FieldDescriptor::Type
Type
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.h:521
upb::MtDataEncoder
Definition: mini_table.hpp:39
kUpb_FieldType_SInt64
@ kUpb_FieldType_SInt64
Definition: upb/upb/upb.h:326
ASSERT_NE
#define ASSERT_NE(val1, val2)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2060
upb_FieldType
upb_FieldType
Definition: upb/upb/upb.h:308
kUpb_FieldMode_Mask
#define kUpb_FieldMode_Mask
Definition: msg_internal.h:90
upb_MiniTable_Field
Definition: msg_internal.h:71
MiniTableTest
Definition: mini_table_test.cc:39
kUpb_MiniTablePlatform_64Bit
@ kUpb_MiniTablePlatform_64Bit
Definition: mini_table.h:115
absl::container_internal::raw_hash_set< absl::container_internal::FlatHashSetPolicy< T >, absl::container_internal::hash_default_hash< T >, absl::container_internal::hash_default_eq< T >, std::allocator< T > >::insert
std::pair< iterator, bool > insert(T &&value)
Definition: abseil-cpp/absl/container/internal/raw_hash_set.h:1382
google::protobuf
Definition: bloaty/third_party/protobuf/benchmarks/util/data_proto2_to_proto3_util.h:12
status
absl::Status status
Definition: rls.cc:251
upb_MiniTable
Definition: msg_internal.h:185
kUpb_FieldModifier_IsRepeated
@ kUpb_FieldModifier_IsRepeated
Definition: mini_table.h:50
arena
grpc_core::ScopedArenaPtr arena
Definition: binder_transport_test.cc:237
mini_table.hpp
testing::TestWithParam
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1883
EXPECT_EQ
#define EXPECT_EQ(a, b)
Definition: iomgr/time_averaged_stats_test.cc:27
kUpb_FieldType_Double
@ kUpb_FieldType_Double
Definition: upb/upb/upb.h:309
upb_IsTypePackable
bool upb_IsTypePackable(upb_FieldType type)
Definition: mini_table.c:113
kUpb_FieldType_Float
@ kUpb_FieldType_Float
Definition: upb/upb/upb.h:310
autogen_x86imm.f
f
Definition: autogen_x86imm.py:9
TEST_P
TEST_P(MiniTableTest, Empty)
Definition: mini_table_test.cc:41
INSTANTIATE_TEST_SUITE_P
INSTANTIATE_TEST_SUITE_P(Platforms, MiniTableTest, testing::Values(kUpb_MiniTablePlatform_32Bit, kUpb_MiniTablePlatform_64Bit))
google::protobuf::FieldDescriptor::IsTypePackable
static bool IsTypePackable(Type field_type)
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.h:2159
absl::flat_hash_set
Definition: abseil-cpp/absl/container/flat_hash_set.h:105
upb_FromBase92
char upb_FromBase92(uint8_t ch)
Definition: mini_table.c:99
_upb_oneofcase_ofs
UPB_INLINE size_t _upb_oneofcase_ofs(const upb_msglayout_field *f)
Definition: php-upb.h:1311
google::protobuf.internal.wire_format.IsTypePackable
def IsTypePackable(field_type)
Definition: bloaty/third_party/protobuf/python/google/protobuf/internal/wire_format.py:259
Empty
Definition: abseil-cpp/absl/container/internal/compressed_tuple_test.cc:33
kUpb_MiniTablePlatform_32Bit
@ kUpb_MiniTablePlatform_32Bit
Definition: mini_table.h:114
upb::Arena
Definition: upb.hpp:68
kUpb_FieldMode_Scalar
@ kUpb_FieldMode_Scalar
Definition: msg_internal.h:86
TEST
TEST(MiniTablePlatformIndependentTest, Base92Roundtrip)
Definition: mini_table_test.cc:170
testing::Values
internal::ValueArray< T... > Values(T... v)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest-param-test.h:335
count
int * count
Definition: bloaty/third_party/googletest/googlemock/test/gmock_stress_test.cc:96
upb.hpp
upb_ToBase92
char upb_ToBase92(int8_t ch)
Definition: mini_table.c:84
ASSERT_TRUE
#define ASSERT_TRUE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1973
upb_MiniTable_Build
upb_MiniTable * upb_MiniTable_Build(const char *data, size_t len, upb_MiniTablePlatform platform, upb_Arena *arena, upb_Status *status)
Definition: mini_table.c:1038
EXPECT_TRUE
#define EXPECT_TRUE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1967
table
uint8_t table[256]
Definition: hpack_parser.cc:456
size
voidpf void uLong size
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
upb::Status
Definition: upb.hpp:35
kUpb_FieldMode_Array
@ kUpb_FieldMode_Array
Definition: msg_internal.h:85
msg_internal.h
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
google::protobuf::FieldDescriptor::MAX_TYPE
@ MAX_TYPE
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.h:546


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:00:40