From 26bd9eee6ee6d116e1cc0dedeb660cd69d7aac45 Mon Sep 17 00:00:00 2001 From: "kenton@google.com" Date: Fri, 21 Nov 2008 00:06:27 +0000 Subject: Integrate changes from internal code. protoc * Enum values may now have custom options, using syntax similar to field options. * Fixed bug where .proto files which use custom options but don't actually define them (i.e. they import another .proto file defining the options) had to explicitly import descriptor.proto. * Adjacent string literals in .proto files will now be concatenated, like in C. C++ * Generated message classes now have a Swap() method which efficiently swaps the contents of two objects. * All message classes now have a SpaceUsed() method which returns an estimate of the number of bytes of allocated memory currently owned by the object. This is particularly useful when you are reusing a single message object to improve performance but want to make sure it doesn't bloat up too large. * New method Message::SerializeAsString() returns a string containing the serialized data. May be more convenient than calling SerializeToString(string*). * In debug mode, log error messages when string-type fields are found to contain bytes that are not valid UTF-8. * Fixed bug where a message with multiple extension ranges couldn't parse extensions. * Fixed bug where MergeFrom(const Message&) didn't do anything if invoked on a message that contained no fields (but possibly contained extensions). * Fixed ShortDebugString() to not be O(n^2). Durr. * Fixed crash in TextFormat parsing if the first token in the input caused a tokenization error. Java * New overload of mergeFrom() which parses a slice of a byte array instead of the whole thing. * New method ByteString.asReadOnlyByteBuffer() does what it sounds like. * Improved performance of isInitialized() when optimizing for code size. Python * Corrected ListFields() signature in Message base class to match what subclasses actually implement. * Some minor refactoring. --- src/google/protobuf/unknown_field_set_unittest.cc | 90 +++++++++++++++++++++++ 1 file changed, 90 insertions(+) (limited to 'src/google/protobuf/unknown_field_set_unittest.cc') diff --git a/src/google/protobuf/unknown_field_set_unittest.cc b/src/google/protobuf/unknown_field_set_unittest.cc index c358e1f1..c7e78b23 100644 --- a/src/google/protobuf/unknown_field_set_unittest.cc +++ b/src/google/protobuf/unknown_field_set_unittest.cc @@ -222,6 +222,30 @@ TEST_F(UnknownFieldSetTest, CopyFrom) { EXPECT_EQ(empty_message_.DebugString(), message.DebugString()); } +TEST_F(UnknownFieldSetTest, Swap) { + unittest::TestEmptyMessage other_message; + ASSERT_TRUE(other_message.ParseFromString(GetBizarroData())); + + EXPECT_GT(empty_message_.unknown_fields().field_count(), 0); + EXPECT_GT(other_message.unknown_fields().field_count(), 0); + const string debug_string = empty_message_.DebugString(); + const string other_debug_string = other_message.DebugString(); + EXPECT_NE(debug_string, other_debug_string); + + empty_message_.Swap(&other_message); + EXPECT_EQ(debug_string, other_message.DebugString()); + EXPECT_EQ(other_debug_string, empty_message_.DebugString()); +} + +TEST_F(UnknownFieldSetTest, SwapWithSelf) { + const string debug_string = empty_message_.DebugString(); + EXPECT_GT(empty_message_.unknown_fields().field_count(), 0); + + empty_message_.Swap(&empty_message_); + EXPECT_GT(empty_message_.unknown_fields().field_count(), 0); + EXPECT_EQ(debug_string, empty_message_.DebugString()); +} + TEST_F(UnknownFieldSetTest, MergeFrom) { unittest::TestEmptyMessage source, destination; @@ -426,6 +450,72 @@ TEST_F(UnknownFieldSetTest, UnknownEnumValue) { } } +TEST_F(UnknownFieldSetTest, SpaceUsedExcludingSelf) { + { + // Make sure an unknown field set has zero space used until a field is + // actually added. + unittest::TestEmptyMessage empty_message; + const int empty_message_size = empty_message.SpaceUsed(); + UnknownFieldSet* unknown_fields = empty_message.mutable_unknown_fields(); + EXPECT_EQ(empty_message_size, empty_message.SpaceUsed()); + unknown_fields->AddField(1)->add_varint(0); + EXPECT_LT(empty_message_size, empty_message.SpaceUsed()); + } + { + // Test varints. + UnknownFieldSet unknown_fields; + UnknownField* field = unknown_fields.AddField(1); + const int base_size = unknown_fields.SpaceUsedExcludingSelf(); + for (int i = 0; i < 16; ++i) { + field->add_varint(i); + } + // Should just defer computation to the RepeatedField. + int expected_size = base_size + field->varint().SpaceUsedExcludingSelf(); + EXPECT_EQ(expected_size, unknown_fields.SpaceUsedExcludingSelf()); + } + { + // Test fixed32s. + UnknownFieldSet unknown_fields; + UnknownField* field = unknown_fields.AddField(1); + const int base_size = unknown_fields.SpaceUsedExcludingSelf(); + for (int i = 0; i < 16; ++i) { + field->add_fixed32(i); + } + int expected_size = base_size + field->fixed32().SpaceUsedExcludingSelf(); + EXPECT_EQ(expected_size, unknown_fields.SpaceUsedExcludingSelf()); + } + { + // Test fixed64s. + UnknownFieldSet unknown_fields; + UnknownField* field = unknown_fields.AddField(1); + const int base_size = unknown_fields.SpaceUsedExcludingSelf(); + for (int i = 0; i < 16; ++i) { + field->add_fixed64(i); + } + int expected_size = base_size + field->fixed64().SpaceUsedExcludingSelf(); + EXPECT_EQ(expected_size, unknown_fields.SpaceUsedExcludingSelf()); + } + { + // Test length-delimited types. + UnknownFieldSet unknown_fields; + UnknownField* field = unknown_fields.AddField(1); + const int base_size = unknown_fields.SpaceUsedExcludingSelf(); + for (int i = 0; i < 16; ++i) { + field->add_length_delimited()->assign("my length delimited string"); + } + int expected_size = base_size + + field->length_delimited().SpaceUsedExcludingSelf(); + EXPECT_EQ(expected_size, unknown_fields.SpaceUsedExcludingSelf()); + } +} + +TEST_F(UnknownFieldSetTest, SpaceUsed) { + UnknownFieldSet unknown_fields; + const int expected_size = sizeof(unknown_fields) + + unknown_fields.SpaceUsedExcludingSelf(); + EXPECT_EQ(expected_size, unknown_fields.SpaceUsed()); +} + } // namespace } // namespace protobuf } // namespace google -- cgit v1.2.3