| 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 26 matching lines...) Expand all Loading... |
| 37 // are actually testing the parser and other parts of the system at the same | 37 // are actually testing the parser and other parts of the system at the same |
| 38 // time, and that problems in the generator may show up as compile-time errors | 38 // time, and that problems in the generator may show up as compile-time errors |
| 39 // rather than unittest failures, which may be surprising. However, testing | 39 // rather than unittest failures, which may be surprising. However, testing |
| 40 // the output of the C++ generator directly would be very hard. We can't very | 40 // the output of the C++ generator directly would be very hard. We can't very |
| 41 // well just check it against golden files since those files would have to be | 41 // well just check it against golden files since those files would have to be |
| 42 // updated for any small change; such a test would be very brittle and probably | 42 // updated for any small change; such a test would be very brittle and probably |
| 43 // not very helpful. What we really want to test is that the code compiles | 43 // not very helpful. What we really want to test is that the code compiles |
| 44 // correctly and produces the interfaces we expect, which is why this test | 44 // correctly and produces the interfaces we expect, which is why this test |
| 45 // is written this way. | 45 // is written this way. |
| 46 | 46 |
| 47 #include <google/protobuf/compiler/cpp/cpp_unittest.h> |
| 48 |
| 47 #include <vector> | 49 #include <vector> |
| 48 | 50 |
| 49 #include <google/protobuf/unittest.pb.h> | 51 #include <google/protobuf/unittest.pb.h> |
| 50 #include <google/protobuf/unittest_optimize_for.pb.h> | 52 #include <google/protobuf/unittest_optimize_for.pb.h> |
| 51 #include <google/protobuf/unittest_embed_optimize_for.pb.h> | 53 #include <google/protobuf/unittest_embed_optimize_for.pb.h> |
| 52 #include <google/protobuf/unittest_no_generic_services.pb.h> | 54 #include <google/protobuf/unittest_no_generic_services.pb.h> |
| 53 #include <google/protobuf/test_util.h> | 55 #include <google/protobuf/test_util.h> |
| 54 #include <google/protobuf/compiler/cpp/cpp_test_bad_identifiers.pb.h> | 56 #include <google/protobuf/compiler/cpp/cpp_test_bad_identifiers.pb.h> |
| 55 #include <google/protobuf/compiler/importer.h> | 57 #include <google/protobuf/compiler/importer.h> |
| 56 #include <google/protobuf/io/coded_stream.h> | 58 #include <google/protobuf/io/coded_stream.h> |
| 57 #include <google/protobuf/io/zero_copy_stream_impl.h> | 59 #include <google/protobuf/io/zero_copy_stream_impl.h> |
| 58 #include <google/protobuf/descriptor.h> | 60 #include <google/protobuf/descriptor.h> |
| 59 #include <google/protobuf/descriptor.pb.h> | 61 #include <google/protobuf/descriptor.pb.h> |
| 60 #include <google/protobuf/dynamic_message.h> | 62 #include <google/protobuf/dynamic_message.h> |
| 61 | 63 |
| 62 #include <google/protobuf/stubs/common.h> | 64 #include <google/protobuf/stubs/common.h> |
| 63 #include <google/protobuf/stubs/strutil.h> | 65 #include <google/protobuf/stubs/strutil.h> |
| 64 #include <google/protobuf/stubs/substitute.h> | 66 #include <google/protobuf/stubs/substitute.h> |
| 65 #include <google/protobuf/testing/googletest.h> | 67 #include <google/protobuf/testing/googletest.h> |
| 66 #include <gtest/gtest.h> | 68 #include <gtest/gtest.h> |
| 67 #include <google/protobuf/stubs/stl_util-inl.h> | 69 #include <google/protobuf/stubs/stl_util.h> |
| 68 | 70 |
| 69 namespace google { | 71 namespace google { |
| 70 namespace protobuf { | 72 namespace protobuf { |
| 71 namespace compiler { | 73 namespace compiler { |
| 72 namespace cpp { | 74 namespace cpp { |
| 73 | 75 |
| 74 // Can't use an anonymous namespace here due to brokenness of Tru64 compiler. | 76 // Can't use an anonymous namespace here due to brokenness of Tru64 compiler. |
| 75 namespace cpp_unittest { | 77 namespace cpp_unittest { |
| 76 | 78 |
| 79 namespace protobuf_unittest = ::protobuf_unittest; |
| 80 |
| 77 | 81 |
| 78 class MockErrorCollector : public MultiFileErrorCollector { | 82 class MockErrorCollector : public MultiFileErrorCollector { |
| 79 public: | 83 public: |
| 80 MockErrorCollector() {} | 84 MockErrorCollector() {} |
| 81 ~MockErrorCollector() {} | 85 ~MockErrorCollector() {} |
| 82 | 86 |
| 83 string text_; | 87 string text_; |
| 84 | 88 |
| 85 // implements ErrorCollector --------------------------------------- | 89 // implements ErrorCollector --------------------------------------- |
| 86 void AddError(const string& filename, int line, int column, | 90 void AddError(const string& filename, int line, int column, |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 167 EXPECT_TRUE(extreme_default.nan_float() != extreme_default.nan_float()); | 171 EXPECT_TRUE(extreme_default.nan_float() != extreme_default.nan_float()); |
| 168 } | 172 } |
| 169 | 173 |
| 170 TEST(GeneratedMessageTest, Trigraph) { | 174 TEST(GeneratedMessageTest, Trigraph) { |
| 171 const unittest::TestExtremeDefaultValues& extreme_default = | 175 const unittest::TestExtremeDefaultValues& extreme_default = |
| 172 unittest::TestExtremeDefaultValues::default_instance(); | 176 unittest::TestExtremeDefaultValues::default_instance(); |
| 173 | 177 |
| 174 EXPECT_EQ("? ? ?? ?? ??? ?\?/ ?\?-", extreme_default.cpp_trigraph()); | 178 EXPECT_EQ("? ? ?? ?? ??? ?\?/ ?\?-", extreme_default.cpp_trigraph()); |
| 175 } | 179 } |
| 176 | 180 |
| 181 TEST(GeneratedMessageTest, ExtremeSmallIntegerDefault) { |
| 182 const unittest::TestExtremeDefaultValues& extreme_default = |
| 183 unittest::TestExtremeDefaultValues::default_instance(); |
| 184 EXPECT_EQ(-0x80000000, kint32min); |
| 185 EXPECT_EQ(GOOGLE_LONGLONG(-0x8000000000000000), kint64min); |
| 186 EXPECT_EQ(kint32min, extreme_default.really_small_int32()); |
| 187 EXPECT_EQ(kint64min, extreme_default.really_small_int64()); |
| 188 } |
| 189 |
| 177 TEST(GeneratedMessageTest, Accessors) { | 190 TEST(GeneratedMessageTest, Accessors) { |
| 178 // Set every field to a unique value then go back and check all those | 191 // Set every field to a unique value then go back and check all those |
| 179 // values. | 192 // values. |
| 180 unittest::TestAllTypes message; | 193 unittest::TestAllTypes message; |
| 181 | 194 |
| 182 TestUtil::SetAllFields(&message); | 195 TestUtil::SetAllFields(&message); |
| 183 TestUtil::ExpectAllFieldsSet(message); | 196 TestUtil::ExpectAllFieldsSet(message); |
| 184 | 197 |
| 185 TestUtil::ModifyRepeatedFields(&message); | 198 TestUtil::ModifyRepeatedFields(&message); |
| 186 TestUtil::ExpectRepeatedFieldsModified(message); | 199 TestUtil::ExpectRepeatedFieldsModified(message); |
| 187 } | 200 } |
| 188 | 201 |
| 189 TEST(GeneratedMessageTest, MutableStringDefault) { | 202 TEST(GeneratedMessageTest, MutableStringDefault) { |
| 190 // mutable_foo() for a string should return a string initialized to its | 203 // mutable_foo() for a string should return a string initialized to its |
| 191 // default value. | 204 // default value. |
| 192 unittest::TestAllTypes message; | 205 unittest::TestAllTypes message; |
| 193 | 206 |
| 194 EXPECT_EQ("hello", *message.mutable_default_string()); | 207 EXPECT_EQ("hello", *message.mutable_default_string()); |
| 195 | 208 |
| 196 // Note that the first time we call mutable_foo(), we get a newly-allocated | 209 // Note that the first time we call mutable_foo(), we get a newly-allocated |
| 197 // string, but if we clear it and call it again, we get the same object again. | 210 // string, but if we clear it and call it again, we get the same object again. |
| 198 // We should verify that it has its default value in both cases. | 211 // We should verify that it has its default value in both cases. |
| 199 message.set_default_string("blah"); | 212 message.set_default_string("blah"); |
| 200 message.Clear(); | 213 message.Clear(); |
| 201 | 214 |
| 202 EXPECT_EQ("hello", *message.mutable_default_string()); | 215 EXPECT_EQ("hello", *message.mutable_default_string()); |
| 203 } | 216 } |
| 204 | 217 |
| 218 TEST(GeneratedMessageTest, StringDefaults) { |
| 219 unittest::TestExtremeDefaultValues message; |
| 220 // Check if '\000' can be used in default string value. |
| 221 EXPECT_EQ(string("hel\000lo", 6), message.string_with_zero()); |
| 222 EXPECT_EQ(string("wor\000ld", 6), message.bytes_with_zero()); |
| 223 } |
| 224 |
| 205 TEST(GeneratedMessageTest, ReleaseString) { | 225 TEST(GeneratedMessageTest, ReleaseString) { |
| 206 // Check that release_foo() starts out NULL, and gives us a value | 226 // Check that release_foo() starts out NULL, and gives us a value |
| 207 // that we can delete after it's been set. | 227 // that we can delete after it's been set. |
| 208 unittest::TestAllTypes message; | 228 unittest::TestAllTypes message; |
| 209 | 229 |
| 210 EXPECT_EQ(NULL, message.release_default_string()); | 230 EXPECT_EQ(NULL, message.release_default_string()); |
| 211 EXPECT_FALSE(message.has_default_string()); | 231 EXPECT_FALSE(message.has_default_string()); |
| 212 EXPECT_EQ("hello", message.default_string()); | 232 EXPECT_EQ("hello", message.default_string()); |
| 213 | 233 |
| 214 message.set_default_string("blah"); | 234 message.set_default_string("blah"); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 237 message.release_optional_nested_message(); | 257 message.release_optional_nested_message(); |
| 238 EXPECT_FALSE(message.has_optional_nested_message()); | 258 EXPECT_FALSE(message.has_optional_nested_message()); |
| 239 ASSERT_TRUE(nest != NULL); | 259 ASSERT_TRUE(nest != NULL); |
| 240 EXPECT_EQ(1, nest->bb()); | 260 EXPECT_EQ(1, nest->bb()); |
| 241 delete nest; | 261 delete nest; |
| 242 | 262 |
| 243 EXPECT_EQ(NULL, message.release_optional_nested_message()); | 263 EXPECT_EQ(NULL, message.release_optional_nested_message()); |
| 244 EXPECT_FALSE(message.has_optional_nested_message()); | 264 EXPECT_FALSE(message.has_optional_nested_message()); |
| 245 } | 265 } |
| 246 | 266 |
| 267 TEST(GeneratedMessageTest, SetAllocatedString) { |
| 268 // Check that set_allocated_foo() works for strings. |
| 269 unittest::TestAllTypes message; |
| 270 |
| 271 EXPECT_FALSE(message.has_optional_string()); |
| 272 const string kHello("hello"); |
| 273 message.set_optional_string(kHello); |
| 274 EXPECT_TRUE(message.has_optional_string()); |
| 275 |
| 276 message.set_allocated_optional_string(NULL); |
| 277 EXPECT_FALSE(message.has_optional_string()); |
| 278 EXPECT_EQ("", message.optional_string()); |
| 279 |
| 280 message.set_allocated_optional_string(new string(kHello)); |
| 281 EXPECT_TRUE(message.has_optional_string()); |
| 282 EXPECT_EQ(kHello, message.optional_string()); |
| 283 } |
| 284 |
| 285 TEST(GeneratedMessageTest, SetAllocatedMessage) { |
| 286 // Check that set_allocated_foo() can be called in all cases. |
| 287 unittest::TestAllTypes message; |
| 288 |
| 289 EXPECT_FALSE(message.has_optional_nested_message()); |
| 290 |
| 291 message.mutable_optional_nested_message()->set_bb(1); |
| 292 EXPECT_TRUE(message.has_optional_nested_message()); |
| 293 |
| 294 message.set_allocated_optional_nested_message(NULL); |
| 295 EXPECT_FALSE(message.has_optional_nested_message()); |
| 296 EXPECT_EQ(&unittest::TestAllTypes::NestedMessage::default_instance(), |
| 297 &message.optional_nested_message()); |
| 298 |
| 299 message.mutable_optional_nested_message()->set_bb(1); |
| 300 unittest::TestAllTypes::NestedMessage* nest = |
| 301 message.release_optional_nested_message(); |
| 302 ASSERT_TRUE(nest != NULL); |
| 303 EXPECT_FALSE(message.has_optional_nested_message()); |
| 304 |
| 305 message.set_allocated_optional_nested_message(nest); |
| 306 EXPECT_TRUE(message.has_optional_nested_message()); |
| 307 EXPECT_EQ(1, message.optional_nested_message().bb()); |
| 308 } |
| 309 |
| 247 TEST(GeneratedMessageTest, Clear) { | 310 TEST(GeneratedMessageTest, Clear) { |
| 248 // Set every field to a unique value, clear the message, then check that | 311 // Set every field to a unique value, clear the message, then check that |
| 249 // it is cleared. | 312 // it is cleared. |
| 250 unittest::TestAllTypes message; | 313 unittest::TestAllTypes message; |
| 251 | 314 |
| 252 TestUtil::SetAllFields(&message); | 315 TestUtil::SetAllFields(&message); |
| 253 message.Clear(); | 316 message.Clear(); |
| 254 TestUtil::ExpectClear(message); | 317 TestUtil::ExpectClear(message); |
| 255 | 318 |
| 256 // Unlike with the defaults test, we do NOT expect that requesting embedded | 319 // Unlike with the defaults test, we do NOT expect that requesting embedded |
| (...skipping 431 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 688 // test_bad_identifiers.proto successfully compiled, then it works. The | 751 // test_bad_identifiers.proto successfully compiled, then it works. The |
| 689 // following is just a token usage to insure that the code is, in fact, | 752 // following is just a token usage to insure that the code is, in fact, |
| 690 // being compiled and linked. | 753 // being compiled and linked. |
| 691 | 754 |
| 692 protobuf_unittest::TestConflictingSymbolNames message; | 755 protobuf_unittest::TestConflictingSymbolNames message; |
| 693 message.set_uint32(1); | 756 message.set_uint32(1); |
| 694 EXPECT_EQ(3, message.ByteSize()); | 757 EXPECT_EQ(3, message.ByteSize()); |
| 695 | 758 |
| 696 message.set_friend_(5); | 759 message.set_friend_(5); |
| 697 EXPECT_EQ(5, message.friend_()); | 760 EXPECT_EQ(5, message.friend_()); |
| 761 |
| 762 // Instantiate extension template functions to test conflicting template |
| 763 // parameter names. |
| 764 typedef protobuf_unittest::TestConflictingSymbolNamesExtension ExtensionMessag
e; |
| 765 message.AddExtension(ExtensionMessage::repeated_int32_ext, 123); |
| 766 EXPECT_EQ(123, |
| 767 message.GetExtension(ExtensionMessage::repeated_int32_ext, 0)); |
| 698 } | 768 } |
| 699 | 769 |
| 700 #ifndef PROTOBUF_TEST_NO_DESCRIPTORS | 770 #ifndef PROTOBUF_TEST_NO_DESCRIPTORS |
| 701 | 771 |
| 702 TEST(GeneratedMessageTest, TestOptimizedForSize) { | 772 TEST(GeneratedMessageTest, TestOptimizedForSize) { |
| 703 // We rely on the tests in reflection_ops_unittest and wire_format_unittest | 773 // We rely on the tests in reflection_ops_unittest and wire_format_unittest |
| 704 // to really test that reflection-based methods work. Here we are mostly | 774 // to really test that reflection-based methods work. Here we are mostly |
| 705 // just making sure that TestOptimizedForSize actually builds and seems to | 775 // just making sure that TestOptimizedForSize actually builds and seems to |
| 706 // function. | 776 // function. |
| 707 | 777 |
| (...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 862 // Make sure we can take the address of _MIN, _MAX and _ARRAYSIZE. | 932 // Make sure we can take the address of _MIN, _MAX and _ARRAYSIZE. |
| 863 void* null_pointer = 0; // NULL may be integer-type, not pointer-type. | 933 void* null_pointer = 0; // NULL may be integer-type, not pointer-type. |
| 864 EXPECT_NE(null_pointer, &unittest::TestAllTypes::NestedEnum_MIN); | 934 EXPECT_NE(null_pointer, &unittest::TestAllTypes::NestedEnum_MIN); |
| 865 EXPECT_NE(null_pointer, &unittest::TestAllTypes::NestedEnum_MAX); | 935 EXPECT_NE(null_pointer, &unittest::TestAllTypes::NestedEnum_MAX); |
| 866 EXPECT_NE(null_pointer, &unittest::TestAllTypes::NestedEnum_ARRAYSIZE); | 936 EXPECT_NE(null_pointer, &unittest::TestAllTypes::NestedEnum_ARRAYSIZE); |
| 867 | 937 |
| 868 EXPECT_NE(null_pointer, &unittest::ForeignEnum_MIN); | 938 EXPECT_NE(null_pointer, &unittest::ForeignEnum_MIN); |
| 869 EXPECT_NE(null_pointer, &unittest::ForeignEnum_MAX); | 939 EXPECT_NE(null_pointer, &unittest::ForeignEnum_MAX); |
| 870 EXPECT_NE(null_pointer, &unittest::ForeignEnum_ARRAYSIZE); | 940 EXPECT_NE(null_pointer, &unittest::ForeignEnum_ARRAYSIZE); |
| 871 | 941 |
| 872 // Make sure we can use _MIN, _MAX and _ARRAYSIZE as switch cases. | 942 // Make sure we can use _MIN and _MAX as switch cases. |
| 873 switch (unittest::SPARSE_A) { | 943 switch (unittest::SPARSE_A) { |
| 874 case unittest::TestSparseEnum_MIN: | 944 case unittest::TestSparseEnum_MIN: |
| 875 case unittest::TestSparseEnum_MAX: | 945 case unittest::TestSparseEnum_MAX: |
| 876 case unittest::TestSparseEnum_ARRAYSIZE: | |
| 877 break; | 946 break; |
| 878 default: | 947 default: |
| 879 break; | 948 break; |
| 880 } | 949 } |
| 881 } | 950 } |
| 882 | 951 |
| 883 #ifndef PROTOBUF_TEST_NO_DESCRIPTORS | 952 #ifndef PROTOBUF_TEST_NO_DESCRIPTORS |
| 884 | 953 |
| 885 TEST(GeneratedEnumTest, Name) { | 954 TEST(GeneratedEnumTest, Name) { |
| 886 // "Names" in the presence of dup values are a bit arbitrary. | 955 // "Names" in the presence of dup values are a bit arbitrary. |
| (...skipping 385 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1272 DescriptorPool::generated_pool()->InternalIsFileLoaded( | 1341 DescriptorPool::generated_pool()->InternalIsFileLoaded( |
| 1273 "google/protobuf/unittest.proto")); | 1342 "google/protobuf/unittest.proto")); |
| 1274 } | 1343 } |
| 1275 | 1344 |
| 1276 } // namespace cpp_unittest | 1345 } // namespace cpp_unittest |
| 1277 | 1346 |
| 1278 } // namespace cpp | 1347 } // namespace cpp |
| 1279 } // namespace compiler | 1348 } // namespace compiler |
| 1280 } // namespace protobuf | 1349 } // namespace protobuf |
| 1281 } // namespace google | 1350 } // namespace google |
| OLD | NEW |