| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/basictypes.h" | 5 #include "base/basictypes.h" |
| 6 #include "base/stringprintf.h" | 6 #include "base/stringprintf.h" |
| 7 #include "testing/gtest/include/gtest/gtest.h" | 7 #include "testing/gtest/include/gtest/gtest.h" |
| 8 | 8 |
| 9 namespace base { | 9 namespace base { |
| 10 | 10 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 } | 21 } |
| 22 | 22 |
| 23 } // namespace | 23 } // namespace |
| 24 | 24 |
| 25 TEST(StringPrintfTest, StringPrintfEmpty) { | 25 TEST(StringPrintfTest, StringPrintfEmpty) { |
| 26 EXPECT_EQ("", StringPrintf("%s", "")); | 26 EXPECT_EQ("", StringPrintf("%s", "")); |
| 27 } | 27 } |
| 28 | 28 |
| 29 TEST(StringPrintfTest, StringPrintfMisc) { | 29 TEST(StringPrintfTest, StringPrintfMisc) { |
| 30 EXPECT_EQ("123hello w", StringPrintf("%3d%2s %1c", 123, "hello", 'w')); | 30 EXPECT_EQ("123hello w", StringPrintf("%3d%2s %1c", 123, "hello", 'w')); |
| 31 #if !defined(OS_ANDROID) |
| 31 EXPECT_EQ(L"123hello w", StringPrintf(L"%3d%2ls %1lc", 123, L"hello", 'w')); | 32 EXPECT_EQ(L"123hello w", StringPrintf(L"%3d%2ls %1lc", 123, L"hello", 'w')); |
| 33 #endif |
| 32 } | 34 } |
| 33 | 35 |
| 34 TEST(StringPrintfTest, StringAppendfEmptyString) { | 36 TEST(StringPrintfTest, StringAppendfEmptyString) { |
| 35 std::string value("Hello"); | 37 std::string value("Hello"); |
| 36 StringAppendF(&value, "%s", ""); | 38 StringAppendF(&value, "%s", ""); |
| 37 EXPECT_EQ("Hello", value); | 39 EXPECT_EQ("Hello", value); |
| 38 | 40 |
| 41 #if !defined(OS_ANDROID) |
| 39 std::wstring valuew(L"Hello"); | 42 std::wstring valuew(L"Hello"); |
| 40 StringAppendF(&valuew, L"%ls", L""); | 43 StringAppendF(&valuew, L"%ls", L""); |
| 41 EXPECT_EQ(L"Hello", valuew); | 44 EXPECT_EQ(L"Hello", valuew); |
| 45 #endif |
| 42 } | 46 } |
| 43 | 47 |
| 44 TEST(StringPrintfTest, StringAppendfString) { | 48 TEST(StringPrintfTest, StringAppendfString) { |
| 45 std::string value("Hello"); | 49 std::string value("Hello"); |
| 46 StringAppendF(&value, " %s", "World"); | 50 StringAppendF(&value, " %s", "World"); |
| 47 EXPECT_EQ("Hello World", value); | 51 EXPECT_EQ("Hello World", value); |
| 48 | 52 |
| 53 #if !defined(OS_ANDROID) |
| 49 std::wstring valuew(L"Hello"); | 54 std::wstring valuew(L"Hello"); |
| 50 StringAppendF(&valuew, L" %ls", L"World"); | 55 StringAppendF(&valuew, L" %ls", L"World"); |
| 51 EXPECT_EQ(L"Hello World", valuew); | 56 EXPECT_EQ(L"Hello World", valuew); |
| 57 #endif |
| 52 } | 58 } |
| 53 | 59 |
| 54 TEST(StringPrintfTest, StringAppendfInt) { | 60 TEST(StringPrintfTest, StringAppendfInt) { |
| 55 std::string value("Hello"); | 61 std::string value("Hello"); |
| 56 StringAppendF(&value, " %d", 123); | 62 StringAppendF(&value, " %d", 123); |
| 57 EXPECT_EQ("Hello 123", value); | 63 EXPECT_EQ("Hello 123", value); |
| 58 | 64 |
| 65 #if !defined(OS_ANDROID) |
| 59 std::wstring valuew(L"Hello"); | 66 std::wstring valuew(L"Hello"); |
| 60 StringAppendF(&valuew, L" %d", 123); | 67 StringAppendF(&valuew, L" %d", 123); |
| 61 EXPECT_EQ(L"Hello 123", valuew); | 68 EXPECT_EQ(L"Hello 123", valuew); |
| 69 #endif |
| 62 } | 70 } |
| 63 | 71 |
| 64 // Make sure that lengths exactly around the initial buffer size are handled | 72 // Make sure that lengths exactly around the initial buffer size are handled |
| 65 // correctly. | 73 // correctly. |
| 66 TEST(StringPrintfTest, StringPrintfBounds) { | 74 TEST(StringPrintfTest, StringPrintfBounds) { |
| 67 const int kSrcLen = 1026; | 75 const int kSrcLen = 1026; |
| 68 char src[kSrcLen]; | 76 char src[kSrcLen]; |
| 69 for (size_t i = 0; i < arraysize(src); i++) | 77 for (size_t i = 0; i < arraysize(src); i++) |
| 70 src[i] = 'A'; | 78 src[i] = 'A'; |
| 71 | 79 |
| 72 wchar_t srcw[kSrcLen]; | 80 wchar_t srcw[kSrcLen]; |
| 73 for (size_t i = 0; i < arraysize(srcw); i++) | 81 for (size_t i = 0; i < arraysize(srcw); i++) |
| 74 srcw[i] = 'A'; | 82 srcw[i] = 'A'; |
| 75 | 83 |
| 76 for (int i = 1; i < 3; i++) { | 84 for (int i = 1; i < 3; i++) { |
| 77 src[kSrcLen - i] = 0; | 85 src[kSrcLen - i] = 0; |
| 78 std::string out; | 86 std::string out; |
| 79 SStringPrintf(&out, "%s", src); | 87 SStringPrintf(&out, "%s", src); |
| 80 EXPECT_STREQ(src, out.c_str()); | 88 EXPECT_STREQ(src, out.c_str()); |
| 81 | 89 |
| 90 #if !defined(OS_ANDROID) |
| 82 srcw[kSrcLen - i] = 0; | 91 srcw[kSrcLen - i] = 0; |
| 83 std::wstring outw; | 92 std::wstring outw; |
| 84 SStringPrintf(&outw, L"%ls", srcw); | 93 SStringPrintf(&outw, L"%ls", srcw); |
| 85 EXPECT_STREQ(srcw, outw.c_str()); | 94 EXPECT_STREQ(srcw, outw.c_str()); |
| 95 #endif |
| 86 } | 96 } |
| 87 } | 97 } |
| 88 | 98 |
| 89 // Test very large sprintfs that will cause the buffer to grow. | 99 // Test very large sprintfs that will cause the buffer to grow. |
| 90 TEST(StringPrintfTest, Grow) { | 100 TEST(StringPrintfTest, Grow) { |
| 91 char src[1026]; | 101 char src[1026]; |
| 92 for (size_t i = 0; i < arraysize(src); i++) | 102 for (size_t i = 0; i < arraysize(src); i++) |
| 93 src[i] = 'A'; | 103 src[i] = 'A'; |
| 94 src[1025] = 0; | 104 src[1025] = 0; |
| 95 | 105 |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 156 EXPECT_STREQ("test test", out.c_str()); | 166 EXPECT_STREQ("test test", out.c_str()); |
| 157 | 167 |
| 158 #if defined(OS_WIN) | 168 #if defined(OS_WIN) |
| 159 std::wstring wout; | 169 std::wstring wout; |
| 160 SStringPrintf(&wout, L"%1$ls %1$ls", L"test"); | 170 SStringPrintf(&wout, L"%1$ls %1$ls", L"test"); |
| 161 EXPECT_STREQ(L"test test", wout.c_str()); | 171 EXPECT_STREQ(L"test test", wout.c_str()); |
| 162 #endif | 172 #endif |
| 163 } | 173 } |
| 164 | 174 |
| 165 } // namespace base | 175 } // namespace base |
| OLD | NEW |