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