Tab Audio Mirroring: WebContentsAudioInputStream is a new implementation which repres...
[chromium-blink-merge.git] / base / stringprintf_unittest.cc
blob305d24a881fda67c28b478313d4477e519b78c8e
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"
9 namespace base {
11 namespace {
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, ...) {
17 va_list ap;
18 va_start(ap, format);
19 StringAppendV(out, format, ap);
20 va_end(ap);
23 } // namespace
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'));
33 #endif
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);
45 #endif
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);
57 #endif
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);
69 #endif
72 // Make sure that lengths exactly around the initial buffer size are handled
73 // correctly.
74 TEST(StringPrintfTest, StringPrintfBounds) {
75 const int kSrcLen = 1026;
76 char src[kSrcLen];
77 for (size_t i = 0; i < arraysize(src); i++)
78 src[i] = 'A';
80 wchar_t srcw[kSrcLen];
81 for (size_t i = 0; i < arraysize(srcw); i++)
82 srcw[i] = 'A';
84 for (int i = 1; i < 3; i++) {
85 src[kSrcLen - i] = 0;
86 std::string out;
87 SStringPrintf(&out, "%s", src);
88 EXPECT_STREQ(src, out.c_str());
90 #if !defined(OS_ANDROID)
91 srcw[kSrcLen - i] = 0;
92 std::wstring outw;
93 SStringPrintf(&outw, L"%ls", srcw);
94 EXPECT_STREQ(srcw, outw.c_str());
95 #endif
99 // Test very large sprintfs that will cause the buffer to grow.
100 TEST(StringPrintfTest, Grow) {
101 char src[1026];
102 for (size_t i = 0; i < arraysize(src); i++)
103 src[i] = 'A';
104 src[1025] = 0;
106 const char* fmt = "%sB%sB%sB%sB%sB%sB%s";
108 std::string out;
109 SStringPrintf(&out, fmt, src, src, src, src, src, src, src);
111 const int kRefSize = 320000;
112 char* ref = new char[kRefSize];
113 #if defined(OS_WIN)
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);
117 #endif
119 EXPECT_STREQ(ref, out.c_str());
120 delete[] ref;
123 TEST(StringPrintfTest, StringAppendV) {
124 std::string out;
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
130 // internal buffer.
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
134 // buffer.
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)
138 src[i] = 'a';
139 src[buf_len] = 0;
141 std::string out;
142 SStringPrintf(&out, "%s", src);
144 EXPECT_STREQ(src, out.c_str());
147 // TODO(evanm): what's the proper cross-platform test here?
148 #if defined(OS_WIN)
149 // sprintf in Visual Studio fails when given U+FFFF. This tests that the
150 // failure case is gracefuly handled.
151 TEST(StringPrintfTest, Invalid) {
152 wchar_t invalid[2];
153 invalid[0] = 0xffff;
154 invalid[1] = 0;
156 std::wstring out;
157 SStringPrintf(&out, L"%ls", invalid);
158 EXPECT_STREQ(L"", out.c_str());
160 #endif
162 // Test that the positional parameters work.
163 TEST(StringPrintfTest, PositionalParameters) {
164 std::string out;
165 SStringPrintf(&out, "%1$s %1$s", "test");
166 EXPECT_STREQ("test test", out.c_str());
168 #if defined(OS_WIN)
169 std::wstring wout;
170 SStringPrintf(&wout, L"%1$ls %1$ls", L"test");
171 EXPECT_STREQ(L"test test", wout.c_str());
172 #endif
175 } // namespace base