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

Unified Diff: base/stl_util_unittest.cc

Issue 851823005: Add unit tests for string_as_array(). (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: nit Created 5 years, 11 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/stl_util_unittest.cc
diff --git a/base/stl_util_unittest.cc b/base/stl_util_unittest.cc
index a3f8e16f2ce2c0a5411d1491254245c80d89ab3d..06ea7cd08f01e03c3a3b6aca8651f6182a328b8f 100644
--- a/base/stl_util_unittest.cc
+++ b/base/stl_util_unittest.cc
@@ -238,5 +238,30 @@ TEST(STLUtilTest, STLIncludes) {
EXPECT_TRUE(STLIncludes<std::set<int> >(a3, a2));
}
+TEST(StringAsArrayTest, Empty) {
+ std::string empty;
+ EXPECT_EQ(nullptr, string_as_array(&empty));
+}
+
+TEST(StringAsArrayTest, NullTerminated) {
+ // If any std::string implementation is not null-terminated, this should
+ // fail. All compilers we use return a null-terminated buffer, but please do
+ // not rely on this fact in your code.
+ std::string str("abcde");
+ str.resize(3);
+ EXPECT_STREQ("abc", string_as_array(&str));
+}
+
+TEST(StringAsArrayTest, WriteCopy) {
+ // With a COW implementation, this test will fail if
+ // string_as_array(&str) is implemented as
+ // const_cast<char*>(str->data()).
+ std::string s1("abc");
+ const std::string s2(s1);
+ string_as_array(&s1)[1] = 'x';
+ EXPECT_EQ("axc", s1);
+ EXPECT_EQ("abc", s2);
+}
+
} // namespace
} // namespace base
« 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