44 #include <type_traits>
49 #include <google/protobuf/unittest.pb.h>
61 using ::protobuf_unittest::TestAllTypes;
62 using ::testing::ElementsAre;
117 int expected_usage = 4 *
sizeof(int) +
sizeof(Arena*);
127 for (
int i = 0;
i < 16;
i++) {
134 for (
int i = 0;
i < 16;
i++) {
138 int expected_usage = 16 *
sizeof(int);
158 field1.Swap(&field2);
173 for (
int i = 0;
i < 16;
i++) {
178 field1.Swap(&field2);
184 for (
int i = 0;
i < 16;
i++) {
195 for (
int i = 0;
i < 16;
i++) {
199 field2.Swap(&field1);
202 for (
int i = 0;
i < 16;
i++) {
208 for (
int i = 2;
i < 18;
i++) {
216 const int* ptr =
field->data();
219 }
while (
field->data() == ptr);
221 return field->size() - 1;
238 int capacity =
field.Capacity();
239 field.Reserve(capacity * 1.5);
249 const int* previous_ptr =
field.data();
280 destination.MergeFrom(
source);
299 destination.CopyFrom(
source);
323 it = me.erase(me.begin(), me.end());
330 it = me.erase(me.begin() + 2, me.end());
339 it = me.erase(me.begin() + 1, me.begin() + 3);
403 std::stringstream ss;
406 me.Add(std::istream_iterator<int>(ss), std::istream_iterator<int>());
488 Arena::CreateMessage<RepeatedField<int>>(&arena);
492 EXPECT_EQ(
nullptr, destination.GetArena());
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());
520 Arena::CreateMessage<RepeatedField<int>>(&arena);
524 Arena::CreateMessage<RepeatedField<int>>(&arena);
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());
539 Arena::CreateMessage<RepeatedField<int>>(&source_arena);
542 Arena destination_arena;
544 Arena::CreateMessage<RepeatedField<int>>(&destination_arena);
546 *destination = std::move(*
source);
555 Arena::CreateMessage<RepeatedField<int>>(&arena);
560 destination = std::move(*
source);
572 Arena::CreateMessage<RepeatedField<int>>(&arena);
574 *destination = std::move(
source);
587 field = std::move(alias);
594 Arena::CreateMessage<RepeatedField<int>>(&arena);
604 TEST(Movable, Works) {
605 class NonMoveConstructible {
607 NonMoveConstructible(NonMoveConstructible&&) =
delete;
608 NonMoveConstructible& operator=(NonMoveConstructible&&) {
return *
this; }
610 class NonMoveAssignable {
612 NonMoveAssignable(NonMoveAssignable&&) {}
613 NonMoveAssignable& operator=(NonMoveConstructible&&) =
delete;
617 NonMovable(NonMovable&&) =
delete;
618 NonMovable& operator=(NonMovable&&) =
delete;
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));
643 field.Mutable(0)->mutable_optional_nested_message());
652 *
field.mutable_data() = 2;
685 #ifdef PROTOBUF_HAS_DEATH_TEST
686 EXPECT_DEBUG_DEATH(
field.Truncate(
field.size() + 1),
"new_size");
693 for (
int sz = 0; sz < 10; ++sz) {
694 for (
int num = 0; num <= sz; ++num) {
698 for (
int i = 0;
i < sz; ++
i)
field.Add(
i);
703 for (
int i = 0;
i < 10; ++
i) catcher[
i] = -1;
729 for (
int i = 0;
i < 32;
i++) {
748 TEST(RepeatedPtrField, Small) {
749 RepeatedPtrField<std::string>
field;
754 field.Add()->assign(
"foo");
761 field.Add()->assign(
"bar");
770 field.Mutable(1)->assign(
"baz");
792 TEST(RepeatedPtrField, Large) {
793 RepeatedPtrField<std::string>
field;
795 for (
int i = 0;
i < 16;
i++) {
801 for (
int i = 0;
i < 16;
i++) {
810 TEST(RepeatedPtrField, SwapSmallSmall) {
811 RepeatedPtrField<std::string> field1;
812 RepeatedPtrField<std::string> field2;
819 field1.Add()->assign(
"foo");
820 field1.Add()->assign(
"bar");
830 field1.Swap(&field2);
840 TEST(RepeatedPtrField, SwapLargeSmall) {
841 RepeatedPtrField<std::string> field1;
842 RepeatedPtrField<std::string> field2;
844 field2.Add()->assign(
"foo");
845 field2.Add()->assign(
"bar");
846 for (
int i = 0;
i < 16;
i++) {
847 *field1.Add() +=
'a' +
i;
849 field1.Swap(&field2);
855 for (
int i = 0;
i < 16;
i++) {
861 TEST(RepeatedPtrField, SwapLargeLarge) {
862 RepeatedPtrField<std::string> field1;
863 RepeatedPtrField<std::string> field2;
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;
871 field2.Swap(&field1);
874 for (
int i = 0;
i < 16;
i++) {
881 for (
int i = 2;
i < 18;
i++) {
887 static int ReservedSpace(RepeatedPtrField<std::string>*
field) {
891 }
while (
field->data() == ptr);
893 return field->size() - 1;
896 TEST(RepeatedPtrField, ReserveMoreThanDouble) {
897 RepeatedPtrField<std::string>
field;
903 TEST(RepeatedPtrField, ReserveLessThanDouble) {
904 RepeatedPtrField<std::string>
field;
907 int capacity =
field.Capacity();
909 field.Reserve(capacity + (capacity >> 2));
914 TEST(RepeatedPtrField, ReserveLessThanExisting) {
915 RepeatedPtrField<std::string>
field;
924 TEST(RepeatedPtrField, ReserveDoesntLoseAllocated) {
928 RepeatedPtrField<std::string>
field;
938 TEST(RepeatedPtrField, ClearedElements) {
939 RepeatedPtrField<std::string>
field;
959 field.AddAllocated(original);
972 field.AddCleared(original);
979 TEST(RepeatedPtrField, AddAlocated) {
980 RepeatedPtrField<std::string>
field;
982 field.Add()->assign(
"filler");
1005 field.AddAllocated(baz);
1012 field.Add()->assign(
"filler2");
1017 field.AddAllocated(qux);
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");
1032 destination.MergeFrom(
source);
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");
1051 destination.CopyFrom(
source);
1058 TEST(RepeatedPtrField, CopyFromSelf) {
1059 RepeatedPtrField<std::string> me;
1060 me.Add()->assign(
"1");
1066 TEST(RepeatedPtrField, Erase) {
1067 RepeatedPtrField<std::string> me;
1075 it = me.erase(me.begin(), me.end());
1082 it = me.erase(me.begin() + 2, me.end());
1091 it = me.erase(me.begin() + 1, me.begin() + 3);
1099 TEST(RepeatedPtrField, CopyConstruct) {
1100 RepeatedPtrField<std::string>
source;
1101 source.Add()->assign(
"1");
1102 source.Add()->assign(
"2");
1104 RepeatedPtrField<std::string> destination(
source);
1111 TEST(RepeatedPtrField, IteratorConstruct_String) {
1112 std::vector<std::string>
values;
1121 RepeatedPtrField<std::string> other(
field.begin(),
field.end());
1127 TEST(RepeatedPtrField, IteratorConstruct_Proto) {
1128 typedef TestAllTypes::NestedMessage Nested;
1129 std::vector<Nested>
values;
1130 values.push_back(Nested());
1132 values.push_back(Nested());
1140 RepeatedPtrField<Nested> other(
field.begin(),
field.end());
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");
1161 TEST(RepeatedPtrField, SelfAssign) {
1163 RepeatedPtrField<std::string>
source, *
p;
1165 source.Add()->assign(
"7");
1166 source.Add()->assign(
"8");
1175 TEST(RepeatedPtrField, MoveConstruct) {
1177 RepeatedPtrField<std::string>
source;
1181 RepeatedPtrField<std::string> destination = std::move(
source);
1190 RepeatedPtrField<std::string>*
source =
1191 Arena::CreateMessage<RepeatedPtrField<std::string>>(&arena);
1194 RepeatedPtrField<std::string> destination = std::move(*
source);
1195 EXPECT_EQ(
nullptr, destination.GetArena());
1203 TEST(RepeatedPtrField, MoveAssign) {
1205 RepeatedPtrField<std::string>
source;
1208 RepeatedPtrField<std::string> destination;
1209 *destination.Add() =
"3";
1211 const std::string*
const* destination_data = destination.data();
1212 destination = std::move(
source);
1213 EXPECT_EQ(source_data, destination.data());
1222 RepeatedPtrField<std::string>*
source =
1223 Arena::CreateMessage<RepeatedPtrField<std::string>>(&arena);
1226 RepeatedPtrField<std::string>* destination =
1227 Arena::CreateMessage<RepeatedPtrField<std::string>>(&arena);
1228 *destination->Add() =
"3";
1230 const std::string*
const* destination_data = destination->data();
1231 *destination = std::move(*
source);
1232 EXPECT_EQ(source_data, destination->data());
1241 RepeatedPtrField<std::string>*
source =
1242 Arena::CreateMessage<RepeatedPtrField<std::string>>(&source_arena);
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);
1257 RepeatedPtrField<std::string>*
source =
1258 Arena::CreateMessage<RepeatedPtrField<std::string>>(&arena);
1261 RepeatedPtrField<std::string> destination;
1262 *destination.Add() =
"3";
1263 destination = std::move(*
source);
1270 RepeatedPtrField<std::string>
source;
1274 RepeatedPtrField<std::string>* destination =
1275 Arena::CreateMessage<RepeatedPtrField<std::string>>(&arena);
1276 *destination->Add() =
"3";
1277 *destination = std::move(
source);
1284 RepeatedPtrField<std::string>
field;
1286 RepeatedPtrField<std::string>& alias =
field;
1290 field = std::move(alias);
1296 RepeatedPtrField<std::string>*
field =
1297 Arena::CreateMessage<RepeatedPtrField<std::string>>(&arena);
1298 *
field->Add() =
"1";
1299 *
field->Add() =
"2";
1307 TEST(RepeatedPtrField, MutableDataIsMutable) {
1308 RepeatedPtrField<std::string>
field;
1318 TEST(RepeatedPtrField, SubscriptOperators) {
1319 RepeatedPtrField<std::string>
field;
1324 const RepeatedPtrField<std::string>& const_field =
field;
1328 TEST(RepeatedPtrField, ExtractSubrange) {
1331 for (
int sz = 0; sz < 10; ++sz) {
1332 for (
int num = 0; num <= sz; ++num) {
1334 for (
int extra = 0; extra < 4; ++extra) {
1335 std::vector<std::string*> subject;
1338 RepeatedPtrField<std::string>
field;
1339 for (
int i = 0;
i < sz + extra; ++
i) {
1341 field.AddAllocated(subject[
i]);
1344 for (
int i = 0;
i < extra; ++
i)
field.RemoveLast();
1350 for (
int i = 0;
i < 10; ++
i) catcher[
i] =
NULL;
1357 for (
int i = 0;
i < num; ++
i)
1369 for (
int i = 0;
i < extra; ++
i)
field.Add();
1374 for (
int i = sz;
i < sz + extra; ++
i) {
1376 for (
int j = sz; j < sz + extra; ++j) {
1377 if (
field.Mutable(j - num) == subject[
i])
count += 1;
1383 for (
int i = 0;
i < num; ++
i)
delete catcher[
i];
1390 TEST(RepeatedPtrField, DeleteSubrange) {
1399 virtual void SetUp() {
1400 for (
int i = 0;
i < 3; ++
i) {
1408 TEST_F(RepeatedFieldIteratorTest, Convertible) {
1415 TEST_F(RepeatedFieldIteratorTest, MutableIteration) {
1427 TEST_F(RepeatedFieldIteratorTest, ConstIteration) {
1439 TEST_F(RepeatedFieldIteratorTest, Mutation) {
1449 virtual void SetUp() {
1458 TEST_F(RepeatedPtrFieldIteratorTest, Convertible) {
1465 TEST_F(RepeatedPtrFieldIteratorTest, MutableIteration) {
1476 TEST_F(RepeatedPtrFieldIteratorTest, ConstIteration) {
1477 const RepeatedPtrField<std::string>& const_proto_array =
proto_array_;
1479 const_proto_array.begin();
1486 EXPECT_EQ(
"baz", *(--const_proto_array.end()));
1489 TEST_F(RepeatedPtrFieldIteratorTest, MutableReverseIteration) {
1500 TEST_F(RepeatedPtrFieldIteratorTest, ConstReverseIteration) {
1501 const RepeatedPtrField<std::string>& const_proto_array =
proto_array_;
1503 const_proto_array.rbegin();
1510 EXPECT_EQ(
"foo", *(--const_proto_array.rend()));
1513 TEST_F(RepeatedPtrFieldIteratorTest, RandomAccess) {
1525 TEST_F(RepeatedPtrFieldIteratorTest, Comparable) {
1539 TEST_F(RepeatedPtrFieldIteratorTest, UninitializedIterator) {
1548 TEST_F(RepeatedPtrFieldIteratorTest, STLAlgorithms_lower_bound) {
1566 TEST_F(RepeatedPtrFieldIteratorTest, Mutation) {
1574 class RepeatedPtrFieldPtrsIteratorTest :
public testing::Test {
1576 virtual void SetUp() {
1587 TEST_F(RepeatedPtrFieldPtrsIteratorTest, ConvertiblePtr) {
1590 static_cast<void>(iter);
1593 TEST_F(RepeatedPtrFieldPtrsIteratorTest, ConvertibleConstPtr) {
1596 static_cast<void>(iter);
1599 TEST_F(RepeatedPtrFieldPtrsIteratorTest, MutablePtrIteration) {
1611 TEST_F(RepeatedPtrFieldPtrsIteratorTest, MutableConstPtrIteration) {
1623 TEST_F(RepeatedPtrFieldPtrsIteratorTest, RandomPtrAccess) {
1636 TEST_F(RepeatedPtrFieldPtrsIteratorTest, RandomConstPtrAccess) {
1649 TEST_F(RepeatedPtrFieldPtrsIteratorTest, ComparablePtr) {
1663 TEST_F(RepeatedPtrFieldPtrsIteratorTest, ComparableConstPtr) {
1679 TEST_F(RepeatedPtrFieldPtrsIteratorTest, UninitializedPtrIterator) {
1688 TEST_F(RepeatedPtrFieldPtrsIteratorTest, UninitializedConstPtrIterator) {
1702 struct StringLessThan {
1708 TEST_F(RepeatedPtrFieldPtrsIteratorTest, PtrSTLAlgorithms_lower_bound) {
1733 &
v, StringLessThan());
1742 TEST_F(RepeatedPtrFieldPtrsIteratorTest, PtrMutation) {
1760 TEST_F(RepeatedPtrFieldPtrsIteratorTest,
Sort) {
1784 class RepeatedFieldInsertionIteratorsTest :
public testing::Test {
1789 typedef TestAllTypes::NestedMessage Nested;
1794 virtual void SetUp() {
1808 halves.push_back(0.0625);
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");
1837 virtual void TearDown() {
1842 TEST_F(RepeatedFieldInsertionIteratorsTest, Fibonacci) {
1850 TEST_F(RepeatedFieldInsertionIteratorsTest, Halves) {
1857 TEST_F(RepeatedFieldInsertionIteratorsTest, Words) {
1859 for (
int i = 0;
i <
words.size(); ++
i)
1863 TEST_F(RepeatedFieldInsertionIteratorsTest, Words2) {
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");
1876 for (
int i = 0;
i <
words.size(); ++
i)
1880 TEST_F(RepeatedFieldInsertionIteratorsTest, Nesteds) {
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);
1897 new_data = goldenproto.add_repeated_nested_message();
1898 new_data->set_bb(
i);
1900 TestAllTypes testproto;
1901 std::copy(
data.begin(),
data.end(),
1903 testproto.mutable_repeated_nested_message()));
1904 EXPECT_EQ(testproto.DebugString(), goldenproto.DebugString());
1907 TEST_F(RepeatedFieldInsertionIteratorsTest,
1908 AllocatedRepeatedPtrFieldWithString) {
1909 std::vector<std::string*>
data;
1910 TestAllTypes goldenproto;
1911 for (
int i = 0;
i < 10; ++
i) {
1913 *new_data =
"name-" +
StrCat(
i);
1914 data.push_back(new_data);
1916 new_data = goldenproto.add_repeated_string();
1917 *new_data =
"name-" +
StrCat(
i);
1919 TestAllTypes testproto;
1920 std::copy(
data.begin(),
data.end(),
1922 testproto.mutable_repeated_string()));
1923 EXPECT_EQ(testproto.DebugString(), goldenproto.DebugString());
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);
1935 new_data = goldenproto.add_repeated_nested_message();
1936 new_data->set_bb(
i);
1938 TestAllTypes testproto;
1939 std::copy(
data.begin(),
data.end(),
1941 testproto.mutable_repeated_nested_message()));
1942 EXPECT_EQ(testproto.DebugString(), goldenproto.DebugString());
1945 TEST_F(RepeatedFieldInsertionIteratorsTest,
1946 UnsafeArenaAllocatedRepeatedPtrFieldWithString) {
1947 std::vector<std::string*>
data;
1948 TestAllTypes goldenproto;
1949 for (
int i = 0;
i < 10; ++
i) {
1951 *new_data =
"name-" +
StrCat(
i);
1952 data.push_back(new_data);
1954 new_data = goldenproto.add_repeated_string();
1955 *new_data =
"name-" +
StrCat(
i);
1957 TestAllTypes testproto;
1958 std::copy(
data.begin(),
data.end(),
1960 testproto.mutable_repeated_string()));
1961 EXPECT_EQ(testproto.DebugString(), goldenproto.DebugString());
1964 TEST_F(RepeatedFieldInsertionIteratorsTest, MoveStrings) {
1965 std::vector<std::string>
src = {
"a",
"b",
"c",
"d"};
1966 std::vector<std::string> copy =
1968 TestAllTypes testproto;
1969 std::move(copy.begin(), copy.end(),
1972 ASSERT_THAT(testproto.repeated_string(), testing::ElementsAreArray(
src));
1975 TEST_F(RepeatedFieldInsertionIteratorsTest, MoveProtos) {
1976 auto make_nested = [](
int32 x) {
1981 std::vector<Nested>
src = {make_nested(3), make_nested(5), make_nested(7)};
1982 std::vector<Nested> copy =
src;
1983 TestAllTypes testproto;
1985 copy.begin(), copy.end(),
1988 ASSERT_EQ(
src.size(), testproto.repeated_nested_message_size());
1989 for (
int i = 0;
i <
src.size(); ++
i) {
1991 testproto.repeated_nested_message(
i).DebugString());