PageRenderTime 37ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 1ms

/src/google/protobuf/lite_unittest.cc

http://protobuf.googlecode.com/
C++ | 185 lines | 128 code | 22 blank | 35 comment | 8 complexity | aaa1bcb1b07f5e9888d0b22f27e40239 MD5 | raw file
Possible License(s): BSD-3-Clause
  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. #include <string>
  32. #include <iostream>
  33. #include <google/protobuf/stubs/common.h>
  34. #include <google/protobuf/test_util_lite.h>
  35. #include <google/protobuf/unittest_lite.pb.h>
  36. using namespace std;
  37. namespace {
  38. // Helper methods to test parsing merge behavior.
  39. void ExpectMessageMerged(const google::protobuf::unittest::TestAllTypesLite& message) {
  40. GOOGLE_CHECK(message.optional_int32() == 3);
  41. GOOGLE_CHECK(message.optional_int64() == 2);
  42. GOOGLE_CHECK(message.optional_string() == "hello");
  43. }
  44. void AssignParsingMergeMessages(
  45. google::protobuf::unittest::TestAllTypesLite* msg1,
  46. google::protobuf::unittest::TestAllTypesLite* msg2,
  47. google::protobuf::unittest::TestAllTypesLite* msg3) {
  48. msg1->set_optional_int32(1);
  49. msg2->set_optional_int64(2);
  50. msg3->set_optional_int32(3);
  51. msg3->set_optional_string("hello");
  52. }
  53. } // namespace
  54. int main(int argc, char* argv[]) {
  55. string data, packed_data;
  56. {
  57. protobuf_unittest::TestAllTypesLite message, message2, message3;
  58. google::protobuf::TestUtilLite::ExpectClear(message);
  59. google::protobuf::TestUtilLite::SetAllFields(&message);
  60. message2.CopyFrom(message);
  61. data = message.SerializeAsString();
  62. message3.ParseFromString(data);
  63. google::protobuf::TestUtilLite::ExpectAllFieldsSet(message);
  64. google::protobuf::TestUtilLite::ExpectAllFieldsSet(message2);
  65. google::protobuf::TestUtilLite::ExpectAllFieldsSet(message3);
  66. google::protobuf::TestUtilLite::ModifyRepeatedFields(&message);
  67. google::protobuf::TestUtilLite::ExpectRepeatedFieldsModified(message);
  68. message.Clear();
  69. google::protobuf::TestUtilLite::ExpectClear(message);
  70. }
  71. {
  72. protobuf_unittest::TestAllExtensionsLite message, message2, message3;
  73. google::protobuf::TestUtilLite::ExpectExtensionsClear(message);
  74. google::protobuf::TestUtilLite::SetAllExtensions(&message);
  75. message2.CopyFrom(message);
  76. string extensions_data = message.SerializeAsString();
  77. GOOGLE_CHECK(extensions_data == data);
  78. message3.ParseFromString(extensions_data);
  79. google::protobuf::TestUtilLite::ExpectAllExtensionsSet(message);
  80. google::protobuf::TestUtilLite::ExpectAllExtensionsSet(message2);
  81. google::protobuf::TestUtilLite::ExpectAllExtensionsSet(message3);
  82. google::protobuf::TestUtilLite::ModifyRepeatedExtensions(&message);
  83. google::protobuf::TestUtilLite::ExpectRepeatedExtensionsModified(message);
  84. message.Clear();
  85. google::protobuf::TestUtilLite::ExpectExtensionsClear(message);
  86. }
  87. {
  88. protobuf_unittest::TestPackedTypesLite message, message2, message3;
  89. google::protobuf::TestUtilLite::ExpectPackedClear(message);
  90. google::protobuf::TestUtilLite::SetPackedFields(&message);
  91. message2.CopyFrom(message);
  92. packed_data = message.SerializeAsString();
  93. message3.ParseFromString(packed_data);
  94. google::protobuf::TestUtilLite::ExpectPackedFieldsSet(message);
  95. google::protobuf::TestUtilLite::ExpectPackedFieldsSet(message2);
  96. google::protobuf::TestUtilLite::ExpectPackedFieldsSet(message3);
  97. google::protobuf::TestUtilLite::ModifyPackedFields(&message);
  98. google::protobuf::TestUtilLite::ExpectPackedFieldsModified(message);
  99. message.Clear();
  100. google::protobuf::TestUtilLite::ExpectPackedClear(message);
  101. }
  102. {
  103. protobuf_unittest::TestPackedExtensionsLite message, message2, message3;
  104. google::protobuf::TestUtilLite::ExpectPackedExtensionsClear(message);
  105. google::protobuf::TestUtilLite::SetPackedExtensions(&message);
  106. message2.CopyFrom(message);
  107. string packed_extensions_data = message.SerializeAsString();
  108. GOOGLE_CHECK(packed_extensions_data == packed_data);
  109. message3.ParseFromString(packed_extensions_data);
  110. google::protobuf::TestUtilLite::ExpectPackedExtensionsSet(message);
  111. google::protobuf::TestUtilLite::ExpectPackedExtensionsSet(message2);
  112. google::protobuf::TestUtilLite::ExpectPackedExtensionsSet(message3);
  113. google::protobuf::TestUtilLite::ModifyPackedExtensions(&message);
  114. google::protobuf::TestUtilLite::ExpectPackedExtensionsModified(message);
  115. message.Clear();
  116. google::protobuf::TestUtilLite::ExpectPackedExtensionsClear(message);
  117. }
  118. {
  119. // Test that if an optional or required message/group field appears multiple
  120. // times in the input, they need to be merged.
  121. google::protobuf::unittest::TestParsingMergeLite::RepeatedFieldsGenerator generator;
  122. google::protobuf::unittest::TestAllTypesLite* msg1;
  123. google::protobuf::unittest::TestAllTypesLite* msg2;
  124. google::protobuf::unittest::TestAllTypesLite* msg3;
  125. #define ASSIGN_REPEATED_FIELD(FIELD) \
  126. msg1 = generator.add_##FIELD(); \
  127. msg2 = generator.add_##FIELD(); \
  128. msg3 = generator.add_##FIELD(); \
  129. AssignParsingMergeMessages(msg1, msg2, msg3)
  130. ASSIGN_REPEATED_FIELD(field1);
  131. ASSIGN_REPEATED_FIELD(field2);
  132. ASSIGN_REPEATED_FIELD(field3);
  133. ASSIGN_REPEATED_FIELD(ext1);
  134. ASSIGN_REPEATED_FIELD(ext2);
  135. #undef ASSIGN_REPEATED_FIELD
  136. #define ASSIGN_REPEATED_GROUP(FIELD) \
  137. msg1 = generator.add_##FIELD()->mutable_field1(); \
  138. msg2 = generator.add_##FIELD()->mutable_field1(); \
  139. msg3 = generator.add_##FIELD()->mutable_field1(); \
  140. AssignParsingMergeMessages(msg1, msg2, msg3)
  141. ASSIGN_REPEATED_GROUP(group1);
  142. ASSIGN_REPEATED_GROUP(group2);
  143. #undef ASSIGN_REPEATED_GROUP
  144. string buffer;
  145. generator.SerializeToString(&buffer);
  146. google::protobuf::unittest::TestParsingMergeLite parsing_merge;
  147. parsing_merge.ParseFromString(buffer);
  148. // Required and optional fields should be merged.
  149. ExpectMessageMerged(parsing_merge.required_all_types());
  150. ExpectMessageMerged(parsing_merge.optional_all_types());
  151. ExpectMessageMerged(
  152. parsing_merge.optionalgroup().optional_group_all_types());
  153. ExpectMessageMerged(parsing_merge.GetExtension(
  154. google::protobuf::unittest::TestParsingMergeLite::optional_ext));
  155. // Repeated fields should not be merged.
  156. GOOGLE_CHECK(parsing_merge.repeated_all_types_size() == 3);
  157. GOOGLE_CHECK(parsing_merge.repeatedgroup_size() == 3);
  158. GOOGLE_CHECK(parsing_merge.ExtensionSize(
  159. google::protobuf::unittest::TestParsingMergeLite::repeated_ext) == 3);
  160. }
  161. cout << "PASS" << endl;
  162. return 0;
  163. }