aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/google/protobuf/map_test.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/google/protobuf/map_test.cc')
-rw-r--r--src/google/protobuf/map_test.cc105
1 files changed, 92 insertions, 13 deletions
diff --git a/src/google/protobuf/map_test.cc b/src/google/protobuf/map_test.cc
index 9db67523..d62ec85f 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;
@@ -2246,6 +2258,73 @@ TEST(TextFormatMapTest, SerializeAndParse) {
MapTestUtil::ExpectMapFieldsSet(dest);
}
+TEST(TextFormatMapTest, Sorted) {
+ unittest::TestMap message;
+ MapTestUtil::MapReflectionTester tester(message.GetDescriptor());
+ tester.SetMapFieldsViaReflection(&message);
+
+ string expected_text;
+ GOOGLE_CHECK_OK(File::GetContents(
+ TestSourceDir() +
+ "/google/protobuf/"
+ "testdata/map_test_data.txt",
+ &expected_text, true));
+
+ EXPECT_EQ(message.DebugString(), expected_text);
+
+ // Test again on the reverse order.
+ unittest::TestMap message2;
+ tester.SetMapFieldsViaReflection(&message2);
+ tester.SwapMapsViaReflection(&message2);
+ EXPECT_EQ(message2.DebugString(), expected_text);
+}
+
+
+// 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[0];
+ 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