stringprintf_unittest.cc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. // Copyright 2013 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "base/strings/stringprintf.h"
  5. #include <errno.h>
  6. #include <stddef.h>
  7. #include "build/build_config.h"
  8. #include "testing/gtest/include/gtest/gtest.h"
  9. namespace base {
  10. namespace {
  11. // A helper for the StringAppendV test that follows.
  12. //
  13. // Just forwards its args to StringAppendV.
  14. template <class CharT>
  15. static void StringAppendVTestHelper(std::basic_string<CharT>* out,
  16. const CharT* format,
  17. ...) {
  18. va_list ap;
  19. va_start(ap, format);
  20. StringAppendV(out, format, ap);
  21. va_end(ap);
  22. }
  23. } // namespace
  24. TEST(StringPrintfTest, StringPrintfEmpty) {
  25. EXPECT_EQ("", StringPrintf("%s", ""));
  26. }
  27. TEST(StringPrintfTest, StringPrintfMisc) {
  28. EXPECT_EQ("123hello w", StringPrintf("%3d%2s %1c", 123, "hello", 'w'));
  29. #if BUILDFLAG(IS_WIN)
  30. EXPECT_EQ(L"123hello w", StringPrintf(L"%3d%2ls %1lc", 123, L"hello", 'w'));
  31. EXPECT_EQ(u"123hello w", StringPrintf(u"%3d%2ls %1lc", 123, u"hello", 'w'));
  32. #endif
  33. }
  34. TEST(StringPrintfTest, StringAppendfEmptyString) {
  35. std::string value("Hello");
  36. StringAppendF(&value, "%s", "");
  37. EXPECT_EQ("Hello", value);
  38. #if BUILDFLAG(IS_WIN)
  39. std::wstring valuew(L"Hello");
  40. StringAppendF(&valuew, L"%ls", L"");
  41. EXPECT_EQ(L"Hello", valuew);
  42. std::u16string value16(u"Hello");
  43. StringAppendF(&value16, u"%ls", u"");
  44. EXPECT_EQ(u"Hello", value16);
  45. #endif
  46. }
  47. TEST(StringPrintfTest, StringAppendfString) {
  48. std::string value("Hello");
  49. StringAppendF(&value, " %s", "World");
  50. EXPECT_EQ("Hello World", value);
  51. #if BUILDFLAG(IS_WIN)
  52. std::wstring valuew(L"Hello");
  53. StringAppendF(&valuew, L" %ls", L"World");
  54. EXPECT_EQ(L"Hello World", valuew);
  55. std::u16string value16(u"Hello");
  56. StringAppendF(&value16, u" %ls", u"World");
  57. EXPECT_EQ(u"Hello World", value16);
  58. #endif
  59. }
  60. TEST(StringPrintfTest, StringAppendfInt) {
  61. std::string value("Hello");
  62. StringAppendF(&value, " %d", 123);
  63. EXPECT_EQ("Hello 123", value);
  64. #if BUILDFLAG(IS_WIN)
  65. std::wstring valuew(L"Hello");
  66. StringAppendF(&valuew, L" %d", 123);
  67. EXPECT_EQ(L"Hello 123", valuew);
  68. std::u16string value16(u"Hello");
  69. StringAppendF(&value16, u" %d", 123);
  70. EXPECT_EQ(u"Hello 123", value16);
  71. #endif
  72. }
  73. // Make sure that lengths exactly around the initial buffer size are handled
  74. // correctly.
  75. TEST(StringPrintfTest, StringPrintfBounds) {
  76. const int kSrcLen = 1026;
  77. char src[kSrcLen];
  78. std::fill_n(src, kSrcLen, 'A');
  79. wchar_t srcw[kSrcLen];
  80. std::fill_n(srcw, kSrcLen, 'A');
  81. char16_t src16[kSrcLen];
  82. std::fill_n(src16, kSrcLen, 'A');
  83. for (int i = 1; i < 3; i++) {
  84. src[kSrcLen - i] = 0;
  85. std::string out;
  86. SStringPrintf(&out, "%s", src);
  87. EXPECT_STREQ(src, out.c_str());
  88. #if BUILDFLAG(IS_WIN)
  89. srcw[kSrcLen - i] = 0;
  90. std::wstring outw;
  91. SStringPrintf(&outw, L"%ls", srcw);
  92. EXPECT_STREQ(srcw, outw.c_str());
  93. src16[kSrcLen - i] = 0;
  94. std::u16string out16;
  95. SStringPrintf(&out16, u"%ls", src16);
  96. // EXPECT_STREQ does not support const char16_t* strings yet.
  97. // Dispatch to the const wchar_t* overload instead.
  98. EXPECT_STREQ(reinterpret_cast<const wchar_t*>(src16),
  99. reinterpret_cast<const wchar_t*>(out16.c_str()));
  100. #endif
  101. }
  102. }
  103. // Test very large sprintfs that will cause the buffer to grow.
  104. TEST(StringPrintfTest, Grow) {
  105. char src[1026];
  106. for (auto& i : src)
  107. i = 'A';
  108. src[1025] = 0;
  109. const char fmt[] = "%sB%sB%sB%sB%sB%sB%s";
  110. std::string out;
  111. SStringPrintf(&out, fmt, src, src, src, src, src, src, src);
  112. const int kRefSize = 320000;
  113. char* ref = new char[kRefSize];
  114. #if BUILDFLAG(IS_WIN)
  115. sprintf_s(ref, kRefSize, fmt, src, src, src, src, src, src, src);
  116. #elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
  117. snprintf(ref, kRefSize, fmt, src, src, src, src, src, src, src);
  118. #endif
  119. EXPECT_STREQ(ref, out.c_str());
  120. delete[] ref;
  121. }
  122. TEST(StringPrintfTest, StringAppendV) {
  123. std::string out;
  124. StringAppendVTestHelper(&out, "%d foo %s", 1, "bar");
  125. EXPECT_EQ("1 foo bar", out);
  126. #if BUILDFLAG(IS_WIN)
  127. std::wstring outw;
  128. StringAppendVTestHelper(&outw, L"%d foo %ls", 1, L"bar");
  129. EXPECT_EQ(L"1 foo bar", outw);
  130. std::u16string out16;
  131. StringAppendVTestHelper(&out16, u"%d foo %ls", 1, u"bar");
  132. EXPECT_EQ(u"1 foo bar", out16);
  133. #endif
  134. }
  135. // Test the boundary condition for the size of the string_util's
  136. // internal buffer.
  137. TEST(StringPrintfTest, GrowBoundary) {
  138. const int kStringUtilBufLen = 1024;
  139. // Our buffer should be one larger than the size of StringAppendVT's stack
  140. // buffer.
  141. // And need extra one for NULL-terminator.
  142. const int kBufLen = kStringUtilBufLen + 1 + 1;
  143. char src[kBufLen];
  144. for (int i = 0; i < kBufLen - 1; ++i)
  145. src[i] = 'a';
  146. src[kBufLen - 1] = 0;
  147. std::string out;
  148. SStringPrintf(&out, "%s", src);
  149. EXPECT_STREQ(src, out.c_str());
  150. }
  151. #if BUILDFLAG(IS_WIN)
  152. TEST(StringPrintfTest, Invalid) {
  153. wchar_t invalid[2];
  154. invalid[0] = 0xffff;
  155. invalid[1] = 0;
  156. std::wstring out;
  157. SStringPrintf(&out, L"%ls", invalid);
  158. EXPECT_STREQ(invalid, out.c_str());
  159. }
  160. #endif
  161. // Test that StringPrintf and StringAppendV do not change errno.
  162. TEST(StringPrintfTest, StringPrintfErrno) {
  163. errno = 1;
  164. EXPECT_EQ("", StringPrintf("%s", ""));
  165. EXPECT_EQ(1, errno);
  166. std::string out;
  167. StringAppendVTestHelper(&out, "%d foo %s", 1, "bar");
  168. EXPECT_EQ(1, errno);
  169. }
  170. } // namespace base