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

140 行
5.3 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/arena.h>
  34. #include <google/protobuf/testing/googletest.h>
  35. #include <gtest/gtest.h>
  36. using UNITTEST::TestAllTypes;
  37. namespace google {
  38. namespace protobuf {
  39. namespace {
  40. // We selectively set/check a few representative fields rather than all fields
  41. // as this test is only expected to cover the basics of lite support.
  42. void SetAllFields(TestAllTypes* m) {
  43. m->set_optional_int32(100);
  44. m->set_optional_string("asdf");
  45. m->set_optional_bytes("jkl;");
  46. m->mutable_optional_nested_message()->set_bb(42);
  47. m->mutable_optional_foreign_message()->set_c(43);
  48. m->set_optional_nested_enum(
  49. UNITTEST::TestAllTypes_NestedEnum_BAZ);
  50. m->set_optional_foreign_enum(
  51. UNITTEST::FOREIGN_BAZ);
  52. m->mutable_optional_lazy_message()->set_bb(45);
  53. m->add_repeated_int32(100);
  54. m->add_repeated_string("asdf");
  55. m->add_repeated_bytes("jkl;");
  56. m->add_repeated_nested_message()->set_bb(46);
  57. m->add_repeated_foreign_message()->set_c(47);
  58. m->add_repeated_nested_enum(
  59. UNITTEST::TestAllTypes_NestedEnum_BAZ);
  60. m->add_repeated_foreign_enum(
  61. UNITTEST::FOREIGN_BAZ);
  62. m->add_repeated_lazy_message()->set_bb(49);
  63. m->set_oneof_uint32(1);
  64. m->mutable_oneof_nested_message()->set_bb(50);
  65. m->set_oneof_string("test"); // only this one remains set
  66. }
  67. void ExpectAllFieldsSet(const TestAllTypes& m) {
  68. EXPECT_EQ(100, m.optional_int32());
  69. EXPECT_EQ("asdf", m.optional_string());
  70. EXPECT_EQ("jkl;", m.optional_bytes());
  71. EXPECT_EQ(true, m.has_optional_nested_message());
  72. EXPECT_EQ(42, m.optional_nested_message().bb());
  73. EXPECT_EQ(true, m.has_optional_foreign_message());
  74. EXPECT_EQ(43, m.optional_foreign_message().c());
  75. EXPECT_EQ(UNITTEST::TestAllTypes_NestedEnum_BAZ,
  76. m.optional_nested_enum());
  77. EXPECT_EQ(UNITTEST::FOREIGN_BAZ,
  78. m.optional_foreign_enum());
  79. EXPECT_EQ(true, m.has_optional_lazy_message());
  80. EXPECT_EQ(45, m.optional_lazy_message().bb());
  81. EXPECT_EQ(1, m.repeated_int32_size());
  82. EXPECT_EQ(100, m.repeated_int32(0));
  83. EXPECT_EQ(1, m.repeated_string_size());
  84. EXPECT_EQ("asdf", m.repeated_string(0));
  85. EXPECT_EQ(1, m.repeated_bytes_size());
  86. EXPECT_EQ("jkl;", m.repeated_bytes(0));
  87. EXPECT_EQ(1, m.repeated_nested_message_size());
  88. EXPECT_EQ(46, m.repeated_nested_message(0).bb());
  89. EXPECT_EQ(1, m.repeated_foreign_message_size());
  90. EXPECT_EQ(47, m.repeated_foreign_message(0).c());
  91. EXPECT_EQ(1, m.repeated_nested_enum_size());
  92. EXPECT_EQ(UNITTEST::TestAllTypes_NestedEnum_BAZ,
  93. m.repeated_nested_enum(0));
  94. EXPECT_EQ(1, m.repeated_foreign_enum_size());
  95. EXPECT_EQ(UNITTEST::FOREIGN_BAZ,
  96. m.repeated_foreign_enum(0));
  97. EXPECT_EQ(1, m.repeated_lazy_message_size());
  98. EXPECT_EQ(49, m.repeated_lazy_message(0).bb());
  99. EXPECT_EQ(UNITTEST::TestAllTypes::kOneofString,
  100. m.oneof_field_case());
  101. EXPECT_EQ("test", m.oneof_string());
  102. }
  103. // In this file we only test some basic functionalities of in proto3 and expect
  104. // the rest is fully tested in proto2 unittests because proto3 shares most code
  105. // with proto2.
  106. TEST(LITE_TEST_NAME, Parsing) {
  107. TestAllTypes original;
  108. SetAllFields(&original);
  109. TestAllTypes msg;
  110. msg.ParseFromString(original.SerializeAsString());
  111. ExpectAllFieldsSet(msg);
  112. }
  113. TEST(LITE_TEST_NAME, Swap) {
  114. // Test Swap().
  115. TestAllTypes msg1;
  116. TestAllTypes msg2;
  117. msg1.set_optional_string("123");
  118. msg2.set_optional_string("3456");
  119. msg1.Swap(&msg2);
  120. EXPECT_EQ("3456", msg1.optional_string());
  121. EXPECT_EQ("123", msg2.optional_string());
  122. EXPECT_EQ(msg1.ByteSize(), msg2.ByteSize() + 1);
  123. }
  124. } // namespace
  125. } // namespace protobuf
  126. } // namespace google