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
3 // found in the LICENSE file.
5 #include "base/basictypes.h"
6 #include "base/stringprintf.h"
7 #include "testing/gtest/include/gtest/gtest.h"
13 // A helper for the StringAppendV test that follows.
15 // Just forwards its args to StringAppendV.
16 static void StringAppendVTestHelper(std::string
* out
, const char* format
, ...) {
19 StringAppendV(out
, format
, ap
);
25 TEST(StringPrintfTest
, StringPrintfEmpty
) {
26 EXPECT_EQ("", StringPrintf("%s", ""));
29 TEST(StringPrintfTest
, StringPrintfMisc
) {
30 EXPECT_EQ("123hello w", StringPrintf("%3d%2s %1c", 123, "hello", 'w'));
31 #if !defined(OS_ANDROID)
32 EXPECT_EQ(L
"123hello w", StringPrintf(L
"%3d%2ls %1lc", 123, L
"hello", 'w'));
36 TEST(StringPrintfTest
, StringAppendfEmptyString
) {
37 std::string
value("Hello");
38 StringAppendF(&value
, "%s", "");
39 EXPECT_EQ("Hello", value
);
41 #if !defined(OS_ANDROID)
42 std::wstring
valuew(L
"Hello");
43 StringAppendF(&valuew
, L
"%ls", L
"");
44 EXPECT_EQ(L
"Hello", valuew
);
48 TEST(StringPrintfTest
, StringAppendfString
) {
49 std::string
value("Hello");
50 StringAppendF(&value
, " %s", "World");
51 EXPECT_EQ("Hello World", value
);
53 #if !defined(OS_ANDROID)
54 std::wstring
valuew(L
"Hello");
55 StringAppendF(&valuew
, L
" %ls", L
"World");
56 EXPECT_EQ(L
"Hello World", valuew
);
60 TEST(StringPrintfTest
, StringAppendfInt
) {
61 std::string
value("Hello");
62 StringAppendF(&value
, " %d", 123);
63 EXPECT_EQ("Hello 123", value
);
65 #if !defined(OS_ANDROID)
66 std::wstring
valuew(L
"Hello");
67 StringAppendF(&valuew
, L
" %d", 123);
68 EXPECT_EQ(L
"Hello 123", valuew
);
72 // Make sure that lengths exactly around the initial buffer size are handled
74 TEST(StringPrintfTest
, StringPrintfBounds
) {
75 const int kSrcLen
= 1026;
77 for (size_t i
= 0; i
< arraysize(src
); i
++)
80 wchar_t srcw
[kSrcLen
];
81 for (size_t i
= 0; i
< arraysize(srcw
); i
++)
84 for (int i
= 1; i
< 3; i
++) {
87 SStringPrintf(&out
, "%s", src
);
88 EXPECT_STREQ(src
, out
.c_str());
90 #if !defined(OS_ANDROID)
91 srcw
[kSrcLen
- i
] = 0;
93 SStringPrintf(&outw
, L
"%ls", srcw
);
94 EXPECT_STREQ(srcw
, outw
.c_str());
99 // Test very large sprintfs that will cause the buffer to grow.
100 TEST(StringPrintfTest
, Grow
) {
102 for (size_t i
= 0; i
< arraysize(src
); i
++)
106 const char* fmt
= "%sB%sB%sB%sB%sB%sB%s";
109 SStringPrintf(&out
, fmt
, src
, src
, src
, src
, src
, src
, src
);
111 const int kRefSize
= 320000;
112 char* ref
= new char[kRefSize
];
114 sprintf_s(ref
, kRefSize
, fmt
, src
, src
, src
, src
, src
, src
, src
);
115 #elif defined(OS_POSIX)
116 snprintf(ref
, kRefSize
, fmt
, src
, src
, src
, src
, src
, src
, src
);
119 EXPECT_STREQ(ref
, out
.c_str());
123 TEST(StringPrintfTest
, StringAppendV
) {
125 StringAppendVTestHelper(&out
, "%d foo %s", 1, "bar");
126 EXPECT_EQ("1 foo bar", out
);
129 // Test the boundary condition for the size of the string_util's
131 TEST(StringPrintfTest
, GrowBoundary
) {
132 const int string_util_buf_len
= 1024;
133 // Our buffer should be one larger than the size of StringAppendVT's stack
135 const int buf_len
= string_util_buf_len
+ 1;
136 char src
[buf_len
+ 1]; // Need extra one for NULL-terminator.
137 for (int i
= 0; i
< buf_len
; ++i
)
142 SStringPrintf(&out
, "%s", src
);
144 EXPECT_STREQ(src
, out
.c_str());
147 // TODO(evanm): what's the proper cross-platform test here?
149 // sprintf in Visual Studio fails when given U+FFFF. This tests that the
150 // failure case is gracefuly handled.
151 TEST(StringPrintfTest
, Invalid
) {
157 SStringPrintf(&out
, L
"%ls", invalid
);
158 EXPECT_STREQ(L
"", out
.c_str());
162 // Test that the positional parameters work.
163 TEST(StringPrintfTest
, PositionalParameters
) {
165 SStringPrintf(&out
, "%1$s %1$s", "test");
166 EXPECT_STREQ("test test", out
.c_str());
170 SStringPrintf(&wout
, L
"%1$ls %1$ls", L
"test");
171 EXPECT_STREQ(L
"test test", wout
.c_str());