Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(129)

Side by Side Diff: base/strings/safe_sprintf_unittest.cc

Issue 310323003: Fix SafeSPrintfTest.Truncation in base_unittests (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Use c++ cast style and fix typo in comment Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 398 matching lines...) Expand 10 before | Expand all | Expand 10 after
409 break; 409 break;
410 } 410 }
411 } 411 }
412 412
413 // All trailing bytes are unchanged. 413 // All trailing bytes are unchanged.
414 for (size_t i = len+1; i < sz+2; ++i) 414 for (size_t i = len+1; i < sz+2; ++i)
415 EXPECT_EQ('X', tmp[i]); 415 EXPECT_EQ('X', tmp[i]);
416 416
417 // The text that was generated by SafeSPrintf() should always match the 417 // The text that was generated by SafeSPrintf() should always match the
418 // equivalent text generated by sprintf(). Please note that the format 418 // equivalent text generated by sprintf(). Please note that the format
419 // string for sprintf() is nor complicated, as it does not have the 419 // string for sprintf() is not complicated, as it does not have the
420 // benefit of getting type information from the C++ compiler. 420 // benefit of getting type information from the C++ compiler.
421 // 421 //
422 // N.B.: It would be so much cleaner to use snprintf(). But unfortunately, 422 // N.B.: It would be so much cleaner to use snprintf(). But unfortunately,
423 // Visual Studio doesn't support this function, and the work-arounds 423 // Visual Studio doesn't support this function, and the work-arounds
424 // are all really awkward. 424 // are all really awkward.
425 char ref[256]; 425 char ref[256];
426 CHECK_LE(sz, sizeof(ref)); 426 CHECK_LE(sz, sizeof(ref));
427 sprintf(ref, "A long string: %%d 00DEADBEEF %lld 0x%llX <NULL>", 427 sprintf(ref, "A long string: %%d 00DEADBEEF %lld 0x%llX <NULL>",
428 static_cast<long long>(std::numeric_limits<intptr_t>::min()), 428 static_cast<long long>(std::numeric_limits<intptr_t>::min()),
429 (long long)PrintLongString); 429 static_cast<unsigned long long>(
430 reinterpret_cast<uintptr_t>(PrintLongString)));
430 ref[sz-1] = '\000'; 431 ref[sz-1] = '\000';
431 432
432 #if defined(NDEBUG) 433 #if defined(NDEBUG)
433 const size_t kSSizeMax = std::numeric_limits<ssize_t>::max(); 434 const size_t kSSizeMax = std::numeric_limits<ssize_t>::max();
434 #else 435 #else
435 const size_t kSSizeMax = internal::GetSafeSPrintfSSizeMaxForTest(); 436 const size_t kSSizeMax = internal::GetSafeSPrintfSSizeMaxForTest();
436 #endif 437 #endif
437 438
438 // Compare the output from SafeSPrintf() to the one from sprintf(). 439 // Compare the output from SafeSPrintf() to the one from sprintf().
439 EXPECT_EQ(std::string(ref).substr(0, kSSizeMax-1), std::string(tmp.get())); 440 EXPECT_EQ(std::string(ref).substr(0, kSSizeMax-1), std::string(tmp.get()));
(...skipping 302 matching lines...) Expand 10 before | Expand all | Expand 10 after
742 void *ptr = str; 743 void *ptr = str;
743 char buf[40]; 744 char buf[40];
744 EXPECT_EQ(10, SafeSPrintf(buf, "%p", str)); 745 EXPECT_EQ(10, SafeSPrintf(buf, "%p", str));
745 EXPECT_EQ("0x80000000", std::string(buf)); 746 EXPECT_EQ("0x80000000", std::string(buf));
746 EXPECT_EQ(10, SafeSPrintf(buf, "%p", ptr)); 747 EXPECT_EQ(10, SafeSPrintf(buf, "%p", ptr));
747 EXPECT_EQ("0x80000000", std::string(buf)); 748 EXPECT_EQ("0x80000000", std::string(buf));
748 } 749 }
749 750
750 } // namespace strings 751 } // namespace strings
751 } // namespace base 752 } // namespace base
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698