诸暨麻将添加redis
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

168 Zeilen
5.9 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 <google/protobuf/any_test.pb.h>
  31. #include <google/protobuf/unittest.pb.h>
  32. #include <gtest/gtest.h>
  33. namespace google {
  34. namespace protobuf {
  35. namespace {
  36. TEST(AnyTest, TestPackAndUnpack) {
  37. protobuf_unittest::TestAny submessage;
  38. submessage.set_int32_value(12345);
  39. protobuf_unittest::TestAny message;
  40. message.mutable_any_value()->PackFrom(submessage);
  41. std::string data = message.SerializeAsString();
  42. ASSERT_TRUE(message.ParseFromString(data));
  43. EXPECT_TRUE(message.has_any_value());
  44. submessage.Clear();
  45. ASSERT_TRUE(message.any_value().UnpackTo(&submessage));
  46. EXPECT_EQ(12345, submessage.int32_value());
  47. }
  48. TEST(AnyTest, TestUnpackWithTypeMismatch) {
  49. protobuf_unittest::TestAny payload;
  50. payload.set_int32_value(13);
  51. google::protobuf::Any any;
  52. any.PackFrom(payload);
  53. // Attempt to unpack into the wrong type.
  54. protobuf_unittest::TestAllTypes dest;
  55. EXPECT_FALSE(any.UnpackTo(&dest));
  56. }
  57. TEST(AnyTest, TestPackAndUnpackAny) {
  58. // We can pack a Any message inside another Any message.
  59. protobuf_unittest::TestAny submessage;
  60. submessage.set_int32_value(12345);
  61. google::protobuf::Any any;
  62. any.PackFrom(submessage);
  63. protobuf_unittest::TestAny message;
  64. message.mutable_any_value()->PackFrom(any);
  65. std::string data = message.SerializeAsString();
  66. ASSERT_TRUE(message.ParseFromString(data));
  67. EXPECT_TRUE(message.has_any_value());
  68. any.Clear();
  69. submessage.Clear();
  70. ASSERT_TRUE(message.any_value().UnpackTo(&any));
  71. ASSERT_TRUE(any.UnpackTo(&submessage));
  72. EXPECT_EQ(12345, submessage.int32_value());
  73. }
  74. TEST(AnyTest, TestPackWithCustomTypeUrl) {
  75. protobuf_unittest::TestAny submessage;
  76. submessage.set_int32_value(12345);
  77. google::protobuf::Any any;
  78. // Pack with a custom type URL prefix.
  79. any.PackFrom(submessage, "type.myservice.com");
  80. EXPECT_EQ("type.myservice.com/protobuf_unittest.TestAny", any.type_url());
  81. // Pack with a custom type URL prefix ending with '/'.
  82. any.PackFrom(submessage, "type.myservice.com/");
  83. EXPECT_EQ("type.myservice.com/protobuf_unittest.TestAny", any.type_url());
  84. // Pack with an empty type URL prefix.
  85. any.PackFrom(submessage, "");
  86. EXPECT_EQ("/protobuf_unittest.TestAny", any.type_url());
  87. // Test unpacking the type.
  88. submessage.Clear();
  89. EXPECT_TRUE(any.UnpackTo(&submessage));
  90. EXPECT_EQ(12345, submessage.int32_value());
  91. }
  92. TEST(AnyTest, TestIs) {
  93. protobuf_unittest::TestAny submessage;
  94. submessage.set_int32_value(12345);
  95. google::protobuf::Any any;
  96. any.PackFrom(submessage);
  97. ASSERT_TRUE(any.ParseFromString(any.SerializeAsString()));
  98. EXPECT_TRUE(any.Is<protobuf_unittest::TestAny>());
  99. EXPECT_FALSE(any.Is<google::protobuf::Any>());
  100. protobuf_unittest::TestAny message;
  101. message.mutable_any_value()->PackFrom(any);
  102. ASSERT_TRUE(message.ParseFromString(message.SerializeAsString()));
  103. EXPECT_FALSE(message.any_value().Is<protobuf_unittest::TestAny>());
  104. EXPECT_TRUE(message.any_value().Is<google::protobuf::Any>());
  105. any.set_type_url("/protobuf_unittest.TestAny");
  106. EXPECT_TRUE(any.Is<protobuf_unittest::TestAny>());
  107. // The type URL must contain at least one "/".
  108. any.set_type_url("protobuf_unittest.TestAny");
  109. EXPECT_FALSE(any.Is<protobuf_unittest::TestAny>());
  110. // The type name after the slash must be fully qualified.
  111. any.set_type_url("/TestAny");
  112. EXPECT_FALSE(any.Is<protobuf_unittest::TestAny>());
  113. }
  114. TEST(AnyTest, MoveConstructor) {
  115. protobuf_unittest::TestAny payload;
  116. payload.set_int32_value(12345);
  117. google::protobuf::Any src;
  118. src.PackFrom(payload);
  119. const char* type_url = src.type_url().data();
  120. google::protobuf::Any dst(std::move(src));
  121. EXPECT_EQ(type_url, dst.type_url().data());
  122. payload.Clear();
  123. ASSERT_TRUE(dst.UnpackTo(&payload));
  124. EXPECT_EQ(12345, payload.int32_value());
  125. }
  126. TEST(AnyTest, MoveAssignment) {
  127. protobuf_unittest::TestAny payload;
  128. payload.set_int32_value(12345);
  129. google::protobuf::Any src;
  130. src.PackFrom(payload);
  131. const char* type_url = src.type_url().data();
  132. google::protobuf::Any dst;
  133. dst = std::move(src);
  134. EXPECT_EQ(type_url, dst.type_url().data());
  135. payload.Clear();
  136. ASSERT_TRUE(dst.UnpackTo(&payload));
  137. EXPECT_EQ(12345, payload.int32_value());
  138. }
  139. } // namespace
  140. } // namespace protobuf
  141. } // namespace google