诸暨麻将添加redis
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

200 行
8.1 KiB

  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // https://developers.google.com/protocol-buffers/
  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. #include <memory>
  31. #include <string>
  32. #include <vector>
  33. #include <google/protobuf/test_util.h>
  34. #include <google/protobuf/unittest.pb.h>
  35. #include <google/protobuf/unittest_proto3_arena.pb.h>
  36. #include <google/protobuf/arena.h>
  37. #include <google/protobuf/testing/googletest.h>
  38. #include <gtest/gtest.h>
  39. using proto3_arena_unittest::TestAllTypes;
  40. namespace google {
  41. namespace protobuf {
  42. namespace {
  43. // We selectively set/check a few representative fields rather than all fields
  44. // as this test is only expected to cover the basics of arena support.
  45. void SetAllFields(TestAllTypes* m) {
  46. m->set_optional_int32(100);
  47. m->set_optional_string("asdf");
  48. m->set_optional_bytes("jkl;");
  49. m->mutable_optional_nested_message()->set_bb(42);
  50. m->mutable_optional_foreign_message()->set_c(43);
  51. m->set_optional_nested_enum(proto3_arena_unittest::TestAllTypes::BAZ);
  52. m->set_optional_foreign_enum(proto3_arena_unittest::FOREIGN_BAZ);
  53. m->mutable_optional_lazy_message()->set_bb(45);
  54. m->add_repeated_int32(100);
  55. m->add_repeated_string("asdf");
  56. m->add_repeated_bytes("jkl;");
  57. m->add_repeated_nested_message()->set_bb(46);
  58. m->add_repeated_foreign_message()->set_c(47);
  59. m->add_repeated_nested_enum(proto3_arena_unittest::TestAllTypes::BAZ);
  60. m->add_repeated_foreign_enum(proto3_arena_unittest::FOREIGN_BAZ);
  61. m->add_repeated_lazy_message()->set_bb(49);
  62. m->set_oneof_uint32(1);
  63. m->mutable_oneof_nested_message()->set_bb(50);
  64. m->set_oneof_string("test"); // only this one remains set
  65. }
  66. void ExpectAllFieldsSet(const TestAllTypes& m) {
  67. EXPECT_EQ(100, m.optional_int32());
  68. EXPECT_EQ("asdf", m.optional_string());
  69. EXPECT_EQ("jkl;", m.optional_bytes());
  70. EXPECT_EQ(true, m.has_optional_nested_message());
  71. EXPECT_EQ(42, m.optional_nested_message().bb());
  72. EXPECT_EQ(true, m.has_optional_foreign_message());
  73. EXPECT_EQ(43, m.optional_foreign_message().c());
  74. EXPECT_EQ(proto3_arena_unittest::TestAllTypes::BAZ, m.optional_nested_enum());
  75. EXPECT_EQ(proto3_arena_unittest::FOREIGN_BAZ, m.optional_foreign_enum());
  76. EXPECT_EQ(true, m.has_optional_lazy_message());
  77. EXPECT_EQ(45, m.optional_lazy_message().bb());
  78. EXPECT_EQ(1, m.repeated_int32_size());
  79. EXPECT_EQ(100, m.repeated_int32(0));
  80. EXPECT_EQ(1, m.repeated_string_size());
  81. EXPECT_EQ("asdf", m.repeated_string(0));
  82. EXPECT_EQ(1, m.repeated_bytes_size());
  83. EXPECT_EQ("jkl;", m.repeated_bytes(0));
  84. EXPECT_EQ(1, m.repeated_nested_message_size());
  85. EXPECT_EQ(46, m.repeated_nested_message(0).bb());
  86. EXPECT_EQ(1, m.repeated_foreign_message_size());
  87. EXPECT_EQ(47, m.repeated_foreign_message(0).c());
  88. EXPECT_EQ(1, m.repeated_nested_enum_size());
  89. EXPECT_EQ(proto3_arena_unittest::TestAllTypes::BAZ,
  90. m.repeated_nested_enum(0));
  91. EXPECT_EQ(1, m.repeated_foreign_enum_size());
  92. EXPECT_EQ(proto3_arena_unittest::FOREIGN_BAZ, m.repeated_foreign_enum(0));
  93. EXPECT_EQ(1, m.repeated_lazy_message_size());
  94. EXPECT_EQ(49, m.repeated_lazy_message(0).bb());
  95. EXPECT_EQ(proto3_arena_unittest::TestAllTypes::kOneofString,
  96. m.oneof_field_case());
  97. EXPECT_EQ("test", m.oneof_string());
  98. }
  99. // In this file we only test some basic functionalities of arena support in
  100. // proto3 and expect the arena support to be fully tested in proto2 unittests
  101. // because proto3 shares most code with proto2.
  102. TEST(Proto3ArenaTest, Parsing) {
  103. TestAllTypes original;
  104. SetAllFields(&original);
  105. Arena arena;
  106. TestAllTypes* arena_message = Arena::CreateMessage<TestAllTypes>(&arena);
  107. arena_message->ParseFromString(original.SerializeAsString());
  108. ExpectAllFieldsSet(*arena_message);
  109. }
  110. TEST(Proto3ArenaTest, UnknownFields) {
  111. TestAllTypes original;
  112. SetAllFields(&original);
  113. Arena arena;
  114. TestAllTypes* arena_message = Arena::CreateMessage<TestAllTypes>(&arena);
  115. arena_message->ParseFromString(original.SerializeAsString());
  116. ExpectAllFieldsSet(*arena_message);
  117. // In proto3 we can still get a pointer to the UnknownFieldSet through
  118. // reflection API.
  119. UnknownFieldSet* unknown_fields =
  120. arena_message->GetReflection()->MutableUnknownFields(arena_message);
  121. // We can modify this UnknownFieldSet.
  122. unknown_fields->AddVarint(1, 2);
  123. // And the unknown fields should be changed.
  124. ASSERT_NE(original.ByteSize(), arena_message->ByteSize());
  125. ASSERT_FALSE(
  126. arena_message->GetReflection()->GetUnknownFields(*arena_message).empty());
  127. }
  128. TEST(Proto3ArenaTest, Swap) {
  129. Arena arena1;
  130. Arena arena2;
  131. // Test Swap().
  132. TestAllTypes* arena1_message = Arena::CreateMessage<TestAllTypes>(&arena1);
  133. TestAllTypes* arena2_message = Arena::CreateMessage<TestAllTypes>(&arena2);
  134. arena1_message->Swap(arena2_message);
  135. EXPECT_EQ(&arena1, arena1_message->GetArena());
  136. EXPECT_EQ(&arena2, arena2_message->GetArena());
  137. }
  138. TEST(Proto3ArenaTest, SetAllocatedMessage) {
  139. Arena arena;
  140. TestAllTypes* arena_message = Arena::CreateMessage<TestAllTypes>(&arena);
  141. TestAllTypes::NestedMessage* nested = new TestAllTypes::NestedMessage;
  142. nested->set_bb(118);
  143. arena_message->set_allocated_optional_nested_message(nested);
  144. EXPECT_EQ(118, arena_message->optional_nested_message().bb());
  145. }
  146. TEST(Proto3ArenaTest, ReleaseMessage) {
  147. Arena arena;
  148. TestAllTypes* arena_message = Arena::CreateMessage<TestAllTypes>(&arena);
  149. arena_message->mutable_optional_nested_message()->set_bb(118);
  150. std::unique_ptr<TestAllTypes::NestedMessage> nested(
  151. arena_message->release_optional_nested_message());
  152. EXPECT_EQ(118, nested->bb());
  153. }
  154. TEST(Proto3ArenaTest, MessageFieldClear) {
  155. // GitHub issue #310: https://github.com/protocolbuffers/protobuf/issues/310
  156. Arena arena;
  157. TestAllTypes* arena_message = Arena::CreateMessage<TestAllTypes>(&arena);
  158. arena_message->mutable_optional_nested_message()->set_bb(118);
  159. // This should not crash, but prior to the bugfix, it tried to use `operator
  160. // delete` the nested message (which is on the arena):
  161. arena_message->Clear();
  162. }
  163. TEST(Proto3ArenaTest, MessageFieldClearViaReflection) {
  164. Arena arena;
  165. TestAllTypes* message = Arena::CreateMessage<TestAllTypes>(&arena);
  166. const Reflection* r = message->GetReflection();
  167. const Descriptor* d = message->GetDescriptor();
  168. const FieldDescriptor* msg_field =
  169. d->FindFieldByName("optional_nested_message");
  170. message->mutable_optional_nested_message()->set_bb(1);
  171. r->ClearField(message, msg_field);
  172. EXPECT_FALSE(message->has_optional_nested_message());
  173. EXPECT_EQ(0, message->optional_nested_message().bb());
  174. }
  175. } // namespace
  176. } // namespace protobuf
  177. } // namespace google