| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/strings/stringprintf.h" | 5 #include "base/strings/stringprintf.h" |
| 6 | 6 |
| 7 #include <errno.h> | 7 #include <errno.h> |
| 8 | 8 |
| 9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 99 } | 99 } |
| 100 } | 100 } |
| 101 | 101 |
| 102 // Test very large sprintfs that will cause the buffer to grow. | 102 // Test very large sprintfs that will cause the buffer to grow. |
| 103 TEST(StringPrintfTest, Grow) { | 103 TEST(StringPrintfTest, Grow) { |
| 104 char src[1026]; | 104 char src[1026]; |
| 105 for (size_t i = 0; i < arraysize(src); i++) | 105 for (size_t i = 0; i < arraysize(src); i++) |
| 106 src[i] = 'A'; | 106 src[i] = 'A'; |
| 107 src[1025] = 0; | 107 src[1025] = 0; |
| 108 | 108 |
| 109 const char* fmt = "%sB%sB%sB%sB%sB%sB%s"; | 109 const char fmt[] = "%sB%sB%sB%sB%sB%sB%s"; |
| 110 | 110 |
| 111 std::string out; | 111 std::string out; |
| 112 SStringPrintf(&out, fmt, src, src, src, src, src, src, src); | 112 SStringPrintf(&out, fmt, src, src, src, src, src, src, src); |
| 113 | 113 |
| 114 const int kRefSize = 320000; | 114 const int kRefSize = 320000; |
| 115 char* ref = new char[kRefSize]; | 115 char* ref = new char[kRefSize]; |
| 116 #if defined(OS_WIN) | 116 #if defined(OS_WIN) |
| 117 sprintf_s(ref, kRefSize, fmt, src, src, src, src, src, src, src); | 117 sprintf_s(ref, kRefSize, fmt, src, src, src, src, src, src, src); |
| 118 #elif defined(OS_POSIX) | 118 #elif defined(OS_POSIX) |
| 119 snprintf(ref, kRefSize, fmt, src, src, src, src, src, src, src); | 119 snprintf(ref, kRefSize, fmt, src, src, src, src, src, src, src); |
| 120 #endif | 120 #endif |
| 121 | 121 |
| 122 EXPECT_STREQ(ref, out.c_str()); | 122 EXPECT_STREQ(ref, out.c_str()); |
| 123 delete[] ref; | 123 delete[] ref; |
| 124 } | 124 } |
| 125 | 125 |
| 126 TEST(StringPrintfTest, StringAppendV) { | 126 TEST(StringPrintfTest, StringAppendV) { |
| 127 std::string out; | 127 std::string out; |
| 128 StringAppendVTestHelper(&out, "%d foo %s", 1, "bar"); | 128 StringAppendVTestHelper(&out, "%d foo %s", 1, "bar"); |
| 129 EXPECT_EQ("1 foo bar", out); | 129 EXPECT_EQ("1 foo bar", out); |
| 130 } | 130 } |
| 131 | 131 |
| 132 // Test the boundary condition for the size of the string_util's | 132 // Test the boundary condition for the size of the string_util's |
| 133 // internal buffer. | 133 // internal buffer. |
| 134 TEST(StringPrintfTest, GrowBoundary) { | 134 TEST(StringPrintfTest, GrowBoundary) { |
| 135 const int string_util_buf_len = 1024; | 135 const int kStringUtilBufLen = 1024; |
| 136 // Our buffer should be one larger than the size of StringAppendVT's stack | 136 // Our buffer should be one larger than the size of StringAppendVT's stack |
| 137 // buffer. | 137 // buffer. |
| 138 const int buf_len = string_util_buf_len + 1; | 138 // And need extra one for NULL-terminator. |
| 139 char src[buf_len + 1]; // Need extra one for NULL-terminator. | 139 const int kBufLen = kStringUtilBufLen + 1 + 1; |
| 140 for (int i = 0; i < buf_len; ++i) | 140 char src[kBufLen]; |
| 141 for (int i = 0; i < kBufLen - 1; ++i) |
| 141 src[i] = 'a'; | 142 src[i] = 'a'; |
| 142 src[buf_len] = 0; | 143 src[kBufLen - 1] = 0; |
| 143 | 144 |
| 144 std::string out; | 145 std::string out; |
| 145 SStringPrintf(&out, "%s", src); | 146 SStringPrintf(&out, "%s", src); |
| 146 | 147 |
| 147 EXPECT_STREQ(src, out.c_str()); | 148 EXPECT_STREQ(src, out.c_str()); |
| 148 } | 149 } |
| 149 | 150 |
| 150 // TODO(evanm): what's the proper cross-platform test here? | 151 // TODO(evanm): what's the proper cross-platform test here? |
| 151 #if defined(OS_WIN) | 152 #if defined(OS_WIN) |
| 152 // sprintf in Visual Studio fails when given U+FFFF. This tests that the | 153 // sprintf in Visual Studio fails when given U+FFFF. This tests that the |
| (...skipping 26 matching lines...) Expand all Loading... |
| 179 TEST(StringPrintfTest, StringPrintfErrno) { | 180 TEST(StringPrintfTest, StringPrintfErrno) { |
| 180 errno = 1; | 181 errno = 1; |
| 181 EXPECT_EQ("", StringPrintf("%s", "")); | 182 EXPECT_EQ("", StringPrintf("%s", "")); |
| 182 EXPECT_EQ(1, errno); | 183 EXPECT_EQ(1, errno); |
| 183 std::string out; | 184 std::string out; |
| 184 StringAppendVTestHelper(&out, "%d foo %s", 1, "bar"); | 185 StringAppendVTestHelper(&out, "%d foo %s", 1, "bar"); |
| 185 EXPECT_EQ(1, errno); | 186 EXPECT_EQ(1, errno); |
| 186 } | 187 } |
| 187 | 188 |
| 188 } // namespace base | 189 } // namespace base |
| OLD | NEW |