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 | |
269 // C++11 is smart enough to handle variadic template arguments. It can | |
270 // deal with arbitrary numbers of arguments. | |
271 #if __cplusplus >= 201103 // C++11 | |
272 EXPECT_EQ(11, SafeSPrintf(buf, "%c%c%c%c%c%c%c%c%c%c%c", | |
273 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11)); | |
274 EXPECT_EQ("\1\2\3\4\5\6\7\10\11\12\13", std::string(buf)); | |
275 EXPECT_EQ(11, SafeSNPrintf(buf, 12, "%c%c%c%c%c%c%c%c%c%c%c", | |
276 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11)); | |
277 EXPECT_EQ("\1\2\3\4\5\6\7\10\11\12\13", std::string(buf)); | |
278 #endif | |
279 } | 267 } |
280 | 268 |
281 TEST(SafeSPrintfTest, DataTypes) { | 269 TEST(SafeSPrintfTest, DataTypes) { |
282 char buf[40]; | 270 char buf[40]; |
283 | 271 |
284 // Bytes | 272 // Bytes |
285 EXPECT_EQ(1, SafeSPrintf(buf, "%d", (uint8_t)1)); | 273 EXPECT_EQ(1, SafeSPrintf(buf, "%d", (uint8_t)1)); |
286 EXPECT_EQ("1", std::string(buf)); | 274 EXPECT_EQ("1", std::string(buf)); |
287 EXPECT_EQ(3, SafeSPrintf(buf, "%d", (uint8_t)-1)); | 275 EXPECT_EQ(3, SafeSPrintf(buf, "%d", (uint8_t)-1)); |
288 EXPECT_EQ("255", std::string(buf)); | 276 EXPECT_EQ("255", std::string(buf)); |
(...skipping 465 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
754 void *ptr = str; | 742 void *ptr = str; |
755 char buf[40]; | 743 char buf[40]; |
756 EXPECT_EQ(10, SafeSPrintf(buf, "%p", str)); | 744 EXPECT_EQ(10, SafeSPrintf(buf, "%p", str)); |
757 EXPECT_EQ("0x80000000", std::string(buf)); | 745 EXPECT_EQ("0x80000000", std::string(buf)); |
758 EXPECT_EQ(10, SafeSPrintf(buf, "%p", ptr)); | 746 EXPECT_EQ(10, SafeSPrintf(buf, "%p", ptr)); |
759 EXPECT_EQ("0x80000000", std::string(buf)); | 747 EXPECT_EQ("0x80000000", std::string(buf)); |
760 } | 748 } |
761 | 749 |
762 } // namespace strings | 750 } // namespace strings |
763 } // namespace base | 751 } // namespace base |
OLD | NEW |