诸暨麻将添加redis
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 
 

327 rindas
13 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. // 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 <memory>
  42. #include <google/protobuf/test_util.h>
  43. #include <google/protobuf/unittest.pb.h>
  44. #include <google/protobuf/unittest_no_field_presence.pb.h>
  45. #include <google/protobuf/descriptor.pb.h>
  46. #include <google/protobuf/descriptor.h>
  47. #include <google/protobuf/dynamic_message.h>
  48. #include <google/protobuf/stubs/logging.h>
  49. #include <google/protobuf/stubs/common.h>
  50. #include <google/protobuf/testing/googletest.h>
  51. #include <gtest/gtest.h>
  52. namespace google {
  53. namespace protobuf {
  54. class DynamicMessageTest : public ::testing::TestWithParam<bool> {
  55. protected:
  56. DescriptorPool pool_;
  57. DynamicMessageFactory factory_;
  58. const Descriptor* descriptor_;
  59. const Message* prototype_;
  60. const Descriptor* extensions_descriptor_;
  61. const Message* extensions_prototype_;
  62. const Descriptor* packed_descriptor_;
  63. const Message* packed_prototype_;
  64. const Descriptor* oneof_descriptor_;
  65. const Message* oneof_prototype_;
  66. const Descriptor* proto3_descriptor_;
  67. const Message* proto3_prototype_;
  68. DynamicMessageTest() : factory_(&pool_) {}
  69. virtual void SetUp() {
  70. // We want to make sure that DynamicMessage works (particularly with
  71. // extensions) even if we use descriptors that are *not* from compiled-in
  72. // types, so we make copies of the descriptors for unittest.proto and
  73. // unittest_import.proto.
  74. FileDescriptorProto unittest_file;
  75. FileDescriptorProto unittest_import_file;
  76. FileDescriptorProto unittest_import_public_file;
  77. FileDescriptorProto unittest_no_field_presence_file;
  78. unittest::TestAllTypes::descriptor()->file()->CopyTo(&unittest_file);
  79. unittest_import::ImportMessage::descriptor()->file()->CopyTo(
  80. &unittest_import_file);
  81. unittest_import::PublicImportMessage::descriptor()->file()->CopyTo(
  82. &unittest_import_public_file);
  83. proto2_nofieldpresence_unittest::TestAllTypes::descriptor()->file()->CopyTo(
  84. &unittest_no_field_presence_file);
  85. ASSERT_TRUE(pool_.BuildFile(unittest_import_public_file) != NULL);
  86. ASSERT_TRUE(pool_.BuildFile(unittest_import_file) != NULL);
  87. ASSERT_TRUE(pool_.BuildFile(unittest_file) != NULL);
  88. ASSERT_TRUE(pool_.BuildFile(unittest_no_field_presence_file) != NULL);
  89. descriptor_ = pool_.FindMessageTypeByName("protobuf_unittest.TestAllTypes");
  90. ASSERT_TRUE(descriptor_ != NULL);
  91. prototype_ = factory_.GetPrototype(descriptor_);
  92. extensions_descriptor_ =
  93. pool_.FindMessageTypeByName("protobuf_unittest.TestAllExtensions");
  94. ASSERT_TRUE(extensions_descriptor_ != NULL);
  95. extensions_prototype_ = factory_.GetPrototype(extensions_descriptor_);
  96. packed_descriptor_ =
  97. pool_.FindMessageTypeByName("protobuf_unittest.TestPackedTypes");
  98. ASSERT_TRUE(packed_descriptor_ != NULL);
  99. packed_prototype_ = factory_.GetPrototype(packed_descriptor_);
  100. oneof_descriptor_ =
  101. pool_.FindMessageTypeByName("protobuf_unittest.TestOneof2");
  102. ASSERT_TRUE(oneof_descriptor_ != NULL);
  103. oneof_prototype_ = factory_.GetPrototype(oneof_descriptor_);
  104. proto3_descriptor_ = pool_.FindMessageTypeByName(
  105. "proto2_nofieldpresence_unittest.TestAllTypes");
  106. ASSERT_TRUE(proto3_descriptor_ != NULL);
  107. proto3_prototype_ = factory_.GetPrototype(proto3_descriptor_);
  108. }
  109. };
  110. TEST_F(DynamicMessageTest, Descriptor) {
  111. // Check that the descriptor on the DynamicMessage matches the descriptor
  112. // passed to GetPrototype().
  113. EXPECT_EQ(prototype_->GetDescriptor(), descriptor_);
  114. }
  115. TEST_F(DynamicMessageTest, OnePrototype) {
  116. // Check that requesting the same prototype twice produces the same object.
  117. EXPECT_EQ(prototype_, factory_.GetPrototype(descriptor_));
  118. }
  119. TEST_F(DynamicMessageTest, Defaults) {
  120. // Check that all default values are set correctly in the initial message.
  121. TestUtil::ReflectionTester reflection_tester(descriptor_);
  122. reflection_tester.ExpectClearViaReflection(*prototype_);
  123. }
  124. TEST_P(DynamicMessageTest, IndependentOffsets) {
  125. // Check that all fields have independent offsets by setting each
  126. // one to a unique value then checking that they all still have those
  127. // unique values (i.e. they don't stomp each other).
  128. Arena arena;
  129. Message* message = prototype_->New(GetParam() ? &arena : NULL);
  130. TestUtil::ReflectionTester reflection_tester(descriptor_);
  131. reflection_tester.SetAllFieldsViaReflection(message);
  132. reflection_tester.ExpectAllFieldsSetViaReflection(*message);
  133. if (!GetParam()) {
  134. delete message;
  135. }
  136. }
  137. TEST_P(DynamicMessageTest, Extensions) {
  138. // Check that extensions work.
  139. Arena arena;
  140. Message* message = extensions_prototype_->New(GetParam() ? &arena : NULL);
  141. TestUtil::ReflectionTester reflection_tester(extensions_descriptor_);
  142. reflection_tester.SetAllFieldsViaReflection(message);
  143. reflection_tester.ExpectAllFieldsSetViaReflection(*message);
  144. if (!GetParam()) {
  145. delete message;
  146. }
  147. }
  148. TEST_P(DynamicMessageTest, PackedFields) {
  149. // Check that packed fields work properly.
  150. Arena arena;
  151. Message* message = packed_prototype_->New(GetParam() ? &arena : NULL);
  152. TestUtil::ReflectionTester reflection_tester(packed_descriptor_);
  153. reflection_tester.SetPackedFieldsViaReflection(message);
  154. reflection_tester.ExpectPackedFieldsSetViaReflection(*message);
  155. if (!GetParam()) {
  156. delete message;
  157. }
  158. }
  159. TEST_P(DynamicMessageTest, Oneof) {
  160. // Check that oneof fields work properly.
  161. Arena arena;
  162. Message* message = oneof_prototype_->New(GetParam() ? &arena : NULL);
  163. // Check default values.
  164. const Descriptor* descriptor = message->GetDescriptor();
  165. const Reflection* reflection = message->GetReflection();
  166. EXPECT_EQ(0, reflection->GetInt32(*message,
  167. descriptor->FindFieldByName("foo_int")));
  168. EXPECT_EQ("", reflection->GetString(
  169. *message, descriptor->FindFieldByName("foo_string")));
  170. EXPECT_EQ("", reflection->GetString(*message,
  171. descriptor->FindFieldByName("foo_cord")));
  172. EXPECT_EQ("", reflection->GetString(
  173. *message, descriptor->FindFieldByName("foo_string_piece")));
  174. EXPECT_EQ("", reflection->GetString(
  175. *message, descriptor->FindFieldByName("foo_bytes")));
  176. EXPECT_EQ(
  177. unittest::TestOneof2::FOO,
  178. reflection->GetEnum(*message, descriptor->FindFieldByName("foo_enum"))
  179. ->number());
  180. const Descriptor* nested_descriptor;
  181. const Message* nested_prototype;
  182. nested_descriptor =
  183. pool_.FindMessageTypeByName("protobuf_unittest.TestOneof2.NestedMessage");
  184. nested_prototype = factory_.GetPrototype(nested_descriptor);
  185. EXPECT_EQ(nested_prototype,
  186. &reflection->GetMessage(
  187. *message, descriptor->FindFieldByName("foo_message")));
  188. const Descriptor* foogroup_descriptor;
  189. const Message* foogroup_prototype;
  190. foogroup_descriptor =
  191. pool_.FindMessageTypeByName("protobuf_unittest.TestOneof2.FooGroup");
  192. foogroup_prototype = factory_.GetPrototype(foogroup_descriptor);
  193. EXPECT_EQ(foogroup_prototype,
  194. &reflection->GetMessage(*message,
  195. descriptor->FindFieldByName("foogroup")));
  196. EXPECT_NE(foogroup_prototype,
  197. &reflection->GetMessage(
  198. *message, descriptor->FindFieldByName("foo_lazy_message")));
  199. EXPECT_EQ(5, reflection->GetInt32(*message,
  200. descriptor->FindFieldByName("bar_int")));
  201. EXPECT_EQ("STRING", reflection->GetString(
  202. *message, descriptor->FindFieldByName("bar_string")));
  203. EXPECT_EQ("CORD", reflection->GetString(
  204. *message, descriptor->FindFieldByName("bar_cord")));
  205. EXPECT_EQ("SPIECE",
  206. reflection->GetString(
  207. *message, descriptor->FindFieldByName("bar_string_piece")));
  208. EXPECT_EQ("BYTES", reflection->GetString(
  209. *message, descriptor->FindFieldByName("bar_bytes")));
  210. EXPECT_EQ(
  211. unittest::TestOneof2::BAR,
  212. reflection->GetEnum(*message, descriptor->FindFieldByName("bar_enum"))
  213. ->number());
  214. // Check set functions.
  215. TestUtil::ReflectionTester reflection_tester(oneof_descriptor_);
  216. reflection_tester.SetOneofViaReflection(message);
  217. reflection_tester.ExpectOneofSetViaReflection(*message);
  218. if (!GetParam()) {
  219. delete message;
  220. }
  221. }
  222. TEST_P(DynamicMessageTest, SpaceUsed) {
  223. // Test that SpaceUsed() works properly
  224. // Since we share the implementation with generated messages, we don't need
  225. // to test very much here. Just make sure it appears to be working.
  226. Arena arena;
  227. Message* message = prototype_->New(GetParam() ? &arena : NULL);
  228. TestUtil::ReflectionTester reflection_tester(descriptor_);
  229. int initial_space_used = message->SpaceUsed();
  230. reflection_tester.SetAllFieldsViaReflection(message);
  231. EXPECT_LT(initial_space_used, message->SpaceUsed());
  232. if (!GetParam()) {
  233. delete message;
  234. }
  235. }
  236. TEST_F(DynamicMessageTest, Arena) {
  237. Arena arena;
  238. Message* message = prototype_->New(&arena);
  239. Message* extension_message = extensions_prototype_->New(&arena);
  240. Message* packed_message = packed_prototype_->New(&arena);
  241. Message* oneof_message = oneof_prototype_->New(&arena);
  242. // avoid unused-variable error.
  243. (void)message;
  244. (void)extension_message;
  245. (void)packed_message;
  246. (void)oneof_message;
  247. // Return without freeing: should not leak.
  248. }
  249. TEST_F(DynamicMessageTest, Proto3) {
  250. Message* message = proto3_prototype_->New();
  251. const Reflection* refl = message->GetReflection();
  252. const Descriptor* desc = message->GetDescriptor();
  253. // Just test a single primtive and single message field here to make sure we
  254. // are getting the no-field-presence semantics elsewhere. DynamicMessage uses
  255. // GeneratedMessageReflection under the hood, so the rest should be fine as
  256. // long as GMR recognizes that we're using a proto3 message.
  257. const FieldDescriptor* optional_int32 =
  258. desc->FindFieldByName("optional_int32");
  259. const FieldDescriptor* optional_msg =
  260. desc->FindFieldByName("optional_nested_message");
  261. EXPECT_TRUE(optional_int32 != NULL);
  262. EXPECT_TRUE(optional_msg != NULL);
  263. EXPECT_EQ(false, refl->HasField(*message, optional_int32));
  264. refl->SetInt32(message, optional_int32, 42);
  265. EXPECT_EQ(true, refl->HasField(*message, optional_int32));
  266. refl->SetInt32(message, optional_int32, 0);
  267. EXPECT_EQ(false, refl->HasField(*message, optional_int32));
  268. EXPECT_EQ(false, refl->HasField(*message, optional_msg));
  269. refl->MutableMessage(message, optional_msg);
  270. EXPECT_EQ(true, refl->HasField(*message, optional_msg));
  271. delete refl->ReleaseMessage(message, optional_msg);
  272. EXPECT_EQ(false, refl->HasField(*message, optional_msg));
  273. // Also ensure that the default instance handles field presence properly.
  274. EXPECT_EQ(false, refl->HasField(*proto3_prototype_, optional_msg));
  275. delete message;
  276. }
  277. INSTANTIATE_TEST_SUITE_P(UseArena, DynamicMessageTest, ::testing::Bool());
  278. } // namespace protobuf
  279. } // namespace google