Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame^] | 1 | // Protocol Buffers - Google's data interchange format |
| 2 | // Copyright 2008 Google Inc. All rights reserved. |
| 3 | // https://developers.google.com/protocol-buffers/ |
| 4 | // |
| 5 | // Redistribution and use in source and binary forms, with or without |
| 6 | // modification, are permitted provided that the following conditions are |
| 7 | // met: |
| 8 | // |
| 9 | // * Redistributions of source code must retain the above copyright |
| 10 | // notice, this list of conditions and the following disclaimer. |
| 11 | // * Redistributions in binary form must reproduce the above |
| 12 | // copyright notice, this list of conditions and the following disclaimer |
| 13 | // in the documentation and/or other materials provided with the |
| 14 | // distribution. |
| 15 | // * Neither the name of Google Inc. nor the names of its |
| 16 | // contributors may be used to endorse or promote products derived from |
| 17 | // this software without specific prior written permission. |
| 18 | // |
| 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 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. |
| 30 | |
| 31 | // Author: kenton@google.com (Kenton Varda) |
| 32 | |
| 33 | #include <string> |
| 34 | #include <iostream> |
| 35 | |
| 36 | #include <google/protobuf/stubs/logging.h> |
| 37 | #include <google/protobuf/stubs/common.h> |
| 38 | #include <google/protobuf/arena_test_util.h> |
| 39 | #include <google/protobuf/map_lite_unittest.pb.h> |
| 40 | #include <google/protobuf/map_lite_test_util.h> |
| 41 | #include <google/protobuf/test_util_lite.h> |
| 42 | #include <google/protobuf/unittest_lite.pb.h> |
| 43 | #include <google/protobuf/io/coded_stream.h> |
| 44 | #include <google/protobuf/io/zero_copy_stream_impl_lite.h> |
| 45 | #include <google/protobuf/wire_format_lite.h> |
| 46 | #include <google/protobuf/wire_format_lite_inl.h> |
| 47 | #include <google/protobuf/stubs/strutil.h> |
| 48 | |
| 49 | using namespace std; |
| 50 | |
| 51 | namespace { |
| 52 | // Helper methods to test parsing merge behavior. |
| 53 | void ExpectMessageMerged(const google::protobuf::unittest::TestAllTypesLite& message) { |
| 54 | GOOGLE_CHECK(message.optional_int32() == 3); |
| 55 | GOOGLE_CHECK(message.optional_int64() == 2); |
| 56 | GOOGLE_CHECK(message.optional_string() == "hello"); |
| 57 | } |
| 58 | |
| 59 | void AssignParsingMergeMessages( |
| 60 | google::protobuf::unittest::TestAllTypesLite* msg1, |
| 61 | google::protobuf::unittest::TestAllTypesLite* msg2, |
| 62 | google::protobuf::unittest::TestAllTypesLite* msg3) { |
| 63 | msg1->set_optional_int32(1); |
| 64 | msg2->set_optional_int64(2); |
| 65 | msg3->set_optional_int32(3); |
| 66 | msg3->set_optional_string("hello"); |
| 67 | } |
| 68 | |
| 69 | void SetAllTypesInEmptyMessageUnknownFields( |
| 70 | google::protobuf::unittest::TestEmptyMessageLite* empty_message) { |
| 71 | protobuf_unittest::TestAllTypesLite message; |
| 72 | google::protobuf::TestUtilLite::ExpectClear(message); |
| 73 | google::protobuf::TestUtilLite::SetAllFields(&message); |
| 74 | string data = message.SerializeAsString(); |
| 75 | empty_message->ParseFromString(data); |
| 76 | } |
| 77 | |
| 78 | void SetSomeTypesInEmptyMessageUnknownFields( |
| 79 | google::protobuf::unittest::TestEmptyMessageLite* empty_message) { |
| 80 | protobuf_unittest::TestAllTypesLite message; |
| 81 | google::protobuf::TestUtilLite::ExpectClear(message); |
| 82 | message.set_optional_int32(101); |
| 83 | message.set_optional_int64(102); |
| 84 | message.set_optional_uint32(103); |
| 85 | message.set_optional_uint64(104); |
| 86 | string data = message.SerializeAsString(); |
| 87 | empty_message->ParseFromString(data); |
| 88 | } |
| 89 | |
| 90 | } // namespace |
| 91 | |
| 92 | #define EXPECT_TRUE GOOGLE_CHECK |
| 93 | #define ASSERT_TRUE GOOGLE_CHECK |
| 94 | #define EXPECT_FALSE(COND) GOOGLE_CHECK(!(COND)) |
| 95 | #define EXPECT_EQ GOOGLE_CHECK_EQ |
| 96 | #define ASSERT_EQ GOOGLE_CHECK_EQ |
| 97 | |
| 98 | int main(int argc, char* argv[]) { |
| 99 | string data, data2, packed_data; |
| 100 | |
| 101 | { |
| 102 | protobuf_unittest::TestAllTypesLite message, message2, message3; |
| 103 | google::protobuf::TestUtilLite::ExpectClear(message); |
| 104 | google::protobuf::TestUtilLite::SetAllFields(&message); |
| 105 | message2.CopyFrom(message); |
| 106 | data = message.SerializeAsString(); |
| 107 | message3.ParseFromString(data); |
| 108 | google::protobuf::TestUtilLite::ExpectAllFieldsSet(message); |
| 109 | google::protobuf::TestUtilLite::ExpectAllFieldsSet(message2); |
| 110 | google::protobuf::TestUtilLite::ExpectAllFieldsSet(message3); |
| 111 | google::protobuf::TestUtilLite::ModifyRepeatedFields(&message); |
| 112 | google::protobuf::TestUtilLite::ExpectRepeatedFieldsModified(message); |
| 113 | message.Clear(); |
| 114 | google::protobuf::TestUtilLite::ExpectClear(message); |
| 115 | } |
| 116 | |
| 117 | { |
| 118 | protobuf_unittest::TestAllExtensionsLite message, message2, message3; |
| 119 | google::protobuf::TestUtilLite::ExpectExtensionsClear(message); |
| 120 | google::protobuf::TestUtilLite::SetAllExtensions(&message); |
| 121 | message2.CopyFrom(message); |
| 122 | string extensions_data = message.SerializeAsString(); |
| 123 | message3.ParseFromString(extensions_data); |
| 124 | google::protobuf::TestUtilLite::ExpectAllExtensionsSet(message); |
| 125 | google::protobuf::TestUtilLite::ExpectAllExtensionsSet(message2); |
| 126 | google::protobuf::TestUtilLite::ExpectAllExtensionsSet(message3); |
| 127 | google::protobuf::TestUtilLite::ModifyRepeatedExtensions(&message); |
| 128 | google::protobuf::TestUtilLite::ExpectRepeatedExtensionsModified(message); |
| 129 | message.Clear(); |
| 130 | google::protobuf::TestUtilLite::ExpectExtensionsClear(message); |
| 131 | } |
| 132 | |
| 133 | { |
| 134 | protobuf_unittest::TestPackedTypesLite message, message2, message3; |
| 135 | google::protobuf::TestUtilLite::ExpectPackedClear(message); |
| 136 | google::protobuf::TestUtilLite::SetPackedFields(&message); |
| 137 | message2.CopyFrom(message); |
| 138 | packed_data = message.SerializeAsString(); |
| 139 | message3.ParseFromString(packed_data); |
| 140 | google::protobuf::TestUtilLite::ExpectPackedFieldsSet(message); |
| 141 | google::protobuf::TestUtilLite::ExpectPackedFieldsSet(message2); |
| 142 | google::protobuf::TestUtilLite::ExpectPackedFieldsSet(message3); |
| 143 | google::protobuf::TestUtilLite::ModifyPackedFields(&message); |
| 144 | google::protobuf::TestUtilLite::ExpectPackedFieldsModified(message); |
| 145 | message.Clear(); |
| 146 | google::protobuf::TestUtilLite::ExpectPackedClear(message); |
| 147 | } |
| 148 | |
| 149 | { |
| 150 | protobuf_unittest::TestPackedExtensionsLite message, message2, message3; |
| 151 | google::protobuf::TestUtilLite::ExpectPackedExtensionsClear(message); |
| 152 | google::protobuf::TestUtilLite::SetPackedExtensions(&message); |
| 153 | message2.CopyFrom(message); |
| 154 | string packed_extensions_data = message.SerializeAsString(); |
| 155 | GOOGLE_CHECK(packed_extensions_data == packed_data); |
| 156 | message3.ParseFromString(packed_extensions_data); |
| 157 | google::protobuf::TestUtilLite::ExpectPackedExtensionsSet(message); |
| 158 | google::protobuf::TestUtilLite::ExpectPackedExtensionsSet(message2); |
| 159 | google::protobuf::TestUtilLite::ExpectPackedExtensionsSet(message3); |
| 160 | google::protobuf::TestUtilLite::ModifyPackedExtensions(&message); |
| 161 | google::protobuf::TestUtilLite::ExpectPackedExtensionsModified(message); |
| 162 | message.Clear(); |
| 163 | google::protobuf::TestUtilLite::ExpectPackedExtensionsClear(message); |
| 164 | } |
| 165 | |
| 166 | { |
| 167 | // Test that if an optional or required message/group field appears multiple |
| 168 | // times in the input, they need to be merged. |
| 169 | google::protobuf::unittest::TestParsingMergeLite::RepeatedFieldsGenerator generator; |
| 170 | google::protobuf::unittest::TestAllTypesLite* msg1; |
| 171 | google::protobuf::unittest::TestAllTypesLite* msg2; |
| 172 | google::protobuf::unittest::TestAllTypesLite* msg3; |
| 173 | |
| 174 | #define ASSIGN_REPEATED_FIELD(FIELD) \ |
| 175 | msg1 = generator.add_##FIELD(); \ |
| 176 | msg2 = generator.add_##FIELD(); \ |
| 177 | msg3 = generator.add_##FIELD(); \ |
| 178 | AssignParsingMergeMessages(msg1, msg2, msg3) |
| 179 | |
| 180 | ASSIGN_REPEATED_FIELD(field1); |
| 181 | ASSIGN_REPEATED_FIELD(field2); |
| 182 | ASSIGN_REPEATED_FIELD(field3); |
| 183 | ASSIGN_REPEATED_FIELD(ext1); |
| 184 | ASSIGN_REPEATED_FIELD(ext2); |
| 185 | |
| 186 | #undef ASSIGN_REPEATED_FIELD |
| 187 | #define ASSIGN_REPEATED_GROUP(FIELD) \ |
| 188 | msg1 = generator.add_##FIELD()->mutable_field1(); \ |
| 189 | msg2 = generator.add_##FIELD()->mutable_field1(); \ |
| 190 | msg3 = generator.add_##FIELD()->mutable_field1(); \ |
| 191 | AssignParsingMergeMessages(msg1, msg2, msg3) |
| 192 | |
| 193 | ASSIGN_REPEATED_GROUP(group1); |
| 194 | ASSIGN_REPEATED_GROUP(group2); |
| 195 | |
| 196 | #undef ASSIGN_REPEATED_GROUP |
| 197 | |
| 198 | string buffer; |
| 199 | generator.SerializeToString(&buffer); |
| 200 | google::protobuf::unittest::TestParsingMergeLite parsing_merge; |
| 201 | parsing_merge.ParseFromString(buffer); |
| 202 | |
| 203 | // Required and optional fields should be merged. |
| 204 | ExpectMessageMerged(parsing_merge.required_all_types()); |
| 205 | ExpectMessageMerged(parsing_merge.optional_all_types()); |
| 206 | ExpectMessageMerged( |
| 207 | parsing_merge.optionalgroup().optional_group_all_types()); |
| 208 | ExpectMessageMerged(parsing_merge.GetExtension( |
| 209 | google::protobuf::unittest::TestParsingMergeLite::optional_ext)); |
| 210 | |
| 211 | // Repeated fields should not be merged. |
| 212 | GOOGLE_CHECK(parsing_merge.repeated_all_types_size() == 3); |
| 213 | GOOGLE_CHECK(parsing_merge.repeatedgroup_size() == 3); |
| 214 | GOOGLE_CHECK(parsing_merge.ExtensionSize( |
| 215 | google::protobuf::unittest::TestParsingMergeLite::repeated_ext) == 3); |
| 216 | } |
| 217 | |
| 218 | // Test unknown fields support for lite messages. |
| 219 | { |
| 220 | protobuf_unittest::TestAllTypesLite message, message2; |
| 221 | protobuf_unittest::TestEmptyMessageLite empty_message; |
| 222 | google::protobuf::TestUtilLite::ExpectClear(message); |
| 223 | google::protobuf::TestUtilLite::SetAllFields(&message); |
| 224 | data = message.SerializeAsString(); |
| 225 | empty_message.ParseFromString(data); |
| 226 | data.clear(); |
| 227 | data = empty_message.SerializeAsString(); |
| 228 | message2.ParseFromString(data); |
| 229 | data = message2.SerializeAsString(); |
| 230 | google::protobuf::TestUtilLite::ExpectAllFieldsSet(message2); |
| 231 | message.Clear(); |
| 232 | google::protobuf::TestUtilLite::ExpectClear(message); |
| 233 | } |
| 234 | |
| 235 | { |
| 236 | protobuf_unittest::TestAllExtensionsLite message, message2; |
| 237 | protobuf_unittest::TestEmptyMessageLite empty_message; |
| 238 | google::protobuf::TestUtilLite::ExpectExtensionsClear(message); |
| 239 | google::protobuf::TestUtilLite::SetAllExtensions(&message); |
| 240 | data = message.SerializeAsString(); |
| 241 | empty_message.ParseFromString(data); |
| 242 | data.clear(); |
| 243 | data = empty_message.SerializeAsString(); |
| 244 | message2.ParseFromString(data); |
| 245 | data = message2.SerializeAsString(); |
| 246 | google::protobuf::TestUtilLite::ExpectAllExtensionsSet(message2); |
| 247 | message.Clear(); |
| 248 | google::protobuf::TestUtilLite::ExpectExtensionsClear(message); |
| 249 | } |
| 250 | |
| 251 | { |
| 252 | protobuf_unittest::TestPackedTypesLite message, message2; |
| 253 | protobuf_unittest::TestEmptyMessageLite empty_message; |
| 254 | google::protobuf::TestUtilLite::ExpectPackedClear(message); |
| 255 | google::protobuf::TestUtilLite::SetPackedFields(&message); |
| 256 | data = message.SerializeAsString(); |
| 257 | empty_message.ParseFromString(data); |
| 258 | data.clear(); |
| 259 | data = empty_message.SerializeAsString(); |
| 260 | message2.ParseFromString(data); |
| 261 | data = message2.SerializeAsString(); |
| 262 | google::protobuf::TestUtilLite::ExpectPackedFieldsSet(message2); |
| 263 | message.Clear(); |
| 264 | google::protobuf::TestUtilLite::ExpectPackedClear(message); |
| 265 | } |
| 266 | |
| 267 | { |
| 268 | protobuf_unittest::TestPackedExtensionsLite message, message2; |
| 269 | protobuf_unittest::TestEmptyMessageLite empty_message; |
| 270 | google::protobuf::TestUtilLite::ExpectPackedExtensionsClear(message); |
| 271 | google::protobuf::TestUtilLite::SetPackedExtensions(&message); |
| 272 | data = message.SerializeAsString(); |
| 273 | empty_message.ParseFromString(data); |
| 274 | data.clear(); |
| 275 | data = empty_message.SerializeAsString(); |
| 276 | message2.ParseFromString(data); |
| 277 | data = message2.SerializeAsString(); |
| 278 | google::protobuf::TestUtilLite::ExpectPackedExtensionsSet(message2); |
| 279 | message.Clear(); |
| 280 | google::protobuf::TestUtilLite::ExpectPackedExtensionsClear(message); |
| 281 | } |
| 282 | |
| 283 | { |
| 284 | // Test Unknown fields swap |
| 285 | protobuf_unittest::TestEmptyMessageLite empty_message, empty_message2; |
| 286 | SetAllTypesInEmptyMessageUnknownFields(&empty_message); |
| 287 | SetSomeTypesInEmptyMessageUnknownFields(&empty_message2); |
| 288 | data = empty_message.SerializeAsString(); |
| 289 | data2 = empty_message2.SerializeAsString(); |
| 290 | empty_message.Swap(&empty_message2); |
| 291 | GOOGLE_CHECK_EQ(data, empty_message2.SerializeAsString()); |
| 292 | GOOGLE_CHECK_EQ(data2, empty_message.SerializeAsString()); |
| 293 | } |
| 294 | |
| 295 | { |
| 296 | // Test unknown fields swap with self |
| 297 | protobuf_unittest::TestEmptyMessageLite empty_message; |
| 298 | SetAllTypesInEmptyMessageUnknownFields(&empty_message); |
| 299 | data = empty_message.SerializeAsString(); |
| 300 | empty_message.Swap(&empty_message); |
| 301 | GOOGLE_CHECK_EQ(data, empty_message.SerializeAsString()); |
| 302 | } |
| 303 | |
| 304 | { |
| 305 | // Test MergeFrom with unknown fields |
| 306 | protobuf_unittest::TestAllTypesLite message, message2; |
| 307 | protobuf_unittest::TestEmptyMessageLite empty_message, empty_message2; |
| 308 | message.set_optional_int32(101); |
| 309 | message.add_repeated_int32(201); |
| 310 | message.set_optional_nested_enum(google::protobuf::unittest::TestAllTypesLite::BAZ); |
| 311 | message2.set_optional_int64(102); |
| 312 | message2.add_repeated_int64(202); |
| 313 | message2.set_optional_foreign_enum(google::protobuf::unittest::FOREIGN_LITE_BAZ); |
| 314 | |
| 315 | data = message.SerializeAsString(); |
| 316 | empty_message.ParseFromString(data); |
| 317 | data = message2.SerializeAsString(); |
| 318 | empty_message2.ParseFromString(data); |
| 319 | message.MergeFrom(message2); |
| 320 | empty_message.MergeFrom(empty_message2); |
| 321 | |
| 322 | data = empty_message.SerializeAsString(); |
| 323 | message2.ParseFromString(data); |
| 324 | // We do not compare the serialized output of a normal message and a lite |
| 325 | // message because the order of fields do not match. We convert lite message |
| 326 | // back into normal message, then compare. |
| 327 | GOOGLE_CHECK_EQ(message.SerializeAsString(), message2.SerializeAsString()); |
| 328 | } |
| 329 | |
| 330 | { |
| 331 | // Test unknown enum value |
| 332 | protobuf_unittest::TestAllTypesLite message; |
| 333 | string buffer; |
| 334 | { |
| 335 | google::protobuf::io::StringOutputStream output_stream(&buffer); |
| 336 | google::protobuf::io::CodedOutputStream coded_output(&output_stream); |
| 337 | google::protobuf::internal::WireFormatLite::WriteTag( |
| 338 | protobuf_unittest::TestAllTypesLite::kOptionalNestedEnumFieldNumber, |
| 339 | google::protobuf::internal::WireFormatLite::WIRETYPE_VARINT, &coded_output); |
| 340 | coded_output.WriteVarint32(10); |
| 341 | google::protobuf::internal::WireFormatLite::WriteTag( |
| 342 | protobuf_unittest::TestAllTypesLite::kRepeatedNestedEnumFieldNumber, |
| 343 | google::protobuf::internal::WireFormatLite::WIRETYPE_VARINT, &coded_output); |
| 344 | coded_output.WriteVarint32(20); |
| 345 | } |
| 346 | message.ParseFromString(buffer); |
| 347 | data = message.SerializeAsString(); |
| 348 | GOOGLE_CHECK_EQ(data, buffer); |
| 349 | } |
| 350 | |
| 351 | { |
| 352 | // Test Clear with unknown fields |
| 353 | protobuf_unittest::TestEmptyMessageLite empty_message; |
| 354 | SetAllTypesInEmptyMessageUnknownFields(&empty_message); |
| 355 | empty_message.Clear(); |
| 356 | GOOGLE_CHECK_EQ(0, empty_message.unknown_fields().size()); |
| 357 | } |
| 358 | |
| 359 | // Tests for map lite ============================================= |
| 360 | |
| 361 | { |
| 362 | // Accessors |
| 363 | protobuf_unittest::TestMapLite message; |
| 364 | |
| 365 | google::protobuf::MapLiteTestUtil::SetMapFields(&message); |
| 366 | google::protobuf::MapLiteTestUtil::ExpectMapFieldsSet(message); |
| 367 | |
| 368 | google::protobuf::MapLiteTestUtil::ModifyMapFields(&message); |
| 369 | google::protobuf::MapLiteTestUtil::ExpectMapFieldsModified(message); |
| 370 | } |
| 371 | |
| 372 | { |
| 373 | // SetMapFieldsInitialized |
| 374 | protobuf_unittest::TestMapLite message; |
| 375 | |
| 376 | google::protobuf::MapLiteTestUtil::SetMapFieldsInitialized(&message); |
| 377 | google::protobuf::MapLiteTestUtil::ExpectMapFieldsSetInitialized(message); |
| 378 | } |
| 379 | |
| 380 | { |
| 381 | // Clear |
| 382 | protobuf_unittest::TestMapLite message; |
| 383 | |
| 384 | google::protobuf::MapLiteTestUtil::SetMapFields(&message); |
| 385 | message.Clear(); |
| 386 | google::protobuf::MapLiteTestUtil::ExpectClear(message); |
| 387 | } |
| 388 | |
| 389 | { |
| 390 | // ClearMessageMap |
| 391 | protobuf_unittest::TestMessageMapLite message; |
| 392 | |
| 393 | // Creates a TestAllTypes with default value |
| 394 | google::protobuf::TestUtilLite::ExpectClear( |
| 395 | (*message.mutable_map_int32_message())[0]); |
| 396 | } |
| 397 | |
| 398 | { |
| 399 | // CopyFrom |
| 400 | protobuf_unittest::TestMapLite message1, message2; |
| 401 | |
| 402 | google::protobuf::MapLiteTestUtil::SetMapFields(&message1); |
| 403 | message2.CopyFrom(message1); |
| 404 | google::protobuf::MapLiteTestUtil::ExpectMapFieldsSet(message2); |
| 405 | |
| 406 | // Copying from self should be a no-op. |
| 407 | message2.CopyFrom(message2); |
| 408 | google::protobuf::MapLiteTestUtil::ExpectMapFieldsSet(message2); |
| 409 | } |
| 410 | |
| 411 | { |
| 412 | // CopyFromMessageMap |
| 413 | protobuf_unittest::TestMessageMapLite message1, message2; |
| 414 | |
| 415 | (*message1.mutable_map_int32_message())[0].add_repeated_int32(100); |
| 416 | (*message2.mutable_map_int32_message())[0].add_repeated_int32(101); |
| 417 | |
| 418 | message1.CopyFrom(message2); |
| 419 | |
| 420 | // Checks repeated field is overwritten. |
| 421 | EXPECT_EQ(1, message1.map_int32_message().at(0).repeated_int32_size()); |
| 422 | EXPECT_EQ(101, message1.map_int32_message().at(0).repeated_int32(0)); |
| 423 | } |
| 424 | |
| 425 | { |
| 426 | // SwapWithEmpty |
| 427 | protobuf_unittest::TestMapLite message1, message2; |
| 428 | |
| 429 | google::protobuf::MapLiteTestUtil::SetMapFields(&message1); |
| 430 | google::protobuf::MapLiteTestUtil::ExpectMapFieldsSet(message1); |
| 431 | google::protobuf::MapLiteTestUtil::ExpectClear(message2); |
| 432 | |
| 433 | message1.Swap(&message2); |
| 434 | google::protobuf::MapLiteTestUtil::ExpectMapFieldsSet(message2); |
| 435 | google::protobuf::MapLiteTestUtil::ExpectClear(message1); |
| 436 | } |
| 437 | |
| 438 | { |
| 439 | // SwapWithSelf |
| 440 | protobuf_unittest::TestMapLite message; |
| 441 | |
| 442 | google::protobuf::MapLiteTestUtil::SetMapFields(&message); |
| 443 | google::protobuf::MapLiteTestUtil::ExpectMapFieldsSet(message); |
| 444 | |
| 445 | message.Swap(&message); |
| 446 | google::protobuf::MapLiteTestUtil::ExpectMapFieldsSet(message); |
| 447 | } |
| 448 | |
| 449 | { |
| 450 | // SwapWithOther |
| 451 | protobuf_unittest::TestMapLite message1, message2; |
| 452 | |
| 453 | google::protobuf::MapLiteTestUtil::SetMapFields(&message1); |
| 454 | google::protobuf::MapLiteTestUtil::SetMapFields(&message2); |
| 455 | google::protobuf::MapLiteTestUtil::ModifyMapFields(&message2); |
| 456 | |
| 457 | message1.Swap(&message2); |
| 458 | google::protobuf::MapLiteTestUtil::ExpectMapFieldsModified(message1); |
| 459 | google::protobuf::MapLiteTestUtil::ExpectMapFieldsSet(message2); |
| 460 | } |
| 461 | |
| 462 | { |
| 463 | // CopyConstructor |
| 464 | protobuf_unittest::TestMapLite message1; |
| 465 | google::protobuf::MapLiteTestUtil::SetMapFields(&message1); |
| 466 | |
| 467 | protobuf_unittest::TestMapLite message2(message1); |
| 468 | google::protobuf::MapLiteTestUtil::ExpectMapFieldsSet(message2); |
| 469 | } |
| 470 | |
| 471 | { |
| 472 | // CopyAssignmentOperator |
| 473 | protobuf_unittest::TestMapLite message1; |
| 474 | google::protobuf::MapLiteTestUtil::SetMapFields(&message1); |
| 475 | |
| 476 | protobuf_unittest::TestMapLite message2; |
| 477 | message2 = message1; |
| 478 | google::protobuf::MapLiteTestUtil::ExpectMapFieldsSet(message2); |
| 479 | |
| 480 | // Make sure that self-assignment does something sane. |
| 481 | message2.operator=(message2); |
| 482 | google::protobuf::MapLiteTestUtil::ExpectMapFieldsSet(message2); |
| 483 | } |
| 484 | |
| 485 | { |
| 486 | // NonEmptyMergeFrom |
| 487 | protobuf_unittest::TestMapLite message1, message2; |
| 488 | |
| 489 | google::protobuf::MapLiteTestUtil::SetMapFields(&message1); |
| 490 | |
| 491 | // This field will test merging into an empty spot. |
| 492 | (*message2.mutable_map_int32_int32())[1] = 1; |
| 493 | message1.mutable_map_int32_int32()->erase(1); |
| 494 | |
| 495 | // This tests overwriting. |
| 496 | (*message2.mutable_map_int32_double())[1] = 1; |
| 497 | (*message1.mutable_map_int32_double())[1] = 2; |
| 498 | |
| 499 | message1.MergeFrom(message2); |
| 500 | google::protobuf::MapLiteTestUtil::ExpectMapFieldsSet(message1); |
| 501 | } |
| 502 | |
| 503 | { |
| 504 | // MergeFromMessageMap |
| 505 | protobuf_unittest::TestMessageMapLite message1, message2; |
| 506 | |
| 507 | (*message1.mutable_map_int32_message())[0].add_repeated_int32(100); |
| 508 | (*message2.mutable_map_int32_message())[0].add_repeated_int32(101); |
| 509 | |
| 510 | message1.MergeFrom(message2); |
| 511 | |
| 512 | // Checks repeated field is overwritten. |
| 513 | EXPECT_EQ(1, message1.map_int32_message().at(0).repeated_int32_size()); |
| 514 | EXPECT_EQ(101, message1.map_int32_message().at(0).repeated_int32(0)); |
| 515 | } |
| 516 | |
| 517 | { |
| 518 | // Test the generated SerializeWithCachedSizesToArray() |
| 519 | protobuf_unittest::TestMapLite message1, message2; |
| 520 | string data; |
| 521 | google::protobuf::MapLiteTestUtil::SetMapFields(&message1); |
| 522 | int size = message1.ByteSize(); |
| 523 | data.resize(size); |
| 524 | ::google::protobuf::uint8* start = reinterpret_cast< ::google::protobuf::uint8*>(::google::protobuf::string_as_array(&data)); |
| 525 | ::google::protobuf::uint8* end = message1.SerializeWithCachedSizesToArray(start); |
| 526 | EXPECT_EQ(size, end - start); |
| 527 | EXPECT_TRUE(message2.ParseFromString(data)); |
| 528 | google::protobuf::MapLiteTestUtil::ExpectMapFieldsSet(message2); |
| 529 | } |
| 530 | |
| 531 | { |
| 532 | // Test the generated SerializeWithCachedSizes() |
| 533 | protobuf_unittest::TestMapLite message1, message2; |
| 534 | google::protobuf::MapLiteTestUtil::SetMapFields(&message1); |
| 535 | int size = message1.ByteSize(); |
| 536 | string data; |
| 537 | data.resize(size); |
| 538 | { |
| 539 | // Allow the output stream to buffer only one byte at a time. |
| 540 | google::protobuf::io::ArrayOutputStream array_stream( |
| 541 | ::google::protobuf::string_as_array(&data), size, 1); |
| 542 | google::protobuf::io::CodedOutputStream output_stream(&array_stream); |
| 543 | message1.SerializeWithCachedSizes(&output_stream); |
| 544 | EXPECT_FALSE(output_stream.HadError()); |
| 545 | EXPECT_EQ(size, output_stream.ByteCount()); |
| 546 | } |
| 547 | EXPECT_TRUE(message2.ParseFromString(data)); |
| 548 | google::protobuf::MapLiteTestUtil::ExpectMapFieldsSet(message2); |
| 549 | } |
| 550 | |
| 551 | |
| 552 | { |
| 553 | // Proto2UnknownEnum |
| 554 | protobuf_unittest::TestEnumMapPlusExtraLite from; |
| 555 | (*from.mutable_known_map_field())[0] = |
| 556 | protobuf_unittest::E_PROTO2_MAP_ENUM_FOO_LITE; |
| 557 | (*from.mutable_unknown_map_field())[0] = |
| 558 | protobuf_unittest::E_PROTO2_MAP_ENUM_EXTRA_LITE; |
| 559 | string data; |
| 560 | from.SerializeToString(&data); |
| 561 | |
| 562 | protobuf_unittest::TestEnumMapLite to; |
| 563 | EXPECT_TRUE(to.ParseFromString(data)); |
| 564 | EXPECT_EQ(0, to.unknown_map_field().size()); |
| 565 | EXPECT_FALSE(to.mutable_unknown_fields()->empty()); |
| 566 | EXPECT_EQ(1, to.known_map_field().size()); |
| 567 | EXPECT_EQ(protobuf_unittest::PROTO2_MAP_ENUM_FOO_LITE, |
| 568 | to.known_map_field().at(0)); |
| 569 | |
| 570 | data.clear(); |
| 571 | from.Clear(); |
| 572 | to.SerializeToString(&data); |
| 573 | EXPECT_TRUE(from.ParseFromString(data)); |
| 574 | EXPECT_EQ(1, from.known_map_field().size()); |
| 575 | EXPECT_EQ(protobuf_unittest::E_PROTO2_MAP_ENUM_FOO_LITE, |
| 576 | from.known_map_field().at(0)); |
| 577 | EXPECT_EQ(1, from.unknown_map_field().size()); |
| 578 | EXPECT_EQ(protobuf_unittest::E_PROTO2_MAP_ENUM_EXTRA_LITE, |
| 579 | from.unknown_map_field().at(0)); |
| 580 | } |
| 581 | |
| 582 | { |
| 583 | // StandardWireFormat |
| 584 | protobuf_unittest::TestMapLite message; |
| 585 | string data = "\x0A\x04\x08\x01\x10\x01"; |
| 586 | |
| 587 | EXPECT_TRUE(message.ParseFromString(data)); |
| 588 | EXPECT_EQ(1, message.map_int32_int32().size()); |
| 589 | EXPECT_EQ(1, message.map_int32_int32().at(1)); |
| 590 | } |
| 591 | |
| 592 | { |
| 593 | // UnorderedWireFormat |
| 594 | protobuf_unittest::TestMapLite message; |
| 595 | |
| 596 | // put value before key in wire format |
| 597 | string data = "\x0A\x04\x10\x01\x08\x02"; |
| 598 | |
| 599 | EXPECT_TRUE(message.ParseFromString(data)); |
| 600 | EXPECT_EQ(1, message.map_int32_int32().size()); |
| 601 | EXPECT_EQ(1, message.map_int32_int32().at(2)); |
| 602 | } |
| 603 | |
| 604 | { |
| 605 | // DuplicatedKeyWireFormat |
| 606 | protobuf_unittest::TestMapLite message; |
| 607 | |
| 608 | // Two key fields in wire format |
| 609 | string data = "\x0A\x06\x08\x01\x08\x02\x10\x01"; |
| 610 | |
| 611 | EXPECT_TRUE(message.ParseFromString(data)); |
| 612 | EXPECT_EQ(1, message.map_int32_int32().size()); |
| 613 | EXPECT_EQ(1, message.map_int32_int32().at(2)); |
| 614 | } |
| 615 | |
| 616 | { |
| 617 | // DuplicatedValueWireFormat |
| 618 | protobuf_unittest::TestMapLite message; |
| 619 | |
| 620 | // Two value fields in wire format |
| 621 | string data = "\x0A\x06\x08\x01\x10\x01\x10\x02"; |
| 622 | |
| 623 | EXPECT_TRUE(message.ParseFromString(data)); |
| 624 | EXPECT_EQ(1, message.map_int32_int32().size()); |
| 625 | EXPECT_EQ(2, message.map_int32_int32().at(1)); |
| 626 | } |
| 627 | |
| 628 | { |
| 629 | // MissedKeyWireFormat |
| 630 | protobuf_unittest::TestMapLite message; |
| 631 | |
| 632 | // No key field in wire format |
| 633 | string data = "\x0A\x02\x10\x01"; |
| 634 | |
| 635 | EXPECT_TRUE(message.ParseFromString(data)); |
| 636 | EXPECT_EQ(1, message.map_int32_int32().size()); |
| 637 | EXPECT_EQ(1, message.map_int32_int32().at(0)); |
| 638 | } |
| 639 | |
| 640 | { |
| 641 | // MissedValueWireFormat |
| 642 | protobuf_unittest::TestMapLite message; |
| 643 | |
| 644 | // No value field in wire format |
| 645 | string data = "\x0A\x02\x08\x01"; |
| 646 | |
| 647 | EXPECT_TRUE(message.ParseFromString(data)); |
| 648 | EXPECT_EQ(1, message.map_int32_int32().size()); |
| 649 | EXPECT_EQ(0, message.map_int32_int32().at(1)); |
| 650 | } |
| 651 | |
| 652 | { |
| 653 | // UnknownFieldWireFormat |
| 654 | protobuf_unittest::TestMapLite message; |
| 655 | |
| 656 | // Unknown field in wire format |
| 657 | string data = "\x0A\x06\x08\x02\x10\x03\x18\x01"; |
| 658 | |
| 659 | EXPECT_TRUE(message.ParseFromString(data)); |
| 660 | EXPECT_EQ(1, message.map_int32_int32().size()); |
| 661 | EXPECT_EQ(3, message.map_int32_int32().at(2)); |
| 662 | } |
| 663 | |
| 664 | { |
| 665 | // CorruptedWireFormat |
| 666 | protobuf_unittest::TestMapLite message; |
| 667 | |
| 668 | // corrupted data in wire format |
| 669 | string data = "\x0A\x06\x08\x02\x11\x03"; |
| 670 | |
| 671 | EXPECT_FALSE(message.ParseFromString(data)); |
| 672 | } |
| 673 | |
| 674 | { |
| 675 | // IsInitialized |
| 676 | protobuf_unittest::TestRequiredMessageMapLite map_message; |
| 677 | |
| 678 | // Add an uninitialized message. |
| 679 | (*map_message.mutable_map_field())[0]; |
| 680 | EXPECT_FALSE(map_message.IsInitialized()); |
| 681 | |
| 682 | // Initialize uninitialized message |
| 683 | (*map_message.mutable_map_field())[0].set_a(0); |
| 684 | (*map_message.mutable_map_field())[0].set_b(0); |
| 685 | (*map_message.mutable_map_field())[0].set_c(0); |
| 686 | EXPECT_TRUE(map_message.IsInitialized()); |
| 687 | } |
| 688 | |
| 689 | std::cout << "PASS" << std::endl; |
| 690 | return 0; |
| 691 | } |