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 EXPECT_EQ(L"123hello w", StringPrintf(L"%3d%2ls %1lc", 123, L"hello", 'w')); | |
32 } | 31 } |
33 | 32 |
34 TEST(StringPrintfTest, StringAppendfEmptyString) { | 33 TEST(StringPrintfTest, StringAppendfEmptyString) { |
35 std::string value("Hello"); | 34 std::string value("Hello"); |
36 StringAppendF(&value, "%s", ""); | 35 StringAppendF(&value, "%s", ""); |
37 EXPECT_EQ("Hello", value); | 36 EXPECT_EQ("Hello", value); |
38 | |
39 std::wstring valuew(L"Hello"); | |
40 StringAppendF(&valuew, L"%ls", L""); | |
41 EXPECT_EQ(L"Hello", valuew); | |
42 } | 37 } |
43 | 38 |
44 TEST(StringPrintfTest, StringAppendfString) { | 39 TEST(StringPrintfTest, StringAppendfString) { |
45 std::string value("Hello"); | 40 std::string value("Hello"); |
46 StringAppendF(&value, " %s", "World"); | 41 StringAppendF(&value, " %s", "World"); |
47 EXPECT_EQ("Hello World", value); | 42 EXPECT_EQ("Hello World", value); |
48 | |
49 std::wstring valuew(L"Hello"); | |
50 StringAppendF(&valuew, L" %ls", L"World"); | |
51 EXPECT_EQ(L"Hello World", valuew); | |
52 } | 43 } |
53 | 44 |
54 TEST(StringPrintfTest, StringAppendfInt) { | 45 TEST(StringPrintfTest, StringAppendfInt) { |
55 std::string value("Hello"); | 46 std::string value("Hello"); |
56 StringAppendF(&value, " %d", 123); | 47 StringAppendF(&value, " %d", 123); |
57 EXPECT_EQ("Hello 123", value); | 48 EXPECT_EQ("Hello 123", value); |
58 | |
59 std::wstring valuew(L"Hello"); | |
60 StringAppendF(&valuew, L" %d", 123); | |
61 EXPECT_EQ(L"Hello 123", valuew); | |
62 } | 49 } |
63 | 50 |
64 // Make sure that lengths exactly around the initial buffer size are handled | 51 // Make sure that lengths exactly around the initial buffer size are handled |
65 // correctly. | 52 // correctly. |
66 TEST(StringPrintfTest, StringPrintfBounds) { | 53 TEST(StringPrintfTest, StringPrintfBounds) { |
67 const int kSrcLen = 1026; | 54 const int kSrcLen = 1026; |
68 char src[kSrcLen]; | 55 char src[kSrcLen]; |
69 for (size_t i = 0; i < arraysize(src); i++) | 56 for (size_t i = 0; i < arraysize(src); i++) |
70 src[i] = 'A'; | 57 src[i] = 'A'; |
71 | 58 |
72 wchar_t srcw[kSrcLen]; | |
73 for (size_t i = 0; i < arraysize(srcw); i++) | |
74 srcw[i] = 'A'; | |
75 | |
76 for (int i = 1; i < 3; i++) { | 59 for (int i = 1; i < 3; i++) { |
77 src[kSrcLen - i] = 0; | 60 src[kSrcLen - i] = 0; |
78 std::string out; | 61 std::string out; |
79 SStringPrintf(&out, "%s", src); | 62 SStringPrintf(&out, "%s", src); |
80 EXPECT_STREQ(src, out.c_str()); | 63 EXPECT_STREQ(src, out.c_str()); |
81 | |
82 srcw[kSrcLen - i] = 0; | |
83 std::wstring outw; | |
84 SStringPrintf(&outw, L"%ls", srcw); | |
85 EXPECT_STREQ(srcw, outw.c_str()); | |
86 } | 64 } |
87 } | 65 } |
88 | 66 |
89 // Test very large sprintfs that will cause the buffer to grow. | 67 // Test very large sprintfs that will cause the buffer to grow. |
90 TEST(StringPrintfTest, Grow) { | 68 TEST(StringPrintfTest, Grow) { |
91 char src[1026]; | 69 char src[1026]; |
92 for (size_t i = 0; i < arraysize(src); i++) | 70 for (size_t i = 0; i < arraysize(src); i++) |
93 src[i] = 'A'; | 71 src[i] = 'A'; |
94 src[1025] = 0; | 72 src[1025] = 0; |
95 | 73 |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
127 for (int i = 0; i < buf_len; ++i) | 105 for (int i = 0; i < buf_len; ++i) |
128 src[i] = 'a'; | 106 src[i] = 'a'; |
129 src[buf_len] = 0; | 107 src[buf_len] = 0; |
130 | 108 |
131 std::string out; | 109 std::string out; |
132 SStringPrintf(&out, "%s", src); | 110 SStringPrintf(&out, "%s", src); |
133 | 111 |
134 EXPECT_STREQ(src, out.c_str()); | 112 EXPECT_STREQ(src, out.c_str()); |
135 } | 113 } |
136 | 114 |
137 // TODO(evanm): what's the proper cross-platform test here? | |
138 #if defined(OS_WIN) | |
139 // sprintf in Visual Studio fails when given U+FFFF. This tests that the | |
140 // failure case is gracefuly handled. | |
141 TEST(StringPrintfTest, Invalid) { | |
142 wchar_t invalid[2]; | |
143 invalid[0] = 0xffff; | |
144 invalid[1] = 0; | |
145 | |
146 std::wstring out; | |
147 SStringPrintf(&out, L"%ls", invalid); | |
148 EXPECT_STREQ(L"", out.c_str()); | |
149 } | |
150 #endif | |
151 | |
152 // Test that the positional parameters work. | 115 // Test that the positional parameters work. |
153 TEST(StringPrintfTest, PositionalParameters) { | 116 TEST(StringPrintfTest, PositionalParameters) { |
154 std::string out; | 117 std::string out; |
155 SStringPrintf(&out, "%1$s %1$s", "test"); | 118 SStringPrintf(&out, "%1$s %1$s", "test"); |
156 EXPECT_STREQ("test test", out.c_str()); | 119 EXPECT_STREQ("test test", out.c_str()); |
157 | |
158 #if defined(OS_WIN) | |
159 std::wstring wout; | |
160 SStringPrintf(&wout, L"%1$ls %1$ls", L"test"); | |
161 EXPECT_STREQ(L"test test", wout.c_str()); | |
162 #endif | |
163 } | 120 } |
164 | 121 |
165 } // namespace base | 122 } // namespace base |
OLD | NEW |