| OLD | NEW |
| 1 // Protocol Buffers - Google's data interchange format | 1 // Protocol Buffers - Google's data interchange format |
| 2 // Copyright 2008 Google Inc. All rights reserved. | 2 // Copyright 2008 Google Inc. All rights reserved. |
| 3 // http://code.google.com/p/protobuf/ | 3 // https://developers.google.com/protocol-buffers/ |
| 4 // | 4 // |
| 5 // Redistribution and use in source and binary forms, with or without | 5 // Redistribution and use in source and binary forms, with or without |
| 6 // modification, are permitted provided that the following conditions are | 6 // modification, are permitted provided that the following conditions are |
| 7 // met: | 7 // met: |
| 8 // | 8 // |
| 9 // * Redistributions of source code must retain the above copyright | 9 // * Redistributions of source code must retain the above copyright |
| 10 // notice, this list of conditions and the following disclaimer. | 10 // notice, this list of conditions and the following disclaimer. |
| 11 // * Redistributions in binary form must reproduce the above | 11 // * Redistributions in binary form must reproduce the above |
| 12 // copyright notice, this list of conditions and the following disclaimer | 12 // copyright notice, this list of conditions and the following disclaimer |
| 13 // in the documentation and/or other materials provided with the | 13 // in the documentation and/or other materials provided with the |
| (...skipping 22 matching lines...) Expand all Loading... |
| 36 // tests handling of unknown fields throughout the system. | 36 // tests handling of unknown fields throughout the system. |
| 37 | 37 |
| 38 #include <google/protobuf/unknown_field_set.h> | 38 #include <google/protobuf/unknown_field_set.h> |
| 39 #include <google/protobuf/descriptor.h> | 39 #include <google/protobuf/descriptor.h> |
| 40 #include <google/protobuf/io/coded_stream.h> | 40 #include <google/protobuf/io/coded_stream.h> |
| 41 #include <google/protobuf/io/zero_copy_stream_impl.h> | 41 #include <google/protobuf/io/zero_copy_stream_impl.h> |
| 42 #include <google/protobuf/wire_format.h> | 42 #include <google/protobuf/wire_format.h> |
| 43 #include <google/protobuf/unittest.pb.h> | 43 #include <google/protobuf/unittest.pb.h> |
| 44 #include <google/protobuf/test_util.h> | 44 #include <google/protobuf/test_util.h> |
| 45 | 45 |
| 46 #include <google/protobuf/stubs/callback.h> |
| 46 #include <google/protobuf/stubs/common.h> | 47 #include <google/protobuf/stubs/common.h> |
| 48 #include <google/protobuf/stubs/logging.h> |
| 49 #include <google/protobuf/stubs/mutex.h> |
| 47 #include <google/protobuf/testing/googletest.h> | 50 #include <google/protobuf/testing/googletest.h> |
| 48 #include <gtest/gtest.h> | 51 #include <gtest/gtest.h> |
| 49 #include <google/protobuf/stubs/stl_util.h> | 52 #include <google/protobuf/stubs/stl_util.h> |
| 50 | 53 |
| 51 namespace google { | 54 namespace google { |
| 52 namespace protobuf { | 55 namespace protobuf { |
| 53 | 56 |
| 54 using internal::WireFormat; | 57 using internal::WireFormat; |
| 55 | 58 |
| 56 class UnknownFieldSetTest : public testing::Test { | 59 class UnknownFieldSetTest : public testing::Test { |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 111 // All fields of TestAllTypes should be present, in numeric order (because | 114 // All fields of TestAllTypes should be present, in numeric order (because |
| 112 // that's the order we parsed them in). Fields that are not valid field | 115 // that's the order we parsed them in). Fields that are not valid field |
| 113 // numbers of TestAllTypes should NOT be present. | 116 // numbers of TestAllTypes should NOT be present. |
| 114 | 117 |
| 115 int pos = 0; | 118 int pos = 0; |
| 116 | 119 |
| 117 for (int i = 0; i < 1000; i++) { | 120 for (int i = 0; i < 1000; i++) { |
| 118 const FieldDescriptor* field = descriptor_->FindFieldByNumber(i); | 121 const FieldDescriptor* field = descriptor_->FindFieldByNumber(i); |
| 119 if (field != NULL) { | 122 if (field != NULL) { |
| 120 ASSERT_LT(pos, unknown_fields_->field_count()); | 123 ASSERT_LT(pos, unknown_fields_->field_count()); |
| 121 EXPECT_EQ(i, unknown_fields_->field(pos++).number()); | 124 // Do not check oneof field if it is not set. |
| 125 if (field->containing_oneof() == NULL) { |
| 126 EXPECT_EQ(i, unknown_fields_->field(pos++).number()); |
| 127 } else if (i == unknown_fields_->field(pos).number()) { |
| 128 pos++; |
| 129 } |
| 122 if (field->is_repeated()) { | 130 if (field->is_repeated()) { |
| 123 // Should have a second instance. | 131 // Should have a second instance. |
| 124 ASSERT_LT(pos, unknown_fields_->field_count()); | 132 ASSERT_LT(pos, unknown_fields_->field_count()); |
| 125 EXPECT_EQ(i, unknown_fields_->field(pos++).number()); | 133 EXPECT_EQ(i, unknown_fields_->field(pos++).number()); |
| 126 } | 134 } |
| 127 } | 135 } |
| 128 } | 136 } |
| 129 EXPECT_EQ(unknown_fields_->field_count(), pos); | 137 EXPECT_EQ(unknown_fields_->field_count(), pos); |
| 130 } | 138 } |
| 131 | 139 |
| (...skipping 338 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 470 EXPECT_EQ(repeated_field->number(), unknown_fields.field(1).number()); | 478 EXPECT_EQ(repeated_field->number(), unknown_fields.field(1).number()); |
| 471 ASSERT_EQ(UnknownField::TYPE_VARINT, unknown_fields.field(1).type()); | 479 ASSERT_EQ(UnknownField::TYPE_VARINT, unknown_fields.field(1).type()); |
| 472 EXPECT_EQ(4, unknown_fields.field(1).varint()); | 480 EXPECT_EQ(4, unknown_fields.field(1).varint()); |
| 473 | 481 |
| 474 EXPECT_EQ(repeated_field->number(), unknown_fields.field(2).number()); | 482 EXPECT_EQ(repeated_field->number(), unknown_fields.field(2).number()); |
| 475 ASSERT_EQ(UnknownField::TYPE_VARINT, unknown_fields.field(2).type()); | 483 ASSERT_EQ(UnknownField::TYPE_VARINT, unknown_fields.field(2).type()); |
| 476 EXPECT_EQ(6, unknown_fields.field(2).varint()); | 484 EXPECT_EQ(6, unknown_fields.field(2).varint()); |
| 477 } | 485 } |
| 478 } | 486 } |
| 479 | 487 |
| 488 TEST_F(UnknownFieldSetTest, SpaceUsedExcludingSelf) { |
| 489 UnknownFieldSet empty; |
| 490 empty.AddVarint(1, 0); |
| 491 EXPECT_EQ(sizeof(vector<UnknownField>) + sizeof(UnknownField), |
| 492 empty.SpaceUsedExcludingSelf()); |
| 493 } |
| 494 |
| 480 TEST_F(UnknownFieldSetTest, SpaceUsed) { | 495 TEST_F(UnknownFieldSetTest, SpaceUsed) { |
| 481 unittest::TestEmptyMessage empty_message; | 496 unittest::TestEmptyMessage empty_message; |
| 482 | 497 |
| 483 // Make sure an unknown field set has zero space used until a field is | 498 // Make sure an unknown field set has zero space used until a field is |
| 484 // actually added. | 499 // actually added. |
| 485 int base_size = empty_message.SpaceUsed(); | 500 int base_size = empty_message.SpaceUsed(); |
| 486 UnknownFieldSet* unknown_fields = empty_message.mutable_unknown_fields(); | 501 UnknownFieldSet* unknown_fields = empty_message.mutable_unknown_fields(); |
| 487 EXPECT_EQ(base_size, empty_message.SpaceUsed()); | 502 EXPECT_EQ(base_size, empty_message.SpaceUsed()); |
| 488 | 503 |
| 489 // Make sure each thing we add to the set increases the SpaceUsed(). | 504 // Make sure each thing we add to the set increases the SpaceUsed(). |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 585 static const int kFieldNumberToDelete5 = 6; | 600 static const int kFieldNumberToDelete5 = 6; |
| 586 static const int kExpectedFieldNumbers5[] = {1, 2, 3, 4, 5}; | 601 static const int kExpectedFieldNumbers5[] = {1, 2, 3, 4, 5}; |
| 587 CheckDeleteByNumber(MAKE_VECTOR(kTestFieldNumbers5), kFieldNumberToDelete5, | 602 CheckDeleteByNumber(MAKE_VECTOR(kTestFieldNumbers5), kFieldNumberToDelete5, |
| 588 MAKE_VECTOR(kExpectedFieldNumbers5)); | 603 MAKE_VECTOR(kExpectedFieldNumbers5)); |
| 589 } | 604 } |
| 590 #undef MAKE_VECTOR | 605 #undef MAKE_VECTOR |
| 591 } // namespace | 606 } // namespace |
| 592 | 607 |
| 593 } // namespace protobuf | 608 } // namespace protobuf |
| 594 } // namespace google | 609 } // namespace google |
| OLD | NEW |