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