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

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

Issue 774933011: Made sizeof code simpler and more obviously correct. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years 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 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
54 EXPECT_EQ('X', buf[1]); 54 EXPECT_EQ('X', buf[1]);
55 buf[0] = 'X'; 55 buf[0] = 'X';
56 } 56 }
57 57
58 TEST(SafeSPrintfTest, NoArguments) { 58 TEST(SafeSPrintfTest, NoArguments) {
59 // Output a text message that doesn't require any substitutions. This 59 // Output a text message that doesn't require any substitutions. This
60 // is roughly equivalent to calling strncpy() (but unlike strncpy(), it does 60 // is roughly equivalent to calling strncpy() (but unlike strncpy(), it does
61 // always add a trailing NUL; it always deduplicates '%' characters). 61 // always add a trailing NUL; it always deduplicates '%' characters).
62 static const char text[] = "hello world"; 62 static const char text[] = "hello world";
63 char ref[20], buf[20]; 63 char ref[20], buf[20];
64 memset(ref, 'X', sizeof(char) * arraysize(buf)); 64 memset(ref, 'X', sizeof(ref));
65 memcpy(buf, ref, sizeof(buf)); 65 memcpy(buf, ref, sizeof(buf));
66 66
67 // A negative buffer size should always result in an error. 67 // A negative buffer size should always result in an error.
68 EXPECT_EQ(-1, SafeSNPrintf(buf, static_cast<size_t>(-1), text)); 68 EXPECT_EQ(-1, SafeSNPrintf(buf, static_cast<size_t>(-1), text));
69 EXPECT_TRUE(!memcmp(buf, ref, sizeof(buf))); 69 EXPECT_TRUE(!memcmp(buf, ref, sizeof(buf)));
70 70
71 // Zero buffer size should always result in an error. 71 // Zero buffer size should always result in an error.
72 EXPECT_EQ(-1, SafeSNPrintf(buf, 0, text)); 72 EXPECT_EQ(-1, SafeSNPrintf(buf, 0, text));
73 EXPECT_TRUE(!memcmp(buf, ref, sizeof(buf))); 73 EXPECT_TRUE(!memcmp(buf, ref, sizeof(buf)));
74 74
(...skipping 675 matching lines...) Expand 10 before | Expand all | Expand 10 after
750 void *ptr = str; 750 void *ptr = str;
751 char buf[40]; 751 char buf[40];
752 EXPECT_EQ(10, SafeSPrintf(buf, "%p", str)); 752 EXPECT_EQ(10, SafeSPrintf(buf, "%p", str));
753 EXPECT_EQ("0x80000000", std::string(buf)); 753 EXPECT_EQ("0x80000000", std::string(buf));
754 EXPECT_EQ(10, SafeSPrintf(buf, "%p", ptr)); 754 EXPECT_EQ(10, SafeSPrintf(buf, "%p", ptr));
755 EXPECT_EQ("0x80000000", std::string(buf)); 755 EXPECT_EQ("0x80000000", std::string(buf));
756 } 756 }
757 757
758 } // namespace strings 758 } // namespace strings
759 } // namespace base 759 } // 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