| 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/safe_sprintf.h" | 5 #include "base/strings/safe_sprintf.h" |
| 6 | 6 |
| 7 #include <stdio.h> | 7 #include <stdio.h> |
| 8 #include <string.h> | 8 #include <string.h> |
| 9 | 9 |
| 10 #include <limits> | 10 #include <limits> |
| (...skipping 246 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 257 EXPECT_EQ("\1\2\3\4\5\6\7", std::string(buf)); | 257 EXPECT_EQ("\1\2\3\4\5\6\7", std::string(buf)); |
| 258 EXPECT_EQ(8, SafeSNPrintf(buf, 11, "%c%c%c%c%c%c%c%c", | 258 EXPECT_EQ(8, SafeSNPrintf(buf, 11, "%c%c%c%c%c%c%c%c", |
| 259 1, 2, 3, 4, 5, 6, 7, 8)); | 259 1, 2, 3, 4, 5, 6, 7, 8)); |
| 260 EXPECT_EQ("\1\2\3\4\5\6\7\10", std::string(buf)); | 260 EXPECT_EQ("\1\2\3\4\5\6\7\10", std::string(buf)); |
| 261 EXPECT_EQ(9, SafeSNPrintf(buf, 11, "%c%c%c%c%c%c%c%c%c", | 261 EXPECT_EQ(9, SafeSNPrintf(buf, 11, "%c%c%c%c%c%c%c%c%c", |
| 262 1, 2, 3, 4, 5, 6, 7, 8, 9)); | 262 1, 2, 3, 4, 5, 6, 7, 8, 9)); |
| 263 EXPECT_EQ("\1\2\3\4\5\6\7\10\11", std::string(buf)); | 263 EXPECT_EQ("\1\2\3\4\5\6\7\10\11", std::string(buf)); |
| 264 EXPECT_EQ(10, SafeSNPrintf(buf, 11, "%c%c%c%c%c%c%c%c%c%c", | 264 EXPECT_EQ(10, SafeSNPrintf(buf, 11, "%c%c%c%c%c%c%c%c%c%c", |
| 265 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)); | 265 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)); |
| 266 EXPECT_EQ("\1\2\3\4\5\6\7\10\11\12", std::string(buf)); | 266 EXPECT_EQ("\1\2\3\4\5\6\7\10\11\12", std::string(buf)); |
| 267 |
| 268 EXPECT_EQ(11, SafeSPrintf(buf, "%c%c%c%c%c%c%c%c%c%c%c", |
| 269 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11)); |
| 270 EXPECT_EQ("\1\2\3\4\5\6\7\10\11\12\13", std::string(buf)); |
| 271 EXPECT_EQ(11, SafeSNPrintf(buf, 12, "%c%c%c%c%c%c%c%c%c%c%c", |
| 272 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11)); |
| 273 EXPECT_EQ("\1\2\3\4\5\6\7\10\11\12\13", std::string(buf)); |
| 267 } | 274 } |
| 268 | 275 |
| 269 TEST(SafeSPrintfTest, DataTypes) { | 276 TEST(SafeSPrintfTest, DataTypes) { |
| 270 char buf[40]; | 277 char buf[40]; |
| 271 | 278 |
| 272 // Bytes | 279 // Bytes |
| 273 EXPECT_EQ(1, SafeSPrintf(buf, "%d", (uint8_t)1)); | 280 EXPECT_EQ(1, SafeSPrintf(buf, "%d", (uint8_t)1)); |
| 274 EXPECT_EQ("1", std::string(buf)); | 281 EXPECT_EQ("1", std::string(buf)); |
| 275 EXPECT_EQ(3, SafeSPrintf(buf, "%d", (uint8_t)-1)); | 282 EXPECT_EQ(3, SafeSPrintf(buf, "%d", (uint8_t)-1)); |
| 276 EXPECT_EQ("255", std::string(buf)); | 283 EXPECT_EQ("255", std::string(buf)); |
| (...skipping 466 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 743 void *ptr = str; | 750 void *ptr = str; |
| 744 char buf[40]; | 751 char buf[40]; |
| 745 EXPECT_EQ(10, SafeSPrintf(buf, "%p", str)); | 752 EXPECT_EQ(10, SafeSPrintf(buf, "%p", str)); |
| 746 EXPECT_EQ("0x80000000", std::string(buf)); | 753 EXPECT_EQ("0x80000000", std::string(buf)); |
| 747 EXPECT_EQ(10, SafeSPrintf(buf, "%p", ptr)); | 754 EXPECT_EQ(10, SafeSPrintf(buf, "%p", ptr)); |
| 748 EXPECT_EQ("0x80000000", std::string(buf)); | 755 EXPECT_EQ("0x80000000", std::string(buf)); |
| 749 } | 756 } |
| 750 | 757 |
| 751 } // namespace strings | 758 } // namespace strings |
| 752 } // namespace base | 759 } // namespace base |
| OLD | NEW |