诸暨麻将添加redis
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

147 linhas
4.6 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/stubs/bytestream.h>
  31. #include <stdio.h>
  32. #include <string.h>
  33. #include <algorithm>
  34. #include <google/protobuf/testing/googletest.h>
  35. #include <gtest/gtest.h>
  36. namespace google {
  37. namespace protobuf {
  38. namespace strings {
  39. namespace {
  40. // We use this class instead of ArrayByteSource to simulate a ByteSource that
  41. // contains multiple fragments. ArrayByteSource returns the entire array in
  42. // one fragment.
  43. class MockByteSource : public ByteSource {
  44. public:
  45. MockByteSource(StringPiece data, int block_size)
  46. : data_(data), block_size_(block_size) {}
  47. size_t Available() const { return data_.size(); }
  48. StringPiece Peek() {
  49. return data_.substr(0, block_size_);
  50. }
  51. void Skip(size_t n) { data_.remove_prefix(n); }
  52. private:
  53. StringPiece data_;
  54. int block_size_;
  55. };
  56. TEST(ByteSourceTest, CopyTo) {
  57. StringPiece data("Hello world!");
  58. MockByteSource source(data, 3);
  59. string str;
  60. StringByteSink sink(&str);
  61. source.CopyTo(&sink, data.size());
  62. EXPECT_EQ(data, str);
  63. }
  64. TEST(ByteSourceTest, CopySubstringTo) {
  65. StringPiece data("Hello world!");
  66. MockByteSource source(data, 3);
  67. source.Skip(1);
  68. string str;
  69. StringByteSink sink(&str);
  70. source.CopyTo(&sink, data.size() - 2);
  71. EXPECT_EQ(data.substr(1, data.size() - 2), str);
  72. EXPECT_EQ("!", source.Peek());
  73. }
  74. TEST(ByteSourceTest, LimitByteSource) {
  75. StringPiece data("Hello world!");
  76. MockByteSource source(data, 3);
  77. LimitByteSource limit_source(&source, 6);
  78. EXPECT_EQ(6, limit_source.Available());
  79. limit_source.Skip(1);
  80. EXPECT_EQ(5, limit_source.Available());
  81. {
  82. string str;
  83. StringByteSink sink(&str);
  84. limit_source.CopyTo(&sink, limit_source.Available());
  85. EXPECT_EQ("ello ", str);
  86. EXPECT_EQ(0, limit_source.Available());
  87. EXPECT_EQ(6, source.Available());
  88. }
  89. {
  90. string str;
  91. StringByteSink sink(&str);
  92. source.CopyTo(&sink, source.Available());
  93. EXPECT_EQ("world!", str);
  94. EXPECT_EQ(0, source.Available());
  95. }
  96. }
  97. TEST(ByteSourceTest, CopyToStringByteSink) {
  98. StringPiece data("Hello world!");
  99. MockByteSource source(data, 3);
  100. string str;
  101. StringByteSink sink(&str);
  102. source.CopyTo(&sink, data.size());
  103. EXPECT_EQ(data, str);
  104. }
  105. // Verify that ByteSink is subclassable and Flush() overridable.
  106. class FlushingByteSink : public StringByteSink {
  107. public:
  108. explicit FlushingByteSink(string* dest) : StringByteSink(dest) {}
  109. virtual void Flush() { Append("z", 1); }
  110. private:
  111. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FlushingByteSink);
  112. };
  113. // Write and Flush via the ByteSink superclass interface.
  114. void WriteAndFlush(ByteSink* s) {
  115. s->Append("abc", 3);
  116. s->Flush();
  117. }
  118. TEST(ByteSinkTest, Flush) {
  119. string str;
  120. FlushingByteSink f_sink(&str);
  121. WriteAndFlush(&f_sink);
  122. EXPECT_STREQ("abcz", str.c_str());
  123. }
  124. } // namespace
  125. } // namespace strings
  126. } // namespace protobuf
  127. } // namespace google