诸暨麻将添加redis
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

153 regels
5.3 KiB

  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2012 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. // from google3/base/stringprintf_unittest.cc
  31. #include <google/protobuf/stubs/stringprintf.h>
  32. #include <cerrno>
  33. #include <string>
  34. #include <google/protobuf/testing/googletest.h>
  35. #include <gtest/gtest.h>
  36. namespace google {
  37. namespace protobuf {
  38. namespace {
  39. TEST(StringPrintfTest, Empty) {
  40. #if 0
  41. // gcc 2.95.3, gcc 4.1.0, and gcc 4.2.2 all warn about this:
  42. // warning: zero-length printf format string.
  43. // so we do not allow them in google3.
  44. EXPECT_EQ("", StringPrintf(""));
  45. #endif
  46. EXPECT_EQ("", StringPrintf("%s", string().c_str()));
  47. EXPECT_EQ("", StringPrintf("%s", ""));
  48. }
  49. TEST(StringPrintfTest, Misc) {
  50. // MSVC and mingw does not support $ format specifier.
  51. #if !defined(_MSC_VER) && !defined(__MINGW32__)
  52. EXPECT_EQ("123hello w", StringPrintf("%3$d%2$s %1$c", 'w', "hello", 123));
  53. #endif // !_MSC_VER
  54. }
  55. TEST(StringAppendFTest, Empty) {
  56. string value("Hello");
  57. const char* empty = "";
  58. StringAppendF(&value, "%s", empty);
  59. EXPECT_EQ("Hello", value);
  60. }
  61. TEST(StringAppendFTest, EmptyString) {
  62. string value("Hello");
  63. StringAppendF(&value, "%s", "");
  64. EXPECT_EQ("Hello", value);
  65. }
  66. TEST(StringAppendFTest, String) {
  67. string value("Hello");
  68. StringAppendF(&value, " %s", "World");
  69. EXPECT_EQ("Hello World", value);
  70. }
  71. TEST(StringAppendFTest, Int) {
  72. string value("Hello");
  73. StringAppendF(&value, " %d", 123);
  74. EXPECT_EQ("Hello 123", value);
  75. }
  76. TEST(StringPrintfTest, Multibyte) {
  77. // If we are in multibyte mode and feed invalid multibyte sequence,
  78. // StringPrintf should return an empty string instead of running
  79. // out of memory while trying to determine destination buffer size.
  80. // see b/4194543.
  81. char* old_locale = setlocale(LC_CTYPE, nullptr);
  82. // Push locale with multibyte mode
  83. setlocale(LC_CTYPE, "en_US.utf8");
  84. const char kInvalidCodePoint[] = "\375\067s";
  85. string value = StringPrintf("%.*s", 3, kInvalidCodePoint);
  86. // In some versions of glibc (e.g. eglibc-2.11.1, aka GRTEv2), snprintf
  87. // returns error given an invalid codepoint. Other versions
  88. // (e.g. eglibc-2.15, aka pre-GRTEv3) emit the codepoint verbatim.
  89. // We test that the output is one of the above.
  90. EXPECT_TRUE(value.empty() || value == kInvalidCodePoint);
  91. // Repeat with longer string, to make sure that the dynamically
  92. // allocated path in StringAppendV is handled correctly.
  93. int n = 2048;
  94. char* buf = new char[n+1];
  95. memset(buf, ' ', n-3);
  96. memcpy(buf + n - 3, kInvalidCodePoint, 4);
  97. value = StringPrintf("%.*s", n, buf);
  98. // See GRTEv2 vs. GRTEv3 comment above.
  99. EXPECT_TRUE(value.empty() || value == buf);
  100. delete[] buf;
  101. setlocale(LC_CTYPE, old_locale);
  102. }
  103. TEST(StringPrintfTest, NoMultibyte) {
  104. // No multibyte handling, but the string contains funny chars.
  105. char* old_locale = setlocale(LC_CTYPE, nullptr);
  106. setlocale(LC_CTYPE, "POSIX");
  107. string value = StringPrintf("%.*s", 3, "\375\067s");
  108. setlocale(LC_CTYPE, old_locale);
  109. EXPECT_EQ("\375\067s", value);
  110. }
  111. TEST(StringPrintfTest, DontOverwriteErrno) {
  112. // Check that errno isn't overwritten unless we're printing
  113. // something significantly larger than what people are normally
  114. // printing in their badly written PLOG() statements.
  115. errno = ECHILD;
  116. string value = StringPrintf("Hello, %s!", "World");
  117. EXPECT_EQ(ECHILD, errno);
  118. }
  119. TEST(StringPrintfTest, LargeBuf) {
  120. // Check that the large buffer is handled correctly.
  121. int n = 2048;
  122. char* buf = new char[n+1];
  123. memset(buf, ' ', n);
  124. buf[n] = 0;
  125. string value = StringPrintf("%s", buf);
  126. EXPECT_EQ(buf, value);
  127. delete[] buf;
  128. }
  129. } // anonymous namespace
  130. } // namespace protobuf
  131. } // namespace google