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

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, 1 month 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
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 1061 matching lines...) Expand 10 before | Expand all | Expand 10 after
1072 EXPECT_TRUE(ContainsOnlyChars("", "")); 1072 EXPECT_TRUE(ContainsOnlyChars("", ""));
1073 EXPECT_FALSE(ContainsOnlyChars("Hello", "")); 1073 EXPECT_FALSE(ContainsOnlyChars("Hello", ""));
1074 1074
1075 EXPECT_TRUE(ContainsOnlyChars("", "1234")); 1075 EXPECT_TRUE(ContainsOnlyChars("", "1234"));
1076 EXPECT_TRUE(ContainsOnlyChars("1", "1234")); 1076 EXPECT_TRUE(ContainsOnlyChars("1", "1234"));
1077 EXPECT_TRUE(ContainsOnlyChars("1", "4321")); 1077 EXPECT_TRUE(ContainsOnlyChars("1", "4321"));
1078 EXPECT_TRUE(ContainsOnlyChars("123", "4321")); 1078 EXPECT_TRUE(ContainsOnlyChars("123", "4321"));
1079 EXPECT_FALSE(ContainsOnlyChars("123a", "4321")); 1079 EXPECT_FALSE(ContainsOnlyChars("123a", "4321"));
1080 } 1080 }
1081 1081
1082 TEST(StringUtilTest, WriteInto) { 1082 class WriteIntoTest : public testing::Test {
1083 protected:
1084 void WritesCorrectly(size_t num_chars) {
1085 std::string buffer;
1086 char kOriginal[] = "supercali";
1087 strncpy(WriteInto(&buffer, num_chars + 1), kOriginal, num_chars);
1088 // Using std::string(buffer.c_str()) instead of |buffer| truncates the
1089 // string at the first \0.
1090 EXPECT_EQ(std::string(kOriginal,
1091 std::min(num_chars, arraysize(kOriginal) - 1)),
1092 std::string(buffer.c_str()));
1093 EXPECT_EQ(num_chars, buffer.size());
1094 }
1095 };
1096
1097 TEST_F(WriteIntoTest, WriteInto) {
1083 // Validate that WriteInto reserves enough space and 1098 // Validate that WriteInto reserves enough space and
1084 // sizes a string correctly. 1099 // sizes a string correctly.
1085 std::string buffer; 1100 WritesCorrectly(1);
1086 const char kOriginal[] = "supercali"; 1101 WritesCorrectly(2);
1087 strncpy(WriteInto(&buffer, 1), kOriginal, 0); 1102 WritesCorrectly(5000);
1088 EXPECT_STREQ("", buffer.c_str());
1089 EXPECT_EQ(0u, buffer.size());
1090 strncpy(WriteInto(&buffer, 2), kOriginal, 1);
1091 EXPECT_STREQ("s", buffer.c_str());
1092 EXPECT_EQ(1u, buffer.size());
1093 strncpy(WriteInto(&buffer, 3), kOriginal, 2);
1094 EXPECT_STREQ("su", buffer.c_str());
1095 EXPECT_EQ(2u, buffer.size());
1096 strncpy(WriteInto(&buffer, 5001), kOriginal, 5000);
1097 EXPECT_STREQ("supercali", buffer.c_str());
1098 EXPECT_EQ(5000u, buffer.size());
1099 strncpy(WriteInto(&buffer, 3), kOriginal, 2);
1100 EXPECT_STREQ("su", buffer.c_str());
1101 EXPECT_EQ(2u, buffer.size());
1102 strncpy(WriteInto(&buffer, 1), kOriginal, 0);
1103 EXPECT_STREQ("", buffer.c_str());
1104 EXPECT_EQ(0u, buffer.size());
1105 1103
1106 // Validate that WriteInto returns NULL only when 1104 // Validate that WriteInto returns NULL only when
1107 // |length_with_null| == 1. 1105 // |length_with_null| == 1.
1106 std::string buffer;
1108 EXPECT_TRUE(WriteInto(&buffer, 1) == NULL); 1107 EXPECT_TRUE(WriteInto(&buffer, 1) == NULL);
1109 EXPECT_TRUE(WriteInto(&buffer, 2) != NULL); 1108 EXPECT_TRUE(WriteInto(&buffer, 2) != NULL);
1110 1109
1111 // Validate that WriteInto doesn't modify other strings 1110 // Validate that WriteInto doesn't modify other strings
1112 // when using a Copy-on-Write implementation. 1111 // when using a Copy-on-Write implementation.
1113 const char kLive[] = "live"; 1112 const char kLive[] = "live";
1114 const char kDead[] = "dead"; 1113 const char kDead[] = "dead";
1115 const std::string live = kLive; 1114 const std::string live = kLive;
1116 std::string dead = live; 1115 std::string dead = live;
1117 strncpy(WriteInto(&dead, 5), kDead, 4); 1116 strncpy(WriteInto(&dead, 5), kDead, 4);
1118 EXPECT_STREQ(kDead, dead.c_str()); 1117 EXPECT_EQ(kDead, dead);
1119 EXPECT_EQ(4u, dead.size()); 1118 EXPECT_EQ(4u, dead.size());
1120 EXPECT_STREQ(kLive, live.c_str()); 1119 EXPECT_EQ(kLive, live);
1121 EXPECT_EQ(4u, live.size()); 1120 EXPECT_EQ(4u, live.size());
1122 } 1121 }
1123 1122
1124 } // namespace base 1123 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698