• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc.  All rights reserved.
3 // https://developers.google.com/protocol-buffers/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 //     * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 //     * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 //     * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 // Author: kenton@google.com (Kenton Varda)
32 //  Based on original Protocol Buffers design by
33 //  Sanjay Ghemawat, Jeff Dean, and others.
34 
35 #ifndef GOOGLE_PROTOBUF_TEST_UTIL_H__
36 #define GOOGLE_PROTOBUF_TEST_UTIL_H__
37 
38 #include <google/protobuf/unittest.pb.h>
39 
40 #define UNITTEST ::protobuf_unittest
41 #define UNITTEST_IMPORT ::protobuf_unittest_import
42 // Must be included when the preprocessor symbols above are defined.
43 #include <google/protobuf/test_util.inc>
44 #undef UNITTEST
45 #undef UNITTEST_IMPORT
46 
47 // Must be included last.
48 #include <google/protobuf/port_def.inc>
49 
50 namespace google {
51 namespace protobuf {
52 // This file doesn't use these declarations, but some .cc files do.
53 namespace unittest = ::protobuf_unittest;
54 namespace unittest_import = ::protobuf_unittest_import;
55 
56 namespace TestUtil {
57 
58 class ReflectionTester {
59  public:
60   // base_descriptor must be a descriptor for TestAllTypes or
61   // TestAllExtensions.  In the former case, ReflectionTester fetches from
62   // it the FieldDescriptors needed to use the reflection interface.  In
63   // the latter case, ReflectionTester searches for extension fields in
64   // its file.
65   explicit ReflectionTester(const Descriptor* base_descriptor);
66 
67   void SetAllFieldsViaReflection(Message* message);
68   void ModifyRepeatedFieldsViaReflection(Message* message);
69   void ExpectAllFieldsSetViaReflection(const Message& message);
70   void ExpectClearViaReflection(const Message& message);
71 
72   void SetPackedFieldsViaReflection(Message* message);
73   void ExpectPackedFieldsSetViaReflection(const Message& message);
74 
75   void RemoveLastRepeatedsViaReflection(Message* message);
76   void ReleaseLastRepeatedsViaReflection(Message* message,
77                                          bool expect_extensions_notnull);
78   void SwapRepeatedsViaReflection(Message* message);
79   void SetAllocatedOptionalMessageFieldsToNullViaReflection(Message* message);
80   static void SetAllocatedOptionalMessageFieldsToMessageViaReflection(
81       Message* from_message, Message* to_message);
82 
83   enum MessageReleaseState {
84     IS_NULL,
85     CAN_BE_NULL,
86     NOT_NULL,
87   };
88   void ExpectMessagesReleasedViaReflection(
89       Message* message, MessageReleaseState expected_release_state);
90 
91   // Set and check functions for TestOneof2 messages. No need to construct
92   // the ReflectionTester by TestAllTypes nor TestAllExtensions.
93   static void SetOneofViaReflection(Message* message);
94   static void ExpectOneofSetViaReflection(const Message& message);
95 
96  private:
97   const FieldDescriptor* F(const std::string& name);
98 
99   const Descriptor* base_descriptor_;
100 
101   const FieldDescriptor* group_a_;
102   const FieldDescriptor* repeated_group_a_;
103   const FieldDescriptor* nested_b_;
104   const FieldDescriptor* foreign_c_;
105   const FieldDescriptor* import_d_;
106   const FieldDescriptor* import_e_;
107 
108   const EnumValueDescriptor* nested_foo_;
109   const EnumValueDescriptor* nested_bar_;
110   const EnumValueDescriptor* nested_baz_;
111   const EnumValueDescriptor* foreign_foo_;
112   const EnumValueDescriptor* foreign_bar_;
113   const EnumValueDescriptor* foreign_baz_;
114   const EnumValueDescriptor* import_foo_;
115   const EnumValueDescriptor* import_bar_;
116   const EnumValueDescriptor* import_baz_;
117 
118   // We have to split this into three function otherwise it creates a stack
119   // frame so large that it triggers a warning.
120   void ExpectAllFieldsSetViaReflection1(const Message& message);
121   void ExpectAllFieldsSetViaReflection2(const Message& message);
122   void ExpectAllFieldsSetViaReflection3(const Message& message);
123 
124   GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ReflectionTester);
125 };
126 
ReflectionTester(const Descriptor * base_descriptor)127 inline TestUtil::ReflectionTester::ReflectionTester(
128     const Descriptor* base_descriptor)
129     : base_descriptor_(base_descriptor) {
130   const DescriptorPool* pool = base_descriptor->file()->pool();
131   std::string package = base_descriptor->file()->package();
132   const FieldDescriptor* import_descriptor =
133       pool->FindFieldByName(package + ".TestAllTypes.optional_import_message");
134   std::string import_package =
135       import_descriptor->message_type()->file()->package();
136 
137   nested_b_ = pool->FindFieldByName(package + ".TestAllTypes.NestedMessage.bb");
138   foreign_c_ = pool->FindFieldByName(package + ".ForeignMessage.c");
139   import_d_ = pool->FindFieldByName(import_package + ".ImportMessage.d");
140   import_e_ = pool->FindFieldByName(import_package + ".PublicImportMessage.e");
141   nested_foo_ = pool->FindEnumValueByName(package + ".TestAllTypes.FOO");
142   nested_bar_ = pool->FindEnumValueByName(package + ".TestAllTypes.BAR");
143   nested_baz_ = pool->FindEnumValueByName(package + ".TestAllTypes.BAZ");
144   foreign_foo_ = pool->FindEnumValueByName(package + ".FOREIGN_FOO");
145   foreign_bar_ = pool->FindEnumValueByName(package + ".FOREIGN_BAR");
146   foreign_baz_ = pool->FindEnumValueByName(package + ".FOREIGN_BAZ");
147   import_foo_ = pool->FindEnumValueByName(import_package + ".IMPORT_FOO");
148   import_bar_ = pool->FindEnumValueByName(import_package + ".IMPORT_BAR");
149   import_baz_ = pool->FindEnumValueByName(import_package + ".IMPORT_BAZ");
150 
151   if (base_descriptor_->name() == "TestAllExtensions") {
152     group_a_ = pool->FindFieldByName(package + ".OptionalGroup_extension.a");
153     repeated_group_a_ =
154         pool->FindFieldByName(package + ".RepeatedGroup_extension.a");
155   } else {
156     group_a_ = pool->FindFieldByName(package + ".TestAllTypes.OptionalGroup.a");
157     repeated_group_a_ =
158         pool->FindFieldByName(package + ".TestAllTypes.RepeatedGroup.a");
159   }
160 
161   EXPECT_TRUE(group_a_ != nullptr);
162   EXPECT_TRUE(repeated_group_a_ != nullptr);
163   EXPECT_TRUE(nested_b_ != nullptr);
164   EXPECT_TRUE(foreign_c_ != nullptr);
165   EXPECT_TRUE(import_d_ != nullptr);
166   EXPECT_TRUE(import_e_ != nullptr);
167   EXPECT_TRUE(nested_foo_ != nullptr);
168   EXPECT_TRUE(nested_bar_ != nullptr);
169   EXPECT_TRUE(nested_baz_ != nullptr);
170   EXPECT_TRUE(foreign_foo_ != nullptr);
171   EXPECT_TRUE(foreign_bar_ != nullptr);
172   EXPECT_TRUE(foreign_baz_ != nullptr);
173   EXPECT_TRUE(import_foo_ != nullptr);
174   EXPECT_TRUE(import_bar_ != nullptr);
175   EXPECT_TRUE(import_baz_ != nullptr);
176 }
177 
178 // Shorthand to get a FieldDescriptor for a field of TestAllTypes.
F(const std::string & name)179 inline const FieldDescriptor* TestUtil::ReflectionTester::F(
180     const std::string& name) {
181   const FieldDescriptor* result = nullptr;
182   if (base_descriptor_->name() == "TestAllExtensions" ||
183       base_descriptor_->name() == "TestPackedExtensions") {
184     result = base_descriptor_->file()->FindExtensionByName(name + "_extension");
185   } else {
186     result = base_descriptor_->FindFieldByName(name);
187   }
188   GOOGLE_CHECK(result != nullptr);
189   return result;
190 }
191 
192 // -------------------------------------------------------------------
193 
SetAllFieldsViaReflection(Message * message)194 inline void TestUtil::ReflectionTester::SetAllFieldsViaReflection(
195     Message* message) {
196   const Reflection* reflection = message->GetReflection();
197   Message* sub_message;
198 
199   reflection->SetInt32(message, F("optional_int32"), 101);
200   reflection->SetInt64(message, F("optional_int64"), 102);
201   reflection->SetUInt32(message, F("optional_uint32"), 103);
202   reflection->SetUInt64(message, F("optional_uint64"), 104);
203   reflection->SetInt32(message, F("optional_sint32"), 105);
204   reflection->SetInt64(message, F("optional_sint64"), 106);
205   reflection->SetUInt32(message, F("optional_fixed32"), 107);
206   reflection->SetUInt64(message, F("optional_fixed64"), 108);
207   reflection->SetInt32(message, F("optional_sfixed32"), 109);
208   reflection->SetInt64(message, F("optional_sfixed64"), 110);
209   reflection->SetFloat(message, F("optional_float"), 111);
210   reflection->SetDouble(message, F("optional_double"), 112);
211   reflection->SetBool(message, F("optional_bool"), true);
212   reflection->SetString(message, F("optional_string"), "115");
213   reflection->SetString(message, F("optional_bytes"), "116");
214 
215   sub_message = reflection->MutableMessage(message, F("optionalgroup"));
216   sub_message->GetReflection()->SetInt32(sub_message, group_a_, 117);
217   sub_message =
218       reflection->MutableMessage(message, F("optional_nested_message"));
219   sub_message->GetReflection()->SetInt32(sub_message, nested_b_, 118);
220   sub_message =
221       reflection->MutableMessage(message, F("optional_foreign_message"));
222   sub_message->GetReflection()->SetInt32(sub_message, foreign_c_, 119);
223   sub_message =
224       reflection->MutableMessage(message, F("optional_import_message"));
225   sub_message->GetReflection()->SetInt32(sub_message, import_d_, 120);
226 
227   reflection->SetEnum(message, F("optional_nested_enum"), nested_baz_);
228   reflection->SetEnum(message, F("optional_foreign_enum"), foreign_baz_);
229   reflection->SetEnum(message, F("optional_import_enum"), import_baz_);
230 
231   reflection->SetString(message, F("optional_string_piece"), "124");
232   reflection->SetString(message, F("optional_cord"), "125");
233 
234   sub_message =
235       reflection->MutableMessage(message, F("optional_public_import_message"));
236   sub_message->GetReflection()->SetInt32(sub_message, import_e_, 126);
237 
238   sub_message = reflection->MutableMessage(message, F("optional_lazy_message"));
239   sub_message->GetReflection()->SetInt32(sub_message, nested_b_, 127);
240 
241   // -----------------------------------------------------------------
242 
243   reflection->AddInt32(message, F("repeated_int32"), 201);
244   reflection->AddInt64(message, F("repeated_int64"), 202);
245   reflection->AddUInt32(message, F("repeated_uint32"), 203);
246   reflection->AddUInt64(message, F("repeated_uint64"), 204);
247   reflection->AddInt32(message, F("repeated_sint32"), 205);
248   reflection->AddInt64(message, F("repeated_sint64"), 206);
249   reflection->AddUInt32(message, F("repeated_fixed32"), 207);
250   reflection->AddUInt64(message, F("repeated_fixed64"), 208);
251   reflection->AddInt32(message, F("repeated_sfixed32"), 209);
252   reflection->AddInt64(message, F("repeated_sfixed64"), 210);
253   reflection->AddFloat(message, F("repeated_float"), 211);
254   reflection->AddDouble(message, F("repeated_double"), 212);
255   reflection->AddBool(message, F("repeated_bool"), true);
256   reflection->AddString(message, F("repeated_string"), "215");
257   reflection->AddString(message, F("repeated_bytes"), "216");
258 
259   sub_message = reflection->AddMessage(message, F("repeatedgroup"));
260   sub_message->GetReflection()->SetInt32(sub_message, repeated_group_a_, 217);
261   sub_message = reflection->AddMessage(message, F("repeated_nested_message"));
262   sub_message->GetReflection()->SetInt32(sub_message, nested_b_, 218);
263   sub_message = reflection->AddMessage(message, F("repeated_foreign_message"));
264   sub_message->GetReflection()->SetInt32(sub_message, foreign_c_, 219);
265   sub_message = reflection->AddMessage(message, F("repeated_import_message"));
266   sub_message->GetReflection()->SetInt32(sub_message, import_d_, 220);
267   sub_message = reflection->AddMessage(message, F("repeated_lazy_message"));
268   sub_message->GetReflection()->SetInt32(sub_message, nested_b_, 227);
269 
270   reflection->AddEnum(message, F("repeated_nested_enum"), nested_bar_);
271   reflection->AddEnum(message, F("repeated_foreign_enum"), foreign_bar_);
272   reflection->AddEnum(message, F("repeated_import_enum"), import_bar_);
273 
274   reflection->AddString(message, F("repeated_string_piece"), "224");
275   reflection->AddString(message, F("repeated_cord"), "225");
276 
277   // Add a second one of each field.
278   reflection->AddInt32(message, F("repeated_int32"), 301);
279   reflection->AddInt64(message, F("repeated_int64"), 302);
280   reflection->AddUInt32(message, F("repeated_uint32"), 303);
281   reflection->AddUInt64(message, F("repeated_uint64"), 304);
282   reflection->AddInt32(message, F("repeated_sint32"), 305);
283   reflection->AddInt64(message, F("repeated_sint64"), 306);
284   reflection->AddUInt32(message, F("repeated_fixed32"), 307);
285   reflection->AddUInt64(message, F("repeated_fixed64"), 308);
286   reflection->AddInt32(message, F("repeated_sfixed32"), 309);
287   reflection->AddInt64(message, F("repeated_sfixed64"), 310);
288   reflection->AddFloat(message, F("repeated_float"), 311);
289   reflection->AddDouble(message, F("repeated_double"), 312);
290   reflection->AddBool(message, F("repeated_bool"), false);
291   reflection->AddString(message, F("repeated_string"), "315");
292   reflection->AddString(message, F("repeated_bytes"), "316");
293 
294   sub_message = reflection->AddMessage(message, F("repeatedgroup"));
295   sub_message->GetReflection()->SetInt32(sub_message, repeated_group_a_, 317);
296   sub_message = reflection->AddMessage(message, F("repeated_nested_message"));
297   sub_message->GetReflection()->SetInt32(sub_message, nested_b_, 318);
298   sub_message = reflection->AddMessage(message, F("repeated_foreign_message"));
299   sub_message->GetReflection()->SetInt32(sub_message, foreign_c_, 319);
300   sub_message = reflection->AddMessage(message, F("repeated_import_message"));
301   sub_message->GetReflection()->SetInt32(sub_message, import_d_, 320);
302   sub_message = reflection->AddMessage(message, F("repeated_lazy_message"));
303   sub_message->GetReflection()->SetInt32(sub_message, nested_b_, 327);
304 
305   reflection->AddEnum(message, F("repeated_nested_enum"), nested_baz_);
306   reflection->AddEnum(message, F("repeated_foreign_enum"), foreign_baz_);
307   reflection->AddEnum(message, F("repeated_import_enum"), import_baz_);
308 
309   reflection->AddString(message, F("repeated_string_piece"), "324");
310   reflection->AddString(message, F("repeated_cord"), "325");
311 
312   // -----------------------------------------------------------------
313 
314   reflection->SetInt32(message, F("default_int32"), 401);
315   reflection->SetInt64(message, F("default_int64"), 402);
316   reflection->SetUInt32(message, F("default_uint32"), 403);
317   reflection->SetUInt64(message, F("default_uint64"), 404);
318   reflection->SetInt32(message, F("default_sint32"), 405);
319   reflection->SetInt64(message, F("default_sint64"), 406);
320   reflection->SetUInt32(message, F("default_fixed32"), 407);
321   reflection->SetUInt64(message, F("default_fixed64"), 408);
322   reflection->SetInt32(message, F("default_sfixed32"), 409);
323   reflection->SetInt64(message, F("default_sfixed64"), 410);
324   reflection->SetFloat(message, F("default_float"), 411);
325   reflection->SetDouble(message, F("default_double"), 412);
326   reflection->SetBool(message, F("default_bool"), false);
327   reflection->SetString(message, F("default_string"), "415");
328   reflection->SetString(message, F("default_bytes"), "416");
329 
330   reflection->SetEnum(message, F("default_nested_enum"), nested_foo_);
331   reflection->SetEnum(message, F("default_foreign_enum"), foreign_foo_);
332   reflection->SetEnum(message, F("default_import_enum"), import_foo_);
333 
334   reflection->SetString(message, F("default_string_piece"), "424");
335   reflection->SetString(message, F("default_cord"), "425");
336 
337   reflection->SetUInt32(message, F("oneof_uint32"), 601);
338   sub_message = reflection->MutableMessage(message, F("oneof_nested_message"));
339   sub_message->GetReflection()->SetInt32(sub_message, nested_b_, 602);
340   reflection->SetString(message, F("oneof_string"), "603");
341   reflection->SetString(message, F("oneof_bytes"), "604");
342 }
343 
SetOneofViaReflection(Message * message)344 inline void TestUtil::ReflectionTester::SetOneofViaReflection(
345     Message* message) {
346   const Descriptor* descriptor = message->GetDescriptor();
347   const Reflection* reflection = message->GetReflection();
348   Message* sub_message = reflection->MutableMessage(
349       message, descriptor->FindFieldByName("foo_lazy_message"));
350   sub_message->GetReflection()->SetInt64(
351       sub_message, sub_message->GetDescriptor()->FindFieldByName("qux_int"),
352       100);
353 
354   reflection->SetString(message, descriptor->FindFieldByName("bar_cord"),
355                         "101");
356   reflection->SetInt32(message, descriptor->FindFieldByName("baz_int"), 102);
357   reflection->SetString(message, descriptor->FindFieldByName("baz_string"),
358                         "103");
359 }
360 
ExpectOneofSetViaReflection(const Message & message)361 inline void TestUtil::ReflectionTester::ExpectOneofSetViaReflection(
362     const Message& message) {
363   const Descriptor* descriptor = message.GetDescriptor();
364   const Reflection* reflection = message.GetReflection();
365   std::string scratch;
366   EXPECT_TRUE(reflection->HasField(
367       message, descriptor->FindFieldByName("foo_lazy_message")));
368   EXPECT_TRUE(
369       reflection->HasField(message, descriptor->FindFieldByName("bar_cord")));
370   EXPECT_TRUE(
371       reflection->HasField(message, descriptor->FindFieldByName("baz_int")));
372   EXPECT_TRUE(
373       reflection->HasField(message, descriptor->FindFieldByName("baz_string")));
374 
375   const Message* sub_message = &reflection->GetMessage(
376       message, descriptor->FindFieldByName("foo_lazy_message"));
377   EXPECT_EQ(100, sub_message->GetReflection()->GetInt64(
378                      *sub_message,
379                      sub_message->GetDescriptor()->FindFieldByName("qux_int")));
380 
381   EXPECT_EQ("101", reflection->GetString(
382                        message, descriptor->FindFieldByName("bar_cord")));
383   EXPECT_EQ("101",
384             reflection->GetStringReference(
385                 message, descriptor->FindFieldByName("bar_cord"), &scratch));
386 
387   EXPECT_EQ(102, reflection->GetInt32(message,
388                                       descriptor->FindFieldByName("baz_int")));
389 
390   EXPECT_EQ("103", reflection->GetString(
391                        message, descriptor->FindFieldByName("baz_string")));
392   EXPECT_EQ("103",
393             reflection->GetStringReference(
394                 message, descriptor->FindFieldByName("baz_string"), &scratch));
395 }
396 
SetPackedFieldsViaReflection(Message * message)397 inline void TestUtil::ReflectionTester::SetPackedFieldsViaReflection(
398     Message* message) {
399   const Reflection* reflection = message->GetReflection();
400   reflection->AddInt32(message, F("packed_int32"), 601);
401   reflection->AddInt64(message, F("packed_int64"), 602);
402   reflection->AddUInt32(message, F("packed_uint32"), 603);
403   reflection->AddUInt64(message, F("packed_uint64"), 604);
404   reflection->AddInt32(message, F("packed_sint32"), 605);
405   reflection->AddInt64(message, F("packed_sint64"), 606);
406   reflection->AddUInt32(message, F("packed_fixed32"), 607);
407   reflection->AddUInt64(message, F("packed_fixed64"), 608);
408   reflection->AddInt32(message, F("packed_sfixed32"), 609);
409   reflection->AddInt64(message, F("packed_sfixed64"), 610);
410   reflection->AddFloat(message, F("packed_float"), 611);
411   reflection->AddDouble(message, F("packed_double"), 612);
412   reflection->AddBool(message, F("packed_bool"), true);
413   reflection->AddEnum(message, F("packed_enum"), foreign_bar_);
414 
415   reflection->AddInt32(message, F("packed_int32"), 701);
416   reflection->AddInt64(message, F("packed_int64"), 702);
417   reflection->AddUInt32(message, F("packed_uint32"), 703);
418   reflection->AddUInt64(message, F("packed_uint64"), 704);
419   reflection->AddInt32(message, F("packed_sint32"), 705);
420   reflection->AddInt64(message, F("packed_sint64"), 706);
421   reflection->AddUInt32(message, F("packed_fixed32"), 707);
422   reflection->AddUInt64(message, F("packed_fixed64"), 708);
423   reflection->AddInt32(message, F("packed_sfixed32"), 709);
424   reflection->AddInt64(message, F("packed_sfixed64"), 710);
425   reflection->AddFloat(message, F("packed_float"), 711);
426   reflection->AddDouble(message, F("packed_double"), 712);
427   reflection->AddBool(message, F("packed_bool"), false);
428   reflection->AddEnum(message, F("packed_enum"), foreign_baz_);
429 }
430 
431 // -------------------------------------------------------------------
432 
ExpectAllFieldsSetViaReflection(const Message & message)433 inline void TestUtil::ReflectionTester::ExpectAllFieldsSetViaReflection(
434     const Message& message) {
435   // We have to split this into three function otherwise it creates a stack
436   // frame so large that it triggers a warning.
437   ExpectAllFieldsSetViaReflection1(message);
438   ExpectAllFieldsSetViaReflection2(message);
439   ExpectAllFieldsSetViaReflection3(message);
440 }
441 
ExpectAllFieldsSetViaReflection1(const Message & message)442 inline void TestUtil::ReflectionTester::ExpectAllFieldsSetViaReflection1(
443     const Message& message) {
444   const Reflection* reflection = message.GetReflection();
445   std::string scratch;
446   const Message* sub_message;
447 
448   EXPECT_TRUE(reflection->HasField(message, F("optional_int32")));
449   EXPECT_TRUE(reflection->HasField(message, F("optional_int64")));
450   EXPECT_TRUE(reflection->HasField(message, F("optional_uint32")));
451   EXPECT_TRUE(reflection->HasField(message, F("optional_uint64")));
452   EXPECT_TRUE(reflection->HasField(message, F("optional_sint32")));
453   EXPECT_TRUE(reflection->HasField(message, F("optional_sint64")));
454   EXPECT_TRUE(reflection->HasField(message, F("optional_fixed32")));
455   EXPECT_TRUE(reflection->HasField(message, F("optional_fixed64")));
456   EXPECT_TRUE(reflection->HasField(message, F("optional_sfixed32")));
457   EXPECT_TRUE(reflection->HasField(message, F("optional_sfixed64")));
458   EXPECT_TRUE(reflection->HasField(message, F("optional_float")));
459   EXPECT_TRUE(reflection->HasField(message, F("optional_double")));
460   EXPECT_TRUE(reflection->HasField(message, F("optional_bool")));
461   EXPECT_TRUE(reflection->HasField(message, F("optional_string")));
462   EXPECT_TRUE(reflection->HasField(message, F("optional_bytes")));
463 
464   EXPECT_TRUE(reflection->HasField(message, F("optionalgroup")));
465   EXPECT_TRUE(reflection->HasField(message, F("optional_nested_message")));
466   EXPECT_TRUE(reflection->HasField(message, F("optional_foreign_message")));
467   EXPECT_TRUE(reflection->HasField(message, F("optional_import_message")));
468   EXPECT_TRUE(
469       reflection->HasField(message, F("optional_public_import_message")));
470   EXPECT_TRUE(reflection->HasField(message, F("optional_lazy_message")));
471 
472   sub_message = &reflection->GetMessage(message, F("optionalgroup"));
473   EXPECT_TRUE(sub_message->GetReflection()->HasField(*sub_message, group_a_));
474   sub_message = &reflection->GetMessage(message, F("optional_nested_message"));
475   EXPECT_TRUE(sub_message->GetReflection()->HasField(*sub_message, nested_b_));
476   sub_message = &reflection->GetMessage(message, F("optional_foreign_message"));
477   EXPECT_TRUE(sub_message->GetReflection()->HasField(*sub_message, foreign_c_));
478   sub_message = &reflection->GetMessage(message, F("optional_import_message"));
479   EXPECT_TRUE(sub_message->GetReflection()->HasField(*sub_message, import_d_));
480   sub_message =
481       &reflection->GetMessage(message, F("optional_public_import_message"));
482   EXPECT_TRUE(sub_message->GetReflection()->HasField(*sub_message, import_e_));
483   sub_message = &reflection->GetMessage(message, F("optional_lazy_message"));
484   EXPECT_TRUE(sub_message->GetReflection()->HasField(*sub_message, nested_b_));
485 
486   EXPECT_TRUE(reflection->HasField(message, F("optional_nested_enum")));
487   EXPECT_TRUE(reflection->HasField(message, F("optional_foreign_enum")));
488   EXPECT_TRUE(reflection->HasField(message, F("optional_import_enum")));
489 
490   EXPECT_TRUE(reflection->HasField(message, F("optional_string_piece")));
491   EXPECT_TRUE(reflection->HasField(message, F("optional_cord")));
492 
493   EXPECT_EQ(101, reflection->GetInt32(message, F("optional_int32")));
494   EXPECT_EQ(102, reflection->GetInt64(message, F("optional_int64")));
495   EXPECT_EQ(103, reflection->GetUInt32(message, F("optional_uint32")));
496   EXPECT_EQ(104, reflection->GetUInt64(message, F("optional_uint64")));
497   EXPECT_EQ(105, reflection->GetInt32(message, F("optional_sint32")));
498   EXPECT_EQ(106, reflection->GetInt64(message, F("optional_sint64")));
499   EXPECT_EQ(107, reflection->GetUInt32(message, F("optional_fixed32")));
500   EXPECT_EQ(108, reflection->GetUInt64(message, F("optional_fixed64")));
501   EXPECT_EQ(109, reflection->GetInt32(message, F("optional_sfixed32")));
502   EXPECT_EQ(110, reflection->GetInt64(message, F("optional_sfixed64")));
503   EXPECT_EQ(111, reflection->GetFloat(message, F("optional_float")));
504   EXPECT_EQ(112, reflection->GetDouble(message, F("optional_double")));
505   EXPECT_TRUE(reflection->GetBool(message, F("optional_bool")));
506   EXPECT_EQ("115", reflection->GetString(message, F("optional_string")));
507   EXPECT_EQ("116", reflection->GetString(message, F("optional_bytes")));
508 
509   EXPECT_EQ("115", reflection->GetStringReference(message, F("optional_string"),
510                                                   &scratch));
511   EXPECT_EQ("116", reflection->GetStringReference(message, F("optional_bytes"),
512                                                   &scratch));
513 
514   sub_message = &reflection->GetMessage(message, F("optionalgroup"));
515   EXPECT_EQ(117,
516             sub_message->GetReflection()->GetInt32(*sub_message, group_a_));
517   sub_message = &reflection->GetMessage(message, F("optional_nested_message"));
518   EXPECT_EQ(118,
519             sub_message->GetReflection()->GetInt32(*sub_message, nested_b_));
520   sub_message = &reflection->GetMessage(message, F("optional_foreign_message"));
521   EXPECT_EQ(119,
522             sub_message->GetReflection()->GetInt32(*sub_message, foreign_c_));
523   sub_message = &reflection->GetMessage(message, F("optional_import_message"));
524   EXPECT_EQ(120,
525             sub_message->GetReflection()->GetInt32(*sub_message, import_d_));
526   sub_message =
527       &reflection->GetMessage(message, F("optional_public_import_message"));
528   EXPECT_EQ(126,
529             sub_message->GetReflection()->GetInt32(*sub_message, import_e_));
530   sub_message = &reflection->GetMessage(message, F("optional_lazy_message"));
531   EXPECT_EQ(127,
532             sub_message->GetReflection()->GetInt32(*sub_message, nested_b_));
533 
534   EXPECT_EQ(nested_baz_,
535             reflection->GetEnum(message, F("optional_nested_enum")));
536   EXPECT_EQ(foreign_baz_,
537             reflection->GetEnum(message, F("optional_foreign_enum")));
538   EXPECT_EQ(import_baz_,
539             reflection->GetEnum(message, F("optional_import_enum")));
540 
541   EXPECT_EQ("124", reflection->GetString(message, F("optional_string_piece")));
542   EXPECT_EQ("124", reflection->GetStringReference(
543                        message, F("optional_string_piece"), &scratch));
544 
545   EXPECT_EQ("125", reflection->GetString(message, F("optional_cord")));
546   EXPECT_EQ("125", reflection->GetStringReference(message, F("optional_cord"),
547                                                   &scratch));
548 
549   EXPECT_TRUE(reflection->HasField(message, F("oneof_bytes")));
550   EXPECT_EQ("604", reflection->GetString(message, F("oneof_bytes")));
551 
552   if (base_descriptor_->name() == "TestAllTypes") {
553     EXPECT_FALSE(reflection->HasField(message, F("oneof_uint32")));
554     EXPECT_FALSE(reflection->HasField(message, F("oneof_string")));
555   } else {
556     EXPECT_TRUE(reflection->HasField(message, F("oneof_uint32")));
557     EXPECT_TRUE(reflection->HasField(message, F("oneof_string")));
558     EXPECT_EQ(601, reflection->GetUInt32(message, F("oneof_uint32")));
559     EXPECT_EQ("603", reflection->GetString(message, F("oneof_string")));
560     sub_message = &reflection->GetMessage(message, F("oneof_nested_message"));
561     EXPECT_EQ(602,
562               sub_message->GetReflection()->GetInt32(*sub_message, nested_b_));
563   }
564 }
565 
ExpectAllFieldsSetViaReflection2(const Message & message)566 inline void TestUtil::ReflectionTester::ExpectAllFieldsSetViaReflection2(
567     const Message& message) {
568   const Reflection* reflection = message.GetReflection();
569   std::string scratch;
570   const Message* sub_message;
571 
572   // -----------------------------------------------------------------
573 
574   ASSERT_EQ(2, reflection->FieldSize(message, F("repeated_int32")));
575   ASSERT_EQ(2, reflection->FieldSize(message, F("repeated_int64")));
576   ASSERT_EQ(2, reflection->FieldSize(message, F("repeated_uint32")));
577   ASSERT_EQ(2, reflection->FieldSize(message, F("repeated_uint64")));
578   ASSERT_EQ(2, reflection->FieldSize(message, F("repeated_sint32")));
579   ASSERT_EQ(2, reflection->FieldSize(message, F("repeated_sint64")));
580   ASSERT_EQ(2, reflection->FieldSize(message, F("repeated_fixed32")));
581   ASSERT_EQ(2, reflection->FieldSize(message, F("repeated_fixed64")));
582   ASSERT_EQ(2, reflection->FieldSize(message, F("repeated_sfixed32")));
583   ASSERT_EQ(2, reflection->FieldSize(message, F("repeated_sfixed64")));
584   ASSERT_EQ(2, reflection->FieldSize(message, F("repeated_float")));
585   ASSERT_EQ(2, reflection->FieldSize(message, F("repeated_double")));
586   ASSERT_EQ(2, reflection->FieldSize(message, F("repeated_bool")));
587   ASSERT_EQ(2, reflection->FieldSize(message, F("repeated_string")));
588   ASSERT_EQ(2, reflection->FieldSize(message, F("repeated_bytes")));
589 
590   ASSERT_EQ(2, reflection->FieldSize(message, F("repeatedgroup")));
591   ASSERT_EQ(2, reflection->FieldSize(message, F("repeated_nested_message")));
592   ASSERT_EQ(2, reflection->FieldSize(message, F("repeated_foreign_message")));
593   ASSERT_EQ(2, reflection->FieldSize(message, F("repeated_import_message")));
594   ASSERT_EQ(2, reflection->FieldSize(message, F("repeated_lazy_message")));
595   ASSERT_EQ(2, reflection->FieldSize(message, F("repeated_nested_enum")));
596   ASSERT_EQ(2, reflection->FieldSize(message, F("repeated_foreign_enum")));
597   ASSERT_EQ(2, reflection->FieldSize(message, F("repeated_import_enum")));
598 
599   ASSERT_EQ(2, reflection->FieldSize(message, F("repeated_string_piece")));
600   ASSERT_EQ(2, reflection->FieldSize(message, F("repeated_cord")));
601 
602   EXPECT_EQ(201, reflection->GetRepeatedInt32(message, F("repeated_int32"), 0));
603   EXPECT_EQ(202, reflection->GetRepeatedInt64(message, F("repeated_int64"), 0));
604   EXPECT_EQ(203,
605             reflection->GetRepeatedUInt32(message, F("repeated_uint32"), 0));
606   EXPECT_EQ(204,
607             reflection->GetRepeatedUInt64(message, F("repeated_uint64"), 0));
608   EXPECT_EQ(205,
609             reflection->GetRepeatedInt32(message, F("repeated_sint32"), 0));
610   EXPECT_EQ(206,
611             reflection->GetRepeatedInt64(message, F("repeated_sint64"), 0));
612   EXPECT_EQ(207,
613             reflection->GetRepeatedUInt32(message, F("repeated_fixed32"), 0));
614   EXPECT_EQ(208,
615             reflection->GetRepeatedUInt64(message, F("repeated_fixed64"), 0));
616   EXPECT_EQ(209,
617             reflection->GetRepeatedInt32(message, F("repeated_sfixed32"), 0));
618   EXPECT_EQ(210,
619             reflection->GetRepeatedInt64(message, F("repeated_sfixed64"), 0));
620   EXPECT_EQ(211, reflection->GetRepeatedFloat(message, F("repeated_float"), 0));
621   EXPECT_EQ(212,
622             reflection->GetRepeatedDouble(message, F("repeated_double"), 0));
623   EXPECT_TRUE(reflection->GetRepeatedBool(message, F("repeated_bool"), 0));
624   EXPECT_EQ("215",
625             reflection->GetRepeatedString(message, F("repeated_string"), 0));
626   EXPECT_EQ("216",
627             reflection->GetRepeatedString(message, F("repeated_bytes"), 0));
628 
629   EXPECT_EQ("215", reflection->GetRepeatedStringReference(
630                        message, F("repeated_string"), 0, &scratch));
631   EXPECT_EQ("216", reflection->GetRepeatedStringReference(
632                        message, F("repeated_bytes"), 0, &scratch));
633 
634   sub_message = &reflection->GetRepeatedMessage(message, F("repeatedgroup"), 0);
635   EXPECT_EQ(217, sub_message->GetReflection()->GetInt32(*sub_message,
636                                                         repeated_group_a_));
637   sub_message =
638       &reflection->GetRepeatedMessage(message, F("repeated_nested_message"), 0);
639   EXPECT_EQ(218,
640             sub_message->GetReflection()->GetInt32(*sub_message, nested_b_));
641   sub_message = &reflection->GetRepeatedMessage(
642       message, F("repeated_foreign_message"), 0);
643   EXPECT_EQ(219,
644             sub_message->GetReflection()->GetInt32(*sub_message, foreign_c_));
645   sub_message =
646       &reflection->GetRepeatedMessage(message, F("repeated_import_message"), 0);
647   EXPECT_EQ(220,
648             sub_message->GetReflection()->GetInt32(*sub_message, import_d_));
649   sub_message =
650       &reflection->GetRepeatedMessage(message, F("repeated_lazy_message"), 0);
651   EXPECT_EQ(227,
652             sub_message->GetReflection()->GetInt32(*sub_message, nested_b_));
653 
654   EXPECT_EQ(nested_bar_,
655             reflection->GetRepeatedEnum(message, F("repeated_nested_enum"), 0));
656   EXPECT_EQ(foreign_bar_, reflection->GetRepeatedEnum(
657                               message, F("repeated_foreign_enum"), 0));
658   EXPECT_EQ(import_bar_,
659             reflection->GetRepeatedEnum(message, F("repeated_import_enum"), 0));
660 
661   EXPECT_EQ("224", reflection->GetRepeatedString(
662                        message, F("repeated_string_piece"), 0));
663   EXPECT_EQ("224", reflection->GetRepeatedStringReference(
664                        message, F("repeated_string_piece"), 0, &scratch));
665 
666   EXPECT_EQ("225",
667             reflection->GetRepeatedString(message, F("repeated_cord"), 0));
668   EXPECT_EQ("225", reflection->GetRepeatedStringReference(
669                        message, F("repeated_cord"), 0, &scratch));
670 
671   EXPECT_EQ(301, reflection->GetRepeatedInt32(message, F("repeated_int32"), 1));
672   EXPECT_EQ(302, reflection->GetRepeatedInt64(message, F("repeated_int64"), 1));
673   EXPECT_EQ(303,
674             reflection->GetRepeatedUInt32(message, F("repeated_uint32"), 1));
675   EXPECT_EQ(304,
676             reflection->GetRepeatedUInt64(message, F("repeated_uint64"), 1));
677   EXPECT_EQ(305,
678             reflection->GetRepeatedInt32(message, F("repeated_sint32"), 1));
679   EXPECT_EQ(306,
680             reflection->GetRepeatedInt64(message, F("repeated_sint64"), 1));
681   EXPECT_EQ(307,
682             reflection->GetRepeatedUInt32(message, F("repeated_fixed32"), 1));
683   EXPECT_EQ(308,
684             reflection->GetRepeatedUInt64(message, F("repeated_fixed64"), 1));
685   EXPECT_EQ(309,
686             reflection->GetRepeatedInt32(message, F("repeated_sfixed32"), 1));
687   EXPECT_EQ(310,
688             reflection->GetRepeatedInt64(message, F("repeated_sfixed64"), 1));
689   EXPECT_EQ(311, reflection->GetRepeatedFloat(message, F("repeated_float"), 1));
690   EXPECT_EQ(312,
691             reflection->GetRepeatedDouble(message, F("repeated_double"), 1));
692   EXPECT_FALSE(reflection->GetRepeatedBool(message, F("repeated_bool"), 1));
693   EXPECT_EQ("315",
694             reflection->GetRepeatedString(message, F("repeated_string"), 1));
695   EXPECT_EQ("316",
696             reflection->GetRepeatedString(message, F("repeated_bytes"), 1));
697 
698   EXPECT_EQ("315", reflection->GetRepeatedStringReference(
699                        message, F("repeated_string"), 1, &scratch));
700   EXPECT_EQ("316", reflection->GetRepeatedStringReference(
701                        message, F("repeated_bytes"), 1, &scratch));
702 
703   sub_message = &reflection->GetRepeatedMessage(message, F("repeatedgroup"), 1);
704   EXPECT_EQ(317, sub_message->GetReflection()->GetInt32(*sub_message,
705                                                         repeated_group_a_));
706   sub_message =
707       &reflection->GetRepeatedMessage(message, F("repeated_nested_message"), 1);
708   EXPECT_EQ(318,
709             sub_message->GetReflection()->GetInt32(*sub_message, nested_b_));
710   sub_message = &reflection->GetRepeatedMessage(
711       message, F("repeated_foreign_message"), 1);
712   EXPECT_EQ(319,
713             sub_message->GetReflection()->GetInt32(*sub_message, foreign_c_));
714   sub_message =
715       &reflection->GetRepeatedMessage(message, F("repeated_import_message"), 1);
716   EXPECT_EQ(320,
717             sub_message->GetReflection()->GetInt32(*sub_message, import_d_));
718   sub_message =
719       &reflection->GetRepeatedMessage(message, F("repeated_lazy_message"), 1);
720   EXPECT_EQ(327,
721             sub_message->GetReflection()->GetInt32(*sub_message, nested_b_));
722 
723   EXPECT_EQ(nested_baz_,
724             reflection->GetRepeatedEnum(message, F("repeated_nested_enum"), 1));
725   EXPECT_EQ(foreign_baz_, reflection->GetRepeatedEnum(
726                               message, F("repeated_foreign_enum"), 1));
727   EXPECT_EQ(import_baz_,
728             reflection->GetRepeatedEnum(message, F("repeated_import_enum"), 1));
729 
730   EXPECT_EQ("324", reflection->GetRepeatedString(
731                        message, F("repeated_string_piece"), 1));
732   EXPECT_EQ("324", reflection->GetRepeatedStringReference(
733                        message, F("repeated_string_piece"), 1, &scratch));
734 
735   EXPECT_EQ("325",
736             reflection->GetRepeatedString(message, F("repeated_cord"), 1));
737   EXPECT_EQ("325", reflection->GetRepeatedStringReference(
738                        message, F("repeated_cord"), 1, &scratch));
739 }
740 
ExpectAllFieldsSetViaReflection3(const Message & message)741 inline void TestUtil::ReflectionTester::ExpectAllFieldsSetViaReflection3(
742     const Message& message) {
743   const Reflection* reflection = message.GetReflection();
744   std::string scratch;
745 
746   // -----------------------------------------------------------------
747 
748   EXPECT_TRUE(reflection->HasField(message, F("default_int32")));
749   EXPECT_TRUE(reflection->HasField(message, F("default_int64")));
750   EXPECT_TRUE(reflection->HasField(message, F("default_uint32")));
751   EXPECT_TRUE(reflection->HasField(message, F("default_uint64")));
752   EXPECT_TRUE(reflection->HasField(message, F("default_sint32")));
753   EXPECT_TRUE(reflection->HasField(message, F("default_sint64")));
754   EXPECT_TRUE(reflection->HasField(message, F("default_fixed32")));
755   EXPECT_TRUE(reflection->HasField(message, F("default_fixed64")));
756   EXPECT_TRUE(reflection->HasField(message, F("default_sfixed32")));
757   EXPECT_TRUE(reflection->HasField(message, F("default_sfixed64")));
758   EXPECT_TRUE(reflection->HasField(message, F("default_float")));
759   EXPECT_TRUE(reflection->HasField(message, F("default_double")));
760   EXPECT_TRUE(reflection->HasField(message, F("default_bool")));
761   EXPECT_TRUE(reflection->HasField(message, F("default_string")));
762   EXPECT_TRUE(reflection->HasField(message, F("default_bytes")));
763 
764   EXPECT_TRUE(reflection->HasField(message, F("default_nested_enum")));
765   EXPECT_TRUE(reflection->HasField(message, F("default_foreign_enum")));
766   EXPECT_TRUE(reflection->HasField(message, F("default_import_enum")));
767 
768   EXPECT_TRUE(reflection->HasField(message, F("default_string_piece")));
769   EXPECT_TRUE(reflection->HasField(message, F("default_cord")));
770 
771   EXPECT_EQ(401, reflection->GetInt32(message, F("default_int32")));
772   EXPECT_EQ(402, reflection->GetInt64(message, F("default_int64")));
773   EXPECT_EQ(403, reflection->GetUInt32(message, F("default_uint32")));
774   EXPECT_EQ(404, reflection->GetUInt64(message, F("default_uint64")));
775   EXPECT_EQ(405, reflection->GetInt32(message, F("default_sint32")));
776   EXPECT_EQ(406, reflection->GetInt64(message, F("default_sint64")));
777   EXPECT_EQ(407, reflection->GetUInt32(message, F("default_fixed32")));
778   EXPECT_EQ(408, reflection->GetUInt64(message, F("default_fixed64")));
779   EXPECT_EQ(409, reflection->GetInt32(message, F("default_sfixed32")));
780   EXPECT_EQ(410, reflection->GetInt64(message, F("default_sfixed64")));
781   EXPECT_EQ(411, reflection->GetFloat(message, F("default_float")));
782   EXPECT_EQ(412, reflection->GetDouble(message, F("default_double")));
783   EXPECT_FALSE(reflection->GetBool(message, F("default_bool")));
784   EXPECT_EQ("415", reflection->GetString(message, F("default_string")));
785   EXPECT_EQ("416", reflection->GetString(message, F("default_bytes")));
786 
787   EXPECT_EQ("415", reflection->GetStringReference(message, F("default_string"),
788                                                   &scratch));
789   EXPECT_EQ("416", reflection->GetStringReference(message, F("default_bytes"),
790                                                   &scratch));
791 
792   EXPECT_EQ(nested_foo_,
793             reflection->GetEnum(message, F("default_nested_enum")));
794   EXPECT_EQ(foreign_foo_,
795             reflection->GetEnum(message, F("default_foreign_enum")));
796   EXPECT_EQ(import_foo_,
797             reflection->GetEnum(message, F("default_import_enum")));
798 
799   EXPECT_EQ("424", reflection->GetString(message, F("default_string_piece")));
800   EXPECT_EQ("424", reflection->GetStringReference(
801                        message, F("default_string_piece"), &scratch));
802 
803   EXPECT_EQ("425", reflection->GetString(message, F("default_cord")));
804   EXPECT_EQ("425", reflection->GetStringReference(message, F("default_cord"),
805                                                   &scratch));
806 }
807 
ExpectPackedFieldsSetViaReflection(const Message & message)808 inline void TestUtil::ReflectionTester::ExpectPackedFieldsSetViaReflection(
809     const Message& message) {
810   const Reflection* reflection = message.GetReflection();
811 
812   ASSERT_EQ(2, reflection->FieldSize(message, F("packed_int32")));
813   ASSERT_EQ(2, reflection->FieldSize(message, F("packed_int64")));
814   ASSERT_EQ(2, reflection->FieldSize(message, F("packed_uint32")));
815   ASSERT_EQ(2, reflection->FieldSize(message, F("packed_uint64")));
816   ASSERT_EQ(2, reflection->FieldSize(message, F("packed_sint32")));
817   ASSERT_EQ(2, reflection->FieldSize(message, F("packed_sint64")));
818   ASSERT_EQ(2, reflection->FieldSize(message, F("packed_fixed32")));
819   ASSERT_EQ(2, reflection->FieldSize(message, F("packed_fixed64")));
820   ASSERT_EQ(2, reflection->FieldSize(message, F("packed_sfixed32")));
821   ASSERT_EQ(2, reflection->FieldSize(message, F("packed_sfixed64")));
822   ASSERT_EQ(2, reflection->FieldSize(message, F("packed_float")));
823   ASSERT_EQ(2, reflection->FieldSize(message, F("packed_double")));
824   ASSERT_EQ(2, reflection->FieldSize(message, F("packed_bool")));
825   ASSERT_EQ(2, reflection->FieldSize(message, F("packed_enum")));
826 
827   EXPECT_EQ(601, reflection->GetRepeatedInt32(message, F("packed_int32"), 0));
828   EXPECT_EQ(602, reflection->GetRepeatedInt64(message, F("packed_int64"), 0));
829   EXPECT_EQ(603, reflection->GetRepeatedUInt32(message, F("packed_uint32"), 0));
830   EXPECT_EQ(604, reflection->GetRepeatedUInt64(message, F("packed_uint64"), 0));
831   EXPECT_EQ(605, reflection->GetRepeatedInt32(message, F("packed_sint32"), 0));
832   EXPECT_EQ(606, reflection->GetRepeatedInt64(message, F("packed_sint64"), 0));
833   EXPECT_EQ(607,
834             reflection->GetRepeatedUInt32(message, F("packed_fixed32"), 0));
835   EXPECT_EQ(608,
836             reflection->GetRepeatedUInt64(message, F("packed_fixed64"), 0));
837   EXPECT_EQ(609,
838             reflection->GetRepeatedInt32(message, F("packed_sfixed32"), 0));
839   EXPECT_EQ(610,
840             reflection->GetRepeatedInt64(message, F("packed_sfixed64"), 0));
841   EXPECT_EQ(611, reflection->GetRepeatedFloat(message, F("packed_float"), 0));
842   EXPECT_EQ(612, reflection->GetRepeatedDouble(message, F("packed_double"), 0));
843   EXPECT_TRUE(reflection->GetRepeatedBool(message, F("packed_bool"), 0));
844   EXPECT_EQ(foreign_bar_,
845             reflection->GetRepeatedEnum(message, F("packed_enum"), 0));
846 
847   EXPECT_EQ(701, reflection->GetRepeatedInt32(message, F("packed_int32"), 1));
848   EXPECT_EQ(702, reflection->GetRepeatedInt64(message, F("packed_int64"), 1));
849   EXPECT_EQ(703, reflection->GetRepeatedUInt32(message, F("packed_uint32"), 1));
850   EXPECT_EQ(704, reflection->GetRepeatedUInt64(message, F("packed_uint64"), 1));
851   EXPECT_EQ(705, reflection->GetRepeatedInt32(message, F("packed_sint32"), 1));
852   EXPECT_EQ(706, reflection->GetRepeatedInt64(message, F("packed_sint64"), 1));
853   EXPECT_EQ(707,
854             reflection->GetRepeatedUInt32(message, F("packed_fixed32"), 1));
855   EXPECT_EQ(708,
856             reflection->GetRepeatedUInt64(message, F("packed_fixed64"), 1));
857   EXPECT_EQ(709,
858             reflection->GetRepeatedInt32(message, F("packed_sfixed32"), 1));
859   EXPECT_EQ(710,
860             reflection->GetRepeatedInt64(message, F("packed_sfixed64"), 1));
861   EXPECT_EQ(711, reflection->GetRepeatedFloat(message, F("packed_float"), 1));
862   EXPECT_EQ(712, reflection->GetRepeatedDouble(message, F("packed_double"), 1));
863   EXPECT_FALSE(reflection->GetRepeatedBool(message, F("packed_bool"), 1));
864   EXPECT_EQ(foreign_baz_,
865             reflection->GetRepeatedEnum(message, F("packed_enum"), 1));
866 }
867 
868 // -------------------------------------------------------------------
869 
ExpectClearViaReflection(const Message & message)870 inline void TestUtil::ReflectionTester::ExpectClearViaReflection(
871     const Message& message) {
872   const Reflection* reflection = message.GetReflection();
873   std::string scratch;
874   const Message* sub_message;
875 
876   // has_blah() should initially be false for all optional fields.
877   EXPECT_FALSE(reflection->HasField(message, F("optional_int32")));
878   EXPECT_FALSE(reflection->HasField(message, F("optional_int64")));
879   EXPECT_FALSE(reflection->HasField(message, F("optional_uint32")));
880   EXPECT_FALSE(reflection->HasField(message, F("optional_uint64")));
881   EXPECT_FALSE(reflection->HasField(message, F("optional_sint32")));
882   EXPECT_FALSE(reflection->HasField(message, F("optional_sint64")));
883   EXPECT_FALSE(reflection->HasField(message, F("optional_fixed32")));
884   EXPECT_FALSE(reflection->HasField(message, F("optional_fixed64")));
885   EXPECT_FALSE(reflection->HasField(message, F("optional_sfixed32")));
886   EXPECT_FALSE(reflection->HasField(message, F("optional_sfixed64")));
887   EXPECT_FALSE(reflection->HasField(message, F("optional_float")));
888   EXPECT_FALSE(reflection->HasField(message, F("optional_double")));
889   EXPECT_FALSE(reflection->HasField(message, F("optional_bool")));
890   EXPECT_FALSE(reflection->HasField(message, F("optional_string")));
891   EXPECT_FALSE(reflection->HasField(message, F("optional_bytes")));
892 
893   EXPECT_FALSE(reflection->HasField(message, F("optionalgroup")));
894   EXPECT_FALSE(reflection->HasField(message, F("optional_nested_message")));
895   EXPECT_FALSE(reflection->HasField(message, F("optional_foreign_message")));
896   EXPECT_FALSE(reflection->HasField(message, F("optional_import_message")));
897   EXPECT_FALSE(
898       reflection->HasField(message, F("optional_public_import_message")));
899   EXPECT_FALSE(reflection->HasField(message, F("optional_lazy_message")));
900 
901   EXPECT_FALSE(reflection->HasField(message, F("optional_nested_enum")));
902   EXPECT_FALSE(reflection->HasField(message, F("optional_foreign_enum")));
903   EXPECT_FALSE(reflection->HasField(message, F("optional_import_enum")));
904 
905   EXPECT_FALSE(reflection->HasField(message, F("optional_string_piece")));
906   EXPECT_FALSE(reflection->HasField(message, F("optional_cord")));
907 
908   // Optional fields without defaults are set to zero or something like it.
909   EXPECT_EQ(0, reflection->GetInt32(message, F("optional_int32")));
910   EXPECT_EQ(0, reflection->GetInt64(message, F("optional_int64")));
911   EXPECT_EQ(0, reflection->GetUInt32(message, F("optional_uint32")));
912   EXPECT_EQ(0, reflection->GetUInt64(message, F("optional_uint64")));
913   EXPECT_EQ(0, reflection->GetInt32(message, F("optional_sint32")));
914   EXPECT_EQ(0, reflection->GetInt64(message, F("optional_sint64")));
915   EXPECT_EQ(0, reflection->GetUInt32(message, F("optional_fixed32")));
916   EXPECT_EQ(0, reflection->GetUInt64(message, F("optional_fixed64")));
917   EXPECT_EQ(0, reflection->GetInt32(message, F("optional_sfixed32")));
918   EXPECT_EQ(0, reflection->GetInt64(message, F("optional_sfixed64")));
919   EXPECT_EQ(0, reflection->GetFloat(message, F("optional_float")));
920   EXPECT_EQ(0, reflection->GetDouble(message, F("optional_double")));
921   EXPECT_FALSE(reflection->GetBool(message, F("optional_bool")));
922   EXPECT_EQ("", reflection->GetString(message, F("optional_string")));
923   EXPECT_EQ("", reflection->GetString(message, F("optional_bytes")));
924 
925   EXPECT_EQ("", reflection->GetStringReference(message, F("optional_string"),
926                                                &scratch));
927   EXPECT_EQ("", reflection->GetStringReference(message, F("optional_bytes"),
928                                                &scratch));
929 
930   // Embedded messages should also be clear.
931   sub_message = &reflection->GetMessage(message, F("optionalgroup"));
932   EXPECT_FALSE(sub_message->GetReflection()->HasField(*sub_message, group_a_));
933   EXPECT_EQ(0, sub_message->GetReflection()->GetInt32(*sub_message, group_a_));
934   sub_message = &reflection->GetMessage(message, F("optional_nested_message"));
935   EXPECT_FALSE(sub_message->GetReflection()->HasField(*sub_message, nested_b_));
936   EXPECT_EQ(0, sub_message->GetReflection()->GetInt32(*sub_message, nested_b_));
937   sub_message = &reflection->GetMessage(message, F("optional_foreign_message"));
938   EXPECT_FALSE(
939       sub_message->GetReflection()->HasField(*sub_message, foreign_c_));
940   EXPECT_EQ(0,
941             sub_message->GetReflection()->GetInt32(*sub_message, foreign_c_));
942   sub_message = &reflection->GetMessage(message, F("optional_import_message"));
943   EXPECT_FALSE(sub_message->GetReflection()->HasField(*sub_message, import_d_));
944   EXPECT_EQ(0, sub_message->GetReflection()->GetInt32(*sub_message, import_d_));
945   sub_message =
946       &reflection->GetMessage(message, F("optional_public_import_message"));
947   EXPECT_FALSE(sub_message->GetReflection()->HasField(*sub_message, import_e_));
948   EXPECT_EQ(0, sub_message->GetReflection()->GetInt32(*sub_message, import_e_));
949   sub_message = &reflection->GetMessage(message, F("optional_lazy_message"));
950   EXPECT_FALSE(sub_message->GetReflection()->HasField(*sub_message, nested_b_));
951   EXPECT_EQ(0, sub_message->GetReflection()->GetInt32(*sub_message, nested_b_));
952 
953   // Enums without defaults are set to the first value in the enum.
954   EXPECT_EQ(nested_foo_,
955             reflection->GetEnum(message, F("optional_nested_enum")));
956   EXPECT_EQ(foreign_foo_,
957             reflection->GetEnum(message, F("optional_foreign_enum")));
958   EXPECT_EQ(import_foo_,
959             reflection->GetEnum(message, F("optional_import_enum")));
960 
961   EXPECT_EQ("", reflection->GetString(message, F("optional_string_piece")));
962   EXPECT_EQ("", reflection->GetStringReference(
963                     message, F("optional_string_piece"), &scratch));
964 
965   EXPECT_EQ("", reflection->GetString(message, F("optional_cord")));
966   EXPECT_EQ("", reflection->GetStringReference(message, F("optional_cord"),
967                                                &scratch));
968 
969   // Repeated fields are empty.
970   EXPECT_EQ(0, reflection->FieldSize(message, F("repeated_int32")));
971   EXPECT_EQ(0, reflection->FieldSize(message, F("repeated_int64")));
972   EXPECT_EQ(0, reflection->FieldSize(message, F("repeated_uint32")));
973   EXPECT_EQ(0, reflection->FieldSize(message, F("repeated_uint64")));
974   EXPECT_EQ(0, reflection->FieldSize(message, F("repeated_sint32")));
975   EXPECT_EQ(0, reflection->FieldSize(message, F("repeated_sint64")));
976   EXPECT_EQ(0, reflection->FieldSize(message, F("repeated_fixed32")));
977   EXPECT_EQ(0, reflection->FieldSize(message, F("repeated_fixed64")));
978   EXPECT_EQ(0, reflection->FieldSize(message, F("repeated_sfixed32")));
979   EXPECT_EQ(0, reflection->FieldSize(message, F("repeated_sfixed64")));
980   EXPECT_EQ(0, reflection->FieldSize(message, F("repeated_float")));
981   EXPECT_EQ(0, reflection->FieldSize(message, F("repeated_double")));
982   EXPECT_EQ(0, reflection->FieldSize(message, F("repeated_bool")));
983   EXPECT_EQ(0, reflection->FieldSize(message, F("repeated_string")));
984   EXPECT_EQ(0, reflection->FieldSize(message, F("repeated_bytes")));
985 
986   EXPECT_EQ(0, reflection->FieldSize(message, F("repeatedgroup")));
987   EXPECT_EQ(0, reflection->FieldSize(message, F("repeated_nested_message")));
988   EXPECT_EQ(0, reflection->FieldSize(message, F("repeated_foreign_message")));
989   EXPECT_EQ(0, reflection->FieldSize(message, F("repeated_import_message")));
990   EXPECT_EQ(0, reflection->FieldSize(message, F("repeated_lazy_message")));
991   EXPECT_EQ(0, reflection->FieldSize(message, F("repeated_nested_enum")));
992   EXPECT_EQ(0, reflection->FieldSize(message, F("repeated_foreign_enum")));
993   EXPECT_EQ(0, reflection->FieldSize(message, F("repeated_import_enum")));
994 
995   EXPECT_EQ(0, reflection->FieldSize(message, F("repeated_string_piece")));
996   EXPECT_EQ(0, reflection->FieldSize(message, F("repeated_cord")));
997 
998   // has_blah() should also be false for all default fields.
999   EXPECT_FALSE(reflection->HasField(message, F("default_int32")));
1000   EXPECT_FALSE(reflection->HasField(message, F("default_int64")));
1001   EXPECT_FALSE(reflection->HasField(message, F("default_uint32")));
1002   EXPECT_FALSE(reflection->HasField(message, F("default_uint64")));
1003   EXPECT_FALSE(reflection->HasField(message, F("default_sint32")));
1004   EXPECT_FALSE(reflection->HasField(message, F("default_sint64")));
1005   EXPECT_FALSE(reflection->HasField(message, F("default_fixed32")));
1006   EXPECT_FALSE(reflection->HasField(message, F("default_fixed64")));
1007   EXPECT_FALSE(reflection->HasField(message, F("default_sfixed32")));
1008   EXPECT_FALSE(reflection->HasField(message, F("default_sfixed64")));
1009   EXPECT_FALSE(reflection->HasField(message, F("default_float")));
1010   EXPECT_FALSE(reflection->HasField(message, F("default_double")));
1011   EXPECT_FALSE(reflection->HasField(message, F("default_bool")));
1012   EXPECT_FALSE(reflection->HasField(message, F("default_string")));
1013   EXPECT_FALSE(reflection->HasField(message, F("default_bytes")));
1014 
1015   EXPECT_FALSE(reflection->HasField(message, F("default_nested_enum")));
1016   EXPECT_FALSE(reflection->HasField(message, F("default_foreign_enum")));
1017   EXPECT_FALSE(reflection->HasField(message, F("default_import_enum")));
1018 
1019   EXPECT_FALSE(reflection->HasField(message, F("default_string_piece")));
1020   EXPECT_FALSE(reflection->HasField(message, F("default_cord")));
1021 
1022   // Fields with defaults have their default values (duh).
1023   EXPECT_EQ(41, reflection->GetInt32(message, F("default_int32")));
1024   EXPECT_EQ(42, reflection->GetInt64(message, F("default_int64")));
1025   EXPECT_EQ(43, reflection->GetUInt32(message, F("default_uint32")));
1026   EXPECT_EQ(44, reflection->GetUInt64(message, F("default_uint64")));
1027   EXPECT_EQ(-45, reflection->GetInt32(message, F("default_sint32")));
1028   EXPECT_EQ(46, reflection->GetInt64(message, F("default_sint64")));
1029   EXPECT_EQ(47, reflection->GetUInt32(message, F("default_fixed32")));
1030   EXPECT_EQ(48, reflection->GetUInt64(message, F("default_fixed64")));
1031   EXPECT_EQ(49, reflection->GetInt32(message, F("default_sfixed32")));
1032   EXPECT_EQ(-50, reflection->GetInt64(message, F("default_sfixed64")));
1033   EXPECT_EQ(51.5, reflection->GetFloat(message, F("default_float")));
1034   EXPECT_EQ(52e3, reflection->GetDouble(message, F("default_double")));
1035   EXPECT_TRUE(reflection->GetBool(message, F("default_bool")));
1036   EXPECT_EQ("hello", reflection->GetString(message, F("default_string")));
1037   EXPECT_EQ("world", reflection->GetString(message, F("default_bytes")));
1038 
1039   EXPECT_EQ("hello", reflection->GetStringReference(
1040                          message, F("default_string"), &scratch));
1041   EXPECT_EQ("world", reflection->GetStringReference(message, F("default_bytes"),
1042                                                     &scratch));
1043 
1044   EXPECT_EQ(nested_bar_,
1045             reflection->GetEnum(message, F("default_nested_enum")));
1046   EXPECT_EQ(foreign_bar_,
1047             reflection->GetEnum(message, F("default_foreign_enum")));
1048   EXPECT_EQ(import_bar_,
1049             reflection->GetEnum(message, F("default_import_enum")));
1050 
1051   EXPECT_EQ("abc", reflection->GetString(message, F("default_string_piece")));
1052   EXPECT_EQ("abc", reflection->GetStringReference(
1053                        message, F("default_string_piece"), &scratch));
1054 
1055   EXPECT_EQ("123", reflection->GetString(message, F("default_cord")));
1056   EXPECT_EQ("123", reflection->GetStringReference(message, F("default_cord"),
1057                                                   &scratch));
1058 }
1059 
1060 // -------------------------------------------------------------------
1061 
ModifyRepeatedFieldsViaReflection(Message * message)1062 inline void TestUtil::ReflectionTester::ModifyRepeatedFieldsViaReflection(
1063     Message* message) {
1064   const Reflection* reflection = message->GetReflection();
1065   Message* sub_message;
1066 
1067   reflection->SetRepeatedInt32(message, F("repeated_int32"), 1, 501);
1068   reflection->SetRepeatedInt64(message, F("repeated_int64"), 1, 502);
1069   reflection->SetRepeatedUInt32(message, F("repeated_uint32"), 1, 503);
1070   reflection->SetRepeatedUInt64(message, F("repeated_uint64"), 1, 504);
1071   reflection->SetRepeatedInt32(message, F("repeated_sint32"), 1, 505);
1072   reflection->SetRepeatedInt64(message, F("repeated_sint64"), 1, 506);
1073   reflection->SetRepeatedUInt32(message, F("repeated_fixed32"), 1, 507);
1074   reflection->SetRepeatedUInt64(message, F("repeated_fixed64"), 1, 508);
1075   reflection->SetRepeatedInt32(message, F("repeated_sfixed32"), 1, 509);
1076   reflection->SetRepeatedInt64(message, F("repeated_sfixed64"), 1, 510);
1077   reflection->SetRepeatedFloat(message, F("repeated_float"), 1, 511);
1078   reflection->SetRepeatedDouble(message, F("repeated_double"), 1, 512);
1079   reflection->SetRepeatedBool(message, F("repeated_bool"), 1, true);
1080   reflection->SetRepeatedString(message, F("repeated_string"), 1, "515");
1081   reflection->SetRepeatedString(message, F("repeated_bytes"), 1, "516");
1082 
1083   sub_message =
1084       reflection->MutableRepeatedMessage(message, F("repeatedgroup"), 1);
1085   sub_message->GetReflection()->SetInt32(sub_message, repeated_group_a_, 517);
1086   sub_message = reflection->MutableRepeatedMessage(
1087       message, F("repeated_nested_message"), 1);
1088   sub_message->GetReflection()->SetInt32(sub_message, nested_b_, 518);
1089   sub_message = reflection->MutableRepeatedMessage(
1090       message, F("repeated_foreign_message"), 1);
1091   sub_message->GetReflection()->SetInt32(sub_message, foreign_c_, 519);
1092   sub_message = reflection->MutableRepeatedMessage(
1093       message, F("repeated_import_message"), 1);
1094   sub_message->GetReflection()->SetInt32(sub_message, import_d_, 520);
1095   sub_message = reflection->MutableRepeatedMessage(
1096       message, F("repeated_lazy_message"), 1);
1097   sub_message->GetReflection()->SetInt32(sub_message, nested_b_, 527);
1098 
1099   reflection->SetRepeatedEnum(message, F("repeated_nested_enum"), 1,
1100                               nested_foo_);
1101   reflection->SetRepeatedEnum(message, F("repeated_foreign_enum"), 1,
1102                               foreign_foo_);
1103   reflection->SetRepeatedEnum(message, F("repeated_import_enum"), 1,
1104                               import_foo_);
1105 
1106   reflection->SetRepeatedString(message, F("repeated_string_piece"), 1, "524");
1107   reflection->SetRepeatedString(message, F("repeated_cord"), 1, "525");
1108 }
1109 
RemoveLastRepeatedsViaReflection(Message * message)1110 inline void TestUtil::ReflectionTester::RemoveLastRepeatedsViaReflection(
1111     Message* message) {
1112   const Reflection* reflection = message->GetReflection();
1113 
1114   std::vector<const FieldDescriptor*> output;
1115   reflection->ListFields(*message, &output);
1116   for (int i = 0; i < output.size(); ++i) {
1117     const FieldDescriptor* field = output[i];
1118     if (!field->is_repeated()) continue;
1119 
1120     reflection->RemoveLast(message, field);
1121   }
1122 }
1123 
ReleaseLastRepeatedsViaReflection(Message * message,bool expect_extensions_notnull)1124 inline void TestUtil::ReflectionTester::ReleaseLastRepeatedsViaReflection(
1125     Message* message, bool expect_extensions_notnull) {
1126   const Reflection* reflection = message->GetReflection();
1127 
1128   std::vector<const FieldDescriptor*> output;
1129   reflection->ListFields(*message, &output);
1130   for (int i = 0; i < output.size(); ++i) {
1131     const FieldDescriptor* field = output[i];
1132     if (!field->is_repeated()) continue;
1133     if (field->cpp_type() != FieldDescriptor::CPPTYPE_MESSAGE) continue;
1134 
1135     Message* released = reflection->ReleaseLast(message, field);
1136     if (!field->is_extension() || expect_extensions_notnull) {
1137       ASSERT_TRUE(released != nullptr)
1138           << "ReleaseLast returned nullptr for: " << field->name();
1139     }
1140     delete released;
1141   }
1142 }
1143 
SwapRepeatedsViaReflection(Message * message)1144 inline void TestUtil::ReflectionTester::SwapRepeatedsViaReflection(
1145     Message* message) {
1146   const Reflection* reflection = message->GetReflection();
1147 
1148   std::vector<const FieldDescriptor*> output;
1149   reflection->ListFields(*message, &output);
1150   for (int i = 0; i < output.size(); ++i) {
1151     const FieldDescriptor* field = output[i];
1152     if (!field->is_repeated()) continue;
1153 
1154     reflection->SwapElements(message, field, 0, 1);
1155   }
1156 }
1157 
1158 inline void TestUtil::ReflectionTester::
SetAllocatedOptionalMessageFieldsToNullViaReflection(Message * message)1159     SetAllocatedOptionalMessageFieldsToNullViaReflection(Message* message) {
1160   const Reflection* reflection = message->GetReflection();
1161 
1162   std::vector<const FieldDescriptor*> fields;
1163   reflection->ListFields(*message, &fields);
1164 
1165   for (int i = 0; i < fields.size(); ++i) {
1166     const FieldDescriptor* field = fields[i];
1167     if (!field->is_optional() ||
1168         field->cpp_type() != FieldDescriptor::CPPTYPE_MESSAGE)
1169       continue;
1170 
1171     reflection->SetAllocatedMessage(message, nullptr, field);
1172   }
1173 }
1174 
1175 inline void TestUtil::ReflectionTester::
SetAllocatedOptionalMessageFieldsToMessageViaReflection(Message * from_message,Message * to_message)1176     SetAllocatedOptionalMessageFieldsToMessageViaReflection(
1177         Message* from_message, Message* to_message) {
1178   EXPECT_EQ(from_message->GetDescriptor(), to_message->GetDescriptor());
1179   const Reflection* from_reflection = from_message->GetReflection();
1180   const Reflection* to_reflection = to_message->GetReflection();
1181 
1182   std::vector<const FieldDescriptor*> fields;
1183   from_reflection->ListFields(*from_message, &fields);
1184 
1185   for (int i = 0; i < fields.size(); ++i) {
1186     const FieldDescriptor* field = fields[i];
1187     if (!field->is_optional() ||
1188         field->cpp_type() != FieldDescriptor::CPPTYPE_MESSAGE)
1189       continue;
1190 
1191     Message* sub_message = from_reflection->ReleaseMessage(from_message, field);
1192     to_reflection->SetAllocatedMessage(to_message, sub_message, field);
1193   }
1194 }
1195 
ExpectMessagesReleasedViaReflection(Message * message,TestUtil::ReflectionTester::MessageReleaseState expected_release_state)1196 inline void TestUtil::ReflectionTester::ExpectMessagesReleasedViaReflection(
1197     Message* message,
1198     TestUtil::ReflectionTester::MessageReleaseState expected_release_state) {
1199   const Reflection* reflection = message->GetReflection();
1200 
1201   static const char* fields[] = {
1202       "optionalgroup",
1203       "optional_nested_message",
1204       "optional_foreign_message",
1205       "optional_import_message",
1206   };
1207   for (int i = 0; i < GOOGLE_ARRAYSIZE(fields); i++) {
1208     Message* released = reflection->ReleaseMessage(message, F(fields[i]));
1209     switch (expected_release_state) {
1210       case IS_NULL:
1211         EXPECT_TRUE(released == nullptr);
1212         break;
1213       case NOT_NULL:
1214         EXPECT_TRUE(released != nullptr);
1215         break;
1216       case CAN_BE_NULL:
1217         break;
1218     }
1219     delete released;
1220     EXPECT_FALSE(reflection->HasField(*message, F(fields[i])));
1221   }
1222 }
1223 
1224 // Check that the passed-in serialization is the canonical serialization we
1225 // expect for a TestFieldOrderings message filled in by
1226 // SetAllFieldsAndExtensions().
ExpectAllFieldsAndExtensionsInOrder(const std::string & serialized)1227 inline void ExpectAllFieldsAndExtensionsInOrder(const std::string& serialized) {
1228   // We set each field individually, serialize separately, and concatenate all
1229   // the strings in canonical order to determine the expected serialization.
1230   std::string expected;
1231   unittest::TestFieldOrderings message;
1232   message.set_my_int(1);  // Field 1.
1233   message.AppendToString(&expected);
1234   message.Clear();
1235   message.SetExtension(unittest::my_extension_int, 23);  // Field 5.
1236   message.AppendToString(&expected);
1237   message.Clear();
1238   message.set_my_string("foo");  // Field 11.
1239   message.AppendToString(&expected);
1240   message.Clear();
1241   message.SetExtension(unittest::my_extension_string, "bar");  // Field 50.
1242   message.AppendToString(&expected);
1243   message.Clear();
1244   message.set_my_float(1.0);  // Field 101.
1245   message.AppendToString(&expected);
1246   message.Clear();
1247 
1248   // We don't EXPECT_EQ() since we don't want to print raw bytes to stdout.
1249   EXPECT_TRUE(serialized == expected);
1250 }
1251 
1252 }  // namespace TestUtil
1253 }  // namespace protobuf
1254 }  // namespace google
1255 
1256 #include <google/protobuf/port_undef.inc>
1257 
1258 #endif  // GOOGLE_PROTOBUF_TEST_UTIL_H__
1259