diff options
author | Jisi Liu <jisi.liu@gmail.com> | 2015-02-28 14:51:22 -0800 |
---|---|---|
committer | Jisi Liu <jisi.liu@gmail.com> | 2015-02-28 17:06:49 -0800 |
commit | 885b612f74f133678bf82808c589331e4c59dad9 (patch) | |
tree | e5f3f65b41af477c52810053b8694896c8bcd1f7 /src/google/protobuf/map_test.cc | |
parent | 1939efed2db35020b7830a4927f10feac47b6757 (diff) |
Down integrate from Google internal branch for C++ and Java.
- Maps for C++ lite
- C++ Arena optimizations.
- Java Lite runtime code size optimization.
Change-Id: I7537a4357c1cb385d23f9e8aa7ffdfeefe079f13
Diffstat (limited to 'src/google/protobuf/map_test.cc')
-rw-r--r-- | src/google/protobuf/map_test.cc | 84 |
1 files changed, 71 insertions, 13 deletions
diff --git a/src/google/protobuf/map_test.cc b/src/google/protobuf/map_test.cc index 9db67523..88cba1f2 100644 --- a/src/google/protobuf/map_test.cc +++ b/src/google/protobuf/map_test.cc @@ -39,7 +39,7 @@ #include <google/protobuf/stubs/common.h> #include <google/protobuf/stubs/stringprintf.h> #include <google/protobuf/testing/file.h> -#include <google/protobuf/map_lite_unittest.pb.h> +#include <google/protobuf/arena_test_util.h> #include <google/protobuf/map_proto2_unittest.pb.h> #include <google/protobuf/map_unittest.pb.h> #include <google/protobuf/map_test_util.h> @@ -191,6 +191,7 @@ TEST_F(MapImplTest, MutableAt) { } #ifdef PROTOBUF_HAS_DEATH_TEST + TEST_F(MapImplTest, MutableAtNonExistDeathTest) { EXPECT_DEATH(map_.at(0), ""); } @@ -198,6 +199,7 @@ TEST_F(MapImplTest, MutableAtNonExistDeathTest) { TEST_F(MapImplTest, ImmutableAtNonExistDeathTest) { EXPECT_DEATH(const_map_.at(0), ""); } + #endif // PROTOBUF_HAS_DEATH_TEST TEST_F(MapImplTest, CountNonExist) { @@ -553,6 +555,14 @@ TEST_F(MapImplTest, ConvertToStdMap) { EXPECT_EQ(101, std_map[100]); } +TEST_F(MapImplTest, ConvertToStdVectorOfPairs) { + map_[100] = 101; + std::vector<std::pair<int32, int32> > std_vec(map_.begin(), map_.end()); + EXPECT_EQ(1, std_vec.size()); + EXPECT_EQ(100, std_vec[0].first); + EXPECT_EQ(101, std_vec[0].second); +} + // Map Field Reflection Test ======================================== static int Func(int i, int j) { @@ -1717,6 +1727,20 @@ TEST(GeneratedMapFieldTest, MissedValueWireFormat) { EXPECT_EQ(0, message.map_int32_int32().at(1)); } +TEST(GeneratedMapFieldTest, MissedValueTextFormat) { + unittest::TestMap message; + + // No value field in text format + string text = + "map_int32_foreign_message {\n" + " key: 1234567890\n" + "}"; + + EXPECT_TRUE(google::protobuf::TextFormat::ParseFromString(text, &message)); + EXPECT_EQ(1, message.map_int32_foreign_message().size()); + EXPECT_EQ(11, message.ByteSize()); +} + TEST(GeneratedMapFieldTest, UnknownFieldWireFormat) { unittest::TestMap message; @@ -1737,18 +1761,6 @@ TEST(GeneratedMapFieldTest, CorruptedWireFormat) { EXPECT_FALSE(message.ParseFromString(data)); } -TEST(GeneratedMapFieldTest, MessageLiteMap) { - unittest::MapLite from, to; - (*from.mutable_map_field())[1] = 1; - - string data; - from.SerializeToString(&data); - to.ParseFromString(data); - - EXPECT_EQ(1, to.map_field().size()); - EXPECT_EQ(1, to.map_field().at(1)); -} - TEST(GeneratedMapFieldTest, IsInitialized) { unittest::TestRequiredMessageMap map_message; @@ -2247,6 +2259,52 @@ TEST(TextFormatMapTest, SerializeAndParse) { } +// arena support ================================================= +TEST(ArenaTest, ParsingAndSerializingNoHeapAllocation) { + // Allocate a large initial block to avoid mallocs during hooked test. + std::vector<char> arena_block(128 * 1024); + ArenaOptions options; + options.initial_block = arena_block.data(); + options.initial_block_size = arena_block.size(); + Arena arena(options); + string data; + data.reserve(128 * 1024); + + { + NoHeapChecker no_heap; + + unittest::TestArenaMap* from = + Arena::CreateMessage<unittest::TestArenaMap>(&arena); + MapTestUtil::SetArenaMapFields(from); + from->SerializeToString(&data); + + unittest::TestArenaMap* to = + Arena::CreateMessage<unittest::TestArenaMap>(&arena); + to->ParseFromString(data); + MapTestUtil::ExpectArenaMapFieldsSet(*to); + } +} + +// Use text format parsing and serializing to test reflection api. +TEST(ArenaTest, RelfectionInTextFormat) { + Arena arena; + string data; + + TextFormat::Printer printer; + TextFormat::Parser parser; + + unittest::TestArenaMap* from = + Arena::CreateMessage<unittest::TestArenaMap>(&arena); + unittest::TestArenaMap* to = + Arena::CreateMessage<unittest::TestArenaMap>(&arena); + + MapTestUtil::SetArenaMapFields(from); + printer.PrintToString(*from, &data); + + EXPECT_TRUE(parser.ParseFromString(data, to)); + MapTestUtil::ExpectArenaMapFieldsSet(*to); +} + } // namespace internal } // namespace protobuf } // namespace google |