| 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 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 141 for (int i = 0; i < kBufLen - 1; ++i) | 141 for (int i = 0; i < kBufLen - 1; ++i) |
| 142 src[i] = 'a'; | 142 src[i] = 'a'; |
| 143 src[kBufLen - 1] = 0; | 143 src[kBufLen - 1] = 0; |
| 144 | 144 |
| 145 std::string out; | 145 std::string out; |
| 146 SStringPrintf(&out, "%s", src); | 146 SStringPrintf(&out, "%s", src); |
| 147 | 147 |
| 148 EXPECT_STREQ(src, out.c_str()); | 148 EXPECT_STREQ(src, out.c_str()); |
| 149 } | 149 } |
| 150 | 150 |
| 151 // TODO(evanm): what's the proper cross-platform test here? | |
| 152 #if defined(OS_WIN) | 151 #if defined(OS_WIN) |
| 153 // sprintf in Visual Studio fails when given U+FFFF. This tests that the | 152 // vswprintf in Visual Studio 2013 fails when given U+FFFF. This tests that the |
| 154 // failure case is gracefuly handled. | 153 // failure case is gracefuly handled. In Visual Studio 2015 the bad character |
| 154 // is passed through. |
| 155 TEST(StringPrintfTest, Invalid) { | 155 TEST(StringPrintfTest, Invalid) { |
| 156 wchar_t invalid[2]; | 156 wchar_t invalid[2]; |
| 157 invalid[0] = 0xffff; | 157 invalid[0] = 0xffff; |
| 158 invalid[1] = 0; | 158 invalid[1] = 0; |
| 159 | 159 |
| 160 std::wstring out; | 160 std::wstring out; |
| 161 SStringPrintf(&out, L"%ls", invalid); | 161 SStringPrintf(&out, L"%ls", invalid); |
| 162 #if _MSC_VER >= 1900 |
| 163 EXPECT_STREQ(invalid, out.c_str()); |
| 164 #else |
| 162 EXPECT_STREQ(L"", out.c_str()); | 165 EXPECT_STREQ(L"", out.c_str()); |
| 166 #endif |
| 163 } | 167 } |
| 164 #endif | 168 #endif |
| 165 | 169 |
| 166 // Test that StringPrintf and StringAppendV do not change errno. | 170 // Test that StringPrintf and StringAppendV do not change errno. |
| 167 TEST(StringPrintfTest, StringPrintfErrno) { | 171 TEST(StringPrintfTest, StringPrintfErrno) { |
| 168 errno = 1; | 172 errno = 1; |
| 169 EXPECT_EQ("", StringPrintf("%s", "")); | 173 EXPECT_EQ("", StringPrintf("%s", "")); |
| 170 EXPECT_EQ(1, errno); | 174 EXPECT_EQ(1, errno); |
| 171 std::string out; | 175 std::string out; |
| 172 StringAppendVTestHelper(&out, "%d foo %s", 1, "bar"); | 176 StringAppendVTestHelper(&out, "%d foo %s", 1, "bar"); |
| 173 EXPECT_EQ(1, errno); | 177 EXPECT_EQ(1, errno); |
| 174 } | 178 } |
| 175 | 179 |
| 176 } // namespace base | 180 } // namespace base |
| OLD | NEW |