Skip to content

Commit 2a658ba

Browse files
committed
Backported the unit tests for the max_serialized_size function.
1 parent d697952 commit 2a658ba

2 files changed

Lines changed: 261 additions & 3 deletions

File tree

test/proto/oneof_fields.proto

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2020-2024 Embedded AMS B.V. - All Rights Reserved
2+
* Copyright (C) 2020-2025 Embedded AMS B.V. - All Rights Reserved
33
*
44
* This file is part of Embedded Proto.
55
*
@@ -16,7 +16,7 @@
1616
* along with Embedded Proto. If not, see <https://www.gnu.org/licenses/>.
1717
*
1818
* For commercial and closed source application please visit:
19-
* <https://EmbeddedProto.com/license/>.
19+
* <https://embeddedproto.com/pricing/>.
2020
*
2121
* Embedded AMS B.V.
2222
* Info:
@@ -92,4 +92,19 @@ message string_bytes_oneof
9292
string name = 1;
9393
bytes data = 2;
9494
}
95-
}
95+
}
96+
97+
message combined_oneof
98+
{
99+
oneof combi
100+
{
101+
message_oneof msg_oneof = 1;
102+
string_bytes_oneof msg_sb_oneof = 2;
103+
}
104+
}
105+
106+
message oneof_sigle {
107+
oneof single_data {
108+
int32 single_num = 1;
109+
}
110+
}

test/test_max_field_size.cpp

