From 13fd045dbb2b4dacea32be162a41d5a4b0d1802f Mon Sep 17 00:00:00 2001 From: Adam Cozzette Date: Tue, 12 Sep 2017 10:32:01 -0700 Subject: Integrated internal changes from Google --- src/google/protobuf/repeated_field_unittest.cc | 294 +++++++++++++++++++++++-- 1 file changed, 276 insertions(+), 18 deletions(-) (limited to 'src/google/protobuf/repeated_field_unittest.cc') diff --git a/src/google/protobuf/repeated_field_unittest.cc b/src/google/protobuf/repeated_field_unittest.cc index ae501465..16919711 100644 --- a/src/google/protobuf/repeated_field_unittest.cc +++ b/src/google/protobuf/repeated_field_unittest.cc @@ -45,17 +45,19 @@ #include #include #include +#include #include #include #include #include namespace google { -using protobuf_unittest::TestAllTypes; - namespace protobuf { namespace { +using ::protobuf_unittest::TestAllTypes; +using ::testing::ElementsAre; + // Test operations on a small RepeatedField. TEST(RepeatedField, Small) { RepeatedField field; @@ -267,15 +269,6 @@ TEST(RepeatedField, MergeFrom) { EXPECT_EQ(5, destination.Get(4)); } -#ifdef PROTOBUF_HAS_DEATH_TEST -#ifndef NDEBUG -TEST(RepeatedField, MergeFromSelf) { - RepeatedField me; - me.Add(3); - EXPECT_DEATH(me.MergeFrom(me), ""); -} -#endif // NDEBUG -#endif // PROTOBUF_HAS_DEATH_TEST TEST(RepeatedField, CopyFrom) { RepeatedField source, destination; @@ -390,6 +383,142 @@ TEST(RepeatedField, SelfAssign) { EXPECT_EQ(8, source.Get(1)); } +#if LANG_CXX11 + +TEST(RepeatedField, MoveConstruct) { + { + RepeatedField source; + source.Add(1); + source.Add(2); + const int* data = source.data(); + RepeatedField destination = std::move(source); + EXPECT_EQ(data, destination.data()); + EXPECT_THAT(destination, ElementsAre(1, 2)); + // This property isn't guaranteed but it's useful to have a test that would + // catch changes in this area. + EXPECT_TRUE(source.empty()); + } + { + Arena arena; + RepeatedField* source = + Arena::CreateMessage>(&arena); + source->Add(1); + source->Add(2); + RepeatedField destination = std::move(*source); + EXPECT_EQ(NULL, destination.GetArena()); + EXPECT_THAT(destination, ElementsAre(1, 2)); + // This property isn't guaranteed but it's useful to have a test that would + // catch changes in this area. + EXPECT_THAT(*source, ElementsAre(1, 2)); + } +} + +TEST(RepeatedField, MoveAssign) { + { + RepeatedField source; + source.Add(1); + source.Add(2); + RepeatedField destination; + destination.Add(3); + const int* source_data = source.data(); + const int* destination_data = destination.data(); + destination = std::move(source); + EXPECT_EQ(source_data, destination.data()); + EXPECT_THAT(destination, ElementsAre(1, 2)); + // This property isn't guaranteed but it's useful to have a test that would + // catch changes in this area. + EXPECT_EQ(destination_data, source.data()); + EXPECT_THAT(source, ElementsAre(3)); + } + { + Arena arena; + RepeatedField* source = + Arena::CreateMessage>(&arena); + source->Add(1); + source->Add(2); + RepeatedField* destination = + Arena::CreateMessage>(&arena); + destination->Add(3); + const int* source_data = source->data(); + const int* destination_data = destination->data(); + *destination = std::move(*source); + EXPECT_EQ(source_data, destination->data()); + EXPECT_THAT(*destination, ElementsAre(1, 2)); + // This property isn't guaranteed but it's useful to have a test that would + // catch changes in this area. + EXPECT_EQ(destination_data, source->data()); + EXPECT_THAT(*source, ElementsAre(3)); + } + { + Arena source_arena; + RepeatedField* source = + Arena::CreateMessage>(&source_arena); + source->Add(1); + source->Add(2); + Arena destination_arena; + RepeatedField* destination = + Arena::CreateMessage>(&destination_arena); + destination->Add(3); + *destination = std::move(*source); + EXPECT_THAT(*destination, ElementsAre(1, 2)); + // This property isn't guaranteed but it's useful to have a test that would + // catch changes in this area. + EXPECT_THAT(*source, ElementsAre(1, 2)); + } + { + Arena arena; + RepeatedField* source = + Arena::CreateMessage>(&arena); + source->Add(1); + source->Add(2); + RepeatedField destination; + destination.Add(3); + destination = std::move(*source); + EXPECT_THAT(destination, ElementsAre(1, 2)); + // This property isn't guaranteed but it's useful to have a test that would + // catch changes in this area. + EXPECT_THAT(*source, ElementsAre(1, 2)); + } + { + RepeatedField source; + source.Add(1); + source.Add(2); + Arena arena; + RepeatedField* destination = + Arena::CreateMessage>(&arena); + destination->Add(3); + *destination = std::move(source); + EXPECT_THAT(*destination, ElementsAre(1, 2)); + // This property isn't guaranteed but it's useful to have a test that would + // catch changes in this area. + EXPECT_THAT(source, ElementsAre(1, 2)); + } + { + RepeatedField field; + // An alias to defeat -Wself-move. + RepeatedField& alias = field; + field.Add(1); + field.Add(2); + const int* data = field.data(); + field = std::move(alias); + EXPECT_EQ(data, field.data()); + EXPECT_THAT(field, ElementsAre(1, 2)); + } + { + Arena arena; + RepeatedField* field = + Arena::CreateMessage>(&arena); + field->Add(1); + field->Add(2); + const int* data = field->data(); + *field = std::move(*field); + EXPECT_EQ(data, field->data()); + EXPECT_THAT(*field, ElementsAre(1, 2)); + } +} + +#endif // LANG_CXX11 + TEST(RepeatedField, MutableDataIsMutable) { RepeatedField field; field.Add(1); @@ -783,13 +912,6 @@ TEST(RepeatedPtrField, MergeFrom) { EXPECT_EQ("5", destination.Get(4)); } -#ifdef PROTOBUF_HAS_DEATH_TEST -TEST(RepeatedPtrField, MergeFromSelf) { - RepeatedPtrField me; - me.Add()->assign("1"); - EXPECT_DEATH(me.MergeFrom(me), ""); -} -#endif // PROTOBUF_HAS_DEATH_TEST TEST(RepeatedPtrField, CopyFrom) { RepeatedPtrField source, destination; @@ -923,6 +1045,142 @@ TEST(RepeatedPtrField, SelfAssign) { EXPECT_EQ("8", source.Get(1)); } +#if LANG_CXX11 + +TEST(RepeatedPtrField, MoveConstruct) { + { + RepeatedPtrField source; + *source.Add() = "1"; + *source.Add() = "2"; + const string* const* data = source.data(); + RepeatedPtrField destination = std::move(source); + EXPECT_EQ(data, destination.data()); + EXPECT_THAT(destination, ElementsAre("1", "2")); + // This property isn't guaranteed but it's useful to have a test that would + // catch changes in this area. + EXPECT_TRUE(source.empty()); + } + { + Arena arena; + RepeatedPtrField* source = + Arena::CreateMessage>(&arena); + *source->Add() = "1"; + *source->Add() = "2"; + RepeatedPtrField destination = std::move(*source); + EXPECT_EQ(NULL, destination.GetArena()); + EXPECT_THAT(destination, ElementsAre("1", "2")); + // This property isn't guaranteed but it's useful to have a test that would + // catch changes in this area. + EXPECT_THAT(*source, ElementsAre("1", "2")); + } +} + +TEST(RepeatedPtrField, MoveAssign) { + { + RepeatedPtrField source; + *source.Add() = "1"; + *source.Add() = "2"; + RepeatedPtrField destination; + *destination.Add() = "3"; + const string* const* source_data = source.data(); + const string* const* destination_data = destination.data(); + destination = std::move(source); + EXPECT_EQ(source_data, destination.data()); + EXPECT_THAT(destination, ElementsAre("1", "2")); + // This property isn't guaranteed but it's useful to have a test that would + // catch changes in this area. + EXPECT_EQ(destination_data, source.data()); + EXPECT_THAT(source, ElementsAre("3")); + } + { + Arena arena; + RepeatedPtrField* source = + Arena::CreateMessage>(&arena); + *source->Add() = "1"; + *source->Add() = "2"; + RepeatedPtrField* destination = + Arena::CreateMessage>(&arena); + *destination->Add() = "3"; + const string* const* source_data = source->data(); + const string* const* destination_data = destination->data(); + *destination = std::move(*source); + EXPECT_EQ(source_data, destination->data()); + EXPECT_THAT(*destination, ElementsAre("1", "2")); + // This property isn't guaranteed but it's useful to have a test that would + // catch changes in this area. + EXPECT_EQ(destination_data, source->data()); + EXPECT_THAT(*source, ElementsAre("3")); + } + { + Arena source_arena; + RepeatedPtrField* source = + Arena::CreateMessage>(&source_arena); + *source->Add() = "1"; + *source->Add() = "2"; + Arena destination_arena; + RepeatedPtrField* destination = + Arena::CreateMessage>(&destination_arena); + *destination->Add() = "3"; + *destination = std::move(*source); + EXPECT_THAT(*destination, ElementsAre("1", "2")); + // This property isn't guaranteed but it's useful to have a test that would + // catch changes in this area. + EXPECT_THAT(*source, ElementsAre("1", "2")); + } + { + Arena arena; + RepeatedPtrField* source = + Arena::CreateMessage>(&arena); + *source->Add() = "1"; + *source->Add() = "2"; + RepeatedPtrField destination; + *destination.Add() = "3"; + destination = std::move(*source); + EXPECT_THAT(destination, ElementsAre("1", "2")); + // This property isn't guaranteed but it's useful to have a test that would + // catch changes in this area. + EXPECT_THAT(*source, ElementsAre("1", "2")); + } + { + RepeatedPtrField source; + *source.Add() = "1"; + *source.Add() = "2"; + Arena arena; + RepeatedPtrField* destination = + Arena::CreateMessage>(&arena); + *destination->Add() = "3"; + *destination = std::move(source); + EXPECT_THAT(*destination, ElementsAre("1", "2")); + // This property isn't guaranteed but it's useful to have a test that would + // catch changes in this area. + EXPECT_THAT(source, ElementsAre("1", "2")); + } + { + RepeatedPtrField field; + // An alias to defeat -Wself-move. + RepeatedPtrField& alias = field; + *field.Add() = "1"; + *field.Add() = "2"; + const string* const* data = field.data(); + field = std::move(alias); + EXPECT_EQ(data, field.data()); + EXPECT_THAT(field, ElementsAre("1", "2")); + } + { + Arena arena; + RepeatedPtrField* field = + Arena::CreateMessage>(&arena); + *field->Add() = "1"; + *field->Add() = "2"; + const string* const* data = field->data(); + *field = std::move(*field); + EXPECT_EQ(data, field->data()); + EXPECT_THAT(*field, ElementsAre("1", "2")); + } +} + +#endif // LANG_CXX11 + TEST(RepeatedPtrField, MutableDataIsMutable) { RepeatedPtrField field; *field.Add() = "1"; -- cgit v1.2.3