| 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 // https://developers.google.com/protocol-buffers/ | 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. |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 46 | 46 |
| 47 #include <google/protobuf/compiler/cpp/cpp_unittest.h> | 47 #include <google/protobuf/compiler/cpp/cpp_unittest.h> |
| 48 | 48 |
| 49 #include <memory> | 49 #include <memory> |
| 50 #ifndef _SHARED_PTR_H | 50 #ifndef _SHARED_PTR_H |
| 51 #include <google/protobuf/stubs/shared_ptr.h> | 51 #include <google/protobuf/stubs/shared_ptr.h> |
| 52 #endif | 52 #endif |
| 53 #include <vector> | 53 #include <vector> |
| 54 | 54 |
| 55 #include <google/protobuf/unittest.pb.h> | 55 #include <google/protobuf/unittest.pb.h> |
| 56 #include <google/protobuf/unittest_no_arena.pb.h> | |
| 57 #include <google/protobuf/unittest_optimize_for.pb.h> | 56 #include <google/protobuf/unittest_optimize_for.pb.h> |
| 58 #include <google/protobuf/unittest_embed_optimize_for.pb.h> | 57 #include <google/protobuf/unittest_embed_optimize_for.pb.h> |
| 59 #if !defined(GOOGLE_PROTOBUF_CMAKE_BUILD) && !defined(_MSC_VER) | 58 #if !defined(GOOGLE_PROTOBUF_CMAKE_BUILD) && !defined(_MSC_VER) |
| 60 // We exclude this large proto from cmake build because it's too large for | 59 // We exclude this large proto from cmake build because it's too large for |
| 61 // visual studio to compile (report internal errors). | 60 // visual studio to compile (report internal errors). |
| 62 #include <google/protobuf/unittest_enormous_descriptor.pb.h> | 61 #include <google/protobuf/unittest_enormous_descriptor.pb.h> |
| 63 #endif | 62 #endif |
| 64 #include <google/protobuf/unittest_no_generic_services.pb.h> | 63 #include <google/protobuf/unittest_no_generic_services.pb.h> |
| 65 #include <google/protobuf/test_util.h> | 64 #include <google/protobuf/test_util.h> |
| 66 #include <google/protobuf/compiler/cpp/cpp_helpers.h> | 65 #include <google/protobuf/compiler/cpp/cpp_helpers.h> |
| 67 #include <google/protobuf/compiler/cpp/cpp_test_bad_identifiers.pb.h> | 66 #include <google/protobuf/compiler/cpp/cpp_test_bad_identifiers.pb.h> |
| 68 #include <google/protobuf/compiler/importer.h> | 67 #include <google/protobuf/compiler/importer.h> |
| 69 #include <google/protobuf/io/coded_stream.h> | 68 #include <google/protobuf/io/coded_stream.h> |
| 70 #include <google/protobuf/io/zero_copy_stream_impl.h> | 69 #include <google/protobuf/io/zero_copy_stream_impl.h> |
| 71 #include <google/protobuf/arena.h> | |
| 72 #include <google/protobuf/descriptor.h> | 70 #include <google/protobuf/descriptor.h> |
| 73 #include <google/protobuf/descriptor.pb.h> | 71 #include <google/protobuf/descriptor.pb.h> |
| 74 #include <google/protobuf/dynamic_message.h> | 72 #include <google/protobuf/dynamic_message.h> |
| 75 | 73 |
| 76 #include <google/protobuf/stubs/callback.h> | 74 #include <google/protobuf/stubs/callback.h> |
| 77 #include <google/protobuf/stubs/common.h> | 75 #include <google/protobuf/stubs/common.h> |
| 78 #include <google/protobuf/stubs/logging.h> | 76 #include <google/protobuf/stubs/logging.h> |
| 79 #include <google/protobuf/stubs/strutil.h> | 77 #include <google/protobuf/stubs/strutil.h> |
| 80 #include <google/protobuf/stubs/substitute.h> | 78 #include <google/protobuf/stubs/substitute.h> |
| 81 #include <google/protobuf/testing/googletest.h> | 79 #include <google/protobuf/testing/googletest.h> |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 124 Importer importer(&source_tree, &error_collector); | 122 Importer importer(&source_tree, &error_collector); |
| 125 | 123 |
| 126 // Import (parse) unittest.proto. | 124 // Import (parse) unittest.proto. |
| 127 const FileDescriptor* parsed_descriptor = | 125 const FileDescriptor* parsed_descriptor = |
| 128 importer.Import("google/protobuf/unittest.proto"); | 126 importer.Import("google/protobuf/unittest.proto"); |
| 129 EXPECT_EQ("", error_collector.text_); | 127 EXPECT_EQ("", error_collector.text_); |
| 130 ASSERT_TRUE(parsed_descriptor != NULL); | 128 ASSERT_TRUE(parsed_descriptor != NULL); |
| 131 | 129 |
| 132 // Test that descriptors are generated correctly by converting them to | 130 // Test that descriptors are generated correctly by converting them to |
| 133 // FileDescriptorProtos and comparing. | 131 // FileDescriptorProtos and comparing. |
| 134 FileDescriptorProto generated_descriptor_proto, parsed_descriptor_proto; | 132 FileDescriptorProto generated_decsriptor_proto, parsed_descriptor_proto; |
| 135 generated_descriptor->CopyTo(&generated_descriptor_proto); | 133 generated_descriptor->CopyTo(&generated_decsriptor_proto); |
| 136 parsed_descriptor->CopyTo(&parsed_descriptor_proto); | 134 parsed_descriptor->CopyTo(&parsed_descriptor_proto); |
| 137 | 135 |
| 138 EXPECT_EQ(parsed_descriptor_proto.DebugString(), | 136 EXPECT_EQ(parsed_descriptor_proto.DebugString(), |
| 139 generated_descriptor_proto.DebugString()); | 137 generated_decsriptor_proto.DebugString()); |
| 140 } | 138 } |
| 141 | 139 |
| 142 #if !defined(GOOGLE_PROTOBUF_CMAKE_BUILD) && !defined(_MSC_VER) | 140 #if !defined(GOOGLE_PROTOBUF_CMAKE_BUILD) && !defined(_MSC_VER) |
| 143 // Test that generated code has proper descriptors: | 141 // Test that generated code has proper descriptors: |
| 144 // Touch a descriptor generated from an enormous message to validate special | 142 // Touch a descriptor generated from an enormous message to validate special |
| 145 // handling for descriptors exceeding the C++ standard's recommended minimum | 143 // handling for descriptors exceeding the C++ standard's recommended minimum |
| 146 // limit for string literal size | 144 // limit for string literal size |
| 147 TEST(GeneratedDescriptorTest, EnormousDescriptor) { | 145 TEST(GeneratedDescriptorTest, EnormousDescriptor) { |
| 148 const Descriptor* generated_descriptor = | 146 const Descriptor* generated_descriptor = |
| 149 TestEnormousDescriptor::descriptor(); | 147 TestEnormousDescriptor::descriptor(); |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 194 const unittest::TestExtremeDefaultValues& extreme_default = | 192 const unittest::TestExtremeDefaultValues& extreme_default = |
| 195 unittest::TestExtremeDefaultValues::default_instance(); | 193 unittest::TestExtremeDefaultValues::default_instance(); |
| 196 | 194 |
| 197 EXPECT_EQ(0.0f, extreme_default.zero_float()); | 195 EXPECT_EQ(0.0f, extreme_default.zero_float()); |
| 198 EXPECT_EQ(1.0f, extreme_default.one_float()); | 196 EXPECT_EQ(1.0f, extreme_default.one_float()); |
| 199 EXPECT_EQ(1.5f, extreme_default.small_float()); | 197 EXPECT_EQ(1.5f, extreme_default.small_float()); |
| 200 EXPECT_EQ(-1.0f, extreme_default.negative_one_float()); | 198 EXPECT_EQ(-1.0f, extreme_default.negative_one_float()); |
| 201 EXPECT_EQ(-1.5f, extreme_default.negative_float()); | 199 EXPECT_EQ(-1.5f, extreme_default.negative_float()); |
| 202 EXPECT_EQ(2.0e8f, extreme_default.large_float()); | 200 EXPECT_EQ(2.0e8f, extreme_default.large_float()); |
| 203 EXPECT_EQ(-8e-28f, extreme_default.small_negative_float()); | 201 EXPECT_EQ(-8e-28f, extreme_default.small_negative_float()); |
| 204 EXPECT_EQ(std::numeric_limits<double>::infinity(), | 202 EXPECT_EQ(numeric_limits<double>::infinity(), |
| 205 extreme_default.inf_double()); | 203 extreme_default.inf_double()); |
| 206 EXPECT_EQ(-std::numeric_limits<double>::infinity(), | 204 EXPECT_EQ(-numeric_limits<double>::infinity(), |
| 207 extreme_default.neg_inf_double()); | 205 extreme_default.neg_inf_double()); |
| 208 EXPECT_TRUE(extreme_default.nan_double() != extreme_default.nan_double()); | 206 EXPECT_TRUE(extreme_default.nan_double() != extreme_default.nan_double()); |
| 209 EXPECT_EQ(std::numeric_limits<float>::infinity(), | 207 EXPECT_EQ(numeric_limits<float>::infinity(), |
| 210 extreme_default.inf_float()); | 208 extreme_default.inf_float()); |
| 211 EXPECT_EQ(-std::numeric_limits<float>::infinity(), | 209 EXPECT_EQ(-numeric_limits<float>::infinity(), |
| 212 extreme_default.neg_inf_float()); | 210 extreme_default.neg_inf_float()); |
| 213 EXPECT_TRUE(extreme_default.nan_float() != extreme_default.nan_float()); | 211 EXPECT_TRUE(extreme_default.nan_float() != extreme_default.nan_float()); |
| 214 } | 212 } |
| 215 | 213 |
| 216 TEST(GeneratedMessageTest, Trigraph) { | 214 TEST(GeneratedMessageTest, Trigraph) { |
| 217 const unittest::TestExtremeDefaultValues& extreme_default = | 215 const unittest::TestExtremeDefaultValues& extreme_default = |
| 218 unittest::TestExtremeDefaultValues::default_instance(); | 216 unittest::TestExtremeDefaultValues::default_instance(); |
| 219 | 217 |
| 220 EXPECT_EQ("? ? ?? ?? ??? ?\?/ ?\?-", extreme_default.cpp_trigraph()); | 218 EXPECT_EQ("? ? ?? ?? ??? ?\?/ ?\?-", extreme_default.cpp_trigraph()); |
| 221 } | 219 } |
| (...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 414 // Verify that we can use a char*,length to add to a repeated string field. | 412 // Verify that we can use a char*,length to add to a repeated string field. |
| 415 message.add_repeated_string("abcdef", 3); | 413 message.add_repeated_string("abcdef", 3); |
| 416 EXPECT_EQ(1, message.repeated_string_size()); | 414 EXPECT_EQ(1, message.repeated_string_size()); |
| 417 EXPECT_EQ("abc", message.repeated_string(0)); | 415 EXPECT_EQ("abc", message.repeated_string(0)); |
| 418 | 416 |
| 419 // Verify that we can use a char*,length to set a repeated string field. | 417 // Verify that we can use a char*,length to set a repeated string field. |
| 420 message.set_repeated_string(0, "wxyz", 2); | 418 message.set_repeated_string(0, "wxyz", 2); |
| 421 EXPECT_EQ("wx", message.repeated_string(0)); | 419 EXPECT_EQ("wx", message.repeated_string(0)); |
| 422 } | 420 } |
| 423 | 421 |
| 424 #if LANG_CXX11 | |
| 425 TEST(GeneratedMessageTest, StringMove) { | |
| 426 // Verify that we trigger the move behavior on a scalar setter. | |
| 427 protobuf_unittest_no_arena::TestAllTypes message; | |
| 428 { | |
| 429 string tmp(32, 'a'); | |
| 430 | |
| 431 const char* old_data = tmp.data(); | |
| 432 message.set_optional_string(std::move(tmp)); | |
| 433 const char* new_data = message.optional_string().data(); | |
| 434 | |
| 435 EXPECT_EQ(old_data, new_data); | |
| 436 EXPECT_EQ(string(32, 'a'), message.optional_string()); | |
| 437 | |
| 438 string tmp2(32, 'b'); | |
| 439 old_data = tmp2.data(); | |
| 440 message.set_optional_string(std::move(tmp2)); | |
| 441 new_data = message.optional_string().data(); | |
| 442 | |
| 443 EXPECT_EQ(old_data, new_data); | |
| 444 EXPECT_EQ(string(32, 'b'), message.optional_string()); | |
| 445 } | |
| 446 | |
| 447 // Verify that we trigger the move behavior on a oneof setter. | |
| 448 { | |
| 449 string tmp(32, 'a'); | |
| 450 | |
| 451 const char* old_data = tmp.data(); | |
| 452 message.set_oneof_string(std::move(tmp)); | |
| 453 const char* new_data = message.oneof_string().data(); | |
| 454 | |
| 455 EXPECT_EQ(old_data, new_data); | |
| 456 EXPECT_EQ(string(32, 'a'), message.oneof_string()); | |
| 457 | |
| 458 string tmp2(32, 'b'); | |
| 459 old_data = tmp2.data(); | |
| 460 message.set_oneof_string(std::move(tmp2)); | |
| 461 new_data = message.oneof_string().data(); | |
| 462 | |
| 463 EXPECT_EQ(old_data, new_data); | |
| 464 EXPECT_EQ(string(32, 'b'), message.oneof_string()); | |
| 465 } | |
| 466 } | |
| 467 #endif | |
| 468 | |
| 469 | 422 |
| 470 TEST(GeneratedMessageTest, CopyFrom) { | 423 TEST(GeneratedMessageTest, CopyFrom) { |
| 471 unittest::TestAllTypes message1, message2; | 424 unittest::TestAllTypes message1, message2; |
| 472 | 425 |
| 473 TestUtil::SetAllFields(&message1); | 426 TestUtil::SetAllFields(&message1); |
| 474 message2.CopyFrom(message1); | 427 message2.CopyFrom(message1); |
| 475 TestUtil::ExpectAllFieldsSet(message2); | 428 TestUtil::ExpectAllFieldsSet(message2); |
| 476 | 429 |
| 477 // Copying from self should be a no-op. | 430 // Copying from self should be a no-op. |
| 478 message2.CopyFrom(message2); | 431 message2.CopyFrom(message2); |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 558 } | 511 } |
| 559 | 512 |
| 560 TEST(GeneratedMessageTest, CopyConstructor) { | 513 TEST(GeneratedMessageTest, CopyConstructor) { |
| 561 unittest::TestAllTypes message1; | 514 unittest::TestAllTypes message1; |
| 562 TestUtil::SetAllFields(&message1); | 515 TestUtil::SetAllFields(&message1); |
| 563 | 516 |
| 564 unittest::TestAllTypes message2(message1); | 517 unittest::TestAllTypes message2(message1); |
| 565 TestUtil::ExpectAllFieldsSet(message2); | 518 TestUtil::ExpectAllFieldsSet(message2); |
| 566 } | 519 } |
| 567 | 520 |
| 568 TEST(GeneratedMessageTest, CopyConstructorWithArenas) { | |
| 569 Arena arena; | |
| 570 unittest::TestAllTypes* message1 = | |
| 571 Arena::CreateMessage<unittest::TestAllTypes>(&arena); | |
| 572 TestUtil::SetAllFields(message1); | |
| 573 | |
| 574 unittest::TestAllTypes message2_stack(*message1); | |
| 575 TestUtil::ExpectAllFieldsSet(message2_stack); | |
| 576 | |
| 577 google::protobuf::scoped_ptr<unittest::TestAllTypes> message2_heap( | |
| 578 new unittest::TestAllTypes(*message1)); | |
| 579 TestUtil::ExpectAllFieldsSet(*message2_heap); | |
| 580 | |
| 581 arena.Reset(); | |
| 582 | |
| 583 // Verify that the copies are still intact. | |
| 584 TestUtil::ExpectAllFieldsSet(message2_stack); | |
| 585 TestUtil::ExpectAllFieldsSet(*message2_heap); | |
| 586 } | |
| 587 | |
| 588 TEST(GeneratedMessageTest, CopyAssignmentOperator) { | 521 TEST(GeneratedMessageTest, CopyAssignmentOperator) { |
| 589 unittest::TestAllTypes message1; | 522 unittest::TestAllTypes message1; |
| 590 TestUtil::SetAllFields(&message1); | 523 TestUtil::SetAllFields(&message1); |
| 591 | 524 |
| 592 unittest::TestAllTypes message2; | 525 unittest::TestAllTypes message2; |
| 593 message2 = message1; | 526 message2 = message1; |
| 594 TestUtil::ExpectAllFieldsSet(message2); | 527 TestUtil::ExpectAllFieldsSet(message2); |
| 595 | 528 |
| 596 // Make sure that self-assignment does something sane. | 529 // Make sure that self-assignment does something sane. |
| 597 message2.operator=(message2); | 530 message2.operator=(message2); |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 660 message1.add_repeated_int32(i); | 593 message1.add_repeated_int32(i); |
| 661 | 594 |
| 662 message1.MergeFrom(message2); | 595 message1.MergeFrom(message2); |
| 663 | 596 |
| 664 TestUtil::ExpectAllFieldsSet(message1); | 597 TestUtil::ExpectAllFieldsSet(message1); |
| 665 } | 598 } |
| 666 | 599 |
| 667 #if !defined(PROTOBUF_TEST_NO_DESCRIPTORS) || \ | 600 #if !defined(PROTOBUF_TEST_NO_DESCRIPTORS) || \ |
| 668 !defined(GOOGLE_PROTOBUF_NO_RTTI) | 601 !defined(GOOGLE_PROTOBUF_NO_RTTI) |
| 669 #ifdef PROTOBUF_HAS_DEATH_TEST | 602 #ifdef PROTOBUF_HAS_DEATH_TEST |
| 670 #ifndef NDEBUG | |
| 671 | 603 |
| 672 TEST(GeneratedMessageTest, MergeFromSelf) { | 604 TEST(GeneratedMessageTest, MergeFromSelf) { |
| 673 unittest::TestAllTypes message; | 605 unittest::TestAllTypes message; |
| 674 EXPECT_DEATH(message.MergeFrom(message), "pb[.]cc.*Check failed:"); | 606 EXPECT_DEATH(message.MergeFrom(message), "Check failed:.*pb[.]cc"); |
| 675 EXPECT_DEATH(message.MergeFrom(implicit_cast<const Message&>(message)), | 607 EXPECT_DEATH(message.MergeFrom(implicit_cast<const Message&>(message)), |
| 676 "pb[.]cc.*Check failed:"); | 608 "Check failed:.*pb[.]cc"); |
| 677 } | 609 } |
| 678 | 610 |
| 679 #endif // NDEBUG | |
| 680 #endif // PROTOBUF_HAS_DEATH_TEST | 611 #endif // PROTOBUF_HAS_DEATH_TEST |
| 681 #endif // !PROTOBUF_TEST_NO_DESCRIPTORS || !GOOGLE_PROTOBUF_NO_RTTI | 612 #endif // !PROTOBUF_TEST_NO_DESCRIPTORS || !GOOGLE_PROTOBUF_NO_RTTI |
| 682 | 613 |
| 683 // Test the generated SerializeWithCachedSizesToArray(), | 614 // Test the generated SerializeWithCachedSizesToArray(), |
| 684 TEST(GeneratedMessageTest, SerializationToArray) { | 615 TEST(GeneratedMessageTest, SerializationToArray) { |
| 685 unittest::TestAllTypes message1, message2; | 616 unittest::TestAllTypes message1, message2; |
| 686 string data; | 617 string data; |
| 687 TestUtil::SetAllFields(&message1); | 618 TestUtil::SetAllFields(&message1); |
| 688 int size = message1.ByteSize(); | 619 int size = message1.ByteSize(); |
| 689 data.resize(size); | 620 data.resize(size); |
| (...skipping 624 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1314 void NotifyOnCancel(Closure* callback) { | 1245 void NotifyOnCancel(Closure* callback) { |
| 1315 ADD_FAILURE() << "NotifyOnCancel() not expected during this test."; | 1246 ADD_FAILURE() << "NotifyOnCancel() not expected during this test."; |
| 1316 } | 1247 } |
| 1317 }; | 1248 }; |
| 1318 | 1249 |
| 1319 GeneratedServiceTest() | 1250 GeneratedServiceTest() |
| 1320 : descriptor_(unittest::TestService::descriptor()), | 1251 : descriptor_(unittest::TestService::descriptor()), |
| 1321 foo_(descriptor_->FindMethodByName("Foo")), | 1252 foo_(descriptor_->FindMethodByName("Foo")), |
| 1322 bar_(descriptor_->FindMethodByName("Bar")), | 1253 bar_(descriptor_->FindMethodByName("Bar")), |
| 1323 stub_(&mock_channel_), | 1254 stub_(&mock_channel_), |
| 1324 done_(NewPermanentCallback(&DoNothing)) {} | 1255 done_(::google::protobuf::internal::NewPermanentCallback(&DoNothing)) {} |
| 1325 | 1256 |
| 1326 virtual void SetUp() { | 1257 virtual void SetUp() { |
| 1327 ASSERT_TRUE(foo_ != NULL); | 1258 ASSERT_TRUE(foo_ != NULL); |
| 1328 ASSERT_TRUE(bar_ != NULL); | 1259 ASSERT_TRUE(bar_ != NULL); |
| 1329 } | 1260 } |
| 1330 | 1261 |
| 1331 const ServiceDescriptor* descriptor_; | 1262 const ServiceDescriptor* descriptor_; |
| 1332 const MethodDescriptor* foo_; | 1263 const MethodDescriptor* foo_; |
| 1333 const MethodDescriptor* bar_; | 1264 const MethodDescriptor* bar_; |
| 1334 | 1265 |
| (...skipping 864 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2199 DescriptorPool::generated_pool()->InternalIsFileLoaded( | 2130 DescriptorPool::generated_pool()->InternalIsFileLoaded( |
| 2200 "google/protobuf/unittest.proto")); | 2131 "google/protobuf/unittest.proto")); |
| 2201 } | 2132 } |
| 2202 | 2133 |
| 2203 } // namespace cpp_unittest | 2134 } // namespace cpp_unittest |
| 2204 | 2135 |
| 2205 } // namespace cpp | 2136 } // namespace cpp |
| 2206 } // namespace compiler | 2137 } // namespace compiler |
| 2207 } // namespace protobuf | 2138 } // namespace protobuf |
| 2208 } // namespace google | 2139 } // namespace google |
| OLD | NEW |