38 #include <google/protobuf/repeated_field.h>
46 #include <type_traits>
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>
59 #include <google/protobuf/port_def.inc>
65 using ::protobuf_unittest::TestAllTypes;
126 int expected_usage = 4 *
sizeof(
int) +
sizeof(
Arena*);
136 for (
int i = 0;
i < 16;
i++) {
143 for (
int i = 0;
i < 16;
i++) {
147 int expected_usage = 16 *
sizeof(
int);
167 field1.Swap(&field2);
182 for (
int i = 0;
i < 16;
i++) {
187 field1.Swap(&field2);
193 for (
int i = 0;
i < 16;
i++) {
204 for (
int i = 0;
i < 16;
i++) {
208 field2.Swap(&field1);
211 for (
int i = 0;
i < 16;
i++) {
217 for (
int i = 2;
i < 18;
i++) {
230 return field->size() - 1;
258 const int* previous_ptr =
field.data();
317 non_clamping_limit + 1),
326 #ifdef PROTOBUF_TEST_ALLOW_LARGE_ALLOC
332 huge_field.Reserve(min_clamping_size);
333 EXPECT_GE(huge_field.Capacity(), min_clamping_size);
336 #ifndef PROTOBUF_ASAN
345 huge_field.Reserve(huge_field.Capacity() + 1);
347 #endif // PROTOBUF_ASAN
348 #endif // PROTOBUF_TEST_ALLOW_LARGE_ALLOC
359 destination.MergeFrom(source);
378 destination.CopyFrom(source);
402 it = me.erase(me.begin(), me.end());
409 it = me.erase(me.begin() + 2, me.end());
418 it = me.erase(me.begin() + 1, me.begin() + 3);
482 std::stringstream ss;
485 me.Add(std::istream_iterator<int>(ss), std::istream_iterator<int>());
494 int vals[] = {2, 27, 2875, 609250};
554 destination = source;
580 const int*
data = source.data();
591 Arena::CreateMessage<RepeatedField<int>>(&
arena);
595 EXPECT_EQ(
nullptr, destination.GetArena());
610 const int* source_data = source.data();
611 const int* destination_data = destination.data();
613 EXPECT_EQ(source_data, destination.data());
617 EXPECT_EQ(destination_data, source.data());
623 Arena::CreateMessage<RepeatedField<int>>(&
arena);
627 Arena::CreateMessage<RepeatedField<int>>(&
arena);
629 const int* source_data = source->data();
630 const int* destination_data = destination->data();
632 EXPECT_EQ(source_data, destination->data());
636 EXPECT_EQ(destination_data, source->data());
642 Arena::CreateMessage<RepeatedField<int>>(&source_arena);
645 Arena destination_arena;
647 Arena::CreateMessage<RepeatedField<int>>(&destination_arena);
658 Arena::CreateMessage<RepeatedField<int>>(&
arena);
675 Arena::CreateMessage<RepeatedField<int>>(&
arena);
697 Arena::CreateMessage<RepeatedField<int>>(&
arena);
707 TEST(Movable, Works) {
708 class NonMoveConstructible {
710 NonMoveConstructible(NonMoveConstructible&&) =
delete;
711 NonMoveConstructible& operator=(NonMoveConstructible&&) {
return *
this; }
713 class NonMoveAssignable {
715 NonMoveAssignable(NonMoveAssignable&&) {}
716 NonMoveAssignable& operator=(NonMoveConstructible&&) =
delete;
720 NonMovable(NonMovable&&) =
delete;
721 NonMovable& operator=(NonMovable&&) =
delete;
738 RepeatedPtrField<TestAllTypes>
field;
739 TestAllTypes test_all_types;
740 auto* optional_nested_message =
741 test_all_types.mutable_optional_nested_message();
742 optional_nested_message->set_bb(42);
746 field.Mutable(0)->mutable_optional_nested_message());
755 *
field.mutable_data() = 2;
788 #ifdef PROTOBUF_HAS_DEATH_TEST
789 EXPECT_DEBUG_DEATH(
field.Truncate(
field.size() + 1),
"new_size");
796 for (
int sz = 0; sz < 10; ++sz) {
801 for (
int i = 0;
i < sz; ++
i)
field.Add(i);
806 for (
int i = 0;
i < 10; ++
i) catcher[i] = -1;
832 for (
int i = 0;
i < 32;
i++) {
850 for (
int i = 0;
i < 1000;
i++) {
859 TEST(RepeatedPtrField, ConstInit) {
860 PROTOBUF_CONSTINIT
static RepeatedPtrField<std::string>
field{};
867 template <
typename X>
868 auto ValidResolutionPointerRange(
const std::string* p)
870 template <
typename X>
873 TEST(RepeatedPtrField, UnambiguousConstructor) {
875 static bool f(std::vector<std::string>) {
return false; }
878 static bool g(std::vector<int>) {
return false; }
903 TEST(RepeatedPtrField, Small) {
904 RepeatedPtrField<std::string>
field;
909 field.Add()->assign(
"foo");
916 field.Add()->assign(
"bar");
925 field.Mutable(1)->assign(
"baz");
948 RepeatedPtrField<std::string>
field;
950 for (
int i = 0;
i < 16;
i++) {
956 for (
int i = 0;
i < 16;
i++) {
965 TEST(RepeatedPtrField, AddAndAssignRanges) {
966 RepeatedPtrField<std::string>
field;
968 const char*
vals[] = {
"abc",
"x",
"yz",
"xyzzy"};
989 TEST(RepeatedPtrField, SwapSmallSmall) {
990 RepeatedPtrField<std::string> field1;
991 RepeatedPtrField<std::string> field2;
998 field1.Add()->assign(
"foo");
999 field1.Add()->assign(
"bar");
1009 field1.Swap(&field2);
1019 TEST(RepeatedPtrField, SwapLargeSmall) {
1020 RepeatedPtrField<std::string> field1;
1021 RepeatedPtrField<std::string> field2;
1023 field2.Add()->assign(
"foo");
1024 field2.Add()->assign(
"bar");
1025 for (
int i = 0;
i < 16;
i++) {
1026 *field1.Add() +=
'a' +
i;
1028 field1.Swap(&field2);
1034 for (
int i = 0;
i < 16;
i++) {
1040 TEST(RepeatedPtrField, SwapLargeLarge) {
1041 RepeatedPtrField<std::string> field1;
1042 RepeatedPtrField<std::string> field2;
1044 field1.Add()->assign(
"foo");
1045 field1.Add()->assign(
"bar");
1046 for (
int i = 0;
i < 16;
i++) {
1047 *field1.Add() +=
'A' +
i;
1048 *field2.Add() +=
'a' +
i;
1050 field2.Swap(&field1);
1053 for (
int i = 0;
i < 16;
i++) {
1060 for (
int i = 2;
i < 18;
i++) {
1062 EXPECT_EQ(field2.Get(i)[0],
'A' + i - 2);
1066 static int ReservedSpace(RepeatedPtrField<std::string>*
field) {
1072 return field->size() - 1;
1075 TEST(RepeatedPtrField, ReserveMoreThanDouble) {
1076 RepeatedPtrField<std::string>
field;
1082 TEST(RepeatedPtrField, ReserveLessThanDouble) {
1083 RepeatedPtrField<std::string>
field;
1093 TEST(RepeatedPtrField, ReserveLessThanExisting) {
1094 RepeatedPtrField<std::string>
field;
1103 TEST(RepeatedPtrField, ReserveDoesntLoseAllocated) {
1107 RepeatedPtrField<std::string>
field;
1117 TEST(RepeatedPtrField, ClearedElements) {
1118 RepeatedPtrField<std::string>
field;
1137 field.UnsafeArenaAddAllocated(original);
1143 #ifndef PROTOBUF_FUTURE_BREAKING_CHANGES
1151 field.AddCleared(original);
1155 #endif // !PROTOBUF_FUTURE_BREAKING_CHANGES
1159 TEST(RepeatedPtrField, AddAllocated) {
1160 RepeatedPtrField<std::string>
field;
1162 field.Add()->assign(
"filler");
1185 field.AddAllocated(baz);
1192 field.Add()->assign(
"filler2");
1197 field.AddAllocated(qux);
1204 TEST(RepeatedPtrField, AddAllocatedDifferentArena) {
1205 RepeatedPtrField<TestAllTypes>
field;
1207 auto*
msg = Arena::CreateMessage<TestAllTypes>(&
arena);
1212 RepeatedPtrField<std::string> source, destination;
1213 source.Add()->assign(
"4");
1214 source.Add()->assign(
"5");
1215 destination.Add()->assign(
"1");
1216 destination.Add()->assign(
"2");
1217 destination.Add()->assign(
"3");
1219 destination.MergeFrom(source);
1231 RepeatedPtrField<std::string> source, destination;
1232 source.Add()->assign(
"4");
1233 source.Add()->assign(
"5");
1234 destination.Add()->assign(
"1");
1235 destination.Add()->assign(
"2");
1236 destination.Add()->assign(
"3");
1238 destination.CopyFrom(source);
1245 TEST(RepeatedPtrField, CopyFromSelf) {
1246 RepeatedPtrField<std::string> me;
1247 me.Add()->assign(
"1");
1253 TEST(RepeatedPtrField, Erase) {
1254 RepeatedPtrField<std::string> me;
1262 it = me.erase(me.begin(), me.end());
1269 it = me.erase(me.begin() + 2, me.end());
1278 it = me.erase(me.begin() + 1, me.begin() + 3);
1287 RepeatedPtrField<std::string> source;
1288 source.Add()->assign(
"1");
1289 source.Add()->assign(
"2");
1291 RepeatedPtrField<std::string> destination(source);
1298 TEST(RepeatedPtrField, IteratorConstruct_String) {
1299 std::vector<std::string>
values;
1308 RepeatedPtrField<std::string> other(
field.begin(),
field.end());
1314 TEST(RepeatedPtrField, IteratorConstruct_Proto) {
1315 typedef TestAllTypes::NestedMessage Nested;
1316 std::vector<Nested>
values;
1317 values.push_back(Nested());
1319 values.push_back(Nested());
1327 RepeatedPtrField<Nested> other(
field.begin(),
field.end());
1333 TEST(RepeatedPtrField, CopyAssign) {
1334 RepeatedPtrField<std::string> source, destination;
1335 source.Add()->assign(
"4");
1336 source.Add()->assign(
"5");
1337 destination.Add()->assign(
"1");
1338 destination.Add()->assign(
"2");
1339 destination.Add()->assign(
"3");
1341 destination = source;
1348 TEST(RepeatedPtrField, SelfAssign) {
1350 RepeatedPtrField<std::string> source, *
p;
1352 source.Add()->assign(
"7");
1353 source.Add()->assign(
"8");
1362 TEST(RepeatedPtrField, MoveConstruct) {
1364 RepeatedPtrField<std::string> source;
1365 *source.Add() =
"1";
1366 *source.Add() =
"2";
1368 RepeatedPtrField<std::string> destination =
std::move(source);
1377 RepeatedPtrField<std::string>* source =
1378 Arena::CreateMessage<RepeatedPtrField<std::string>>(&
arena);
1379 *source->Add() =
"1";
1380 *source->Add() =
"2";
1381 RepeatedPtrField<std::string> destination =
std::move(*source);
1382 EXPECT_EQ(
nullptr, destination.GetArena());
1390 TEST(RepeatedPtrField, MoveAssign) {
1392 RepeatedPtrField<std::string> source;
1393 *source.Add() =
"1";
1394 *source.Add() =
"2";
1395 RepeatedPtrField<std::string> destination;
1396 *destination.Add() =
"3";
1397 const std::string*
const* source_data = source.data();
1398 const std::string*
const* destination_data = destination.data();
1400 EXPECT_EQ(source_data, destination.data());
1404 EXPECT_EQ(destination_data, source.data());
1409 RepeatedPtrField<std::string>* source =
1410 Arena::CreateMessage<RepeatedPtrField<std::string>>(&
arena);
1411 *source->Add() =
"1";
1412 *source->Add() =
"2";
1413 RepeatedPtrField<std::string>* destination =
1414 Arena::CreateMessage<RepeatedPtrField<std::string>>(&
arena);
1415 *destination->Add() =
"3";
1416 const std::string*
const* source_data = source->data();
1417 const std::string*
const* destination_data = destination->data();
1419 EXPECT_EQ(source_data, destination->data());
1423 EXPECT_EQ(destination_data, source->data());
1428 RepeatedPtrField<std::string>* source =
1429 Arena::CreateMessage<RepeatedPtrField<std::string>>(&source_arena);
1430 *source->Add() =
"1";
1431 *source->Add() =
"2";
1432 Arena destination_arena;
1433 RepeatedPtrField<std::string>* destination =
1434 Arena::CreateMessage<RepeatedPtrField<std::string>>(&destination_arena);
1435 *destination->Add() =
"3";
1444 RepeatedPtrField<std::string>* source =
1445 Arena::CreateMessage<RepeatedPtrField<std::string>>(&
arena);
1446 *source->Add() =
"1";
1447 *source->Add() =
"2";
1448 RepeatedPtrField<std::string> destination;
1449 *destination.Add() =
"3";
1457 RepeatedPtrField<std::string> source;
1458 *source.Add() =
"1";
1459 *source.Add() =
"2";
1461 RepeatedPtrField<std::string>* destination =
1462 Arena::CreateMessage<RepeatedPtrField<std::string>>(&
arena);
1463 *destination->Add() =
"3";
1471 RepeatedPtrField<std::string>
field;
1473 RepeatedPtrField<std::string>& alias =
field;
1483 RepeatedPtrField<std::string>*
field =
1484 Arena::CreateMessage<RepeatedPtrField<std::string>>(&
arena);
1485 *
field->Add() =
"1";
1486 *
field->Add() =
"2";
1494 TEST(RepeatedPtrField, MutableDataIsMutable) {
1495 RepeatedPtrField<std::string>
field;
1505 TEST(RepeatedPtrField, SubscriptOperators) {
1506 RepeatedPtrField<std::string>
field;
1511 const RepeatedPtrField<std::string>& const_field =
field;
1515 TEST(RepeatedPtrField, ExtractSubrange) {
1518 for (
int sz = 0; sz < 10; ++sz) {
1521 for (
int extra = 0; extra < 4; ++extra) {
1522 std::vector<std::string*> subject;
1528 for (
int i = 0;
i < sz + extra; ++
i) {
1530 field.AddAllocated(subject[i]);
1533 for (
int i = 0;
i < extra; ++
i)
field.RemoveLast();
1539 for (
int i = 0;
i < 10; ++
i) catcher[i] =
nullptr;
1546 for (
int i = 0;
i <
num; ++
i)
1551 for (
int i = 0;
i <
start; ++
i)
1558 for (
int i = 0;
i < extra; ++
i)
field.Add();
1563 for (
int i = sz;
i < sz + extra; ++
i) {
1565 for (
int j = sz;
j < sz + extra; ++
j) {
1572 for (
int i = 0;
i <
num; ++
i)
delete catcher[i];
1579 TEST(RepeatedPtrField, DeleteSubrange) {
1588 virtual void SetUp() {
1589 for (
int i = 0;
i < 3; ++
i) {
1597 TEST_F(RepeatedFieldIteratorTest, Convertible) {
1604 TEST_F(RepeatedFieldIteratorTest, MutableIteration) {
1616 TEST_F(RepeatedFieldIteratorTest, ConstIteration) {
1628 TEST_F(RepeatedFieldIteratorTest, Mutation) {
1638 virtual void SetUp() {
1647 TEST_F(RepeatedPtrFieldIteratorTest, Convertible) {
1654 TEST_F(RepeatedPtrFieldIteratorTest, MutableIteration) {
1665 TEST_F(RepeatedPtrFieldIteratorTest, ConstIteration) {
1666 const RepeatedPtrField<std::string>& const_proto_array =
proto_array_;
1668 const_proto_array.begin();
1675 EXPECT_EQ(
"baz", *(--const_proto_array.end()));
1678 TEST_F(RepeatedPtrFieldIteratorTest, MutableReverseIteration) {
1689 TEST_F(RepeatedPtrFieldIteratorTest, ConstReverseIteration) {
1690 const RepeatedPtrField<std::string>& const_proto_array =
proto_array_;
1692 const_proto_array.rbegin();
1699 EXPECT_EQ(
"foo", *(--const_proto_array.rend()));
1702 TEST_F(RepeatedPtrFieldIteratorTest, RandomAccess) {
1714 TEST_F(RepeatedPtrFieldIteratorTest, Comparable) {
1728 TEST_F(RepeatedPtrFieldIteratorTest, UninitializedIterator) {
1737 TEST_F(RepeatedPtrFieldIteratorTest, STLAlgorithms_lower_bound) {
1755 TEST_F(RepeatedPtrFieldIteratorTest, Mutation) {
1763 class RepeatedPtrFieldPtrsIteratorTest :
public testing::Test {
1765 virtual void SetUp() {
1776 TEST_F(RepeatedPtrFieldPtrsIteratorTest, ConvertiblePtr) {
1779 static_cast<void>(
iter);
1782 TEST_F(RepeatedPtrFieldPtrsIteratorTest, ConvertibleConstPtr) {
1785 static_cast<void>(
iter);
1788 TEST_F(RepeatedPtrFieldPtrsIteratorTest, MutablePtrIteration) {
1800 TEST_F(RepeatedPtrFieldPtrsIteratorTest, MutableConstPtrIteration) {
1812 TEST_F(RepeatedPtrFieldPtrsIteratorTest, RandomPtrAccess) {
1825 TEST_F(RepeatedPtrFieldPtrsIteratorTest, RandomConstPtrAccess) {
1838 TEST_F(RepeatedPtrFieldPtrsIteratorTest, ComparablePtr) {
1852 TEST_F(RepeatedPtrFieldPtrsIteratorTest, ComparableConstPtr) {
1868 TEST_F(RepeatedPtrFieldPtrsIteratorTest, UninitializedPtrIterator) {
1877 TEST_F(RepeatedPtrFieldPtrsIteratorTest, UninitializedConstPtrIterator) {
1891 struct StringLessThan {
1897 TEST_F(RepeatedPtrFieldPtrsIteratorTest, PtrSTLAlgorithms_lower_bound) {
1922 &
v, StringLessThan());
1931 TEST_F(RepeatedPtrFieldPtrsIteratorTest, PtrMutation) {
1949 TEST_F(RepeatedPtrFieldPtrsIteratorTest,
Sort) {
1973 class RepeatedFieldInsertionIteratorsTest :
public testing::Test {
1978 typedef TestAllTypes::NestedMessage Nested;
1983 virtual void SetUp() {
1997 halves.push_back(0.0625);
2001 words.push_back(
"Able");
2002 words.push_back(
"was");
2003 words.push_back(
"I");
2004 words.push_back(
"ere");
2005 words.push_back(
"I");
2006 words.push_back(
"saw");
2007 words.push_back(
"Elba");
2026 virtual void TearDown() {
2041 TEST_F(RepeatedFieldInsertionIteratorsTest, Halves) {
2048 TEST_F(RepeatedFieldInsertionIteratorsTest, Words) {
2050 for (
int i = 0;
i <
words.size(); ++
i)
2054 TEST_F(RepeatedFieldInsertionIteratorsTest, Words2) {
2056 words.push_back(
"sing");
2057 words.push_back(
"a");
2058 words.push_back(
"song");
2059 words.push_back(
"of");
2060 words.push_back(
"six");
2061 words.push_back(
"pence");
2067 for (
int i = 0;
i <
words.size(); ++
i)
2071 TEST_F(RepeatedFieldInsertionIteratorsTest, Nesteds) {
2079 TEST_F(RepeatedFieldInsertionIteratorsTest,
2080 AllocatedRepeatedPtrFieldWithStringIntData) {
2081 std::vector<Nested*>
data;
2082 TestAllTypes goldenproto;
2083 for (
int i = 0;
i < 10; ++
i) {
2084 Nested* new_data =
new Nested;
2085 new_data->set_bb(i);
2086 data.push_back(new_data);
2088 new_data = goldenproto.add_repeated_nested_message();
2089 new_data->set_bb(i);
2091 TestAllTypes testproto;
2094 testproto.mutable_repeated_nested_message()));
2095 EXPECT_EQ(testproto.DebugString(), goldenproto.DebugString());
2098 TEST_F(RepeatedFieldInsertionIteratorsTest,
2099 AllocatedRepeatedPtrFieldWithString) {
2100 std::vector<std::string*>
data;
2101 TestAllTypes goldenproto;
2102 for (
int i = 0;
i < 10; ++
i) {
2104 *new_data =
"name-" +
StrCat(i);
2105 data.push_back(new_data);
2107 new_data = goldenproto.add_repeated_string();
2108 *new_data =
"name-" +
StrCat(i);
2110 TestAllTypes testproto;
2113 testproto.mutable_repeated_string()));
2114 EXPECT_EQ(testproto.DebugString(), goldenproto.DebugString());
2117 TEST_F(RepeatedFieldInsertionIteratorsTest,
2118 UnsafeArenaAllocatedRepeatedPtrFieldWithStringIntData) {
2119 std::vector<Nested*>
data;
2121 auto* goldenproto = Arena::CreateMessage<TestAllTypes>(&
arena);
2122 for (
int i = 0;
i < 10; ++
i) {
2123 auto* new_data = goldenproto->add_repeated_nested_message();
2124 new_data->set_bb(i);
2125 data.push_back(new_data);
2127 auto* testproto = Arena::CreateMessage<TestAllTypes>(&
arena);
2130 testproto->mutable_repeated_nested_message()));
2131 EXPECT_EQ(testproto->DebugString(), goldenproto->DebugString());
2134 TEST_F(RepeatedFieldInsertionIteratorsTest,
2135 UnsafeArenaAllocatedRepeatedPtrFieldWithString) {
2136 std::vector<std::string*>
data;
2138 auto* goldenproto = Arena::CreateMessage<TestAllTypes>(&
arena);
2139 for (
int i = 0;
i < 10; ++
i) {
2140 auto* new_data = goldenproto->add_repeated_string();
2141 *new_data =
"name-" +
StrCat(i);
2142 data.push_back(new_data);
2144 auto* testproto = Arena::CreateMessage<TestAllTypes>(&
arena);
2147 testproto->mutable_repeated_string()));
2148 EXPECT_EQ(testproto->DebugString(), goldenproto->DebugString());
2151 TEST_F(RepeatedFieldInsertionIteratorsTest, MoveStrings) {
2152 std::vector<std::string> src = {
"a",
"b",
"c",
"d"};
2153 std::vector<std::string>
copy =
2155 TestAllTypes testproto;
2162 TEST_F(RepeatedFieldInsertionIteratorsTest, MoveProtos) {
2163 auto make_nested = [](
int32 x) {
2168 std::vector<Nested> src = {make_nested(3), make_nested(5), make_nested(7)};
2169 std::vector<Nested>
copy = src;
2170 TestAllTypes testproto;
2175 ASSERT_EQ(src.size(), testproto.repeated_nested_message_size());
2176 for (
int i = 0;
i < src.size(); ++
i) {
2178 testproto.repeated_nested_message(i).DebugString());
2187 #include <google/protobuf/port_undef.inc>