Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(44)

Side by Side Diff: third_party/protobuf/src/google/protobuf/repeated_field_unittest.cc

Issue 1842653006: Update //third_party/protobuf to version 3. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: update sync unittest and README.chromium Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Protocol Buffers - Google's data interchange format 1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved. 2 // Copyright 2008 Google Inc. All rights reserved.
3 // http://code.google.com/p/protobuf/ 3 // https://developers.google.com/protocol-buffers/
4 // 4 //
5 // Redistribution and use in source and binary forms, with or without 5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are 6 // modification, are permitted provided that the following conditions are
7 // met: 7 // met:
8 // 8 //
9 // * Redistributions of source code must retain the above copyright 9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer. 10 // notice, this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above 11 // * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer 12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the 13 // in the documentation and/or other materials provided with the
(...skipping 21 matching lines...) Expand all
35 // TODO(kenton): Improve this unittest to bring it up to the standards of 35 // TODO(kenton): Improve this unittest to bring it up to the standards of
36 // other proto2 unittests. 36 // other proto2 unittests.
37 37
38 #include <algorithm> 38 #include <algorithm>
39 #include <limits> 39 #include <limits>
40 #include <list> 40 #include <list>
41 #include <vector> 41 #include <vector>
42 42
43 #include <google/protobuf/repeated_field.h> 43 #include <google/protobuf/repeated_field.h>
44 44
45 #include <google/protobuf/stubs/logging.h>
45 #include <google/protobuf/stubs/common.h> 46 #include <google/protobuf/stubs/common.h>
46 #include <google/protobuf/unittest.pb.h> 47 #include <google/protobuf/unittest.pb.h>
47 #include <google/protobuf/stubs/strutil.h> 48 #include <google/protobuf/stubs/strutil.h>
48 #include <google/protobuf/testing/googletest.h> 49 #include <google/protobuf/testing/googletest.h>
49 #include <gtest/gtest.h> 50 #include <gtest/gtest.h>
50 #include <google/protobuf/stubs/stl_util.h> 51 #include <google/protobuf/stubs/stl_util.h>
51 52
52 namespace google { 53 namespace google {
53 using protobuf_unittest::TestAllTypes; 54 using protobuf_unittest::TestAllTypes;
54 55
55 namespace protobuf { 56 namespace protobuf {
56 namespace { 57 namespace {
57 58
58 // Test operations on a small RepeatedField. 59 // Test operations on a small RepeatedField.
59 TEST(RepeatedField, Small) { 60 TEST(RepeatedField, Small) {
60 RepeatedField<int> field; 61 RepeatedField<int> field;
61 62
63 EXPECT_TRUE(field.empty());
62 EXPECT_EQ(field.size(), 0); 64 EXPECT_EQ(field.size(), 0);
63 65
64 field.Add(5); 66 field.Add(5);
65 67
68 EXPECT_FALSE(field.empty());
66 EXPECT_EQ(field.size(), 1); 69 EXPECT_EQ(field.size(), 1);
67 EXPECT_EQ(field.Get(0), 5); 70 EXPECT_EQ(field.Get(0), 5);
68 71
69 field.Add(42); 72 field.Add(42);
70 73
74 EXPECT_FALSE(field.empty());
71 EXPECT_EQ(field.size(), 2); 75 EXPECT_EQ(field.size(), 2);
72 EXPECT_EQ(field.Get(0), 5); 76 EXPECT_EQ(field.Get(0), 5);
73 EXPECT_EQ(field.Get(1), 42); 77 EXPECT_EQ(field.Get(1), 42);
74 78
75 field.Set(1, 23); 79 field.Set(1, 23);
76 80
81 EXPECT_FALSE(field.empty());
77 EXPECT_EQ(field.size(), 2); 82 EXPECT_EQ(field.size(), 2);
78 EXPECT_EQ(field.Get(0), 5); 83 EXPECT_EQ(field.Get(0), 5);
79 EXPECT_EQ(field.Get(1), 23); 84 EXPECT_EQ(field.Get(1), 23);
80 85
81 field.RemoveLast(); 86 field.RemoveLast();
82 87
88 EXPECT_FALSE(field.empty());
83 EXPECT_EQ(field.size(), 1); 89 EXPECT_EQ(field.size(), 1);
84 EXPECT_EQ(field.Get(0), 5); 90 EXPECT_EQ(field.Get(0), 5);
85 91
86 field.Clear(); 92 field.Clear();
87 93
94 EXPECT_TRUE(field.empty());
88 EXPECT_EQ(field.size(), 0); 95 EXPECT_EQ(field.size(), 0);
89 int expected_usage = 4 * sizeof(int); 96 // Additional bytes are for 'struct Rep' header.
97 int expected_usage = 4 * sizeof(int) + sizeof(Arena*);
90 EXPECT_EQ(field.SpaceUsedExcludingSelf(), expected_usage); 98 EXPECT_EQ(field.SpaceUsedExcludingSelf(), expected_usage);
91 } 99 }
92 100
93 101
94 // Test operations on a RepeatedField which is large enough to allocate a 102 // Test operations on a RepeatedField which is large enough to allocate a
95 // separate array. 103 // separate array.
96 TEST(RepeatedField, Large) { 104 TEST(RepeatedField, Large) {
97 RepeatedField<int> field; 105 RepeatedField<int> field;
98 106
99 for (int i = 0; i < 16; i++) { 107 for (int i = 0; i < 16; i++) {
100 field.Add(i * i); 108 field.Add(i * i);
101 } 109 }
102 110
111 EXPECT_FALSE(field.empty());
103 EXPECT_EQ(field.size(), 16); 112 EXPECT_EQ(field.size(), 16);
104 113
105 for (int i = 0; i < 16; i++) { 114 for (int i = 0; i < 16; i++) {
106 EXPECT_EQ(field.Get(i), i * i); 115 EXPECT_EQ(field.Get(i), i * i);
107 } 116 }
108 117
109 int expected_usage = 16 * sizeof(int); 118 int expected_usage = 16 * sizeof(int);
110 EXPECT_GE(field.SpaceUsedExcludingSelf(), expected_usage); 119 EXPECT_GE(field.SpaceUsedExcludingSelf(), expected_usage);
111 } 120 }
112 121
113 // Test swapping between various types of RepeatedFields. 122 // Test swapping between various types of RepeatedFields.
114 TEST(RepeatedField, SwapSmallSmall) { 123 TEST(RepeatedField, SwapSmallSmall) {
115 RepeatedField<int> field1; 124 RepeatedField<int> field1;
116 RepeatedField<int> field2; 125 RepeatedField<int> field2;
117 126
118 field1.Add(5); 127 field1.Add(5);
119 field1.Add(42); 128 field1.Add(42);
120 129
130 EXPECT_FALSE(field1.empty());
131 EXPECT_EQ(field1.size(), 2);
132 EXPECT_EQ(field1.Get(0), 5);
133 EXPECT_EQ(field1.Get(1), 42);
134
135 EXPECT_TRUE(field2.empty());
136 EXPECT_EQ(field2.size(), 0);
137
121 field1.Swap(&field2); 138 field1.Swap(&field2);
122 139
140 EXPECT_TRUE(field1.empty());
123 EXPECT_EQ(field1.size(), 0); 141 EXPECT_EQ(field1.size(), 0);
142
143 EXPECT_FALSE(field2.empty());
124 EXPECT_EQ(field2.size(), 2); 144 EXPECT_EQ(field2.size(), 2);
125 EXPECT_EQ(field2.Get(0), 5); 145 EXPECT_EQ(field2.Get(0), 5);
126 EXPECT_EQ(field2.Get(1), 42); 146 EXPECT_EQ(field2.Get(1), 42);
127 } 147 }
128 148
129 TEST(RepeatedField, SwapLargeSmall) { 149 TEST(RepeatedField, SwapLargeSmall) {
130 RepeatedField<int> field1; 150 RepeatedField<int> field1;
131 RepeatedField<int> field2; 151 RepeatedField<int> field2;
132 152
133 for (int i = 0; i < 16; i++) { 153 for (int i = 0; i < 16; i++) {
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
205 // field to not re-allocate at all. 225 // field to not re-allocate at all.
206 RepeatedField<int> field; 226 RepeatedField<int> field;
207 field.Reserve(20); 227 field.Reserve(20);
208 const int* previous_ptr = field.data(); 228 const int* previous_ptr = field.data();
209 field.Reserve(10); 229 field.Reserve(10);
210 230
211 EXPECT_EQ(previous_ptr, field.data()); 231 EXPECT_EQ(previous_ptr, field.data());
212 EXPECT_EQ(20, ReservedSpace(&field)); 232 EXPECT_EQ(20, ReservedSpace(&field));
213 } 233 }
214 234
235 TEST(RepeatedField, Resize) {
236 RepeatedField<int> field;
237 field.Resize(2, 1);
238 EXPECT_EQ(2, field.size());
239 field.Resize(5, 2);
240 EXPECT_EQ(5, field.size());
241 field.Resize(4, 3);
242 ASSERT_EQ(4, field.size());
243 EXPECT_EQ(1, field.Get(0));
244 EXPECT_EQ(1, field.Get(1));
245 EXPECT_EQ(2, field.Get(2));
246 EXPECT_EQ(2, field.Get(3));
247 field.Resize(0, 4);
248 EXPECT_TRUE(field.empty());
249 }
250
215 TEST(RepeatedField, MergeFrom) { 251 TEST(RepeatedField, MergeFrom) {
216 RepeatedField<int> source, destination; 252 RepeatedField<int> source, destination;
217 source.Add(4); 253 source.Add(4);
218 source.Add(5); 254 source.Add(5);
219 destination.Add(1); 255 destination.Add(1);
220 destination.Add(2); 256 destination.Add(2);
221 destination.Add(3); 257 destination.Add(3);
222 258
223 destination.MergeFrom(source); 259 destination.MergeFrom(source);
224 260
225 ASSERT_EQ(5, destination.size()); 261 ASSERT_EQ(5, destination.size());
226 EXPECT_EQ(1, destination.Get(0)); 262 EXPECT_EQ(1, destination.Get(0));
227 EXPECT_EQ(2, destination.Get(1)); 263 EXPECT_EQ(2, destination.Get(1));
228 EXPECT_EQ(3, destination.Get(2)); 264 EXPECT_EQ(3, destination.Get(2));
229 EXPECT_EQ(4, destination.Get(3)); 265 EXPECT_EQ(4, destination.Get(3));
230 EXPECT_EQ(5, destination.Get(4)); 266 EXPECT_EQ(5, destination.Get(4));
231 } 267 }
232 268
269 #ifdef PROTOBUF_HAS_DEATH_TEST
270 TEST(RepeatedField, MergeFromSelf) {
271 RepeatedField<int> me;
272 me.Add(3);
273 EXPECT_DEATH(me.MergeFrom(me), "");
274 }
275 #endif // PROTOBUF_HAS_DEATH_TEST
276
233 TEST(RepeatedField, CopyFrom) { 277 TEST(RepeatedField, CopyFrom) {
234 RepeatedField<int> source, destination; 278 RepeatedField<int> source, destination;
235 source.Add(4); 279 source.Add(4);
236 source.Add(5); 280 source.Add(5);
237 destination.Add(1); 281 destination.Add(1);
238 destination.Add(2); 282 destination.Add(2);
239 destination.Add(3); 283 destination.Add(3);
240 284
241 destination.CopyFrom(source); 285 destination.CopyFrom(source);
242 286
243 ASSERT_EQ(2, destination.size()); 287 ASSERT_EQ(2, destination.size());
244 EXPECT_EQ(4, destination.Get(0)); 288 EXPECT_EQ(4, destination.Get(0));
245 EXPECT_EQ(5, destination.Get(1)); 289 EXPECT_EQ(5, destination.Get(1));
246 } 290 }
247 291
292 TEST(RepeatedField, CopyFromSelf) {
293 RepeatedField<int> me;
294 me.Add(3);
295 me.CopyFrom(me);
296 ASSERT_EQ(1, me.size());
297 EXPECT_EQ(3, me.Get(0));
298 }
299
300 TEST(RepeatedField, Erase) {
301 RepeatedField<int> me;
302 RepeatedField<int>::iterator it = me.erase(me.begin(), me.end());
303 EXPECT_TRUE(me.begin() == it);
304 EXPECT_EQ(0, me.size());
305
306 me.Add(1);
307 me.Add(2);
308 me.Add(3);
309 it = me.erase(me.begin(), me.end());
310 EXPECT_TRUE(me.begin() == it);
311 EXPECT_EQ(0, me.size());
312
313 me.Add(4);
314 me.Add(5);
315 me.Add(6);
316 it = me.erase(me.begin() + 2, me.end());
317 EXPECT_TRUE(me.begin() + 2 == it);
318 EXPECT_EQ(2, me.size());
319 EXPECT_EQ(4, me.Get(0));
320 EXPECT_EQ(5, me.Get(1));
321
322 me.Add(6);
323 me.Add(7);
324 me.Add(8);
325 it = me.erase(me.begin() + 1, me.begin() + 3);
326 EXPECT_TRUE(me.begin() + 1 == it);
327 EXPECT_EQ(3, me.size());
328 EXPECT_EQ(4, me.Get(0));
329 EXPECT_EQ(7, me.Get(1));
330 EXPECT_EQ(8, me.Get(2));
331 }
332
248 TEST(RepeatedField, CopyConstruct) { 333 TEST(RepeatedField, CopyConstruct) {
249 RepeatedField<int> source; 334 RepeatedField<int> source;
250 source.Add(1); 335 source.Add(1);
251 source.Add(2); 336 source.Add(2);
252 337
253 RepeatedField<int> destination(source); 338 RepeatedField<int> destination(source);
254 339
255 ASSERT_EQ(2, destination.size()); 340 ASSERT_EQ(2, destination.size());
256 EXPECT_EQ(1, destination.Get(0)); 341 EXPECT_EQ(1, destination.Get(0));
257 EXPECT_EQ(2, destination.Get(1)); 342 EXPECT_EQ(2, destination.Get(1));
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
365 // Does the resulting array contain the right values? 450 // Does the resulting array contain the right values?
366 for (int i = 0; i < start; ++i) 451 for (int i = 0; i < start; ++i)
367 EXPECT_EQ(field.Get(i), i); 452 EXPECT_EQ(field.Get(i), i);
368 for (int i = start; i < field.size(); ++i) 453 for (int i = start; i < field.size(); ++i)
369 EXPECT_EQ(field.Get(i), i + num); 454 EXPECT_EQ(field.Get(i), i + num);
370 } 455 }
371 } 456 }
372 } 457 }
373 } 458 }
374 459
460 TEST(RepeatedField, ClearThenReserveMore) {
461 // Test that Reserve properly destroys the old internal array when it's forced
462 // to allocate a new one, even when cleared-but-not-deleted objects are
463 // present. Use a 'string' and > 16 bytes length so that the elements are
464 // non-POD and allocate -- the leak checker will catch any skipped destructor
465 // calls here.
466 RepeatedField<string> field;
467 for (int i = 0; i < 32; i++) {
468 field.Add(string("abcdefghijklmnopqrstuvwxyz0123456789"));
469 }
470 EXPECT_EQ(32, field.size());
471 field.Clear();
472 EXPECT_EQ(0, field.size());
473 EXPECT_EQ(32, field.Capacity());
474
475 field.Reserve(1024);
476 EXPECT_EQ(0, field.size());
477 EXPECT_EQ(1024, field.Capacity());
478 // Finish test -- |field| should destroy the cleared-but-not-yet-destroyed
479 // strings.
480 }
481
375 // =================================================================== 482 // ===================================================================
376 // RepeatedPtrField tests. These pretty much just mirror the RepeatedField 483 // RepeatedPtrField tests. These pretty much just mirror the RepeatedField
377 // tests above. 484 // tests above.
378 485
379 TEST(RepeatedPtrField, Small) { 486 TEST(RepeatedPtrField, Small) {
380 RepeatedPtrField<string> field; 487 RepeatedPtrField<string> field;
381 488
489 EXPECT_TRUE(field.empty());
382 EXPECT_EQ(field.size(), 0); 490 EXPECT_EQ(field.size(), 0);
383 491
384 field.Add()->assign("foo"); 492 field.Add()->assign("foo");
385 493
494 EXPECT_FALSE(field.empty());
386 EXPECT_EQ(field.size(), 1); 495 EXPECT_EQ(field.size(), 1);
387 EXPECT_EQ(field.Get(0), "foo"); 496 EXPECT_EQ(field.Get(0), "foo");
388 497
389 field.Add()->assign("bar"); 498 field.Add()->assign("bar");
390 499
500 EXPECT_FALSE(field.empty());
391 EXPECT_EQ(field.size(), 2); 501 EXPECT_EQ(field.size(), 2);
392 EXPECT_EQ(field.Get(0), "foo"); 502 EXPECT_EQ(field.Get(0), "foo");
393 EXPECT_EQ(field.Get(1), "bar"); 503 EXPECT_EQ(field.Get(1), "bar");
394 504
395 field.Mutable(1)->assign("baz"); 505 field.Mutable(1)->assign("baz");
396 506
507 EXPECT_FALSE(field.empty());
397 EXPECT_EQ(field.size(), 2); 508 EXPECT_EQ(field.size(), 2);
398 EXPECT_EQ(field.Get(0), "foo"); 509 EXPECT_EQ(field.Get(0), "foo");
399 EXPECT_EQ(field.Get(1), "baz"); 510 EXPECT_EQ(field.Get(1), "baz");
400 511
401 field.RemoveLast(); 512 field.RemoveLast();
402 513
514 EXPECT_FALSE(field.empty());
403 EXPECT_EQ(field.size(), 1); 515 EXPECT_EQ(field.size(), 1);
404 EXPECT_EQ(field.Get(0), "foo"); 516 EXPECT_EQ(field.Get(0), "foo");
405 517
406 field.Clear(); 518 field.Clear();
407 519
520 EXPECT_TRUE(field.empty());
408 EXPECT_EQ(field.size(), 0); 521 EXPECT_EQ(field.size(), 0);
409 } 522 }
410 523
411
412 TEST(RepeatedPtrField, Large) { 524 TEST(RepeatedPtrField, Large) {
413 RepeatedPtrField<string> field; 525 RepeatedPtrField<string> field;
414 526
415 for (int i = 0; i < 16; i++) { 527 for (int i = 0; i < 16; i++) {
416 *field.Add() += 'a' + i; 528 *field.Add() += 'a' + i;
417 } 529 }
418 530
419 EXPECT_EQ(field.size(), 16); 531 EXPECT_EQ(field.size(), 16);
420 532
421 for (int i = 0; i < 16; i++) { 533 for (int i = 0; i < 16; i++) {
422 EXPECT_EQ(field.Get(i).size(), 1); 534 EXPECT_EQ(field.Get(i).size(), 1);
423 EXPECT_EQ(field.Get(i)[0], 'a' + i); 535 EXPECT_EQ(field.Get(i)[0], 'a' + i);
424 } 536 }
425 537
426 int min_expected_usage = 16 * sizeof(string); 538 int min_expected_usage = 16 * sizeof(string);
427 EXPECT_GE(field.SpaceUsedExcludingSelf(), min_expected_usage); 539 EXPECT_GE(field.SpaceUsedExcludingSelf(), min_expected_usage);
428 } 540 }
429 541
430 TEST(RepeatedPtrField, SwapSmallSmall) { 542 TEST(RepeatedPtrField, SwapSmallSmall) {
431 RepeatedPtrField<string> field1; 543 RepeatedPtrField<string> field1;
432 RepeatedPtrField<string> field2; 544 RepeatedPtrField<string> field2;
433 545
546 EXPECT_TRUE(field1.empty());
547 EXPECT_EQ(field1.size(), 0);
548 EXPECT_TRUE(field2.empty());
549 EXPECT_EQ(field2.size(), 0);
550
434 field1.Add()->assign("foo"); 551 field1.Add()->assign("foo");
435 field1.Add()->assign("bar"); 552 field1.Add()->assign("bar");
553
554 EXPECT_FALSE(field1.empty());
555 EXPECT_EQ(field1.size(), 2);
556 EXPECT_EQ(field1.Get(0), "foo");
557 EXPECT_EQ(field1.Get(1), "bar");
558
559 EXPECT_TRUE(field2.empty());
560 EXPECT_EQ(field2.size(), 0);
561
436 field1.Swap(&field2); 562 field1.Swap(&field2);
437 563
564 EXPECT_TRUE(field1.empty());
438 EXPECT_EQ(field1.size(), 0); 565 EXPECT_EQ(field1.size(), 0);
566
439 EXPECT_EQ(field2.size(), 2); 567 EXPECT_EQ(field2.size(), 2);
440 EXPECT_EQ(field2.Get(0), "foo"); 568 EXPECT_EQ(field2.Get(0), "foo");
441 EXPECT_EQ(field2.Get(1), "bar"); 569 EXPECT_EQ(field2.Get(1), "bar");
442 } 570 }
443 571
444 TEST(RepeatedPtrField, SwapLargeSmall) { 572 TEST(RepeatedPtrField, SwapLargeSmall) {
445 RepeatedPtrField<string> field1; 573 RepeatedPtrField<string> field1;
446 RepeatedPtrField<string> field2; 574 RepeatedPtrField<string> field2;
447 575
448 field2.Add()->assign("foo"); 576 field2.Add()->assign("foo");
(...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after
632 destination.MergeFrom(source); 760 destination.MergeFrom(source);
633 761
634 ASSERT_EQ(5, destination.size()); 762 ASSERT_EQ(5, destination.size());
635 EXPECT_EQ("1", destination.Get(0)); 763 EXPECT_EQ("1", destination.Get(0));
636 EXPECT_EQ("2", destination.Get(1)); 764 EXPECT_EQ("2", destination.Get(1));
637 EXPECT_EQ("3", destination.Get(2)); 765 EXPECT_EQ("3", destination.Get(2));
638 EXPECT_EQ("4", destination.Get(3)); 766 EXPECT_EQ("4", destination.Get(3));
639 EXPECT_EQ("5", destination.Get(4)); 767 EXPECT_EQ("5", destination.Get(4));
640 } 768 }
641 769
770 #ifdef PROTOBUF_HAS_DEATH_TEST
771 TEST(RepeatedPtrField, MergeFromSelf) {
772 RepeatedPtrField<string> me;
773 me.Add()->assign("1");
774 EXPECT_DEATH(me.MergeFrom(me), "");
775 }
776 #endif // PROTOBUF_HAS_DEATH_TEST
777
642 TEST(RepeatedPtrField, CopyFrom) { 778 TEST(RepeatedPtrField, CopyFrom) {
643 RepeatedPtrField<string> source, destination; 779 RepeatedPtrField<string> source, destination;
644 source.Add()->assign("4"); 780 source.Add()->assign("4");
645 source.Add()->assign("5"); 781 source.Add()->assign("5");
646 destination.Add()->assign("1"); 782 destination.Add()->assign("1");
647 destination.Add()->assign("2"); 783 destination.Add()->assign("2");
648 destination.Add()->assign("3"); 784 destination.Add()->assign("3");
649 785
650 destination.CopyFrom(source); 786 destination.CopyFrom(source);
651 787
652 ASSERT_EQ(2, destination.size()); 788 ASSERT_EQ(2, destination.size());
653 EXPECT_EQ("4", destination.Get(0)); 789 EXPECT_EQ("4", destination.Get(0));
654 EXPECT_EQ("5", destination.Get(1)); 790 EXPECT_EQ("5", destination.Get(1));
655 } 791 }
656 792
793 TEST(RepeatedPtrField, CopyFromSelf) {
794 RepeatedPtrField<string> me;
795 me.Add()->assign("1");
796 me.CopyFrom(me);
797 ASSERT_EQ(1, me.size());
798 EXPECT_EQ("1", me.Get(0));
799 }
800
801 TEST(RepeatedPtrField, Erase) {
802 RepeatedPtrField<string> me;
803 RepeatedPtrField<string>::iterator it = me.erase(me.begin(), me.end());
804 EXPECT_TRUE(me.begin() == it);
805 EXPECT_EQ(0, me.size());
806
807 *me.Add() = "1";
808 *me.Add() = "2";
809 *me.Add() = "3";
810 it = me.erase(me.begin(), me.end());
811 EXPECT_TRUE(me.begin() == it);
812 EXPECT_EQ(0, me.size());
813
814 *me.Add() = "4";
815 *me.Add() = "5";
816 *me.Add() = "6";
817 it = me.erase(me.begin() + 2, me.end());
818 EXPECT_TRUE(me.begin() + 2 == it);
819 EXPECT_EQ(2, me.size());
820 EXPECT_EQ("4", me.Get(0));
821 EXPECT_EQ("5", me.Get(1));
822
823 *me.Add() = "6";
824 *me.Add() = "7";
825 *me.Add() = "8";
826 it = me.erase(me.begin() + 1, me.begin() + 3);
827 EXPECT_TRUE(me.begin() + 1 == it);
828 EXPECT_EQ(3, me.size());
829 EXPECT_EQ("4", me.Get(0));
830 EXPECT_EQ("7", me.Get(1));
831 EXPECT_EQ("8", me.Get(2));
832 }
833
657 TEST(RepeatedPtrField, CopyConstruct) { 834 TEST(RepeatedPtrField, CopyConstruct) {
658 RepeatedPtrField<string> source; 835 RepeatedPtrField<string> source;
659 source.Add()->assign("1"); 836 source.Add()->assign("1");
660 source.Add()->assign("2"); 837 source.Add()->assign("2");
661 838
662 RepeatedPtrField<string> destination(source); 839 RepeatedPtrField<string> destination(source);
663 840
664 ASSERT_EQ(2, destination.size()); 841 ASSERT_EQ(2, destination.size());
665 EXPECT_EQ("1", destination.Get(0)); 842 EXPECT_EQ("1", destination.Get(0));
666 EXPECT_EQ("2", destination.Get(1)); 843 EXPECT_EQ("2", destination.Get(1));
(...skipping 303 matching lines...) Expand 10 before | Expand all | Expand 10 after
970 proto_array_.Add()->assign("a"); 1147 proto_array_.Add()->assign("a");
971 proto_array_.Add()->assign("c"); 1148 proto_array_.Add()->assign("c");
972 proto_array_.Add()->assign("d"); 1149 proto_array_.Add()->assign("d");
973 proto_array_.Add()->assign("n"); 1150 proto_array_.Add()->assign("n");
974 proto_array_.Add()->assign("p"); 1151 proto_array_.Add()->assign("p");
975 proto_array_.Add()->assign("x"); 1152 proto_array_.Add()->assign("x");
976 proto_array_.Add()->assign("y"); 1153 proto_array_.Add()->assign("y");
977 1154
978 string v = "f"; 1155 string v = "f";
979 RepeatedPtrField<string>::const_iterator it = 1156 RepeatedPtrField<string>::const_iterator it =
980 lower_bound(proto_array_.begin(), proto_array_.end(), v); 1157 std::lower_bound(proto_array_.begin(), proto_array_.end(), v);
981 1158
982 EXPECT_EQ(*it, "n"); 1159 EXPECT_EQ(*it, "n");
983 EXPECT_TRUE(it == proto_array_.begin() + 3); 1160 EXPECT_TRUE(it == proto_array_.begin() + 3);
984 } 1161 }
985 1162
986 TEST_F(RepeatedPtrFieldIteratorTest, Mutation) { 1163 TEST_F(RepeatedPtrFieldIteratorTest, Mutation) {
987 RepeatedPtrField<string>::iterator iter = proto_array_.begin(); 1164 RepeatedPtrField<string>::iterator iter = proto_array_.begin();
988 *iter = "qux"; 1165 *iter = "qux";
989 EXPECT_EQ("qux", proto_array_.Get(0)); 1166 EXPECT_EQ("qux", proto_array_.Get(0));
990 } 1167 }
991 1168
992 // ------------------------------------------------------------------- 1169 // -------------------------------------------------------------------
993 1170
994 class RepeatedPtrFieldPtrsIteratorTest : public testing::Test { 1171 class RepeatedPtrFieldPtrsIteratorTest : public testing::Test {
995 protected: 1172 protected:
996 virtual void SetUp() { 1173 virtual void SetUp() {
997 proto_array_.Add()->assign("foo"); 1174 proto_array_.Add()->assign("foo");
998 proto_array_.Add()->assign("bar"); 1175 proto_array_.Add()->assign("bar");
999 proto_array_.Add()->assign("baz"); 1176 proto_array_.Add()->assign("baz");
1000 const_proto_array_ = &proto_array_; 1177 const_proto_array_ = &proto_array_;
1001 } 1178 }
1002 1179
1003 RepeatedPtrField<string> proto_array_; 1180 RepeatedPtrField<string> proto_array_;
1004 const RepeatedPtrField<string>* const_proto_array_; 1181 const RepeatedPtrField<string>* const_proto_array_;
1005 }; 1182 };
1006 1183
1007 TEST_F(RepeatedPtrFieldPtrsIteratorTest, ConvertiblePtr) { 1184 TEST_F(RepeatedPtrFieldPtrsIteratorTest, ConvertiblePtr) {
1008 RepeatedPtrField<string>::pointer_iterator iter = 1185 RepeatedPtrField<string>::pointer_iterator iter =
1009 proto_array_.pointer_begin(); 1186 proto_array_.pointer_begin();
1010 (void) iter; 1187 static_cast<void>(iter);
1011 } 1188 }
1012 1189
1013 TEST_F(RepeatedPtrFieldPtrsIteratorTest, ConvertibleConstPtr) { 1190 TEST_F(RepeatedPtrFieldPtrsIteratorTest, ConvertibleConstPtr) {
1014 RepeatedPtrField<string>::const_pointer_iterator iter = 1191 RepeatedPtrField<string>::const_pointer_iterator iter =
1015 const_proto_array_->pointer_begin(); 1192 const_proto_array_->pointer_begin();
1016 (void) iter; 1193 static_cast<void>(iter);
1017 } 1194 }
1018 1195
1019 TEST_F(RepeatedPtrFieldPtrsIteratorTest, MutablePtrIteration) { 1196 TEST_F(RepeatedPtrFieldPtrsIteratorTest, MutablePtrIteration) {
1020 RepeatedPtrField<string>::pointer_iterator iter = 1197 RepeatedPtrField<string>::pointer_iterator iter =
1021 proto_array_.pointer_begin(); 1198 proto_array_.pointer_begin();
1022 EXPECT_EQ("foo", **iter); 1199 EXPECT_EQ("foo", **iter);
1023 ++iter; 1200 ++iter;
1024 EXPECT_EQ("bar", **(iter++)); 1201 EXPECT_EQ("bar", **(iter++));
1025 EXPECT_EQ("baz", **iter); 1202 EXPECT_EQ("baz", **iter);
1026 ++iter; 1203 ++iter;
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
1115 } 1292 }
1116 1293
1117 // This comparison functor is required by the tests for RepeatedPtrOverPtrs. 1294 // This comparison functor is required by the tests for RepeatedPtrOverPtrs.
1118 // They operate on strings and need to compare strings as strings in 1295 // They operate on strings and need to compare strings as strings in
1119 // any stl algorithm, even though the iterator returns a pointer to a string 1296 // any stl algorithm, even though the iterator returns a pointer to a string
1120 // - i.e. *iter has type string*. 1297 // - i.e. *iter has type string*.
1121 struct StringLessThan { 1298 struct StringLessThan {
1122 bool operator()(const string* z, const string& y) { 1299 bool operator()(const string* z, const string& y) {
1123 return *z < y; 1300 return *z < y;
1124 } 1301 }
1125 bool operator()(const string* z, const string* y) { 1302 bool operator()(const string* z, const string* y) const { return *z < *y; }
1126 return *z < *y;
1127 }
1128 }; 1303 };
1129 1304
1130 TEST_F(RepeatedPtrFieldPtrsIteratorTest, PtrSTLAlgorithms_lower_bound) { 1305 TEST_F(RepeatedPtrFieldPtrsIteratorTest, PtrSTLAlgorithms_lower_bound) {
1131 proto_array_.Clear(); 1306 proto_array_.Clear();
1132 proto_array_.Add()->assign("a"); 1307 proto_array_.Add()->assign("a");
1133 proto_array_.Add()->assign("c"); 1308 proto_array_.Add()->assign("c");
1134 proto_array_.Add()->assign("d"); 1309 proto_array_.Add()->assign("d");
1135 proto_array_.Add()->assign("n"); 1310 proto_array_.Add()->assign("n");
1136 proto_array_.Add()->assign("p"); 1311 proto_array_.Add()->assign("p");
1137 proto_array_.Add()->assign("x"); 1312 proto_array_.Add()->assign("x");
1138 proto_array_.Add()->assign("y"); 1313 proto_array_.Add()->assign("y");
1139 1314
1140 { 1315 {
1141 string v = "f"; 1316 string v = "f";
1142 RepeatedPtrField<string>::pointer_iterator it = 1317 RepeatedPtrField<string>::pointer_iterator it =
1143 lower_bound(proto_array_.pointer_begin(), proto_array_.pointer_end(), 1318 std::lower_bound(proto_array_.pointer_begin(),
1144 &v, StringLessThan()); 1319 proto_array_.pointer_end(), &v, StringLessThan());
1145 1320
1146 GOOGLE_CHECK(*it != NULL); 1321 GOOGLE_CHECK(*it != NULL);
1147 1322
1148 EXPECT_EQ(**it, "n"); 1323 EXPECT_EQ(**it, "n");
1149 EXPECT_TRUE(it == proto_array_.pointer_begin() + 3); 1324 EXPECT_TRUE(it == proto_array_.pointer_begin() + 3);
1150 } 1325 }
1151 { 1326 {
1152 string v = "f"; 1327 string v = "f";
1153 RepeatedPtrField<string>::const_pointer_iterator it = 1328 RepeatedPtrField<string>::const_pointer_iterator it = std::lower_bound(
1154 lower_bound(const_proto_array_->pointer_begin(), 1329 const_proto_array_->pointer_begin(), const_proto_array_->pointer_end(),
1155 const_proto_array_->pointer_end(), 1330 &v, StringLessThan());
1156 &v, StringLessThan());
1157 1331
1158 GOOGLE_CHECK(*it != NULL); 1332 GOOGLE_CHECK(*it != NULL);
1159 1333
1160 EXPECT_EQ(**it, "n"); 1334 EXPECT_EQ(**it, "n");
1161 EXPECT_TRUE(it == const_proto_array_->pointer_begin() + 3); 1335 EXPECT_TRUE(it == const_proto_array_->pointer_begin() + 3);
1162 } 1336 }
1163 } 1337 }
1164 1338
1165 TEST_F(RepeatedPtrFieldPtrsIteratorTest, PtrMutation) { 1339 TEST_F(RepeatedPtrFieldPtrsIteratorTest, PtrMutation) {
1166 RepeatedPtrField<string>::pointer_iterator iter = 1340 RepeatedPtrField<string>::pointer_iterator iter =
(...skipping 17 matching lines...) Expand all
1184 proto_array_.Add()->assign("c"); 1358 proto_array_.Add()->assign("c");
1185 proto_array_.Add()->assign("d"); 1359 proto_array_.Add()->assign("d");
1186 proto_array_.Add()->assign("n"); 1360 proto_array_.Add()->assign("n");
1187 proto_array_.Add()->assign("p"); 1361 proto_array_.Add()->assign("p");
1188 proto_array_.Add()->assign("a"); 1362 proto_array_.Add()->assign("a");
1189 proto_array_.Add()->assign("y"); 1363 proto_array_.Add()->assign("y");
1190 proto_array_.Add()->assign("x"); 1364 proto_array_.Add()->assign("x");
1191 EXPECT_EQ("foo", proto_array_.Get(0)); 1365 EXPECT_EQ("foo", proto_array_.Get(0));
1192 EXPECT_EQ("n", proto_array_.Get(5)); 1366 EXPECT_EQ("n", proto_array_.Get(5));
1193 EXPECT_EQ("x", proto_array_.Get(9)); 1367 EXPECT_EQ("x", proto_array_.Get(9));
1194 sort(proto_array_.pointer_begin(), 1368 std::sort(proto_array_.pointer_begin(), proto_array_.pointer_end(),
1195 proto_array_.pointer_end(), 1369 StringLessThan());
1196 StringLessThan());
1197 EXPECT_EQ("a", proto_array_.Get(0)); 1370 EXPECT_EQ("a", proto_array_.Get(0));
1198 EXPECT_EQ("baz", proto_array_.Get(2)); 1371 EXPECT_EQ("baz", proto_array_.Get(2));
1199 EXPECT_EQ("y", proto_array_.Get(9)); 1372 EXPECT_EQ("y", proto_array_.Get(9));
1200 } 1373 }
1201 1374
1202 1375
1203 // ----------------------------------------------------------------------------- 1376 // -----------------------------------------------------------------------------
1204 // Unit-tests for the insert iterators 1377 // Unit-tests for the insert iterators
1205 // google::protobuf::RepeatedFieldBackInserter, 1378 // google::protobuf::RepeatedFieldBackInserter,
1206 // google::protobuf::AllocatedRepeatedPtrFieldBackInserter 1379 // google::protobuf::AllocatedRepeatedPtrFieldBackInserter
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
1250 RepeatedFieldBackInserter( 1423 RepeatedFieldBackInserter(
1251 protobuffer.mutable_repeated_nested_message())); 1424 protobuffer.mutable_repeated_nested_message()));
1252 1425
1253 nested_ptrs.push_back(new Nested); 1426 nested_ptrs.push_back(new Nested);
1254 nested_ptrs.back()->set_bb(170); 1427 nested_ptrs.back()->set_bb(170);
1255 nested_ptrs.push_back(new Nested); 1428 nested_ptrs.push_back(new Nested);
1256 nested_ptrs.back()->set_bb(47110); 1429 nested_ptrs.back()->set_bb(47110);
1257 std::copy(nested_ptrs.begin(), nested_ptrs.end(), 1430 std::copy(nested_ptrs.begin(), nested_ptrs.end(),
1258 RepeatedFieldBackInserter( 1431 RepeatedFieldBackInserter(
1259 protobuffer.mutable_repeated_nested_message())); 1432 protobuffer.mutable_repeated_nested_message()));
1260
1261 } 1433 }
1262 1434
1263 virtual void TearDown() { 1435 virtual void TearDown() {
1264 STLDeleteContainerPointers(nested_ptrs.begin(), nested_ptrs.end()); 1436 STLDeleteContainerPointers(nested_ptrs.begin(), nested_ptrs.end());
1265 } 1437 }
1266 }; 1438 };
1267 1439
1268 TEST_F(RepeatedFieldInsertionIteratorsTest, Fibonacci) { 1440 TEST_F(RepeatedFieldInsertionIteratorsTest, Fibonacci) {
1269 EXPECT_TRUE(std::equal(fibonacci.begin(), 1441 EXPECT_TRUE(std::equal(fibonacci.begin(),
1270 fibonacci.end(), 1442 fibonacci.end(),
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
1319 TestAllTypes goldenproto; 1491 TestAllTypes goldenproto;
1320 for (int i = 0; i < 10; ++i) { 1492 for (int i = 0; i < 10; ++i) {
1321 Nested* new_data = new Nested; 1493 Nested* new_data = new Nested;
1322 new_data->set_bb(i); 1494 new_data->set_bb(i);
1323 data.push_back(new_data); 1495 data.push_back(new_data);
1324 1496
1325 new_data = goldenproto.add_repeated_nested_message(); 1497 new_data = goldenproto.add_repeated_nested_message();
1326 new_data->set_bb(i); 1498 new_data->set_bb(i);
1327 } 1499 }
1328 TestAllTypes testproto; 1500 TestAllTypes testproto;
1329 copy(data.begin(), data.end(), 1501 std::copy(data.begin(), data.end(),
1330 AllocatedRepeatedPtrFieldBackInserter( 1502 AllocatedRepeatedPtrFieldBackInserter(
1331 testproto.mutable_repeated_nested_message())); 1503 testproto.mutable_repeated_nested_message()));
1332 EXPECT_EQ(testproto.DebugString(), goldenproto.DebugString()); 1504 EXPECT_EQ(testproto.DebugString(), goldenproto.DebugString());
1333 } 1505 }
1334 1506
1335 TEST_F(RepeatedFieldInsertionIteratorsTest, 1507 TEST_F(RepeatedFieldInsertionIteratorsTest,
1336 AllocatedRepeatedPtrFieldWithString) { 1508 AllocatedRepeatedPtrFieldWithString) {
1337 vector<string*> data; 1509 vector<string*> data;
1338 TestAllTypes goldenproto; 1510 TestAllTypes goldenproto;
1339 for (int i = 0; i < 10; ++i) { 1511 for (int i = 0; i < 10; ++i) {
1340 string* new_data = new string; 1512 string* new_data = new string;
1341 *new_data = "name-" + SimpleItoa(i); 1513 *new_data = "name-" + SimpleItoa(i);
1342 data.push_back(new_data); 1514 data.push_back(new_data);
1343 1515
1344 new_data = goldenproto.add_repeated_string(); 1516 new_data = goldenproto.add_repeated_string();
1345 *new_data = "name-" + SimpleItoa(i); 1517 *new_data = "name-" + SimpleItoa(i);
1346 } 1518 }
1347 TestAllTypes testproto; 1519 TestAllTypes testproto;
1348 copy(data.begin(), data.end(), 1520 std::copy(data.begin(), data.end(), AllocatedRepeatedPtrFieldBackInserter(
1349 AllocatedRepeatedPtrFieldBackInserter( 1521 testproto.mutable_repeated_string()));
1350 testproto.mutable_repeated_string()));
1351 EXPECT_EQ(testproto.DebugString(), goldenproto.DebugString()); 1522 EXPECT_EQ(testproto.DebugString(), goldenproto.DebugString());
1352 } 1523 }
1353 1524
1354 } // namespace 1525 } // namespace
1355 1526
1356 } // namespace protobuf 1527 } // namespace protobuf
1357 } // namespace google 1528 } // namespace google
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698