166
174
TestUtil::ExpectAllExtensionsSet(message);
169
TEST(ExtensionSetTest, Serialization) {
170
// Serialize as TestAllExtensions and parse as TestAllTypes to insure wire
171
// compatibility of extensions.
172
unittest::TestAllExtensions source;
173
unittest::TestAllTypes destination;
176
TestUtil::SetAllExtensions(&source);
177
source.SerializeToString(&data);
178
EXPECT_TRUE(destination.ParseFromString(data));
179
TestUtil::ExpectAllFieldsSet(destination);
177
TEST(ExtensionSetTest, SerializationToArray) {
178
// Serialize as TestAllExtensions and parse as TestAllTypes to insure wire
179
// compatibility of extensions.
181
// This checks serialization to a flat array by explicitly reserving space in
182
// the string and calling the generated message's
183
// SerializeWithCachedSizesToArray.
184
unittest::TestAllExtensions source;
185
unittest::TestAllTypes destination;
186
TestUtil::SetAllExtensions(&source);
187
int size = source.ByteSize();
190
uint8* target = reinterpret_cast<uint8*>(string_as_array(&data));
191
uint8* end = source.SerializeWithCachedSizesToArray(target);
192
EXPECT_EQ(size, end - target);
193
EXPECT_TRUE(destination.ParseFromString(data));
194
TestUtil::ExpectAllFieldsSet(destination);
197
TEST(ExtensionSetTest, SerializationToStream) {
198
// Serialize as TestAllExtensions and parse as TestAllTypes to insure wire
199
// compatibility of extensions.
201
// This checks serialization to an output stream by creating an array output
202
// stream that can only buffer 1 byte at a time - this prevents the message
203
// from ever jumping to the fast path, ensuring that serialization happens via
204
// the CodedOutputStream.
205
unittest::TestAllExtensions source;
206
unittest::TestAllTypes destination;
207
TestUtil::SetAllExtensions(&source);
208
int size = source.ByteSize();
212
io::ArrayOutputStream array_stream(string_as_array(&data), size, 1);
213
io::CodedOutputStream output_stream(&array_stream);
214
source.SerializeWithCachedSizes(&output_stream);
215
ASSERT_FALSE(output_stream.HadError());
217
EXPECT_TRUE(destination.ParseFromString(data));
218
TestUtil::ExpectAllFieldsSet(destination);
221
TEST(ExtensionSetTest, PackedSerializationToArray) {
222
// Serialize as TestPackedExtensions and parse as TestPackedTypes to insure
223
// wire compatibility of extensions.
225
// This checks serialization to a flat array by explicitly reserving space in
226
// the string and calling the generated message's
227
// SerializeWithCachedSizesToArray.
228
unittest::TestPackedExtensions source;
229
unittest::TestPackedTypes destination;
230
TestUtil::SetPackedExtensions(&source);
231
int size = source.ByteSize();
234
uint8* target = reinterpret_cast<uint8*>(string_as_array(&data));
235
uint8* end = source.SerializeWithCachedSizesToArray(target);
236
EXPECT_EQ(size, end - target);
237
EXPECT_TRUE(destination.ParseFromString(data));
238
TestUtil::ExpectPackedFieldsSet(destination);
241
TEST(ExtensionSetTest, PackedSerializationToStream) {
242
// Serialize as TestPackedExtensions and parse as TestPackedTypes to insure
243
// wire compatibility of extensions.
245
// This checks serialization to an output stream by creating an array output
246
// stream that can only buffer 1 byte at a time - this prevents the message
247
// from ever jumping to the fast path, ensuring that serialization happens via
248
// the CodedOutputStream.
249
unittest::TestPackedExtensions source;
250
unittest::TestPackedTypes destination;
251
TestUtil::SetPackedExtensions(&source);
252
int size = source.ByteSize();
256
io::ArrayOutputStream array_stream(string_as_array(&data), size, 1);
257
io::CodedOutputStream output_stream(&array_stream);
258
source.SerializeWithCachedSizes(&output_stream);
259
ASSERT_FALSE(output_stream.HadError());
261
EXPECT_TRUE(destination.ParseFromString(data));
262
TestUtil::ExpectPackedFieldsSet(destination);
182
265
TEST(ExtensionSetTest, Parsing) {