1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 // * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31 // Author: kenton@google.com (Kenton Varda)
32 // Based on original Protocol Buffers design by
33 // Sanjay Ghemawat, Jeff Dean, and others.
34 //
35 // TODO(kenton): Improve this unittest to bring it up to the standards of
36 // other proto2 unittests.
37
38 #include <google/protobuf/repeated_field.h>
39
40 #include <algorithm>
41 #include <limits>
42 #include <list>
43 #include <sstream>
44 #include <type_traits>
45 #include <vector>
46
47 #include <google/protobuf/stubs/logging.h>
48 #include <google/protobuf/stubs/common.h>
49 #include <google/protobuf/unittest.pb.h>
50 #include <google/protobuf/stubs/strutil.h>
51 #include <gmock/gmock.h>
52 #include <google/protobuf/testing/googletest.h>
53 #include <gtest/gtest.h>
54
55 #include <google/protobuf/stubs/stl_util.h>
56
57 namespace google {
58 namespace protobuf {
59 namespace {
60
61 using ::protobuf_unittest::TestAllTypes;
62 using ::testing::ElementsAre;
63
64 // Test operations on a small RepeatedField.
TEST(RepeatedField,Small)65 TEST(RepeatedField, Small) {
66 RepeatedField<int> field;
67
68 EXPECT_TRUE(field.empty());
69 EXPECT_EQ(field.size(), 0);
70
71 field.Add(5);
72
73 EXPECT_FALSE(field.empty());
74 EXPECT_EQ(field.size(), 1);
75 EXPECT_EQ(field.Get(0), 5);
76 EXPECT_EQ(field.at(0), 5);
77
78 field.Add(42);
79
80 EXPECT_FALSE(field.empty());
81 EXPECT_EQ(field.size(), 2);
82 EXPECT_EQ(field.Get(0), 5);
83 EXPECT_EQ(field.at(0), 5);
84 EXPECT_EQ(field.Get(1), 42);
85 EXPECT_EQ(field.at(1), 42);
86
87 field.Set(1, 23);
88
89 EXPECT_FALSE(field.empty());
90 EXPECT_EQ(field.size(), 2);
91 EXPECT_EQ(field.Get(0), 5);
92 EXPECT_EQ(field.at(0), 5);
93 EXPECT_EQ(field.Get(1), 23);
94 EXPECT_EQ(field.at(1), 23);
95
96 field.at(1) = 25;
97
98 EXPECT_FALSE(field.empty());
99 EXPECT_EQ(field.size(), 2);
100 EXPECT_EQ(field.Get(0), 5);
101 EXPECT_EQ(field.at(0), 5);
102 EXPECT_EQ(field.Get(1), 25);
103 EXPECT_EQ(field.at(1), 25);
104
105 field.RemoveLast();
106
107 EXPECT_FALSE(field.empty());
108 EXPECT_EQ(field.size(), 1);
109 EXPECT_EQ(field.Get(0), 5);
110 EXPECT_EQ(field.at(0), 5);
111
112 field.Clear();
113
114 EXPECT_TRUE(field.empty());
115 EXPECT_EQ(field.size(), 0);
116 // Additional bytes are for 'struct Rep' header.
117 int expected_usage = 4 * sizeof(int) + sizeof(Arena*);
118 EXPECT_GE(field.SpaceUsedExcludingSelf(), expected_usage);
119 }
120
121
122 // Test operations on a RepeatedField which is large enough to allocate a
123 // separate array.
TEST(RepeatedField,Large)124 TEST(RepeatedField, Large) {
125 RepeatedField<int> field;
126
127 for (int i = 0; i < 16; i++) {
128 field.Add(i * i);
129 }
130
131 EXPECT_FALSE(field.empty());
132 EXPECT_EQ(field.size(), 16);
133
134 for (int i = 0; i < 16; i++) {
135 EXPECT_EQ(field.Get(i), i * i);
136 }
137
138 int expected_usage = 16 * sizeof(int);
139 EXPECT_GE(field.SpaceUsedExcludingSelf(), expected_usage);
140 }
141
142 // Test swapping between various types of RepeatedFields.
TEST(RepeatedField,SwapSmallSmall)143 TEST(RepeatedField, SwapSmallSmall) {
144 RepeatedField<int> field1;
145 RepeatedField<int> field2;
146
147 field1.Add(5);
148 field1.Add(42);
149
150 EXPECT_FALSE(field1.empty());
151 EXPECT_EQ(field1.size(), 2);
152 EXPECT_EQ(field1.Get(0), 5);
153 EXPECT_EQ(field1.Get(1), 42);
154
155 EXPECT_TRUE(field2.empty());
156 EXPECT_EQ(field2.size(), 0);
157
158 field1.Swap(&field2);
159
160 EXPECT_TRUE(field1.empty());
161 EXPECT_EQ(field1.size(), 0);
162
163 EXPECT_FALSE(field2.empty());
164 EXPECT_EQ(field2.size(), 2);
165 EXPECT_EQ(field2.Get(0), 5);
166 EXPECT_EQ(field2.Get(1), 42);
167 }
168
TEST(RepeatedField,SwapLargeSmall)169 TEST(RepeatedField, SwapLargeSmall) {
170 RepeatedField<int> field1;
171 RepeatedField<int> field2;
172
173 for (int i = 0; i < 16; i++) {
174 field1.Add(i * i);
175 }
176 field2.Add(5);
177 field2.Add(42);
178 field1.Swap(&field2);
179
180 EXPECT_EQ(field1.size(), 2);
181 EXPECT_EQ(field1.Get(0), 5);
182 EXPECT_EQ(field1.Get(1), 42);
183 EXPECT_EQ(field2.size(), 16);
184 for (int i = 0; i < 16; i++) {
185 EXPECT_EQ(field2.Get(i), i * i);
186 }
187 }
188
TEST(RepeatedField,SwapLargeLarge)189 TEST(RepeatedField, SwapLargeLarge) {
190 RepeatedField<int> field1;
191 RepeatedField<int> field2;
192
193 field1.Add(5);
194 field1.Add(42);
195 for (int i = 0; i < 16; i++) {
196 field1.Add(i);
197 field2.Add(i * i);
198 }
199 field2.Swap(&field1);
200
201 EXPECT_EQ(field1.size(), 16);
202 for (int i = 0; i < 16; i++) {
203 EXPECT_EQ(field1.Get(i), i * i);
204 }
205 EXPECT_EQ(field2.size(), 18);
206 EXPECT_EQ(field2.Get(0), 5);
207 EXPECT_EQ(field2.Get(1), 42);
208 for (int i = 2; i < 18; i++) {
209 EXPECT_EQ(field2.Get(i), i - 2);
210 }
211 }
212
213 // Determines how much space was reserved by the given field by adding elements
214 // to it until it re-allocates its space.
ReservedSpace(RepeatedField<int> * field)215 static int ReservedSpace(RepeatedField<int>* field) {
216 const int* ptr = field->data();
217 do {
218 field->Add(0);
219 } while (field->data() == ptr);
220
221 return field->size() - 1;
222 }
223
TEST(RepeatedField,ReserveMoreThanDouble)224 TEST(RepeatedField, ReserveMoreThanDouble) {
225 // Reserve more than double the previous space in the field and expect the
226 // field to reserve exactly the amount specified.
227 RepeatedField<int> field;
228 field.Reserve(20);
229
230 EXPECT_LE(20, ReservedSpace(&field));
231 }
232
TEST(RepeatedField,ReserveLessThanDouble)233 TEST(RepeatedField, ReserveLessThanDouble) {
234 // Reserve less than double the previous space in the field and expect the
235 // field to grow by double instead.
236 RepeatedField<int> field;
237 field.Reserve(20);
238 int capacity = field.Capacity();
239 field.Reserve(capacity * 1.5);
240
241 EXPECT_LE(2 * capacity, ReservedSpace(&field));
242 }
243
TEST(RepeatedField,ReserveLessThanExisting)244 TEST(RepeatedField, ReserveLessThanExisting) {
245 // Reserve less than the previous space in the field and expect the
246 // field to not re-allocate at all.
247 RepeatedField<int> field;
248 field.Reserve(20);
249 const int* previous_ptr = field.data();
250 field.Reserve(10);
251
252 EXPECT_EQ(previous_ptr, field.data());
253 EXPECT_LE(20, ReservedSpace(&field));
254 }
255
TEST(RepeatedField,Resize)256 TEST(RepeatedField, Resize) {
257 RepeatedField<int> field;
258 field.Resize(2, 1);
259 EXPECT_EQ(2, field.size());
260 field.Resize(5, 2);
261 EXPECT_EQ(5, field.size());
262 field.Resize(4, 3);
263 ASSERT_EQ(4, field.size());
264 EXPECT_EQ(1, field.Get(0));
265 EXPECT_EQ(1, field.Get(1));
266 EXPECT_EQ(2, field.Get(2));
267 EXPECT_EQ(2, field.Get(3));
268 field.Resize(0, 4);
269 EXPECT_TRUE(field.empty());
270 }
271
TEST(RepeatedField,MergeFrom)272 TEST(RepeatedField, MergeFrom) {
273 RepeatedField<int> source, destination;
274 source.Add(4);
275 source.Add(5);
276 destination.Add(1);
277 destination.Add(2);
278 destination.Add(3);
279
280 destination.MergeFrom(source);
281
282 ASSERT_EQ(5, destination.size());
283 EXPECT_EQ(1, destination.Get(0));
284 EXPECT_EQ(2, destination.Get(1));
285 EXPECT_EQ(3, destination.Get(2));
286 EXPECT_EQ(4, destination.Get(3));
287 EXPECT_EQ(5, destination.Get(4));
288 }
289
290
TEST(RepeatedField,CopyFrom)291 TEST(RepeatedField, CopyFrom) {
292 RepeatedField<int> source, destination;
293 source.Add(4);
294 source.Add(5);
295 destination.Add(1);
296 destination.Add(2);
297 destination.Add(3);
298
299 destination.CopyFrom(source);
300
301 ASSERT_EQ(2, destination.size());
302 EXPECT_EQ(4, destination.Get(0));
303 EXPECT_EQ(5, destination.Get(1));
304 }
305
TEST(RepeatedField,CopyFromSelf)306 TEST(RepeatedField, CopyFromSelf) {
307 RepeatedField<int> me;
308 me.Add(3);
309 me.CopyFrom(me);
310 ASSERT_EQ(1, me.size());
311 EXPECT_EQ(3, me.Get(0));
312 }
313
TEST(RepeatedField,Erase)314 TEST(RepeatedField, Erase) {
315 RepeatedField<int> me;
316 RepeatedField<int>::iterator it = me.erase(me.begin(), me.end());
317 EXPECT_TRUE(me.begin() == it);
318 EXPECT_EQ(0, me.size());
319
320 me.Add(1);
321 me.Add(2);
322 me.Add(3);
323 it = me.erase(me.begin(), me.end());
324 EXPECT_TRUE(me.begin() == it);
325 EXPECT_EQ(0, me.size());
326
327 me.Add(4);
328 me.Add(5);
329 me.Add(6);
330 it = me.erase(me.begin() + 2, me.end());
331 EXPECT_TRUE(me.begin() + 2 == it);
332 EXPECT_EQ(2, me.size());
333 EXPECT_EQ(4, me.Get(0));
334 EXPECT_EQ(5, me.Get(1));
335
336 me.Add(6);
337 me.Add(7);
338 me.Add(8);
339 it = me.erase(me.begin() + 1, me.begin() + 3);
340 EXPECT_TRUE(me.begin() + 1 == it);
341 EXPECT_EQ(3, me.size());
342 EXPECT_EQ(4, me.Get(0));
343 EXPECT_EQ(7, me.Get(1));
344 EXPECT_EQ(8, me.Get(2));
345 }
346
347 // Add contents of empty container to an empty field.
TEST(RepeatedField,AddRange1)348 TEST(RepeatedField, AddRange1) {
349 RepeatedField<int> me;
350 std::vector<int> values;
351
352 me.Add(values.begin(), values.end());
353 ASSERT_EQ(me.size(), 0);
354 }
355
356 // Add contents of container with one thing to an empty field.
TEST(RepeatedField,AddRange2)357 TEST(RepeatedField, AddRange2) {
358 RepeatedField<int> me;
359 std::vector<int> values;
360 values.push_back(-1);
361
362 me.Add(values.begin(), values.end());
363 ASSERT_EQ(me.size(), 1);
364 ASSERT_EQ(me.Get(0), values[0]);
365 }
366
367 // Add contents of container with more than one thing to an empty field.
TEST(RepeatedField,AddRange3)368 TEST(RepeatedField, AddRange3) {
369 RepeatedField<int> me;
370 std::vector<int> values;
371 values.push_back(0);
372 values.push_back(1);
373
374 me.Add(values.begin(), values.end());
375 ASSERT_EQ(me.size(), 2);
376 ASSERT_EQ(me.Get(0), values[0]);
377 ASSERT_EQ(me.Get(1), values[1]);
378 }
379
380 // Add contents of container with more than one thing to a non-empty field.
TEST(RepeatedField,AddRange4)381 TEST(RepeatedField, AddRange4) {
382 RepeatedField<int> me;
383 me.Add(0);
384 me.Add(1);
385
386 std::vector<int> values;
387 values.push_back(2);
388 values.push_back(3);
389
390 me.Add(values.begin(), values.end());
391 ASSERT_EQ(me.size(), 4);
392 ASSERT_EQ(me.Get(0), 0);
393 ASSERT_EQ(me.Get(1), 1);
394 ASSERT_EQ(me.Get(2), values[0]);
395 ASSERT_EQ(me.Get(3), values[1]);
396 }
397
398 // Add contents of a stringstream in order to test code paths where there is
399 // an input iterator.
TEST(RepeatedField,AddRange5)400 TEST(RepeatedField, AddRange5) {
401 RepeatedField<int> me;
402
403 std::stringstream ss;
404 ss << 1 << ' ' << 2;
405
406 me.Add(std::istream_iterator<int>(ss), std::istream_iterator<int>());
407 ASSERT_EQ(me.size(), 2);
408 ASSERT_EQ(me.Get(0), 1);
409 ASSERT_EQ(me.Get(1), 2);
410 }
411
TEST(RepeatedField,CopyConstruct)412 TEST(RepeatedField, CopyConstruct) {
413 RepeatedField<int> source;
414 source.Add(1);
415 source.Add(2);
416
417 RepeatedField<int> destination(source);
418
419 ASSERT_EQ(2, destination.size());
420 EXPECT_EQ(1, destination.Get(0));
421 EXPECT_EQ(2, destination.Get(1));
422 }
423
TEST(RepeatedField,IteratorConstruct)424 TEST(RepeatedField, IteratorConstruct) {
425 std::vector<int> values;
426 RepeatedField<int> empty(values.begin(), values.end());
427 ASSERT_EQ(values.size(), empty.size());
428
429 values.push_back(1);
430 values.push_back(2);
431
432 RepeatedField<int> field(values.begin(), values.end());
433 ASSERT_EQ(values.size(), field.size());
434 EXPECT_EQ(values[0], field.Get(0));
435 EXPECT_EQ(values[1], field.Get(1));
436
437 RepeatedField<int> other(field.begin(), field.end());
438 ASSERT_EQ(values.size(), other.size());
439 EXPECT_EQ(values[0], other.Get(0));
440 EXPECT_EQ(values[1], other.Get(1));
441 }
442
TEST(RepeatedField,CopyAssign)443 TEST(RepeatedField, CopyAssign) {
444 RepeatedField<int> source, destination;
445 source.Add(4);
446 source.Add(5);
447 destination.Add(1);
448 destination.Add(2);
449 destination.Add(3);
450
451 destination = source;
452
453 ASSERT_EQ(2, destination.size());
454 EXPECT_EQ(4, destination.Get(0));
455 EXPECT_EQ(5, destination.Get(1));
456 }
457
TEST(RepeatedField,SelfAssign)458 TEST(RepeatedField, SelfAssign) {
459 // Verify that assignment to self does not destroy data.
460 RepeatedField<int> source, *p;
461 p = &source;
462 source.Add(7);
463 source.Add(8);
464
465 *p = source;
466
467 ASSERT_EQ(2, source.size());
468 EXPECT_EQ(7, source.Get(0));
469 EXPECT_EQ(8, source.Get(1));
470 }
471
TEST(RepeatedField,MoveConstruct)472 TEST(RepeatedField, MoveConstruct) {
473 {
474 RepeatedField<int> source;
475 source.Add(1);
476 source.Add(2);
477 const int* data = source.data();
478 RepeatedField<int> destination = std::move(source);
479 EXPECT_EQ(data, destination.data());
480 EXPECT_THAT(destination, ElementsAre(1, 2));
481 // This property isn't guaranteed but it's useful to have a test that would
482 // catch changes in this area.
483 EXPECT_TRUE(source.empty());
484 }
485 {
486 Arena arena;
487 RepeatedField<int>* source =
488 Arena::CreateMessage<RepeatedField<int>>(&arena);
489 source->Add(1);
490 source->Add(2);
491 RepeatedField<int> destination = std::move(*source);
492 EXPECT_EQ(nullptr, destination.GetArena());
493 EXPECT_THAT(destination, ElementsAre(1, 2));
494 // This property isn't guaranteed but it's useful to have a test that would
495 // catch changes in this area.
496 EXPECT_THAT(*source, ElementsAre(1, 2));
497 }
498 }
499
TEST(RepeatedField,MoveAssign)500 TEST(RepeatedField, MoveAssign) {
501 {
502 RepeatedField<int> source;
503 source.Add(1);
504 source.Add(2);
505 RepeatedField<int> destination;
506 destination.Add(3);
507 const int* source_data = source.data();
508 const int* destination_data = destination.data();
509 destination = std::move(source);
510 EXPECT_EQ(source_data, destination.data());
511 EXPECT_THAT(destination, ElementsAre(1, 2));
512 // This property isn't guaranteed but it's useful to have a test that would
513 // catch changes in this area.
514 EXPECT_EQ(destination_data, source.data());
515 EXPECT_THAT(source, ElementsAre(3));
516 }
517 {
518 Arena arena;
519 RepeatedField<int>* source =
520 Arena::CreateMessage<RepeatedField<int>>(&arena);
521 source->Add(1);
522 source->Add(2);
523 RepeatedField<int>* destination =
524 Arena::CreateMessage<RepeatedField<int>>(&arena);
525 destination->Add(3);
526 const int* source_data = source->data();
527 const int* destination_data = destination->data();
528 *destination = std::move(*source);
529 EXPECT_EQ(source_data, destination->data());
530 EXPECT_THAT(*destination, ElementsAre(1, 2));
531 // This property isn't guaranteed but it's useful to have a test that would
532 // catch changes in this area.
533 EXPECT_EQ(destination_data, source->data());
534 EXPECT_THAT(*source, ElementsAre(3));
535 }
536 {
537 Arena source_arena;
538 RepeatedField<int>* source =
539 Arena::CreateMessage<RepeatedField<int>>(&source_arena);
540 source->Add(1);
541 source->Add(2);
542 Arena destination_arena;
543 RepeatedField<int>* destination =
544 Arena::CreateMessage<RepeatedField<int>>(&destination_arena);
545 destination->Add(3);
546 *destination = std::move(*source);
547 EXPECT_THAT(*destination, ElementsAre(1, 2));
548 // This property isn't guaranteed but it's useful to have a test that would
549 // catch changes in this area.
550 EXPECT_THAT(*source, ElementsAre(1, 2));
551 }
552 {
553 Arena arena;
554 RepeatedField<int>* source =
555 Arena::CreateMessage<RepeatedField<int>>(&arena);
556 source->Add(1);
557 source->Add(2);
558 RepeatedField<int> destination;
559 destination.Add(3);
560 destination = std::move(*source);
561 EXPECT_THAT(destination, ElementsAre(1, 2));
562 // This property isn't guaranteed but it's useful to have a test that would
563 // catch changes in this area.
564 EXPECT_THAT(*source, ElementsAre(1, 2));
565 }
566 {
567 RepeatedField<int> source;
568 source.Add(1);
569 source.Add(2);
570 Arena arena;
571 RepeatedField<int>* destination =
572 Arena::CreateMessage<RepeatedField<int>>(&arena);
573 destination->Add(3);
574 *destination = std::move(source);
575 EXPECT_THAT(*destination, ElementsAre(1, 2));
576 // This property isn't guaranteed but it's useful to have a test that would
577 // catch changes in this area.
578 EXPECT_THAT(source, ElementsAre(1, 2));
579 }
580 {
581 RepeatedField<int> field;
582 // An alias to defeat -Wself-move.
583 RepeatedField<int>& alias = field;
584 field.Add(1);
585 field.Add(2);
586 const int* data = field.data();
587 field = std::move(alias);
588 EXPECT_EQ(data, field.data());
589 EXPECT_THAT(field, ElementsAre(1, 2));
590 }
591 {
592 Arena arena;
593 RepeatedField<int>* field =
594 Arena::CreateMessage<RepeatedField<int>>(&arena);
595 field->Add(1);
596 field->Add(2);
597 const int* data = field->data();
598 *field = std::move(*field);
599 EXPECT_EQ(data, field->data());
600 EXPECT_THAT(*field, ElementsAre(1, 2));
601 }
602 }
603
TEST(Movable,Works)604 TEST(Movable, Works) {
605 class NonMoveConstructible {
606 public:
607 NonMoveConstructible(NonMoveConstructible&&) = delete;
608 NonMoveConstructible& operator=(NonMoveConstructible&&) { return *this; }
609 };
610 class NonMoveAssignable {
611 public:
612 NonMoveAssignable(NonMoveAssignable&&) {}
613 NonMoveAssignable& operator=(NonMoveConstructible&&) = delete;
614 };
615 class NonMovable {
616 public:
617 NonMovable(NonMovable&&) = delete;
618 NonMovable& operator=(NonMovable&&) = delete;
619 };
620
621 EXPECT_TRUE(internal::IsMovable<std::string>::value);
622
623 EXPECT_FALSE(std::is_move_constructible<NonMoveConstructible>::value);
624 EXPECT_TRUE(std::is_move_assignable<NonMoveConstructible>::value);
625 EXPECT_FALSE(internal::IsMovable<NonMoveConstructible>::value);
626
627 EXPECT_TRUE(std::is_move_constructible<NonMoveAssignable>::value);
628 EXPECT_FALSE(std::is_move_assignable<NonMoveAssignable>::value);
629 EXPECT_FALSE(internal::IsMovable<NonMoveAssignable>::value);
630
631 EXPECT_FALSE(internal::IsMovable<NonMovable>::value);
632 }
633
TEST(RepeatedField,MoveAdd)634 TEST(RepeatedField, MoveAdd) {
635 RepeatedPtrField<TestAllTypes> field;
636 TestAllTypes test_all_types;
637 auto* optional_nested_message =
638 test_all_types.mutable_optional_nested_message();
639 optional_nested_message->set_bb(42);
640 field.Add(std::move(test_all_types));
641
642 EXPECT_EQ(optional_nested_message,
643 field.Mutable(0)->mutable_optional_nested_message());
644 }
645
TEST(RepeatedField,MutableDataIsMutable)646 TEST(RepeatedField, MutableDataIsMutable) {
647 RepeatedField<int> field;
648 field.Add(1);
649 EXPECT_EQ(1, field.Get(0));
650 // The fact that this line compiles would be enough, but we'll check the
651 // value anyway.
652 *field.mutable_data() = 2;
653 EXPECT_EQ(2, field.Get(0));
654 }
655
TEST(RepeatedField,SubscriptOperators)656 TEST(RepeatedField, SubscriptOperators) {
657 RepeatedField<int> field;
658 field.Add(1);
659 EXPECT_EQ(1, field.Get(0));
660 EXPECT_EQ(1, field[0]);
661 EXPECT_EQ(field.Mutable(0), &field[0]);
662 const RepeatedField<int>& const_field = field;
663 EXPECT_EQ(field.data(), &const_field[0]);
664 }
665
TEST(RepeatedField,Truncate)666 TEST(RepeatedField, Truncate) {
667 RepeatedField<int> field;
668
669 field.Add(12);
670 field.Add(34);
671 field.Add(56);
672 field.Add(78);
673 EXPECT_EQ(4, field.size());
674
675 field.Truncate(3);
676 EXPECT_EQ(3, field.size());
677
678 field.Add(90);
679 EXPECT_EQ(4, field.size());
680 EXPECT_EQ(90, field.Get(3));
681
682 // Truncations that don't change the size are allowed, but growing is not
683 // allowed.
684 field.Truncate(field.size());
685 #ifdef PROTOBUF_HAS_DEATH_TEST
686 EXPECT_DEBUG_DEATH(field.Truncate(field.size() + 1), "new_size");
687 #endif
688 }
689
690
TEST(RepeatedField,ExtractSubrange)691 TEST(RepeatedField, ExtractSubrange) {
692 // Exhaustively test every subrange in arrays of all sizes from 0 through 9.
693 for (int sz = 0; sz < 10; ++sz) {
694 for (int num = 0; num <= sz; ++num) {
695 for (int start = 0; start < sz - num; ++start) {
696 // Create RepeatedField with sz elements having values 0 through sz-1.
697 RepeatedField<int32> field;
698 for (int i = 0; i < sz; ++i) field.Add(i);
699 EXPECT_EQ(field.size(), sz);
700
701 // Create a catcher array and call ExtractSubrange.
702 int32 catcher[10];
703 for (int i = 0; i < 10; ++i) catcher[i] = -1;
704 field.ExtractSubrange(start, num, catcher);
705
706 // Does the resulting array have the right size?
707 EXPECT_EQ(field.size(), sz - num);
708
709 // Were the removed elements extracted into the catcher array?
710 for (int i = 0; i < num; ++i) EXPECT_EQ(catcher[i], start + i);
711 EXPECT_EQ(catcher[num], -1);
712
713 // Does the resulting array contain the right values?
714 for (int i = 0; i < start; ++i) EXPECT_EQ(field.Get(i), i);
715 for (int i = start; i < field.size(); ++i)
716 EXPECT_EQ(field.Get(i), i + num);
717 }
718 }
719 }
720 }
721
TEST(RepeatedField,ClearThenReserveMore)722 TEST(RepeatedField, ClearThenReserveMore) {
723 // Test that Reserve properly destroys the old internal array when it's forced
724 // to allocate a new one, even when cleared-but-not-deleted objects are
725 // present. Use a 'string' and > 16 bytes length so that the elements are
726 // non-POD and allocate -- the leak checker will catch any skipped destructor
727 // calls here.
728 RepeatedField<std::string> field;
729 for (int i = 0; i < 32; i++) {
730 field.Add(std::string("abcdefghijklmnopqrstuvwxyz0123456789"));
731 }
732 EXPECT_EQ(32, field.size());
733 field.Clear();
734 EXPECT_EQ(0, field.size());
735 EXPECT_LE(32, field.Capacity());
736
737 field.Reserve(1024);
738 EXPECT_EQ(0, field.size());
739 EXPECT_LE(1024, field.Capacity());
740 // Finish test -- |field| should destroy the cleared-but-not-yet-destroyed
741 // strings.
742 }
743
744 // ===================================================================
745 // RepeatedPtrField tests. These pretty much just mirror the RepeatedField
746 // tests above.
747
TEST(RepeatedPtrField,Small)748 TEST(RepeatedPtrField, Small) {
749 RepeatedPtrField<std::string> field;
750
751 EXPECT_TRUE(field.empty());
752 EXPECT_EQ(field.size(), 0);
753
754 field.Add()->assign("foo");
755
756 EXPECT_FALSE(field.empty());
757 EXPECT_EQ(field.size(), 1);
758 EXPECT_EQ(field.Get(0), "foo");
759 EXPECT_EQ(field.at(0), "foo");
760
761 field.Add()->assign("bar");
762
763 EXPECT_FALSE(field.empty());
764 EXPECT_EQ(field.size(), 2);
765 EXPECT_EQ(field.Get(0), "foo");
766 EXPECT_EQ(field.at(0), "foo");
767 EXPECT_EQ(field.Get(1), "bar");
768 EXPECT_EQ(field.at(1), "bar");
769
770 field.Mutable(1)->assign("baz");
771
772 EXPECT_FALSE(field.empty());
773 EXPECT_EQ(field.size(), 2);
774 EXPECT_EQ(field.Get(0), "foo");
775 EXPECT_EQ(field.at(0), "foo");
776 EXPECT_EQ(field.Get(1), "baz");
777 EXPECT_EQ(field.at(1), "baz");
778
779 field.RemoveLast();
780
781 EXPECT_FALSE(field.empty());
782 EXPECT_EQ(field.size(), 1);
783 EXPECT_EQ(field.Get(0), "foo");
784 EXPECT_EQ(field.at(0), "foo");
785
786 field.Clear();
787
788 EXPECT_TRUE(field.empty());
789 EXPECT_EQ(field.size(), 0);
790 }
791
TEST(RepeatedPtrField,Large)792 TEST(RepeatedPtrField, Large) {
793 RepeatedPtrField<std::string> field;
794
795 for (int i = 0; i < 16; i++) {
796 *field.Add() += 'a' + i;
797 }
798
799 EXPECT_EQ(field.size(), 16);
800
801 for (int i = 0; i < 16; i++) {
802 EXPECT_EQ(field.Get(i).size(), 1);
803 EXPECT_EQ(field.Get(i)[0], 'a' + i);
804 }
805
806 int min_expected_usage = 16 * sizeof(std::string);
807 EXPECT_GE(field.SpaceUsedExcludingSelf(), min_expected_usage);
808 }
809
TEST(RepeatedPtrField,SwapSmallSmall)810 TEST(RepeatedPtrField, SwapSmallSmall) {
811 RepeatedPtrField<std::string> field1;
812 RepeatedPtrField<std::string> field2;
813
814 EXPECT_TRUE(field1.empty());
815 EXPECT_EQ(field1.size(), 0);
816 EXPECT_TRUE(field2.empty());
817 EXPECT_EQ(field2.size(), 0);
818
819 field1.Add()->assign("foo");
820 field1.Add()->assign("bar");
821
822 EXPECT_FALSE(field1.empty());
823 EXPECT_EQ(field1.size(), 2);
824 EXPECT_EQ(field1.Get(0), "foo");
825 EXPECT_EQ(field1.Get(1), "bar");
826
827 EXPECT_TRUE(field2.empty());
828 EXPECT_EQ(field2.size(), 0);
829
830 field1.Swap(&field2);
831
832 EXPECT_TRUE(field1.empty());
833 EXPECT_EQ(field1.size(), 0);
834
835 EXPECT_EQ(field2.size(), 2);
836 EXPECT_EQ(field2.Get(0), "foo");
837 EXPECT_EQ(field2.Get(1), "bar");
838 }
839
TEST(RepeatedPtrField,SwapLargeSmall)840 TEST(RepeatedPtrField, SwapLargeSmall) {
841 RepeatedPtrField<std::string> field1;
842 RepeatedPtrField<std::string> field2;
843
844 field2.Add()->assign("foo");
845 field2.Add()->assign("bar");
846 for (int i = 0; i < 16; i++) {
847 *field1.Add() += 'a' + i;
848 }
849 field1.Swap(&field2);
850
851 EXPECT_EQ(field1.size(), 2);
852 EXPECT_EQ(field1.Get(0), "foo");
853 EXPECT_EQ(field1.Get(1), "bar");
854 EXPECT_EQ(field2.size(), 16);
855 for (int i = 0; i < 16; i++) {
856 EXPECT_EQ(field2.Get(i).size(), 1);
857 EXPECT_EQ(field2.Get(i)[0], 'a' + i);
858 }
859 }
860
TEST(RepeatedPtrField,SwapLargeLarge)861 TEST(RepeatedPtrField, SwapLargeLarge) {
862 RepeatedPtrField<std::string> field1;
863 RepeatedPtrField<std::string> field2;
864
865 field1.Add()->assign("foo");
866 field1.Add()->assign("bar");
867 for (int i = 0; i < 16; i++) {
868 *field1.Add() += 'A' + i;
869 *field2.Add() += 'a' + i;
870 }
871 field2.Swap(&field1);
872
873 EXPECT_EQ(field1.size(), 16);
874 for (int i = 0; i < 16; i++) {
875 EXPECT_EQ(field1.Get(i).size(), 1);
876 EXPECT_EQ(field1.Get(i)[0], 'a' + i);
877 }
878 EXPECT_EQ(field2.size(), 18);
879 EXPECT_EQ(field2.Get(0), "foo");
880 EXPECT_EQ(field2.Get(1), "bar");
881 for (int i = 2; i < 18; i++) {
882 EXPECT_EQ(field2.Get(i).size(), 1);
883 EXPECT_EQ(field2.Get(i)[0], 'A' + i - 2);
884 }
885 }
886
ReservedSpace(RepeatedPtrField<std::string> * field)887 static int ReservedSpace(RepeatedPtrField<std::string>* field) {
888 const std::string* const* ptr = field->data();
889 do {
890 field->Add();
891 } while (field->data() == ptr);
892
893 return field->size() - 1;
894 }
895
TEST(RepeatedPtrField,ReserveMoreThanDouble)896 TEST(RepeatedPtrField, ReserveMoreThanDouble) {
897 RepeatedPtrField<std::string> field;
898 field.Reserve(20);
899
900 EXPECT_LE(20, ReservedSpace(&field));
901 }
902
TEST(RepeatedPtrField,ReserveLessThanDouble)903 TEST(RepeatedPtrField, ReserveLessThanDouble) {
904 RepeatedPtrField<std::string> field;
905 field.Reserve(20);
906
907 int capacity = field.Capacity();
908 // Grow by 1.5x
909 field.Reserve(capacity + (capacity >> 2));
910
911 EXPECT_LE(2 * capacity, ReservedSpace(&field));
912 }
913
TEST(RepeatedPtrField,ReserveLessThanExisting)914 TEST(RepeatedPtrField, ReserveLessThanExisting) {
915 RepeatedPtrField<std::string> field;
916 field.Reserve(20);
917 const std::string* const* previous_ptr = field.data();
918 field.Reserve(10);
919
920 EXPECT_EQ(previous_ptr, field.data());
921 EXPECT_LE(20, ReservedSpace(&field));
922 }
923
TEST(RepeatedPtrField,ReserveDoesntLoseAllocated)924 TEST(RepeatedPtrField, ReserveDoesntLoseAllocated) {
925 // Check that a bug is fixed: An earlier implementation of Reserve()
926 // failed to copy pointers to allocated-but-cleared objects, possibly
927 // leading to segfaults.
928 RepeatedPtrField<std::string> field;
929 std::string* first = field.Add();
930 field.RemoveLast();
931
932 field.Reserve(20);
933 EXPECT_EQ(first, field.Add());
934 }
935
936 // Clearing elements is tricky with RepeatedPtrFields since the memory for
937 // the elements is retained and reused.
TEST(RepeatedPtrField,ClearedElements)938 TEST(RepeatedPtrField, ClearedElements) {
939 RepeatedPtrField<std::string> field;
940
941 std::string* original = field.Add();
942 *original = "foo";
943
944 EXPECT_EQ(field.ClearedCount(), 0);
945
946 field.RemoveLast();
947 EXPECT_TRUE(original->empty());
948 EXPECT_EQ(field.ClearedCount(), 1);
949
950 EXPECT_EQ(field.Add(),
951 original); // Should return same string for reuse.
952
953 EXPECT_EQ(field.ReleaseLast(), original); // We take ownership.
954 EXPECT_EQ(field.ClearedCount(), 0);
955
956 EXPECT_NE(field.Add(), original); // Should NOT return the same string.
957 EXPECT_EQ(field.ClearedCount(), 0);
958
959 field.AddAllocated(original); // Give ownership back.
960 EXPECT_EQ(field.ClearedCount(), 0);
961 EXPECT_EQ(field.Mutable(1), original);
962
963 field.Clear();
964 EXPECT_EQ(field.ClearedCount(), 2);
965 EXPECT_EQ(field.ReleaseCleared(), original); // Take ownership again.
966 EXPECT_EQ(field.ClearedCount(), 1);
967 EXPECT_NE(field.Add(), original);
968 EXPECT_EQ(field.ClearedCount(), 0);
969 EXPECT_NE(field.Add(), original);
970 EXPECT_EQ(field.ClearedCount(), 0);
971
972 field.AddCleared(original); // Give ownership back, but as a cleared object.
973 EXPECT_EQ(field.ClearedCount(), 1);
974 EXPECT_EQ(field.Add(), original);
975 EXPECT_EQ(field.ClearedCount(), 0);
976 }
977
978 // Test all code paths in AddAllocated().
TEST(RepeatedPtrField,AddAlocated)979 TEST(RepeatedPtrField, AddAlocated) {
980 RepeatedPtrField<std::string> field;
981 while (field.size() < field.Capacity()) {
982 field.Add()->assign("filler");
983 }
984
985 int index = field.size();
986
987 // First branch: Field is at capacity with no cleared objects.
988 std::string* foo = new std::string("foo");
989 field.AddAllocated(foo);
990 EXPECT_EQ(index + 1, field.size());
991 EXPECT_EQ(0, field.ClearedCount());
992 EXPECT_EQ(foo, &field.Get(index));
993
994 // Last branch: Field is not at capacity and there are no cleared objects.
995 std::string* bar = new std::string("bar");
996 field.AddAllocated(bar);
997 ++index;
998 EXPECT_EQ(index + 1, field.size());
999 EXPECT_EQ(0, field.ClearedCount());
1000 EXPECT_EQ(bar, &field.Get(index));
1001
1002 // Third branch: Field is not at capacity and there are no cleared objects.
1003 field.RemoveLast();
1004 std::string* baz = new std::string("baz");
1005 field.AddAllocated(baz);
1006 EXPECT_EQ(index + 1, field.size());
1007 EXPECT_EQ(1, field.ClearedCount());
1008 EXPECT_EQ(baz, &field.Get(index));
1009
1010 // Second branch: Field is at capacity but has some cleared objects.
1011 while (field.size() < field.Capacity()) {
1012 field.Add()->assign("filler2");
1013 }
1014 field.RemoveLast();
1015 index = field.size();
1016 std::string* qux = new std::string("qux");
1017 field.AddAllocated(qux);
1018 EXPECT_EQ(index + 1, field.size());
1019 // We should have discarded the cleared object.
1020 EXPECT_EQ(0, field.ClearedCount());
1021 EXPECT_EQ(qux, &field.Get(index));
1022 }
1023
TEST(RepeatedPtrField,MergeFrom)1024 TEST(RepeatedPtrField, MergeFrom) {
1025 RepeatedPtrField<std::string> source, destination;
1026 source.Add()->assign("4");
1027 source.Add()->assign("5");
1028 destination.Add()->assign("1");
1029 destination.Add()->assign("2");
1030 destination.Add()->assign("3");
1031
1032 destination.MergeFrom(source);
1033
1034 ASSERT_EQ(5, destination.size());
1035 EXPECT_EQ("1", destination.Get(0));
1036 EXPECT_EQ("2", destination.Get(1));
1037 EXPECT_EQ("3", destination.Get(2));
1038 EXPECT_EQ("4", destination.Get(3));
1039 EXPECT_EQ("5", destination.Get(4));
1040 }
1041
1042
TEST(RepeatedPtrField,CopyFrom)1043 TEST(RepeatedPtrField, CopyFrom) {
1044 RepeatedPtrField<std::string> source, destination;
1045 source.Add()->assign("4");
1046 source.Add()->assign("5");
1047 destination.Add()->assign("1");
1048 destination.Add()->assign("2");
1049 destination.Add()->assign("3");
1050
1051 destination.CopyFrom(source);
1052
1053 ASSERT_EQ(2, destination.size());
1054 EXPECT_EQ("4", destination.Get(0));
1055 EXPECT_EQ("5", destination.Get(1));
1056 }
1057
TEST(RepeatedPtrField,CopyFromSelf)1058 TEST(RepeatedPtrField, CopyFromSelf) {
1059 RepeatedPtrField<std::string> me;
1060 me.Add()->assign("1");
1061 me.CopyFrom(me);
1062 ASSERT_EQ(1, me.size());
1063 EXPECT_EQ("1", me.Get(0));
1064 }
1065
TEST(RepeatedPtrField,Erase)1066 TEST(RepeatedPtrField, Erase) {
1067 RepeatedPtrField<std::string> me;
1068 RepeatedPtrField<std::string>::iterator it = me.erase(me.begin(), me.end());
1069 EXPECT_TRUE(me.begin() == it);
1070 EXPECT_EQ(0, me.size());
1071
1072 *me.Add() = "1";
1073 *me.Add() = "2";
1074 *me.Add() = "3";
1075 it = me.erase(me.begin(), me.end());
1076 EXPECT_TRUE(me.begin() == it);
1077 EXPECT_EQ(0, me.size());
1078
1079 *me.Add() = "4";
1080 *me.Add() = "5";
1081 *me.Add() = "6";
1082 it = me.erase(me.begin() + 2, me.end());
1083 EXPECT_TRUE(me.begin() + 2 == it);
1084 EXPECT_EQ(2, me.size());
1085 EXPECT_EQ("4", me.Get(0));
1086 EXPECT_EQ("5", me.Get(1));
1087
1088 *me.Add() = "6";
1089 *me.Add() = "7";
1090 *me.Add() = "8";
1091 it = me.erase(me.begin() + 1, me.begin() + 3);
1092 EXPECT_TRUE(me.begin() + 1 == it);
1093 EXPECT_EQ(3, me.size());
1094 EXPECT_EQ("4", me.Get(0));
1095 EXPECT_EQ("7", me.Get(1));
1096 EXPECT_EQ("8", me.Get(2));
1097 }
1098
TEST(RepeatedPtrField,CopyConstruct)1099 TEST(RepeatedPtrField, CopyConstruct) {
1100 RepeatedPtrField<std::string> source;
1101 source.Add()->assign("1");
1102 source.Add()->assign("2");
1103
1104 RepeatedPtrField<std::string> destination(source);
1105
1106 ASSERT_EQ(2, destination.size());
1107 EXPECT_EQ("1", destination.Get(0));
1108 EXPECT_EQ("2", destination.Get(1));
1109 }
1110
TEST(RepeatedPtrField,IteratorConstruct_String)1111 TEST(RepeatedPtrField, IteratorConstruct_String) {
1112 std::vector<std::string> values;
1113 values.push_back("1");
1114 values.push_back("2");
1115
1116 RepeatedPtrField<std::string> field(values.begin(), values.end());
1117 ASSERT_EQ(values.size(), field.size());
1118 EXPECT_EQ(values[0], field.Get(0));
1119 EXPECT_EQ(values[1], field.Get(1));
1120
1121 RepeatedPtrField<std::string> other(field.begin(), field.end());
1122 ASSERT_EQ(values.size(), other.size());
1123 EXPECT_EQ(values[0], other.Get(0));
1124 EXPECT_EQ(values[1], other.Get(1));
1125 }
1126
TEST(RepeatedPtrField,IteratorConstruct_Proto)1127 TEST(RepeatedPtrField, IteratorConstruct_Proto) {
1128 typedef TestAllTypes::NestedMessage Nested;
1129 std::vector<Nested> values;
1130 values.push_back(Nested());
1131 values.back().set_bb(1);
1132 values.push_back(Nested());
1133 values.back().set_bb(2);
1134
1135 RepeatedPtrField<Nested> field(values.begin(), values.end());
1136 ASSERT_EQ(values.size(), field.size());
1137 EXPECT_EQ(values[0].bb(), field.Get(0).bb());
1138 EXPECT_EQ(values[1].bb(), field.Get(1).bb());
1139
1140 RepeatedPtrField<Nested> other(field.begin(), field.end());
1141 ASSERT_EQ(values.size(), other.size());
1142 EXPECT_EQ(values[0].bb(), other.Get(0).bb());
1143 EXPECT_EQ(values[1].bb(), other.Get(1).bb());
1144 }
1145
TEST(RepeatedPtrField,CopyAssign)1146 TEST(RepeatedPtrField, CopyAssign) {
1147 RepeatedPtrField<std::string> source, destination;
1148 source.Add()->assign("4");
1149 source.Add()->assign("5");
1150 destination.Add()->assign("1");
1151 destination.Add()->assign("2");
1152 destination.Add()->assign("3");
1153
1154 destination = source;
1155
1156 ASSERT_EQ(2, destination.size());
1157 EXPECT_EQ("4", destination.Get(0));
1158 EXPECT_EQ("5", destination.Get(1));
1159 }
1160
TEST(RepeatedPtrField,SelfAssign)1161 TEST(RepeatedPtrField, SelfAssign) {
1162 // Verify that assignment to self does not destroy data.
1163 RepeatedPtrField<std::string> source, *p;
1164 p = &source;
1165 source.Add()->assign("7");
1166 source.Add()->assign("8");
1167
1168 *p = source;
1169
1170 ASSERT_EQ(2, source.size());
1171 EXPECT_EQ("7", source.Get(0));
1172 EXPECT_EQ("8", source.Get(1));
1173 }
1174
TEST(RepeatedPtrField,MoveConstruct)1175 TEST(RepeatedPtrField, MoveConstruct) {
1176 {
1177 RepeatedPtrField<std::string> source;
1178 *source.Add() = "1";
1179 *source.Add() = "2";
1180 const std::string* const* data = source.data();
1181 RepeatedPtrField<std::string> destination = std::move(source);
1182 EXPECT_EQ(data, destination.data());
1183 EXPECT_THAT(destination, ElementsAre("1", "2"));
1184 // This property isn't guaranteed but it's useful to have a test that would
1185 // catch changes in this area.
1186 EXPECT_TRUE(source.empty());
1187 }
1188 {
1189 Arena arena;
1190 RepeatedPtrField<std::string>* source =
1191 Arena::CreateMessage<RepeatedPtrField<std::string>>(&arena);
1192 *source->Add() = "1";
1193 *source->Add() = "2";
1194 RepeatedPtrField<std::string> destination = std::move(*source);
1195 EXPECT_EQ(nullptr, destination.GetArena());
1196 EXPECT_THAT(destination, ElementsAre("1", "2"));
1197 // This property isn't guaranteed but it's useful to have a test that would
1198 // catch changes in this area.
1199 EXPECT_THAT(*source, ElementsAre("1", "2"));
1200 }
1201 }
1202
TEST(RepeatedPtrField,MoveAssign)1203 TEST(RepeatedPtrField, MoveAssign) {
1204 {
1205 RepeatedPtrField<std::string> source;
1206 *source.Add() = "1";
1207 *source.Add() = "2";
1208 RepeatedPtrField<std::string> destination;
1209 *destination.Add() = "3";
1210 const std::string* const* source_data = source.data();
1211 const std::string* const* destination_data = destination.data();
1212 destination = std::move(source);
1213 EXPECT_EQ(source_data, destination.data());
1214 EXPECT_THAT(destination, ElementsAre("1", "2"));
1215 // This property isn't guaranteed but it's useful to have a test that would
1216 // catch changes in this area.
1217 EXPECT_EQ(destination_data, source.data());
1218 EXPECT_THAT(source, ElementsAre("3"));
1219 }
1220 {
1221 Arena arena;
1222 RepeatedPtrField<std::string>* source =
1223 Arena::CreateMessage<RepeatedPtrField<std::string>>(&arena);
1224 *source->Add() = "1";
1225 *source->Add() = "2";
1226 RepeatedPtrField<std::string>* destination =
1227 Arena::CreateMessage<RepeatedPtrField<std::string>>(&arena);
1228 *destination->Add() = "3";
1229 const std::string* const* source_data = source->data();
1230 const std::string* const* destination_data = destination->data();
1231 *destination = std::move(*source);
1232 EXPECT_EQ(source_data, destination->data());
1233 EXPECT_THAT(*destination, ElementsAre("1", "2"));
1234 // This property isn't guaranteed but it's useful to have a test that would
1235 // catch changes in this area.
1236 EXPECT_EQ(destination_data, source->data());
1237 EXPECT_THAT(*source, ElementsAre("3"));
1238 }
1239 {
1240 Arena source_arena;
1241 RepeatedPtrField<std::string>* source =
1242 Arena::CreateMessage<RepeatedPtrField<std::string>>(&source_arena);
1243 *source->Add() = "1";
1244 *source->Add() = "2";
1245 Arena destination_arena;
1246 RepeatedPtrField<std::string>* destination =
1247 Arena::CreateMessage<RepeatedPtrField<std::string>>(&destination_arena);
1248 *destination->Add() = "3";
1249 *destination = std::move(*source);
1250 EXPECT_THAT(*destination, ElementsAre("1", "2"));
1251 // This property isn't guaranteed but it's useful to have a test that would
1252 // catch changes in this area.
1253 EXPECT_THAT(*source, ElementsAre("1", "2"));
1254 }
1255 {
1256 Arena arena;
1257 RepeatedPtrField<std::string>* source =
1258 Arena::CreateMessage<RepeatedPtrField<std::string>>(&arena);
1259 *source->Add() = "1";
1260 *source->Add() = "2";
1261 RepeatedPtrField<std::string> destination;
1262 *destination.Add() = "3";
1263 destination = std::move(*source);
1264 EXPECT_THAT(destination, ElementsAre("1", "2"));
1265 // This property isn't guaranteed but it's useful to have a test that would
1266 // catch changes in this area.
1267 EXPECT_THAT(*source, ElementsAre("1", "2"));
1268 }
1269 {
1270 RepeatedPtrField<std::string> source;
1271 *source.Add() = "1";
1272 *source.Add() = "2";
1273 Arena arena;
1274 RepeatedPtrField<std::string>* destination =
1275 Arena::CreateMessage<RepeatedPtrField<std::string>>(&arena);
1276 *destination->Add() = "3";
1277 *destination = std::move(source);
1278 EXPECT_THAT(*destination, ElementsAre("1", "2"));
1279 // This property isn't guaranteed but it's useful to have a test that would
1280 // catch changes in this area.
1281 EXPECT_THAT(source, ElementsAre("1", "2"));
1282 }
1283 {
1284 RepeatedPtrField<std::string> field;
1285 // An alias to defeat -Wself-move.
1286 RepeatedPtrField<std::string>& alias = field;
1287 *field.Add() = "1";
1288 *field.Add() = "2";
1289 const std::string* const* data = field.data();
1290 field = std::move(alias);
1291 EXPECT_EQ(data, field.data());
1292 EXPECT_THAT(field, ElementsAre("1", "2"));
1293 }
1294 {
1295 Arena arena;
1296 RepeatedPtrField<std::string>* field =
1297 Arena::CreateMessage<RepeatedPtrField<std::string>>(&arena);
1298 *field->Add() = "1";
1299 *field->Add() = "2";
1300 const std::string* const* data = field->data();
1301 *field = std::move(*field);
1302 EXPECT_EQ(data, field->data());
1303 EXPECT_THAT(*field, ElementsAre("1", "2"));
1304 }
1305 }
1306
TEST(RepeatedPtrField,MutableDataIsMutable)1307 TEST(RepeatedPtrField, MutableDataIsMutable) {
1308 RepeatedPtrField<std::string> field;
1309 *field.Add() = "1";
1310 EXPECT_EQ("1", field.Get(0));
1311 // The fact that this line compiles would be enough, but we'll check the
1312 // value anyway.
1313 std::string** data = field.mutable_data();
1314 **data = "2";
1315 EXPECT_EQ("2", field.Get(0));
1316 }
1317
TEST(RepeatedPtrField,SubscriptOperators)1318 TEST(RepeatedPtrField, SubscriptOperators) {
1319 RepeatedPtrField<std::string> field;
1320 *field.Add() = "1";
1321 EXPECT_EQ("1", field.Get(0));
1322 EXPECT_EQ("1", field[0]);
1323 EXPECT_EQ(field.Mutable(0), &field[0]);
1324 const RepeatedPtrField<std::string>& const_field = field;
1325 EXPECT_EQ(*field.data(), &const_field[0]);
1326 }
1327
TEST(RepeatedPtrField,ExtractSubrange)1328 TEST(RepeatedPtrField, ExtractSubrange) {
1329 // Exhaustively test every subrange in arrays of all sizes from 0 through 9
1330 // with 0 through 3 cleared elements at the end.
1331 for (int sz = 0; sz < 10; ++sz) {
1332 for (int num = 0; num <= sz; ++num) {
1333 for (int start = 0; start < sz - num; ++start) {
1334 for (int extra = 0; extra < 4; ++extra) {
1335 std::vector<std::string*> subject;
1336
1337 // Create an array with "sz" elements and "extra" cleared elements.
1338 RepeatedPtrField<std::string> field;
1339 for (int i = 0; i < sz + extra; ++i) {
1340 subject.push_back(new std::string());
1341 field.AddAllocated(subject[i]);
1342 }
1343 EXPECT_EQ(field.size(), sz + extra);
1344 for (int i = 0; i < extra; ++i) field.RemoveLast();
1345 EXPECT_EQ(field.size(), sz);
1346 EXPECT_EQ(field.ClearedCount(), extra);
1347
1348 // Create a catcher array and call ExtractSubrange.
1349 std::string* catcher[10];
1350 for (int i = 0; i < 10; ++i) catcher[i] = NULL;
1351 field.ExtractSubrange(start, num, catcher);
1352
1353 // Does the resulting array have the right size?
1354 EXPECT_EQ(field.size(), sz - num);
1355
1356 // Were the removed elements extracted into the catcher array?
1357 for (int i = 0; i < num; ++i)
1358 EXPECT_EQ(catcher[i], subject[start + i]);
1359 EXPECT_EQ(NULL, catcher[num]);
1360
1361 // Does the resulting array contain the right values?
1362 for (int i = 0; i < start; ++i)
1363 EXPECT_EQ(field.Mutable(i), subject[i]);
1364 for (int i = start; i < field.size(); ++i)
1365 EXPECT_EQ(field.Mutable(i), subject[i + num]);
1366
1367 // Reinstate the cleared elements.
1368 EXPECT_EQ(field.ClearedCount(), extra);
1369 for (int i = 0; i < extra; ++i) field.Add();
1370 EXPECT_EQ(field.ClearedCount(), 0);
1371 EXPECT_EQ(field.size(), sz - num + extra);
1372
1373 // Make sure the extra elements are all there (in some order).
1374 for (int i = sz; i < sz + extra; ++i) {
1375 int count = 0;
1376 for (int j = sz; j < sz + extra; ++j) {
1377 if (field.Mutable(j - num) == subject[i]) count += 1;
1378 }
1379 EXPECT_EQ(count, 1);
1380 }
1381
1382 // Release the caught elements.
1383 for (int i = 0; i < num; ++i) delete catcher[i];
1384 }
1385 }
1386 }
1387 }
1388 }
1389
TEST(RepeatedPtrField,DeleteSubrange)1390 TEST(RepeatedPtrField, DeleteSubrange) {
1391 // DeleteSubrange is a trivial extension of ExtendSubrange.
1392 }
1393
1394 // ===================================================================
1395
1396 // Iterator tests stolen from net/proto/proto-array_unittest.
1397 class RepeatedFieldIteratorTest : public testing::Test {
1398 protected:
SetUp()1399 virtual void SetUp() {
1400 for (int i = 0; i < 3; ++i) {
1401 proto_array_.Add(i);
1402 }
1403 }
1404
1405 RepeatedField<int> proto_array_;
1406 };
1407
TEST_F(RepeatedFieldIteratorTest,Convertible)1408 TEST_F(RepeatedFieldIteratorTest, Convertible) {
1409 RepeatedField<int>::iterator iter = proto_array_.begin();
1410 RepeatedField<int>::const_iterator c_iter = iter;
1411 RepeatedField<int>::value_type value = *c_iter;
1412 EXPECT_EQ(0, value);
1413 }
1414
TEST_F(RepeatedFieldIteratorTest,MutableIteration)1415 TEST_F(RepeatedFieldIteratorTest, MutableIteration) {
1416 RepeatedField<int>::iterator iter = proto_array_.begin();
1417 EXPECT_EQ(0, *iter);
1418 ++iter;
1419 EXPECT_EQ(1, *iter++);
1420 EXPECT_EQ(2, *iter);
1421 ++iter;
1422 EXPECT_TRUE(proto_array_.end() == iter);
1423
1424 EXPECT_EQ(2, *(proto_array_.end() - 1));
1425 }
1426
TEST_F(RepeatedFieldIteratorTest,ConstIteration)1427 TEST_F(RepeatedFieldIteratorTest, ConstIteration) {
1428 const RepeatedField<int>& const_proto_array = proto_array_;
1429 RepeatedField<int>::const_iterator iter = const_proto_array.begin();
1430 EXPECT_EQ(0, *iter);
1431 ++iter;
1432 EXPECT_EQ(1, *iter++);
1433 EXPECT_EQ(2, *iter);
1434 ++iter;
1435 EXPECT_TRUE(proto_array_.end() == iter);
1436 EXPECT_EQ(2, *(proto_array_.end() - 1));
1437 }
1438
TEST_F(RepeatedFieldIteratorTest,Mutation)1439 TEST_F(RepeatedFieldIteratorTest, Mutation) {
1440 RepeatedField<int>::iterator iter = proto_array_.begin();
1441 *iter = 7;
1442 EXPECT_EQ(7, proto_array_.Get(0));
1443 }
1444
1445 // -------------------------------------------------------------------
1446
1447 class RepeatedPtrFieldIteratorTest : public testing::Test {
1448 protected:
SetUp()1449 virtual void SetUp() {
1450 proto_array_.Add()->assign("foo");
1451 proto_array_.Add()->assign("bar");
1452 proto_array_.Add()->assign("baz");
1453 }
1454
1455 RepeatedPtrField<std::string> proto_array_;
1456 };
1457
TEST_F(RepeatedPtrFieldIteratorTest,Convertible)1458 TEST_F(RepeatedPtrFieldIteratorTest, Convertible) {
1459 RepeatedPtrField<std::string>::iterator iter = proto_array_.begin();
1460 RepeatedPtrField<std::string>::const_iterator c_iter = iter;
1461 RepeatedPtrField<std::string>::value_type value = *c_iter;
1462 EXPECT_EQ("foo", value);
1463 }
1464
TEST_F(RepeatedPtrFieldIteratorTest,MutableIteration)1465 TEST_F(RepeatedPtrFieldIteratorTest, MutableIteration) {
1466 RepeatedPtrField<std::string>::iterator iter = proto_array_.begin();
1467 EXPECT_EQ("foo", *iter);
1468 ++iter;
1469 EXPECT_EQ("bar", *(iter++));
1470 EXPECT_EQ("baz", *iter);
1471 ++iter;
1472 EXPECT_TRUE(proto_array_.end() == iter);
1473 EXPECT_EQ("baz", *(--proto_array_.end()));
1474 }
1475
TEST_F(RepeatedPtrFieldIteratorTest,ConstIteration)1476 TEST_F(RepeatedPtrFieldIteratorTest, ConstIteration) {
1477 const RepeatedPtrField<std::string>& const_proto_array = proto_array_;
1478 RepeatedPtrField<std::string>::const_iterator iter =
1479 const_proto_array.begin();
1480 EXPECT_EQ("foo", *iter);
1481 ++iter;
1482 EXPECT_EQ("bar", *(iter++));
1483 EXPECT_EQ("baz", *iter);
1484 ++iter;
1485 EXPECT_TRUE(const_proto_array.end() == iter);
1486 EXPECT_EQ("baz", *(--const_proto_array.end()));
1487 }
1488
TEST_F(RepeatedPtrFieldIteratorTest,MutableReverseIteration)1489 TEST_F(RepeatedPtrFieldIteratorTest, MutableReverseIteration) {
1490 RepeatedPtrField<std::string>::reverse_iterator iter = proto_array_.rbegin();
1491 EXPECT_EQ("baz", *iter);
1492 ++iter;
1493 EXPECT_EQ("bar", *(iter++));
1494 EXPECT_EQ("foo", *iter);
1495 ++iter;
1496 EXPECT_TRUE(proto_array_.rend() == iter);
1497 EXPECT_EQ("foo", *(--proto_array_.rend()));
1498 }
1499
TEST_F(RepeatedPtrFieldIteratorTest,ConstReverseIteration)1500 TEST_F(RepeatedPtrFieldIteratorTest, ConstReverseIteration) {
1501 const RepeatedPtrField<std::string>& const_proto_array = proto_array_;
1502 RepeatedPtrField<std::string>::const_reverse_iterator iter =
1503 const_proto_array.rbegin();
1504 EXPECT_EQ("baz", *iter);
1505 ++iter;
1506 EXPECT_EQ("bar", *(iter++));
1507 EXPECT_EQ("foo", *iter);
1508 ++iter;
1509 EXPECT_TRUE(const_proto_array.rend() == iter);
1510 EXPECT_EQ("foo", *(--const_proto_array.rend()));
1511 }
1512
TEST_F(RepeatedPtrFieldIteratorTest,RandomAccess)1513 TEST_F(RepeatedPtrFieldIteratorTest, RandomAccess) {
1514 RepeatedPtrField<std::string>::iterator iter = proto_array_.begin();
1515 RepeatedPtrField<std::string>::iterator iter2 = iter;
1516 ++iter2;
1517 ++iter2;
1518 EXPECT_TRUE(iter + 2 == iter2);
1519 EXPECT_TRUE(iter == iter2 - 2);
1520 EXPECT_EQ("baz", iter[2]);
1521 EXPECT_EQ("baz", *(iter + 2));
1522 EXPECT_EQ(3, proto_array_.end() - proto_array_.begin());
1523 }
1524
TEST_F(RepeatedPtrFieldIteratorTest,Comparable)1525 TEST_F(RepeatedPtrFieldIteratorTest, Comparable) {
1526 RepeatedPtrField<std::string>::const_iterator iter = proto_array_.begin();
1527 RepeatedPtrField<std::string>::const_iterator iter2 = iter + 1;
1528 EXPECT_TRUE(iter == iter);
1529 EXPECT_TRUE(iter != iter2);
1530 EXPECT_TRUE(iter < iter2);
1531 EXPECT_TRUE(iter <= iter2);
1532 EXPECT_TRUE(iter <= iter);
1533 EXPECT_TRUE(iter2 > iter);
1534 EXPECT_TRUE(iter2 >= iter);
1535 EXPECT_TRUE(iter >= iter);
1536 }
1537
1538 // Uninitialized iterator does not point to any of the RepeatedPtrField.
TEST_F(RepeatedPtrFieldIteratorTest,UninitializedIterator)1539 TEST_F(RepeatedPtrFieldIteratorTest, UninitializedIterator) {
1540 RepeatedPtrField<std::string>::iterator iter;
1541 EXPECT_TRUE(iter != proto_array_.begin());
1542 EXPECT_TRUE(iter != proto_array_.begin() + 1);
1543 EXPECT_TRUE(iter != proto_array_.begin() + 2);
1544 EXPECT_TRUE(iter != proto_array_.begin() + 3);
1545 EXPECT_TRUE(iter != proto_array_.end());
1546 }
1547
TEST_F(RepeatedPtrFieldIteratorTest,STLAlgorithms_lower_bound)1548 TEST_F(RepeatedPtrFieldIteratorTest, STLAlgorithms_lower_bound) {
1549 proto_array_.Clear();
1550 proto_array_.Add()->assign("a");
1551 proto_array_.Add()->assign("c");
1552 proto_array_.Add()->assign("d");
1553 proto_array_.Add()->assign("n");
1554 proto_array_.Add()->assign("p");
1555 proto_array_.Add()->assign("x");
1556 proto_array_.Add()->assign("y");
1557
1558 std::string v = "f";
1559 RepeatedPtrField<std::string>::const_iterator it =
1560 std::lower_bound(proto_array_.begin(), proto_array_.end(), v);
1561
1562 EXPECT_EQ(*it, "n");
1563 EXPECT_TRUE(it == proto_array_.begin() + 3);
1564 }
1565
TEST_F(RepeatedPtrFieldIteratorTest,Mutation)1566 TEST_F(RepeatedPtrFieldIteratorTest, Mutation) {
1567 RepeatedPtrField<std::string>::iterator iter = proto_array_.begin();
1568 *iter = "qux";
1569 EXPECT_EQ("qux", proto_array_.Get(0));
1570 }
1571
1572 // -------------------------------------------------------------------
1573
1574 class RepeatedPtrFieldPtrsIteratorTest : public testing::Test {
1575 protected:
SetUp()1576 virtual void SetUp() {
1577 proto_array_.Add()->assign("foo");
1578 proto_array_.Add()->assign("bar");
1579 proto_array_.Add()->assign("baz");
1580 const_proto_array_ = &proto_array_;
1581 }
1582
1583 RepeatedPtrField<std::string> proto_array_;
1584 const RepeatedPtrField<std::string>* const_proto_array_;
1585 };
1586
TEST_F(RepeatedPtrFieldPtrsIteratorTest,ConvertiblePtr)1587 TEST_F(RepeatedPtrFieldPtrsIteratorTest, ConvertiblePtr) {
1588 RepeatedPtrField<std::string>::pointer_iterator iter =
1589 proto_array_.pointer_begin();
1590 static_cast<void>(iter);
1591 }
1592
TEST_F(RepeatedPtrFieldPtrsIteratorTest,ConvertibleConstPtr)1593 TEST_F(RepeatedPtrFieldPtrsIteratorTest, ConvertibleConstPtr) {
1594 RepeatedPtrField<std::string>::const_pointer_iterator iter =
1595 const_proto_array_->pointer_begin();
1596 static_cast<void>(iter);
1597 }
1598
TEST_F(RepeatedPtrFieldPtrsIteratorTest,MutablePtrIteration)1599 TEST_F(RepeatedPtrFieldPtrsIteratorTest, MutablePtrIteration) {
1600 RepeatedPtrField<std::string>::pointer_iterator iter =
1601 proto_array_.pointer_begin();
1602 EXPECT_EQ("foo", **iter);
1603 ++iter;
1604 EXPECT_EQ("bar", **(iter++));
1605 EXPECT_EQ("baz", **iter);
1606 ++iter;
1607 EXPECT_TRUE(proto_array_.pointer_end() == iter);
1608 EXPECT_EQ("baz", **(--proto_array_.pointer_end()));
1609 }
1610
TEST_F(RepeatedPtrFieldPtrsIteratorTest,MutableConstPtrIteration)1611 TEST_F(RepeatedPtrFieldPtrsIteratorTest, MutableConstPtrIteration) {
1612 RepeatedPtrField<std::string>::const_pointer_iterator iter =
1613 const_proto_array_->pointer_begin();
1614 EXPECT_EQ("foo", **iter);
1615 ++iter;
1616 EXPECT_EQ("bar", **(iter++));
1617 EXPECT_EQ("baz", **iter);
1618 ++iter;
1619 EXPECT_TRUE(const_proto_array_->pointer_end() == iter);
1620 EXPECT_EQ("baz", **(--const_proto_array_->pointer_end()));
1621 }
1622
TEST_F(RepeatedPtrFieldPtrsIteratorTest,RandomPtrAccess)1623 TEST_F(RepeatedPtrFieldPtrsIteratorTest, RandomPtrAccess) {
1624 RepeatedPtrField<std::string>::pointer_iterator iter =
1625 proto_array_.pointer_begin();
1626 RepeatedPtrField<std::string>::pointer_iterator iter2 = iter;
1627 ++iter2;
1628 ++iter2;
1629 EXPECT_TRUE(iter + 2 == iter2);
1630 EXPECT_TRUE(iter == iter2 - 2);
1631 EXPECT_EQ("baz", *iter[2]);
1632 EXPECT_EQ("baz", **(iter + 2));
1633 EXPECT_EQ(3, proto_array_.end() - proto_array_.begin());
1634 }
1635
TEST_F(RepeatedPtrFieldPtrsIteratorTest,RandomConstPtrAccess)1636 TEST_F(RepeatedPtrFieldPtrsIteratorTest, RandomConstPtrAccess) {
1637 RepeatedPtrField<std::string>::const_pointer_iterator iter =
1638 const_proto_array_->pointer_begin();
1639 RepeatedPtrField<std::string>::const_pointer_iterator iter2 = iter;
1640 ++iter2;
1641 ++iter2;
1642 EXPECT_TRUE(iter + 2 == iter2);
1643 EXPECT_TRUE(iter == iter2 - 2);
1644 EXPECT_EQ("baz", *iter[2]);
1645 EXPECT_EQ("baz", **(iter + 2));
1646 EXPECT_EQ(3, const_proto_array_->end() - const_proto_array_->begin());
1647 }
1648
TEST_F(RepeatedPtrFieldPtrsIteratorTest,ComparablePtr)1649 TEST_F(RepeatedPtrFieldPtrsIteratorTest, ComparablePtr) {
1650 RepeatedPtrField<std::string>::pointer_iterator iter =
1651 proto_array_.pointer_begin();
1652 RepeatedPtrField<std::string>::pointer_iterator iter2 = iter + 1;
1653 EXPECT_TRUE(iter == iter);
1654 EXPECT_TRUE(iter != iter2);
1655 EXPECT_TRUE(iter < iter2);
1656 EXPECT_TRUE(iter <= iter2);
1657 EXPECT_TRUE(iter <= iter);
1658 EXPECT_TRUE(iter2 > iter);
1659 EXPECT_TRUE(iter2 >= iter);
1660 EXPECT_TRUE(iter >= iter);
1661 }
1662
TEST_F(RepeatedPtrFieldPtrsIteratorTest,ComparableConstPtr)1663 TEST_F(RepeatedPtrFieldPtrsIteratorTest, ComparableConstPtr) {
1664 RepeatedPtrField<std::string>::const_pointer_iterator iter =
1665 const_proto_array_->pointer_begin();
1666 RepeatedPtrField<std::string>::const_pointer_iterator iter2 = iter + 1;
1667 EXPECT_TRUE(iter == iter);
1668 EXPECT_TRUE(iter != iter2);
1669 EXPECT_TRUE(iter < iter2);
1670 EXPECT_TRUE(iter <= iter2);
1671 EXPECT_TRUE(iter <= iter);
1672 EXPECT_TRUE(iter2 > iter);
1673 EXPECT_TRUE(iter2 >= iter);
1674 EXPECT_TRUE(iter >= iter);
1675 }
1676
1677 // Uninitialized iterator does not point to any of the RepeatedPtrOverPtrs.
1678 // Dereferencing an uninitialized iterator crashes the process.
TEST_F(RepeatedPtrFieldPtrsIteratorTest,UninitializedPtrIterator)1679 TEST_F(RepeatedPtrFieldPtrsIteratorTest, UninitializedPtrIterator) {
1680 RepeatedPtrField<std::string>::pointer_iterator iter;
1681 EXPECT_TRUE(iter != proto_array_.pointer_begin());
1682 EXPECT_TRUE(iter != proto_array_.pointer_begin() + 1);
1683 EXPECT_TRUE(iter != proto_array_.pointer_begin() + 2);
1684 EXPECT_TRUE(iter != proto_array_.pointer_begin() + 3);
1685 EXPECT_TRUE(iter != proto_array_.pointer_end());
1686 }
1687
TEST_F(RepeatedPtrFieldPtrsIteratorTest,UninitializedConstPtrIterator)1688 TEST_F(RepeatedPtrFieldPtrsIteratorTest, UninitializedConstPtrIterator) {
1689 RepeatedPtrField<std::string>::const_pointer_iterator iter;
1690 EXPECT_TRUE(iter != const_proto_array_->pointer_begin());
1691 EXPECT_TRUE(iter != const_proto_array_->pointer_begin() + 1);
1692 EXPECT_TRUE(iter != const_proto_array_->pointer_begin() + 2);
1693 EXPECT_TRUE(iter != const_proto_array_->pointer_begin() + 3);
1694 EXPECT_TRUE(iter != const_proto_array_->pointer_end());
1695 }
1696
1697 // This comparison functor is required by the tests for RepeatedPtrOverPtrs.
1698 // They operate on strings and need to compare strings as strings in
1699 // any stl algorithm, even though the iterator returns a pointer to a
1700 // string
1701 // - i.e. *iter has type std::string*.
1702 struct StringLessThan {
operator ()google::protobuf::__anon462f493b0111::StringLessThan1703 bool operator()(const std::string* z, const std::string* y) const {
1704 return *z < *y;
1705 }
1706 };
1707
TEST_F(RepeatedPtrFieldPtrsIteratorTest,PtrSTLAlgorithms_lower_bound)1708 TEST_F(RepeatedPtrFieldPtrsIteratorTest, PtrSTLAlgorithms_lower_bound) {
1709 proto_array_.Clear();
1710 proto_array_.Add()->assign("a");
1711 proto_array_.Add()->assign("c");
1712 proto_array_.Add()->assign("d");
1713 proto_array_.Add()->assign("n");
1714 proto_array_.Add()->assign("p");
1715 proto_array_.Add()->assign("x");
1716 proto_array_.Add()->assign("y");
1717
1718 {
1719 std::string v = "f";
1720 RepeatedPtrField<std::string>::pointer_iterator it =
1721 std::lower_bound(proto_array_.pointer_begin(),
1722 proto_array_.pointer_end(), &v, StringLessThan());
1723
1724 GOOGLE_CHECK(*it != NULL);
1725
1726 EXPECT_EQ(**it, "n");
1727 EXPECT_TRUE(it == proto_array_.pointer_begin() + 3);
1728 }
1729 {
1730 std::string v = "f";
1731 RepeatedPtrField<std::string>::const_pointer_iterator it = std::lower_bound(
1732 const_proto_array_->pointer_begin(), const_proto_array_->pointer_end(),
1733 &v, StringLessThan());
1734
1735 GOOGLE_CHECK(*it != NULL);
1736
1737 EXPECT_EQ(**it, "n");
1738 EXPECT_TRUE(it == const_proto_array_->pointer_begin() + 3);
1739 }
1740 }
1741
TEST_F(RepeatedPtrFieldPtrsIteratorTest,PtrMutation)1742 TEST_F(RepeatedPtrFieldPtrsIteratorTest, PtrMutation) {
1743 RepeatedPtrField<std::string>::pointer_iterator iter =
1744 proto_array_.pointer_begin();
1745 **iter = "qux";
1746 EXPECT_EQ("qux", proto_array_.Get(0));
1747
1748 EXPECT_EQ("bar", proto_array_.Get(1));
1749 EXPECT_EQ("baz", proto_array_.Get(2));
1750 ++iter;
1751 delete *iter;
1752 *iter = new std::string("a");
1753 ++iter;
1754 delete *iter;
1755 *iter = new std::string("b");
1756 EXPECT_EQ("a", proto_array_.Get(1));
1757 EXPECT_EQ("b", proto_array_.Get(2));
1758 }
1759
TEST_F(RepeatedPtrFieldPtrsIteratorTest,Sort)1760 TEST_F(RepeatedPtrFieldPtrsIteratorTest, Sort) {
1761 proto_array_.Add()->assign("c");
1762 proto_array_.Add()->assign("d");
1763 proto_array_.Add()->assign("n");
1764 proto_array_.Add()->assign("p");
1765 proto_array_.Add()->assign("a");
1766 proto_array_.Add()->assign("y");
1767 proto_array_.Add()->assign("x");
1768 EXPECT_EQ("foo", proto_array_.Get(0));
1769 EXPECT_EQ("n", proto_array_.Get(5));
1770 EXPECT_EQ("x", proto_array_.Get(9));
1771 std::sort(proto_array_.pointer_begin(), proto_array_.pointer_end(),
1772 StringLessThan());
1773 EXPECT_EQ("a", proto_array_.Get(0));
1774 EXPECT_EQ("baz", proto_array_.Get(2));
1775 EXPECT_EQ("y", proto_array_.Get(9));
1776 }
1777
1778 // -----------------------------------------------------------------------------
1779 // Unit-tests for the insert iterators
1780 // google::protobuf::RepeatedFieldBackInserter,
1781 // google::protobuf::AllocatedRepeatedPtrFieldBackInserter
1782 // Ported from util/gtl/proto-array-iterators_unittest.
1783
1784 class RepeatedFieldInsertionIteratorsTest : public testing::Test {
1785 protected:
1786 std::list<double> halves;
1787 std::list<int> fibonacci;
1788 std::vector<std::string> words;
1789 typedef TestAllTypes::NestedMessage Nested;
1790 Nested nesteds[2];
1791 std::vector<Nested*> nested_ptrs;
1792 TestAllTypes protobuffer;
1793
SetUp()1794 virtual void SetUp() {
1795 fibonacci.push_back(1);
1796 fibonacci.push_back(1);
1797 fibonacci.push_back(2);
1798 fibonacci.push_back(3);
1799 fibonacci.push_back(5);
1800 fibonacci.push_back(8);
1801 std::copy(fibonacci.begin(), fibonacci.end(),
1802 RepeatedFieldBackInserter(protobuffer.mutable_repeated_int32()));
1803
1804 halves.push_back(1.0);
1805 halves.push_back(0.5);
1806 halves.push_back(0.25);
1807 halves.push_back(0.125);
1808 halves.push_back(0.0625);
1809 std::copy(halves.begin(), halves.end(),
1810 RepeatedFieldBackInserter(protobuffer.mutable_repeated_double()));
1811
1812 words.push_back("Able");
1813 words.push_back("was");
1814 words.push_back("I");
1815 words.push_back("ere");
1816 words.push_back("I");
1817 words.push_back("saw");
1818 words.push_back("Elba");
1819 std::copy(words.begin(), words.end(),
1820 RepeatedFieldBackInserter(protobuffer.mutable_repeated_string()));
1821
1822 nesteds[0].set_bb(17);
1823 nesteds[1].set_bb(4711);
1824 std::copy(&nesteds[0], &nesteds[2],
1825 RepeatedFieldBackInserter(
1826 protobuffer.mutable_repeated_nested_message()));
1827
1828 nested_ptrs.push_back(new Nested);
1829 nested_ptrs.back()->set_bb(170);
1830 nested_ptrs.push_back(new Nested);
1831 nested_ptrs.back()->set_bb(47110);
1832 std::copy(nested_ptrs.begin(), nested_ptrs.end(),
1833 RepeatedFieldBackInserter(
1834 protobuffer.mutable_repeated_nested_message()));
1835 }
1836
TearDown()1837 virtual void TearDown() {
1838 STLDeleteContainerPointers(nested_ptrs.begin(), nested_ptrs.end());
1839 }
1840 };
1841
TEST_F(RepeatedFieldInsertionIteratorsTest,Fibonacci)1842 TEST_F(RepeatedFieldInsertionIteratorsTest, Fibonacci) {
1843 EXPECT_TRUE(std::equal(fibonacci.begin(), fibonacci.end(),
1844 protobuffer.repeated_int32().begin()));
1845 EXPECT_TRUE(std::equal(protobuffer.repeated_int32().begin(),
1846 protobuffer.repeated_int32().end(),
1847 fibonacci.begin()));
1848 }
1849
TEST_F(RepeatedFieldInsertionIteratorsTest,Halves)1850 TEST_F(RepeatedFieldInsertionIteratorsTest, Halves) {
1851 EXPECT_TRUE(std::equal(halves.begin(), halves.end(),
1852 protobuffer.repeated_double().begin()));
1853 EXPECT_TRUE(std::equal(protobuffer.repeated_double().begin(),
1854 protobuffer.repeated_double().end(), halves.begin()));
1855 }
1856
TEST_F(RepeatedFieldInsertionIteratorsTest,Words)1857 TEST_F(RepeatedFieldInsertionIteratorsTest, Words) {
1858 ASSERT_EQ(words.size(), protobuffer.repeated_string_size());
1859 for (int i = 0; i < words.size(); ++i)
1860 EXPECT_EQ(words.at(i), protobuffer.repeated_string(i));
1861 }
1862
TEST_F(RepeatedFieldInsertionIteratorsTest,Words2)1863 TEST_F(RepeatedFieldInsertionIteratorsTest, Words2) {
1864 words.clear();
1865 words.push_back("sing");
1866 words.push_back("a");
1867 words.push_back("song");
1868 words.push_back("of");
1869 words.push_back("six");
1870 words.push_back("pence");
1871 protobuffer.mutable_repeated_string()->Clear();
1872 std::copy(
1873 words.begin(), words.end(),
1874 RepeatedPtrFieldBackInserter(protobuffer.mutable_repeated_string()));
1875 ASSERT_EQ(words.size(), protobuffer.repeated_string_size());
1876 for (int i = 0; i < words.size(); ++i)
1877 EXPECT_EQ(words.at(i), protobuffer.repeated_string(i));
1878 }
1879
TEST_F(RepeatedFieldInsertionIteratorsTest,Nesteds)1880 TEST_F(RepeatedFieldInsertionIteratorsTest, Nesteds) {
1881 ASSERT_EQ(protobuffer.repeated_nested_message_size(), 4);
1882 EXPECT_EQ(protobuffer.repeated_nested_message(0).bb(), 17);
1883 EXPECT_EQ(protobuffer.repeated_nested_message(1).bb(), 4711);
1884 EXPECT_EQ(protobuffer.repeated_nested_message(2).bb(), 170);
1885 EXPECT_EQ(protobuffer.repeated_nested_message(3).bb(), 47110);
1886 }
1887
TEST_F(RepeatedFieldInsertionIteratorsTest,AllocatedRepeatedPtrFieldWithStringIntData)1888 TEST_F(RepeatedFieldInsertionIteratorsTest,
1889 AllocatedRepeatedPtrFieldWithStringIntData) {
1890 std::vector<Nested*> data;
1891 TestAllTypes goldenproto;
1892 for (int i = 0; i < 10; ++i) {
1893 Nested* new_data = new Nested;
1894 new_data->set_bb(i);
1895 data.push_back(new_data);
1896
1897 new_data = goldenproto.add_repeated_nested_message();
1898 new_data->set_bb(i);
1899 }
1900 TestAllTypes testproto;
1901 std::copy(data.begin(), data.end(),
1902 AllocatedRepeatedPtrFieldBackInserter(
1903 testproto.mutable_repeated_nested_message()));
1904 EXPECT_EQ(testproto.DebugString(), goldenproto.DebugString());
1905 }
1906
TEST_F(RepeatedFieldInsertionIteratorsTest,AllocatedRepeatedPtrFieldWithString)1907 TEST_F(RepeatedFieldInsertionIteratorsTest,
1908 AllocatedRepeatedPtrFieldWithString) {
1909 std::vector<std::string*> data;
1910 TestAllTypes goldenproto;
1911 for (int i = 0; i < 10; ++i) {
1912 std::string* new_data = new std::string;
1913 *new_data = "name-" + StrCat(i);
1914 data.push_back(new_data);
1915
1916 new_data = goldenproto.add_repeated_string();
1917 *new_data = "name-" + StrCat(i);
1918 }
1919 TestAllTypes testproto;
1920 std::copy(data.begin(), data.end(),
1921 AllocatedRepeatedPtrFieldBackInserter(
1922 testproto.mutable_repeated_string()));
1923 EXPECT_EQ(testproto.DebugString(), goldenproto.DebugString());
1924 }
1925
TEST_F(RepeatedFieldInsertionIteratorsTest,UnsafeArenaAllocatedRepeatedPtrFieldWithStringIntData)1926 TEST_F(RepeatedFieldInsertionIteratorsTest,
1927 UnsafeArenaAllocatedRepeatedPtrFieldWithStringIntData) {
1928 std::vector<Nested*> data;
1929 TestAllTypes goldenproto;
1930 for (int i = 0; i < 10; ++i) {
1931 Nested* new_data = new Nested;
1932 new_data->set_bb(i);
1933 data.push_back(new_data);
1934
1935 new_data = goldenproto.add_repeated_nested_message();
1936 new_data->set_bb(i);
1937 }
1938 TestAllTypes testproto;
1939 std::copy(data.begin(), data.end(),
1940 UnsafeArenaAllocatedRepeatedPtrFieldBackInserter(
1941 testproto.mutable_repeated_nested_message()));
1942 EXPECT_EQ(testproto.DebugString(), goldenproto.DebugString());
1943 }
1944
TEST_F(RepeatedFieldInsertionIteratorsTest,UnsafeArenaAllocatedRepeatedPtrFieldWithString)1945 TEST_F(RepeatedFieldInsertionIteratorsTest,
1946 UnsafeArenaAllocatedRepeatedPtrFieldWithString) {
1947 std::vector<std::string*> data;
1948 TestAllTypes goldenproto;
1949 for (int i = 0; i < 10; ++i) {
1950 std::string* new_data = new std::string;
1951 *new_data = "name-" + StrCat(i);
1952 data.push_back(new_data);
1953
1954 new_data = goldenproto.add_repeated_string();
1955 *new_data = "name-" + StrCat(i);
1956 }
1957 TestAllTypes testproto;
1958 std::copy(data.begin(), data.end(),
1959 UnsafeArenaAllocatedRepeatedPtrFieldBackInserter(
1960 testproto.mutable_repeated_string()));
1961 EXPECT_EQ(testproto.DebugString(), goldenproto.DebugString());
1962 }
1963
TEST_F(RepeatedFieldInsertionIteratorsTest,MoveStrings)1964 TEST_F(RepeatedFieldInsertionIteratorsTest, MoveStrings) {
1965 std::vector<std::string> src = {"a", "b", "c", "d"};
1966 std::vector<std::string> copy =
1967 src; // copy since move leaves in undefined state
1968 TestAllTypes testproto;
1969 std::move(copy.begin(), copy.end(),
1970 RepeatedFieldBackInserter(testproto.mutable_repeated_string()));
1971
1972 ASSERT_THAT(testproto.repeated_string(), testing::ElementsAreArray(src));
1973 }
1974
TEST_F(RepeatedFieldInsertionIteratorsTest,MoveProtos)1975 TEST_F(RepeatedFieldInsertionIteratorsTest, MoveProtos) {
1976 auto make_nested = [](int32 x) {
1977 Nested ret;
1978 ret.set_bb(x);
1979 return ret;
1980 };
1981 std::vector<Nested> src = {make_nested(3), make_nested(5), make_nested(7)};
1982 std::vector<Nested> copy = src; // copy since move leaves in undefined state
1983 TestAllTypes testproto;
1984 std::move(
1985 copy.begin(), copy.end(),
1986 RepeatedFieldBackInserter(testproto.mutable_repeated_nested_message()));
1987
1988 ASSERT_EQ(src.size(), testproto.repeated_nested_message_size());
1989 for (int i = 0; i < src.size(); ++i) {
1990 EXPECT_EQ(src[i].DebugString(),
1991 testproto.repeated_nested_message(i).DebugString());
1992 }
1993 }
1994
1995 } // namespace
1996
1997 } // namespace protobuf
1998 } // namespace google
1999