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

Unified 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « base/string_util.h ('k') | chrome/browser/automation/testing_automation_provider_win.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/string_util_unittest.cc
===================================================================
--- base/string_util_unittest.cc (revision 111826)
+++ base/string_util_unittest.cc (working copy)
@@ -1113,35 +1113,28 @@
EXPECT_FALSE(ContainsOnlyChars("123a", "4321"));
}
-TEST(StringUtilTest, WriteInto) {
+class WriteIntoTest : public testing::Test {
+ protected:
+ static void WritesCorrectly(size_t num_chars) {
+ std::string buffer;
+ char kOriginal[] = "supercali";
+ strncpy(WriteInto(&buffer, num_chars + 1), kOriginal, num_chars);
+ // Using std::string(buffer.c_str()) instead of |buffer| truncates the
+ // string at the first \0.
+ EXPECT_EQ(std::string(kOriginal,
+ std::min(num_chars, arraysize(kOriginal) - 1)),
+ std::string(buffer.c_str()));
+ EXPECT_EQ(num_chars, buffer.size());
+ }
+};
+
+TEST_F(WriteIntoTest, WriteInto) {
// Validate that WriteInto reserves enough space and
// sizes a string correctly.
- std::string buffer;
- const char kOriginal[] = "supercali";
- strncpy(WriteInto(&buffer, 1), kOriginal, 0);
- EXPECT_STREQ("", buffer.c_str());
- EXPECT_EQ(0u, buffer.size());
- strncpy(WriteInto(&buffer, 2), kOriginal, 1);
- EXPECT_STREQ("s", buffer.c_str());
- EXPECT_EQ(1u, buffer.size());
- strncpy(WriteInto(&buffer, 3), kOriginal, 2);
- EXPECT_STREQ("su", buffer.c_str());
- EXPECT_EQ(2u, buffer.size());
- strncpy(WriteInto(&buffer, 5001), kOriginal, 5000);
- EXPECT_STREQ("supercali", buffer.c_str());
- EXPECT_EQ(5000u, buffer.size());
- strncpy(WriteInto(&buffer, 3), kOriginal, 2);
- EXPECT_STREQ("su", buffer.c_str());
- EXPECT_EQ(2u, buffer.size());
- strncpy(WriteInto(&buffer, 1), kOriginal, 0);
- EXPECT_STREQ("", buffer.c_str());
- EXPECT_EQ(0u, buffer.size());
+ WritesCorrectly(1);
+ WritesCorrectly(2);
+ WritesCorrectly(5000);
- // Validate that WriteInto returns NULL only when
- // |length_with_null| == 1.
- EXPECT_TRUE(WriteInto(&buffer, 1) == NULL);
- EXPECT_TRUE(WriteInto(&buffer, 2) != NULL);
-
// Validate that WriteInto doesn't modify other strings
// when using a Copy-on-Write implementation.
const char kLive[] = "live";
@@ -1149,9 +1142,9 @@
const std::string live = kLive;
std::string dead = live;
strncpy(WriteInto(&dead, 5), kDead, 4);
- EXPECT_STREQ(kDead, dead.c_str());
+ EXPECT_EQ(kDead, dead);
EXPECT_EQ(4u, dead.size());
- EXPECT_STREQ(kLive, live.c_str());
+ EXPECT_EQ(kLive, live);
EXPECT_EQ(4u, live.size());
}
« 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