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