PageRenderTime 193ms CodeModel.GetById 71ms app.highlight 111ms RepoModel.GetById 1ms app.codeStats 1ms

/thirdparty/breakpad/third_party/protobuf/protobuf/src/google/protobuf/test_util.cc

http://github.com/tomahawk-player/tomahawk
C++ | 1322 lines | 1069 code | 167 blank | 86 comment | 0 complexity | 039444602f9bf5e9379fb4486e99fbb2 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

   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_TR

Large files files are truncated, but you can click here to view the full file