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 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
60 // visual studio to compile (report internal errors). | 60 // visual studio to compile (report internal errors). |
61 #include <google/protobuf/unittest_enormous_descriptor.pb.h> | 61 #include <google/protobuf/unittest_enormous_descriptor.pb.h> |
62 #endif | 62 #endif |
63 #include <google/protobuf/unittest_no_generic_services.pb.h> | 63 #include <google/protobuf/unittest_no_generic_services.pb.h> |
64 #include <google/protobuf/test_util.h> | 64 #include <google/protobuf/test_util.h> |
65 #include <google/protobuf/compiler/cpp/cpp_helpers.h> | 65 #include <google/protobuf/compiler/cpp/cpp_helpers.h> |
66 #include <google/protobuf/compiler/cpp/cpp_test_bad_identifiers.pb.h> | 66 #include <google/protobuf/compiler/cpp/cpp_test_bad_identifiers.pb.h> |
67 #include <google/protobuf/compiler/importer.h> | 67 #include <google/protobuf/compiler/importer.h> |
68 #include <google/protobuf/io/coded_stream.h> | 68 #include <google/protobuf/io/coded_stream.h> |
69 #include <google/protobuf/io/zero_copy_stream_impl.h> | 69 #include <google/protobuf/io/zero_copy_stream_impl.h> |
| 70 #include <google/protobuf/arena.h> |
70 #include <google/protobuf/descriptor.h> | 71 #include <google/protobuf/descriptor.h> |
71 #include <google/protobuf/descriptor.pb.h> | 72 #include <google/protobuf/descriptor.pb.h> |
72 #include <google/protobuf/dynamic_message.h> | 73 #include <google/protobuf/dynamic_message.h> |
73 | 74 |
74 #include <google/protobuf/stubs/callback.h> | 75 #include <google/protobuf/stubs/callback.h> |
75 #include <google/protobuf/stubs/common.h> | 76 #include <google/protobuf/stubs/common.h> |
76 #include <google/protobuf/stubs/logging.h> | 77 #include <google/protobuf/stubs/logging.h> |
77 #include <google/protobuf/stubs/strutil.h> | 78 #include <google/protobuf/stubs/strutil.h> |
78 #include <google/protobuf/stubs/substitute.h> | 79 #include <google/protobuf/stubs/substitute.h> |
79 #include <google/protobuf/testing/googletest.h> | 80 #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); | 123 Importer importer(&source_tree, &error_collector); |
123 | 124 |
124 // Import (parse) unittest.proto. | 125 // Import (parse) unittest.proto. |
125 const FileDescriptor* parsed_descriptor = | 126 const FileDescriptor* parsed_descriptor = |
126 importer.Import("google/protobuf/unittest.proto"); | 127 importer.Import("google/protobuf/unittest.proto"); |
127 EXPECT_EQ("", error_collector.text_); | 128 EXPECT_EQ("", error_collector.text_); |
128 ASSERT_TRUE(parsed_descriptor != NULL); | 129 ASSERT_TRUE(parsed_descriptor != NULL); |
129 | 130 |
130 // Test that descriptors are generated correctly by converting them to | 131 // Test that descriptors are generated correctly by converting them to |
131 // FileDescriptorProtos and comparing. | 132 // FileDescriptorProtos and comparing. |
132 FileDescriptorProto generated_decsriptor_proto, parsed_descriptor_proto; | 133 FileDescriptorProto generated_descriptor_proto, parsed_descriptor_proto; |
133 generated_descriptor->CopyTo(&generated_decsriptor_proto); | 134 generated_descriptor->CopyTo(&generated_descriptor_proto); |
134 parsed_descriptor->CopyTo(&parsed_descriptor_proto); | 135 parsed_descriptor->CopyTo(&parsed_descriptor_proto); |
135 | 136 |
136 EXPECT_EQ(parsed_descriptor_proto.DebugString(), | 137 EXPECT_EQ(parsed_descriptor_proto.DebugString(), |
137 generated_decsriptor_proto.DebugString()); | 138 generated_descriptor_proto.DebugString()); |
138 } | 139 } |
139 | 140 |
140 #if !defined(GOOGLE_PROTOBUF_CMAKE_BUILD) && !defined(_MSC_VER) | 141 #if !defined(GOOGLE_PROTOBUF_CMAKE_BUILD) && !defined(_MSC_VER) |
141 // Test that generated code has proper descriptors: | 142 // Test that generated code has proper descriptors: |
142 // Touch a descriptor generated from an enormous message to validate special | 143 // Touch a descriptor generated from an enormous message to validate special |
143 // handling for descriptors exceeding the C++ standard's recommended minimum | 144 // handling for descriptors exceeding the C++ standard's recommended minimum |
144 // limit for string literal size | 145 // limit for string literal size |
145 TEST(GeneratedDescriptorTest, EnormousDescriptor) { | 146 TEST(GeneratedDescriptorTest, EnormousDescriptor) { |
146 const Descriptor* generated_descriptor = | 147 const Descriptor* generated_descriptor = |
147 TestEnormousDescriptor::descriptor(); | 148 TestEnormousDescriptor::descriptor(); |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
192 const unittest::TestExtremeDefaultValues& extreme_default = | 193 const unittest::TestExtremeDefaultValues& extreme_default = |
193 unittest::TestExtremeDefaultValues::default_instance(); | 194 unittest::TestExtremeDefaultValues::default_instance(); |
194 | 195 |
195 EXPECT_EQ(0.0f, extreme_default.zero_float()); | 196 EXPECT_EQ(0.0f, extreme_default.zero_float()); |
196 EXPECT_EQ(1.0f, extreme_default.one_float()); | 197 EXPECT_EQ(1.0f, extreme_default.one_float()); |
197 EXPECT_EQ(1.5f, extreme_default.small_float()); | 198 EXPECT_EQ(1.5f, extreme_default.small_float()); |
198 EXPECT_EQ(-1.0f, extreme_default.negative_one_float()); | 199 EXPECT_EQ(-1.0f, extreme_default.negative_one_float()); |
199 EXPECT_EQ(-1.5f, extreme_default.negative_float()); | 200 EXPECT_EQ(-1.5f, extreme_default.negative_float()); |
200 EXPECT_EQ(2.0e8f, extreme_default.large_float()); | 201 EXPECT_EQ(2.0e8f, extreme_default.large_float()); |
201 EXPECT_EQ(-8e-28f, extreme_default.small_negative_float()); | 202 EXPECT_EQ(-8e-28f, extreme_default.small_negative_float()); |
202 EXPECT_EQ(numeric_limits<double>::infinity(), | 203 EXPECT_EQ(std::numeric_limits<double>::infinity(), |
203 extreme_default.inf_double()); | 204 extreme_default.inf_double()); |
204 EXPECT_EQ(-numeric_limits<double>::infinity(), | 205 EXPECT_EQ(-std::numeric_limits<double>::infinity(), |
205 extreme_default.neg_inf_double()); | 206 extreme_default.neg_inf_double()); |
206 EXPECT_TRUE(extreme_default.nan_double() != extreme_default.nan_double()); | 207 EXPECT_TRUE(extreme_default.nan_double() != extreme_default.nan_double()); |
207 EXPECT_EQ(numeric_limits<float>::infinity(), | 208 EXPECT_EQ(std::numeric_limits<float>::infinity(), |
208 extreme_default.inf_float()); | 209 extreme_default.inf_float()); |
209 EXPECT_EQ(-numeric_limits<float>::infinity(), | 210 EXPECT_EQ(-std::numeric_limits<float>::infinity(), |
210 extreme_default.neg_inf_float()); | 211 extreme_default.neg_inf_float()); |
211 EXPECT_TRUE(extreme_default.nan_float() != extreme_default.nan_float()); | 212 EXPECT_TRUE(extreme_default.nan_float() != extreme_default.nan_float()); |
212 } | 213 } |
213 | 214 |
214 TEST(GeneratedMessageTest, Trigraph) { | 215 TEST(GeneratedMessageTest, Trigraph) { |
215 const unittest::TestExtremeDefaultValues& extreme_default = | 216 const unittest::TestExtremeDefaultValues& extreme_default = |
216 unittest::TestExtremeDefaultValues::default_instance(); | 217 unittest::TestExtremeDefaultValues::default_instance(); |
217 | 218 |
218 EXPECT_EQ("? ? ?? ?? ??? ?\?/ ?\?-", extreme_default.cpp_trigraph()); | 219 EXPECT_EQ("? ? ?? ?? ??? ?\?/ ?\?-", extreme_default.cpp_trigraph()); |
219 } | 220 } |
(...skipping 291 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
511 } | 512 } |
512 | 513 |
513 TEST(GeneratedMessageTest, CopyConstructor) { | 514 TEST(GeneratedMessageTest, CopyConstructor) { |
514 unittest::TestAllTypes message1; | 515 unittest::TestAllTypes message1; |
515 TestUtil::SetAllFields(&message1); | 516 TestUtil::SetAllFields(&message1); |
516 | 517 |
517 unittest::TestAllTypes message2(message1); | 518 unittest::TestAllTypes message2(message1); |
518 TestUtil::ExpectAllFieldsSet(message2); | 519 TestUtil::ExpectAllFieldsSet(message2); |
519 } | 520 } |
520 | 521 |
| 522 TEST(GeneratedMessageTest, CopyConstructorWithArenas) { |
| 523 Arena arena; |
| 524 unittest::TestAllTypes* message1 = |
| 525 Arena::CreateMessage<unittest::TestAllTypes>(&arena); |
| 526 TestUtil::SetAllFields(message1); |
| 527 |
| 528 unittest::TestAllTypes message2_stack(*message1); |
| 529 TestUtil::ExpectAllFieldsSet(message2_stack); |
| 530 |
| 531 google::protobuf::scoped_ptr<unittest::TestAllTypes> message2_heap( |
| 532 new unittest::TestAllTypes(*message1)); |
| 533 TestUtil::ExpectAllFieldsSet(*message2_heap); |
| 534 |
| 535 arena.Reset(); |
| 536 |
| 537 // Verify that the copies are still intact. |
| 538 TestUtil::ExpectAllFieldsSet(message2_stack); |
| 539 TestUtil::ExpectAllFieldsSet(*message2_heap); |
| 540 } |
| 541 |
521 TEST(GeneratedMessageTest, CopyAssignmentOperator) { | 542 TEST(GeneratedMessageTest, CopyAssignmentOperator) { |
522 unittest::TestAllTypes message1; | 543 unittest::TestAllTypes message1; |
523 TestUtil::SetAllFields(&message1); | 544 TestUtil::SetAllFields(&message1); |
524 | 545 |
525 unittest::TestAllTypes message2; | 546 unittest::TestAllTypes message2; |
526 message2 = message1; | 547 message2 = message1; |
527 TestUtil::ExpectAllFieldsSet(message2); | 548 TestUtil::ExpectAllFieldsSet(message2); |
528 | 549 |
529 // Make sure that self-assignment does something sane. | 550 // Make sure that self-assignment does something sane. |
530 message2.operator=(message2); | 551 message2.operator=(message2); |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
593 message1.add_repeated_int32(i); | 614 message1.add_repeated_int32(i); |
594 | 615 |
595 message1.MergeFrom(message2); | 616 message1.MergeFrom(message2); |
596 | 617 |
597 TestUtil::ExpectAllFieldsSet(message1); | 618 TestUtil::ExpectAllFieldsSet(message1); |
598 } | 619 } |
599 | 620 |
600 #if !defined(PROTOBUF_TEST_NO_DESCRIPTORS) || \ | 621 #if !defined(PROTOBUF_TEST_NO_DESCRIPTORS) || \ |
601 !defined(GOOGLE_PROTOBUF_NO_RTTI) | 622 !defined(GOOGLE_PROTOBUF_NO_RTTI) |
602 #ifdef PROTOBUF_HAS_DEATH_TEST | 623 #ifdef PROTOBUF_HAS_DEATH_TEST |
| 624 #ifndef NDEBUG |
603 | 625 |
604 TEST(GeneratedMessageTest, MergeFromSelf) { | 626 TEST(GeneratedMessageTest, MergeFromSelf) { |
605 unittest::TestAllTypes message; | 627 unittest::TestAllTypes message; |
606 EXPECT_DEATH(message.MergeFrom(message), "Check failed:.*pb[.]cc"); | 628 EXPECT_DEATH(message.MergeFrom(message), "pb[.]cc.*Check failed:"); |
607 EXPECT_DEATH(message.MergeFrom(implicit_cast<const Message&>(message)), | 629 EXPECT_DEATH(message.MergeFrom(implicit_cast<const Message&>(message)), |
608 "Check failed:.*pb[.]cc"); | 630 "pb[.]cc.*Check failed:"); |
609 } | 631 } |
610 | 632 |
| 633 #endif // NDEBUG |
611 #endif // PROTOBUF_HAS_DEATH_TEST | 634 #endif // PROTOBUF_HAS_DEATH_TEST |
612 #endif // !PROTOBUF_TEST_NO_DESCRIPTORS || !GOOGLE_PROTOBUF_NO_RTTI | 635 #endif // !PROTOBUF_TEST_NO_DESCRIPTORS || !GOOGLE_PROTOBUF_NO_RTTI |
613 | 636 |
614 // Test the generated SerializeWithCachedSizesToArray(), | 637 // Test the generated SerializeWithCachedSizesToArray(), |
615 TEST(GeneratedMessageTest, SerializationToArray) { | 638 TEST(GeneratedMessageTest, SerializationToArray) { |
616 unittest::TestAllTypes message1, message2; | 639 unittest::TestAllTypes message1, message2; |
617 string data; | 640 string data; |
618 TestUtil::SetAllFields(&message1); | 641 TestUtil::SetAllFields(&message1); |
619 int size = message1.ByteSize(); | 642 int size = message1.ByteSize(); |
620 data.resize(size); | 643 data.resize(size); |
(...skipping 624 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1245 void NotifyOnCancel(Closure* callback) { | 1268 void NotifyOnCancel(Closure* callback) { |
1246 ADD_FAILURE() << "NotifyOnCancel() not expected during this test."; | 1269 ADD_FAILURE() << "NotifyOnCancel() not expected during this test."; |
1247 } | 1270 } |
1248 }; | 1271 }; |
1249 | 1272 |
1250 GeneratedServiceTest() | 1273 GeneratedServiceTest() |
1251 : descriptor_(unittest::TestService::descriptor()), | 1274 : descriptor_(unittest::TestService::descriptor()), |
1252 foo_(descriptor_->FindMethodByName("Foo")), | 1275 foo_(descriptor_->FindMethodByName("Foo")), |
1253 bar_(descriptor_->FindMethodByName("Bar")), | 1276 bar_(descriptor_->FindMethodByName("Bar")), |
1254 stub_(&mock_channel_), | 1277 stub_(&mock_channel_), |
1255 done_(::google::protobuf::internal::NewPermanentCallback(&DoNothing)) {} | 1278 done_(NewPermanentCallback(&DoNothing)) {} |
1256 | 1279 |
1257 virtual void SetUp() { | 1280 virtual void SetUp() { |
1258 ASSERT_TRUE(foo_ != NULL); | 1281 ASSERT_TRUE(foo_ != NULL); |
1259 ASSERT_TRUE(bar_ != NULL); | 1282 ASSERT_TRUE(bar_ != NULL); |
1260 } | 1283 } |
1261 | 1284 |
1262 const ServiceDescriptor* descriptor_; | 1285 const ServiceDescriptor* descriptor_; |
1263 const MethodDescriptor* foo_; | 1286 const MethodDescriptor* foo_; |
1264 const MethodDescriptor* bar_; | 1287 const MethodDescriptor* bar_; |
1265 | 1288 |
(...skipping 864 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2130 DescriptorPool::generated_pool()->InternalIsFileLoaded( | 2153 DescriptorPool::generated_pool()->InternalIsFileLoaded( |
2131 "google/protobuf/unittest.proto")); | 2154 "google/protobuf/unittest.proto")); |
2132 } | 2155 } |
2133 | 2156 |
2134 } // namespace cpp_unittest | 2157 } // namespace cpp_unittest |
2135 | 2158 |
2136 } // namespace cpp | 2159 } // namespace cpp |
2137 } // namespace compiler | 2160 } // namespace compiler |
2138 } // namespace protobuf | 2161 } // namespace protobuf |
2139 } // namespace google | 2162 } // namespace google |
OLD | NEW |