/thirdparty/breakpad/third_party/protobuf/protobuf/src/google/protobuf/dynamic_message_unittest.cc

http://github.com/tomahawk-player/tomahawk · C++ · 162 lines · 79 code · 27 blank · 56 comment · 5 complexity · f4b789f3843d6196b5223f3dcfb8bb4d MD5 · raw file

  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // http://code.google.com/p/protobuf/
  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. // Author: kenton@google.com (Kenton Varda)
  31. // Based on original Protocol Buffers design by
  32. // Sanjay Ghemawat, Jeff Dean, and others.
  33. //
  34. // Since the reflection interface for DynamicMessage is implemented by
  35. // GenericMessageReflection, the only thing we really have to test is
  36. // that DynamicMessage correctly sets up the information that
  37. // GenericMessageReflection needs to use. So, we focus on that in this
  38. // test. Other tests, such as generic_message_reflection_unittest and
  39. // reflection_ops_unittest, cover the rest of the functionality used by
  40. // DynamicMessage.
  41. #include <google/protobuf/stubs/common.h>
  42. #include <google/protobuf/dynamic_message.h>
  43. #include <google/protobuf/descriptor.h>
  44. #include <google/protobuf/descriptor.pb.h>
  45. #include <google/protobuf/test_util.h>
  46. #include <google/protobuf/unittest.pb.h>
  47. #include <google/protobuf/testing/googletest.h>
  48. #include <gtest/gtest.h>
  49. namespace google {
  50. namespace protobuf {
  51. class DynamicMessageTest : public testing::Test {
  52. protected:
  53. DescriptorPool pool_;
  54. DynamicMessageFactory factory_;
  55. const Descriptor* descriptor_;
  56. const Message* prototype_;
  57. const Descriptor* extensions_descriptor_;
  58. const Message* extensions_prototype_;
  59. const Descriptor* packed_descriptor_;
  60. const Message* packed_prototype_;
  61. DynamicMessageTest(): factory_(&pool_) {}
  62. virtual void SetUp() {
  63. // We want to make sure that DynamicMessage works (particularly with
  64. // extensions) even if we use descriptors that are *not* from compiled-in
  65. // types, so we make copies of the descriptors for unittest.proto and
  66. // unittest_import.proto.
  67. FileDescriptorProto unittest_file;
  68. FileDescriptorProto unittest_import_file;
  69. unittest::TestAllTypes::descriptor()->file()->CopyTo(&unittest_file);
  70. unittest_import::ImportMessage::descriptor()->file()->CopyTo(
  71. &unittest_import_file);
  72. ASSERT_TRUE(pool_.BuildFile(unittest_import_file) != NULL);
  73. ASSERT_TRUE(pool_.BuildFile(unittest_file) != NULL);
  74. descriptor_ = pool_.FindMessageTypeByName("protobuf_unittest.TestAllTypes");
  75. ASSERT_TRUE(descriptor_ != NULL);
  76. prototype_ = factory_.GetPrototype(descriptor_);
  77. extensions_descriptor_ =
  78. pool_.FindMessageTypeByName("protobuf_unittest.TestAllExtensions");
  79. ASSERT_TRUE(extensions_descriptor_ != NULL);
  80. extensions_prototype_ = factory_.GetPrototype(extensions_descriptor_);
  81. packed_descriptor_ =
  82. pool_.FindMessageTypeByName("protobuf_unittest.TestPackedTypes");
  83. ASSERT_TRUE(packed_descriptor_ != NULL);
  84. packed_prototype_ = factory_.GetPrototype(packed_descriptor_);
  85. }
  86. };
  87. TEST_F(DynamicMessageTest, Descriptor) {
  88. // Check that the descriptor on the DynamicMessage matches the descriptor
  89. // passed to GetPrototype().
  90. EXPECT_EQ(prototype_->GetDescriptor(), descriptor_);
  91. }
  92. TEST_F(DynamicMessageTest, OnePrototype) {
  93. // Check that requesting the same prototype twice produces the same object.
  94. EXPECT_EQ(prototype_, factory_.GetPrototype(descriptor_));
  95. }
  96. TEST_F(DynamicMessageTest, Defaults) {
  97. // Check that all default values are set correctly in the initial message.
  98. TestUtil::ReflectionTester reflection_tester(descriptor_);
  99. reflection_tester.ExpectClearViaReflection(*prototype_);
  100. }
  101. TEST_F(DynamicMessageTest, IndependentOffsets) {
  102. // Check that all fields have independent offsets by setting each
  103. // one to a unique value then checking that they all still have those
  104. // unique values (i.e. they don't stomp each other).
  105. scoped_ptr<Message> message(prototype_->New());
  106. TestUtil::ReflectionTester reflection_tester(descriptor_);
  107. reflection_tester.SetAllFieldsViaReflection(message.get());
  108. reflection_tester.ExpectAllFieldsSetViaReflection(*message);
  109. }
  110. TEST_F(DynamicMessageTest, Extensions) {
  111. // Check that extensions work.
  112. scoped_ptr<Message> message(extensions_prototype_->New());
  113. TestUtil::ReflectionTester reflection_tester(extensions_descriptor_);
  114. reflection_tester.SetAllFieldsViaReflection(message.get());
  115. reflection_tester.ExpectAllFieldsSetViaReflection(*message);
  116. }
  117. TEST_F(DynamicMessageTest, PackedFields) {
  118. // Check that packed fields work properly.
  119. scoped_ptr<Message> message(packed_prototype_->New());
  120. TestUtil::ReflectionTester reflection_tester(packed_descriptor_);
  121. reflection_tester.SetPackedFieldsViaReflection(message.get());
  122. reflection_tester.ExpectPackedFieldsSetViaReflection(*message);
  123. }
  124. TEST_F(DynamicMessageTest, SpaceUsed) {
  125. // Test that SpaceUsed() works properly
  126. // Since we share the implementation with generated messages, we don't need
  127. // to test very much here. Just make sure it appears to be working.
  128. scoped_ptr<Message> message(prototype_->New());
  129. TestUtil::ReflectionTester reflection_tester(descriptor_);
  130. int initial_space_used = message->SpaceUsed();
  131. reflection_tester.SetAllFieldsViaReflection(message.get());
  132. EXPECT_LT(initial_space_used, message->SpaceUsed());
  133. }
  134. } // namespace protobuf
  135. } // namespace google