diff options
author | Ryan Harrison <rharrison@chromium.org> | 2017-08-15 10:37:59 -0400 |
---|---|---|
committer | Chromium commit bot <commit-bot@chromium.org> | 2017-08-15 15:03:10 +0000 |
commit | 8a1758bf11c2d741e0cddc761b1dd2cdf564db93 (patch) | |
tree | 82cbafc46f574a05ae0c1d1d3d7f9ebde6cb932d /core/fxcrt/cfx_bytestring_unittest.cpp | |
parent | 171cb27a720036c48ae3a6176084e880742af0a9 (diff) | |
download | pdfium-8a1758bf11c2d741e0cddc761b1dd2cdf564db93.tar.xz |
Remove GetAt from string classes
This method duplicates the behaviour of the const [] operator and
doesn't offer any additional safety. Folding them into one
implementation.
SetAt is retained, since implementing the non-const [] operator to
replace SetAt has potential performance concerns. Specifically many
non-obvious cases of reading an element using [] will cause a realloc
& copy.
BUG=pdfium:860
Change-Id: I3ef5e5e5a15376f040256b646eb0d90636e24b67
Reviewed-on: https://pdfium-review.googlesource.com/10870
Commit-Queue: Ryan Harrison <rharrison@chromium.org>
Reviewed-by: Tom Sepez <tsepez@chromium.org>
Diffstat (limited to 'core/fxcrt/cfx_bytestring_unittest.cpp')
-rw-r--r-- | core/fxcrt/cfx_bytestring_unittest.cpp | 79 |
1 files changed, 28 insertions, 51 deletions
diff --git a/core/fxcrt/cfx_bytestring_unittest.cpp b/core/fxcrt/cfx_bytestring_unittest.cpp index 680a37e342..35f407f6ec 100644 --- a/core/fxcrt/cfx_bytestring_unittest.cpp +++ b/core/fxcrt/cfx_bytestring_unittest.cpp @@ -11,26 +11,8 @@ #include "testing/gtest/include/gtest/gtest.h" #include "third_party/base/stl_util.h" -TEST(fxcrt, ByteStringGetAt) { - CFX_ByteString short_string("a"); - CFX_ByteString longer_string("abc"); - CFX_ByteString embedded_nul_string("ab\0c", 4); - -#ifndef NDEBUG - EXPECT_DEATH({ short_string.GetAt(-1); }, ".*"); -#endif - EXPECT_EQ('a', short_string.GetAt(0)); -#ifndef NDEBUG - EXPECT_DEATH({ short_string.GetAt(1); }, ".*"); -#endif - EXPECT_EQ('c', longer_string.GetAt(2)); - EXPECT_EQ('b', embedded_nul_string.GetAt(1)); - EXPECT_EQ('\0', embedded_nul_string.GetAt(2)); - EXPECT_EQ('c', embedded_nul_string.GetAt(3)); -} - -TEST(fxcrt, ByteStringOperatorSubscript) { - CFX_ByteString abc("abc"); +TEST(fxcrt, ByteStringElementAccess) { + const CFX_ByteString abc("abc"); #ifndef NDEBUG EXPECT_DEATH({ abc[-1]; }, ".*"); #endif @@ -40,22 +22,35 @@ TEST(fxcrt, ByteStringOperatorSubscript) { #ifndef NDEBUG EXPECT_DEATH({ abc[3]; }, ".*"); #endif -} -TEST(fxcrt, ByteStringSetAt) { - // CFX_ByteString includes the NUL terminator for non-empty strings. - CFX_ByteString abc("abc"); + CFX_ByteString mutable_abc = abc; + EXPECT_EQ(abc.c_str(), mutable_abc.c_str()); + EXPECT_EQ('a', mutable_abc[0]); + EXPECT_EQ('b', mutable_abc[1]); + EXPECT_EQ('c', mutable_abc[2]); + EXPECT_EQ(abc.c_str(), mutable_abc.c_str()); + #ifndef NDEBUG - EXPECT_DEATH({ abc.SetAt(-1, 'd'); }, ".*"); + EXPECT_DEATH({ mutable_abc.SetAt(-1, 'd'); }, ".*"); + EXPECT_EQ("abc", abc); #endif - abc.SetAt(0, 'd'); - EXPECT_EQ("dbc", abc); - abc.SetAt(1, 'e'); - EXPECT_EQ("dec", abc); - abc.SetAt(2, 'f'); - EXPECT_EQ("def", abc); + const char* c_str = abc.c_str(); + mutable_abc.SetAt(0, 'd'); + EXPECT_EQ(c_str, abc.c_str()); + EXPECT_NE(c_str, mutable_abc.c_str()); + EXPECT_EQ("abc", abc); + EXPECT_EQ("dbc", mutable_abc); + + mutable_abc.SetAt(1, 'e'); + EXPECT_EQ("abc", abc); + EXPECT_EQ("dec", mutable_abc); + + mutable_abc.SetAt(2, 'f'); + EXPECT_EQ("abc", abc); + EXPECT_EQ("def", mutable_abc); #ifndef NDEBUG - EXPECT_DEATH({ abc.SetAt(3, 'g'); }, ".*"); + EXPECT_DEATH({ mutable_abc.SetAt(3, 'g'); }, ".*"); + EXPECT_EQ("abc", abc); #endif } @@ -943,25 +938,7 @@ TEST(fxcrt, ByteStringCMid) { EXPECT_EQ(trailing_substring, longer_string.Mid(4, 3)); } -TEST(fxcrt, ByteStringCGetAt) { - CFX_ByteStringC short_string("a"); - CFX_ByteStringC longer_string("abc"); - CFX_ByteStringC embedded_nul_string("ab\0c", 4); - -#ifndef NDEBUG - EXPECT_DEATH({ short_string.GetAt(-1); }, ".*"); -#endif - EXPECT_EQ('a', static_cast<char>(short_string.GetAt(0))); -#ifndef NDEBUG - EXPECT_DEATH({ short_string.GetAt(1); }, ".*"); -#endif - EXPECT_EQ('c', static_cast<char>(longer_string.GetAt(2))); - EXPECT_EQ('b', static_cast<char>(embedded_nul_string.GetAt(1))); - EXPECT_EQ('\0', static_cast<char>(embedded_nul_string.GetAt(2))); - EXPECT_EQ('c', static_cast<char>(embedded_nul_string.GetAt(3))); -} - -TEST(fxcrt, ByteStringCOperatorSubscript) { +TEST(fxcrt, ByteStringCElementAccess) { // CFX_ByteStringC includes the NUL terminator for non-empty strings. CFX_ByteStringC abc("abc"); #ifndef NDEBUG |