PageRenderTime 125ms CodeModel.GetById 2ms app.highlight 115ms RepoModel.GetById 2ms app.codeStats 0ms

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

http://github.com/tomahawk-player/tomahawk
C++ | 1086 lines | 799 code | 203 blank | 84 comment | 49 complexity | 7770c142e3de0ece2a385b90ffac904a MD5 | raw 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// TODO(kenton):  Improve this unittest to bring it up to the standards of
  36//   other proto2 unittests.
  37
  38#include <algorithm>
  39#include <list>
  40#include <vector>
  41
  42#include <google/protobuf/repeated_field.h>
  43
  44#include <google/protobuf/stubs/common.h>
  45#include <google/protobuf/unittest.pb.h>
  46#include <google/protobuf/stubs/strutil.h>
  47#include <google/protobuf/testing/googletest.h>
  48#include <gtest/gtest.h>
  49#include <google/protobuf/stubs/stl_util-inl.h>
  50
  51namespace google {
  52using protobuf_unittest::TestAllTypes;
  53
  54namespace protobuf {
  55namespace {
  56
  57// Test operations on a RepeatedField which is small enough that it does
  58// not allocate a separate array for storage.
  59TEST(RepeatedField, Small) {
  60  RepeatedField<int> field;
  61
  62  EXPECT_EQ(field.size(), 0);
  63
  64  field.Add(5);
  65
  66  EXPECT_EQ(field.size(), 1);
  67  EXPECT_EQ(field.Get(0), 5);
  68
  69  field.Add(42);
  70
  71  EXPECT_EQ(field.size(), 2);
  72  EXPECT_EQ(field.Get(0), 5);
  73  EXPECT_EQ(field.Get(1), 42);
  74
  75  field.Set(1, 23);
  76
  77  EXPECT_EQ(field.size(), 2);
  78  EXPECT_EQ(field.Get(0), 5);
  79  EXPECT_EQ(field.Get(1), 23);
  80  EXPECT_EQ(field.SpaceUsedExcludingSelf(), 0);
  81
  82  field.RemoveLast();
  83
  84  EXPECT_EQ(field.size(), 1);
  85  EXPECT_EQ(field.Get(0), 5);
  86
  87  field.Clear();
  88
  89  EXPECT_EQ(field.size(), 0);
  90  EXPECT_EQ(field.SpaceUsedExcludingSelf(), 0);
  91}
  92
  93// Test operations on a RepeatedField which is large enough to allocate a
  94// separate array.
  95TEST(RepeatedField, Large) {
  96  RepeatedField<int> field;
  97
  98  for (int i = 0; i < 16; i++) {
  99    field.Add(i * i);
 100  }
 101
 102  EXPECT_EQ(field.size(), 16);
 103
 104  for (int i = 0; i < 16; i++) {
 105    EXPECT_EQ(field.Get(i), i * i);
 106  }
 107
 108  int expected_usage = 16 * sizeof(int);
 109  EXPECT_GE(field.SpaceUsedExcludingSelf(), expected_usage);
 110}
 111
 112// Test swapping between various types of RepeatedFields.
 113TEST(RepeatedField, SwapSmallSmall) {
 114  RepeatedField<int> field1;
 115  RepeatedField<int> field2;
 116
 117  field1.Add(5);
 118  field1.Add(42);
 119
 120  field1.Swap(&field2);
 121
 122  EXPECT_EQ(field1.size(), 0);
 123  EXPECT_EQ(field2.size(), 2);
 124  EXPECT_EQ(field2.Get(0), 5);
 125  EXPECT_EQ(field2.Get(1), 42);
 126}
 127
 128TEST(RepeatedField, SwapLargeSmall) {
 129  RepeatedField<int> field1;
 130  RepeatedField<int> field2;
 131
 132  for (int i = 0; i < 16; i++) {
 133    field1.Add(i * i);
 134  }
 135  field2.Add(5);
 136  field2.Add(42);
 137  field1.Swap(&field2);
 138
 139  EXPECT_EQ(field1.size(), 2);
 140  EXPECT_EQ(field1.Get(0), 5);
 141  EXPECT_EQ(field1.Get(1), 42);
 142  EXPECT_EQ(field2.size(), 16);
 143  for (int i = 0; i < 16; i++) {
 144    EXPECT_EQ(field2.Get(i), i * i);
 145  }
 146}
 147
 148TEST(RepeatedField, SwapLargeLarge) {
 149  RepeatedField<int> field1;
 150  RepeatedField<int> field2;
 151
 152  field1.Add(5);
 153  field1.Add(42);
 154  for (int i = 0; i < 16; i++) {
 155    field1.Add(i);
 156    field2.Add(i * i);
 157  }
 158  field2.Swap(&field1);
 159
 160  EXPECT_EQ(field1.size(), 16);
 161  for (int i = 0; i < 16; i++) {
 162    EXPECT_EQ(field1.Get(i), i * i);
 163  }
 164  EXPECT_EQ(field2.size(), 18);
 165  EXPECT_EQ(field2.Get(0), 5);
 166  EXPECT_EQ(field2.Get(1), 42);
 167  for (int i = 2; i < 18; i++) {
 168    EXPECT_EQ(field2.Get(i), i - 2);
 169  }
 170}
 171
 172// Determines how much space was reserved by the given field by adding elements
 173// to it until it re-allocates its space.
 174static int ReservedSpace(RepeatedField<int>* field) {
 175  const int* ptr = field->data();
 176  do {
 177    field->Add(0);
 178  } while (field->data() == ptr);
 179
 180  return field->size() - 1;
 181}
 182
 183TEST(RepeatedField, ReserveMoreThanDouble) {
 184  // Reserve more than double the previous space in the field and expect the
 185  // field to reserve exactly the amount specified.
 186  RepeatedField<int> field;
 187  field.Reserve(20);
 188
 189  EXPECT_EQ(20, ReservedSpace(&field));
 190}
 191
 192TEST(RepeatedField, ReserveLessThanDouble) {
 193  // Reserve less than double the previous space in the field and expect the
 194  // field to grow by double instead.
 195  RepeatedField<int> field;
 196  field.Reserve(20);
 197  field.Reserve(30);
 198
 199  EXPECT_EQ(40, ReservedSpace(&field));
 200}
 201
 202TEST(RepeatedField, ReserveLessThanExisting) {
 203  // Reserve less than the previous space in the field and expect the
 204  // field to not re-allocate at all.
 205  RepeatedField<int> field;
 206  field.Reserve(20);
 207  const int* previous_ptr = field.data();
 208  field.Reserve(10);
 209
 210  EXPECT_EQ(previous_ptr, field.data());
 211  EXPECT_EQ(20, ReservedSpace(&field));
 212}
 213
 214TEST(RepeatedField, MergeFrom) {
 215  RepeatedField<int> source, destination;
 216
 217  source.Add(4);
 218  source.Add(5);
 219
 220  destination.Add(1);
 221  destination.Add(2);
 222  destination.Add(3);
 223
 224  destination.MergeFrom(source);
 225
 226  ASSERT_EQ(5, destination.size());
 227
 228  EXPECT_EQ(1, destination.Get(0));
 229  EXPECT_EQ(2, destination.Get(1));
 230  EXPECT_EQ(3, destination.Get(2));
 231  EXPECT_EQ(4, destination.Get(3));
 232  EXPECT_EQ(5, destination.Get(4));
 233}
 234
 235TEST(RepeatedField, CopyFrom) {
 236  RepeatedField<int> source, destination;
 237
 238  source.Add(4);
 239  source.Add(5);
 240
 241  destination.Add(1);
 242  destination.Add(2);
 243  destination.Add(3);
 244
 245  destination.CopyFrom(source);
 246
 247  ASSERT_EQ(2, destination.size());
 248
 249  EXPECT_EQ(4, destination.Get(0));
 250  EXPECT_EQ(5, destination.Get(1));
 251}
 252
 253TEST(RepeatedField, CopyConstruct) {
 254  RepeatedField<int> source;
 255  source.Add(1);
 256  source.Add(2);
 257
 258  RepeatedField<int> destination(source);
 259
 260  ASSERT_EQ(2, destination.size());
 261  EXPECT_EQ(1, destination.Get(0));
 262  EXPECT_EQ(2, destination.Get(1));
 263}
 264
 265TEST(RepeatedField, CopyAssign) {
 266  RepeatedField<int> source, destination;
 267
 268  source.Add(4);
 269  source.Add(5);
 270
 271  destination.Add(1);
 272  destination.Add(2);
 273  destination.Add(3);
 274
 275  destination = source;
 276
 277  ASSERT_EQ(2, destination.size());
 278
 279  EXPECT_EQ(4, destination.Get(0));
 280  EXPECT_EQ(5, destination.Get(1));
 281}
 282
 283TEST(RepeatedField, MutableDataIsMutable) {
 284  RepeatedField<int> field;
 285  field.Add(1);
 286  EXPECT_EQ(1, field.Get(0));
 287  // The fact that this line compiles would be enough, but we'll check the
 288  // value anyway.
 289  *field.mutable_data() = 2;
 290  EXPECT_EQ(2, field.Get(0));
 291}
 292
 293TEST(RepeatedField, Truncate) {
 294  RepeatedField<int> field;
 295
 296  field.Add(12);
 297  field.Add(34);
 298  field.Add(56);
 299  field.Add(78);
 300  EXPECT_EQ(4, field.size());
 301
 302  field.Truncate(3);
 303  EXPECT_EQ(3, field.size());
 304
 305  field.Add(90);
 306  EXPECT_EQ(4, field.size());
 307  EXPECT_EQ(90, field.Get(3));
 308
 309  // Truncations that don't change the size are allowed, but growing is not
 310  // allowed.
 311  field.Truncate(field.size());
 312#ifdef GTEST_HAS_DEATH_TEST
 313  EXPECT_DEBUG_DEATH(field.Truncate(field.size() + 1), "new_size");
 314#endif
 315}
 316
 317
 318// ===================================================================
 319// RepeatedPtrField tests.  These pretty much just mirror the RepeatedField
 320// tests above.
 321
 322TEST(RepeatedPtrField, Small) {
 323  RepeatedPtrField<string> field;
 324
 325  EXPECT_EQ(field.size(), 0);
 326
 327  field.Add()->assign("foo");
 328
 329  EXPECT_EQ(field.size(), 1);
 330  EXPECT_EQ(field.Get(0), "foo");
 331
 332  field.Add()->assign("bar");
 333
 334  EXPECT_EQ(field.size(), 2);
 335  EXPECT_EQ(field.Get(0), "foo");
 336  EXPECT_EQ(field.Get(1), "bar");
 337
 338  field.Mutable(1)->assign("baz");
 339
 340  EXPECT_EQ(field.size(), 2);
 341  EXPECT_EQ(field.Get(0), "foo");
 342  EXPECT_EQ(field.Get(1), "baz");
 343
 344  field.RemoveLast();
 345
 346  EXPECT_EQ(field.size(), 1);
 347  EXPECT_EQ(field.Get(0), "foo");
 348
 349  field.Clear();
 350
 351  EXPECT_EQ(field.size(), 0);
 352}
 353
 354TEST(RepeatedPtrField, Large) {
 355  RepeatedPtrField<string> field;
 356
 357  for (int i = 0; i < 16; i++) {
 358    *field.Add() += 'a' + i;
 359  }
 360
 361  EXPECT_EQ(field.size(), 16);
 362
 363  for (int i = 0; i < 16; i++) {
 364    EXPECT_EQ(field.Get(i).size(), 1);
 365    EXPECT_EQ(field.Get(i)[0], 'a' + i);
 366  }
 367
 368  int min_expected_usage = 16 * sizeof(string);
 369  EXPECT_GE(field.SpaceUsedExcludingSelf(), min_expected_usage);
 370}
 371
 372TEST(RepeatedPtrField, SwapSmallSmall) {
 373  RepeatedPtrField<string> field1;
 374  RepeatedPtrField<string> field2;
 375
 376  field1.Add()->assign("foo");
 377  field1.Add()->assign("bar");
 378  field1.Swap(&field2);
 379
 380  EXPECT_EQ(field1.size(), 0);
 381  EXPECT_EQ(field2.size(), 2);
 382  EXPECT_EQ(field2.Get(0), "foo");
 383  EXPECT_EQ(field2.Get(1), "bar");
 384}
 385
 386TEST(RepeatedPtrField, SwapLargeSmall) {
 387  RepeatedPtrField<string> field1;
 388  RepeatedPtrField<string> field2;
 389
 390  field2.Add()->assign("foo");
 391  field2.Add()->assign("bar");
 392  for (int i = 0; i < 16; i++) {
 393    *field1.Add() += 'a' + i;
 394  }
 395  field1.Swap(&field2);
 396
 397  EXPECT_EQ(field1.size(), 2);
 398  EXPECT_EQ(field1.Get(0), "foo");
 399  EXPECT_EQ(field1.Get(1), "bar");
 400  EXPECT_EQ(field2.size(), 16);
 401  for (int i = 0; i < 16; i++) {
 402    EXPECT_EQ(field2.Get(i).size(), 1);
 403    EXPECT_EQ(field2.Get(i)[0], 'a' + i);
 404  }
 405}
 406
 407TEST(RepeatedPtrField, SwapLargeLarge) {
 408  RepeatedPtrField<string> field1;
 409  RepeatedPtrField<string> field2;
 410
 411  field1.Add()->assign("foo");
 412  field1.Add()->assign("bar");
 413  for (int i = 0; i < 16; i++) {
 414    *field1.Add() += 'A' + i;
 415    *field2.Add() += 'a' + i;
 416  }
 417  field2.Swap(&field1);
 418
 419  EXPECT_EQ(field1.size(), 16);
 420  for (int i = 0; i < 16; i++) {
 421    EXPECT_EQ(field1.Get(i).size(), 1);
 422    EXPECT_EQ(field1.Get(i)[0], 'a' + i);
 423  }
 424  EXPECT_EQ(field2.size(), 18);
 425  EXPECT_EQ(field2.Get(0), "foo");
 426  EXPECT_EQ(field2.Get(1), "bar");
 427  for (int i = 2; i < 18; i++) {
 428    EXPECT_EQ(field2.Get(i).size(), 1);
 429    EXPECT_EQ(field2.Get(i)[0], 'A' + i - 2);
 430  }
 431}
 432
 433static int ReservedSpace(RepeatedPtrField<string>* field) {
 434  const string* const* ptr = field->data();
 435  do {
 436    field->Add();
 437  } while (field->data() == ptr);
 438
 439  return field->size() - 1;
 440}
 441
 442TEST(RepeatedPtrField, ReserveMoreThanDouble) {
 443  RepeatedPtrField<string> field;
 444  field.Reserve(20);
 445
 446  EXPECT_EQ(20, ReservedSpace(&field));
 447}
 448
 449TEST(RepeatedPtrField, ReserveLessThanDouble) {
 450  RepeatedPtrField<string> field;
 451  field.Reserve(20);
 452  field.Reserve(30);
 453
 454  EXPECT_EQ(40, ReservedSpace(&field));
 455}
 456
 457TEST(RepeatedPtrField, ReserveLessThanExisting) {
 458  RepeatedPtrField<string> field;
 459  field.Reserve(20);
 460  const string* const* previous_ptr = field.data();
 461  field.Reserve(10);
 462
 463  EXPECT_EQ(previous_ptr, field.data());
 464  EXPECT_EQ(20, ReservedSpace(&field));
 465}
 466
 467TEST(RepeatedPtrField, ReserveDoesntLoseAllocated) {
 468  // Check that a bug is fixed:  An earlier implementation of Reserve()
 469  // failed to copy pointers to allocated-but-cleared objects, possibly
 470  // leading to segfaults.
 471  RepeatedPtrField<string> field;
 472  string* first = field.Add();
 473  field.RemoveLast();
 474
 475  field.Reserve(20);
 476  EXPECT_EQ(first, field.Add());
 477}
 478
 479// Clearing elements is tricky with RepeatedPtrFields since the memory for
 480// the elements is retained and reused.
 481TEST(RepeatedPtrField, ClearedElements) {
 482  RepeatedPtrField<string> field;
 483
 484  string* original = field.Add();
 485  *original = "foo";
 486
 487  EXPECT_EQ(field.ClearedCount(), 0);
 488
 489  field.RemoveLast();
 490  EXPECT_TRUE(original->empty());
 491  EXPECT_EQ(field.ClearedCount(), 1);
 492
 493  EXPECT_EQ(field.Add(), original);  // Should return same string for reuse.
 494
 495  EXPECT_EQ(field.ReleaseLast(), original);  // We take ownership.
 496  EXPECT_EQ(field.ClearedCount(), 0);
 497
 498  EXPECT_NE(field.Add(), original);  // Should NOT return the same string.
 499  EXPECT_EQ(field.ClearedCount(), 0);
 500
 501  field.AddAllocated(original);  // Give ownership back.
 502  EXPECT_EQ(field.ClearedCount(), 0);
 503  EXPECT_EQ(field.Mutable(1), original);
 504
 505  field.Clear();
 506  EXPECT_EQ(field.ClearedCount(), 2);
 507  EXPECT_EQ(field.ReleaseCleared(), original);  // Take ownership again.
 508  EXPECT_EQ(field.ClearedCount(), 1);
 509  EXPECT_NE(field.Add(), original);
 510  EXPECT_EQ(field.ClearedCount(), 0);
 511  EXPECT_NE(field.Add(), original);
 512  EXPECT_EQ(field.ClearedCount(), 0);
 513
 514  field.AddCleared(original);  // Give ownership back, but as a cleared object.
 515  EXPECT_EQ(field.ClearedCount(), 1);
 516  EXPECT_EQ(field.Add(), original);
 517  EXPECT_EQ(field.ClearedCount(), 0);
 518}
 519
 520// Test all code paths in AddAllocated().
 521TEST(RepeatedPtrField, AddAlocated) {
 522  RepeatedPtrField<string> field;
 523  while (field.size() < field.Capacity()) {
 524    field.Add()->assign("filler");
 525  }
 526
 527  int index = field.size();
 528
 529  // First branch:  Field is at capacity with no cleared objects.
 530  string* foo = new string("foo");
 531  field.AddAllocated(foo);
 532  EXPECT_EQ(index + 1, field.size());
 533  EXPECT_EQ(0, field.ClearedCount());
 534  EXPECT_EQ(foo, &field.Get(index));
 535
 536  // Last branch:  Field is not at capacity and there are no cleared objects.
 537  string* bar = new string("bar");
 538  field.AddAllocated(bar);
 539  ++index;
 540  EXPECT_EQ(index + 1, field.size());
 541  EXPECT_EQ(0, field.ClearedCount());
 542  EXPECT_EQ(bar, &field.Get(index));
 543
 544  // Third branch:  Field is not at capacity and there are no cleared objects.
 545  field.RemoveLast();
 546  string* baz = new string("baz");
 547  field.AddAllocated(baz);
 548  EXPECT_EQ(index + 1, field.size());
 549  EXPECT_EQ(1, field.ClearedCount());
 550  EXPECT_EQ(baz, &field.Get(index));
 551
 552  // Second branch:  Field is at capacity but has some cleared objects.
 553  while (field.size() < field.Capacity()) {
 554    field.Add()->assign("filler2");
 555  }
 556  field.RemoveLast();
 557  index = field.size();
 558  string* qux = new string("qux");
 559  field.AddAllocated(qux);
 560  EXPECT_EQ(index + 1, field.size());
 561  // We should have discarded the cleared object.
 562  EXPECT_EQ(0, field.ClearedCount());
 563  EXPECT_EQ(qux, &field.Get(index));
 564}
 565
 566TEST(RepeatedPtrField, MergeFrom) {
 567  RepeatedPtrField<string> source, destination;
 568
 569  source.Add()->assign("4");
 570  source.Add()->assign("5");
 571
 572  destination.Add()->assign("1");
 573  destination.Add()->assign("2");
 574  destination.Add()->assign("3");
 575
 576  destination.MergeFrom(source);
 577
 578  ASSERT_EQ(5, destination.size());
 579
 580  EXPECT_EQ("1", destination.Get(0));
 581  EXPECT_EQ("2", destination.Get(1));
 582  EXPECT_EQ("3", destination.Get(2));
 583  EXPECT_EQ("4", destination.Get(3));
 584  EXPECT_EQ("5", destination.Get(4));
 585}
 586
 587TEST(RepeatedPtrField, CopyFrom) {
 588  RepeatedPtrField<string> source, destination;
 589
 590  source.Add()->assign("4");
 591  source.Add()->assign("5");
 592
 593  destination.Add()->assign("1");
 594  destination.Add()->assign("2");
 595  destination.Add()->assign("3");
 596
 597  destination.CopyFrom(source);
 598
 599  ASSERT_EQ(2, destination.size());
 600
 601  EXPECT_EQ("4", destination.Get(0));
 602  EXPECT_EQ("5", destination.Get(1));
 603}
 604
 605TEST(RepeatedPtrField, CopyConstruct) {
 606  RepeatedPtrField<string> source;
 607
 608  source.Add()->assign("1");
 609  source.Add()->assign("2");
 610
 611  RepeatedPtrField<string> destination(source);
 612
 613  ASSERT_EQ(2, destination.size());
 614  EXPECT_EQ("1", destination.Get(0));
 615  EXPECT_EQ("2", destination.Get(1));
 616}
 617
 618TEST(RepeatedPtrField, CopyAssign) {
 619  RepeatedPtrField<string> source, destination;
 620
 621  source.Add()->assign("4");
 622  source.Add()->assign("5");
 623
 624  destination.Add()->assign("1");
 625  destination.Add()->assign("2");
 626  destination.Add()->assign("3");
 627
 628  destination = source;
 629
 630  ASSERT_EQ(2, destination.size());
 631
 632  EXPECT_EQ("4", destination.Get(0));
 633  EXPECT_EQ("5", destination.Get(1));
 634}
 635
 636TEST(RepeatedPtrField, MutableDataIsMutable) {
 637  RepeatedPtrField<string> field;
 638  *field.Add() = "1";
 639  EXPECT_EQ("1", field.Get(0));
 640  // The fact that this line compiles would be enough, but we'll check the
 641  // value anyway.
 642  string** data = field.mutable_data();
 643  **data = "2";
 644  EXPECT_EQ("2", field.Get(0));
 645}
 646
 647// ===================================================================
 648
 649// Iterator tests stolen from net/proto/proto-array_unittest.
 650class RepeatedFieldIteratorTest : public testing::Test {
 651 protected:
 652  virtual void SetUp() {
 653    for (int i = 0; i < 3; ++i) {
 654      proto_array_.Add(i);
 655    }
 656  }
 657
 658  RepeatedField<int> proto_array_;
 659};
 660
 661TEST_F(RepeatedFieldIteratorTest, Convertible) {
 662  RepeatedField<int>::iterator iter = proto_array_.begin();
 663  RepeatedField<int>::const_iterator c_iter = iter;
 664  RepeatedField<int>::value_type value = *c_iter;
 665  EXPECT_EQ(0, value);
 666}
 667
 668TEST_F(RepeatedFieldIteratorTest, MutableIteration) {
 669  RepeatedField<int>::iterator iter = proto_array_.begin();
 670  EXPECT_EQ(0, *iter);
 671  ++iter;
 672  EXPECT_EQ(1, *iter++);
 673  EXPECT_EQ(2, *iter);
 674  ++iter;
 675  EXPECT_TRUE(proto_array_.end() == iter);
 676
 677  EXPECT_EQ(2, *(proto_array_.end() - 1));
 678}
 679
 680TEST_F(RepeatedFieldIteratorTest, ConstIteration) {
 681  const RepeatedField<int>& const_proto_array = proto_array_;
 682  RepeatedField<int>::const_iterator iter = const_proto_array.begin();
 683  EXPECT_EQ(0, *iter);
 684  ++iter;
 685  EXPECT_EQ(1, *iter++);
 686  EXPECT_EQ(2, *iter);
 687  ++iter;
 688  EXPECT_TRUE(proto_array_.end() == iter);
 689  EXPECT_EQ(2, *(proto_array_.end() - 1));
 690}
 691
 692TEST_F(RepeatedFieldIteratorTest, Mutation) {
 693  RepeatedField<int>::iterator iter = proto_array_.begin();
 694  *iter = 7;
 695  EXPECT_EQ(7, proto_array_.Get(0));
 696}
 697
 698// -------------------------------------------------------------------
 699
 700class RepeatedPtrFieldIteratorTest : public testing::Test {
 701 protected:
 702  virtual void SetUp() {
 703    proto_array_.Add()->assign("foo");
 704    proto_array_.Add()->assign("bar");
 705    proto_array_.Add()->assign("baz");
 706  }
 707
 708  RepeatedPtrField<string> proto_array_;
 709};
 710
 711TEST_F(RepeatedPtrFieldIteratorTest, Convertible) {
 712  RepeatedPtrField<string>::iterator iter = proto_array_.begin();
 713  RepeatedPtrField<string>::const_iterator c_iter = iter;
 714  RepeatedPtrField<string>::value_type value = *c_iter;
 715  EXPECT_EQ("foo", value);
 716}
 717
 718TEST_F(RepeatedPtrFieldIteratorTest, MutableIteration) {
 719  RepeatedPtrField<string>::iterator iter = proto_array_.begin();
 720  EXPECT_EQ("foo", *iter);
 721  ++iter;
 722  EXPECT_EQ("bar", *(iter++));
 723  EXPECT_EQ("baz", *iter);
 724  ++iter;
 725  EXPECT_TRUE(proto_array_.end() == iter);
 726  EXPECT_EQ("baz", *(--proto_array_.end()));
 727}
 728
 729TEST_F(RepeatedPtrFieldIteratorTest, ConstIteration) {
 730  const RepeatedPtrField<string>& const_proto_array = proto_array_;
 731  RepeatedPtrField<string>::const_iterator iter = const_proto_array.begin();
 732  EXPECT_EQ("foo", *iter);
 733  ++iter;
 734  EXPECT_EQ("bar", *(iter++));
 735  EXPECT_EQ("baz", *iter);
 736  ++iter;
 737  EXPECT_TRUE(const_proto_array.end() == iter);
 738  EXPECT_EQ("baz", *(--const_proto_array.end()));
 739}
 740
 741TEST_F(RepeatedPtrFieldIteratorTest, RandomAccess) {
 742  RepeatedPtrField<string>::iterator iter = proto_array_.begin();
 743  RepeatedPtrField<string>::iterator iter2 = iter;
 744  ++iter2;
 745  ++iter2;
 746  EXPECT_TRUE(iter + 2 == iter2);
 747  EXPECT_TRUE(iter == iter2 - 2);
 748  EXPECT_EQ("baz", iter[2]);
 749  EXPECT_EQ("baz", *(iter + 2));
 750  EXPECT_EQ(3, proto_array_.end() - proto_array_.begin());
 751}
 752
 753TEST_F(RepeatedPtrFieldIteratorTest, Comparable) {
 754  RepeatedPtrField<string>::const_iterator iter = proto_array_.begin();
 755  RepeatedPtrField<string>::const_iterator iter2 = iter + 1;
 756  EXPECT_TRUE(iter == iter);
 757  EXPECT_TRUE(iter != iter2);
 758  EXPECT_TRUE(iter < iter2);
 759  EXPECT_TRUE(iter <= iter2);
 760  EXPECT_TRUE(iter <= iter);
 761  EXPECT_TRUE(iter2 > iter);
 762  EXPECT_TRUE(iter2 >= iter);
 763  EXPECT_TRUE(iter >= iter);
 764}
 765
 766// Uninitialized iterator does not point to any of the RepeatedPtrField.
 767TEST_F(RepeatedPtrFieldIteratorTest, UninitializedIterator) {
 768  RepeatedPtrField<string>::iterator iter;
 769  EXPECT_TRUE(iter != proto_array_.begin());
 770  EXPECT_TRUE(iter != proto_array_.begin() + 1);
 771  EXPECT_TRUE(iter != proto_array_.begin() + 2);
 772  EXPECT_TRUE(iter != proto_array_.begin() + 3);
 773  EXPECT_TRUE(iter != proto_array_.end());
 774}
 775
 776TEST_F(RepeatedPtrFieldIteratorTest, STLAlgorithms_lower_bound) {
 777  proto_array_.Clear();
 778  proto_array_.Add()->assign("a");
 779  proto_array_.Add()->assign("c");
 780  proto_array_.Add()->assign("d");
 781  proto_array_.Add()->assign("n");
 782  proto_array_.Add()->assign("p");
 783  proto_array_.Add()->assign("x");
 784  proto_array_.Add()->assign("y");
 785
 786  string v = "f";
 787  RepeatedPtrField<string>::const_iterator it =
 788      lower_bound(proto_array_.begin(), proto_array_.end(), v);
 789
 790  EXPECT_EQ(*it, "n");
 791  EXPECT_TRUE(it == proto_array_.begin() + 3);
 792}
 793
 794TEST_F(RepeatedPtrFieldIteratorTest, Mutation) {
 795  RepeatedPtrField<string>::iterator iter = proto_array_.begin();
 796  *iter = "qux";
 797  EXPECT_EQ("qux", proto_array_.Get(0));
 798}
 799
 800// -------------------------------------------------------------------
 801
 802class RepeatedPtrFieldPtrsIteratorTest : public testing::Test {
 803 protected:
 804  virtual void SetUp() {
 805    proto_array_.Add()->assign("foo");
 806    proto_array_.Add()->assign("bar");
 807    proto_array_.Add()->assign("baz");
 808  }
 809
 810  RepeatedPtrField<string> proto_array_;
 811};
 812
 813TEST_F(RepeatedPtrFieldPtrsIteratorTest, ConvertiblePtr) {
 814  RepeatedPtrField<string>::pointer_iterator iter =
 815      proto_array_.pointer_begin();
 816}
 817
 818TEST_F(RepeatedPtrFieldPtrsIteratorTest, MutablePtrIteration) {
 819  RepeatedPtrField<string>::pointer_iterator iter =
 820      proto_array_.pointer_begin();
 821  EXPECT_EQ("foo", **iter);
 822  ++iter;
 823  EXPECT_EQ("bar", **(iter++));
 824  EXPECT_EQ("baz", **iter);
 825  ++iter;
 826  EXPECT_TRUE(proto_array_.pointer_end() == iter);
 827  EXPECT_EQ("baz", **(--proto_array_.pointer_end()));
 828}
 829
 830TEST_F(RepeatedPtrFieldPtrsIteratorTest, RandomPtrAccess) {
 831  RepeatedPtrField<string>::pointer_iterator iter =
 832      proto_array_.pointer_begin();
 833  RepeatedPtrField<string>::pointer_iterator iter2 = iter;
 834  ++iter2;
 835  ++iter2;
 836  EXPECT_TRUE(iter + 2 == iter2);
 837  EXPECT_TRUE(iter == iter2 - 2);
 838  EXPECT_EQ("baz", *iter[2]);
 839  EXPECT_EQ("baz", **(iter + 2));
 840  EXPECT_EQ(3, proto_array_.end() - proto_array_.begin());
 841}
 842
 843TEST_F(RepeatedPtrFieldPtrsIteratorTest, ComparablePtr) {
 844  RepeatedPtrField<string>::pointer_iterator iter =
 845      proto_array_.pointer_begin();
 846  RepeatedPtrField<string>::pointer_iterator iter2 = iter + 1;
 847  EXPECT_TRUE(iter == iter);
 848  EXPECT_TRUE(iter != iter2);
 849  EXPECT_TRUE(iter < iter2);
 850  EXPECT_TRUE(iter <= iter2);
 851  EXPECT_TRUE(iter <= iter);
 852  EXPECT_TRUE(iter2 > iter);
 853  EXPECT_TRUE(iter2 >= iter);
 854  EXPECT_TRUE(iter >= iter);
 855}
 856
 857// Uninitialized iterator does not point to any of the RepeatedPtrOverPtrs.
 858// Dereferencing an uninitialized iterator crashes the process.
 859TEST_F(RepeatedPtrFieldPtrsIteratorTest, UninitializedPtrIterator) {
 860  RepeatedPtrField<string>::pointer_iterator iter;
 861  EXPECT_TRUE(iter != proto_array_.pointer_begin());
 862  EXPECT_TRUE(iter != proto_array_.pointer_begin() + 1);
 863  EXPECT_TRUE(iter != proto_array_.pointer_begin() + 2);
 864  EXPECT_TRUE(iter != proto_array_.pointer_begin() + 3);
 865  EXPECT_TRUE(iter != proto_array_.pointer_end());
 866}
 867
 868
 869// This comparison functor is required by the tests for RepeatedPtrOverPtrs.
 870// They operate on strings and need to compare strings as strings in
 871// any stl algorithm, even though the iterator returns a pointer to a string
 872// - i.e. *iter has type string*.
 873struct StringLessThan {
 874  bool operator()(const string* z, const string& y) {
 875    return *z < y;
 876  }
 877  bool operator()(const string* z, const string* y) {
 878    return *z < *y;
 879  }
 880};
 881
 882TEST_F(RepeatedPtrFieldPtrsIteratorTest, PtrSTLAlgorithms_lower_bound) {
 883  proto_array_.Clear();
 884  proto_array_.Add()->assign("a");
 885  proto_array_.Add()->assign("c");
 886  proto_array_.Add()->assign("d");
 887  proto_array_.Add()->assign("n");
 888  proto_array_.Add()->assign("p");
 889  proto_array_.Add()->assign("x");
 890  proto_array_.Add()->assign("y");
 891
 892  RepeatedPtrField<string>::pointer_iterator iter =
 893      proto_array_.pointer_begin();
 894  string v = "f";
 895  RepeatedPtrField<string>::pointer_iterator it =
 896      lower_bound(proto_array_.pointer_begin(), proto_array_.pointer_end(),
 897                  &v, StringLessThan());
 898
 899  GOOGLE_CHECK(*it != NULL);
 900
 901  EXPECT_EQ(**it, "n");
 902  EXPECT_TRUE(it == proto_array_.pointer_begin() + 3);
 903}
 904
 905TEST_F(RepeatedPtrFieldPtrsIteratorTest, PtrMutation) {
 906  RepeatedPtrField<string>::pointer_iterator iter =
 907      proto_array_.pointer_begin();
 908  **iter = "qux";
 909  EXPECT_EQ("qux", proto_array_.Get(0));
 910
 911  EXPECT_EQ("bar", proto_array_.Get(1));
 912  EXPECT_EQ("baz", proto_array_.Get(2));
 913  ++iter;
 914  delete *iter;
 915  *iter = new string("a");
 916  ++iter;
 917  delete *iter;
 918  *iter = new string("b");
 919  EXPECT_EQ("a", proto_array_.Get(1));
 920  EXPECT_EQ("b", proto_array_.Get(2));
 921}
 922
 923TEST_F(RepeatedPtrFieldPtrsIteratorTest, Sort) {
 924  proto_array_.Add()->assign("c");
 925  proto_array_.Add()->assign("d");
 926  proto_array_.Add()->assign("n");
 927  proto_array_.Add()->assign("p");
 928  proto_array_.Add()->assign("a");
 929  proto_array_.Add()->assign("y");
 930  proto_array_.Add()->assign("x");
 931  EXPECT_EQ("foo", proto_array_.Get(0));
 932  EXPECT_EQ("n", proto_array_.Get(5));
 933  EXPECT_EQ("x", proto_array_.Get(9));
 934  sort(proto_array_.pointer_begin(),
 935       proto_array_.pointer_end(),
 936       StringLessThan());
 937  EXPECT_EQ("a", proto_array_.Get(0));
 938  EXPECT_EQ("baz", proto_array_.Get(2));
 939  EXPECT_EQ("y", proto_array_.Get(9));
 940}
 941
 942
 943// -----------------------------------------------------------------------------
 944// Unit-tests for the insert iterators
 945// google::protobuf::RepeatedFieldBackInserter,
 946// google::protobuf::AllocatedRepeatedPtrFieldBackInserter
 947// Ported from util/gtl/proto-array-iterators_unittest.
 948
 949class RepeatedFieldInsertionIteratorsTest : public testing::Test {
 950 protected:
 951  std::list<double> halves;
 952  std::list<int> fibonacci;
 953  std::vector<string> words;
 954  typedef TestAllTypes::NestedMessage Nested;
 955  Nested nesteds[2];
 956  std::vector<Nested*> nested_ptrs;
 957  TestAllTypes protobuffer;
 958
 959  virtual void SetUp() {
 960    fibonacci.push_back(1);
 961    fibonacci.push_back(1);
 962    fibonacci.push_back(2);
 963    fibonacci.push_back(3);
 964    fibonacci.push_back(5);
 965    fibonacci.push_back(8);
 966    std::copy(fibonacci.begin(), fibonacci.end(),
 967              RepeatedFieldBackInserter(protobuffer.mutable_repeated_int32()));
 968
 969    halves.push_back(1.0);
 970    halves.push_back(0.5);
 971    halves.push_back(0.25);
 972    halves.push_back(0.125);
 973    halves.push_back(0.0625);
 974    std::copy(halves.begin(), halves.end(),
 975              RepeatedFieldBackInserter(protobuffer.mutable_repeated_double()));
 976
 977    words.push_back("Able");
 978    words.push_back("was");
 979    words.push_back("I");
 980    words.push_back("ere");
 981    words.push_back("I");
 982    words.push_back("saw");
 983    words.push_back("Elba");
 984    std::copy(words.begin(), words.end(),
 985              RepeatedFieldBackInserter(protobuffer.mutable_repeated_string()));
 986
 987    nesteds[0].set_bb(17);
 988    nesteds[1].set_bb(4711);
 989    std::copy(&nesteds[0], &nesteds[2],
 990              RepeatedFieldBackInserter(
 991                  protobuffer.mutable_repeated_nested_message()));
 992
 993    nested_ptrs.push_back(new Nested);
 994    nested_ptrs.back()->set_bb(170);
 995    nested_ptrs.push_back(new Nested);
 996    nested_ptrs.back()->set_bb(47110);
 997    std::copy(nested_ptrs.begin(), nested_ptrs.end(),
 998              RepeatedFieldBackInserter(
 999                  protobuffer.mutable_repeated_nested_message()));
1000
1001  }
1002
1003  virtual void TearDown() {
1004    STLDeleteContainerPointers(nested_ptrs.begin(), nested_ptrs.end());
1005  }
1006};
1007
1008TEST_F(RepeatedFieldInsertionIteratorsTest, Fibonacci) {
1009  EXPECT_TRUE(std::equal(fibonacci.begin(),
1010                         fibonacci.end(),
1011                         protobuffer.repeated_int32().begin()));
1012  EXPECT_TRUE(std::equal(protobuffer.repeated_int32().begin(),
1013                         protobuffer.repeated_int32().end(),
1014                         fibonacci.begin()));
1015}
1016
1017TEST_F(RepeatedFieldInsertionIteratorsTest, Halves) {
1018  EXPECT_TRUE(std::equal(halves.begin(),
1019                         halves.end(),
1020                         protobuffer.repeated_double().begin()));
1021  EXPECT_TRUE(std::equal(protobuffer.repeated_double().begin(),
1022                         protobuffer.repeated_double().end(),
1023                         halves.begin()));
1024}
1025
1026TEST_F(RepeatedFieldInsertionIteratorsTest, Words) {
1027  ASSERT_EQ(words.size(), protobuffer.repeated_string_size());
1028  EXPECT_EQ(words.at(0), protobuffer.repeated_string(0));
1029  EXPECT_EQ(words.at(1), protobuffer.repeated_string(1));
1030  EXPECT_EQ(words.at(2), protobuffer.repeated_string(2));
1031  EXPECT_EQ(words.at(3), protobuffer.repeated_string(3));
1032  EXPECT_EQ(words.at(4), protobuffer.repeated_string(4));
1033  EXPECT_EQ(words.at(5), protobuffer.repeated_string(5));
1034  EXPECT_EQ(words.at(6), protobuffer.repeated_string(6));
1035}
1036
1037TEST_F(RepeatedFieldInsertionIteratorsTest, Nesteds) {
1038  ASSERT_EQ(protobuffer.repeated_nested_message_size(), 4);
1039  EXPECT_EQ(protobuffer.repeated_nested_message(0).bb(), 17);
1040  EXPECT_EQ(protobuffer.repeated_nested_message(1).bb(), 4711);
1041  EXPECT_EQ(protobuffer.repeated_nested_message(2).bb(), 170);
1042  EXPECT_EQ(protobuffer.repeated_nested_message(3).bb(), 47110);
1043}
1044
1045TEST_F(RepeatedFieldInsertionIteratorsTest,
1046       AllocatedRepeatedPtrFieldWithStringIntData) {
1047  vector<Nested*> data;
1048  TestAllTypes goldenproto;
1049  for (int i = 0; i < 10; ++i) {
1050    Nested* new_data = new Nested;
1051    new_data->set_bb(i);
1052    data.push_back(new_data);
1053
1054    new_data = goldenproto.add_repeated_nested_message();
1055    new_data->set_bb(i);
1056  }
1057  TestAllTypes testproto;
1058  copy(data.begin(), data.end(),
1059       AllocatedRepeatedPtrFieldBackInserter(
1060           testproto.mutable_repeated_nested_message()));
1061  EXPECT_EQ(testproto.DebugString(), goldenproto.DebugString());
1062}
1063
1064TEST_F(RepeatedFieldInsertionIteratorsTest,
1065       AllocatedRepeatedPtrFieldWithString) {
1066  vector<string*> data;
1067  TestAllTypes goldenproto;
1068  for (int i = 0; i < 10; ++i) {
1069    string* new_data = new string;
1070    *new_data = "name-" + SimpleItoa(i);
1071    data.push_back(new_data);
1072
1073    new_data = goldenproto.add_repeated_string();
1074    *new_data = "name-" + SimpleItoa(i);
1075  }
1076  TestAllTypes testproto;
1077  copy(data.begin(), data.end(),
1078       AllocatedRepeatedPtrFieldBackInserter(
1079           testproto.mutable_repeated_string()));
1080  EXPECT_EQ(testproto.DebugString(), goldenproto.DebugString());
1081}
1082
1083}  // namespace
1084
1085}  // namespace protobuf
1086}  // namespace google