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

Side by Side Diff: base/string_util_unittest.cc

Issue 8418034: Make string_util::WriteInto() DCHECK() that the supplied |length_with_null| > 1, meaning that the... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 9 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 | Annotate | Revision Log
« no previous file with comments | « base/string_util.h ('k') | chrome/browser/automation/testing_automation_provider_win.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 <math.h> 5 #include <math.h>
6 #include <stdarg.h> 6 #include <stdarg.h>
7 7
8 #include <limits> 8 #include <limits>
9 #include <sstream> 9 #include <sstream>
10 10
(...skipping 1095 matching lines...) Expand 10 before | Expand all | Expand 10 after
1106 EXPECT_TRUE(ContainsOnlyChars("", "")); 1106 EXPECT_TRUE(ContainsOnlyChars("", ""));
1107 EXPECT_FALSE(ContainsOnlyChars("Hello", "")); 1107 EXPECT_FALSE(ContainsOnlyChars("Hello", ""));
1108 1108
1109 EXPECT_TRUE(ContainsOnlyChars("", "1234")); 1109 EXPECT_TRUE(ContainsOnlyChars("", "1234"));
1110 EXPECT_TRUE(ContainsOnlyChars("1", "1234")); 1110 EXPECT_TRUE(ContainsOnlyChars("1", "1234"));
1111 EXPECT_TRUE(ContainsOnlyChars("1", "4321")); 1111 EXPECT_TRUE(ContainsOnlyChars("1", "4321"));
1112 EXPECT_TRUE(ContainsOnlyChars("123", "4321")); 1112 EXPECT_TRUE(ContainsOnlyChars("123", "4321"));
1113 EXPECT_FALSE(ContainsOnlyChars("123a", "4321")); 1113 EXPECT_FALSE(ContainsOnlyChars("123a", "4321"));
1114 } 1114 }
1115 1115
1116 TEST(StringUtilTest, WriteInto) { 1116 class WriteIntoTest : public testing::Test {
1117 protected:
1118 static void WritesCorrectly(size_t num_chars) {
1119 std::string buffer;
1120 char kOriginal[] = "supercali";
1121 strncpy(WriteInto(&buffer, num_chars + 1), kOriginal, num_chars);
1122 // Using std::string(buffer.c_str()) instead of |buffer| truncates the
1123 // string at the first \0.
1124 EXPECT_EQ(std::string(kOriginal,
1125 std::min(num_chars, arraysize(kOriginal) - 1)),
1126 std::string(buffer.c_str()));
1127 EXPECT_EQ(num_chars, buffer.size());
1128 }
1129 };
1130
1131 TEST_F(WriteIntoTest, WriteInto) {
1117 // Validate that WriteInto reserves enough space and 1132 // Validate that WriteInto reserves enough space and
1118 // sizes a string correctly. 1133 // sizes a string correctly.
1119 std::string buffer; 1134 WritesCorrectly(1);
1120 const char kOriginal[] = "supercali"; 1135 WritesCorrectly(2);
1121 strncpy(WriteInto(&buffer, 1), kOriginal, 0); 1136 WritesCorrectly(5000);
1122 EXPECT_STREQ("", buffer.c_str());
1123 EXPECT_EQ(0u, buffer.size());
1124 strncpy(WriteInto(&buffer, 2), kOriginal, 1);
1125 EXPECT_STREQ("s", buffer.c_str());
1126 EXPECT_EQ(1u, buffer.size());
1127 strncpy(WriteInto(&buffer, 3), kOriginal, 2);
1128 EXPECT_STREQ("su", buffer.c_str());
1129 EXPECT_EQ(2u, buffer.size());
1130 strncpy(WriteInto(&buffer, 5001), kOriginal, 5000);
1131 EXPECT_STREQ("supercali", buffer.c_str());
1132 EXPECT_EQ(5000u, buffer.size());
1133 strncpy(WriteInto(&buffer, 3), kOriginal, 2);
1134 EXPECT_STREQ("su", buffer.c_str());
1135 EXPECT_EQ(2u, buffer.size());
1136 strncpy(WriteInto(&buffer, 1), kOriginal, 0);
1137 EXPECT_STREQ("", buffer.c_str());
1138 EXPECT_EQ(0u, buffer.size());
1139
1140 // Validate that WriteInto returns NULL only when
1141 // |length_with_null| == 1.
1142 EXPECT_TRUE(WriteInto(&buffer, 1) == NULL);
1143 EXPECT_TRUE(WriteInto(&buffer, 2) != NULL);
1144 1137
1145 // Validate that WriteInto doesn't modify other strings 1138 // Validate that WriteInto doesn't modify other strings
1146 // when using a Copy-on-Write implementation. 1139 // when using a Copy-on-Write implementation.
1147 const char kLive[] = "live"; 1140 const char kLive[] = "live";
1148 const char kDead[] = "dead"; 1141 const char kDead[] = "dead";
1149 const std::string live = kLive; 1142 const std::string live = kLive;
1150 std::string dead = live; 1143 std::string dead = live;
1151 strncpy(WriteInto(&dead, 5), kDead, 4); 1144 strncpy(WriteInto(&dead, 5), kDead, 4);
1152 EXPECT_STREQ(kDead, dead.c_str()); 1145 EXPECT_EQ(kDead, dead);
1153 EXPECT_EQ(4u, dead.size()); 1146 EXPECT_EQ(4u, dead.size());
1154 EXPECT_STREQ(kLive, live.c_str()); 1147 EXPECT_EQ(kLive, live);
1155 EXPECT_EQ(4u, live.size()); 1148 EXPECT_EQ(4u, live.size());
1156 } 1149 }
1157 1150
1158 } // namespace base 1151 } // namespace base
OLDNEW
« no previous file with comments | « base/string_util.h ('k') | chrome/browser/automation/testing_automation_provider_win.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698