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 13 matching lines...) Expand all Loading... |
24 } | 24 } |
25 | 25 |
26 } // namespace | 26 } // namespace |
27 | 27 |
28 TEST(StringPrintfTest, StringPrintfEmpty) { | 28 TEST(StringPrintfTest, StringPrintfEmpty) { |
29 EXPECT_EQ("", StringPrintf("%s", "")); | 29 EXPECT_EQ("", StringPrintf("%s", "")); |
30 } | 30 } |
31 | 31 |
32 TEST(StringPrintfTest, StringPrintfMisc) { | 32 TEST(StringPrintfTest, StringPrintfMisc) { |
33 EXPECT_EQ("123hello w", StringPrintf("%3d%2s %1c", 123, "hello", 'w')); | 33 EXPECT_EQ("123hello w", StringPrintf("%3d%2s %1c", 123, "hello", 'w')); |
34 #if defined(OS_WIN) | |
35 EXPECT_EQ(L"123hello w", StringPrintf(L"%3d%2ls %1lc", 123, L"hello", 'w')); | |
36 #endif | |
37 } | 34 } |
38 | 35 |
39 TEST(StringPrintfTest, StringAppendfEmptyString) { | 36 TEST(StringPrintfTest, StringAppendfEmptyString) { |
40 std::string value("Hello"); | 37 std::string value("Hello"); |
41 StringAppendF(&value, "%s", ""); | 38 StringAppendF(&value, "%s", ""); |
42 EXPECT_EQ("Hello", value); | 39 EXPECT_EQ("Hello", value); |
43 | |
44 #if defined(OS_WIN) | |
45 std::wstring valuew(L"Hello"); | |
46 StringAppendF(&valuew, L"%ls", L""); | |
47 EXPECT_EQ(L"Hello", valuew); | |
48 #endif | |
49 } | 40 } |
50 | 41 |
51 TEST(StringPrintfTest, StringAppendfString) { | 42 TEST(StringPrintfTest, StringAppendfString) { |
52 std::string value("Hello"); | 43 std::string value("Hello"); |
53 StringAppendF(&value, " %s", "World"); | 44 StringAppendF(&value, " %s", "World"); |
54 EXPECT_EQ("Hello World", value); | 45 EXPECT_EQ("Hello World", value); |
55 | |
56 #if defined(OS_WIN) | |
57 std::wstring valuew(L"Hello"); | |
58 StringAppendF(&valuew, L" %ls", L"World"); | |
59 EXPECT_EQ(L"Hello World", valuew); | |
60 #endif | |
61 } | 46 } |
62 | 47 |
63 TEST(StringPrintfTest, StringAppendfInt) { | 48 TEST(StringPrintfTest, StringAppendfInt) { |
64 std::string value("Hello"); | 49 std::string value("Hello"); |
65 StringAppendF(&value, " %d", 123); | 50 StringAppendF(&value, " %d", 123); |
66 EXPECT_EQ("Hello 123", value); | 51 EXPECT_EQ("Hello 123", value); |
67 | |
68 #if defined(OS_WIN) | |
69 std::wstring valuew(L"Hello"); | |
70 StringAppendF(&valuew, L" %d", 123); | |
71 EXPECT_EQ(L"Hello 123", valuew); | |
72 #endif | |
73 } | 52 } |
74 | 53 |
75 // Make sure that lengths exactly around the initial buffer size are handled | 54 // Make sure that lengths exactly around the initial buffer size are handled |
76 // correctly. | 55 // correctly. |
77 TEST(StringPrintfTest, StringPrintfBounds) { | 56 TEST(StringPrintfTest, StringPrintfBounds) { |
78 const int kSrcLen = 1026; | 57 const int kSrcLen = 1026; |
79 char src[kSrcLen]; | 58 char src[kSrcLen]; |
80 for (size_t i = 0; i < arraysize(src); i++) | 59 for (size_t i = 0; i < arraysize(src); i++) |
81 src[i] = 'A'; | 60 src[i] = 'A'; |
82 | 61 |
83 wchar_t srcw[kSrcLen]; | 62 wchar_t srcw[kSrcLen]; |
84 for (size_t i = 0; i < arraysize(srcw); i++) | 63 for (size_t i = 0; i < arraysize(srcw); i++) |
85 srcw[i] = 'A'; | 64 srcw[i] = 'A'; |
86 | 65 |
87 for (int i = 1; i < 3; i++) { | 66 for (int i = 1; i < 3; i++) { |
88 src[kSrcLen - i] = 0; | 67 src[kSrcLen - i] = 0; |
89 std::string out; | 68 std::string out; |
90 SStringPrintf(&out, "%s", src); | 69 SStringPrintf(&out, "%s", src); |
91 EXPECT_STREQ(src, out.c_str()); | 70 EXPECT_STREQ(src, out.c_str()); |
92 | |
93 #if defined(OS_WIN) | |
94 srcw[kSrcLen - i] = 0; | |
95 std::wstring outw; | |
96 SStringPrintf(&outw, L"%ls", srcw); | |
97 EXPECT_STREQ(srcw, outw.c_str()); | |
98 #endif | |
99 } | 71 } |
100 } | 72 } |
101 | 73 |
102 // Test very large sprintfs that will cause the buffer to grow. | 74 // Test very large sprintfs that will cause the buffer to grow. |
103 TEST(StringPrintfTest, Grow) { | 75 TEST(StringPrintfTest, Grow) { |
104 char src[1026]; | 76 char src[1026]; |
105 for (size_t i = 0; i < arraysize(src); i++) | 77 for (size_t i = 0; i < arraysize(src); i++) |
106 src[i] = 'A'; | 78 src[i] = 'A'; |
107 src[1025] = 0; | 79 src[1025] = 0; |
108 | 80 |
109 const char fmt[] = "%sB%sB%sB%sB%sB%sB%s"; | 81 const char fmt[] = "%sB%sB%sB%sB%sB%sB%s"; |
110 | 82 |
111 std::string out; | 83 std::string out; |
112 SStringPrintf(&out, fmt, src, src, src, src, src, src, src); | 84 SStringPrintf(&out, fmt, src, src, src, src, src, src, src); |
113 | 85 |
114 const int kRefSize = 320000; | 86 const int kRefSize = 320000; |
115 char* ref = new char[kRefSize]; | 87 char* ref = new char[kRefSize]; |
116 #if defined(OS_WIN) | 88 #if defined(OS_POSIX) |
117 sprintf_s(ref, kRefSize, fmt, src, src, src, src, src, src, src); | |
118 #elif defined(OS_POSIX) | |
119 snprintf(ref, kRefSize, fmt, src, src, src, src, src, src, src); | 89 snprintf(ref, kRefSize, fmt, src, src, src, src, src, src, src); |
120 #endif | 90 #endif |
121 | 91 |
122 EXPECT_STREQ(ref, out.c_str()); | 92 EXPECT_STREQ(ref, out.c_str()); |
123 delete[] ref; | 93 delete[] ref; |
124 } | 94 } |
125 | 95 |
126 TEST(StringPrintfTest, StringAppendV) { | 96 TEST(StringPrintfTest, StringAppendV) { |
127 std::string out; | 97 std::string out; |
128 StringAppendVTestHelper(&out, "%d foo %s", 1, "bar"); | 98 StringAppendVTestHelper(&out, "%d foo %s", 1, "bar"); |
(...skipping 12 matching lines...) Expand all Loading... |
141 for (int i = 0; i < kBufLen - 1; ++i) | 111 for (int i = 0; i < kBufLen - 1; ++i) |
142 src[i] = 'a'; | 112 src[i] = 'a'; |
143 src[kBufLen - 1] = 0; | 113 src[kBufLen - 1] = 0; |
144 | 114 |
145 std::string out; | 115 std::string out; |
146 SStringPrintf(&out, "%s", src); | 116 SStringPrintf(&out, "%s", src); |
147 | 117 |
148 EXPECT_STREQ(src, out.c_str()); | 118 EXPECT_STREQ(src, out.c_str()); |
149 } | 119 } |
150 | 120 |
151 // TODO(evanm): what's the proper cross-platform test here? | |
152 #if defined(OS_WIN) | |
153 // sprintf in Visual Studio fails when given U+FFFF. This tests that the | |
154 // failure case is gracefuly handled. | |
155 TEST(StringPrintfTest, Invalid) { | |
156 wchar_t invalid[2]; | |
157 invalid[0] = 0xffff; | |
158 invalid[1] = 0; | |
159 | |
160 std::wstring out; | |
161 SStringPrintf(&out, L"%ls", invalid); | |
162 EXPECT_STREQ(L"", out.c_str()); | |
163 } | |
164 #endif | |
165 | |
166 // Test that StringPrintf and StringAppendV do not change errno. | 121 // Test that StringPrintf and StringAppendV do not change errno. |
167 TEST(StringPrintfTest, StringPrintfErrno) { | 122 TEST(StringPrintfTest, StringPrintfErrno) { |
168 errno = 1; | 123 errno = 1; |
169 EXPECT_EQ("", StringPrintf("%s", "")); | 124 EXPECT_EQ("", StringPrintf("%s", "")); |
170 EXPECT_EQ(1, errno); | 125 EXPECT_EQ(1, errno); |
171 std::string out; | 126 std::string out; |
172 StringAppendVTestHelper(&out, "%d foo %s", 1, "bar"); | 127 StringAppendVTestHelper(&out, "%d foo %s", 1, "bar"); |
173 EXPECT_EQ(1, errno); | 128 EXPECT_EQ(1, errno); |
174 } | 129 } |
175 | 130 |
176 } // namespace base | 131 } // namespace base |
OLD | NEW |