Lines changed: 243 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,243 @@
1+
/*
2+
* Copyright (C) 2020-2025 Embedded AMS B.V. - All Rights Reserved
3+
*
4+
* This file is part of Embedded Proto.
5+
*
6+
* Embedded Proto is open source software: you can redistribute it and/or
7+
* modify it under the terms of the GNU General Public License as published
8+
* by the Free Software Foundation, version 3 of the license.
9+
*
10+
* Embedded Proto is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with Embedded Proto. If not, see <https://www.gnu.org/licenses/>.
17+
*
18+
* For commercial and closed source application please visit:
19+
* <https://embeddedproto.com/pricing/>.
20+
*
21+
* Embedded AMS B.V.
22+
* Info:
23+
* info at EmbeddedProto dot com
24+
*
25+
* Postal address:
26+
* Atoomweg 2
27+
* 1627 LE, Hoorn
28+
* the Netherlands
29+
*/
30+
31+
#include "gtest/gtest.h"
32+
#include "gmock/gmock.h"
33+
34+
#include <WireFormatter.h>
35+
#include <Fields.h>
36+
#include <FieldStringBytes.h>
37+
#include <RepeatedFieldFixedSize.h>
38+
#include <WriteBufferFixedSize.h>
39+
40+
#include <simple_types.h>
41+
#include <oneof_fields.h>
42+
43+
namespace test_max_field_size
44+
{
45+
46+
TEST(MaxFieldSize, Wireformatter_VarintSize)
47+
{
48+
EXPECT_EQ(1, EmbeddedProto::WireFormatter::VarintSize(0));
49+
EXPECT_EQ(1, EmbeddedProto::WireFormatter::VarintSize(1));
50+
EXPECT_EQ(1, EmbeddedProto::WireFormatter::VarintSize(127));
51+
EXPECT_EQ(2, EmbeddedProto::WireFormatter::VarintSize(128));
52+
EXPECT_EQ(2, EmbeddedProto::WireFormatter::VarintSize(16383));
53+
EXPECT_EQ(3, EmbeddedProto::WireFormatter::VarintSize(16384));
54+
EXPECT_EQ(3, EmbeddedProto::WireFormatter::VarintSize(2097151));
55+
EXPECT_EQ(4, EmbeddedProto::WireFormatter::VarintSize(2097152));
56+
EXPECT_EQ(4, EmbeddedProto::WireFormatter::VarintSize(268435455));
57+
EXPECT_EQ(5, EmbeddedProto::WireFormatter::VarintSize(268435456));
58+
EXPECT_EQ(5, EmbeddedProto::WireFormatter::VarintSize(34359738367));
59+
EXPECT_EQ(6, EmbeddedProto::WireFormatter::VarintSize(34359738368));
60+
EXPECT_EQ(6, EmbeddedProto::WireFormatter::VarintSize(4398046511103));
61+
EXPECT_EQ(7, EmbeddedProto::WireFormatter::VarintSize(4398046511104));
62+
EXPECT_EQ(7, EmbeddedProto::WireFormatter::VarintSize(562949953421311));
63+
EXPECT_EQ(8, EmbeddedProto::WireFormatter::VarintSize(562949953421312));
64+
EXPECT_EQ(8, EmbeddedProto::WireFormatter::VarintSize(72057594037927935));
65+
EXPECT_EQ(9, EmbeddedProto::WireFormatter::VarintSize(72057594037927936));
66+
EXPECT_EQ(9, EmbeddedProto::WireFormatter::VarintSize(9223372036854775807));
67+
68+
// Max values
69+
EXPECT_EQ(10, EmbeddedProto::WireFormatter::VarintSize(9223372036854775808ULL));
70+
EXPECT_EQ(10, EmbeddedProto::WireFormatter::VarintSize(18446744073709551615ULL));
71+
}
72+
73+
TEST(MaxFieldSize, Field_max_serialized_size)
74+
{
75+
EXPECT_EQ(6, EmbeddedProto::int32::max_serialized_size(1));
76+
EXPECT_EQ(11, EmbeddedProto::int64::max_serialized_size(1));
77+
EXPECT_EQ(6, EmbeddedProto::uint32::max_serialized_size(1));
78+
EXPECT_EQ(11, EmbeddedProto::uint64::max_serialized_size(1));
79+
EXPECT_EQ(6, EmbeddedProto::sint32::max_serialized_size(1));
80+
EXPECT_EQ(11, EmbeddedProto::sint64::max_serialized_size(1));
81+
EXPECT_EQ(5, EmbeddedProto::fixed32::max_serialized_size(1));
82+
EXPECT_EQ(9, EmbeddedProto::fixed64::max_serialized_size(1));
83+
EXPECT_EQ(5, EmbeddedProto::sfixed32::max_serialized_size(1));
84+
EXPECT_EQ(9, EmbeddedProto::sfixed64::max_serialized_size(1));
85+
EXPECT_EQ(5, EmbeddedProto::floatfixed::max_serialized_size(1));
86+
EXPECT_EQ(9, EmbeddedProto::doublefixed::max_serialized_size(1));
87+
EXPECT_EQ(2, EmbeddedProto::boolean::max_serialized_size(1));
88+
89+
// Next with a large ID such that the tag becomes multiple bytes
90+
EXPECT_EQ(7, EmbeddedProto::int32::max_serialized_size(16));
91+
EXPECT_EQ(12, EmbeddedProto::int64::max_serialized_size(16));
92+
EXPECT_EQ(7, EmbeddedProto::uint32::max_serialized_size(16));
93+
EXPECT_EQ(12, EmbeddedProto::uint64::max_serialized_size(16));
94+
EXPECT_EQ(7, EmbeddedProto::sint32::max_serialized_size(16));
95+
EXPECT_EQ(12, EmbeddedProto::sint64::max_serialized_size(16));
96+
EXPECT_EQ(6, EmbeddedProto::fixed32::max_serialized_size(16));
97+
EXPECT_EQ(10, EmbeddedProto::fixed64::max_serialized_size(16));
98+
EXPECT_EQ(6, EmbeddedProto::sfixed32::max_serialized_size(16));
99+
EXPECT_EQ(10, EmbeddedProto::sfixed64::max_serialized_size(16));
100+
EXPECT_EQ(6, EmbeddedProto::floatfixed::max_serialized_size(16));
101+
EXPECT_EQ(10, EmbeddedProto::doublefixed::max_serialized_size(16));
102+
EXPECT_EQ(3, EmbeddedProto::boolean::max_serialized_size(16));
103+
104+
}
105+
106+
template<class FIELD_TYPE, class BASE_TYPE>
107+
void helper_test(const BASE_TYPE max)
108+
{
109+
EmbeddedProto::WriteBufferFixedSize<10> buffer;
110+
//constexpr BASE_TYPE max = std::numeric_limits<BASE_TYPE>::digits == 64 ? static_cast<BASE_TYPE>(18446744073709551615) : static_cast<BASE_TYPE>(4294967295);
111+
FIELD_TYPE field = max; // std::numeric_limits<BASE_TYPE>::max();
112+
field.serialize(buffer);
113+
EXPECT_EQ(buffer.get_size(), FIELD_TYPE::max_serialized_size());
114+
}
115+
116+
void helper_set_most_consuming_value(Test_Simple_Types& msg)
117+
{
118+
msg.set_a_int32(std::numeric_limits<int32_t>::max());
119+
msg.set_a_int64(-9223372036854775807);
120+
msg.set_a_uint32(std::numeric_limits<uint32_t>::max());
121+
msg.set_a_uint64(std::numeric_limits<uint64_t>::max());
122+
msg.set_a_sint32(std::numeric_limits<int32_t>::max());
123+
msg.set_a_sint64(std::numeric_limits<int64_t>::max());
124+
msg.set_a_bool(true);
125+
msg.set_a_enum(Test_Enum::TWOBILLION);
126+
msg.set_a_fixed64(std::numeric_limits<uint64_t>::max());
127+
msg.set_a_sfixed64(std::numeric_limits<int64_t>::max());
128+
msg.set_a_double(std::numeric_limits<double>::max());
129+
msg.set_a_fixed32(std::numeric_limits<uint32_t>::max());
130+
msg.set_a_sfixed32(std::numeric_limits<int32_t>::max());
131+
msg.set_a_float(std::numeric_limits<float>::max());
132+
133+
msg.set_a_nested_enum(Test_Simple_Types::Nested_Enum::NE_C);
134+
}
135+
136+
TEST(MaxFieldSize, SimpleTypesMsg_max_serialized_size)
137+
{
138+
helper_test<EmbeddedProto::int32, int32_t>(2147483647);
139+
helper_test<EmbeddedProto::int64, int64_t>(-9223372036854775807);
140+
helper_test<EmbeddedProto::uint32, uint32_t>(4294967295);
141+
helper_test<EmbeddedProto::uint64, uint64_t>(18446744073709551615ull);
142+
helper_test<EmbeddedProto::sint32, int32_t>(2147483647);
143+
helper_test<EmbeddedProto::sint64, int64_t>(9223372036854775807);
144+
helper_test<EmbeddedProto::fixed32, uint32_t>(4294967295);
145+
helper_test<EmbeddedProto::fixed64, uint64_t>(18446744073709551615ull);
146+
helper_test<EmbeddedProto::sfixed32, int32_t>(2147483647);
147+
helper_test<EmbeddedProto::sfixed64, int64_t>(9223372036854775807);
148+
helper_test<EmbeddedProto::floatfixed, float>(std::numeric_limits<float>::max());
149+
helper_test<EmbeddedProto::doublefixed, double>(std::numeric_limits<double>::max());
150+
helper_test<EmbeddedProto::boolean, bool>(true);
151+
152+
EmbeddedProto::WriteBufferFixedSize<150> buffer;
153+
Test_Simple_Types msg;
154+
helper_set_most_consuming_value(msg);
155+
156+
msg.serialize(buffer);
157+
EXPECT_EQ((Test_Simple_Types::max_serialized_size()), buffer.get_size());
158+
159+
}
160+
161+
TEST(MaxFieldSize, FieldStringBytes_max_serialized_size)
162+
{
163+
// N byte + varint for the number of bytes + tag size.
164+
EXPECT_EQ(1+1+1, EmbeddedProto::FieldBytes<1>::max_serialized_size(1));
165+
EXPECT_EQ(15+1+1, EmbeddedProto::FieldBytes<15>::max_serialized_size(1));
166+
EXPECT_EQ(127+1+1, EmbeddedProto::FieldBytes<127>::max_serialized_size(1));
167+
EXPECT_EQ(128+2+1, EmbeddedProto::FieldBytes<128>::max_serialized_size(1));
168+
}
169+
170+
TEST(MaxFieldSize, RepeatedFieldFixedSize_Packed)
171+
{
172+
constexpr uint32_t max_ser_size_A = EmbeddedProto::RepeatedFieldFixedSize<EmbeddedProto::uint32, 3>::max_serialized_size(1);
173+
constexpr uint32_t max_ser_size_B = EmbeddedProto::RepeatedFieldFixedSize<EmbeddedProto::uint32, 3>::max_serialized_size(16);
174+
constexpr uint32_t max_ser_size_C = EmbeddedProto::RepeatedFieldFixedSize<EmbeddedProto::uint32, 128>::max_serialized_size(1);
175+
// id + size + data
176+
EXPECT_EQ(1 + 1 + (3*5), max_ser_size_A);
177+
EXPECT_EQ(2 + 1 + (3*5), max_ser_size_B);
178+
EXPECT_EQ(1 + 2 + (128*5), max_ser_size_C);
179+
}
180+
181+
TEST(MaxFieldSize, RepeatedFieldFixedSize_Unpacked)
182+
{
183+
EmbeddedProto::RepeatedFieldFixedSize<Test_Simple_Types, 3> rffs;
184+
helper_set_most_consuming_value(rffs[0]);
185+
helper_set_most_consuming_value(rffs[1]);
186+
helper_set_most_consuming_value(rffs[2]);
187+
188+
EmbeddedProto::WriteBufferFixedSize<500> buffer;
189+
190+
constexpr uint32_t max_ser_size_A = EmbeddedProto::RepeatedFieldFixedSize<Test_Simple_Types, 3>::max_serialized_size(1);
191+
rffs.serialize_with_id(1, buffer, false);
192+
EXPECT_EQ(buffer.get_size(), max_ser_size_A);
193+
194+
buffer.clear();
195+
constexpr uint32_t max_ser_size_B = EmbeddedProto::RepeatedFieldFixedSize<Test_Simple_Types, 3>::max_serialized_size(16);
196+
rffs.serialize_with_id(16, buffer, false);
197+
EXPECT_EQ(buffer.get_size(), max_ser_size_B);
198+
}
199+
200+
TEST(MaxFieldSize, OneofMaxFieldSize)
201+
{
202+
message_oneof msg;
203+
msg.set_a(std::numeric_limits<int32_t>::max());
204+
msg.set_x(std::numeric_limits<int32_t>::max());
205+
msg.set_b(std::numeric_limits<int32_t>::max());
206+
msg.set_w(std::numeric_limits<float>::max());
207+
msg.mutable_msg_ABC().set_varA(std::numeric_limits<int32_t>::max());
208+
msg.mutable_msg_ABC().set_varB(std::numeric_limits<int32_t>::max());
209+
msg.mutable_msg_ABC().set_varC(std::numeric_limits<int32_t>::max());
210+
211+
EmbeddedProto::WriteBufferFixedSize<200> buffer;
212+
213+
msg.serialize(buffer);
214+
EXPECT_EQ(buffer.get_size(), message_oneof::max_serialized_size());
215+
216+
buffer.clear();
217+
constexpr uint32_t N_CHARS = 25+1;
218+
string_bytes_oneof<N_CHARS, 1> msg_chars;
219+
msg_chars.mutable_name().set("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
220+
msg_chars.serialize(buffer);
221+
EXPECT_EQ(buffer.get_size(), (string_bytes_oneof<N_CHARS,1>::max_serialized_size()));
222+
223+
buffer.clear();
224+
constexpr uint32_t N_BYTES = 75;
225+
string_bytes_oneof<1, N_BYTES> msg_bytes;
226+
for(uint32_t i = 0; i < N_BYTES; ++i) { msg_bytes.mutable_data()[i] = 0xFF; }
227+
msg_bytes.serialize(buffer);
228+
EXPECT_EQ(buffer.get_size(), (string_bytes_oneof<1,N_BYTES>::max_serialized_size()));
229+
230+
buffer.clear();
231+
combined_oneof<1,1> msg_combiA;
232+
msg_combiA.mutable_msg_oneof() = msg;
233+
msg_combiA.serialize(buffer);
234+
EXPECT_EQ(buffer.get_size(), (combined_oneof<1,1>::max_serialized_size()));
235+
236+
buffer.clear();
237+
combined_oneof<1, N_BYTES> msg_combiB;
238+
msg_combiB.mutable_msg_sb_oneof() = msg_bytes;
239+
msg_combiB.serialize(buffer);
240+
EXPECT_EQ(buffer.get_size(), (combined_oneof<1,N_BYTES>::max_serialized_size()));
241+
}
242+
243+
} // End of namespace test_max_field_size

0 commit comments

Comments
 (0)