• 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 #ifdef _WIN32
36 // Verify that #including windows.h does not break anything (e.g. because
37 // windows.h #defines GetMessage() as a macro).
38 #include <windows.h>
39 #endif
40 
41 #include <google/protobuf/test_util.h>
42 #include <google/protobuf/descriptor.h>
43 #include <google/protobuf/message.h>
44 
45 #include <google/protobuf/stubs/logging.h>
46 #include <google/protobuf/stubs/common.h>
47 #include <google/protobuf/testing/googletest.h>
48 #include <gtest/gtest.h>
49 
50 namespace google {
51 namespace protobuf {
52 
SetAllFields(unittest::TestAllTypes * message)53 void TestUtil::SetAllFields(unittest::TestAllTypes* message) {
54   SetOptionalFields(message);
55   AddRepeatedFields1(message);
56   AddRepeatedFields2(message);
57   SetDefaultFields(message);
58   SetOneofFields(message);
59 }
60 
SetOptionalFields(unittest::TestAllTypes * message)61 void TestUtil::SetOptionalFields(unittest::TestAllTypes* message) {
62   message->set_optional_int32   (101);
63   message->set_optional_int64   (102);
64   message->set_optional_uint32  (103);
65   message->set_optional_uint64  (104);
66   message->set_optional_sint32  (105);
67   message->set_optional_sint64  (106);
68   message->set_optional_fixed32 (107);
69   message->set_optional_fixed64 (108);
70   message->set_optional_sfixed32(109);
71   message->set_optional_sfixed64(110);
72   message->set_optional_float   (111);
73   message->set_optional_double  (112);
74   message->set_optional_bool    (true);
75   message->set_optional_string  ("115");
76   message->set_optional_bytes   ("116");
77 
78   message->mutable_optionalgroup                 ()->set_a(117);
79   message->mutable_optional_nested_message       ()->set_bb(118);
80   message->mutable_optional_foreign_message      ()->set_c(119);
81   message->mutable_optional_import_message       ()->set_d(120);
82   message->mutable_optional_public_import_message()->set_e(126);
83   message->mutable_optional_lazy_message         ()->set_bb(127);
84 
85   message->set_optional_nested_enum (unittest::TestAllTypes::BAZ);
86   message->set_optional_foreign_enum(unittest::FOREIGN_BAZ      );
87   message->set_optional_import_enum (unittest_import::IMPORT_BAZ);
88 
89   // StringPiece and Cord fields are only accessible via reflection in the
90   // open source release; see comments in compiler/cpp/string_field.cc.
91 #ifndef PROTOBUF_TEST_NO_DESCRIPTORS
92   message->GetReflection()->SetString(
93     message,
94     message->GetDescriptor()->FindFieldByName("optional_string_piece"),
95     "124");
96   message->GetReflection()->SetString(
97     message,
98     message->GetDescriptor()->FindFieldByName("optional_cord"),
99     "125");
100 #endif  // !PROTOBUF_TEST_NO_DESCRIPTORS
101 }
102 
103 // -------------------------------------------------------------------
104 
AddRepeatedFields1(unittest::TestAllTypes * message)105 void TestUtil::AddRepeatedFields1(unittest::TestAllTypes* message) {
106   message->add_repeated_int32   (201);
107   message->add_repeated_int64   (202);
108   message->add_repeated_uint32  (203);
109   message->add_repeated_uint64  (204);
110   message->add_repeated_sint32  (205);
111   message->add_repeated_sint64  (206);
112   message->add_repeated_fixed32 (207);
113   message->add_repeated_fixed64 (208);
114   message->add_repeated_sfixed32(209);
115   message->add_repeated_sfixed64(210);
116   message->add_repeated_float   (211);
117   message->add_repeated_double  (212);
118   message->add_repeated_bool    (true);
119   message->add_repeated_string  ("215");
120   message->add_repeated_bytes   ("216");
121 
122   message->add_repeatedgroup           ()->set_a(217);
123   message->add_repeated_nested_message ()->set_bb(218);
124   message->add_repeated_foreign_message()->set_c(219);
125   message->add_repeated_import_message ()->set_d(220);
126   message->add_repeated_lazy_message   ()->set_bb(227);
127 
128   message->add_repeated_nested_enum (unittest::TestAllTypes::BAR);
129   message->add_repeated_foreign_enum(unittest::FOREIGN_BAR      );
130   message->add_repeated_import_enum (unittest_import::IMPORT_BAR);
131 
132 #ifndef PROTOBUF_TEST_NO_DESCRIPTORS
133   message->GetReflection()->AddString(
134     message,
135     message->GetDescriptor()->FindFieldByName("repeated_string_piece"),
136     "224");
137   message->GetReflection()->AddString(
138     message,
139     message->GetDescriptor()->FindFieldByName("repeated_cord"),
140     "225");
141 #endif  // !PROTOBUF_TEST_NO_DESCRIPTORS
142 }
143 
AddRepeatedFields2(unittest::TestAllTypes * message)144 void TestUtil::AddRepeatedFields2(unittest::TestAllTypes* message) {
145   // Add a second one of each field.
146   message->add_repeated_int32   (301);
147   message->add_repeated_int64   (302);
148   message->add_repeated_uint32  (303);
149   message->add_repeated_uint64  (304);
150   message->add_repeated_sint32  (305);
151   message->add_repeated_sint64  (306);
152   message->add_repeated_fixed32 (307);
153   message->add_repeated_fixed64 (308);
154   message->add_repeated_sfixed32(309);
155   message->add_repeated_sfixed64(310);
156   message->add_repeated_float   (311);
157   message->add_repeated_double  (312);
158   message->add_repeated_bool    (false);
159   message->add_repeated_string  ("315");
160   message->add_repeated_bytes   ("316");
161 
162   message->add_repeatedgroup           ()->set_a(317);
163   message->add_repeated_nested_message ()->set_bb(318);
164   message->add_repeated_foreign_message()->set_c(319);
165   message->add_repeated_import_message ()->set_d(320);
166   message->add_repeated_lazy_message   ()->set_bb(327);
167 
168   message->add_repeated_nested_enum (unittest::TestAllTypes::BAZ);
169   message->add_repeated_foreign_enum(unittest::FOREIGN_BAZ      );
170   message->add_repeated_import_enum (unittest_import::IMPORT_BAZ);
171 
172 #ifndef PROTOBUF_TEST_NO_DESCRIPTORS
173   message->GetReflection()->AddString(
174     message,
175     message->GetDescriptor()->FindFieldByName("repeated_string_piece"),
176     "324");
177   message->GetReflection()->AddString(
178     message,
179     message->GetDescriptor()->FindFieldByName("repeated_cord"),
180     "325");
181 #endif  // !PROTOBUF_TEST_NO_DESCRIPTORS
182 }
183 
184 // -------------------------------------------------------------------
185 
SetDefaultFields(unittest::TestAllTypes * message)186 void TestUtil::SetDefaultFields(unittest::TestAllTypes* message) {
187   message->set_default_int32   (401);
188   message->set_default_int64   (402);
189   message->set_default_uint32  (403);
190   message->set_default_uint64  (404);
191   message->set_default_sint32  (405);
192   message->set_default_sint64  (406);
193   message->set_default_fixed32 (407);
194   message->set_default_fixed64 (408);
195   message->set_default_sfixed32(409);
196   message->set_default_sfixed64(410);
197   message->set_default_float   (411);
198   message->set_default_double  (412);
199   message->set_default_bool    (false);
200   message->set_default_string  ("415");
201   message->set_default_bytes   ("416");
202 
203   message->set_default_nested_enum (unittest::TestAllTypes::FOO);
204   message->set_default_foreign_enum(unittest::FOREIGN_FOO      );
205   message->set_default_import_enum (unittest_import::IMPORT_FOO);
206 
207 #ifndef PROTOBUF_TEST_NO_DESCRIPTORS
208   message->GetReflection()->SetString(
209     message,
210     message->GetDescriptor()->FindFieldByName("default_string_piece"),
211     "424");
212   message->GetReflection()->SetString(
213     message,
214     message->GetDescriptor()->FindFieldByName("default_cord"),
215     "425");
216 #endif  // !PROTOBUF_TEST_NO_DESCRIPTORS
217 }
218 
219 // -------------------------------------------------------------------
220 
ModifyRepeatedFields(unittest::TestAllTypes * message)221 void TestUtil::ModifyRepeatedFields(unittest::TestAllTypes* message) {
222   message->set_repeated_int32   (1, 501);
223   message->set_repeated_int64   (1, 502);
224   message->set_repeated_uint32  (1, 503);
225   message->set_repeated_uint64  (1, 504);
226   message->set_repeated_sint32  (1, 505);
227   message->set_repeated_sint64  (1, 506);
228   message->set_repeated_fixed32 (1, 507);
229   message->set_repeated_fixed64 (1, 508);
230   message->set_repeated_sfixed32(1, 509);
231   message->set_repeated_sfixed64(1, 510);
232   message->set_repeated_float   (1, 511);
233   message->set_repeated_double  (1, 512);
234   message->set_repeated_bool    (1, true);
235   message->set_repeated_string  (1, "515");
236   message->set_repeated_bytes   (1, "516");
237 
238   message->mutable_repeatedgroup           (1)->set_a(517);
239   message->mutable_repeated_nested_message (1)->set_bb(518);
240   message->mutable_repeated_foreign_message(1)->set_c(519);
241   message->mutable_repeated_import_message (1)->set_d(520);
242   message->mutable_repeated_lazy_message   (1)->set_bb(527);
243 
244   message->set_repeated_nested_enum (1, unittest::TestAllTypes::FOO);
245   message->set_repeated_foreign_enum(1, unittest::FOREIGN_FOO      );
246   message->set_repeated_import_enum (1, unittest_import::IMPORT_FOO);
247 
248 #ifndef PROTOBUF_TEST_NO_DESCRIPTORS
249   message->GetReflection()->SetRepeatedString(
250     message,
251     message->GetDescriptor()->FindFieldByName("repeated_string_piece"),
252     1, "524");
253   message->GetReflection()->SetRepeatedString(
254     message,
255     message->GetDescriptor()->FindFieldByName("repeated_cord"),
256     1, "525");
257 #endif  // !PROTOBUF_TEST_NO_DESCRIPTORS
258 }
259 
260 // ------------------------------------------------------------------
SetOneofFields(unittest::TestAllTypes * message)261 void TestUtil::SetOneofFields(unittest::TestAllTypes* message) {
262   message->set_oneof_uint32(601);
263   message->mutable_oneof_nested_message()->set_bb(602);
264   message->set_oneof_string("603");
265   message->set_oneof_bytes("604");
266 }
267 
268 // -------------------------------------------------------------------
269 
ExpectAllFieldsSet(const unittest::TestAllTypes & message)270 void TestUtil::ExpectAllFieldsSet(const unittest::TestAllTypes& message) {
271   EXPECT_TRUE(message.has_optional_int32   ());
272   EXPECT_TRUE(message.has_optional_int64   ());
273   EXPECT_TRUE(message.has_optional_uint32  ());
274   EXPECT_TRUE(message.has_optional_uint64  ());
275   EXPECT_TRUE(message.has_optional_sint32  ());
276   EXPECT_TRUE(message.has_optional_sint64  ());
277   EXPECT_TRUE(message.has_optional_fixed32 ());
278   EXPECT_TRUE(message.has_optional_fixed64 ());
279   EXPECT_TRUE(message.has_optional_sfixed32());
280   EXPECT_TRUE(message.has_optional_sfixed64());
281   EXPECT_TRUE(message.has_optional_float   ());
282   EXPECT_TRUE(message.has_optional_double  ());
283   EXPECT_TRUE(message.has_optional_bool    ());
284   EXPECT_TRUE(message.has_optional_string  ());
285   EXPECT_TRUE(message.has_optional_bytes   ());
286 
287   EXPECT_TRUE(message.has_optionalgroup                 ());
288   EXPECT_TRUE(message.has_optional_nested_message       ());
289   EXPECT_TRUE(message.has_optional_foreign_message      ());
290   EXPECT_TRUE(message.has_optional_import_message       ());
291   EXPECT_TRUE(message.has_optional_public_import_message());
292   EXPECT_TRUE(message.has_optional_lazy_message         ());
293 
294   EXPECT_TRUE(message.optionalgroup                 ().has_a());
295   EXPECT_TRUE(message.optional_nested_message       ().has_bb());
296   EXPECT_TRUE(message.optional_foreign_message      ().has_c());
297   EXPECT_TRUE(message.optional_import_message       ().has_d());
298   EXPECT_TRUE(message.optional_public_import_message().has_e());
299   EXPECT_TRUE(message.optional_lazy_message         ().has_bb());
300 
301   EXPECT_TRUE(message.has_optional_nested_enum ());
302   EXPECT_TRUE(message.has_optional_foreign_enum());
303   EXPECT_TRUE(message.has_optional_import_enum ());
304 
305 #ifndef PROTOBUF_TEST_NO_DESCRIPTORS
306   EXPECT_TRUE(message.has_optional_string_piece());
307   EXPECT_TRUE(message.has_optional_cord());
308 #endif
309 
310   EXPECT_EQ(101  , message.optional_int32   ());
311   EXPECT_EQ(102  , message.optional_int64   ());
312   EXPECT_EQ(103  , message.optional_uint32  ());
313   EXPECT_EQ(104  , message.optional_uint64  ());
314   EXPECT_EQ(105  , message.optional_sint32  ());
315   EXPECT_EQ(106  , message.optional_sint64  ());
316   EXPECT_EQ(107  , message.optional_fixed32 ());
317   EXPECT_EQ(108  , message.optional_fixed64 ());
318   EXPECT_EQ(109  , message.optional_sfixed32());
319   EXPECT_EQ(110  , message.optional_sfixed64());
320   EXPECT_EQ(111  , message.optional_float   ());
321   EXPECT_EQ(112  , message.optional_double  ());
322   EXPECT_TRUE(     message.optional_bool    ());
323   EXPECT_EQ("115", message.optional_string  ());
324   EXPECT_EQ("116", message.optional_bytes   ());
325 
326   EXPECT_EQ(117, message.optionalgroup                  ().a());
327   EXPECT_EQ(118, message.optional_nested_message        ().bb());
328   EXPECT_EQ(119, message.optional_foreign_message       ().c());
329   EXPECT_EQ(120, message.optional_import_message        ().d());
330   EXPECT_EQ(126, message.optional_public_import_message ().e());
331   EXPECT_EQ(127, message.optional_lazy_message          ().bb());
332 
333   EXPECT_EQ(unittest::TestAllTypes::BAZ, message.optional_nested_enum ());
334   EXPECT_EQ(unittest::FOREIGN_BAZ      , message.optional_foreign_enum());
335   EXPECT_EQ(unittest_import::IMPORT_BAZ, message.optional_import_enum ());
336 
337 
338   // -----------------------------------------------------------------
339 
340   ASSERT_EQ(2, message.repeated_int32_size   ());
341   ASSERT_EQ(2, message.repeated_int64_size   ());
342   ASSERT_EQ(2, message.repeated_uint32_size  ());
343   ASSERT_EQ(2, message.repeated_uint64_size  ());
344   ASSERT_EQ(2, message.repeated_sint32_size  ());
345   ASSERT_EQ(2, message.repeated_sint64_size  ());
346   ASSERT_EQ(2, message.repeated_fixed32_size ());
347   ASSERT_EQ(2, message.repeated_fixed64_size ());
348   ASSERT_EQ(2, message.repeated_sfixed32_size());
349   ASSERT_EQ(2, message.repeated_sfixed64_size());
350   ASSERT_EQ(2, message.repeated_float_size   ());
351   ASSERT_EQ(2, message.repeated_double_size  ());
352   ASSERT_EQ(2, message.repeated_bool_size    ());
353   ASSERT_EQ(2, message.repeated_string_size  ());
354   ASSERT_EQ(2, message.repeated_bytes_size   ());
355 
356   ASSERT_EQ(2, message.repeatedgroup_size           ());
357   ASSERT_EQ(2, message.repeated_nested_message_size ());
358   ASSERT_EQ(2, message.repeated_foreign_message_size());
359   ASSERT_EQ(2, message.repeated_import_message_size ());
360   ASSERT_EQ(2, message.repeated_lazy_message_size   ());
361   ASSERT_EQ(2, message.repeated_nested_enum_size    ());
362   ASSERT_EQ(2, message.repeated_foreign_enum_size   ());
363   ASSERT_EQ(2, message.repeated_import_enum_size    ());
364 
365 #ifndef PROTOBUF_TEST_NO_DESCRIPTORS
366   ASSERT_EQ(2, message.repeated_string_piece_size());
367   ASSERT_EQ(2, message.repeated_cord_size());
368 #endif
369 
370   EXPECT_EQ(201  , message.repeated_int32   (0));
371   EXPECT_EQ(202  , message.repeated_int64   (0));
372   EXPECT_EQ(203  , message.repeated_uint32  (0));
373   EXPECT_EQ(204  , message.repeated_uint64  (0));
374   EXPECT_EQ(205  , message.repeated_sint32  (0));
375   EXPECT_EQ(206  , message.repeated_sint64  (0));
376   EXPECT_EQ(207  , message.repeated_fixed32 (0));
377   EXPECT_EQ(208  , message.repeated_fixed64 (0));
378   EXPECT_EQ(209  , message.repeated_sfixed32(0));
379   EXPECT_EQ(210  , message.repeated_sfixed64(0));
380   EXPECT_EQ(211  , message.repeated_float   (0));
381   EXPECT_EQ(212  , message.repeated_double  (0));
382   EXPECT_TRUE(     message.repeated_bool    (0));
383   EXPECT_EQ("215", message.repeated_string  (0));
384   EXPECT_EQ("216", message.repeated_bytes   (0));
385 
386   EXPECT_EQ(217, message.repeatedgroup           (0).a());
387   EXPECT_EQ(218, message.repeated_nested_message (0).bb());
388   EXPECT_EQ(219, message.repeated_foreign_message(0).c());
389   EXPECT_EQ(220, message.repeated_import_message (0).d());
390   EXPECT_EQ(227, message.repeated_lazy_message   (0).bb());
391 
392 
393   EXPECT_EQ(unittest::TestAllTypes::BAR, message.repeated_nested_enum (0));
394   EXPECT_EQ(unittest::FOREIGN_BAR      , message.repeated_foreign_enum(0));
395   EXPECT_EQ(unittest_import::IMPORT_BAR, message.repeated_import_enum (0));
396 
397   EXPECT_EQ(301  , message.repeated_int32   (1));
398   EXPECT_EQ(302  , message.repeated_int64   (1));
399   EXPECT_EQ(303  , message.repeated_uint32  (1));
400   EXPECT_EQ(304  , message.repeated_uint64  (1));
401   EXPECT_EQ(305  , message.repeated_sint32  (1));
402   EXPECT_EQ(306  , message.repeated_sint64  (1));
403   EXPECT_EQ(307  , message.repeated_fixed32 (1));
404   EXPECT_EQ(308  , message.repeated_fixed64 (1));
405   EXPECT_EQ(309  , message.repeated_sfixed32(1));
406   EXPECT_EQ(310  , message.repeated_sfixed64(1));
407   EXPECT_EQ(311  , message.repeated_float   (1));
408   EXPECT_EQ(312  , message.repeated_double  (1));
409   EXPECT_FALSE(    message.repeated_bool    (1));
410   EXPECT_EQ("315", message.repeated_string  (1));
411   EXPECT_EQ("316", message.repeated_bytes   (1));
412 
413   EXPECT_EQ(317, message.repeatedgroup           (1).a());
414   EXPECT_EQ(318, message.repeated_nested_message (1).bb());
415   EXPECT_EQ(319, message.repeated_foreign_message(1).c());
416   EXPECT_EQ(320, message.repeated_import_message (1).d());
417   EXPECT_EQ(327, message.repeated_lazy_message   (1).bb());
418 
419   EXPECT_EQ(unittest::TestAllTypes::BAZ, message.repeated_nested_enum (1));
420   EXPECT_EQ(unittest::FOREIGN_BAZ      , message.repeated_foreign_enum(1));
421   EXPECT_EQ(unittest_import::IMPORT_BAZ, message.repeated_import_enum (1));
422 
423 
424   // -----------------------------------------------------------------
425 
426   EXPECT_TRUE(message.has_default_int32   ());
427   EXPECT_TRUE(message.has_default_int64   ());
428   EXPECT_TRUE(message.has_default_uint32  ());
429   EXPECT_TRUE(message.has_default_uint64  ());
430   EXPECT_TRUE(message.has_default_sint32  ());
431   EXPECT_TRUE(message.has_default_sint64  ());
432   EXPECT_TRUE(message.has_default_fixed32 ());
433   EXPECT_TRUE(message.has_default_fixed64 ());
434   EXPECT_TRUE(message.has_default_sfixed32());
435   EXPECT_TRUE(message.has_default_sfixed64());
436   EXPECT_TRUE(message.has_default_float   ());
437   EXPECT_TRUE(message.has_default_double  ());
438   EXPECT_TRUE(message.has_default_bool    ());
439   EXPECT_TRUE(message.has_default_string  ());
440   EXPECT_TRUE(message.has_default_bytes   ());
441 
442   EXPECT_TRUE(message.has_default_nested_enum ());
443   EXPECT_TRUE(message.has_default_foreign_enum());
444   EXPECT_TRUE(message.has_default_import_enum ());
445 
446 
447   EXPECT_EQ(401  , message.default_int32   ());
448   EXPECT_EQ(402  , message.default_int64   ());
449   EXPECT_EQ(403  , message.default_uint32  ());
450   EXPECT_EQ(404  , message.default_uint64  ());
451   EXPECT_EQ(405  , message.default_sint32  ());
452   EXPECT_EQ(406  , message.default_sint64  ());
453   EXPECT_EQ(407  , message.default_fixed32 ());
454   EXPECT_EQ(408  , message.default_fixed64 ());
455   EXPECT_EQ(409  , message.default_sfixed32());
456   EXPECT_EQ(410  , message.default_sfixed64());
457   EXPECT_EQ(411  , message.default_float   ());
458   EXPECT_EQ(412  , message.default_double  ());
459   EXPECT_FALSE(    message.default_bool    ());
460   EXPECT_EQ("415", message.default_string  ());
461   EXPECT_EQ("416", message.default_bytes   ());
462 
463   EXPECT_EQ(unittest::TestAllTypes::FOO, message.default_nested_enum ());
464   EXPECT_EQ(unittest::FOREIGN_FOO      , message.default_foreign_enum());
465   EXPECT_EQ(unittest_import::IMPORT_FOO, message.default_import_enum ());
466 
467 
468   EXPECT_FALSE(message.has_oneof_uint32        ());
469   EXPECT_FALSE(message.has_oneof_nested_message());
470   EXPECT_FALSE(message.has_oneof_string        ());
471   EXPECT_TRUE(message.has_oneof_bytes          ());
472 
473   EXPECT_EQ("604", message.oneof_bytes());
474 }
475 
476 // -------------------------------------------------------------------
477 
ExpectClear(const unittest::TestAllTypes & message)478 void TestUtil::ExpectClear(const unittest::TestAllTypes& message) {
479   // has_blah() should initially be false for all optional fields.
480   EXPECT_FALSE(message.has_optional_int32   ());
481   EXPECT_FALSE(message.has_optional_int64   ());
482   EXPECT_FALSE(message.has_optional_uint32  ());
483   EXPECT_FALSE(message.has_optional_uint64  ());
484   EXPECT_FALSE(message.has_optional_sint32  ());
485   EXPECT_FALSE(message.has_optional_sint64  ());
486   EXPECT_FALSE(message.has_optional_fixed32 ());
487   EXPECT_FALSE(message.has_optional_fixed64 ());
488   EXPECT_FALSE(message.has_optional_sfixed32());
489   EXPECT_FALSE(message.has_optional_sfixed64());
490   EXPECT_FALSE(message.has_optional_float   ());
491   EXPECT_FALSE(message.has_optional_double  ());
492   EXPECT_FALSE(message.has_optional_bool    ());
493   EXPECT_FALSE(message.has_optional_string  ());
494   EXPECT_FALSE(message.has_optional_bytes   ());
495 
496   EXPECT_FALSE(message.has_optionalgroup                 ());
497   EXPECT_FALSE(message.has_optional_nested_message       ());
498   EXPECT_FALSE(message.has_optional_foreign_message      ());
499   EXPECT_FALSE(message.has_optional_import_message       ());
500   EXPECT_FALSE(message.has_optional_public_import_message());
501   EXPECT_FALSE(message.has_optional_lazy_message         ());
502 
503   EXPECT_FALSE(message.has_optional_nested_enum ());
504   EXPECT_FALSE(message.has_optional_foreign_enum());
505   EXPECT_FALSE(message.has_optional_import_enum ());
506 
507   EXPECT_FALSE(message.has_optional_string_piece());
508   EXPECT_FALSE(message.has_optional_cord());
509 
510   // Optional fields without defaults are set to zero or something like it.
511   EXPECT_EQ(0    , message.optional_int32   ());
512   EXPECT_EQ(0    , message.optional_int64   ());
513   EXPECT_EQ(0    , message.optional_uint32  ());
514   EXPECT_EQ(0    , message.optional_uint64  ());
515   EXPECT_EQ(0    , message.optional_sint32  ());
516   EXPECT_EQ(0    , message.optional_sint64  ());
517   EXPECT_EQ(0    , message.optional_fixed32 ());
518   EXPECT_EQ(0    , message.optional_fixed64 ());
519   EXPECT_EQ(0    , message.optional_sfixed32());
520   EXPECT_EQ(0    , message.optional_sfixed64());
521   EXPECT_EQ(0    , message.optional_float   ());
522   EXPECT_EQ(0    , message.optional_double  ());
523   EXPECT_FALSE(    message.optional_bool    ());
524   EXPECT_EQ(""   , message.optional_string  ());
525   EXPECT_EQ(""   , message.optional_bytes   ());
526 
527   // Embedded messages should also be clear.
528   EXPECT_FALSE(message.optionalgroup                 ().has_a());
529   EXPECT_FALSE(message.optional_nested_message       ().has_bb());
530   EXPECT_FALSE(message.optional_foreign_message      ().has_c());
531   EXPECT_FALSE(message.optional_import_message       ().has_d());
532   EXPECT_FALSE(message.optional_public_import_message().has_e());
533   EXPECT_FALSE(message.optional_lazy_message         ().has_bb());
534 
535   EXPECT_EQ(0, message.optionalgroup                 ().a());
536   EXPECT_EQ(0, message.optional_nested_message       ().bb());
537   EXPECT_EQ(0, message.optional_foreign_message      ().c());
538   EXPECT_EQ(0, message.optional_import_message       ().d());
539   EXPECT_EQ(0, message.optional_public_import_message().e());
540   EXPECT_EQ(0, message.optional_lazy_message         ().bb());
541 
542   // Enums without defaults are set to the first value in the enum.
543   EXPECT_EQ(unittest::TestAllTypes::FOO, message.optional_nested_enum ());
544   EXPECT_EQ(unittest::FOREIGN_FOO      , message.optional_foreign_enum());
545   EXPECT_EQ(unittest_import::IMPORT_FOO, message.optional_import_enum ());
546 
547 
548   // Repeated fields are empty.
549   EXPECT_EQ(0, message.repeated_int32_size   ());
550   EXPECT_EQ(0, message.repeated_int64_size   ());
551   EXPECT_EQ(0, message.repeated_uint32_size  ());
552   EXPECT_EQ(0, message.repeated_uint64_size  ());
553   EXPECT_EQ(0, message.repeated_sint32_size  ());
554   EXPECT_EQ(0, message.repeated_sint64_size  ());
555   EXPECT_EQ(0, message.repeated_fixed32_size ());
556   EXPECT_EQ(0, message.repeated_fixed64_size ());
557   EXPECT_EQ(0, message.repeated_sfixed32_size());
558   EXPECT_EQ(0, message.repeated_sfixed64_size());
559   EXPECT_EQ(0, message.repeated_float_size   ());
560   EXPECT_EQ(0, message.repeated_double_size  ());
561   EXPECT_EQ(0, message.repeated_bool_size    ());
562   EXPECT_EQ(0, message.repeated_string_size  ());
563   EXPECT_EQ(0, message.repeated_bytes_size   ());
564 
565   EXPECT_EQ(0, message.repeatedgroup_size           ());
566   EXPECT_EQ(0, message.repeated_nested_message_size ());
567   EXPECT_EQ(0, message.repeated_foreign_message_size());
568   EXPECT_EQ(0, message.repeated_import_message_size ());
569   EXPECT_EQ(0, message.repeated_lazy_message_size   ());
570   EXPECT_EQ(0, message.repeated_nested_enum_size    ());
571   EXPECT_EQ(0, message.repeated_foreign_enum_size   ());
572   EXPECT_EQ(0, message.repeated_import_enum_size    ());
573 
574   EXPECT_EQ(0, message.repeated_string_piece_size());
575   EXPECT_EQ(0, message.repeated_cord_size());
576 
577   // has_blah() should also be false for all default fields.
578   EXPECT_FALSE(message.has_default_int32   ());
579   EXPECT_FALSE(message.has_default_int64   ());
580   EXPECT_FALSE(message.has_default_uint32  ());
581   EXPECT_FALSE(message.has_default_uint64  ());
582   EXPECT_FALSE(message.has_default_sint32  ());
583   EXPECT_FALSE(message.has_default_sint64  ());
584   EXPECT_FALSE(message.has_default_fixed32 ());
585   EXPECT_FALSE(message.has_default_fixed64 ());
586   EXPECT_FALSE(message.has_default_sfixed32());
587   EXPECT_FALSE(message.has_default_sfixed64());
588   EXPECT_FALSE(message.has_default_float   ());
589   EXPECT_FALSE(message.has_default_double  ());
590   EXPECT_FALSE(message.has_default_bool    ());
591   EXPECT_FALSE(message.has_default_string  ());
592   EXPECT_FALSE(message.has_default_bytes   ());
593 
594   EXPECT_FALSE(message.has_default_nested_enum ());
595   EXPECT_FALSE(message.has_default_foreign_enum());
596   EXPECT_FALSE(message.has_default_import_enum ());
597 
598 
599   // Fields with defaults have their default values (duh).
600   EXPECT_EQ( 41    , message.default_int32   ());
601   EXPECT_EQ( 42    , message.default_int64   ());
602   EXPECT_EQ( 43    , message.default_uint32  ());
603   EXPECT_EQ( 44    , message.default_uint64  ());
604   EXPECT_EQ(-45    , message.default_sint32  ());
605   EXPECT_EQ( 46    , message.default_sint64  ());
606   EXPECT_EQ( 47    , message.default_fixed32 ());
607   EXPECT_EQ( 48    , message.default_fixed64 ());
608   EXPECT_EQ( 49    , message.default_sfixed32());
609   EXPECT_EQ(-50    , message.default_sfixed64());
610   EXPECT_EQ( 51.5  , message.default_float   ());
611   EXPECT_EQ( 52e3  , message.default_double  ());
612   EXPECT_TRUE(       message.default_bool    ());
613   EXPECT_EQ("hello", message.default_string  ());
614   EXPECT_EQ("world", message.default_bytes   ());
615 
616   EXPECT_EQ(unittest::TestAllTypes::BAR, message.default_nested_enum ());
617   EXPECT_EQ(unittest::FOREIGN_BAR      , message.default_foreign_enum());
618   EXPECT_EQ(unittest_import::IMPORT_BAR, message.default_import_enum ());
619 
620 
621   EXPECT_FALSE(message.has_oneof_uint32        ());
622   EXPECT_FALSE(message.has_oneof_nested_message());
623   EXPECT_FALSE(message.has_oneof_string        ());
624   EXPECT_FALSE(message.has_oneof_bytes         ());
625 }
626 
627 // -------------------------------------------------------------------
628 
ExpectRepeatedFieldsModified(const unittest::TestAllTypes & message)629 void TestUtil::ExpectRepeatedFieldsModified(
630     const unittest::TestAllTypes& message) {
631   // ModifyRepeatedFields only sets the second repeated element of each
632   // field.  In addition to verifying this, we also verify that the first
633   // element and size were *not* modified.
634   ASSERT_EQ(2, message.repeated_int32_size   ());
635   ASSERT_EQ(2, message.repeated_int64_size   ());
636   ASSERT_EQ(2, message.repeated_uint32_size  ());
637   ASSERT_EQ(2, message.repeated_uint64_size  ());
638   ASSERT_EQ(2, message.repeated_sint32_size  ());
639   ASSERT_EQ(2, message.repeated_sint64_size  ());
640   ASSERT_EQ(2, message.repeated_fixed32_size ());
641   ASSERT_EQ(2, message.repeated_fixed64_size ());
642   ASSERT_EQ(2, message.repeated_sfixed32_size());
643   ASSERT_EQ(2, message.repeated_sfixed64_size());
644   ASSERT_EQ(2, message.repeated_float_size   ());
645   ASSERT_EQ(2, message.repeated_double_size  ());
646   ASSERT_EQ(2, message.repeated_bool_size    ());
647   ASSERT_EQ(2, message.repeated_string_size  ());
648   ASSERT_EQ(2, message.repeated_bytes_size   ());
649 
650   ASSERT_EQ(2, message.repeatedgroup_size           ());
651   ASSERT_EQ(2, message.repeated_nested_message_size ());
652   ASSERT_EQ(2, message.repeated_foreign_message_size());
653   ASSERT_EQ(2, message.repeated_import_message_size ());
654   ASSERT_EQ(2, message.repeated_lazy_message_size   ());
655   ASSERT_EQ(2, message.repeated_nested_enum_size    ());
656   ASSERT_EQ(2, message.repeated_foreign_enum_size   ());
657   ASSERT_EQ(2, message.repeated_import_enum_size    ());
658 
659 #ifndef PROTOBUF_TEST_NO_DESCRIPTORS
660   ASSERT_EQ(2, message.repeated_string_piece_size());
661   ASSERT_EQ(2, message.repeated_cord_size());
662 #endif
663 
664   EXPECT_EQ(201  , message.repeated_int32   (0));
665   EXPECT_EQ(202  , message.repeated_int64   (0));
666   EXPECT_EQ(203  , message.repeated_uint32  (0));
667   EXPECT_EQ(204  , message.repeated_uint64  (0));
668   EXPECT_EQ(205  , message.repeated_sint32  (0));
669   EXPECT_EQ(206  , message.repeated_sint64  (0));
670   EXPECT_EQ(207  , message.repeated_fixed32 (0));
671   EXPECT_EQ(208  , message.repeated_fixed64 (0));
672   EXPECT_EQ(209  , message.repeated_sfixed32(0));
673   EXPECT_EQ(210  , message.repeated_sfixed64(0));
674   EXPECT_EQ(211  , message.repeated_float   (0));
675   EXPECT_EQ(212  , message.repeated_double  (0));
676   EXPECT_TRUE(     message.repeated_bool    (0));
677   EXPECT_EQ("215", message.repeated_string  (0));
678   EXPECT_EQ("216", message.repeated_bytes   (0));
679 
680   EXPECT_EQ(217, message.repeatedgroup           (0).a());
681   EXPECT_EQ(218, message.repeated_nested_message (0).bb());
682   EXPECT_EQ(219, message.repeated_foreign_message(0).c());
683   EXPECT_EQ(220, message.repeated_import_message (0).d());
684   EXPECT_EQ(227, message.repeated_lazy_message   (0).bb());
685 
686   EXPECT_EQ(unittest::TestAllTypes::BAR, message.repeated_nested_enum (0));
687   EXPECT_EQ(unittest::FOREIGN_BAR      , message.repeated_foreign_enum(0));
688   EXPECT_EQ(unittest_import::IMPORT_BAR, message.repeated_import_enum (0));
689 
690 
691   // Actually verify the second (modified) elements now.
692   EXPECT_EQ(501  , message.repeated_int32   (1));
693   EXPECT_EQ(502  , message.repeated_int64   (1));
694   EXPECT_EQ(503  , message.repeated_uint32  (1));
695   EXPECT_EQ(504  , message.repeated_uint64  (1));
696   EXPECT_EQ(505  , message.repeated_sint32  (1));
697   EXPECT_EQ(506  , message.repeated_sint64  (1));
698   EXPECT_EQ(507  , message.repeated_fixed32 (1));
699   EXPECT_EQ(508  , message.repeated_fixed64 (1));
700   EXPECT_EQ(509  , message.repeated_sfixed32(1));
701   EXPECT_EQ(510  , message.repeated_sfixed64(1));
702   EXPECT_EQ(511  , message.repeated_float   (1));
703   EXPECT_EQ(512  , message.repeated_double  (1));
704   EXPECT_TRUE(     message.repeated_bool    (1));
705   EXPECT_EQ("515", message.repeated_string  (1));
706   EXPECT_EQ("516", message.repeated_bytes   (1));
707 
708   EXPECT_EQ(517, message.repeatedgroup           (1).a());
709   EXPECT_EQ(518, message.repeated_nested_message (1).bb());
710   EXPECT_EQ(519, message.repeated_foreign_message(1).c());
711   EXPECT_EQ(520, message.repeated_import_message (1).d());
712   EXPECT_EQ(527, message.repeated_lazy_message   (1).bb());
713 
714   EXPECT_EQ(unittest::TestAllTypes::FOO, message.repeated_nested_enum (1));
715   EXPECT_EQ(unittest::FOREIGN_FOO      , message.repeated_foreign_enum(1));
716   EXPECT_EQ(unittest_import::IMPORT_FOO, message.repeated_import_enum (1));
717 
718 }
719 
720 // -------------------------------------------------------------------
721 
SetPackedFields(unittest::TestPackedTypes * message)722 void TestUtil::SetPackedFields(unittest::TestPackedTypes* message) {
723   message->add_packed_int32   (601);
724   message->add_packed_int64   (602);
725   message->add_packed_uint32  (603);
726   message->add_packed_uint64  (604);
727   message->add_packed_sint32  (605);
728   message->add_packed_sint64  (606);
729   message->add_packed_fixed32 (607);
730   message->add_packed_fixed64 (608);
731   message->add_packed_sfixed32(609);
732   message->add_packed_sfixed64(610);
733   message->add_packed_float   (611);
734   message->add_packed_double  (612);
735   message->add_packed_bool    (true);
736   message->add_packed_enum    (unittest::FOREIGN_BAR);
737   // add a second one of each field
738   message->add_packed_int32   (701);
739   message->add_packed_int64   (702);
740   message->add_packed_uint32  (703);
741   message->add_packed_uint64  (704);
742   message->add_packed_sint32  (705);
743   message->add_packed_sint64  (706);
744   message->add_packed_fixed32 (707);
745   message->add_packed_fixed64 (708);
746   message->add_packed_sfixed32(709);
747   message->add_packed_sfixed64(710);
748   message->add_packed_float   (711);
749   message->add_packed_double  (712);
750   message->add_packed_bool    (false);
751   message->add_packed_enum    (unittest::FOREIGN_BAZ);
752 }
753 
SetUnpackedFields(unittest::TestUnpackedTypes * message)754 void TestUtil::SetUnpackedFields(unittest::TestUnpackedTypes* message) {
755   // The values applied here must match those of SetPackedFields.
756 
757   message->add_unpacked_int32   (601);
758   message->add_unpacked_int64   (602);
759   message->add_unpacked_uint32  (603);
760   message->add_unpacked_uint64  (604);
761   message->add_unpacked_sint32  (605);
762   message->add_unpacked_sint64  (606);
763   message->add_unpacked_fixed32 (607);
764   message->add_unpacked_fixed64 (608);
765   message->add_unpacked_sfixed32(609);
766   message->add_unpacked_sfixed64(610);
767   message->add_unpacked_float   (611);
768   message->add_unpacked_double  (612);
769   message->add_unpacked_bool    (true);
770   message->add_unpacked_enum    (unittest::FOREIGN_BAR);
771   // add a second one of each field
772   message->add_unpacked_int32   (701);
773   message->add_unpacked_int64   (702);
774   message->add_unpacked_uint32  (703);
775   message->add_unpacked_uint64  (704);
776   message->add_unpacked_sint32  (705);
777   message->add_unpacked_sint64  (706);
778   message->add_unpacked_fixed32 (707);
779   message->add_unpacked_fixed64 (708);
780   message->add_unpacked_sfixed32(709);
781   message->add_unpacked_sfixed64(710);
782   message->add_unpacked_float   (711);
783   message->add_unpacked_double  (712);
784   message->add_unpacked_bool    (false);
785   message->add_unpacked_enum    (unittest::FOREIGN_BAZ);
786 }
787 
788 // -------------------------------------------------------------------
789 
ModifyPackedFields(unittest::TestPackedTypes * message)790 void TestUtil::ModifyPackedFields(unittest::TestPackedTypes* message) {
791   message->set_packed_int32   (1, 801);
792   message->set_packed_int64   (1, 802);
793   message->set_packed_uint32  (1, 803);
794   message->set_packed_uint64  (1, 804);
795   message->set_packed_sint32  (1, 805);
796   message->set_packed_sint64  (1, 806);
797   message->set_packed_fixed32 (1, 807);
798   message->set_packed_fixed64 (1, 808);
799   message->set_packed_sfixed32(1, 809);
800   message->set_packed_sfixed64(1, 810);
801   message->set_packed_float   (1, 811);
802   message->set_packed_double  (1, 812);
803   message->set_packed_bool    (1, true);
804   message->set_packed_enum    (1, unittest::FOREIGN_FOO);
805 }
806 
807 // -------------------------------------------------------------------
808 
ExpectPackedFieldsSet(const unittest::TestPackedTypes & message)809 void TestUtil::ExpectPackedFieldsSet(const unittest::TestPackedTypes& message) {
810   ASSERT_EQ(2, message.packed_int32_size   ());
811   ASSERT_EQ(2, message.packed_int64_size   ());
812   ASSERT_EQ(2, message.packed_uint32_size  ());
813   ASSERT_EQ(2, message.packed_uint64_size  ());
814   ASSERT_EQ(2, message.packed_sint32_size  ());
815   ASSERT_EQ(2, message.packed_sint64_size  ());
816   ASSERT_EQ(2, message.packed_fixed32_size ());
817   ASSERT_EQ(2, message.packed_fixed64_size ());
818   ASSERT_EQ(2, message.packed_sfixed32_size());
819   ASSERT_EQ(2, message.packed_sfixed64_size());
820   ASSERT_EQ(2, message.packed_float_size   ());
821   ASSERT_EQ(2, message.packed_double_size  ());
822   ASSERT_EQ(2, message.packed_bool_size    ());
823   ASSERT_EQ(2, message.packed_enum_size    ());
824 
825   EXPECT_EQ(601  , message.packed_int32   (0));
826   EXPECT_EQ(602  , message.packed_int64   (0));
827   EXPECT_EQ(603  , message.packed_uint32  (0));
828   EXPECT_EQ(604  , message.packed_uint64  (0));
829   EXPECT_EQ(605  , message.packed_sint32  (0));
830   EXPECT_EQ(606  , message.packed_sint64  (0));
831   EXPECT_EQ(607  , message.packed_fixed32 (0));
832   EXPECT_EQ(608  , message.packed_fixed64 (0));
833   EXPECT_EQ(609  , message.packed_sfixed32(0));
834   EXPECT_EQ(610  , message.packed_sfixed64(0));
835   EXPECT_EQ(611  , message.packed_float   (0));
836   EXPECT_EQ(612  , message.packed_double  (0));
837   EXPECT_TRUE(     message.packed_bool    (0));
838   EXPECT_EQ(unittest::FOREIGN_BAR, message.packed_enum(0));
839 
840   EXPECT_EQ(701  , message.packed_int32   (1));
841   EXPECT_EQ(702  , message.packed_int64   (1));
842   EXPECT_EQ(703  , message.packed_uint32  (1));
843   EXPECT_EQ(704  , message.packed_uint64  (1));
844   EXPECT_EQ(705  , message.packed_sint32  (1));
845   EXPECT_EQ(706  , message.packed_sint64  (1));
846   EXPECT_EQ(707  , message.packed_fixed32 (1));
847   EXPECT_EQ(708  , message.packed_fixed64 (1));
848   EXPECT_EQ(709  , message.packed_sfixed32(1));
849   EXPECT_EQ(710  , message.packed_sfixed64(1));
850   EXPECT_EQ(711  , message.packed_float   (1));
851   EXPECT_EQ(712  , message.packed_double  (1));
852   EXPECT_FALSE(    message.packed_bool    (1));
853   EXPECT_EQ(unittest::FOREIGN_BAZ, message.packed_enum(1));
854 }
855 
ExpectUnpackedFieldsSet(const unittest::TestUnpackedTypes & message)856 void TestUtil::ExpectUnpackedFieldsSet(
857     const unittest::TestUnpackedTypes& message) {
858   // The values expected here must match those of ExpectPackedFieldsSet.
859 
860   ASSERT_EQ(2, message.unpacked_int32_size   ());
861   ASSERT_EQ(2, message.unpacked_int64_size   ());
862   ASSERT_EQ(2, message.unpacked_uint32_size  ());
863   ASSERT_EQ(2, message.unpacked_uint64_size  ());
864   ASSERT_EQ(2, message.unpacked_sint32_size  ());
865   ASSERT_EQ(2, message.unpacked_sint64_size  ());
866   ASSERT_EQ(2, message.unpacked_fixed32_size ());
867   ASSERT_EQ(2, message.unpacked_fixed64_size ());
868   ASSERT_EQ(2, message.unpacked_sfixed32_size());
869   ASSERT_EQ(2, message.unpacked_sfixed64_size());
870   ASSERT_EQ(2, message.unpacked_float_size   ());
871   ASSERT_EQ(2, message.unpacked_double_size  ());
872   ASSERT_EQ(2, message.unpacked_bool_size    ());
873   ASSERT_EQ(2, message.unpacked_enum_size    ());
874 
875   EXPECT_EQ(601  , message.unpacked_int32   (0));
876   EXPECT_EQ(602  , message.unpacked_int64   (0));
877   EXPECT_EQ(603  , message.unpacked_uint32  (0));
878   EXPECT_EQ(604  , message.unpacked_uint64  (0));
879   EXPECT_EQ(605  , message.unpacked_sint32  (0));
880   EXPECT_EQ(606  , message.unpacked_sint64  (0));
881   EXPECT_EQ(607  , message.unpacked_fixed32 (0));
882   EXPECT_EQ(608  , message.unpacked_fixed64 (0));
883   EXPECT_EQ(609  , message.unpacked_sfixed32(0));
884   EXPECT_EQ(610  , message.unpacked_sfixed64(0));
885   EXPECT_EQ(611  , message.unpacked_float   (0));
886   EXPECT_EQ(612  , message.unpacked_double  (0));
887   EXPECT_TRUE(     message.unpacked_bool    (0));
888   EXPECT_EQ(unittest::FOREIGN_BAR, message.unpacked_enum(0));
889 
890   EXPECT_EQ(701  , message.unpacked_int32   (1));
891   EXPECT_EQ(702  , message.unpacked_int64   (1));
892   EXPECT_EQ(703  , message.unpacked_uint32  (1));
893   EXPECT_EQ(704  , message.unpacked_uint64  (1));
894   EXPECT_EQ(705  , message.unpacked_sint32  (1));
895   EXPECT_EQ(706  , message.unpacked_sint64  (1));
896   EXPECT_EQ(707  , message.unpacked_fixed32 (1));
897   EXPECT_EQ(708  , message.unpacked_fixed64 (1));
898   EXPECT_EQ(709  , message.unpacked_sfixed32(1));
899   EXPECT_EQ(710  , message.unpacked_sfixed64(1));
900   EXPECT_EQ(711  , message.unpacked_float   (1));
901   EXPECT_EQ(712  , message.unpacked_double  (1));
902   EXPECT_FALSE(    message.unpacked_bool    (1));
903   EXPECT_EQ(unittest::FOREIGN_BAZ, message.unpacked_enum(1));
904 }
905 
906 // -------------------------------------------------------------------
907 
ExpectPackedClear(const unittest::TestPackedTypes & message)908 void TestUtil::ExpectPackedClear(
909     const unittest::TestPackedTypes& message) {
910   // Packed repeated fields are empty.
911   EXPECT_EQ(0, message.packed_int32_size   ());
912   EXPECT_EQ(0, message.packed_int64_size   ());
913   EXPECT_EQ(0, message.packed_uint32_size  ());
914   EXPECT_EQ(0, message.packed_uint64_size  ());
915   EXPECT_EQ(0, message.packed_sint32_size  ());
916   EXPECT_EQ(0, message.packed_sint64_size  ());
917   EXPECT_EQ(0, message.packed_fixed32_size ());
918   EXPECT_EQ(0, message.packed_fixed64_size ());
919   EXPECT_EQ(0, message.packed_sfixed32_size());
920   EXPECT_EQ(0, message.packed_sfixed64_size());
921   EXPECT_EQ(0, message.packed_float_size   ());
922   EXPECT_EQ(0, message.packed_double_size  ());
923   EXPECT_EQ(0, message.packed_bool_size    ());
924   EXPECT_EQ(0, message.packed_enum_size    ());
925 }
926 
927 // -------------------------------------------------------------------
928 
ExpectPackedFieldsModified(const unittest::TestPackedTypes & message)929 void TestUtil::ExpectPackedFieldsModified(
930     const unittest::TestPackedTypes& message) {
931   // Do the same for packed repeated fields.
932   ASSERT_EQ(2, message.packed_int32_size   ());
933   ASSERT_EQ(2, message.packed_int64_size   ());
934   ASSERT_EQ(2, message.packed_uint32_size  ());
935   ASSERT_EQ(2, message.packed_uint64_size  ());
936   ASSERT_EQ(2, message.packed_sint32_size  ());
937   ASSERT_EQ(2, message.packed_sint64_size  ());
938   ASSERT_EQ(2, message.packed_fixed32_size ());
939   ASSERT_EQ(2, message.packed_fixed64_size ());
940   ASSERT_EQ(2, message.packed_sfixed32_size());
941   ASSERT_EQ(2, message.packed_sfixed64_size());
942   ASSERT_EQ(2, message.packed_float_size   ());
943   ASSERT_EQ(2, message.packed_double_size  ());
944   ASSERT_EQ(2, message.packed_bool_size    ());
945   ASSERT_EQ(2, message.packed_enum_size    ());
946 
947   EXPECT_EQ(601  , message.packed_int32   (0));
948   EXPECT_EQ(602  , message.packed_int64   (0));
949   EXPECT_EQ(603  , message.packed_uint32  (0));
950   EXPECT_EQ(604  , message.packed_uint64  (0));
951   EXPECT_EQ(605  , message.packed_sint32  (0));
952   EXPECT_EQ(606  , message.packed_sint64  (0));
953   EXPECT_EQ(607  , message.packed_fixed32 (0));
954   EXPECT_EQ(608  , message.packed_fixed64 (0));
955   EXPECT_EQ(609  , message.packed_sfixed32(0));
956   EXPECT_EQ(610  , message.packed_sfixed64(0));
957   EXPECT_EQ(611  , message.packed_float   (0));
958   EXPECT_EQ(612  , message.packed_double  (0));
959   EXPECT_TRUE(     message.packed_bool    (0));
960   EXPECT_EQ(unittest::FOREIGN_BAR, message.packed_enum(0));
961   // Actually verify the second (modified) elements now.
962   EXPECT_EQ(801  , message.packed_int32   (1));
963   EXPECT_EQ(802  , message.packed_int64   (1));
964   EXPECT_EQ(803  , message.packed_uint32  (1));
965   EXPECT_EQ(804  , message.packed_uint64  (1));
966   EXPECT_EQ(805  , message.packed_sint32  (1));
967   EXPECT_EQ(806  , message.packed_sint64  (1));
968   EXPECT_EQ(807  , message.packed_fixed32 (1));
969   EXPECT_EQ(808  , message.packed_fixed64 (1));
970   EXPECT_EQ(809  , message.packed_sfixed32(1));
971   EXPECT_EQ(810  , message.packed_sfixed64(1));
972   EXPECT_EQ(811  , message.packed_float   (1));
973   EXPECT_EQ(812  , message.packed_double  (1));
974   EXPECT_TRUE(     message.packed_bool    (1));
975   EXPECT_EQ(unittest::FOREIGN_FOO, message.packed_enum(1));
976 }
977 
978 // ===================================================================
979 // Extensions
980 //
981 // All this code is exactly equivalent to the above code except that it's
982 // manipulating extension fields instead of normal ones.
983 //
984 // I gave up on the 80-char limit here.  Sorry.
985 
SetAllExtensions(unittest::TestAllExtensions * message)986 void TestUtil::SetAllExtensions(unittest::TestAllExtensions* message) {
987   message->SetExtension(unittest::optional_int32_extension   , 101);
988   message->SetExtension(unittest::optional_int64_extension   , 102);
989   message->SetExtension(unittest::optional_uint32_extension  , 103);
990   message->SetExtension(unittest::optional_uint64_extension  , 104);
991   message->SetExtension(unittest::optional_sint32_extension  , 105);
992   message->SetExtension(unittest::optional_sint64_extension  , 106);
993   message->SetExtension(unittest::optional_fixed32_extension , 107);
994   message->SetExtension(unittest::optional_fixed64_extension , 108);
995   message->SetExtension(unittest::optional_sfixed32_extension, 109);
996   message->SetExtension(unittest::optional_sfixed64_extension, 110);
997   message->SetExtension(unittest::optional_float_extension   , 111);
998   message->SetExtension(unittest::optional_double_extension  , 112);
999   message->SetExtension(unittest::optional_bool_extension    , true);
1000   message->SetExtension(unittest::optional_string_extension  , "115");
1001   message->SetExtension(unittest::optional_bytes_extension   , "116");
1002 
1003   message->MutableExtension(unittest::optionalgroup_extension           )->set_a(117);
1004   message->MutableExtension(unittest::optional_nested_message_extension )->set_bb(118);
1005   message->MutableExtension(unittest::optional_foreign_message_extension)->set_c(119);
1006   message->MutableExtension(unittest::optional_import_message_extension )->set_d(120);
1007 
1008   message->SetExtension(unittest::optional_nested_enum_extension , unittest::TestAllTypes::BAZ);
1009   message->SetExtension(unittest::optional_foreign_enum_extension, unittest::FOREIGN_BAZ      );
1010   message->SetExtension(unittest::optional_import_enum_extension , unittest_import::IMPORT_BAZ);
1011 
1012   message->SetExtension(unittest::optional_string_piece_extension, "124");
1013   message->SetExtension(unittest::optional_cord_extension, "125");
1014 
1015   message->MutableExtension(unittest::optional_public_import_message_extension)->set_e(126);
1016   message->MutableExtension(unittest::optional_lazy_message_extension)->set_bb(127);
1017 
1018   // -----------------------------------------------------------------
1019 
1020   message->AddExtension(unittest::repeated_int32_extension   , 201);
1021   message->AddExtension(unittest::repeated_int64_extension   , 202);
1022   message->AddExtension(unittest::repeated_uint32_extension  , 203);
1023   message->AddExtension(unittest::repeated_uint64_extension  , 204);
1024   message->AddExtension(unittest::repeated_sint32_extension  , 205);
1025   message->AddExtension(unittest::repeated_sint64_extension  , 206);
1026   message->AddExtension(unittest::repeated_fixed32_extension , 207);
1027   message->AddExtension(unittest::repeated_fixed64_extension , 208);
1028   message->AddExtension(unittest::repeated_sfixed32_extension, 209);
1029   message->AddExtension(unittest::repeated_sfixed64_extension, 210);
1030   message->AddExtension(unittest::repeated_float_extension   , 211);
1031   message->AddExtension(unittest::repeated_double_extension  , 212);
1032   message->AddExtension(unittest::repeated_bool_extension    , true);
1033   message->AddExtension(unittest::repeated_string_extension  , "215");
1034   message->AddExtension(unittest::repeated_bytes_extension   , "216");
1035 
1036   message->AddExtension(unittest::repeatedgroup_extension           )->set_a(217);
1037   message->AddExtension(unittest::repeated_nested_message_extension )->set_bb(218);
1038   message->AddExtension(unittest::repeated_foreign_message_extension)->set_c(219);
1039   message->AddExtension(unittest::repeated_import_message_extension )->set_d(220);
1040   message->AddExtension(unittest::repeated_lazy_message_extension   )->set_bb(227);
1041 
1042   message->AddExtension(unittest::repeated_nested_enum_extension , unittest::TestAllTypes::BAR);
1043   message->AddExtension(unittest::repeated_foreign_enum_extension, unittest::FOREIGN_BAR      );
1044   message->AddExtension(unittest::repeated_import_enum_extension , unittest_import::IMPORT_BAR);
1045 
1046   message->AddExtension(unittest::repeated_string_piece_extension, "224");
1047   message->AddExtension(unittest::repeated_cord_extension, "225");
1048 
1049   // Add a second one of each field.
1050   message->AddExtension(unittest::repeated_int32_extension   , 301);
1051   message->AddExtension(unittest::repeated_int64_extension   , 302);
1052   message->AddExtension(unittest::repeated_uint32_extension  , 303);
1053   message->AddExtension(unittest::repeated_uint64_extension  , 304);
1054   message->AddExtension(unittest::repeated_sint32_extension  , 305);
1055   message->AddExtension(unittest::repeated_sint64_extension  , 306);
1056   message->AddExtension(unittest::repeated_fixed32_extension , 307);
1057   message->AddExtension(unittest::repeated_fixed64_extension , 308);
1058   message->AddExtension(unittest::repeated_sfixed32_extension, 309);
1059   message->AddExtension(unittest::repeated_sfixed64_extension, 310);
1060   message->AddExtension(unittest::repeated_float_extension   , 311);
1061   message->AddExtension(unittest::repeated_double_extension  , 312);
1062   message->AddExtension(unittest::repeated_bool_extension    , false);
1063   message->AddExtension(unittest::repeated_string_extension  , "315");
1064   message->AddExtension(unittest::repeated_bytes_extension   , "316");
1065 
1066   message->AddExtension(unittest::repeatedgroup_extension           )->set_a(317);
1067   message->AddExtension(unittest::repeated_nested_message_extension )->set_bb(318);
1068   message->AddExtension(unittest::repeated_foreign_message_extension)->set_c(319);
1069   message->AddExtension(unittest::repeated_import_message_extension )->set_d(320);
1070   message->AddExtension(unittest::repeated_lazy_message_extension   )->set_bb(327);
1071 
1072   message->AddExtension(unittest::repeated_nested_enum_extension , unittest::TestAllTypes::BAZ);
1073   message->AddExtension(unittest::repeated_foreign_enum_extension, unittest::FOREIGN_BAZ      );
1074   message->AddExtension(unittest::repeated_import_enum_extension , unittest_import::IMPORT_BAZ);
1075 
1076   message->AddExtension(unittest::repeated_string_piece_extension, "324");
1077   message->AddExtension(unittest::repeated_cord_extension, "325");
1078 
1079   // -----------------------------------------------------------------
1080 
1081   message->SetExtension(unittest::default_int32_extension   , 401);
1082   message->SetExtension(unittest::default_int64_extension   , 402);
1083   message->SetExtension(unittest::default_uint32_extension  , 403);
1084   message->SetExtension(unittest::default_uint64_extension  , 404);
1085   message->SetExtension(unittest::default_sint32_extension  , 405);
1086   message->SetExtension(unittest::default_sint64_extension  , 406);
1087   message->SetExtension(unittest::default_fixed32_extension , 407);
1088   message->SetExtension(unittest::default_fixed64_extension , 408);
1089   message->SetExtension(unittest::default_sfixed32_extension, 409);
1090   message->SetExtension(unittest::default_sfixed64_extension, 410);
1091   message->SetExtension(unittest::default_float_extension   , 411);
1092   message->SetExtension(unittest::default_double_extension  , 412);
1093   message->SetExtension(unittest::default_bool_extension    , false);
1094   message->SetExtension(unittest::default_string_extension  , "415");
1095   message->SetExtension(unittest::default_bytes_extension   , "416");
1096 
1097   message->SetExtension(unittest::default_nested_enum_extension , unittest::TestAllTypes::FOO);
1098   message->SetExtension(unittest::default_foreign_enum_extension, unittest::FOREIGN_FOO      );
1099   message->SetExtension(unittest::default_import_enum_extension , unittest_import::IMPORT_FOO);
1100 
1101   message->SetExtension(unittest::default_string_piece_extension, "424");
1102   message->SetExtension(unittest::default_cord_extension, "425");
1103 
1104   SetOneofFields(message);
1105 }
1106 
SetOneofFields(unittest::TestAllExtensions * message)1107 void TestUtil::SetOneofFields(unittest::TestAllExtensions* message) {
1108   message->SetExtension(unittest::oneof_uint32_extension, 601);
1109   message->MutableExtension(unittest::oneof_nested_message_extension)->set_bb(602);
1110   message->SetExtension(unittest::oneof_string_extension, "603");
1111   message->SetExtension(unittest::oneof_bytes_extension, "604");
1112 }
1113 
1114 // -------------------------------------------------------------------
1115 
SetAllFieldsAndExtensions(unittest::TestFieldOrderings * message)1116 void TestUtil::SetAllFieldsAndExtensions(
1117     unittest::TestFieldOrderings* message) {
1118   GOOGLE_CHECK(message);
1119   message->set_my_int(1);
1120   message->set_my_string("foo");
1121   message->set_my_float(1.0);
1122   message->SetExtension(unittest::my_extension_int, 23);
1123   message->SetExtension(unittest::my_extension_string, "bar");
1124 }
1125 
1126 // -------------------------------------------------------------------
1127 
ModifyRepeatedExtensions(unittest::TestAllExtensions * message)1128 void TestUtil::ModifyRepeatedExtensions(unittest::TestAllExtensions* message) {
1129   message->SetExtension(unittest::repeated_int32_extension   , 1, 501);
1130   message->SetExtension(unittest::repeated_int64_extension   , 1, 502);
1131   message->SetExtension(unittest::repeated_uint32_extension  , 1, 503);
1132   message->SetExtension(unittest::repeated_uint64_extension  , 1, 504);
1133   message->SetExtension(unittest::repeated_sint32_extension  , 1, 505);
1134   message->SetExtension(unittest::repeated_sint64_extension  , 1, 506);
1135   message->SetExtension(unittest::repeated_fixed32_extension , 1, 507);
1136   message->SetExtension(unittest::repeated_fixed64_extension , 1, 508);
1137   message->SetExtension(unittest::repeated_sfixed32_extension, 1, 509);
1138   message->SetExtension(unittest::repeated_sfixed64_extension, 1, 510);
1139   message->SetExtension(unittest::repeated_float_extension   , 1, 511);
1140   message->SetExtension(unittest::repeated_double_extension  , 1, 512);
1141   message->SetExtension(unittest::repeated_bool_extension    , 1, true);
1142   message->SetExtension(unittest::repeated_string_extension  , 1, "515");
1143   message->SetExtension(unittest::repeated_bytes_extension   , 1, "516");
1144 
1145   message->MutableExtension(unittest::repeatedgroup_extension           , 1)->set_a(517);
1146   message->MutableExtension(unittest::repeated_nested_message_extension , 1)->set_bb(518);
1147   message->MutableExtension(unittest::repeated_foreign_message_extension, 1)->set_c(519);
1148   message->MutableExtension(unittest::repeated_import_message_extension , 1)->set_d(520);
1149   message->MutableExtension(unittest::repeated_lazy_message_extension   , 1)->set_bb(527);
1150 
1151   message->SetExtension(unittest::repeated_nested_enum_extension , 1, unittest::TestAllTypes::FOO);
1152   message->SetExtension(unittest::repeated_foreign_enum_extension, 1, unittest::FOREIGN_FOO      );
1153   message->SetExtension(unittest::repeated_import_enum_extension , 1, unittest_import::IMPORT_FOO);
1154 
1155   message->SetExtension(unittest::repeated_string_piece_extension, 1, "524");
1156   message->SetExtension(unittest::repeated_cord_extension, 1, "525");
1157 }
1158 
1159 // -------------------------------------------------------------------
1160 
ExpectAllExtensionsSet(const unittest::TestAllExtensions & message)1161 void TestUtil::ExpectAllExtensionsSet(
1162     const unittest::TestAllExtensions& message) {
1163   EXPECT_TRUE(message.HasExtension(unittest::optional_int32_extension   ));
1164   EXPECT_TRUE(message.HasExtension(unittest::optional_int64_extension   ));
1165   EXPECT_TRUE(message.HasExtension(unittest::optional_uint32_extension  ));
1166   EXPECT_TRUE(message.HasExtension(unittest::optional_uint64_extension  ));
1167   EXPECT_TRUE(message.HasExtension(unittest::optional_sint32_extension  ));
1168   EXPECT_TRUE(message.HasExtension(unittest::optional_sint64_extension  ));
1169   EXPECT_TRUE(message.HasExtension(unittest::optional_fixed32_extension ));
1170   EXPECT_TRUE(message.HasExtension(unittest::optional_fixed64_extension ));
1171   EXPECT_TRUE(message.HasExtension(unittest::optional_sfixed32_extension));
1172   EXPECT_TRUE(message.HasExtension(unittest::optional_sfixed64_extension));
1173   EXPECT_TRUE(message.HasExtension(unittest::optional_float_extension   ));
1174   EXPECT_TRUE(message.HasExtension(unittest::optional_double_extension  ));
1175   EXPECT_TRUE(message.HasExtension(unittest::optional_bool_extension    ));
1176   EXPECT_TRUE(message.HasExtension(unittest::optional_string_extension  ));
1177   EXPECT_TRUE(message.HasExtension(unittest::optional_bytes_extension   ));
1178 
1179   EXPECT_TRUE(message.HasExtension(unittest::optionalgroup_extension                 ));
1180   EXPECT_TRUE(message.HasExtension(unittest::optional_nested_message_extension       ));
1181   EXPECT_TRUE(message.HasExtension(unittest::optional_foreign_message_extension      ));
1182   EXPECT_TRUE(message.HasExtension(unittest::optional_import_message_extension       ));
1183   EXPECT_TRUE(message.HasExtension(unittest::optional_public_import_message_extension));
1184   EXPECT_TRUE(message.HasExtension(unittest::optional_lazy_message_extension         ));
1185 
1186   EXPECT_TRUE(message.GetExtension(unittest::optionalgroup_extension                 ).has_a());
1187   EXPECT_TRUE(message.GetExtension(unittest::optional_nested_message_extension       ).has_bb());
1188   EXPECT_TRUE(message.GetExtension(unittest::optional_foreign_message_extension      ).has_c());
1189   EXPECT_TRUE(message.GetExtension(unittest::optional_import_message_extension       ).has_d());
1190   EXPECT_TRUE(message.GetExtension(unittest::optional_public_import_message_extension).has_e());
1191   EXPECT_TRUE(message.GetExtension(unittest::optional_lazy_message_extension         ).has_bb());
1192 
1193   EXPECT_TRUE(message.HasExtension(unittest::optional_nested_enum_extension ));
1194   EXPECT_TRUE(message.HasExtension(unittest::optional_foreign_enum_extension));
1195   EXPECT_TRUE(message.HasExtension(unittest::optional_import_enum_extension ));
1196 
1197   EXPECT_TRUE(message.HasExtension(unittest::optional_string_piece_extension));
1198   EXPECT_TRUE(message.HasExtension(unittest::optional_cord_extension));
1199 
1200   EXPECT_EQ(101  , message.GetExtension(unittest::optional_int32_extension   ));
1201   EXPECT_EQ(102  , message.GetExtension(unittest::optional_int64_extension   ));
1202   EXPECT_EQ(103  , message.GetExtension(unittest::optional_uint32_extension  ));
1203   EXPECT_EQ(104  , message.GetExtension(unittest::optional_uint64_extension  ));
1204   EXPECT_EQ(105  , message.GetExtension(unittest::optional_sint32_extension  ));
1205   EXPECT_EQ(106  , message.GetExtension(unittest::optional_sint64_extension  ));
1206   EXPECT_EQ(107  , message.GetExtension(unittest::optional_fixed32_extension ));
1207   EXPECT_EQ(108  , message.GetExtension(unittest::optional_fixed64_extension ));
1208   EXPECT_EQ(109  , message.GetExtension(unittest::optional_sfixed32_extension));
1209   EXPECT_EQ(110  , message.GetExtension(unittest::optional_sfixed64_extension));
1210   EXPECT_EQ(111  , message.GetExtension(unittest::optional_float_extension   ));
1211   EXPECT_EQ(112  , message.GetExtension(unittest::optional_double_extension  ));
1212   EXPECT_TRUE(     message.GetExtension(unittest::optional_bool_extension    ));
1213   EXPECT_EQ("115", message.GetExtension(unittest::optional_string_extension  ));
1214   EXPECT_EQ("116", message.GetExtension(unittest::optional_bytes_extension   ));
1215 
1216   EXPECT_EQ(117, message.GetExtension(unittest::optionalgroup_extension           ).a());
1217   EXPECT_EQ(118, message.GetExtension(unittest::optional_nested_message_extension ).bb());
1218   EXPECT_EQ(119, message.GetExtension(unittest::optional_foreign_message_extension).c());
1219   EXPECT_EQ(120, message.GetExtension(unittest::optional_import_message_extension ).d());
1220 
1221   EXPECT_EQ(unittest::TestAllTypes::BAZ, message.GetExtension(unittest::optional_nested_enum_extension ));
1222   EXPECT_EQ(unittest::FOREIGN_BAZ      , message.GetExtension(unittest::optional_foreign_enum_extension));
1223   EXPECT_EQ(unittest_import::IMPORT_BAZ, message.GetExtension(unittest::optional_import_enum_extension ));
1224 
1225   EXPECT_EQ("124", message.GetExtension(unittest::optional_string_piece_extension));
1226   EXPECT_EQ("125", message.GetExtension(unittest::optional_cord_extension));
1227   EXPECT_EQ(126, message.GetExtension(unittest::optional_public_import_message_extension ).e());
1228   EXPECT_EQ(127, message.GetExtension(unittest::optional_lazy_message_extension).bb());
1229 
1230   // -----------------------------------------------------------------
1231 
1232   ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_int32_extension   ));
1233   ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_int64_extension   ));
1234   ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_uint32_extension  ));
1235   ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_uint64_extension  ));
1236   ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_sint32_extension  ));
1237   ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_sint64_extension  ));
1238   ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_fixed32_extension ));
1239   ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_fixed64_extension ));
1240   ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_sfixed32_extension));
1241   ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_sfixed64_extension));
1242   ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_float_extension   ));
1243   ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_double_extension  ));
1244   ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_bool_extension    ));
1245   ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_string_extension  ));
1246   ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_bytes_extension   ));
1247 
1248   ASSERT_EQ(2, message.ExtensionSize(unittest::repeatedgroup_extension           ));
1249   ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_nested_message_extension ));
1250   ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_foreign_message_extension));
1251   ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_import_message_extension ));
1252   ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_lazy_message_extension   ));
1253   ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_nested_enum_extension    ));
1254   ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_foreign_enum_extension   ));
1255   ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_import_enum_extension    ));
1256 
1257   ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_string_piece_extension));
1258   ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_cord_extension));
1259 
1260   EXPECT_EQ(201  , message.GetExtension(unittest::repeated_int32_extension   , 0));
1261   EXPECT_EQ(202  , message.GetExtension(unittest::repeated_int64_extension   , 0));
1262   EXPECT_EQ(203  , message.GetExtension(unittest::repeated_uint32_extension  , 0));
1263   EXPECT_EQ(204  , message.GetExtension(unittest::repeated_uint64_extension  , 0));
1264   EXPECT_EQ(205  , message.GetExtension(unittest::repeated_sint32_extension  , 0));
1265   EXPECT_EQ(206  , message.GetExtension(unittest::repeated_sint64_extension  , 0));
1266   EXPECT_EQ(207  , message.GetExtension(unittest::repeated_fixed32_extension , 0));
1267   EXPECT_EQ(208  , message.GetExtension(unittest::repeated_fixed64_extension , 0));
1268   EXPECT_EQ(209  , message.GetExtension(unittest::repeated_sfixed32_extension, 0));
1269   EXPECT_EQ(210  , message.GetExtension(unittest::repeated_sfixed64_extension, 0));
1270   EXPECT_EQ(211  , message.GetExtension(unittest::repeated_float_extension   , 0));
1271   EXPECT_EQ(212  , message.GetExtension(unittest::repeated_double_extension  , 0));
1272   EXPECT_TRUE(     message.GetExtension(unittest::repeated_bool_extension    , 0));
1273   EXPECT_EQ("215", message.GetExtension(unittest::repeated_string_extension  , 0));
1274   EXPECT_EQ("216", message.GetExtension(unittest::repeated_bytes_extension   , 0));
1275 
1276   EXPECT_EQ(217, message.GetExtension(unittest::repeatedgroup_extension           , 0).a());
1277   EXPECT_EQ(218, message.GetExtension(unittest::repeated_nested_message_extension , 0).bb());
1278   EXPECT_EQ(219, message.GetExtension(unittest::repeated_foreign_message_extension, 0).c());
1279   EXPECT_EQ(220, message.GetExtension(unittest::repeated_import_message_extension , 0).d());
1280   EXPECT_EQ(227, message.GetExtension(unittest::repeated_lazy_message_extension   , 0).bb());
1281 
1282   EXPECT_EQ(unittest::TestAllTypes::BAR, message.GetExtension(unittest::repeated_nested_enum_extension , 0));
1283   EXPECT_EQ(unittest::FOREIGN_BAR      , message.GetExtension(unittest::repeated_foreign_enum_extension, 0));
1284   EXPECT_EQ(unittest_import::IMPORT_BAR, message.GetExtension(unittest::repeated_import_enum_extension , 0));
1285 
1286   EXPECT_EQ("224", message.GetExtension(unittest::repeated_string_piece_extension, 0));
1287   EXPECT_EQ("225", message.GetExtension(unittest::repeated_cord_extension, 0));
1288 
1289   EXPECT_EQ(301  , message.GetExtension(unittest::repeated_int32_extension   , 1));
1290   EXPECT_EQ(302  , message.GetExtension(unittest::repeated_int64_extension   , 1));
1291   EXPECT_EQ(303  , message.GetExtension(unittest::repeated_uint32_extension  , 1));
1292   EXPECT_EQ(304  , message.GetExtension(unittest::repeated_uint64_extension  , 1));
1293   EXPECT_EQ(305  , message.GetExtension(unittest::repeated_sint32_extension  , 1));
1294   EXPECT_EQ(306  , message.GetExtension(unittest::repeated_sint64_extension  , 1));
1295   EXPECT_EQ(307  , message.GetExtension(unittest::repeated_fixed32_extension , 1));
1296   EXPECT_EQ(308  , message.GetExtension(unittest::repeated_fixed64_extension , 1));
1297   EXPECT_EQ(309  , message.GetExtension(unittest::repeated_sfixed32_extension, 1));
1298   EXPECT_EQ(310  , message.GetExtension(unittest::repeated_sfixed64_extension, 1));
1299   EXPECT_EQ(311  , message.GetExtension(unittest::repeated_float_extension   , 1));
1300   EXPECT_EQ(312  , message.GetExtension(unittest::repeated_double_extension  , 1));
1301   EXPECT_FALSE(    message.GetExtension(unittest::repeated_bool_extension    , 1));
1302   EXPECT_EQ("315", message.GetExtension(unittest::repeated_string_extension  , 1));
1303   EXPECT_EQ("316", message.GetExtension(unittest::repeated_bytes_extension   , 1));
1304 
1305   EXPECT_EQ(317, message.GetExtension(unittest::repeatedgroup_extension           , 1).a());
1306   EXPECT_EQ(318, message.GetExtension(unittest::repeated_nested_message_extension , 1).bb());
1307   EXPECT_EQ(319, message.GetExtension(unittest::repeated_foreign_message_extension, 1).c());
1308   EXPECT_EQ(320, message.GetExtension(unittest::repeated_import_message_extension , 1).d());
1309   EXPECT_EQ(327, message.GetExtension(unittest::repeated_lazy_message_extension   , 1).bb());
1310 
1311   EXPECT_EQ(unittest::TestAllTypes::BAZ, message.GetExtension(unittest::repeated_nested_enum_extension , 1));
1312   EXPECT_EQ(unittest::FOREIGN_BAZ      , message.GetExtension(unittest::repeated_foreign_enum_extension, 1));
1313   EXPECT_EQ(unittest_import::IMPORT_BAZ, message.GetExtension(unittest::repeated_import_enum_extension , 1));
1314 
1315   EXPECT_EQ("324", message.GetExtension(unittest::repeated_string_piece_extension, 1));
1316   EXPECT_EQ("325", message.GetExtension(unittest::repeated_cord_extension, 1));
1317 
1318   // -----------------------------------------------------------------
1319 
1320   EXPECT_TRUE(message.HasExtension(unittest::default_int32_extension   ));
1321   EXPECT_TRUE(message.HasExtension(unittest::default_int64_extension   ));
1322   EXPECT_TRUE(message.HasExtension(unittest::default_uint32_extension  ));
1323   EXPECT_TRUE(message.HasExtension(unittest::default_uint64_extension  ));
1324   EXPECT_TRUE(message.HasExtension(unittest::default_sint32_extension  ));
1325   EXPECT_TRUE(message.HasExtension(unittest::default_sint64_extension  ));
1326   EXPECT_TRUE(message.HasExtension(unittest::default_fixed32_extension ));
1327   EXPECT_TRUE(message.HasExtension(unittest::default_fixed64_extension ));
1328   EXPECT_TRUE(message.HasExtension(unittest::default_sfixed32_extension));
1329   EXPECT_TRUE(message.HasExtension(unittest::default_sfixed64_extension));
1330   EXPECT_TRUE(message.HasExtension(unittest::default_float_extension   ));
1331   EXPECT_TRUE(message.HasExtension(unittest::default_double_extension  ));
1332   EXPECT_TRUE(message.HasExtension(unittest::default_bool_extension    ));
1333   EXPECT_TRUE(message.HasExtension(unittest::default_string_extension  ));
1334   EXPECT_TRUE(message.HasExtension(unittest::default_bytes_extension   ));
1335 
1336   EXPECT_TRUE(message.HasExtension(unittest::default_nested_enum_extension ));
1337   EXPECT_TRUE(message.HasExtension(unittest::default_foreign_enum_extension));
1338   EXPECT_TRUE(message.HasExtension(unittest::default_import_enum_extension ));
1339 
1340   EXPECT_TRUE(message.HasExtension(unittest::default_string_piece_extension));
1341   EXPECT_TRUE(message.HasExtension(unittest::default_cord_extension));
1342 
1343   EXPECT_EQ(401  , message.GetExtension(unittest::default_int32_extension   ));
1344   EXPECT_EQ(402  , message.GetExtension(unittest::default_int64_extension   ));
1345   EXPECT_EQ(403  , message.GetExtension(unittest::default_uint32_extension  ));
1346   EXPECT_EQ(404  , message.GetExtension(unittest::default_uint64_extension  ));
1347   EXPECT_EQ(405  , message.GetExtension(unittest::default_sint32_extension  ));
1348   EXPECT_EQ(406  , message.GetExtension(unittest::default_sint64_extension  ));
1349   EXPECT_EQ(407  , message.GetExtension(unittest::default_fixed32_extension ));
1350   EXPECT_EQ(408  , message.GetExtension(unittest::default_fixed64_extension ));
1351   EXPECT_EQ(409  , message.GetExtension(unittest::default_sfixed32_extension));
1352   EXPECT_EQ(410  , message.GetExtension(unittest::default_sfixed64_extension));
1353   EXPECT_EQ(411  , message.GetExtension(unittest::default_float_extension   ));
1354   EXPECT_EQ(412  , message.GetExtension(unittest::default_double_extension  ));
1355   EXPECT_FALSE(    message.GetExtension(unittest::default_bool_extension    ));
1356   EXPECT_EQ("415", message.GetExtension(unittest::default_string_extension  ));
1357   EXPECT_EQ("416", message.GetExtension(unittest::default_bytes_extension   ));
1358 
1359   EXPECT_EQ(unittest::TestAllTypes::FOO, message.GetExtension(unittest::default_nested_enum_extension ));
1360   EXPECT_EQ(unittest::FOREIGN_FOO      , message.GetExtension(unittest::default_foreign_enum_extension));
1361   EXPECT_EQ(unittest_import::IMPORT_FOO, message.GetExtension(unittest::default_import_enum_extension ));
1362 
1363   EXPECT_EQ("424", message.GetExtension(unittest::default_string_piece_extension));
1364   EXPECT_EQ("425", message.GetExtension(unittest::default_cord_extension));
1365 
1366   EXPECT_TRUE(message.HasExtension(unittest::oneof_uint32_extension));
1367   EXPECT_TRUE(message.GetExtension(unittest::oneof_nested_message_extension).has_bb());
1368   EXPECT_TRUE(message.HasExtension(unittest::oneof_string_extension));
1369   EXPECT_TRUE(message.HasExtension(unittest::oneof_bytes_extension));
1370 
1371   EXPECT_EQ(601, message.GetExtension(unittest::oneof_uint32_extension));
1372   EXPECT_EQ(602, message.GetExtension(unittest::oneof_nested_message_extension).bb());
1373   EXPECT_EQ("603", message.GetExtension(unittest::oneof_string_extension));
1374   EXPECT_EQ("604", message.GetExtension(unittest::oneof_bytes_extension));
1375 }
1376 
1377 // -------------------------------------------------------------------
1378 
ExpectExtensionsClear(const unittest::TestAllExtensions & message)1379 void TestUtil::ExpectExtensionsClear(
1380     const unittest::TestAllExtensions& message) {
1381   string serialized;
1382   ASSERT_TRUE(message.SerializeToString(&serialized));
1383   EXPECT_EQ("", serialized);
1384   EXPECT_EQ(0, message.ByteSize());
1385 
1386   // has_blah() should initially be false for all optional fields.
1387   EXPECT_FALSE(message.HasExtension(unittest::optional_int32_extension   ));
1388   EXPECT_FALSE(message.HasExtension(unittest::optional_int64_extension   ));
1389   EXPECT_FALSE(message.HasExtension(unittest::optional_uint32_extension  ));
1390   EXPECT_FALSE(message.HasExtension(unittest::optional_uint64_extension  ));
1391   EXPECT_FALSE(message.HasExtension(unittest::optional_sint32_extension  ));
1392   EXPECT_FALSE(message.HasExtension(unittest::optional_sint64_extension  ));
1393   EXPECT_FALSE(message.HasExtension(unittest::optional_fixed32_extension ));
1394   EXPECT_FALSE(message.HasExtension(unittest::optional_fixed64_extension ));
1395   EXPECT_FALSE(message.HasExtension(unittest::optional_sfixed32_extension));
1396   EXPECT_FALSE(message.HasExtension(unittest::optional_sfixed64_extension));
1397   EXPECT_FALSE(message.HasExtension(unittest::optional_float_extension   ));
1398   EXPECT_FALSE(message.HasExtension(unittest::optional_double_extension  ));
1399   EXPECT_FALSE(message.HasExtension(unittest::optional_bool_extension    ));
1400   EXPECT_FALSE(message.HasExtension(unittest::optional_string_extension  ));
1401   EXPECT_FALSE(message.HasExtension(unittest::optional_bytes_extension   ));
1402 
1403   EXPECT_FALSE(message.HasExtension(unittest::optionalgroup_extension                 ));
1404   EXPECT_FALSE(message.HasExtension(unittest::optional_nested_message_extension       ));
1405   EXPECT_FALSE(message.HasExtension(unittest::optional_foreign_message_extension      ));
1406   EXPECT_FALSE(message.HasExtension(unittest::optional_import_message_extension       ));
1407   EXPECT_FALSE(message.HasExtension(unittest::optional_public_import_message_extension));
1408   EXPECT_FALSE(message.HasExtension(unittest::optional_lazy_message_extension         ));
1409 
1410   EXPECT_FALSE(message.HasExtension(unittest::optional_nested_enum_extension ));
1411   EXPECT_FALSE(message.HasExtension(unittest::optional_foreign_enum_extension));
1412   EXPECT_FALSE(message.HasExtension(unittest::optional_import_enum_extension ));
1413 
1414   EXPECT_FALSE(message.HasExtension(unittest::optional_string_piece_extension));
1415   EXPECT_FALSE(message.HasExtension(unittest::optional_cord_extension));
1416 
1417   // Optional fields without defaults are set to zero or something like it.
1418   EXPECT_EQ(0    , message.GetExtension(unittest::optional_int32_extension   ));
1419   EXPECT_EQ(0    , message.GetExtension(unittest::optional_int64_extension   ));
1420   EXPECT_EQ(0    , message.GetExtension(unittest::optional_uint32_extension  ));
1421   EXPECT_EQ(0    , message.GetExtension(unittest::optional_uint64_extension  ));
1422   EXPECT_EQ(0    , message.GetExtension(unittest::optional_sint32_extension  ));
1423   EXPECT_EQ(0    , message.GetExtension(unittest::optional_sint64_extension  ));
1424   EXPECT_EQ(0    , message.GetExtension(unittest::optional_fixed32_extension ));
1425   EXPECT_EQ(0    , message.GetExtension(unittest::optional_fixed64_extension ));
1426   EXPECT_EQ(0    , message.GetExtension(unittest::optional_sfixed32_extension));
1427   EXPECT_EQ(0    , message.GetExtension(unittest::optional_sfixed64_extension));
1428   EXPECT_EQ(0    , message.GetExtension(unittest::optional_float_extension   ));
1429   EXPECT_EQ(0    , message.GetExtension(unittest::optional_double_extension  ));
1430   EXPECT_FALSE(    message.GetExtension(unittest::optional_bool_extension    ));
1431   EXPECT_EQ(""   , message.GetExtension(unittest::optional_string_extension  ));
1432   EXPECT_EQ(""   , message.GetExtension(unittest::optional_bytes_extension   ));
1433 
1434   // Embedded messages should also be clear.
1435   EXPECT_FALSE(message.GetExtension(unittest::optionalgroup_extension                 ).has_a());
1436   EXPECT_FALSE(message.GetExtension(unittest::optional_nested_message_extension       ).has_bb());
1437   EXPECT_FALSE(message.GetExtension(unittest::optional_foreign_message_extension      ).has_c());
1438   EXPECT_FALSE(message.GetExtension(unittest::optional_import_message_extension       ).has_d());
1439   EXPECT_FALSE(message.GetExtension(unittest::optional_public_import_message_extension).has_e());
1440   EXPECT_FALSE(message.GetExtension(unittest::optional_lazy_message_extension         ).has_bb());
1441 
1442   EXPECT_EQ(0, message.GetExtension(unittest::optionalgroup_extension                 ).a());
1443   EXPECT_EQ(0, message.GetExtension(unittest::optional_nested_message_extension       ).bb());
1444   EXPECT_EQ(0, message.GetExtension(unittest::optional_foreign_message_extension      ).c());
1445   EXPECT_EQ(0, message.GetExtension(unittest::optional_import_message_extension       ).d());
1446   EXPECT_EQ(0, message.GetExtension(unittest::optional_public_import_message_extension).e());
1447   EXPECT_EQ(0, message.GetExtension(unittest::optional_lazy_message_extension         ).bb());
1448 
1449   // Enums without defaults are set to the first value in the enum.
1450   EXPECT_EQ(unittest::TestAllTypes::FOO, message.GetExtension(unittest::optional_nested_enum_extension ));
1451   EXPECT_EQ(unittest::FOREIGN_FOO      , message.GetExtension(unittest::optional_foreign_enum_extension));
1452   EXPECT_EQ(unittest_import::IMPORT_FOO, message.GetExtension(unittest::optional_import_enum_extension ));
1453 
1454   EXPECT_EQ("", message.GetExtension(unittest::optional_string_piece_extension));
1455   EXPECT_EQ("", message.GetExtension(unittest::optional_cord_extension));
1456 
1457   // Repeated fields are empty.
1458   EXPECT_EQ(0, message.ExtensionSize(unittest::repeated_int32_extension   ));
1459   EXPECT_EQ(0, message.ExtensionSize(unittest::repeated_int64_extension   ));
1460   EXPECT_EQ(0, message.ExtensionSize(unittest::repeated_uint32_extension  ));
1461   EXPECT_EQ(0, message.ExtensionSize(unittest::repeated_uint64_extension  ));
1462   EXPECT_EQ(0, message.ExtensionSize(unittest::repeated_sint32_extension  ));
1463   EXPECT_EQ(0, message.ExtensionSize(unittest::repeated_sint64_extension  ));
1464   EXPECT_EQ(0, message.ExtensionSize(unittest::repeated_fixed32_extension ));
1465   EXPECT_EQ(0, message.ExtensionSize(unittest::repeated_fixed64_extension ));
1466   EXPECT_EQ(0, message.ExtensionSize(unittest::repeated_sfixed32_extension));
1467   EXPECT_EQ(0, message.ExtensionSize(unittest::repeated_sfixed64_extension));
1468   EXPECT_EQ(0, message.ExtensionSize(unittest::repeated_float_extension   ));
1469   EXPECT_EQ(0, message.ExtensionSize(unittest::repeated_double_extension  ));
1470   EXPECT_EQ(0, message.ExtensionSize(unittest::repeated_bool_extension    ));
1471   EXPECT_EQ(0, message.ExtensionSize(unittest::repeated_string_extension  ));
1472   EXPECT_EQ(0, message.ExtensionSize(unittest::repeated_bytes_extension   ));
1473 
1474   EXPECT_EQ(0, message.ExtensionSize(unittest::repeatedgroup_extension           ));
1475   EXPECT_EQ(0, message.ExtensionSize(unittest::repeated_nested_message_extension ));
1476   EXPECT_EQ(0, message.ExtensionSize(unittest::repeated_foreign_message_extension));
1477   EXPECT_EQ(0, message.ExtensionSize(unittest::repeated_import_message_extension ));
1478   EXPECT_EQ(0, message.ExtensionSize(unittest::repeated_lazy_message_extension   ));
1479   EXPECT_EQ(0, message.ExtensionSize(unittest::repeated_nested_enum_extension    ));
1480   EXPECT_EQ(0, message.ExtensionSize(unittest::repeated_foreign_enum_extension   ));
1481   EXPECT_EQ(0, message.ExtensionSize(unittest::repeated_import_enum_extension    ));
1482 
1483   EXPECT_EQ(0, message.ExtensionSize(unittest::repeated_string_piece_extension));
1484   EXPECT_EQ(0, message.ExtensionSize(unittest::repeated_cord_extension));
1485 
1486   // has_blah() should also be false for all default fields.
1487   EXPECT_FALSE(message.HasExtension(unittest::default_int32_extension   ));
1488   EXPECT_FALSE(message.HasExtension(unittest::default_int64_extension   ));
1489   EXPECT_FALSE(message.HasExtension(unittest::default_uint32_extension  ));
1490   EXPECT_FALSE(message.HasExtension(unittest::default_uint64_extension  ));
1491   EXPECT_FALSE(message.HasExtension(unittest::default_sint32_extension  ));
1492   EXPECT_FALSE(message.HasExtension(unittest::default_sint64_extension  ));
1493   EXPECT_FALSE(message.HasExtension(unittest::default_fixed32_extension ));
1494   EXPECT_FALSE(message.HasExtension(unittest::default_fixed64_extension ));
1495   EXPECT_FALSE(message.HasExtension(unittest::default_sfixed32_extension));
1496   EXPECT_FALSE(message.HasExtension(unittest::default_sfixed64_extension));
1497   EXPECT_FALSE(message.HasExtension(unittest::default_float_extension   ));
1498   EXPECT_FALSE(message.HasExtension(unittest::default_double_extension  ));
1499   EXPECT_FALSE(message.HasExtension(unittest::default_bool_extension    ));
1500   EXPECT_FALSE(message.HasExtension(unittest::default_string_extension  ));
1501   EXPECT_FALSE(message.HasExtension(unittest::default_bytes_extension   ));
1502 
1503   EXPECT_FALSE(message.HasExtension(unittest::default_nested_enum_extension ));
1504   EXPECT_FALSE(message.HasExtension(unittest::default_foreign_enum_extension));
1505   EXPECT_FALSE(message.HasExtension(unittest::default_import_enum_extension ));
1506 
1507   EXPECT_FALSE(message.HasExtension(unittest::default_string_piece_extension));
1508   EXPECT_FALSE(message.HasExtension(unittest::default_cord_extension));
1509 
1510   // Fields with defaults have their default values (duh).
1511   EXPECT_EQ( 41    , message.GetExtension(unittest::default_int32_extension   ));
1512   EXPECT_EQ( 42    , message.GetExtension(unittest::default_int64_extension   ));
1513   EXPECT_EQ( 43    , message.GetExtension(unittest::default_uint32_extension  ));
1514   EXPECT_EQ( 44    , message.GetExtension(unittest::default_uint64_extension  ));
1515   EXPECT_EQ(-45    , message.GetExtension(unittest::default_sint32_extension  ));
1516   EXPECT_EQ( 46    , message.GetExtension(unittest::default_sint64_extension  ));
1517   EXPECT_EQ( 47    , message.GetExtension(unittest::default_fixed32_extension ));
1518   EXPECT_EQ( 48    , message.GetExtension(unittest::default_fixed64_extension ));
1519   EXPECT_EQ( 49    , message.GetExtension(unittest::default_sfixed32_extension));
1520   EXPECT_EQ(-50    , message.GetExtension(unittest::default_sfixed64_extension));
1521   EXPECT_EQ( 51.5  , message.GetExtension(unittest::default_float_extension   ));
1522   EXPECT_EQ( 52e3  , message.GetExtension(unittest::default_double_extension  ));
1523   EXPECT_TRUE(       message.GetExtension(unittest::default_bool_extension    ));
1524   EXPECT_EQ("hello", message.GetExtension(unittest::default_string_extension  ));
1525   EXPECT_EQ("world", message.GetExtension(unittest::default_bytes_extension   ));
1526 
1527   EXPECT_EQ(unittest::TestAllTypes::BAR, message.GetExtension(unittest::default_nested_enum_extension ));
1528   EXPECT_EQ(unittest::FOREIGN_BAR      , message.GetExtension(unittest::default_foreign_enum_extension));
1529   EXPECT_EQ(unittest_import::IMPORT_BAR, message.GetExtension(unittest::default_import_enum_extension ));
1530 
1531   EXPECT_EQ("abc", message.GetExtension(unittest::default_string_piece_extension));
1532   EXPECT_EQ("123", message.GetExtension(unittest::default_cord_extension));
1533 
1534   EXPECT_FALSE(message.HasExtension(unittest::oneof_uint32_extension));
1535   EXPECT_FALSE(message.GetExtension(unittest::oneof_nested_message_extension).has_bb());
1536   EXPECT_FALSE(message.HasExtension(unittest::oneof_string_extension));
1537   EXPECT_FALSE(message.HasExtension(unittest::oneof_bytes_extension));
1538 }
1539 
1540 // -------------------------------------------------------------------
1541 
ExpectRepeatedExtensionsModified(const unittest::TestAllExtensions & message)1542 void TestUtil::ExpectRepeatedExtensionsModified(
1543     const unittest::TestAllExtensions& message) {
1544   // ModifyRepeatedFields only sets the second repeated element of each
1545   // field.  In addition to verifying this, we also verify that the first
1546   // element and size were *not* modified.
1547   ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_int32_extension   ));
1548   ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_int64_extension   ));
1549   ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_uint32_extension  ));
1550   ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_uint64_extension  ));
1551   ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_sint32_extension  ));
1552   ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_sint64_extension  ));
1553   ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_fixed32_extension ));
1554   ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_fixed64_extension ));
1555   ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_sfixed32_extension));
1556   ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_sfixed64_extension));
1557   ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_float_extension   ));
1558   ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_double_extension  ));
1559   ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_bool_extension    ));
1560   ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_string_extension  ));
1561   ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_bytes_extension   ));
1562 
1563   ASSERT_EQ(2, message.ExtensionSize(unittest::repeatedgroup_extension           ));
1564   ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_nested_message_extension ));
1565   ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_foreign_message_extension));
1566   ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_import_message_extension ));
1567   ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_lazy_message_extension   ));
1568   ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_nested_enum_extension    ));
1569   ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_foreign_enum_extension   ));
1570   ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_import_enum_extension    ));
1571 
1572   ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_string_piece_extension));
1573   ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_cord_extension));
1574 
1575   EXPECT_EQ(201  , message.GetExtension(unittest::repeated_int32_extension   , 0));
1576   EXPECT_EQ(202  , message.GetExtension(unittest::repeated_int64_extension   , 0));
1577   EXPECT_EQ(203  , message.GetExtension(unittest::repeated_uint32_extension  , 0));
1578   EXPECT_EQ(204  , message.GetExtension(unittest::repeated_uint64_extension  , 0));
1579   EXPECT_EQ(205  , message.GetExtension(unittest::repeated_sint32_extension  , 0));
1580   EXPECT_EQ(206  , message.GetExtension(unittest::repeated_sint64_extension  , 0));
1581   EXPECT_EQ(207  , message.GetExtension(unittest::repeated_fixed32_extension , 0));
1582   EXPECT_EQ(208  , message.GetExtension(unittest::repeated_fixed64_extension , 0));
1583   EXPECT_EQ(209  , message.GetExtension(unittest::repeated_sfixed32_extension, 0));
1584   EXPECT_EQ(210  , message.GetExtension(unittest::repeated_sfixed64_extension, 0));
1585   EXPECT_EQ(211  , message.GetExtension(unittest::repeated_float_extension   , 0));
1586   EXPECT_EQ(212  , message.GetExtension(unittest::repeated_double_extension  , 0));
1587   EXPECT_TRUE(     message.GetExtension(unittest::repeated_bool_extension    , 0));
1588   EXPECT_EQ("215", message.GetExtension(unittest::repeated_string_extension  , 0));
1589   EXPECT_EQ("216", message.GetExtension(unittest::repeated_bytes_extension   , 0));
1590 
1591   EXPECT_EQ(217, message.GetExtension(unittest::repeatedgroup_extension           , 0).a());
1592   EXPECT_EQ(218, message.GetExtension(unittest::repeated_nested_message_extension , 0).bb());
1593   EXPECT_EQ(219, message.GetExtension(unittest::repeated_foreign_message_extension, 0).c());
1594   EXPECT_EQ(220, message.GetExtension(unittest::repeated_import_message_extension , 0).d());
1595   EXPECT_EQ(227, message.GetExtension(unittest::repeated_lazy_message_extension   , 0).bb());
1596 
1597   EXPECT_EQ(unittest::TestAllTypes::BAR, message.GetExtension(unittest::repeated_nested_enum_extension , 0));
1598   EXPECT_EQ(unittest::FOREIGN_BAR      , message.GetExtension(unittest::repeated_foreign_enum_extension, 0));
1599   EXPECT_EQ(unittest_import::IMPORT_BAR, message.GetExtension(unittest::repeated_import_enum_extension , 0));
1600 
1601   EXPECT_EQ("224", message.GetExtension(unittest::repeated_string_piece_extension, 0));
1602   EXPECT_EQ("225", message.GetExtension(unittest::repeated_cord_extension, 0));
1603 
1604   // Actually verify the second (modified) elements now.
1605   EXPECT_EQ(501  , message.GetExtension(unittest::repeated_int32_extension   , 1));
1606   EXPECT_EQ(502  , message.GetExtension(unittest::repeated_int64_extension   , 1));
1607   EXPECT_EQ(503  , message.GetExtension(unittest::repeated_uint32_extension  , 1));
1608   EXPECT_EQ(504  , message.GetExtension(unittest::repeated_uint64_extension  , 1));
1609   EXPECT_EQ(505  , message.GetExtension(unittest::repeated_sint32_extension  , 1));
1610   EXPECT_EQ(506  , message.GetExtension(unittest::repeated_sint64_extension  , 1));
1611   EXPECT_EQ(507  , message.GetExtension(unittest::repeated_fixed32_extension , 1));
1612   EXPECT_EQ(508  , message.GetExtension(unittest::repeated_fixed64_extension , 1));
1613   EXPECT_EQ(509  , message.GetExtension(unittest::repeated_sfixed32_extension, 1));
1614   EXPECT_EQ(510  , message.GetExtension(unittest::repeated_sfixed64_extension, 1));
1615   EXPECT_EQ(511  , message.GetExtension(unittest::repeated_float_extension   , 1));
1616   EXPECT_EQ(512  , message.GetExtension(unittest::repeated_double_extension  , 1));
1617   EXPECT_TRUE(     message.GetExtension(unittest::repeated_bool_extension    , 1));
1618   EXPECT_EQ("515", message.GetExtension(unittest::repeated_string_extension  , 1));
1619   EXPECT_EQ("516", message.GetExtension(unittest::repeated_bytes_extension   , 1));
1620 
1621   EXPECT_EQ(517, message.GetExtension(unittest::repeatedgroup_extension           , 1).a());
1622   EXPECT_EQ(518, message.GetExtension(unittest::repeated_nested_message_extension , 1).bb());
1623   EXPECT_EQ(519, message.GetExtension(unittest::repeated_foreign_message_extension, 1).c());
1624   EXPECT_EQ(520, message.GetExtension(unittest::repeated_import_message_extension , 1).d());
1625   EXPECT_EQ(527, message.GetExtension(unittest::repeated_lazy_message_extension   , 1).bb());
1626 
1627   EXPECT_EQ(unittest::TestAllTypes::FOO, message.GetExtension(unittest::repeated_nested_enum_extension , 1));
1628   EXPECT_EQ(unittest::FOREIGN_FOO      , message.GetExtension(unittest::repeated_foreign_enum_extension, 1));
1629   EXPECT_EQ(unittest_import::IMPORT_FOO, message.GetExtension(unittest::repeated_import_enum_extension , 1));
1630 
1631   EXPECT_EQ("524", message.GetExtension(unittest::repeated_string_piece_extension, 1));
1632   EXPECT_EQ("525", message.GetExtension(unittest::repeated_cord_extension, 1));
1633 }
1634 
1635 // -------------------------------------------------------------------
1636 
SetPackedExtensions(unittest::TestPackedExtensions * message)1637 void TestUtil::SetPackedExtensions(unittest::TestPackedExtensions* message) {
1638   message->AddExtension(unittest::packed_int32_extension   , 601);
1639   message->AddExtension(unittest::packed_int64_extension   , 602);
1640   message->AddExtension(unittest::packed_uint32_extension  , 603);
1641   message->AddExtension(unittest::packed_uint64_extension  , 604);
1642   message->AddExtension(unittest::packed_sint32_extension  , 605);
1643   message->AddExtension(unittest::packed_sint64_extension  , 606);
1644   message->AddExtension(unittest::packed_fixed32_extension , 607);
1645   message->AddExtension(unittest::packed_fixed64_extension , 608);
1646   message->AddExtension(unittest::packed_sfixed32_extension, 609);
1647   message->AddExtension(unittest::packed_sfixed64_extension, 610);
1648   message->AddExtension(unittest::packed_float_extension   , 611);
1649   message->AddExtension(unittest::packed_double_extension  , 612);
1650   message->AddExtension(unittest::packed_bool_extension    , true);
1651   message->AddExtension(unittest::packed_enum_extension, unittest::FOREIGN_BAR);
1652   // add a second one of each field
1653   message->AddExtension(unittest::packed_int32_extension   , 701);
1654   message->AddExtension(unittest::packed_int64_extension   , 702);
1655   message->AddExtension(unittest::packed_uint32_extension  , 703);
1656   message->AddExtension(unittest::packed_uint64_extension  , 704);
1657   message->AddExtension(unittest::packed_sint32_extension  , 705);
1658   message->AddExtension(unittest::packed_sint64_extension  , 706);
1659   message->AddExtension(unittest::packed_fixed32_extension , 707);
1660   message->AddExtension(unittest::packed_fixed64_extension , 708);
1661   message->AddExtension(unittest::packed_sfixed32_extension, 709);
1662   message->AddExtension(unittest::packed_sfixed64_extension, 710);
1663   message->AddExtension(unittest::packed_float_extension   , 711);
1664   message->AddExtension(unittest::packed_double_extension  , 712);
1665   message->AddExtension(unittest::packed_bool_extension    , false);
1666   message->AddExtension(unittest::packed_enum_extension, unittest::FOREIGN_BAZ);
1667 }
1668 
1669 // -------------------------------------------------------------------
1670 
ModifyPackedExtensions(unittest::TestPackedExtensions * message)1671 void TestUtil::ModifyPackedExtensions(unittest::TestPackedExtensions* message) {
1672   message->SetExtension(unittest::packed_int32_extension   , 1, 801);
1673   message->SetExtension(unittest::packed_int64_extension   , 1, 802);
1674   message->SetExtension(unittest::packed_uint32_extension  , 1, 803);
1675   message->SetExtension(unittest::packed_uint64_extension  , 1, 804);
1676   message->SetExtension(unittest::packed_sint32_extension  , 1, 805);
1677   message->SetExtension(unittest::packed_sint64_extension  , 1, 806);
1678   message->SetExtension(unittest::packed_fixed32_extension , 1, 807);
1679   message->SetExtension(unittest::packed_fixed64_extension , 1, 808);
1680   message->SetExtension(unittest::packed_sfixed32_extension, 1, 809);
1681   message->SetExtension(unittest::packed_sfixed64_extension, 1, 810);
1682   message->SetExtension(unittest::packed_float_extension   , 1, 811);
1683   message->SetExtension(unittest::packed_double_extension  , 1, 812);
1684   message->SetExtension(unittest::packed_bool_extension    , 1, true);
1685   message->SetExtension(unittest::packed_enum_extension    , 1,
1686                         unittest::FOREIGN_FOO);
1687 }
1688 
1689 // -------------------------------------------------------------------
1690 
ExpectPackedExtensionsSet(const unittest::TestPackedExtensions & message)1691 void TestUtil::ExpectPackedExtensionsSet(
1692     const unittest::TestPackedExtensions& message) {
1693   ASSERT_EQ(2, message.ExtensionSize(unittest::packed_int32_extension   ));
1694   ASSERT_EQ(2, message.ExtensionSize(unittest::packed_int64_extension   ));
1695   ASSERT_EQ(2, message.ExtensionSize(unittest::packed_uint32_extension  ));
1696   ASSERT_EQ(2, message.ExtensionSize(unittest::packed_uint64_extension  ));
1697   ASSERT_EQ(2, message.ExtensionSize(unittest::packed_sint32_extension  ));
1698   ASSERT_EQ(2, message.ExtensionSize(unittest::packed_sint64_extension  ));
1699   ASSERT_EQ(2, message.ExtensionSize(unittest::packed_fixed32_extension ));
1700   ASSERT_EQ(2, message.ExtensionSize(unittest::packed_fixed64_extension ));
1701   ASSERT_EQ(2, message.ExtensionSize(unittest::packed_sfixed32_extension));
1702   ASSERT_EQ(2, message.ExtensionSize(unittest::packed_sfixed64_extension));
1703   ASSERT_EQ(2, message.ExtensionSize(unittest::packed_float_extension   ));
1704   ASSERT_EQ(2, message.ExtensionSize(unittest::packed_double_extension  ));
1705   ASSERT_EQ(2, message.ExtensionSize(unittest::packed_bool_extension    ));
1706   ASSERT_EQ(2, message.ExtensionSize(unittest::packed_enum_extension    ));
1707 
1708   EXPECT_EQ(601  , message.GetExtension(unittest::packed_int32_extension   , 0));
1709   EXPECT_EQ(602  , message.GetExtension(unittest::packed_int64_extension   , 0));
1710   EXPECT_EQ(603  , message.GetExtension(unittest::packed_uint32_extension  , 0));
1711   EXPECT_EQ(604  , message.GetExtension(unittest::packed_uint64_extension  , 0));
1712   EXPECT_EQ(605  , message.GetExtension(unittest::packed_sint32_extension  , 0));
1713   EXPECT_EQ(606  , message.GetExtension(unittest::packed_sint64_extension  , 0));
1714   EXPECT_EQ(607  , message.GetExtension(unittest::packed_fixed32_extension , 0));
1715   EXPECT_EQ(608  , message.GetExtension(unittest::packed_fixed64_extension , 0));
1716   EXPECT_EQ(609  , message.GetExtension(unittest::packed_sfixed32_extension, 0));
1717   EXPECT_EQ(610  , message.GetExtension(unittest::packed_sfixed64_extension, 0));
1718   EXPECT_EQ(611  , message.GetExtension(unittest::packed_float_extension   , 0));
1719   EXPECT_EQ(612  , message.GetExtension(unittest::packed_double_extension  , 0));
1720   EXPECT_TRUE(     message.GetExtension(unittest::packed_bool_extension    , 0));
1721   EXPECT_EQ(unittest::FOREIGN_BAR,
1722             message.GetExtension(unittest::packed_enum_extension, 0));
1723   EXPECT_EQ(701  , message.GetExtension(unittest::packed_int32_extension   , 1));
1724   EXPECT_EQ(702  , message.GetExtension(unittest::packed_int64_extension   , 1));
1725   EXPECT_EQ(703  , message.GetExtension(unittest::packed_uint32_extension  , 1));
1726   EXPECT_EQ(704  , message.GetExtension(unittest::packed_uint64_extension  , 1));
1727   EXPECT_EQ(705  , message.GetExtension(unittest::packed_sint32_extension  , 1));
1728   EXPECT_EQ(706  , message.GetExtension(unittest::packed_sint64_extension  , 1));
1729   EXPECT_EQ(707  , message.GetExtension(unittest::packed_fixed32_extension , 1));
1730   EXPECT_EQ(708  , message.GetExtension(unittest::packed_fixed64_extension , 1));
1731   EXPECT_EQ(709  , message.GetExtension(unittest::packed_sfixed32_extension, 1));
1732   EXPECT_EQ(710  , message.GetExtension(unittest::packed_sfixed64_extension, 1));
1733   EXPECT_EQ(711  , message.GetExtension(unittest::packed_float_extension   , 1));
1734   EXPECT_EQ(712  , message.GetExtension(unittest::packed_double_extension  , 1));
1735   EXPECT_FALSE(    message.GetExtension(unittest::packed_bool_extension    , 1));
1736   EXPECT_EQ(unittest::FOREIGN_BAZ,
1737             message.GetExtension(unittest::packed_enum_extension, 1));
1738 }
1739 
1740 // -------------------------------------------------------------------
1741 
ExpectPackedExtensionsClear(const unittest::TestPackedExtensions & message)1742 void TestUtil::ExpectPackedExtensionsClear(
1743     const unittest::TestPackedExtensions& message) {
1744   EXPECT_EQ(0, message.ExtensionSize(unittest::packed_int32_extension   ));
1745   EXPECT_EQ(0, message.ExtensionSize(unittest::packed_int64_extension   ));
1746   EXPECT_EQ(0, message.ExtensionSize(unittest::packed_uint32_extension  ));
1747   EXPECT_EQ(0, message.ExtensionSize(unittest::packed_uint64_extension  ));
1748   EXPECT_EQ(0, message.ExtensionSize(unittest::packed_sint32_extension  ));
1749   EXPECT_EQ(0, message.ExtensionSize(unittest::packed_sint64_extension  ));
1750   EXPECT_EQ(0, message.ExtensionSize(unittest::packed_fixed32_extension ));
1751   EXPECT_EQ(0, message.ExtensionSize(unittest::packed_fixed64_extension ));
1752   EXPECT_EQ(0, message.ExtensionSize(unittest::packed_sfixed32_extension));
1753   EXPECT_EQ(0, message.ExtensionSize(unittest::packed_sfixed64_extension));
1754   EXPECT_EQ(0, message.ExtensionSize(unittest::packed_float_extension   ));
1755   EXPECT_EQ(0, message.ExtensionSize(unittest::packed_double_extension  ));
1756   EXPECT_EQ(0, message.ExtensionSize(unittest::packed_bool_extension    ));
1757   EXPECT_EQ(0, message.ExtensionSize(unittest::packed_enum_extension    ));
1758 }
1759 
1760 // -------------------------------------------------------------------
1761 
ExpectPackedExtensionsModified(const unittest::TestPackedExtensions & message)1762 void TestUtil::ExpectPackedExtensionsModified(
1763     const unittest::TestPackedExtensions& message) {
1764   ASSERT_EQ(2, message.ExtensionSize(unittest::packed_int32_extension   ));
1765   ASSERT_EQ(2, message.ExtensionSize(unittest::packed_int64_extension   ));
1766   ASSERT_EQ(2, message.ExtensionSize(unittest::packed_uint32_extension  ));
1767   ASSERT_EQ(2, message.ExtensionSize(unittest::packed_uint64_extension  ));
1768   ASSERT_EQ(2, message.ExtensionSize(unittest::packed_sint32_extension  ));
1769   ASSERT_EQ(2, message.ExtensionSize(unittest::packed_sint64_extension  ));
1770   ASSERT_EQ(2, message.ExtensionSize(unittest::packed_fixed32_extension ));
1771   ASSERT_EQ(2, message.ExtensionSize(unittest::packed_fixed64_extension ));
1772   ASSERT_EQ(2, message.ExtensionSize(unittest::packed_sfixed32_extension));
1773   ASSERT_EQ(2, message.ExtensionSize(unittest::packed_sfixed64_extension));
1774   ASSERT_EQ(2, message.ExtensionSize(unittest::packed_float_extension   ));
1775   ASSERT_EQ(2, message.ExtensionSize(unittest::packed_double_extension  ));
1776   ASSERT_EQ(2, message.ExtensionSize(unittest::packed_bool_extension    ));
1777   ASSERT_EQ(2, message.ExtensionSize(unittest::packed_enum_extension    ));
1778   EXPECT_EQ(601  , message.GetExtension(unittest::packed_int32_extension   , 0));
1779   EXPECT_EQ(602  , message.GetExtension(unittest::packed_int64_extension   , 0));
1780   EXPECT_EQ(603  , message.GetExtension(unittest::packed_uint32_extension  , 0));
1781   EXPECT_EQ(604  , message.GetExtension(unittest::packed_uint64_extension  , 0));
1782   EXPECT_EQ(605  , message.GetExtension(unittest::packed_sint32_extension  , 0));
1783   EXPECT_EQ(606  , message.GetExtension(unittest::packed_sint64_extension  , 0));
1784   EXPECT_EQ(607  , message.GetExtension(unittest::packed_fixed32_extension , 0));
1785   EXPECT_EQ(608  , message.GetExtension(unittest::packed_fixed64_extension , 0));
1786   EXPECT_EQ(609  , message.GetExtension(unittest::packed_sfixed32_extension, 0));
1787   EXPECT_EQ(610  , message.GetExtension(unittest::packed_sfixed64_extension, 0));
1788   EXPECT_EQ(611  , message.GetExtension(unittest::packed_float_extension   , 0));
1789   EXPECT_EQ(612  , message.GetExtension(unittest::packed_double_extension  , 0));
1790   EXPECT_TRUE(     message.GetExtension(unittest::packed_bool_extension    , 0));
1791   EXPECT_EQ(unittest::FOREIGN_BAR,
1792             message.GetExtension(unittest::packed_enum_extension, 0));
1793 
1794   // Actually verify the second (modified) elements now.
1795   EXPECT_EQ(801  , message.GetExtension(unittest::packed_int32_extension   , 1));
1796   EXPECT_EQ(802  , message.GetExtension(unittest::packed_int64_extension   , 1));
1797   EXPECT_EQ(803  , message.GetExtension(unittest::packed_uint32_extension  , 1));
1798   EXPECT_EQ(804  , message.GetExtension(unittest::packed_uint64_extension  , 1));
1799   EXPECT_EQ(805  , message.GetExtension(unittest::packed_sint32_extension  , 1));
1800   EXPECT_EQ(806  , message.GetExtension(unittest::packed_sint64_extension  , 1));
1801   EXPECT_EQ(807  , message.GetExtension(unittest::packed_fixed32_extension , 1));
1802   EXPECT_EQ(808  , message.GetExtension(unittest::packed_fixed64_extension , 1));
1803   EXPECT_EQ(809  , message.GetExtension(unittest::packed_sfixed32_extension, 1));
1804   EXPECT_EQ(810  , message.GetExtension(unittest::packed_sfixed64_extension, 1));
1805   EXPECT_EQ(811  , message.GetExtension(unittest::packed_float_extension   , 1));
1806   EXPECT_EQ(812  , message.GetExtension(unittest::packed_double_extension  , 1));
1807   EXPECT_TRUE(     message.GetExtension(unittest::packed_bool_extension    , 1));
1808   EXPECT_EQ(unittest::FOREIGN_FOO,
1809             message.GetExtension(unittest::packed_enum_extension, 1));
1810 }
1811 
1812 // -------------------------------------------------------------------
1813 
ExpectUnpackedExtensionsSet(const unittest::TestUnpackedExtensions & message)1814 void TestUtil::ExpectUnpackedExtensionsSet(
1815     const unittest::TestUnpackedExtensions& message) {
1816   ASSERT_EQ(2, message.ExtensionSize(unittest::unpacked_int32_extension   ));
1817   ASSERT_EQ(2, message.ExtensionSize(unittest::unpacked_int64_extension   ));
1818   ASSERT_EQ(2, message.ExtensionSize(unittest::unpacked_uint32_extension  ));
1819   ASSERT_EQ(2, message.ExtensionSize(unittest::unpacked_uint64_extension  ));
1820   ASSERT_EQ(2, message.ExtensionSize(unittest::unpacked_sint32_extension  ));
1821   ASSERT_EQ(2, message.ExtensionSize(unittest::unpacked_sint64_extension  ));
1822   ASSERT_EQ(2, message.ExtensionSize(unittest::unpacked_fixed32_extension ));
1823   ASSERT_EQ(2, message.ExtensionSize(unittest::unpacked_fixed64_extension ));
1824   ASSERT_EQ(2, message.ExtensionSize(unittest::unpacked_sfixed32_extension));
1825   ASSERT_EQ(2, message.ExtensionSize(unittest::unpacked_sfixed64_extension));
1826   ASSERT_EQ(2, message.ExtensionSize(unittest::unpacked_float_extension   ));
1827   ASSERT_EQ(2, message.ExtensionSize(unittest::unpacked_double_extension  ));
1828   ASSERT_EQ(2, message.ExtensionSize(unittest::unpacked_bool_extension    ));
1829   ASSERT_EQ(2, message.ExtensionSize(unittest::unpacked_enum_extension    ));
1830 
1831   EXPECT_EQ(601  , message.GetExtension(unittest::unpacked_int32_extension   , 0));
1832   EXPECT_EQ(602  , message.GetExtension(unittest::unpacked_int64_extension   , 0));
1833   EXPECT_EQ(603  , message.GetExtension(unittest::unpacked_uint32_extension  , 0));
1834   EXPECT_EQ(604  , message.GetExtension(unittest::unpacked_uint64_extension  , 0));
1835   EXPECT_EQ(605  , message.GetExtension(unittest::unpacked_sint32_extension  , 0));
1836   EXPECT_EQ(606  , message.GetExtension(unittest::unpacked_sint64_extension  , 0));
1837   EXPECT_EQ(607  , message.GetExtension(unittest::unpacked_fixed32_extension , 0));
1838   EXPECT_EQ(608  , message.GetExtension(unittest::unpacked_fixed64_extension , 0));
1839   EXPECT_EQ(609  , message.GetExtension(unittest::unpacked_sfixed32_extension, 0));
1840   EXPECT_EQ(610  , message.GetExtension(unittest::unpacked_sfixed64_extension, 0));
1841   EXPECT_EQ(611  , message.GetExtension(unittest::unpacked_float_extension   , 0));
1842   EXPECT_EQ(612  , message.GetExtension(unittest::unpacked_double_extension  , 0));
1843   EXPECT_EQ(true , message.GetExtension(unittest::unpacked_bool_extension    , 0));
1844   EXPECT_EQ(unittest::FOREIGN_BAR,
1845             message.GetExtension(unittest::unpacked_enum_extension, 0));
1846   EXPECT_EQ(701  , message.GetExtension(unittest::unpacked_int32_extension   , 1));
1847   EXPECT_EQ(702  , message.GetExtension(unittest::unpacked_int64_extension   , 1));
1848   EXPECT_EQ(703  , message.GetExtension(unittest::unpacked_uint32_extension  , 1));
1849   EXPECT_EQ(704  , message.GetExtension(unittest::unpacked_uint64_extension  , 1));
1850   EXPECT_EQ(705  , message.GetExtension(unittest::unpacked_sint32_extension  , 1));
1851   EXPECT_EQ(706  , message.GetExtension(unittest::unpacked_sint64_extension  , 1));
1852   EXPECT_EQ(707  , message.GetExtension(unittest::unpacked_fixed32_extension , 1));
1853   EXPECT_EQ(708  , message.GetExtension(unittest::unpacked_fixed64_extension , 1));
1854   EXPECT_EQ(709  , message.GetExtension(unittest::unpacked_sfixed32_extension, 1));
1855   EXPECT_EQ(710  , message.GetExtension(unittest::unpacked_sfixed64_extension, 1));
1856   EXPECT_EQ(711  , message.GetExtension(unittest::unpacked_float_extension   , 1));
1857   EXPECT_EQ(712  , message.GetExtension(unittest::unpacked_double_extension  , 1));
1858   EXPECT_EQ(false, message.GetExtension(unittest::unpacked_bool_extension    , 1));
1859   EXPECT_EQ(unittest::FOREIGN_BAZ,
1860             message.GetExtension(unittest::unpacked_enum_extension, 1));
1861 }
1862 
1863 // -------------------------------------------------------------------
1864 
ExpectAllFieldsAndExtensionsInOrder(const string & serialized)1865 void TestUtil::ExpectAllFieldsAndExtensionsInOrder(const string& serialized) {
1866   // We set each field individually, serialize separately, and concatenate all
1867   // the strings in canonical order to determine the expected serialization.
1868   string expected;
1869   unittest::TestFieldOrderings message;
1870   message.set_my_int(1);  // Field 1.
1871   message.AppendToString(&expected);
1872   message.Clear();
1873   message.SetExtension(unittest::my_extension_int, 23);  // Field 5.
1874   message.AppendToString(&expected);
1875   message.Clear();
1876   message.set_my_string("foo");  // Field 11.
1877   message.AppendToString(&expected);
1878   message.Clear();
1879   message.SetExtension(unittest::my_extension_string, "bar");  // Field 50.
1880   message.AppendToString(&expected);
1881   message.Clear();
1882   message.set_my_float(1.0);  // Field 101.
1883   message.AppendToString(&expected);
1884   message.Clear();
1885 
1886   // We don't EXPECT_EQ() since we don't want to print raw bytes to stdout.
1887   EXPECT_TRUE(serialized == expected);
1888 }
1889 
ExpectLastRepeatedsRemoved(const unittest::TestAllTypes & message)1890 void TestUtil::ExpectLastRepeatedsRemoved(
1891     const unittest::TestAllTypes& message) {
1892   ASSERT_EQ(1, message.repeated_int32_size   ());
1893   ASSERT_EQ(1, message.repeated_int64_size   ());
1894   ASSERT_EQ(1, message.repeated_uint32_size  ());
1895   ASSERT_EQ(1, message.repeated_uint64_size  ());
1896   ASSERT_EQ(1, message.repeated_sint32_size  ());
1897   ASSERT_EQ(1, message.repeated_sint64_size  ());
1898   ASSERT_EQ(1, message.repeated_fixed32_size ());
1899   ASSERT_EQ(1, message.repeated_fixed64_size ());
1900   ASSERT_EQ(1, message.repeated_sfixed32_size());
1901   ASSERT_EQ(1, message.repeated_sfixed64_size());
1902   ASSERT_EQ(1, message.repeated_float_size   ());
1903   ASSERT_EQ(1, message.repeated_double_size  ());
1904   ASSERT_EQ(1, message.repeated_bool_size    ());
1905   ASSERT_EQ(1, message.repeated_string_size  ());
1906   ASSERT_EQ(1, message.repeated_bytes_size   ());
1907 
1908   ASSERT_EQ(1, message.repeatedgroup_size           ());
1909   ASSERT_EQ(1, message.repeated_nested_message_size ());
1910   ASSERT_EQ(1, message.repeated_foreign_message_size());
1911   ASSERT_EQ(1, message.repeated_import_message_size ());
1912   ASSERT_EQ(1, message.repeated_import_message_size ());
1913   ASSERT_EQ(1, message.repeated_nested_enum_size    ());
1914   ASSERT_EQ(1, message.repeated_foreign_enum_size   ());
1915   ASSERT_EQ(1, message.repeated_import_enum_size    ());
1916 
1917 #ifndef PROTOBUF_TEST_NO_DESCRIPTORS
1918   ASSERT_EQ(1, message.repeated_string_piece_size());
1919   ASSERT_EQ(1, message.repeated_cord_size());
1920 #endif
1921 
1922   // Test that the remaining element is the correct one.
1923   EXPECT_EQ(201  , message.repeated_int32   (0));
1924   EXPECT_EQ(202  , message.repeated_int64   (0));
1925   EXPECT_EQ(203  , message.repeated_uint32  (0));
1926   EXPECT_EQ(204  , message.repeated_uint64  (0));
1927   EXPECT_EQ(205  , message.repeated_sint32  (0));
1928   EXPECT_EQ(206  , message.repeated_sint64  (0));
1929   EXPECT_EQ(207  , message.repeated_fixed32 (0));
1930   EXPECT_EQ(208  , message.repeated_fixed64 (0));
1931   EXPECT_EQ(209  , message.repeated_sfixed32(0));
1932   EXPECT_EQ(210  , message.repeated_sfixed64(0));
1933   EXPECT_EQ(211  , message.repeated_float   (0));
1934   EXPECT_EQ(212  , message.repeated_double  (0));
1935   EXPECT_TRUE(     message.repeated_bool    (0));
1936   EXPECT_EQ("215", message.repeated_string  (0));
1937   EXPECT_EQ("216", message.repeated_bytes   (0));
1938 
1939   EXPECT_EQ(217, message.repeatedgroup           (0).a());
1940   EXPECT_EQ(218, message.repeated_nested_message (0).bb());
1941   EXPECT_EQ(219, message.repeated_foreign_message(0).c());
1942   EXPECT_EQ(220, message.repeated_import_message (0).d());
1943   EXPECT_EQ(220, message.repeated_import_message (0).d());
1944 
1945   EXPECT_EQ(unittest::TestAllTypes::BAR, message.repeated_nested_enum (0));
1946   EXPECT_EQ(unittest::FOREIGN_BAR      , message.repeated_foreign_enum(0));
1947   EXPECT_EQ(unittest_import::IMPORT_BAR, message.repeated_import_enum (0));
1948 }
1949 
ExpectLastRepeatedExtensionsRemoved(const unittest::TestAllExtensions & message)1950 void TestUtil::ExpectLastRepeatedExtensionsRemoved(
1951     const unittest::TestAllExtensions& message) {
1952 
1953   // Test that one element was removed.
1954   ASSERT_EQ(1, message.ExtensionSize(unittest::repeated_int32_extension   ));
1955   ASSERT_EQ(1, message.ExtensionSize(unittest::repeated_int64_extension   ));
1956   ASSERT_EQ(1, message.ExtensionSize(unittest::repeated_uint32_extension  ));
1957   ASSERT_EQ(1, message.ExtensionSize(unittest::repeated_uint64_extension  ));
1958   ASSERT_EQ(1, message.ExtensionSize(unittest::repeated_sint32_extension  ));
1959   ASSERT_EQ(1, message.ExtensionSize(unittest::repeated_sint64_extension  ));
1960   ASSERT_EQ(1, message.ExtensionSize(unittest::repeated_fixed32_extension ));
1961   ASSERT_EQ(1, message.ExtensionSize(unittest::repeated_fixed64_extension ));
1962   ASSERT_EQ(1, message.ExtensionSize(unittest::repeated_sfixed32_extension));
1963   ASSERT_EQ(1, message.ExtensionSize(unittest::repeated_sfixed64_extension));
1964   ASSERT_EQ(1, message.ExtensionSize(unittest::repeated_float_extension   ));
1965   ASSERT_EQ(1, message.ExtensionSize(unittest::repeated_double_extension  ));
1966   ASSERT_EQ(1, message.ExtensionSize(unittest::repeated_bool_extension    ));
1967   ASSERT_EQ(1, message.ExtensionSize(unittest::repeated_string_extension  ));
1968   ASSERT_EQ(1, message.ExtensionSize(unittest::repeated_bytes_extension   ));
1969 
1970   ASSERT_EQ(1, message.ExtensionSize(unittest::repeatedgroup_extension           ));
1971   ASSERT_EQ(1, message.ExtensionSize(unittest::repeated_nested_message_extension ));
1972   ASSERT_EQ(1, message.ExtensionSize(unittest::repeated_foreign_message_extension));
1973   ASSERT_EQ(1, message.ExtensionSize(unittest::repeated_import_message_extension ));
1974   ASSERT_EQ(1, message.ExtensionSize(unittest::repeated_lazy_message_extension   ));
1975   ASSERT_EQ(1, message.ExtensionSize(unittest::repeated_nested_enum_extension    ));
1976   ASSERT_EQ(1, message.ExtensionSize(unittest::repeated_foreign_enum_extension   ));
1977   ASSERT_EQ(1, message.ExtensionSize(unittest::repeated_import_enum_extension    ));
1978 
1979   ASSERT_EQ(1, message.ExtensionSize(unittest::repeated_string_piece_extension));
1980   ASSERT_EQ(1, message.ExtensionSize(unittest::repeated_cord_extension));
1981 
1982   // Test that the remaining element is the correct one.
1983   EXPECT_EQ(201  , message.GetExtension(unittest::repeated_int32_extension   , 0));
1984   EXPECT_EQ(202  , message.GetExtension(unittest::repeated_int64_extension   , 0));
1985   EXPECT_EQ(203  , message.GetExtension(unittest::repeated_uint32_extension  , 0));
1986   EXPECT_EQ(204  , message.GetExtension(unittest::repeated_uint64_extension  , 0));
1987   EXPECT_EQ(205  , message.GetExtension(unittest::repeated_sint32_extension  , 0));
1988   EXPECT_EQ(206  , message.GetExtension(unittest::repeated_sint64_extension  , 0));
1989   EXPECT_EQ(207  , message.GetExtension(unittest::repeated_fixed32_extension , 0));
1990   EXPECT_EQ(208  , message.GetExtension(unittest::repeated_fixed64_extension , 0));
1991   EXPECT_EQ(209  , message.GetExtension(unittest::repeated_sfixed32_extension, 0));
1992   EXPECT_EQ(210  , message.GetExtension(unittest::repeated_sfixed64_extension, 0));
1993   EXPECT_EQ(211  , message.GetExtension(unittest::repeated_float_extension   , 0));
1994   EXPECT_EQ(212  , message.GetExtension(unittest::repeated_double_extension  , 0));
1995   EXPECT_TRUE(     message.GetExtension(unittest::repeated_bool_extension    , 0));
1996   EXPECT_EQ("215", message.GetExtension(unittest::repeated_string_extension  , 0));
1997   EXPECT_EQ("216", message.GetExtension(unittest::repeated_bytes_extension   , 0));
1998 
1999   EXPECT_EQ(217, message.GetExtension(unittest::repeatedgroup_extension           , 0).a());
2000   EXPECT_EQ(218, message.GetExtension(unittest::repeated_nested_message_extension , 0).bb());
2001   EXPECT_EQ(219, message.GetExtension(unittest::repeated_foreign_message_extension, 0).c());
2002   EXPECT_EQ(220, message.GetExtension(unittest::repeated_import_message_extension , 0).d());
2003   EXPECT_EQ(227, message.GetExtension(unittest::repeated_lazy_message_extension   , 0).bb());
2004 
2005   EXPECT_EQ(unittest::TestAllTypes::BAR, message.GetExtension(unittest::repeated_nested_enum_extension , 0));
2006   EXPECT_EQ(unittest::FOREIGN_BAR      , message.GetExtension(unittest::repeated_foreign_enum_extension, 0));
2007   EXPECT_EQ(unittest_import::IMPORT_BAR, message.GetExtension(unittest::repeated_import_enum_extension , 0));
2008 
2009   EXPECT_EQ("224", message.GetExtension(unittest::repeated_string_piece_extension, 0));
2010   EXPECT_EQ("225", message.GetExtension(unittest::repeated_cord_extension, 0));
2011 }
2012 
ExpectLastRepeatedsReleased(const unittest::TestAllTypes & message)2013 void TestUtil::ExpectLastRepeatedsReleased(
2014     const unittest::TestAllTypes& message) {
2015   ASSERT_EQ(1, message.repeatedgroup_size           ());
2016   ASSERT_EQ(1, message.repeated_nested_message_size ());
2017   ASSERT_EQ(1, message.repeated_foreign_message_size());
2018   ASSERT_EQ(1, message.repeated_import_message_size ());
2019   ASSERT_EQ(1, message.repeated_import_message_size ());
2020 
2021   EXPECT_EQ(217, message.repeatedgroup           (0).a());
2022   EXPECT_EQ(218, message.repeated_nested_message (0).bb());
2023   EXPECT_EQ(219, message.repeated_foreign_message(0).c());
2024   EXPECT_EQ(220, message.repeated_import_message (0).d());
2025   EXPECT_EQ(220, message.repeated_import_message (0).d());
2026 }
2027 
ExpectLastRepeatedExtensionsReleased(const unittest::TestAllExtensions & message)2028 void TestUtil::ExpectLastRepeatedExtensionsReleased(
2029     const unittest::TestAllExtensions& message) {
2030   ASSERT_EQ(1, message.ExtensionSize(unittest::repeatedgroup_extension           ));
2031   ASSERT_EQ(1, message.ExtensionSize(unittest::repeated_nested_message_extension ));
2032   ASSERT_EQ(1, message.ExtensionSize(unittest::repeated_foreign_message_extension));
2033   ASSERT_EQ(1, message.ExtensionSize(unittest::repeated_import_message_extension ));
2034   ASSERT_EQ(1, message.ExtensionSize(unittest::repeated_lazy_message_extension   ));
2035 
2036   EXPECT_EQ(217, message.GetExtension(unittest::repeatedgroup_extension           , 0).a());
2037   EXPECT_EQ(218, message.GetExtension(unittest::repeated_nested_message_extension , 0).bb());
2038   EXPECT_EQ(219, message.GetExtension(unittest::repeated_foreign_message_extension, 0).c());
2039   EXPECT_EQ(220, message.GetExtension(unittest::repeated_import_message_extension , 0).d());
2040   EXPECT_EQ(227, message.GetExtension(unittest::repeated_lazy_message_extension   , 0).bb());
2041 }
2042 
ExpectRepeatedsSwapped(const unittest::TestAllTypes & message)2043 void TestUtil::ExpectRepeatedsSwapped(
2044     const unittest::TestAllTypes& message) {
2045   ASSERT_EQ(2, message.repeated_int32_size   ());
2046   ASSERT_EQ(2, message.repeated_int64_size   ());
2047   ASSERT_EQ(2, message.repeated_uint32_size  ());
2048   ASSERT_EQ(2, message.repeated_uint64_size  ());
2049   ASSERT_EQ(2, message.repeated_sint32_size  ());
2050   ASSERT_EQ(2, message.repeated_sint64_size  ());
2051   ASSERT_EQ(2, message.repeated_fixed32_size ());
2052   ASSERT_EQ(2, message.repeated_fixed64_size ());
2053   ASSERT_EQ(2, message.repeated_sfixed32_size());
2054   ASSERT_EQ(2, message.repeated_sfixed64_size());
2055   ASSERT_EQ(2, message.repeated_float_size   ());
2056   ASSERT_EQ(2, message.repeated_double_size  ());
2057   ASSERT_EQ(2, message.repeated_bool_size    ());
2058   ASSERT_EQ(2, message.repeated_string_size  ());
2059   ASSERT_EQ(2, message.repeated_bytes_size   ());
2060 
2061   ASSERT_EQ(2, message.repeatedgroup_size           ());
2062   ASSERT_EQ(2, message.repeated_nested_message_size ());
2063   ASSERT_EQ(2, message.repeated_foreign_message_size());
2064   ASSERT_EQ(2, message.repeated_import_message_size ());
2065   ASSERT_EQ(2, message.repeated_import_message_size ());
2066   ASSERT_EQ(2, message.repeated_nested_enum_size    ());
2067   ASSERT_EQ(2, message.repeated_foreign_enum_size   ());
2068   ASSERT_EQ(2, message.repeated_import_enum_size    ());
2069 
2070 #ifndef PROTOBUF_TEST_NO_DESCRIPTORS
2071   ASSERT_EQ(2, message.repeated_string_piece_size());
2072   ASSERT_EQ(2, message.repeated_cord_size());
2073 #endif
2074 
2075   // Test that the first element and second element are flipped.
2076   EXPECT_EQ(201  , message.repeated_int32   (1));
2077   EXPECT_EQ(202  , message.repeated_int64   (1));
2078   EXPECT_EQ(203  , message.repeated_uint32  (1));
2079   EXPECT_EQ(204  , message.repeated_uint64  (1));
2080   EXPECT_EQ(205  , message.repeated_sint32  (1));
2081   EXPECT_EQ(206  , message.repeated_sint64  (1));
2082   EXPECT_EQ(207  , message.repeated_fixed32 (1));
2083   EXPECT_EQ(208  , message.repeated_fixed64 (1));
2084   EXPECT_EQ(209  , message.repeated_sfixed32(1));
2085   EXPECT_EQ(210  , message.repeated_sfixed64(1));
2086   EXPECT_EQ(211  , message.repeated_float   (1));
2087   EXPECT_EQ(212  , message.repeated_double  (1));
2088   EXPECT_TRUE(     message.repeated_bool    (1));
2089   EXPECT_EQ("215", message.repeated_string  (1));
2090   EXPECT_EQ("216", message.repeated_bytes   (1));
2091 
2092   EXPECT_EQ(217, message.repeatedgroup           (1).a());
2093   EXPECT_EQ(218, message.repeated_nested_message (1).bb());
2094   EXPECT_EQ(219, message.repeated_foreign_message(1).c());
2095   EXPECT_EQ(220, message.repeated_import_message (1).d());
2096   EXPECT_EQ(220, message.repeated_import_message (1).d());
2097 
2098   EXPECT_EQ(unittest::TestAllTypes::BAR, message.repeated_nested_enum (1));
2099   EXPECT_EQ(unittest::FOREIGN_BAR      , message.repeated_foreign_enum(1));
2100   EXPECT_EQ(unittest_import::IMPORT_BAR, message.repeated_import_enum (1));
2101 
2102   EXPECT_EQ(301  , message.repeated_int32   (0));
2103   EXPECT_EQ(302  , message.repeated_int64   (0));
2104   EXPECT_EQ(303  , message.repeated_uint32  (0));
2105   EXPECT_EQ(304  , message.repeated_uint64  (0));
2106   EXPECT_EQ(305  , message.repeated_sint32  (0));
2107   EXPECT_EQ(306  , message.repeated_sint64  (0));
2108   EXPECT_EQ(307  , message.repeated_fixed32 (0));
2109   EXPECT_EQ(308  , message.repeated_fixed64 (0));
2110   EXPECT_EQ(309  , message.repeated_sfixed32(0));
2111   EXPECT_EQ(310  , message.repeated_sfixed64(0));
2112   EXPECT_EQ(311  , message.repeated_float   (0));
2113   EXPECT_EQ(312  , message.repeated_double  (0));
2114   EXPECT_FALSE(    message.repeated_bool    (0));
2115   EXPECT_EQ("315", message.repeated_string  (0));
2116   EXPECT_EQ("316", message.repeated_bytes   (0));
2117 
2118   EXPECT_EQ(317, message.repeatedgroup           (0).a());
2119   EXPECT_EQ(318, message.repeated_nested_message (0).bb());
2120   EXPECT_EQ(319, message.repeated_foreign_message(0).c());
2121   EXPECT_EQ(320, message.repeated_import_message (0).d());
2122   EXPECT_EQ(320, message.repeated_import_message (0).d());
2123 
2124   EXPECT_EQ(unittest::TestAllTypes::BAZ, message.repeated_nested_enum (0));
2125   EXPECT_EQ(unittest::FOREIGN_BAZ      , message.repeated_foreign_enum(0));
2126   EXPECT_EQ(unittest_import::IMPORT_BAZ, message.repeated_import_enum (0));
2127 }
2128 
ExpectRepeatedExtensionsSwapped(const unittest::TestAllExtensions & message)2129 void TestUtil::ExpectRepeatedExtensionsSwapped(
2130     const unittest::TestAllExtensions& message) {
2131 
2132   ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_int32_extension   ));
2133   ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_int64_extension   ));
2134   ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_uint32_extension  ));
2135   ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_uint64_extension  ));
2136   ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_sint32_extension  ));
2137   ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_sint64_extension  ));
2138   ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_fixed32_extension ));
2139   ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_fixed64_extension ));
2140   ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_sfixed32_extension));
2141   ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_sfixed64_extension));
2142   ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_float_extension   ));
2143   ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_double_extension  ));
2144   ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_bool_extension    ));
2145   ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_string_extension  ));
2146   ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_bytes_extension   ));
2147 
2148   ASSERT_EQ(2, message.ExtensionSize(unittest::repeatedgroup_extension           ));
2149   ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_nested_message_extension ));
2150   ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_foreign_message_extension));
2151   ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_import_message_extension ));
2152   ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_lazy_message_extension   ));
2153   ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_nested_enum_extension    ));
2154   ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_foreign_enum_extension   ));
2155   ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_import_enum_extension    ));
2156 
2157   ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_string_piece_extension));
2158   ASSERT_EQ(2, message.ExtensionSize(unittest::repeated_cord_extension));
2159 
2160   EXPECT_EQ(201  , message.GetExtension(unittest::repeated_int32_extension   , 1));
2161   EXPECT_EQ(202  , message.GetExtension(unittest::repeated_int64_extension   , 1));
2162   EXPECT_EQ(203  , message.GetExtension(unittest::repeated_uint32_extension  , 1));
2163   EXPECT_EQ(204  , message.GetExtension(unittest::repeated_uint64_extension  , 1));
2164   EXPECT_EQ(205  , message.GetExtension(unittest::repeated_sint32_extension  , 1));
2165   EXPECT_EQ(206  , message.GetExtension(unittest::repeated_sint64_extension  , 1));
2166   EXPECT_EQ(207  , message.GetExtension(unittest::repeated_fixed32_extension , 1));
2167   EXPECT_EQ(208  , message.GetExtension(unittest::repeated_fixed64_extension , 1));
2168   EXPECT_EQ(209  , message.GetExtension(unittest::repeated_sfixed32_extension, 1));
2169   EXPECT_EQ(210  , message.GetExtension(unittest::repeated_sfixed64_extension, 1));
2170   EXPECT_EQ(211  , message.GetExtension(unittest::repeated_float_extension   , 1));
2171   EXPECT_EQ(212  , message.GetExtension(unittest::repeated_double_extension  , 1));
2172   EXPECT_TRUE(     message.GetExtension(unittest::repeated_bool_extension    , 1));
2173   EXPECT_EQ("215", message.GetExtension(unittest::repeated_string_extension  , 1));
2174   EXPECT_EQ("216", message.GetExtension(unittest::repeated_bytes_extension   , 1));
2175 
2176   EXPECT_EQ(217, message.GetExtension(unittest::repeatedgroup_extension           , 1).a());
2177   EXPECT_EQ(218, message.GetExtension(unittest::repeated_nested_message_extension , 1).bb());
2178   EXPECT_EQ(219, message.GetExtension(unittest::repeated_foreign_message_extension, 1).c());
2179   EXPECT_EQ(220, message.GetExtension(unittest::repeated_import_message_extension , 1).d());
2180   EXPECT_EQ(227, message.GetExtension(unittest::repeated_lazy_message_extension   , 1).bb());
2181 
2182   EXPECT_EQ(unittest::TestAllTypes::BAR, message.GetExtension(unittest::repeated_nested_enum_extension , 1));
2183   EXPECT_EQ(unittest::FOREIGN_BAR      , message.GetExtension(unittest::repeated_foreign_enum_extension, 1));
2184   EXPECT_EQ(unittest_import::IMPORT_BAR, message.GetExtension(unittest::repeated_import_enum_extension , 1));
2185 
2186   EXPECT_EQ("224", message.GetExtension(unittest::repeated_string_piece_extension, 1));
2187   EXPECT_EQ("225", message.GetExtension(unittest::repeated_cord_extension, 1));
2188 
2189   EXPECT_EQ(301  , message.GetExtension(unittest::repeated_int32_extension   , 0));
2190   EXPECT_EQ(302  , message.GetExtension(unittest::repeated_int64_extension   , 0));
2191   EXPECT_EQ(303  , message.GetExtension(unittest::repeated_uint32_extension  , 0));
2192   EXPECT_EQ(304  , message.GetExtension(unittest::repeated_uint64_extension  , 0));
2193   EXPECT_EQ(305  , message.GetExtension(unittest::repeated_sint32_extension  , 0));
2194   EXPECT_EQ(306  , message.GetExtension(unittest::repeated_sint64_extension  , 0));
2195   EXPECT_EQ(307  , message.GetExtension(unittest::repeated_fixed32_extension , 0));
2196   EXPECT_EQ(308  , message.GetExtension(unittest::repeated_fixed64_extension , 0));
2197   EXPECT_EQ(309  , message.GetExtension(unittest::repeated_sfixed32_extension, 0));
2198   EXPECT_EQ(310  , message.GetExtension(unittest::repeated_sfixed64_extension, 0));
2199   EXPECT_EQ(311  , message.GetExtension(unittest::repeated_float_extension   , 0));
2200   EXPECT_EQ(312  , message.GetExtension(unittest::repeated_double_extension  , 0));
2201   EXPECT_FALSE(    message.GetExtension(unittest::repeated_bool_extension    , 0));
2202   EXPECT_EQ("315", message.GetExtension(unittest::repeated_string_extension  , 0));
2203   EXPECT_EQ("316", message.GetExtension(unittest::repeated_bytes_extension   , 0));
2204 
2205   EXPECT_EQ(317, message.GetExtension(unittest::repeatedgroup_extension           , 0).a());
2206   EXPECT_EQ(318, message.GetExtension(unittest::repeated_nested_message_extension , 0).bb());
2207   EXPECT_EQ(319, message.GetExtension(unittest::repeated_foreign_message_extension, 0).c());
2208   EXPECT_EQ(320, message.GetExtension(unittest::repeated_import_message_extension , 0).d());
2209   EXPECT_EQ(327, message.GetExtension(unittest::repeated_lazy_message_extension   , 0).bb());
2210 
2211   EXPECT_EQ(unittest::TestAllTypes::BAZ, message.GetExtension(unittest::repeated_nested_enum_extension , 0));
2212   EXPECT_EQ(unittest::FOREIGN_BAZ      , message.GetExtension(unittest::repeated_foreign_enum_extension, 0));
2213   EXPECT_EQ(unittest_import::IMPORT_BAZ, message.GetExtension(unittest::repeated_import_enum_extension , 0));
2214 
2215   EXPECT_EQ("324", message.GetExtension(unittest::repeated_string_piece_extension, 0));
2216   EXPECT_EQ("325", message.GetExtension(unittest::repeated_cord_extension, 0));
2217 }
2218 
SetOneof1(unittest::TestOneof2 * message)2219 void TestUtil::SetOneof1(unittest::TestOneof2* message) {
2220   message->mutable_foo_lazy_message()->set_qux_int(100);
2221   message->set_bar_string("101");
2222   message->set_baz_int(102);
2223   message->set_baz_string("103");
2224 }
2225 
SetOneof2(unittest::TestOneof2 * message)2226 void TestUtil::SetOneof2(unittest::TestOneof2* message) {
2227   message->set_foo_int(200);
2228   message->set_bar_enum(unittest::TestOneof2::BAZ);
2229   message->set_baz_int(202);
2230   message->set_baz_string("203");
2231 }
2232 
ExpectOneofSet1(const unittest::TestOneof2 & message)2233 void TestUtil::ExpectOneofSet1(const unittest::TestOneof2& message) {
2234   ExpectAtMostOneFieldSetInOneof(message);
2235 
2236   EXPECT_TRUE(message.has_foo_lazy_message          ());
2237   EXPECT_TRUE(message.foo_lazy_message().has_qux_int());
2238 
2239   EXPECT_TRUE(message.has_bar_string());
2240   EXPECT_TRUE(message.has_baz_int   ());
2241   EXPECT_TRUE(message.has_baz_string());
2242 
2243   ASSERT_EQ(0, message.foo_lazy_message().corge_int_size());
2244 
2245   EXPECT_EQ(100  , message.foo_lazy_message().qux_int());
2246   EXPECT_EQ("101", message.bar_string                ());
2247   EXPECT_EQ(102  , message.baz_int                   ());
2248   EXPECT_EQ("103", message.baz_string                ());
2249 }
2250 
ExpectOneofSet2(const unittest::TestOneof2 & message)2251 void TestUtil::ExpectOneofSet2(const unittest::TestOneof2& message) {
2252   ExpectAtMostOneFieldSetInOneof(message);
2253 
2254   EXPECT_TRUE(message.has_foo_int   ());
2255   EXPECT_TRUE(message.has_bar_enum  ());
2256   EXPECT_TRUE(message.has_baz_int   ());
2257   EXPECT_TRUE(message.has_baz_string());
2258 
2259   EXPECT_EQ(200                      , message.foo_int   ());
2260   EXPECT_EQ(unittest::TestOneof2::BAZ, message.bar_enum  ());
2261   EXPECT_EQ(202                      , message.baz_int   ());
2262   EXPECT_EQ("203"                    , message.baz_string());
2263 }
2264 
ExpectOneofClear(const unittest::TestOneof2 & message)2265 void TestUtil::ExpectOneofClear(const unittest::TestOneof2& message) {
2266   EXPECT_FALSE(message.has_foo_int());
2267   EXPECT_FALSE(message.has_foo_string());
2268   EXPECT_FALSE(message.has_foo_bytes());
2269   EXPECT_FALSE(message.has_foo_enum());
2270   EXPECT_FALSE(message.has_foo_message());
2271   EXPECT_FALSE(message.has_foogroup());
2272   EXPECT_FALSE(message.has_foo_lazy_message());
2273 
2274   EXPECT_FALSE(message.has_bar_int());
2275   EXPECT_FALSE(message.has_bar_string());
2276   EXPECT_FALSE(message.has_bar_bytes());
2277   EXPECT_FALSE(message.has_bar_enum());
2278 
2279   EXPECT_FALSE(message.has_baz_int());
2280   EXPECT_FALSE(message.has_baz_string());
2281 
2282   EXPECT_EQ(unittest::TestOneof2::FOO_NOT_SET, message.foo_case());
2283   EXPECT_EQ(unittest::TestOneof2::BAR_NOT_SET, message.bar_case());
2284 }
2285 
ExpectAtMostOneFieldSetInOneof(const unittest::TestOneof2 & message)2286 void TestUtil::ExpectAtMostOneFieldSetInOneof(
2287     const unittest::TestOneof2& message) {
2288   int count = 0;
2289   if (message.has_foo_int()) count++;
2290   if (message.has_foo_string()) count++;
2291   if (message.has_foo_bytes()) count++;
2292   if (message.has_foo_enum()) count++;
2293   if (message.has_foo_message()) count++;
2294   if (message.has_foogroup()) count++;
2295   if (message.has_foo_lazy_message()) count++;
2296   EXPECT_LE(count, 1);
2297   count = 0;
2298   if (message.has_bar_int()) count++;
2299   if (message.has_bar_string()) count++;
2300   if (message.has_bar_bytes()) count++;
2301   if (message.has_bar_enum()) count++;
2302   EXPECT_TRUE(count == 0 || count == 1);
2303 }
2304 
2305 // ===================================================================
2306 
ReflectionTester(const Descriptor * base_descriptor)2307 TestUtil::ReflectionTester::ReflectionTester(
2308     const Descriptor* base_descriptor)
2309   : base_descriptor_(base_descriptor) {
2310 
2311   const DescriptorPool* pool = base_descriptor->file()->pool();
2312 
2313   nested_b_ =
2314     pool->FindFieldByName("protobuf_unittest.TestAllTypes.NestedMessage.bb");
2315   foreign_c_ =
2316     pool->FindFieldByName("protobuf_unittest.ForeignMessage.c");
2317   import_d_ =
2318     pool->FindFieldByName("protobuf_unittest_import.ImportMessage.d");
2319   import_e_ =
2320     pool->FindFieldByName("protobuf_unittest_import.PublicImportMessage.e");
2321   nested_foo_ =
2322     pool->FindEnumValueByName("protobuf_unittest.TestAllTypes.FOO");
2323   nested_bar_ =
2324     pool->FindEnumValueByName("protobuf_unittest.TestAllTypes.BAR");
2325   nested_baz_ =
2326     pool->FindEnumValueByName("protobuf_unittest.TestAllTypes.BAZ");
2327   foreign_foo_ =
2328     pool->FindEnumValueByName("protobuf_unittest.FOREIGN_FOO");
2329   foreign_bar_ =
2330     pool->FindEnumValueByName("protobuf_unittest.FOREIGN_BAR");
2331   foreign_baz_ =
2332     pool->FindEnumValueByName("protobuf_unittest.FOREIGN_BAZ");
2333   import_foo_ =
2334     pool->FindEnumValueByName("protobuf_unittest_import.IMPORT_FOO");
2335   import_bar_ =
2336     pool->FindEnumValueByName("protobuf_unittest_import.IMPORT_BAR");
2337   import_baz_ =
2338     pool->FindEnumValueByName("protobuf_unittest_import.IMPORT_BAZ");
2339 
2340   if (base_descriptor_->name() == "TestAllExtensions") {
2341     group_a_ =
2342       pool->FindFieldByName("protobuf_unittest.OptionalGroup_extension.a");
2343     repeated_group_a_ =
2344       pool->FindFieldByName("protobuf_unittest.RepeatedGroup_extension.a");
2345   } else {
2346     group_a_ =
2347       pool->FindFieldByName("protobuf_unittest.TestAllTypes.OptionalGroup.a");
2348     repeated_group_a_ =
2349       pool->FindFieldByName("protobuf_unittest.TestAllTypes.RepeatedGroup.a");
2350   }
2351 
2352   EXPECT_TRUE(group_a_          != NULL);
2353   EXPECT_TRUE(repeated_group_a_ != NULL);
2354   EXPECT_TRUE(nested_b_         != NULL);
2355   EXPECT_TRUE(foreign_c_        != NULL);
2356   EXPECT_TRUE(import_d_         != NULL);
2357   EXPECT_TRUE(import_e_         != NULL);
2358   EXPECT_TRUE(nested_foo_       != NULL);
2359   EXPECT_TRUE(nested_bar_       != NULL);
2360   EXPECT_TRUE(nested_baz_       != NULL);
2361   EXPECT_TRUE(foreign_foo_      != NULL);
2362   EXPECT_TRUE(foreign_bar_      != NULL);
2363   EXPECT_TRUE(foreign_baz_      != NULL);
2364   EXPECT_TRUE(import_foo_       != NULL);
2365   EXPECT_TRUE(import_bar_       != NULL);
2366   EXPECT_TRUE(import_baz_       != NULL);
2367 }
2368 
2369 // Shorthand to get a FieldDescriptor for a field of unittest::TestAllTypes.
F(const string & name)2370 const FieldDescriptor* TestUtil::ReflectionTester::F(const string& name) {
2371   const FieldDescriptor* result = NULL;
2372   if (base_descriptor_->name() == "TestAllExtensions" ||
2373       base_descriptor_->name() == "TestPackedExtensions") {
2374     result = base_descriptor_->file()->FindExtensionByName(name + "_extension");
2375   } else {
2376     result = base_descriptor_->FindFieldByName(name);
2377   }
2378   GOOGLE_CHECK(result != NULL);
2379   return result;
2380 }
2381 
2382 // -------------------------------------------------------------------
2383 
SetAllFieldsViaReflection(Message * message)2384 void TestUtil::ReflectionTester::SetAllFieldsViaReflection(Message* message) {
2385   const Reflection* reflection = message->GetReflection();
2386   Message* sub_message;
2387 
2388   reflection->SetInt32 (message, F("optional_int32"   ), 101);
2389   reflection->SetInt64 (message, F("optional_int64"   ), 102);
2390   reflection->SetUInt32(message, F("optional_uint32"  ), 103);
2391   reflection->SetUInt64(message, F("optional_uint64"  ), 104);
2392   reflection->SetInt32 (message, F("optional_sint32"  ), 105);
2393   reflection->SetInt64 (message, F("optional_sint64"  ), 106);
2394   reflection->SetUInt32(message, F("optional_fixed32" ), 107);
2395   reflection->SetUInt64(message, F("optional_fixed64" ), 108);
2396   reflection->SetInt32 (message, F("optional_sfixed32"), 109);
2397   reflection->SetInt64 (message, F("optional_sfixed64"), 110);
2398   reflection->SetFloat (message, F("optional_float"   ), 111);
2399   reflection->SetDouble(message, F("optional_double"  ), 112);
2400   reflection->SetBool  (message, F("optional_bool"    ), true);
2401   reflection->SetString(message, F("optional_string"  ), "115");
2402   reflection->SetString(message, F("optional_bytes"   ), "116");
2403 
2404   sub_message = reflection->MutableMessage(message, F("optionalgroup"));
2405   sub_message->GetReflection()->SetInt32(sub_message, group_a_, 117);
2406   sub_message = reflection->MutableMessage(message, F("optional_nested_message"));
2407   sub_message->GetReflection()->SetInt32(sub_message, nested_b_, 118);
2408   sub_message = reflection->MutableMessage(message, F("optional_foreign_message"));
2409   sub_message->GetReflection()->SetInt32(sub_message, foreign_c_, 119);
2410   sub_message = reflection->MutableMessage(message, F("optional_import_message"));
2411   sub_message->GetReflection()->SetInt32(sub_message, import_d_, 120);
2412 
2413   reflection->SetEnum(message, F("optional_nested_enum" ),  nested_baz_);
2414   reflection->SetEnum(message, F("optional_foreign_enum"), foreign_baz_);
2415   reflection->SetEnum(message, F("optional_import_enum" ),  import_baz_);
2416 
2417   reflection->SetString(message, F("optional_string_piece"), "124");
2418   reflection->SetString(message, F("optional_cord"), "125");
2419 
2420   sub_message = reflection->MutableMessage(message, F("optional_public_import_message"));
2421   sub_message->GetReflection()->SetInt32(sub_message, import_e_, 126);
2422 
2423   sub_message = reflection->MutableMessage(message, F("optional_lazy_message"));
2424   sub_message->GetReflection()->SetInt32(sub_message, nested_b_, 127);
2425 
2426   // -----------------------------------------------------------------
2427 
2428   reflection->AddInt32 (message, F("repeated_int32"   ), 201);
2429   reflection->AddInt64 (message, F("repeated_int64"   ), 202);
2430   reflection->AddUInt32(message, F("repeated_uint32"  ), 203);
2431   reflection->AddUInt64(message, F("repeated_uint64"  ), 204);
2432   reflection->AddInt32 (message, F("repeated_sint32"  ), 205);
2433   reflection->AddInt64 (message, F("repeated_sint64"  ), 206);
2434   reflection->AddUInt32(message, F("repeated_fixed32" ), 207);
2435   reflection->AddUInt64(message, F("repeated_fixed64" ), 208);
2436   reflection->AddInt32 (message, F("repeated_sfixed32"), 209);
2437   reflection->AddInt64 (message, F("repeated_sfixed64"), 210);
2438   reflection->AddFloat (message, F("repeated_float"   ), 211);
2439   reflection->AddDouble(message, F("repeated_double"  ), 212);
2440   reflection->AddBool  (message, F("repeated_bool"    ), true);
2441   reflection->AddString(message, F("repeated_string"  ), "215");
2442   reflection->AddString(message, F("repeated_bytes"   ), "216");
2443 
2444   sub_message = reflection->AddMessage(message, F("repeatedgroup"));
2445   sub_message->GetReflection()->SetInt32(sub_message, repeated_group_a_, 217);
2446   sub_message = reflection->AddMessage(message, F("repeated_nested_message"));
2447   sub_message->GetReflection()->SetInt32(sub_message, nested_b_, 218);
2448   sub_message = reflection->AddMessage(message, F("repeated_foreign_message"));
2449   sub_message->GetReflection()->SetInt32(sub_message, foreign_c_, 219);
2450   sub_message = reflection->AddMessage(message, F("repeated_import_message"));
2451   sub_message->GetReflection()->SetInt32(sub_message, import_d_, 220);
2452   sub_message = reflection->AddMessage(message, F("repeated_lazy_message"));
2453   sub_message->GetReflection()->SetInt32(sub_message, nested_b_, 227);
2454 
2455   reflection->AddEnum(message, F("repeated_nested_enum" ),  nested_bar_);
2456   reflection->AddEnum(message, F("repeated_foreign_enum"), foreign_bar_);
2457   reflection->AddEnum(message, F("repeated_import_enum" ),  import_bar_);
2458 
2459   reflection->AddString(message, F("repeated_string_piece"), "224");
2460   reflection->AddString(message, F("repeated_cord"), "225");
2461 
2462   // Add a second one of each field.
2463   reflection->AddInt32 (message, F("repeated_int32"   ), 301);
2464   reflection->AddInt64 (message, F("repeated_int64"   ), 302);
2465   reflection->AddUInt32(message, F("repeated_uint32"  ), 303);
2466   reflection->AddUInt64(message, F("repeated_uint64"  ), 304);
2467   reflection->AddInt32 (message, F("repeated_sint32"  ), 305);
2468   reflection->AddInt64 (message, F("repeated_sint64"  ), 306);
2469   reflection->AddUInt32(message, F("repeated_fixed32" ), 307);
2470   reflection->AddUInt64(message, F("repeated_fixed64" ), 308);
2471   reflection->AddInt32 (message, F("repeated_sfixed32"), 309);
2472   reflection->AddInt64 (message, F("repeated_sfixed64"), 310);
2473   reflection->AddFloat (message, F("repeated_float"   ), 311);
2474   reflection->AddDouble(message, F("repeated_double"  ), 312);
2475   reflection->AddBool  (message, F("repeated_bool"    ), false);
2476   reflection->AddString(message, F("repeated_string"  ), "315");
2477   reflection->AddString(message, F("repeated_bytes"   ), "316");
2478 
2479   sub_message = reflection->AddMessage(message, F("repeatedgroup"));
2480   sub_message->GetReflection()->SetInt32(sub_message, repeated_group_a_, 317);
2481   sub_message = reflection->AddMessage(message, F("repeated_nested_message"));
2482   sub_message->GetReflection()->SetInt32(sub_message, nested_b_, 318);
2483   sub_message = reflection->AddMessage(message, F("repeated_foreign_message"));
2484   sub_message->GetReflection()->SetInt32(sub_message, foreign_c_, 319);
2485   sub_message = reflection->AddMessage(message, F("repeated_import_message"));
2486   sub_message->GetReflection()->SetInt32(sub_message, import_d_, 320);
2487   sub_message = reflection->AddMessage(message, F("repeated_lazy_message"));
2488   sub_message->GetReflection()->SetInt32(sub_message, nested_b_, 327);
2489 
2490   reflection->AddEnum(message, F("repeated_nested_enum" ),  nested_baz_);
2491   reflection->AddEnum(message, F("repeated_foreign_enum"), foreign_baz_);
2492   reflection->AddEnum(message, F("repeated_import_enum" ),  import_baz_);
2493 
2494   reflection->AddString(message, F("repeated_string_piece"), "324");
2495   reflection->AddString(message, F("repeated_cord"), "325");
2496 
2497   // -----------------------------------------------------------------
2498 
2499   reflection->SetInt32 (message, F("default_int32"   ), 401);
2500   reflection->SetInt64 (message, F("default_int64"   ), 402);
2501   reflection->SetUInt32(message, F("default_uint32"  ), 403);
2502   reflection->SetUInt64(message, F("default_uint64"  ), 404);
2503   reflection->SetInt32 (message, F("default_sint32"  ), 405);
2504   reflection->SetInt64 (message, F("default_sint64"  ), 406);
2505   reflection->SetUInt32(message, F("default_fixed32" ), 407);
2506   reflection->SetUInt64(message, F("default_fixed64" ), 408);
2507   reflection->SetInt32 (message, F("default_sfixed32"), 409);
2508   reflection->SetInt64 (message, F("default_sfixed64"), 410);
2509   reflection->SetFloat (message, F("default_float"   ), 411);
2510   reflection->SetDouble(message, F("default_double"  ), 412);
2511   reflection->SetBool  (message, F("default_bool"    ), false);
2512   reflection->SetString(message, F("default_string"  ), "415");
2513   reflection->SetString(message, F("default_bytes"   ), "416");
2514 
2515   reflection->SetEnum(message, F("default_nested_enum" ),  nested_foo_);
2516   reflection->SetEnum(message, F("default_foreign_enum"), foreign_foo_);
2517   reflection->SetEnum(message, F("default_import_enum" ),  import_foo_);
2518 
2519   reflection->SetString(message, F("default_string_piece"), "424");
2520   reflection->SetString(message, F("default_cord"), "425");
2521 
2522   reflection->SetUInt32(message, F("oneof_uint32"   ), 601);
2523   sub_message = reflection->MutableMessage(message, F("oneof_nested_message"));
2524   sub_message->GetReflection()->SetInt32(sub_message, nested_b_, 602);
2525   reflection->SetString(message, F("oneof_string"), "603");
2526   reflection->SetString(message, F("oneof_bytes" ), "604");
2527 }
2528 
SetOneofViaReflection(Message * message)2529 void TestUtil::ReflectionTester::SetOneofViaReflection(Message* message) {
2530   const Descriptor* descriptor = message->GetDescriptor();
2531   const Reflection* reflection = message->GetReflection();
2532   Message* sub_message = reflection->MutableMessage(
2533       message, descriptor->FindFieldByName("foo_lazy_message"));
2534   sub_message->GetReflection()->SetInt64(
2535       sub_message,
2536       descriptor->file()->pool()->FindFieldByName(
2537           "protobuf_unittest.TestOneof2.NestedMessage.qux_int"),
2538       100);
2539 
2540   reflection->SetString(message,
2541                         descriptor->FindFieldByName("bar_cord"),
2542                         "101");
2543   reflection->SetInt32(message,
2544                         descriptor->FindFieldByName("baz_int"),
2545                        102);
2546   reflection->SetString(message,
2547                         descriptor->FindFieldByName("baz_string"),
2548                         "103");
2549 }
2550 
ExpectOneofSetViaReflection(const Message & message)2551 void TestUtil::ReflectionTester::ExpectOneofSetViaReflection(
2552     const Message& message) {
2553   const Descriptor* descriptor = message.GetDescriptor();
2554   const Reflection* reflection = message.GetReflection();
2555   string scratch;
2556   EXPECT_TRUE(reflection->HasField(
2557       message, descriptor->FindFieldByName("foo_lazy_message")));
2558   EXPECT_TRUE(reflection->HasField(
2559       message, descriptor->FindFieldByName("bar_cord")));
2560   EXPECT_TRUE(reflection->HasField(
2561       message, descriptor->FindFieldByName("baz_int")));
2562   EXPECT_TRUE(reflection->HasField(
2563       message, descriptor->FindFieldByName("baz_string")));
2564 
2565   const Message* sub_message = &reflection->GetMessage(
2566       message, descriptor->FindFieldByName("foo_lazy_message"));
2567   EXPECT_EQ(100, sub_message->GetReflection()->GetInt64(
2568       *sub_message,
2569       descriptor->file()->pool()->FindFieldByName(
2570           "protobuf_unittest.TestOneof2.NestedMessage.qux_int")));
2571 
2572   EXPECT_EQ("101", reflection->GetString(
2573       message, descriptor->FindFieldByName("bar_cord")));
2574   EXPECT_EQ("101", reflection->GetStringReference(
2575       message, descriptor->FindFieldByName("bar_cord"), &scratch));
2576 
2577   EXPECT_EQ(102, reflection->GetInt32(
2578       message, descriptor->FindFieldByName("baz_int")));
2579 
2580   EXPECT_EQ("103", reflection->GetString(
2581       message, descriptor->FindFieldByName("baz_string")));
2582   EXPECT_EQ("103", reflection->GetStringReference(
2583       message, descriptor->FindFieldByName("baz_string"), &scratch));
2584 }
2585 
SetPackedFieldsViaReflection(Message * message)2586 void TestUtil::ReflectionTester::SetPackedFieldsViaReflection(
2587     Message* message) {
2588   const Reflection* reflection = message->GetReflection();
2589   reflection->AddInt32 (message, F("packed_int32"   ), 601);
2590   reflection->AddInt64 (message, F("packed_int64"   ), 602);
2591   reflection->AddUInt32(message, F("packed_uint32"  ), 603);
2592   reflection->AddUInt64(message, F("packed_uint64"  ), 604);
2593   reflection->AddInt32 (message, F("packed_sint32"  ), 605);
2594   reflection->AddInt64 (message, F("packed_sint64"  ), 606);
2595   reflection->AddUInt32(message, F("packed_fixed32" ), 607);
2596   reflection->AddUInt64(message, F("packed_fixed64" ), 608);
2597   reflection->AddInt32 (message, F("packed_sfixed32"), 609);
2598   reflection->AddInt64 (message, F("packed_sfixed64"), 610);
2599   reflection->AddFloat (message, F("packed_float"   ), 611);
2600   reflection->AddDouble(message, F("packed_double"  ), 612);
2601   reflection->AddBool  (message, F("packed_bool"    ), true);
2602   reflection->AddEnum  (message, F("packed_enum"    ), foreign_bar_);
2603 
2604   reflection->AddInt32 (message, F("packed_int32"   ), 701);
2605   reflection->AddInt64 (message, F("packed_int64"   ), 702);
2606   reflection->AddUInt32(message, F("packed_uint32"  ), 703);
2607   reflection->AddUInt64(message, F("packed_uint64"  ), 704);
2608   reflection->AddInt32 (message, F("packed_sint32"  ), 705);
2609   reflection->AddInt64 (message, F("packed_sint64"  ), 706);
2610   reflection->AddUInt32(message, F("packed_fixed32" ), 707);
2611   reflection->AddUInt64(message, F("packed_fixed64" ), 708);
2612   reflection->AddInt32 (message, F("packed_sfixed32"), 709);
2613   reflection->AddInt64 (message, F("packed_sfixed64"), 710);
2614   reflection->AddFloat (message, F("packed_float"   ), 711);
2615   reflection->AddDouble(message, F("packed_double"  ), 712);
2616   reflection->AddBool  (message, F("packed_bool"    ), false);
2617   reflection->AddEnum  (message, F("packed_enum"    ), foreign_baz_);
2618 }
2619 
2620 // -------------------------------------------------------------------
2621 
ExpectAllFieldsSetViaReflection(const Message & message)2622 void TestUtil::ReflectionTester::ExpectAllFieldsSetViaReflection(
2623     const Message& message) {
2624   // We have to split this into three function otherwise it creates a stack
2625   // frame so large that it triggers a warning.
2626   ExpectAllFieldsSetViaReflection1(message);
2627   ExpectAllFieldsSetViaReflection2(message);
2628   ExpectAllFieldsSetViaReflection3(message);
2629 }
2630 
ExpectAllFieldsSetViaReflection1(const Message & message)2631 void TestUtil::ReflectionTester::ExpectAllFieldsSetViaReflection1(
2632     const Message& message) {
2633   const Reflection* reflection = message.GetReflection();
2634   string scratch;
2635   const Message* sub_message;
2636 
2637   EXPECT_TRUE(reflection->HasField(message, F("optional_int32"   )));
2638   EXPECT_TRUE(reflection->HasField(message, F("optional_int64"   )));
2639   EXPECT_TRUE(reflection->HasField(message, F("optional_uint32"  )));
2640   EXPECT_TRUE(reflection->HasField(message, F("optional_uint64"  )));
2641   EXPECT_TRUE(reflection->HasField(message, F("optional_sint32"  )));
2642   EXPECT_TRUE(reflection->HasField(message, F("optional_sint64"  )));
2643   EXPECT_TRUE(reflection->HasField(message, F("optional_fixed32" )));
2644   EXPECT_TRUE(reflection->HasField(message, F("optional_fixed64" )));
2645   EXPECT_TRUE(reflection->HasField(message, F("optional_sfixed32")));
2646   EXPECT_TRUE(reflection->HasField(message, F("optional_sfixed64")));
2647   EXPECT_TRUE(reflection->HasField(message, F("optional_float"   )));
2648   EXPECT_TRUE(reflection->HasField(message, F("optional_double"  )));
2649   EXPECT_TRUE(reflection->HasField(message, F("optional_bool"    )));
2650   EXPECT_TRUE(reflection->HasField(message, F("optional_string"  )));
2651   EXPECT_TRUE(reflection->HasField(message, F("optional_bytes"   )));
2652 
2653   EXPECT_TRUE(reflection->HasField(message, F("optionalgroup"                 )));
2654   EXPECT_TRUE(reflection->HasField(message, F("optional_nested_message"       )));
2655   EXPECT_TRUE(reflection->HasField(message, F("optional_foreign_message"      )));
2656   EXPECT_TRUE(reflection->HasField(message, F("optional_import_message"       )));
2657   EXPECT_TRUE(reflection->HasField(message, F("optional_public_import_message")));
2658   EXPECT_TRUE(reflection->HasField(message, F("optional_lazy_message"         )));
2659 
2660   sub_message = &reflection->GetMessage(message, F("optionalgroup"));
2661   EXPECT_TRUE(sub_message->GetReflection()->HasField(*sub_message, group_a_));
2662   sub_message = &reflection->GetMessage(message, F("optional_nested_message"));
2663   EXPECT_TRUE(sub_message->GetReflection()->HasField(*sub_message, nested_b_));
2664   sub_message = &reflection->GetMessage(message, F("optional_foreign_message"));
2665   EXPECT_TRUE(sub_message->GetReflection()->HasField(*sub_message, foreign_c_));
2666   sub_message = &reflection->GetMessage(message, F("optional_import_message"));
2667   EXPECT_TRUE(sub_message->GetReflection()->HasField(*sub_message, import_d_));
2668   sub_message = &reflection->GetMessage(message, F("optional_public_import_message"));
2669   EXPECT_TRUE(sub_message->GetReflection()->HasField(*sub_message, import_e_));
2670   sub_message = &reflection->GetMessage(message, F("optional_lazy_message"));
2671   EXPECT_TRUE(sub_message->GetReflection()->HasField(*sub_message, nested_b_));
2672 
2673   EXPECT_TRUE(reflection->HasField(message, F("optional_nested_enum" )));
2674   EXPECT_TRUE(reflection->HasField(message, F("optional_foreign_enum")));
2675   EXPECT_TRUE(reflection->HasField(message, F("optional_import_enum" )));
2676 
2677   EXPECT_TRUE(reflection->HasField(message, F("optional_string_piece")));
2678   EXPECT_TRUE(reflection->HasField(message, F("optional_cord")));
2679 
2680   EXPECT_EQ(101  , reflection->GetInt32 (message, F("optional_int32"   )));
2681   EXPECT_EQ(102  , reflection->GetInt64 (message, F("optional_int64"   )));
2682   EXPECT_EQ(103  , reflection->GetUInt32(message, F("optional_uint32"  )));
2683   EXPECT_EQ(104  , reflection->GetUInt64(message, F("optional_uint64"  )));
2684   EXPECT_EQ(105  , reflection->GetInt32 (message, F("optional_sint32"  )));
2685   EXPECT_EQ(106  , reflection->GetInt64 (message, F("optional_sint64"  )));
2686   EXPECT_EQ(107  , reflection->GetUInt32(message, F("optional_fixed32" )));
2687   EXPECT_EQ(108  , reflection->GetUInt64(message, F("optional_fixed64" )));
2688   EXPECT_EQ(109  , reflection->GetInt32 (message, F("optional_sfixed32")));
2689   EXPECT_EQ(110  , reflection->GetInt64 (message, F("optional_sfixed64")));
2690   EXPECT_EQ(111  , reflection->GetFloat (message, F("optional_float"   )));
2691   EXPECT_EQ(112  , reflection->GetDouble(message, F("optional_double"  )));
2692   EXPECT_TRUE(     reflection->GetBool  (message, F("optional_bool"    )));
2693   EXPECT_EQ("115", reflection->GetString(message, F("optional_string"  )));
2694   EXPECT_EQ("116", reflection->GetString(message, F("optional_bytes"   )));
2695 
2696   EXPECT_EQ("115", reflection->GetStringReference(message, F("optional_string"), &scratch));
2697   EXPECT_EQ("116", reflection->GetStringReference(message, F("optional_bytes" ), &scratch));
2698 
2699   sub_message = &reflection->GetMessage(message, F("optionalgroup"));
2700   EXPECT_EQ(117, sub_message->GetReflection()->GetInt32(*sub_message, group_a_));
2701   sub_message = &reflection->GetMessage(message, F("optional_nested_message"));
2702   EXPECT_EQ(118, sub_message->GetReflection()->GetInt32(*sub_message, nested_b_));
2703   sub_message = &reflection->GetMessage(message, F("optional_foreign_message"));
2704   EXPECT_EQ(119, sub_message->GetReflection()->GetInt32(*sub_message, foreign_c_));
2705   sub_message = &reflection->GetMessage(message, F("optional_import_message"));
2706   EXPECT_EQ(120, sub_message->GetReflection()->GetInt32(*sub_message, import_d_));
2707   sub_message = &reflection->GetMessage(message, F("optional_public_import_message"));
2708   EXPECT_EQ(126, sub_message->GetReflection()->GetInt32(*sub_message, import_e_));
2709   sub_message = &reflection->GetMessage(message, F("optional_lazy_message"));
2710   EXPECT_EQ(127, sub_message->GetReflection()->GetInt32(*sub_message, nested_b_));
2711 
2712   EXPECT_EQ( nested_baz_, reflection->GetEnum(message, F("optional_nested_enum" )));
2713   EXPECT_EQ(foreign_baz_, reflection->GetEnum(message, F("optional_foreign_enum")));
2714   EXPECT_EQ( import_baz_, reflection->GetEnum(message, F("optional_import_enum" )));
2715 
2716   EXPECT_EQ("124", reflection->GetString(message, F("optional_string_piece")));
2717   EXPECT_EQ("124", reflection->GetStringReference(message, F("optional_string_piece"), &scratch));
2718 
2719   EXPECT_EQ("125", reflection->GetString(message, F("optional_cord")));
2720   EXPECT_EQ("125", reflection->GetStringReference(message, F("optional_cord"), &scratch));
2721 
2722   EXPECT_TRUE(reflection->HasField(message, F("oneof_bytes" )));
2723   EXPECT_EQ("604", reflection->GetString(message, F("oneof_bytes"   )));
2724 
2725   if (base_descriptor_->name() == "TestAllTypes") {
2726     EXPECT_FALSE(reflection->HasField(message, F("oneof_uint32")));
2727     EXPECT_FALSE(reflection->HasField(message, F("oneof_string")));
2728   } else {
2729     EXPECT_TRUE(reflection->HasField(message, F("oneof_uint32")));
2730     EXPECT_TRUE(reflection->HasField(message, F("oneof_string")));
2731     EXPECT_EQ(601  , reflection->GetUInt32(message, F("oneof_uint32")));
2732     EXPECT_EQ("603", reflection->GetString(message, F("oneof_string")));
2733     sub_message = &reflection->GetMessage(message, F("oneof_nested_message"));
2734     EXPECT_EQ(602, sub_message->GetReflection()->GetInt32(*sub_message, nested_b_));
2735   }
2736 }
2737 
ExpectAllFieldsSetViaReflection2(const Message & message)2738 void TestUtil::ReflectionTester::ExpectAllFieldsSetViaReflection2(
2739     const Message& message) {
2740   const Reflection* reflection = message.GetReflection();
2741   string scratch;
2742   const Message* sub_message;
2743 
2744   // -----------------------------------------------------------------
2745 
2746   ASSERT_EQ(2, reflection->FieldSize(message, F("repeated_int32"   )));
2747   ASSERT_EQ(2, reflection->FieldSize(message, F("repeated_int64"   )));
2748   ASSERT_EQ(2, reflection->FieldSize(message, F("repeated_uint32"  )));
2749   ASSERT_EQ(2, reflection->FieldSize(message, F("repeated_uint64"  )));
2750   ASSERT_EQ(2, reflection->FieldSize(message, F("repeated_sint32"  )));
2751   ASSERT_EQ(2, reflection->FieldSize(message, F("repeated_sint64"  )));
2752   ASSERT_EQ(2, reflection->FieldSize(message, F("repeated_fixed32" )));
2753   ASSERT_EQ(2, reflection->FieldSize(message, F("repeated_fixed64" )));
2754   ASSERT_EQ(2, reflection->FieldSize(message, F("repeated_sfixed32")));
2755   ASSERT_EQ(2, reflection->FieldSize(message, F("repeated_sfixed64")));
2756   ASSERT_EQ(2, reflection->FieldSize(message, F("repeated_float"   )));
2757   ASSERT_EQ(2, reflection->FieldSize(message, F("repeated_double"  )));
2758   ASSERT_EQ(2, reflection->FieldSize(message, F("repeated_bool"    )));
2759   ASSERT_EQ(2, reflection->FieldSize(message, F("repeated_string"  )));
2760   ASSERT_EQ(2, reflection->FieldSize(message, F("repeated_bytes"   )));
2761 
2762   ASSERT_EQ(2, reflection->FieldSize(message, F("repeatedgroup"           )));
2763   ASSERT_EQ(2, reflection->FieldSize(message, F("repeated_nested_message" )));
2764   ASSERT_EQ(2, reflection->FieldSize(message, F("repeated_foreign_message")));
2765   ASSERT_EQ(2, reflection->FieldSize(message, F("repeated_import_message" )));
2766   ASSERT_EQ(2, reflection->FieldSize(message, F("repeated_lazy_message"   )));
2767   ASSERT_EQ(2, reflection->FieldSize(message, F("repeated_nested_enum"    )));
2768   ASSERT_EQ(2, reflection->FieldSize(message, F("repeated_foreign_enum"   )));
2769   ASSERT_EQ(2, reflection->FieldSize(message, F("repeated_import_enum"    )));
2770 
2771   ASSERT_EQ(2, reflection->FieldSize(message, F("repeated_string_piece")));
2772   ASSERT_EQ(2, reflection->FieldSize(message, F("repeated_cord")));
2773 
2774   EXPECT_EQ(201  , reflection->GetRepeatedInt32 (message, F("repeated_int32"   ), 0));
2775   EXPECT_EQ(202  , reflection->GetRepeatedInt64 (message, F("repeated_int64"   ), 0));
2776   EXPECT_EQ(203  , reflection->GetRepeatedUInt32(message, F("repeated_uint32"  ), 0));
2777   EXPECT_EQ(204  , reflection->GetRepeatedUInt64(message, F("repeated_uint64"  ), 0));
2778   EXPECT_EQ(205  , reflection->GetRepeatedInt32 (message, F("repeated_sint32"  ), 0));
2779   EXPECT_EQ(206  , reflection->GetRepeatedInt64 (message, F("repeated_sint64"  ), 0));
2780   EXPECT_EQ(207  , reflection->GetRepeatedUInt32(message, F("repeated_fixed32" ), 0));
2781   EXPECT_EQ(208  , reflection->GetRepeatedUInt64(message, F("repeated_fixed64" ), 0));
2782   EXPECT_EQ(209  , reflection->GetRepeatedInt32 (message, F("repeated_sfixed32"), 0));
2783   EXPECT_EQ(210  , reflection->GetRepeatedInt64 (message, F("repeated_sfixed64"), 0));
2784   EXPECT_EQ(211  , reflection->GetRepeatedFloat (message, F("repeated_float"   ), 0));
2785   EXPECT_EQ(212  , reflection->GetRepeatedDouble(message, F("repeated_double"  ), 0));
2786   EXPECT_TRUE(     reflection->GetRepeatedBool  (message, F("repeated_bool"    ), 0));
2787   EXPECT_EQ("215", reflection->GetRepeatedString(message, F("repeated_string"  ), 0));
2788   EXPECT_EQ("216", reflection->GetRepeatedString(message, F("repeated_bytes"   ), 0));
2789 
2790   EXPECT_EQ("215", reflection->GetRepeatedStringReference(message, F("repeated_string"), 0, &scratch));
2791   EXPECT_EQ("216", reflection->GetRepeatedStringReference(message, F("repeated_bytes"), 0, &scratch));
2792 
2793   sub_message = &reflection->GetRepeatedMessage(message, F("repeatedgroup"), 0);
2794   EXPECT_EQ(217, sub_message->GetReflection()->GetInt32(*sub_message, repeated_group_a_));
2795   sub_message = &reflection->GetRepeatedMessage(message, F("repeated_nested_message"), 0);
2796   EXPECT_EQ(218, sub_message->GetReflection()->GetInt32(*sub_message, nested_b_));
2797   sub_message = &reflection->GetRepeatedMessage(message, F("repeated_foreign_message"), 0);
2798   EXPECT_EQ(219, sub_message->GetReflection()->GetInt32(*sub_message, foreign_c_));
2799   sub_message = &reflection->GetRepeatedMessage(message, F("repeated_import_message"), 0);
2800   EXPECT_EQ(220, sub_message->GetReflection()->GetInt32(*sub_message, import_d_));
2801   sub_message = &reflection->GetRepeatedMessage(message, F("repeated_lazy_message"), 0);
2802   EXPECT_EQ(227, sub_message->GetReflection()->GetInt32(*sub_message, nested_b_));
2803 
2804   EXPECT_EQ( nested_bar_, reflection->GetRepeatedEnum(message, F("repeated_nested_enum" ),0));
2805   EXPECT_EQ(foreign_bar_, reflection->GetRepeatedEnum(message, F("repeated_foreign_enum"),0));
2806   EXPECT_EQ( import_bar_, reflection->GetRepeatedEnum(message, F("repeated_import_enum" ),0));
2807 
2808   EXPECT_EQ("224", reflection->GetRepeatedString(message, F("repeated_string_piece"), 0));
2809   EXPECT_EQ("224", reflection->GetRepeatedStringReference(
2810                         message, F("repeated_string_piece"), 0, &scratch));
2811 
2812   EXPECT_EQ("225", reflection->GetRepeatedString(message, F("repeated_cord"), 0));
2813   EXPECT_EQ("225", reflection->GetRepeatedStringReference(
2814                         message, F("repeated_cord"), 0, &scratch));
2815 
2816   EXPECT_EQ(301  , reflection->GetRepeatedInt32 (message, F("repeated_int32"   ), 1));
2817   EXPECT_EQ(302  , reflection->GetRepeatedInt64 (message, F("repeated_int64"   ), 1));
2818   EXPECT_EQ(303  , reflection->GetRepeatedUInt32(message, F("repeated_uint32"  ), 1));
2819   EXPECT_EQ(304  , reflection->GetRepeatedUInt64(message, F("repeated_uint64"  ), 1));
2820   EXPECT_EQ(305  , reflection->GetRepeatedInt32 (message, F("repeated_sint32"  ), 1));
2821   EXPECT_EQ(306  , reflection->GetRepeatedInt64 (message, F("repeated_sint64"  ), 1));
2822   EXPECT_EQ(307  , reflection->GetRepeatedUInt32(message, F("repeated_fixed32" ), 1));
2823   EXPECT_EQ(308  , reflection->GetRepeatedUInt64(message, F("repeated_fixed64" ), 1));
2824   EXPECT_EQ(309  , reflection->GetRepeatedInt32 (message, F("repeated_sfixed32"), 1));
2825   EXPECT_EQ(310  , reflection->GetRepeatedInt64 (message, F("repeated_sfixed64"), 1));
2826   EXPECT_EQ(311  , reflection->GetRepeatedFloat (message, F("repeated_float"   ), 1));
2827   EXPECT_EQ(312  , reflection->GetRepeatedDouble(message, F("repeated_double"  ), 1));
2828   EXPECT_FALSE(    reflection->GetRepeatedBool  (message, F("repeated_bool"    ), 1));
2829   EXPECT_EQ("315", reflection->GetRepeatedString(message, F("repeated_string"  ), 1));
2830   EXPECT_EQ("316", reflection->GetRepeatedString(message, F("repeated_bytes"   ), 1));
2831 
2832   EXPECT_EQ("315", reflection->GetRepeatedStringReference(message, F("repeated_string"),
2833                                                           1, &scratch));
2834   EXPECT_EQ("316", reflection->GetRepeatedStringReference(message, F("repeated_bytes"),
2835                                                           1, &scratch));
2836 
2837   sub_message = &reflection->GetRepeatedMessage(message, F("repeatedgroup"), 1);
2838   EXPECT_EQ(317, sub_message->GetReflection()->GetInt32(*sub_message, repeated_group_a_));
2839   sub_message = &reflection->GetRepeatedMessage(message, F("repeated_nested_message"), 1);
2840   EXPECT_EQ(318, sub_message->GetReflection()->GetInt32(*sub_message, nested_b_));
2841   sub_message = &reflection->GetRepeatedMessage(message, F("repeated_foreign_message"), 1);
2842   EXPECT_EQ(319, sub_message->GetReflection()->GetInt32(*sub_message, foreign_c_));
2843   sub_message = &reflection->GetRepeatedMessage(message, F("repeated_import_message"), 1);
2844   EXPECT_EQ(320, sub_message->GetReflection()->GetInt32(*sub_message, import_d_));
2845   sub_message = &reflection->GetRepeatedMessage(message, F("repeated_lazy_message"), 1);
2846   EXPECT_EQ(327, sub_message->GetReflection()->GetInt32(*sub_message, nested_b_));
2847 
2848   EXPECT_EQ( nested_baz_, reflection->GetRepeatedEnum(message, F("repeated_nested_enum" ),1));
2849   EXPECT_EQ(foreign_baz_, reflection->GetRepeatedEnum(message, F("repeated_foreign_enum"),1));
2850   EXPECT_EQ( import_baz_, reflection->GetRepeatedEnum(message, F("repeated_import_enum" ),1));
2851 
2852   EXPECT_EQ("324", reflection->GetRepeatedString(message, F("repeated_string_piece"), 1));
2853   EXPECT_EQ("324", reflection->GetRepeatedStringReference(
2854                         message, F("repeated_string_piece"), 1, &scratch));
2855 
2856   EXPECT_EQ("325", reflection->GetRepeatedString(message, F("repeated_cord"), 1));
2857   EXPECT_EQ("325", reflection->GetRepeatedStringReference(
2858                         message, F("repeated_cord"), 1, &scratch));
2859 }
2860 
ExpectAllFieldsSetViaReflection3(const Message & message)2861 void TestUtil::ReflectionTester::ExpectAllFieldsSetViaReflection3(
2862     const Message& message) {
2863   const Reflection* reflection = message.GetReflection();
2864   string scratch;
2865 
2866   // -----------------------------------------------------------------
2867 
2868   EXPECT_TRUE(reflection->HasField(message, F("default_int32"   )));
2869   EXPECT_TRUE(reflection->HasField(message, F("default_int64"   )));
2870   EXPECT_TRUE(reflection->HasField(message, F("default_uint32"  )));
2871   EXPECT_TRUE(reflection->HasField(message, F("default_uint64"  )));
2872   EXPECT_TRUE(reflection->HasField(message, F("default_sint32"  )));
2873   EXPECT_TRUE(reflection->HasField(message, F("default_sint64"  )));
2874   EXPECT_TRUE(reflection->HasField(message, F("default_fixed32" )));
2875   EXPECT_TRUE(reflection->HasField(message, F("default_fixed64" )));
2876   EXPECT_TRUE(reflection->HasField(message, F("default_sfixed32")));
2877   EXPECT_TRUE(reflection->HasField(message, F("default_sfixed64")));
2878   EXPECT_TRUE(reflection->HasField(message, F("default_float"   )));
2879   EXPECT_TRUE(reflection->HasField(message, F("default_double"  )));
2880   EXPECT_TRUE(reflection->HasField(message, F("default_bool"    )));
2881   EXPECT_TRUE(reflection->HasField(message, F("default_string"  )));
2882   EXPECT_TRUE(reflection->HasField(message, F("default_bytes"   )));
2883 
2884   EXPECT_TRUE(reflection->HasField(message, F("default_nested_enum" )));
2885   EXPECT_TRUE(reflection->HasField(message, F("default_foreign_enum")));
2886   EXPECT_TRUE(reflection->HasField(message, F("default_import_enum" )));
2887 
2888   EXPECT_TRUE(reflection->HasField(message, F("default_string_piece")));
2889   EXPECT_TRUE(reflection->HasField(message, F("default_cord")));
2890 
2891   EXPECT_EQ(401  , reflection->GetInt32 (message, F("default_int32"   )));
2892   EXPECT_EQ(402  , reflection->GetInt64 (message, F("default_int64"   )));
2893   EXPECT_EQ(403  , reflection->GetUInt32(message, F("default_uint32"  )));
2894   EXPECT_EQ(404  , reflection->GetUInt64(message, F("default_uint64"  )));
2895   EXPECT_EQ(405  , reflection->GetInt32 (message, F("default_sint32"  )));
2896   EXPECT_EQ(406  , reflection->GetInt64 (message, F("default_sint64"  )));
2897   EXPECT_EQ(407  , reflection->GetUInt32(message, F("default_fixed32" )));
2898   EXPECT_EQ(408  , reflection->GetUInt64(message, F("default_fixed64" )));
2899   EXPECT_EQ(409  , reflection->GetInt32 (message, F("default_sfixed32")));
2900   EXPECT_EQ(410  , reflection->GetInt64 (message, F("default_sfixed64")));
2901   EXPECT_EQ(411  , reflection->GetFloat (message, F("default_float"   )));
2902   EXPECT_EQ(412  , reflection->GetDouble(message, F("default_double"  )));
2903   EXPECT_FALSE(    reflection->GetBool  (message, F("default_bool"    )));
2904   EXPECT_EQ("415", reflection->GetString(message, F("default_string"  )));
2905   EXPECT_EQ("416", reflection->GetString(message, F("default_bytes"   )));
2906 
2907   EXPECT_EQ("415", reflection->GetStringReference(message, F("default_string"), &scratch));
2908   EXPECT_EQ("416", reflection->GetStringReference(message, F("default_bytes" ), &scratch));
2909 
2910   EXPECT_EQ( nested_foo_, reflection->GetEnum(message, F("default_nested_enum" )));
2911   EXPECT_EQ(foreign_foo_, reflection->GetEnum(message, F("default_foreign_enum")));
2912   EXPECT_EQ( import_foo_, reflection->GetEnum(message, F("default_import_enum" )));
2913 
2914   EXPECT_EQ("424", reflection->GetString(message, F("default_string_piece")));
2915   EXPECT_EQ("424", reflection->GetStringReference(message, F("default_string_piece"),
2916                                                   &scratch));
2917 
2918   EXPECT_EQ("425", reflection->GetString(message, F("default_cord")));
2919   EXPECT_EQ("425", reflection->GetStringReference(message, F("default_cord"), &scratch));
2920 }
2921 
ExpectPackedFieldsSetViaReflection(const Message & message)2922 void TestUtil::ReflectionTester::ExpectPackedFieldsSetViaReflection(
2923     const Message& message) {
2924   const Reflection* reflection = message.GetReflection();
2925 
2926   ASSERT_EQ(2, reflection->FieldSize(message, F("packed_int32"   )));
2927   ASSERT_EQ(2, reflection->FieldSize(message, F("packed_int64"   )));
2928   ASSERT_EQ(2, reflection->FieldSize(message, F("packed_uint32"  )));
2929   ASSERT_EQ(2, reflection->FieldSize(message, F("packed_uint64"  )));
2930   ASSERT_EQ(2, reflection->FieldSize(message, F("packed_sint32"  )));
2931   ASSERT_EQ(2, reflection->FieldSize(message, F("packed_sint64"  )));
2932   ASSERT_EQ(2, reflection->FieldSize(message, F("packed_fixed32" )));
2933   ASSERT_EQ(2, reflection->FieldSize(message, F("packed_fixed64" )));
2934   ASSERT_EQ(2, reflection->FieldSize(message, F("packed_sfixed32")));
2935   ASSERT_EQ(2, reflection->FieldSize(message, F("packed_sfixed64")));
2936   ASSERT_EQ(2, reflection->FieldSize(message, F("packed_float"   )));
2937   ASSERT_EQ(2, reflection->FieldSize(message, F("packed_double"  )));
2938   ASSERT_EQ(2, reflection->FieldSize(message, F("packed_bool"    )));
2939   ASSERT_EQ(2, reflection->FieldSize(message, F("packed_enum"    )));
2940 
2941   EXPECT_EQ(601  , reflection->GetRepeatedInt32 (message, F("packed_int32"   ), 0));
2942   EXPECT_EQ(602  , reflection->GetRepeatedInt64 (message, F("packed_int64"   ), 0));
2943   EXPECT_EQ(603  , reflection->GetRepeatedUInt32(message, F("packed_uint32"  ), 0));
2944   EXPECT_EQ(604  , reflection->GetRepeatedUInt64(message, F("packed_uint64"  ), 0));
2945   EXPECT_EQ(605  , reflection->GetRepeatedInt32 (message, F("packed_sint32"  ), 0));
2946   EXPECT_EQ(606  , reflection->GetRepeatedInt64 (message, F("packed_sint64"  ), 0));
2947   EXPECT_EQ(607  , reflection->GetRepeatedUInt32(message, F("packed_fixed32" ), 0));
2948   EXPECT_EQ(608  , reflection->GetRepeatedUInt64(message, F("packed_fixed64" ), 0));
2949   EXPECT_EQ(609  , reflection->GetRepeatedInt32 (message, F("packed_sfixed32"), 0));
2950   EXPECT_EQ(610  , reflection->GetRepeatedInt64 (message, F("packed_sfixed64"), 0));
2951   EXPECT_EQ(611  , reflection->GetRepeatedFloat (message, F("packed_float"   ), 0));
2952   EXPECT_EQ(612  , reflection->GetRepeatedDouble(message, F("packed_double"  ), 0));
2953   EXPECT_TRUE(     reflection->GetRepeatedBool  (message, F("packed_bool"    ), 0));
2954   EXPECT_EQ(foreign_bar_,
2955             reflection->GetRepeatedEnum(message, F("packed_enum"), 0));
2956 
2957   EXPECT_EQ(701  , reflection->GetRepeatedInt32 (message, F("packed_int32"   ), 1));
2958   EXPECT_EQ(702  , reflection->GetRepeatedInt64 (message, F("packed_int64"   ), 1));
2959   EXPECT_EQ(703  , reflection->GetRepeatedUInt32(message, F("packed_uint32"  ), 1));
2960   EXPECT_EQ(704  , reflection->GetRepeatedUInt64(message, F("packed_uint64"  ), 1));
2961   EXPECT_EQ(705  , reflection->GetRepeatedInt32 (message, F("packed_sint32"  ), 1));
2962   EXPECT_EQ(706  , reflection->GetRepeatedInt64 (message, F("packed_sint64"  ), 1));
2963   EXPECT_EQ(707  , reflection->GetRepeatedUInt32(message, F("packed_fixed32" ), 1));
2964   EXPECT_EQ(708  , reflection->GetRepeatedUInt64(message, F("packed_fixed64" ), 1));
2965   EXPECT_EQ(709  , reflection->GetRepeatedInt32 (message, F("packed_sfixed32"), 1));
2966   EXPECT_EQ(710  , reflection->GetRepeatedInt64 (message, F("packed_sfixed64"), 1));
2967   EXPECT_EQ(711  , reflection->GetRepeatedFloat (message, F("packed_float"   ), 1));
2968   EXPECT_EQ(712  , reflection->GetRepeatedDouble(message, F("packed_double"  ), 1));
2969   EXPECT_FALSE(    reflection->GetRepeatedBool  (message, F("packed_bool"    ), 1));
2970   EXPECT_EQ(foreign_baz_,
2971             reflection->GetRepeatedEnum(message, F("packed_enum"), 1));
2972 }
2973 
2974 // -------------------------------------------------------------------
2975 
ExpectClearViaReflection(const Message & message)2976 void TestUtil::ReflectionTester::ExpectClearViaReflection(
2977     const Message& message) {
2978   const Reflection* reflection = message.GetReflection();
2979   string scratch;
2980   const Message* sub_message;
2981 
2982   // has_blah() should initially be false for all optional fields.
2983   EXPECT_FALSE(reflection->HasField(message, F("optional_int32"   )));
2984   EXPECT_FALSE(reflection->HasField(message, F("optional_int64"   )));
2985   EXPECT_FALSE(reflection->HasField(message, F("optional_uint32"  )));
2986   EXPECT_FALSE(reflection->HasField(message, F("optional_uint64"  )));
2987   EXPECT_FALSE(reflection->HasField(message, F("optional_sint32"  )));
2988   EXPECT_FALSE(reflection->HasField(message, F("optional_sint64"  )));
2989   EXPECT_FALSE(reflection->HasField(message, F("optional_fixed32" )));
2990   EXPECT_FALSE(reflection->HasField(message, F("optional_fixed64" )));
2991   EXPECT_FALSE(reflection->HasField(message, F("optional_sfixed32")));
2992   EXPECT_FALSE(reflection->HasField(message, F("optional_sfixed64")));
2993   EXPECT_FALSE(reflection->HasField(message, F("optional_float"   )));
2994   EXPECT_FALSE(reflection->HasField(message, F("optional_double"  )));
2995   EXPECT_FALSE(reflection->HasField(message, F("optional_bool"    )));
2996   EXPECT_FALSE(reflection->HasField(message, F("optional_string"  )));
2997   EXPECT_FALSE(reflection->HasField(message, F("optional_bytes"   )));
2998 
2999   EXPECT_FALSE(reflection->HasField(message, F("optionalgroup"           )));
3000   EXPECT_FALSE(reflection->HasField(message, F("optional_nested_message" )));
3001   EXPECT_FALSE(reflection->HasField(message, F("optional_foreign_message")));
3002   EXPECT_FALSE(reflection->HasField(message, F("optional_import_message" )));
3003   EXPECT_FALSE(reflection->HasField(message, F("optional_public_import_message")));
3004   EXPECT_FALSE(reflection->HasField(message, F("optional_lazy_message")));
3005 
3006   EXPECT_FALSE(reflection->HasField(message, F("optional_nested_enum" )));
3007   EXPECT_FALSE(reflection->HasField(message, F("optional_foreign_enum")));
3008   EXPECT_FALSE(reflection->HasField(message, F("optional_import_enum" )));
3009 
3010   EXPECT_FALSE(reflection->HasField(message, F("optional_string_piece")));
3011   EXPECT_FALSE(reflection->HasField(message, F("optional_cord")));
3012 
3013   // Optional fields without defaults are set to zero or something like it.
3014   EXPECT_EQ(0    , reflection->GetInt32 (message, F("optional_int32"   )));
3015   EXPECT_EQ(0    , reflection->GetInt64 (message, F("optional_int64"   )));
3016   EXPECT_EQ(0    , reflection->GetUInt32(message, F("optional_uint32"  )));
3017   EXPECT_EQ(0    , reflection->GetUInt64(message, F("optional_uint64"  )));
3018   EXPECT_EQ(0    , reflection->GetInt32 (message, F("optional_sint32"  )));
3019   EXPECT_EQ(0    , reflection->GetInt64 (message, F("optional_sint64"  )));
3020   EXPECT_EQ(0    , reflection->GetUInt32(message, F("optional_fixed32" )));
3021   EXPECT_EQ(0    , reflection->GetUInt64(message, F("optional_fixed64" )));
3022   EXPECT_EQ(0    , reflection->GetInt32 (message, F("optional_sfixed32")));
3023   EXPECT_EQ(0    , reflection->GetInt64 (message, F("optional_sfixed64")));
3024   EXPECT_EQ(0    , reflection->GetFloat (message, F("optional_float"   )));
3025   EXPECT_EQ(0    , reflection->GetDouble(message, F("optional_double"  )));
3026   EXPECT_FALSE(    reflection->GetBool  (message, F("optional_bool"    )));
3027   EXPECT_EQ(""   , reflection->GetString(message, F("optional_string"  )));
3028   EXPECT_EQ(""   , reflection->GetString(message, F("optional_bytes"   )));
3029 
3030   EXPECT_EQ("", reflection->GetStringReference(message, F("optional_string"), &scratch));
3031   EXPECT_EQ("", reflection->GetStringReference(message, F("optional_bytes" ), &scratch));
3032 
3033   // Embedded messages should also be clear.
3034   sub_message = &reflection->GetMessage(message, F("optionalgroup"));
3035   EXPECT_FALSE(sub_message->GetReflection()->HasField(*sub_message, group_a_));
3036   EXPECT_EQ(0, sub_message->GetReflection()->GetInt32(*sub_message, group_a_));
3037   sub_message = &reflection->GetMessage(message, F("optional_nested_message"));
3038   EXPECT_FALSE(sub_message->GetReflection()->HasField(*sub_message, nested_b_));
3039   EXPECT_EQ(0, sub_message->GetReflection()->GetInt32(*sub_message, nested_b_));
3040   sub_message = &reflection->GetMessage(message, F("optional_foreign_message"));
3041   EXPECT_FALSE(sub_message->GetReflection()->HasField(*sub_message, foreign_c_));
3042   EXPECT_EQ(0, sub_message->GetReflection()->GetInt32(*sub_message, foreign_c_));
3043   sub_message = &reflection->GetMessage(message, F("optional_import_message"));
3044   EXPECT_FALSE(sub_message->GetReflection()->HasField(*sub_message, import_d_));
3045   EXPECT_EQ(0, sub_message->GetReflection()->GetInt32(*sub_message, import_d_));
3046   sub_message = &reflection->GetMessage(message, F("optional_public_import_message"));
3047   EXPECT_FALSE(sub_message->GetReflection()->HasField(*sub_message, import_e_));
3048   EXPECT_EQ(0, sub_message->GetReflection()->GetInt32(*sub_message, import_e_));
3049   sub_message = &reflection->GetMessage(message, F("optional_lazy_message"));
3050   EXPECT_FALSE(sub_message->GetReflection()->HasField(*sub_message, nested_b_));
3051   EXPECT_EQ(0, sub_message->GetReflection()->GetInt32(*sub_message, nested_b_));
3052 
3053   // Enums without defaults are set to the first value in the enum.
3054   EXPECT_EQ( nested_foo_, reflection->GetEnum(message, F("optional_nested_enum" )));
3055   EXPECT_EQ(foreign_foo_, reflection->GetEnum(message, F("optional_foreign_enum")));
3056   EXPECT_EQ( import_foo_, reflection->GetEnum(message, F("optional_import_enum" )));
3057 
3058   EXPECT_EQ("", reflection->GetString(message, F("optional_string_piece")));
3059   EXPECT_EQ("", reflection->GetStringReference(message, F("optional_string_piece"), &scratch));
3060 
3061   EXPECT_EQ("", reflection->GetString(message, F("optional_cord")));
3062   EXPECT_EQ("", reflection->GetStringReference(message, F("optional_cord"), &scratch));
3063 
3064   // Repeated fields are empty.
3065   EXPECT_EQ(0, reflection->FieldSize(message, F("repeated_int32"   )));
3066   EXPECT_EQ(0, reflection->FieldSize(message, F("repeated_int64"   )));
3067   EXPECT_EQ(0, reflection->FieldSize(message, F("repeated_uint32"  )));
3068   EXPECT_EQ(0, reflection->FieldSize(message, F("repeated_uint64"  )));
3069   EXPECT_EQ(0, reflection->FieldSize(message, F("repeated_sint32"  )));
3070   EXPECT_EQ(0, reflection->FieldSize(message, F("repeated_sint64"  )));
3071   EXPECT_EQ(0, reflection->FieldSize(message, F("repeated_fixed32" )));
3072   EXPECT_EQ(0, reflection->FieldSize(message, F("repeated_fixed64" )));
3073   EXPECT_EQ(0, reflection->FieldSize(message, F("repeated_sfixed32")));
3074   EXPECT_EQ(0, reflection->FieldSize(message, F("repeated_sfixed64")));
3075   EXPECT_EQ(0, reflection->FieldSize(message, F("repeated_float"   )));
3076   EXPECT_EQ(0, reflection->FieldSize(message, F("repeated_double"  )));
3077   EXPECT_EQ(0, reflection->FieldSize(message, F("repeated_bool"    )));
3078   EXPECT_EQ(0, reflection->FieldSize(message, F("repeated_string"  )));
3079   EXPECT_EQ(0, reflection->FieldSize(message, F("repeated_bytes"   )));
3080 
3081   EXPECT_EQ(0, reflection->FieldSize(message, F("repeatedgroup"           )));
3082   EXPECT_EQ(0, reflection->FieldSize(message, F("repeated_nested_message" )));
3083   EXPECT_EQ(0, reflection->FieldSize(message, F("repeated_foreign_message")));
3084   EXPECT_EQ(0, reflection->FieldSize(message, F("repeated_import_message" )));
3085   EXPECT_EQ(0, reflection->FieldSize(message, F("repeated_lazy_message"   )));
3086   EXPECT_EQ(0, reflection->FieldSize(message, F("repeated_nested_enum"    )));
3087   EXPECT_EQ(0, reflection->FieldSize(message, F("repeated_foreign_enum"   )));
3088   EXPECT_EQ(0, reflection->FieldSize(message, F("repeated_import_enum"    )));
3089 
3090   EXPECT_EQ(0, reflection->FieldSize(message, F("repeated_string_piece")));
3091   EXPECT_EQ(0, reflection->FieldSize(message, F("repeated_cord")));
3092 
3093   // has_blah() should also be false for all default fields.
3094   EXPECT_FALSE(reflection->HasField(message, F("default_int32"   )));
3095   EXPECT_FALSE(reflection->HasField(message, F("default_int64"   )));
3096   EXPECT_FALSE(reflection->HasField(message, F("default_uint32"  )));
3097   EXPECT_FALSE(reflection->HasField(message, F("default_uint64"  )));
3098   EXPECT_FALSE(reflection->HasField(message, F("default_sint32"  )));
3099   EXPECT_FALSE(reflection->HasField(message, F("default_sint64"  )));
3100   EXPECT_FALSE(reflection->HasField(message, F("default_fixed32" )));
3101   EXPECT_FALSE(reflection->HasField(message, F("default_fixed64" )));
3102   EXPECT_FALSE(reflection->HasField(message, F("default_sfixed32")));
3103   EXPECT_FALSE(reflection->HasField(message, F("default_sfixed64")));
3104   EXPECT_FALSE(reflection->HasField(message, F("default_float"   )));
3105   EXPECT_FALSE(reflection->HasField(message, F("default_double"  )));
3106   EXPECT_FALSE(reflection->HasField(message, F("default_bool"    )));
3107   EXPECT_FALSE(reflection->HasField(message, F("default_string"  )));
3108   EXPECT_FALSE(reflection->HasField(message, F("default_bytes"   )));
3109 
3110   EXPECT_FALSE(reflection->HasField(message, F("default_nested_enum" )));
3111   EXPECT_FALSE(reflection->HasField(message, F("default_foreign_enum")));
3112   EXPECT_FALSE(reflection->HasField(message, F("default_import_enum" )));
3113 
3114   EXPECT_FALSE(reflection->HasField(message, F("default_string_piece")));
3115   EXPECT_FALSE(reflection->HasField(message, F("default_cord")));
3116 
3117   // Fields with defaults have their default values (duh).
3118   EXPECT_EQ( 41    , reflection->GetInt32 (message, F("default_int32"   )));
3119   EXPECT_EQ( 42    , reflection->GetInt64 (message, F("default_int64"   )));
3120   EXPECT_EQ( 43    , reflection->GetUInt32(message, F("default_uint32"  )));
3121   EXPECT_EQ( 44    , reflection->GetUInt64(message, F("default_uint64"  )));
3122   EXPECT_EQ(-45    , reflection->GetInt32 (message, F("default_sint32"  )));
3123   EXPECT_EQ( 46    , reflection->GetInt64 (message, F("default_sint64"  )));
3124   EXPECT_EQ( 47    , reflection->GetUInt32(message, F("default_fixed32" )));
3125   EXPECT_EQ( 48    , reflection->GetUInt64(message, F("default_fixed64" )));
3126   EXPECT_EQ( 49    , reflection->GetInt32 (message, F("default_sfixed32")));
3127   EXPECT_EQ(-50    , reflection->GetInt64 (message, F("default_sfixed64")));
3128   EXPECT_EQ( 51.5  , reflection->GetFloat (message, F("default_float"   )));
3129   EXPECT_EQ( 52e3  , reflection->GetDouble(message, F("default_double"  )));
3130   EXPECT_TRUE(       reflection->GetBool  (message, F("default_bool"    )));
3131   EXPECT_EQ("hello", reflection->GetString(message, F("default_string"  )));
3132   EXPECT_EQ("world", reflection->GetString(message, F("default_bytes"   )));
3133 
3134   EXPECT_EQ("hello", reflection->GetStringReference(message, F("default_string"), &scratch));
3135   EXPECT_EQ("world", reflection->GetStringReference(message, F("default_bytes" ), &scratch));
3136 
3137   EXPECT_EQ( nested_bar_, reflection->GetEnum(message, F("default_nested_enum" )));
3138   EXPECT_EQ(foreign_bar_, reflection->GetEnum(message, F("default_foreign_enum")));
3139   EXPECT_EQ( import_bar_, reflection->GetEnum(message, F("default_import_enum" )));
3140 
3141   EXPECT_EQ("abc", reflection->GetString(message, F("default_string_piece")));
3142   EXPECT_EQ("abc", reflection->GetStringReference(message, F("default_string_piece"), &scratch));
3143 
3144   EXPECT_EQ("123", reflection->GetString(message, F("default_cord")));
3145   EXPECT_EQ("123", reflection->GetStringReference(message, F("default_cord"), &scratch));
3146 }
3147 
ExpectPackedClearViaReflection(const Message & message)3148 void TestUtil::ReflectionTester::ExpectPackedClearViaReflection(
3149     const Message& message) {
3150   const Reflection* reflection = message.GetReflection();
3151 
3152   EXPECT_EQ(0, reflection->FieldSize(message, F("packed_int32"   )));
3153   EXPECT_EQ(0, reflection->FieldSize(message, F("packed_int64"   )));
3154   EXPECT_EQ(0, reflection->FieldSize(message, F("packed_uint32"  )));
3155   EXPECT_EQ(0, reflection->FieldSize(message, F("packed_uint64"  )));
3156   EXPECT_EQ(0, reflection->FieldSize(message, F("packed_sint32"  )));
3157   EXPECT_EQ(0, reflection->FieldSize(message, F("packed_sint64"  )));
3158   EXPECT_EQ(0, reflection->FieldSize(message, F("packed_fixed32" )));
3159   EXPECT_EQ(0, reflection->FieldSize(message, F("packed_fixed64" )));
3160   EXPECT_EQ(0, reflection->FieldSize(message, F("packed_sfixed32")));
3161   EXPECT_EQ(0, reflection->FieldSize(message, F("packed_sfixed64")));
3162   EXPECT_EQ(0, reflection->FieldSize(message, F("packed_float"   )));
3163   EXPECT_EQ(0, reflection->FieldSize(message, F("packed_double"  )));
3164   EXPECT_EQ(0, reflection->FieldSize(message, F("packed_bool"    )));
3165   EXPECT_EQ(0, reflection->FieldSize(message, F("packed_enum"    )));
3166 }
3167 
3168 // -------------------------------------------------------------------
3169 
ModifyRepeatedFieldsViaReflection(Message * message)3170 void TestUtil::ReflectionTester::ModifyRepeatedFieldsViaReflection(
3171     Message* message) {
3172   const Reflection* reflection = message->GetReflection();
3173   Message* sub_message;
3174 
3175   reflection->SetRepeatedInt32 (message, F("repeated_int32"   ), 1, 501);
3176   reflection->SetRepeatedInt64 (message, F("repeated_int64"   ), 1, 502);
3177   reflection->SetRepeatedUInt32(message, F("repeated_uint32"  ), 1, 503);
3178   reflection->SetRepeatedUInt64(message, F("repeated_uint64"  ), 1, 504);
3179   reflection->SetRepeatedInt32 (message, F("repeated_sint32"  ), 1, 505);
3180   reflection->SetRepeatedInt64 (message, F("repeated_sint64"  ), 1, 506);
3181   reflection->SetRepeatedUInt32(message, F("repeated_fixed32" ), 1, 507);
3182   reflection->SetRepeatedUInt64(message, F("repeated_fixed64" ), 1, 508);
3183   reflection->SetRepeatedInt32 (message, F("repeated_sfixed32"), 1, 509);
3184   reflection->SetRepeatedInt64 (message, F("repeated_sfixed64"), 1, 510);
3185   reflection->SetRepeatedFloat (message, F("repeated_float"   ), 1, 511);
3186   reflection->SetRepeatedDouble(message, F("repeated_double"  ), 1, 512);
3187   reflection->SetRepeatedBool  (message, F("repeated_bool"    ), 1, true);
3188   reflection->SetRepeatedString(message, F("repeated_string"  ), 1, "515");
3189   reflection->SetRepeatedString(message, F("repeated_bytes"   ), 1, "516");
3190 
3191   sub_message = reflection->MutableRepeatedMessage(message, F("repeatedgroup"), 1);
3192   sub_message->GetReflection()->SetInt32(sub_message, repeated_group_a_, 517);
3193   sub_message = reflection->MutableRepeatedMessage(message, F("repeated_nested_message"), 1);
3194   sub_message->GetReflection()->SetInt32(sub_message, nested_b_, 518);
3195   sub_message = reflection->MutableRepeatedMessage(message, F("repeated_foreign_message"), 1);
3196   sub_message->GetReflection()->SetInt32(sub_message, foreign_c_, 519);
3197   sub_message = reflection->MutableRepeatedMessage(message, F("repeated_import_message"), 1);
3198   sub_message->GetReflection()->SetInt32(sub_message, import_d_, 520);
3199   sub_message = reflection->MutableRepeatedMessage(message, F("repeated_lazy_message"), 1);
3200   sub_message->GetReflection()->SetInt32(sub_message, nested_b_, 527);
3201 
3202   reflection->SetRepeatedEnum(message, F("repeated_nested_enum" ), 1,  nested_foo_);
3203   reflection->SetRepeatedEnum(message, F("repeated_foreign_enum"), 1, foreign_foo_);
3204   reflection->SetRepeatedEnum(message, F("repeated_import_enum" ), 1,  import_foo_);
3205 
3206   reflection->SetRepeatedString(message, F("repeated_string_piece"), 1, "524");
3207   reflection->SetRepeatedString(message, F("repeated_cord"), 1, "525");
3208 }
3209 
ModifyPackedFieldsViaReflection(Message * message)3210 void TestUtil::ReflectionTester::ModifyPackedFieldsViaReflection(
3211     Message* message) {
3212   const Reflection* reflection = message->GetReflection();
3213   reflection->SetRepeatedInt32 (message, F("packed_int32"   ), 1, 801);
3214   reflection->SetRepeatedInt64 (message, F("packed_int64"   ), 1, 802);
3215   reflection->SetRepeatedUInt32(message, F("packed_uint32"  ), 1, 803);
3216   reflection->SetRepeatedUInt64(message, F("packed_uint64"  ), 1, 804);
3217   reflection->SetRepeatedInt32 (message, F("packed_sint32"  ), 1, 805);
3218   reflection->SetRepeatedInt64 (message, F("packed_sint64"  ), 1, 806);
3219   reflection->SetRepeatedUInt32(message, F("packed_fixed32" ), 1, 807);
3220   reflection->SetRepeatedUInt64(message, F("packed_fixed64" ), 1, 808);
3221   reflection->SetRepeatedInt32 (message, F("packed_sfixed32"), 1, 809);
3222   reflection->SetRepeatedInt64 (message, F("packed_sfixed64"), 1, 810);
3223   reflection->SetRepeatedFloat (message, F("packed_float"   ), 1, 811);
3224   reflection->SetRepeatedDouble(message, F("packed_double"  ), 1, 812);
3225   reflection->SetRepeatedBool  (message, F("packed_bool"    ), 1, true);
3226   reflection->SetRepeatedEnum  (message, F("packed_enum"    ), 1, foreign_foo_);
3227 }
3228 
RemoveLastRepeatedsViaReflection(Message * message)3229 void TestUtil::ReflectionTester::RemoveLastRepeatedsViaReflection(
3230     Message* message) {
3231   const Reflection* reflection = message->GetReflection();
3232 
3233   vector<const FieldDescriptor*> output;
3234   reflection->ListFields(*message, &output);
3235   for (int i=0; i<output.size(); ++i) {
3236     const FieldDescriptor* field = output[i];
3237     if (!field->is_repeated()) continue;
3238 
3239     reflection->RemoveLast(message, field);
3240   }
3241 }
3242 
ReleaseLastRepeatedsViaReflection(Message * message,bool expect_extensions_notnull)3243 void TestUtil::ReflectionTester::ReleaseLastRepeatedsViaReflection(
3244     Message* message, bool expect_extensions_notnull) {
3245   const Reflection* reflection = message->GetReflection();
3246 
3247   vector<const FieldDescriptor*> output;
3248   reflection->ListFields(*message, &output);
3249   for (int i=0; i<output.size(); ++i) {
3250     const FieldDescriptor* field = output[i];
3251     if (!field->is_repeated()) continue;
3252     if (field->cpp_type() != FieldDescriptor::CPPTYPE_MESSAGE) continue;
3253 
3254     Message* released = reflection->ReleaseLast(message, field);
3255     if (!field->is_extension() || expect_extensions_notnull) {
3256       ASSERT_TRUE(released != NULL) << "ReleaseLast returned NULL for: "
3257                                     << field->name();
3258     }
3259     delete released;
3260   }
3261 }
3262 
SwapRepeatedsViaReflection(Message * message)3263 void TestUtil::ReflectionTester::SwapRepeatedsViaReflection(Message* message) {
3264   const Reflection* reflection = message->GetReflection();
3265 
3266   vector<const FieldDescriptor*> output;
3267   reflection->ListFields(*message, &output);
3268   for (int i=0; i<output.size(); ++i) {
3269     const FieldDescriptor* field = output[i];
3270     if (!field->is_repeated()) continue;
3271 
3272     reflection->SwapElements(message, field, 0, 1);
3273   }
3274 }
3275 
3276 void TestUtil::ReflectionTester::
SetAllocatedOptionalMessageFieldsToNullViaReflection(Message * message)3277 SetAllocatedOptionalMessageFieldsToNullViaReflection(
3278     Message* message) {
3279   const Reflection* reflection = message->GetReflection();
3280 
3281   vector<const FieldDescriptor*> fields;
3282   reflection->ListFields(*message, &fields);
3283 
3284   for (int i = 0; i < fields.size(); ++i) {
3285     const FieldDescriptor* field = fields[i];
3286     if (!field->is_optional() ||
3287         field->cpp_type() != FieldDescriptor::CPPTYPE_MESSAGE) continue;
3288 
3289     reflection->SetAllocatedMessage(message, NULL, field);
3290   }
3291 }
3292 
3293 void TestUtil::ReflectionTester::
SetAllocatedOptionalMessageFieldsToMessageViaReflection(Message * from_message,Message * to_message)3294 SetAllocatedOptionalMessageFieldsToMessageViaReflection(
3295     Message* from_message,
3296     Message* to_message) {
3297   EXPECT_EQ(from_message->GetDescriptor(), to_message->GetDescriptor());
3298   const Reflection* from_reflection = from_message->GetReflection();
3299   const Reflection* to_reflection = to_message->GetReflection();
3300 
3301   vector<const FieldDescriptor*> fields;
3302   from_reflection->ListFields(*from_message, &fields);
3303 
3304   for (int i = 0; i < fields.size(); ++i) {
3305     const FieldDescriptor* field = fields[i];
3306     if (!field->is_optional() ||
3307         field->cpp_type() != FieldDescriptor::CPPTYPE_MESSAGE) continue;
3308 
3309     Message* sub_message =
3310         from_reflection->ReleaseMessage(from_message, field);
3311     to_reflection->SetAllocatedMessage(to_message, sub_message, field);
3312   }
3313 }
3314 
ExpectMessagesReleasedViaReflection(Message * message,TestUtil::ReflectionTester::MessageReleaseState expected_release_state)3315 void TestUtil::ReflectionTester::ExpectMessagesReleasedViaReflection(
3316     Message* message,
3317     TestUtil::ReflectionTester::MessageReleaseState expected_release_state) {
3318   const Reflection* reflection = message->GetReflection();
3319 
3320   static const char* fields[] = {
3321     "optionalgroup",
3322     "optional_nested_message",
3323     "optional_foreign_message",
3324     "optional_import_message",
3325   };
3326   for (int i = 0; i < GOOGLE_ARRAYSIZE(fields); i++) {
3327     const Message& sub_message = reflection->GetMessage(*message, F(fields[i]));
3328     Message* released = reflection->ReleaseMessage(message, F(fields[i]));
3329     switch (expected_release_state) {
3330       case IS_NULL:
3331         EXPECT_TRUE(released == NULL);
3332         break;
3333       case NOT_NULL:
3334         EXPECT_TRUE(released != NULL);
3335         EXPECT_EQ(&sub_message, released);
3336         break;
3337       case CAN_BE_NULL:
3338         break;
3339     }
3340     delete released;
3341     EXPECT_FALSE(reflection->HasField(*message, F(fields[i])));
3342   }
3343 }
3344 
3345 }  // namespace protobuf
3346 }  // namespace google
3347