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_ANDROID) | 34 #if defined(OS_WIN) |
35 EXPECT_EQ(L"123hello w", StringPrintf(L"%3d%2ls %1lc", 123, L"hello", 'w')); | 35 EXPECT_EQ(L"123hello w", StringPrintf(L"%3d%2ls %1lc", 123, L"hello", 'w')); |
36 #endif | 36 #endif |
37 } | 37 } |
38 | 38 |
39 TEST(StringPrintfTest, StringAppendfEmptyString) { | 39 TEST(StringPrintfTest, StringAppendfEmptyString) { |
40 std::string value("Hello"); | 40 std::string value("Hello"); |
41 StringAppendF(&value, "%s", ""); | 41 StringAppendF(&value, "%s", ""); |
42 EXPECT_EQ("Hello", value); | 42 EXPECT_EQ("Hello", value); |
43 | 43 |
44 #if !defined(OS_ANDROID) | 44 #if defined(OS_WIN) |
45 std::wstring valuew(L"Hello"); | 45 std::wstring valuew(L"Hello"); |
46 StringAppendF(&valuew, L"%ls", L""); | 46 StringAppendF(&valuew, L"%ls", L""); |
47 EXPECT_EQ(L"Hello", valuew); | 47 EXPECT_EQ(L"Hello", valuew); |
48 #endif | 48 #endif |
49 } | 49 } |
50 | 50 |
51 TEST(StringPrintfTest, StringAppendfString) { | 51 TEST(StringPrintfTest, StringAppendfString) { |
52 std::string value("Hello"); | 52 std::string value("Hello"); |
53 StringAppendF(&value, " %s", "World"); | 53 StringAppendF(&value, " %s", "World"); |
54 EXPECT_EQ("Hello World", value); | 54 EXPECT_EQ("Hello World", value); |
55 | 55 |
56 #if !defined(OS_ANDROID) | 56 #if defined(OS_WIN) |
57 std::wstring valuew(L"Hello"); | 57 std::wstring valuew(L"Hello"); |
58 StringAppendF(&valuew, L" %ls", L"World"); | 58 StringAppendF(&valuew, L" %ls", L"World"); |
59 EXPECT_EQ(L"Hello World", valuew); | 59 EXPECT_EQ(L"Hello World", valuew); |
60 #endif | 60 #endif |
61 } | 61 } |
62 | 62 |
63 TEST(StringPrintfTest, StringAppendfInt) { | 63 TEST(StringPrintfTest, StringAppendfInt) { |
64 std::string value("Hello"); | 64 std::string value("Hello"); |
65 StringAppendF(&value, " %d", 123); | 65 StringAppendF(&value, " %d", 123); |
66 EXPECT_EQ("Hello 123", value); | 66 EXPECT_EQ("Hello 123", value); |
67 | 67 |
68 #if !defined(OS_ANDROID) | 68 #if defined(OS_WIN) |
69 std::wstring valuew(L"Hello"); | 69 std::wstring valuew(L"Hello"); |
70 StringAppendF(&valuew, L" %d", 123); | 70 StringAppendF(&valuew, L" %d", 123); |
71 EXPECT_EQ(L"Hello 123", valuew); | 71 EXPECT_EQ(L"Hello 123", valuew); |
72 #endif | 72 #endif |
73 } | 73 } |
74 | 74 |
75 // Make sure that lengths exactly around the initial buffer size are handled | 75 // Make sure that lengths exactly around the initial buffer size are handled |
76 // correctly. | 76 // correctly. |
77 TEST(StringPrintfTest, StringPrintfBounds) { | 77 TEST(StringPrintfTest, StringPrintfBounds) { |
78 const int kSrcLen = 1026; | 78 const int kSrcLen = 1026; |
79 char src[kSrcLen]; | 79 char src[kSrcLen]; |
80 for (size_t i = 0; i < arraysize(src); i++) | 80 for (size_t i = 0; i < arraysize(src); i++) |
81 src[i] = 'A'; | 81 src[i] = 'A'; |
82 | 82 |
83 wchar_t srcw[kSrcLen]; | 83 wchar_t srcw[kSrcLen]; |
84 for (size_t i = 0; i < arraysize(srcw); i++) | 84 for (size_t i = 0; i < arraysize(srcw); i++) |
85 srcw[i] = 'A'; | 85 srcw[i] = 'A'; |
86 | 86 |
87 for (int i = 1; i < 3; i++) { | 87 for (int i = 1; i < 3; i++) { |
88 src[kSrcLen - i] = 0; | 88 src[kSrcLen - i] = 0; |
89 std::string out; | 89 std::string out; |
90 SStringPrintf(&out, "%s", src); | 90 SStringPrintf(&out, "%s", src); |
91 EXPECT_STREQ(src, out.c_str()); | 91 EXPECT_STREQ(src, out.c_str()); |
92 | 92 |
93 #if !defined(OS_ANDROID) | 93 #if defined(OS_WIN) |
94 srcw[kSrcLen - i] = 0; | 94 srcw[kSrcLen - i] = 0; |
95 std::wstring outw; | 95 std::wstring outw; |
96 SStringPrintf(&outw, L"%ls", srcw); | 96 SStringPrintf(&outw, L"%ls", srcw); |
97 EXPECT_STREQ(srcw, outw.c_str()); | 97 EXPECT_STREQ(srcw, outw.c_str()); |
98 #endif | 98 #endif |
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) { |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
180 TEST(StringPrintfTest, StringPrintfErrno) { | 180 TEST(StringPrintfTest, StringPrintfErrno) { |
181 errno = 1; | 181 errno = 1; |
182 EXPECT_EQ("", StringPrintf("%s", "")); | 182 EXPECT_EQ("", StringPrintf("%s", "")); |
183 EXPECT_EQ(1, errno); | 183 EXPECT_EQ(1, errno); |
184 std::string out; | 184 std::string out; |
185 StringAppendVTestHelper(&out, "%d foo %s", 1, "bar"); | 185 StringAppendVTestHelper(&out, "%d foo %s", 1, "bar"); |
186 EXPECT_EQ(1, errno); | 186 EXPECT_EQ(1, errno); |
187 } | 187 } |
188 | 188 |
189 } // namespace base | 189 } // namespace base |
OLD | NEW |