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 // http://code.google.com/p/protobuf/ |
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. |
(...skipping 15 matching lines...) Expand all Loading... |
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
30 | 30 |
31 // Author: kenton@google.com (Kenton Varda) | 31 // Author: kenton@google.com (Kenton Varda) |
32 | 32 |
33 #include <string> | 33 #include <string> |
34 #include <iostream> | 34 #include <iostream> |
35 | 35 |
| 36 #include <google/protobuf/stubs/common.h> |
36 #include <google/protobuf/test_util_lite.h> | 37 #include <google/protobuf/test_util_lite.h> |
37 #include <google/protobuf/stubs/common.h> | 38 #include <google/protobuf/unittest_lite.pb.h> |
38 | 39 |
39 using namespace std; | 40 using namespace std; |
40 | 41 |
| 42 namespace { |
| 43 // Helper methods to test parsing merge behavior. |
| 44 void ExpectMessageMerged(const google::protobuf::unittest::TestAllTypesLite& mes
sage) { |
| 45 GOOGLE_CHECK(message.optional_int32() == 3); |
| 46 GOOGLE_CHECK(message.optional_int64() == 2); |
| 47 GOOGLE_CHECK(message.optional_string() == "hello"); |
| 48 } |
| 49 |
| 50 void AssignParsingMergeMessages( |
| 51 google::protobuf::unittest::TestAllTypesLite* msg1, |
| 52 google::protobuf::unittest::TestAllTypesLite* msg2, |
| 53 google::protobuf::unittest::TestAllTypesLite* msg3) { |
| 54 msg1->set_optional_int32(1); |
| 55 msg2->set_optional_int64(2); |
| 56 msg3->set_optional_int32(3); |
| 57 msg3->set_optional_string("hello"); |
| 58 } |
| 59 |
| 60 } // namespace |
| 61 |
41 int main(int argc, char* argv[]) { | 62 int main(int argc, char* argv[]) { |
42 string data, packed_data; | 63 string data, packed_data; |
43 | 64 |
44 { | 65 { |
45 protobuf_unittest::TestAllTypesLite message, message2, message3; | 66 protobuf_unittest::TestAllTypesLite message, message2, message3; |
46 google::protobuf::TestUtilLite::ExpectClear(message); | 67 google::protobuf::TestUtilLite::ExpectClear(message); |
47 google::protobuf::TestUtilLite::SetAllFields(&message); | 68 google::protobuf::TestUtilLite::SetAllFields(&message); |
48 message2.CopyFrom(message); | 69 message2.CopyFrom(message); |
49 data = message.SerializeAsString(); | 70 data = message.SerializeAsString(); |
50 message3.ParseFromString(data); | 71 message3.ParseFromString(data); |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
100 message3.ParseFromString(packed_extensions_data); | 121 message3.ParseFromString(packed_extensions_data); |
101 google::protobuf::TestUtilLite::ExpectPackedExtensionsSet(message); | 122 google::protobuf::TestUtilLite::ExpectPackedExtensionsSet(message); |
102 google::protobuf::TestUtilLite::ExpectPackedExtensionsSet(message2); | 123 google::protobuf::TestUtilLite::ExpectPackedExtensionsSet(message2); |
103 google::protobuf::TestUtilLite::ExpectPackedExtensionsSet(message3); | 124 google::protobuf::TestUtilLite::ExpectPackedExtensionsSet(message3); |
104 google::protobuf::TestUtilLite::ModifyPackedExtensions(&message); | 125 google::protobuf::TestUtilLite::ModifyPackedExtensions(&message); |
105 google::protobuf::TestUtilLite::ExpectPackedExtensionsModified(message); | 126 google::protobuf::TestUtilLite::ExpectPackedExtensionsModified(message); |
106 message.Clear(); | 127 message.Clear(); |
107 google::protobuf::TestUtilLite::ExpectPackedExtensionsClear(message); | 128 google::protobuf::TestUtilLite::ExpectPackedExtensionsClear(message); |
108 } | 129 } |
109 | 130 |
| 131 { |
| 132 // Test that if an optional or required message/group field appears multiple |
| 133 // times in the input, they need to be merged. |
| 134 google::protobuf::unittest::TestParsingMergeLite::RepeatedFieldsGenerator ge
nerator; |
| 135 google::protobuf::unittest::TestAllTypesLite* msg1; |
| 136 google::protobuf::unittest::TestAllTypesLite* msg2; |
| 137 google::protobuf::unittest::TestAllTypesLite* msg3; |
| 138 |
| 139 #define ASSIGN_REPEATED_FIELD(FIELD) \ |
| 140 msg1 = generator.add_##FIELD(); \ |
| 141 msg2 = generator.add_##FIELD(); \ |
| 142 msg3 = generator.add_##FIELD(); \ |
| 143 AssignParsingMergeMessages(msg1, msg2, msg3) |
| 144 |
| 145 ASSIGN_REPEATED_FIELD(field1); |
| 146 ASSIGN_REPEATED_FIELD(field2); |
| 147 ASSIGN_REPEATED_FIELD(field3); |
| 148 ASSIGN_REPEATED_FIELD(ext1); |
| 149 ASSIGN_REPEATED_FIELD(ext2); |
| 150 |
| 151 #undef ASSIGN_REPEATED_FIELD |
| 152 #define ASSIGN_REPEATED_GROUP(FIELD) \ |
| 153 msg1 = generator.add_##FIELD()->mutable_field1(); \ |
| 154 msg2 = generator.add_##FIELD()->mutable_field1(); \ |
| 155 msg3 = generator.add_##FIELD()->mutable_field1(); \ |
| 156 AssignParsingMergeMessages(msg1, msg2, msg3) |
| 157 |
| 158 ASSIGN_REPEATED_GROUP(group1); |
| 159 ASSIGN_REPEATED_GROUP(group2); |
| 160 |
| 161 #undef ASSIGN_REPEATED_GROUP |
| 162 |
| 163 string buffer; |
| 164 generator.SerializeToString(&buffer); |
| 165 google::protobuf::unittest::TestParsingMergeLite parsing_merge; |
| 166 parsing_merge.ParseFromString(buffer); |
| 167 |
| 168 // Required and optional fields should be merged. |
| 169 ExpectMessageMerged(parsing_merge.required_all_types()); |
| 170 ExpectMessageMerged(parsing_merge.optional_all_types()); |
| 171 ExpectMessageMerged( |
| 172 parsing_merge.optionalgroup().optional_group_all_types()); |
| 173 ExpectMessageMerged(parsing_merge.GetExtension( |
| 174 google::protobuf::unittest::TestParsingMergeLite::optional_ext)); |
| 175 |
| 176 // Repeated fields should not be merged. |
| 177 GOOGLE_CHECK(parsing_merge.repeated_all_types_size() == 3); |
| 178 GOOGLE_CHECK(parsing_merge.repeatedgroup_size() == 3); |
| 179 GOOGLE_CHECK(parsing_merge.ExtensionSize( |
| 180 google::protobuf::unittest::TestParsingMergeLite::repeated_ext) == 3); |
| 181 } |
| 182 |
110 cout << "PASS" << endl; | 183 cout << "PASS" << endl; |
111 return 0; | 184 return 0; |
112 } | 185 } |
OLD | NEW |