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